| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1 | /* | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2 |  *  Generic process-grouping system. | 
 | 3 |  * | 
 | 4 |  *  Based originally on the cpuset system, extracted by Paul Menage | 
 | 5 |  *  Copyright (C) 2006 Google, Inc | 
 | 6 |  * | 
| Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 7 |  *  Notifications support | 
 | 8 |  *  Copyright (C) 2009 Nokia Corporation | 
 | 9 |  *  Author: Kirill A. Shutemov | 
 | 10 |  * | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 11 |  *  Copyright notices from the original cpuset code: | 
 | 12 |  *  -------------------------------------------------- | 
 | 13 |  *  Copyright (C) 2003 BULL SA. | 
 | 14 |  *  Copyright (C) 2004-2006 Silicon Graphics, Inc. | 
 | 15 |  * | 
 | 16 |  *  Portions derived from Patrick Mochel's sysfs code. | 
 | 17 |  *  sysfs is Copyright (c) 2001-3 Patrick Mochel | 
 | 18 |  * | 
 | 19 |  *  2003-10-10 Written by Simon Derr. | 
 | 20 |  *  2003-10-22 Updates by Stephen Hemminger. | 
 | 21 |  *  2004 May-July Rework by Paul Jackson. | 
 | 22 |  *  --------------------------------------------------- | 
 | 23 |  * | 
 | 24 |  *  This file is subject to the terms and conditions of the GNU General Public | 
 | 25 |  *  License.  See the file COPYING in the main directory of the Linux | 
 | 26 |  *  distribution for more details. | 
 | 27 |  */ | 
 | 28 |  | 
 | 29 | #include <linux/cgroup.h> | 
| eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 30 | #include <linux/cred.h> | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 31 | #include <linux/ctype.h> | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 32 | #include <linux/errno.h> | 
 | 33 | #include <linux/fs.h> | 
| eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 34 | #include <linux/init_task.h> | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 35 | #include <linux/kernel.h> | 
 | 36 | #include <linux/list.h> | 
 | 37 | #include <linux/mm.h> | 
 | 38 | #include <linux/mutex.h> | 
 | 39 | #include <linux/mount.h> | 
 | 40 | #include <linux/pagemap.h> | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 41 | #include <linux/proc_fs.h> | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 42 | #include <linux/rcupdate.h> | 
 | 43 | #include <linux/sched.h> | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 44 | #include <linux/backing-dev.h> | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 45 | #include <linux/seq_file.h> | 
 | 46 | #include <linux/slab.h> | 
 | 47 | #include <linux/magic.h> | 
 | 48 | #include <linux/spinlock.h> | 
 | 49 | #include <linux/string.h> | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 50 | #include <linux/sort.h> | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 51 | #include <linux/kmod.h> | 
| Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 52 | #include <linux/module.h> | 
| Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 53 | #include <linux/delayacct.h> | 
 | 54 | #include <linux/cgroupstats.h> | 
| Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 55 | #include <linux/hash.h> | 
| Al Viro | 3f8206d | 2008-07-26 03:46:43 -0400 | [diff] [blame] | 56 | #include <linux/namei.h> | 
| Li Zefan | 096b7fe | 2009-07-29 15:04:04 -0700 | [diff] [blame] | 57 | #include <linux/pid_namespace.h> | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 58 | #include <linux/idr.h> | 
| Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 59 | #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */ | 
| Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 60 | #include <linux/eventfd.h> | 
 | 61 | #include <linux/poll.h> | 
| Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 62 | #include <linux/flex_array.h> /* used in cgroup_attach_proc */ | 
| Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 63 |  | 
| Arun Sharma | 6006349 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 64 | #include <linux/atomic.h> | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 65 |  | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 66 | /* | 
 | 67 |  * cgroup_mutex is the master lock.  Any modification to cgroup or its | 
 | 68 |  * hierarchy must be performed while holding it. | 
 | 69 |  * | 
 | 70 |  * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify | 
 | 71 |  * cgroupfs_root of any cgroup hierarchy - subsys list, flags, | 
 | 72 |  * release_agent_path and so on.  Modifying requires both cgroup_mutex and | 
 | 73 |  * cgroup_root_mutex.  Readers can acquire either of the two.  This is to | 
 | 74 |  * break the following locking order cycle. | 
 | 75 |  * | 
 | 76 |  *  A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem | 
 | 77 |  *  B. namespace_sem -> cgroup_mutex | 
 | 78 |  * | 
 | 79 |  * B happens only through cgroup_show_options() and using cgroup_root_mutex | 
 | 80 |  * breaks it. | 
 | 81 |  */ | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 82 | static DEFINE_MUTEX(cgroup_mutex); | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 83 | static DEFINE_MUTEX(cgroup_root_mutex); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 84 |  | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 85 | /* | 
 | 86 |  * Generate an array of cgroup subsystem pointers. At boot time, this is | 
 | 87 |  * populated up to CGROUP_BUILTIN_SUBSYS_COUNT, and modular subsystems are | 
 | 88 |  * registered after that. The mutable section of this array is protected by | 
 | 89 |  * cgroup_mutex. | 
 | 90 |  */ | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 91 | #define SUBSYS(_x) &_x ## _subsys, | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 92 | static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = { | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 93 | #include <linux/cgroup_subsys.h> | 
 | 94 | }; | 
 | 95 |  | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 96 | #define MAX_CGROUP_ROOT_NAMELEN 64 | 
 | 97 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 98 | /* | 
 | 99 |  * A cgroupfs_root represents the root of a cgroup hierarchy, | 
 | 100 |  * and may be associated with a superblock to form an active | 
 | 101 |  * hierarchy | 
 | 102 |  */ | 
 | 103 | struct cgroupfs_root { | 
 | 104 | 	struct super_block *sb; | 
 | 105 |  | 
 | 106 | 	/* | 
 | 107 | 	 * The bitmask of subsystems intended to be attached to this | 
 | 108 | 	 * hierarchy | 
 | 109 | 	 */ | 
 | 110 | 	unsigned long subsys_bits; | 
 | 111 |  | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 112 | 	/* Unique id for this hierarchy. */ | 
 | 113 | 	int hierarchy_id; | 
 | 114 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 115 | 	/* The bitmask of subsystems currently attached to this hierarchy */ | 
 | 116 | 	unsigned long actual_subsys_bits; | 
 | 117 |  | 
 | 118 | 	/* A list running through the attached subsystems */ | 
 | 119 | 	struct list_head subsys_list; | 
 | 120 |  | 
 | 121 | 	/* The root cgroup for this hierarchy */ | 
 | 122 | 	struct cgroup top_cgroup; | 
 | 123 |  | 
 | 124 | 	/* Tracks how many cgroups are currently defined in hierarchy.*/ | 
 | 125 | 	int number_of_cgroups; | 
 | 126 |  | 
| Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 127 | 	/* A list running through the active hierarchies */ | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 128 | 	struct list_head root_list; | 
 | 129 |  | 
 | 130 | 	/* Hierarchy-specific flags */ | 
 | 131 | 	unsigned long flags; | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 132 |  | 
| Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 133 | 	/* The path to use for release notifications. */ | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 134 | 	char release_agent_path[PATH_MAX]; | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 135 |  | 
 | 136 | 	/* The name for this hierarchy - may be empty */ | 
 | 137 | 	char name[MAX_CGROUP_ROOT_NAMELEN]; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 138 | }; | 
 | 139 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 140 | /* | 
 | 141 |  * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the | 
 | 142 |  * subsystems that are otherwise unattached - it never has more than a | 
 | 143 |  * single cgroup, and all tasks are part of that cgroup. | 
 | 144 |  */ | 
 | 145 | static struct cgroupfs_root rootnode; | 
 | 146 |  | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 147 | /* | 
 | 148 |  * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when | 
 | 149 |  * cgroup_subsys->use_id != 0. | 
 | 150 |  */ | 
 | 151 | #define CSS_ID_MAX	(65535) | 
 | 152 | struct css_id { | 
 | 153 | 	/* | 
 | 154 | 	 * The css to which this ID points. This pointer is set to valid value | 
 | 155 | 	 * after cgroup is populated. If cgroup is removed, this will be NULL. | 
 | 156 | 	 * This pointer is expected to be RCU-safe because destroy() | 
 | 157 | 	 * is called after synchronize_rcu(). But for safe use, css_is_removed() | 
 | 158 | 	 * css_tryget() should be used for avoiding race. | 
 | 159 | 	 */ | 
| Arnd Bergmann | 2c392b8 | 2010-02-24 19:41:39 +0100 | [diff] [blame] | 160 | 	struct cgroup_subsys_state __rcu *css; | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 161 | 	/* | 
 | 162 | 	 * ID of this css. | 
 | 163 | 	 */ | 
 | 164 | 	unsigned short id; | 
 | 165 | 	/* | 
 | 166 | 	 * Depth in hierarchy which this ID belongs to. | 
 | 167 | 	 */ | 
 | 168 | 	unsigned short depth; | 
 | 169 | 	/* | 
 | 170 | 	 * ID is freed by RCU. (and lookup routine is RCU safe.) | 
 | 171 | 	 */ | 
 | 172 | 	struct rcu_head rcu_head; | 
 | 173 | 	/* | 
 | 174 | 	 * Hierarchy of CSS ID belongs to. | 
 | 175 | 	 */ | 
 | 176 | 	unsigned short stack[0]; /* Array of Length (depth+1) */ | 
 | 177 | }; | 
 | 178 |  | 
| Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 179 | /* | 
| Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 180 |  * cgroup_event represents events which userspace want to receive. | 
| Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 181 |  */ | 
 | 182 | struct cgroup_event { | 
 | 183 | 	/* | 
 | 184 | 	 * Cgroup which the event belongs to. | 
 | 185 | 	 */ | 
 | 186 | 	struct cgroup *cgrp; | 
 | 187 | 	/* | 
 | 188 | 	 * Control file which the event associated. | 
 | 189 | 	 */ | 
 | 190 | 	struct cftype *cft; | 
 | 191 | 	/* | 
 | 192 | 	 * eventfd to signal userspace about the event. | 
 | 193 | 	 */ | 
 | 194 | 	struct eventfd_ctx *eventfd; | 
 | 195 | 	/* | 
 | 196 | 	 * Each of these stored in a list by the cgroup. | 
 | 197 | 	 */ | 
 | 198 | 	struct list_head list; | 
 | 199 | 	/* | 
 | 200 | 	 * All fields below needed to unregister event when | 
 | 201 | 	 * userspace closes eventfd. | 
 | 202 | 	 */ | 
 | 203 | 	poll_table pt; | 
 | 204 | 	wait_queue_head_t *wqh; | 
 | 205 | 	wait_queue_t wait; | 
 | 206 | 	struct work_struct remove; | 
 | 207 | }; | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 208 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 209 | /* The list of hierarchy roots */ | 
 | 210 |  | 
 | 211 | static LIST_HEAD(roots); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 212 | static int root_count; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 213 |  | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 214 | static DEFINE_IDA(hierarchy_ida); | 
 | 215 | static int next_hierarchy_id; | 
 | 216 | static DEFINE_SPINLOCK(hierarchy_id_lock); | 
 | 217 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 218 | /* dummytop is a shorthand for the dummy hierarchy's top cgroup */ | 
 | 219 | #define dummytop (&rootnode.top_cgroup) | 
 | 220 |  | 
 | 221 | /* This flag indicates whether tasks in the fork and exit paths should | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 222 |  * check for fork/exit handlers to call. This avoids us having to do | 
 | 223 |  * extra work in the fork/exit path if none of the subsystems need to | 
 | 224 |  * be called. | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 225 |  */ | 
| Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 226 | static int need_forkexit_callback __read_mostly; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 227 |  | 
| Paul E. McKenney | d11c563 | 2010-02-22 17:04:50 -0800 | [diff] [blame] | 228 | #ifdef CONFIG_PROVE_LOCKING | 
 | 229 | int cgroup_lock_is_held(void) | 
 | 230 | { | 
 | 231 | 	return lockdep_is_held(&cgroup_mutex); | 
 | 232 | } | 
 | 233 | #else /* #ifdef CONFIG_PROVE_LOCKING */ | 
 | 234 | int cgroup_lock_is_held(void) | 
 | 235 | { | 
 | 236 | 	return mutex_is_locked(&cgroup_mutex); | 
 | 237 | } | 
 | 238 | #endif /* #else #ifdef CONFIG_PROVE_LOCKING */ | 
 | 239 |  | 
 | 240 | EXPORT_SYMBOL_GPL(cgroup_lock_is_held); | 
 | 241 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 242 | /* convenient tests for these bits */ | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 243 | inline int cgroup_is_removed(const struct cgroup *cgrp) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 244 | { | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 245 | 	return test_bit(CGRP_REMOVED, &cgrp->flags); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 246 | } | 
 | 247 |  | 
 | 248 | /* bits in struct cgroupfs_root flags field */ | 
 | 249 | enum { | 
 | 250 | 	ROOT_NOPREFIX, /* mounted subsystems have no named prefix */ | 
 | 251 | }; | 
 | 252 |  | 
| Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 253 | static int cgroup_is_releasable(const struct cgroup *cgrp) | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 254 | { | 
 | 255 | 	const int bits = | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 256 | 		(1 << CGRP_RELEASABLE) | | 
 | 257 | 		(1 << CGRP_NOTIFY_ON_RELEASE); | 
 | 258 | 	return (cgrp->flags & bits) == bits; | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 259 | } | 
 | 260 |  | 
| Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 261 | static int notify_on_release(const struct cgroup *cgrp) | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 262 | { | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 263 | 	return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 264 | } | 
 | 265 |  | 
| Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 266 | static int clone_children(const struct cgroup *cgrp) | 
 | 267 | { | 
 | 268 | 	return test_bit(CGRP_CLONE_CHILDREN, &cgrp->flags); | 
 | 269 | } | 
 | 270 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 271 | /* | 
 | 272 |  * for_each_subsys() allows you to iterate on each subsystem attached to | 
 | 273 |  * an active hierarchy | 
 | 274 |  */ | 
 | 275 | #define for_each_subsys(_root, _ss) \ | 
 | 276 | list_for_each_entry(_ss, &_root->subsys_list, sibling) | 
 | 277 |  | 
| Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 278 | /* for_each_active_root() allows you to iterate across the active hierarchies */ | 
 | 279 | #define for_each_active_root(_root) \ | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 280 | list_for_each_entry(_root, &roots, root_list) | 
 | 281 |  | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 282 | /* the list of cgroups eligible for automatic release. Protected by | 
 | 283 |  * release_list_lock */ | 
 | 284 | static LIST_HEAD(release_list); | 
| Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 285 | static DEFINE_RAW_SPINLOCK(release_list_lock); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 286 | static void cgroup_release_agent(struct work_struct *work); | 
 | 287 | static DECLARE_WORK(release_agent_work, cgroup_release_agent); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 288 | static void check_for_release(struct cgroup *cgrp); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 289 |  | 
| Colin Cross | befae2f | 2010-11-23 21:37:04 -0800 | [diff] [blame] | 290 | /* | 
 | 291 |  * A queue for waiters to do rmdir() cgroup. A tasks will sleep when | 
 | 292 |  * cgroup->count == 0 && list_empty(&cgroup->children) && subsys has some | 
 | 293 |  * reference to css->refcnt. In general, this refcnt is expected to goes down | 
 | 294 |  * to zero, soon. | 
 | 295 |  * | 
 | 296 |  * CGRP_WAIT_ON_RMDIR flag is set under cgroup's inode->i_mutex; | 
 | 297 |  */ | 
 | 298 | static DECLARE_WAIT_QUEUE_HEAD(cgroup_rmdir_waitq); | 
 | 299 |  | 
 | 300 | static void cgroup_wakeup_rmdir_waiter(struct cgroup *cgrp) | 
 | 301 | { | 
 | 302 | 	if (unlikely(test_and_clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags))) | 
 | 303 | 		wake_up_all(&cgroup_rmdir_waitq); | 
 | 304 | } | 
 | 305 |  | 
 | 306 | void cgroup_exclude_rmdir(struct cgroup_subsys_state *css) | 
 | 307 | { | 
 | 308 | 	css_get(css); | 
 | 309 | } | 
 | 310 |  | 
 | 311 | void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state *css) | 
 | 312 | { | 
 | 313 | 	cgroup_wakeup_rmdir_waiter(css->cgroup); | 
 | 314 | 	css_put(css); | 
 | 315 | } | 
 | 316 |  | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 317 | /* Link structure for associating css_set objects with cgroups */ | 
 | 318 | struct cg_cgroup_link { | 
 | 319 | 	/* | 
 | 320 | 	 * List running through cg_cgroup_links associated with a | 
 | 321 | 	 * cgroup, anchored on cgroup->css_sets | 
 | 322 | 	 */ | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 323 | 	struct list_head cgrp_link_list; | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 324 | 	struct cgroup *cgrp; | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 325 | 	/* | 
 | 326 | 	 * List running through cg_cgroup_links pointing at a | 
 | 327 | 	 * single css_set object, anchored on css_set->cg_links | 
 | 328 | 	 */ | 
 | 329 | 	struct list_head cg_link_list; | 
 | 330 | 	struct css_set *cg; | 
 | 331 | }; | 
 | 332 |  | 
 | 333 | /* The default css_set - used by init and its children prior to any | 
 | 334 |  * hierarchies being mounted. It contains a pointer to the root state | 
 | 335 |  * for each subsystem. Also used to anchor the list of css_sets. Not | 
 | 336 |  * reference-counted, to improve performance when child cgroups | 
 | 337 |  * haven't been created. | 
 | 338 |  */ | 
 | 339 |  | 
 | 340 | static struct css_set init_css_set; | 
 | 341 | static struct cg_cgroup_link init_css_set_link; | 
 | 342 |  | 
| Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 343 | static int cgroup_init_idr(struct cgroup_subsys *ss, | 
 | 344 | 			   struct cgroup_subsys_state *css); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 345 |  | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 346 | /* css_set_lock protects the list of css_set objects, and the | 
 | 347 |  * chain of tasks off each css_set.  Nests outside task->alloc_lock | 
 | 348 |  * due to cgroup_iter_start() */ | 
 | 349 | static DEFINE_RWLOCK(css_set_lock); | 
 | 350 | static int css_set_count; | 
 | 351 |  | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 352 | /* | 
 | 353 |  * hash table for cgroup groups. This improves the performance to find | 
 | 354 |  * an existing css_set. This hash doesn't (currently) take into | 
 | 355 |  * account cgroups in empty hierarchies. | 
 | 356 |  */ | 
| Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 357 | #define CSS_SET_HASH_BITS	7 | 
 | 358 | #define CSS_SET_TABLE_SIZE	(1 << CSS_SET_HASH_BITS) | 
 | 359 | static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE]; | 
 | 360 |  | 
 | 361 | static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[]) | 
 | 362 | { | 
 | 363 | 	int i; | 
 | 364 | 	int index; | 
 | 365 | 	unsigned long tmp = 0UL; | 
 | 366 |  | 
 | 367 | 	for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) | 
 | 368 | 		tmp += (unsigned long)css[i]; | 
 | 369 | 	tmp = (tmp >> 16) ^ tmp; | 
 | 370 |  | 
 | 371 | 	index = hash_long(tmp, CSS_SET_HASH_BITS); | 
 | 372 |  | 
 | 373 | 	return &css_set_table[index]; | 
 | 374 | } | 
 | 375 |  | 
| Colin Cross | befae2f | 2010-11-23 21:37:04 -0800 | [diff] [blame] | 376 | static void free_css_set_work(struct work_struct *work) | 
 | 377 | { | 
 | 378 | 	struct css_set *cg = container_of(work, struct css_set, work); | 
 | 379 | 	struct cg_cgroup_link *link; | 
 | 380 | 	struct cg_cgroup_link *saved_link; | 
 | 381 |  | 
 | 382 | 	write_lock(&css_set_lock); | 
 | 383 | 	list_for_each_entry_safe(link, saved_link, &cg->cg_links, | 
 | 384 | 				 cg_link_list) { | 
 | 385 | 		struct cgroup *cgrp = link->cgrp; | 
 | 386 | 		list_del(&link->cg_link_list); | 
 | 387 | 		list_del(&link->cgrp_link_list); | 
 | 388 | 		if (atomic_dec_and_test(&cgrp->count)) { | 
 | 389 | 			check_for_release(cgrp); | 
 | 390 | 			cgroup_wakeup_rmdir_waiter(cgrp); | 
 | 391 | 		} | 
 | 392 | 		kfree(link); | 
 | 393 | 	} | 
 | 394 | 	write_unlock(&css_set_lock); | 
 | 395 |  | 
 | 396 | 	kfree(cg); | 
 | 397 | } | 
 | 398 |  | 
 | 399 | static void free_css_set_rcu(struct rcu_head *obj) | 
 | 400 | { | 
 | 401 | 	struct css_set *cg = container_of(obj, struct css_set, rcu_head); | 
 | 402 |  | 
 | 403 | 	INIT_WORK(&cg->work, free_css_set_work); | 
 | 404 | 	schedule_work(&cg->work); | 
 | 405 | } | 
 | 406 |  | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 407 | /* We don't maintain the lists running through each css_set to its | 
 | 408 |  * task until after the first call to cgroup_iter_start(). This | 
 | 409 |  * reduces the fork()/exit() overhead for people who have cgroups | 
 | 410 |  * compiled into their kernel but not actually in use */ | 
| Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 411 | static int use_task_css_set_links __read_mostly; | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 412 |  | 
| Colin Cross | c0f6fa8 | 2010-11-23 21:37:03 -0800 | [diff] [blame] | 413 | /* | 
 | 414 |  * refcounted get/put for css_set objects | 
 | 415 |  */ | 
 | 416 | static inline void get_css_set(struct css_set *cg) | 
 | 417 | { | 
 | 418 | 	atomic_inc(&cg->refcount); | 
 | 419 | } | 
 | 420 |  | 
 | 421 | static void put_css_set(struct css_set *cg) | 
| Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 422 | { | 
| Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 423 | 	/* | 
 | 424 | 	 * Ensure that the refcount doesn't hit zero while any readers | 
 | 425 | 	 * can see it. Similar to atomic_dec_and_lock(), but for an | 
 | 426 | 	 * rwlock | 
 | 427 | 	 */ | 
 | 428 | 	if (atomic_add_unless(&cg->refcount, -1, 1)) | 
 | 429 | 		return; | 
 | 430 | 	write_lock(&css_set_lock); | 
 | 431 | 	if (!atomic_dec_and_test(&cg->refcount)) { | 
 | 432 | 		write_unlock(&css_set_lock); | 
 | 433 | 		return; | 
 | 434 | 	} | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 435 |  | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 436 | 	hlist_del(&cg->hlist); | 
 | 437 | 	css_set_count--; | 
 | 438 |  | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 439 | 	write_unlock(&css_set_lock); | 
| Colin Cross | befae2f | 2010-11-23 21:37:04 -0800 | [diff] [blame] | 440 | 	call_rcu(&cg->rcu_head, free_css_set_rcu); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 441 | } | 
 | 442 |  | 
 | 443 | /* | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 444 |  * compare_css_sets - helper function for find_existing_css_set(). | 
 | 445 |  * @cg: candidate css_set being tested | 
 | 446 |  * @old_cg: existing css_set for a task | 
 | 447 |  * @new_cgrp: cgroup that's being entered by the task | 
 | 448 |  * @template: desired set of css pointers in css_set (pre-calculated) | 
 | 449 |  * | 
 | 450 |  * Returns true if "cg" matches "old_cg" except for the hierarchy | 
 | 451 |  * which "new_cgrp" belongs to, for which it should match "new_cgrp". | 
 | 452 |  */ | 
 | 453 | static bool compare_css_sets(struct css_set *cg, | 
 | 454 | 			     struct css_set *old_cg, | 
 | 455 | 			     struct cgroup *new_cgrp, | 
 | 456 | 			     struct cgroup_subsys_state *template[]) | 
 | 457 | { | 
 | 458 | 	struct list_head *l1, *l2; | 
 | 459 |  | 
 | 460 | 	if (memcmp(template, cg->subsys, sizeof(cg->subsys))) { | 
 | 461 | 		/* Not all subsystems matched */ | 
 | 462 | 		return false; | 
 | 463 | 	} | 
 | 464 |  | 
 | 465 | 	/* | 
 | 466 | 	 * Compare cgroup pointers in order to distinguish between | 
 | 467 | 	 * different cgroups in heirarchies with no subsystems. We | 
 | 468 | 	 * could get by with just this check alone (and skip the | 
 | 469 | 	 * memcmp above) but on most setups the memcmp check will | 
 | 470 | 	 * avoid the need for this more expensive check on almost all | 
 | 471 | 	 * candidates. | 
 | 472 | 	 */ | 
 | 473 |  | 
 | 474 | 	l1 = &cg->cg_links; | 
 | 475 | 	l2 = &old_cg->cg_links; | 
 | 476 | 	while (1) { | 
 | 477 | 		struct cg_cgroup_link *cgl1, *cgl2; | 
 | 478 | 		struct cgroup *cg1, *cg2; | 
 | 479 |  | 
 | 480 | 		l1 = l1->next; | 
 | 481 | 		l2 = l2->next; | 
 | 482 | 		/* See if we reached the end - both lists are equal length. */ | 
 | 483 | 		if (l1 == &cg->cg_links) { | 
 | 484 | 			BUG_ON(l2 != &old_cg->cg_links); | 
 | 485 | 			break; | 
 | 486 | 		} else { | 
 | 487 | 			BUG_ON(l2 == &old_cg->cg_links); | 
 | 488 | 		} | 
 | 489 | 		/* Locate the cgroups associated with these links. */ | 
 | 490 | 		cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list); | 
 | 491 | 		cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list); | 
 | 492 | 		cg1 = cgl1->cgrp; | 
 | 493 | 		cg2 = cgl2->cgrp; | 
 | 494 | 		/* Hierarchies should be linked in the same order. */ | 
 | 495 | 		BUG_ON(cg1->root != cg2->root); | 
 | 496 |  | 
 | 497 | 		/* | 
 | 498 | 		 * If this hierarchy is the hierarchy of the cgroup | 
 | 499 | 		 * that's changing, then we need to check that this | 
 | 500 | 		 * css_set points to the new cgroup; if it's any other | 
 | 501 | 		 * hierarchy, then this css_set should point to the | 
 | 502 | 		 * same cgroup as the old css_set. | 
 | 503 | 		 */ | 
 | 504 | 		if (cg1->root == new_cgrp->root) { | 
 | 505 | 			if (cg1 != new_cgrp) | 
 | 506 | 				return false; | 
 | 507 | 		} else { | 
 | 508 | 			if (cg1 != cg2) | 
 | 509 | 				return false; | 
 | 510 | 		} | 
 | 511 | 	} | 
 | 512 | 	return true; | 
 | 513 | } | 
 | 514 |  | 
 | 515 | /* | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 516 |  * find_existing_css_set() is a helper for | 
 | 517 |  * find_css_set(), and checks to see whether an existing | 
| Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 518 |  * css_set is suitable. | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 519 |  * | 
 | 520 |  * oldcg: the cgroup group that we're using before the cgroup | 
 | 521 |  * transition | 
 | 522 |  * | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 523 |  * cgrp: the cgroup that we're moving into | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 524 |  * | 
 | 525 |  * template: location in which to build the desired set of subsystem | 
 | 526 |  * state objects for the new cgroup group | 
 | 527 |  */ | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 528 | static struct css_set *find_existing_css_set( | 
 | 529 | 	struct css_set *oldcg, | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 530 | 	struct cgroup *cgrp, | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 531 | 	struct cgroup_subsys_state *template[]) | 
 | 532 | { | 
 | 533 | 	int i; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 534 | 	struct cgroupfs_root *root = cgrp->root; | 
| Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 535 | 	struct hlist_head *hhead; | 
 | 536 | 	struct hlist_node *node; | 
 | 537 | 	struct css_set *cg; | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 538 |  | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 539 | 	/* | 
 | 540 | 	 * Build the set of subsystem state objects that we want to see in the | 
 | 541 | 	 * new css_set. while subsystems can change globally, the entries here | 
 | 542 | 	 * won't change, so no need for locking. | 
 | 543 | 	 */ | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 544 | 	for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { | 
| Li Zefan | 8d53d55 | 2008-02-23 15:24:11 -0800 | [diff] [blame] | 545 | 		if (root->subsys_bits & (1UL << i)) { | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 546 | 			/* Subsystem is in this hierarchy. So we want | 
 | 547 | 			 * the subsystem state from the new | 
 | 548 | 			 * cgroup */ | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 549 | 			template[i] = cgrp->subsys[i]; | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 550 | 		} else { | 
 | 551 | 			/* Subsystem is not in this hierarchy, so we | 
 | 552 | 			 * don't want to change the subsystem state */ | 
 | 553 | 			template[i] = oldcg->subsys[i]; | 
 | 554 | 		} | 
 | 555 | 	} | 
 | 556 |  | 
| Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 557 | 	hhead = css_set_hash(template); | 
 | 558 | 	hlist_for_each_entry(cg, node, hhead, hlist) { | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 559 | 		if (!compare_css_sets(cg, oldcg, cgrp, template)) | 
 | 560 | 			continue; | 
 | 561 |  | 
 | 562 | 		/* This css_set matches what we need */ | 
 | 563 | 		return cg; | 
| Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 564 | 	} | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 565 |  | 
 | 566 | 	/* No existing cgroup group matched */ | 
 | 567 | 	return NULL; | 
 | 568 | } | 
 | 569 |  | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 570 | static void free_cg_links(struct list_head *tmp) | 
 | 571 | { | 
| KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 572 | 	struct cg_cgroup_link *link; | 
 | 573 | 	struct cg_cgroup_link *saved_link; | 
 | 574 |  | 
 | 575 | 	list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) { | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 576 | 		list_del(&link->cgrp_link_list); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 577 | 		kfree(link); | 
 | 578 | 	} | 
 | 579 | } | 
 | 580 |  | 
 | 581 | /* | 
| Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 582 |  * allocate_cg_links() allocates "count" cg_cgroup_link structures | 
 | 583 |  * and chains them on tmp through their cgrp_link_list fields. Returns 0 on | 
 | 584 |  * success or a negative error | 
 | 585 |  */ | 
 | 586 | static int allocate_cg_links(int count, struct list_head *tmp) | 
 | 587 | { | 
 | 588 | 	struct cg_cgroup_link *link; | 
 | 589 | 	int i; | 
 | 590 | 	INIT_LIST_HEAD(tmp); | 
 | 591 | 	for (i = 0; i < count; i++) { | 
 | 592 | 		link = kmalloc(sizeof(*link), GFP_KERNEL); | 
 | 593 | 		if (!link) { | 
 | 594 | 			free_cg_links(tmp); | 
 | 595 | 			return -ENOMEM; | 
 | 596 | 		} | 
 | 597 | 		list_add(&link->cgrp_link_list, tmp); | 
 | 598 | 	} | 
 | 599 | 	return 0; | 
 | 600 | } | 
 | 601 |  | 
| Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 602 | /** | 
 | 603 |  * link_css_set - a helper function to link a css_set to a cgroup | 
 | 604 |  * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links() | 
 | 605 |  * @cg: the css_set to be linked | 
 | 606 |  * @cgrp: the destination cgroup | 
 | 607 |  */ | 
 | 608 | static void link_css_set(struct list_head *tmp_cg_links, | 
 | 609 | 			 struct css_set *cg, struct cgroup *cgrp) | 
 | 610 | { | 
 | 611 | 	struct cg_cgroup_link *link; | 
 | 612 |  | 
 | 613 | 	BUG_ON(list_empty(tmp_cg_links)); | 
 | 614 | 	link = list_first_entry(tmp_cg_links, struct cg_cgroup_link, | 
 | 615 | 				cgrp_link_list); | 
 | 616 | 	link->cg = cg; | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 617 | 	link->cgrp = cgrp; | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 618 | 	atomic_inc(&cgrp->count); | 
| Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 619 | 	list_move(&link->cgrp_link_list, &cgrp->css_sets); | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 620 | 	/* | 
 | 621 | 	 * Always add links to the tail of the list so that the list | 
 | 622 | 	 * is sorted by order of hierarchy creation | 
 | 623 | 	 */ | 
 | 624 | 	list_add_tail(&link->cg_link_list, &cg->cg_links); | 
| Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 625 | } | 
 | 626 |  | 
| Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 627 | /* | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 628 |  * find_css_set() takes an existing cgroup group and a | 
 | 629 |  * cgroup object, and returns a css_set object that's | 
 | 630 |  * equivalent to the old group, but with the given cgroup | 
 | 631 |  * substituted into the appropriate hierarchy. Must be called with | 
 | 632 |  * cgroup_mutex held | 
 | 633 |  */ | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 634 | static struct css_set *find_css_set( | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 635 | 	struct css_set *oldcg, struct cgroup *cgrp) | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 636 | { | 
 | 637 | 	struct css_set *res; | 
 | 638 | 	struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT]; | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 639 |  | 
 | 640 | 	struct list_head tmp_cg_links; | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 641 |  | 
| Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 642 | 	struct hlist_head *hhead; | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 643 | 	struct cg_cgroup_link *link; | 
| Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 644 |  | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 645 | 	/* First see if we already have a cgroup group that matches | 
 | 646 | 	 * the desired set */ | 
| Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 647 | 	read_lock(&css_set_lock); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 648 | 	res = find_existing_css_set(oldcg, cgrp, template); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 649 | 	if (res) | 
 | 650 | 		get_css_set(res); | 
| Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 651 | 	read_unlock(&css_set_lock); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 652 |  | 
 | 653 | 	if (res) | 
 | 654 | 		return res; | 
 | 655 |  | 
 | 656 | 	res = kmalloc(sizeof(*res), GFP_KERNEL); | 
 | 657 | 	if (!res) | 
 | 658 | 		return NULL; | 
 | 659 |  | 
 | 660 | 	/* Allocate all the cg_cgroup_link objects that we'll need */ | 
 | 661 | 	if (allocate_cg_links(root_count, &tmp_cg_links) < 0) { | 
 | 662 | 		kfree(res); | 
 | 663 | 		return NULL; | 
 | 664 | 	} | 
 | 665 |  | 
| Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 666 | 	atomic_set(&res->refcount, 1); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 667 | 	INIT_LIST_HEAD(&res->cg_links); | 
 | 668 | 	INIT_LIST_HEAD(&res->tasks); | 
| Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 669 | 	INIT_HLIST_NODE(&res->hlist); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 670 |  | 
 | 671 | 	/* Copy the set of subsystem state objects generated in | 
 | 672 | 	 * find_existing_css_set() */ | 
 | 673 | 	memcpy(res->subsys, template, sizeof(res->subsys)); | 
 | 674 |  | 
 | 675 | 	write_lock(&css_set_lock); | 
 | 676 | 	/* Add reference counts and links from the new css_set. */ | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 677 | 	list_for_each_entry(link, &oldcg->cg_links, cg_link_list) { | 
 | 678 | 		struct cgroup *c = link->cgrp; | 
 | 679 | 		if (c->root == cgrp->root) | 
 | 680 | 			c = cgrp; | 
 | 681 | 		link_css_set(&tmp_cg_links, res, c); | 
 | 682 | 	} | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 683 |  | 
 | 684 | 	BUG_ON(!list_empty(&tmp_cg_links)); | 
 | 685 |  | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 686 | 	css_set_count++; | 
| Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 687 |  | 
 | 688 | 	/* Add this cgroup group to the hash table */ | 
 | 689 | 	hhead = css_set_hash(res->subsys); | 
 | 690 | 	hlist_add_head(&res->hlist, hhead); | 
 | 691 |  | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 692 | 	write_unlock(&css_set_lock); | 
 | 693 |  | 
 | 694 | 	return res; | 
| Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 695 | } | 
 | 696 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 697 | /* | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 698 |  * Return the cgroup for "task" from the given hierarchy. Must be | 
 | 699 |  * called with cgroup_mutex held. | 
 | 700 |  */ | 
 | 701 | static struct cgroup *task_cgroup_from_root(struct task_struct *task, | 
 | 702 | 					    struct cgroupfs_root *root) | 
 | 703 | { | 
 | 704 | 	struct css_set *css; | 
 | 705 | 	struct cgroup *res = NULL; | 
 | 706 |  | 
 | 707 | 	BUG_ON(!mutex_is_locked(&cgroup_mutex)); | 
 | 708 | 	read_lock(&css_set_lock); | 
 | 709 | 	/* | 
 | 710 | 	 * No need to lock the task - since we hold cgroup_mutex the | 
 | 711 | 	 * task can't change groups, so the only thing that can happen | 
 | 712 | 	 * is that it exits and its css is set back to init_css_set. | 
 | 713 | 	 */ | 
 | 714 | 	css = task->cgroups; | 
 | 715 | 	if (css == &init_css_set) { | 
 | 716 | 		res = &root->top_cgroup; | 
 | 717 | 	} else { | 
 | 718 | 		struct cg_cgroup_link *link; | 
 | 719 | 		list_for_each_entry(link, &css->cg_links, cg_link_list) { | 
 | 720 | 			struct cgroup *c = link->cgrp; | 
 | 721 | 			if (c->root == root) { | 
 | 722 | 				res = c; | 
 | 723 | 				break; | 
 | 724 | 			} | 
 | 725 | 		} | 
 | 726 | 	} | 
 | 727 | 	read_unlock(&css_set_lock); | 
 | 728 | 	BUG_ON(!res); | 
 | 729 | 	return res; | 
 | 730 | } | 
 | 731 |  | 
 | 732 | /* | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 733 |  * There is one global cgroup mutex. We also require taking | 
 | 734 |  * task_lock() when dereferencing a task's cgroup subsys pointers. | 
 | 735 |  * See "The task_lock() exception", at the end of this comment. | 
 | 736 |  * | 
 | 737 |  * A task must hold cgroup_mutex to modify cgroups. | 
 | 738 |  * | 
 | 739 |  * Any task can increment and decrement the count field without lock. | 
 | 740 |  * So in general, code holding cgroup_mutex can't rely on the count | 
 | 741 |  * field not changing.  However, if the count goes to zero, then only | 
| Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 742 |  * cgroup_attach_task() can increment it again.  Because a count of zero | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 743 |  * means that no tasks are currently attached, therefore there is no | 
 | 744 |  * way a task attached to that cgroup can fork (the other way to | 
 | 745 |  * increment the count).  So code holding cgroup_mutex can safely | 
 | 746 |  * assume that if the count is zero, it will stay zero. Similarly, if | 
 | 747 |  * a task holds cgroup_mutex on a cgroup with zero count, it | 
 | 748 |  * knows that the cgroup won't be removed, as cgroup_rmdir() | 
 | 749 |  * needs that mutex. | 
 | 750 |  * | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 751 |  * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't | 
 | 752 |  * (usually) take cgroup_mutex.  These are the two most performance | 
 | 753 |  * critical pieces of code here.  The exception occurs on cgroup_exit(), | 
 | 754 |  * when a task in a notify_on_release cgroup exits.  Then cgroup_mutex | 
 | 755 |  * is taken, and if the cgroup count is zero, a usermode call made | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 756 |  * to the release agent with the name of the cgroup (path relative to | 
 | 757 |  * the root of cgroup file system) as the argument. | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 758 |  * | 
 | 759 |  * A cgroup can only be deleted if both its 'count' of using tasks | 
 | 760 |  * is zero, and its list of 'children' cgroups is empty.  Since all | 
 | 761 |  * tasks in the system use _some_ cgroup, and since there is always at | 
 | 762 |  * least one task in the system (init, pid == 1), therefore, top_cgroup | 
 | 763 |  * always has either children cgroups and/or using tasks.  So we don't | 
 | 764 |  * need a special hack to ensure that top_cgroup cannot be deleted. | 
 | 765 |  * | 
 | 766 |  *	The task_lock() exception | 
 | 767 |  * | 
 | 768 |  * The need for this exception arises from the action of | 
| Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 769 |  * cgroup_attach_task(), which overwrites one tasks cgroup pointer with | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 770 |  * another.  It does so using cgroup_mutex, however there are | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 771 |  * several performance critical places that need to reference | 
| Colin Cross | befae2f | 2010-11-23 21:37:04 -0800 | [diff] [blame] | 772 |  * task->cgroups without the expense of grabbing a system global | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 773 |  * mutex.  Therefore except as noted below, when dereferencing or, as | 
| Colin Cross | befae2f | 2010-11-23 21:37:04 -0800 | [diff] [blame] | 774 |  * in cgroup_attach_task(), modifying a task's cgroups pointer we use | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 775 |  * task_lock(), which acts on a spinlock (task->alloc_lock) already in | 
 | 776 |  * the task_struct routinely used for such matters. | 
 | 777 |  * | 
 | 778 |  * P.S.  One more locking exception.  RCU is used to guard the | 
| Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 779 |  * update of a tasks cgroup pointer by cgroup_attach_task() | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 780 |  */ | 
 | 781 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 782 | /** | 
 | 783 |  * cgroup_lock - lock out any changes to cgroup structures | 
 | 784 |  * | 
 | 785 |  */ | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 786 | void cgroup_lock(void) | 
 | 787 | { | 
 | 788 | 	mutex_lock(&cgroup_mutex); | 
 | 789 | } | 
| Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 790 | EXPORT_SYMBOL_GPL(cgroup_lock); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 791 |  | 
 | 792 | /** | 
 | 793 |  * cgroup_unlock - release lock on cgroup changes | 
 | 794 |  * | 
 | 795 |  * Undo the lock taken in a previous cgroup_lock() call. | 
 | 796 |  */ | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 797 | void cgroup_unlock(void) | 
 | 798 | { | 
 | 799 | 	mutex_unlock(&cgroup_mutex); | 
 | 800 | } | 
| Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 801 | EXPORT_SYMBOL_GPL(cgroup_unlock); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 802 |  | 
 | 803 | /* | 
 | 804 |  * A couple of forward declarations required, due to cyclic reference loop: | 
 | 805 |  * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir -> | 
 | 806 |  * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations | 
 | 807 |  * -> cgroup_mkdir. | 
 | 808 |  */ | 
 | 809 |  | 
| Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 810 | static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode); | 
| Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 811 | static struct dentry *cgroup_lookup(struct inode *, struct dentry *, struct nameidata *); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 812 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 813 | static int cgroup_populate_dir(struct cgroup *cgrp); | 
| Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 814 | static const struct inode_operations cgroup_dir_inode_operations; | 
| Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 815 | static const struct file_operations proc_cgroupstats_operations; | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 816 |  | 
 | 817 | static struct backing_dev_info cgroup_backing_dev_info = { | 
| Jens Axboe | d993831 | 2009-06-12 14:45:52 +0200 | [diff] [blame] | 818 | 	.name		= "cgroup", | 
| Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 819 | 	.capabilities	= BDI_CAP_NO_ACCT_AND_WRITEBACK, | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 820 | }; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 821 |  | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 822 | static int alloc_css_id(struct cgroup_subsys *ss, | 
 | 823 | 			struct cgroup *parent, struct cgroup *child); | 
 | 824 |  | 
| Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 825 | static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 826 | { | 
 | 827 | 	struct inode *inode = new_inode(sb); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 828 |  | 
 | 829 | 	if (inode) { | 
| Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 830 | 		inode->i_ino = get_next_ino(); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 831 | 		inode->i_mode = mode; | 
| David Howells | 76aac0e | 2008-11-14 10:39:12 +1100 | [diff] [blame] | 832 | 		inode->i_uid = current_fsuid(); | 
 | 833 | 		inode->i_gid = current_fsgid(); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 834 | 		inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; | 
 | 835 | 		inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info; | 
 | 836 | 	} | 
 | 837 | 	return inode; | 
 | 838 | } | 
 | 839 |  | 
| KAMEZAWA Hiroyuki | 4fca88c | 2008-02-07 00:14:27 -0800 | [diff] [blame] | 840 | /* | 
 | 841 |  * Call subsys's pre_destroy handler. | 
 | 842 |  * This is called before css refcnt check. | 
 | 843 |  */ | 
| KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 844 | static int cgroup_call_pre_destroy(struct cgroup *cgrp) | 
| KAMEZAWA Hiroyuki | 4fca88c | 2008-02-07 00:14:27 -0800 | [diff] [blame] | 845 | { | 
 | 846 | 	struct cgroup_subsys *ss; | 
| KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 847 | 	int ret = 0; | 
 | 848 |  | 
| KAMEZAWA Hiroyuki | 4fca88c | 2008-02-07 00:14:27 -0800 | [diff] [blame] | 849 | 	for_each_subsys(cgrp->root, ss) | 
| KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 850 | 		if (ss->pre_destroy) { | 
| Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 851 | 			ret = ss->pre_destroy(cgrp); | 
| KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 852 | 			if (ret) | 
| Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 853 | 				break; | 
| KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 854 | 		} | 
| Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 855 |  | 
| KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 856 | 	return ret; | 
| KAMEZAWA Hiroyuki | 4fca88c | 2008-02-07 00:14:27 -0800 | [diff] [blame] | 857 | } | 
 | 858 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 859 | static void cgroup_diput(struct dentry *dentry, struct inode *inode) | 
 | 860 | { | 
 | 861 | 	/* is dentry a directory ? if so, kfree() associated cgroup */ | 
 | 862 | 	if (S_ISDIR(inode->i_mode)) { | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 863 | 		struct cgroup *cgrp = dentry->d_fsdata; | 
| Paul Menage | 8dc4f3e | 2008-02-07 00:13:45 -0800 | [diff] [blame] | 864 | 		struct cgroup_subsys *ss; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 865 | 		BUG_ON(!(cgroup_is_removed(cgrp))); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 866 | 		/* It's possible for external users to be holding css | 
 | 867 | 		 * reference counts on a cgroup; css_put() needs to | 
 | 868 | 		 * be able to access the cgroup after decrementing | 
 | 869 | 		 * the reference count in order to know if it needs to | 
 | 870 | 		 * queue the cgroup to be handled by the release | 
 | 871 | 		 * agent */ | 
 | 872 | 		synchronize_rcu(); | 
| Paul Menage | 8dc4f3e | 2008-02-07 00:13:45 -0800 | [diff] [blame] | 873 |  | 
 | 874 | 		mutex_lock(&cgroup_mutex); | 
 | 875 | 		/* | 
 | 876 | 		 * Release the subsystem state objects. | 
 | 877 | 		 */ | 
| Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 878 | 		for_each_subsys(cgrp->root, ss) | 
| Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 879 | 			ss->destroy(cgrp); | 
| Paul Menage | 8dc4f3e | 2008-02-07 00:13:45 -0800 | [diff] [blame] | 880 |  | 
 | 881 | 		cgrp->root->number_of_cgroups--; | 
 | 882 | 		mutex_unlock(&cgroup_mutex); | 
 | 883 |  | 
| Paul Menage | a47295e | 2009-01-07 18:07:44 -0800 | [diff] [blame] | 884 | 		/* | 
 | 885 | 		 * Drop the active superblock reference that we took when we | 
 | 886 | 		 * created the cgroup | 
 | 887 | 		 */ | 
| Paul Menage | 8dc4f3e | 2008-02-07 00:13:45 -0800 | [diff] [blame] | 888 | 		deactivate_super(cgrp->root->sb); | 
 | 889 |  | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 890 | 		/* | 
 | 891 | 		 * if we're getting rid of the cgroup, refcount should ensure | 
 | 892 | 		 * that there are no pidlists left. | 
 | 893 | 		 */ | 
 | 894 | 		BUG_ON(!list_empty(&cgrp->pidlists)); | 
 | 895 |  | 
| Lai Jiangshan | f2da1c4 | 2011-03-15 17:55:16 +0800 | [diff] [blame] | 896 | 		kfree_rcu(cgrp, rcu_head); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 897 | 	} | 
 | 898 | 	iput(inode); | 
 | 899 | } | 
 | 900 |  | 
| Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 901 | static int cgroup_delete(const struct dentry *d) | 
 | 902 | { | 
 | 903 | 	return 1; | 
 | 904 | } | 
 | 905 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 906 | static void remove_dir(struct dentry *d) | 
 | 907 | { | 
 | 908 | 	struct dentry *parent = dget(d->d_parent); | 
 | 909 |  | 
 | 910 | 	d_delete(d); | 
 | 911 | 	simple_rmdir(parent->d_inode, d); | 
 | 912 | 	dput(parent); | 
 | 913 | } | 
 | 914 |  | 
 | 915 | static void cgroup_clear_directory(struct dentry *dentry) | 
 | 916 | { | 
 | 917 | 	struct list_head *node; | 
 | 918 |  | 
 | 919 | 	BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex)); | 
| Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 920 | 	spin_lock(&dentry->d_lock); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 921 | 	node = dentry->d_subdirs.next; | 
 | 922 | 	while (node != &dentry->d_subdirs) { | 
 | 923 | 		struct dentry *d = list_entry(node, struct dentry, d_u.d_child); | 
| Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 924 |  | 
 | 925 | 		spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 926 | 		list_del_init(node); | 
 | 927 | 		if (d->d_inode) { | 
 | 928 | 			/* This should never be called on a cgroup | 
 | 929 | 			 * directory with child cgroups */ | 
 | 930 | 			BUG_ON(d->d_inode->i_mode & S_IFDIR); | 
| Nick Piggin | dc0474b | 2011-01-07 17:49:43 +1100 | [diff] [blame] | 931 | 			dget_dlock(d); | 
| Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 932 | 			spin_unlock(&d->d_lock); | 
 | 933 | 			spin_unlock(&dentry->d_lock); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 934 | 			d_delete(d); | 
 | 935 | 			simple_unlink(dentry->d_inode, d); | 
 | 936 | 			dput(d); | 
| Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 937 | 			spin_lock(&dentry->d_lock); | 
 | 938 | 		} else | 
 | 939 | 			spin_unlock(&d->d_lock); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 940 | 		node = dentry->d_subdirs.next; | 
 | 941 | 	} | 
| Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 942 | 	spin_unlock(&dentry->d_lock); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 943 | } | 
 | 944 |  | 
 | 945 | /* | 
 | 946 |  * NOTE : the dentry must have been dget()'ed | 
 | 947 |  */ | 
 | 948 | static void cgroup_d_remove_dir(struct dentry *dentry) | 
 | 949 | { | 
| Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 950 | 	struct dentry *parent; | 
 | 951 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 952 | 	cgroup_clear_directory(dentry); | 
 | 953 |  | 
| Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 954 | 	parent = dentry->d_parent; | 
 | 955 | 	spin_lock(&parent->d_lock); | 
| Li Zefan | 3ec762a | 2011-01-14 11:34:34 +0800 | [diff] [blame] | 956 | 	spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 957 | 	list_del_init(&dentry->d_u.d_child); | 
| Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 958 | 	spin_unlock(&dentry->d_lock); | 
 | 959 | 	spin_unlock(&parent->d_lock); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 960 | 	remove_dir(dentry); | 
 | 961 | } | 
 | 962 |  | 
| KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 963 | /* | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 964 |  * Call with cgroup_mutex held. Drops reference counts on modules, including | 
 | 965 |  * any duplicate ones that parse_cgroupfs_options took. If this function | 
 | 966 |  * returns an error, no reference counts are touched. | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 967 |  */ | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 968 | static int rebind_subsystems(struct cgroupfs_root *root, | 
 | 969 | 			      unsigned long final_bits) | 
 | 970 | { | 
 | 971 | 	unsigned long added_bits, removed_bits; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 972 | 	struct cgroup *cgrp = &root->top_cgroup; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 973 | 	int i; | 
 | 974 |  | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 975 | 	BUG_ON(!mutex_is_locked(&cgroup_mutex)); | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 976 | 	BUG_ON(!mutex_is_locked(&cgroup_root_mutex)); | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 977 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 978 | 	removed_bits = root->actual_subsys_bits & ~final_bits; | 
 | 979 | 	added_bits = final_bits & ~root->actual_subsys_bits; | 
 | 980 | 	/* Check that any added subsystems are currently free */ | 
 | 981 | 	for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { | 
| Li Zefan | 8d53d55 | 2008-02-23 15:24:11 -0800 | [diff] [blame] | 982 | 		unsigned long bit = 1UL << i; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 983 | 		struct cgroup_subsys *ss = subsys[i]; | 
 | 984 | 		if (!(bit & added_bits)) | 
 | 985 | 			continue; | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 986 | 		/* | 
 | 987 | 		 * Nobody should tell us to do a subsys that doesn't exist: | 
 | 988 | 		 * parse_cgroupfs_options should catch that case and refcounts | 
 | 989 | 		 * ensure that subsystems won't disappear once selected. | 
 | 990 | 		 */ | 
 | 991 | 		BUG_ON(ss == NULL); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 992 | 		if (ss->root != &rootnode) { | 
 | 993 | 			/* Subsystem isn't free */ | 
 | 994 | 			return -EBUSY; | 
 | 995 | 		} | 
 | 996 | 	} | 
 | 997 |  | 
 | 998 | 	/* Currently we don't handle adding/removing subsystems when | 
 | 999 | 	 * any child cgroups exist. This is theoretically supportable | 
 | 1000 | 	 * but involves complex error handling, so it's being left until | 
 | 1001 | 	 * later */ | 
| Paul Menage | 307257c | 2008-12-15 13:54:22 -0800 | [diff] [blame] | 1002 | 	if (root->number_of_cgroups > 1) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1003 | 		return -EBUSY; | 
 | 1004 |  | 
 | 1005 | 	/* Process each subsystem */ | 
 | 1006 | 	for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { | 
 | 1007 | 		struct cgroup_subsys *ss = subsys[i]; | 
 | 1008 | 		unsigned long bit = 1UL << i; | 
 | 1009 | 		if (bit & added_bits) { | 
 | 1010 | 			/* We're binding this subsystem to this hierarchy */ | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1011 | 			BUG_ON(ss == NULL); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1012 | 			BUG_ON(cgrp->subsys[i]); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1013 | 			BUG_ON(!dummytop->subsys[i]); | 
 | 1014 | 			BUG_ON(dummytop->subsys[i]->cgroup != dummytop); | 
| Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 1015 | 			mutex_lock(&ss->hierarchy_mutex); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1016 | 			cgrp->subsys[i] = dummytop->subsys[i]; | 
 | 1017 | 			cgrp->subsys[i]->cgroup = cgrp; | 
| Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1018 | 			list_move(&ss->sibling, &root->subsys_list); | 
| Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 1019 | 			ss->root = root; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1020 | 			if (ss->bind) | 
| Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1021 | 				ss->bind(cgrp); | 
| Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 1022 | 			mutex_unlock(&ss->hierarchy_mutex); | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1023 | 			/* refcount was already taken, and we're keeping it */ | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1024 | 		} else if (bit & removed_bits) { | 
 | 1025 | 			/* We're removing this subsystem */ | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1026 | 			BUG_ON(ss == NULL); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1027 | 			BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]); | 
 | 1028 | 			BUG_ON(cgrp->subsys[i]->cgroup != cgrp); | 
| Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 1029 | 			mutex_lock(&ss->hierarchy_mutex); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1030 | 			if (ss->bind) | 
| Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1031 | 				ss->bind(dummytop); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1032 | 			dummytop->subsys[i]->cgroup = dummytop; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1033 | 			cgrp->subsys[i] = NULL; | 
| Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 1034 | 			subsys[i]->root = &rootnode; | 
| Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1035 | 			list_move(&ss->sibling, &rootnode.subsys_list); | 
| Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 1036 | 			mutex_unlock(&ss->hierarchy_mutex); | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1037 | 			/* subsystem is now free - drop reference on module */ | 
 | 1038 | 			module_put(ss->module); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1039 | 		} else if (bit & final_bits) { | 
 | 1040 | 			/* Subsystem state should already exist */ | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1041 | 			BUG_ON(ss == NULL); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1042 | 			BUG_ON(!cgrp->subsys[i]); | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1043 | 			/* | 
 | 1044 | 			 * a refcount was taken, but we already had one, so | 
 | 1045 | 			 * drop the extra reference. | 
 | 1046 | 			 */ | 
 | 1047 | 			module_put(ss->module); | 
 | 1048 | #ifdef CONFIG_MODULE_UNLOAD | 
 | 1049 | 			BUG_ON(ss->module && !module_refcount(ss->module)); | 
 | 1050 | #endif | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1051 | 		} else { | 
 | 1052 | 			/* Subsystem state shouldn't exist */ | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1053 | 			BUG_ON(cgrp->subsys[i]); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1054 | 		} | 
 | 1055 | 	} | 
 | 1056 | 	root->subsys_bits = root->actual_subsys_bits = final_bits; | 
 | 1057 | 	synchronize_rcu(); | 
 | 1058 |  | 
 | 1059 | 	return 0; | 
 | 1060 | } | 
 | 1061 |  | 
| Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1062 | static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1063 | { | 
| Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1064 | 	struct cgroupfs_root *root = dentry->d_sb->s_fs_info; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1065 | 	struct cgroup_subsys *ss; | 
 | 1066 |  | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1067 | 	mutex_lock(&cgroup_root_mutex); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1068 | 	for_each_subsys(root, ss) | 
 | 1069 | 		seq_printf(seq, ",%s", ss->name); | 
 | 1070 | 	if (test_bit(ROOT_NOPREFIX, &root->flags)) | 
 | 1071 | 		seq_puts(seq, ",noprefix"); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1072 | 	if (strlen(root->release_agent_path)) | 
 | 1073 | 		seq_printf(seq, ",release_agent=%s", root->release_agent_path); | 
| Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 1074 | 	if (clone_children(&root->top_cgroup)) | 
 | 1075 | 		seq_puts(seq, ",clone_children"); | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1076 | 	if (strlen(root->name)) | 
 | 1077 | 		seq_printf(seq, ",name=%s", root->name); | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1078 | 	mutex_unlock(&cgroup_root_mutex); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1079 | 	return 0; | 
 | 1080 | } | 
 | 1081 |  | 
 | 1082 | struct cgroup_sb_opts { | 
 | 1083 | 	unsigned long subsys_bits; | 
 | 1084 | 	unsigned long flags; | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1085 | 	char *release_agent; | 
| Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 1086 | 	bool clone_children; | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1087 | 	char *name; | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1088 | 	/* User explicitly requested empty subsystem */ | 
 | 1089 | 	bool none; | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1090 |  | 
 | 1091 | 	struct cgroupfs_root *new_root; | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1092 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1093 | }; | 
 | 1094 |  | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1095 | /* | 
 | 1096 |  * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1097 |  * with cgroup_mutex held to protect the subsys[] array. This function takes | 
 | 1098 |  * refcounts on subsystems to be used, unless it returns error, in which case | 
 | 1099 |  * no refcounts are taken. | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1100 |  */ | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1101 | static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1102 | { | 
| Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1103 | 	char *token, *o = data; | 
 | 1104 | 	bool all_ss = false, one_ss = false; | 
| Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1105 | 	unsigned long mask = (unsigned long)-1; | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1106 | 	int i; | 
 | 1107 | 	bool module_pin_failed = false; | 
| Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1108 |  | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1109 | 	BUG_ON(!mutex_is_locked(&cgroup_mutex)); | 
 | 1110 |  | 
| Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1111 | #ifdef CONFIG_CPUSETS | 
 | 1112 | 	mask = ~(1UL << cpuset_subsys_id); | 
 | 1113 | #endif | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1114 |  | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1115 | 	memset(opts, 0, sizeof(*opts)); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1116 |  | 
 | 1117 | 	while ((token = strsep(&o, ",")) != NULL) { | 
 | 1118 | 		if (!*token) | 
 | 1119 | 			return -EINVAL; | 
| Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1120 | 		if (!strcmp(token, "none")) { | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1121 | 			/* Explicitly have no subsystems */ | 
 | 1122 | 			opts->none = true; | 
| Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1123 | 			continue; | 
 | 1124 | 		} | 
 | 1125 | 		if (!strcmp(token, "all")) { | 
 | 1126 | 			/* Mutually exclusive option 'all' + subsystem name */ | 
 | 1127 | 			if (one_ss) | 
 | 1128 | 				return -EINVAL; | 
 | 1129 | 			all_ss = true; | 
 | 1130 | 			continue; | 
 | 1131 | 		} | 
 | 1132 | 		if (!strcmp(token, "noprefix")) { | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1133 | 			set_bit(ROOT_NOPREFIX, &opts->flags); | 
| Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1134 | 			continue; | 
 | 1135 | 		} | 
 | 1136 | 		if (!strcmp(token, "clone_children")) { | 
| Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 1137 | 			opts->clone_children = true; | 
| Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1138 | 			continue; | 
 | 1139 | 		} | 
 | 1140 | 		if (!strncmp(token, "release_agent=", 14)) { | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1141 | 			/* Specifying two release agents is forbidden */ | 
 | 1142 | 			if (opts->release_agent) | 
 | 1143 | 				return -EINVAL; | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1144 | 			opts->release_agent = | 
| Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1145 | 				kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1146 | 			if (!opts->release_agent) | 
 | 1147 | 				return -ENOMEM; | 
| Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1148 | 			continue; | 
 | 1149 | 		} | 
 | 1150 | 		if (!strncmp(token, "name=", 5)) { | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1151 | 			const char *name = token + 5; | 
 | 1152 | 			/* Can't specify an empty name */ | 
 | 1153 | 			if (!strlen(name)) | 
 | 1154 | 				return -EINVAL; | 
 | 1155 | 			/* Must match [\w.-]+ */ | 
 | 1156 | 			for (i = 0; i < strlen(name); i++) { | 
 | 1157 | 				char c = name[i]; | 
 | 1158 | 				if (isalnum(c)) | 
 | 1159 | 					continue; | 
 | 1160 | 				if ((c == '.') || (c == '-') || (c == '_')) | 
 | 1161 | 					continue; | 
 | 1162 | 				return -EINVAL; | 
 | 1163 | 			} | 
 | 1164 | 			/* Specifying two names is forbidden */ | 
 | 1165 | 			if (opts->name) | 
 | 1166 | 				return -EINVAL; | 
 | 1167 | 			opts->name = kstrndup(name, | 
| Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1168 | 					      MAX_CGROUP_ROOT_NAMELEN - 1, | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1169 | 					      GFP_KERNEL); | 
 | 1170 | 			if (!opts->name) | 
 | 1171 | 				return -ENOMEM; | 
| Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1172 |  | 
 | 1173 | 			continue; | 
 | 1174 | 		} | 
 | 1175 |  | 
 | 1176 | 		for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { | 
 | 1177 | 			struct cgroup_subsys *ss = subsys[i]; | 
 | 1178 | 			if (ss == NULL) | 
 | 1179 | 				continue; | 
 | 1180 | 			if (strcmp(token, ss->name)) | 
 | 1181 | 				continue; | 
 | 1182 | 			if (ss->disabled) | 
 | 1183 | 				continue; | 
 | 1184 |  | 
 | 1185 | 			/* Mutually exclusive option 'all' + subsystem name */ | 
 | 1186 | 			if (all_ss) | 
 | 1187 | 				return -EINVAL; | 
 | 1188 | 			set_bit(i, &opts->subsys_bits); | 
 | 1189 | 			one_ss = true; | 
 | 1190 |  | 
 | 1191 | 			break; | 
 | 1192 | 		} | 
 | 1193 | 		if (i == CGROUP_SUBSYS_COUNT) | 
 | 1194 | 			return -ENOENT; | 
 | 1195 | 	} | 
 | 1196 |  | 
 | 1197 | 	/* | 
 | 1198 | 	 * If the 'all' option was specified select all the subsystems, | 
| Li Zefan | 0d19ea8 | 2011-12-27 14:25:55 +0800 | [diff] [blame] | 1199 | 	 * otherwise if 'none', 'name=' and a subsystem name options | 
 | 1200 | 	 * were not specified, let's default to 'all' | 
| Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1201 | 	 */ | 
| Li Zefan | 0d19ea8 | 2011-12-27 14:25:55 +0800 | [diff] [blame] | 1202 | 	if (all_ss || (!one_ss && !opts->none && !opts->name)) { | 
| Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1203 | 		for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { | 
 | 1204 | 			struct cgroup_subsys *ss = subsys[i]; | 
 | 1205 | 			if (ss == NULL) | 
 | 1206 | 				continue; | 
 | 1207 | 			if (ss->disabled) | 
 | 1208 | 				continue; | 
 | 1209 | 			set_bit(i, &opts->subsys_bits); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1210 | 		} | 
 | 1211 | 	} | 
 | 1212 |  | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1213 | 	/* Consistency checks */ | 
 | 1214 |  | 
| Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1215 | 	/* | 
 | 1216 | 	 * Option noprefix was introduced just for backward compatibility | 
 | 1217 | 	 * with the old cpuset, so we allow noprefix only if mounting just | 
 | 1218 | 	 * the cpuset subsystem. | 
 | 1219 | 	 */ | 
 | 1220 | 	if (test_bit(ROOT_NOPREFIX, &opts->flags) && | 
 | 1221 | 	    (opts->subsys_bits & mask)) | 
 | 1222 | 		return -EINVAL; | 
 | 1223 |  | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1224 |  | 
 | 1225 | 	/* Can't specify "none" and some subsystems */ | 
 | 1226 | 	if (opts->subsys_bits && opts->none) | 
 | 1227 | 		return -EINVAL; | 
 | 1228 |  | 
 | 1229 | 	/* | 
 | 1230 | 	 * We either have to specify by name or by subsystems. (So all | 
 | 1231 | 	 * empty hierarchies must have a name). | 
 | 1232 | 	 */ | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1233 | 	if (!opts->subsys_bits && !opts->name) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1234 | 		return -EINVAL; | 
 | 1235 |  | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1236 | 	/* | 
 | 1237 | 	 * Grab references on all the modules we'll need, so the subsystems | 
 | 1238 | 	 * don't dance around before rebind_subsystems attaches them. This may | 
 | 1239 | 	 * take duplicate reference counts on a subsystem that's already used, | 
 | 1240 | 	 * but rebind_subsystems handles this case. | 
 | 1241 | 	 */ | 
 | 1242 | 	for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) { | 
 | 1243 | 		unsigned long bit = 1UL << i; | 
 | 1244 |  | 
 | 1245 | 		if (!(bit & opts->subsys_bits)) | 
 | 1246 | 			continue; | 
 | 1247 | 		if (!try_module_get(subsys[i]->module)) { | 
 | 1248 | 			module_pin_failed = true; | 
 | 1249 | 			break; | 
 | 1250 | 		} | 
 | 1251 | 	} | 
 | 1252 | 	if (module_pin_failed) { | 
 | 1253 | 		/* | 
 | 1254 | 		 * oops, one of the modules was going away. this means that we | 
 | 1255 | 		 * raced with a module_delete call, and to the user this is | 
 | 1256 | 		 * essentially a "subsystem doesn't exist" case. | 
 | 1257 | 		 */ | 
 | 1258 | 		for (i--; i >= CGROUP_BUILTIN_SUBSYS_COUNT; i--) { | 
 | 1259 | 			/* drop refcounts only on the ones we took */ | 
 | 1260 | 			unsigned long bit = 1UL << i; | 
 | 1261 |  | 
 | 1262 | 			if (!(bit & opts->subsys_bits)) | 
 | 1263 | 				continue; | 
 | 1264 | 			module_put(subsys[i]->module); | 
 | 1265 | 		} | 
 | 1266 | 		return -ENOENT; | 
 | 1267 | 	} | 
 | 1268 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1269 | 	return 0; | 
 | 1270 | } | 
 | 1271 |  | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1272 | static void drop_parsed_module_refcounts(unsigned long subsys_bits) | 
 | 1273 | { | 
 | 1274 | 	int i; | 
 | 1275 | 	for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) { | 
 | 1276 | 		unsigned long bit = 1UL << i; | 
 | 1277 |  | 
 | 1278 | 		if (!(bit & subsys_bits)) | 
 | 1279 | 			continue; | 
 | 1280 | 		module_put(subsys[i]->module); | 
 | 1281 | 	} | 
 | 1282 | } | 
 | 1283 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1284 | static int cgroup_remount(struct super_block *sb, int *flags, char *data) | 
 | 1285 | { | 
 | 1286 | 	int ret = 0; | 
 | 1287 | 	struct cgroupfs_root *root = sb->s_fs_info; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1288 | 	struct cgroup *cgrp = &root->top_cgroup; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1289 | 	struct cgroup_sb_opts opts; | 
 | 1290 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1291 | 	mutex_lock(&cgrp->dentry->d_inode->i_mutex); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1292 | 	mutex_lock(&cgroup_mutex); | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1293 | 	mutex_lock(&cgroup_root_mutex); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1294 |  | 
 | 1295 | 	/* See what subsystems are wanted */ | 
 | 1296 | 	ret = parse_cgroupfs_options(data, &opts); | 
 | 1297 | 	if (ret) | 
 | 1298 | 		goto out_unlock; | 
 | 1299 |  | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1300 | 	/* Don't allow flags or name to change at remount */ | 
 | 1301 | 	if (opts.flags != root->flags || | 
 | 1302 | 	    (opts.name && strcmp(opts.name, root->name))) { | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1303 | 		ret = -EINVAL; | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1304 | 		drop_parsed_module_refcounts(opts.subsys_bits); | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1305 | 		goto out_unlock; | 
 | 1306 | 	} | 
 | 1307 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1308 | 	ret = rebind_subsystems(root, opts.subsys_bits); | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1309 | 	if (ret) { | 
 | 1310 | 		drop_parsed_module_refcounts(opts.subsys_bits); | 
| Li Zefan | 0670e08 | 2009-04-02 16:57:30 -0700 | [diff] [blame] | 1311 | 		goto out_unlock; | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1312 | 	} | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1313 |  | 
 | 1314 | 	/* (re)populate subsystem files */ | 
| Li Zefan | 0670e08 | 2009-04-02 16:57:30 -0700 | [diff] [blame] | 1315 | 	cgroup_populate_dir(cgrp); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1316 |  | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1317 | 	if (opts.release_agent) | 
 | 1318 | 		strcpy(root->release_agent_path, opts.release_agent); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1319 |  out_unlock: | 
| Jesper Juhl | 66bdc9c | 2009-04-02 16:57:27 -0700 | [diff] [blame] | 1320 | 	kfree(opts.release_agent); | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1321 | 	kfree(opts.name); | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1322 | 	mutex_unlock(&cgroup_root_mutex); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1323 | 	mutex_unlock(&cgroup_mutex); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1324 | 	mutex_unlock(&cgrp->dentry->d_inode->i_mutex); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1325 | 	return ret; | 
 | 1326 | } | 
 | 1327 |  | 
| Alexey Dobriyan | b87221d | 2009-09-21 17:01:09 -0700 | [diff] [blame] | 1328 | static const struct super_operations cgroup_ops = { | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1329 | 	.statfs = simple_statfs, | 
 | 1330 | 	.drop_inode = generic_delete_inode, | 
 | 1331 | 	.show_options = cgroup_show_options, | 
 | 1332 | 	.remount_fs = cgroup_remount, | 
 | 1333 | }; | 
 | 1334 |  | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1335 | static void init_cgroup_housekeeping(struct cgroup *cgrp) | 
 | 1336 | { | 
 | 1337 | 	INIT_LIST_HEAD(&cgrp->sibling); | 
 | 1338 | 	INIT_LIST_HEAD(&cgrp->children); | 
 | 1339 | 	INIT_LIST_HEAD(&cgrp->css_sets); | 
 | 1340 | 	INIT_LIST_HEAD(&cgrp->release_list); | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1341 | 	INIT_LIST_HEAD(&cgrp->pidlists); | 
 | 1342 | 	mutex_init(&cgrp->pidlist_mutex); | 
| Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 1343 | 	INIT_LIST_HEAD(&cgrp->event_list); | 
 | 1344 | 	spin_lock_init(&cgrp->event_list_lock); | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1345 | } | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1346 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1347 | static void init_cgroup_root(struct cgroupfs_root *root) | 
 | 1348 | { | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1349 | 	struct cgroup *cgrp = &root->top_cgroup; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1350 | 	INIT_LIST_HEAD(&root->subsys_list); | 
 | 1351 | 	INIT_LIST_HEAD(&root->root_list); | 
 | 1352 | 	root->number_of_cgroups = 1; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1353 | 	cgrp->root = root; | 
 | 1354 | 	cgrp->top_cgroup = cgrp; | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1355 | 	init_cgroup_housekeeping(cgrp); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1356 | } | 
 | 1357 |  | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1358 | static bool init_root_id(struct cgroupfs_root *root) | 
 | 1359 | { | 
 | 1360 | 	int ret = 0; | 
 | 1361 |  | 
 | 1362 | 	do { | 
 | 1363 | 		if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL)) | 
 | 1364 | 			return false; | 
 | 1365 | 		spin_lock(&hierarchy_id_lock); | 
 | 1366 | 		/* Try to allocate the next unused ID */ | 
 | 1367 | 		ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id, | 
 | 1368 | 					&root->hierarchy_id); | 
 | 1369 | 		if (ret == -ENOSPC) | 
 | 1370 | 			/* Try again starting from 0 */ | 
 | 1371 | 			ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id); | 
 | 1372 | 		if (!ret) { | 
 | 1373 | 			next_hierarchy_id = root->hierarchy_id + 1; | 
 | 1374 | 		} else if (ret != -EAGAIN) { | 
 | 1375 | 			/* Can only get here if the 31-bit IDR is full ... */ | 
 | 1376 | 			BUG_ON(ret); | 
 | 1377 | 		} | 
 | 1378 | 		spin_unlock(&hierarchy_id_lock); | 
 | 1379 | 	} while (ret); | 
 | 1380 | 	return true; | 
 | 1381 | } | 
 | 1382 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1383 | static int cgroup_test_super(struct super_block *sb, void *data) | 
 | 1384 | { | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1385 | 	struct cgroup_sb_opts *opts = data; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1386 | 	struct cgroupfs_root *root = sb->s_fs_info; | 
 | 1387 |  | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1388 | 	/* If we asked for a name then it must match */ | 
 | 1389 | 	if (opts->name && strcmp(opts->name, root->name)) | 
 | 1390 | 		return 0; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1391 |  | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1392 | 	/* | 
 | 1393 | 	 * If we asked for subsystems (or explicitly for no | 
 | 1394 | 	 * subsystems) then they must match | 
 | 1395 | 	 */ | 
 | 1396 | 	if ((opts->subsys_bits || opts->none) | 
 | 1397 | 	    && (opts->subsys_bits != root->subsys_bits)) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1398 | 		return 0; | 
 | 1399 |  | 
 | 1400 | 	return 1; | 
 | 1401 | } | 
 | 1402 |  | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1403 | static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts) | 
 | 1404 | { | 
 | 1405 | 	struct cgroupfs_root *root; | 
 | 1406 |  | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1407 | 	if (!opts->subsys_bits && !opts->none) | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1408 | 		return NULL; | 
 | 1409 |  | 
 | 1410 | 	root = kzalloc(sizeof(*root), GFP_KERNEL); | 
 | 1411 | 	if (!root) | 
 | 1412 | 		return ERR_PTR(-ENOMEM); | 
 | 1413 |  | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1414 | 	if (!init_root_id(root)) { | 
 | 1415 | 		kfree(root); | 
 | 1416 | 		return ERR_PTR(-ENOMEM); | 
 | 1417 | 	} | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1418 | 	init_cgroup_root(root); | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1419 |  | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1420 | 	root->subsys_bits = opts->subsys_bits; | 
 | 1421 | 	root->flags = opts->flags; | 
 | 1422 | 	if (opts->release_agent) | 
 | 1423 | 		strcpy(root->release_agent_path, opts->release_agent); | 
 | 1424 | 	if (opts->name) | 
 | 1425 | 		strcpy(root->name, opts->name); | 
| Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 1426 | 	if (opts->clone_children) | 
 | 1427 | 		set_bit(CGRP_CLONE_CHILDREN, &root->top_cgroup.flags); | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1428 | 	return root; | 
 | 1429 | } | 
 | 1430 |  | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1431 | static void cgroup_drop_root(struct cgroupfs_root *root) | 
 | 1432 | { | 
 | 1433 | 	if (!root) | 
 | 1434 | 		return; | 
 | 1435 |  | 
 | 1436 | 	BUG_ON(!root->hierarchy_id); | 
 | 1437 | 	spin_lock(&hierarchy_id_lock); | 
 | 1438 | 	ida_remove(&hierarchy_ida, root->hierarchy_id); | 
 | 1439 | 	spin_unlock(&hierarchy_id_lock); | 
 | 1440 | 	kfree(root); | 
 | 1441 | } | 
 | 1442 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1443 | static int cgroup_set_super(struct super_block *sb, void *data) | 
 | 1444 | { | 
 | 1445 | 	int ret; | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1446 | 	struct cgroup_sb_opts *opts = data; | 
 | 1447 |  | 
 | 1448 | 	/* If we don't have a new root, we can't set up a new sb */ | 
 | 1449 | 	if (!opts->new_root) | 
 | 1450 | 		return -EINVAL; | 
 | 1451 |  | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1452 | 	BUG_ON(!opts->subsys_bits && !opts->none); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1453 |  | 
 | 1454 | 	ret = set_anon_super(sb, NULL); | 
 | 1455 | 	if (ret) | 
 | 1456 | 		return ret; | 
 | 1457 |  | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1458 | 	sb->s_fs_info = opts->new_root; | 
 | 1459 | 	opts->new_root->sb = sb; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1460 |  | 
 | 1461 | 	sb->s_blocksize = PAGE_CACHE_SIZE; | 
 | 1462 | 	sb->s_blocksize_bits = PAGE_CACHE_SHIFT; | 
 | 1463 | 	sb->s_magic = CGROUP_SUPER_MAGIC; | 
 | 1464 | 	sb->s_op = &cgroup_ops; | 
 | 1465 |  | 
 | 1466 | 	return 0; | 
 | 1467 | } | 
 | 1468 |  | 
 | 1469 | static int cgroup_get_rootdir(struct super_block *sb) | 
 | 1470 | { | 
| Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1471 | 	static const struct dentry_operations cgroup_dops = { | 
 | 1472 | 		.d_iput = cgroup_diput, | 
| Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 1473 | 		.d_delete = cgroup_delete, | 
| Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1474 | 	}; | 
 | 1475 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1476 | 	struct inode *inode = | 
 | 1477 | 		cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1478 |  | 
 | 1479 | 	if (!inode) | 
 | 1480 | 		return -ENOMEM; | 
 | 1481 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1482 | 	inode->i_fop = &simple_dir_operations; | 
 | 1483 | 	inode->i_op = &cgroup_dir_inode_operations; | 
 | 1484 | 	/* directories start off with i_nlink == 2 (for "." entry) */ | 
 | 1485 | 	inc_nlink(inode); | 
| Al Viro | 48fde70 | 2012-01-08 22:15:13 -0500 | [diff] [blame] | 1486 | 	sb->s_root = d_make_root(inode); | 
 | 1487 | 	if (!sb->s_root) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1488 | 		return -ENOMEM; | 
| Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1489 | 	/* for everything else we want ->d_op set */ | 
 | 1490 | 	sb->s_d_op = &cgroup_dops; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1491 | 	return 0; | 
 | 1492 | } | 
 | 1493 |  | 
| Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1494 | static struct dentry *cgroup_mount(struct file_system_type *fs_type, | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1495 | 			 int flags, const char *unused_dev_name, | 
| Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1496 | 			 void *data) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1497 | { | 
 | 1498 | 	struct cgroup_sb_opts opts; | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1499 | 	struct cgroupfs_root *root; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1500 | 	int ret = 0; | 
 | 1501 | 	struct super_block *sb; | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1502 | 	struct cgroupfs_root *new_root; | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1503 | 	struct inode *inode; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1504 |  | 
 | 1505 | 	/* First find the desired set of subsystems */ | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1506 | 	mutex_lock(&cgroup_mutex); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1507 | 	ret = parse_cgroupfs_options(data, &opts); | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1508 | 	mutex_unlock(&cgroup_mutex); | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1509 | 	if (ret) | 
 | 1510 | 		goto out_err; | 
 | 1511 |  | 
 | 1512 | 	/* | 
 | 1513 | 	 * Allocate a new cgroup root. We may not need it if we're | 
 | 1514 | 	 * reusing an existing hierarchy. | 
 | 1515 | 	 */ | 
 | 1516 | 	new_root = cgroup_root_from_opts(&opts); | 
 | 1517 | 	if (IS_ERR(new_root)) { | 
 | 1518 | 		ret = PTR_ERR(new_root); | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1519 | 		goto drop_modules; | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1520 | 	} | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1521 | 	opts.new_root = new_root; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1522 |  | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1523 | 	/* Locate an existing or new sb for this hierarchy */ | 
 | 1524 | 	sb = sget(fs_type, cgroup_test_super, cgroup_set_super, &opts); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1525 | 	if (IS_ERR(sb)) { | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1526 | 		ret = PTR_ERR(sb); | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1527 | 		cgroup_drop_root(opts.new_root); | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1528 | 		goto drop_modules; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1529 | 	} | 
 | 1530 |  | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1531 | 	root = sb->s_fs_info; | 
 | 1532 | 	BUG_ON(!root); | 
 | 1533 | 	if (root == opts.new_root) { | 
 | 1534 | 		/* We used the new root structure, so this is a new hierarchy */ | 
 | 1535 | 		struct list_head tmp_cg_links; | 
| Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1536 | 		struct cgroup *root_cgrp = &root->top_cgroup; | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1537 | 		struct cgroupfs_root *existing_root; | 
| eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1538 | 		const struct cred *cred; | 
| Li Zefan | 28fd5df | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 1539 | 		int i; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1540 |  | 
 | 1541 | 		BUG_ON(sb->s_root != NULL); | 
 | 1542 |  | 
 | 1543 | 		ret = cgroup_get_rootdir(sb); | 
 | 1544 | 		if (ret) | 
 | 1545 | 			goto drop_new_super; | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1546 | 		inode = sb->s_root->d_inode; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1547 |  | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1548 | 		mutex_lock(&inode->i_mutex); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1549 | 		mutex_lock(&cgroup_mutex); | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1550 | 		mutex_lock(&cgroup_root_mutex); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1551 |  | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1552 | 		/* Check for name clashes with existing mounts */ | 
 | 1553 | 		ret = -EBUSY; | 
 | 1554 | 		if (strlen(root->name)) | 
 | 1555 | 			for_each_active_root(existing_root) | 
 | 1556 | 				if (!strcmp(existing_root->name, root->name)) | 
 | 1557 | 					goto unlock_drop; | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1558 |  | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1559 | 		/* | 
 | 1560 | 		 * We're accessing css_set_count without locking | 
 | 1561 | 		 * css_set_lock here, but that's OK - it can only be | 
 | 1562 | 		 * increased by someone holding cgroup_lock, and | 
 | 1563 | 		 * that's us. The worst that can happen is that we | 
 | 1564 | 		 * have some link structures left over | 
 | 1565 | 		 */ | 
 | 1566 | 		ret = allocate_cg_links(css_set_count, &tmp_cg_links); | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1567 | 		if (ret) | 
 | 1568 | 			goto unlock_drop; | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1569 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1570 | 		ret = rebind_subsystems(root, root->subsys_bits); | 
 | 1571 | 		if (ret == -EBUSY) { | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1572 | 			free_cg_links(&tmp_cg_links); | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1573 | 			goto unlock_drop; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1574 | 		} | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1575 | 		/* | 
 | 1576 | 		 * There must be no failure case after here, since rebinding | 
 | 1577 | 		 * takes care of subsystems' refcounts, which are explicitly | 
 | 1578 | 		 * dropped in the failure exit path. | 
 | 1579 | 		 */ | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1580 |  | 
 | 1581 | 		/* EBUSY should be the only error here */ | 
 | 1582 | 		BUG_ON(ret); | 
 | 1583 |  | 
 | 1584 | 		list_add(&root->root_list, &roots); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1585 | 		root_count++; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1586 |  | 
| Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1587 | 		sb->s_root->d_fsdata = root_cgrp; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1588 | 		root->top_cgroup.dentry = sb->s_root; | 
 | 1589 |  | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1590 | 		/* Link the top cgroup in this hierarchy into all | 
 | 1591 | 		 * the css_set objects */ | 
 | 1592 | 		write_lock(&css_set_lock); | 
| Li Zefan | 28fd5df | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 1593 | 		for (i = 0; i < CSS_SET_TABLE_SIZE; i++) { | 
 | 1594 | 			struct hlist_head *hhead = &css_set_table[i]; | 
 | 1595 | 			struct hlist_node *node; | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1596 | 			struct css_set *cg; | 
| Li Zefan | 28fd5df | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 1597 |  | 
| Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1598 | 			hlist_for_each_entry(cg, node, hhead, hlist) | 
 | 1599 | 				link_css_set(&tmp_cg_links, cg, root_cgrp); | 
| Li Zefan | 28fd5df | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 1600 | 		} | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1601 | 		write_unlock(&css_set_lock); | 
 | 1602 |  | 
 | 1603 | 		free_cg_links(&tmp_cg_links); | 
 | 1604 |  | 
| Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1605 | 		BUG_ON(!list_empty(&root_cgrp->sibling)); | 
 | 1606 | 		BUG_ON(!list_empty(&root_cgrp->children)); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1607 | 		BUG_ON(root->number_of_cgroups != 1); | 
 | 1608 |  | 
| eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1609 | 		cred = override_creds(&init_cred); | 
| Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1610 | 		cgroup_populate_dir(root_cgrp); | 
| eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1611 | 		revert_creds(cred); | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1612 | 		mutex_unlock(&cgroup_root_mutex); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1613 | 		mutex_unlock(&cgroup_mutex); | 
| Xiaotian Feng | 34f77a9 | 2009-09-23 15:56:18 -0700 | [diff] [blame] | 1614 | 		mutex_unlock(&inode->i_mutex); | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1615 | 	} else { | 
 | 1616 | 		/* | 
 | 1617 | 		 * We re-used an existing hierarchy - the new root (if | 
 | 1618 | 		 * any) is not needed | 
 | 1619 | 		 */ | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1620 | 		cgroup_drop_root(opts.new_root); | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1621 | 		/* no subsys rebinding, so refcounts don't change */ | 
 | 1622 | 		drop_parsed_module_refcounts(opts.subsys_bits); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1623 | 	} | 
 | 1624 |  | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1625 | 	kfree(opts.release_agent); | 
 | 1626 | 	kfree(opts.name); | 
| Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1627 | 	return dget(sb->s_root); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1628 |  | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1629 |  unlock_drop: | 
 | 1630 | 	mutex_unlock(&cgroup_root_mutex); | 
 | 1631 | 	mutex_unlock(&cgroup_mutex); | 
 | 1632 | 	mutex_unlock(&inode->i_mutex); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1633 |  drop_new_super: | 
| Al Viro | 6f5bbff | 2009-05-06 01:34:22 -0400 | [diff] [blame] | 1634 | 	deactivate_locked_super(sb); | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1635 |  drop_modules: | 
 | 1636 | 	drop_parsed_module_refcounts(opts.subsys_bits); | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1637 |  out_err: | 
 | 1638 | 	kfree(opts.release_agent); | 
 | 1639 | 	kfree(opts.name); | 
| Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1640 | 	return ERR_PTR(ret); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1641 | } | 
 | 1642 |  | 
 | 1643 | static void cgroup_kill_sb(struct super_block *sb) { | 
 | 1644 | 	struct cgroupfs_root *root = sb->s_fs_info; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1645 | 	struct cgroup *cgrp = &root->top_cgroup; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1646 | 	int ret; | 
| KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1647 | 	struct cg_cgroup_link *link; | 
 | 1648 | 	struct cg_cgroup_link *saved_link; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1649 |  | 
 | 1650 | 	BUG_ON(!root); | 
 | 1651 |  | 
 | 1652 | 	BUG_ON(root->number_of_cgroups != 1); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1653 | 	BUG_ON(!list_empty(&cgrp->children)); | 
 | 1654 | 	BUG_ON(!list_empty(&cgrp->sibling)); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1655 |  | 
 | 1656 | 	mutex_lock(&cgroup_mutex); | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1657 | 	mutex_lock(&cgroup_root_mutex); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1658 |  | 
 | 1659 | 	/* Rebind all subsystems back to the default hierarchy */ | 
 | 1660 | 	ret = rebind_subsystems(root, 0); | 
 | 1661 | 	/* Shouldn't be able to fail ... */ | 
 | 1662 | 	BUG_ON(ret); | 
 | 1663 |  | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1664 | 	/* | 
 | 1665 | 	 * Release all the links from css_sets to this hierarchy's | 
 | 1666 | 	 * root cgroup | 
 | 1667 | 	 */ | 
 | 1668 | 	write_lock(&css_set_lock); | 
| KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1669 |  | 
 | 1670 | 	list_for_each_entry_safe(link, saved_link, &cgrp->css_sets, | 
 | 1671 | 				 cgrp_link_list) { | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1672 | 		list_del(&link->cg_link_list); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1673 | 		list_del(&link->cgrp_link_list); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1674 | 		kfree(link); | 
 | 1675 | 	} | 
 | 1676 | 	write_unlock(&css_set_lock); | 
 | 1677 |  | 
| Paul Menage | 839ec54 | 2009-01-29 14:25:22 -0800 | [diff] [blame] | 1678 | 	if (!list_empty(&root->root_list)) { | 
 | 1679 | 		list_del(&root->root_list); | 
 | 1680 | 		root_count--; | 
 | 1681 | 	} | 
| Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 1682 |  | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1683 | 	mutex_unlock(&cgroup_root_mutex); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1684 | 	mutex_unlock(&cgroup_mutex); | 
 | 1685 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1686 | 	kill_litter_super(sb); | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1687 | 	cgroup_drop_root(root); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1688 | } | 
 | 1689 |  | 
 | 1690 | static struct file_system_type cgroup_fs_type = { | 
 | 1691 | 	.name = "cgroup", | 
| Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1692 | 	.mount = cgroup_mount, | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1693 | 	.kill_sb = cgroup_kill_sb, | 
 | 1694 | }; | 
 | 1695 |  | 
| Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 1696 | static struct kobject *cgroup_kobj; | 
 | 1697 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1698 | static inline struct cgroup *__d_cgrp(struct dentry *dentry) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1699 | { | 
 | 1700 | 	return dentry->d_fsdata; | 
 | 1701 | } | 
 | 1702 |  | 
 | 1703 | static inline struct cftype *__d_cft(struct dentry *dentry) | 
 | 1704 | { | 
 | 1705 | 	return dentry->d_fsdata; | 
 | 1706 | } | 
 | 1707 |  | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1708 | /** | 
 | 1709 |  * cgroup_path - generate the path of a cgroup | 
 | 1710 |  * @cgrp: the cgroup in question | 
 | 1711 |  * @buf: the buffer to write the path into | 
 | 1712 |  * @buflen: the length of the buffer | 
 | 1713 |  * | 
| Paul Menage | a47295e | 2009-01-07 18:07:44 -0800 | [diff] [blame] | 1714 |  * Called with cgroup_mutex held or else with an RCU-protected cgroup | 
 | 1715 |  * reference.  Writes path of cgroup into buf.  Returns 0 on success, | 
 | 1716 |  * -errno on error. | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1717 |  */ | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1718 | int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1719 | { | 
 | 1720 | 	char *start; | 
| Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1721 | 	struct dentry *dentry = rcu_dereference_check(cgrp->dentry, | 
| Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1722 | 						      cgroup_lock_is_held()); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1723 |  | 
| Paul Menage | a47295e | 2009-01-07 18:07:44 -0800 | [diff] [blame] | 1724 | 	if (!dentry || cgrp == dummytop) { | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1725 | 		/* | 
 | 1726 | 		 * Inactive subsystems have no dentry for their root | 
 | 1727 | 		 * cgroup | 
 | 1728 | 		 */ | 
 | 1729 | 		strcpy(buf, "/"); | 
 | 1730 | 		return 0; | 
 | 1731 | 	} | 
 | 1732 |  | 
 | 1733 | 	start = buf + buflen; | 
 | 1734 |  | 
 | 1735 | 	*--start = '\0'; | 
 | 1736 | 	for (;;) { | 
| Paul Menage | a47295e | 2009-01-07 18:07:44 -0800 | [diff] [blame] | 1737 | 		int len = dentry->d_name.len; | 
| Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1738 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1739 | 		if ((start -= len) < buf) | 
 | 1740 | 			return -ENAMETOOLONG; | 
| Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1741 | 		memcpy(start, dentry->d_name.name, len); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1742 | 		cgrp = cgrp->parent; | 
 | 1743 | 		if (!cgrp) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1744 | 			break; | 
| Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1745 |  | 
 | 1746 | 		dentry = rcu_dereference_check(cgrp->dentry, | 
| Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1747 | 					       cgroup_lock_is_held()); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1748 | 		if (!cgrp->parent) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1749 | 			continue; | 
 | 1750 | 		if (--start < buf) | 
 | 1751 | 			return -ENAMETOOLONG; | 
 | 1752 | 		*start = '/'; | 
 | 1753 | 	} | 
 | 1754 | 	memmove(buf, start, buf + buflen - start); | 
 | 1755 | 	return 0; | 
 | 1756 | } | 
| Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 1757 | EXPORT_SYMBOL_GPL(cgroup_path); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1758 |  | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1759 | /* | 
| Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1760 |  * Control Group taskset | 
 | 1761 |  */ | 
| Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1762 | struct task_and_cgroup { | 
 | 1763 | 	struct task_struct	*task; | 
 | 1764 | 	struct cgroup		*cgrp; | 
| Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1765 | 	struct css_set		*cg; | 
| Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1766 | }; | 
 | 1767 |  | 
| Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1768 | struct cgroup_taskset { | 
 | 1769 | 	struct task_and_cgroup	single; | 
 | 1770 | 	struct flex_array	*tc_array; | 
 | 1771 | 	int			tc_array_len; | 
 | 1772 | 	int			idx; | 
 | 1773 | 	struct cgroup		*cur_cgrp; | 
 | 1774 | }; | 
 | 1775 |  | 
 | 1776 | /** | 
 | 1777 |  * cgroup_taskset_first - reset taskset and return the first task | 
 | 1778 |  * @tset: taskset of interest | 
 | 1779 |  * | 
 | 1780 |  * @tset iteration is initialized and the first task is returned. | 
 | 1781 |  */ | 
 | 1782 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset) | 
 | 1783 | { | 
 | 1784 | 	if (tset->tc_array) { | 
 | 1785 | 		tset->idx = 0; | 
 | 1786 | 		return cgroup_taskset_next(tset); | 
 | 1787 | 	} else { | 
 | 1788 | 		tset->cur_cgrp = tset->single.cgrp; | 
 | 1789 | 		return tset->single.task; | 
 | 1790 | 	} | 
 | 1791 | } | 
 | 1792 | EXPORT_SYMBOL_GPL(cgroup_taskset_first); | 
 | 1793 |  | 
 | 1794 | /** | 
 | 1795 |  * cgroup_taskset_next - iterate to the next task in taskset | 
 | 1796 |  * @tset: taskset of interest | 
 | 1797 |  * | 
 | 1798 |  * Return the next task in @tset.  Iteration must have been initialized | 
 | 1799 |  * with cgroup_taskset_first(). | 
 | 1800 |  */ | 
 | 1801 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset) | 
 | 1802 | { | 
 | 1803 | 	struct task_and_cgroup *tc; | 
 | 1804 |  | 
 | 1805 | 	if (!tset->tc_array || tset->idx >= tset->tc_array_len) | 
 | 1806 | 		return NULL; | 
 | 1807 |  | 
 | 1808 | 	tc = flex_array_get(tset->tc_array, tset->idx++); | 
 | 1809 | 	tset->cur_cgrp = tc->cgrp; | 
 | 1810 | 	return tc->task; | 
 | 1811 | } | 
 | 1812 | EXPORT_SYMBOL_GPL(cgroup_taskset_next); | 
 | 1813 |  | 
 | 1814 | /** | 
 | 1815 |  * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task | 
 | 1816 |  * @tset: taskset of interest | 
 | 1817 |  * | 
 | 1818 |  * Return the cgroup for the current (last returned) task of @tset.  This | 
 | 1819 |  * function must be preceded by either cgroup_taskset_first() or | 
 | 1820 |  * cgroup_taskset_next(). | 
 | 1821 |  */ | 
 | 1822 | struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset) | 
 | 1823 | { | 
 | 1824 | 	return tset->cur_cgrp; | 
 | 1825 | } | 
 | 1826 | EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup); | 
 | 1827 |  | 
 | 1828 | /** | 
 | 1829 |  * cgroup_taskset_size - return the number of tasks in taskset | 
 | 1830 |  * @tset: taskset of interest | 
 | 1831 |  */ | 
 | 1832 | int cgroup_taskset_size(struct cgroup_taskset *tset) | 
 | 1833 | { | 
 | 1834 | 	return tset->tc_array ? tset->tc_array_len : 1; | 
 | 1835 | } | 
 | 1836 | EXPORT_SYMBOL_GPL(cgroup_taskset_size); | 
 | 1837 |  | 
 | 1838 |  | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1839 | /* | 
 | 1840 |  * cgroup_task_migrate - move a task from one cgroup to another. | 
 | 1841 |  * | 
 | 1842 |  * 'guarantee' is set if the caller promises that a new css_set for the task | 
 | 1843 |  * will already exist. If not set, this function might sleep, and can fail with | 
| Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1844 |  * -ENOMEM. Must be called with cgroup_mutex and threadgroup locked. | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1845 |  */ | 
| Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1846 | static void cgroup_task_migrate(struct cgroup *cgrp, struct cgroup *oldcgrp, | 
 | 1847 | 				struct task_struct *tsk, struct css_set *newcg) | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1848 | { | 
 | 1849 | 	struct css_set *oldcg; | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1850 |  | 
 | 1851 | 	/* | 
| Mandeep Singh Baines | 026085e | 2011-12-21 20:18:35 -0800 | [diff] [blame] | 1852 | 	 * We are synchronized through threadgroup_lock() against PF_EXITING | 
 | 1853 | 	 * setting such that we can't race against cgroup_exit() changing the | 
 | 1854 | 	 * css_set to init_css_set and dropping the old one. | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1855 | 	 */ | 
| Frederic Weisbecker | c84cdf7 | 2011-12-21 20:03:18 +0100 | [diff] [blame] | 1856 | 	WARN_ON_ONCE(tsk->flags & PF_EXITING); | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1857 | 	oldcg = tsk->cgroups; | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1858 |  | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1859 | 	task_lock(tsk); | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1860 | 	rcu_assign_pointer(tsk->cgroups, newcg); | 
 | 1861 | 	task_unlock(tsk); | 
 | 1862 |  | 
 | 1863 | 	/* Update the css_set linked lists if we're using them */ | 
 | 1864 | 	write_lock(&css_set_lock); | 
 | 1865 | 	if (!list_empty(&tsk->cg_list)) | 
 | 1866 | 		list_move(&tsk->cg_list, &newcg->tasks); | 
 | 1867 | 	write_unlock(&css_set_lock); | 
 | 1868 |  | 
 | 1869 | 	/* | 
 | 1870 | 	 * We just gained a reference on oldcg by taking it from the task. As | 
 | 1871 | 	 * trading it for newcg is protected by cgroup_mutex, we're safe to drop | 
 | 1872 | 	 * it here; it will be freed under RCU. | 
 | 1873 | 	 */ | 
 | 1874 | 	put_css_set(oldcg); | 
 | 1875 |  | 
 | 1876 | 	set_bit(CGRP_RELEASABLE, &oldcgrp->flags); | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1877 | } | 
 | 1878 |  | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1879 | /** | 
 | 1880 |  * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp' | 
 | 1881 |  * @cgrp: the cgroup the task is attaching to | 
 | 1882 |  * @tsk: the task to be attached | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1883 |  * | 
| Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1884 |  * Call with cgroup_mutex and threadgroup locked. May take task_lock of | 
 | 1885 |  * @tsk during call. | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1886 |  */ | 
| Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 1887 | int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk) | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1888 | { | 
| Tejun Heo | 8f12191 | 2012-03-29 22:03:33 -0700 | [diff] [blame] | 1889 | 	int retval = 0; | 
| Daisuke Nishimura | 2468c72 | 2010-03-10 15:22:03 -0800 | [diff] [blame] | 1890 | 	struct cgroup_subsys *ss, *failed_ss = NULL; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1891 | 	struct cgroup *oldcgrp; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1892 | 	struct cgroupfs_root *root = cgrp->root; | 
| Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1893 | 	struct cgroup_taskset tset = { }; | 
| Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1894 | 	struct css_set *newcg; | 
| Colin Cross | befae2f | 2010-11-23 21:37:04 -0800 | [diff] [blame] | 1895 | 	struct css_set *cg; | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1896 |  | 
| Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1897 | 	/* @tsk either already exited or can't exit until the end */ | 
 | 1898 | 	if (tsk->flags & PF_EXITING) | 
 | 1899 | 		return -ESRCH; | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1900 |  | 
 | 1901 | 	/* Nothing to do if the task is already in that cgroup */ | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1902 | 	oldcgrp = task_cgroup_from_root(tsk, root); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1903 | 	if (cgrp == oldcgrp) | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1904 | 		return 0; | 
 | 1905 |  | 
| Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1906 | 	tset.single.task = tsk; | 
 | 1907 | 	tset.single.cgrp = oldcgrp; | 
 | 1908 |  | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1909 | 	for_each_subsys(root, ss) { | 
 | 1910 | 		if (ss->can_attach) { | 
| Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1911 | 			retval = ss->can_attach(cgrp, &tset); | 
| Daisuke Nishimura | 2468c72 | 2010-03-10 15:22:03 -0800 | [diff] [blame] | 1912 | 			if (retval) { | 
 | 1913 | 				/* | 
 | 1914 | 				 * Remember on which subsystem the can_attach() | 
 | 1915 | 				 * failed, so that we only call cancel_attach() | 
 | 1916 | 				 * against the subsystems whose can_attach() | 
 | 1917 | 				 * succeeded. (See below) | 
 | 1918 | 				 */ | 
 | 1919 | 				failed_ss = ss; | 
 | 1920 | 				goto out; | 
 | 1921 | 			} | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1922 | 		} | 
 | 1923 | 	} | 
 | 1924 |  | 
| Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1925 | 	newcg = find_css_set(tsk->cgroups, cgrp); | 
 | 1926 | 	if (!newcg) { | 
 | 1927 | 		retval = -ENOMEM; | 
| Daisuke Nishimura | 2468c72 | 2010-03-10 15:22:03 -0800 | [diff] [blame] | 1928 | 		goto out; | 
| Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1929 | 	} | 
 | 1930 |  | 
| Colin Cross | befae2f | 2010-11-23 21:37:04 -0800 | [diff] [blame] | 1931 | 	task_lock(tsk); | 
 | 1932 | 	cg = tsk->cgroups; | 
 | 1933 | 	get_css_set(cg); | 
 | 1934 | 	task_unlock(tsk); | 
 | 1935 |  | 
| Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1936 | 	cgroup_task_migrate(cgrp, oldcgrp, tsk, newcg); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1937 |  | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1938 | 	for_each_subsys(root, ss) { | 
| Paul Jackson | e18f631 | 2008-02-07 00:13:44 -0800 | [diff] [blame] | 1939 | 		if (ss->attach) | 
| Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1940 | 			ss->attach(cgrp, &tset); | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1941 | 	} | 
| Colin Cross | c0f6fa8 | 2010-11-23 21:37:03 -0800 | [diff] [blame] | 1942 | 	set_bit(CGRP_RELEASABLE, &cgrp->flags); | 
| Colin Cross | befae2f | 2010-11-23 21:37:04 -0800 | [diff] [blame] | 1943 | 	/* put_css_set will not destroy cg until after an RCU grace period */ | 
 | 1944 | 	put_css_set(cg); | 
| KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 1945 |  | 
 | 1946 | 	/* | 
 | 1947 | 	 * wake up rmdir() waiter. the rmdir should fail since the cgroup | 
 | 1948 | 	 * is no longer empty. | 
 | 1949 | 	 */ | 
| KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 1950 | 	cgroup_wakeup_rmdir_waiter(cgrp); | 
| Daisuke Nishimura | 2468c72 | 2010-03-10 15:22:03 -0800 | [diff] [blame] | 1951 | out: | 
 | 1952 | 	if (retval) { | 
 | 1953 | 		for_each_subsys(root, ss) { | 
 | 1954 | 			if (ss == failed_ss) | 
 | 1955 | 				/* | 
 | 1956 | 				 * This subsystem was the one that failed the | 
 | 1957 | 				 * can_attach() check earlier, so we don't need | 
 | 1958 | 				 * to call cancel_attach() against it or any | 
 | 1959 | 				 * remaining subsystems. | 
 | 1960 | 				 */ | 
 | 1961 | 				break; | 
 | 1962 | 			if (ss->cancel_attach) | 
| Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1963 | 				ss->cancel_attach(cgrp, &tset); | 
| Daisuke Nishimura | 2468c72 | 2010-03-10 15:22:03 -0800 | [diff] [blame] | 1964 | 		} | 
 | 1965 | 	} | 
 | 1966 | 	return retval; | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1967 | } | 
 | 1968 |  | 
| Sridhar Samudrala | d7926ee | 2010-05-30 22:24:39 +0200 | [diff] [blame] | 1969 | /** | 
| Michael S. Tsirkin | 31583bb | 2010-09-09 16:37:37 -0700 | [diff] [blame] | 1970 |  * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from' | 
 | 1971 |  * @from: attach to all cgroups of a given task | 
| Sridhar Samudrala | d7926ee | 2010-05-30 22:24:39 +0200 | [diff] [blame] | 1972 |  * @tsk: the task to be attached | 
 | 1973 |  */ | 
| Michael S. Tsirkin | 31583bb | 2010-09-09 16:37:37 -0700 | [diff] [blame] | 1974 | int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk) | 
| Sridhar Samudrala | d7926ee | 2010-05-30 22:24:39 +0200 | [diff] [blame] | 1975 | { | 
 | 1976 | 	struct cgroupfs_root *root; | 
| Sridhar Samudrala | d7926ee | 2010-05-30 22:24:39 +0200 | [diff] [blame] | 1977 | 	int retval = 0; | 
 | 1978 |  | 
 | 1979 | 	cgroup_lock(); | 
 | 1980 | 	for_each_active_root(root) { | 
| Michael S. Tsirkin | 31583bb | 2010-09-09 16:37:37 -0700 | [diff] [blame] | 1981 | 		struct cgroup *from_cg = task_cgroup_from_root(from, root); | 
 | 1982 |  | 
 | 1983 | 		retval = cgroup_attach_task(from_cg, tsk); | 
| Sridhar Samudrala | d7926ee | 2010-05-30 22:24:39 +0200 | [diff] [blame] | 1984 | 		if (retval) | 
 | 1985 | 			break; | 
 | 1986 | 	} | 
 | 1987 | 	cgroup_unlock(); | 
 | 1988 |  | 
 | 1989 | 	return retval; | 
 | 1990 | } | 
| Michael S. Tsirkin | 31583bb | 2010-09-09 16:37:37 -0700 | [diff] [blame] | 1991 | EXPORT_SYMBOL_GPL(cgroup_attach_task_all); | 
| Sridhar Samudrala | d7926ee | 2010-05-30 22:24:39 +0200 | [diff] [blame] | 1992 |  | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1993 | /** | 
 | 1994 |  * cgroup_attach_proc - attach all threads in a threadgroup to a cgroup | 
 | 1995 |  * @cgrp: the cgroup to attach to | 
 | 1996 |  * @leader: the threadgroup leader task_struct of the group to be attached | 
 | 1997 |  * | 
| Tejun Heo | 257058a | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1998 |  * Call holding cgroup_mutex and the group_rwsem of the leader. Will take | 
 | 1999 |  * task_lock of each thread in leader's threadgroup individually in turn. | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2000 |  */ | 
| Kirill A. Shutemov | 1c6c3fa | 2011-12-27 07:46:25 +0200 | [diff] [blame] | 2001 | static int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader) | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2002 | { | 
 | 2003 | 	int retval, i, group_size; | 
 | 2004 | 	struct cgroup_subsys *ss, *failed_ss = NULL; | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2005 | 	/* guaranteed to be initialized later, but the compiler needs this */ | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2006 | 	struct cgroupfs_root *root = cgrp->root; | 
 | 2007 | 	/* threadgroup list cursor and array */ | 
 | 2008 | 	struct task_struct *tsk; | 
| Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2009 | 	struct task_and_cgroup *tc; | 
| Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2010 | 	struct flex_array *group; | 
| Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2011 | 	struct cgroup_taskset tset = { }; | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2012 |  | 
 | 2013 | 	/* | 
 | 2014 | 	 * step 0: in order to do expensive, possibly blocking operations for | 
 | 2015 | 	 * every thread, we cannot iterate the thread group list, since it needs | 
 | 2016 | 	 * rcu or tasklist locked. instead, build an array of all threads in the | 
| Tejun Heo | 257058a | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2017 | 	 * group - group_rwsem prevents new threads from appearing, and if | 
 | 2018 | 	 * threads exit, this will just be an over-estimate. | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2019 | 	 */ | 
 | 2020 | 	group_size = get_nr_threads(leader); | 
| Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2021 | 	/* flex_array supports very large thread-groups better than kmalloc. */ | 
| Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2022 | 	group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL); | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2023 | 	if (!group) | 
 | 2024 | 		return -ENOMEM; | 
| Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2025 | 	/* pre-allocate to guarantee space while iterating in rcu read-side. */ | 
 | 2026 | 	retval = flex_array_prealloc(group, 0, group_size - 1, GFP_KERNEL); | 
 | 2027 | 	if (retval) | 
 | 2028 | 		goto out_free_group_list; | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2029 |  | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2030 | 	tsk = leader; | 
 | 2031 | 	i = 0; | 
| Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2032 | 	/* | 
 | 2033 | 	 * Prevent freeing of tasks while we take a snapshot. Tasks that are | 
 | 2034 | 	 * already PF_EXITING could be freed from underneath us unless we | 
 | 2035 | 	 * take an rcu_read_lock. | 
 | 2036 | 	 */ | 
 | 2037 | 	rcu_read_lock(); | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2038 | 	do { | 
| Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2039 | 		struct task_and_cgroup ent; | 
 | 2040 |  | 
| Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2041 | 		/* @tsk either already exited or can't exit until the end */ | 
 | 2042 | 		if (tsk->flags & PF_EXITING) | 
 | 2043 | 			continue; | 
 | 2044 |  | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2045 | 		/* as per above, nr_threads may decrease, but not increase. */ | 
 | 2046 | 		BUG_ON(i >= group_size); | 
| Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2047 | 		ent.task = tsk; | 
 | 2048 | 		ent.cgrp = task_cgroup_from_root(tsk, root); | 
| Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2049 | 		/* nothing to do if this task is already in the cgroup */ | 
 | 2050 | 		if (ent.cgrp == cgrp) | 
 | 2051 | 			continue; | 
| Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2052 | 		/* | 
 | 2053 | 		 * saying GFP_ATOMIC has no effect here because we did prealloc | 
 | 2054 | 		 * earlier, but it's good form to communicate our expectations. | 
 | 2055 | 		 */ | 
| Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2056 | 		retval = flex_array_put(group, i, &ent, GFP_ATOMIC); | 
| Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2057 | 		BUG_ON(retval != 0); | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2058 | 		i++; | 
 | 2059 | 	} while_each_thread(leader, tsk); | 
| Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2060 | 	rcu_read_unlock(); | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2061 | 	/* remember the number of threads in the array for later. */ | 
 | 2062 | 	group_size = i; | 
| Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2063 | 	tset.tc_array = group; | 
 | 2064 | 	tset.tc_array_len = group_size; | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2065 |  | 
| Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2066 | 	/* methods shouldn't be called if no task is actually migrating */ | 
 | 2067 | 	retval = 0; | 
| Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2068 | 	if (!group_size) | 
| Mandeep Singh Baines | b07ef77 | 2011-12-21 20:18:36 -0800 | [diff] [blame] | 2069 | 		goto out_free_group_list; | 
| Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2070 |  | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2071 | 	/* | 
 | 2072 | 	 * step 1: check that we can legitimately attach to the cgroup. | 
 | 2073 | 	 */ | 
 | 2074 | 	for_each_subsys(root, ss) { | 
 | 2075 | 		if (ss->can_attach) { | 
| Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2076 | 			retval = ss->can_attach(cgrp, &tset); | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2077 | 			if (retval) { | 
 | 2078 | 				failed_ss = ss; | 
 | 2079 | 				goto out_cancel_attach; | 
 | 2080 | 			} | 
 | 2081 | 		} | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2082 | 	} | 
 | 2083 |  | 
 | 2084 | 	/* | 
 | 2085 | 	 * step 2: make sure css_sets exist for all threads to be migrated. | 
 | 2086 | 	 * we use find_css_set, which allocates a new one if necessary. | 
 | 2087 | 	 */ | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2088 | 	for (i = 0; i < group_size; i++) { | 
| Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2089 | 		tc = flex_array_get(group, i); | 
| Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2090 | 		tc->cg = find_css_set(tc->task->cgroups, cgrp); | 
 | 2091 | 		if (!tc->cg) { | 
 | 2092 | 			retval = -ENOMEM; | 
 | 2093 | 			goto out_put_css_set_refs; | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2094 | 		} | 
 | 2095 | 	} | 
 | 2096 |  | 
 | 2097 | 	/* | 
| Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2098 | 	 * step 3: now that we're guaranteed success wrt the css_sets, | 
 | 2099 | 	 * proceed to move all tasks to the new cgroup.  There are no | 
 | 2100 | 	 * failure cases after here, so this is the commit point. | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2101 | 	 */ | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2102 | 	for (i = 0; i < group_size; i++) { | 
| Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2103 | 		tc = flex_array_get(group, i); | 
| Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2104 | 		cgroup_task_migrate(cgrp, tc->cgrp, tc->task, tc->cg); | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2105 | 	} | 
 | 2106 | 	/* nothing is sensitive to fork() after this point. */ | 
 | 2107 |  | 
 | 2108 | 	/* | 
| Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2109 | 	 * step 4: do subsystem attach callbacks. | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2110 | 	 */ | 
 | 2111 | 	for_each_subsys(root, ss) { | 
 | 2112 | 		if (ss->attach) | 
| Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2113 | 			ss->attach(cgrp, &tset); | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2114 | 	} | 
 | 2115 |  | 
 | 2116 | 	/* | 
 | 2117 | 	 * step 5: success! and cleanup | 
 | 2118 | 	 */ | 
 | 2119 | 	synchronize_rcu(); | 
 | 2120 | 	cgroup_wakeup_rmdir_waiter(cgrp); | 
 | 2121 | 	retval = 0; | 
| Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2122 | out_put_css_set_refs: | 
 | 2123 | 	if (retval) { | 
 | 2124 | 		for (i = 0; i < group_size; i++) { | 
 | 2125 | 			tc = flex_array_get(group, i); | 
 | 2126 | 			if (!tc->cg) | 
 | 2127 | 				break; | 
 | 2128 | 			put_css_set(tc->cg); | 
 | 2129 | 		} | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2130 | 	} | 
 | 2131 | out_cancel_attach: | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2132 | 	if (retval) { | 
 | 2133 | 		for_each_subsys(root, ss) { | 
| Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2134 | 			if (ss == failed_ss) | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2135 | 				break; | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2136 | 			if (ss->cancel_attach) | 
| Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2137 | 				ss->cancel_attach(cgrp, &tset); | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2138 | 		} | 
 | 2139 | 	} | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2140 | out_free_group_list: | 
| Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2141 | 	flex_array_free(group); | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2142 | 	return retval; | 
 | 2143 | } | 
 | 2144 |  | 
| Colin Cross | 4a12178 | 2011-07-12 19:53:24 -0700 | [diff] [blame] | 2145 | static int cgroup_allow_attach(struct cgroup *cgrp, struct cgroup_taskset *tset) | 
 | 2146 | { | 
 | 2147 | 	struct cgroup_subsys *ss; | 
 | 2148 | 	int ret; | 
 | 2149 |  | 
 | 2150 | 	for_each_subsys(cgrp->root, ss) { | 
 | 2151 | 		if (ss->allow_attach) { | 
 | 2152 | 			ret = ss->allow_attach(cgrp, tset); | 
 | 2153 | 			if (ret) | 
 | 2154 | 				return ret; | 
 | 2155 | 		} else { | 
 | 2156 | 			return -EACCES; | 
 | 2157 | 		} | 
 | 2158 | 	} | 
 | 2159 |  | 
 | 2160 | 	return 0; | 
 | 2161 | } | 
 | 2162 |  | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2163 | /* | 
 | 2164 |  * Find the task_struct of the task to attach by vpid and pass it along to the | 
| Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2165 |  * function to attach either it or all tasks in its threadgroup. Will lock | 
 | 2166 |  * cgroup_mutex and threadgroup; may take task_lock of task. | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2167 |  */ | 
 | 2168 | static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup) | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2169 | { | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2170 | 	struct task_struct *tsk; | 
| David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2171 | 	const struct cred *cred = current_cred(), *tcred; | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2172 | 	int ret; | 
 | 2173 |  | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2174 | 	if (!cgroup_lock_live_group(cgrp)) | 
 | 2175 | 		return -ENODEV; | 
 | 2176 |  | 
| Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2177 | retry_find_task: | 
 | 2178 | 	rcu_read_lock(); | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2179 | 	if (pid) { | 
| Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2180 | 		tsk = find_task_by_vpid(pid); | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2181 | 		if (!tsk) { | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2182 | 			rcu_read_unlock(); | 
| Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2183 | 			ret= -ESRCH; | 
 | 2184 | 			goto out_unlock_cgroup; | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2185 | 		} | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2186 | 		/* | 
 | 2187 | 		 * even if we're attaching all tasks in the thread group, we | 
 | 2188 | 		 * only need to check permissions on one of them. | 
 | 2189 | 		 */ | 
| David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2190 | 		tcred = __task_cred(tsk); | 
 | 2191 | 		if (cred->euid && | 
 | 2192 | 		    cred->euid != tcred->uid && | 
 | 2193 | 		    cred->euid != tcred->suid) { | 
| Colin Cross | 4a12178 | 2011-07-12 19:53:24 -0700 | [diff] [blame] | 2194 | 			/* | 
 | 2195 | 			 * if the default permission check fails, give each | 
 | 2196 | 			 * cgroup a chance to extend the permission check | 
 | 2197 | 			 */ | 
 | 2198 | 			struct cgroup_taskset tset = { }; | 
 | 2199 | 			tset.single.task = tsk; | 
 | 2200 | 			tset.single.cgrp = cgrp; | 
 | 2201 | 			ret = cgroup_allow_attach(cgrp, &tset); | 
 | 2202 | 			if (ret) { | 
 | 2203 | 				rcu_read_unlock(); | 
 | 2204 | 				goto out_unlock_cgroup; | 
 | 2205 | 			} | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2206 | 		} | 
| Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2207 | 	} else | 
 | 2208 | 		tsk = current; | 
| Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2209 |  | 
 | 2210 | 	if (threadgroup) | 
| Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2211 | 		tsk = tsk->group_leader; | 
 | 2212 | 	get_task_struct(tsk); | 
 | 2213 | 	rcu_read_unlock(); | 
| Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2214 |  | 
| Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2215 | 	threadgroup_lock(tsk); | 
 | 2216 | 	if (threadgroup) { | 
 | 2217 | 		if (!thread_group_leader(tsk)) { | 
 | 2218 | 			/* | 
 | 2219 | 			 * a race with de_thread from another thread's exec() | 
 | 2220 | 			 * may strip us of our leadership, if this happens, | 
 | 2221 | 			 * there is no choice but to throw this task away and | 
 | 2222 | 			 * try again; this is | 
 | 2223 | 			 * "double-double-toil-and-trouble-check locking". | 
 | 2224 | 			 */ | 
 | 2225 | 			threadgroup_unlock(tsk); | 
 | 2226 | 			put_task_struct(tsk); | 
 | 2227 | 			goto retry_find_task; | 
 | 2228 | 		} | 
 | 2229 | 		ret = cgroup_attach_proc(cgrp, tsk); | 
 | 2230 | 	} else | 
 | 2231 | 		ret = cgroup_attach_task(cgrp, tsk); | 
| Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2232 | 	threadgroup_unlock(tsk); | 
 | 2233 |  | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2234 | 	put_task_struct(tsk); | 
| Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2235 | out_unlock_cgroup: | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2236 | 	cgroup_unlock(); | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2237 | 	return ret; | 
 | 2238 | } | 
 | 2239 |  | 
| Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2240 | static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid) | 
 | 2241 | { | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2242 | 	return attach_task_by_pid(cgrp, pid, false); | 
 | 2243 | } | 
 | 2244 |  | 
 | 2245 | static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid) | 
 | 2246 | { | 
| Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2247 | 	return attach_task_by_pid(cgrp, tgid, true); | 
| Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2248 | } | 
 | 2249 |  | 
| Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2250 | /** | 
 | 2251 |  * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive. | 
 | 2252 |  * @cgrp: the cgroup to be checked for liveness | 
 | 2253 |  * | 
| Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2254 |  * On success, returns true; the lock should be later released with | 
 | 2255 |  * cgroup_unlock(). On failure returns false with no lock held. | 
| Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2256 |  */ | 
| Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2257 | bool cgroup_lock_live_group(struct cgroup *cgrp) | 
| Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2258 | { | 
 | 2259 | 	mutex_lock(&cgroup_mutex); | 
 | 2260 | 	if (cgroup_is_removed(cgrp)) { | 
 | 2261 | 		mutex_unlock(&cgroup_mutex); | 
 | 2262 | 		return false; | 
 | 2263 | 	} | 
 | 2264 | 	return true; | 
 | 2265 | } | 
| Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 2266 | EXPORT_SYMBOL_GPL(cgroup_lock_live_group); | 
| Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2267 |  | 
 | 2268 | static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft, | 
 | 2269 | 				      const char *buffer) | 
 | 2270 | { | 
 | 2271 | 	BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX); | 
| Evgeny Kuznetsov | f4a2589 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 2272 | 	if (strlen(buffer) >= PATH_MAX) | 
 | 2273 | 		return -EINVAL; | 
| Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2274 | 	if (!cgroup_lock_live_group(cgrp)) | 
 | 2275 | 		return -ENODEV; | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2276 | 	mutex_lock(&cgroup_root_mutex); | 
| Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2277 | 	strcpy(cgrp->root->release_agent_path, buffer); | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2278 | 	mutex_unlock(&cgroup_root_mutex); | 
| Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2279 | 	cgroup_unlock(); | 
| Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2280 | 	return 0; | 
 | 2281 | } | 
 | 2282 |  | 
 | 2283 | static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft, | 
 | 2284 | 				     struct seq_file *seq) | 
 | 2285 | { | 
 | 2286 | 	if (!cgroup_lock_live_group(cgrp)) | 
 | 2287 | 		return -ENODEV; | 
 | 2288 | 	seq_puts(seq, cgrp->root->release_agent_path); | 
 | 2289 | 	seq_putc(seq, '\n'); | 
| Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2290 | 	cgroup_unlock(); | 
| Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2291 | 	return 0; | 
 | 2292 | } | 
 | 2293 |  | 
| Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2294 | /* A buffer size big enough for numbers or short strings */ | 
 | 2295 | #define CGROUP_LOCAL_BUFFER_SIZE 64 | 
 | 2296 |  | 
| Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2297 | static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft, | 
| Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2298 | 				struct file *file, | 
 | 2299 | 				const char __user *userbuf, | 
 | 2300 | 				size_t nbytes, loff_t *unused_ppos) | 
| Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2301 | { | 
| Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2302 | 	char buffer[CGROUP_LOCAL_BUFFER_SIZE]; | 
| Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2303 | 	int retval = 0; | 
| Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2304 | 	char *end; | 
 | 2305 |  | 
 | 2306 | 	if (!nbytes) | 
 | 2307 | 		return -EINVAL; | 
 | 2308 | 	if (nbytes >= sizeof(buffer)) | 
 | 2309 | 		return -E2BIG; | 
 | 2310 | 	if (copy_from_user(buffer, userbuf, nbytes)) | 
 | 2311 | 		return -EFAULT; | 
 | 2312 |  | 
 | 2313 | 	buffer[nbytes] = 0;     /* nul-terminate */ | 
| Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2314 | 	if (cft->write_u64) { | 
| KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2315 | 		u64 val = simple_strtoull(strstrip(buffer), &end, 0); | 
| Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2316 | 		if (*end) | 
 | 2317 | 			return -EINVAL; | 
 | 2318 | 		retval = cft->write_u64(cgrp, cft, val); | 
 | 2319 | 	} else { | 
| KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2320 | 		s64 val = simple_strtoll(strstrip(buffer), &end, 0); | 
| Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2321 | 		if (*end) | 
 | 2322 | 			return -EINVAL; | 
 | 2323 | 		retval = cft->write_s64(cgrp, cft, val); | 
 | 2324 | 	} | 
| Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2325 | 	if (!retval) | 
 | 2326 | 		retval = nbytes; | 
 | 2327 | 	return retval; | 
 | 2328 | } | 
 | 2329 |  | 
| Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2330 | static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft, | 
 | 2331 | 				   struct file *file, | 
 | 2332 | 				   const char __user *userbuf, | 
 | 2333 | 				   size_t nbytes, loff_t *unused_ppos) | 
 | 2334 | { | 
| Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2335 | 	char local_buffer[CGROUP_LOCAL_BUFFER_SIZE]; | 
| Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2336 | 	int retval = 0; | 
 | 2337 | 	size_t max_bytes = cft->max_write_len; | 
 | 2338 | 	char *buffer = local_buffer; | 
 | 2339 |  | 
 | 2340 | 	if (!max_bytes) | 
 | 2341 | 		max_bytes = sizeof(local_buffer) - 1; | 
 | 2342 | 	if (nbytes >= max_bytes) | 
 | 2343 | 		return -E2BIG; | 
 | 2344 | 	/* Allocate a dynamic buffer if we need one */ | 
 | 2345 | 	if (nbytes >= sizeof(local_buffer)) { | 
 | 2346 | 		buffer = kmalloc(nbytes + 1, GFP_KERNEL); | 
 | 2347 | 		if (buffer == NULL) | 
 | 2348 | 			return -ENOMEM; | 
 | 2349 | 	} | 
| Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 2350 | 	if (nbytes && copy_from_user(buffer, userbuf, nbytes)) { | 
 | 2351 | 		retval = -EFAULT; | 
 | 2352 | 		goto out; | 
 | 2353 | 	} | 
| Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2354 |  | 
 | 2355 | 	buffer[nbytes] = 0;     /* nul-terminate */ | 
| KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2356 | 	retval = cft->write_string(cgrp, cft, strstrip(buffer)); | 
| Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2357 | 	if (!retval) | 
 | 2358 | 		retval = nbytes; | 
| Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 2359 | out: | 
| Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2360 | 	if (buffer != local_buffer) | 
 | 2361 | 		kfree(buffer); | 
 | 2362 | 	return retval; | 
 | 2363 | } | 
 | 2364 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2365 | static ssize_t cgroup_file_write(struct file *file, const char __user *buf, | 
 | 2366 | 						size_t nbytes, loff_t *ppos) | 
 | 2367 | { | 
 | 2368 | 	struct cftype *cft = __d_cft(file->f_dentry); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2369 | 	struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2370 |  | 
| Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2371 | 	if (cgroup_is_removed(cgrp)) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2372 | 		return -ENODEV; | 
| Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2373 | 	if (cft->write) | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2374 | 		return cft->write(cgrp, cft, file, buf, nbytes, ppos); | 
| Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2375 | 	if (cft->write_u64 || cft->write_s64) | 
 | 2376 | 		return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos); | 
| Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2377 | 	if (cft->write_string) | 
 | 2378 | 		return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos); | 
| Pavel Emelyanov | d447ea2 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 2379 | 	if (cft->trigger) { | 
 | 2380 | 		int ret = cft->trigger(cgrp, (unsigned int)cft->private); | 
 | 2381 | 		return ret ? ret : nbytes; | 
 | 2382 | 	} | 
| Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2383 | 	return -EINVAL; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2384 | } | 
 | 2385 |  | 
| Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2386 | static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft, | 
 | 2387 | 			       struct file *file, | 
 | 2388 | 			       char __user *buf, size_t nbytes, | 
 | 2389 | 			       loff_t *ppos) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2390 | { | 
| Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2391 | 	char tmp[CGROUP_LOCAL_BUFFER_SIZE]; | 
| Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2392 | 	u64 val = cft->read_u64(cgrp, cft); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2393 | 	int len = sprintf(tmp, "%llu\n", (unsigned long long) val); | 
 | 2394 |  | 
 | 2395 | 	return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); | 
 | 2396 | } | 
 | 2397 |  | 
| Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2398 | static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft, | 
 | 2399 | 			       struct file *file, | 
 | 2400 | 			       char __user *buf, size_t nbytes, | 
 | 2401 | 			       loff_t *ppos) | 
 | 2402 | { | 
| Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2403 | 	char tmp[CGROUP_LOCAL_BUFFER_SIZE]; | 
| Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2404 | 	s64 val = cft->read_s64(cgrp, cft); | 
 | 2405 | 	int len = sprintf(tmp, "%lld\n", (long long) val); | 
 | 2406 |  | 
 | 2407 | 	return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); | 
 | 2408 | } | 
 | 2409 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2410 | static ssize_t cgroup_file_read(struct file *file, char __user *buf, | 
 | 2411 | 				   size_t nbytes, loff_t *ppos) | 
 | 2412 | { | 
 | 2413 | 	struct cftype *cft = __d_cft(file->f_dentry); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2414 | 	struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2415 |  | 
| Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2416 | 	if (cgroup_is_removed(cgrp)) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2417 | 		return -ENODEV; | 
 | 2418 |  | 
 | 2419 | 	if (cft->read) | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2420 | 		return cft->read(cgrp, cft, file, buf, nbytes, ppos); | 
| Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2421 | 	if (cft->read_u64) | 
 | 2422 | 		return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos); | 
| Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2423 | 	if (cft->read_s64) | 
 | 2424 | 		return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2425 | 	return -EINVAL; | 
 | 2426 | } | 
 | 2427 |  | 
| Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2428 | /* | 
 | 2429 |  * seqfile ops/methods for returning structured data. Currently just | 
 | 2430 |  * supports string->u64 maps, but can be extended in future. | 
 | 2431 |  */ | 
 | 2432 |  | 
 | 2433 | struct cgroup_seqfile_state { | 
 | 2434 | 	struct cftype *cft; | 
 | 2435 | 	struct cgroup *cgroup; | 
 | 2436 | }; | 
 | 2437 |  | 
 | 2438 | static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value) | 
 | 2439 | { | 
 | 2440 | 	struct seq_file *sf = cb->state; | 
 | 2441 | 	return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value); | 
 | 2442 | } | 
 | 2443 |  | 
 | 2444 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) | 
 | 2445 | { | 
 | 2446 | 	struct cgroup_seqfile_state *state = m->private; | 
 | 2447 | 	struct cftype *cft = state->cft; | 
| Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2448 | 	if (cft->read_map) { | 
 | 2449 | 		struct cgroup_map_cb cb = { | 
 | 2450 | 			.fill = cgroup_map_add, | 
 | 2451 | 			.state = m, | 
 | 2452 | 		}; | 
 | 2453 | 		return cft->read_map(state->cgroup, cft, &cb); | 
 | 2454 | 	} | 
 | 2455 | 	return cft->read_seq_string(state->cgroup, cft, m); | 
| Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2456 | } | 
 | 2457 |  | 
| Adrian Bunk | 96930a6 | 2008-07-25 19:46:21 -0700 | [diff] [blame] | 2458 | static int cgroup_seqfile_release(struct inode *inode, struct file *file) | 
| Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2459 | { | 
 | 2460 | 	struct seq_file *seq = file->private_data; | 
 | 2461 | 	kfree(seq->private); | 
 | 2462 | 	return single_release(inode, file); | 
 | 2463 | } | 
 | 2464 |  | 
| Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2465 | static const struct file_operations cgroup_seqfile_operations = { | 
| Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2466 | 	.read = seq_read, | 
| Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2467 | 	.write = cgroup_file_write, | 
| Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2468 | 	.llseek = seq_lseek, | 
 | 2469 | 	.release = cgroup_seqfile_release, | 
 | 2470 | }; | 
 | 2471 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2472 | static int cgroup_file_open(struct inode *inode, struct file *file) | 
 | 2473 | { | 
 | 2474 | 	int err; | 
 | 2475 | 	struct cftype *cft; | 
 | 2476 |  | 
 | 2477 | 	err = generic_file_open(inode, file); | 
 | 2478 | 	if (err) | 
 | 2479 | 		return err; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2480 | 	cft = __d_cft(file->f_dentry); | 
| Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2481 |  | 
| Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2482 | 	if (cft->read_map || cft->read_seq_string) { | 
| Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2483 | 		struct cgroup_seqfile_state *state = | 
 | 2484 | 			kzalloc(sizeof(*state), GFP_USER); | 
 | 2485 | 		if (!state) | 
 | 2486 | 			return -ENOMEM; | 
 | 2487 | 		state->cft = cft; | 
 | 2488 | 		state->cgroup = __d_cgrp(file->f_dentry->d_parent); | 
 | 2489 | 		file->f_op = &cgroup_seqfile_operations; | 
 | 2490 | 		err = single_open(file, cgroup_seqfile_show, state); | 
 | 2491 | 		if (err < 0) | 
 | 2492 | 			kfree(state); | 
 | 2493 | 	} else if (cft->open) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2494 | 		err = cft->open(inode, file); | 
 | 2495 | 	else | 
 | 2496 | 		err = 0; | 
 | 2497 |  | 
 | 2498 | 	return err; | 
 | 2499 | } | 
 | 2500 |  | 
 | 2501 | static int cgroup_file_release(struct inode *inode, struct file *file) | 
 | 2502 | { | 
 | 2503 | 	struct cftype *cft = __d_cft(file->f_dentry); | 
 | 2504 | 	if (cft->release) | 
 | 2505 | 		return cft->release(inode, file); | 
 | 2506 | 	return 0; | 
 | 2507 | } | 
 | 2508 |  | 
 | 2509 | /* | 
 | 2510 |  * cgroup_rename - Only allow simple rename of directories in place. | 
 | 2511 |  */ | 
 | 2512 | static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry, | 
 | 2513 | 			    struct inode *new_dir, struct dentry *new_dentry) | 
 | 2514 | { | 
 | 2515 | 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) | 
 | 2516 | 		return -ENOTDIR; | 
 | 2517 | 	if (new_dentry->d_inode) | 
 | 2518 | 		return -EEXIST; | 
 | 2519 | 	if (old_dir != new_dir) | 
 | 2520 | 		return -EIO; | 
 | 2521 | 	return simple_rename(old_dir, old_dentry, new_dir, new_dentry); | 
 | 2522 | } | 
 | 2523 |  | 
| Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2524 | static const struct file_operations cgroup_file_operations = { | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2525 | 	.read = cgroup_file_read, | 
 | 2526 | 	.write = cgroup_file_write, | 
 | 2527 | 	.llseek = generic_file_llseek, | 
 | 2528 | 	.open = cgroup_file_open, | 
 | 2529 | 	.release = cgroup_file_release, | 
 | 2530 | }; | 
 | 2531 |  | 
| Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 2532 | static const struct inode_operations cgroup_dir_inode_operations = { | 
| Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 2533 | 	.lookup = cgroup_lookup, | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2534 | 	.mkdir = cgroup_mkdir, | 
 | 2535 | 	.rmdir = cgroup_rmdir, | 
 | 2536 | 	.rename = cgroup_rename, | 
 | 2537 | }; | 
 | 2538 |  | 
| Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 2539 | static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) | 
 | 2540 | { | 
 | 2541 | 	if (dentry->d_name.len > NAME_MAX) | 
 | 2542 | 		return ERR_PTR(-ENAMETOOLONG); | 
 | 2543 | 	d_add(dentry, NULL); | 
 | 2544 | 	return NULL; | 
 | 2545 | } | 
 | 2546 |  | 
| Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 2547 | /* | 
 | 2548 |  * Check if a file is a control file | 
 | 2549 |  */ | 
 | 2550 | static inline struct cftype *__file_cft(struct file *file) | 
 | 2551 | { | 
 | 2552 | 	if (file->f_dentry->d_inode->i_fop != &cgroup_file_operations) | 
 | 2553 | 		return ERR_PTR(-EINVAL); | 
 | 2554 | 	return __d_cft(file->f_dentry); | 
 | 2555 | } | 
 | 2556 |  | 
| Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2557 | static int cgroup_create_file(struct dentry *dentry, umode_t mode, | 
| Nick Piggin | 5adcee1 | 2011-01-07 17:49:20 +1100 | [diff] [blame] | 2558 | 				struct super_block *sb) | 
 | 2559 | { | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2560 | 	struct inode *inode; | 
 | 2561 |  | 
 | 2562 | 	if (!dentry) | 
 | 2563 | 		return -ENOENT; | 
 | 2564 | 	if (dentry->d_inode) | 
 | 2565 | 		return -EEXIST; | 
 | 2566 |  | 
 | 2567 | 	inode = cgroup_new_inode(mode, sb); | 
 | 2568 | 	if (!inode) | 
 | 2569 | 		return -ENOMEM; | 
 | 2570 |  | 
 | 2571 | 	if (S_ISDIR(mode)) { | 
 | 2572 | 		inode->i_op = &cgroup_dir_inode_operations; | 
 | 2573 | 		inode->i_fop = &simple_dir_operations; | 
 | 2574 |  | 
 | 2575 | 		/* start off with i_nlink == 2 (for "." entry) */ | 
 | 2576 | 		inc_nlink(inode); | 
 | 2577 |  | 
 | 2578 | 		/* start with the directory inode held, so that we can | 
 | 2579 | 		 * populate it without racing with another mkdir */ | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2580 | 		mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2581 | 	} else if (S_ISREG(mode)) { | 
 | 2582 | 		inode->i_size = 0; | 
 | 2583 | 		inode->i_fop = &cgroup_file_operations; | 
 | 2584 | 	} | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2585 | 	d_instantiate(dentry, inode); | 
 | 2586 | 	dget(dentry);	/* Extra count - pin the dentry in core */ | 
 | 2587 | 	return 0; | 
 | 2588 | } | 
 | 2589 |  | 
 | 2590 | /* | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2591 |  * cgroup_create_dir - create a directory for an object. | 
 | 2592 |  * @cgrp: the cgroup we create the directory for. It must have a valid | 
 | 2593 |  *        ->parent field. And we are going to fill its ->dentry field. | 
 | 2594 |  * @dentry: dentry of the new cgroup | 
 | 2595 |  * @mode: mode to set on new directory. | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2596 |  */ | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2597 | static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry, | 
| Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2598 | 				umode_t mode) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2599 | { | 
 | 2600 | 	struct dentry *parent; | 
 | 2601 | 	int error = 0; | 
 | 2602 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2603 | 	parent = cgrp->parent->dentry; | 
 | 2604 | 	error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2605 | 	if (!error) { | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2606 | 		dentry->d_fsdata = cgrp; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2607 | 		inc_nlink(parent->d_inode); | 
| Paul Menage | a47295e | 2009-01-07 18:07:44 -0800 | [diff] [blame] | 2608 | 		rcu_assign_pointer(cgrp->dentry, dentry); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2609 | 		dget(dentry); | 
 | 2610 | 	} | 
 | 2611 | 	dput(dentry); | 
 | 2612 |  | 
 | 2613 | 	return error; | 
 | 2614 | } | 
 | 2615 |  | 
| Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2616 | /** | 
 | 2617 |  * cgroup_file_mode - deduce file mode of a control file | 
 | 2618 |  * @cft: the control file in question | 
 | 2619 |  * | 
 | 2620 |  * returns cft->mode if ->mode is not 0 | 
 | 2621 |  * returns S_IRUGO|S_IWUSR if it has both a read and a write handler | 
 | 2622 |  * returns S_IRUGO if it has only a read handler | 
 | 2623 |  * returns S_IWUSR if it has only a write hander | 
 | 2624 |  */ | 
| Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2625 | static umode_t cgroup_file_mode(const struct cftype *cft) | 
| Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2626 | { | 
| Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2627 | 	umode_t mode = 0; | 
| Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2628 |  | 
 | 2629 | 	if (cft->mode) | 
 | 2630 | 		return cft->mode; | 
 | 2631 |  | 
 | 2632 | 	if (cft->read || cft->read_u64 || cft->read_s64 || | 
 | 2633 | 	    cft->read_map || cft->read_seq_string) | 
 | 2634 | 		mode |= S_IRUGO; | 
 | 2635 |  | 
 | 2636 | 	if (cft->write || cft->write_u64 || cft->write_s64 || | 
 | 2637 | 	    cft->write_string || cft->trigger) | 
 | 2638 | 		mode |= S_IWUSR; | 
 | 2639 |  | 
 | 2640 | 	return mode; | 
 | 2641 | } | 
 | 2642 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2643 | int cgroup_add_file(struct cgroup *cgrp, | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2644 | 		       struct cgroup_subsys *subsys, | 
 | 2645 | 		       const struct cftype *cft) | 
 | 2646 | { | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2647 | 	struct dentry *dir = cgrp->dentry; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2648 | 	struct dentry *dentry; | 
 | 2649 | 	int error; | 
| Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2650 | 	umode_t mode; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2651 |  | 
 | 2652 | 	char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 }; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2653 | 	if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) { | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2654 | 		strcpy(name, subsys->name); | 
 | 2655 | 		strcat(name, "."); | 
 | 2656 | 	} | 
 | 2657 | 	strcat(name, cft->name); | 
 | 2658 | 	BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex)); | 
 | 2659 | 	dentry = lookup_one_len(name, dir, strlen(name)); | 
 | 2660 | 	if (!IS_ERR(dentry)) { | 
| Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2661 | 		mode = cgroup_file_mode(cft); | 
 | 2662 | 		error = cgroup_create_file(dentry, mode | S_IFREG, | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2663 | 						cgrp->root->sb); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2664 | 		if (!error) | 
 | 2665 | 			dentry->d_fsdata = (void *)cft; | 
 | 2666 | 		dput(dentry); | 
 | 2667 | 	} else | 
 | 2668 | 		error = PTR_ERR(dentry); | 
 | 2669 | 	return error; | 
 | 2670 | } | 
| Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 2671 | EXPORT_SYMBOL_GPL(cgroup_add_file); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2672 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2673 | int cgroup_add_files(struct cgroup *cgrp, | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2674 | 			struct cgroup_subsys *subsys, | 
 | 2675 | 			const struct cftype cft[], | 
 | 2676 | 			int count) | 
 | 2677 | { | 
 | 2678 | 	int i, err; | 
 | 2679 | 	for (i = 0; i < count; i++) { | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2680 | 		err = cgroup_add_file(cgrp, subsys, &cft[i]); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2681 | 		if (err) | 
 | 2682 | 			return err; | 
 | 2683 | 	} | 
 | 2684 | 	return 0; | 
 | 2685 | } | 
| Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 2686 | EXPORT_SYMBOL_GPL(cgroup_add_files); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2687 |  | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2688 | /** | 
 | 2689 |  * cgroup_task_count - count the number of tasks in a cgroup. | 
 | 2690 |  * @cgrp: the cgroup in question | 
 | 2691 |  * | 
 | 2692 |  * Return the number of tasks in the cgroup. | 
 | 2693 |  */ | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2694 | int cgroup_task_count(const struct cgroup *cgrp) | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2695 | { | 
 | 2696 | 	int count = 0; | 
| KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 2697 | 	struct cg_cgroup_link *link; | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2698 |  | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2699 | 	read_lock(&css_set_lock); | 
| KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 2700 | 	list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) { | 
| Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 2701 | 		count += atomic_read(&link->cg->refcount); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2702 | 	} | 
 | 2703 | 	read_unlock(&css_set_lock); | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2704 | 	return count; | 
 | 2705 | } | 
 | 2706 |  | 
 | 2707 | /* | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2708 |  * Advance a list_head iterator.  The iterator should be positioned at | 
 | 2709 |  * the start of a css_set | 
 | 2710 |  */ | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2711 | static void cgroup_advance_iter(struct cgroup *cgrp, | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 2712 | 				struct cgroup_iter *it) | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2713 | { | 
 | 2714 | 	struct list_head *l = it->cg_link; | 
 | 2715 | 	struct cg_cgroup_link *link; | 
 | 2716 | 	struct css_set *cg; | 
 | 2717 |  | 
 | 2718 | 	/* Advance to the next non-empty css_set */ | 
 | 2719 | 	do { | 
 | 2720 | 		l = l->next; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2721 | 		if (l == &cgrp->css_sets) { | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2722 | 			it->cg_link = NULL; | 
 | 2723 | 			return; | 
 | 2724 | 		} | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2725 | 		link = list_entry(l, struct cg_cgroup_link, cgrp_link_list); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2726 | 		cg = link->cg; | 
 | 2727 | 	} while (list_empty(&cg->tasks)); | 
 | 2728 | 	it->cg_link = l; | 
 | 2729 | 	it->task = cg->tasks.next; | 
 | 2730 | } | 
 | 2731 |  | 
| Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2732 | /* | 
 | 2733 |  * To reduce the fork() overhead for systems that are not actually | 
 | 2734 |  * using their cgroups capability, we don't maintain the lists running | 
 | 2735 |  * through each css_set to its tasks until we see the list actually | 
 | 2736 |  * used - in other words after the first call to cgroup_iter_start(). | 
| Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2737 |  */ | 
| Adrian Bunk | 3df91fe | 2008-04-29 00:59:54 -0700 | [diff] [blame] | 2738 | static void cgroup_enable_task_cg_lists(void) | 
| Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2739 | { | 
 | 2740 | 	struct task_struct *p, *g; | 
 | 2741 | 	write_lock(&css_set_lock); | 
 | 2742 | 	use_task_css_set_links = 1; | 
| Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2743 | 	/* | 
 | 2744 | 	 * We need tasklist_lock because RCU is not safe against | 
 | 2745 | 	 * while_each_thread(). Besides, a forking task that has passed | 
 | 2746 | 	 * cgroup_post_fork() without seeing use_task_css_set_links = 1 | 
 | 2747 | 	 * is not guaranteed to have its child immediately visible in the | 
 | 2748 | 	 * tasklist if we walk through it with RCU. | 
 | 2749 | 	 */ | 
 | 2750 | 	read_lock(&tasklist_lock); | 
| Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2751 | 	do_each_thread(g, p) { | 
 | 2752 | 		task_lock(p); | 
| Li Zefan | 0e04388 | 2008-04-17 11:37:15 +0800 | [diff] [blame] | 2753 | 		/* | 
 | 2754 | 		 * We should check if the process is exiting, otherwise | 
 | 2755 | 		 * it will race with cgroup_exit() in that the list | 
 | 2756 | 		 * entry won't be deleted though the process has exited. | 
 | 2757 | 		 */ | 
 | 2758 | 		if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list)) | 
| Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2759 | 			list_add(&p->cg_list, &p->cgroups->tasks); | 
 | 2760 | 		task_unlock(p); | 
 | 2761 | 	} while_each_thread(g, p); | 
| Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2762 | 	read_unlock(&tasklist_lock); | 
| Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2763 | 	write_unlock(&css_set_lock); | 
 | 2764 | } | 
 | 2765 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2766 | void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it) | 
| Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 2767 | 	__acquires(css_set_lock) | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2768 | { | 
 | 2769 | 	/* | 
 | 2770 | 	 * The first time anyone tries to iterate across a cgroup, | 
 | 2771 | 	 * we need to enable the list linking each css_set to its | 
 | 2772 | 	 * tasks, and fix up all existing tasks. | 
 | 2773 | 	 */ | 
| Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2774 | 	if (!use_task_css_set_links) | 
 | 2775 | 		cgroup_enable_task_cg_lists(); | 
 | 2776 |  | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2777 | 	read_lock(&css_set_lock); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2778 | 	it->cg_link = &cgrp->css_sets; | 
 | 2779 | 	cgroup_advance_iter(cgrp, it); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2780 | } | 
 | 2781 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2782 | struct task_struct *cgroup_iter_next(struct cgroup *cgrp, | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2783 | 					struct cgroup_iter *it) | 
 | 2784 | { | 
 | 2785 | 	struct task_struct *res; | 
 | 2786 | 	struct list_head *l = it->task; | 
| Lai Jiangshan | 2019f63 | 2009-01-07 18:07:36 -0800 | [diff] [blame] | 2787 | 	struct cg_cgroup_link *link; | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2788 |  | 
 | 2789 | 	/* If the iterator cg is NULL, we have no tasks */ | 
 | 2790 | 	if (!it->cg_link) | 
 | 2791 | 		return NULL; | 
 | 2792 | 	res = list_entry(l, struct task_struct, cg_list); | 
 | 2793 | 	/* Advance iterator to find next entry */ | 
 | 2794 | 	l = l->next; | 
| Lai Jiangshan | 2019f63 | 2009-01-07 18:07:36 -0800 | [diff] [blame] | 2795 | 	link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list); | 
 | 2796 | 	if (l == &link->cg->tasks) { | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2797 | 		/* We reached the end of this task list - move on to | 
 | 2798 | 		 * the next cg_cgroup_link */ | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2799 | 		cgroup_advance_iter(cgrp, it); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2800 | 	} else { | 
 | 2801 | 		it->task = l; | 
 | 2802 | 	} | 
 | 2803 | 	return res; | 
 | 2804 | } | 
 | 2805 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2806 | void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it) | 
| Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 2807 | 	__releases(css_set_lock) | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2808 | { | 
 | 2809 | 	read_unlock(&css_set_lock); | 
 | 2810 | } | 
 | 2811 |  | 
| Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2812 | static inline int started_after_time(struct task_struct *t1, | 
 | 2813 | 				     struct timespec *time, | 
 | 2814 | 				     struct task_struct *t2) | 
 | 2815 | { | 
 | 2816 | 	int start_diff = timespec_compare(&t1->start_time, time); | 
 | 2817 | 	if (start_diff > 0) { | 
 | 2818 | 		return 1; | 
 | 2819 | 	} else if (start_diff < 0) { | 
 | 2820 | 		return 0; | 
 | 2821 | 	} else { | 
 | 2822 | 		/* | 
 | 2823 | 		 * Arbitrarily, if two processes started at the same | 
 | 2824 | 		 * time, we'll say that the lower pointer value | 
 | 2825 | 		 * started first. Note that t2 may have exited by now | 
 | 2826 | 		 * so this may not be a valid pointer any longer, but | 
 | 2827 | 		 * that's fine - it still serves to distinguish | 
 | 2828 | 		 * between two tasks started (effectively) simultaneously. | 
 | 2829 | 		 */ | 
 | 2830 | 		return t1 > t2; | 
 | 2831 | 	} | 
 | 2832 | } | 
 | 2833 |  | 
 | 2834 | /* | 
 | 2835 |  * This function is a callback from heap_insert() and is used to order | 
 | 2836 |  * the heap. | 
 | 2837 |  * In this case we order the heap in descending task start time. | 
 | 2838 |  */ | 
 | 2839 | static inline int started_after(void *p1, void *p2) | 
 | 2840 | { | 
 | 2841 | 	struct task_struct *t1 = p1; | 
 | 2842 | 	struct task_struct *t2 = p2; | 
 | 2843 | 	return started_after_time(t1, &t2->start_time, t2); | 
 | 2844 | } | 
 | 2845 |  | 
 | 2846 | /** | 
 | 2847 |  * cgroup_scan_tasks - iterate though all the tasks in a cgroup | 
 | 2848 |  * @scan: struct cgroup_scanner containing arguments for the scan | 
 | 2849 |  * | 
 | 2850 |  * Arguments include pointers to callback functions test_task() and | 
 | 2851 |  * process_task(). | 
 | 2852 |  * Iterate through all the tasks in a cgroup, calling test_task() for each, | 
 | 2853 |  * and if it returns true, call process_task() for it also. | 
 | 2854 |  * The test_task pointer may be NULL, meaning always true (select all tasks). | 
 | 2855 |  * Effectively duplicates cgroup_iter_{start,next,end}() | 
 | 2856 |  * but does not lock css_set_lock for the call to process_task(). | 
 | 2857 |  * The struct cgroup_scanner may be embedded in any structure of the caller's | 
 | 2858 |  * creation. | 
 | 2859 |  * It is guaranteed that process_task() will act on every task that | 
 | 2860 |  * is a member of the cgroup for the duration of this call. This | 
 | 2861 |  * function may or may not call process_task() for tasks that exit | 
 | 2862 |  * or move to a different cgroup during the call, or are forked or | 
 | 2863 |  * move into the cgroup during the call. | 
 | 2864 |  * | 
 | 2865 |  * Note that test_task() may be called with locks held, and may in some | 
 | 2866 |  * situations be called multiple times for the same task, so it should | 
 | 2867 |  * be cheap. | 
 | 2868 |  * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been | 
 | 2869 |  * pre-allocated and will be used for heap operations (and its "gt" member will | 
 | 2870 |  * be overwritten), else a temporary heap will be used (allocation of which | 
 | 2871 |  * may cause this function to fail). | 
 | 2872 |  */ | 
 | 2873 | int cgroup_scan_tasks(struct cgroup_scanner *scan) | 
 | 2874 | { | 
 | 2875 | 	int retval, i; | 
 | 2876 | 	struct cgroup_iter it; | 
 | 2877 | 	struct task_struct *p, *dropped; | 
 | 2878 | 	/* Never dereference latest_task, since it's not refcounted */ | 
 | 2879 | 	struct task_struct *latest_task = NULL; | 
 | 2880 | 	struct ptr_heap tmp_heap; | 
 | 2881 | 	struct ptr_heap *heap; | 
 | 2882 | 	struct timespec latest_time = { 0, 0 }; | 
 | 2883 |  | 
 | 2884 | 	if (scan->heap) { | 
 | 2885 | 		/* The caller supplied our heap and pre-allocated its memory */ | 
 | 2886 | 		heap = scan->heap; | 
 | 2887 | 		heap->gt = &started_after; | 
 | 2888 | 	} else { | 
 | 2889 | 		/* We need to allocate our own heap memory */ | 
 | 2890 | 		heap = &tmp_heap; | 
 | 2891 | 		retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after); | 
 | 2892 | 		if (retval) | 
 | 2893 | 			/* cannot allocate the heap */ | 
 | 2894 | 			return retval; | 
 | 2895 | 	} | 
 | 2896 |  | 
 | 2897 |  again: | 
 | 2898 | 	/* | 
 | 2899 | 	 * Scan tasks in the cgroup, using the scanner's "test_task" callback | 
 | 2900 | 	 * to determine which are of interest, and using the scanner's | 
 | 2901 | 	 * "process_task" callback to process any of them that need an update. | 
 | 2902 | 	 * Since we don't want to hold any locks during the task updates, | 
 | 2903 | 	 * gather tasks to be processed in a heap structure. | 
 | 2904 | 	 * The heap is sorted by descending task start time. | 
 | 2905 | 	 * If the statically-sized heap fills up, we overflow tasks that | 
 | 2906 | 	 * started later, and in future iterations only consider tasks that | 
 | 2907 | 	 * started after the latest task in the previous pass. This | 
 | 2908 | 	 * guarantees forward progress and that we don't miss any tasks. | 
 | 2909 | 	 */ | 
 | 2910 | 	heap->size = 0; | 
 | 2911 | 	cgroup_iter_start(scan->cg, &it); | 
 | 2912 | 	while ((p = cgroup_iter_next(scan->cg, &it))) { | 
 | 2913 | 		/* | 
 | 2914 | 		 * Only affect tasks that qualify per the caller's callback, | 
 | 2915 | 		 * if he provided one | 
 | 2916 | 		 */ | 
 | 2917 | 		if (scan->test_task && !scan->test_task(p, scan)) | 
 | 2918 | 			continue; | 
 | 2919 | 		/* | 
 | 2920 | 		 * Only process tasks that started after the last task | 
 | 2921 | 		 * we processed | 
 | 2922 | 		 */ | 
 | 2923 | 		if (!started_after_time(p, &latest_time, latest_task)) | 
 | 2924 | 			continue; | 
 | 2925 | 		dropped = heap_insert(heap, p); | 
 | 2926 | 		if (dropped == NULL) { | 
 | 2927 | 			/* | 
 | 2928 | 			 * The new task was inserted; the heap wasn't | 
 | 2929 | 			 * previously full | 
 | 2930 | 			 */ | 
 | 2931 | 			get_task_struct(p); | 
 | 2932 | 		} else if (dropped != p) { | 
 | 2933 | 			/* | 
 | 2934 | 			 * The new task was inserted, and pushed out a | 
 | 2935 | 			 * different task | 
 | 2936 | 			 */ | 
 | 2937 | 			get_task_struct(p); | 
 | 2938 | 			put_task_struct(dropped); | 
 | 2939 | 		} | 
 | 2940 | 		/* | 
 | 2941 | 		 * Else the new task was newer than anything already in | 
 | 2942 | 		 * the heap and wasn't inserted | 
 | 2943 | 		 */ | 
 | 2944 | 	} | 
 | 2945 | 	cgroup_iter_end(scan->cg, &it); | 
 | 2946 |  | 
 | 2947 | 	if (heap->size) { | 
 | 2948 | 		for (i = 0; i < heap->size; i++) { | 
| Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 2949 | 			struct task_struct *q = heap->ptrs[i]; | 
| Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2950 | 			if (i == 0) { | 
| Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 2951 | 				latest_time = q->start_time; | 
 | 2952 | 				latest_task = q; | 
| Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2953 | 			} | 
 | 2954 | 			/* Process the task per the caller's callback */ | 
| Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 2955 | 			scan->process_task(q, scan); | 
 | 2956 | 			put_task_struct(q); | 
| Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2957 | 		} | 
 | 2958 | 		/* | 
 | 2959 | 		 * If we had to process any tasks at all, scan again | 
 | 2960 | 		 * in case some of them were in the middle of forking | 
 | 2961 | 		 * children that didn't get processed. | 
 | 2962 | 		 * Not the most efficient way to do it, but it avoids | 
 | 2963 | 		 * having to take callback_mutex in the fork path | 
 | 2964 | 		 */ | 
 | 2965 | 		goto again; | 
 | 2966 | 	} | 
 | 2967 | 	if (heap == &tmp_heap) | 
 | 2968 | 		heap_free(&tmp_heap); | 
 | 2969 | 	return 0; | 
 | 2970 | } | 
 | 2971 |  | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2972 | /* | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2973 |  * Stuff for reading the 'tasks'/'procs' files. | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2974 |  * | 
 | 2975 |  * Reading this file can return large amounts of data if a cgroup has | 
 | 2976 |  * *lots* of attached tasks. So it may need several calls to read(), | 
 | 2977 |  * but we cannot guarantee that the information we produce is correct | 
 | 2978 |  * unless we produce it entirely atomically. | 
 | 2979 |  * | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2980 |  */ | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2981 |  | 
| Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 2982 | /* which pidlist file are we talking about? */ | 
 | 2983 | enum cgroup_filetype { | 
 | 2984 | 	CGROUP_FILE_PROCS, | 
 | 2985 | 	CGROUP_FILE_TASKS, | 
 | 2986 | }; | 
 | 2987 |  | 
 | 2988 | /* | 
 | 2989 |  * A pidlist is a list of pids that virtually represents the contents of one | 
 | 2990 |  * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists, | 
 | 2991 |  * a pair (one each for procs, tasks) for each pid namespace that's relevant | 
 | 2992 |  * to the cgroup. | 
 | 2993 |  */ | 
 | 2994 | struct cgroup_pidlist { | 
 | 2995 | 	/* | 
 | 2996 | 	 * used to find which pidlist is wanted. doesn't change as long as | 
 | 2997 | 	 * this particular list stays in the list. | 
 | 2998 | 	*/ | 
 | 2999 | 	struct { enum cgroup_filetype type; struct pid_namespace *ns; } key; | 
 | 3000 | 	/* array of xids */ | 
 | 3001 | 	pid_t *list; | 
 | 3002 | 	/* how many elements the above list has */ | 
 | 3003 | 	int length; | 
 | 3004 | 	/* how many files are using the current array */ | 
 | 3005 | 	int use_count; | 
 | 3006 | 	/* each of these stored in a list by its cgroup */ | 
 | 3007 | 	struct list_head links; | 
 | 3008 | 	/* pointer to the cgroup we belong to, for list removal purposes */ | 
 | 3009 | 	struct cgroup *owner; | 
 | 3010 | 	/* protects the other fields */ | 
 | 3011 | 	struct rw_semaphore mutex; | 
 | 3012 | }; | 
 | 3013 |  | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3014 | /* | 
| Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3015 |  * The following two functions "fix" the issue where there are more pids | 
 | 3016 |  * than kmalloc will give memory for; in such cases, we use vmalloc/vfree. | 
 | 3017 |  * TODO: replace with a kernel-wide solution to this problem | 
 | 3018 |  */ | 
 | 3019 | #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2)) | 
 | 3020 | static void *pidlist_allocate(int count) | 
 | 3021 | { | 
 | 3022 | 	if (PIDLIST_TOO_LARGE(count)) | 
 | 3023 | 		return vmalloc(count * sizeof(pid_t)); | 
 | 3024 | 	else | 
 | 3025 | 		return kmalloc(count * sizeof(pid_t), GFP_KERNEL); | 
 | 3026 | } | 
 | 3027 | static void pidlist_free(void *p) | 
 | 3028 | { | 
 | 3029 | 	if (is_vmalloc_addr(p)) | 
 | 3030 | 		vfree(p); | 
 | 3031 | 	else | 
 | 3032 | 		kfree(p); | 
 | 3033 | } | 
 | 3034 | static void *pidlist_resize(void *p, int newcount) | 
 | 3035 | { | 
 | 3036 | 	void *newlist; | 
 | 3037 | 	/* note: if new alloc fails, old p will still be valid either way */ | 
 | 3038 | 	if (is_vmalloc_addr(p)) { | 
 | 3039 | 		newlist = vmalloc(newcount * sizeof(pid_t)); | 
 | 3040 | 		if (!newlist) | 
 | 3041 | 			return NULL; | 
 | 3042 | 		memcpy(newlist, p, newcount * sizeof(pid_t)); | 
 | 3043 | 		vfree(p); | 
 | 3044 | 	} else { | 
 | 3045 | 		newlist = krealloc(p, newcount * sizeof(pid_t), GFP_KERNEL); | 
 | 3046 | 	} | 
 | 3047 | 	return newlist; | 
 | 3048 | } | 
 | 3049 |  | 
 | 3050 | /* | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3051 |  * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries | 
 | 3052 |  * If the new stripped list is sufficiently smaller and there's enough memory | 
 | 3053 |  * to allocate a new buffer, will let go of the unneeded memory. Returns the | 
 | 3054 |  * number of unique elements. | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3055 |  */ | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3056 | /* is the size difference enough that we should re-allocate the array? */ | 
 | 3057 | #define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new)) | 
 | 3058 | static int pidlist_uniq(pid_t **p, int length) | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3059 | { | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3060 | 	int src, dest = 1; | 
 | 3061 | 	pid_t *list = *p; | 
 | 3062 | 	pid_t *newlist; | 
 | 3063 |  | 
 | 3064 | 	/* | 
 | 3065 | 	 * we presume the 0th element is unique, so i starts at 1. trivial | 
 | 3066 | 	 * edge cases first; no work needs to be done for either | 
 | 3067 | 	 */ | 
 | 3068 | 	if (length == 0 || length == 1) | 
 | 3069 | 		return length; | 
 | 3070 | 	/* src and dest walk down the list; dest counts unique elements */ | 
 | 3071 | 	for (src = 1; src < length; src++) { | 
 | 3072 | 		/* find next unique element */ | 
 | 3073 | 		while (list[src] == list[src-1]) { | 
 | 3074 | 			src++; | 
 | 3075 | 			if (src == length) | 
 | 3076 | 				goto after; | 
 | 3077 | 		} | 
 | 3078 | 		/* dest always points to where the next unique element goes */ | 
 | 3079 | 		list[dest] = list[src]; | 
 | 3080 | 		dest++; | 
 | 3081 | 	} | 
 | 3082 | after: | 
 | 3083 | 	/* | 
 | 3084 | 	 * if the length difference is large enough, we want to allocate a | 
 | 3085 | 	 * smaller buffer to save memory. if this fails due to out of memory, | 
 | 3086 | 	 * we'll just stay with what we've got. | 
 | 3087 | 	 */ | 
 | 3088 | 	if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) { | 
| Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3089 | 		newlist = pidlist_resize(list, dest); | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3090 | 		if (newlist) | 
 | 3091 | 			*p = newlist; | 
 | 3092 | 	} | 
 | 3093 | 	return dest; | 
 | 3094 | } | 
 | 3095 |  | 
 | 3096 | static int cmppid(const void *a, const void *b) | 
 | 3097 | { | 
 | 3098 | 	return *(pid_t *)a - *(pid_t *)b; | 
 | 3099 | } | 
 | 3100 |  | 
 | 3101 | /* | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3102 |  * find the appropriate pidlist for our purpose (given procs vs tasks) | 
 | 3103 |  * returns with the lock on that pidlist already held, and takes care | 
 | 3104 |  * of the use count, or returns NULL with no locks held if we're out of | 
 | 3105 |  * memory. | 
 | 3106 |  */ | 
 | 3107 | static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp, | 
 | 3108 | 						  enum cgroup_filetype type) | 
 | 3109 | { | 
 | 3110 | 	struct cgroup_pidlist *l; | 
 | 3111 | 	/* don't need task_nsproxy() if we're looking at ourself */ | 
| Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3112 | 	struct pid_namespace *ns = current->nsproxy->pid_ns; | 
 | 3113 |  | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3114 | 	/* | 
 | 3115 | 	 * We can't drop the pidlist_mutex before taking the l->mutex in case | 
 | 3116 | 	 * the last ref-holder is trying to remove l from the list at the same | 
 | 3117 | 	 * time. Holding the pidlist_mutex precludes somebody taking whichever | 
 | 3118 | 	 * list we find out from under us - compare release_pid_array(). | 
 | 3119 | 	 */ | 
 | 3120 | 	mutex_lock(&cgrp->pidlist_mutex); | 
 | 3121 | 	list_for_each_entry(l, &cgrp->pidlists, links) { | 
 | 3122 | 		if (l->key.type == type && l->key.ns == ns) { | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3123 | 			/* make sure l doesn't vanish out from under us */ | 
 | 3124 | 			down_write(&l->mutex); | 
 | 3125 | 			mutex_unlock(&cgrp->pidlist_mutex); | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3126 | 			return l; | 
 | 3127 | 		} | 
 | 3128 | 	} | 
 | 3129 | 	/* entry not found; create a new one */ | 
 | 3130 | 	l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL); | 
 | 3131 | 	if (!l) { | 
 | 3132 | 		mutex_unlock(&cgrp->pidlist_mutex); | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3133 | 		return l; | 
 | 3134 | 	} | 
 | 3135 | 	init_rwsem(&l->mutex); | 
 | 3136 | 	down_write(&l->mutex); | 
 | 3137 | 	l->key.type = type; | 
| Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3138 | 	l->key.ns = get_pid_ns(ns); | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3139 | 	l->use_count = 0; /* don't increment here */ | 
 | 3140 | 	l->list = NULL; | 
 | 3141 | 	l->owner = cgrp; | 
 | 3142 | 	list_add(&l->links, &cgrp->pidlists); | 
 | 3143 | 	mutex_unlock(&cgrp->pidlist_mutex); | 
 | 3144 | 	return l; | 
 | 3145 | } | 
 | 3146 |  | 
 | 3147 | /* | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3148 |  * Load a cgroup's pidarray with either procs' tgids or tasks' pids | 
 | 3149 |  */ | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3150 | static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, | 
 | 3151 | 			      struct cgroup_pidlist **lp) | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3152 | { | 
 | 3153 | 	pid_t *array; | 
 | 3154 | 	int length; | 
 | 3155 | 	int pid, n = 0; /* used for populating the array */ | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3156 | 	struct cgroup_iter it; | 
 | 3157 | 	struct task_struct *tsk; | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3158 | 	struct cgroup_pidlist *l; | 
 | 3159 |  | 
 | 3160 | 	/* | 
 | 3161 | 	 * If cgroup gets more users after we read count, we won't have | 
 | 3162 | 	 * enough space - tough.  This race is indistinguishable to the | 
 | 3163 | 	 * caller from the case that the additional cgroup users didn't | 
 | 3164 | 	 * show up until sometime later on. | 
 | 3165 | 	 */ | 
 | 3166 | 	length = cgroup_task_count(cgrp); | 
| Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3167 | 	array = pidlist_allocate(length); | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3168 | 	if (!array) | 
 | 3169 | 		return -ENOMEM; | 
 | 3170 | 	/* now, populate the array */ | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3171 | 	cgroup_iter_start(cgrp, &it); | 
 | 3172 | 	while ((tsk = cgroup_iter_next(cgrp, &it))) { | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3173 | 		if (unlikely(n == length)) | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3174 | 			break; | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3175 | 		/* get tgid or pid for procs or tasks file respectively */ | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3176 | 		if (type == CGROUP_FILE_PROCS) | 
 | 3177 | 			pid = task_tgid_vnr(tsk); | 
 | 3178 | 		else | 
 | 3179 | 			pid = task_pid_vnr(tsk); | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3180 | 		if (pid > 0) /* make sure to only use valid results */ | 
 | 3181 | 			array[n++] = pid; | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3182 | 	} | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3183 | 	cgroup_iter_end(cgrp, &it); | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3184 | 	length = n; | 
 | 3185 | 	/* now sort & (if procs) strip out duplicates */ | 
 | 3186 | 	sort(array, length, sizeof(pid_t), cmppid, NULL); | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3187 | 	if (type == CGROUP_FILE_PROCS) | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3188 | 		length = pidlist_uniq(&array, length); | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3189 | 	l = cgroup_pidlist_find(cgrp, type); | 
 | 3190 | 	if (!l) { | 
| Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3191 | 		pidlist_free(array); | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3192 | 		return -ENOMEM; | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3193 | 	} | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3194 | 	/* store array, freeing old if necessary - lock already held */ | 
| Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3195 | 	pidlist_free(l->list); | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3196 | 	l->list = array; | 
 | 3197 | 	l->length = length; | 
 | 3198 | 	l->use_count++; | 
 | 3199 | 	up_write(&l->mutex); | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3200 | 	*lp = l; | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3201 | 	return 0; | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3202 | } | 
 | 3203 |  | 
| Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3204 | /** | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3205 |  * cgroupstats_build - build and fill cgroupstats | 
| Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3206 |  * @stats: cgroupstats to fill information into | 
 | 3207 |  * @dentry: A dentry entry belonging to the cgroup for which stats have | 
 | 3208 |  * been requested. | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3209 |  * | 
 | 3210 |  * Build and fill cgroupstats so that taskstats can export it to user | 
 | 3211 |  * space. | 
| Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3212 |  */ | 
 | 3213 | int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) | 
 | 3214 | { | 
 | 3215 | 	int ret = -EINVAL; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3216 | 	struct cgroup *cgrp; | 
| Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3217 | 	struct cgroup_iter it; | 
 | 3218 | 	struct task_struct *tsk; | 
| Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3219 |  | 
| Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3220 | 	/* | 
| Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3221 | 	 * Validate dentry by checking the superblock operations, | 
 | 3222 | 	 * and make sure it's a directory. | 
| Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3223 | 	 */ | 
| Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3224 | 	if (dentry->d_sb->s_op != &cgroup_ops || | 
 | 3225 | 	    !S_ISDIR(dentry->d_inode->i_mode)) | 
| Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3226 | 		 goto err; | 
 | 3227 |  | 
 | 3228 | 	ret = 0; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3229 | 	cgrp = dentry->d_fsdata; | 
| Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3230 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3231 | 	cgroup_iter_start(cgrp, &it); | 
 | 3232 | 	while ((tsk = cgroup_iter_next(cgrp, &it))) { | 
| Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3233 | 		switch (tsk->state) { | 
 | 3234 | 		case TASK_RUNNING: | 
 | 3235 | 			stats->nr_running++; | 
 | 3236 | 			break; | 
 | 3237 | 		case TASK_INTERRUPTIBLE: | 
 | 3238 | 			stats->nr_sleeping++; | 
 | 3239 | 			break; | 
 | 3240 | 		case TASK_UNINTERRUPTIBLE: | 
 | 3241 | 			stats->nr_uninterruptible++; | 
 | 3242 | 			break; | 
 | 3243 | 		case TASK_STOPPED: | 
 | 3244 | 			stats->nr_stopped++; | 
 | 3245 | 			break; | 
 | 3246 | 		default: | 
 | 3247 | 			if (delayacct_is_task_waiting_on_io(tsk)) | 
 | 3248 | 				stats->nr_io_wait++; | 
 | 3249 | 			break; | 
 | 3250 | 		} | 
 | 3251 | 	} | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3252 | 	cgroup_iter_end(cgrp, &it); | 
| Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3253 |  | 
| Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3254 | err: | 
 | 3255 | 	return ret; | 
 | 3256 | } | 
 | 3257 |  | 
| Paul Menage | 8f3ff20 | 2009-09-23 15:56:25 -0700 | [diff] [blame] | 3258 |  | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3259 | /* | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3260 |  * seq_file methods for the tasks/procs files. The seq_file position is the | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3261 |  * next pid to display; the seq_file iterator is a pointer to the pid | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3262 |  * in the cgroup->l->list array. | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3263 |  */ | 
 | 3264 |  | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3265 | static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos) | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3266 | { | 
 | 3267 | 	/* | 
 | 3268 | 	 * Initially we receive a position value that corresponds to | 
 | 3269 | 	 * one more than the last pid shown (or 0 on the first call or | 
 | 3270 | 	 * after a seek to the start). Use a binary-search to find the | 
 | 3271 | 	 * next pid to display, if any | 
 | 3272 | 	 */ | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3273 | 	struct cgroup_pidlist *l = s->private; | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3274 | 	int index = 0, pid = *pos; | 
 | 3275 | 	int *iter; | 
 | 3276 |  | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3277 | 	down_read(&l->mutex); | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3278 | 	if (pid) { | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3279 | 		int end = l->length; | 
| Stephen Rothwell | 2077776 | 2008-10-21 16:11:20 +1100 | [diff] [blame] | 3280 |  | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3281 | 		while (index < end) { | 
 | 3282 | 			int mid = (index + end) / 2; | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3283 | 			if (l->list[mid] == pid) { | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3284 | 				index = mid; | 
 | 3285 | 				break; | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3286 | 			} else if (l->list[mid] <= pid) | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3287 | 				index = mid + 1; | 
 | 3288 | 			else | 
 | 3289 | 				end = mid; | 
 | 3290 | 		} | 
 | 3291 | 	} | 
 | 3292 | 	/* If we're off the end of the array, we're done */ | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3293 | 	if (index >= l->length) | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3294 | 		return NULL; | 
 | 3295 | 	/* Update the abstract position to be the actual pid that we found */ | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3296 | 	iter = l->list + index; | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3297 | 	*pos = *iter; | 
 | 3298 | 	return iter; | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3299 | } | 
 | 3300 |  | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3301 | static void cgroup_pidlist_stop(struct seq_file *s, void *v) | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3302 | { | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3303 | 	struct cgroup_pidlist *l = s->private; | 
 | 3304 | 	up_read(&l->mutex); | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3305 | } | 
 | 3306 |  | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3307 | static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos) | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3308 | { | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3309 | 	struct cgroup_pidlist *l = s->private; | 
 | 3310 | 	pid_t *p = v; | 
 | 3311 | 	pid_t *end = l->list + l->length; | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3312 | 	/* | 
 | 3313 | 	 * Advance to the next pid in the array. If this goes off the | 
 | 3314 | 	 * end, we're done | 
 | 3315 | 	 */ | 
 | 3316 | 	p++; | 
 | 3317 | 	if (p >= end) { | 
 | 3318 | 		return NULL; | 
 | 3319 | 	} else { | 
 | 3320 | 		*pos = *p; | 
 | 3321 | 		return p; | 
 | 3322 | 	} | 
 | 3323 | } | 
 | 3324 |  | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3325 | static int cgroup_pidlist_show(struct seq_file *s, void *v) | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3326 | { | 
 | 3327 | 	return seq_printf(s, "%d\n", *(int *)v); | 
 | 3328 | } | 
 | 3329 |  | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3330 | /* | 
 | 3331 |  * seq_operations functions for iterating on pidlists through seq_file - | 
 | 3332 |  * independent of whether it's tasks or procs | 
 | 3333 |  */ | 
 | 3334 | static const struct seq_operations cgroup_pidlist_seq_operations = { | 
 | 3335 | 	.start = cgroup_pidlist_start, | 
 | 3336 | 	.stop = cgroup_pidlist_stop, | 
 | 3337 | 	.next = cgroup_pidlist_next, | 
 | 3338 | 	.show = cgroup_pidlist_show, | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3339 | }; | 
 | 3340 |  | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3341 | static void cgroup_release_pid_array(struct cgroup_pidlist *l) | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3342 | { | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3343 | 	/* | 
 | 3344 | 	 * the case where we're the last user of this particular pidlist will | 
 | 3345 | 	 * have us remove it from the cgroup's list, which entails taking the | 
 | 3346 | 	 * mutex. since in pidlist_find the pidlist->lock depends on cgroup-> | 
 | 3347 | 	 * pidlist_mutex, we have to take pidlist_mutex first. | 
 | 3348 | 	 */ | 
 | 3349 | 	mutex_lock(&l->owner->pidlist_mutex); | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3350 | 	down_write(&l->mutex); | 
 | 3351 | 	BUG_ON(!l->use_count); | 
 | 3352 | 	if (!--l->use_count) { | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3353 | 		/* we're the last user if refcount is 0; remove and free */ | 
 | 3354 | 		list_del(&l->links); | 
 | 3355 | 		mutex_unlock(&l->owner->pidlist_mutex); | 
| Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3356 | 		pidlist_free(l->list); | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3357 | 		put_pid_ns(l->key.ns); | 
 | 3358 | 		up_write(&l->mutex); | 
 | 3359 | 		kfree(l); | 
 | 3360 | 		return; | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3361 | 	} | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3362 | 	mutex_unlock(&l->owner->pidlist_mutex); | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3363 | 	up_write(&l->mutex); | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3364 | } | 
 | 3365 |  | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3366 | static int cgroup_pidlist_release(struct inode *inode, struct file *file) | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3367 | { | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3368 | 	struct cgroup_pidlist *l; | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3369 | 	if (!(file->f_mode & FMODE_READ)) | 
 | 3370 | 		return 0; | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3371 | 	/* | 
 | 3372 | 	 * the seq_file will only be initialized if the file was opened for | 
 | 3373 | 	 * reading; hence we check if it's not null only in that case. | 
 | 3374 | 	 */ | 
 | 3375 | 	l = ((struct seq_file *)file->private_data)->private; | 
 | 3376 | 	cgroup_release_pid_array(l); | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3377 | 	return seq_release(inode, file); | 
 | 3378 | } | 
 | 3379 |  | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3380 | static const struct file_operations cgroup_pidlist_operations = { | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3381 | 	.read = seq_read, | 
 | 3382 | 	.llseek = seq_lseek, | 
 | 3383 | 	.write = cgroup_file_write, | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3384 | 	.release = cgroup_pidlist_release, | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3385 | }; | 
 | 3386 |  | 
 | 3387 | /* | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3388 |  * The following functions handle opens on a file that displays a pidlist | 
 | 3389 |  * (tasks or procs). Prepare an array of the process/thread IDs of whoever's | 
 | 3390 |  * in the cgroup. | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3391 |  */ | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3392 | /* helper function for the two below it */ | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3393 | static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type) | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3394 | { | 
 | 3395 | 	struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3396 | 	struct cgroup_pidlist *l; | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3397 | 	int retval; | 
 | 3398 |  | 
 | 3399 | 	/* Nothing to do for write-only files */ | 
 | 3400 | 	if (!(file->f_mode & FMODE_READ)) | 
 | 3401 | 		return 0; | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3402 |  | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3403 | 	/* have the array populated */ | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3404 | 	retval = pidlist_array_load(cgrp, type, &l); | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3405 | 	if (retval) | 
 | 3406 | 		return retval; | 
 | 3407 | 	/* configure file information */ | 
 | 3408 | 	file->f_op = &cgroup_pidlist_operations; | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3409 |  | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3410 | 	retval = seq_open(file, &cgroup_pidlist_seq_operations); | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3411 | 	if (retval) { | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3412 | 		cgroup_release_pid_array(l); | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3413 | 		return retval; | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3414 | 	} | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3415 | 	((struct seq_file *)file->private_data)->private = l; | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3416 | 	return 0; | 
 | 3417 | } | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3418 | static int cgroup_tasks_open(struct inode *unused, struct file *file) | 
 | 3419 | { | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3420 | 	return cgroup_pidlist_open(file, CGROUP_FILE_TASKS); | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3421 | } | 
 | 3422 | static int cgroup_procs_open(struct inode *unused, struct file *file) | 
 | 3423 | { | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3424 | 	return cgroup_pidlist_open(file, CGROUP_FILE_PROCS); | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3425 | } | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3426 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3427 | static u64 cgroup_read_notify_on_release(struct cgroup *cgrp, | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3428 | 					    struct cftype *cft) | 
 | 3429 | { | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3430 | 	return notify_on_release(cgrp); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3431 | } | 
 | 3432 |  | 
| Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3433 | static int cgroup_write_notify_on_release(struct cgroup *cgrp, | 
 | 3434 | 					  struct cftype *cft, | 
 | 3435 | 					  u64 val) | 
 | 3436 | { | 
 | 3437 | 	clear_bit(CGRP_RELEASABLE, &cgrp->flags); | 
 | 3438 | 	if (val) | 
 | 3439 | 		set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); | 
 | 3440 | 	else | 
 | 3441 | 		clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); | 
 | 3442 | 	return 0; | 
 | 3443 | } | 
 | 3444 |  | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3445 | /* | 
| Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3446 |  * Unregister event and free resources. | 
 | 3447 |  * | 
 | 3448 |  * Gets called from workqueue. | 
 | 3449 |  */ | 
 | 3450 | static void cgroup_event_remove(struct work_struct *work) | 
 | 3451 | { | 
 | 3452 | 	struct cgroup_event *event = container_of(work, struct cgroup_event, | 
 | 3453 | 			remove); | 
 | 3454 | 	struct cgroup *cgrp = event->cgrp; | 
 | 3455 |  | 
| Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3456 | 	event->cft->unregister_event(cgrp, event->cft, event->eventfd); | 
 | 3457 |  | 
 | 3458 | 	eventfd_ctx_put(event->eventfd); | 
| Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3459 | 	kfree(event); | 
| Kirill A. Shutemov | a0a4db5 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 3460 | 	dput(cgrp->dentry); | 
| Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3461 | } | 
 | 3462 |  | 
 | 3463 | /* | 
 | 3464 |  * Gets called on POLLHUP on eventfd when user closes it. | 
 | 3465 |  * | 
 | 3466 |  * Called with wqh->lock held and interrupts disabled. | 
 | 3467 |  */ | 
 | 3468 | static int cgroup_event_wake(wait_queue_t *wait, unsigned mode, | 
 | 3469 | 		int sync, void *key) | 
 | 3470 | { | 
 | 3471 | 	struct cgroup_event *event = container_of(wait, | 
 | 3472 | 			struct cgroup_event, wait); | 
 | 3473 | 	struct cgroup *cgrp = event->cgrp; | 
 | 3474 | 	unsigned long flags = (unsigned long)key; | 
 | 3475 |  | 
 | 3476 | 	if (flags & POLLHUP) { | 
| Changli Gao | a93d2f1 | 2010-05-07 14:33:26 +0800 | [diff] [blame] | 3477 | 		__remove_wait_queue(event->wqh, &event->wait); | 
| Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3478 | 		spin_lock(&cgrp->event_list_lock); | 
 | 3479 | 		list_del(&event->list); | 
 | 3480 | 		spin_unlock(&cgrp->event_list_lock); | 
 | 3481 | 		/* | 
 | 3482 | 		 * We are in atomic context, but cgroup_event_remove() may | 
 | 3483 | 		 * sleep, so we have to call it in workqueue. | 
 | 3484 | 		 */ | 
 | 3485 | 		schedule_work(&event->remove); | 
 | 3486 | 	} | 
 | 3487 |  | 
 | 3488 | 	return 0; | 
 | 3489 | } | 
 | 3490 |  | 
 | 3491 | static void cgroup_event_ptable_queue_proc(struct file *file, | 
 | 3492 | 		wait_queue_head_t *wqh, poll_table *pt) | 
 | 3493 | { | 
 | 3494 | 	struct cgroup_event *event = container_of(pt, | 
 | 3495 | 			struct cgroup_event, pt); | 
 | 3496 |  | 
 | 3497 | 	event->wqh = wqh; | 
 | 3498 | 	add_wait_queue(wqh, &event->wait); | 
 | 3499 | } | 
 | 3500 |  | 
 | 3501 | /* | 
 | 3502 |  * Parse input and register new cgroup event handler. | 
 | 3503 |  * | 
 | 3504 |  * Input must be in format '<event_fd> <control_fd> <args>'. | 
 | 3505 |  * Interpretation of args is defined by control file implementation. | 
 | 3506 |  */ | 
 | 3507 | static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft, | 
 | 3508 | 				      const char *buffer) | 
 | 3509 | { | 
 | 3510 | 	struct cgroup_event *event = NULL; | 
 | 3511 | 	unsigned int efd, cfd; | 
 | 3512 | 	struct file *efile = NULL; | 
 | 3513 | 	struct file *cfile = NULL; | 
 | 3514 | 	char *endp; | 
 | 3515 | 	int ret; | 
 | 3516 |  | 
 | 3517 | 	efd = simple_strtoul(buffer, &endp, 10); | 
 | 3518 | 	if (*endp != ' ') | 
 | 3519 | 		return -EINVAL; | 
 | 3520 | 	buffer = endp + 1; | 
 | 3521 |  | 
 | 3522 | 	cfd = simple_strtoul(buffer, &endp, 10); | 
 | 3523 | 	if ((*endp != ' ') && (*endp != '\0')) | 
 | 3524 | 		return -EINVAL; | 
 | 3525 | 	buffer = endp + 1; | 
 | 3526 |  | 
 | 3527 | 	event = kzalloc(sizeof(*event), GFP_KERNEL); | 
 | 3528 | 	if (!event) | 
 | 3529 | 		return -ENOMEM; | 
 | 3530 | 	event->cgrp = cgrp; | 
 | 3531 | 	INIT_LIST_HEAD(&event->list); | 
 | 3532 | 	init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc); | 
 | 3533 | 	init_waitqueue_func_entry(&event->wait, cgroup_event_wake); | 
 | 3534 | 	INIT_WORK(&event->remove, cgroup_event_remove); | 
 | 3535 |  | 
 | 3536 | 	efile = eventfd_fget(efd); | 
 | 3537 | 	if (IS_ERR(efile)) { | 
 | 3538 | 		ret = PTR_ERR(efile); | 
 | 3539 | 		goto fail; | 
 | 3540 | 	} | 
 | 3541 |  | 
 | 3542 | 	event->eventfd = eventfd_ctx_fileget(efile); | 
 | 3543 | 	if (IS_ERR(event->eventfd)) { | 
 | 3544 | 		ret = PTR_ERR(event->eventfd); | 
 | 3545 | 		goto fail; | 
 | 3546 | 	} | 
 | 3547 |  | 
 | 3548 | 	cfile = fget(cfd); | 
 | 3549 | 	if (!cfile) { | 
 | 3550 | 		ret = -EBADF; | 
 | 3551 | 		goto fail; | 
 | 3552 | 	} | 
 | 3553 |  | 
 | 3554 | 	/* the process need read permission on control file */ | 
| Al Viro | 3bfa784 | 2011-06-19 12:55:10 -0400 | [diff] [blame] | 3555 | 	/* AV: shouldn't we check that it's been opened for read instead? */ | 
 | 3556 | 	ret = inode_permission(cfile->f_path.dentry->d_inode, MAY_READ); | 
| Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3557 | 	if (ret < 0) | 
 | 3558 | 		goto fail; | 
 | 3559 |  | 
 | 3560 | 	event->cft = __file_cft(cfile); | 
 | 3561 | 	if (IS_ERR(event->cft)) { | 
 | 3562 | 		ret = PTR_ERR(event->cft); | 
 | 3563 | 		goto fail; | 
 | 3564 | 	} | 
 | 3565 |  | 
 | 3566 | 	if (!event->cft->register_event || !event->cft->unregister_event) { | 
 | 3567 | 		ret = -EINVAL; | 
 | 3568 | 		goto fail; | 
 | 3569 | 	} | 
 | 3570 |  | 
 | 3571 | 	ret = event->cft->register_event(cgrp, event->cft, | 
 | 3572 | 			event->eventfd, buffer); | 
 | 3573 | 	if (ret) | 
 | 3574 | 		goto fail; | 
 | 3575 |  | 
 | 3576 | 	if (efile->f_op->poll(efile, &event->pt) & POLLHUP) { | 
 | 3577 | 		event->cft->unregister_event(cgrp, event->cft, event->eventfd); | 
 | 3578 | 		ret = 0; | 
 | 3579 | 		goto fail; | 
 | 3580 | 	} | 
 | 3581 |  | 
| Kirill A. Shutemov | a0a4db5 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 3582 | 	/* | 
 | 3583 | 	 * Events should be removed after rmdir of cgroup directory, but before | 
 | 3584 | 	 * destroying subsystem state objects. Let's take reference to cgroup | 
 | 3585 | 	 * directory dentry to do that. | 
 | 3586 | 	 */ | 
 | 3587 | 	dget(cgrp->dentry); | 
 | 3588 |  | 
| Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3589 | 	spin_lock(&cgrp->event_list_lock); | 
 | 3590 | 	list_add(&event->list, &cgrp->event_list); | 
 | 3591 | 	spin_unlock(&cgrp->event_list_lock); | 
 | 3592 |  | 
 | 3593 | 	fput(cfile); | 
 | 3594 | 	fput(efile); | 
 | 3595 |  | 
 | 3596 | 	return 0; | 
 | 3597 |  | 
 | 3598 | fail: | 
 | 3599 | 	if (cfile) | 
 | 3600 | 		fput(cfile); | 
 | 3601 |  | 
 | 3602 | 	if (event && event->eventfd && !IS_ERR(event->eventfd)) | 
 | 3603 | 		eventfd_ctx_put(event->eventfd); | 
 | 3604 |  | 
 | 3605 | 	if (!IS_ERR_OR_NULL(efile)) | 
 | 3606 | 		fput(efile); | 
 | 3607 |  | 
 | 3608 | 	kfree(event); | 
 | 3609 |  | 
 | 3610 | 	return ret; | 
 | 3611 | } | 
 | 3612 |  | 
| Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3613 | static u64 cgroup_clone_children_read(struct cgroup *cgrp, | 
 | 3614 | 				    struct cftype *cft) | 
 | 3615 | { | 
 | 3616 | 	return clone_children(cgrp); | 
 | 3617 | } | 
 | 3618 |  | 
 | 3619 | static int cgroup_clone_children_write(struct cgroup *cgrp, | 
 | 3620 | 				     struct cftype *cft, | 
 | 3621 | 				     u64 val) | 
 | 3622 | { | 
 | 3623 | 	if (val) | 
 | 3624 | 		set_bit(CGRP_CLONE_CHILDREN, &cgrp->flags); | 
 | 3625 | 	else | 
 | 3626 | 		clear_bit(CGRP_CLONE_CHILDREN, &cgrp->flags); | 
 | 3627 | 	return 0; | 
 | 3628 | } | 
 | 3629 |  | 
| Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3630 | /* | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3631 |  * for the common functions, 'private' gives the type of file | 
 | 3632 |  */ | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3633 | /* for hysterical raisins, we can't put this on the older files */ | 
 | 3634 | #define CGROUP_FILE_GENERIC_PREFIX "cgroup." | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3635 | static struct cftype files[] = { | 
 | 3636 | 	{ | 
 | 3637 | 		.name = "tasks", | 
 | 3638 | 		.open = cgroup_tasks_open, | 
| Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3639 | 		.write_u64 = cgroup_tasks_write, | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3640 | 		.release = cgroup_pidlist_release, | 
| Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 3641 | 		.mode = S_IRUGO | S_IWUSR, | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3642 | 	}, | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3643 | 	{ | 
 | 3644 | 		.name = CGROUP_FILE_GENERIC_PREFIX "procs", | 
 | 3645 | 		.open = cgroup_procs_open, | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 3646 | 		.write_u64 = cgroup_procs_write, | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3647 | 		.release = cgroup_pidlist_release, | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 3648 | 		.mode = S_IRUGO | S_IWUSR, | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3649 | 	}, | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3650 | 	{ | 
 | 3651 | 		.name = "notify_on_release", | 
| Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 3652 | 		.read_u64 = cgroup_read_notify_on_release, | 
| Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3653 | 		.write_u64 = cgroup_write_notify_on_release, | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3654 | 	}, | 
| Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3655 | 	{ | 
 | 3656 | 		.name = CGROUP_FILE_GENERIC_PREFIX "event_control", | 
 | 3657 | 		.write_string = cgroup_write_event_control, | 
 | 3658 | 		.mode = S_IWUGO, | 
 | 3659 | 	}, | 
| Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3660 | 	{ | 
 | 3661 | 		.name = "cgroup.clone_children", | 
 | 3662 | 		.read_u64 = cgroup_clone_children_read, | 
 | 3663 | 		.write_u64 = cgroup_clone_children_write, | 
 | 3664 | 	}, | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3665 | }; | 
 | 3666 |  | 
 | 3667 | static struct cftype cft_release_agent = { | 
 | 3668 | 	.name = "release_agent", | 
| Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 3669 | 	.read_seq_string = cgroup_release_agent_show, | 
 | 3670 | 	.write_string = cgroup_release_agent_write, | 
 | 3671 | 	.max_write_len = PATH_MAX, | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3672 | }; | 
 | 3673 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3674 | static int cgroup_populate_dir(struct cgroup *cgrp) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3675 | { | 
 | 3676 | 	int err; | 
 | 3677 | 	struct cgroup_subsys *ss; | 
 | 3678 |  | 
 | 3679 | 	/* First clear out any existing files */ | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3680 | 	cgroup_clear_directory(cgrp->dentry); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3681 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3682 | 	err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files)); | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3683 | 	if (err < 0) | 
 | 3684 | 		return err; | 
 | 3685 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3686 | 	if (cgrp == cgrp->top_cgroup) { | 
 | 3687 | 		if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0) | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3688 | 			return err; | 
 | 3689 | 	} | 
 | 3690 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3691 | 	for_each_subsys(cgrp->root, ss) { | 
 | 3692 | 		if (ss->populate && (err = ss->populate(ss, cgrp)) < 0) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3693 | 			return err; | 
 | 3694 | 	} | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 3695 | 	/* This cgroup is ready now */ | 
 | 3696 | 	for_each_subsys(cgrp->root, ss) { | 
 | 3697 | 		struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; | 
 | 3698 | 		/* | 
 | 3699 | 		 * Update id->css pointer and make this css visible from | 
 | 3700 | 		 * CSS ID functions. This pointer will be dereferened | 
 | 3701 | 		 * from RCU-read-side without locks. | 
 | 3702 | 		 */ | 
 | 3703 | 		if (css->id) | 
 | 3704 | 			rcu_assign_pointer(css->id->css, css); | 
 | 3705 | 	} | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3706 |  | 
 | 3707 | 	return 0; | 
 | 3708 | } | 
 | 3709 |  | 
 | 3710 | static void init_cgroup_css(struct cgroup_subsys_state *css, | 
 | 3711 | 			       struct cgroup_subsys *ss, | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3712 | 			       struct cgroup *cgrp) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3713 | { | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3714 | 	css->cgroup = cgrp; | 
| Paul Menage | e7c5ec9 | 2009-01-07 18:08:38 -0800 | [diff] [blame] | 3715 | 	atomic_set(&css->refcnt, 1); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3716 | 	css->flags = 0; | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 3717 | 	css->id = NULL; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3718 | 	if (cgrp == dummytop) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3719 | 		set_bit(CSS_ROOT, &css->flags); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3720 | 	BUG_ON(cgrp->subsys[ss->subsys_id]); | 
 | 3721 | 	cgrp->subsys[ss->subsys_id] = css; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3722 | } | 
 | 3723 |  | 
| Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 3724 | static void cgroup_lock_hierarchy(struct cgroupfs_root *root) | 
 | 3725 | { | 
 | 3726 | 	/* We need to take each hierarchy_mutex in a consistent order */ | 
 | 3727 | 	int i; | 
 | 3728 |  | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 3729 | 	/* | 
 | 3730 | 	 * No worry about a race with rebind_subsystems that might mess up the | 
 | 3731 | 	 * locking order, since both parties are under cgroup_mutex. | 
 | 3732 | 	 */ | 
| Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 3733 | 	for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { | 
 | 3734 | 		struct cgroup_subsys *ss = subsys[i]; | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 3735 | 		if (ss == NULL) | 
 | 3736 | 			continue; | 
| Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 3737 | 		if (ss->root == root) | 
| Li Zefan | cfebe56 | 2009-02-11 13:04:36 -0800 | [diff] [blame] | 3738 | 			mutex_lock(&ss->hierarchy_mutex); | 
| Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 3739 | 	} | 
 | 3740 | } | 
 | 3741 |  | 
 | 3742 | static void cgroup_unlock_hierarchy(struct cgroupfs_root *root) | 
 | 3743 | { | 
 | 3744 | 	int i; | 
 | 3745 |  | 
 | 3746 | 	for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { | 
 | 3747 | 		struct cgroup_subsys *ss = subsys[i]; | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 3748 | 		if (ss == NULL) | 
 | 3749 | 			continue; | 
| Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 3750 | 		if (ss->root == root) | 
 | 3751 | 			mutex_unlock(&ss->hierarchy_mutex); | 
 | 3752 | 	} | 
 | 3753 | } | 
 | 3754 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3755 | /* | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3756 |  * cgroup_create - create a cgroup | 
 | 3757 |  * @parent: cgroup that will be parent of the new cgroup | 
 | 3758 |  * @dentry: dentry of the new cgroup | 
 | 3759 |  * @mode: mode to set on new inode | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3760 |  * | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3761 |  * Must be called with the mutex on the parent inode held | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3762 |  */ | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3763 | static long cgroup_create(struct cgroup *parent, struct dentry *dentry, | 
| Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 3764 | 			     umode_t mode) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3765 | { | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3766 | 	struct cgroup *cgrp; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3767 | 	struct cgroupfs_root *root = parent->root; | 
 | 3768 | 	int err = 0; | 
 | 3769 | 	struct cgroup_subsys *ss; | 
 | 3770 | 	struct super_block *sb = root->sb; | 
 | 3771 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3772 | 	cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL); | 
 | 3773 | 	if (!cgrp) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3774 | 		return -ENOMEM; | 
 | 3775 |  | 
 | 3776 | 	/* Grab a reference on the superblock so the hierarchy doesn't | 
 | 3777 | 	 * get deleted on unmount if there are child cgroups.  This | 
 | 3778 | 	 * can be done outside cgroup_mutex, since the sb can't | 
 | 3779 | 	 * disappear while someone has an open control file on the | 
 | 3780 | 	 * fs */ | 
 | 3781 | 	atomic_inc(&sb->s_active); | 
 | 3782 |  | 
 | 3783 | 	mutex_lock(&cgroup_mutex); | 
 | 3784 |  | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3785 | 	init_cgroup_housekeeping(cgrp); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3786 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3787 | 	cgrp->parent = parent; | 
 | 3788 | 	cgrp->root = parent->root; | 
 | 3789 | 	cgrp->top_cgroup = parent->top_cgroup; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3790 |  | 
| Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 3791 | 	if (notify_on_release(parent)) | 
 | 3792 | 		set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); | 
 | 3793 |  | 
| Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3794 | 	if (clone_children(parent)) | 
 | 3795 | 		set_bit(CGRP_CLONE_CHILDREN, &cgrp->flags); | 
 | 3796 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3797 | 	for_each_subsys(root, ss) { | 
| Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 3798 | 		struct cgroup_subsys_state *css = ss->create(cgrp); | 
| Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 3799 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3800 | 		if (IS_ERR(css)) { | 
 | 3801 | 			err = PTR_ERR(css); | 
 | 3802 | 			goto err_destroy; | 
 | 3803 | 		} | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3804 | 		init_cgroup_css(css, ss, cgrp); | 
| Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 3805 | 		if (ss->use_id) { | 
 | 3806 | 			err = alloc_css_id(ss, parent, cgrp); | 
 | 3807 | 			if (err) | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 3808 | 				goto err_destroy; | 
| Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 3809 | 		} | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 3810 | 		/* At error, ->destroy() callback has to free assigned ID. */ | 
| Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3811 | 		if (clone_children(parent) && ss->post_clone) | 
| Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 3812 | 			ss->post_clone(cgrp); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3813 | 	} | 
 | 3814 |  | 
| Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 3815 | 	cgroup_lock_hierarchy(root); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3816 | 	list_add(&cgrp->sibling, &cgrp->parent->children); | 
| Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 3817 | 	cgroup_unlock_hierarchy(root); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3818 | 	root->number_of_cgroups++; | 
 | 3819 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3820 | 	err = cgroup_create_dir(cgrp, dentry, mode); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3821 | 	if (err < 0) | 
 | 3822 | 		goto err_remove; | 
 | 3823 |  | 
| Colin Cross | c0f6fa8 | 2010-11-23 21:37:03 -0800 | [diff] [blame] | 3824 | 	set_bit(CGRP_RELEASABLE, &parent->flags); | 
 | 3825 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3826 | 	/* The cgroup directory was pre-locked for us */ | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3827 | 	BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex)); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3828 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3829 | 	err = cgroup_populate_dir(cgrp); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3830 | 	/* If err < 0, we have a half-filled directory - oh well ;) */ | 
 | 3831 |  | 
 | 3832 | 	mutex_unlock(&cgroup_mutex); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3833 | 	mutex_unlock(&cgrp->dentry->d_inode->i_mutex); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3834 |  | 
 | 3835 | 	return 0; | 
 | 3836 |  | 
 | 3837 |  err_remove: | 
 | 3838 |  | 
| KAMEZAWA Hiroyuki | baef99a | 2009-01-29 14:25:10 -0800 | [diff] [blame] | 3839 | 	cgroup_lock_hierarchy(root); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3840 | 	list_del(&cgrp->sibling); | 
| KAMEZAWA Hiroyuki | baef99a | 2009-01-29 14:25:10 -0800 | [diff] [blame] | 3841 | 	cgroup_unlock_hierarchy(root); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3842 | 	root->number_of_cgroups--; | 
 | 3843 |  | 
 | 3844 |  err_destroy: | 
 | 3845 |  | 
 | 3846 | 	for_each_subsys(root, ss) { | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3847 | 		if (cgrp->subsys[ss->subsys_id]) | 
| Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 3848 | 			ss->destroy(cgrp); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3849 | 	} | 
 | 3850 |  | 
 | 3851 | 	mutex_unlock(&cgroup_mutex); | 
 | 3852 |  | 
 | 3853 | 	/* Release the reference count that we took on the superblock */ | 
 | 3854 | 	deactivate_super(sb); | 
 | 3855 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3856 | 	kfree(cgrp); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3857 | 	return err; | 
 | 3858 | } | 
 | 3859 |  | 
| Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 3860 | static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3861 | { | 
 | 3862 | 	struct cgroup *c_parent = dentry->d_parent->d_fsdata; | 
 | 3863 |  | 
 | 3864 | 	/* the vfs holds inode->i_mutex already */ | 
 | 3865 | 	return cgroup_create(c_parent, dentry, mode | S_IFDIR); | 
 | 3866 | } | 
 | 3867 |  | 
| Li Zefan | 55b6fd0 | 2008-07-29 22:33:20 -0700 | [diff] [blame] | 3868 | static int cgroup_has_css_refs(struct cgroup *cgrp) | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3869 | { | 
 | 3870 | 	/* Check the reference count on each subsystem. Since we | 
 | 3871 | 	 * already established that there are no tasks in the | 
| Paul Menage | e7c5ec9 | 2009-01-07 18:08:38 -0800 | [diff] [blame] | 3872 | 	 * cgroup, if the css refcount is also 1, then there should | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3873 | 	 * be no outstanding references, so the subsystem is safe to | 
 | 3874 | 	 * destroy. We scan across all subsystems rather than using | 
 | 3875 | 	 * the per-hierarchy linked list of mounted subsystems since | 
 | 3876 | 	 * we can be called via check_for_release() with no | 
 | 3877 | 	 * synchronization other than RCU, and the subsystem linked | 
 | 3878 | 	 * list isn't RCU-safe */ | 
 | 3879 | 	int i; | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 3880 | 	/* | 
 | 3881 | 	 * We won't need to lock the subsys array, because the subsystems | 
 | 3882 | 	 * we're concerned about aren't going anywhere since our cgroup root | 
 | 3883 | 	 * has a reference on them. | 
 | 3884 | 	 */ | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3885 | 	for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { | 
 | 3886 | 		struct cgroup_subsys *ss = subsys[i]; | 
 | 3887 | 		struct cgroup_subsys_state *css; | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 3888 | 		/* Skip subsystems not present or not in this hierarchy */ | 
 | 3889 | 		if (ss == NULL || ss->root != cgrp->root) | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3890 | 			continue; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3891 | 		css = cgrp->subsys[ss->subsys_id]; | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3892 | 		/* When called from check_for_release() it's possible | 
 | 3893 | 		 * that by this point the cgroup has been removed | 
 | 3894 | 		 * and the css deleted. But a false-positive doesn't | 
 | 3895 | 		 * matter, since it can only happen if the cgroup | 
 | 3896 | 		 * has been deleted and hence no longer needs the | 
 | 3897 | 		 * release agent to be called anyway. */ | 
| Paul Menage | e7c5ec9 | 2009-01-07 18:08:38 -0800 | [diff] [blame] | 3898 | 		if (css && (atomic_read(&css->refcnt) > 1)) | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3899 | 			return 1; | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3900 | 	} | 
 | 3901 | 	return 0; | 
 | 3902 | } | 
 | 3903 |  | 
| Paul Menage | e7c5ec9 | 2009-01-07 18:08:38 -0800 | [diff] [blame] | 3904 | /* | 
 | 3905 |  * Atomically mark all (or else none) of the cgroup's CSS objects as | 
 | 3906 |  * CSS_REMOVED. Return true on success, or false if the cgroup has | 
 | 3907 |  * busy subsystems. Call with cgroup_mutex held | 
 | 3908 |  */ | 
 | 3909 |  | 
 | 3910 | static int cgroup_clear_css_refs(struct cgroup *cgrp) | 
 | 3911 | { | 
 | 3912 | 	struct cgroup_subsys *ss; | 
 | 3913 | 	unsigned long flags; | 
 | 3914 | 	bool failed = false; | 
 | 3915 | 	local_irq_save(flags); | 
 | 3916 | 	for_each_subsys(cgrp->root, ss) { | 
 | 3917 | 		struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; | 
 | 3918 | 		int refcnt; | 
| Paul Menage | 804b3c2 | 2009-01-29 14:25:21 -0800 | [diff] [blame] | 3919 | 		while (1) { | 
| Paul Menage | e7c5ec9 | 2009-01-07 18:08:38 -0800 | [diff] [blame] | 3920 | 			/* We can only remove a CSS with a refcnt==1 */ | 
 | 3921 | 			refcnt = atomic_read(&css->refcnt); | 
 | 3922 | 			if (refcnt > 1) { | 
 | 3923 | 				failed = true; | 
 | 3924 | 				goto done; | 
 | 3925 | 			} | 
 | 3926 | 			BUG_ON(!refcnt); | 
 | 3927 | 			/* | 
 | 3928 | 			 * Drop the refcnt to 0 while we check other | 
 | 3929 | 			 * subsystems. This will cause any racing | 
 | 3930 | 			 * css_tryget() to spin until we set the | 
 | 3931 | 			 * CSS_REMOVED bits or abort | 
 | 3932 | 			 */ | 
| Paul Menage | 804b3c2 | 2009-01-29 14:25:21 -0800 | [diff] [blame] | 3933 | 			if (atomic_cmpxchg(&css->refcnt, refcnt, 0) == refcnt) | 
 | 3934 | 				break; | 
 | 3935 | 			cpu_relax(); | 
 | 3936 | 		} | 
| Paul Menage | e7c5ec9 | 2009-01-07 18:08:38 -0800 | [diff] [blame] | 3937 | 	} | 
 | 3938 |  done: | 
 | 3939 | 	for_each_subsys(cgrp->root, ss) { | 
 | 3940 | 		struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; | 
 | 3941 | 		if (failed) { | 
 | 3942 | 			/* | 
 | 3943 | 			 * Restore old refcnt if we previously managed | 
 | 3944 | 			 * to clear it from 1 to 0 | 
 | 3945 | 			 */ | 
 | 3946 | 			if (!atomic_read(&css->refcnt)) | 
 | 3947 | 				atomic_set(&css->refcnt, 1); | 
 | 3948 | 		} else { | 
 | 3949 | 			/* Commit the fact that the CSS is removed */ | 
 | 3950 | 			set_bit(CSS_REMOVED, &css->flags); | 
 | 3951 | 		} | 
 | 3952 | 	} | 
 | 3953 | 	local_irq_restore(flags); | 
 | 3954 | 	return !failed; | 
 | 3955 | } | 
 | 3956 |  | 
| Colin Cross | befae2f | 2010-11-23 21:37:04 -0800 | [diff] [blame] | 3957 | /* checks if all of the css_sets attached to a cgroup have a refcount of 0. | 
 | 3958 |  * Must be called with css_set_lock held */ | 
 | 3959 | static int cgroup_css_sets_empty(struct cgroup *cgrp) | 
 | 3960 | { | 
 | 3961 | 	struct cg_cgroup_link *link; | 
 | 3962 |  | 
 | 3963 | 	list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) { | 
 | 3964 | 		struct css_set *cg = link->cg; | 
 | 3965 | 		if (atomic_read(&cg->refcount) > 0) | 
 | 3966 | 			return 0; | 
 | 3967 | 	} | 
 | 3968 |  | 
 | 3969 | 	return 1; | 
 | 3970 | } | 
 | 3971 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3972 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry) | 
 | 3973 | { | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3974 | 	struct cgroup *cgrp = dentry->d_fsdata; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3975 | 	struct dentry *d; | 
 | 3976 | 	struct cgroup *parent; | 
| KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 3977 | 	DEFINE_WAIT(wait); | 
| Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 3978 | 	struct cgroup_event *event, *tmp; | 
| KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 3979 | 	int ret; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3980 |  | 
 | 3981 | 	/* the vfs holds both inode->i_mutex already */ | 
| KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 3982 | again: | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3983 | 	mutex_lock(&cgroup_mutex); | 
| Colin Cross | befae2f | 2010-11-23 21:37:04 -0800 | [diff] [blame] | 3984 | 	if (!cgroup_css_sets_empty(cgrp)) { | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3985 | 		mutex_unlock(&cgroup_mutex); | 
 | 3986 | 		return -EBUSY; | 
 | 3987 | 	} | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3988 | 	if (!list_empty(&cgrp->children)) { | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3989 | 		mutex_unlock(&cgroup_mutex); | 
 | 3990 | 		return -EBUSY; | 
 | 3991 | 	} | 
| KAMEZAWA Hiroyuki | 3fa59df | 2008-11-19 15:36:34 -0800 | [diff] [blame] | 3992 | 	mutex_unlock(&cgroup_mutex); | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3993 |  | 
| KAMEZAWA Hiroyuki | 4fca88c | 2008-02-07 00:14:27 -0800 | [diff] [blame] | 3994 | 	/* | 
| KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 3995 | 	 * In general, subsystem has no css->refcnt after pre_destroy(). But | 
 | 3996 | 	 * in racy cases, subsystem may have to get css->refcnt after | 
 | 3997 | 	 * pre_destroy() and it makes rmdir return with -EBUSY. This sometimes | 
 | 3998 | 	 * make rmdir return -EBUSY too often. To avoid that, we use waitqueue | 
 | 3999 | 	 * for cgroup's rmdir. CGRP_WAIT_ON_RMDIR is for synchronizing rmdir | 
 | 4000 | 	 * and subsystem's reference count handling. Please see css_get/put | 
 | 4001 | 	 * and css_tryget() and cgroup_wakeup_rmdir_waiter() implementation. | 
 | 4002 | 	 */ | 
 | 4003 | 	set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags); | 
 | 4004 |  | 
 | 4005 | 	/* | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4006 | 	 * Call pre_destroy handlers of subsys. Notify subsystems | 
 | 4007 | 	 * that rmdir() request comes. | 
| KAMEZAWA Hiroyuki | 4fca88c | 2008-02-07 00:14:27 -0800 | [diff] [blame] | 4008 | 	 */ | 
| KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 4009 | 	ret = cgroup_call_pre_destroy(cgrp); | 
| KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 4010 | 	if (ret) { | 
 | 4011 | 		clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags); | 
| KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 4012 | 		return ret; | 
| KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 4013 | 	} | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4014 |  | 
| KAMEZAWA Hiroyuki | 3fa59df | 2008-11-19 15:36:34 -0800 | [diff] [blame] | 4015 | 	mutex_lock(&cgroup_mutex); | 
 | 4016 | 	parent = cgrp->parent; | 
| Colin Cross | befae2f | 2010-11-23 21:37:04 -0800 | [diff] [blame] | 4017 | 	if (!cgroup_css_sets_empty(cgrp) || !list_empty(&cgrp->children)) { | 
| KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 4018 | 		clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4019 | 		mutex_unlock(&cgroup_mutex); | 
 | 4020 | 		return -EBUSY; | 
 | 4021 | 	} | 
| KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 4022 | 	prepare_to_wait(&cgroup_rmdir_waitq, &wait, TASK_INTERRUPTIBLE); | 
| KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 4023 | 	if (!cgroup_clear_css_refs(cgrp)) { | 
 | 4024 | 		mutex_unlock(&cgroup_mutex); | 
| KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 4025 | 		/* | 
 | 4026 | 		 * Because someone may call cgroup_wakeup_rmdir_waiter() before | 
 | 4027 | 		 * prepare_to_wait(), we need to check this flag. | 
 | 4028 | 		 */ | 
 | 4029 | 		if (test_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags)) | 
 | 4030 | 			schedule(); | 
| KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 4031 | 		finish_wait(&cgroup_rmdir_waitq, &wait); | 
 | 4032 | 		clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags); | 
 | 4033 | 		if (signal_pending(current)) | 
 | 4034 | 			return -EINTR; | 
 | 4035 | 		goto again; | 
 | 4036 | 	} | 
 | 4037 | 	/* NO css_tryget() can success after here. */ | 
 | 4038 | 	finish_wait(&cgroup_rmdir_waitq, &wait); | 
 | 4039 | 	clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4040 |  | 
| Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4041 | 	raw_spin_lock(&release_list_lock); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4042 | 	set_bit(CGRP_REMOVED, &cgrp->flags); | 
 | 4043 | 	if (!list_empty(&cgrp->release_list)) | 
| Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 4044 | 		list_del_init(&cgrp->release_list); | 
| Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4045 | 	raw_spin_unlock(&release_list_lock); | 
| Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 4046 |  | 
 | 4047 | 	cgroup_lock_hierarchy(cgrp->root); | 
 | 4048 | 	/* delete this cgroup from parent->children */ | 
| Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 4049 | 	list_del_init(&cgrp->sibling); | 
| Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 4050 | 	cgroup_unlock_hierarchy(cgrp->root); | 
 | 4051 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4052 | 	d = dget(cgrp->dentry); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4053 |  | 
 | 4054 | 	cgroup_d_remove_dir(d); | 
 | 4055 | 	dput(d); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4056 |  | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4057 | 	check_for_release(parent); | 
 | 4058 |  | 
| Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4059 | 	/* | 
 | 4060 | 	 * Unregister events and notify userspace. | 
 | 4061 | 	 * Notify userspace about cgroup removing only after rmdir of cgroup | 
 | 4062 | 	 * directory to avoid race between userspace and kernelspace | 
 | 4063 | 	 */ | 
 | 4064 | 	spin_lock(&cgrp->event_list_lock); | 
 | 4065 | 	list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) { | 
 | 4066 | 		list_del(&event->list); | 
 | 4067 | 		remove_wait_queue(event->wqh, &event->wait); | 
 | 4068 | 		eventfd_signal(event->eventfd, 1); | 
 | 4069 | 		schedule_work(&event->remove); | 
 | 4070 | 	} | 
 | 4071 | 	spin_unlock(&cgrp->event_list_lock); | 
 | 4072 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4073 | 	mutex_unlock(&cgroup_mutex); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4074 | 	return 0; | 
 | 4075 | } | 
 | 4076 |  | 
| Li Zefan | 06a1192 | 2008-04-29 01:00:07 -0700 | [diff] [blame] | 4077 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4078 | { | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4079 | 	struct cgroup_subsys_state *css; | 
| Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4080 |  | 
 | 4081 | 	printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4082 |  | 
 | 4083 | 	/* Create the top cgroup state for this subsystem */ | 
| Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 4084 | 	list_add(&ss->sibling, &rootnode.subsys_list); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4085 | 	ss->root = &rootnode; | 
| Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 4086 | 	css = ss->create(dummytop); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4087 | 	/* We don't handle early failures gracefully */ | 
 | 4088 | 	BUG_ON(IS_ERR(css)); | 
 | 4089 | 	init_cgroup_css(css, ss, dummytop); | 
 | 4090 |  | 
| Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4091 | 	/* Update the init_css_set to contain a subsys | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4092 | 	 * pointer to this state - since the subsystem is | 
| Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4093 | 	 * newly registered, all tasks and hence the | 
 | 4094 | 	 * init_css_set is in the subsystem's top cgroup. */ | 
 | 4095 | 	init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id]; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4096 |  | 
 | 4097 | 	need_forkexit_callback |= ss->fork || ss->exit; | 
 | 4098 |  | 
| Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4099 | 	/* At system boot, before all subsystems have been | 
 | 4100 | 	 * registered, no tasks have been forked, so we don't | 
 | 4101 | 	 * need to invoke fork callbacks here. */ | 
 | 4102 | 	BUG_ON(!list_empty(&init_task.tasks)); | 
 | 4103 |  | 
| Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 4104 | 	mutex_init(&ss->hierarchy_mutex); | 
| Li Zefan | cfebe56 | 2009-02-11 13:04:36 -0800 | [diff] [blame] | 4105 | 	lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4106 | 	ss->active = 1; | 
| Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4107 |  | 
 | 4108 | 	/* this function shouldn't be used with modular subsystems, since they | 
 | 4109 | 	 * need to register a subsys_id, among other things */ | 
 | 4110 | 	BUG_ON(ss->module); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4111 | } | 
 | 4112 |  | 
 | 4113 | /** | 
| Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4114 |  * cgroup_load_subsys: load and register a modular subsystem at runtime | 
 | 4115 |  * @ss: the subsystem to load | 
 | 4116 |  * | 
 | 4117 |  * This function should be called in a modular subsystem's initcall. If the | 
| Thomas Weber | 8839316 | 2010-03-16 11:47:56 +0100 | [diff] [blame] | 4118 |  * subsystem is built as a module, it will be assigned a new subsys_id and set | 
| Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4119 |  * up for use. If the subsystem is built-in anyway, work is delegated to the | 
 | 4120 |  * simpler cgroup_init_subsys. | 
 | 4121 |  */ | 
 | 4122 | int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss) | 
 | 4123 | { | 
 | 4124 | 	int i; | 
 | 4125 | 	struct cgroup_subsys_state *css; | 
 | 4126 |  | 
 | 4127 | 	/* check name and function validity */ | 
 | 4128 | 	if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN || | 
 | 4129 | 	    ss->create == NULL || ss->destroy == NULL) | 
 | 4130 | 		return -EINVAL; | 
 | 4131 |  | 
 | 4132 | 	/* | 
 | 4133 | 	 * we don't support callbacks in modular subsystems. this check is | 
 | 4134 | 	 * before the ss->module check for consistency; a subsystem that could | 
 | 4135 | 	 * be a module should still have no callbacks even if the user isn't | 
 | 4136 | 	 * compiling it as one. | 
 | 4137 | 	 */ | 
 | 4138 | 	if (ss->fork || ss->exit) | 
 | 4139 | 		return -EINVAL; | 
 | 4140 |  | 
 | 4141 | 	/* | 
 | 4142 | 	 * an optionally modular subsystem is built-in: we want to do nothing, | 
 | 4143 | 	 * since cgroup_init_subsys will have already taken care of it. | 
 | 4144 | 	 */ | 
 | 4145 | 	if (ss->module == NULL) { | 
 | 4146 | 		/* a few sanity checks */ | 
 | 4147 | 		BUG_ON(ss->subsys_id >= CGROUP_BUILTIN_SUBSYS_COUNT); | 
 | 4148 | 		BUG_ON(subsys[ss->subsys_id] != ss); | 
 | 4149 | 		return 0; | 
 | 4150 | 	} | 
 | 4151 |  | 
 | 4152 | 	/* | 
 | 4153 | 	 * need to register a subsys id before anything else - for example, | 
 | 4154 | 	 * init_cgroup_css needs it. | 
 | 4155 | 	 */ | 
 | 4156 | 	mutex_lock(&cgroup_mutex); | 
 | 4157 | 	/* find the first empty slot in the array */ | 
 | 4158 | 	for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) { | 
 | 4159 | 		if (subsys[i] == NULL) | 
 | 4160 | 			break; | 
 | 4161 | 	} | 
 | 4162 | 	if (i == CGROUP_SUBSYS_COUNT) { | 
 | 4163 | 		/* maximum number of subsystems already registered! */ | 
 | 4164 | 		mutex_unlock(&cgroup_mutex); | 
 | 4165 | 		return -EBUSY; | 
 | 4166 | 	} | 
 | 4167 | 	/* assign ourselves the subsys_id */ | 
 | 4168 | 	ss->subsys_id = i; | 
 | 4169 | 	subsys[i] = ss; | 
 | 4170 |  | 
 | 4171 | 	/* | 
 | 4172 | 	 * no ss->create seems to need anything important in the ss struct, so | 
 | 4173 | 	 * this can happen first (i.e. before the rootnode attachment). | 
 | 4174 | 	 */ | 
| Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 4175 | 	css = ss->create(dummytop); | 
| Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4176 | 	if (IS_ERR(css)) { | 
 | 4177 | 		/* failure case - need to deassign the subsys[] slot. */ | 
 | 4178 | 		subsys[i] = NULL; | 
 | 4179 | 		mutex_unlock(&cgroup_mutex); | 
 | 4180 | 		return PTR_ERR(css); | 
 | 4181 | 	} | 
 | 4182 |  | 
 | 4183 | 	list_add(&ss->sibling, &rootnode.subsys_list); | 
 | 4184 | 	ss->root = &rootnode; | 
 | 4185 |  | 
 | 4186 | 	/* our new subsystem will be attached to the dummy hierarchy. */ | 
 | 4187 | 	init_cgroup_css(css, ss, dummytop); | 
 | 4188 | 	/* init_idr must be after init_cgroup_css because it sets css->id. */ | 
 | 4189 | 	if (ss->use_id) { | 
 | 4190 | 		int ret = cgroup_init_idr(ss, css); | 
 | 4191 | 		if (ret) { | 
 | 4192 | 			dummytop->subsys[ss->subsys_id] = NULL; | 
| Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 4193 | 			ss->destroy(dummytop); | 
| Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4194 | 			subsys[i] = NULL; | 
 | 4195 | 			mutex_unlock(&cgroup_mutex); | 
 | 4196 | 			return ret; | 
 | 4197 | 		} | 
 | 4198 | 	} | 
 | 4199 |  | 
 | 4200 | 	/* | 
 | 4201 | 	 * Now we need to entangle the css into the existing css_sets. unlike | 
 | 4202 | 	 * in cgroup_init_subsys, there are now multiple css_sets, so each one | 
 | 4203 | 	 * will need a new pointer to it; done by iterating the css_set_table. | 
 | 4204 | 	 * furthermore, modifying the existing css_sets will corrupt the hash | 
 | 4205 | 	 * table state, so each changed css_set will need its hash recomputed. | 
 | 4206 | 	 * this is all done under the css_set_lock. | 
 | 4207 | 	 */ | 
 | 4208 | 	write_lock(&css_set_lock); | 
 | 4209 | 	for (i = 0; i < CSS_SET_TABLE_SIZE; i++) { | 
 | 4210 | 		struct css_set *cg; | 
 | 4211 | 		struct hlist_node *node, *tmp; | 
 | 4212 | 		struct hlist_head *bucket = &css_set_table[i], *new_bucket; | 
 | 4213 |  | 
 | 4214 | 		hlist_for_each_entry_safe(cg, node, tmp, bucket, hlist) { | 
 | 4215 | 			/* skip entries that we already rehashed */ | 
 | 4216 | 			if (cg->subsys[ss->subsys_id]) | 
 | 4217 | 				continue; | 
 | 4218 | 			/* remove existing entry */ | 
 | 4219 | 			hlist_del(&cg->hlist); | 
 | 4220 | 			/* set new value */ | 
 | 4221 | 			cg->subsys[ss->subsys_id] = css; | 
 | 4222 | 			/* recompute hash and restore entry */ | 
 | 4223 | 			new_bucket = css_set_hash(cg->subsys); | 
 | 4224 | 			hlist_add_head(&cg->hlist, new_bucket); | 
 | 4225 | 		} | 
 | 4226 | 	} | 
 | 4227 | 	write_unlock(&css_set_lock); | 
 | 4228 |  | 
 | 4229 | 	mutex_init(&ss->hierarchy_mutex); | 
 | 4230 | 	lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key); | 
 | 4231 | 	ss->active = 1; | 
 | 4232 |  | 
| Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4233 | 	/* success! */ | 
 | 4234 | 	mutex_unlock(&cgroup_mutex); | 
 | 4235 | 	return 0; | 
 | 4236 | } | 
 | 4237 | EXPORT_SYMBOL_GPL(cgroup_load_subsys); | 
 | 4238 |  | 
 | 4239 | /** | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4240 |  * cgroup_unload_subsys: unload a modular subsystem | 
 | 4241 |  * @ss: the subsystem to unload | 
 | 4242 |  * | 
 | 4243 |  * This function should be called in a modular subsystem's exitcall. When this | 
 | 4244 |  * function is invoked, the refcount on the subsystem's module will be 0, so | 
 | 4245 |  * the subsystem will not be attached to any hierarchy. | 
 | 4246 |  */ | 
 | 4247 | void cgroup_unload_subsys(struct cgroup_subsys *ss) | 
 | 4248 | { | 
 | 4249 | 	struct cg_cgroup_link *link; | 
 | 4250 | 	struct hlist_head *hhead; | 
 | 4251 |  | 
 | 4252 | 	BUG_ON(ss->module == NULL); | 
 | 4253 |  | 
 | 4254 | 	/* | 
 | 4255 | 	 * we shouldn't be called if the subsystem is in use, and the use of | 
 | 4256 | 	 * try_module_get in parse_cgroupfs_options should ensure that it | 
 | 4257 | 	 * doesn't start being used while we're killing it off. | 
 | 4258 | 	 */ | 
 | 4259 | 	BUG_ON(ss->root != &rootnode); | 
 | 4260 |  | 
 | 4261 | 	mutex_lock(&cgroup_mutex); | 
 | 4262 | 	/* deassign the subsys_id */ | 
 | 4263 | 	BUG_ON(ss->subsys_id < CGROUP_BUILTIN_SUBSYS_COUNT); | 
 | 4264 | 	subsys[ss->subsys_id] = NULL; | 
 | 4265 |  | 
 | 4266 | 	/* remove subsystem from rootnode's list of subsystems */ | 
| Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 4267 | 	list_del_init(&ss->sibling); | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4268 |  | 
 | 4269 | 	/* | 
 | 4270 | 	 * disentangle the css from all css_sets attached to the dummytop. as | 
 | 4271 | 	 * in loading, we need to pay our respects to the hashtable gods. | 
 | 4272 | 	 */ | 
 | 4273 | 	write_lock(&css_set_lock); | 
 | 4274 | 	list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) { | 
 | 4275 | 		struct css_set *cg = link->cg; | 
 | 4276 |  | 
 | 4277 | 		hlist_del(&cg->hlist); | 
 | 4278 | 		BUG_ON(!cg->subsys[ss->subsys_id]); | 
 | 4279 | 		cg->subsys[ss->subsys_id] = NULL; | 
 | 4280 | 		hhead = css_set_hash(cg->subsys); | 
 | 4281 | 		hlist_add_head(&cg->hlist, hhead); | 
 | 4282 | 	} | 
 | 4283 | 	write_unlock(&css_set_lock); | 
 | 4284 |  | 
 | 4285 | 	/* | 
 | 4286 | 	 * remove subsystem's css from the dummytop and free it - need to free | 
 | 4287 | 	 * before marking as null because ss->destroy needs the cgrp->subsys | 
 | 4288 | 	 * pointer to find their state. note that this also takes care of | 
 | 4289 | 	 * freeing the css_id. | 
 | 4290 | 	 */ | 
| Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 4291 | 	ss->destroy(dummytop); | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4292 | 	dummytop->subsys[ss->subsys_id] = NULL; | 
 | 4293 |  | 
 | 4294 | 	mutex_unlock(&cgroup_mutex); | 
 | 4295 | } | 
 | 4296 | EXPORT_SYMBOL_GPL(cgroup_unload_subsys); | 
 | 4297 |  | 
 | 4298 | /** | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4299 |  * cgroup_init_early - cgroup initialization at system boot | 
 | 4300 |  * | 
 | 4301 |  * Initialize cgroups at system boot, and initialize any | 
 | 4302 |  * subsystems that request early init. | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4303 |  */ | 
 | 4304 | int __init cgroup_init_early(void) | 
 | 4305 | { | 
 | 4306 | 	int i; | 
| Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 4307 | 	atomic_set(&init_css_set.refcount, 1); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4308 | 	INIT_LIST_HEAD(&init_css_set.cg_links); | 
 | 4309 | 	INIT_LIST_HEAD(&init_css_set.tasks); | 
| Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4310 | 	INIT_HLIST_NODE(&init_css_set.hlist); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4311 | 	css_set_count = 1; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4312 | 	init_cgroup_root(&rootnode); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4313 | 	root_count = 1; | 
 | 4314 | 	init_task.cgroups = &init_css_set; | 
 | 4315 |  | 
 | 4316 | 	init_css_set_link.cg = &init_css_set; | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4317 | 	init_css_set_link.cgrp = dummytop; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4318 | 	list_add(&init_css_set_link.cgrp_link_list, | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4319 | 		 &rootnode.top_cgroup.css_sets); | 
 | 4320 | 	list_add(&init_css_set_link.cg_link_list, | 
 | 4321 | 		 &init_css_set.cg_links); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4322 |  | 
| Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4323 | 	for (i = 0; i < CSS_SET_TABLE_SIZE; i++) | 
 | 4324 | 		INIT_HLIST_HEAD(&css_set_table[i]); | 
 | 4325 |  | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4326 | 	/* at bootup time, we don't worry about modular subsystems */ | 
 | 4327 | 	for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) { | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4328 | 		struct cgroup_subsys *ss = subsys[i]; | 
 | 4329 |  | 
 | 4330 | 		BUG_ON(!ss->name); | 
 | 4331 | 		BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN); | 
 | 4332 | 		BUG_ON(!ss->create); | 
 | 4333 | 		BUG_ON(!ss->destroy); | 
 | 4334 | 		if (ss->subsys_id != i) { | 
| Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4335 | 			printk(KERN_ERR "cgroup: Subsys %s id == %d\n", | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4336 | 			       ss->name, ss->subsys_id); | 
 | 4337 | 			BUG(); | 
 | 4338 | 		} | 
 | 4339 |  | 
 | 4340 | 		if (ss->early_init) | 
 | 4341 | 			cgroup_init_subsys(ss); | 
 | 4342 | 	} | 
 | 4343 | 	return 0; | 
 | 4344 | } | 
 | 4345 |  | 
 | 4346 | /** | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4347 |  * cgroup_init - cgroup initialization | 
 | 4348 |  * | 
 | 4349 |  * Register cgroup filesystem and /proc file, and initialize | 
 | 4350 |  * any subsystems that didn't request early init. | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4351 |  */ | 
 | 4352 | int __init cgroup_init(void) | 
 | 4353 | { | 
 | 4354 | 	int err; | 
 | 4355 | 	int i; | 
| Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4356 | 	struct hlist_head *hhead; | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4357 |  | 
 | 4358 | 	err = bdi_init(&cgroup_backing_dev_info); | 
 | 4359 | 	if (err) | 
 | 4360 | 		return err; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4361 |  | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4362 | 	/* at bootup time, we don't worry about modular subsystems */ | 
 | 4363 | 	for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) { | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4364 | 		struct cgroup_subsys *ss = subsys[i]; | 
 | 4365 | 		if (!ss->early_init) | 
 | 4366 | 			cgroup_init_subsys(ss); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4367 | 		if (ss->use_id) | 
| Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4368 | 			cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4369 | 	} | 
 | 4370 |  | 
| Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4371 | 	/* Add init_css_set to the hash table */ | 
 | 4372 | 	hhead = css_set_hash(init_css_set.subsys); | 
 | 4373 | 	hlist_add_head(&init_css_set.hlist, hhead); | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4374 | 	BUG_ON(!init_root_id(&rootnode)); | 
| Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4375 |  | 
 | 4376 | 	cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj); | 
 | 4377 | 	if (!cgroup_kobj) { | 
 | 4378 | 		err = -ENOMEM; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4379 | 		goto out; | 
| Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4380 | 	} | 
 | 4381 |  | 
 | 4382 | 	err = register_filesystem(&cgroup_fs_type); | 
 | 4383 | 	if (err < 0) { | 
 | 4384 | 		kobject_put(cgroup_kobj); | 
 | 4385 | 		goto out; | 
 | 4386 | 	} | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4387 |  | 
| Li Zefan | 46ae220 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 4388 | 	proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations); | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4389 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4390 | out: | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4391 | 	if (err) | 
 | 4392 | 		bdi_destroy(&cgroup_backing_dev_info); | 
 | 4393 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4394 | 	return err; | 
 | 4395 | } | 
| Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4396 |  | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4397 | /* | 
 | 4398 |  * proc_cgroup_show() | 
 | 4399 |  *  - Print task's cgroup paths into seq_file, one line for each hierarchy | 
 | 4400 |  *  - Used for /proc/<pid>/cgroup. | 
 | 4401 |  *  - No need to task_lock(tsk) on this tsk->cgroup reference, as it | 
 | 4402 |  *    doesn't really matter if tsk->cgroup changes after we read it, | 
| Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 4403 |  *    and we take cgroup_mutex, keeping cgroup_attach_task() from changing it | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4404 |  *    anyway.  No need to check that tsk->cgroup != NULL, thanks to | 
 | 4405 |  *    the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks | 
 | 4406 |  *    cgroup to top_cgroup. | 
 | 4407 |  */ | 
 | 4408 |  | 
 | 4409 | /* TODO: Use a proper seq_file iterator */ | 
 | 4410 | static int proc_cgroup_show(struct seq_file *m, void *v) | 
 | 4411 | { | 
 | 4412 | 	struct pid *pid; | 
 | 4413 | 	struct task_struct *tsk; | 
 | 4414 | 	char *buf; | 
 | 4415 | 	int retval; | 
 | 4416 | 	struct cgroupfs_root *root; | 
 | 4417 |  | 
 | 4418 | 	retval = -ENOMEM; | 
 | 4419 | 	buf = kmalloc(PAGE_SIZE, GFP_KERNEL); | 
 | 4420 | 	if (!buf) | 
 | 4421 | 		goto out; | 
 | 4422 |  | 
 | 4423 | 	retval = -ESRCH; | 
 | 4424 | 	pid = m->private; | 
 | 4425 | 	tsk = get_pid_task(pid, PIDTYPE_PID); | 
 | 4426 | 	if (!tsk) | 
 | 4427 | 		goto out_free; | 
 | 4428 |  | 
 | 4429 | 	retval = 0; | 
 | 4430 |  | 
 | 4431 | 	mutex_lock(&cgroup_mutex); | 
 | 4432 |  | 
| Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 4433 | 	for_each_active_root(root) { | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4434 | 		struct cgroup_subsys *ss; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4435 | 		struct cgroup *cgrp; | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4436 | 		int count = 0; | 
 | 4437 |  | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4438 | 		seq_printf(m, "%d:", root->hierarchy_id); | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4439 | 		for_each_subsys(root, ss) | 
 | 4440 | 			seq_printf(m, "%s%s", count++ ? "," : "", ss->name); | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 4441 | 		if (strlen(root->name)) | 
 | 4442 | 			seq_printf(m, "%sname=%s", count ? "," : "", | 
 | 4443 | 				   root->name); | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4444 | 		seq_putc(m, ':'); | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4445 | 		cgrp = task_cgroup_from_root(tsk, root); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4446 | 		retval = cgroup_path(cgrp, buf, PAGE_SIZE); | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4447 | 		if (retval < 0) | 
 | 4448 | 			goto out_unlock; | 
 | 4449 | 		seq_puts(m, buf); | 
 | 4450 | 		seq_putc(m, '\n'); | 
 | 4451 | 	} | 
 | 4452 |  | 
 | 4453 | out_unlock: | 
 | 4454 | 	mutex_unlock(&cgroup_mutex); | 
 | 4455 | 	put_task_struct(tsk); | 
 | 4456 | out_free: | 
 | 4457 | 	kfree(buf); | 
 | 4458 | out: | 
 | 4459 | 	return retval; | 
 | 4460 | } | 
 | 4461 |  | 
 | 4462 | static int cgroup_open(struct inode *inode, struct file *file) | 
 | 4463 | { | 
 | 4464 | 	struct pid *pid = PROC_I(inode)->pid; | 
 | 4465 | 	return single_open(file, proc_cgroup_show, pid); | 
 | 4466 | } | 
 | 4467 |  | 
| Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 4468 | const struct file_operations proc_cgroup_operations = { | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4469 | 	.open		= cgroup_open, | 
 | 4470 | 	.read		= seq_read, | 
 | 4471 | 	.llseek		= seq_lseek, | 
 | 4472 | 	.release	= single_release, | 
 | 4473 | }; | 
 | 4474 |  | 
 | 4475 | /* Display information about each subsystem and each hierarchy */ | 
 | 4476 | static int proc_cgroupstats_show(struct seq_file *m, void *v) | 
 | 4477 | { | 
 | 4478 | 	int i; | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4479 |  | 
| Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4480 | 	seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n"); | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4481 | 	/* | 
 | 4482 | 	 * ideally we don't want subsystems moving around while we do this. | 
 | 4483 | 	 * cgroup_mutex is also necessary to guarantee an atomic snapshot of | 
 | 4484 | 	 * subsys/hierarchy state. | 
 | 4485 | 	 */ | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4486 | 	mutex_lock(&cgroup_mutex); | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4487 | 	for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { | 
 | 4488 | 		struct cgroup_subsys *ss = subsys[i]; | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4489 | 		if (ss == NULL) | 
 | 4490 | 			continue; | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4491 | 		seq_printf(m, "%s\t%d\t%d\t%d\n", | 
 | 4492 | 			   ss->name, ss->root->hierarchy_id, | 
| Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4493 | 			   ss->root->number_of_cgroups, !ss->disabled); | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4494 | 	} | 
 | 4495 | 	mutex_unlock(&cgroup_mutex); | 
 | 4496 | 	return 0; | 
 | 4497 | } | 
 | 4498 |  | 
 | 4499 | static int cgroupstats_open(struct inode *inode, struct file *file) | 
 | 4500 | { | 
| Al Viro | 9dce07f1 | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 4501 | 	return single_open(file, proc_cgroupstats_show, NULL); | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4502 | } | 
 | 4503 |  | 
| Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 4504 | static const struct file_operations proc_cgroupstats_operations = { | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4505 | 	.open = cgroupstats_open, | 
 | 4506 | 	.read = seq_read, | 
 | 4507 | 	.llseek = seq_lseek, | 
 | 4508 | 	.release = single_release, | 
 | 4509 | }; | 
 | 4510 |  | 
| Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4511 | /** | 
 | 4512 |  * cgroup_fork - attach newly forked task to its parents cgroup. | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4513 |  * @child: pointer to task_struct of forking parent process. | 
| Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4514 |  * | 
 | 4515 |  * Description: A task inherits its parent's cgroup at fork(). | 
 | 4516 |  * | 
 | 4517 |  * A pointer to the shared css_set was automatically copied in | 
 | 4518 |  * fork.c by dup_task_struct().  However, we ignore that copy, since | 
| Frederic Weisbecker | 7e381b0e | 2011-12-21 20:03:19 +0100 | [diff] [blame] | 4519 |  * it was not made under the protection of RCU, cgroup_mutex or | 
 | 4520 |  * threadgroup_change_begin(), so it might no longer be a valid | 
 | 4521 |  * cgroup pointer.  cgroup_attach_task() might have already changed | 
 | 4522 |  * current->cgroups, allowing the previously referenced cgroup | 
 | 4523 |  * group to be removed and freed. | 
 | 4524 |  * | 
 | 4525 |  * Outside the pointer validity we also need to process the css_set | 
 | 4526 |  * inheritance between threadgoup_change_begin() and | 
 | 4527 |  * threadgoup_change_end(), this way there is no leak in any process | 
 | 4528 |  * wide migration performed by cgroup_attach_proc() that could otherwise | 
 | 4529 |  * miss a thread because it is too early or too late in the fork stage. | 
| Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4530 |  * | 
 | 4531 |  * At the point that cgroup_fork() is called, 'current' is the parent | 
 | 4532 |  * task, and the passed argument 'child' points to the child task. | 
 | 4533 |  */ | 
 | 4534 | void cgroup_fork(struct task_struct *child) | 
 | 4535 | { | 
| Frederic Weisbecker | 7e381b0e | 2011-12-21 20:03:19 +0100 | [diff] [blame] | 4536 | 	/* | 
 | 4537 | 	 * We don't need to task_lock() current because current->cgroups | 
 | 4538 | 	 * can't be changed concurrently here. The parent obviously hasn't | 
 | 4539 | 	 * exited and called cgroup_exit(), and we are synchronized against | 
 | 4540 | 	 * cgroup migration through threadgroup_change_begin(). | 
 | 4541 | 	 */ | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4542 | 	child->cgroups = current->cgroups; | 
 | 4543 | 	get_css_set(child->cgroups); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4544 | 	INIT_LIST_HEAD(&child->cg_list); | 
| Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4545 | } | 
 | 4546 |  | 
 | 4547 | /** | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4548 |  * cgroup_fork_callbacks - run fork callbacks | 
 | 4549 |  * @child: the new task | 
 | 4550 |  * | 
 | 4551 |  * Called on a new task very soon before adding it to the | 
 | 4552 |  * tasklist. No need to take any locks since no-one can | 
 | 4553 |  * be operating on this task. | 
| Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4554 |  */ | 
 | 4555 | void cgroup_fork_callbacks(struct task_struct *child) | 
 | 4556 | { | 
 | 4557 | 	if (need_forkexit_callback) { | 
 | 4558 | 		int i; | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4559 | 		/* | 
 | 4560 | 		 * forkexit callbacks are only supported for builtin | 
 | 4561 | 		 * subsystems, and the builtin section of the subsys array is | 
 | 4562 | 		 * immutable, so we don't need to lock the subsys array here. | 
 | 4563 | 		 */ | 
 | 4564 | 		for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) { | 
| Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4565 | 			struct cgroup_subsys *ss = subsys[i]; | 
 | 4566 | 			if (ss->fork) | 
| Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 4567 | 				ss->fork(child); | 
| Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4568 | 		} | 
 | 4569 | 	} | 
 | 4570 | } | 
 | 4571 |  | 
 | 4572 | /** | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4573 |  * cgroup_post_fork - called on a new task after adding it to the task list | 
 | 4574 |  * @child: the task in question | 
 | 4575 |  * | 
 | 4576 |  * Adds the task to the list running through its css_set if necessary. | 
 | 4577 |  * Has to be after the task is visible on the task list in case we race | 
 | 4578 |  * with the first call to cgroup_iter_start() - to guarantee that the | 
 | 4579 |  * new task ends up on its list. | 
 | 4580 |  */ | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4581 | void cgroup_post_fork(struct task_struct *child) | 
 | 4582 | { | 
| Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 4583 | 	/* | 
 | 4584 | 	 * use_task_css_set_links is set to 1 before we walk the tasklist | 
 | 4585 | 	 * under the tasklist_lock and we read it here after we added the child | 
 | 4586 | 	 * to the tasklist under the tasklist_lock as well. If the child wasn't | 
 | 4587 | 	 * yet in the tasklist when we walked through it from | 
 | 4588 | 	 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value | 
 | 4589 | 	 * should be visible now due to the paired locking and barriers implied | 
 | 4590 | 	 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock | 
 | 4591 | 	 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock | 
 | 4592 | 	 * lock on fork. | 
 | 4593 | 	 */ | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4594 | 	if (use_task_css_set_links) { | 
 | 4595 | 		write_lock(&css_set_lock); | 
| Frederic Weisbecker | 7e3aa30 | 2011-12-23 04:25:23 +0100 | [diff] [blame] | 4596 | 		if (list_empty(&child->cg_list)) { | 
 | 4597 | 			/* | 
 | 4598 | 			 * It's safe to use child->cgroups without task_lock() | 
 | 4599 | 			 * here because we are protected through | 
 | 4600 | 			 * threadgroup_change_begin() against concurrent | 
 | 4601 | 			 * css_set change in cgroup_task_migrate(). Also | 
 | 4602 | 			 * the task can't exit at that point until | 
 | 4603 | 			 * wake_up_new_task() is called, so we are protected | 
 | 4604 | 			 * against cgroup_exit() setting child->cgroup to | 
 | 4605 | 			 * init_css_set. | 
 | 4606 | 			 */ | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4607 | 			list_add(&child->cg_list, &child->cgroups->tasks); | 
| Frederic Weisbecker | 7e3aa30 | 2011-12-23 04:25:23 +0100 | [diff] [blame] | 4608 | 		} | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4609 | 		write_unlock(&css_set_lock); | 
 | 4610 | 	} | 
 | 4611 | } | 
 | 4612 | /** | 
| Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4613 |  * cgroup_exit - detach cgroup from exiting task | 
 | 4614 |  * @tsk: pointer to task_struct of exiting process | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4615 |  * @run_callback: run exit callbacks? | 
| Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4616 |  * | 
 | 4617 |  * Description: Detach cgroup from @tsk and release it. | 
 | 4618 |  * | 
 | 4619 |  * Note that cgroups marked notify_on_release force every task in | 
 | 4620 |  * them to take the global cgroup_mutex mutex when exiting. | 
 | 4621 |  * This could impact scaling on very large systems.  Be reluctant to | 
 | 4622 |  * use notify_on_release cgroups where very high task exit scaling | 
 | 4623 |  * is required on large systems. | 
 | 4624 |  * | 
 | 4625 |  * the_top_cgroup_hack: | 
 | 4626 |  * | 
 | 4627 |  *    Set the exiting tasks cgroup to the root cgroup (top_cgroup). | 
 | 4628 |  * | 
 | 4629 |  *    We call cgroup_exit() while the task is still competent to | 
 | 4630 |  *    handle notify_on_release(), then leave the task attached to the | 
 | 4631 |  *    root cgroup in each hierarchy for the remainder of its exit. | 
 | 4632 |  * | 
 | 4633 |  *    To do this properly, we would increment the reference count on | 
 | 4634 |  *    top_cgroup, and near the very end of the kernel/exit.c do_exit() | 
 | 4635 |  *    code we would add a second cgroup function call, to drop that | 
 | 4636 |  *    reference.  This would just create an unnecessary hot spot on | 
 | 4637 |  *    the top_cgroup reference count, to no avail. | 
 | 4638 |  * | 
 | 4639 |  *    Normally, holding a reference to a cgroup without bumping its | 
 | 4640 |  *    count is unsafe.   The cgroup could go away, or someone could | 
 | 4641 |  *    attach us to a different cgroup, decrementing the count on | 
 | 4642 |  *    the first cgroup that we never incremented.  But in this case, | 
 | 4643 |  *    top_cgroup isn't going away, and either task has PF_EXITING set, | 
| Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 4644 |  *    which wards off any cgroup_attach_task() attempts, or task is a failed | 
 | 4645 |  *    fork, never visible to cgroup_attach_task. | 
| Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4646 |  */ | 
 | 4647 | void cgroup_exit(struct task_struct *tsk, int run_callbacks) | 
 | 4648 | { | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4649 | 	struct css_set *cg; | 
| Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4650 | 	int i; | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4651 |  | 
 | 4652 | 	/* | 
 | 4653 | 	 * Unlink from the css_set task list if necessary. | 
 | 4654 | 	 * Optimistically check cg_list before taking | 
 | 4655 | 	 * css_set_lock | 
 | 4656 | 	 */ | 
 | 4657 | 	if (!list_empty(&tsk->cg_list)) { | 
 | 4658 | 		write_lock(&css_set_lock); | 
 | 4659 | 		if (!list_empty(&tsk->cg_list)) | 
| Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 4660 | 			list_del_init(&tsk->cg_list); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4661 | 		write_unlock(&css_set_lock); | 
 | 4662 | 	} | 
 | 4663 |  | 
| Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4664 | 	/* Reassign the task to the init_css_set. */ | 
 | 4665 | 	task_lock(tsk); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4666 | 	cg = tsk->cgroups; | 
 | 4667 | 	tsk->cgroups = &init_css_set; | 
| Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4668 |  | 
 | 4669 | 	if (run_callbacks && need_forkexit_callback) { | 
 | 4670 | 		/* | 
 | 4671 | 		 * modular subsystems can't use callbacks, so no need to lock | 
 | 4672 | 		 * the subsys array | 
 | 4673 | 		 */ | 
 | 4674 | 		for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) { | 
 | 4675 | 			struct cgroup_subsys *ss = subsys[i]; | 
 | 4676 | 			if (ss->exit) { | 
 | 4677 | 				struct cgroup *old_cgrp = | 
 | 4678 | 					rcu_dereference_raw(cg->subsys[i])->cgroup; | 
 | 4679 | 				struct cgroup *cgrp = task_cgroup(tsk, i); | 
| Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 4680 | 				ss->exit(cgrp, old_cgrp, tsk); | 
| Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4681 | 			} | 
 | 4682 | 		} | 
 | 4683 | 	} | 
| Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4684 | 	task_unlock(tsk); | 
| Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4685 |  | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4686 | 	if (cg) | 
| Colin Cross | c0f6fa8 | 2010-11-23 21:37:03 -0800 | [diff] [blame] | 4687 | 		put_css_set(cg); | 
| Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4688 | } | 
| Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 4689 |  | 
 | 4690 | /** | 
| Grzegorz Nosek | 313e924 | 2009-04-02 16:57:23 -0700 | [diff] [blame] | 4691 |  * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4692 |  * @cgrp: the cgroup in question | 
| Grzegorz Nosek | 313e924 | 2009-04-02 16:57:23 -0700 | [diff] [blame] | 4693 |  * @task: the task in question | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4694 |  * | 
| Grzegorz Nosek | 313e924 | 2009-04-02 16:57:23 -0700 | [diff] [blame] | 4695 |  * See if @cgrp is a descendant of @task's cgroup in the appropriate | 
 | 4696 |  * hierarchy. | 
| Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 4697 |  * | 
 | 4698 |  * If we are sending in dummytop, then presumably we are creating | 
 | 4699 |  * the top cgroup in the subsystem. | 
 | 4700 |  * | 
 | 4701 |  * Called only by the ns (nsproxy) cgroup. | 
 | 4702 |  */ | 
| Grzegorz Nosek | 313e924 | 2009-04-02 16:57:23 -0700 | [diff] [blame] | 4703 | int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task) | 
| Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 4704 | { | 
 | 4705 | 	int ret; | 
 | 4706 | 	struct cgroup *target; | 
| Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 4707 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4708 | 	if (cgrp == dummytop) | 
| Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 4709 | 		return 1; | 
 | 4710 |  | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4711 | 	target = task_cgroup_from_root(task, cgrp->root); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4712 | 	while (cgrp != target && cgrp!= cgrp->top_cgroup) | 
 | 4713 | 		cgrp = cgrp->parent; | 
 | 4714 | 	ret = (cgrp == target); | 
| Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 4715 | 	return ret; | 
 | 4716 | } | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4717 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4718 | static void check_for_release(struct cgroup *cgrp) | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4719 | { | 
 | 4720 | 	/* All of these checks rely on RCU to keep the cgroup | 
 | 4721 | 	 * structure alive */ | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4722 | 	if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count) | 
 | 4723 | 	    && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) { | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4724 | 		/* Control Group is currently removeable. If it's not | 
 | 4725 | 		 * already queued for a userspace notification, queue | 
 | 4726 | 		 * it now */ | 
 | 4727 | 		int need_schedule_work = 0; | 
| Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4728 | 		raw_spin_lock(&release_list_lock); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4729 | 		if (!cgroup_is_removed(cgrp) && | 
 | 4730 | 		    list_empty(&cgrp->release_list)) { | 
 | 4731 | 			list_add(&cgrp->release_list, &release_list); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4732 | 			need_schedule_work = 1; | 
 | 4733 | 		} | 
| Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4734 | 		raw_spin_unlock(&release_list_lock); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4735 | 		if (need_schedule_work) | 
 | 4736 | 			schedule_work(&release_agent_work); | 
 | 4737 | 	} | 
 | 4738 | } | 
 | 4739 |  | 
| Daisuke Nishimura | d7b9fff | 2010-03-10 15:22:05 -0800 | [diff] [blame] | 4740 | /* Caller must verify that the css is not for root cgroup */ | 
| Colin Cross | c0f6fa8 | 2010-11-23 21:37:03 -0800 | [diff] [blame] | 4741 | void __css_get(struct cgroup_subsys_state *css, int count) | 
 | 4742 | { | 
 | 4743 | 	atomic_add(count, &css->refcnt); | 
 | 4744 | 	set_bit(CGRP_RELEASABLE, &css->cgroup->flags); | 
 | 4745 | } | 
 | 4746 | EXPORT_SYMBOL_GPL(__css_get); | 
 | 4747 |  | 
 | 4748 | /* Caller must verify that the css is not for root cgroup */ | 
| Daisuke Nishimura | d7b9fff | 2010-03-10 15:22:05 -0800 | [diff] [blame] | 4749 | void __css_put(struct cgroup_subsys_state *css, int count) | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4750 | { | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4751 | 	struct cgroup *cgrp = css->cgroup; | 
| KAMEZAWA Hiroyuki | 3dece83 | 2009-10-01 15:44:09 -0700 | [diff] [blame] | 4752 | 	int val; | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4753 | 	rcu_read_lock(); | 
| Daisuke Nishimura | d7b9fff | 2010-03-10 15:22:05 -0800 | [diff] [blame] | 4754 | 	val = atomic_sub_return(count, &css->refcnt); | 
| KAMEZAWA Hiroyuki | 3dece83 | 2009-10-01 15:44:09 -0700 | [diff] [blame] | 4755 | 	if (val == 1) { | 
| Colin Cross | c0f6fa8 | 2010-11-23 21:37:03 -0800 | [diff] [blame] | 4756 | 		check_for_release(cgrp); | 
| KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 4757 | 		cgroup_wakeup_rmdir_waiter(cgrp); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4758 | 	} | 
 | 4759 | 	rcu_read_unlock(); | 
| KAMEZAWA Hiroyuki | 3dece83 | 2009-10-01 15:44:09 -0700 | [diff] [blame] | 4760 | 	WARN_ON_ONCE(val < 1); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4761 | } | 
| Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 4762 | EXPORT_SYMBOL_GPL(__css_put); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4763 |  | 
 | 4764 | /* | 
 | 4765 |  * Notify userspace when a cgroup is released, by running the | 
 | 4766 |  * configured release agent with the name of the cgroup (path | 
 | 4767 |  * relative to the root of cgroup file system) as the argument. | 
 | 4768 |  * | 
 | 4769 |  * Most likely, this user command will try to rmdir this cgroup. | 
 | 4770 |  * | 
 | 4771 |  * This races with the possibility that some other task will be | 
 | 4772 |  * attached to this cgroup before it is removed, or that some other | 
 | 4773 |  * user task will 'mkdir' a child cgroup of this cgroup.  That's ok. | 
 | 4774 |  * The presumed 'rmdir' will fail quietly if this cgroup is no longer | 
 | 4775 |  * unused, and this cgroup will be reprieved from its death sentence, | 
 | 4776 |  * to continue to serve a useful existence.  Next time it's released, | 
 | 4777 |  * we will get notified again, if it still has 'notify_on_release' set. | 
 | 4778 |  * | 
 | 4779 |  * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which | 
 | 4780 |  * means only wait until the task is successfully execve()'d.  The | 
 | 4781 |  * separate release agent task is forked by call_usermodehelper(), | 
 | 4782 |  * then control in this thread returns here, without waiting for the | 
 | 4783 |  * release agent task.  We don't bother to wait because the caller of | 
 | 4784 |  * this routine has no use for the exit status of the release agent | 
 | 4785 |  * task, so no sense holding our caller up for that. | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4786 |  */ | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4787 | static void cgroup_release_agent(struct work_struct *work) | 
 | 4788 | { | 
 | 4789 | 	BUG_ON(work != &release_agent_work); | 
 | 4790 | 	mutex_lock(&cgroup_mutex); | 
| Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4791 | 	raw_spin_lock(&release_list_lock); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4792 | 	while (!list_empty(&release_list)) { | 
 | 4793 | 		char *argv[3], *envp[3]; | 
 | 4794 | 		int i; | 
| Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 4795 | 		char *pathbuf = NULL, *agentbuf = NULL; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4796 | 		struct cgroup *cgrp = list_entry(release_list.next, | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4797 | 						    struct cgroup, | 
 | 4798 | 						    release_list); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4799 | 		list_del_init(&cgrp->release_list); | 
| Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4800 | 		raw_spin_unlock(&release_list_lock); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4801 | 		pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); | 
| Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 4802 | 		if (!pathbuf) | 
 | 4803 | 			goto continue_free; | 
 | 4804 | 		if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0) | 
 | 4805 | 			goto continue_free; | 
 | 4806 | 		agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL); | 
 | 4807 | 		if (!agentbuf) | 
 | 4808 | 			goto continue_free; | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4809 |  | 
 | 4810 | 		i = 0; | 
| Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 4811 | 		argv[i++] = agentbuf; | 
 | 4812 | 		argv[i++] = pathbuf; | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4813 | 		argv[i] = NULL; | 
 | 4814 |  | 
 | 4815 | 		i = 0; | 
 | 4816 | 		/* minimal command environment */ | 
 | 4817 | 		envp[i++] = "HOME=/"; | 
 | 4818 | 		envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; | 
 | 4819 | 		envp[i] = NULL; | 
 | 4820 |  | 
 | 4821 | 		/* Drop the lock while we invoke the usermode helper, | 
 | 4822 | 		 * since the exec could involve hitting disk and hence | 
 | 4823 | 		 * be a slow process */ | 
 | 4824 | 		mutex_unlock(&cgroup_mutex); | 
 | 4825 | 		call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4826 | 		mutex_lock(&cgroup_mutex); | 
| Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 4827 |  continue_free: | 
 | 4828 | 		kfree(pathbuf); | 
 | 4829 | 		kfree(agentbuf); | 
| Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4830 | 		raw_spin_lock(&release_list_lock); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4831 | 	} | 
| Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4832 | 	raw_spin_unlock(&release_list_lock); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4833 | 	mutex_unlock(&cgroup_mutex); | 
 | 4834 | } | 
| Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4835 |  | 
 | 4836 | static int __init cgroup_disable(char *str) | 
 | 4837 | { | 
 | 4838 | 	int i; | 
 | 4839 | 	char *token; | 
 | 4840 |  | 
 | 4841 | 	while ((token = strsep(&str, ",")) != NULL) { | 
 | 4842 | 		if (!*token) | 
 | 4843 | 			continue; | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4844 | 		/* | 
 | 4845 | 		 * cgroup_disable, being at boot time, can't know about module | 
 | 4846 | 		 * subsystems, so we don't worry about them. | 
 | 4847 | 		 */ | 
 | 4848 | 		for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) { | 
| Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4849 | 			struct cgroup_subsys *ss = subsys[i]; | 
 | 4850 |  | 
 | 4851 | 			if (!strcmp(token, ss->name)) { | 
 | 4852 | 				ss->disabled = 1; | 
 | 4853 | 				printk(KERN_INFO "Disabling %s control group" | 
 | 4854 | 					" subsystem\n", ss->name); | 
 | 4855 | 				break; | 
 | 4856 | 			} | 
 | 4857 | 		} | 
 | 4858 | 	} | 
 | 4859 | 	return 1; | 
 | 4860 | } | 
 | 4861 | __setup("cgroup_disable=", cgroup_disable); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4862 |  | 
 | 4863 | /* | 
 | 4864 |  * Functons for CSS ID. | 
 | 4865 |  */ | 
 | 4866 |  | 
 | 4867 | /* | 
 | 4868 |  *To get ID other than 0, this should be called when !cgroup_is_removed(). | 
 | 4869 |  */ | 
 | 4870 | unsigned short css_id(struct cgroup_subsys_state *css) | 
 | 4871 | { | 
| KAMEZAWA Hiroyuki | 7f0f154 | 2010-05-11 14:06:58 -0700 | [diff] [blame] | 4872 | 	struct css_id *cssid; | 
 | 4873 |  | 
 | 4874 | 	/* | 
 | 4875 | 	 * This css_id() can return correct value when somone has refcnt | 
 | 4876 | 	 * on this or this is under rcu_read_lock(). Once css->id is allocated, | 
 | 4877 | 	 * it's unchanged until freed. | 
 | 4878 | 	 */ | 
| Michal Hocko | d8bf4ca | 2011-07-08 14:39:41 +0200 | [diff] [blame] | 4879 | 	cssid = rcu_dereference_check(css->id, atomic_read(&css->refcnt)); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4880 |  | 
 | 4881 | 	if (cssid) | 
 | 4882 | 		return cssid->id; | 
 | 4883 | 	return 0; | 
 | 4884 | } | 
| Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 4885 | EXPORT_SYMBOL_GPL(css_id); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4886 |  | 
 | 4887 | unsigned short css_depth(struct cgroup_subsys_state *css) | 
 | 4888 | { | 
| KAMEZAWA Hiroyuki | 7f0f154 | 2010-05-11 14:06:58 -0700 | [diff] [blame] | 4889 | 	struct css_id *cssid; | 
 | 4890 |  | 
| Michal Hocko | d8bf4ca | 2011-07-08 14:39:41 +0200 | [diff] [blame] | 4891 | 	cssid = rcu_dereference_check(css->id, atomic_read(&css->refcnt)); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4892 |  | 
 | 4893 | 	if (cssid) | 
 | 4894 | 		return cssid->depth; | 
 | 4895 | 	return 0; | 
 | 4896 | } | 
| Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 4897 | EXPORT_SYMBOL_GPL(css_depth); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4898 |  | 
| KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 4899 | /** | 
 | 4900 |  *  css_is_ancestor - test "root" css is an ancestor of "child" | 
 | 4901 |  * @child: the css to be tested. | 
 | 4902 |  * @root: the css supporsed to be an ancestor of the child. | 
 | 4903 |  * | 
 | 4904 |  * Returns true if "root" is an ancestor of "child" in its hierarchy. Because | 
 | 4905 |  * this function reads css->id, this use rcu_dereference() and rcu_read_lock(). | 
 | 4906 |  * But, considering usual usage, the csses should be valid objects after test. | 
 | 4907 |  * Assuming that the caller will do some action to the child if this returns | 
 | 4908 |  * returns true, the caller must take "child";s reference count. | 
 | 4909 |  * If "child" is valid object and this returns true, "root" is valid, too. | 
 | 4910 |  */ | 
 | 4911 |  | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4912 | bool css_is_ancestor(struct cgroup_subsys_state *child, | 
| KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 4913 | 		    const struct cgroup_subsys_state *root) | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4914 | { | 
| KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 4915 | 	struct css_id *child_id; | 
 | 4916 | 	struct css_id *root_id; | 
 | 4917 | 	bool ret = true; | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4918 |  | 
| KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 4919 | 	rcu_read_lock(); | 
 | 4920 | 	child_id  = rcu_dereference(child->id); | 
 | 4921 | 	root_id = rcu_dereference(root->id); | 
 | 4922 | 	if (!child_id | 
 | 4923 | 	    || !root_id | 
 | 4924 | 	    || (child_id->depth < root_id->depth) | 
 | 4925 | 	    || (child_id->stack[root_id->depth] != root_id->id)) | 
 | 4926 | 		ret = false; | 
 | 4927 | 	rcu_read_unlock(); | 
 | 4928 | 	return ret; | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4929 | } | 
 | 4930 |  | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4931 | void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css) | 
 | 4932 | { | 
 | 4933 | 	struct css_id *id = css->id; | 
 | 4934 | 	/* When this is called before css_id initialization, id can be NULL */ | 
 | 4935 | 	if (!id) | 
 | 4936 | 		return; | 
 | 4937 |  | 
 | 4938 | 	BUG_ON(!ss->use_id); | 
 | 4939 |  | 
 | 4940 | 	rcu_assign_pointer(id->css, NULL); | 
 | 4941 | 	rcu_assign_pointer(css->id, NULL); | 
| Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 4942 | 	spin_lock(&ss->id_lock); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4943 | 	idr_remove(&ss->idr, id->id); | 
| Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 4944 | 	spin_unlock(&ss->id_lock); | 
| Lai Jiangshan | 025cea9 | 2011-03-15 17:56:10 +0800 | [diff] [blame] | 4945 | 	kfree_rcu(id, rcu_head); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4946 | } | 
| Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 4947 | EXPORT_SYMBOL_GPL(free_css_id); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4948 |  | 
 | 4949 | /* | 
 | 4950 |  * This is called by init or create(). Then, calls to this function are | 
 | 4951 |  * always serialized (By cgroup_mutex() at create()). | 
 | 4952 |  */ | 
 | 4953 |  | 
 | 4954 | static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth) | 
 | 4955 | { | 
 | 4956 | 	struct css_id *newid; | 
 | 4957 | 	int myid, error, size; | 
 | 4958 |  | 
 | 4959 | 	BUG_ON(!ss->use_id); | 
 | 4960 |  | 
 | 4961 | 	size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1); | 
 | 4962 | 	newid = kzalloc(size, GFP_KERNEL); | 
 | 4963 | 	if (!newid) | 
 | 4964 | 		return ERR_PTR(-ENOMEM); | 
 | 4965 | 	/* get id */ | 
 | 4966 | 	if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) { | 
 | 4967 | 		error = -ENOMEM; | 
 | 4968 | 		goto err_out; | 
 | 4969 | 	} | 
| Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 4970 | 	spin_lock(&ss->id_lock); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4971 | 	/* Don't use 0. allocates an ID of 1-65535 */ | 
 | 4972 | 	error = idr_get_new_above(&ss->idr, newid, 1, &myid); | 
| Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 4973 | 	spin_unlock(&ss->id_lock); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4974 |  | 
 | 4975 | 	/* Returns error when there are no free spaces for new ID.*/ | 
 | 4976 | 	if (error) { | 
 | 4977 | 		error = -ENOSPC; | 
 | 4978 | 		goto err_out; | 
 | 4979 | 	} | 
 | 4980 | 	if (myid > CSS_ID_MAX) | 
 | 4981 | 		goto remove_idr; | 
 | 4982 |  | 
 | 4983 | 	newid->id = myid; | 
 | 4984 | 	newid->depth = depth; | 
 | 4985 | 	return newid; | 
 | 4986 | remove_idr: | 
 | 4987 | 	error = -ENOSPC; | 
| Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 4988 | 	spin_lock(&ss->id_lock); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4989 | 	idr_remove(&ss->idr, myid); | 
| Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 4990 | 	spin_unlock(&ss->id_lock); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4991 | err_out: | 
 | 4992 | 	kfree(newid); | 
 | 4993 | 	return ERR_PTR(error); | 
 | 4994 |  | 
 | 4995 | } | 
 | 4996 |  | 
| Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4997 | static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss, | 
 | 4998 | 					    struct cgroup_subsys_state *rootcss) | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4999 | { | 
 | 5000 | 	struct css_id *newid; | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5001 |  | 
| Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5002 | 	spin_lock_init(&ss->id_lock); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5003 | 	idr_init(&ss->idr); | 
 | 5004 |  | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5005 | 	newid = get_new_cssid(ss, 0); | 
 | 5006 | 	if (IS_ERR(newid)) | 
 | 5007 | 		return PTR_ERR(newid); | 
 | 5008 |  | 
 | 5009 | 	newid->stack[0] = newid->id; | 
 | 5010 | 	newid->css = rootcss; | 
 | 5011 | 	rootcss->id = newid; | 
 | 5012 | 	return 0; | 
 | 5013 | } | 
 | 5014 |  | 
 | 5015 | static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent, | 
 | 5016 | 			struct cgroup *child) | 
 | 5017 | { | 
 | 5018 | 	int subsys_id, i, depth = 0; | 
 | 5019 | 	struct cgroup_subsys_state *parent_css, *child_css; | 
| Li Zefan | fae9c79 | 2010-04-22 17:30:00 +0800 | [diff] [blame] | 5020 | 	struct css_id *child_id, *parent_id; | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5021 |  | 
 | 5022 | 	subsys_id = ss->subsys_id; | 
 | 5023 | 	parent_css = parent->subsys[subsys_id]; | 
 | 5024 | 	child_css = child->subsys[subsys_id]; | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5025 | 	parent_id = parent_css->id; | 
| Greg Thelen | 94b3dd0 | 2010-06-04 14:15:03 -0700 | [diff] [blame] | 5026 | 	depth = parent_id->depth + 1; | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5027 |  | 
 | 5028 | 	child_id = get_new_cssid(ss, depth); | 
 | 5029 | 	if (IS_ERR(child_id)) | 
 | 5030 | 		return PTR_ERR(child_id); | 
 | 5031 |  | 
 | 5032 | 	for (i = 0; i < depth; i++) | 
 | 5033 | 		child_id->stack[i] = parent_id->stack[i]; | 
 | 5034 | 	child_id->stack[depth] = child_id->id; | 
 | 5035 | 	/* | 
 | 5036 | 	 * child_id->css pointer will be set after this cgroup is available | 
 | 5037 | 	 * see cgroup_populate_dir() | 
 | 5038 | 	 */ | 
 | 5039 | 	rcu_assign_pointer(child_css->id, child_id); | 
 | 5040 |  | 
 | 5041 | 	return 0; | 
 | 5042 | } | 
 | 5043 |  | 
 | 5044 | /** | 
 | 5045 |  * css_lookup - lookup css by id | 
 | 5046 |  * @ss: cgroup subsys to be looked into. | 
 | 5047 |  * @id: the id | 
 | 5048 |  * | 
 | 5049 |  * Returns pointer to cgroup_subsys_state if there is valid one with id. | 
 | 5050 |  * NULL if not. Should be called under rcu_read_lock() | 
 | 5051 |  */ | 
 | 5052 | struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id) | 
 | 5053 | { | 
 | 5054 | 	struct css_id *cssid = NULL; | 
 | 5055 |  | 
 | 5056 | 	BUG_ON(!ss->use_id); | 
 | 5057 | 	cssid = idr_find(&ss->idr, id); | 
 | 5058 |  | 
 | 5059 | 	if (unlikely(!cssid)) | 
 | 5060 | 		return NULL; | 
 | 5061 |  | 
 | 5062 | 	return rcu_dereference(cssid->css); | 
 | 5063 | } | 
| Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5064 | EXPORT_SYMBOL_GPL(css_lookup); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5065 |  | 
 | 5066 | /** | 
 | 5067 |  * css_get_next - lookup next cgroup under specified hierarchy. | 
 | 5068 |  * @ss: pointer to subsystem | 
 | 5069 |  * @id: current position of iteration. | 
 | 5070 |  * @root: pointer to css. search tree under this. | 
 | 5071 |  * @foundid: position of found object. | 
 | 5072 |  * | 
 | 5073 |  * Search next css under the specified hierarchy of rootid. Calling under | 
 | 5074 |  * rcu_read_lock() is necessary. Returns NULL if it reaches the end. | 
 | 5075 |  */ | 
 | 5076 | struct cgroup_subsys_state * | 
 | 5077 | css_get_next(struct cgroup_subsys *ss, int id, | 
 | 5078 | 	     struct cgroup_subsys_state *root, int *foundid) | 
 | 5079 | { | 
 | 5080 | 	struct cgroup_subsys_state *ret = NULL; | 
 | 5081 | 	struct css_id *tmp; | 
 | 5082 | 	int tmpid; | 
 | 5083 | 	int rootid = css_id(root); | 
 | 5084 | 	int depth = css_depth(root); | 
 | 5085 |  | 
 | 5086 | 	if (!rootid) | 
 | 5087 | 		return NULL; | 
 | 5088 |  | 
 | 5089 | 	BUG_ON(!ss->use_id); | 
| Hugh Dickins | ca464d6 | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5090 | 	WARN_ON_ONCE(!rcu_read_lock_held()); | 
 | 5091 |  | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5092 | 	/* fill start point for scan */ | 
 | 5093 | 	tmpid = id; | 
 | 5094 | 	while (1) { | 
 | 5095 | 		/* | 
 | 5096 | 		 * scan next entry from bitmap(tree), tmpid is updated after | 
 | 5097 | 		 * idr_get_next(). | 
 | 5098 | 		 */ | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5099 | 		tmp = idr_get_next(&ss->idr, &tmpid); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5100 | 		if (!tmp) | 
 | 5101 | 			break; | 
 | 5102 | 		if (tmp->depth >= depth && tmp->stack[depth] == rootid) { | 
 | 5103 | 			ret = rcu_dereference(tmp->css); | 
 | 5104 | 			if (ret) { | 
 | 5105 | 				*foundid = tmpid; | 
 | 5106 | 				break; | 
 | 5107 | 			} | 
 | 5108 | 		} | 
 | 5109 | 		/* continue to scan from next id */ | 
 | 5110 | 		tmpid = tmpid + 1; | 
 | 5111 | 	} | 
 | 5112 | 	return ret; | 
 | 5113 | } | 
 | 5114 |  | 
| Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5115 | /* | 
 | 5116 |  * get corresponding css from file open on cgroupfs directory | 
 | 5117 |  */ | 
 | 5118 | struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id) | 
 | 5119 | { | 
 | 5120 | 	struct cgroup *cgrp; | 
 | 5121 | 	struct inode *inode; | 
 | 5122 | 	struct cgroup_subsys_state *css; | 
 | 5123 |  | 
 | 5124 | 	inode = f->f_dentry->d_inode; | 
 | 5125 | 	/* check in cgroup filesystem dir */ | 
 | 5126 | 	if (inode->i_op != &cgroup_dir_inode_operations) | 
 | 5127 | 		return ERR_PTR(-EBADF); | 
 | 5128 |  | 
 | 5129 | 	if (id < 0 || id >= CGROUP_SUBSYS_COUNT) | 
 | 5130 | 		return ERR_PTR(-EINVAL); | 
 | 5131 |  | 
 | 5132 | 	/* get cgroup */ | 
 | 5133 | 	cgrp = __d_cgrp(f->f_dentry); | 
 | 5134 | 	css = cgrp->subsys[id]; | 
 | 5135 | 	return css ? css : ERR_PTR(-ENOENT); | 
 | 5136 | } | 
 | 5137 |  | 
| Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5138 | #ifdef CONFIG_CGROUP_DEBUG | 
| Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 5139 | static struct cgroup_subsys_state *debug_create(struct cgroup *cont) | 
| Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5140 | { | 
 | 5141 | 	struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL); | 
 | 5142 |  | 
 | 5143 | 	if (!css) | 
 | 5144 | 		return ERR_PTR(-ENOMEM); | 
 | 5145 |  | 
 | 5146 | 	return css; | 
 | 5147 | } | 
 | 5148 |  | 
| Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 5149 | static void debug_destroy(struct cgroup *cont) | 
| Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5150 | { | 
 | 5151 | 	kfree(cont->subsys[debug_subsys_id]); | 
 | 5152 | } | 
 | 5153 |  | 
 | 5154 | static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft) | 
 | 5155 | { | 
 | 5156 | 	return atomic_read(&cont->count); | 
 | 5157 | } | 
 | 5158 |  | 
 | 5159 | static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft) | 
 | 5160 | { | 
 | 5161 | 	return cgroup_task_count(cont); | 
 | 5162 | } | 
 | 5163 |  | 
 | 5164 | static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft) | 
 | 5165 | { | 
 | 5166 | 	return (u64)(unsigned long)current->cgroups; | 
 | 5167 | } | 
 | 5168 |  | 
 | 5169 | static u64 current_css_set_refcount_read(struct cgroup *cont, | 
 | 5170 | 					   struct cftype *cft) | 
 | 5171 | { | 
 | 5172 | 	u64 count; | 
 | 5173 |  | 
 | 5174 | 	rcu_read_lock(); | 
 | 5175 | 	count = atomic_read(¤t->cgroups->refcount); | 
 | 5176 | 	rcu_read_unlock(); | 
 | 5177 | 	return count; | 
 | 5178 | } | 
 | 5179 |  | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5180 | static int current_css_set_cg_links_read(struct cgroup *cont, | 
 | 5181 | 					 struct cftype *cft, | 
 | 5182 | 					 struct seq_file *seq) | 
 | 5183 | { | 
 | 5184 | 	struct cg_cgroup_link *link; | 
 | 5185 | 	struct css_set *cg; | 
 | 5186 |  | 
 | 5187 | 	read_lock(&css_set_lock); | 
 | 5188 | 	rcu_read_lock(); | 
 | 5189 | 	cg = rcu_dereference(current->cgroups); | 
 | 5190 | 	list_for_each_entry(link, &cg->cg_links, cg_link_list) { | 
 | 5191 | 		struct cgroup *c = link->cgrp; | 
 | 5192 | 		const char *name; | 
 | 5193 |  | 
 | 5194 | 		if (c->dentry) | 
 | 5195 | 			name = c->dentry->d_name.name; | 
 | 5196 | 		else | 
 | 5197 | 			name = "?"; | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5198 | 		seq_printf(seq, "Root %d group %s\n", | 
 | 5199 | 			   c->root->hierarchy_id, name); | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5200 | 	} | 
 | 5201 | 	rcu_read_unlock(); | 
 | 5202 | 	read_unlock(&css_set_lock); | 
 | 5203 | 	return 0; | 
 | 5204 | } | 
 | 5205 |  | 
 | 5206 | #define MAX_TASKS_SHOWN_PER_CSS 25 | 
 | 5207 | static int cgroup_css_links_read(struct cgroup *cont, | 
 | 5208 | 				 struct cftype *cft, | 
 | 5209 | 				 struct seq_file *seq) | 
 | 5210 | { | 
 | 5211 | 	struct cg_cgroup_link *link; | 
 | 5212 |  | 
 | 5213 | 	read_lock(&css_set_lock); | 
 | 5214 | 	list_for_each_entry(link, &cont->css_sets, cgrp_link_list) { | 
 | 5215 | 		struct css_set *cg = link->cg; | 
 | 5216 | 		struct task_struct *task; | 
 | 5217 | 		int count = 0; | 
 | 5218 | 		seq_printf(seq, "css_set %p\n", cg); | 
 | 5219 | 		list_for_each_entry(task, &cg->tasks, cg_list) { | 
 | 5220 | 			if (count++ > MAX_TASKS_SHOWN_PER_CSS) { | 
 | 5221 | 				seq_puts(seq, "  ...\n"); | 
 | 5222 | 				break; | 
 | 5223 | 			} else { | 
 | 5224 | 				seq_printf(seq, "  task %d\n", | 
 | 5225 | 					   task_pid_vnr(task)); | 
 | 5226 | 			} | 
 | 5227 | 		} | 
 | 5228 | 	} | 
 | 5229 | 	read_unlock(&css_set_lock); | 
 | 5230 | 	return 0; | 
 | 5231 | } | 
 | 5232 |  | 
| Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5233 | static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft) | 
 | 5234 | { | 
 | 5235 | 	return test_bit(CGRP_RELEASABLE, &cgrp->flags); | 
 | 5236 | } | 
 | 5237 |  | 
 | 5238 | static struct cftype debug_files[] =  { | 
 | 5239 | 	{ | 
 | 5240 | 		.name = "cgroup_refcount", | 
 | 5241 | 		.read_u64 = cgroup_refcount_read, | 
 | 5242 | 	}, | 
 | 5243 | 	{ | 
 | 5244 | 		.name = "taskcount", | 
 | 5245 | 		.read_u64 = debug_taskcount_read, | 
 | 5246 | 	}, | 
 | 5247 |  | 
 | 5248 | 	{ | 
 | 5249 | 		.name = "current_css_set", | 
 | 5250 | 		.read_u64 = current_css_set_read, | 
 | 5251 | 	}, | 
 | 5252 |  | 
 | 5253 | 	{ | 
 | 5254 | 		.name = "current_css_set_refcount", | 
 | 5255 | 		.read_u64 = current_css_set_refcount_read, | 
 | 5256 | 	}, | 
 | 5257 |  | 
 | 5258 | 	{ | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5259 | 		.name = "current_css_set_cg_links", | 
 | 5260 | 		.read_seq_string = current_css_set_cg_links_read, | 
 | 5261 | 	}, | 
 | 5262 |  | 
 | 5263 | 	{ | 
 | 5264 | 		.name = "cgroup_css_links", | 
 | 5265 | 		.read_seq_string = cgroup_css_links_read, | 
 | 5266 | 	}, | 
 | 5267 |  | 
 | 5268 | 	{ | 
| Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5269 | 		.name = "releasable", | 
 | 5270 | 		.read_u64 = releasable_read, | 
 | 5271 | 	}, | 
 | 5272 | }; | 
 | 5273 |  | 
 | 5274 | static int debug_populate(struct cgroup_subsys *ss, struct cgroup *cont) | 
 | 5275 | { | 
 | 5276 | 	return cgroup_add_files(cont, ss, debug_files, | 
 | 5277 | 				ARRAY_SIZE(debug_files)); | 
 | 5278 | } | 
 | 5279 |  | 
 | 5280 | struct cgroup_subsys debug_subsys = { | 
 | 5281 | 	.name = "debug", | 
 | 5282 | 	.create = debug_create, | 
 | 5283 | 	.destroy = debug_destroy, | 
 | 5284 | 	.populate = debug_populate, | 
 | 5285 | 	.subsys_id = debug_subsys_id, | 
 | 5286 | }; | 
 | 5287 | #endif /* CONFIG_CGROUP_DEBUG */ |