Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / kernel / cgroup.c
blob052c0a2495ccadecf5adb7898c978ea59acb5a30
1 /*
2 * Generic process-grouping system.
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
7 * Copyright notices from the original cpuset code:
8 * --------------------------------------------------
9 * Copyright (C) 2003 BULL SA.
10 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
12 * Portions derived from Patrick Mochel's sysfs code.
13 * sysfs is Copyright (c) 2001-3 Patrick Mochel
15 * 2003-10-10 Written by Simon Derr.
16 * 2003-10-22 Updates by Stephen Hemminger.
17 * 2004 May-July Rework by Paul Jackson.
18 * ---------------------------------------------------
20 * This file is subject to the terms and conditions of the GNU General Public
21 * License. See the file COPYING in the main directory of the Linux
22 * distribution for more details.
25 #include <linux/cgroup.h>
26 #include <linux/errno.h>
27 #include <linux/fs.h>
28 #include <linux/kernel.h>
29 #include <linux/list.h>
30 #include <linux/mm.h>
31 #include <linux/mutex.h>
32 #include <linux/mount.h>
33 #include <linux/pagemap.h>
34 #include <linux/proc_fs.h>
35 #include <linux/rcupdate.h>
36 #include <linux/sched.h>
37 #include <linux/backing-dev.h>
38 #include <linux/seq_file.h>
39 #include <linux/slab.h>
40 #include <linux/magic.h>
41 #include <linux/spinlock.h>
42 #include <linux/string.h>
43 #include <linux/sort.h>
44 #include <linux/kmod.h>
45 #include <linux/delayacct.h>
46 #include <linux/cgroupstats.h>
48 #include <asm/atomic.h>
50 static DEFINE_MUTEX(cgroup_mutex);
52 /* Generate an array of cgroup subsystem pointers */
53 #define SUBSYS(_x) &_x ## _subsys,
55 static struct cgroup_subsys *subsys[] = {
56 #include <linux/cgroup_subsys.h>
60 * A cgroupfs_root represents the root of a cgroup hierarchy,
61 * and may be associated with a superblock to form an active
62 * hierarchy
64 struct cgroupfs_root {
65 struct super_block *sb;
68 * The bitmask of subsystems intended to be attached to this
69 * hierarchy
71 unsigned long subsys_bits;
73 /* The bitmask of subsystems currently attached to this hierarchy */
74 unsigned long actual_subsys_bits;
76 /* A list running through the attached subsystems */
77 struct list_head subsys_list;
79 /* The root cgroup for this hierarchy */
80 struct cgroup top_cgroup;
82 /* Tracks how many cgroups are currently defined in hierarchy.*/
83 int number_of_cgroups;
85 /* A list running through the mounted hierarchies */
86 struct list_head root_list;
88 /* Hierarchy-specific flags */
89 unsigned long flags;
91 /* The path to use for release notifications. No locking
92 * between setting and use - so if userspace updates this
93 * while child cgroups exist, you could miss a
94 * notification. We ensure that it's always a valid
95 * NUL-terminated string */
96 char release_agent_path[PATH_MAX];
101 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
102 * subsystems that are otherwise unattached - it never has more than a
103 * single cgroup, and all tasks are part of that cgroup.
105 static struct cgroupfs_root rootnode;
107 /* The list of hierarchy roots */
109 static LIST_HEAD(roots);
110 static int root_count;
112 /* dummytop is a shorthand for the dummy hierarchy's top cgroup */
113 #define dummytop (&rootnode.top_cgroup)
115 /* This flag indicates whether tasks in the fork and exit paths should
116 <<<<<<< HEAD:kernel/cgroup.c
117 * take callback_mutex and check for fork/exit handlers to call. This
118 * avoids us having to do extra work in the fork/exit path if none of the
119 * subsystems need to be called.
120 =======
121 * check for fork/exit handlers to call. This avoids us having to do
122 * extra work in the fork/exit path if none of the subsystems need to
123 * be called.
124 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
126 static int need_forkexit_callback;
128 /* bits in struct cgroup flags field */
129 enum {
130 /* Control Group is dead */
131 CGRP_REMOVED,
132 /* Control Group has previously had a child cgroup or a task,
133 * but no longer (only if CGRP_NOTIFY_ON_RELEASE is set) */
134 CGRP_RELEASABLE,
135 /* Control Group requires release notifications to userspace */
136 CGRP_NOTIFY_ON_RELEASE,
139 /* convenient tests for these bits */
140 inline int cgroup_is_removed(const struct cgroup *cgrp)
142 return test_bit(CGRP_REMOVED, &cgrp->flags);
145 /* bits in struct cgroupfs_root flags field */
146 enum {
147 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
150 static int cgroup_is_releasable(const struct cgroup *cgrp)
152 const int bits =
153 (1 << CGRP_RELEASABLE) |
154 (1 << CGRP_NOTIFY_ON_RELEASE);
155 return (cgrp->flags & bits) == bits;
158 static int notify_on_release(const struct cgroup *cgrp)
160 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
164 * for_each_subsys() allows you to iterate on each subsystem attached to
165 * an active hierarchy
167 #define for_each_subsys(_root, _ss) \
168 list_for_each_entry(_ss, &_root->subsys_list, sibling)
170 /* for_each_root() allows you to iterate across the active hierarchies */
171 #define for_each_root(_root) \
172 list_for_each_entry(_root, &roots, root_list)
174 /* the list of cgroups eligible for automatic release. Protected by
175 * release_list_lock */
176 static LIST_HEAD(release_list);
177 static DEFINE_SPINLOCK(release_list_lock);
178 static void cgroup_release_agent(struct work_struct *work);
179 static DECLARE_WORK(release_agent_work, cgroup_release_agent);
180 static void check_for_release(struct cgroup *cgrp);
182 /* Link structure for associating css_set objects with cgroups */
183 struct cg_cgroup_link {
185 * List running through cg_cgroup_links associated with a
186 * cgroup, anchored on cgroup->css_sets
188 struct list_head cgrp_link_list;
190 * List running through cg_cgroup_links pointing at a
191 * single css_set object, anchored on css_set->cg_links
193 struct list_head cg_link_list;
194 struct css_set *cg;
197 /* The default css_set - used by init and its children prior to any
198 * hierarchies being mounted. It contains a pointer to the root state
199 * for each subsystem. Also used to anchor the list of css_sets. Not
200 * reference-counted, to improve performance when child cgroups
201 * haven't been created.
204 static struct css_set init_css_set;
205 static struct cg_cgroup_link init_css_set_link;
207 /* css_set_lock protects the list of css_set objects, and the
208 * chain of tasks off each css_set. Nests outside task->alloc_lock
209 * due to cgroup_iter_start() */
210 static DEFINE_RWLOCK(css_set_lock);
211 static int css_set_count;
213 /* We don't maintain the lists running through each css_set to its
214 * task until after the first call to cgroup_iter_start(). This
215 * reduces the fork()/exit() overhead for people who have cgroups
216 * compiled into their kernel but not actually in use */
217 static int use_task_css_set_links;
219 /* When we create or destroy a css_set, the operation simply
220 * takes/releases a reference count on all the cgroups referenced
221 * by subsystems in this css_set. This can end up multiple-counting
222 * some cgroups, but that's OK - the ref-count is just a
223 * busy/not-busy indicator; ensuring that we only count each cgroup
224 * once would require taking a global lock to ensure that no
225 * subsystems moved between hierarchies while we were doing so.
227 * Possible TODO: decide at boot time based on the number of
228 * registered subsystems and the number of CPUs or NUMA nodes whether
229 * it's better for performance to ref-count every subsystem, or to
230 * take a global lock and only add one ref count to each hierarchy.
234 * unlink a css_set from the list and free it
236 static void unlink_css_set(struct css_set *cg)
238 write_lock(&css_set_lock);
239 list_del(&cg->list);
240 css_set_count--;
241 while (!list_empty(&cg->cg_links)) {
242 struct cg_cgroup_link *link;
243 link = list_entry(cg->cg_links.next,
244 struct cg_cgroup_link, cg_link_list);
245 list_del(&link->cg_link_list);
246 list_del(&link->cgrp_link_list);
247 kfree(link);
249 write_unlock(&css_set_lock);
252 static void __release_css_set(struct kref *k, int taskexit)
254 int i;
255 struct css_set *cg = container_of(k, struct css_set, ref);
257 unlink_css_set(cg);
259 rcu_read_lock();
260 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
261 struct cgroup *cgrp = cg->subsys[i]->cgroup;
262 if (atomic_dec_and_test(&cgrp->count) &&
263 notify_on_release(cgrp)) {
264 if (taskexit)
265 set_bit(CGRP_RELEASABLE, &cgrp->flags);
266 check_for_release(cgrp);
269 rcu_read_unlock();
270 kfree(cg);
273 static void release_css_set(struct kref *k)
275 __release_css_set(k, 0);
278 static void release_css_set_taskexit(struct kref *k)
280 __release_css_set(k, 1);
284 * refcounted get/put for css_set objects
286 static inline void get_css_set(struct css_set *cg)
288 kref_get(&cg->ref);
291 static inline void put_css_set(struct css_set *cg)
293 kref_put(&cg->ref, release_css_set);
296 static inline void put_css_set_taskexit(struct css_set *cg)
298 kref_put(&cg->ref, release_css_set_taskexit);
302 * find_existing_css_set() is a helper for
303 * find_css_set(), and checks to see whether an existing
304 * css_set is suitable. This currently walks a linked-list for
305 * simplicity; a later patch will use a hash table for better
306 * performance
308 * oldcg: the cgroup group that we're using before the cgroup
309 * transition
311 * cgrp: the cgroup that we're moving into
313 * template: location in which to build the desired set of subsystem
314 * state objects for the new cgroup group
316 <<<<<<< HEAD:kernel/cgroup.c
318 =======
319 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
320 static struct css_set *find_existing_css_set(
321 struct css_set *oldcg,
322 struct cgroup *cgrp,
323 struct cgroup_subsys_state *template[])
325 int i;
326 struct cgroupfs_root *root = cgrp->root;
327 struct list_head *l = &init_css_set.list;
329 /* Built the set of subsystem state objects that we want to
330 * see in the new css_set */
331 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
332 <<<<<<< HEAD:kernel/cgroup.c
333 if (root->subsys_bits & (1ull << i)) {
334 =======
335 if (root->subsys_bits & (1UL << i)) {
336 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
337 /* Subsystem is in this hierarchy. So we want
338 * the subsystem state from the new
339 * cgroup */
340 template[i] = cgrp->subsys[i];
341 } else {
342 /* Subsystem is not in this hierarchy, so we
343 * don't want to change the subsystem state */
344 template[i] = oldcg->subsys[i];
348 /* Look through existing cgroup groups to find one to reuse */
349 do {
350 struct css_set *cg =
351 list_entry(l, struct css_set, list);
353 if (!memcmp(template, cg->subsys, sizeof(cg->subsys))) {
354 /* All subsystems matched */
355 return cg;
357 /* Try the next cgroup group */
358 l = l->next;
359 } while (l != &init_css_set.list);
361 /* No existing cgroup group matched */
362 return NULL;
366 * allocate_cg_links() allocates "count" cg_cgroup_link structures
367 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
368 * success or a negative error
370 <<<<<<< HEAD:kernel/cgroup.c
372 =======
373 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
374 static int allocate_cg_links(int count, struct list_head *tmp)
376 struct cg_cgroup_link *link;
377 int i;
378 INIT_LIST_HEAD(tmp);
379 for (i = 0; i < count; i++) {
380 link = kmalloc(sizeof(*link), GFP_KERNEL);
381 if (!link) {
382 while (!list_empty(tmp)) {
383 link = list_entry(tmp->next,
384 struct cg_cgroup_link,
385 cgrp_link_list);
386 list_del(&link->cgrp_link_list);
387 kfree(link);
389 return -ENOMEM;
391 list_add(&link->cgrp_link_list, tmp);
393 return 0;
396 static void free_cg_links(struct list_head *tmp)
398 while (!list_empty(tmp)) {
399 struct cg_cgroup_link *link;
400 link = list_entry(tmp->next,
401 struct cg_cgroup_link,
402 cgrp_link_list);
403 list_del(&link->cgrp_link_list);
404 kfree(link);
409 * find_css_set() takes an existing cgroup group and a
410 * cgroup object, and returns a css_set object that's
411 * equivalent to the old group, but with the given cgroup
412 * substituted into the appropriate hierarchy. Must be called with
413 * cgroup_mutex held
415 <<<<<<< HEAD:kernel/cgroup.c
417 =======
418 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
419 static struct css_set *find_css_set(
420 struct css_set *oldcg, struct cgroup *cgrp)
422 struct css_set *res;
423 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
424 int i;
426 struct list_head tmp_cg_links;
427 struct cg_cgroup_link *link;
429 /* First see if we already have a cgroup group that matches
430 * the desired set */
431 write_lock(&css_set_lock);
432 res = find_existing_css_set(oldcg, cgrp, template);
433 if (res)
434 get_css_set(res);
435 write_unlock(&css_set_lock);
437 if (res)
438 return res;
440 res = kmalloc(sizeof(*res), GFP_KERNEL);
441 if (!res)
442 return NULL;
444 /* Allocate all the cg_cgroup_link objects that we'll need */
445 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
446 kfree(res);
447 return NULL;
450 kref_init(&res->ref);
451 INIT_LIST_HEAD(&res->cg_links);
452 INIT_LIST_HEAD(&res->tasks);
454 /* Copy the set of subsystem state objects generated in
455 * find_existing_css_set() */
456 memcpy(res->subsys, template, sizeof(res->subsys));
458 write_lock(&css_set_lock);
459 /* Add reference counts and links from the new css_set. */
460 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
461 struct cgroup *cgrp = res->subsys[i]->cgroup;
462 struct cgroup_subsys *ss = subsys[i];
463 atomic_inc(&cgrp->count);
465 * We want to add a link once per cgroup, so we
466 * only do it for the first subsystem in each
467 * hierarchy
469 if (ss->root->subsys_list.next == &ss->sibling) {
470 BUG_ON(list_empty(&tmp_cg_links));
471 link = list_entry(tmp_cg_links.next,
472 struct cg_cgroup_link,
473 cgrp_link_list);
474 list_del(&link->cgrp_link_list);
475 list_add(&link->cgrp_link_list, &cgrp->css_sets);
476 link->cg = res;
477 list_add(&link->cg_link_list, &res->cg_links);
480 if (list_empty(&rootnode.subsys_list)) {
481 link = list_entry(tmp_cg_links.next,
482 struct cg_cgroup_link,
483 cgrp_link_list);
484 list_del(&link->cgrp_link_list);
485 list_add(&link->cgrp_link_list, &dummytop->css_sets);
486 link->cg = res;
487 list_add(&link->cg_link_list, &res->cg_links);
490 BUG_ON(!list_empty(&tmp_cg_links));
492 /* Link this cgroup group into the list */
493 list_add(&res->list, &init_css_set.list);
494 css_set_count++;
495 <<<<<<< HEAD:kernel/cgroup.c
496 INIT_LIST_HEAD(&res->tasks);
497 =======
498 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
499 write_unlock(&css_set_lock);
501 return res;
505 * There is one global cgroup mutex. We also require taking
506 * task_lock() when dereferencing a task's cgroup subsys pointers.
507 * See "The task_lock() exception", at the end of this comment.
509 * A task must hold cgroup_mutex to modify cgroups.
511 * Any task can increment and decrement the count field without lock.
512 * So in general, code holding cgroup_mutex can't rely on the count
513 * field not changing. However, if the count goes to zero, then only
514 * cgroup_attach_task() can increment it again. Because a count of zero
515 * means that no tasks are currently attached, therefore there is no
516 * way a task attached to that cgroup can fork (the other way to
517 * increment the count). So code holding cgroup_mutex can safely
518 * assume that if the count is zero, it will stay zero. Similarly, if
519 * a task holds cgroup_mutex on a cgroup with zero count, it
520 * knows that the cgroup won't be removed, as cgroup_rmdir()
521 * needs that mutex.
523 * The cgroup_common_file_write handler for operations that modify
524 * the cgroup hierarchy holds cgroup_mutex across the entire operation,
525 * single threading all such cgroup modifications across the system.
527 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
528 * (usually) take cgroup_mutex. These are the two most performance
529 * critical pieces of code here. The exception occurs on cgroup_exit(),
530 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
531 * is taken, and if the cgroup count is zero, a usermode call made
532 <<<<<<< HEAD:kernel/cgroup.c
533 * to /sbin/cgroup_release_agent with the name of the cgroup (path
534 * relative to the root of cgroup file system) as the argument.
535 =======
536 * to the release agent with the name of the cgroup (path relative to
537 * the root of cgroup file system) as the argument.
538 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
540 * A cgroup can only be deleted if both its 'count' of using tasks
541 * is zero, and its list of 'children' cgroups is empty. Since all
542 * tasks in the system use _some_ cgroup, and since there is always at
543 * least one task in the system (init, pid == 1), therefore, top_cgroup
544 * always has either children cgroups and/or using tasks. So we don't
545 * need a special hack to ensure that top_cgroup cannot be deleted.
547 * The task_lock() exception
549 * The need for this exception arises from the action of
550 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
551 <<<<<<< HEAD:kernel/cgroup.c
552 * another. It does so using cgroup_mutexe, however there are
553 =======
554 * another. It does so using cgroup_mutex, however there are
555 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
556 * several performance critical places that need to reference
557 * task->cgroup without the expense of grabbing a system global
558 * mutex. Therefore except as noted below, when dereferencing or, as
559 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
560 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
561 * the task_struct routinely used for such matters.
563 * P.S. One more locking exception. RCU is used to guard the
564 * update of a tasks cgroup pointer by cgroup_attach_task()
568 * cgroup_lock - lock out any changes to cgroup structures
571 <<<<<<< HEAD:kernel/cgroup.c
573 =======
574 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
575 void cgroup_lock(void)
577 mutex_lock(&cgroup_mutex);
581 * cgroup_unlock - release lock on cgroup changes
583 * Undo the lock taken in a previous cgroup_lock() call.
585 <<<<<<< HEAD:kernel/cgroup.c
587 =======
588 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
589 void cgroup_unlock(void)
591 mutex_unlock(&cgroup_mutex);
595 * A couple of forward declarations required, due to cyclic reference loop:
596 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
597 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
598 * -> cgroup_mkdir.
601 static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode);
602 static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
603 static int cgroup_populate_dir(struct cgroup *cgrp);
604 static struct inode_operations cgroup_dir_inode_operations;
605 static struct file_operations proc_cgroupstats_operations;
607 static struct backing_dev_info cgroup_backing_dev_info = {
608 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
611 static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
613 struct inode *inode = new_inode(sb);
615 if (inode) {
616 inode->i_mode = mode;
617 inode->i_uid = current->fsuid;
618 inode->i_gid = current->fsgid;
619 inode->i_blocks = 0;
620 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
621 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
623 return inode;
627 * Call subsys's pre_destroy handler.
628 * This is called before css refcnt check.
630 <<<<<<< HEAD:kernel/cgroup.c
632 =======
633 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
634 static void cgroup_call_pre_destroy(struct cgroup *cgrp)
636 struct cgroup_subsys *ss;
637 for_each_subsys(cgrp->root, ss)
638 if (ss->pre_destroy && cgrp->subsys[ss->subsys_id])
639 ss->pre_destroy(ss, cgrp);
640 return;
643 <<<<<<< HEAD:kernel/cgroup.c
645 =======
646 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
647 static void cgroup_diput(struct dentry *dentry, struct inode *inode)
649 /* is dentry a directory ? if so, kfree() associated cgroup */
650 if (S_ISDIR(inode->i_mode)) {
651 struct cgroup *cgrp = dentry->d_fsdata;
652 struct cgroup_subsys *ss;
653 BUG_ON(!(cgroup_is_removed(cgrp)));
654 /* It's possible for external users to be holding css
655 * reference counts on a cgroup; css_put() needs to
656 * be able to access the cgroup after decrementing
657 * the reference count in order to know if it needs to
658 * queue the cgroup to be handled by the release
659 * agent */
660 synchronize_rcu();
662 mutex_lock(&cgroup_mutex);
664 * Release the subsystem state objects.
666 for_each_subsys(cgrp->root, ss) {
667 if (cgrp->subsys[ss->subsys_id])
668 ss->destroy(ss, cgrp);
671 cgrp->root->number_of_cgroups--;
672 mutex_unlock(&cgroup_mutex);
674 /* Drop the active superblock reference that we took when we
675 * created the cgroup */
676 deactivate_super(cgrp->root->sb);
678 kfree(cgrp);
680 iput(inode);
683 static void remove_dir(struct dentry *d)
685 struct dentry *parent = dget(d->d_parent);
687 d_delete(d);
688 simple_rmdir(parent->d_inode, d);
689 dput(parent);
692 static void cgroup_clear_directory(struct dentry *dentry)
694 struct list_head *node;
696 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
697 spin_lock(&dcache_lock);
698 node = dentry->d_subdirs.next;
699 while (node != &dentry->d_subdirs) {
700 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
701 list_del_init(node);
702 if (d->d_inode) {
703 /* This should never be called on a cgroup
704 * directory with child cgroups */
705 BUG_ON(d->d_inode->i_mode & S_IFDIR);
706 d = dget_locked(d);
707 spin_unlock(&dcache_lock);
708 d_delete(d);
709 simple_unlink(dentry->d_inode, d);
710 dput(d);
711 spin_lock(&dcache_lock);
713 node = dentry->d_subdirs.next;
715 spin_unlock(&dcache_lock);
719 * NOTE : the dentry must have been dget()'ed
721 static void cgroup_d_remove_dir(struct dentry *dentry)
723 cgroup_clear_directory(dentry);
725 spin_lock(&dcache_lock);
726 list_del_init(&dentry->d_u.d_child);
727 spin_unlock(&dcache_lock);
728 remove_dir(dentry);
731 static int rebind_subsystems(struct cgroupfs_root *root,
732 unsigned long final_bits)
734 unsigned long added_bits, removed_bits;
735 struct cgroup *cgrp = &root->top_cgroup;
736 int i;
738 removed_bits = root->actual_subsys_bits & ~final_bits;
739 added_bits = final_bits & ~root->actual_subsys_bits;
740 /* Check that any added subsystems are currently free */
741 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
742 <<<<<<< HEAD:kernel/cgroup.c
743 unsigned long long bit = 1ull << i;
744 =======
745 unsigned long bit = 1UL << i;
746 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
747 struct cgroup_subsys *ss = subsys[i];
748 if (!(bit & added_bits))
749 continue;
750 if (ss->root != &rootnode) {
751 /* Subsystem isn't free */
752 return -EBUSY;
756 /* Currently we don't handle adding/removing subsystems when
757 * any child cgroups exist. This is theoretically supportable
758 * but involves complex error handling, so it's being left until
759 * later */
760 if (!list_empty(&cgrp->children))
761 return -EBUSY;
763 /* Process each subsystem */
764 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
765 struct cgroup_subsys *ss = subsys[i];
766 unsigned long bit = 1UL << i;
767 if (bit & added_bits) {
768 /* We're binding this subsystem to this hierarchy */
769 BUG_ON(cgrp->subsys[i]);
770 BUG_ON(!dummytop->subsys[i]);
771 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
772 cgrp->subsys[i] = dummytop->subsys[i];
773 cgrp->subsys[i]->cgroup = cgrp;
774 list_add(&ss->sibling, &root->subsys_list);
775 rcu_assign_pointer(ss->root, root);
776 if (ss->bind)
777 ss->bind(ss, cgrp);
779 } else if (bit & removed_bits) {
780 /* We're removing this subsystem */
781 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
782 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
783 if (ss->bind)
784 ss->bind(ss, dummytop);
785 dummytop->subsys[i]->cgroup = dummytop;
786 cgrp->subsys[i] = NULL;
787 rcu_assign_pointer(subsys[i]->root, &rootnode);
788 list_del(&ss->sibling);
789 } else if (bit & final_bits) {
790 /* Subsystem state should already exist */
791 BUG_ON(!cgrp->subsys[i]);
792 } else {
793 /* Subsystem state shouldn't exist */
794 BUG_ON(cgrp->subsys[i]);
797 root->subsys_bits = root->actual_subsys_bits = final_bits;
798 synchronize_rcu();
800 return 0;
803 static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs)
805 struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info;
806 struct cgroup_subsys *ss;
808 mutex_lock(&cgroup_mutex);
809 for_each_subsys(root, ss)
810 seq_printf(seq, ",%s", ss->name);
811 if (test_bit(ROOT_NOPREFIX, &root->flags))
812 seq_puts(seq, ",noprefix");
813 if (strlen(root->release_agent_path))
814 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
815 mutex_unlock(&cgroup_mutex);
816 return 0;
819 struct cgroup_sb_opts {
820 unsigned long subsys_bits;
821 unsigned long flags;
822 char *release_agent;
825 /* Convert a hierarchy specifier into a bitmask of subsystems and
826 * flags. */
827 static int parse_cgroupfs_options(char *data,
828 struct cgroup_sb_opts *opts)
830 char *token, *o = data ?: "all";
832 opts->subsys_bits = 0;
833 opts->flags = 0;
834 opts->release_agent = NULL;
836 while ((token = strsep(&o, ",")) != NULL) {
837 if (!*token)
838 return -EINVAL;
839 if (!strcmp(token, "all")) {
840 opts->subsys_bits = (1 << CGROUP_SUBSYS_COUNT) - 1;
841 } else if (!strcmp(token, "noprefix")) {
842 set_bit(ROOT_NOPREFIX, &opts->flags);
843 } else if (!strncmp(token, "release_agent=", 14)) {
844 /* Specifying two release agents is forbidden */
845 if (opts->release_agent)
846 return -EINVAL;
847 opts->release_agent = kzalloc(PATH_MAX, GFP_KERNEL);
848 if (!opts->release_agent)
849 return -ENOMEM;
850 strncpy(opts->release_agent, token + 14, PATH_MAX - 1);
851 opts->release_agent[PATH_MAX - 1] = 0;
852 } else {
853 struct cgroup_subsys *ss;
854 int i;
855 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
856 ss = subsys[i];
857 if (!strcmp(token, ss->name)) {
858 set_bit(i, &opts->subsys_bits);
859 break;
862 if (i == CGROUP_SUBSYS_COUNT)
863 return -ENOENT;
867 /* We can't have an empty hierarchy */
868 if (!opts->subsys_bits)
869 return -EINVAL;
871 return 0;
874 static int cgroup_remount(struct super_block *sb, int *flags, char *data)
876 int ret = 0;
877 struct cgroupfs_root *root = sb->s_fs_info;
878 struct cgroup *cgrp = &root->top_cgroup;
879 struct cgroup_sb_opts opts;
881 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
882 mutex_lock(&cgroup_mutex);
884 /* See what subsystems are wanted */
885 ret = parse_cgroupfs_options(data, &opts);
886 if (ret)
887 goto out_unlock;
889 /* Don't allow flags to change at remount */
890 if (opts.flags != root->flags) {
891 ret = -EINVAL;
892 goto out_unlock;
895 ret = rebind_subsystems(root, opts.subsys_bits);
897 /* (re)populate subsystem files */
898 if (!ret)
899 cgroup_populate_dir(cgrp);
901 if (opts.release_agent)
902 strcpy(root->release_agent_path, opts.release_agent);
903 out_unlock:
904 if (opts.release_agent)
905 kfree(opts.release_agent);
906 mutex_unlock(&cgroup_mutex);
907 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
908 return ret;
911 static struct super_operations cgroup_ops = {
912 .statfs = simple_statfs,
913 .drop_inode = generic_delete_inode,
914 .show_options = cgroup_show_options,
915 .remount_fs = cgroup_remount,
918 static void init_cgroup_root(struct cgroupfs_root *root)
920 struct cgroup *cgrp = &root->top_cgroup;
921 INIT_LIST_HEAD(&root->subsys_list);
922 INIT_LIST_HEAD(&root->root_list);
923 root->number_of_cgroups = 1;
924 cgrp->root = root;
925 cgrp->top_cgroup = cgrp;
926 INIT_LIST_HEAD(&cgrp->sibling);
927 INIT_LIST_HEAD(&cgrp->children);
928 INIT_LIST_HEAD(&cgrp->css_sets);
929 INIT_LIST_HEAD(&cgrp->release_list);
932 static int cgroup_test_super(struct super_block *sb, void *data)
934 struct cgroupfs_root *new = data;
935 struct cgroupfs_root *root = sb->s_fs_info;
937 /* First check subsystems */
938 if (new->subsys_bits != root->subsys_bits)
939 return 0;
941 /* Next check flags */
942 if (new->flags != root->flags)
943 return 0;
945 return 1;
948 static int cgroup_set_super(struct super_block *sb, void *data)
950 int ret;
951 struct cgroupfs_root *root = data;
953 ret = set_anon_super(sb, NULL);
954 if (ret)
955 return ret;
957 sb->s_fs_info = root;
958 root->sb = sb;
960 sb->s_blocksize = PAGE_CACHE_SIZE;
961 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
962 sb->s_magic = CGROUP_SUPER_MAGIC;
963 sb->s_op = &cgroup_ops;
965 return 0;
968 static int cgroup_get_rootdir(struct super_block *sb)
970 struct inode *inode =
971 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
972 struct dentry *dentry;
974 if (!inode)
975 return -ENOMEM;
977 <<<<<<< HEAD:kernel/cgroup.c
978 inode->i_op = &simple_dir_inode_operations;
979 =======
980 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
981 inode->i_fop = &simple_dir_operations;
982 inode->i_op = &cgroup_dir_inode_operations;
983 /* directories start off with i_nlink == 2 (for "." entry) */
984 inc_nlink(inode);
985 dentry = d_alloc_root(inode);
986 if (!dentry) {
987 iput(inode);
988 return -ENOMEM;
990 sb->s_root = dentry;
991 return 0;
994 static int cgroup_get_sb(struct file_system_type *fs_type,
995 int flags, const char *unused_dev_name,
996 void *data, struct vfsmount *mnt)
998 struct cgroup_sb_opts opts;
999 int ret = 0;
1000 struct super_block *sb;
1001 struct cgroupfs_root *root;
1002 struct list_head tmp_cg_links, *l;
1003 INIT_LIST_HEAD(&tmp_cg_links);
1005 /* First find the desired set of subsystems */
1006 ret = parse_cgroupfs_options(data, &opts);
1007 if (ret) {
1008 if (opts.release_agent)
1009 kfree(opts.release_agent);
1010 return ret;
1013 root = kzalloc(sizeof(*root), GFP_KERNEL);
1014 <<<<<<< HEAD:kernel/cgroup.c
1015 if (!root)
1016 =======
1017 if (!root) {
1018 if (opts.release_agent)
1019 kfree(opts.release_agent);
1020 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
1021 return -ENOMEM;
1022 <<<<<<< HEAD:kernel/cgroup.c
1023 =======
1025 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
1027 init_cgroup_root(root);
1028 root->subsys_bits = opts.subsys_bits;
1029 root->flags = opts.flags;
1030 if (opts.release_agent) {
1031 strcpy(root->release_agent_path, opts.release_agent);
1032 kfree(opts.release_agent);
1035 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, root);
1037 if (IS_ERR(sb)) {
1038 kfree(root);
1039 return PTR_ERR(sb);
1042 if (sb->s_fs_info != root) {
1043 /* Reusing an existing superblock */
1044 BUG_ON(sb->s_root == NULL);
1045 kfree(root);
1046 root = NULL;
1047 } else {
1048 /* New superblock */
1049 struct cgroup *cgrp = &root->top_cgroup;
1050 struct inode *inode;
1052 BUG_ON(sb->s_root != NULL);
1054 ret = cgroup_get_rootdir(sb);
1055 if (ret)
1056 goto drop_new_super;
1057 inode = sb->s_root->d_inode;
1059 mutex_lock(&inode->i_mutex);
1060 mutex_lock(&cgroup_mutex);
1063 * We're accessing css_set_count without locking
1064 * css_set_lock here, but that's OK - it can only be
1065 * increased by someone holding cgroup_lock, and
1066 * that's us. The worst that can happen is that we
1067 * have some link structures left over
1069 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
1070 if (ret) {
1071 mutex_unlock(&cgroup_mutex);
1072 mutex_unlock(&inode->i_mutex);
1073 goto drop_new_super;
1076 ret = rebind_subsystems(root, root->subsys_bits);
1077 if (ret == -EBUSY) {
1078 mutex_unlock(&cgroup_mutex);
1079 mutex_unlock(&inode->i_mutex);
1080 goto drop_new_super;
1083 /* EBUSY should be the only error here */
1084 BUG_ON(ret);
1086 list_add(&root->root_list, &roots);
1087 root_count++;
1089 sb->s_root->d_fsdata = &root->top_cgroup;
1090 root->top_cgroup.dentry = sb->s_root;
1092 /* Link the top cgroup in this hierarchy into all
1093 * the css_set objects */
1094 write_lock(&css_set_lock);
1095 l = &init_css_set.list;
1096 do {
1097 struct css_set *cg;
1098 struct cg_cgroup_link *link;
1099 cg = list_entry(l, struct css_set, list);
1100 BUG_ON(list_empty(&tmp_cg_links));
1101 link = list_entry(tmp_cg_links.next,
1102 struct cg_cgroup_link,
1103 cgrp_link_list);
1104 list_del(&link->cgrp_link_list);
1105 link->cg = cg;
1106 list_add(&link->cgrp_link_list,
1107 &root->top_cgroup.css_sets);
1108 list_add(&link->cg_link_list, &cg->cg_links);
1109 l = l->next;
1110 } while (l != &init_css_set.list);
1111 write_unlock(&css_set_lock);
1113 free_cg_links(&tmp_cg_links);
1115 BUG_ON(!list_empty(&cgrp->sibling));
1116 BUG_ON(!list_empty(&cgrp->children));
1117 BUG_ON(root->number_of_cgroups != 1);
1119 cgroup_populate_dir(cgrp);
1120 mutex_unlock(&inode->i_mutex);
1121 mutex_unlock(&cgroup_mutex);
1124 return simple_set_mnt(mnt, sb);
1126 drop_new_super:
1127 up_write(&sb->s_umount);
1128 deactivate_super(sb);
1129 free_cg_links(&tmp_cg_links);
1130 return ret;
1133 static void cgroup_kill_sb(struct super_block *sb) {
1134 struct cgroupfs_root *root = sb->s_fs_info;
1135 struct cgroup *cgrp = &root->top_cgroup;
1136 int ret;
1138 BUG_ON(!root);
1140 BUG_ON(root->number_of_cgroups != 1);
1141 BUG_ON(!list_empty(&cgrp->children));
1142 BUG_ON(!list_empty(&cgrp->sibling));
1144 mutex_lock(&cgroup_mutex);
1146 /* Rebind all subsystems back to the default hierarchy */
1147 ret = rebind_subsystems(root, 0);
1148 /* Shouldn't be able to fail ... */
1149 BUG_ON(ret);
1152 * Release all the links from css_sets to this hierarchy's
1153 * root cgroup
1155 write_lock(&css_set_lock);
1156 while (!list_empty(&cgrp->css_sets)) {
1157 struct cg_cgroup_link *link;
1158 link = list_entry(cgrp->css_sets.next,
1159 struct cg_cgroup_link, cgrp_link_list);
1160 list_del(&link->cg_link_list);
1161 list_del(&link->cgrp_link_list);
1162 kfree(link);
1164 write_unlock(&css_set_lock);
1166 if (!list_empty(&root->root_list)) {
1167 list_del(&root->root_list);
1168 root_count--;
1170 mutex_unlock(&cgroup_mutex);
1172 kfree(root);
1173 kill_litter_super(sb);
1176 static struct file_system_type cgroup_fs_type = {
1177 .name = "cgroup",
1178 .get_sb = cgroup_get_sb,
1179 .kill_sb = cgroup_kill_sb,
1182 static inline struct cgroup *__d_cgrp(struct dentry *dentry)
1184 return dentry->d_fsdata;
1187 static inline struct cftype *__d_cft(struct dentry *dentry)
1189 return dentry->d_fsdata;
1192 <<<<<<< HEAD:kernel/cgroup.c
1194 * Called with cgroup_mutex held. Writes path of cgroup into buf.
1195 =======
1197 * cgroup_path - generate the path of a cgroup
1198 * @cgrp: the cgroup in question
1199 * @buf: the buffer to write the path into
1200 * @buflen: the length of the buffer
1202 * Called with cgroup_mutex held. Writes path of cgroup into buf.
1203 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
1204 * Returns 0 on success, -errno on error.
1206 int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
1208 char *start;
1210 if (cgrp == dummytop) {
1212 * Inactive subsystems have no dentry for their root
1213 * cgroup
1215 strcpy(buf, "/");
1216 return 0;
1219 start = buf + buflen;
1221 *--start = '\0';
1222 for (;;) {
1223 int len = cgrp->dentry->d_name.len;
1224 if ((start -= len) < buf)
1225 return -ENAMETOOLONG;
1226 memcpy(start, cgrp->dentry->d_name.name, len);
1227 cgrp = cgrp->parent;
1228 if (!cgrp)
1229 break;
1230 if (!cgrp->parent)
1231 continue;
1232 if (--start < buf)
1233 return -ENAMETOOLONG;
1234 *start = '/';
1236 memmove(buf, start, buf + buflen - start);
1237 return 0;
1241 * Return the first subsystem attached to a cgroup's hierarchy, and
1242 * its subsystem id.
1245 static void get_first_subsys(const struct cgroup *cgrp,
1246 struct cgroup_subsys_state **css, int *subsys_id)
1248 const struct cgroupfs_root *root = cgrp->root;
1249 const struct cgroup_subsys *test_ss;
1250 BUG_ON(list_empty(&root->subsys_list));
1251 test_ss = list_entry(root->subsys_list.next,
1252 struct cgroup_subsys, sibling);
1253 if (css) {
1254 *css = cgrp->subsys[test_ss->subsys_id];
1255 BUG_ON(!*css);
1257 if (subsys_id)
1258 *subsys_id = test_ss->subsys_id;
1261 <<<<<<< HEAD:kernel/cgroup.c
1263 * Attach task 'tsk' to cgroup 'cgrp'
1264 =======
1266 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1267 * @cgrp: the cgroup the task is attaching to
1268 * @tsk: the task to be attached
1269 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
1271 <<<<<<< HEAD:kernel/cgroup.c
1272 * Call holding cgroup_mutex. May take task_lock of
1273 * the task 'pid' during call.
1274 =======
1275 * Call holding cgroup_mutex. May take task_lock of
1276 * the task 'tsk' during call.
1277 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
1279 int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
1281 int retval = 0;
1282 struct cgroup_subsys *ss;
1283 struct cgroup *oldcgrp;
1284 struct css_set *cg = tsk->cgroups;
1285 struct css_set *newcg;
1286 struct cgroupfs_root *root = cgrp->root;
1287 int subsys_id;
1289 get_first_subsys(cgrp, NULL, &subsys_id);
1291 /* Nothing to do if the task is already in that cgroup */
1292 oldcgrp = task_cgroup(tsk, subsys_id);
1293 if (cgrp == oldcgrp)
1294 return 0;
1296 for_each_subsys(root, ss) {
1297 if (ss->can_attach) {
1298 retval = ss->can_attach(ss, cgrp, tsk);
1299 if (retval)
1300 return retval;
1305 * Locate or allocate a new css_set for this task,
1306 * based on its final set of cgroups
1308 newcg = find_css_set(cg, cgrp);
1309 if (!newcg)
1310 return -ENOMEM;
1312 task_lock(tsk);
1313 if (tsk->flags & PF_EXITING) {
1314 task_unlock(tsk);
1315 put_css_set(newcg);
1316 return -ESRCH;
1318 rcu_assign_pointer(tsk->cgroups, newcg);
1319 task_unlock(tsk);
1321 /* Update the css_set linked lists if we're using them */
1322 write_lock(&css_set_lock);
1323 if (!list_empty(&tsk->cg_list)) {
1324 list_del(&tsk->cg_list);
1325 list_add(&tsk->cg_list, &newcg->tasks);
1327 write_unlock(&css_set_lock);
1329 for_each_subsys(root, ss) {
1330 if (ss->attach)
1331 ss->attach(ss, cgrp, oldcgrp, tsk);
1333 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
1334 synchronize_rcu();
1335 put_css_set(cg);
1336 return 0;
1340 * Attach task with pid 'pid' to cgroup 'cgrp'. Call with
1341 * cgroup_mutex, may take task_lock of task
1343 static int attach_task_by_pid(struct cgroup *cgrp, char *pidbuf)
1345 pid_t pid;
1346 struct task_struct *tsk;
1347 int ret;
1349 if (sscanf(pidbuf, "%d", &pid) != 1)
1350 return -EIO;
1352 if (pid) {
1353 rcu_read_lock();
1354 tsk = find_task_by_vpid(pid);
1355 if (!tsk || tsk->flags & PF_EXITING) {
1356 rcu_read_unlock();
1357 return -ESRCH;
1359 get_task_struct(tsk);
1360 rcu_read_unlock();
1362 if ((current->euid) && (current->euid != tsk->uid)
1363 && (current->euid != tsk->suid)) {
1364 put_task_struct(tsk);
1365 return -EACCES;
1367 } else {
1368 tsk = current;
1369 get_task_struct(tsk);
1372 ret = cgroup_attach_task(cgrp, tsk);
1373 put_task_struct(tsk);
1374 return ret;
1377 /* The various types of files and directories in a cgroup file system */
1378 <<<<<<< HEAD:kernel/cgroup.c
1380 =======
1381 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
1382 enum cgroup_filetype {
1383 FILE_ROOT,
1384 FILE_DIR,
1385 FILE_TASKLIST,
1386 FILE_NOTIFY_ON_RELEASE,
1387 FILE_RELEASABLE,
1388 FILE_RELEASE_AGENT,
1391 static ssize_t cgroup_write_uint(struct cgroup *cgrp, struct cftype *cft,
1392 struct file *file,
1393 const char __user *userbuf,
1394 size_t nbytes, loff_t *unused_ppos)
1396 char buffer[64];
1397 int retval = 0;
1398 u64 val;
1399 char *end;
1401 if (!nbytes)
1402 return -EINVAL;
1403 if (nbytes >= sizeof(buffer))
1404 return -E2BIG;
1405 if (copy_from_user(buffer, userbuf, nbytes))
1406 return -EFAULT;
1408 buffer[nbytes] = 0; /* nul-terminate */
1410 /* strip newline if necessary */
1411 if (nbytes && (buffer[nbytes-1] == '\n'))
1412 buffer[nbytes-1] = 0;
1413 val = simple_strtoull(buffer, &end, 0);
1414 if (*end)
1415 return -EINVAL;
1417 /* Pass to subsystem */
1418 retval = cft->write_uint(cgrp, cft, val);
1419 if (!retval)
1420 retval = nbytes;
1421 return retval;
1424 static ssize_t cgroup_common_file_write(struct cgroup *cgrp,
1425 struct cftype *cft,
1426 struct file *file,
1427 const char __user *userbuf,
1428 size_t nbytes, loff_t *unused_ppos)
1430 enum cgroup_filetype type = cft->private;
1431 char *buffer;
1432 int retval = 0;
1434 if (nbytes >= PATH_MAX)
1435 return -E2BIG;
1437 /* +1 for nul-terminator */
1438 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
1439 if (buffer == NULL)
1440 return -ENOMEM;
1442 if (copy_from_user(buffer, userbuf, nbytes)) {
1443 retval = -EFAULT;
1444 goto out1;
1446 buffer[nbytes] = 0; /* nul-terminate */
1447 strstrip(buffer); /* strip -just- trailing whitespace */
1449 mutex_lock(&cgroup_mutex);
1452 * This was already checked for in cgroup_file_write(), but
1453 * check again now we're holding cgroup_mutex.
1455 if (cgroup_is_removed(cgrp)) {
1456 retval = -ENODEV;
1457 goto out2;
1460 switch (type) {
1461 case FILE_TASKLIST:
1462 retval = attach_task_by_pid(cgrp, buffer);
1463 break;
1464 case FILE_NOTIFY_ON_RELEASE:
1465 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
1466 if (simple_strtoul(buffer, NULL, 10) != 0)
1467 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
1468 else
1469 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
1470 break;
1471 case FILE_RELEASE_AGENT:
1472 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
1473 strcpy(cgrp->root->release_agent_path, buffer);
1474 break;
1475 default:
1476 retval = -EINVAL;
1477 goto out2;
1480 if (retval == 0)
1481 retval = nbytes;
1482 out2:
1483 mutex_unlock(&cgroup_mutex);
1484 out1:
1485 kfree(buffer);
1486 return retval;
1489 static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
1490 size_t nbytes, loff_t *ppos)
1492 struct cftype *cft = __d_cft(file->f_dentry);
1493 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
1495 if (!cft || cgroup_is_removed(cgrp))
1496 return -ENODEV;
1497 if (cft->write)
1498 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
1499 if (cft->write_uint)
1500 return cgroup_write_uint(cgrp, cft, file, buf, nbytes, ppos);
1501 return -EINVAL;
1504 static ssize_t cgroup_read_uint(struct cgroup *cgrp, struct cftype *cft,
1505 struct file *file,
1506 char __user *buf, size_t nbytes,
1507 loff_t *ppos)
1509 char tmp[64];
1510 u64 val = cft->read_uint(cgrp, cft);
1511 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
1513 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1516 static ssize_t cgroup_common_file_read(struct cgroup *cgrp,
1517 struct cftype *cft,
1518 struct file *file,
1519 char __user *buf,
1520 size_t nbytes, loff_t *ppos)
1522 enum cgroup_filetype type = cft->private;
1523 char *page;
1524 ssize_t retval = 0;
1525 char *s;
1527 if (!(page = (char *)__get_free_page(GFP_KERNEL)))
1528 return -ENOMEM;
1530 s = page;
1532 switch (type) {
1533 case FILE_RELEASE_AGENT:
1535 struct cgroupfs_root *root;
1536 size_t n;
1537 mutex_lock(&cgroup_mutex);
1538 root = cgrp->root;
1539 n = strnlen(root->release_agent_path,
1540 sizeof(root->release_agent_path));
1541 n = min(n, (size_t) PAGE_SIZE);
1542 strncpy(s, root->release_agent_path, n);
1543 mutex_unlock(&cgroup_mutex);
1544 s += n;
1545 break;
1547 default:
1548 retval = -EINVAL;
1549 goto out;
1551 *s++ = '\n';
1553 retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page);
1554 out:
1555 free_page((unsigned long)page);
1556 return retval;
1559 static ssize_t cgroup_file_read(struct file *file, char __user *buf,
1560 size_t nbytes, loff_t *ppos)
1562 struct cftype *cft = __d_cft(file->f_dentry);
1563 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
1565 if (!cft || cgroup_is_removed(cgrp))
1566 return -ENODEV;
1568 if (cft->read)
1569 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
1570 if (cft->read_uint)
1571 return cgroup_read_uint(cgrp, cft, file, buf, nbytes, ppos);
1572 return -EINVAL;
1575 static int cgroup_file_open(struct inode *inode, struct file *file)
1577 int err;
1578 struct cftype *cft;
1580 err = generic_file_open(inode, file);
1581 if (err)
1582 return err;
1584 cft = __d_cft(file->f_dentry);
1585 if (!cft)
1586 return -ENODEV;
1587 if (cft->open)
1588 err = cft->open(inode, file);
1589 else
1590 err = 0;
1592 return err;
1595 static int cgroup_file_release(struct inode *inode, struct file *file)
1597 struct cftype *cft = __d_cft(file->f_dentry);
1598 if (cft->release)
1599 return cft->release(inode, file);
1600 return 0;
1604 * cgroup_rename - Only allow simple rename of directories in place.
1606 static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
1607 struct inode *new_dir, struct dentry *new_dentry)
1609 if (!S_ISDIR(old_dentry->d_inode->i_mode))
1610 return -ENOTDIR;
1611 if (new_dentry->d_inode)
1612 return -EEXIST;
1613 if (old_dir != new_dir)
1614 return -EIO;
1615 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
1618 static struct file_operations cgroup_file_operations = {
1619 .read = cgroup_file_read,
1620 .write = cgroup_file_write,
1621 .llseek = generic_file_llseek,
1622 .open = cgroup_file_open,
1623 .release = cgroup_file_release,
1626 static struct inode_operations cgroup_dir_inode_operations = {
1627 .lookup = simple_lookup,
1628 .mkdir = cgroup_mkdir,
1629 .rmdir = cgroup_rmdir,
1630 .rename = cgroup_rename,
1633 static int cgroup_create_file(struct dentry *dentry, int mode,
1634 struct super_block *sb)
1636 static struct dentry_operations cgroup_dops = {
1637 .d_iput = cgroup_diput,
1640 struct inode *inode;
1642 if (!dentry)
1643 return -ENOENT;
1644 if (dentry->d_inode)
1645 return -EEXIST;
1647 inode = cgroup_new_inode(mode, sb);
1648 if (!inode)
1649 return -ENOMEM;
1651 if (S_ISDIR(mode)) {
1652 inode->i_op = &cgroup_dir_inode_operations;
1653 inode->i_fop = &simple_dir_operations;
1655 /* start off with i_nlink == 2 (for "." entry) */
1656 inc_nlink(inode);
1658 /* start with the directory inode held, so that we can
1659 * populate it without racing with another mkdir */
1660 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
1661 } else if (S_ISREG(mode)) {
1662 inode->i_size = 0;
1663 inode->i_fop = &cgroup_file_operations;
1665 dentry->d_op = &cgroup_dops;
1666 d_instantiate(dentry, inode);
1667 dget(dentry); /* Extra count - pin the dentry in core */
1668 return 0;
1672 <<<<<<< HEAD:kernel/cgroup.c
1673 * cgroup_create_dir - create a directory for an object.
1674 * cgrp: the cgroup we create the directory for.
1675 * It must have a valid ->parent field
1676 * And we are going to fill its ->dentry field.
1677 * dentry: dentry of the new cgroup
1678 * mode: mode to set on new directory.
1679 =======
1680 * cgroup_create_dir - create a directory for an object.
1681 * @cgrp: the cgroup we create the directory for. It must have a valid
1682 * ->parent field. And we are going to fill its ->dentry field.
1683 * @dentry: dentry of the new cgroup
1684 * @mode: mode to set on new directory.
1685 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
1687 static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
1688 int mode)
1690 struct dentry *parent;
1691 int error = 0;
1693 parent = cgrp->parent->dentry;
1694 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
1695 if (!error) {
1696 dentry->d_fsdata = cgrp;
1697 inc_nlink(parent->d_inode);
1698 cgrp->dentry = dentry;
1699 dget(dentry);
1701 dput(dentry);
1703 return error;
1706 int cgroup_add_file(struct cgroup *cgrp,
1707 struct cgroup_subsys *subsys,
1708 const struct cftype *cft)
1710 struct dentry *dir = cgrp->dentry;
1711 struct dentry *dentry;
1712 int error;
1714 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
1715 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
1716 strcpy(name, subsys->name);
1717 strcat(name, ".");
1719 strcat(name, cft->name);
1720 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
1721 dentry = lookup_one_len(name, dir, strlen(name));
1722 if (!IS_ERR(dentry)) {
1723 error = cgroup_create_file(dentry, 0644 | S_IFREG,
1724 cgrp->root->sb);
1725 if (!error)
1726 dentry->d_fsdata = (void *)cft;
1727 dput(dentry);
1728 } else
1729 error = PTR_ERR(dentry);
1730 return error;
1733 int cgroup_add_files(struct cgroup *cgrp,
1734 struct cgroup_subsys *subsys,
1735 const struct cftype cft[],
1736 int count)
1738 int i, err;
1739 for (i = 0; i < count; i++) {
1740 err = cgroup_add_file(cgrp, subsys, &cft[i]);
1741 if (err)
1742 return err;
1744 return 0;
1747 <<<<<<< HEAD:kernel/cgroup.c
1748 /* Count the number of tasks in a cgroup. */
1750 =======
1752 * cgroup_task_count - count the number of tasks in a cgroup.
1753 * @cgrp: the cgroup in question
1755 * Return the number of tasks in the cgroup.
1757 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
1758 int cgroup_task_count(const struct cgroup *cgrp)
1760 int count = 0;
1761 struct list_head *l;
1763 read_lock(&css_set_lock);
1764 l = cgrp->css_sets.next;
1765 while (l != &cgrp->css_sets) {
1766 struct cg_cgroup_link *link =
1767 list_entry(l, struct cg_cgroup_link, cgrp_link_list);
1768 count += atomic_read(&link->cg->ref.refcount);
1769 l = l->next;
1771 read_unlock(&css_set_lock);
1772 return count;
1776 * Advance a list_head iterator. The iterator should be positioned at
1777 * the start of a css_set
1779 static void cgroup_advance_iter(struct cgroup *cgrp,
1780 struct cgroup_iter *it)
1782 struct list_head *l = it->cg_link;
1783 struct cg_cgroup_link *link;
1784 struct css_set *cg;
1786 /* Advance to the next non-empty css_set */
1787 do {
1788 l = l->next;
1789 if (l == &cgrp->css_sets) {
1790 it->cg_link = NULL;
1791 return;
1793 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
1794 cg = link->cg;
1795 } while (list_empty(&cg->tasks));
1796 it->cg_link = l;
1797 it->task = cg->tasks.next;
1801 * To reduce the fork() overhead for systems that are not actually
1802 * using their cgroups capability, we don't maintain the lists running
1803 * through each css_set to its tasks until we see the list actually
1804 * used - in other words after the first call to cgroup_iter_start().
1806 * The tasklist_lock is not held here, as do_each_thread() and
1807 * while_each_thread() are protected by RCU.
1809 void cgroup_enable_task_cg_lists(void)
1811 struct task_struct *p, *g;
1812 write_lock(&css_set_lock);
1813 use_task_css_set_links = 1;
1814 do_each_thread(g, p) {
1815 task_lock(p);
1816 if (list_empty(&p->cg_list))
1817 list_add(&p->cg_list, &p->cgroups->tasks);
1818 task_unlock(p);
1819 } while_each_thread(g, p);
1820 write_unlock(&css_set_lock);
1823 void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
1826 * The first time anyone tries to iterate across a cgroup,
1827 * we need to enable the list linking each css_set to its
1828 * tasks, and fix up all existing tasks.
1830 if (!use_task_css_set_links)
1831 cgroup_enable_task_cg_lists();
1833 read_lock(&css_set_lock);
1834 it->cg_link = &cgrp->css_sets;
1835 cgroup_advance_iter(cgrp, it);
1838 struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
1839 struct cgroup_iter *it)
1841 struct task_struct *res;
1842 struct list_head *l = it->task;
1844 /* If the iterator cg is NULL, we have no tasks */
1845 if (!it->cg_link)
1846 return NULL;
1847 res = list_entry(l, struct task_struct, cg_list);
1848 /* Advance iterator to find next entry */
1849 l = l->next;
1850 if (l == &res->cgroups->tasks) {
1851 /* We reached the end of this task list - move on to
1852 * the next cg_cgroup_link */
1853 cgroup_advance_iter(cgrp, it);
1854 } else {
1855 it->task = l;
1857 return res;
1860 void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
1862 read_unlock(&css_set_lock);
1865 static inline int started_after_time(struct task_struct *t1,
1866 struct timespec *time,
1867 struct task_struct *t2)
1869 int start_diff = timespec_compare(&t1->start_time, time);
1870 if (start_diff > 0) {
1871 return 1;
1872 } else if (start_diff < 0) {
1873 return 0;
1874 } else {
1876 * Arbitrarily, if two processes started at the same
1877 * time, we'll say that the lower pointer value
1878 * started first. Note that t2 may have exited by now
1879 * so this may not be a valid pointer any longer, but
1880 * that's fine - it still serves to distinguish
1881 * between two tasks started (effectively) simultaneously.
1883 return t1 > t2;
1888 * This function is a callback from heap_insert() and is used to order
1889 * the heap.
1890 * In this case we order the heap in descending task start time.
1892 static inline int started_after(void *p1, void *p2)
1894 struct task_struct *t1 = p1;
1895 struct task_struct *t2 = p2;
1896 return started_after_time(t1, &t2->start_time, t2);
1900 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
1901 * @scan: struct cgroup_scanner containing arguments for the scan
1903 * Arguments include pointers to callback functions test_task() and
1904 * process_task().
1905 * Iterate through all the tasks in a cgroup, calling test_task() for each,
1906 * and if it returns true, call process_task() for it also.
1907 * The test_task pointer may be NULL, meaning always true (select all tasks).
1908 * Effectively duplicates cgroup_iter_{start,next,end}()
1909 * but does not lock css_set_lock for the call to process_task().
1910 * The struct cgroup_scanner may be embedded in any structure of the caller's
1911 * creation.
1912 * It is guaranteed that process_task() will act on every task that
1913 * is a member of the cgroup for the duration of this call. This
1914 * function may or may not call process_task() for tasks that exit
1915 * or move to a different cgroup during the call, or are forked or
1916 * move into the cgroup during the call.
1918 * Note that test_task() may be called with locks held, and may in some
1919 * situations be called multiple times for the same task, so it should
1920 * be cheap.
1921 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
1922 * pre-allocated and will be used for heap operations (and its "gt" member will
1923 * be overwritten), else a temporary heap will be used (allocation of which
1924 * may cause this function to fail).
1926 int cgroup_scan_tasks(struct cgroup_scanner *scan)
1928 int retval, i;
1929 struct cgroup_iter it;
1930 struct task_struct *p, *dropped;
1931 /* Never dereference latest_task, since it's not refcounted */
1932 struct task_struct *latest_task = NULL;
1933 struct ptr_heap tmp_heap;
1934 struct ptr_heap *heap;
1935 struct timespec latest_time = { 0, 0 };
1937 if (scan->heap) {
1938 /* The caller supplied our heap and pre-allocated its memory */
1939 heap = scan->heap;
1940 heap->gt = &started_after;
1941 } else {
1942 /* We need to allocate our own heap memory */
1943 heap = &tmp_heap;
1944 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
1945 if (retval)
1946 /* cannot allocate the heap */
1947 return retval;
1950 again:
1952 * Scan tasks in the cgroup, using the scanner's "test_task" callback
1953 * to determine which are of interest, and using the scanner's
1954 * "process_task" callback to process any of them that need an update.
1955 * Since we don't want to hold any locks during the task updates,
1956 * gather tasks to be processed in a heap structure.
1957 * The heap is sorted by descending task start time.
1958 * If the statically-sized heap fills up, we overflow tasks that
1959 * started later, and in future iterations only consider tasks that
1960 * started after the latest task in the previous pass. This
1961 * guarantees forward progress and that we don't miss any tasks.
1963 heap->size = 0;
1964 cgroup_iter_start(scan->cg, &it);
1965 while ((p = cgroup_iter_next(scan->cg, &it))) {
1967 * Only affect tasks that qualify per the caller's callback,
1968 * if he provided one
1970 if (scan->test_task && !scan->test_task(p, scan))
1971 continue;
1973 * Only process tasks that started after the last task
1974 * we processed
1976 if (!started_after_time(p, &latest_time, latest_task))
1977 continue;
1978 dropped = heap_insert(heap, p);
1979 if (dropped == NULL) {
1981 * The new task was inserted; the heap wasn't
1982 * previously full
1984 get_task_struct(p);
1985 } else if (dropped != p) {
1987 * The new task was inserted, and pushed out a
1988 * different task
1990 get_task_struct(p);
1991 put_task_struct(dropped);
1994 * Else the new task was newer than anything already in
1995 * the heap and wasn't inserted
1998 cgroup_iter_end(scan->cg, &it);
2000 if (heap->size) {
2001 for (i = 0; i < heap->size; i++) {
2002 struct task_struct *p = heap->ptrs[i];
2003 if (i == 0) {
2004 latest_time = p->start_time;
2005 latest_task = p;
2007 /* Process the task per the caller's callback */
2008 scan->process_task(p, scan);
2009 put_task_struct(p);
2012 * If we had to process any tasks at all, scan again
2013 * in case some of them were in the middle of forking
2014 * children that didn't get processed.
2015 * Not the most efficient way to do it, but it avoids
2016 * having to take callback_mutex in the fork path
2018 goto again;
2020 if (heap == &tmp_heap)
2021 heap_free(&tmp_heap);
2022 return 0;
2026 * Stuff for reading the 'tasks' file.
2028 * Reading this file can return large amounts of data if a cgroup has
2029 * *lots* of attached tasks. So it may need several calls to read(),
2030 * but we cannot guarantee that the information we produce is correct
2031 * unless we produce it entirely atomically.
2033 * Upon tasks file open(), a struct ctr_struct is allocated, that
2034 * will have a pointer to an array (also allocated here). The struct
2035 * ctr_struct * is stored in file->private_data. Its resources will
2036 * be freed by release() when the file is closed. The array is used
2037 * to sprintf the PIDs and then used by read().
2039 struct ctr_struct {
2040 char *buf;
2041 int bufsz;
2045 * Load into 'pidarray' up to 'npids' of the tasks using cgroup
2046 * 'cgrp'. Return actual number of pids loaded. No need to
2047 * task_lock(p) when reading out p->cgroup, since we're in an RCU
2048 * read section, so the css_set can't go away, and is
2049 * immutable after creation.
2051 static int pid_array_load(pid_t *pidarray, int npids, struct cgroup *cgrp)
2053 int n = 0;
2054 struct cgroup_iter it;
2055 struct task_struct *tsk;
2056 cgroup_iter_start(cgrp, &it);
2057 while ((tsk = cgroup_iter_next(cgrp, &it))) {
2058 if (unlikely(n == npids))
2059 break;
2060 pidarray[n++] = task_pid_vnr(tsk);
2062 cgroup_iter_end(cgrp, &it);
2063 return n;
2067 <<<<<<< HEAD:kernel/cgroup.c
2068 * Build and fill cgroupstats so that taskstats can export it to user
2069 * space.
2071 =======
2072 * cgroupstats_build - build and fill cgroupstats
2073 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
2074 * @stats: cgroupstats to fill information into
2075 * @dentry: A dentry entry belonging to the cgroup for which stats have
2076 * been requested.
2077 <<<<<<< HEAD:kernel/cgroup.c
2078 =======
2080 * Build and fill cgroupstats so that taskstats can export it to user
2081 * space.
2082 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
2084 int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
2086 int ret = -EINVAL;
2087 struct cgroup *cgrp;
2088 struct cgroup_iter it;
2089 struct task_struct *tsk;
2091 * Validate dentry by checking the superblock operations
2093 if (dentry->d_sb->s_op != &cgroup_ops)
2094 goto err;
2096 ret = 0;
2097 cgrp = dentry->d_fsdata;
2098 rcu_read_lock();
2100 cgroup_iter_start(cgrp, &it);
2101 while ((tsk = cgroup_iter_next(cgrp, &it))) {
2102 switch (tsk->state) {
2103 case TASK_RUNNING:
2104 stats->nr_running++;
2105 break;
2106 case TASK_INTERRUPTIBLE:
2107 stats->nr_sleeping++;
2108 break;
2109 case TASK_UNINTERRUPTIBLE:
2110 stats->nr_uninterruptible++;
2111 break;
2112 case TASK_STOPPED:
2113 stats->nr_stopped++;
2114 break;
2115 default:
2116 if (delayacct_is_task_waiting_on_io(tsk))
2117 stats->nr_io_wait++;
2118 break;
2121 cgroup_iter_end(cgrp, &it);
2123 rcu_read_unlock();
2124 err:
2125 return ret;
2128 static int cmppid(const void *a, const void *b)
2130 return *(pid_t *)a - *(pid_t *)b;
2134 * Convert array 'a' of 'npids' pid_t's to a string of newline separated
2135 * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
2136 * count 'cnt' of how many chars would be written if buf were large enough.
2138 static int pid_array_to_buf(char *buf, int sz, pid_t *a, int npids)
2140 int cnt = 0;
2141 int i;
2143 for (i = 0; i < npids; i++)
2144 cnt += snprintf(buf + cnt, max(sz - cnt, 0), "%d\n", a[i]);
2145 return cnt;
2149 * Handle an open on 'tasks' file. Prepare a buffer listing the
2150 * process id's of tasks currently attached to the cgroup being opened.
2152 * Does not require any specific cgroup mutexes, and does not take any.
2154 static int cgroup_tasks_open(struct inode *unused, struct file *file)
2156 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
2157 struct ctr_struct *ctr;
2158 pid_t *pidarray;
2159 int npids;
2160 char c;
2162 if (!(file->f_mode & FMODE_READ))
2163 return 0;
2165 ctr = kmalloc(sizeof(*ctr), GFP_KERNEL);
2166 if (!ctr)
2167 goto err0;
2170 * If cgroup gets more users after we read count, we won't have
2171 * enough space - tough. This race is indistinguishable to the
2172 * caller from the case that the additional cgroup users didn't
2173 * show up until sometime later on.
2175 npids = cgroup_task_count(cgrp);
2176 if (npids) {
2177 pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
2178 if (!pidarray)
2179 goto err1;
2181 npids = pid_array_load(pidarray, npids, cgrp);
2182 sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
2184 /* Call pid_array_to_buf() twice, first just to get bufsz */
2185 ctr->bufsz = pid_array_to_buf(&c, sizeof(c), pidarray, npids) + 1;
2186 ctr->buf = kmalloc(ctr->bufsz, GFP_KERNEL);
2187 if (!ctr->buf)
2188 goto err2;
2189 ctr->bufsz = pid_array_to_buf(ctr->buf, ctr->bufsz, pidarray, npids);
2191 kfree(pidarray);
2192 } else {
2193 ctr->buf = 0;
2194 ctr->bufsz = 0;
2196 file->private_data = ctr;
2197 return 0;
2199 err2:
2200 kfree(pidarray);
2201 err1:
2202 kfree(ctr);
2203 err0:
2204 return -ENOMEM;
2207 static ssize_t cgroup_tasks_read(struct cgroup *cgrp,
2208 struct cftype *cft,
2209 struct file *file, char __user *buf,
2210 size_t nbytes, loff_t *ppos)
2212 struct ctr_struct *ctr = file->private_data;
2214 return simple_read_from_buffer(buf, nbytes, ppos, ctr->buf, ctr->bufsz);
2217 static int cgroup_tasks_release(struct inode *unused_inode,
2218 struct file *file)
2220 struct ctr_struct *ctr;
2222 if (file->f_mode & FMODE_READ) {
2223 ctr = file->private_data;
2224 kfree(ctr->buf);
2225 kfree(ctr);
2227 return 0;
2230 static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
2231 struct cftype *cft)
2233 return notify_on_release(cgrp);
2236 static u64 cgroup_read_releasable(struct cgroup *cgrp, struct cftype *cft)
2238 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
2242 * for the common functions, 'private' gives the type of file
2244 static struct cftype files[] = {
2246 .name = "tasks",
2247 .open = cgroup_tasks_open,
2248 .read = cgroup_tasks_read,
2249 .write = cgroup_common_file_write,
2250 .release = cgroup_tasks_release,
2251 .private = FILE_TASKLIST,
2255 .name = "notify_on_release",
2256 .read_uint = cgroup_read_notify_on_release,
2257 .write = cgroup_common_file_write,
2258 .private = FILE_NOTIFY_ON_RELEASE,
2262 .name = "releasable",
2263 .read_uint = cgroup_read_releasable,
2264 .private = FILE_RELEASABLE,
2268 static struct cftype cft_release_agent = {
2269 .name = "release_agent",
2270 .read = cgroup_common_file_read,
2271 .write = cgroup_common_file_write,
2272 .private = FILE_RELEASE_AGENT,
2275 static int cgroup_populate_dir(struct cgroup *cgrp)
2277 int err;
2278 struct cgroup_subsys *ss;
2280 /* First clear out any existing files */
2281 cgroup_clear_directory(cgrp->dentry);
2283 err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
2284 if (err < 0)
2285 return err;
2287 if (cgrp == cgrp->top_cgroup) {
2288 if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
2289 return err;
2292 for_each_subsys(cgrp->root, ss) {
2293 if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
2294 return err;
2297 return 0;
2300 static void init_cgroup_css(struct cgroup_subsys_state *css,
2301 struct cgroup_subsys *ss,
2302 struct cgroup *cgrp)
2304 css->cgroup = cgrp;
2305 atomic_set(&css->refcnt, 0);
2306 css->flags = 0;
2307 if (cgrp == dummytop)
2308 set_bit(CSS_ROOT, &css->flags);
2309 BUG_ON(cgrp->subsys[ss->subsys_id]);
2310 cgrp->subsys[ss->subsys_id] = css;
2314 <<<<<<< HEAD:kernel/cgroup.c
2315 * cgroup_create - create a cgroup
2316 * parent: cgroup that will be parent of the new cgroup.
2317 * name: name of the new cgroup. Will be strcpy'ed.
2318 * mode: mode to set on new inode
2319 =======
2320 * cgroup_create - create a cgroup
2321 * @parent: cgroup that will be parent of the new cgroup
2322 * @dentry: dentry of the new cgroup
2323 * @mode: mode to set on new inode
2324 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
2326 <<<<<<< HEAD:kernel/cgroup.c
2327 * Must be called with the mutex on the parent inode held
2328 =======
2329 * Must be called with the mutex on the parent inode held
2330 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
2332 <<<<<<< HEAD:kernel/cgroup.c
2334 =======
2335 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
2336 static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
2337 int mode)
2339 struct cgroup *cgrp;
2340 struct cgroupfs_root *root = parent->root;
2341 int err = 0;
2342 struct cgroup_subsys *ss;
2343 struct super_block *sb = root->sb;
2345 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
2346 if (!cgrp)
2347 return -ENOMEM;
2349 /* Grab a reference on the superblock so the hierarchy doesn't
2350 * get deleted on unmount if there are child cgroups. This
2351 * can be done outside cgroup_mutex, since the sb can't
2352 * disappear while someone has an open control file on the
2353 * fs */
2354 atomic_inc(&sb->s_active);
2356 mutex_lock(&cgroup_mutex);
2358 <<<<<<< HEAD:kernel/cgroup.c
2359 cgrp->flags = 0;
2360 =======
2361 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
2362 INIT_LIST_HEAD(&cgrp->sibling);
2363 INIT_LIST_HEAD(&cgrp->children);
2364 INIT_LIST_HEAD(&cgrp->css_sets);
2365 INIT_LIST_HEAD(&cgrp->release_list);
2367 cgrp->parent = parent;
2368 cgrp->root = parent->root;
2369 cgrp->top_cgroup = parent->top_cgroup;
2371 <<<<<<< HEAD:kernel/cgroup.c
2372 =======
2373 if (notify_on_release(parent))
2374 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2376 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
2377 for_each_subsys(root, ss) {
2378 struct cgroup_subsys_state *css = ss->create(ss, cgrp);
2379 if (IS_ERR(css)) {
2380 err = PTR_ERR(css);
2381 goto err_destroy;
2383 init_cgroup_css(css, ss, cgrp);
2386 list_add(&cgrp->sibling, &cgrp->parent->children);
2387 root->number_of_cgroups++;
2389 err = cgroup_create_dir(cgrp, dentry, mode);
2390 if (err < 0)
2391 goto err_remove;
2393 /* The cgroup directory was pre-locked for us */
2394 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
2396 err = cgroup_populate_dir(cgrp);
2397 /* If err < 0, we have a half-filled directory - oh well ;) */
2399 mutex_unlock(&cgroup_mutex);
2400 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
2402 return 0;
2404 err_remove:
2406 list_del(&cgrp->sibling);
2407 root->number_of_cgroups--;
2409 err_destroy:
2411 for_each_subsys(root, ss) {
2412 if (cgrp->subsys[ss->subsys_id])
2413 ss->destroy(ss, cgrp);
2416 mutex_unlock(&cgroup_mutex);
2418 /* Release the reference count that we took on the superblock */
2419 deactivate_super(sb);
2421 kfree(cgrp);
2422 return err;
2425 static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2427 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
2429 /* the vfs holds inode->i_mutex already */
2430 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
2433 static inline int cgroup_has_css_refs(struct cgroup *cgrp)
2435 /* Check the reference count on each subsystem. Since we
2436 * already established that there are no tasks in the
2437 * cgroup, if the css refcount is also 0, then there should
2438 * be no outstanding references, so the subsystem is safe to
2439 * destroy. We scan across all subsystems rather than using
2440 * the per-hierarchy linked list of mounted subsystems since
2441 * we can be called via check_for_release() with no
2442 * synchronization other than RCU, and the subsystem linked
2443 * list isn't RCU-safe */
2444 int i;
2445 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2446 struct cgroup_subsys *ss = subsys[i];
2447 struct cgroup_subsys_state *css;
2448 /* Skip subsystems not in this hierarchy */
2449 if (ss->root != cgrp->root)
2450 continue;
2451 css = cgrp->subsys[ss->subsys_id];
2452 /* When called from check_for_release() it's possible
2453 * that by this point the cgroup has been removed
2454 * and the css deleted. But a false-positive doesn't
2455 * matter, since it can only happen if the cgroup
2456 * has been deleted and hence no longer needs the
2457 * release agent to be called anyway. */
2458 if (css && atomic_read(&css->refcnt))
2459 return 1;
2461 return 0;
2464 static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
2466 struct cgroup *cgrp = dentry->d_fsdata;
2467 struct dentry *d;
2468 struct cgroup *parent;
2469 struct super_block *sb;
2470 struct cgroupfs_root *root;
2472 /* the vfs holds both inode->i_mutex already */
2474 mutex_lock(&cgroup_mutex);
2475 if (atomic_read(&cgrp->count) != 0) {
2476 mutex_unlock(&cgroup_mutex);
2477 return -EBUSY;
2479 if (!list_empty(&cgrp->children)) {
2480 mutex_unlock(&cgroup_mutex);
2481 return -EBUSY;
2484 parent = cgrp->parent;
2485 root = cgrp->root;
2486 sb = root->sb;
2487 <<<<<<< HEAD:kernel/cgroup.c
2488 =======
2490 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
2492 <<<<<<< HEAD:kernel/cgroup.c
2493 * Call pre_destroy handlers of subsys
2494 =======
2495 * Call pre_destroy handlers of subsys. Notify subsystems
2496 * that rmdir() request comes.
2497 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
2499 cgroup_call_pre_destroy(cgrp);
2500 <<<<<<< HEAD:kernel/cgroup.c
2502 * Notify subsyses that rmdir() request comes.
2504 =======
2505 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
2507 if (cgroup_has_css_refs(cgrp)) {
2508 mutex_unlock(&cgroup_mutex);
2509 return -EBUSY;
2512 spin_lock(&release_list_lock);
2513 set_bit(CGRP_REMOVED, &cgrp->flags);
2514 if (!list_empty(&cgrp->release_list))
2515 list_del(&cgrp->release_list);
2516 spin_unlock(&release_list_lock);
2517 /* delete my sibling from parent->children */
2518 list_del(&cgrp->sibling);
2519 spin_lock(&cgrp->dentry->d_lock);
2520 d = dget(cgrp->dentry);
2521 cgrp->dentry = NULL;
2522 spin_unlock(&d->d_lock);
2524 cgroup_d_remove_dir(d);
2525 dput(d);
2527 set_bit(CGRP_RELEASABLE, &parent->flags);
2528 check_for_release(parent);
2530 mutex_unlock(&cgroup_mutex);
2531 return 0;
2534 static void cgroup_init_subsys(struct cgroup_subsys *ss)
2536 struct cgroup_subsys_state *css;
2537 struct list_head *l;
2539 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
2541 /* Create the top cgroup state for this subsystem */
2542 ss->root = &rootnode;
2543 css = ss->create(ss, dummytop);
2544 /* We don't handle early failures gracefully */
2545 BUG_ON(IS_ERR(css));
2546 init_cgroup_css(css, ss, dummytop);
2548 /* Update all cgroup groups to contain a subsys
2549 * pointer to this state - since the subsystem is
2550 * newly registered, all tasks and hence all cgroup
2551 * groups are in the subsystem's top cgroup. */
2552 write_lock(&css_set_lock);
2553 l = &init_css_set.list;
2554 do {
2555 struct css_set *cg =
2556 list_entry(l, struct css_set, list);
2557 cg->subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
2558 l = l->next;
2559 } while (l != &init_css_set.list);
2560 write_unlock(&css_set_lock);
2562 /* If this subsystem requested that it be notified with fork
2563 * events, we should send it one now for every process in the
2564 * system */
2565 if (ss->fork) {
2566 struct task_struct *g, *p;
2568 read_lock(&tasklist_lock);
2569 do_each_thread(g, p) {
2570 ss->fork(ss, p);
2571 } while_each_thread(g, p);
2572 read_unlock(&tasklist_lock);
2575 need_forkexit_callback |= ss->fork || ss->exit;
2577 ss->active = 1;
2581 <<<<<<< HEAD:kernel/cgroup.c
2582 * cgroup_init_early - initialize cgroups at system boot, and
2583 * initialize any subsystems that request early init.
2584 =======
2585 * cgroup_init_early - cgroup initialization at system boot
2587 * Initialize cgroups at system boot, and initialize any
2588 * subsystems that request early init.
2589 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
2591 int __init cgroup_init_early(void)
2593 int i;
2594 kref_init(&init_css_set.ref);
2595 kref_get(&init_css_set.ref);
2596 INIT_LIST_HEAD(&init_css_set.list);
2597 INIT_LIST_HEAD(&init_css_set.cg_links);
2598 INIT_LIST_HEAD(&init_css_set.tasks);
2599 css_set_count = 1;
2600 init_cgroup_root(&rootnode);
2601 list_add(&rootnode.root_list, &roots);
2602 root_count = 1;
2603 init_task.cgroups = &init_css_set;
2605 init_css_set_link.cg = &init_css_set;
2606 list_add(&init_css_set_link.cgrp_link_list,
2607 &rootnode.top_cgroup.css_sets);
2608 list_add(&init_css_set_link.cg_link_list,
2609 &init_css_set.cg_links);
2611 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2612 struct cgroup_subsys *ss = subsys[i];
2614 BUG_ON(!ss->name);
2615 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
2616 BUG_ON(!ss->create);
2617 BUG_ON(!ss->destroy);
2618 if (ss->subsys_id != i) {
2619 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
2620 ss->name, ss->subsys_id);
2621 BUG();
2624 if (ss->early_init)
2625 cgroup_init_subsys(ss);
2627 return 0;
2631 <<<<<<< HEAD:kernel/cgroup.c
2632 * cgroup_init - register cgroup filesystem and /proc file, and
2633 * initialize any subsystems that didn't request early init.
2634 =======
2635 * cgroup_init - cgroup initialization
2637 * Register cgroup filesystem and /proc file, and initialize
2638 * any subsystems that didn't request early init.
2639 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
2641 int __init cgroup_init(void)
2643 int err;
2644 int i;
2645 struct proc_dir_entry *entry;
2647 err = bdi_init(&cgroup_backing_dev_info);
2648 if (err)
2649 return err;
2651 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2652 struct cgroup_subsys *ss = subsys[i];
2653 if (!ss->early_init)
2654 cgroup_init_subsys(ss);
2657 err = register_filesystem(&cgroup_fs_type);
2658 if (err < 0)
2659 goto out;
2661 entry = create_proc_entry("cgroups", 0, NULL);
2662 if (entry)
2663 entry->proc_fops = &proc_cgroupstats_operations;
2665 out:
2666 if (err)
2667 bdi_destroy(&cgroup_backing_dev_info);
2669 return err;
2673 * proc_cgroup_show()
2674 * - Print task's cgroup paths into seq_file, one line for each hierarchy
2675 * - Used for /proc/<pid>/cgroup.
2676 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
2677 * doesn't really matter if tsk->cgroup changes after we read it,
2678 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
2679 * anyway. No need to check that tsk->cgroup != NULL, thanks to
2680 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
2681 * cgroup to top_cgroup.
2684 /* TODO: Use a proper seq_file iterator */
2685 static int proc_cgroup_show(struct seq_file *m, void *v)
2687 struct pid *pid;
2688 struct task_struct *tsk;
2689 char *buf;
2690 int retval;
2691 struct cgroupfs_root *root;
2693 retval = -ENOMEM;
2694 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2695 if (!buf)
2696 goto out;
2698 retval = -ESRCH;
2699 pid = m->private;
2700 tsk = get_pid_task(pid, PIDTYPE_PID);
2701 if (!tsk)
2702 goto out_free;
2704 retval = 0;
2706 mutex_lock(&cgroup_mutex);
2708 for_each_root(root) {
2709 struct cgroup_subsys *ss;
2710 struct cgroup *cgrp;
2711 int subsys_id;
2712 int count = 0;
2714 /* Skip this hierarchy if it has no active subsystems */
2715 if (!root->actual_subsys_bits)
2716 continue;
2717 for_each_subsys(root, ss)
2718 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
2719 seq_putc(m, ':');
2720 get_first_subsys(&root->top_cgroup, NULL, &subsys_id);
2721 cgrp = task_cgroup(tsk, subsys_id);
2722 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
2723 if (retval < 0)
2724 goto out_unlock;
2725 seq_puts(m, buf);
2726 seq_putc(m, '\n');
2729 out_unlock:
2730 mutex_unlock(&cgroup_mutex);
2731 put_task_struct(tsk);
2732 out_free:
2733 kfree(buf);
2734 out:
2735 return retval;
2738 static int cgroup_open(struct inode *inode, struct file *file)
2740 struct pid *pid = PROC_I(inode)->pid;
2741 return single_open(file, proc_cgroup_show, pid);
2744 struct file_operations proc_cgroup_operations = {
2745 .open = cgroup_open,
2746 .read = seq_read,
2747 .llseek = seq_lseek,
2748 .release = single_release,
2751 /* Display information about each subsystem and each hierarchy */
2752 static int proc_cgroupstats_show(struct seq_file *m, void *v)
2754 int i;
2756 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\n");
2757 mutex_lock(&cgroup_mutex);
2758 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2759 struct cgroup_subsys *ss = subsys[i];
2760 seq_printf(m, "%s\t%lu\t%d\n",
2761 ss->name, ss->root->subsys_bits,
2762 ss->root->number_of_cgroups);
2764 mutex_unlock(&cgroup_mutex);
2765 return 0;
2768 static int cgroupstats_open(struct inode *inode, struct file *file)
2770 return single_open(file, proc_cgroupstats_show, 0);
2773 static struct file_operations proc_cgroupstats_operations = {
2774 .open = cgroupstats_open,
2775 .read = seq_read,
2776 .llseek = seq_lseek,
2777 .release = single_release,
2781 * cgroup_fork - attach newly forked task to its parents cgroup.
2782 <<<<<<< HEAD:kernel/cgroup.c
2783 * @tsk: pointer to task_struct of forking parent process.
2784 =======
2785 * @child: pointer to task_struct of forking parent process.
2786 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
2788 * Description: A task inherits its parent's cgroup at fork().
2790 * A pointer to the shared css_set was automatically copied in
2791 * fork.c by dup_task_struct(). However, we ignore that copy, since
2792 * it was not made under the protection of RCU or cgroup_mutex, so
2793 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
2794 * have already changed current->cgroups, allowing the previously
2795 * referenced cgroup group to be removed and freed.
2797 * At the point that cgroup_fork() is called, 'current' is the parent
2798 * task, and the passed argument 'child' points to the child task.
2800 void cgroup_fork(struct task_struct *child)
2802 task_lock(current);
2803 child->cgroups = current->cgroups;
2804 get_css_set(child->cgroups);
2805 task_unlock(current);
2806 INIT_LIST_HEAD(&child->cg_list);
2810 <<<<<<< HEAD:kernel/cgroup.c
2811 * cgroup_fork_callbacks - called on a new task very soon before
2812 * adding it to the tasklist. No need to take any locks since no-one
2813 * can be operating on this task
2814 =======
2815 * cgroup_fork_callbacks - run fork callbacks
2816 * @child: the new task
2818 * Called on a new task very soon before adding it to the
2819 * tasklist. No need to take any locks since no-one can
2820 * be operating on this task.
2821 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
2823 void cgroup_fork_callbacks(struct task_struct *child)
2825 if (need_forkexit_callback) {
2826 int i;
2827 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2828 struct cgroup_subsys *ss = subsys[i];
2829 if (ss->fork)
2830 ss->fork(ss, child);
2836 <<<<<<< HEAD:kernel/cgroup.c
2837 * cgroup_post_fork - called on a new task after adding it to the
2838 * task list. Adds the task to the list running through its css_set
2839 * if necessary. Has to be after the task is visible on the task list
2840 * in case we race with the first call to cgroup_iter_start() - to
2841 * guarantee that the new task ends up on its list. */
2842 =======
2843 * cgroup_post_fork - called on a new task after adding it to the task list
2844 * @child: the task in question
2846 * Adds the task to the list running through its css_set if necessary.
2847 * Has to be after the task is visible on the task list in case we race
2848 * with the first call to cgroup_iter_start() - to guarantee that the
2849 * new task ends up on its list.
2851 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
2852 void cgroup_post_fork(struct task_struct *child)
2854 if (use_task_css_set_links) {
2855 write_lock(&css_set_lock);
2856 if (list_empty(&child->cg_list))
2857 list_add(&child->cg_list, &child->cgroups->tasks);
2858 write_unlock(&css_set_lock);
2862 * cgroup_exit - detach cgroup from exiting task
2863 * @tsk: pointer to task_struct of exiting process
2864 <<<<<<< HEAD:kernel/cgroup.c
2865 =======
2866 * @run_callback: run exit callbacks?
2867 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
2869 * Description: Detach cgroup from @tsk and release it.
2871 * Note that cgroups marked notify_on_release force every task in
2872 * them to take the global cgroup_mutex mutex when exiting.
2873 * This could impact scaling on very large systems. Be reluctant to
2874 * use notify_on_release cgroups where very high task exit scaling
2875 * is required on large systems.
2877 * the_top_cgroup_hack:
2879 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
2881 * We call cgroup_exit() while the task is still competent to
2882 * handle notify_on_release(), then leave the task attached to the
2883 * root cgroup in each hierarchy for the remainder of its exit.
2885 * To do this properly, we would increment the reference count on
2886 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
2887 * code we would add a second cgroup function call, to drop that
2888 * reference. This would just create an unnecessary hot spot on
2889 * the top_cgroup reference count, to no avail.
2891 * Normally, holding a reference to a cgroup without bumping its
2892 * count is unsafe. The cgroup could go away, or someone could
2893 * attach us to a different cgroup, decrementing the count on
2894 * the first cgroup that we never incremented. But in this case,
2895 * top_cgroup isn't going away, and either task has PF_EXITING set,
2896 * which wards off any cgroup_attach_task() attempts, or task is a failed
2897 * fork, never visible to cgroup_attach_task.
2898 <<<<<<< HEAD:kernel/cgroup.c
2900 =======
2901 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
2903 void cgroup_exit(struct task_struct *tsk, int run_callbacks)
2905 int i;
2906 struct css_set *cg;
2908 if (run_callbacks && need_forkexit_callback) {
2909 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2910 struct cgroup_subsys *ss = subsys[i];
2911 if (ss->exit)
2912 ss->exit(ss, tsk);
2917 * Unlink from the css_set task list if necessary.
2918 * Optimistically check cg_list before taking
2919 * css_set_lock
2921 if (!list_empty(&tsk->cg_list)) {
2922 write_lock(&css_set_lock);
2923 if (!list_empty(&tsk->cg_list))
2924 list_del(&tsk->cg_list);
2925 write_unlock(&css_set_lock);
2928 /* Reassign the task to the init_css_set. */
2929 task_lock(tsk);
2930 cg = tsk->cgroups;
2931 tsk->cgroups = &init_css_set;
2932 task_unlock(tsk);
2933 if (cg)
2934 put_css_set_taskexit(cg);
2938 <<<<<<< HEAD:kernel/cgroup.c
2939 * cgroup_clone - duplicate the current cgroup in the hierarchy
2940 * that the given subsystem is attached to, and move this task into
2941 * the new child
2942 =======
2943 * cgroup_clone - clone the cgroup the given subsystem is attached to
2944 * @tsk: the task to be moved
2945 * @subsys: the given subsystem
2947 * Duplicate the current cgroup in the hierarchy that the given
2948 * subsystem is attached to, and move this task into the new
2949 * child.
2950 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
2952 int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys)
2954 struct dentry *dentry;
2955 int ret = 0;
2956 char nodename[MAX_CGROUP_TYPE_NAMELEN];
2957 struct cgroup *parent, *child;
2958 struct inode *inode;
2959 struct css_set *cg;
2960 struct cgroupfs_root *root;
2961 struct cgroup_subsys *ss;
2963 /* We shouldn't be called by an unregistered subsystem */
2964 BUG_ON(!subsys->active);
2966 /* First figure out what hierarchy and cgroup we're dealing
2967 * with, and pin them so we can drop cgroup_mutex */
2968 mutex_lock(&cgroup_mutex);
2969 again:
2970 root = subsys->root;
2971 if (root == &rootnode) {
2972 printk(KERN_INFO
2973 "Not cloning cgroup for unused subsystem %s\n",
2974 subsys->name);
2975 mutex_unlock(&cgroup_mutex);
2976 return 0;
2978 cg = tsk->cgroups;
2979 parent = task_cgroup(tsk, subsys->subsys_id);
2981 snprintf(nodename, MAX_CGROUP_TYPE_NAMELEN, "node_%d", tsk->pid);
2983 /* Pin the hierarchy */
2984 atomic_inc(&parent->root->sb->s_active);
2986 /* Keep the cgroup alive */
2987 get_css_set(cg);
2988 mutex_unlock(&cgroup_mutex);
2990 /* Now do the VFS work to create a cgroup */
2991 inode = parent->dentry->d_inode;
2993 /* Hold the parent directory mutex across this operation to
2994 * stop anyone else deleting the new cgroup */
2995 mutex_lock(&inode->i_mutex);
2996 dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename));
2997 if (IS_ERR(dentry)) {
2998 printk(KERN_INFO
2999 "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename,
3000 PTR_ERR(dentry));
3001 ret = PTR_ERR(dentry);
3002 goto out_release;
3005 /* Create the cgroup directory, which also creates the cgroup */
3006 ret = vfs_mkdir(inode, dentry, S_IFDIR | 0755);
3007 child = __d_cgrp(dentry);
3008 dput(dentry);
3009 if (ret) {
3010 printk(KERN_INFO
3011 "Failed to create cgroup %s: %d\n", nodename,
3012 ret);
3013 goto out_release;
3016 if (!child) {
3017 printk(KERN_INFO
3018 "Couldn't find new cgroup %s\n", nodename);
3019 ret = -ENOMEM;
3020 goto out_release;
3023 /* The cgroup now exists. Retake cgroup_mutex and check
3024 * that we're still in the same state that we thought we
3025 * were. */
3026 mutex_lock(&cgroup_mutex);
3027 if ((root != subsys->root) ||
3028 (parent != task_cgroup(tsk, subsys->subsys_id))) {
3029 /* Aargh, we raced ... */
3030 mutex_unlock(&inode->i_mutex);
3031 put_css_set(cg);
3033 deactivate_super(parent->root->sb);
3034 /* The cgroup is still accessible in the VFS, but
3035 * we're not going to try to rmdir() it at this
3036 * point. */
3037 printk(KERN_INFO
3038 "Race in cgroup_clone() - leaking cgroup %s\n",
3039 nodename);
3040 goto again;
3043 /* do any required auto-setup */
3044 for_each_subsys(root, ss) {
3045 if (ss->post_clone)
3046 ss->post_clone(ss, child);
3049 /* All seems fine. Finish by moving the task into the new cgroup */
3050 ret = cgroup_attach_task(child, tsk);
3051 mutex_unlock(&cgroup_mutex);
3053 out_release:
3054 mutex_unlock(&inode->i_mutex);
3056 mutex_lock(&cgroup_mutex);
3057 put_css_set(cg);
3058 mutex_unlock(&cgroup_mutex);
3059 deactivate_super(parent->root->sb);
3060 return ret;
3063 <<<<<<< HEAD:kernel/cgroup.c
3065 * See if "cgrp" is a descendant of the current task's cgroup in
3066 * the appropriate hierarchy
3067 =======
3069 * cgroup_is_descendant - see if @cgrp is a descendant of current task's cgrp
3070 * @cgrp: the cgroup in question
3072 * See if @cgrp is a descendant of the current task's cgroup in
3073 * the appropriate hierarchy.
3074 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
3076 * If we are sending in dummytop, then presumably we are creating
3077 * the top cgroup in the subsystem.
3079 * Called only by the ns (nsproxy) cgroup.
3081 int cgroup_is_descendant(const struct cgroup *cgrp)
3083 int ret;
3084 struct cgroup *target;
3085 int subsys_id;
3087 if (cgrp == dummytop)
3088 return 1;
3090 get_first_subsys(cgrp, NULL, &subsys_id);
3091 target = task_cgroup(current, subsys_id);
3092 while (cgrp != target && cgrp!= cgrp->top_cgroup)
3093 cgrp = cgrp->parent;
3094 ret = (cgrp == target);
3095 return ret;
3098 static void check_for_release(struct cgroup *cgrp)
3100 /* All of these checks rely on RCU to keep the cgroup
3101 * structure alive */
3102 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
3103 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
3104 /* Control Group is currently removeable. If it's not
3105 * already queued for a userspace notification, queue
3106 * it now */
3107 int need_schedule_work = 0;
3108 spin_lock(&release_list_lock);
3109 if (!cgroup_is_removed(cgrp) &&
3110 list_empty(&cgrp->release_list)) {
3111 list_add(&cgrp->release_list, &release_list);
3112 need_schedule_work = 1;
3114 spin_unlock(&release_list_lock);
3115 if (need_schedule_work)
3116 schedule_work(&release_agent_work);
3120 void __css_put(struct cgroup_subsys_state *css)
3122 struct cgroup *cgrp = css->cgroup;
3123 rcu_read_lock();
3124 if (atomic_dec_and_test(&css->refcnt) && notify_on_release(cgrp)) {
3125 set_bit(CGRP_RELEASABLE, &cgrp->flags);
3126 check_for_release(cgrp);
3128 rcu_read_unlock();
3132 * Notify userspace when a cgroup is released, by running the
3133 * configured release agent with the name of the cgroup (path
3134 * relative to the root of cgroup file system) as the argument.
3136 * Most likely, this user command will try to rmdir this cgroup.
3138 * This races with the possibility that some other task will be
3139 * attached to this cgroup before it is removed, or that some other
3140 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
3141 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
3142 * unused, and this cgroup will be reprieved from its death sentence,
3143 * to continue to serve a useful existence. Next time it's released,
3144 * we will get notified again, if it still has 'notify_on_release' set.
3146 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
3147 * means only wait until the task is successfully execve()'d. The
3148 * separate release agent task is forked by call_usermodehelper(),
3149 * then control in this thread returns here, without waiting for the
3150 * release agent task. We don't bother to wait because the caller of
3151 * this routine has no use for the exit status of the release agent
3152 * task, so no sense holding our caller up for that.
3153 <<<<<<< HEAD:kernel/cgroup.c
3155 =======
3156 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
3158 <<<<<<< HEAD:kernel/cgroup.c
3160 =======
3161 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/cgroup.c
3162 static void cgroup_release_agent(struct work_struct *work)
3164 BUG_ON(work != &release_agent_work);
3165 mutex_lock(&cgroup_mutex);
3166 spin_lock(&release_list_lock);
3167 while (!list_empty(&release_list)) {
3168 char *argv[3], *envp[3];
3169 int i;
3170 char *pathbuf;
3171 struct cgroup *cgrp = list_entry(release_list.next,
3172 struct cgroup,
3173 release_list);
3174 list_del_init(&cgrp->release_list);
3175 spin_unlock(&release_list_lock);
3176 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3177 if (!pathbuf) {
3178 spin_lock(&release_list_lock);
3179 continue;
3182 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0) {
3183 kfree(pathbuf);
3184 spin_lock(&release_list_lock);
3185 continue;
3188 i = 0;
3189 argv[i++] = cgrp->root->release_agent_path;
3190 argv[i++] = (char *)pathbuf;
3191 argv[i] = NULL;
3193 i = 0;
3194 /* minimal command environment */
3195 envp[i++] = "HOME=/";
3196 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
3197 envp[i] = NULL;
3199 /* Drop the lock while we invoke the usermode helper,
3200 * since the exec could involve hitting disk and hence
3201 * be a slow process */
3202 mutex_unlock(&cgroup_mutex);
3203 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
3204 kfree(pathbuf);
3205 mutex_lock(&cgroup_mutex);
3206 spin_lock(&release_list_lock);
3208 spin_unlock(&release_list_lock);
3209 mutex_unlock(&cgroup_mutex);