| 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 */ | 
| Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 63 | #include <linux/kthread.h> | 
| Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 64 |  | 
| Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 65 | #include <linux/atomic.h> | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 66 |  | 
| Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 67 | /* css deactivation bias, makes css->refcnt negative to deny new trygets */ | 
 | 68 | #define CSS_DEACT_BIAS		INT_MIN | 
 | 69 |  | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 70 | /* | 
 | 71 |  * cgroup_mutex is the master lock.  Any modification to cgroup or its | 
 | 72 |  * hierarchy must be performed while holding it. | 
 | 73 |  * | 
 | 74 |  * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify | 
 | 75 |  * cgroupfs_root of any cgroup hierarchy - subsys list, flags, | 
 | 76 |  * release_agent_path and so on.  Modifying requires both cgroup_mutex and | 
 | 77 |  * cgroup_root_mutex.  Readers can acquire either of the two.  This is to | 
 | 78 |  * break the following locking order cycle. | 
 | 79 |  * | 
 | 80 |  *  A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem | 
 | 81 |  *  B. namespace_sem -> cgroup_mutex | 
 | 82 |  * | 
 | 83 |  * B happens only through cgroup_show_options() and using cgroup_root_mutex | 
 | 84 |  * breaks it. | 
 | 85 |  */ | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 86 | static DEFINE_MUTEX(cgroup_mutex); | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 87 | static DEFINE_MUTEX(cgroup_root_mutex); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 88 |  | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 89 | /* | 
 | 90 |  * Generate an array of cgroup subsystem pointers. At boot time, this is | 
| Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 91 |  * populated with the built in subsystems, and modular subsystems are | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 92 |  * registered after that. The mutable section of this array is protected by | 
 | 93 |  * cgroup_mutex. | 
 | 94 |  */ | 
| Daniel Wagner | 80f4c87 | 2012-09-12 16:12:06 +0200 | [diff] [blame] | 95 | #define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys, | 
| Daniel Wagner | 5fc0b02 | 2012-09-12 16:12:05 +0200 | [diff] [blame] | 96 | #define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option) | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 97 | static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = { | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 98 | #include <linux/cgroup_subsys.h> | 
 | 99 | }; | 
 | 100 |  | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 101 | #define MAX_CGROUP_ROOT_NAMELEN 64 | 
 | 102 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 103 | /* | 
 | 104 |  * A cgroupfs_root represents the root of a cgroup hierarchy, | 
 | 105 |  * and may be associated with a superblock to form an active | 
 | 106 |  * hierarchy | 
 | 107 |  */ | 
 | 108 | struct cgroupfs_root { | 
 | 109 | 	struct super_block *sb; | 
 | 110 |  | 
 | 111 | 	/* | 
 | 112 | 	 * The bitmask of subsystems intended to be attached to this | 
 | 113 | 	 * hierarchy | 
 | 114 | 	 */ | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 115 | 	unsigned long subsys_mask; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 116 |  | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 117 | 	/* Unique id for this hierarchy. */ | 
 | 118 | 	int hierarchy_id; | 
 | 119 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 120 | 	/* The bitmask of subsystems currently attached to this hierarchy */ | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 121 | 	unsigned long actual_subsys_mask; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 122 |  | 
 | 123 | 	/* A list running through the attached subsystems */ | 
 | 124 | 	struct list_head subsys_list; | 
 | 125 |  | 
 | 126 | 	/* The root cgroup for this hierarchy */ | 
 | 127 | 	struct cgroup top_cgroup; | 
 | 128 |  | 
 | 129 | 	/* Tracks how many cgroups are currently defined in hierarchy.*/ | 
 | 130 | 	int number_of_cgroups; | 
 | 131 |  | 
| Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 132 | 	/* A list running through the active hierarchies */ | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 133 | 	struct list_head root_list; | 
 | 134 |  | 
| Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 135 | 	/* All cgroups on this root, cgroup_mutex protected */ | 
 | 136 | 	struct list_head allcg_list; | 
 | 137 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 138 | 	/* Hierarchy-specific flags */ | 
 | 139 | 	unsigned long flags; | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 140 |  | 
| Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 141 | 	/* IDs for cgroups in this hierarchy */ | 
 | 142 | 	struct ida cgroup_ida; | 
 | 143 |  | 
| Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 144 | 	/* The path to use for release notifications. */ | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 145 | 	char release_agent_path[PATH_MAX]; | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 146 |  | 
 | 147 | 	/* The name for this hierarchy - may be empty */ | 
 | 148 | 	char name[MAX_CGROUP_ROOT_NAMELEN]; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 149 | }; | 
 | 150 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 151 | /* | 
 | 152 |  * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the | 
 | 153 |  * subsystems that are otherwise unattached - it never has more than a | 
 | 154 |  * single cgroup, and all tasks are part of that cgroup. | 
 | 155 |  */ | 
 | 156 | static struct cgroupfs_root rootnode; | 
 | 157 |  | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 158 | /* | 
| Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 159 |  * cgroupfs file entry, pointed to from leaf dentry->d_fsdata. | 
 | 160 |  */ | 
 | 161 | struct cfent { | 
 | 162 | 	struct list_head		node; | 
 | 163 | 	struct dentry			*dentry; | 
 | 164 | 	struct cftype			*type; | 
 | 165 | }; | 
 | 166 |  | 
 | 167 | /* | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 168 |  * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when | 
 | 169 |  * cgroup_subsys->use_id != 0. | 
 | 170 |  */ | 
 | 171 | #define CSS_ID_MAX	(65535) | 
 | 172 | struct css_id { | 
 | 173 | 	/* | 
 | 174 | 	 * The css to which this ID points. This pointer is set to valid value | 
 | 175 | 	 * after cgroup is populated. If cgroup is removed, this will be NULL. | 
 | 176 | 	 * This pointer is expected to be RCU-safe because destroy() | 
| Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 177 | 	 * is called after synchronize_rcu(). But for safe use, css_tryget() | 
 | 178 | 	 * should be used for avoiding race. | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 179 | 	 */ | 
| Arnd Bergmann | 2c392b8 | 2010-02-24 19:41:39 +0100 | [diff] [blame] | 180 | 	struct cgroup_subsys_state __rcu *css; | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 181 | 	/* | 
 | 182 | 	 * ID of this css. | 
 | 183 | 	 */ | 
 | 184 | 	unsigned short id; | 
 | 185 | 	/* | 
 | 186 | 	 * Depth in hierarchy which this ID belongs to. | 
 | 187 | 	 */ | 
 | 188 | 	unsigned short depth; | 
 | 189 | 	/* | 
 | 190 | 	 * ID is freed by RCU. (and lookup routine is RCU safe.) | 
 | 191 | 	 */ | 
 | 192 | 	struct rcu_head rcu_head; | 
 | 193 | 	/* | 
 | 194 | 	 * Hierarchy of CSS ID belongs to. | 
 | 195 | 	 */ | 
 | 196 | 	unsigned short stack[0]; /* Array of Length (depth+1) */ | 
 | 197 | }; | 
 | 198 |  | 
| Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 199 | /* | 
| Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 200 |  * cgroup_event represents events which userspace want to receive. | 
| Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 201 |  */ | 
 | 202 | struct cgroup_event { | 
 | 203 | 	/* | 
 | 204 | 	 * Cgroup which the event belongs to. | 
 | 205 | 	 */ | 
 | 206 | 	struct cgroup *cgrp; | 
 | 207 | 	/* | 
 | 208 | 	 * Control file which the event associated. | 
 | 209 | 	 */ | 
 | 210 | 	struct cftype *cft; | 
 | 211 | 	/* | 
 | 212 | 	 * eventfd to signal userspace about the event. | 
 | 213 | 	 */ | 
 | 214 | 	struct eventfd_ctx *eventfd; | 
 | 215 | 	/* | 
 | 216 | 	 * Each of these stored in a list by the cgroup. | 
 | 217 | 	 */ | 
 | 218 | 	struct list_head list; | 
 | 219 | 	/* | 
 | 220 | 	 * All fields below needed to unregister event when | 
 | 221 | 	 * userspace closes eventfd. | 
 | 222 | 	 */ | 
 | 223 | 	poll_table pt; | 
 | 224 | 	wait_queue_head_t *wqh; | 
 | 225 | 	wait_queue_t wait; | 
 | 226 | 	struct work_struct remove; | 
 | 227 | }; | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 228 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 229 | /* The list of hierarchy roots */ | 
 | 230 |  | 
 | 231 | static LIST_HEAD(roots); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 232 | static int root_count; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 233 |  | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 234 | static DEFINE_IDA(hierarchy_ida); | 
 | 235 | static int next_hierarchy_id; | 
 | 236 | static DEFINE_SPINLOCK(hierarchy_id_lock); | 
 | 237 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 238 | /* dummytop is a shorthand for the dummy hierarchy's top cgroup */ | 
 | 239 | #define dummytop (&rootnode.top_cgroup) | 
 | 240 |  | 
 | 241 | /* This flag indicates whether tasks in the fork and exit paths should | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 242 |  * check for fork/exit handlers to call. This avoids us having to do | 
 | 243 |  * extra work in the fork/exit path if none of the subsystems need to | 
 | 244 |  * be called. | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 245 |  */ | 
| Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 246 | static int need_forkexit_callback __read_mostly; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 247 |  | 
| Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 248 | static int cgroup_destroy_locked(struct cgroup *cgrp); | 
| Gao feng | 879a3d9 | 2012-12-01 00:21:28 +0800 | [diff] [blame] | 249 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys, | 
 | 250 | 			      struct cftype cfts[], bool is_add); | 
| Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 251 |  | 
| Paul E. McKenney | d11c563 | 2010-02-22 17:04:50 -0800 | [diff] [blame] | 252 | #ifdef CONFIG_PROVE_LOCKING | 
 | 253 | int cgroup_lock_is_held(void) | 
 | 254 | { | 
 | 255 | 	return lockdep_is_held(&cgroup_mutex); | 
 | 256 | } | 
 | 257 | #else /* #ifdef CONFIG_PROVE_LOCKING */ | 
 | 258 | int cgroup_lock_is_held(void) | 
 | 259 | { | 
 | 260 | 	return mutex_is_locked(&cgroup_mutex); | 
 | 261 | } | 
 | 262 | #endif /* #else #ifdef CONFIG_PROVE_LOCKING */ | 
 | 263 |  | 
 | 264 | EXPORT_SYMBOL_GPL(cgroup_lock_is_held); | 
 | 265 |  | 
| Salman Qazi | 8e3bbf4 | 2012-06-14 14:55:30 -0700 | [diff] [blame] | 266 | static int css_unbias_refcnt(int refcnt) | 
 | 267 | { | 
 | 268 | 	return refcnt >= 0 ? refcnt : refcnt - CSS_DEACT_BIAS; | 
 | 269 | } | 
 | 270 |  | 
| Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 271 | /* the current nr of refs, always >= 0 whether @css is deactivated or not */ | 
 | 272 | static int css_refcnt(struct cgroup_subsys_state *css) | 
 | 273 | { | 
 | 274 | 	int v = atomic_read(&css->refcnt); | 
 | 275 |  | 
| Salman Qazi | 8e3bbf4 | 2012-06-14 14:55:30 -0700 | [diff] [blame] | 276 | 	return css_unbias_refcnt(v); | 
| Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 277 | } | 
 | 278 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 279 | /* convenient tests for these bits */ | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 280 | inline int cgroup_is_removed(const struct cgroup *cgrp) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 281 | { | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 282 | 	return test_bit(CGRP_REMOVED, &cgrp->flags); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 283 | } | 
 | 284 |  | 
 | 285 | /* bits in struct cgroupfs_root flags field */ | 
 | 286 | enum { | 
| Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 287 | 	ROOT_NOPREFIX,	/* mounted subsystems have no named prefix */ | 
 | 288 | 	ROOT_XATTR,	/* supports extended attributes */ | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 289 | }; | 
 | 290 |  | 
| Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 291 | static int cgroup_is_releasable(const struct cgroup *cgrp) | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 292 | { | 
 | 293 | 	const int bits = | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 294 | 		(1 << CGRP_RELEASABLE) | | 
 | 295 | 		(1 << CGRP_NOTIFY_ON_RELEASE); | 
 | 296 | 	return (cgrp->flags & bits) == bits; | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 297 | } | 
 | 298 |  | 
| Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 299 | static int notify_on_release(const struct cgroup *cgrp) | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 300 | { | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 301 | 	return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 302 | } | 
 | 303 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 304 | /* | 
 | 305 |  * for_each_subsys() allows you to iterate on each subsystem attached to | 
 | 306 |  * an active hierarchy | 
 | 307 |  */ | 
 | 308 | #define for_each_subsys(_root, _ss) \ | 
 | 309 | list_for_each_entry(_ss, &_root->subsys_list, sibling) | 
 | 310 |  | 
| Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 311 | /* for_each_active_root() allows you to iterate across the active hierarchies */ | 
 | 312 | #define for_each_active_root(_root) \ | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 313 | list_for_each_entry(_root, &roots, root_list) | 
 | 314 |  | 
| Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 315 | static inline struct cgroup *__d_cgrp(struct dentry *dentry) | 
 | 316 | { | 
 | 317 | 	return dentry->d_fsdata; | 
 | 318 | } | 
 | 319 |  | 
| Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 320 | static inline struct cfent *__d_cfe(struct dentry *dentry) | 
| Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 321 | { | 
 | 322 | 	return dentry->d_fsdata; | 
 | 323 | } | 
 | 324 |  | 
| Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 325 | static inline struct cftype *__d_cft(struct dentry *dentry) | 
 | 326 | { | 
 | 327 | 	return __d_cfe(dentry)->type; | 
 | 328 | } | 
 | 329 |  | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 330 | /* the list of cgroups eligible for automatic release. Protected by | 
 | 331 |  * release_list_lock */ | 
 | 332 | static LIST_HEAD(release_list); | 
| Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 333 | static DEFINE_RAW_SPINLOCK(release_list_lock); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 334 | static void cgroup_release_agent(struct work_struct *work); | 
 | 335 | static DECLARE_WORK(release_agent_work, cgroup_release_agent); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 336 | static void check_for_release(struct cgroup *cgrp); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 337 |  | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 338 | /* Link structure for associating css_set objects with cgroups */ | 
 | 339 | struct cg_cgroup_link { | 
 | 340 | 	/* | 
 | 341 | 	 * List running through cg_cgroup_links associated with a | 
 | 342 | 	 * cgroup, anchored on cgroup->css_sets | 
 | 343 | 	 */ | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 344 | 	struct list_head cgrp_link_list; | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 345 | 	struct cgroup *cgrp; | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 346 | 	/* | 
 | 347 | 	 * List running through cg_cgroup_links pointing at a | 
 | 348 | 	 * single css_set object, anchored on css_set->cg_links | 
 | 349 | 	 */ | 
 | 350 | 	struct list_head cg_link_list; | 
 | 351 | 	struct css_set *cg; | 
 | 352 | }; | 
 | 353 |  | 
 | 354 | /* The default css_set - used by init and its children prior to any | 
 | 355 |  * hierarchies being mounted. It contains a pointer to the root state | 
 | 356 |  * for each subsystem. Also used to anchor the list of css_sets. Not | 
 | 357 |  * reference-counted, to improve performance when child cgroups | 
 | 358 |  * haven't been created. | 
 | 359 |  */ | 
 | 360 |  | 
 | 361 | static struct css_set init_css_set; | 
 | 362 | static struct cg_cgroup_link init_css_set_link; | 
 | 363 |  | 
| Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 364 | static int cgroup_init_idr(struct cgroup_subsys *ss, | 
 | 365 | 			   struct cgroup_subsys_state *css); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 366 |  | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 367 | /* css_set_lock protects the list of css_set objects, and the | 
 | 368 |  * chain of tasks off each css_set.  Nests outside task->alloc_lock | 
 | 369 |  * due to cgroup_iter_start() */ | 
 | 370 | static DEFINE_RWLOCK(css_set_lock); | 
 | 371 | static int css_set_count; | 
 | 372 |  | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 373 | /* | 
 | 374 |  * hash table for cgroup groups. This improves the performance to find | 
 | 375 |  * an existing css_set. This hash doesn't (currently) take into | 
 | 376 |  * account cgroups in empty hierarchies. | 
 | 377 |  */ | 
| Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 378 | #define CSS_SET_HASH_BITS	7 | 
 | 379 | #define CSS_SET_TABLE_SIZE	(1 << CSS_SET_HASH_BITS) | 
 | 380 | static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE]; | 
 | 381 |  | 
 | 382 | static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[]) | 
 | 383 | { | 
 | 384 | 	int i; | 
 | 385 | 	int index; | 
 | 386 | 	unsigned long tmp = 0UL; | 
 | 387 |  | 
 | 388 | 	for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) | 
 | 389 | 		tmp += (unsigned long)css[i]; | 
 | 390 | 	tmp = (tmp >> 16) ^ tmp; | 
 | 391 |  | 
 | 392 | 	index = hash_long(tmp, CSS_SET_HASH_BITS); | 
 | 393 |  | 
 | 394 | 	return &css_set_table[index]; | 
 | 395 | } | 
 | 396 |  | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 397 | /* We don't maintain the lists running through each css_set to its | 
 | 398 |  * task until after the first call to cgroup_iter_start(). This | 
 | 399 |  * reduces the fork()/exit() overhead for people who have cgroups | 
 | 400 |  * compiled into their kernel but not actually in use */ | 
| Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 401 | static int use_task_css_set_links __read_mostly; | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 402 |  | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 403 | static void __put_css_set(struct css_set *cg, int taskexit) | 
| Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 404 | { | 
| KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 405 | 	struct cg_cgroup_link *link; | 
 | 406 | 	struct cg_cgroup_link *saved_link; | 
| Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 407 | 	/* | 
 | 408 | 	 * Ensure that the refcount doesn't hit zero while any readers | 
 | 409 | 	 * can see it. Similar to atomic_dec_and_lock(), but for an | 
 | 410 | 	 * rwlock | 
 | 411 | 	 */ | 
 | 412 | 	if (atomic_add_unless(&cg->refcount, -1, 1)) | 
 | 413 | 		return; | 
 | 414 | 	write_lock(&css_set_lock); | 
 | 415 | 	if (!atomic_dec_and_test(&cg->refcount)) { | 
 | 416 | 		write_unlock(&css_set_lock); | 
 | 417 | 		return; | 
 | 418 | 	} | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 419 |  | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 420 | 	/* This css_set is dead. unlink it and release cgroup refcounts */ | 
 | 421 | 	hlist_del(&cg->hlist); | 
 | 422 | 	css_set_count--; | 
 | 423 |  | 
 | 424 | 	list_for_each_entry_safe(link, saved_link, &cg->cg_links, | 
 | 425 | 				 cg_link_list) { | 
 | 426 | 		struct cgroup *cgrp = link->cgrp; | 
 | 427 | 		list_del(&link->cg_link_list); | 
 | 428 | 		list_del(&link->cgrp_link_list); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 429 | 		if (atomic_dec_and_test(&cgrp->count) && | 
 | 430 | 		    notify_on_release(cgrp)) { | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 431 | 			if (taskexit) | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 432 | 				set_bit(CGRP_RELEASABLE, &cgrp->flags); | 
 | 433 | 			check_for_release(cgrp); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 434 | 		} | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 435 |  | 
 | 436 | 		kfree(link); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 437 | 	} | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 438 |  | 
 | 439 | 	write_unlock(&css_set_lock); | 
| Lai Jiangshan | 30088ad | 2011-03-15 17:53:46 +0800 | [diff] [blame] | 440 | 	kfree_rcu(cg, rcu_head); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 441 | } | 
 | 442 |  | 
 | 443 | /* | 
 | 444 |  * refcounted get/put for css_set objects | 
 | 445 |  */ | 
 | 446 | static inline void get_css_set(struct css_set *cg) | 
 | 447 | { | 
| Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 448 | 	atomic_inc(&cg->refcount); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 449 | } | 
 | 450 |  | 
 | 451 | static inline void put_css_set(struct css_set *cg) | 
 | 452 | { | 
| Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 453 | 	__put_css_set(cg, 0); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 454 | } | 
 | 455 |  | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 456 | static inline void put_css_set_taskexit(struct css_set *cg) | 
 | 457 | { | 
| Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 458 | 	__put_css_set(cg, 1); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 459 | } | 
 | 460 |  | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 461 | /* | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 462 |  * compare_css_sets - helper function for find_existing_css_set(). | 
 | 463 |  * @cg: candidate css_set being tested | 
 | 464 |  * @old_cg: existing css_set for a task | 
 | 465 |  * @new_cgrp: cgroup that's being entered by the task | 
 | 466 |  * @template: desired set of css pointers in css_set (pre-calculated) | 
 | 467 |  * | 
 | 468 |  * Returns true if "cg" matches "old_cg" except for the hierarchy | 
 | 469 |  * which "new_cgrp" belongs to, for which it should match "new_cgrp". | 
 | 470 |  */ | 
 | 471 | static bool compare_css_sets(struct css_set *cg, | 
 | 472 | 			     struct css_set *old_cg, | 
 | 473 | 			     struct cgroup *new_cgrp, | 
 | 474 | 			     struct cgroup_subsys_state *template[]) | 
 | 475 | { | 
 | 476 | 	struct list_head *l1, *l2; | 
 | 477 |  | 
 | 478 | 	if (memcmp(template, cg->subsys, sizeof(cg->subsys))) { | 
 | 479 | 		/* Not all subsystems matched */ | 
 | 480 | 		return false; | 
 | 481 | 	} | 
 | 482 |  | 
 | 483 | 	/* | 
 | 484 | 	 * Compare cgroup pointers in order to distinguish between | 
 | 485 | 	 * different cgroups in heirarchies with no subsystems. We | 
 | 486 | 	 * could get by with just this check alone (and skip the | 
 | 487 | 	 * memcmp above) but on most setups the memcmp check will | 
 | 488 | 	 * avoid the need for this more expensive check on almost all | 
 | 489 | 	 * candidates. | 
 | 490 | 	 */ | 
 | 491 |  | 
 | 492 | 	l1 = &cg->cg_links; | 
 | 493 | 	l2 = &old_cg->cg_links; | 
 | 494 | 	while (1) { | 
 | 495 | 		struct cg_cgroup_link *cgl1, *cgl2; | 
 | 496 | 		struct cgroup *cg1, *cg2; | 
 | 497 |  | 
 | 498 | 		l1 = l1->next; | 
 | 499 | 		l2 = l2->next; | 
 | 500 | 		/* See if we reached the end - both lists are equal length. */ | 
 | 501 | 		if (l1 == &cg->cg_links) { | 
 | 502 | 			BUG_ON(l2 != &old_cg->cg_links); | 
 | 503 | 			break; | 
 | 504 | 		} else { | 
 | 505 | 			BUG_ON(l2 == &old_cg->cg_links); | 
 | 506 | 		} | 
 | 507 | 		/* Locate the cgroups associated with these links. */ | 
 | 508 | 		cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list); | 
 | 509 | 		cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list); | 
 | 510 | 		cg1 = cgl1->cgrp; | 
 | 511 | 		cg2 = cgl2->cgrp; | 
 | 512 | 		/* Hierarchies should be linked in the same order. */ | 
 | 513 | 		BUG_ON(cg1->root != cg2->root); | 
 | 514 |  | 
 | 515 | 		/* | 
 | 516 | 		 * If this hierarchy is the hierarchy of the cgroup | 
 | 517 | 		 * that's changing, then we need to check that this | 
 | 518 | 		 * css_set points to the new cgroup; if it's any other | 
 | 519 | 		 * hierarchy, then this css_set should point to the | 
 | 520 | 		 * same cgroup as the old css_set. | 
 | 521 | 		 */ | 
 | 522 | 		if (cg1->root == new_cgrp->root) { | 
 | 523 | 			if (cg1 != new_cgrp) | 
 | 524 | 				return false; | 
 | 525 | 		} else { | 
 | 526 | 			if (cg1 != cg2) | 
 | 527 | 				return false; | 
 | 528 | 		} | 
 | 529 | 	} | 
 | 530 | 	return true; | 
 | 531 | } | 
 | 532 |  | 
 | 533 | /* | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 534 |  * find_existing_css_set() is a helper for | 
 | 535 |  * find_css_set(), and checks to see whether an existing | 
| Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 536 |  * css_set is suitable. | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 537 |  * | 
 | 538 |  * oldcg: the cgroup group that we're using before the cgroup | 
 | 539 |  * transition | 
 | 540 |  * | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 541 |  * cgrp: the cgroup that we're moving into | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 542 |  * | 
 | 543 |  * template: location in which to build the desired set of subsystem | 
 | 544 |  * state objects for the new cgroup group | 
 | 545 |  */ | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 546 | static struct css_set *find_existing_css_set( | 
 | 547 | 	struct css_set *oldcg, | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 548 | 	struct cgroup *cgrp, | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 549 | 	struct cgroup_subsys_state *template[]) | 
 | 550 | { | 
 | 551 | 	int i; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 552 | 	struct cgroupfs_root *root = cgrp->root; | 
| Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 553 | 	struct hlist_head *hhead; | 
 | 554 | 	struct hlist_node *node; | 
 | 555 | 	struct css_set *cg; | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 556 |  | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 557 | 	/* | 
 | 558 | 	 * Build the set of subsystem state objects that we want to see in the | 
 | 559 | 	 * new css_set. while subsystems can change globally, the entries here | 
 | 560 | 	 * won't change, so no need for locking. | 
 | 561 | 	 */ | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 562 | 	for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 563 | 		if (root->subsys_mask & (1UL << i)) { | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 564 | 			/* Subsystem is in this hierarchy. So we want | 
 | 565 | 			 * the subsystem state from the new | 
 | 566 | 			 * cgroup */ | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 567 | 			template[i] = cgrp->subsys[i]; | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 568 | 		} else { | 
 | 569 | 			/* Subsystem is not in this hierarchy, so we | 
 | 570 | 			 * don't want to change the subsystem state */ | 
 | 571 | 			template[i] = oldcg->subsys[i]; | 
 | 572 | 		} | 
 | 573 | 	} | 
 | 574 |  | 
| Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 575 | 	hhead = css_set_hash(template); | 
 | 576 | 	hlist_for_each_entry(cg, node, hhead, hlist) { | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 577 | 		if (!compare_css_sets(cg, oldcg, cgrp, template)) | 
 | 578 | 			continue; | 
 | 579 |  | 
 | 580 | 		/* This css_set matches what we need */ | 
 | 581 | 		return cg; | 
| Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 582 | 	} | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 583 |  | 
 | 584 | 	/* No existing cgroup group matched */ | 
 | 585 | 	return NULL; | 
 | 586 | } | 
 | 587 |  | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 588 | static void free_cg_links(struct list_head *tmp) | 
 | 589 | { | 
| KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 590 | 	struct cg_cgroup_link *link; | 
 | 591 | 	struct cg_cgroup_link *saved_link; | 
 | 592 |  | 
 | 593 | 	list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) { | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 594 | 		list_del(&link->cgrp_link_list); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 595 | 		kfree(link); | 
 | 596 | 	} | 
 | 597 | } | 
 | 598 |  | 
 | 599 | /* | 
| Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 600 |  * allocate_cg_links() allocates "count" cg_cgroup_link structures | 
 | 601 |  * and chains them on tmp through their cgrp_link_list fields. Returns 0 on | 
 | 602 |  * success or a negative error | 
 | 603 |  */ | 
 | 604 | static int allocate_cg_links(int count, struct list_head *tmp) | 
 | 605 | { | 
 | 606 | 	struct cg_cgroup_link *link; | 
 | 607 | 	int i; | 
 | 608 | 	INIT_LIST_HEAD(tmp); | 
 | 609 | 	for (i = 0; i < count; i++) { | 
 | 610 | 		link = kmalloc(sizeof(*link), GFP_KERNEL); | 
 | 611 | 		if (!link) { | 
 | 612 | 			free_cg_links(tmp); | 
 | 613 | 			return -ENOMEM; | 
 | 614 | 		} | 
 | 615 | 		list_add(&link->cgrp_link_list, tmp); | 
 | 616 | 	} | 
 | 617 | 	return 0; | 
 | 618 | } | 
 | 619 |  | 
| Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 620 | /** | 
 | 621 |  * link_css_set - a helper function to link a css_set to a cgroup | 
 | 622 |  * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links() | 
 | 623 |  * @cg: the css_set to be linked | 
 | 624 |  * @cgrp: the destination cgroup | 
 | 625 |  */ | 
 | 626 | static void link_css_set(struct list_head *tmp_cg_links, | 
 | 627 | 			 struct css_set *cg, struct cgroup *cgrp) | 
 | 628 | { | 
 | 629 | 	struct cg_cgroup_link *link; | 
 | 630 |  | 
 | 631 | 	BUG_ON(list_empty(tmp_cg_links)); | 
 | 632 | 	link = list_first_entry(tmp_cg_links, struct cg_cgroup_link, | 
 | 633 | 				cgrp_link_list); | 
 | 634 | 	link->cg = cg; | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 635 | 	link->cgrp = cgrp; | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 636 | 	atomic_inc(&cgrp->count); | 
| Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 637 | 	list_move(&link->cgrp_link_list, &cgrp->css_sets); | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 638 | 	/* | 
 | 639 | 	 * Always add links to the tail of the list so that the list | 
 | 640 | 	 * is sorted by order of hierarchy creation | 
 | 641 | 	 */ | 
 | 642 | 	list_add_tail(&link->cg_link_list, &cg->cg_links); | 
| Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 643 | } | 
 | 644 |  | 
| Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 645 | /* | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 646 |  * find_css_set() takes an existing cgroup group and a | 
 | 647 |  * cgroup object, and returns a css_set object that's | 
 | 648 |  * equivalent to the old group, but with the given cgroup | 
 | 649 |  * substituted into the appropriate hierarchy. Must be called with | 
 | 650 |  * cgroup_mutex held | 
 | 651 |  */ | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 652 | static struct css_set *find_css_set( | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 653 | 	struct css_set *oldcg, struct cgroup *cgrp) | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 654 | { | 
 | 655 | 	struct css_set *res; | 
 | 656 | 	struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT]; | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 657 |  | 
 | 658 | 	struct list_head tmp_cg_links; | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 659 |  | 
| Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 660 | 	struct hlist_head *hhead; | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 661 | 	struct cg_cgroup_link *link; | 
| Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 662 |  | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 663 | 	/* First see if we already have a cgroup group that matches | 
 | 664 | 	 * the desired set */ | 
| Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 665 | 	read_lock(&css_set_lock); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 666 | 	res = find_existing_css_set(oldcg, cgrp, template); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 667 | 	if (res) | 
 | 668 | 		get_css_set(res); | 
| Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 669 | 	read_unlock(&css_set_lock); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 670 |  | 
 | 671 | 	if (res) | 
 | 672 | 		return res; | 
 | 673 |  | 
 | 674 | 	res = kmalloc(sizeof(*res), GFP_KERNEL); | 
 | 675 | 	if (!res) | 
 | 676 | 		return NULL; | 
 | 677 |  | 
 | 678 | 	/* Allocate all the cg_cgroup_link objects that we'll need */ | 
 | 679 | 	if (allocate_cg_links(root_count, &tmp_cg_links) < 0) { | 
 | 680 | 		kfree(res); | 
 | 681 | 		return NULL; | 
 | 682 | 	} | 
 | 683 |  | 
| Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 684 | 	atomic_set(&res->refcount, 1); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 685 | 	INIT_LIST_HEAD(&res->cg_links); | 
 | 686 | 	INIT_LIST_HEAD(&res->tasks); | 
| Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 687 | 	INIT_HLIST_NODE(&res->hlist); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 688 |  | 
 | 689 | 	/* Copy the set of subsystem state objects generated in | 
 | 690 | 	 * find_existing_css_set() */ | 
 | 691 | 	memcpy(res->subsys, template, sizeof(res->subsys)); | 
 | 692 |  | 
 | 693 | 	write_lock(&css_set_lock); | 
 | 694 | 	/* Add reference counts and links from the new css_set. */ | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 695 | 	list_for_each_entry(link, &oldcg->cg_links, cg_link_list) { | 
 | 696 | 		struct cgroup *c = link->cgrp; | 
 | 697 | 		if (c->root == cgrp->root) | 
 | 698 | 			c = cgrp; | 
 | 699 | 		link_css_set(&tmp_cg_links, res, c); | 
 | 700 | 	} | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 701 |  | 
 | 702 | 	BUG_ON(!list_empty(&tmp_cg_links)); | 
 | 703 |  | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 704 | 	css_set_count++; | 
| Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 705 |  | 
 | 706 | 	/* Add this cgroup group to the hash table */ | 
 | 707 | 	hhead = css_set_hash(res->subsys); | 
 | 708 | 	hlist_add_head(&res->hlist, hhead); | 
 | 709 |  | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 710 | 	write_unlock(&css_set_lock); | 
 | 711 |  | 
 | 712 | 	return res; | 
| Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 713 | } | 
 | 714 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 715 | /* | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 716 |  * Return the cgroup for "task" from the given hierarchy. Must be | 
 | 717 |  * called with cgroup_mutex held. | 
 | 718 |  */ | 
 | 719 | static struct cgroup *task_cgroup_from_root(struct task_struct *task, | 
 | 720 | 					    struct cgroupfs_root *root) | 
 | 721 | { | 
 | 722 | 	struct css_set *css; | 
 | 723 | 	struct cgroup *res = NULL; | 
 | 724 |  | 
 | 725 | 	BUG_ON(!mutex_is_locked(&cgroup_mutex)); | 
 | 726 | 	read_lock(&css_set_lock); | 
 | 727 | 	/* | 
 | 728 | 	 * No need to lock the task - since we hold cgroup_mutex the | 
 | 729 | 	 * task can't change groups, so the only thing that can happen | 
 | 730 | 	 * is that it exits and its css is set back to init_css_set. | 
 | 731 | 	 */ | 
 | 732 | 	css = task->cgroups; | 
 | 733 | 	if (css == &init_css_set) { | 
 | 734 | 		res = &root->top_cgroup; | 
 | 735 | 	} else { | 
 | 736 | 		struct cg_cgroup_link *link; | 
 | 737 | 		list_for_each_entry(link, &css->cg_links, cg_link_list) { | 
 | 738 | 			struct cgroup *c = link->cgrp; | 
 | 739 | 			if (c->root == root) { | 
 | 740 | 				res = c; | 
 | 741 | 				break; | 
 | 742 | 			} | 
 | 743 | 		} | 
 | 744 | 	} | 
 | 745 | 	read_unlock(&css_set_lock); | 
 | 746 | 	BUG_ON(!res); | 
 | 747 | 	return res; | 
 | 748 | } | 
 | 749 |  | 
 | 750 | /* | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 751 |  * There is one global cgroup mutex. We also require taking | 
 | 752 |  * task_lock() when dereferencing a task's cgroup subsys pointers. | 
 | 753 |  * See "The task_lock() exception", at the end of this comment. | 
 | 754 |  * | 
 | 755 |  * A task must hold cgroup_mutex to modify cgroups. | 
 | 756 |  * | 
 | 757 |  * Any task can increment and decrement the count field without lock. | 
 | 758 |  * So in general, code holding cgroup_mutex can't rely on the count | 
 | 759 |  * field not changing.  However, if the count goes to zero, then only | 
| Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 760 |  * cgroup_attach_task() can increment it again.  Because a count of zero | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 761 |  * means that no tasks are currently attached, therefore there is no | 
 | 762 |  * way a task attached to that cgroup can fork (the other way to | 
 | 763 |  * increment the count).  So code holding cgroup_mutex can safely | 
 | 764 |  * assume that if the count is zero, it will stay zero. Similarly, if | 
 | 765 |  * a task holds cgroup_mutex on a cgroup with zero count, it | 
 | 766 |  * knows that the cgroup won't be removed, as cgroup_rmdir() | 
 | 767 |  * needs that mutex. | 
 | 768 |  * | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 769 |  * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't | 
 | 770 |  * (usually) take cgroup_mutex.  These are the two most performance | 
 | 771 |  * critical pieces of code here.  The exception occurs on cgroup_exit(), | 
 | 772 |  * when a task in a notify_on_release cgroup exits.  Then cgroup_mutex | 
 | 773 |  * 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] | 774 |  * to the release agent with the name of the cgroup (path relative to | 
 | 775 |  * the root of cgroup file system) as the argument. | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 776 |  * | 
 | 777 |  * A cgroup can only be deleted if both its 'count' of using tasks | 
 | 778 |  * is zero, and its list of 'children' cgroups is empty.  Since all | 
 | 779 |  * tasks in the system use _some_ cgroup, and since there is always at | 
 | 780 |  * least one task in the system (init, pid == 1), therefore, top_cgroup | 
 | 781 |  * always has either children cgroups and/or using tasks.  So we don't | 
 | 782 |  * need a special hack to ensure that top_cgroup cannot be deleted. | 
 | 783 |  * | 
 | 784 |  *	The task_lock() exception | 
 | 785 |  * | 
 | 786 |  * The need for this exception arises from the action of | 
| Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 787 |  * cgroup_attach_task(), which overwrites one task's cgroup pointer with | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 788 |  * another.  It does so using cgroup_mutex, however there are | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 789 |  * several performance critical places that need to reference | 
 | 790 |  * task->cgroup without the expense of grabbing a system global | 
 | 791 |  * mutex.  Therefore except as noted below, when dereferencing or, as | 
| Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 792 |  * in cgroup_attach_task(), modifying a task's cgroup pointer we use | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 793 |  * task_lock(), which acts on a spinlock (task->alloc_lock) already in | 
 | 794 |  * the task_struct routinely used for such matters. | 
 | 795 |  * | 
 | 796 |  * P.S.  One more locking exception.  RCU is used to guard the | 
| Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 797 |  * update of a tasks cgroup pointer by cgroup_attach_task() | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 798 |  */ | 
 | 799 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 800 | /** | 
 | 801 |  * cgroup_lock - lock out any changes to cgroup structures | 
 | 802 |  * | 
 | 803 |  */ | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 804 | void cgroup_lock(void) | 
 | 805 | { | 
 | 806 | 	mutex_lock(&cgroup_mutex); | 
 | 807 | } | 
| Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 808 | EXPORT_SYMBOL_GPL(cgroup_lock); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 809 |  | 
 | 810 | /** | 
 | 811 |  * cgroup_unlock - release lock on cgroup changes | 
 | 812 |  * | 
 | 813 |  * Undo the lock taken in a previous cgroup_lock() call. | 
 | 814 |  */ | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 815 | void cgroup_unlock(void) | 
 | 816 | { | 
 | 817 | 	mutex_unlock(&cgroup_mutex); | 
 | 818 | } | 
| Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 819 | EXPORT_SYMBOL_GPL(cgroup_unlock); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 820 |  | 
 | 821 | /* | 
 | 822 |  * A couple of forward declarations required, due to cyclic reference loop: | 
 | 823 |  * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir -> | 
 | 824 |  * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations | 
 | 825 |  * -> cgroup_mkdir. | 
 | 826 |  */ | 
 | 827 |  | 
| Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 828 | static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode); | 
| Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 829 | static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 830 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry); | 
| Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 831 | static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files, | 
 | 832 | 			       unsigned long subsys_mask); | 
| Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 833 | static const struct inode_operations cgroup_dir_inode_operations; | 
| Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 834 | static const struct file_operations proc_cgroupstats_operations; | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 835 |  | 
 | 836 | static struct backing_dev_info cgroup_backing_dev_info = { | 
| Jens Axboe | d993831 | 2009-06-12 14:45:52 +0200 | [diff] [blame] | 837 | 	.name		= "cgroup", | 
| Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 838 | 	.capabilities	= BDI_CAP_NO_ACCT_AND_WRITEBACK, | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 839 | }; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 840 |  | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 841 | static int alloc_css_id(struct cgroup_subsys *ss, | 
 | 842 | 			struct cgroup *parent, struct cgroup *child); | 
 | 843 |  | 
| Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 844 | 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] | 845 | { | 
 | 846 | 	struct inode *inode = new_inode(sb); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 847 |  | 
 | 848 | 	if (inode) { | 
| Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 849 | 		inode->i_ino = get_next_ino(); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 850 | 		inode->i_mode = mode; | 
| David Howells | 76aac0e | 2008-11-14 10:39:12 +1100 | [diff] [blame] | 851 | 		inode->i_uid = current_fsuid(); | 
 | 852 | 		inode->i_gid = current_fsgid(); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 853 | 		inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; | 
 | 854 | 		inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info; | 
 | 855 | 	} | 
 | 856 | 	return inode; | 
 | 857 | } | 
 | 858 |  | 
 | 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) | 
| Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 879 | 			ss->css_free(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 | 		/* | 
| Tejun Heo | 7db5b3c | 2012-07-07 15:55:47 -0700 | [diff] [blame] | 885 | 		 * Drop the active superblock reference that we took when we | 
 | 886 | 		 * created the cgroup | 
| Paul Menage | a47295e | 2009-01-07 18:07:44 -0800 | [diff] [blame] | 887 | 		 */ | 
| Tejun Heo | 7db5b3c | 2012-07-07 15:55:47 -0700 | [diff] [blame] | 888 | 		deactivate_super(cgrp->root->sb); | 
| Paul Menage | 8dc4f3e | 2008-02-07 00:13:45 -0800 | [diff] [blame] | 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 |  | 
| Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 896 | 		simple_xattrs_free(&cgrp->xattrs); | 
 | 897 |  | 
| Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 898 | 		ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id); | 
| Lai Jiangshan | f2da1c4 | 2011-03-15 17:55:16 +0800 | [diff] [blame] | 899 | 		kfree_rcu(cgrp, rcu_head); | 
| Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 900 | 	} else { | 
 | 901 | 		struct cfent *cfe = __d_cfe(dentry); | 
 | 902 | 		struct cgroup *cgrp = dentry->d_parent->d_fsdata; | 
| Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 903 | 		struct cftype *cft = cfe->type; | 
| Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 904 |  | 
 | 905 | 		WARN_ONCE(!list_empty(&cfe->node) && | 
 | 906 | 			  cgrp != &cgrp->root->top_cgroup, | 
 | 907 | 			  "cfe still linked for %s\n", cfe->type->name); | 
 | 908 | 		kfree(cfe); | 
| Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 909 | 		simple_xattrs_free(&cft->xattrs); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 910 | 	} | 
 | 911 | 	iput(inode); | 
 | 912 | } | 
 | 913 |  | 
| Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 914 | static int cgroup_delete(const struct dentry *d) | 
 | 915 | { | 
 | 916 | 	return 1; | 
 | 917 | } | 
 | 918 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 919 | static void remove_dir(struct dentry *d) | 
 | 920 | { | 
 | 921 | 	struct dentry *parent = dget(d->d_parent); | 
 | 922 |  | 
 | 923 | 	d_delete(d); | 
 | 924 | 	simple_rmdir(parent->d_inode, d); | 
 | 925 | 	dput(parent); | 
 | 926 | } | 
 | 927 |  | 
| Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 928 | static int cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 929 | { | 
| Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 930 | 	struct cfent *cfe; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 931 |  | 
| Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 932 | 	lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); | 
 | 933 | 	lockdep_assert_held(&cgroup_mutex); | 
| Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 934 |  | 
| Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 935 | 	list_for_each_entry(cfe, &cgrp->files, node) { | 
 | 936 | 		struct dentry *d = cfe->dentry; | 
 | 937 |  | 
 | 938 | 		if (cft && cfe->type != cft) | 
 | 939 | 			continue; | 
 | 940 |  | 
 | 941 | 		dget(d); | 
 | 942 | 		d_delete(d); | 
| Tejun Heo | ce27e31 | 2012-07-03 10:38:06 -0700 | [diff] [blame] | 943 | 		simple_unlink(cgrp->dentry->d_inode, d); | 
| Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 944 | 		list_del_init(&cfe->node); | 
 | 945 | 		dput(d); | 
 | 946 |  | 
 | 947 | 		return 0; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 948 | 	} | 
| Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 949 | 	return -ENOENT; | 
 | 950 | } | 
 | 951 |  | 
| Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 952 | /** | 
 | 953 |  * cgroup_clear_directory - selective removal of base and subsystem files | 
 | 954 |  * @dir: directory containing the files | 
 | 955 |  * @base_files: true if the base files should be removed | 
 | 956 |  * @subsys_mask: mask of the subsystem ids whose files should be removed | 
 | 957 |  */ | 
 | 958 | static void cgroup_clear_directory(struct dentry *dir, bool base_files, | 
 | 959 | 				   unsigned long subsys_mask) | 
| Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 960 | { | 
 | 961 | 	struct cgroup *cgrp = __d_cgrp(dir); | 
| Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 962 | 	struct cgroup_subsys *ss; | 
| Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 963 |  | 
| Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 964 | 	for_each_subsys(cgrp->root, ss) { | 
 | 965 | 		struct cftype_set *set; | 
 | 966 | 		if (!test_bit(ss->subsys_id, &subsys_mask)) | 
 | 967 | 			continue; | 
 | 968 | 		list_for_each_entry(set, &ss->cftsets, node) | 
| Gao feng | 879a3d9 | 2012-12-01 00:21:28 +0800 | [diff] [blame] | 969 | 			cgroup_addrm_files(cgrp, NULL, set->cfts, false); | 
| Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 970 | 	} | 
 | 971 | 	if (base_files) { | 
 | 972 | 		while (!list_empty(&cgrp->files)) | 
 | 973 | 			cgroup_rm_file(cgrp, NULL); | 
 | 974 | 	} | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 975 | } | 
 | 976 |  | 
 | 977 | /* | 
 | 978 |  * NOTE : the dentry must have been dget()'ed | 
 | 979 |  */ | 
 | 980 | static void cgroup_d_remove_dir(struct dentry *dentry) | 
 | 981 | { | 
| Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 982 | 	struct dentry *parent; | 
| Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 983 | 	struct cgroupfs_root *root = dentry->d_sb->s_fs_info; | 
| Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 984 |  | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 985 | 	cgroup_clear_directory(dentry, true, root->subsys_mask); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 986 |  | 
| Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 987 | 	parent = dentry->d_parent; | 
 | 988 | 	spin_lock(&parent->d_lock); | 
| Li Zefan | 3ec762a | 2011-01-14 11:34:34 +0800 | [diff] [blame] | 989 | 	spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 990 | 	list_del_init(&dentry->d_u.d_child); | 
| Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 991 | 	spin_unlock(&dentry->d_lock); | 
 | 992 | 	spin_unlock(&parent->d_lock); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 993 | 	remove_dir(dentry); | 
 | 994 | } | 
 | 995 |  | 
| KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 996 | /* | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 997 |  * Call with cgroup_mutex held. Drops reference counts on modules, including | 
 | 998 |  * any duplicate ones that parse_cgroupfs_options took. If this function | 
 | 999 |  * returns an error, no reference counts are touched. | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1000 |  */ | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1001 | static int rebind_subsystems(struct cgroupfs_root *root, | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1002 | 			      unsigned long final_subsys_mask) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1003 | { | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1004 | 	unsigned long added_mask, removed_mask; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1005 | 	struct cgroup *cgrp = &root->top_cgroup; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1006 | 	int i; | 
 | 1007 |  | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1008 | 	BUG_ON(!mutex_is_locked(&cgroup_mutex)); | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1009 | 	BUG_ON(!mutex_is_locked(&cgroup_root_mutex)); | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1010 |  | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1011 | 	removed_mask = root->actual_subsys_mask & ~final_subsys_mask; | 
 | 1012 | 	added_mask = final_subsys_mask & ~root->actual_subsys_mask; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1013 | 	/* Check that any added subsystems are currently free */ | 
 | 1014 | 	for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { | 
| Li Zefan | 8d53d55 | 2008-02-23 15:24:11 -0800 | [diff] [blame] | 1015 | 		unsigned long bit = 1UL << i; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1016 | 		struct cgroup_subsys *ss = subsys[i]; | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1017 | 		if (!(bit & added_mask)) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1018 | 			continue; | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1019 | 		/* | 
 | 1020 | 		 * Nobody should tell us to do a subsys that doesn't exist: | 
 | 1021 | 		 * parse_cgroupfs_options should catch that case and refcounts | 
 | 1022 | 		 * ensure that subsystems won't disappear once selected. | 
 | 1023 | 		 */ | 
 | 1024 | 		BUG_ON(ss == NULL); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1025 | 		if (ss->root != &rootnode) { | 
 | 1026 | 			/* Subsystem isn't free */ | 
 | 1027 | 			return -EBUSY; | 
 | 1028 | 		} | 
 | 1029 | 	} | 
 | 1030 |  | 
 | 1031 | 	/* Currently we don't handle adding/removing subsystems when | 
 | 1032 | 	 * any child cgroups exist. This is theoretically supportable | 
 | 1033 | 	 * but involves complex error handling, so it's being left until | 
 | 1034 | 	 * later */ | 
| Paul Menage | 307257c | 2008-12-15 13:54:22 -0800 | [diff] [blame] | 1035 | 	if (root->number_of_cgroups > 1) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1036 | 		return -EBUSY; | 
 | 1037 |  | 
 | 1038 | 	/* Process each subsystem */ | 
 | 1039 | 	for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { | 
 | 1040 | 		struct cgroup_subsys *ss = subsys[i]; | 
 | 1041 | 		unsigned long bit = 1UL << i; | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1042 | 		if (bit & added_mask) { | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1043 | 			/* We're binding this subsystem to this hierarchy */ | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1044 | 			BUG_ON(ss == NULL); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1045 | 			BUG_ON(cgrp->subsys[i]); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1046 | 			BUG_ON(!dummytop->subsys[i]); | 
 | 1047 | 			BUG_ON(dummytop->subsys[i]->cgroup != dummytop); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1048 | 			cgrp->subsys[i] = dummytop->subsys[i]; | 
 | 1049 | 			cgrp->subsys[i]->cgroup = cgrp; | 
| Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1050 | 			list_move(&ss->sibling, &root->subsys_list); | 
| Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 1051 | 			ss->root = root; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1052 | 			if (ss->bind) | 
| Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1053 | 				ss->bind(cgrp); | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1054 | 			/* refcount was already taken, and we're keeping it */ | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1055 | 		} else if (bit & removed_mask) { | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1056 | 			/* We're removing this subsystem */ | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1057 | 			BUG_ON(ss == NULL); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1058 | 			BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]); | 
 | 1059 | 			BUG_ON(cgrp->subsys[i]->cgroup != cgrp); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1060 | 			if (ss->bind) | 
| Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1061 | 				ss->bind(dummytop); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1062 | 			dummytop->subsys[i]->cgroup = dummytop; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1063 | 			cgrp->subsys[i] = NULL; | 
| Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 1064 | 			subsys[i]->root = &rootnode; | 
| Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1065 | 			list_move(&ss->sibling, &rootnode.subsys_list); | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1066 | 			/* subsystem is now free - drop reference on module */ | 
 | 1067 | 			module_put(ss->module); | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1068 | 		} else if (bit & final_subsys_mask) { | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1069 | 			/* Subsystem state should already exist */ | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1070 | 			BUG_ON(ss == NULL); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1071 | 			BUG_ON(!cgrp->subsys[i]); | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1072 | 			/* | 
 | 1073 | 			 * a refcount was taken, but we already had one, so | 
 | 1074 | 			 * drop the extra reference. | 
 | 1075 | 			 */ | 
 | 1076 | 			module_put(ss->module); | 
 | 1077 | #ifdef CONFIG_MODULE_UNLOAD | 
 | 1078 | 			BUG_ON(ss->module && !module_refcount(ss->module)); | 
 | 1079 | #endif | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1080 | 		} else { | 
 | 1081 | 			/* Subsystem state shouldn't exist */ | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1082 | 			BUG_ON(cgrp->subsys[i]); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1083 | 		} | 
 | 1084 | 	} | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1085 | 	root->subsys_mask = root->actual_subsys_mask = final_subsys_mask; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1086 | 	synchronize_rcu(); | 
 | 1087 |  | 
 | 1088 | 	return 0; | 
 | 1089 | } | 
 | 1090 |  | 
| Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1091 | static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1092 | { | 
| Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1093 | 	struct cgroupfs_root *root = dentry->d_sb->s_fs_info; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1094 | 	struct cgroup_subsys *ss; | 
 | 1095 |  | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1096 | 	mutex_lock(&cgroup_root_mutex); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1097 | 	for_each_subsys(root, ss) | 
 | 1098 | 		seq_printf(seq, ",%s", ss->name); | 
 | 1099 | 	if (test_bit(ROOT_NOPREFIX, &root->flags)) | 
 | 1100 | 		seq_puts(seq, ",noprefix"); | 
| Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1101 | 	if (test_bit(ROOT_XATTR, &root->flags)) | 
 | 1102 | 		seq_puts(seq, ",xattr"); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1103 | 	if (strlen(root->release_agent_path)) | 
 | 1104 | 		seq_printf(seq, ",release_agent=%s", root->release_agent_path); | 
| Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1105 | 	if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags)) | 
| Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 1106 | 		seq_puts(seq, ",clone_children"); | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1107 | 	if (strlen(root->name)) | 
 | 1108 | 		seq_printf(seq, ",name=%s", root->name); | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1109 | 	mutex_unlock(&cgroup_root_mutex); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1110 | 	return 0; | 
 | 1111 | } | 
 | 1112 |  | 
 | 1113 | struct cgroup_sb_opts { | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1114 | 	unsigned long subsys_mask; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1115 | 	unsigned long flags; | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1116 | 	char *release_agent; | 
| Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1117 | 	bool cpuset_clone_children; | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1118 | 	char *name; | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1119 | 	/* User explicitly requested empty subsystem */ | 
 | 1120 | 	bool none; | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1121 |  | 
 | 1122 | 	struct cgroupfs_root *new_root; | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1123 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1124 | }; | 
 | 1125 |  | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1126 | /* | 
 | 1127 |  * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1128 |  * with cgroup_mutex held to protect the subsys[] array. This function takes | 
 | 1129 |  * refcounts on subsystems to be used, unless it returns error, in which case | 
 | 1130 |  * no refcounts are taken. | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1131 |  */ | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1132 | static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1133 | { | 
| Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1134 | 	char *token, *o = data; | 
 | 1135 | 	bool all_ss = false, one_ss = false; | 
| Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1136 | 	unsigned long mask = (unsigned long)-1; | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1137 | 	int i; | 
 | 1138 | 	bool module_pin_failed = false; | 
| Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1139 |  | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1140 | 	BUG_ON(!mutex_is_locked(&cgroup_mutex)); | 
 | 1141 |  | 
| Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1142 | #ifdef CONFIG_CPUSETS | 
 | 1143 | 	mask = ~(1UL << cpuset_subsys_id); | 
 | 1144 | #endif | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1145 |  | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1146 | 	memset(opts, 0, sizeof(*opts)); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1147 |  | 
 | 1148 | 	while ((token = strsep(&o, ",")) != NULL) { | 
 | 1149 | 		if (!*token) | 
 | 1150 | 			return -EINVAL; | 
| Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1151 | 		if (!strcmp(token, "none")) { | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1152 | 			/* Explicitly have no subsystems */ | 
 | 1153 | 			opts->none = true; | 
| Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1154 | 			continue; | 
 | 1155 | 		} | 
 | 1156 | 		if (!strcmp(token, "all")) { | 
 | 1157 | 			/* Mutually exclusive option 'all' + subsystem name */ | 
 | 1158 | 			if (one_ss) | 
 | 1159 | 				return -EINVAL; | 
 | 1160 | 			all_ss = true; | 
 | 1161 | 			continue; | 
 | 1162 | 		} | 
 | 1163 | 		if (!strcmp(token, "noprefix")) { | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1164 | 			set_bit(ROOT_NOPREFIX, &opts->flags); | 
| Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1165 | 			continue; | 
 | 1166 | 		} | 
 | 1167 | 		if (!strcmp(token, "clone_children")) { | 
| Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1168 | 			opts->cpuset_clone_children = true; | 
| Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1169 | 			continue; | 
 | 1170 | 		} | 
| Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1171 | 		if (!strcmp(token, "xattr")) { | 
 | 1172 | 			set_bit(ROOT_XATTR, &opts->flags); | 
 | 1173 | 			continue; | 
 | 1174 | 		} | 
| Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1175 | 		if (!strncmp(token, "release_agent=", 14)) { | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1176 | 			/* Specifying two release agents is forbidden */ | 
 | 1177 | 			if (opts->release_agent) | 
 | 1178 | 				return -EINVAL; | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1179 | 			opts->release_agent = | 
| Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1180 | 				kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1181 | 			if (!opts->release_agent) | 
 | 1182 | 				return -ENOMEM; | 
| Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1183 | 			continue; | 
 | 1184 | 		} | 
 | 1185 | 		if (!strncmp(token, "name=", 5)) { | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1186 | 			const char *name = token + 5; | 
 | 1187 | 			/* Can't specify an empty name */ | 
 | 1188 | 			if (!strlen(name)) | 
 | 1189 | 				return -EINVAL; | 
 | 1190 | 			/* Must match [\w.-]+ */ | 
 | 1191 | 			for (i = 0; i < strlen(name); i++) { | 
 | 1192 | 				char c = name[i]; | 
 | 1193 | 				if (isalnum(c)) | 
 | 1194 | 					continue; | 
 | 1195 | 				if ((c == '.') || (c == '-') || (c == '_')) | 
 | 1196 | 					continue; | 
 | 1197 | 				return -EINVAL; | 
 | 1198 | 			} | 
 | 1199 | 			/* Specifying two names is forbidden */ | 
 | 1200 | 			if (opts->name) | 
 | 1201 | 				return -EINVAL; | 
 | 1202 | 			opts->name = kstrndup(name, | 
| Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1203 | 					      MAX_CGROUP_ROOT_NAMELEN - 1, | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1204 | 					      GFP_KERNEL); | 
 | 1205 | 			if (!opts->name) | 
 | 1206 | 				return -ENOMEM; | 
| Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1207 |  | 
 | 1208 | 			continue; | 
 | 1209 | 		} | 
 | 1210 |  | 
 | 1211 | 		for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { | 
 | 1212 | 			struct cgroup_subsys *ss = subsys[i]; | 
 | 1213 | 			if (ss == NULL) | 
 | 1214 | 				continue; | 
 | 1215 | 			if (strcmp(token, ss->name)) | 
 | 1216 | 				continue; | 
 | 1217 | 			if (ss->disabled) | 
 | 1218 | 				continue; | 
 | 1219 |  | 
 | 1220 | 			/* Mutually exclusive option 'all' + subsystem name */ | 
 | 1221 | 			if (all_ss) | 
 | 1222 | 				return -EINVAL; | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1223 | 			set_bit(i, &opts->subsys_mask); | 
| Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1224 | 			one_ss = true; | 
 | 1225 |  | 
 | 1226 | 			break; | 
 | 1227 | 		} | 
 | 1228 | 		if (i == CGROUP_SUBSYS_COUNT) | 
 | 1229 | 			return -ENOENT; | 
 | 1230 | 	} | 
 | 1231 |  | 
 | 1232 | 	/* | 
 | 1233 | 	 * If the 'all' option was specified select all the subsystems, | 
| Li Zefan | 0d19ea8 | 2011-12-27 14:25:55 +0800 | [diff] [blame] | 1234 | 	 * otherwise if 'none', 'name=' and a subsystem name options | 
 | 1235 | 	 * were not specified, let's default to 'all' | 
| Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1236 | 	 */ | 
| Li Zefan | 0d19ea8 | 2011-12-27 14:25:55 +0800 | [diff] [blame] | 1237 | 	if (all_ss || (!one_ss && !opts->none && !opts->name)) { | 
| Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1238 | 		for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { | 
 | 1239 | 			struct cgroup_subsys *ss = subsys[i]; | 
 | 1240 | 			if (ss == NULL) | 
 | 1241 | 				continue; | 
 | 1242 | 			if (ss->disabled) | 
 | 1243 | 				continue; | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1244 | 			set_bit(i, &opts->subsys_mask); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1245 | 		} | 
 | 1246 | 	} | 
 | 1247 |  | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1248 | 	/* Consistency checks */ | 
 | 1249 |  | 
| Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1250 | 	/* | 
 | 1251 | 	 * Option noprefix was introduced just for backward compatibility | 
 | 1252 | 	 * with the old cpuset, so we allow noprefix only if mounting just | 
 | 1253 | 	 * the cpuset subsystem. | 
 | 1254 | 	 */ | 
 | 1255 | 	if (test_bit(ROOT_NOPREFIX, &opts->flags) && | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1256 | 	    (opts->subsys_mask & mask)) | 
| Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1257 | 		return -EINVAL; | 
 | 1258 |  | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1259 |  | 
 | 1260 | 	/* Can't specify "none" and some subsystems */ | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1261 | 	if (opts->subsys_mask && opts->none) | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1262 | 		return -EINVAL; | 
 | 1263 |  | 
 | 1264 | 	/* | 
 | 1265 | 	 * We either have to specify by name or by subsystems. (So all | 
 | 1266 | 	 * empty hierarchies must have a name). | 
 | 1267 | 	 */ | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1268 | 	if (!opts->subsys_mask && !opts->name) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1269 | 		return -EINVAL; | 
 | 1270 |  | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1271 | 	/* | 
 | 1272 | 	 * Grab references on all the modules we'll need, so the subsystems | 
 | 1273 | 	 * don't dance around before rebind_subsystems attaches them. This may | 
 | 1274 | 	 * take duplicate reference counts on a subsystem that's already used, | 
 | 1275 | 	 * but rebind_subsystems handles this case. | 
 | 1276 | 	 */ | 
| Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 1277 | 	for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1278 | 		unsigned long bit = 1UL << i; | 
 | 1279 |  | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1280 | 		if (!(bit & opts->subsys_mask)) | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1281 | 			continue; | 
 | 1282 | 		if (!try_module_get(subsys[i]->module)) { | 
 | 1283 | 			module_pin_failed = true; | 
 | 1284 | 			break; | 
 | 1285 | 		} | 
 | 1286 | 	} | 
 | 1287 | 	if (module_pin_failed) { | 
 | 1288 | 		/* | 
 | 1289 | 		 * oops, one of the modules was going away. this means that we | 
 | 1290 | 		 * raced with a module_delete call, and to the user this is | 
 | 1291 | 		 * essentially a "subsystem doesn't exist" case. | 
 | 1292 | 		 */ | 
| Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 1293 | 		for (i--; i >= 0; i--) { | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1294 | 			/* drop refcounts only on the ones we took */ | 
 | 1295 | 			unsigned long bit = 1UL << i; | 
 | 1296 |  | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1297 | 			if (!(bit & opts->subsys_mask)) | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1298 | 				continue; | 
 | 1299 | 			module_put(subsys[i]->module); | 
 | 1300 | 		} | 
 | 1301 | 		return -ENOENT; | 
 | 1302 | 	} | 
 | 1303 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1304 | 	return 0; | 
 | 1305 | } | 
 | 1306 |  | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1307 | static void drop_parsed_module_refcounts(unsigned long subsys_mask) | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1308 | { | 
 | 1309 | 	int i; | 
| Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 1310 | 	for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1311 | 		unsigned long bit = 1UL << i; | 
 | 1312 |  | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1313 | 		if (!(bit & subsys_mask)) | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1314 | 			continue; | 
 | 1315 | 		module_put(subsys[i]->module); | 
 | 1316 | 	} | 
 | 1317 | } | 
 | 1318 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1319 | static int cgroup_remount(struct super_block *sb, int *flags, char *data) | 
 | 1320 | { | 
 | 1321 | 	int ret = 0; | 
 | 1322 | 	struct cgroupfs_root *root = sb->s_fs_info; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1323 | 	struct cgroup *cgrp = &root->top_cgroup; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1324 | 	struct cgroup_sb_opts opts; | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1325 | 	unsigned long added_mask, removed_mask; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1326 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1327 | 	mutex_lock(&cgrp->dentry->d_inode->i_mutex); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1328 | 	mutex_lock(&cgroup_mutex); | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1329 | 	mutex_lock(&cgroup_root_mutex); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1330 |  | 
 | 1331 | 	/* See what subsystems are wanted */ | 
 | 1332 | 	ret = parse_cgroupfs_options(data, &opts); | 
 | 1333 | 	if (ret) | 
 | 1334 | 		goto out_unlock; | 
 | 1335 |  | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1336 | 	if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent) | 
| Tejun Heo | 8b5a5a9 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1337 | 		pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n", | 
 | 1338 | 			   task_tgid_nr(current), current->comm); | 
 | 1339 |  | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1340 | 	added_mask = opts.subsys_mask & ~root->subsys_mask; | 
 | 1341 | 	removed_mask = root->subsys_mask & ~opts.subsys_mask; | 
| Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1342 |  | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1343 | 	/* Don't allow flags or name to change at remount */ | 
 | 1344 | 	if (opts.flags != root->flags || | 
 | 1345 | 	    (opts.name && strcmp(opts.name, root->name))) { | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1346 | 		ret = -EINVAL; | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1347 | 		drop_parsed_module_refcounts(opts.subsys_mask); | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1348 | 		goto out_unlock; | 
 | 1349 | 	} | 
 | 1350 |  | 
| Gao feng | 7083d03 | 2012-12-03 09:28:18 +0800 | [diff] [blame] | 1351 | 	/* | 
 | 1352 | 	 * Clear out the files of subsystems that should be removed, do | 
 | 1353 | 	 * this before rebind_subsystems, since rebind_subsystems may | 
 | 1354 | 	 * change this hierarchy's subsys_list. | 
 | 1355 | 	 */ | 
 | 1356 | 	cgroup_clear_directory(cgrp->dentry, false, removed_mask); | 
 | 1357 |  | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1358 | 	ret = rebind_subsystems(root, opts.subsys_mask); | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1359 | 	if (ret) { | 
| Gao feng | 7083d03 | 2012-12-03 09:28:18 +0800 | [diff] [blame] | 1360 | 		/* rebind_subsystems failed, re-populate the removed files */ | 
 | 1361 | 		cgroup_populate_dir(cgrp, false, removed_mask); | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1362 | 		drop_parsed_module_refcounts(opts.subsys_mask); | 
| Li Zefan | 0670e08 | 2009-04-02 16:57:30 -0700 | [diff] [blame] | 1363 | 		goto out_unlock; | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1364 | 	} | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1365 |  | 
| Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1366 | 	/* re-populate subsystem files */ | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1367 | 	cgroup_populate_dir(cgrp, false, added_mask); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1368 |  | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1369 | 	if (opts.release_agent) | 
 | 1370 | 		strcpy(root->release_agent_path, opts.release_agent); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1371 |  out_unlock: | 
| Jesper Juhl | 66bdc9c | 2009-04-02 16:57:27 -0700 | [diff] [blame] | 1372 | 	kfree(opts.release_agent); | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1373 | 	kfree(opts.name); | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1374 | 	mutex_unlock(&cgroup_root_mutex); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1375 | 	mutex_unlock(&cgroup_mutex); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1376 | 	mutex_unlock(&cgrp->dentry->d_inode->i_mutex); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1377 | 	return ret; | 
 | 1378 | } | 
 | 1379 |  | 
| Alexey Dobriyan | b87221d | 2009-09-21 17:01:09 -0700 | [diff] [blame] | 1380 | static const struct super_operations cgroup_ops = { | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1381 | 	.statfs = simple_statfs, | 
 | 1382 | 	.drop_inode = generic_delete_inode, | 
 | 1383 | 	.show_options = cgroup_show_options, | 
 | 1384 | 	.remount_fs = cgroup_remount, | 
 | 1385 | }; | 
 | 1386 |  | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1387 | static void init_cgroup_housekeeping(struct cgroup *cgrp) | 
 | 1388 | { | 
 | 1389 | 	INIT_LIST_HEAD(&cgrp->sibling); | 
 | 1390 | 	INIT_LIST_HEAD(&cgrp->children); | 
| Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1391 | 	INIT_LIST_HEAD(&cgrp->files); | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1392 | 	INIT_LIST_HEAD(&cgrp->css_sets); | 
| Tejun Heo | 2243076 | 2012-11-19 08:13:35 -0800 | [diff] [blame] | 1393 | 	INIT_LIST_HEAD(&cgrp->allcg_node); | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1394 | 	INIT_LIST_HEAD(&cgrp->release_list); | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1395 | 	INIT_LIST_HEAD(&cgrp->pidlists); | 
 | 1396 | 	mutex_init(&cgrp->pidlist_mutex); | 
| Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 1397 | 	INIT_LIST_HEAD(&cgrp->event_list); | 
 | 1398 | 	spin_lock_init(&cgrp->event_list_lock); | 
| Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1399 | 	simple_xattrs_init(&cgrp->xattrs); | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1400 | } | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1401 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1402 | static void init_cgroup_root(struct cgroupfs_root *root) | 
 | 1403 | { | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1404 | 	struct cgroup *cgrp = &root->top_cgroup; | 
| Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1405 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1406 | 	INIT_LIST_HEAD(&root->subsys_list); | 
 | 1407 | 	INIT_LIST_HEAD(&root->root_list); | 
| Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1408 | 	INIT_LIST_HEAD(&root->allcg_list); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1409 | 	root->number_of_cgroups = 1; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1410 | 	cgrp->root = root; | 
 | 1411 | 	cgrp->top_cgroup = cgrp; | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1412 | 	init_cgroup_housekeeping(cgrp); | 
| Li Zhong | fddfb02 | 2012-11-28 17:15:21 +0800 | [diff] [blame] | 1413 | 	list_add_tail(&cgrp->allcg_node, &root->allcg_list); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1414 | } | 
 | 1415 |  | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1416 | static bool init_root_id(struct cgroupfs_root *root) | 
 | 1417 | { | 
 | 1418 | 	int ret = 0; | 
 | 1419 |  | 
 | 1420 | 	do { | 
 | 1421 | 		if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL)) | 
 | 1422 | 			return false; | 
 | 1423 | 		spin_lock(&hierarchy_id_lock); | 
 | 1424 | 		/* Try to allocate the next unused ID */ | 
 | 1425 | 		ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id, | 
 | 1426 | 					&root->hierarchy_id); | 
 | 1427 | 		if (ret == -ENOSPC) | 
 | 1428 | 			/* Try again starting from 0 */ | 
 | 1429 | 			ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id); | 
 | 1430 | 		if (!ret) { | 
 | 1431 | 			next_hierarchy_id = root->hierarchy_id + 1; | 
 | 1432 | 		} else if (ret != -EAGAIN) { | 
 | 1433 | 			/* Can only get here if the 31-bit IDR is full ... */ | 
 | 1434 | 			BUG_ON(ret); | 
 | 1435 | 		} | 
 | 1436 | 		spin_unlock(&hierarchy_id_lock); | 
 | 1437 | 	} while (ret); | 
 | 1438 | 	return true; | 
 | 1439 | } | 
 | 1440 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1441 | static int cgroup_test_super(struct super_block *sb, void *data) | 
 | 1442 | { | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1443 | 	struct cgroup_sb_opts *opts = data; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1444 | 	struct cgroupfs_root *root = sb->s_fs_info; | 
 | 1445 |  | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1446 | 	/* If we asked for a name then it must match */ | 
 | 1447 | 	if (opts->name && strcmp(opts->name, root->name)) | 
 | 1448 | 		return 0; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1449 |  | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1450 | 	/* | 
 | 1451 | 	 * If we asked for subsystems (or explicitly for no | 
 | 1452 | 	 * subsystems) then they must match | 
 | 1453 | 	 */ | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1454 | 	if ((opts->subsys_mask || opts->none) | 
 | 1455 | 	    && (opts->subsys_mask != root->subsys_mask)) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1456 | 		return 0; | 
 | 1457 |  | 
 | 1458 | 	return 1; | 
 | 1459 | } | 
 | 1460 |  | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1461 | static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts) | 
 | 1462 | { | 
 | 1463 | 	struct cgroupfs_root *root; | 
 | 1464 |  | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1465 | 	if (!opts->subsys_mask && !opts->none) | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1466 | 		return NULL; | 
 | 1467 |  | 
 | 1468 | 	root = kzalloc(sizeof(*root), GFP_KERNEL); | 
 | 1469 | 	if (!root) | 
 | 1470 | 		return ERR_PTR(-ENOMEM); | 
 | 1471 |  | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1472 | 	if (!init_root_id(root)) { | 
 | 1473 | 		kfree(root); | 
 | 1474 | 		return ERR_PTR(-ENOMEM); | 
 | 1475 | 	} | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1476 | 	init_cgroup_root(root); | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1477 |  | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1478 | 	root->subsys_mask = opts->subsys_mask; | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1479 | 	root->flags = opts->flags; | 
| Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 1480 | 	ida_init(&root->cgroup_ida); | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1481 | 	if (opts->release_agent) | 
 | 1482 | 		strcpy(root->release_agent_path, opts->release_agent); | 
 | 1483 | 	if (opts->name) | 
 | 1484 | 		strcpy(root->name, opts->name); | 
| Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1485 | 	if (opts->cpuset_clone_children) | 
 | 1486 | 		set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags); | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1487 | 	return root; | 
 | 1488 | } | 
 | 1489 |  | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1490 | static void cgroup_drop_root(struct cgroupfs_root *root) | 
 | 1491 | { | 
 | 1492 | 	if (!root) | 
 | 1493 | 		return; | 
 | 1494 |  | 
 | 1495 | 	BUG_ON(!root->hierarchy_id); | 
 | 1496 | 	spin_lock(&hierarchy_id_lock); | 
 | 1497 | 	ida_remove(&hierarchy_ida, root->hierarchy_id); | 
 | 1498 | 	spin_unlock(&hierarchy_id_lock); | 
| Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 1499 | 	ida_destroy(&root->cgroup_ida); | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1500 | 	kfree(root); | 
 | 1501 | } | 
 | 1502 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1503 | static int cgroup_set_super(struct super_block *sb, void *data) | 
 | 1504 | { | 
 | 1505 | 	int ret; | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1506 | 	struct cgroup_sb_opts *opts = data; | 
 | 1507 |  | 
 | 1508 | 	/* If we don't have a new root, we can't set up a new sb */ | 
 | 1509 | 	if (!opts->new_root) | 
 | 1510 | 		return -EINVAL; | 
 | 1511 |  | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1512 | 	BUG_ON(!opts->subsys_mask && !opts->none); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1513 |  | 
 | 1514 | 	ret = set_anon_super(sb, NULL); | 
 | 1515 | 	if (ret) | 
 | 1516 | 		return ret; | 
 | 1517 |  | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1518 | 	sb->s_fs_info = opts->new_root; | 
 | 1519 | 	opts->new_root->sb = sb; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1520 |  | 
 | 1521 | 	sb->s_blocksize = PAGE_CACHE_SIZE; | 
 | 1522 | 	sb->s_blocksize_bits = PAGE_CACHE_SHIFT; | 
 | 1523 | 	sb->s_magic = CGROUP_SUPER_MAGIC; | 
 | 1524 | 	sb->s_op = &cgroup_ops; | 
 | 1525 |  | 
 | 1526 | 	return 0; | 
 | 1527 | } | 
 | 1528 |  | 
 | 1529 | static int cgroup_get_rootdir(struct super_block *sb) | 
 | 1530 | { | 
| Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1531 | 	static const struct dentry_operations cgroup_dops = { | 
 | 1532 | 		.d_iput = cgroup_diput, | 
| Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 1533 | 		.d_delete = cgroup_delete, | 
| Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1534 | 	}; | 
 | 1535 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1536 | 	struct inode *inode = | 
 | 1537 | 		cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1538 |  | 
 | 1539 | 	if (!inode) | 
 | 1540 | 		return -ENOMEM; | 
 | 1541 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1542 | 	inode->i_fop = &simple_dir_operations; | 
 | 1543 | 	inode->i_op = &cgroup_dir_inode_operations; | 
 | 1544 | 	/* directories start off with i_nlink == 2 (for "." entry) */ | 
 | 1545 | 	inc_nlink(inode); | 
| Al Viro | 48fde70 | 2012-01-08 22:15:13 -0500 | [diff] [blame] | 1546 | 	sb->s_root = d_make_root(inode); | 
 | 1547 | 	if (!sb->s_root) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1548 | 		return -ENOMEM; | 
| Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1549 | 	/* for everything else we want ->d_op set */ | 
 | 1550 | 	sb->s_d_op = &cgroup_dops; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1551 | 	return 0; | 
 | 1552 | } | 
 | 1553 |  | 
| Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1554 | static struct dentry *cgroup_mount(struct file_system_type *fs_type, | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1555 | 			 int flags, const char *unused_dev_name, | 
| Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1556 | 			 void *data) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1557 | { | 
 | 1558 | 	struct cgroup_sb_opts opts; | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1559 | 	struct cgroupfs_root *root; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1560 | 	int ret = 0; | 
 | 1561 | 	struct super_block *sb; | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1562 | 	struct cgroupfs_root *new_root; | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1563 | 	struct inode *inode; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1564 |  | 
 | 1565 | 	/* First find the desired set of subsystems */ | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1566 | 	mutex_lock(&cgroup_mutex); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1567 | 	ret = parse_cgroupfs_options(data, &opts); | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1568 | 	mutex_unlock(&cgroup_mutex); | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1569 | 	if (ret) | 
 | 1570 | 		goto out_err; | 
 | 1571 |  | 
 | 1572 | 	/* | 
 | 1573 | 	 * Allocate a new cgroup root. We may not need it if we're | 
 | 1574 | 	 * reusing an existing hierarchy. | 
 | 1575 | 	 */ | 
 | 1576 | 	new_root = cgroup_root_from_opts(&opts); | 
 | 1577 | 	if (IS_ERR(new_root)) { | 
 | 1578 | 		ret = PTR_ERR(new_root); | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1579 | 		goto drop_modules; | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1580 | 	} | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1581 | 	opts.new_root = new_root; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1582 |  | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1583 | 	/* Locate an existing or new sb for this hierarchy */ | 
| David Howells | 9249e17 | 2012-06-25 12:55:37 +0100 | [diff] [blame] | 1584 | 	sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1585 | 	if (IS_ERR(sb)) { | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1586 | 		ret = PTR_ERR(sb); | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1587 | 		cgroup_drop_root(opts.new_root); | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1588 | 		goto drop_modules; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1589 | 	} | 
 | 1590 |  | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1591 | 	root = sb->s_fs_info; | 
 | 1592 | 	BUG_ON(!root); | 
 | 1593 | 	if (root == opts.new_root) { | 
 | 1594 | 		/* We used the new root structure, so this is a new hierarchy */ | 
 | 1595 | 		struct list_head tmp_cg_links; | 
| Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1596 | 		struct cgroup *root_cgrp = &root->top_cgroup; | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1597 | 		struct cgroupfs_root *existing_root; | 
| eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1598 | 		const struct cred *cred; | 
| Li Zefan | 28fd5df | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 1599 | 		int i; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1600 |  | 
 | 1601 | 		BUG_ON(sb->s_root != NULL); | 
 | 1602 |  | 
 | 1603 | 		ret = cgroup_get_rootdir(sb); | 
 | 1604 | 		if (ret) | 
 | 1605 | 			goto drop_new_super; | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1606 | 		inode = sb->s_root->d_inode; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1607 |  | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1608 | 		mutex_lock(&inode->i_mutex); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1609 | 		mutex_lock(&cgroup_mutex); | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1610 | 		mutex_lock(&cgroup_root_mutex); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1611 |  | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1612 | 		/* Check for name clashes with existing mounts */ | 
 | 1613 | 		ret = -EBUSY; | 
 | 1614 | 		if (strlen(root->name)) | 
 | 1615 | 			for_each_active_root(existing_root) | 
 | 1616 | 				if (!strcmp(existing_root->name, root->name)) | 
 | 1617 | 					goto unlock_drop; | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1618 |  | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1619 | 		/* | 
 | 1620 | 		 * We're accessing css_set_count without locking | 
 | 1621 | 		 * css_set_lock here, but that's OK - it can only be | 
 | 1622 | 		 * increased by someone holding cgroup_lock, and | 
 | 1623 | 		 * that's us. The worst that can happen is that we | 
 | 1624 | 		 * have some link structures left over | 
 | 1625 | 		 */ | 
 | 1626 | 		ret = allocate_cg_links(css_set_count, &tmp_cg_links); | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1627 | 		if (ret) | 
 | 1628 | 			goto unlock_drop; | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1629 |  | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1630 | 		ret = rebind_subsystems(root, root->subsys_mask); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1631 | 		if (ret == -EBUSY) { | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1632 | 			free_cg_links(&tmp_cg_links); | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1633 | 			goto unlock_drop; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1634 | 		} | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1635 | 		/* | 
 | 1636 | 		 * There must be no failure case after here, since rebinding | 
 | 1637 | 		 * takes care of subsystems' refcounts, which are explicitly | 
 | 1638 | 		 * dropped in the failure exit path. | 
 | 1639 | 		 */ | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1640 |  | 
 | 1641 | 		/* EBUSY should be the only error here */ | 
 | 1642 | 		BUG_ON(ret); | 
 | 1643 |  | 
 | 1644 | 		list_add(&root->root_list, &roots); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1645 | 		root_count++; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1646 |  | 
| Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1647 | 		sb->s_root->d_fsdata = root_cgrp; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1648 | 		root->top_cgroup.dentry = sb->s_root; | 
 | 1649 |  | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1650 | 		/* Link the top cgroup in this hierarchy into all | 
 | 1651 | 		 * the css_set objects */ | 
 | 1652 | 		write_lock(&css_set_lock); | 
| Li Zefan | 28fd5df | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 1653 | 		for (i = 0; i < CSS_SET_TABLE_SIZE; i++) { | 
 | 1654 | 			struct hlist_head *hhead = &css_set_table[i]; | 
 | 1655 | 			struct hlist_node *node; | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1656 | 			struct css_set *cg; | 
| Li Zefan | 28fd5df | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 1657 |  | 
| Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1658 | 			hlist_for_each_entry(cg, node, hhead, hlist) | 
 | 1659 | 				link_css_set(&tmp_cg_links, cg, root_cgrp); | 
| Li Zefan | 28fd5df | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 1660 | 		} | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1661 | 		write_unlock(&css_set_lock); | 
 | 1662 |  | 
 | 1663 | 		free_cg_links(&tmp_cg_links); | 
 | 1664 |  | 
| Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1665 | 		BUG_ON(!list_empty(&root_cgrp->children)); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1666 | 		BUG_ON(root->number_of_cgroups != 1); | 
 | 1667 |  | 
| eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1668 | 		cred = override_creds(&init_cred); | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1669 | 		cgroup_populate_dir(root_cgrp, true, root->subsys_mask); | 
| eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1670 | 		revert_creds(cred); | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1671 | 		mutex_unlock(&cgroup_root_mutex); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1672 | 		mutex_unlock(&cgroup_mutex); | 
| Xiaotian Feng | 34f77a9 | 2009-09-23 15:56:18 -0700 | [diff] [blame] | 1673 | 		mutex_unlock(&inode->i_mutex); | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1674 | 	} else { | 
 | 1675 | 		/* | 
 | 1676 | 		 * We re-used an existing hierarchy - the new root (if | 
 | 1677 | 		 * any) is not needed | 
 | 1678 | 		 */ | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1679 | 		cgroup_drop_root(opts.new_root); | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1680 | 		/* no subsys rebinding, so refcounts don't change */ | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1681 | 		drop_parsed_module_refcounts(opts.subsys_mask); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1682 | 	} | 
 | 1683 |  | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1684 | 	kfree(opts.release_agent); | 
 | 1685 | 	kfree(opts.name); | 
| Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1686 | 	return dget(sb->s_root); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1687 |  | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1688 |  unlock_drop: | 
 | 1689 | 	mutex_unlock(&cgroup_root_mutex); | 
 | 1690 | 	mutex_unlock(&cgroup_mutex); | 
 | 1691 | 	mutex_unlock(&inode->i_mutex); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1692 |  drop_new_super: | 
| Al Viro | 6f5bbff | 2009-05-06 01:34:22 -0400 | [diff] [blame] | 1693 | 	deactivate_locked_super(sb); | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1694 |  drop_modules: | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1695 | 	drop_parsed_module_refcounts(opts.subsys_mask); | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1696 |  out_err: | 
 | 1697 | 	kfree(opts.release_agent); | 
 | 1698 | 	kfree(opts.name); | 
| Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1699 | 	return ERR_PTR(ret); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1700 | } | 
 | 1701 |  | 
 | 1702 | static void cgroup_kill_sb(struct super_block *sb) { | 
 | 1703 | 	struct cgroupfs_root *root = sb->s_fs_info; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1704 | 	struct cgroup *cgrp = &root->top_cgroup; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1705 | 	int ret; | 
| KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1706 | 	struct cg_cgroup_link *link; | 
 | 1707 | 	struct cg_cgroup_link *saved_link; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1708 |  | 
 | 1709 | 	BUG_ON(!root); | 
 | 1710 |  | 
 | 1711 | 	BUG_ON(root->number_of_cgroups != 1); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1712 | 	BUG_ON(!list_empty(&cgrp->children)); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1713 |  | 
 | 1714 | 	mutex_lock(&cgroup_mutex); | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1715 | 	mutex_lock(&cgroup_root_mutex); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1716 |  | 
 | 1717 | 	/* Rebind all subsystems back to the default hierarchy */ | 
 | 1718 | 	ret = rebind_subsystems(root, 0); | 
 | 1719 | 	/* Shouldn't be able to fail ... */ | 
 | 1720 | 	BUG_ON(ret); | 
 | 1721 |  | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1722 | 	/* | 
 | 1723 | 	 * Release all the links from css_sets to this hierarchy's | 
 | 1724 | 	 * root cgroup | 
 | 1725 | 	 */ | 
 | 1726 | 	write_lock(&css_set_lock); | 
| KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1727 |  | 
 | 1728 | 	list_for_each_entry_safe(link, saved_link, &cgrp->css_sets, | 
 | 1729 | 				 cgrp_link_list) { | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1730 | 		list_del(&link->cg_link_list); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1731 | 		list_del(&link->cgrp_link_list); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1732 | 		kfree(link); | 
 | 1733 | 	} | 
 | 1734 | 	write_unlock(&css_set_lock); | 
 | 1735 |  | 
| Paul Menage | 839ec54 | 2009-01-29 14:25:22 -0800 | [diff] [blame] | 1736 | 	if (!list_empty(&root->root_list)) { | 
 | 1737 | 		list_del(&root->root_list); | 
 | 1738 | 		root_count--; | 
 | 1739 | 	} | 
| Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 1740 |  | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1741 | 	mutex_unlock(&cgroup_root_mutex); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1742 | 	mutex_unlock(&cgroup_mutex); | 
 | 1743 |  | 
| Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1744 | 	simple_xattrs_free(&cgrp->xattrs); | 
 | 1745 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1746 | 	kill_litter_super(sb); | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1747 | 	cgroup_drop_root(root); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1748 | } | 
 | 1749 |  | 
 | 1750 | static struct file_system_type cgroup_fs_type = { | 
 | 1751 | 	.name = "cgroup", | 
| Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1752 | 	.mount = cgroup_mount, | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1753 | 	.kill_sb = cgroup_kill_sb, | 
 | 1754 | }; | 
 | 1755 |  | 
| Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 1756 | static struct kobject *cgroup_kobj; | 
 | 1757 |  | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1758 | /** | 
 | 1759 |  * cgroup_path - generate the path of a cgroup | 
 | 1760 |  * @cgrp: the cgroup in question | 
 | 1761 |  * @buf: the buffer to write the path into | 
 | 1762 |  * @buflen: the length of the buffer | 
 | 1763 |  * | 
| Paul Menage | a47295e | 2009-01-07 18:07:44 -0800 | [diff] [blame] | 1764 |  * Called with cgroup_mutex held or else with an RCU-protected cgroup | 
 | 1765 |  * reference.  Writes path of cgroup into buf.  Returns 0 on success, | 
 | 1766 |  * -errno on error. | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1767 |  */ | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1768 | int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1769 | { | 
| Tejun Heo | febfcef | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 1770 | 	struct dentry *dentry = cgrp->dentry; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1771 | 	char *start; | 
| Tejun Heo | febfcef | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 1772 |  | 
 | 1773 | 	rcu_lockdep_assert(rcu_read_lock_held() || cgroup_lock_is_held(), | 
 | 1774 | 			   "cgroup_path() called without proper locking"); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1775 |  | 
| Paul Menage | a47295e | 2009-01-07 18:07:44 -0800 | [diff] [blame] | 1776 | 	if (!dentry || cgrp == dummytop) { | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1777 | 		/* | 
 | 1778 | 		 * Inactive subsystems have no dentry for their root | 
 | 1779 | 		 * cgroup | 
 | 1780 | 		 */ | 
 | 1781 | 		strcpy(buf, "/"); | 
 | 1782 | 		return 0; | 
 | 1783 | 	} | 
 | 1784 |  | 
| Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1785 | 	start = buf + buflen - 1; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1786 |  | 
| Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1787 | 	*start = '\0'; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1788 | 	for (;;) { | 
| Paul Menage | a47295e | 2009-01-07 18:07:44 -0800 | [diff] [blame] | 1789 | 		int len = dentry->d_name.len; | 
| Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1790 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1791 | 		if ((start -= len) < buf) | 
 | 1792 | 			return -ENAMETOOLONG; | 
| Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1793 | 		memcpy(start, dentry->d_name.name, len); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1794 | 		cgrp = cgrp->parent; | 
 | 1795 | 		if (!cgrp) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1796 | 			break; | 
| Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1797 |  | 
| Tejun Heo | febfcef | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 1798 | 		dentry = cgrp->dentry; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1799 | 		if (!cgrp->parent) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1800 | 			continue; | 
 | 1801 | 		if (--start < buf) | 
 | 1802 | 			return -ENAMETOOLONG; | 
 | 1803 | 		*start = '/'; | 
 | 1804 | 	} | 
 | 1805 | 	memmove(buf, start, buf + buflen - start); | 
 | 1806 | 	return 0; | 
 | 1807 | } | 
| Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 1808 | EXPORT_SYMBOL_GPL(cgroup_path); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1809 |  | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1810 | /* | 
| Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1811 |  * Control Group taskset | 
 | 1812 |  */ | 
| Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1813 | struct task_and_cgroup { | 
 | 1814 | 	struct task_struct	*task; | 
 | 1815 | 	struct cgroup		*cgrp; | 
| Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1816 | 	struct css_set		*cg; | 
| Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1817 | }; | 
 | 1818 |  | 
| Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1819 | struct cgroup_taskset { | 
 | 1820 | 	struct task_and_cgroup	single; | 
 | 1821 | 	struct flex_array	*tc_array; | 
 | 1822 | 	int			tc_array_len; | 
 | 1823 | 	int			idx; | 
 | 1824 | 	struct cgroup		*cur_cgrp; | 
 | 1825 | }; | 
 | 1826 |  | 
 | 1827 | /** | 
 | 1828 |  * cgroup_taskset_first - reset taskset and return the first task | 
 | 1829 |  * @tset: taskset of interest | 
 | 1830 |  * | 
 | 1831 |  * @tset iteration is initialized and the first task is returned. | 
 | 1832 |  */ | 
 | 1833 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset) | 
 | 1834 | { | 
 | 1835 | 	if (tset->tc_array) { | 
 | 1836 | 		tset->idx = 0; | 
 | 1837 | 		return cgroup_taskset_next(tset); | 
 | 1838 | 	} else { | 
 | 1839 | 		tset->cur_cgrp = tset->single.cgrp; | 
 | 1840 | 		return tset->single.task; | 
 | 1841 | 	} | 
 | 1842 | } | 
 | 1843 | EXPORT_SYMBOL_GPL(cgroup_taskset_first); | 
 | 1844 |  | 
 | 1845 | /** | 
 | 1846 |  * cgroup_taskset_next - iterate to the next task in taskset | 
 | 1847 |  * @tset: taskset of interest | 
 | 1848 |  * | 
 | 1849 |  * Return the next task in @tset.  Iteration must have been initialized | 
 | 1850 |  * with cgroup_taskset_first(). | 
 | 1851 |  */ | 
 | 1852 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset) | 
 | 1853 | { | 
 | 1854 | 	struct task_and_cgroup *tc; | 
 | 1855 |  | 
 | 1856 | 	if (!tset->tc_array || tset->idx >= tset->tc_array_len) | 
 | 1857 | 		return NULL; | 
 | 1858 |  | 
 | 1859 | 	tc = flex_array_get(tset->tc_array, tset->idx++); | 
 | 1860 | 	tset->cur_cgrp = tc->cgrp; | 
 | 1861 | 	return tc->task; | 
 | 1862 | } | 
 | 1863 | EXPORT_SYMBOL_GPL(cgroup_taskset_next); | 
 | 1864 |  | 
 | 1865 | /** | 
 | 1866 |  * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task | 
 | 1867 |  * @tset: taskset of interest | 
 | 1868 |  * | 
 | 1869 |  * Return the cgroup for the current (last returned) task of @tset.  This | 
 | 1870 |  * function must be preceded by either cgroup_taskset_first() or | 
 | 1871 |  * cgroup_taskset_next(). | 
 | 1872 |  */ | 
 | 1873 | struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset) | 
 | 1874 | { | 
 | 1875 | 	return tset->cur_cgrp; | 
 | 1876 | } | 
 | 1877 | EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup); | 
 | 1878 |  | 
 | 1879 | /** | 
 | 1880 |  * cgroup_taskset_size - return the number of tasks in taskset | 
 | 1881 |  * @tset: taskset of interest | 
 | 1882 |  */ | 
 | 1883 | int cgroup_taskset_size(struct cgroup_taskset *tset) | 
 | 1884 | { | 
 | 1885 | 	return tset->tc_array ? tset->tc_array_len : 1; | 
 | 1886 | } | 
 | 1887 | EXPORT_SYMBOL_GPL(cgroup_taskset_size); | 
 | 1888 |  | 
 | 1889 |  | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1890 | /* | 
 | 1891 |  * cgroup_task_migrate - move a task from one cgroup to another. | 
 | 1892 |  * | 
| Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 1893 |  * Must be called with cgroup_mutex and threadgroup locked. | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1894 |  */ | 
| Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1895 | static void cgroup_task_migrate(struct cgroup *cgrp, struct cgroup *oldcgrp, | 
 | 1896 | 				struct task_struct *tsk, struct css_set *newcg) | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1897 | { | 
 | 1898 | 	struct css_set *oldcg; | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1899 |  | 
 | 1900 | 	/* | 
| Mandeep Singh Baines | 026085e | 2011-12-21 20:18:35 -0800 | [diff] [blame] | 1901 | 	 * We are synchronized through threadgroup_lock() against PF_EXITING | 
 | 1902 | 	 * setting such that we can't race against cgroup_exit() changing the | 
 | 1903 | 	 * css_set to init_css_set and dropping the old one. | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1904 | 	 */ | 
| Frederic Weisbecker | c84cdf7 | 2011-12-21 20:03:18 +0100 | [diff] [blame] | 1905 | 	WARN_ON_ONCE(tsk->flags & PF_EXITING); | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1906 | 	oldcg = tsk->cgroups; | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1907 |  | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1908 | 	task_lock(tsk); | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1909 | 	rcu_assign_pointer(tsk->cgroups, newcg); | 
 | 1910 | 	task_unlock(tsk); | 
 | 1911 |  | 
 | 1912 | 	/* Update the css_set linked lists if we're using them */ | 
 | 1913 | 	write_lock(&css_set_lock); | 
 | 1914 | 	if (!list_empty(&tsk->cg_list)) | 
 | 1915 | 		list_move(&tsk->cg_list, &newcg->tasks); | 
 | 1916 | 	write_unlock(&css_set_lock); | 
 | 1917 |  | 
 | 1918 | 	/* | 
 | 1919 | 	 * We just gained a reference on oldcg by taking it from the task. As | 
 | 1920 | 	 * trading it for newcg is protected by cgroup_mutex, we're safe to drop | 
 | 1921 | 	 * it here; it will be freed under RCU. | 
 | 1922 | 	 */ | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1923 | 	set_bit(CGRP_RELEASABLE, &oldcgrp->flags); | 
| Daisuke Nishimura | 1f5320d | 2012-10-04 16:37:16 +0900 | [diff] [blame] | 1924 | 	put_css_set(oldcg); | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1925 | } | 
 | 1926 |  | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1927 | /** | 
 | 1928 |  * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp' | 
 | 1929 |  * @cgrp: the cgroup the task is attaching to | 
 | 1930 |  * @tsk: the task to be attached | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1931 |  * | 
| Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1932 |  * Call with cgroup_mutex and threadgroup locked. May take task_lock of | 
 | 1933 |  * @tsk during call. | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1934 |  */ | 
| Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 1935 | int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk) | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1936 | { | 
| Tejun Heo | 8f12191 | 2012-03-29 22:03:33 -0700 | [diff] [blame] | 1937 | 	int retval = 0; | 
| Daisuke Nishimura | 2468c72 | 2010-03-10 15:22:03 -0800 | [diff] [blame] | 1938 | 	struct cgroup_subsys *ss, *failed_ss = NULL; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1939 | 	struct cgroup *oldcgrp; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1940 | 	struct cgroupfs_root *root = cgrp->root; | 
| Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1941 | 	struct cgroup_taskset tset = { }; | 
| Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1942 | 	struct css_set *newcg; | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1943 |  | 
| Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1944 | 	/* @tsk either already exited or can't exit until the end */ | 
 | 1945 | 	if (tsk->flags & PF_EXITING) | 
 | 1946 | 		return -ESRCH; | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1947 |  | 
 | 1948 | 	/* Nothing to do if the task is already in that cgroup */ | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1949 | 	oldcgrp = task_cgroup_from_root(tsk, root); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1950 | 	if (cgrp == oldcgrp) | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1951 | 		return 0; | 
 | 1952 |  | 
| Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1953 | 	tset.single.task = tsk; | 
 | 1954 | 	tset.single.cgrp = oldcgrp; | 
 | 1955 |  | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1956 | 	for_each_subsys(root, ss) { | 
 | 1957 | 		if (ss->can_attach) { | 
| Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1958 | 			retval = ss->can_attach(cgrp, &tset); | 
| Daisuke Nishimura | 2468c72 | 2010-03-10 15:22:03 -0800 | [diff] [blame] | 1959 | 			if (retval) { | 
 | 1960 | 				/* | 
 | 1961 | 				 * Remember on which subsystem the can_attach() | 
 | 1962 | 				 * failed, so that we only call cancel_attach() | 
 | 1963 | 				 * against the subsystems whose can_attach() | 
 | 1964 | 				 * succeeded. (See below) | 
 | 1965 | 				 */ | 
 | 1966 | 				failed_ss = ss; | 
 | 1967 | 				goto out; | 
 | 1968 | 			} | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1969 | 		} | 
 | 1970 | 	} | 
 | 1971 |  | 
| Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1972 | 	newcg = find_css_set(tsk->cgroups, cgrp); | 
 | 1973 | 	if (!newcg) { | 
 | 1974 | 		retval = -ENOMEM; | 
| Daisuke Nishimura | 2468c72 | 2010-03-10 15:22:03 -0800 | [diff] [blame] | 1975 | 		goto out; | 
| Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1976 | 	} | 
 | 1977 |  | 
 | 1978 | 	cgroup_task_migrate(cgrp, oldcgrp, tsk, newcg); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1979 |  | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1980 | 	for_each_subsys(root, ss) { | 
| Paul Jackson | e18f631 | 2008-02-07 00:13:44 -0800 | [diff] [blame] | 1981 | 		if (ss->attach) | 
| Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1982 | 			ss->attach(cgrp, &tset); | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1983 | 	} | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1984 |  | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1985 | 	synchronize_rcu(); | 
| Daisuke Nishimura | 2468c72 | 2010-03-10 15:22:03 -0800 | [diff] [blame] | 1986 | out: | 
 | 1987 | 	if (retval) { | 
 | 1988 | 		for_each_subsys(root, ss) { | 
 | 1989 | 			if (ss == failed_ss) | 
 | 1990 | 				/* | 
 | 1991 | 				 * This subsystem was the one that failed the | 
 | 1992 | 				 * can_attach() check earlier, so we don't need | 
 | 1993 | 				 * to call cancel_attach() against it or any | 
 | 1994 | 				 * remaining subsystems. | 
 | 1995 | 				 */ | 
 | 1996 | 				break; | 
 | 1997 | 			if (ss->cancel_attach) | 
| Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1998 | 				ss->cancel_attach(cgrp, &tset); | 
| Daisuke Nishimura | 2468c72 | 2010-03-10 15:22:03 -0800 | [diff] [blame] | 1999 | 		} | 
 | 2000 | 	} | 
 | 2001 | 	return retval; | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2002 | } | 
 | 2003 |  | 
| Sridhar Samudrala | d7926ee | 2010-05-30 22:24:39 +0200 | [diff] [blame] | 2004 | /** | 
| Michael S. Tsirkin | 31583bb | 2010-09-09 16:37:37 -0700 | [diff] [blame] | 2005 |  * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from' | 
 | 2006 |  * @from: attach to all cgroups of a given task | 
| Sridhar Samudrala | d7926ee | 2010-05-30 22:24:39 +0200 | [diff] [blame] | 2007 |  * @tsk: the task to be attached | 
 | 2008 |  */ | 
| Michael S. Tsirkin | 31583bb | 2010-09-09 16:37:37 -0700 | [diff] [blame] | 2009 | 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] | 2010 | { | 
 | 2011 | 	struct cgroupfs_root *root; | 
| Sridhar Samudrala | d7926ee | 2010-05-30 22:24:39 +0200 | [diff] [blame] | 2012 | 	int retval = 0; | 
 | 2013 |  | 
 | 2014 | 	cgroup_lock(); | 
 | 2015 | 	for_each_active_root(root) { | 
| Michael S. Tsirkin | 31583bb | 2010-09-09 16:37:37 -0700 | [diff] [blame] | 2016 | 		struct cgroup *from_cg = task_cgroup_from_root(from, root); | 
 | 2017 |  | 
 | 2018 | 		retval = cgroup_attach_task(from_cg, tsk); | 
| Sridhar Samudrala | d7926ee | 2010-05-30 22:24:39 +0200 | [diff] [blame] | 2019 | 		if (retval) | 
 | 2020 | 			break; | 
 | 2021 | 	} | 
 | 2022 | 	cgroup_unlock(); | 
 | 2023 |  | 
 | 2024 | 	return retval; | 
 | 2025 | } | 
| Michael S. Tsirkin | 31583bb | 2010-09-09 16:37:37 -0700 | [diff] [blame] | 2026 | EXPORT_SYMBOL_GPL(cgroup_attach_task_all); | 
| Sridhar Samudrala | d7926ee | 2010-05-30 22:24:39 +0200 | [diff] [blame] | 2027 |  | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2028 | /** | 
 | 2029 |  * cgroup_attach_proc - attach all threads in a threadgroup to a cgroup | 
 | 2030 |  * @cgrp: the cgroup to attach to | 
 | 2031 |  * @leader: the threadgroup leader task_struct of the group to be attached | 
 | 2032 |  * | 
| Tejun Heo | 257058a | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2033 |  * Call holding cgroup_mutex and the group_rwsem of the leader. Will take | 
 | 2034 |  * task_lock of each thread in leader's threadgroup individually in turn. | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2035 |  */ | 
| Kirill A. Shutemov | 1c6c3fa | 2011-12-27 07:46:25 +0200 | [diff] [blame] | 2036 | static int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader) | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2037 | { | 
 | 2038 | 	int retval, i, group_size; | 
 | 2039 | 	struct cgroup_subsys *ss, *failed_ss = NULL; | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2040 | 	/* guaranteed to be initialized later, but the compiler needs this */ | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2041 | 	struct cgroupfs_root *root = cgrp->root; | 
 | 2042 | 	/* threadgroup list cursor and array */ | 
 | 2043 | 	struct task_struct *tsk; | 
| Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2044 | 	struct task_and_cgroup *tc; | 
| Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2045 | 	struct flex_array *group; | 
| Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2046 | 	struct cgroup_taskset tset = { }; | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2047 |  | 
 | 2048 | 	/* | 
 | 2049 | 	 * step 0: in order to do expensive, possibly blocking operations for | 
 | 2050 | 	 * every thread, we cannot iterate the thread group list, since it needs | 
 | 2051 | 	 * 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] | 2052 | 	 * group - group_rwsem prevents new threads from appearing, and if | 
 | 2053 | 	 * threads exit, this will just be an over-estimate. | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2054 | 	 */ | 
 | 2055 | 	group_size = get_nr_threads(leader); | 
| Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2056 | 	/* flex_array supports very large thread-groups better than kmalloc. */ | 
| Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2057 | 	group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL); | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2058 | 	if (!group) | 
 | 2059 | 		return -ENOMEM; | 
| Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2060 | 	/* pre-allocate to guarantee space while iterating in rcu read-side. */ | 
 | 2061 | 	retval = flex_array_prealloc(group, 0, group_size - 1, GFP_KERNEL); | 
 | 2062 | 	if (retval) | 
 | 2063 | 		goto out_free_group_list; | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2064 |  | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2065 | 	tsk = leader; | 
 | 2066 | 	i = 0; | 
| Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2067 | 	/* | 
 | 2068 | 	 * Prevent freeing of tasks while we take a snapshot. Tasks that are | 
 | 2069 | 	 * already PF_EXITING could be freed from underneath us unless we | 
 | 2070 | 	 * take an rcu_read_lock. | 
 | 2071 | 	 */ | 
 | 2072 | 	rcu_read_lock(); | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2073 | 	do { | 
| Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2074 | 		struct task_and_cgroup ent; | 
 | 2075 |  | 
| Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2076 | 		/* @tsk either already exited or can't exit until the end */ | 
 | 2077 | 		if (tsk->flags & PF_EXITING) | 
 | 2078 | 			continue; | 
 | 2079 |  | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2080 | 		/* as per above, nr_threads may decrease, but not increase. */ | 
 | 2081 | 		BUG_ON(i >= group_size); | 
| Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2082 | 		ent.task = tsk; | 
 | 2083 | 		ent.cgrp = task_cgroup_from_root(tsk, root); | 
| Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2084 | 		/* nothing to do if this task is already in the cgroup */ | 
 | 2085 | 		if (ent.cgrp == cgrp) | 
 | 2086 | 			continue; | 
| Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2087 | 		/* | 
 | 2088 | 		 * saying GFP_ATOMIC has no effect here because we did prealloc | 
 | 2089 | 		 * earlier, but it's good form to communicate our expectations. | 
 | 2090 | 		 */ | 
| Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2091 | 		retval = flex_array_put(group, i, &ent, GFP_ATOMIC); | 
| Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2092 | 		BUG_ON(retval != 0); | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2093 | 		i++; | 
 | 2094 | 	} while_each_thread(leader, tsk); | 
| Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2095 | 	rcu_read_unlock(); | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2096 | 	/* remember the number of threads in the array for later. */ | 
 | 2097 | 	group_size = i; | 
| Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2098 | 	tset.tc_array = group; | 
 | 2099 | 	tset.tc_array_len = group_size; | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2100 |  | 
| Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2101 | 	/* methods shouldn't be called if no task is actually migrating */ | 
 | 2102 | 	retval = 0; | 
| Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2103 | 	if (!group_size) | 
| Mandeep Singh Baines | b07ef77 | 2011-12-21 20:18:36 -0800 | [diff] [blame] | 2104 | 		goto out_free_group_list; | 
| Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2105 |  | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2106 | 	/* | 
 | 2107 | 	 * step 1: check that we can legitimately attach to the cgroup. | 
 | 2108 | 	 */ | 
 | 2109 | 	for_each_subsys(root, ss) { | 
 | 2110 | 		if (ss->can_attach) { | 
| Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2111 | 			retval = ss->can_attach(cgrp, &tset); | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2112 | 			if (retval) { | 
 | 2113 | 				failed_ss = ss; | 
 | 2114 | 				goto out_cancel_attach; | 
 | 2115 | 			} | 
 | 2116 | 		} | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2117 | 	} | 
 | 2118 |  | 
 | 2119 | 	/* | 
 | 2120 | 	 * step 2: make sure css_sets exist for all threads to be migrated. | 
 | 2121 | 	 * we use find_css_set, which allocates a new one if necessary. | 
 | 2122 | 	 */ | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2123 | 	for (i = 0; i < group_size; i++) { | 
| Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2124 | 		tc = flex_array_get(group, i); | 
| Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2125 | 		tc->cg = find_css_set(tc->task->cgroups, cgrp); | 
 | 2126 | 		if (!tc->cg) { | 
 | 2127 | 			retval = -ENOMEM; | 
 | 2128 | 			goto out_put_css_set_refs; | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2129 | 		} | 
 | 2130 | 	} | 
 | 2131 |  | 
 | 2132 | 	/* | 
| Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2133 | 	 * step 3: now that we're guaranteed success wrt the css_sets, | 
 | 2134 | 	 * proceed to move all tasks to the new cgroup.  There are no | 
 | 2135 | 	 * failure cases after here, so this is the commit point. | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2136 | 	 */ | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2137 | 	for (i = 0; i < group_size; i++) { | 
| Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2138 | 		tc = flex_array_get(group, i); | 
| Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2139 | 		cgroup_task_migrate(cgrp, tc->cgrp, tc->task, tc->cg); | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2140 | 	} | 
 | 2141 | 	/* nothing is sensitive to fork() after this point. */ | 
 | 2142 |  | 
 | 2143 | 	/* | 
| Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2144 | 	 * step 4: do subsystem attach callbacks. | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2145 | 	 */ | 
 | 2146 | 	for_each_subsys(root, ss) { | 
 | 2147 | 		if (ss->attach) | 
| Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2148 | 			ss->attach(cgrp, &tset); | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2149 | 	} | 
 | 2150 |  | 
 | 2151 | 	/* | 
 | 2152 | 	 * step 5: success! and cleanup | 
 | 2153 | 	 */ | 
 | 2154 | 	synchronize_rcu(); | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2155 | 	retval = 0; | 
| Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2156 | out_put_css_set_refs: | 
 | 2157 | 	if (retval) { | 
 | 2158 | 		for (i = 0; i < group_size; i++) { | 
 | 2159 | 			tc = flex_array_get(group, i); | 
 | 2160 | 			if (!tc->cg) | 
 | 2161 | 				break; | 
 | 2162 | 			put_css_set(tc->cg); | 
 | 2163 | 		} | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2164 | 	} | 
 | 2165 | out_cancel_attach: | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2166 | 	if (retval) { | 
 | 2167 | 		for_each_subsys(root, ss) { | 
| Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2168 | 			if (ss == failed_ss) | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2169 | 				break; | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2170 | 			if (ss->cancel_attach) | 
| Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2171 | 				ss->cancel_attach(cgrp, &tset); | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2172 | 		} | 
 | 2173 | 	} | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2174 | out_free_group_list: | 
| Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2175 | 	flex_array_free(group); | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2176 | 	return retval; | 
 | 2177 | } | 
 | 2178 |  | 
 | 2179 | /* | 
 | 2180 |  * 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] | 2181 |  * function to attach either it or all tasks in its threadgroup. Will lock | 
 | 2182 |  * cgroup_mutex and threadgroup; may take task_lock of task. | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2183 |  */ | 
 | 2184 | 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] | 2185 | { | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2186 | 	struct task_struct *tsk; | 
| David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2187 | 	const struct cred *cred = current_cred(), *tcred; | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2188 | 	int ret; | 
 | 2189 |  | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2190 | 	if (!cgroup_lock_live_group(cgrp)) | 
 | 2191 | 		return -ENODEV; | 
 | 2192 |  | 
| Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2193 | retry_find_task: | 
 | 2194 | 	rcu_read_lock(); | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2195 | 	if (pid) { | 
| Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2196 | 		tsk = find_task_by_vpid(pid); | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2197 | 		if (!tsk) { | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2198 | 			rcu_read_unlock(); | 
| Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2199 | 			ret= -ESRCH; | 
 | 2200 | 			goto out_unlock_cgroup; | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2201 | 		} | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2202 | 		/* | 
 | 2203 | 		 * even if we're attaching all tasks in the thread group, we | 
 | 2204 | 		 * only need to check permissions on one of them. | 
 | 2205 | 		 */ | 
| David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2206 | 		tcred = __task_cred(tsk); | 
| Eric W. Biederman | 14a590c | 2012-03-12 15:44:39 -0700 | [diff] [blame] | 2207 | 		if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && | 
 | 2208 | 		    !uid_eq(cred->euid, tcred->uid) && | 
 | 2209 | 		    !uid_eq(cred->euid, tcred->suid)) { | 
| David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2210 | 			rcu_read_unlock(); | 
| Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2211 | 			ret = -EACCES; | 
 | 2212 | 			goto out_unlock_cgroup; | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2213 | 		} | 
| Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2214 | 	} else | 
 | 2215 | 		tsk = current; | 
| Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2216 |  | 
 | 2217 | 	if (threadgroup) | 
| Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2218 | 		tsk = tsk->group_leader; | 
| Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2219 |  | 
 | 2220 | 	/* | 
 | 2221 | 	 * Workqueue threads may acquire PF_THREAD_BOUND and become | 
 | 2222 | 	 * trapped in a cpuset, or RT worker may be born in a cgroup | 
 | 2223 | 	 * with no rt_runtime allocated.  Just say no. | 
 | 2224 | 	 */ | 
 | 2225 | 	if (tsk == kthreadd_task || (tsk->flags & PF_THREAD_BOUND)) { | 
 | 2226 | 		ret = -EINVAL; | 
 | 2227 | 		rcu_read_unlock(); | 
 | 2228 | 		goto out_unlock_cgroup; | 
 | 2229 | 	} | 
 | 2230 |  | 
| Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2231 | 	get_task_struct(tsk); | 
 | 2232 | 	rcu_read_unlock(); | 
| Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2233 |  | 
| Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2234 | 	threadgroup_lock(tsk); | 
 | 2235 | 	if (threadgroup) { | 
 | 2236 | 		if (!thread_group_leader(tsk)) { | 
 | 2237 | 			/* | 
 | 2238 | 			 * a race with de_thread from another thread's exec() | 
 | 2239 | 			 * may strip us of our leadership, if this happens, | 
 | 2240 | 			 * there is no choice but to throw this task away and | 
 | 2241 | 			 * try again; this is | 
 | 2242 | 			 * "double-double-toil-and-trouble-check locking". | 
 | 2243 | 			 */ | 
 | 2244 | 			threadgroup_unlock(tsk); | 
 | 2245 | 			put_task_struct(tsk); | 
 | 2246 | 			goto retry_find_task; | 
 | 2247 | 		} | 
 | 2248 | 		ret = cgroup_attach_proc(cgrp, tsk); | 
 | 2249 | 	} else | 
 | 2250 | 		ret = cgroup_attach_task(cgrp, tsk); | 
| Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2251 | 	threadgroup_unlock(tsk); | 
 | 2252 |  | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2253 | 	put_task_struct(tsk); | 
| Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2254 | out_unlock_cgroup: | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2255 | 	cgroup_unlock(); | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2256 | 	return ret; | 
 | 2257 | } | 
 | 2258 |  | 
| Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2259 | static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid) | 
 | 2260 | { | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2261 | 	return attach_task_by_pid(cgrp, pid, false); | 
 | 2262 | } | 
 | 2263 |  | 
 | 2264 | static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid) | 
 | 2265 | { | 
| Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2266 | 	return attach_task_by_pid(cgrp, tgid, true); | 
| Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2267 | } | 
 | 2268 |  | 
| Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2269 | /** | 
 | 2270 |  * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive. | 
 | 2271 |  * @cgrp: the cgroup to be checked for liveness | 
 | 2272 |  * | 
| Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2273 |  * On success, returns true; the lock should be later released with | 
 | 2274 |  * cgroup_unlock(). On failure returns false with no lock held. | 
| Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2275 |  */ | 
| Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2276 | bool cgroup_lock_live_group(struct cgroup *cgrp) | 
| Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2277 | { | 
 | 2278 | 	mutex_lock(&cgroup_mutex); | 
 | 2279 | 	if (cgroup_is_removed(cgrp)) { | 
 | 2280 | 		mutex_unlock(&cgroup_mutex); | 
 | 2281 | 		return false; | 
 | 2282 | 	} | 
 | 2283 | 	return true; | 
 | 2284 | } | 
| Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 2285 | EXPORT_SYMBOL_GPL(cgroup_lock_live_group); | 
| Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2286 |  | 
 | 2287 | static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft, | 
 | 2288 | 				      const char *buffer) | 
 | 2289 | { | 
 | 2290 | 	BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX); | 
| Evgeny Kuznetsov | f4a2589 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 2291 | 	if (strlen(buffer) >= PATH_MAX) | 
 | 2292 | 		return -EINVAL; | 
| Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2293 | 	if (!cgroup_lock_live_group(cgrp)) | 
 | 2294 | 		return -ENODEV; | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2295 | 	mutex_lock(&cgroup_root_mutex); | 
| Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2296 | 	strcpy(cgrp->root->release_agent_path, buffer); | 
| Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2297 | 	mutex_unlock(&cgroup_root_mutex); | 
| Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2298 | 	cgroup_unlock(); | 
| Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2299 | 	return 0; | 
 | 2300 | } | 
 | 2301 |  | 
 | 2302 | static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft, | 
 | 2303 | 				     struct seq_file *seq) | 
 | 2304 | { | 
 | 2305 | 	if (!cgroup_lock_live_group(cgrp)) | 
 | 2306 | 		return -ENODEV; | 
 | 2307 | 	seq_puts(seq, cgrp->root->release_agent_path); | 
 | 2308 | 	seq_putc(seq, '\n'); | 
| Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2309 | 	cgroup_unlock(); | 
| Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2310 | 	return 0; | 
 | 2311 | } | 
 | 2312 |  | 
| Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2313 | /* A buffer size big enough for numbers or short strings */ | 
 | 2314 | #define CGROUP_LOCAL_BUFFER_SIZE 64 | 
 | 2315 |  | 
| Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2316 | static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft, | 
| Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2317 | 				struct file *file, | 
 | 2318 | 				const char __user *userbuf, | 
 | 2319 | 				size_t nbytes, loff_t *unused_ppos) | 
| Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2320 | { | 
| Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2321 | 	char buffer[CGROUP_LOCAL_BUFFER_SIZE]; | 
| Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2322 | 	int retval = 0; | 
| Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2323 | 	char *end; | 
 | 2324 |  | 
 | 2325 | 	if (!nbytes) | 
 | 2326 | 		return -EINVAL; | 
 | 2327 | 	if (nbytes >= sizeof(buffer)) | 
 | 2328 | 		return -E2BIG; | 
 | 2329 | 	if (copy_from_user(buffer, userbuf, nbytes)) | 
 | 2330 | 		return -EFAULT; | 
 | 2331 |  | 
 | 2332 | 	buffer[nbytes] = 0;     /* nul-terminate */ | 
| Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2333 | 	if (cft->write_u64) { | 
| KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2334 | 		u64 val = simple_strtoull(strstrip(buffer), &end, 0); | 
| Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2335 | 		if (*end) | 
 | 2336 | 			return -EINVAL; | 
 | 2337 | 		retval = cft->write_u64(cgrp, cft, val); | 
 | 2338 | 	} else { | 
| KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2339 | 		s64 val = simple_strtoll(strstrip(buffer), &end, 0); | 
| Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2340 | 		if (*end) | 
 | 2341 | 			return -EINVAL; | 
 | 2342 | 		retval = cft->write_s64(cgrp, cft, val); | 
 | 2343 | 	} | 
| Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2344 | 	if (!retval) | 
 | 2345 | 		retval = nbytes; | 
 | 2346 | 	return retval; | 
 | 2347 | } | 
 | 2348 |  | 
| Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2349 | static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft, | 
 | 2350 | 				   struct file *file, | 
 | 2351 | 				   const char __user *userbuf, | 
 | 2352 | 				   size_t nbytes, loff_t *unused_ppos) | 
 | 2353 | { | 
| Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2354 | 	char local_buffer[CGROUP_LOCAL_BUFFER_SIZE]; | 
| Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2355 | 	int retval = 0; | 
 | 2356 | 	size_t max_bytes = cft->max_write_len; | 
 | 2357 | 	char *buffer = local_buffer; | 
 | 2358 |  | 
 | 2359 | 	if (!max_bytes) | 
 | 2360 | 		max_bytes = sizeof(local_buffer) - 1; | 
 | 2361 | 	if (nbytes >= max_bytes) | 
 | 2362 | 		return -E2BIG; | 
 | 2363 | 	/* Allocate a dynamic buffer if we need one */ | 
 | 2364 | 	if (nbytes >= sizeof(local_buffer)) { | 
 | 2365 | 		buffer = kmalloc(nbytes + 1, GFP_KERNEL); | 
 | 2366 | 		if (buffer == NULL) | 
 | 2367 | 			return -ENOMEM; | 
 | 2368 | 	} | 
| Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 2369 | 	if (nbytes && copy_from_user(buffer, userbuf, nbytes)) { | 
 | 2370 | 		retval = -EFAULT; | 
 | 2371 | 		goto out; | 
 | 2372 | 	} | 
| Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2373 |  | 
 | 2374 | 	buffer[nbytes] = 0;     /* nul-terminate */ | 
| KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2375 | 	retval = cft->write_string(cgrp, cft, strstrip(buffer)); | 
| Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2376 | 	if (!retval) | 
 | 2377 | 		retval = nbytes; | 
| Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 2378 | out: | 
| Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2379 | 	if (buffer != local_buffer) | 
 | 2380 | 		kfree(buffer); | 
 | 2381 | 	return retval; | 
 | 2382 | } | 
 | 2383 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2384 | static ssize_t cgroup_file_write(struct file *file, const char __user *buf, | 
 | 2385 | 						size_t nbytes, loff_t *ppos) | 
 | 2386 | { | 
 | 2387 | 	struct cftype *cft = __d_cft(file->f_dentry); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2388 | 	struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2389 |  | 
| Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2390 | 	if (cgroup_is_removed(cgrp)) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2391 | 		return -ENODEV; | 
| Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2392 | 	if (cft->write) | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2393 | 		return cft->write(cgrp, cft, file, buf, nbytes, ppos); | 
| Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2394 | 	if (cft->write_u64 || cft->write_s64) | 
 | 2395 | 		return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos); | 
| Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2396 | 	if (cft->write_string) | 
 | 2397 | 		return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos); | 
| Pavel Emelyanov | d447ea2 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 2398 | 	if (cft->trigger) { | 
 | 2399 | 		int ret = cft->trigger(cgrp, (unsigned int)cft->private); | 
 | 2400 | 		return ret ? ret : nbytes; | 
 | 2401 | 	} | 
| Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2402 | 	return -EINVAL; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2403 | } | 
 | 2404 |  | 
| Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2405 | static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft, | 
 | 2406 | 			       struct file *file, | 
 | 2407 | 			       char __user *buf, size_t nbytes, | 
 | 2408 | 			       loff_t *ppos) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2409 | { | 
| Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2410 | 	char tmp[CGROUP_LOCAL_BUFFER_SIZE]; | 
| Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2411 | 	u64 val = cft->read_u64(cgrp, cft); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2412 | 	int len = sprintf(tmp, "%llu\n", (unsigned long long) val); | 
 | 2413 |  | 
 | 2414 | 	return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); | 
 | 2415 | } | 
 | 2416 |  | 
| Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2417 | static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft, | 
 | 2418 | 			       struct file *file, | 
 | 2419 | 			       char __user *buf, size_t nbytes, | 
 | 2420 | 			       loff_t *ppos) | 
 | 2421 | { | 
| Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2422 | 	char tmp[CGROUP_LOCAL_BUFFER_SIZE]; | 
| Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2423 | 	s64 val = cft->read_s64(cgrp, cft); | 
 | 2424 | 	int len = sprintf(tmp, "%lld\n", (long long) val); | 
 | 2425 |  | 
 | 2426 | 	return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); | 
 | 2427 | } | 
 | 2428 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2429 | static ssize_t cgroup_file_read(struct file *file, char __user *buf, | 
 | 2430 | 				   size_t nbytes, loff_t *ppos) | 
 | 2431 | { | 
 | 2432 | 	struct cftype *cft = __d_cft(file->f_dentry); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2433 | 	struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2434 |  | 
| Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2435 | 	if (cgroup_is_removed(cgrp)) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2436 | 		return -ENODEV; | 
 | 2437 |  | 
 | 2438 | 	if (cft->read) | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2439 | 		return cft->read(cgrp, cft, file, buf, nbytes, ppos); | 
| Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2440 | 	if (cft->read_u64) | 
 | 2441 | 		return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos); | 
| Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2442 | 	if (cft->read_s64) | 
 | 2443 | 		return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2444 | 	return -EINVAL; | 
 | 2445 | } | 
 | 2446 |  | 
| Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2447 | /* | 
 | 2448 |  * seqfile ops/methods for returning structured data. Currently just | 
 | 2449 |  * supports string->u64 maps, but can be extended in future. | 
 | 2450 |  */ | 
 | 2451 |  | 
 | 2452 | struct cgroup_seqfile_state { | 
 | 2453 | 	struct cftype *cft; | 
 | 2454 | 	struct cgroup *cgroup; | 
 | 2455 | }; | 
 | 2456 |  | 
 | 2457 | static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value) | 
 | 2458 | { | 
 | 2459 | 	struct seq_file *sf = cb->state; | 
 | 2460 | 	return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value); | 
 | 2461 | } | 
 | 2462 |  | 
 | 2463 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) | 
 | 2464 | { | 
 | 2465 | 	struct cgroup_seqfile_state *state = m->private; | 
 | 2466 | 	struct cftype *cft = state->cft; | 
| Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2467 | 	if (cft->read_map) { | 
 | 2468 | 		struct cgroup_map_cb cb = { | 
 | 2469 | 			.fill = cgroup_map_add, | 
 | 2470 | 			.state = m, | 
 | 2471 | 		}; | 
 | 2472 | 		return cft->read_map(state->cgroup, cft, &cb); | 
 | 2473 | 	} | 
 | 2474 | 	return cft->read_seq_string(state->cgroup, cft, m); | 
| Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2475 | } | 
 | 2476 |  | 
| Adrian Bunk | 96930a6 | 2008-07-25 19:46:21 -0700 | [diff] [blame] | 2477 | static int cgroup_seqfile_release(struct inode *inode, struct file *file) | 
| Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2478 | { | 
 | 2479 | 	struct seq_file *seq = file->private_data; | 
 | 2480 | 	kfree(seq->private); | 
 | 2481 | 	return single_release(inode, file); | 
 | 2482 | } | 
 | 2483 |  | 
| Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2484 | static const struct file_operations cgroup_seqfile_operations = { | 
| Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2485 | 	.read = seq_read, | 
| Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2486 | 	.write = cgroup_file_write, | 
| Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2487 | 	.llseek = seq_lseek, | 
 | 2488 | 	.release = cgroup_seqfile_release, | 
 | 2489 | }; | 
 | 2490 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2491 | static int cgroup_file_open(struct inode *inode, struct file *file) | 
 | 2492 | { | 
 | 2493 | 	int err; | 
 | 2494 | 	struct cftype *cft; | 
 | 2495 |  | 
 | 2496 | 	err = generic_file_open(inode, file); | 
 | 2497 | 	if (err) | 
 | 2498 | 		return err; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2499 | 	cft = __d_cft(file->f_dentry); | 
| Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2500 |  | 
| Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2501 | 	if (cft->read_map || cft->read_seq_string) { | 
| Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2502 | 		struct cgroup_seqfile_state *state = | 
 | 2503 | 			kzalloc(sizeof(*state), GFP_USER); | 
 | 2504 | 		if (!state) | 
 | 2505 | 			return -ENOMEM; | 
 | 2506 | 		state->cft = cft; | 
 | 2507 | 		state->cgroup = __d_cgrp(file->f_dentry->d_parent); | 
 | 2508 | 		file->f_op = &cgroup_seqfile_operations; | 
 | 2509 | 		err = single_open(file, cgroup_seqfile_show, state); | 
 | 2510 | 		if (err < 0) | 
 | 2511 | 			kfree(state); | 
 | 2512 | 	} else if (cft->open) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2513 | 		err = cft->open(inode, file); | 
 | 2514 | 	else | 
 | 2515 | 		err = 0; | 
 | 2516 |  | 
 | 2517 | 	return err; | 
 | 2518 | } | 
 | 2519 |  | 
 | 2520 | static int cgroup_file_release(struct inode *inode, struct file *file) | 
 | 2521 | { | 
 | 2522 | 	struct cftype *cft = __d_cft(file->f_dentry); | 
 | 2523 | 	if (cft->release) | 
 | 2524 | 		return cft->release(inode, file); | 
 | 2525 | 	return 0; | 
 | 2526 | } | 
 | 2527 |  | 
 | 2528 | /* | 
 | 2529 |  * cgroup_rename - Only allow simple rename of directories in place. | 
 | 2530 |  */ | 
 | 2531 | static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry, | 
 | 2532 | 			    struct inode *new_dir, struct dentry *new_dentry) | 
 | 2533 | { | 
 | 2534 | 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) | 
 | 2535 | 		return -ENOTDIR; | 
 | 2536 | 	if (new_dentry->d_inode) | 
 | 2537 | 		return -EEXIST; | 
 | 2538 | 	if (old_dir != new_dir) | 
 | 2539 | 		return -EIO; | 
 | 2540 | 	return simple_rename(old_dir, old_dentry, new_dir, new_dentry); | 
 | 2541 | } | 
 | 2542 |  | 
| Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2543 | static struct simple_xattrs *__d_xattrs(struct dentry *dentry) | 
 | 2544 | { | 
 | 2545 | 	if (S_ISDIR(dentry->d_inode->i_mode)) | 
 | 2546 | 		return &__d_cgrp(dentry)->xattrs; | 
 | 2547 | 	else | 
 | 2548 | 		return &__d_cft(dentry)->xattrs; | 
 | 2549 | } | 
 | 2550 |  | 
 | 2551 | static inline int xattr_enabled(struct dentry *dentry) | 
 | 2552 | { | 
 | 2553 | 	struct cgroupfs_root *root = dentry->d_sb->s_fs_info; | 
 | 2554 | 	return test_bit(ROOT_XATTR, &root->flags); | 
 | 2555 | } | 
 | 2556 |  | 
 | 2557 | static bool is_valid_xattr(const char *name) | 
 | 2558 | { | 
 | 2559 | 	if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) || | 
 | 2560 | 	    !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) | 
 | 2561 | 		return true; | 
 | 2562 | 	return false; | 
 | 2563 | } | 
 | 2564 |  | 
 | 2565 | static int cgroup_setxattr(struct dentry *dentry, const char *name, | 
 | 2566 | 			   const void *val, size_t size, int flags) | 
 | 2567 | { | 
 | 2568 | 	if (!xattr_enabled(dentry)) | 
 | 2569 | 		return -EOPNOTSUPP; | 
 | 2570 | 	if (!is_valid_xattr(name)) | 
 | 2571 | 		return -EINVAL; | 
 | 2572 | 	return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags); | 
 | 2573 | } | 
 | 2574 |  | 
 | 2575 | static int cgroup_removexattr(struct dentry *dentry, const char *name) | 
 | 2576 | { | 
 | 2577 | 	if (!xattr_enabled(dentry)) | 
 | 2578 | 		return -EOPNOTSUPP; | 
 | 2579 | 	if (!is_valid_xattr(name)) | 
 | 2580 | 		return -EINVAL; | 
 | 2581 | 	return simple_xattr_remove(__d_xattrs(dentry), name); | 
 | 2582 | } | 
 | 2583 |  | 
 | 2584 | static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name, | 
 | 2585 | 			       void *buf, size_t size) | 
 | 2586 | { | 
 | 2587 | 	if (!xattr_enabled(dentry)) | 
 | 2588 | 		return -EOPNOTSUPP; | 
 | 2589 | 	if (!is_valid_xattr(name)) | 
 | 2590 | 		return -EINVAL; | 
 | 2591 | 	return simple_xattr_get(__d_xattrs(dentry), name, buf, size); | 
 | 2592 | } | 
 | 2593 |  | 
 | 2594 | static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size) | 
 | 2595 | { | 
 | 2596 | 	if (!xattr_enabled(dentry)) | 
 | 2597 | 		return -EOPNOTSUPP; | 
 | 2598 | 	return simple_xattr_list(__d_xattrs(dentry), buf, size); | 
 | 2599 | } | 
 | 2600 |  | 
| Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2601 | static const struct file_operations cgroup_file_operations = { | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2602 | 	.read = cgroup_file_read, | 
 | 2603 | 	.write = cgroup_file_write, | 
 | 2604 | 	.llseek = generic_file_llseek, | 
 | 2605 | 	.open = cgroup_file_open, | 
 | 2606 | 	.release = cgroup_file_release, | 
 | 2607 | }; | 
 | 2608 |  | 
| Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2609 | static const struct inode_operations cgroup_file_inode_operations = { | 
 | 2610 | 	.setxattr = cgroup_setxattr, | 
 | 2611 | 	.getxattr = cgroup_getxattr, | 
 | 2612 | 	.listxattr = cgroup_listxattr, | 
 | 2613 | 	.removexattr = cgroup_removexattr, | 
 | 2614 | }; | 
 | 2615 |  | 
| Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 2616 | static const struct inode_operations cgroup_dir_inode_operations = { | 
| Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 2617 | 	.lookup = cgroup_lookup, | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2618 | 	.mkdir = cgroup_mkdir, | 
 | 2619 | 	.rmdir = cgroup_rmdir, | 
 | 2620 | 	.rename = cgroup_rename, | 
| Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2621 | 	.setxattr = cgroup_setxattr, | 
 | 2622 | 	.getxattr = cgroup_getxattr, | 
 | 2623 | 	.listxattr = cgroup_listxattr, | 
 | 2624 | 	.removexattr = cgroup_removexattr, | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2625 | }; | 
 | 2626 |  | 
| Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 2627 | static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) | 
| Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 2628 | { | 
 | 2629 | 	if (dentry->d_name.len > NAME_MAX) | 
 | 2630 | 		return ERR_PTR(-ENAMETOOLONG); | 
 | 2631 | 	d_add(dentry, NULL); | 
 | 2632 | 	return NULL; | 
 | 2633 | } | 
 | 2634 |  | 
| Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 2635 | /* | 
 | 2636 |  * Check if a file is a control file | 
 | 2637 |  */ | 
 | 2638 | static inline struct cftype *__file_cft(struct file *file) | 
 | 2639 | { | 
 | 2640 | 	if (file->f_dentry->d_inode->i_fop != &cgroup_file_operations) | 
 | 2641 | 		return ERR_PTR(-EINVAL); | 
 | 2642 | 	return __d_cft(file->f_dentry); | 
 | 2643 | } | 
 | 2644 |  | 
| Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2645 | static int cgroup_create_file(struct dentry *dentry, umode_t mode, | 
| Nick Piggin | 5adcee1 | 2011-01-07 17:49:20 +1100 | [diff] [blame] | 2646 | 				struct super_block *sb) | 
 | 2647 | { | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2648 | 	struct inode *inode; | 
 | 2649 |  | 
 | 2650 | 	if (!dentry) | 
 | 2651 | 		return -ENOENT; | 
 | 2652 | 	if (dentry->d_inode) | 
 | 2653 | 		return -EEXIST; | 
 | 2654 |  | 
 | 2655 | 	inode = cgroup_new_inode(mode, sb); | 
 | 2656 | 	if (!inode) | 
 | 2657 | 		return -ENOMEM; | 
 | 2658 |  | 
 | 2659 | 	if (S_ISDIR(mode)) { | 
 | 2660 | 		inode->i_op = &cgroup_dir_inode_operations; | 
 | 2661 | 		inode->i_fop = &simple_dir_operations; | 
 | 2662 |  | 
 | 2663 | 		/* start off with i_nlink == 2 (for "." entry) */ | 
 | 2664 | 		inc_nlink(inode); | 
| Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 2665 | 		inc_nlink(dentry->d_parent->d_inode); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2666 |  | 
| Tejun Heo | b8a2df6 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 2667 | 		/* | 
 | 2668 | 		 * Control reaches here with cgroup_mutex held. | 
 | 2669 | 		 * @inode->i_mutex should nest outside cgroup_mutex but we | 
 | 2670 | 		 * want to populate it immediately without releasing | 
 | 2671 | 		 * cgroup_mutex.  As @inode isn't visible to anyone else | 
 | 2672 | 		 * yet, trylock will always succeed without affecting | 
 | 2673 | 		 * lockdep checks. | 
 | 2674 | 		 */ | 
 | 2675 | 		WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex)); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2676 | 	} else if (S_ISREG(mode)) { | 
 | 2677 | 		inode->i_size = 0; | 
 | 2678 | 		inode->i_fop = &cgroup_file_operations; | 
| Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2679 | 		inode->i_op = &cgroup_file_inode_operations; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2680 | 	} | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2681 | 	d_instantiate(dentry, inode); | 
 | 2682 | 	dget(dentry);	/* Extra count - pin the dentry in core */ | 
 | 2683 | 	return 0; | 
 | 2684 | } | 
 | 2685 |  | 
| Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2686 | /** | 
 | 2687 |  * cgroup_file_mode - deduce file mode of a control file | 
 | 2688 |  * @cft: the control file in question | 
 | 2689 |  * | 
 | 2690 |  * returns cft->mode if ->mode is not 0 | 
 | 2691 |  * returns S_IRUGO|S_IWUSR if it has both a read and a write handler | 
 | 2692 |  * returns S_IRUGO if it has only a read handler | 
 | 2693 |  * returns S_IWUSR if it has only a write hander | 
 | 2694 |  */ | 
| Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2695 | static umode_t cgroup_file_mode(const struct cftype *cft) | 
| Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2696 | { | 
| Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2697 | 	umode_t mode = 0; | 
| Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2698 |  | 
 | 2699 | 	if (cft->mode) | 
 | 2700 | 		return cft->mode; | 
 | 2701 |  | 
 | 2702 | 	if (cft->read || cft->read_u64 || cft->read_s64 || | 
 | 2703 | 	    cft->read_map || cft->read_seq_string) | 
 | 2704 | 		mode |= S_IRUGO; | 
 | 2705 |  | 
 | 2706 | 	if (cft->write || cft->write_u64 || cft->write_s64 || | 
 | 2707 | 	    cft->write_string || cft->trigger) | 
 | 2708 | 		mode |= S_IWUSR; | 
 | 2709 |  | 
 | 2710 | 	return mode; | 
 | 2711 | } | 
 | 2712 |  | 
| Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2713 | static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys, | 
| Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2714 | 			   struct cftype *cft) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2715 | { | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2716 | 	struct dentry *dir = cgrp->dentry; | 
| Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2717 | 	struct cgroup *parent = __d_cgrp(dir); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2718 | 	struct dentry *dentry; | 
| Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2719 | 	struct cfent *cfe; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2720 | 	int error; | 
| Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2721 | 	umode_t mode; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2722 | 	char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 }; | 
| Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2723 |  | 
| Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2724 | 	simple_xattrs_init(&cft->xattrs); | 
 | 2725 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2726 | 	if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) { | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2727 | 		strcpy(name, subsys->name); | 
 | 2728 | 		strcat(name, "."); | 
 | 2729 | 	} | 
 | 2730 | 	strcat(name, cft->name); | 
| Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2731 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2732 | 	BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex)); | 
| Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2733 |  | 
 | 2734 | 	cfe = kzalloc(sizeof(*cfe), GFP_KERNEL); | 
 | 2735 | 	if (!cfe) | 
 | 2736 | 		return -ENOMEM; | 
 | 2737 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2738 | 	dentry = lookup_one_len(name, dir, strlen(name)); | 
| Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2739 | 	if (IS_ERR(dentry)) { | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2740 | 		error = PTR_ERR(dentry); | 
| Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2741 | 		goto out; | 
 | 2742 | 	} | 
 | 2743 |  | 
 | 2744 | 	mode = cgroup_file_mode(cft); | 
 | 2745 | 	error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb); | 
 | 2746 | 	if (!error) { | 
 | 2747 | 		cfe->type = (void *)cft; | 
 | 2748 | 		cfe->dentry = dentry; | 
 | 2749 | 		dentry->d_fsdata = cfe; | 
 | 2750 | 		list_add_tail(&cfe->node, &parent->files); | 
 | 2751 | 		cfe = NULL; | 
 | 2752 | 	} | 
 | 2753 | 	dput(dentry); | 
 | 2754 | out: | 
 | 2755 | 	kfree(cfe); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2756 | 	return error; | 
 | 2757 | } | 
 | 2758 |  | 
| Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2759 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys, | 
| Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2760 | 			      struct cftype cfts[], bool is_add) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2761 | { | 
| Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2762 | 	struct cftype *cft; | 
| Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2763 | 	int err, ret = 0; | 
 | 2764 |  | 
 | 2765 | 	for (cft = cfts; cft->name[0] != '\0'; cft++) { | 
| Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2766 | 		/* does cft->flags tell us to skip this file on @cgrp? */ | 
 | 2767 | 		if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent) | 
 | 2768 | 			continue; | 
 | 2769 | 		if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent) | 
 | 2770 | 			continue; | 
 | 2771 |  | 
| Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2772 | 		if (is_add) | 
 | 2773 | 			err = cgroup_add_file(cgrp, subsys, cft); | 
 | 2774 | 		else | 
 | 2775 | 			err = cgroup_rm_file(cgrp, cft); | 
| Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2776 | 		if (err) { | 
| Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2777 | 			pr_warning("cgroup_addrm_files: failed to %s %s, err=%d\n", | 
 | 2778 | 				   is_add ? "add" : "remove", cft->name, err); | 
| Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2779 | 			ret = err; | 
 | 2780 | 		} | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2781 | 	} | 
| Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2782 | 	return ret; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2783 | } | 
 | 2784 |  | 
| Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2785 | static DEFINE_MUTEX(cgroup_cft_mutex); | 
 | 2786 |  | 
 | 2787 | static void cgroup_cfts_prepare(void) | 
 | 2788 | 	__acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex) | 
 | 2789 | { | 
 | 2790 | 	/* | 
 | 2791 | 	 * Thanks to the entanglement with vfs inode locking, we can't walk | 
 | 2792 | 	 * the existing cgroups under cgroup_mutex and create files. | 
 | 2793 | 	 * Instead, we increment reference on all cgroups and build list of | 
 | 2794 | 	 * them using @cgrp->cft_q_node.  Grab cgroup_cft_mutex to ensure | 
 | 2795 | 	 * exclusive access to the field. | 
 | 2796 | 	 */ | 
 | 2797 | 	mutex_lock(&cgroup_cft_mutex); | 
 | 2798 | 	mutex_lock(&cgroup_mutex); | 
 | 2799 | } | 
 | 2800 |  | 
 | 2801 | static void cgroup_cfts_commit(struct cgroup_subsys *ss, | 
| Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2802 | 			       struct cftype *cfts, bool is_add) | 
| Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2803 | 	__releases(&cgroup_mutex) __releases(&cgroup_cft_mutex) | 
 | 2804 | { | 
 | 2805 | 	LIST_HEAD(pending); | 
 | 2806 | 	struct cgroup *cgrp, *n; | 
| Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2807 |  | 
 | 2808 | 	/* %NULL @cfts indicates abort and don't bother if @ss isn't attached */ | 
 | 2809 | 	if (cfts && ss->root != &rootnode) { | 
 | 2810 | 		list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) { | 
 | 2811 | 			dget(cgrp->dentry); | 
 | 2812 | 			list_add_tail(&cgrp->cft_q_node, &pending); | 
 | 2813 | 		} | 
 | 2814 | 	} | 
 | 2815 |  | 
 | 2816 | 	mutex_unlock(&cgroup_mutex); | 
 | 2817 |  | 
 | 2818 | 	/* | 
 | 2819 | 	 * All new cgroups will see @cfts update on @ss->cftsets.  Add/rm | 
 | 2820 | 	 * files for all cgroups which were created before. | 
 | 2821 | 	 */ | 
 | 2822 | 	list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) { | 
 | 2823 | 		struct inode *inode = cgrp->dentry->d_inode; | 
 | 2824 |  | 
 | 2825 | 		mutex_lock(&inode->i_mutex); | 
 | 2826 | 		mutex_lock(&cgroup_mutex); | 
 | 2827 | 		if (!cgroup_is_removed(cgrp)) | 
| Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2828 | 			cgroup_addrm_files(cgrp, ss, cfts, is_add); | 
| Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2829 | 		mutex_unlock(&cgroup_mutex); | 
 | 2830 | 		mutex_unlock(&inode->i_mutex); | 
 | 2831 |  | 
 | 2832 | 		list_del_init(&cgrp->cft_q_node); | 
 | 2833 | 		dput(cgrp->dentry); | 
 | 2834 | 	} | 
 | 2835 |  | 
 | 2836 | 	mutex_unlock(&cgroup_cft_mutex); | 
 | 2837 | } | 
 | 2838 |  | 
 | 2839 | /** | 
 | 2840 |  * cgroup_add_cftypes - add an array of cftypes to a subsystem | 
 | 2841 |  * @ss: target cgroup subsystem | 
 | 2842 |  * @cfts: zero-length name terminated array of cftypes | 
 | 2843 |  * | 
 | 2844 |  * Register @cfts to @ss.  Files described by @cfts are created for all | 
 | 2845 |  * existing cgroups to which @ss is attached and all future cgroups will | 
 | 2846 |  * have them too.  This function can be called anytime whether @ss is | 
 | 2847 |  * attached or not. | 
 | 2848 |  * | 
 | 2849 |  * Returns 0 on successful registration, -errno on failure.  Note that this | 
 | 2850 |  * function currently returns 0 as long as @cfts registration is successful | 
 | 2851 |  * even if some file creation attempts on existing cgroups fail. | 
 | 2852 |  */ | 
| Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2853 | int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) | 
| Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2854 | { | 
 | 2855 | 	struct cftype_set *set; | 
 | 2856 |  | 
 | 2857 | 	set = kzalloc(sizeof(*set), GFP_KERNEL); | 
 | 2858 | 	if (!set) | 
 | 2859 | 		return -ENOMEM; | 
 | 2860 |  | 
 | 2861 | 	cgroup_cfts_prepare(); | 
 | 2862 | 	set->cfts = cfts; | 
 | 2863 | 	list_add_tail(&set->node, &ss->cftsets); | 
| Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2864 | 	cgroup_cfts_commit(ss, cfts, true); | 
| Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2865 |  | 
 | 2866 | 	return 0; | 
 | 2867 | } | 
 | 2868 | EXPORT_SYMBOL_GPL(cgroup_add_cftypes); | 
 | 2869 |  | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2870 | /** | 
| Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2871 |  * cgroup_rm_cftypes - remove an array of cftypes from a subsystem | 
 | 2872 |  * @ss: target cgroup subsystem | 
 | 2873 |  * @cfts: zero-length name terminated array of cftypes | 
 | 2874 |  * | 
 | 2875 |  * Unregister @cfts from @ss.  Files described by @cfts are removed from | 
 | 2876 |  * all existing cgroups to which @ss is attached and all future cgroups | 
 | 2877 |  * won't have them either.  This function can be called anytime whether @ss | 
 | 2878 |  * is attached or not. | 
 | 2879 |  * | 
 | 2880 |  * Returns 0 on successful unregistration, -ENOENT if @cfts is not | 
 | 2881 |  * registered with @ss. | 
 | 2882 |  */ | 
| Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2883 | int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) | 
| Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2884 | { | 
 | 2885 | 	struct cftype_set *set; | 
 | 2886 |  | 
 | 2887 | 	cgroup_cfts_prepare(); | 
 | 2888 |  | 
 | 2889 | 	list_for_each_entry(set, &ss->cftsets, node) { | 
 | 2890 | 		if (set->cfts == cfts) { | 
 | 2891 | 			list_del_init(&set->node); | 
 | 2892 | 			cgroup_cfts_commit(ss, cfts, false); | 
 | 2893 | 			return 0; | 
 | 2894 | 		} | 
 | 2895 | 	} | 
 | 2896 |  | 
 | 2897 | 	cgroup_cfts_commit(ss, NULL, false); | 
 | 2898 | 	return -ENOENT; | 
 | 2899 | } | 
 | 2900 |  | 
 | 2901 | /** | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2902 |  * cgroup_task_count - count the number of tasks in a cgroup. | 
 | 2903 |  * @cgrp: the cgroup in question | 
 | 2904 |  * | 
 | 2905 |  * Return the number of tasks in the cgroup. | 
 | 2906 |  */ | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2907 | int cgroup_task_count(const struct cgroup *cgrp) | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2908 | { | 
 | 2909 | 	int count = 0; | 
| KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 2910 | 	struct cg_cgroup_link *link; | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2911 |  | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2912 | 	read_lock(&css_set_lock); | 
| KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 2913 | 	list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) { | 
| Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 2914 | 		count += atomic_read(&link->cg->refcount); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2915 | 	} | 
 | 2916 | 	read_unlock(&css_set_lock); | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2917 | 	return count; | 
 | 2918 | } | 
 | 2919 |  | 
 | 2920 | /* | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2921 |  * Advance a list_head iterator.  The iterator should be positioned at | 
 | 2922 |  * the start of a css_set | 
 | 2923 |  */ | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2924 | static void cgroup_advance_iter(struct cgroup *cgrp, | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 2925 | 				struct cgroup_iter *it) | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2926 | { | 
 | 2927 | 	struct list_head *l = it->cg_link; | 
 | 2928 | 	struct cg_cgroup_link *link; | 
 | 2929 | 	struct css_set *cg; | 
 | 2930 |  | 
 | 2931 | 	/* Advance to the next non-empty css_set */ | 
 | 2932 | 	do { | 
 | 2933 | 		l = l->next; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2934 | 		if (l == &cgrp->css_sets) { | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2935 | 			it->cg_link = NULL; | 
 | 2936 | 			return; | 
 | 2937 | 		} | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2938 | 		link = list_entry(l, struct cg_cgroup_link, cgrp_link_list); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2939 | 		cg = link->cg; | 
 | 2940 | 	} while (list_empty(&cg->tasks)); | 
 | 2941 | 	it->cg_link = l; | 
 | 2942 | 	it->task = cg->tasks.next; | 
 | 2943 | } | 
 | 2944 |  | 
| Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2945 | /* | 
 | 2946 |  * To reduce the fork() overhead for systems that are not actually | 
 | 2947 |  * using their cgroups capability, we don't maintain the lists running | 
 | 2948 |  * through each css_set to its tasks until we see the list actually | 
 | 2949 |  * used - in other words after the first call to cgroup_iter_start(). | 
| Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2950 |  */ | 
| Adrian Bunk | 3df91fe | 2008-04-29 00:59:54 -0700 | [diff] [blame] | 2951 | static void cgroup_enable_task_cg_lists(void) | 
| Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2952 | { | 
 | 2953 | 	struct task_struct *p, *g; | 
 | 2954 | 	write_lock(&css_set_lock); | 
 | 2955 | 	use_task_css_set_links = 1; | 
| Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2956 | 	/* | 
 | 2957 | 	 * We need tasklist_lock because RCU is not safe against | 
 | 2958 | 	 * while_each_thread(). Besides, a forking task that has passed | 
 | 2959 | 	 * cgroup_post_fork() without seeing use_task_css_set_links = 1 | 
 | 2960 | 	 * is not guaranteed to have its child immediately visible in the | 
 | 2961 | 	 * tasklist if we walk through it with RCU. | 
 | 2962 | 	 */ | 
 | 2963 | 	read_lock(&tasklist_lock); | 
| Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2964 | 	do_each_thread(g, p) { | 
 | 2965 | 		task_lock(p); | 
| Li Zefan | 0e04388 | 2008-04-17 11:37:15 +0800 | [diff] [blame] | 2966 | 		/* | 
 | 2967 | 		 * We should check if the process is exiting, otherwise | 
 | 2968 | 		 * it will race with cgroup_exit() in that the list | 
 | 2969 | 		 * entry won't be deleted though the process has exited. | 
 | 2970 | 		 */ | 
 | 2971 | 		if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list)) | 
| Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2972 | 			list_add(&p->cg_list, &p->cgroups->tasks); | 
 | 2973 | 		task_unlock(p); | 
 | 2974 | 	} while_each_thread(g, p); | 
| Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2975 | 	read_unlock(&tasklist_lock); | 
| Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2976 | 	write_unlock(&css_set_lock); | 
 | 2977 | } | 
 | 2978 |  | 
| Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2979 | /** | 
 | 2980 |  * cgroup_next_descendant_pre - find the next descendant for pre-order walk | 
 | 2981 |  * @pos: the current position (%NULL to initiate traversal) | 
 | 2982 |  * @cgroup: cgroup whose descendants to walk | 
 | 2983 |  * | 
 | 2984 |  * To be used by cgroup_for_each_descendant_pre().  Find the next | 
 | 2985 |  * descendant to visit for pre-order traversal of @cgroup's descendants. | 
 | 2986 |  */ | 
 | 2987 | struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos, | 
 | 2988 | 					  struct cgroup *cgroup) | 
 | 2989 | { | 
 | 2990 | 	struct cgroup *next; | 
 | 2991 |  | 
 | 2992 | 	WARN_ON_ONCE(!rcu_read_lock_held()); | 
 | 2993 |  | 
 | 2994 | 	/* if first iteration, pretend we just visited @cgroup */ | 
 | 2995 | 	if (!pos) { | 
 | 2996 | 		if (list_empty(&cgroup->children)) | 
 | 2997 | 			return NULL; | 
 | 2998 | 		pos = cgroup; | 
 | 2999 | 	} | 
 | 3000 |  | 
 | 3001 | 	/* visit the first child if exists */ | 
 | 3002 | 	next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling); | 
 | 3003 | 	if (next) | 
 | 3004 | 		return next; | 
 | 3005 |  | 
 | 3006 | 	/* no child, visit my or the closest ancestor's next sibling */ | 
 | 3007 | 	do { | 
 | 3008 | 		next = list_entry_rcu(pos->sibling.next, struct cgroup, | 
 | 3009 | 				      sibling); | 
 | 3010 | 		if (&next->sibling != &pos->parent->children) | 
 | 3011 | 			return next; | 
 | 3012 |  | 
 | 3013 | 		pos = pos->parent; | 
 | 3014 | 	} while (pos != cgroup); | 
 | 3015 |  | 
 | 3016 | 	return NULL; | 
 | 3017 | } | 
 | 3018 | EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre); | 
 | 3019 |  | 
 | 3020 | static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos) | 
 | 3021 | { | 
 | 3022 | 	struct cgroup *last; | 
 | 3023 |  | 
 | 3024 | 	do { | 
 | 3025 | 		last = pos; | 
 | 3026 | 		pos = list_first_or_null_rcu(&pos->children, struct cgroup, | 
 | 3027 | 					     sibling); | 
 | 3028 | 	} while (pos); | 
 | 3029 |  | 
 | 3030 | 	return last; | 
 | 3031 | } | 
 | 3032 |  | 
 | 3033 | /** | 
 | 3034 |  * cgroup_next_descendant_post - find the next descendant for post-order walk | 
 | 3035 |  * @pos: the current position (%NULL to initiate traversal) | 
 | 3036 |  * @cgroup: cgroup whose descendants to walk | 
 | 3037 |  * | 
 | 3038 |  * To be used by cgroup_for_each_descendant_post().  Find the next | 
 | 3039 |  * descendant to visit for post-order traversal of @cgroup's descendants. | 
 | 3040 |  */ | 
 | 3041 | struct cgroup *cgroup_next_descendant_post(struct cgroup *pos, | 
 | 3042 | 					   struct cgroup *cgroup) | 
 | 3043 | { | 
 | 3044 | 	struct cgroup *next; | 
 | 3045 |  | 
 | 3046 | 	WARN_ON_ONCE(!rcu_read_lock_held()); | 
 | 3047 |  | 
 | 3048 | 	/* if first iteration, visit the leftmost descendant */ | 
 | 3049 | 	if (!pos) { | 
 | 3050 | 		next = cgroup_leftmost_descendant(cgroup); | 
 | 3051 | 		return next != cgroup ? next : NULL; | 
 | 3052 | 	} | 
 | 3053 |  | 
 | 3054 | 	/* if there's an unvisited sibling, visit its leftmost descendant */ | 
 | 3055 | 	next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling); | 
 | 3056 | 	if (&next->sibling != &pos->parent->children) | 
 | 3057 | 		return cgroup_leftmost_descendant(next); | 
 | 3058 |  | 
 | 3059 | 	/* no sibling left, visit parent */ | 
 | 3060 | 	next = pos->parent; | 
 | 3061 | 	return next != cgroup ? next : NULL; | 
 | 3062 | } | 
 | 3063 | EXPORT_SYMBOL_GPL(cgroup_next_descendant_post); | 
 | 3064 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3065 | void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it) | 
| Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3066 | 	__acquires(css_set_lock) | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3067 | { | 
 | 3068 | 	/* | 
 | 3069 | 	 * The first time anyone tries to iterate across a cgroup, | 
 | 3070 | 	 * we need to enable the list linking each css_set to its | 
 | 3071 | 	 * tasks, and fix up all existing tasks. | 
 | 3072 | 	 */ | 
| Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3073 | 	if (!use_task_css_set_links) | 
 | 3074 | 		cgroup_enable_task_cg_lists(); | 
 | 3075 |  | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3076 | 	read_lock(&css_set_lock); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3077 | 	it->cg_link = &cgrp->css_sets; | 
 | 3078 | 	cgroup_advance_iter(cgrp, it); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3079 | } | 
 | 3080 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3081 | struct task_struct *cgroup_iter_next(struct cgroup *cgrp, | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3082 | 					struct cgroup_iter *it) | 
 | 3083 | { | 
 | 3084 | 	struct task_struct *res; | 
 | 3085 | 	struct list_head *l = it->task; | 
| Lai Jiangshan | 2019f63 | 2009-01-07 18:07:36 -0800 | [diff] [blame] | 3086 | 	struct cg_cgroup_link *link; | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3087 |  | 
 | 3088 | 	/* If the iterator cg is NULL, we have no tasks */ | 
 | 3089 | 	if (!it->cg_link) | 
 | 3090 | 		return NULL; | 
 | 3091 | 	res = list_entry(l, struct task_struct, cg_list); | 
 | 3092 | 	/* Advance iterator to find next entry */ | 
 | 3093 | 	l = l->next; | 
| Lai Jiangshan | 2019f63 | 2009-01-07 18:07:36 -0800 | [diff] [blame] | 3094 | 	link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list); | 
 | 3095 | 	if (l == &link->cg->tasks) { | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3096 | 		/* We reached the end of this task list - move on to | 
 | 3097 | 		 * the next cg_cgroup_link */ | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3098 | 		cgroup_advance_iter(cgrp, it); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3099 | 	} else { | 
 | 3100 | 		it->task = l; | 
 | 3101 | 	} | 
 | 3102 | 	return res; | 
 | 3103 | } | 
 | 3104 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3105 | void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it) | 
| Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3106 | 	__releases(css_set_lock) | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3107 | { | 
 | 3108 | 	read_unlock(&css_set_lock); | 
 | 3109 | } | 
 | 3110 |  | 
| Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3111 | static inline int started_after_time(struct task_struct *t1, | 
 | 3112 | 				     struct timespec *time, | 
 | 3113 | 				     struct task_struct *t2) | 
 | 3114 | { | 
 | 3115 | 	int start_diff = timespec_compare(&t1->start_time, time); | 
 | 3116 | 	if (start_diff > 0) { | 
 | 3117 | 		return 1; | 
 | 3118 | 	} else if (start_diff < 0) { | 
 | 3119 | 		return 0; | 
 | 3120 | 	} else { | 
 | 3121 | 		/* | 
 | 3122 | 		 * Arbitrarily, if two processes started at the same | 
 | 3123 | 		 * time, we'll say that the lower pointer value | 
 | 3124 | 		 * started first. Note that t2 may have exited by now | 
 | 3125 | 		 * so this may not be a valid pointer any longer, but | 
 | 3126 | 		 * that's fine - it still serves to distinguish | 
 | 3127 | 		 * between two tasks started (effectively) simultaneously. | 
 | 3128 | 		 */ | 
 | 3129 | 		return t1 > t2; | 
 | 3130 | 	} | 
 | 3131 | } | 
 | 3132 |  | 
 | 3133 | /* | 
 | 3134 |  * This function is a callback from heap_insert() and is used to order | 
 | 3135 |  * the heap. | 
 | 3136 |  * In this case we order the heap in descending task start time. | 
 | 3137 |  */ | 
 | 3138 | static inline int started_after(void *p1, void *p2) | 
 | 3139 | { | 
 | 3140 | 	struct task_struct *t1 = p1; | 
 | 3141 | 	struct task_struct *t2 = p2; | 
 | 3142 | 	return started_after_time(t1, &t2->start_time, t2); | 
 | 3143 | } | 
 | 3144 |  | 
 | 3145 | /** | 
 | 3146 |  * cgroup_scan_tasks - iterate though all the tasks in a cgroup | 
 | 3147 |  * @scan: struct cgroup_scanner containing arguments for the scan | 
 | 3148 |  * | 
 | 3149 |  * Arguments include pointers to callback functions test_task() and | 
 | 3150 |  * process_task(). | 
 | 3151 |  * Iterate through all the tasks in a cgroup, calling test_task() for each, | 
 | 3152 |  * and if it returns true, call process_task() for it also. | 
 | 3153 |  * The test_task pointer may be NULL, meaning always true (select all tasks). | 
 | 3154 |  * Effectively duplicates cgroup_iter_{start,next,end}() | 
 | 3155 |  * but does not lock css_set_lock for the call to process_task(). | 
 | 3156 |  * The struct cgroup_scanner may be embedded in any structure of the caller's | 
 | 3157 |  * creation. | 
 | 3158 |  * It is guaranteed that process_task() will act on every task that | 
 | 3159 |  * is a member of the cgroup for the duration of this call. This | 
 | 3160 |  * function may or may not call process_task() for tasks that exit | 
 | 3161 |  * or move to a different cgroup during the call, or are forked or | 
 | 3162 |  * move into the cgroup during the call. | 
 | 3163 |  * | 
 | 3164 |  * Note that test_task() may be called with locks held, and may in some | 
 | 3165 |  * situations be called multiple times for the same task, so it should | 
 | 3166 |  * be cheap. | 
 | 3167 |  * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been | 
 | 3168 |  * pre-allocated and will be used for heap operations (and its "gt" member will | 
 | 3169 |  * be overwritten), else a temporary heap will be used (allocation of which | 
 | 3170 |  * may cause this function to fail). | 
 | 3171 |  */ | 
 | 3172 | int cgroup_scan_tasks(struct cgroup_scanner *scan) | 
 | 3173 | { | 
 | 3174 | 	int retval, i; | 
 | 3175 | 	struct cgroup_iter it; | 
 | 3176 | 	struct task_struct *p, *dropped; | 
 | 3177 | 	/* Never dereference latest_task, since it's not refcounted */ | 
 | 3178 | 	struct task_struct *latest_task = NULL; | 
 | 3179 | 	struct ptr_heap tmp_heap; | 
 | 3180 | 	struct ptr_heap *heap; | 
 | 3181 | 	struct timespec latest_time = { 0, 0 }; | 
 | 3182 |  | 
 | 3183 | 	if (scan->heap) { | 
 | 3184 | 		/* The caller supplied our heap and pre-allocated its memory */ | 
 | 3185 | 		heap = scan->heap; | 
 | 3186 | 		heap->gt = &started_after; | 
 | 3187 | 	} else { | 
 | 3188 | 		/* We need to allocate our own heap memory */ | 
 | 3189 | 		heap = &tmp_heap; | 
 | 3190 | 		retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after); | 
 | 3191 | 		if (retval) | 
 | 3192 | 			/* cannot allocate the heap */ | 
 | 3193 | 			return retval; | 
 | 3194 | 	} | 
 | 3195 |  | 
 | 3196 |  again: | 
 | 3197 | 	/* | 
 | 3198 | 	 * Scan tasks in the cgroup, using the scanner's "test_task" callback | 
 | 3199 | 	 * to determine which are of interest, and using the scanner's | 
 | 3200 | 	 * "process_task" callback to process any of them that need an update. | 
 | 3201 | 	 * Since we don't want to hold any locks during the task updates, | 
 | 3202 | 	 * gather tasks to be processed in a heap structure. | 
 | 3203 | 	 * The heap is sorted by descending task start time. | 
 | 3204 | 	 * If the statically-sized heap fills up, we overflow tasks that | 
 | 3205 | 	 * started later, and in future iterations only consider tasks that | 
 | 3206 | 	 * started after the latest task in the previous pass. This | 
 | 3207 | 	 * guarantees forward progress and that we don't miss any tasks. | 
 | 3208 | 	 */ | 
 | 3209 | 	heap->size = 0; | 
 | 3210 | 	cgroup_iter_start(scan->cg, &it); | 
 | 3211 | 	while ((p = cgroup_iter_next(scan->cg, &it))) { | 
 | 3212 | 		/* | 
 | 3213 | 		 * Only affect tasks that qualify per the caller's callback, | 
 | 3214 | 		 * if he provided one | 
 | 3215 | 		 */ | 
 | 3216 | 		if (scan->test_task && !scan->test_task(p, scan)) | 
 | 3217 | 			continue; | 
 | 3218 | 		/* | 
 | 3219 | 		 * Only process tasks that started after the last task | 
 | 3220 | 		 * we processed | 
 | 3221 | 		 */ | 
 | 3222 | 		if (!started_after_time(p, &latest_time, latest_task)) | 
 | 3223 | 			continue; | 
 | 3224 | 		dropped = heap_insert(heap, p); | 
 | 3225 | 		if (dropped == NULL) { | 
 | 3226 | 			/* | 
 | 3227 | 			 * The new task was inserted; the heap wasn't | 
 | 3228 | 			 * previously full | 
 | 3229 | 			 */ | 
 | 3230 | 			get_task_struct(p); | 
 | 3231 | 		} else if (dropped != p) { | 
 | 3232 | 			/* | 
 | 3233 | 			 * The new task was inserted, and pushed out a | 
 | 3234 | 			 * different task | 
 | 3235 | 			 */ | 
 | 3236 | 			get_task_struct(p); | 
 | 3237 | 			put_task_struct(dropped); | 
 | 3238 | 		} | 
 | 3239 | 		/* | 
 | 3240 | 		 * Else the new task was newer than anything already in | 
 | 3241 | 		 * the heap and wasn't inserted | 
 | 3242 | 		 */ | 
 | 3243 | 	} | 
 | 3244 | 	cgroup_iter_end(scan->cg, &it); | 
 | 3245 |  | 
 | 3246 | 	if (heap->size) { | 
 | 3247 | 		for (i = 0; i < heap->size; i++) { | 
| Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3248 | 			struct task_struct *q = heap->ptrs[i]; | 
| Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3249 | 			if (i == 0) { | 
| Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3250 | 				latest_time = q->start_time; | 
 | 3251 | 				latest_task = q; | 
| Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3252 | 			} | 
 | 3253 | 			/* Process the task per the caller's callback */ | 
| Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3254 | 			scan->process_task(q, scan); | 
 | 3255 | 			put_task_struct(q); | 
| Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3256 | 		} | 
 | 3257 | 		/* | 
 | 3258 | 		 * If we had to process any tasks at all, scan again | 
 | 3259 | 		 * in case some of them were in the middle of forking | 
 | 3260 | 		 * children that didn't get processed. | 
 | 3261 | 		 * Not the most efficient way to do it, but it avoids | 
 | 3262 | 		 * having to take callback_mutex in the fork path | 
 | 3263 | 		 */ | 
 | 3264 | 		goto again; | 
 | 3265 | 	} | 
 | 3266 | 	if (heap == &tmp_heap) | 
 | 3267 | 		heap_free(&tmp_heap); | 
 | 3268 | 	return 0; | 
 | 3269 | } | 
 | 3270 |  | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3271 | /* | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3272 |  * Stuff for reading the 'tasks'/'procs' files. | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3273 |  * | 
 | 3274 |  * Reading this file can return large amounts of data if a cgroup has | 
 | 3275 |  * *lots* of attached tasks. So it may need several calls to read(), | 
 | 3276 |  * but we cannot guarantee that the information we produce is correct | 
 | 3277 |  * unless we produce it entirely atomically. | 
 | 3278 |  * | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3279 |  */ | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3280 |  | 
| Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3281 | /* which pidlist file are we talking about? */ | 
 | 3282 | enum cgroup_filetype { | 
 | 3283 | 	CGROUP_FILE_PROCS, | 
 | 3284 | 	CGROUP_FILE_TASKS, | 
 | 3285 | }; | 
 | 3286 |  | 
 | 3287 | /* | 
 | 3288 |  * A pidlist is a list of pids that virtually represents the contents of one | 
 | 3289 |  * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists, | 
 | 3290 |  * a pair (one each for procs, tasks) for each pid namespace that's relevant | 
 | 3291 |  * to the cgroup. | 
 | 3292 |  */ | 
 | 3293 | struct cgroup_pidlist { | 
 | 3294 | 	/* | 
 | 3295 | 	 * used to find which pidlist is wanted. doesn't change as long as | 
 | 3296 | 	 * this particular list stays in the list. | 
 | 3297 | 	*/ | 
 | 3298 | 	struct { enum cgroup_filetype type; struct pid_namespace *ns; } key; | 
 | 3299 | 	/* array of xids */ | 
 | 3300 | 	pid_t *list; | 
 | 3301 | 	/* how many elements the above list has */ | 
 | 3302 | 	int length; | 
 | 3303 | 	/* how many files are using the current array */ | 
 | 3304 | 	int use_count; | 
 | 3305 | 	/* each of these stored in a list by its cgroup */ | 
 | 3306 | 	struct list_head links; | 
 | 3307 | 	/* pointer to the cgroup we belong to, for list removal purposes */ | 
 | 3308 | 	struct cgroup *owner; | 
 | 3309 | 	/* protects the other fields */ | 
 | 3310 | 	struct rw_semaphore mutex; | 
 | 3311 | }; | 
 | 3312 |  | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3313 | /* | 
| Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3314 |  * The following two functions "fix" the issue where there are more pids | 
 | 3315 |  * than kmalloc will give memory for; in such cases, we use vmalloc/vfree. | 
 | 3316 |  * TODO: replace with a kernel-wide solution to this problem | 
 | 3317 |  */ | 
 | 3318 | #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2)) | 
 | 3319 | static void *pidlist_allocate(int count) | 
 | 3320 | { | 
 | 3321 | 	if (PIDLIST_TOO_LARGE(count)) | 
 | 3322 | 		return vmalloc(count * sizeof(pid_t)); | 
 | 3323 | 	else | 
 | 3324 | 		return kmalloc(count * sizeof(pid_t), GFP_KERNEL); | 
 | 3325 | } | 
 | 3326 | static void pidlist_free(void *p) | 
 | 3327 | { | 
 | 3328 | 	if (is_vmalloc_addr(p)) | 
 | 3329 | 		vfree(p); | 
 | 3330 | 	else | 
 | 3331 | 		kfree(p); | 
 | 3332 | } | 
 | 3333 | static void *pidlist_resize(void *p, int newcount) | 
 | 3334 | { | 
 | 3335 | 	void *newlist; | 
 | 3336 | 	/* note: if new alloc fails, old p will still be valid either way */ | 
 | 3337 | 	if (is_vmalloc_addr(p)) { | 
 | 3338 | 		newlist = vmalloc(newcount * sizeof(pid_t)); | 
 | 3339 | 		if (!newlist) | 
 | 3340 | 			return NULL; | 
 | 3341 | 		memcpy(newlist, p, newcount * sizeof(pid_t)); | 
 | 3342 | 		vfree(p); | 
 | 3343 | 	} else { | 
 | 3344 | 		newlist = krealloc(p, newcount * sizeof(pid_t), GFP_KERNEL); | 
 | 3345 | 	} | 
 | 3346 | 	return newlist; | 
 | 3347 | } | 
 | 3348 |  | 
 | 3349 | /* | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3350 |  * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries | 
 | 3351 |  * If the new stripped list is sufficiently smaller and there's enough memory | 
 | 3352 |  * to allocate a new buffer, will let go of the unneeded memory. Returns the | 
 | 3353 |  * number of unique elements. | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3354 |  */ | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3355 | /* is the size difference enough that we should re-allocate the array? */ | 
 | 3356 | #define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new)) | 
 | 3357 | static int pidlist_uniq(pid_t **p, int length) | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3358 | { | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3359 | 	int src, dest = 1; | 
 | 3360 | 	pid_t *list = *p; | 
 | 3361 | 	pid_t *newlist; | 
 | 3362 |  | 
 | 3363 | 	/* | 
 | 3364 | 	 * we presume the 0th element is unique, so i starts at 1. trivial | 
 | 3365 | 	 * edge cases first; no work needs to be done for either | 
 | 3366 | 	 */ | 
 | 3367 | 	if (length == 0 || length == 1) | 
 | 3368 | 		return length; | 
 | 3369 | 	/* src and dest walk down the list; dest counts unique elements */ | 
 | 3370 | 	for (src = 1; src < length; src++) { | 
 | 3371 | 		/* find next unique element */ | 
 | 3372 | 		while (list[src] == list[src-1]) { | 
 | 3373 | 			src++; | 
 | 3374 | 			if (src == length) | 
 | 3375 | 				goto after; | 
 | 3376 | 		} | 
 | 3377 | 		/* dest always points to where the next unique element goes */ | 
 | 3378 | 		list[dest] = list[src]; | 
 | 3379 | 		dest++; | 
 | 3380 | 	} | 
 | 3381 | after: | 
 | 3382 | 	/* | 
 | 3383 | 	 * if the length difference is large enough, we want to allocate a | 
 | 3384 | 	 * smaller buffer to save memory. if this fails due to out of memory, | 
 | 3385 | 	 * we'll just stay with what we've got. | 
 | 3386 | 	 */ | 
 | 3387 | 	if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) { | 
| Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3388 | 		newlist = pidlist_resize(list, dest); | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3389 | 		if (newlist) | 
 | 3390 | 			*p = newlist; | 
 | 3391 | 	} | 
 | 3392 | 	return dest; | 
 | 3393 | } | 
 | 3394 |  | 
 | 3395 | static int cmppid(const void *a, const void *b) | 
 | 3396 | { | 
 | 3397 | 	return *(pid_t *)a - *(pid_t *)b; | 
 | 3398 | } | 
 | 3399 |  | 
 | 3400 | /* | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3401 |  * find the appropriate pidlist for our purpose (given procs vs tasks) | 
 | 3402 |  * returns with the lock on that pidlist already held, and takes care | 
 | 3403 |  * of the use count, or returns NULL with no locks held if we're out of | 
 | 3404 |  * memory. | 
 | 3405 |  */ | 
 | 3406 | static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp, | 
 | 3407 | 						  enum cgroup_filetype type) | 
 | 3408 | { | 
 | 3409 | 	struct cgroup_pidlist *l; | 
 | 3410 | 	/* don't need task_nsproxy() if we're looking at ourself */ | 
| Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 3411 | 	struct pid_namespace *ns = task_active_pid_ns(current); | 
| Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3412 |  | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3413 | 	/* | 
 | 3414 | 	 * We can't drop the pidlist_mutex before taking the l->mutex in case | 
 | 3415 | 	 * the last ref-holder is trying to remove l from the list at the same | 
 | 3416 | 	 * time. Holding the pidlist_mutex precludes somebody taking whichever | 
 | 3417 | 	 * list we find out from under us - compare release_pid_array(). | 
 | 3418 | 	 */ | 
 | 3419 | 	mutex_lock(&cgrp->pidlist_mutex); | 
 | 3420 | 	list_for_each_entry(l, &cgrp->pidlists, links) { | 
 | 3421 | 		if (l->key.type == type && l->key.ns == ns) { | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3422 | 			/* make sure l doesn't vanish out from under us */ | 
 | 3423 | 			down_write(&l->mutex); | 
 | 3424 | 			mutex_unlock(&cgrp->pidlist_mutex); | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3425 | 			return l; | 
 | 3426 | 		} | 
 | 3427 | 	} | 
 | 3428 | 	/* entry not found; create a new one */ | 
 | 3429 | 	l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL); | 
 | 3430 | 	if (!l) { | 
 | 3431 | 		mutex_unlock(&cgrp->pidlist_mutex); | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3432 | 		return l; | 
 | 3433 | 	} | 
 | 3434 | 	init_rwsem(&l->mutex); | 
 | 3435 | 	down_write(&l->mutex); | 
 | 3436 | 	l->key.type = type; | 
| Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3437 | 	l->key.ns = get_pid_ns(ns); | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3438 | 	l->use_count = 0; /* don't increment here */ | 
 | 3439 | 	l->list = NULL; | 
 | 3440 | 	l->owner = cgrp; | 
 | 3441 | 	list_add(&l->links, &cgrp->pidlists); | 
 | 3442 | 	mutex_unlock(&cgrp->pidlist_mutex); | 
 | 3443 | 	return l; | 
 | 3444 | } | 
 | 3445 |  | 
 | 3446 | /* | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3447 |  * Load a cgroup's pidarray with either procs' tgids or tasks' pids | 
 | 3448 |  */ | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3449 | static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, | 
 | 3450 | 			      struct cgroup_pidlist **lp) | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3451 | { | 
 | 3452 | 	pid_t *array; | 
 | 3453 | 	int length; | 
 | 3454 | 	int pid, n = 0; /* used for populating the array */ | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3455 | 	struct cgroup_iter it; | 
 | 3456 | 	struct task_struct *tsk; | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3457 | 	struct cgroup_pidlist *l; | 
 | 3458 |  | 
 | 3459 | 	/* | 
 | 3460 | 	 * If cgroup gets more users after we read count, we won't have | 
 | 3461 | 	 * enough space - tough.  This race is indistinguishable to the | 
 | 3462 | 	 * caller from the case that the additional cgroup users didn't | 
 | 3463 | 	 * show up until sometime later on. | 
 | 3464 | 	 */ | 
 | 3465 | 	length = cgroup_task_count(cgrp); | 
| Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3466 | 	array = pidlist_allocate(length); | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3467 | 	if (!array) | 
 | 3468 | 		return -ENOMEM; | 
 | 3469 | 	/* now, populate the array */ | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3470 | 	cgroup_iter_start(cgrp, &it); | 
 | 3471 | 	while ((tsk = cgroup_iter_next(cgrp, &it))) { | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3472 | 		if (unlikely(n == length)) | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3473 | 			break; | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3474 | 		/* get tgid or pid for procs or tasks file respectively */ | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3475 | 		if (type == CGROUP_FILE_PROCS) | 
 | 3476 | 			pid = task_tgid_vnr(tsk); | 
 | 3477 | 		else | 
 | 3478 | 			pid = task_pid_vnr(tsk); | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3479 | 		if (pid > 0) /* make sure to only use valid results */ | 
 | 3480 | 			array[n++] = pid; | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3481 | 	} | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3482 | 	cgroup_iter_end(cgrp, &it); | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3483 | 	length = n; | 
 | 3484 | 	/* now sort & (if procs) strip out duplicates */ | 
 | 3485 | 	sort(array, length, sizeof(pid_t), cmppid, NULL); | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3486 | 	if (type == CGROUP_FILE_PROCS) | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3487 | 		length = pidlist_uniq(&array, length); | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3488 | 	l = cgroup_pidlist_find(cgrp, type); | 
 | 3489 | 	if (!l) { | 
| Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3490 | 		pidlist_free(array); | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3491 | 		return -ENOMEM; | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3492 | 	} | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3493 | 	/* store array, freeing old if necessary - lock already held */ | 
| Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3494 | 	pidlist_free(l->list); | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3495 | 	l->list = array; | 
 | 3496 | 	l->length = length; | 
 | 3497 | 	l->use_count++; | 
 | 3498 | 	up_write(&l->mutex); | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3499 | 	*lp = l; | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3500 | 	return 0; | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3501 | } | 
 | 3502 |  | 
| Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3503 | /** | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3504 |  * cgroupstats_build - build and fill cgroupstats | 
| Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3505 |  * @stats: cgroupstats to fill information into | 
 | 3506 |  * @dentry: A dentry entry belonging to the cgroup for which stats have | 
 | 3507 |  * been requested. | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3508 |  * | 
 | 3509 |  * Build and fill cgroupstats so that taskstats can export it to user | 
 | 3510 |  * space. | 
| Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3511 |  */ | 
 | 3512 | int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) | 
 | 3513 | { | 
 | 3514 | 	int ret = -EINVAL; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3515 | 	struct cgroup *cgrp; | 
| Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3516 | 	struct cgroup_iter it; | 
 | 3517 | 	struct task_struct *tsk; | 
| Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3518 |  | 
| Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3519 | 	/* | 
| Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3520 | 	 * Validate dentry by checking the superblock operations, | 
 | 3521 | 	 * and make sure it's a directory. | 
| Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3522 | 	 */ | 
| Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3523 | 	if (dentry->d_sb->s_op != &cgroup_ops || | 
 | 3524 | 	    !S_ISDIR(dentry->d_inode->i_mode)) | 
| Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3525 | 		 goto err; | 
 | 3526 |  | 
 | 3527 | 	ret = 0; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3528 | 	cgrp = dentry->d_fsdata; | 
| Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3529 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3530 | 	cgroup_iter_start(cgrp, &it); | 
 | 3531 | 	while ((tsk = cgroup_iter_next(cgrp, &it))) { | 
| Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3532 | 		switch (tsk->state) { | 
 | 3533 | 		case TASK_RUNNING: | 
 | 3534 | 			stats->nr_running++; | 
 | 3535 | 			break; | 
 | 3536 | 		case TASK_INTERRUPTIBLE: | 
 | 3537 | 			stats->nr_sleeping++; | 
 | 3538 | 			break; | 
 | 3539 | 		case TASK_UNINTERRUPTIBLE: | 
 | 3540 | 			stats->nr_uninterruptible++; | 
 | 3541 | 			break; | 
 | 3542 | 		case TASK_STOPPED: | 
 | 3543 | 			stats->nr_stopped++; | 
 | 3544 | 			break; | 
 | 3545 | 		default: | 
 | 3546 | 			if (delayacct_is_task_waiting_on_io(tsk)) | 
 | 3547 | 				stats->nr_io_wait++; | 
 | 3548 | 			break; | 
 | 3549 | 		} | 
 | 3550 | 	} | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3551 | 	cgroup_iter_end(cgrp, &it); | 
| Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3552 |  | 
| Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3553 | err: | 
 | 3554 | 	return ret; | 
 | 3555 | } | 
 | 3556 |  | 
| Paul Menage | 8f3ff20 | 2009-09-23 15:56:25 -0700 | [diff] [blame] | 3557 |  | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3558 | /* | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3559 |  * 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] | 3560 |  * 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] | 3561 |  * in the cgroup->l->list array. | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3562 |  */ | 
 | 3563 |  | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3564 | static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos) | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3565 | { | 
 | 3566 | 	/* | 
 | 3567 | 	 * Initially we receive a position value that corresponds to | 
 | 3568 | 	 * one more than the last pid shown (or 0 on the first call or | 
 | 3569 | 	 * after a seek to the start). Use a binary-search to find the | 
 | 3570 | 	 * next pid to display, if any | 
 | 3571 | 	 */ | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3572 | 	struct cgroup_pidlist *l = s->private; | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3573 | 	int index = 0, pid = *pos; | 
 | 3574 | 	int *iter; | 
 | 3575 |  | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3576 | 	down_read(&l->mutex); | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3577 | 	if (pid) { | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3578 | 		int end = l->length; | 
| Stephen Rothwell | 2077776 | 2008-10-21 16:11:20 +1100 | [diff] [blame] | 3579 |  | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3580 | 		while (index < end) { | 
 | 3581 | 			int mid = (index + end) / 2; | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3582 | 			if (l->list[mid] == pid) { | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3583 | 				index = mid; | 
 | 3584 | 				break; | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3585 | 			} else if (l->list[mid] <= pid) | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3586 | 				index = mid + 1; | 
 | 3587 | 			else | 
 | 3588 | 				end = mid; | 
 | 3589 | 		} | 
 | 3590 | 	} | 
 | 3591 | 	/* If we're off the end of the array, we're done */ | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3592 | 	if (index >= l->length) | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3593 | 		return NULL; | 
 | 3594 | 	/* Update the abstract position to be the actual pid that we found */ | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3595 | 	iter = l->list + index; | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3596 | 	*pos = *iter; | 
 | 3597 | 	return iter; | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3598 | } | 
 | 3599 |  | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3600 | static void cgroup_pidlist_stop(struct seq_file *s, void *v) | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3601 | { | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3602 | 	struct cgroup_pidlist *l = s->private; | 
 | 3603 | 	up_read(&l->mutex); | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3604 | } | 
 | 3605 |  | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3606 | 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] | 3607 | { | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3608 | 	struct cgroup_pidlist *l = s->private; | 
 | 3609 | 	pid_t *p = v; | 
 | 3610 | 	pid_t *end = l->list + l->length; | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3611 | 	/* | 
 | 3612 | 	 * Advance to the next pid in the array. If this goes off the | 
 | 3613 | 	 * end, we're done | 
 | 3614 | 	 */ | 
 | 3615 | 	p++; | 
 | 3616 | 	if (p >= end) { | 
 | 3617 | 		return NULL; | 
 | 3618 | 	} else { | 
 | 3619 | 		*pos = *p; | 
 | 3620 | 		return p; | 
 | 3621 | 	} | 
 | 3622 | } | 
 | 3623 |  | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3624 | static int cgroup_pidlist_show(struct seq_file *s, void *v) | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3625 | { | 
 | 3626 | 	return seq_printf(s, "%d\n", *(int *)v); | 
 | 3627 | } | 
 | 3628 |  | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3629 | /* | 
 | 3630 |  * seq_operations functions for iterating on pidlists through seq_file - | 
 | 3631 |  * independent of whether it's tasks or procs | 
 | 3632 |  */ | 
 | 3633 | static const struct seq_operations cgroup_pidlist_seq_operations = { | 
 | 3634 | 	.start = cgroup_pidlist_start, | 
 | 3635 | 	.stop = cgroup_pidlist_stop, | 
 | 3636 | 	.next = cgroup_pidlist_next, | 
 | 3637 | 	.show = cgroup_pidlist_show, | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3638 | }; | 
 | 3639 |  | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3640 | static void cgroup_release_pid_array(struct cgroup_pidlist *l) | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3641 | { | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3642 | 	/* | 
 | 3643 | 	 * the case where we're the last user of this particular pidlist will | 
 | 3644 | 	 * have us remove it from the cgroup's list, which entails taking the | 
 | 3645 | 	 * mutex. since in pidlist_find the pidlist->lock depends on cgroup-> | 
 | 3646 | 	 * pidlist_mutex, we have to take pidlist_mutex first. | 
 | 3647 | 	 */ | 
 | 3648 | 	mutex_lock(&l->owner->pidlist_mutex); | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3649 | 	down_write(&l->mutex); | 
 | 3650 | 	BUG_ON(!l->use_count); | 
 | 3651 | 	if (!--l->use_count) { | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3652 | 		/* we're the last user if refcount is 0; remove and free */ | 
 | 3653 | 		list_del(&l->links); | 
 | 3654 | 		mutex_unlock(&l->owner->pidlist_mutex); | 
| Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3655 | 		pidlist_free(l->list); | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3656 | 		put_pid_ns(l->key.ns); | 
 | 3657 | 		up_write(&l->mutex); | 
 | 3658 | 		kfree(l); | 
 | 3659 | 		return; | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3660 | 	} | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3661 | 	mutex_unlock(&l->owner->pidlist_mutex); | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3662 | 	up_write(&l->mutex); | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3663 | } | 
 | 3664 |  | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3665 | static int cgroup_pidlist_release(struct inode *inode, struct file *file) | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3666 | { | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3667 | 	struct cgroup_pidlist *l; | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3668 | 	if (!(file->f_mode & FMODE_READ)) | 
 | 3669 | 		return 0; | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3670 | 	/* | 
 | 3671 | 	 * the seq_file will only be initialized if the file was opened for | 
 | 3672 | 	 * reading; hence we check if it's not null only in that case. | 
 | 3673 | 	 */ | 
 | 3674 | 	l = ((struct seq_file *)file->private_data)->private; | 
 | 3675 | 	cgroup_release_pid_array(l); | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3676 | 	return seq_release(inode, file); | 
 | 3677 | } | 
 | 3678 |  | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3679 | static const struct file_operations cgroup_pidlist_operations = { | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3680 | 	.read = seq_read, | 
 | 3681 | 	.llseek = seq_lseek, | 
 | 3682 | 	.write = cgroup_file_write, | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3683 | 	.release = cgroup_pidlist_release, | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3684 | }; | 
 | 3685 |  | 
 | 3686 | /* | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3687 |  * The following functions handle opens on a file that displays a pidlist | 
 | 3688 |  * (tasks or procs). Prepare an array of the process/thread IDs of whoever's | 
 | 3689 |  * in the cgroup. | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3690 |  */ | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3691 | /* helper function for the two below it */ | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3692 | static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type) | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3693 | { | 
 | 3694 | 	struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3695 | 	struct cgroup_pidlist *l; | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3696 | 	int retval; | 
 | 3697 |  | 
 | 3698 | 	/* Nothing to do for write-only files */ | 
 | 3699 | 	if (!(file->f_mode & FMODE_READ)) | 
 | 3700 | 		return 0; | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3701 |  | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3702 | 	/* have the array populated */ | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3703 | 	retval = pidlist_array_load(cgrp, type, &l); | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3704 | 	if (retval) | 
 | 3705 | 		return retval; | 
 | 3706 | 	/* configure file information */ | 
 | 3707 | 	file->f_op = &cgroup_pidlist_operations; | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3708 |  | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3709 | 	retval = seq_open(file, &cgroup_pidlist_seq_operations); | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3710 | 	if (retval) { | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3711 | 		cgroup_release_pid_array(l); | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3712 | 		return retval; | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3713 | 	} | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3714 | 	((struct seq_file *)file->private_data)->private = l; | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3715 | 	return 0; | 
 | 3716 | } | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3717 | static int cgroup_tasks_open(struct inode *unused, struct file *file) | 
 | 3718 | { | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3719 | 	return cgroup_pidlist_open(file, CGROUP_FILE_TASKS); | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3720 | } | 
 | 3721 | static int cgroup_procs_open(struct inode *unused, struct file *file) | 
 | 3722 | { | 
| Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3723 | 	return cgroup_pidlist_open(file, CGROUP_FILE_PROCS); | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3724 | } | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3725 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3726 | static u64 cgroup_read_notify_on_release(struct cgroup *cgrp, | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3727 | 					    struct cftype *cft) | 
 | 3728 | { | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3729 | 	return notify_on_release(cgrp); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3730 | } | 
 | 3731 |  | 
| Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3732 | static int cgroup_write_notify_on_release(struct cgroup *cgrp, | 
 | 3733 | 					  struct cftype *cft, | 
 | 3734 | 					  u64 val) | 
 | 3735 | { | 
 | 3736 | 	clear_bit(CGRP_RELEASABLE, &cgrp->flags); | 
 | 3737 | 	if (val) | 
 | 3738 | 		set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); | 
 | 3739 | 	else | 
 | 3740 | 		clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); | 
 | 3741 | 	return 0; | 
 | 3742 | } | 
 | 3743 |  | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3744 | /* | 
| Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3745 |  * Unregister event and free resources. | 
 | 3746 |  * | 
 | 3747 |  * Gets called from workqueue. | 
 | 3748 |  */ | 
 | 3749 | static void cgroup_event_remove(struct work_struct *work) | 
 | 3750 | { | 
 | 3751 | 	struct cgroup_event *event = container_of(work, struct cgroup_event, | 
 | 3752 | 			remove); | 
 | 3753 | 	struct cgroup *cgrp = event->cgrp; | 
 | 3754 |  | 
| Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3755 | 	event->cft->unregister_event(cgrp, event->cft, event->eventfd); | 
 | 3756 |  | 
 | 3757 | 	eventfd_ctx_put(event->eventfd); | 
| Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3758 | 	kfree(event); | 
| Kirill A. Shutemov | a0a4db5 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 3759 | 	dput(cgrp->dentry); | 
| Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3760 | } | 
 | 3761 |  | 
 | 3762 | /* | 
 | 3763 |  * Gets called on POLLHUP on eventfd when user closes it. | 
 | 3764 |  * | 
 | 3765 |  * Called with wqh->lock held and interrupts disabled. | 
 | 3766 |  */ | 
 | 3767 | static int cgroup_event_wake(wait_queue_t *wait, unsigned mode, | 
 | 3768 | 		int sync, void *key) | 
 | 3769 | { | 
 | 3770 | 	struct cgroup_event *event = container_of(wait, | 
 | 3771 | 			struct cgroup_event, wait); | 
 | 3772 | 	struct cgroup *cgrp = event->cgrp; | 
 | 3773 | 	unsigned long flags = (unsigned long)key; | 
 | 3774 |  | 
 | 3775 | 	if (flags & POLLHUP) { | 
| Changli Gao | a93d2f1 | 2010-05-07 14:33:26 +0800 | [diff] [blame] | 3776 | 		__remove_wait_queue(event->wqh, &event->wait); | 
| Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3777 | 		spin_lock(&cgrp->event_list_lock); | 
| Greg Thelen | 9718ceb | 2012-11-28 13:50:45 -0800 | [diff] [blame] | 3778 | 		list_del_init(&event->list); | 
| Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3779 | 		spin_unlock(&cgrp->event_list_lock); | 
 | 3780 | 		/* | 
 | 3781 | 		 * We are in atomic context, but cgroup_event_remove() may | 
 | 3782 | 		 * sleep, so we have to call it in workqueue. | 
 | 3783 | 		 */ | 
 | 3784 | 		schedule_work(&event->remove); | 
 | 3785 | 	} | 
 | 3786 |  | 
 | 3787 | 	return 0; | 
 | 3788 | } | 
 | 3789 |  | 
 | 3790 | static void cgroup_event_ptable_queue_proc(struct file *file, | 
 | 3791 | 		wait_queue_head_t *wqh, poll_table *pt) | 
 | 3792 | { | 
 | 3793 | 	struct cgroup_event *event = container_of(pt, | 
 | 3794 | 			struct cgroup_event, pt); | 
 | 3795 |  | 
 | 3796 | 	event->wqh = wqh; | 
 | 3797 | 	add_wait_queue(wqh, &event->wait); | 
 | 3798 | } | 
 | 3799 |  | 
 | 3800 | /* | 
 | 3801 |  * Parse input and register new cgroup event handler. | 
 | 3802 |  * | 
 | 3803 |  * Input must be in format '<event_fd> <control_fd> <args>'. | 
 | 3804 |  * Interpretation of args is defined by control file implementation. | 
 | 3805 |  */ | 
 | 3806 | static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft, | 
 | 3807 | 				      const char *buffer) | 
 | 3808 | { | 
 | 3809 | 	struct cgroup_event *event = NULL; | 
 | 3810 | 	unsigned int efd, cfd; | 
 | 3811 | 	struct file *efile = NULL; | 
 | 3812 | 	struct file *cfile = NULL; | 
 | 3813 | 	char *endp; | 
 | 3814 | 	int ret; | 
 | 3815 |  | 
 | 3816 | 	efd = simple_strtoul(buffer, &endp, 10); | 
 | 3817 | 	if (*endp != ' ') | 
 | 3818 | 		return -EINVAL; | 
 | 3819 | 	buffer = endp + 1; | 
 | 3820 |  | 
 | 3821 | 	cfd = simple_strtoul(buffer, &endp, 10); | 
 | 3822 | 	if ((*endp != ' ') && (*endp != '\0')) | 
 | 3823 | 		return -EINVAL; | 
 | 3824 | 	buffer = endp + 1; | 
 | 3825 |  | 
 | 3826 | 	event = kzalloc(sizeof(*event), GFP_KERNEL); | 
 | 3827 | 	if (!event) | 
 | 3828 | 		return -ENOMEM; | 
 | 3829 | 	event->cgrp = cgrp; | 
 | 3830 | 	INIT_LIST_HEAD(&event->list); | 
 | 3831 | 	init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc); | 
 | 3832 | 	init_waitqueue_func_entry(&event->wait, cgroup_event_wake); | 
 | 3833 | 	INIT_WORK(&event->remove, cgroup_event_remove); | 
 | 3834 |  | 
 | 3835 | 	efile = eventfd_fget(efd); | 
 | 3836 | 	if (IS_ERR(efile)) { | 
 | 3837 | 		ret = PTR_ERR(efile); | 
 | 3838 | 		goto fail; | 
 | 3839 | 	} | 
 | 3840 |  | 
 | 3841 | 	event->eventfd = eventfd_ctx_fileget(efile); | 
 | 3842 | 	if (IS_ERR(event->eventfd)) { | 
 | 3843 | 		ret = PTR_ERR(event->eventfd); | 
 | 3844 | 		goto fail; | 
 | 3845 | 	} | 
 | 3846 |  | 
 | 3847 | 	cfile = fget(cfd); | 
 | 3848 | 	if (!cfile) { | 
 | 3849 | 		ret = -EBADF; | 
 | 3850 | 		goto fail; | 
 | 3851 | 	} | 
 | 3852 |  | 
 | 3853 | 	/* the process need read permission on control file */ | 
| Al Viro | 3bfa784 | 2011-06-19 12:55:10 -0400 | [diff] [blame] | 3854 | 	/* AV: shouldn't we check that it's been opened for read instead? */ | 
 | 3855 | 	ret = inode_permission(cfile->f_path.dentry->d_inode, MAY_READ); | 
| Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3856 | 	if (ret < 0) | 
 | 3857 | 		goto fail; | 
 | 3858 |  | 
 | 3859 | 	event->cft = __file_cft(cfile); | 
 | 3860 | 	if (IS_ERR(event->cft)) { | 
 | 3861 | 		ret = PTR_ERR(event->cft); | 
 | 3862 | 		goto fail; | 
 | 3863 | 	} | 
 | 3864 |  | 
 | 3865 | 	if (!event->cft->register_event || !event->cft->unregister_event) { | 
 | 3866 | 		ret = -EINVAL; | 
 | 3867 | 		goto fail; | 
 | 3868 | 	} | 
 | 3869 |  | 
 | 3870 | 	ret = event->cft->register_event(cgrp, event->cft, | 
 | 3871 | 			event->eventfd, buffer); | 
 | 3872 | 	if (ret) | 
 | 3873 | 		goto fail; | 
 | 3874 |  | 
 | 3875 | 	if (efile->f_op->poll(efile, &event->pt) & POLLHUP) { | 
 | 3876 | 		event->cft->unregister_event(cgrp, event->cft, event->eventfd); | 
 | 3877 | 		ret = 0; | 
 | 3878 | 		goto fail; | 
 | 3879 | 	} | 
 | 3880 |  | 
| Kirill A. Shutemov | a0a4db5 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 3881 | 	/* | 
 | 3882 | 	 * Events should be removed after rmdir of cgroup directory, but before | 
 | 3883 | 	 * destroying subsystem state objects. Let's take reference to cgroup | 
 | 3884 | 	 * directory dentry to do that. | 
 | 3885 | 	 */ | 
 | 3886 | 	dget(cgrp->dentry); | 
 | 3887 |  | 
| Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3888 | 	spin_lock(&cgrp->event_list_lock); | 
 | 3889 | 	list_add(&event->list, &cgrp->event_list); | 
 | 3890 | 	spin_unlock(&cgrp->event_list_lock); | 
 | 3891 |  | 
 | 3892 | 	fput(cfile); | 
 | 3893 | 	fput(efile); | 
 | 3894 |  | 
 | 3895 | 	return 0; | 
 | 3896 |  | 
 | 3897 | fail: | 
 | 3898 | 	if (cfile) | 
 | 3899 | 		fput(cfile); | 
 | 3900 |  | 
 | 3901 | 	if (event && event->eventfd && !IS_ERR(event->eventfd)) | 
 | 3902 | 		eventfd_ctx_put(event->eventfd); | 
 | 3903 |  | 
 | 3904 | 	if (!IS_ERR_OR_NULL(efile)) | 
 | 3905 | 		fput(efile); | 
 | 3906 |  | 
 | 3907 | 	kfree(event); | 
 | 3908 |  | 
 | 3909 | 	return ret; | 
 | 3910 | } | 
 | 3911 |  | 
| Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3912 | static u64 cgroup_clone_children_read(struct cgroup *cgrp, | 
 | 3913 | 				    struct cftype *cft) | 
 | 3914 | { | 
| Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 3915 | 	return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); | 
| Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3916 | } | 
 | 3917 |  | 
 | 3918 | static int cgroup_clone_children_write(struct cgroup *cgrp, | 
 | 3919 | 				     struct cftype *cft, | 
 | 3920 | 				     u64 val) | 
 | 3921 | { | 
 | 3922 | 	if (val) | 
| Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 3923 | 		set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); | 
| Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3924 | 	else | 
| Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 3925 | 		clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); | 
| Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3926 | 	return 0; | 
 | 3927 | } | 
 | 3928 |  | 
| Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3929 | /* | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3930 |  * for the common functions, 'private' gives the type of file | 
 | 3931 |  */ | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3932 | /* for hysterical raisins, we can't put this on the older files */ | 
 | 3933 | #define CGROUP_FILE_GENERIC_PREFIX "cgroup." | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3934 | static struct cftype files[] = { | 
 | 3935 | 	{ | 
 | 3936 | 		.name = "tasks", | 
 | 3937 | 		.open = cgroup_tasks_open, | 
| Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3938 | 		.write_u64 = cgroup_tasks_write, | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3939 | 		.release = cgroup_pidlist_release, | 
| Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 3940 | 		.mode = S_IRUGO | S_IWUSR, | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3941 | 	}, | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3942 | 	{ | 
 | 3943 | 		.name = CGROUP_FILE_GENERIC_PREFIX "procs", | 
 | 3944 | 		.open = cgroup_procs_open, | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 3945 | 		.write_u64 = cgroup_procs_write, | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3946 | 		.release = cgroup_pidlist_release, | 
| Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 3947 | 		.mode = S_IRUGO | S_IWUSR, | 
| Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3948 | 	}, | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3949 | 	{ | 
 | 3950 | 		.name = "notify_on_release", | 
| Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 3951 | 		.read_u64 = cgroup_read_notify_on_release, | 
| Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3952 | 		.write_u64 = cgroup_write_notify_on_release, | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3953 | 	}, | 
| Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3954 | 	{ | 
 | 3955 | 		.name = CGROUP_FILE_GENERIC_PREFIX "event_control", | 
 | 3956 | 		.write_string = cgroup_write_event_control, | 
 | 3957 | 		.mode = S_IWUGO, | 
 | 3958 | 	}, | 
| Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3959 | 	{ | 
 | 3960 | 		.name = "cgroup.clone_children", | 
 | 3961 | 		.read_u64 = cgroup_clone_children_read, | 
 | 3962 | 		.write_u64 = cgroup_clone_children_write, | 
 | 3963 | 	}, | 
| Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3964 | 	{ | 
 | 3965 | 		.name = "release_agent", | 
 | 3966 | 		.flags = CFTYPE_ONLY_ON_ROOT, | 
 | 3967 | 		.read_seq_string = cgroup_release_agent_show, | 
 | 3968 | 		.write_string = cgroup_release_agent_write, | 
 | 3969 | 		.max_write_len = PATH_MAX, | 
 | 3970 | 	}, | 
| Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3971 | 	{ }	/* terminate */ | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3972 | }; | 
 | 3973 |  | 
| Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3974 | /** | 
 | 3975 |  * cgroup_populate_dir - selectively creation of files in a directory | 
 | 3976 |  * @cgrp: target cgroup | 
 | 3977 |  * @base_files: true if the base files should be added | 
 | 3978 |  * @subsys_mask: mask of the subsystem ids whose files should be added | 
 | 3979 |  */ | 
 | 3980 | static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files, | 
 | 3981 | 			       unsigned long subsys_mask) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3982 | { | 
 | 3983 | 	int err; | 
 | 3984 | 	struct cgroup_subsys *ss; | 
 | 3985 |  | 
| Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3986 | 	if (base_files) { | 
 | 3987 | 		err = cgroup_addrm_files(cgrp, NULL, files, true); | 
 | 3988 | 		if (err < 0) | 
 | 3989 | 			return err; | 
 | 3990 | 	} | 
| Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3991 |  | 
| Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3992 | 	/* process cftsets of each subsystem */ | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3993 | 	for_each_subsys(cgrp->root, ss) { | 
| Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3994 | 		struct cftype_set *set; | 
| Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3995 | 		if (!test_bit(ss->subsys_id, &subsys_mask)) | 
 | 3996 | 			continue; | 
| Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3997 |  | 
| Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3998 | 		list_for_each_entry(set, &ss->cftsets, node) | 
| Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 3999 | 			cgroup_addrm_files(cgrp, ss, set->cfts, true); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4000 | 	} | 
| Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4001 |  | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4002 | 	/* This cgroup is ready now */ | 
 | 4003 | 	for_each_subsys(cgrp->root, ss) { | 
 | 4004 | 		struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; | 
 | 4005 | 		/* | 
 | 4006 | 		 * Update id->css pointer and make this css visible from | 
 | 4007 | 		 * CSS ID functions. This pointer will be dereferened | 
 | 4008 | 		 * from RCU-read-side without locks. | 
 | 4009 | 		 */ | 
 | 4010 | 		if (css->id) | 
 | 4011 | 			rcu_assign_pointer(css->id->css, css); | 
 | 4012 | 	} | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4013 |  | 
 | 4014 | 	return 0; | 
 | 4015 | } | 
 | 4016 |  | 
| Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4017 | static void css_dput_fn(struct work_struct *work) | 
 | 4018 | { | 
 | 4019 | 	struct cgroup_subsys_state *css = | 
 | 4020 | 		container_of(work, struct cgroup_subsys_state, dput_work); | 
| Tejun Heo | 5db9a4d | 2012-07-07 16:08:18 -0700 | [diff] [blame] | 4021 | 	struct dentry *dentry = css->cgroup->dentry; | 
 | 4022 | 	struct super_block *sb = dentry->d_sb; | 
| Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4023 |  | 
| Tejun Heo | 5db9a4d | 2012-07-07 16:08:18 -0700 | [diff] [blame] | 4024 | 	atomic_inc(&sb->s_active); | 
 | 4025 | 	dput(dentry); | 
 | 4026 | 	deactivate_super(sb); | 
| Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4027 | } | 
 | 4028 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4029 | static void init_cgroup_css(struct cgroup_subsys_state *css, | 
 | 4030 | 			       struct cgroup_subsys *ss, | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4031 | 			       struct cgroup *cgrp) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4032 | { | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4033 | 	css->cgroup = cgrp; | 
| Paul Menage | e7c5ec9 | 2009-01-07 18:08:38 -0800 | [diff] [blame] | 4034 | 	atomic_set(&css->refcnt, 1); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4035 | 	css->flags = 0; | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4036 | 	css->id = NULL; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4037 | 	if (cgrp == dummytop) | 
| Tejun Heo | 38b53ab | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4038 | 		css->flags |= CSS_ROOT; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4039 | 	BUG_ON(cgrp->subsys[ss->subsys_id]); | 
 | 4040 | 	cgrp->subsys[ss->subsys_id] = css; | 
| Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4041 |  | 
 | 4042 | 	/* | 
| Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4043 | 	 * css holds an extra ref to @cgrp->dentry which is put on the last | 
 | 4044 | 	 * css_put().  dput() requires process context, which css_put() may | 
 | 4045 | 	 * be called without.  @css->dput_work will be used to invoke | 
 | 4046 | 	 * dput() asynchronously from css_put(). | 
| Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4047 | 	 */ | 
 | 4048 | 	INIT_WORK(&css->dput_work, css_dput_fn); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4049 | } | 
 | 4050 |  | 
| Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4051 | /* invoke ->post_create() on a new CSS and mark it online if successful */ | 
 | 4052 | static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp) | 
| Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4053 | { | 
| Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4054 | 	int ret = 0; | 
 | 4055 |  | 
| Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4056 | 	lockdep_assert_held(&cgroup_mutex); | 
 | 4057 |  | 
| Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4058 | 	if (ss->css_online) | 
 | 4059 | 		ret = ss->css_online(cgrp); | 
| Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4060 | 	if (!ret) | 
 | 4061 | 		cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE; | 
 | 4062 | 	return ret; | 
| Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4063 | } | 
 | 4064 |  | 
 | 4065 | /* if the CSS is online, invoke ->pre_destory() on it and mark it offline */ | 
 | 4066 | static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp) | 
 | 4067 | 	__releases(&cgroup_mutex) __acquires(&cgroup_mutex) | 
 | 4068 | { | 
 | 4069 | 	struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; | 
 | 4070 |  | 
 | 4071 | 	lockdep_assert_held(&cgroup_mutex); | 
 | 4072 |  | 
 | 4073 | 	if (!(css->flags & CSS_ONLINE)) | 
 | 4074 | 		return; | 
 | 4075 |  | 
 | 4076 | 	/* | 
| Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4077 | 	 * css_offline() should be called with cgroup_mutex unlocked.  See | 
| Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4078 | 	 * 3fa59dfbc3 ("cgroup: fix potential deadlock in pre_destroy") for | 
 | 4079 | 	 * details.  This temporary unlocking should go away once | 
 | 4080 | 	 * cgroup_mutex is unexported from controllers. | 
 | 4081 | 	 */ | 
| Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4082 | 	if (ss->css_offline) { | 
| Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4083 | 		mutex_unlock(&cgroup_mutex); | 
| Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4084 | 		ss->css_offline(cgrp); | 
| Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4085 | 		mutex_lock(&cgroup_mutex); | 
 | 4086 | 	} | 
 | 4087 |  | 
 | 4088 | 	cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE; | 
 | 4089 | } | 
 | 4090 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4091 | /* | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4092 |  * cgroup_create - create a cgroup | 
 | 4093 |  * @parent: cgroup that will be parent of the new cgroup | 
 | 4094 |  * @dentry: dentry of the new cgroup | 
 | 4095 |  * @mode: mode to set on new inode | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4096 |  * | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4097 |  * Must be called with the mutex on the parent inode held | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4098 |  */ | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4099 | static long cgroup_create(struct cgroup *parent, struct dentry *dentry, | 
| Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 4100 | 			     umode_t mode) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4101 | { | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4102 | 	struct cgroup *cgrp; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4103 | 	struct cgroupfs_root *root = parent->root; | 
 | 4104 | 	int err = 0; | 
 | 4105 | 	struct cgroup_subsys *ss; | 
 | 4106 | 	struct super_block *sb = root->sb; | 
 | 4107 |  | 
| Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4108 | 	/* allocate the cgroup and its ID, 0 is reserved for the root */ | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4109 | 	cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL); | 
 | 4110 | 	if (!cgrp) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4111 | 		return -ENOMEM; | 
 | 4112 |  | 
| Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4113 | 	cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL); | 
 | 4114 | 	if (cgrp->id < 0) | 
 | 4115 | 		goto err_free_cgrp; | 
 | 4116 |  | 
| Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4117 | 	/* | 
 | 4118 | 	 * Only live parents can have children.  Note that the liveliness | 
 | 4119 | 	 * check isn't strictly necessary because cgroup_mkdir() and | 
 | 4120 | 	 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it | 
 | 4121 | 	 * anyway so that locking is contained inside cgroup proper and we | 
 | 4122 | 	 * don't get nasty surprises if we ever grow another caller. | 
 | 4123 | 	 */ | 
 | 4124 | 	if (!cgroup_lock_live_group(parent)) { | 
 | 4125 | 		err = -ENODEV; | 
| Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4126 | 		goto err_free_id; | 
| Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4127 | 	} | 
 | 4128 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4129 | 	/* Grab a reference on the superblock so the hierarchy doesn't | 
 | 4130 | 	 * get deleted on unmount if there are child cgroups.  This | 
 | 4131 | 	 * can be done outside cgroup_mutex, since the sb can't | 
 | 4132 | 	 * disappear while someone has an open control file on the | 
 | 4133 | 	 * fs */ | 
 | 4134 | 	atomic_inc(&sb->s_active); | 
 | 4135 |  | 
| Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 4136 | 	init_cgroup_housekeeping(cgrp); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4137 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4138 | 	cgrp->parent = parent; | 
 | 4139 | 	cgrp->root = parent->root; | 
 | 4140 | 	cgrp->top_cgroup = parent->top_cgroup; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4141 |  | 
| Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 4142 | 	if (notify_on_release(parent)) | 
 | 4143 | 		set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); | 
 | 4144 |  | 
| Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4145 | 	if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) | 
 | 4146 | 		set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); | 
| Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4147 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4148 | 	for_each_subsys(root, ss) { | 
| Tejun Heo | 8c7f6ed | 2012-09-13 12:20:58 -0700 | [diff] [blame] | 4149 | 		struct cgroup_subsys_state *css; | 
| Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4150 |  | 
| Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4151 | 		css = ss->css_alloc(cgrp); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4152 | 		if (IS_ERR(css)) { | 
 | 4153 | 			err = PTR_ERR(css); | 
| Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4154 | 			goto err_free_all; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4155 | 		} | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4156 | 		init_cgroup_css(css, ss, cgrp); | 
| Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4157 | 		if (ss->use_id) { | 
 | 4158 | 			err = alloc_css_id(ss, parent, cgrp); | 
 | 4159 | 			if (err) | 
| Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4160 | 				goto err_free_all; | 
| Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4161 | 		} | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4162 | 	} | 
 | 4163 |  | 
| Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4164 | 	/* | 
 | 4165 | 	 * Create directory.  cgroup_create_file() returns with the new | 
 | 4166 | 	 * directory locked on success so that it can be populated without | 
 | 4167 | 	 * dropping cgroup_mutex. | 
 | 4168 | 	 */ | 
| Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4169 | 	err = cgroup_create_file(dentry, S_IFDIR | mode, sb); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4170 | 	if (err < 0) | 
| Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4171 | 		goto err_free_all; | 
| Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4172 | 	lockdep_assert_held(&dentry->d_inode->i_mutex); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4173 |  | 
| Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4174 | 	/* allocation complete, commit to creation */ | 
| Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4175 | 	dentry->d_fsdata = cgrp; | 
| Tejun Heo | febfcef | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4176 | 	cgrp->dentry = dentry; | 
| Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4177 | 	list_add_tail(&cgrp->allcg_node, &root->allcg_list); | 
 | 4178 | 	list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children); | 
 | 4179 | 	root->number_of_cgroups++; | 
| Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4180 |  | 
| Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4181 | 	/* each css holds a ref to the cgroup's dentry */ | 
 | 4182 | 	for_each_subsys(root, ss) | 
| Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4183 | 		dget(dentry); | 
| Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4184 |  | 
| Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4185 | 	/* creation succeeded, notify subsystems */ | 
 | 4186 | 	for_each_subsys(root, ss) { | 
 | 4187 | 		err = online_css(ss, cgrp); | 
 | 4188 | 		if (err) | 
 | 4189 | 			goto err_destroy; | 
| Glauber Costa | 1f869e8 | 2012-11-30 17:31:23 +0400 | [diff] [blame] | 4190 |  | 
 | 4191 | 		if (ss->broken_hierarchy && !ss->warned_broken_hierarchy && | 
 | 4192 | 		    parent->parent) { | 
 | 4193 | 			pr_warning("cgroup: %s (%d) created nested cgroup for controller \"%s\" which has incomplete hierarchy support. Nested cgroups may change behavior in the future.\n", | 
 | 4194 | 				   current->comm, current->pid, ss->name); | 
 | 4195 | 			if (!strcmp(ss->name, "memory")) | 
 | 4196 | 				pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n"); | 
 | 4197 | 			ss->warned_broken_hierarchy = true; | 
 | 4198 | 		} | 
| Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4199 | 	} | 
 | 4200 |  | 
| Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 4201 | 	err = cgroup_populate_dir(cgrp, true, root->subsys_mask); | 
| Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4202 | 	if (err) | 
 | 4203 | 		goto err_destroy; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4204 |  | 
 | 4205 | 	mutex_unlock(&cgroup_mutex); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4206 | 	mutex_unlock(&cgrp->dentry->d_inode->i_mutex); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4207 |  | 
 | 4208 | 	return 0; | 
 | 4209 |  | 
| Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4210 | err_free_all: | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4211 | 	for_each_subsys(root, ss) { | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4212 | 		if (cgrp->subsys[ss->subsys_id]) | 
| Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4213 | 			ss->css_free(cgrp); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4214 | 	} | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4215 | 	mutex_unlock(&cgroup_mutex); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4216 | 	/* Release the reference count that we took on the superblock */ | 
 | 4217 | 	deactivate_super(sb); | 
| Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4218 | err_free_id: | 
 | 4219 | 	ida_simple_remove(&root->cgroup_ida, cgrp->id); | 
| Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4220 | err_free_cgrp: | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4221 | 	kfree(cgrp); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4222 | 	return err; | 
| Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4223 |  | 
 | 4224 | err_destroy: | 
 | 4225 | 	cgroup_destroy_locked(cgrp); | 
 | 4226 | 	mutex_unlock(&cgroup_mutex); | 
 | 4227 | 	mutex_unlock(&dentry->d_inode->i_mutex); | 
 | 4228 | 	return err; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4229 | } | 
 | 4230 |  | 
| Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 4231 | 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] | 4232 | { | 
 | 4233 | 	struct cgroup *c_parent = dentry->d_parent->d_fsdata; | 
 | 4234 |  | 
 | 4235 | 	/* the vfs holds inode->i_mutex already */ | 
 | 4236 | 	return cgroup_create(c_parent, dentry, mode | S_IFDIR); | 
 | 4237 | } | 
 | 4238 |  | 
| Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4239 | /* | 
 | 4240 |  * Check the reference count on each subsystem. Since we already | 
 | 4241 |  * established that there are no tasks in the cgroup, if the css refcount | 
 | 4242 |  * is also 1, then there should be no outstanding references, so the | 
 | 4243 |  * subsystem is safe to destroy. We scan across all subsystems rather than | 
 | 4244 |  * using the per-hierarchy linked list of mounted subsystems since we can | 
 | 4245 |  * be called via check_for_release() with no synchronization other than | 
 | 4246 |  * RCU, and the subsystem linked list isn't RCU-safe. | 
 | 4247 |  */ | 
| Li Zefan | 55b6fd0 | 2008-07-29 22:33:20 -0700 | [diff] [blame] | 4248 | static int cgroup_has_css_refs(struct cgroup *cgrp) | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4249 | { | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4250 | 	int i; | 
| Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4251 |  | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4252 | 	/* | 
 | 4253 | 	 * We won't need to lock the subsys array, because the subsystems | 
 | 4254 | 	 * we're concerned about aren't going anywhere since our cgroup root | 
 | 4255 | 	 * has a reference on them. | 
 | 4256 | 	 */ | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4257 | 	for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { | 
 | 4258 | 		struct cgroup_subsys *ss = subsys[i]; | 
 | 4259 | 		struct cgroup_subsys_state *css; | 
| Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4260 |  | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4261 | 		/* Skip subsystems not present or not in this hierarchy */ | 
 | 4262 | 		if (ss == NULL || ss->root != cgrp->root) | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4263 | 			continue; | 
| Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4264 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4265 | 		css = cgrp->subsys[ss->subsys_id]; | 
| Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4266 | 		/* | 
 | 4267 | 		 * When called from check_for_release() it's possible | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4268 | 		 * that by this point the cgroup has been removed | 
 | 4269 | 		 * and the css deleted. But a false-positive doesn't | 
 | 4270 | 		 * matter, since it can only happen if the cgroup | 
 | 4271 | 		 * has been deleted and hence no longer needs the | 
| Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4272 | 		 * release agent to be called anyway. | 
 | 4273 | 		 */ | 
 | 4274 | 		if (css && css_refcnt(css) > 1) | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4275 | 			return 1; | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4276 | 	} | 
 | 4277 | 	return 0; | 
 | 4278 | } | 
 | 4279 |  | 
| Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4280 | static int cgroup_destroy_locked(struct cgroup *cgrp) | 
 | 4281 | 	__releases(&cgroup_mutex) __acquires(&cgroup_mutex) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4282 | { | 
| Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4283 | 	struct dentry *d = cgrp->dentry; | 
 | 4284 | 	struct cgroup *parent = cgrp->parent; | 
| KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 4285 | 	DEFINE_WAIT(wait); | 
| Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4286 | 	struct cgroup_event *event, *tmp; | 
| Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4287 | 	struct cgroup_subsys *ss; | 
| Greg Thelen | 205a872 | 2012-11-28 13:50:44 -0800 | [diff] [blame] | 4288 | 	LIST_HEAD(tmp_list); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4289 |  | 
| Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4290 | 	lockdep_assert_held(&d->d_inode->i_mutex); | 
 | 4291 | 	lockdep_assert_held(&cgroup_mutex); | 
 | 4292 |  | 
 | 4293 | 	if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4294 | 		return -EBUSY; | 
| Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4295 |  | 
| Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4296 | 	/* | 
 | 4297 | 	 * Block new css_tryget() by deactivating refcnt and mark @cgrp | 
 | 4298 | 	 * removed.  This makes future css_tryget() and child creation | 
 | 4299 | 	 * attempts fail thus maintaining the removal conditions verified | 
 | 4300 | 	 * above. | 
 | 4301 | 	 */ | 
| Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4302 | 	for_each_subsys(cgrp->root, ss) { | 
 | 4303 | 		struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; | 
 | 4304 |  | 
 | 4305 | 		WARN_ON(atomic_read(&css->refcnt) < 0); | 
 | 4306 | 		atomic_add(CSS_DEACT_BIAS, &css->refcnt); | 
| KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 4307 | 	} | 
| Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4308 | 	set_bit(CGRP_REMOVED, &cgrp->flags); | 
 | 4309 |  | 
| Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4310 | 	/* tell subsystems to initate destruction */ | 
| Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4311 | 	for_each_subsys(cgrp->root, ss) | 
| Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4312 | 		offline_css(ss, cgrp); | 
| Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4313 |  | 
 | 4314 | 	/* | 
| Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4315 | 	 * Put all the base refs.  Each css holds an extra reference to the | 
 | 4316 | 	 * cgroup's dentry and cgroup removal proceeds regardless of css | 
 | 4317 | 	 * refs.  On the last put of each css, whenever that may be, the | 
 | 4318 | 	 * extra dentry ref is put so that dentry destruction happens only | 
 | 4319 | 	 * after all css's are released. | 
 | 4320 | 	 */ | 
| Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4321 | 	for_each_subsys(cgrp->root, ss) | 
 | 4322 | 		css_put(cgrp->subsys[ss->subsys_id]); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4323 |  | 
| Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4324 | 	raw_spin_lock(&release_list_lock); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4325 | 	if (!list_empty(&cgrp->release_list)) | 
| Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 4326 | 		list_del_init(&cgrp->release_list); | 
| Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4327 | 	raw_spin_unlock(&release_list_lock); | 
| Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 4328 |  | 
| Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 4329 | 	/* delete this cgroup from parent->children */ | 
| Tejun Heo | eb6fd50 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4330 | 	list_del_rcu(&cgrp->sibling); | 
| Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 4331 | 	list_del_init(&cgrp->allcg_node); | 
 | 4332 |  | 
| Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4333 | 	dget(d); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4334 | 	cgroup_d_remove_dir(d); | 
 | 4335 | 	dput(d); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4336 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4337 | 	set_bit(CGRP_RELEASABLE, &parent->flags); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4338 | 	check_for_release(parent); | 
 | 4339 |  | 
| Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4340 | 	/* | 
 | 4341 | 	 * Unregister events and notify userspace. | 
 | 4342 | 	 * Notify userspace about cgroup removing only after rmdir of cgroup | 
| Greg Thelen | 205a872 | 2012-11-28 13:50:44 -0800 | [diff] [blame] | 4343 | 	 * directory to avoid race between userspace and kernelspace. Use | 
 | 4344 | 	 * a temporary list to avoid a deadlock with cgroup_event_wake(). Since | 
 | 4345 | 	 * cgroup_event_wake() is called with the wait queue head locked, | 
 | 4346 | 	 * remove_wait_queue() cannot be called while holding event_list_lock. | 
| Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4347 | 	 */ | 
 | 4348 | 	spin_lock(&cgrp->event_list_lock); | 
| Greg Thelen | 205a872 | 2012-11-28 13:50:44 -0800 | [diff] [blame] | 4349 | 	list_splice_init(&cgrp->event_list, &tmp_list); | 
 | 4350 | 	spin_unlock(&cgrp->event_list_lock); | 
 | 4351 | 	list_for_each_entry_safe(event, tmp, &tmp_list, list) { | 
| Greg Thelen | 9718ceb | 2012-11-28 13:50:45 -0800 | [diff] [blame] | 4352 | 		list_del_init(&event->list); | 
| Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4353 | 		remove_wait_queue(event->wqh, &event->wait); | 
 | 4354 | 		eventfd_signal(event->eventfd, 1); | 
 | 4355 | 		schedule_work(&event->remove); | 
 | 4356 | 	} | 
| Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4357 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4358 | 	return 0; | 
 | 4359 | } | 
 | 4360 |  | 
| Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4361 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry) | 
 | 4362 | { | 
 | 4363 | 	int ret; | 
 | 4364 |  | 
 | 4365 | 	mutex_lock(&cgroup_mutex); | 
 | 4366 | 	ret = cgroup_destroy_locked(dentry->d_fsdata); | 
 | 4367 | 	mutex_unlock(&cgroup_mutex); | 
 | 4368 |  | 
 | 4369 | 	return ret; | 
 | 4370 | } | 
 | 4371 |  | 
| Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4372 | static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss) | 
 | 4373 | { | 
 | 4374 | 	INIT_LIST_HEAD(&ss->cftsets); | 
 | 4375 |  | 
 | 4376 | 	/* | 
 | 4377 | 	 * base_cftset is embedded in subsys itself, no need to worry about | 
 | 4378 | 	 * deregistration. | 
 | 4379 | 	 */ | 
 | 4380 | 	if (ss->base_cftypes) { | 
 | 4381 | 		ss->base_cftset.cfts = ss->base_cftypes; | 
 | 4382 | 		list_add_tail(&ss->base_cftset.node, &ss->cftsets); | 
 | 4383 | 	} | 
 | 4384 | } | 
 | 4385 |  | 
| Li Zefan | 06a1192 | 2008-04-29 01:00:07 -0700 | [diff] [blame] | 4386 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss) | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4387 | { | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4388 | 	struct cgroup_subsys_state *css; | 
| Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4389 |  | 
 | 4390 | 	printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4391 |  | 
| Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4392 | 	mutex_lock(&cgroup_mutex); | 
 | 4393 |  | 
| Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4394 | 	/* init base cftset */ | 
 | 4395 | 	cgroup_init_cftsets(ss); | 
 | 4396 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4397 | 	/* Create the top cgroup state for this subsystem */ | 
| Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 4398 | 	list_add(&ss->sibling, &rootnode.subsys_list); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4399 | 	ss->root = &rootnode; | 
| Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4400 | 	css = ss->css_alloc(dummytop); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4401 | 	/* We don't handle early failures gracefully */ | 
 | 4402 | 	BUG_ON(IS_ERR(css)); | 
 | 4403 | 	init_cgroup_css(css, ss, dummytop); | 
 | 4404 |  | 
| Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4405 | 	/* Update the init_css_set to contain a subsys | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4406 | 	 * pointer to this state - since the subsystem is | 
| Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4407 | 	 * newly registered, all tasks and hence the | 
 | 4408 | 	 * init_css_set is in the subsystem's top cgroup. */ | 
| Tejun Heo | b48c6a8 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4409 | 	init_css_set.subsys[ss->subsys_id] = css; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4410 |  | 
 | 4411 | 	need_forkexit_callback |= ss->fork || ss->exit; | 
 | 4412 |  | 
| Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4413 | 	/* At system boot, before all subsystems have been | 
 | 4414 | 	 * registered, no tasks have been forked, so we don't | 
 | 4415 | 	 * need to invoke fork callbacks here. */ | 
 | 4416 | 	BUG_ON(!list_empty(&init_task.tasks)); | 
 | 4417 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4418 | 	ss->active = 1; | 
| Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4419 | 	BUG_ON(online_css(ss, dummytop)); | 
| Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4420 |  | 
| Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4421 | 	mutex_unlock(&cgroup_mutex); | 
 | 4422 |  | 
| Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4423 | 	/* this function shouldn't be used with modular subsystems, since they | 
 | 4424 | 	 * need to register a subsys_id, among other things */ | 
 | 4425 | 	BUG_ON(ss->module); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4426 | } | 
 | 4427 |  | 
 | 4428 | /** | 
| Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4429 |  * cgroup_load_subsys: load and register a modular subsystem at runtime | 
 | 4430 |  * @ss: the subsystem to load | 
 | 4431 |  * | 
 | 4432 |  * 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] | 4433 |  * 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] | 4434 |  * up for use. If the subsystem is built-in anyway, work is delegated to the | 
 | 4435 |  * simpler cgroup_init_subsys. | 
 | 4436 |  */ | 
 | 4437 | int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss) | 
 | 4438 | { | 
| Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4439 | 	struct cgroup_subsys_state *css; | 
| Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4440 | 	int i, ret; | 
| Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4441 |  | 
 | 4442 | 	/* check name and function validity */ | 
 | 4443 | 	if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN || | 
| Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4444 | 	    ss->css_alloc == NULL || ss->css_free == NULL) | 
| Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4445 | 		return -EINVAL; | 
 | 4446 |  | 
 | 4447 | 	/* | 
 | 4448 | 	 * we don't support callbacks in modular subsystems. this check is | 
 | 4449 | 	 * before the ss->module check for consistency; a subsystem that could | 
 | 4450 | 	 * be a module should still have no callbacks even if the user isn't | 
 | 4451 | 	 * compiling it as one. | 
 | 4452 | 	 */ | 
 | 4453 | 	if (ss->fork || ss->exit) | 
 | 4454 | 		return -EINVAL; | 
 | 4455 |  | 
 | 4456 | 	/* | 
 | 4457 | 	 * an optionally modular subsystem is built-in: we want to do nothing, | 
 | 4458 | 	 * since cgroup_init_subsys will have already taken care of it. | 
 | 4459 | 	 */ | 
 | 4460 | 	if (ss->module == NULL) { | 
| Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4461 | 		/* a sanity check */ | 
| Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4462 | 		BUG_ON(subsys[ss->subsys_id] != ss); | 
 | 4463 | 		return 0; | 
 | 4464 | 	} | 
 | 4465 |  | 
| Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4466 | 	/* init base cftset */ | 
 | 4467 | 	cgroup_init_cftsets(ss); | 
 | 4468 |  | 
| Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4469 | 	mutex_lock(&cgroup_mutex); | 
| Daniel Wagner | 8a8e04d | 2012-09-12 16:12:07 +0200 | [diff] [blame] | 4470 | 	subsys[ss->subsys_id] = ss; | 
| Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4471 |  | 
 | 4472 | 	/* | 
| Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4473 | 	 * no ss->css_alloc seems to need anything important in the ss | 
 | 4474 | 	 * struct, so this can happen first (i.e. before the rootnode | 
 | 4475 | 	 * attachment). | 
| Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4476 | 	 */ | 
| Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4477 | 	css = ss->css_alloc(dummytop); | 
| Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4478 | 	if (IS_ERR(css)) { | 
 | 4479 | 		/* failure case - need to deassign the subsys[] slot. */ | 
| Daniel Wagner | 8a8e04d | 2012-09-12 16:12:07 +0200 | [diff] [blame] | 4480 | 		subsys[ss->subsys_id] = NULL; | 
| Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4481 | 		mutex_unlock(&cgroup_mutex); | 
 | 4482 | 		return PTR_ERR(css); | 
 | 4483 | 	} | 
 | 4484 |  | 
 | 4485 | 	list_add(&ss->sibling, &rootnode.subsys_list); | 
 | 4486 | 	ss->root = &rootnode; | 
 | 4487 |  | 
 | 4488 | 	/* our new subsystem will be attached to the dummy hierarchy. */ | 
 | 4489 | 	init_cgroup_css(css, ss, dummytop); | 
 | 4490 | 	/* init_idr must be after init_cgroup_css because it sets css->id. */ | 
 | 4491 | 	if (ss->use_id) { | 
| Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4492 | 		ret = cgroup_init_idr(ss, css); | 
 | 4493 | 		if (ret) | 
 | 4494 | 			goto err_unload; | 
| Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4495 | 	} | 
 | 4496 |  | 
 | 4497 | 	/* | 
 | 4498 | 	 * Now we need to entangle the css into the existing css_sets. unlike | 
 | 4499 | 	 * in cgroup_init_subsys, there are now multiple css_sets, so each one | 
 | 4500 | 	 * will need a new pointer to it; done by iterating the css_set_table. | 
 | 4501 | 	 * furthermore, modifying the existing css_sets will corrupt the hash | 
 | 4502 | 	 * table state, so each changed css_set will need its hash recomputed. | 
 | 4503 | 	 * this is all done under the css_set_lock. | 
 | 4504 | 	 */ | 
 | 4505 | 	write_lock(&css_set_lock); | 
 | 4506 | 	for (i = 0; i < CSS_SET_TABLE_SIZE; i++) { | 
 | 4507 | 		struct css_set *cg; | 
 | 4508 | 		struct hlist_node *node, *tmp; | 
 | 4509 | 		struct hlist_head *bucket = &css_set_table[i], *new_bucket; | 
 | 4510 |  | 
 | 4511 | 		hlist_for_each_entry_safe(cg, node, tmp, bucket, hlist) { | 
 | 4512 | 			/* skip entries that we already rehashed */ | 
 | 4513 | 			if (cg->subsys[ss->subsys_id]) | 
 | 4514 | 				continue; | 
 | 4515 | 			/* remove existing entry */ | 
 | 4516 | 			hlist_del(&cg->hlist); | 
 | 4517 | 			/* set new value */ | 
 | 4518 | 			cg->subsys[ss->subsys_id] = css; | 
 | 4519 | 			/* recompute hash and restore entry */ | 
 | 4520 | 			new_bucket = css_set_hash(cg->subsys); | 
 | 4521 | 			hlist_add_head(&cg->hlist, new_bucket); | 
 | 4522 | 		} | 
 | 4523 | 	} | 
 | 4524 | 	write_unlock(&css_set_lock); | 
 | 4525 |  | 
| Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4526 | 	ss->active = 1; | 
| Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4527 | 	ret = online_css(ss, dummytop); | 
 | 4528 | 	if (ret) | 
 | 4529 | 		goto err_unload; | 
| Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4530 |  | 
| Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4531 | 	/* success! */ | 
 | 4532 | 	mutex_unlock(&cgroup_mutex); | 
 | 4533 | 	return 0; | 
| Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4534 |  | 
 | 4535 | err_unload: | 
 | 4536 | 	mutex_unlock(&cgroup_mutex); | 
 | 4537 | 	/* @ss can't be mounted here as try_module_get() would fail */ | 
 | 4538 | 	cgroup_unload_subsys(ss); | 
 | 4539 | 	return ret; | 
| Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4540 | } | 
 | 4541 | EXPORT_SYMBOL_GPL(cgroup_load_subsys); | 
 | 4542 |  | 
 | 4543 | /** | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4544 |  * cgroup_unload_subsys: unload a modular subsystem | 
 | 4545 |  * @ss: the subsystem to unload | 
 | 4546 |  * | 
 | 4547 |  * This function should be called in a modular subsystem's exitcall. When this | 
 | 4548 |  * function is invoked, the refcount on the subsystem's module will be 0, so | 
 | 4549 |  * the subsystem will not be attached to any hierarchy. | 
 | 4550 |  */ | 
 | 4551 | void cgroup_unload_subsys(struct cgroup_subsys *ss) | 
 | 4552 | { | 
 | 4553 | 	struct cg_cgroup_link *link; | 
 | 4554 | 	struct hlist_head *hhead; | 
 | 4555 |  | 
 | 4556 | 	BUG_ON(ss->module == NULL); | 
 | 4557 |  | 
 | 4558 | 	/* | 
 | 4559 | 	 * we shouldn't be called if the subsystem is in use, and the use of | 
 | 4560 | 	 * try_module_get in parse_cgroupfs_options should ensure that it | 
 | 4561 | 	 * doesn't start being used while we're killing it off. | 
 | 4562 | 	 */ | 
 | 4563 | 	BUG_ON(ss->root != &rootnode); | 
 | 4564 |  | 
 | 4565 | 	mutex_lock(&cgroup_mutex); | 
| Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4566 |  | 
| Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4567 | 	offline_css(ss, dummytop); | 
| Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4568 | 	ss->active = 0; | 
 | 4569 |  | 
 | 4570 | 	if (ss->use_id) { | 
 | 4571 | 		idr_remove_all(&ss->idr); | 
 | 4572 | 		idr_destroy(&ss->idr); | 
 | 4573 | 	} | 
 | 4574 |  | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4575 | 	/* deassign the subsys_id */ | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4576 | 	subsys[ss->subsys_id] = NULL; | 
 | 4577 |  | 
 | 4578 | 	/* remove subsystem from rootnode's list of subsystems */ | 
| Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 4579 | 	list_del_init(&ss->sibling); | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4580 |  | 
 | 4581 | 	/* | 
 | 4582 | 	 * disentangle the css from all css_sets attached to the dummytop. as | 
 | 4583 | 	 * in loading, we need to pay our respects to the hashtable gods. | 
 | 4584 | 	 */ | 
 | 4585 | 	write_lock(&css_set_lock); | 
 | 4586 | 	list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) { | 
 | 4587 | 		struct css_set *cg = link->cg; | 
 | 4588 |  | 
 | 4589 | 		hlist_del(&cg->hlist); | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4590 | 		cg->subsys[ss->subsys_id] = NULL; | 
 | 4591 | 		hhead = css_set_hash(cg->subsys); | 
 | 4592 | 		hlist_add_head(&cg->hlist, hhead); | 
 | 4593 | 	} | 
 | 4594 | 	write_unlock(&css_set_lock); | 
 | 4595 |  | 
 | 4596 | 	/* | 
| Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4597 | 	 * remove subsystem's css from the dummytop and free it - need to | 
 | 4598 | 	 * free before marking as null because ss->css_free needs the | 
 | 4599 | 	 * cgrp->subsys pointer to find their state. note that this also | 
 | 4600 | 	 * takes care of freeing the css_id. | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4601 | 	 */ | 
| Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4602 | 	ss->css_free(dummytop); | 
| Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4603 | 	dummytop->subsys[ss->subsys_id] = NULL; | 
 | 4604 |  | 
 | 4605 | 	mutex_unlock(&cgroup_mutex); | 
 | 4606 | } | 
 | 4607 | EXPORT_SYMBOL_GPL(cgroup_unload_subsys); | 
 | 4608 |  | 
 | 4609 | /** | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4610 |  * cgroup_init_early - cgroup initialization at system boot | 
 | 4611 |  * | 
 | 4612 |  * Initialize cgroups at system boot, and initialize any | 
 | 4613 |  * subsystems that request early init. | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4614 |  */ | 
 | 4615 | int __init cgroup_init_early(void) | 
 | 4616 | { | 
 | 4617 | 	int i; | 
| Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 4618 | 	atomic_set(&init_css_set.refcount, 1); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4619 | 	INIT_LIST_HEAD(&init_css_set.cg_links); | 
 | 4620 | 	INIT_LIST_HEAD(&init_css_set.tasks); | 
| Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4621 | 	INIT_HLIST_NODE(&init_css_set.hlist); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4622 | 	css_set_count = 1; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4623 | 	init_cgroup_root(&rootnode); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4624 | 	root_count = 1; | 
 | 4625 | 	init_task.cgroups = &init_css_set; | 
 | 4626 |  | 
 | 4627 | 	init_css_set_link.cg = &init_css_set; | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4628 | 	init_css_set_link.cgrp = dummytop; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4629 | 	list_add(&init_css_set_link.cgrp_link_list, | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4630 | 		 &rootnode.top_cgroup.css_sets); | 
 | 4631 | 	list_add(&init_css_set_link.cg_link_list, | 
 | 4632 | 		 &init_css_set.cg_links); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4633 |  | 
| Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4634 | 	for (i = 0; i < CSS_SET_TABLE_SIZE; i++) | 
 | 4635 | 		INIT_HLIST_HEAD(&css_set_table[i]); | 
 | 4636 |  | 
| Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4637 | 	for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4638 | 		struct cgroup_subsys *ss = subsys[i]; | 
 | 4639 |  | 
| Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4640 | 		/* at bootup time, we don't worry about modular subsystems */ | 
 | 4641 | 		if (!ss || ss->module) | 
 | 4642 | 			continue; | 
 | 4643 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4644 | 		BUG_ON(!ss->name); | 
 | 4645 | 		BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN); | 
| Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4646 | 		BUG_ON(!ss->css_alloc); | 
 | 4647 | 		BUG_ON(!ss->css_free); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4648 | 		if (ss->subsys_id != i) { | 
| Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4649 | 			printk(KERN_ERR "cgroup: Subsys %s id == %d\n", | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4650 | 			       ss->name, ss->subsys_id); | 
 | 4651 | 			BUG(); | 
 | 4652 | 		} | 
 | 4653 |  | 
 | 4654 | 		if (ss->early_init) | 
 | 4655 | 			cgroup_init_subsys(ss); | 
 | 4656 | 	} | 
 | 4657 | 	return 0; | 
 | 4658 | } | 
 | 4659 |  | 
 | 4660 | /** | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4661 |  * cgroup_init - cgroup initialization | 
 | 4662 |  * | 
 | 4663 |  * Register cgroup filesystem and /proc file, and initialize | 
 | 4664 |  * any subsystems that didn't request early init. | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4665 |  */ | 
 | 4666 | int __init cgroup_init(void) | 
 | 4667 | { | 
 | 4668 | 	int err; | 
 | 4669 | 	int i; | 
| Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4670 | 	struct hlist_head *hhead; | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4671 |  | 
 | 4672 | 	err = bdi_init(&cgroup_backing_dev_info); | 
 | 4673 | 	if (err) | 
 | 4674 | 		return err; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4675 |  | 
| Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4676 | 	for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4677 | 		struct cgroup_subsys *ss = subsys[i]; | 
| Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4678 |  | 
 | 4679 | 		/* at bootup time, we don't worry about modular subsystems */ | 
 | 4680 | 		if (!ss || ss->module) | 
 | 4681 | 			continue; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4682 | 		if (!ss->early_init) | 
 | 4683 | 			cgroup_init_subsys(ss); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4684 | 		if (ss->use_id) | 
| Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4685 | 			cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]); | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4686 | 	} | 
 | 4687 |  | 
| Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4688 | 	/* Add init_css_set to the hash table */ | 
 | 4689 | 	hhead = css_set_hash(init_css_set.subsys); | 
 | 4690 | 	hlist_add_head(&init_css_set.hlist, hhead); | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4691 | 	BUG_ON(!init_root_id(&rootnode)); | 
| Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4692 |  | 
 | 4693 | 	cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj); | 
 | 4694 | 	if (!cgroup_kobj) { | 
 | 4695 | 		err = -ENOMEM; | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4696 | 		goto out; | 
| Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4697 | 	} | 
 | 4698 |  | 
 | 4699 | 	err = register_filesystem(&cgroup_fs_type); | 
 | 4700 | 	if (err < 0) { | 
 | 4701 | 		kobject_put(cgroup_kobj); | 
 | 4702 | 		goto out; | 
 | 4703 | 	} | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4704 |  | 
| Li Zefan | 46ae220 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 4705 | 	proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations); | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4706 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4707 | out: | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4708 | 	if (err) | 
 | 4709 | 		bdi_destroy(&cgroup_backing_dev_info); | 
 | 4710 |  | 
| Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4711 | 	return err; | 
 | 4712 | } | 
| Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4713 |  | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4714 | /* | 
 | 4715 |  * proc_cgroup_show() | 
 | 4716 |  *  - Print task's cgroup paths into seq_file, one line for each hierarchy | 
 | 4717 |  *  - Used for /proc/<pid>/cgroup. | 
 | 4718 |  *  - No need to task_lock(tsk) on this tsk->cgroup reference, as it | 
 | 4719 |  *    doesn't really matter if tsk->cgroup changes after we read it, | 
| Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 4720 |  *    and we take cgroup_mutex, keeping cgroup_attach_task() from changing it | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4721 |  *    anyway.  No need to check that tsk->cgroup != NULL, thanks to | 
 | 4722 |  *    the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks | 
 | 4723 |  *    cgroup to top_cgroup. | 
 | 4724 |  */ | 
 | 4725 |  | 
 | 4726 | /* TODO: Use a proper seq_file iterator */ | 
 | 4727 | static int proc_cgroup_show(struct seq_file *m, void *v) | 
 | 4728 | { | 
 | 4729 | 	struct pid *pid; | 
 | 4730 | 	struct task_struct *tsk; | 
 | 4731 | 	char *buf; | 
 | 4732 | 	int retval; | 
 | 4733 | 	struct cgroupfs_root *root; | 
 | 4734 |  | 
 | 4735 | 	retval = -ENOMEM; | 
 | 4736 | 	buf = kmalloc(PAGE_SIZE, GFP_KERNEL); | 
 | 4737 | 	if (!buf) | 
 | 4738 | 		goto out; | 
 | 4739 |  | 
 | 4740 | 	retval = -ESRCH; | 
 | 4741 | 	pid = m->private; | 
 | 4742 | 	tsk = get_pid_task(pid, PIDTYPE_PID); | 
 | 4743 | 	if (!tsk) | 
 | 4744 | 		goto out_free; | 
 | 4745 |  | 
 | 4746 | 	retval = 0; | 
 | 4747 |  | 
 | 4748 | 	mutex_lock(&cgroup_mutex); | 
 | 4749 |  | 
| Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 4750 | 	for_each_active_root(root) { | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4751 | 		struct cgroup_subsys *ss; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4752 | 		struct cgroup *cgrp; | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4753 | 		int count = 0; | 
 | 4754 |  | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4755 | 		seq_printf(m, "%d:", root->hierarchy_id); | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4756 | 		for_each_subsys(root, ss) | 
 | 4757 | 			seq_printf(m, "%s%s", count++ ? "," : "", ss->name); | 
| Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 4758 | 		if (strlen(root->name)) | 
 | 4759 | 			seq_printf(m, "%sname=%s", count ? "," : "", | 
 | 4760 | 				   root->name); | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4761 | 		seq_putc(m, ':'); | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4762 | 		cgrp = task_cgroup_from_root(tsk, root); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4763 | 		retval = cgroup_path(cgrp, buf, PAGE_SIZE); | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4764 | 		if (retval < 0) | 
 | 4765 | 			goto out_unlock; | 
 | 4766 | 		seq_puts(m, buf); | 
 | 4767 | 		seq_putc(m, '\n'); | 
 | 4768 | 	} | 
 | 4769 |  | 
 | 4770 | out_unlock: | 
 | 4771 | 	mutex_unlock(&cgroup_mutex); | 
 | 4772 | 	put_task_struct(tsk); | 
 | 4773 | out_free: | 
 | 4774 | 	kfree(buf); | 
 | 4775 | out: | 
 | 4776 | 	return retval; | 
 | 4777 | } | 
 | 4778 |  | 
 | 4779 | static int cgroup_open(struct inode *inode, struct file *file) | 
 | 4780 | { | 
 | 4781 | 	struct pid *pid = PROC_I(inode)->pid; | 
 | 4782 | 	return single_open(file, proc_cgroup_show, pid); | 
 | 4783 | } | 
 | 4784 |  | 
| Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 4785 | const struct file_operations proc_cgroup_operations = { | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4786 | 	.open		= cgroup_open, | 
 | 4787 | 	.read		= seq_read, | 
 | 4788 | 	.llseek		= seq_lseek, | 
 | 4789 | 	.release	= single_release, | 
 | 4790 | }; | 
 | 4791 |  | 
 | 4792 | /* Display information about each subsystem and each hierarchy */ | 
 | 4793 | static int proc_cgroupstats_show(struct seq_file *m, void *v) | 
 | 4794 | { | 
 | 4795 | 	int i; | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4796 |  | 
| Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4797 | 	seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n"); | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4798 | 	/* | 
 | 4799 | 	 * ideally we don't want subsystems moving around while we do this. | 
 | 4800 | 	 * cgroup_mutex is also necessary to guarantee an atomic snapshot of | 
 | 4801 | 	 * subsys/hierarchy state. | 
 | 4802 | 	 */ | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4803 | 	mutex_lock(&cgroup_mutex); | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4804 | 	for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { | 
 | 4805 | 		struct cgroup_subsys *ss = subsys[i]; | 
| Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4806 | 		if (ss == NULL) | 
 | 4807 | 			continue; | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4808 | 		seq_printf(m, "%s\t%d\t%d\t%d\n", | 
 | 4809 | 			   ss->name, ss->root->hierarchy_id, | 
| Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4810 | 			   ss->root->number_of_cgroups, !ss->disabled); | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4811 | 	} | 
 | 4812 | 	mutex_unlock(&cgroup_mutex); | 
 | 4813 | 	return 0; | 
 | 4814 | } | 
 | 4815 |  | 
 | 4816 | static int cgroupstats_open(struct inode *inode, struct file *file) | 
 | 4817 | { | 
| Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 4818 | 	return single_open(file, proc_cgroupstats_show, NULL); | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4819 | } | 
 | 4820 |  | 
| Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 4821 | static const struct file_operations proc_cgroupstats_operations = { | 
| Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4822 | 	.open = cgroupstats_open, | 
 | 4823 | 	.read = seq_read, | 
 | 4824 | 	.llseek = seq_lseek, | 
 | 4825 | 	.release = single_release, | 
 | 4826 | }; | 
 | 4827 |  | 
| Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4828 | /** | 
 | 4829 |  * cgroup_fork - attach newly forked task to its parents cgroup. | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4830 |  * @child: pointer to task_struct of forking parent process. | 
| Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4831 |  * | 
 | 4832 |  * Description: A task inherits its parent's cgroup at fork(). | 
 | 4833 |  * | 
 | 4834 |  * A pointer to the shared css_set was automatically copied in | 
 | 4835 |  * fork.c by dup_task_struct().  However, we ignore that copy, since | 
| Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4836 |  * it was not made under the protection of RCU or cgroup_mutex, so | 
 | 4837 |  * might no longer be a valid cgroup pointer.  cgroup_attach_task() might | 
 | 4838 |  * have already changed current->cgroups, allowing the previously | 
 | 4839 |  * referenced cgroup group to be removed and freed. | 
| Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4840 |  * | 
 | 4841 |  * At the point that cgroup_fork() is called, 'current' is the parent | 
 | 4842 |  * task, and the passed argument 'child' points to the child task. | 
 | 4843 |  */ | 
 | 4844 | void cgroup_fork(struct task_struct *child) | 
 | 4845 | { | 
| Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4846 | 	task_lock(current); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4847 | 	child->cgroups = current->cgroups; | 
 | 4848 | 	get_css_set(child->cgroups); | 
| Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4849 | 	task_unlock(current); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4850 | 	INIT_LIST_HEAD(&child->cg_list); | 
| Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4851 | } | 
 | 4852 |  | 
 | 4853 | /** | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4854 |  * cgroup_post_fork - called on a new task after adding it to the task list | 
 | 4855 |  * @child: the task in question | 
 | 4856 |  * | 
| Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4857 |  * Adds the task to the list running through its css_set if necessary and | 
 | 4858 |  * call the subsystem fork() callbacks.  Has to be after the task is | 
 | 4859 |  * visible on the task list in case we race with the first call to | 
 | 4860 |  * cgroup_iter_start() - to guarantee that the new task ends up on its | 
 | 4861 |  * list. | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4862 |  */ | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4863 | void cgroup_post_fork(struct task_struct *child) | 
 | 4864 | { | 
| Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4865 | 	int i; | 
 | 4866 |  | 
| Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 4867 | 	/* | 
 | 4868 | 	 * use_task_css_set_links is set to 1 before we walk the tasklist | 
 | 4869 | 	 * under the tasklist_lock and we read it here after we added the child | 
 | 4870 | 	 * to the tasklist under the tasklist_lock as well. If the child wasn't | 
 | 4871 | 	 * yet in the tasklist when we walked through it from | 
 | 4872 | 	 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value | 
 | 4873 | 	 * should be visible now due to the paired locking and barriers implied | 
 | 4874 | 	 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock | 
 | 4875 | 	 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock | 
 | 4876 | 	 * lock on fork. | 
 | 4877 | 	 */ | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4878 | 	if (use_task_css_set_links) { | 
 | 4879 | 		write_lock(&css_set_lock); | 
| Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 4880 | 		task_lock(child); | 
 | 4881 | 		if (list_empty(&child->cg_list)) | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4882 | 			list_add(&child->cg_list, &child->cgroups->tasks); | 
| Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 4883 | 		task_unlock(child); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4884 | 		write_unlock(&css_set_lock); | 
 | 4885 | 	} | 
| Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4886 |  | 
 | 4887 | 	/* | 
 | 4888 | 	 * Call ss->fork().  This must happen after @child is linked on | 
 | 4889 | 	 * css_set; otherwise, @child might change state between ->fork() | 
 | 4890 | 	 * and addition to css_set. | 
 | 4891 | 	 */ | 
 | 4892 | 	if (need_forkexit_callback) { | 
 | 4893 | 		for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { | 
 | 4894 | 			struct cgroup_subsys *ss = subsys[i]; | 
 | 4895 |  | 
 | 4896 | 			/* | 
 | 4897 | 			 * fork/exit callbacks are supported only for | 
 | 4898 | 			 * builtin subsystems and we don't need further | 
 | 4899 | 			 * synchronization as they never go away. | 
 | 4900 | 			 */ | 
 | 4901 | 			if (!ss || ss->module) | 
 | 4902 | 				continue; | 
 | 4903 |  | 
 | 4904 | 			if (ss->fork) | 
 | 4905 | 				ss->fork(child); | 
 | 4906 | 		} | 
 | 4907 | 	} | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4908 | } | 
| Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4909 |  | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4910 | /** | 
| Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4911 |  * cgroup_exit - detach cgroup from exiting task | 
 | 4912 |  * @tsk: pointer to task_struct of exiting process | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4913 |  * @run_callback: run exit callbacks? | 
| Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4914 |  * | 
 | 4915 |  * Description: Detach cgroup from @tsk and release it. | 
 | 4916 |  * | 
 | 4917 |  * Note that cgroups marked notify_on_release force every task in | 
 | 4918 |  * them to take the global cgroup_mutex mutex when exiting. | 
 | 4919 |  * This could impact scaling on very large systems.  Be reluctant to | 
 | 4920 |  * use notify_on_release cgroups where very high task exit scaling | 
 | 4921 |  * is required on large systems. | 
 | 4922 |  * | 
 | 4923 |  * the_top_cgroup_hack: | 
 | 4924 |  * | 
 | 4925 |  *    Set the exiting tasks cgroup to the root cgroup (top_cgroup). | 
 | 4926 |  * | 
 | 4927 |  *    We call cgroup_exit() while the task is still competent to | 
 | 4928 |  *    handle notify_on_release(), then leave the task attached to the | 
 | 4929 |  *    root cgroup in each hierarchy for the remainder of its exit. | 
 | 4930 |  * | 
 | 4931 |  *    To do this properly, we would increment the reference count on | 
 | 4932 |  *    top_cgroup, and near the very end of the kernel/exit.c do_exit() | 
 | 4933 |  *    code we would add a second cgroup function call, to drop that | 
 | 4934 |  *    reference.  This would just create an unnecessary hot spot on | 
 | 4935 |  *    the top_cgroup reference count, to no avail. | 
 | 4936 |  * | 
 | 4937 |  *    Normally, holding a reference to a cgroup without bumping its | 
 | 4938 |  *    count is unsafe.   The cgroup could go away, or someone could | 
 | 4939 |  *    attach us to a different cgroup, decrementing the count on | 
 | 4940 |  *    the first cgroup that we never incremented.  But in this case, | 
 | 4941 |  *    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] | 4942 |  *    which wards off any cgroup_attach_task() attempts, or task is a failed | 
 | 4943 |  *    fork, never visible to cgroup_attach_task. | 
| Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4944 |  */ | 
 | 4945 | void cgroup_exit(struct task_struct *tsk, int run_callbacks) | 
 | 4946 | { | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4947 | 	struct css_set *cg; | 
| Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4948 | 	int i; | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4949 |  | 
 | 4950 | 	/* | 
 | 4951 | 	 * Unlink from the css_set task list if necessary. | 
 | 4952 | 	 * Optimistically check cg_list before taking | 
 | 4953 | 	 * css_set_lock | 
 | 4954 | 	 */ | 
 | 4955 | 	if (!list_empty(&tsk->cg_list)) { | 
 | 4956 | 		write_lock(&css_set_lock); | 
 | 4957 | 		if (!list_empty(&tsk->cg_list)) | 
| Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 4958 | 			list_del_init(&tsk->cg_list); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4959 | 		write_unlock(&css_set_lock); | 
 | 4960 | 	} | 
 | 4961 |  | 
| Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4962 | 	/* Reassign the task to the init_css_set. */ | 
 | 4963 | 	task_lock(tsk); | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4964 | 	cg = tsk->cgroups; | 
 | 4965 | 	tsk->cgroups = &init_css_set; | 
| Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4966 |  | 
 | 4967 | 	if (run_callbacks && need_forkexit_callback) { | 
| Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4968 | 		for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { | 
| Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4969 | 			struct cgroup_subsys *ss = subsys[i]; | 
| Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4970 |  | 
 | 4971 | 			/* modular subsystems can't use callbacks */ | 
 | 4972 | 			if (!ss || ss->module) | 
 | 4973 | 				continue; | 
 | 4974 |  | 
| Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4975 | 			if (ss->exit) { | 
 | 4976 | 				struct cgroup *old_cgrp = | 
 | 4977 | 					rcu_dereference_raw(cg->subsys[i])->cgroup; | 
 | 4978 | 				struct cgroup *cgrp = task_cgroup(tsk, i); | 
| Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 4979 | 				ss->exit(cgrp, old_cgrp, tsk); | 
| Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4980 | 			} | 
 | 4981 | 		} | 
 | 4982 | 	} | 
| Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4983 | 	task_unlock(tsk); | 
| Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4984 |  | 
| Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4985 | 	if (cg) | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4986 | 		put_css_set_taskexit(cg); | 
| Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4987 | } | 
| Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 4988 |  | 
 | 4989 | /** | 
| Grzegorz Nosek | 313e924 | 2009-04-02 16:57:23 -0700 | [diff] [blame] | 4990 |  * 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] | 4991 |  * @cgrp: the cgroup in question | 
| Grzegorz Nosek | 313e924 | 2009-04-02 16:57:23 -0700 | [diff] [blame] | 4992 |  * @task: the task in question | 
| Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4993 |  * | 
| Grzegorz Nosek | 313e924 | 2009-04-02 16:57:23 -0700 | [diff] [blame] | 4994 |  * See if @cgrp is a descendant of @task's cgroup in the appropriate | 
 | 4995 |  * hierarchy. | 
| Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 4996 |  * | 
 | 4997 |  * If we are sending in dummytop, then presumably we are creating | 
 | 4998 |  * the top cgroup in the subsystem. | 
 | 4999 |  * | 
 | 5000 |  * Called only by the ns (nsproxy) cgroup. | 
 | 5001 |  */ | 
| Grzegorz Nosek | 313e924 | 2009-04-02 16:57:23 -0700 | [diff] [blame] | 5002 | int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task) | 
| Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 5003 | { | 
 | 5004 | 	int ret; | 
 | 5005 | 	struct cgroup *target; | 
| Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 5006 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5007 | 	if (cgrp == dummytop) | 
| Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 5008 | 		return 1; | 
 | 5009 |  | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5010 | 	target = task_cgroup_from_root(task, cgrp->root); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5011 | 	while (cgrp != target && cgrp!= cgrp->top_cgroup) | 
 | 5012 | 		cgrp = cgrp->parent; | 
 | 5013 | 	ret = (cgrp == target); | 
| Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 5014 | 	return ret; | 
 | 5015 | } | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5016 |  | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5017 | static void check_for_release(struct cgroup *cgrp) | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5018 | { | 
 | 5019 | 	/* All of these checks rely on RCU to keep the cgroup | 
 | 5020 | 	 * structure alive */ | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5021 | 	if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count) | 
 | 5022 | 	    && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) { | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5023 | 		/* Control Group is currently removeable. If it's not | 
 | 5024 | 		 * already queued for a userspace notification, queue | 
 | 5025 | 		 * it now */ | 
 | 5026 | 		int need_schedule_work = 0; | 
| Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5027 | 		raw_spin_lock(&release_list_lock); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5028 | 		if (!cgroup_is_removed(cgrp) && | 
 | 5029 | 		    list_empty(&cgrp->release_list)) { | 
 | 5030 | 			list_add(&cgrp->release_list, &release_list); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5031 | 			need_schedule_work = 1; | 
 | 5032 | 		} | 
| Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5033 | 		raw_spin_unlock(&release_list_lock); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5034 | 		if (need_schedule_work) | 
 | 5035 | 			schedule_work(&release_agent_work); | 
 | 5036 | 	} | 
 | 5037 | } | 
 | 5038 |  | 
| Daisuke Nishimura | d7b9fff | 2010-03-10 15:22:05 -0800 | [diff] [blame] | 5039 | /* Caller must verify that the css is not for root cgroup */ | 
| Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5040 | bool __css_tryget(struct cgroup_subsys_state *css) | 
 | 5041 | { | 
| Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 5042 | 	while (true) { | 
 | 5043 | 		int t, v; | 
| Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5044 |  | 
| Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 5045 | 		v = css_refcnt(css); | 
 | 5046 | 		t = atomic_cmpxchg(&css->refcnt, v, v + 1); | 
 | 5047 | 		if (likely(t == v)) | 
| Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5048 | 			return true; | 
| Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 5049 | 		else if (t < 0) | 
 | 5050 | 			return false; | 
| Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5051 | 		cpu_relax(); | 
| Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 5052 | 	} | 
| Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5053 | } | 
 | 5054 | EXPORT_SYMBOL_GPL(__css_tryget); | 
 | 5055 |  | 
 | 5056 | /* Caller must verify that the css is not for root cgroup */ | 
 | 5057 | void __css_put(struct cgroup_subsys_state *css) | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5058 | { | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5059 | 	struct cgroup *cgrp = css->cgroup; | 
| Salman Qazi | 8e3bbf4 | 2012-06-14 14:55:30 -0700 | [diff] [blame] | 5060 | 	int v; | 
| Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5061 |  | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5062 | 	rcu_read_lock(); | 
| Salman Qazi | 8e3bbf4 | 2012-06-14 14:55:30 -0700 | [diff] [blame] | 5063 | 	v = css_unbias_refcnt(atomic_dec_return(&css->refcnt)); | 
 | 5064 |  | 
 | 5065 | 	switch (v) { | 
| Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5066 | 	case 1: | 
| KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 5067 | 		if (notify_on_release(cgrp)) { | 
 | 5068 | 			set_bit(CGRP_RELEASABLE, &cgrp->flags); | 
 | 5069 | 			check_for_release(cgrp); | 
 | 5070 | 		} | 
| Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5071 | 		break; | 
 | 5072 | 	case 0: | 
| Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 5073 | 		schedule_work(&css->dput_work); | 
| Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5074 | 		break; | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5075 | 	} | 
 | 5076 | 	rcu_read_unlock(); | 
 | 5077 | } | 
| Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5078 | EXPORT_SYMBOL_GPL(__css_put); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5079 |  | 
 | 5080 | /* | 
 | 5081 |  * Notify userspace when a cgroup is released, by running the | 
 | 5082 |  * configured release agent with the name of the cgroup (path | 
 | 5083 |  * relative to the root of cgroup file system) as the argument. | 
 | 5084 |  * | 
 | 5085 |  * Most likely, this user command will try to rmdir this cgroup. | 
 | 5086 |  * | 
 | 5087 |  * This races with the possibility that some other task will be | 
 | 5088 |  * attached to this cgroup before it is removed, or that some other | 
 | 5089 |  * user task will 'mkdir' a child cgroup of this cgroup.  That's ok. | 
 | 5090 |  * The presumed 'rmdir' will fail quietly if this cgroup is no longer | 
 | 5091 |  * unused, and this cgroup will be reprieved from its death sentence, | 
 | 5092 |  * to continue to serve a useful existence.  Next time it's released, | 
 | 5093 |  * we will get notified again, if it still has 'notify_on_release' set. | 
 | 5094 |  * | 
 | 5095 |  * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which | 
 | 5096 |  * means only wait until the task is successfully execve()'d.  The | 
 | 5097 |  * separate release agent task is forked by call_usermodehelper(), | 
 | 5098 |  * then control in this thread returns here, without waiting for the | 
 | 5099 |  * release agent task.  We don't bother to wait because the caller of | 
 | 5100 |  * this routine has no use for the exit status of the release agent | 
 | 5101 |  * task, so no sense holding our caller up for that. | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5102 |  */ | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5103 | static void cgroup_release_agent(struct work_struct *work) | 
 | 5104 | { | 
 | 5105 | 	BUG_ON(work != &release_agent_work); | 
 | 5106 | 	mutex_lock(&cgroup_mutex); | 
| Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5107 | 	raw_spin_lock(&release_list_lock); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5108 | 	while (!list_empty(&release_list)) { | 
 | 5109 | 		char *argv[3], *envp[3]; | 
 | 5110 | 		int i; | 
| Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5111 | 		char *pathbuf = NULL, *agentbuf = NULL; | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5112 | 		struct cgroup *cgrp = list_entry(release_list.next, | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5113 | 						    struct cgroup, | 
 | 5114 | 						    release_list); | 
| Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5115 | 		list_del_init(&cgrp->release_list); | 
| Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5116 | 		raw_spin_unlock(&release_list_lock); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5117 | 		pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); | 
| Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5118 | 		if (!pathbuf) | 
 | 5119 | 			goto continue_free; | 
 | 5120 | 		if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0) | 
 | 5121 | 			goto continue_free; | 
 | 5122 | 		agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL); | 
 | 5123 | 		if (!agentbuf) | 
 | 5124 | 			goto continue_free; | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5125 |  | 
 | 5126 | 		i = 0; | 
| Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5127 | 		argv[i++] = agentbuf; | 
 | 5128 | 		argv[i++] = pathbuf; | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5129 | 		argv[i] = NULL; | 
 | 5130 |  | 
 | 5131 | 		i = 0; | 
 | 5132 | 		/* minimal command environment */ | 
 | 5133 | 		envp[i++] = "HOME=/"; | 
 | 5134 | 		envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; | 
 | 5135 | 		envp[i] = NULL; | 
 | 5136 |  | 
 | 5137 | 		/* Drop the lock while we invoke the usermode helper, | 
 | 5138 | 		 * since the exec could involve hitting disk and hence | 
 | 5139 | 		 * be a slow process */ | 
 | 5140 | 		mutex_unlock(&cgroup_mutex); | 
 | 5141 | 		call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5142 | 		mutex_lock(&cgroup_mutex); | 
| Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5143 |  continue_free: | 
 | 5144 | 		kfree(pathbuf); | 
 | 5145 | 		kfree(agentbuf); | 
| Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5146 | 		raw_spin_lock(&release_list_lock); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5147 | 	} | 
| Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5148 | 	raw_spin_unlock(&release_list_lock); | 
| Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5149 | 	mutex_unlock(&cgroup_mutex); | 
 | 5150 | } | 
| Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5151 |  | 
 | 5152 | static int __init cgroup_disable(char *str) | 
 | 5153 | { | 
 | 5154 | 	int i; | 
 | 5155 | 	char *token; | 
 | 5156 |  | 
 | 5157 | 	while ((token = strsep(&str, ",")) != NULL) { | 
 | 5158 | 		if (!*token) | 
 | 5159 | 			continue; | 
| Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 5160 | 		for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { | 
| Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5161 | 			struct cgroup_subsys *ss = subsys[i]; | 
 | 5162 |  | 
| Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 5163 | 			/* | 
 | 5164 | 			 * cgroup_disable, being at boot time, can't | 
 | 5165 | 			 * know about module subsystems, so we don't | 
 | 5166 | 			 * worry about them. | 
 | 5167 | 			 */ | 
 | 5168 | 			if (!ss || ss->module) | 
 | 5169 | 				continue; | 
 | 5170 |  | 
| Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5171 | 			if (!strcmp(token, ss->name)) { | 
 | 5172 | 				ss->disabled = 1; | 
 | 5173 | 				printk(KERN_INFO "Disabling %s control group" | 
 | 5174 | 					" subsystem\n", ss->name); | 
 | 5175 | 				break; | 
 | 5176 | 			} | 
 | 5177 | 		} | 
 | 5178 | 	} | 
 | 5179 | 	return 1; | 
 | 5180 | } | 
 | 5181 | __setup("cgroup_disable=", cgroup_disable); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5182 |  | 
 | 5183 | /* | 
 | 5184 |  * Functons for CSS ID. | 
 | 5185 |  */ | 
 | 5186 |  | 
 | 5187 | /* | 
 | 5188 |  *To get ID other than 0, this should be called when !cgroup_is_removed(). | 
 | 5189 |  */ | 
 | 5190 | unsigned short css_id(struct cgroup_subsys_state *css) | 
 | 5191 | { | 
| KAMEZAWA Hiroyuki | 7f0f154 | 2010-05-11 14:06:58 -0700 | [diff] [blame] | 5192 | 	struct css_id *cssid; | 
 | 5193 |  | 
 | 5194 | 	/* | 
 | 5195 | 	 * This css_id() can return correct value when somone has refcnt | 
 | 5196 | 	 * on this or this is under rcu_read_lock(). Once css->id is allocated, | 
 | 5197 | 	 * it's unchanged until freed. | 
 | 5198 | 	 */ | 
| Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5199 | 	cssid = rcu_dereference_check(css->id, css_refcnt(css)); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5200 |  | 
 | 5201 | 	if (cssid) | 
 | 5202 | 		return cssid->id; | 
 | 5203 | 	return 0; | 
 | 5204 | } | 
| Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5205 | EXPORT_SYMBOL_GPL(css_id); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5206 |  | 
 | 5207 | unsigned short css_depth(struct cgroup_subsys_state *css) | 
 | 5208 | { | 
| KAMEZAWA Hiroyuki | 7f0f154 | 2010-05-11 14:06:58 -0700 | [diff] [blame] | 5209 | 	struct css_id *cssid; | 
 | 5210 |  | 
| Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5211 | 	cssid = rcu_dereference_check(css->id, css_refcnt(css)); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5212 |  | 
 | 5213 | 	if (cssid) | 
 | 5214 | 		return cssid->depth; | 
 | 5215 | 	return 0; | 
 | 5216 | } | 
| Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5217 | EXPORT_SYMBOL_GPL(css_depth); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5218 |  | 
| KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5219 | /** | 
 | 5220 |  *  css_is_ancestor - test "root" css is an ancestor of "child" | 
 | 5221 |  * @child: the css to be tested. | 
 | 5222 |  * @root: the css supporsed to be an ancestor of the child. | 
 | 5223 |  * | 
 | 5224 |  * Returns true if "root" is an ancestor of "child" in its hierarchy. Because | 
| Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 5225 |  * this function reads css->id, the caller must hold rcu_read_lock(). | 
| KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5226 |  * But, considering usual usage, the csses should be valid objects after test. | 
 | 5227 |  * Assuming that the caller will do some action to the child if this returns | 
 | 5228 |  * returns true, the caller must take "child";s reference count. | 
 | 5229 |  * If "child" is valid object and this returns true, "root" is valid, too. | 
 | 5230 |  */ | 
 | 5231 |  | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5232 | bool css_is_ancestor(struct cgroup_subsys_state *child, | 
| KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 5233 | 		    const struct cgroup_subsys_state *root) | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5234 | { | 
| KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5235 | 	struct css_id *child_id; | 
 | 5236 | 	struct css_id *root_id; | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5237 |  | 
| KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5238 | 	child_id  = rcu_dereference(child->id); | 
| Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 5239 | 	if (!child_id) | 
 | 5240 | 		return false; | 
| KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5241 | 	root_id = rcu_dereference(root->id); | 
| Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 5242 | 	if (!root_id) | 
 | 5243 | 		return false; | 
 | 5244 | 	if (child_id->depth < root_id->depth) | 
 | 5245 | 		return false; | 
 | 5246 | 	if (child_id->stack[root_id->depth] != root_id->id) | 
 | 5247 | 		return false; | 
 | 5248 | 	return true; | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5249 | } | 
 | 5250 |  | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5251 | void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css) | 
 | 5252 | { | 
 | 5253 | 	struct css_id *id = css->id; | 
 | 5254 | 	/* When this is called before css_id initialization, id can be NULL */ | 
 | 5255 | 	if (!id) | 
 | 5256 | 		return; | 
 | 5257 |  | 
 | 5258 | 	BUG_ON(!ss->use_id); | 
 | 5259 |  | 
 | 5260 | 	rcu_assign_pointer(id->css, NULL); | 
 | 5261 | 	rcu_assign_pointer(css->id, NULL); | 
| Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5262 | 	spin_lock(&ss->id_lock); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5263 | 	idr_remove(&ss->idr, id->id); | 
| Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5264 | 	spin_unlock(&ss->id_lock); | 
| Lai Jiangshan | 025cea9 | 2011-03-15 17:56:10 +0800 | [diff] [blame] | 5265 | 	kfree_rcu(id, rcu_head); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5266 | } | 
| Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5267 | EXPORT_SYMBOL_GPL(free_css_id); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5268 |  | 
 | 5269 | /* | 
 | 5270 |  * This is called by init or create(). Then, calls to this function are | 
 | 5271 |  * always serialized (By cgroup_mutex() at create()). | 
 | 5272 |  */ | 
 | 5273 |  | 
 | 5274 | static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth) | 
 | 5275 | { | 
 | 5276 | 	struct css_id *newid; | 
 | 5277 | 	int myid, error, size; | 
 | 5278 |  | 
 | 5279 | 	BUG_ON(!ss->use_id); | 
 | 5280 |  | 
 | 5281 | 	size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1); | 
 | 5282 | 	newid = kzalloc(size, GFP_KERNEL); | 
 | 5283 | 	if (!newid) | 
 | 5284 | 		return ERR_PTR(-ENOMEM); | 
 | 5285 | 	/* get id */ | 
 | 5286 | 	if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) { | 
 | 5287 | 		error = -ENOMEM; | 
 | 5288 | 		goto err_out; | 
 | 5289 | 	} | 
| Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5290 | 	spin_lock(&ss->id_lock); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5291 | 	/* Don't use 0. allocates an ID of 1-65535 */ | 
 | 5292 | 	error = idr_get_new_above(&ss->idr, newid, 1, &myid); | 
| Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5293 | 	spin_unlock(&ss->id_lock); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5294 |  | 
 | 5295 | 	/* Returns error when there are no free spaces for new ID.*/ | 
 | 5296 | 	if (error) { | 
 | 5297 | 		error = -ENOSPC; | 
 | 5298 | 		goto err_out; | 
 | 5299 | 	} | 
 | 5300 | 	if (myid > CSS_ID_MAX) | 
 | 5301 | 		goto remove_idr; | 
 | 5302 |  | 
 | 5303 | 	newid->id = myid; | 
 | 5304 | 	newid->depth = depth; | 
 | 5305 | 	return newid; | 
 | 5306 | remove_idr: | 
 | 5307 | 	error = -ENOSPC; | 
| Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5308 | 	spin_lock(&ss->id_lock); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5309 | 	idr_remove(&ss->idr, myid); | 
| Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5310 | 	spin_unlock(&ss->id_lock); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5311 | err_out: | 
 | 5312 | 	kfree(newid); | 
 | 5313 | 	return ERR_PTR(error); | 
 | 5314 |  | 
 | 5315 | } | 
 | 5316 |  | 
| Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 5317 | static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss, | 
 | 5318 | 					    struct cgroup_subsys_state *rootcss) | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5319 | { | 
 | 5320 | 	struct css_id *newid; | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5321 |  | 
| Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5322 | 	spin_lock_init(&ss->id_lock); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5323 | 	idr_init(&ss->idr); | 
 | 5324 |  | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5325 | 	newid = get_new_cssid(ss, 0); | 
 | 5326 | 	if (IS_ERR(newid)) | 
 | 5327 | 		return PTR_ERR(newid); | 
 | 5328 |  | 
 | 5329 | 	newid->stack[0] = newid->id; | 
 | 5330 | 	newid->css = rootcss; | 
 | 5331 | 	rootcss->id = newid; | 
 | 5332 | 	return 0; | 
 | 5333 | } | 
 | 5334 |  | 
 | 5335 | static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent, | 
 | 5336 | 			struct cgroup *child) | 
 | 5337 | { | 
 | 5338 | 	int subsys_id, i, depth = 0; | 
 | 5339 | 	struct cgroup_subsys_state *parent_css, *child_css; | 
| Li Zefan | fae9c79 | 2010-04-22 17:30:00 +0800 | [diff] [blame] | 5340 | 	struct css_id *child_id, *parent_id; | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5341 |  | 
 | 5342 | 	subsys_id = ss->subsys_id; | 
 | 5343 | 	parent_css = parent->subsys[subsys_id]; | 
 | 5344 | 	child_css = child->subsys[subsys_id]; | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5345 | 	parent_id = parent_css->id; | 
| Greg Thelen | 94b3dd0 | 2010-06-04 14:15:03 -0700 | [diff] [blame] | 5346 | 	depth = parent_id->depth + 1; | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5347 |  | 
 | 5348 | 	child_id = get_new_cssid(ss, depth); | 
 | 5349 | 	if (IS_ERR(child_id)) | 
 | 5350 | 		return PTR_ERR(child_id); | 
 | 5351 |  | 
 | 5352 | 	for (i = 0; i < depth; i++) | 
 | 5353 | 		child_id->stack[i] = parent_id->stack[i]; | 
 | 5354 | 	child_id->stack[depth] = child_id->id; | 
 | 5355 | 	/* | 
 | 5356 | 	 * child_id->css pointer will be set after this cgroup is available | 
 | 5357 | 	 * see cgroup_populate_dir() | 
 | 5358 | 	 */ | 
 | 5359 | 	rcu_assign_pointer(child_css->id, child_id); | 
 | 5360 |  | 
 | 5361 | 	return 0; | 
 | 5362 | } | 
 | 5363 |  | 
 | 5364 | /** | 
 | 5365 |  * css_lookup - lookup css by id | 
 | 5366 |  * @ss: cgroup subsys to be looked into. | 
 | 5367 |  * @id: the id | 
 | 5368 |  * | 
 | 5369 |  * Returns pointer to cgroup_subsys_state if there is valid one with id. | 
 | 5370 |  * NULL if not. Should be called under rcu_read_lock() | 
 | 5371 |  */ | 
 | 5372 | struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id) | 
 | 5373 | { | 
 | 5374 | 	struct css_id *cssid = NULL; | 
 | 5375 |  | 
 | 5376 | 	BUG_ON(!ss->use_id); | 
 | 5377 | 	cssid = idr_find(&ss->idr, id); | 
 | 5378 |  | 
 | 5379 | 	if (unlikely(!cssid)) | 
 | 5380 | 		return NULL; | 
 | 5381 |  | 
 | 5382 | 	return rcu_dereference(cssid->css); | 
 | 5383 | } | 
| Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5384 | EXPORT_SYMBOL_GPL(css_lookup); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5385 |  | 
 | 5386 | /** | 
 | 5387 |  * css_get_next - lookup next cgroup under specified hierarchy. | 
 | 5388 |  * @ss: pointer to subsystem | 
 | 5389 |  * @id: current position of iteration. | 
 | 5390 |  * @root: pointer to css. search tree under this. | 
 | 5391 |  * @foundid: position of found object. | 
 | 5392 |  * | 
 | 5393 |  * Search next css under the specified hierarchy of rootid. Calling under | 
 | 5394 |  * rcu_read_lock() is necessary. Returns NULL if it reaches the end. | 
 | 5395 |  */ | 
 | 5396 | struct cgroup_subsys_state * | 
 | 5397 | css_get_next(struct cgroup_subsys *ss, int id, | 
 | 5398 | 	     struct cgroup_subsys_state *root, int *foundid) | 
 | 5399 | { | 
 | 5400 | 	struct cgroup_subsys_state *ret = NULL; | 
 | 5401 | 	struct css_id *tmp; | 
 | 5402 | 	int tmpid; | 
 | 5403 | 	int rootid = css_id(root); | 
 | 5404 | 	int depth = css_depth(root); | 
 | 5405 |  | 
 | 5406 | 	if (!rootid) | 
 | 5407 | 		return NULL; | 
 | 5408 |  | 
 | 5409 | 	BUG_ON(!ss->use_id); | 
| Hugh Dickins | ca464d6 | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5410 | 	WARN_ON_ONCE(!rcu_read_lock_held()); | 
 | 5411 |  | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5412 | 	/* fill start point for scan */ | 
 | 5413 | 	tmpid = id; | 
 | 5414 | 	while (1) { | 
 | 5415 | 		/* | 
 | 5416 | 		 * scan next entry from bitmap(tree), tmpid is updated after | 
 | 5417 | 		 * idr_get_next(). | 
 | 5418 | 		 */ | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5419 | 		tmp = idr_get_next(&ss->idr, &tmpid); | 
| KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5420 | 		if (!tmp) | 
 | 5421 | 			break; | 
 | 5422 | 		if (tmp->depth >= depth && tmp->stack[depth] == rootid) { | 
 | 5423 | 			ret = rcu_dereference(tmp->css); | 
 | 5424 | 			if (ret) { | 
 | 5425 | 				*foundid = tmpid; | 
 | 5426 | 				break; | 
 | 5427 | 			} | 
 | 5428 | 		} | 
 | 5429 | 		/* continue to scan from next id */ | 
 | 5430 | 		tmpid = tmpid + 1; | 
 | 5431 | 	} | 
 | 5432 | 	return ret; | 
 | 5433 | } | 
 | 5434 |  | 
| Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5435 | /* | 
 | 5436 |  * get corresponding css from file open on cgroupfs directory | 
 | 5437 |  */ | 
 | 5438 | struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id) | 
 | 5439 | { | 
 | 5440 | 	struct cgroup *cgrp; | 
 | 5441 | 	struct inode *inode; | 
 | 5442 | 	struct cgroup_subsys_state *css; | 
 | 5443 |  | 
 | 5444 | 	inode = f->f_dentry->d_inode; | 
 | 5445 | 	/* check in cgroup filesystem dir */ | 
 | 5446 | 	if (inode->i_op != &cgroup_dir_inode_operations) | 
 | 5447 | 		return ERR_PTR(-EBADF); | 
 | 5448 |  | 
 | 5449 | 	if (id < 0 || id >= CGROUP_SUBSYS_COUNT) | 
 | 5450 | 		return ERR_PTR(-EINVAL); | 
 | 5451 |  | 
 | 5452 | 	/* get cgroup */ | 
 | 5453 | 	cgrp = __d_cgrp(f->f_dentry); | 
 | 5454 | 	css = cgrp->subsys[id]; | 
 | 5455 | 	return css ? css : ERR_PTR(-ENOENT); | 
 | 5456 | } | 
 | 5457 |  | 
| Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5458 | #ifdef CONFIG_CGROUP_DEBUG | 
| Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5459 | static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont) | 
| Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5460 | { | 
 | 5461 | 	struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL); | 
 | 5462 |  | 
 | 5463 | 	if (!css) | 
 | 5464 | 		return ERR_PTR(-ENOMEM); | 
 | 5465 |  | 
 | 5466 | 	return css; | 
 | 5467 | } | 
 | 5468 |  | 
| Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5469 | static void debug_css_free(struct cgroup *cont) | 
| Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5470 | { | 
 | 5471 | 	kfree(cont->subsys[debug_subsys_id]); | 
 | 5472 | } | 
 | 5473 |  | 
 | 5474 | static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft) | 
 | 5475 | { | 
 | 5476 | 	return atomic_read(&cont->count); | 
 | 5477 | } | 
 | 5478 |  | 
 | 5479 | static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft) | 
 | 5480 | { | 
 | 5481 | 	return cgroup_task_count(cont); | 
 | 5482 | } | 
 | 5483 |  | 
 | 5484 | static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft) | 
 | 5485 | { | 
 | 5486 | 	return (u64)(unsigned long)current->cgroups; | 
 | 5487 | } | 
 | 5488 |  | 
 | 5489 | static u64 current_css_set_refcount_read(struct cgroup *cont, | 
 | 5490 | 					   struct cftype *cft) | 
 | 5491 | { | 
 | 5492 | 	u64 count; | 
 | 5493 |  | 
 | 5494 | 	rcu_read_lock(); | 
 | 5495 | 	count = atomic_read(¤t->cgroups->refcount); | 
 | 5496 | 	rcu_read_unlock(); | 
 | 5497 | 	return count; | 
 | 5498 | } | 
 | 5499 |  | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5500 | static int current_css_set_cg_links_read(struct cgroup *cont, | 
 | 5501 | 					 struct cftype *cft, | 
 | 5502 | 					 struct seq_file *seq) | 
 | 5503 | { | 
 | 5504 | 	struct cg_cgroup_link *link; | 
 | 5505 | 	struct css_set *cg; | 
 | 5506 |  | 
 | 5507 | 	read_lock(&css_set_lock); | 
 | 5508 | 	rcu_read_lock(); | 
 | 5509 | 	cg = rcu_dereference(current->cgroups); | 
 | 5510 | 	list_for_each_entry(link, &cg->cg_links, cg_link_list) { | 
 | 5511 | 		struct cgroup *c = link->cgrp; | 
 | 5512 | 		const char *name; | 
 | 5513 |  | 
 | 5514 | 		if (c->dentry) | 
 | 5515 | 			name = c->dentry->d_name.name; | 
 | 5516 | 		else | 
 | 5517 | 			name = "?"; | 
| Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5518 | 		seq_printf(seq, "Root %d group %s\n", | 
 | 5519 | 			   c->root->hierarchy_id, name); | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5520 | 	} | 
 | 5521 | 	rcu_read_unlock(); | 
 | 5522 | 	read_unlock(&css_set_lock); | 
 | 5523 | 	return 0; | 
 | 5524 | } | 
 | 5525 |  | 
 | 5526 | #define MAX_TASKS_SHOWN_PER_CSS 25 | 
 | 5527 | static int cgroup_css_links_read(struct cgroup *cont, | 
 | 5528 | 				 struct cftype *cft, | 
 | 5529 | 				 struct seq_file *seq) | 
 | 5530 | { | 
 | 5531 | 	struct cg_cgroup_link *link; | 
 | 5532 |  | 
 | 5533 | 	read_lock(&css_set_lock); | 
 | 5534 | 	list_for_each_entry(link, &cont->css_sets, cgrp_link_list) { | 
 | 5535 | 		struct css_set *cg = link->cg; | 
 | 5536 | 		struct task_struct *task; | 
 | 5537 | 		int count = 0; | 
 | 5538 | 		seq_printf(seq, "css_set %p\n", cg); | 
 | 5539 | 		list_for_each_entry(task, &cg->tasks, cg_list) { | 
 | 5540 | 			if (count++ > MAX_TASKS_SHOWN_PER_CSS) { | 
 | 5541 | 				seq_puts(seq, "  ...\n"); | 
 | 5542 | 				break; | 
 | 5543 | 			} else { | 
 | 5544 | 				seq_printf(seq, "  task %d\n", | 
 | 5545 | 					   task_pid_vnr(task)); | 
 | 5546 | 			} | 
 | 5547 | 		} | 
 | 5548 | 	} | 
 | 5549 | 	read_unlock(&css_set_lock); | 
 | 5550 | 	return 0; | 
 | 5551 | } | 
 | 5552 |  | 
| Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5553 | static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft) | 
 | 5554 | { | 
 | 5555 | 	return test_bit(CGRP_RELEASABLE, &cgrp->flags); | 
 | 5556 | } | 
 | 5557 |  | 
 | 5558 | static struct cftype debug_files[] =  { | 
 | 5559 | 	{ | 
 | 5560 | 		.name = "cgroup_refcount", | 
 | 5561 | 		.read_u64 = cgroup_refcount_read, | 
 | 5562 | 	}, | 
 | 5563 | 	{ | 
 | 5564 | 		.name = "taskcount", | 
 | 5565 | 		.read_u64 = debug_taskcount_read, | 
 | 5566 | 	}, | 
 | 5567 |  | 
 | 5568 | 	{ | 
 | 5569 | 		.name = "current_css_set", | 
 | 5570 | 		.read_u64 = current_css_set_read, | 
 | 5571 | 	}, | 
 | 5572 |  | 
 | 5573 | 	{ | 
 | 5574 | 		.name = "current_css_set_refcount", | 
 | 5575 | 		.read_u64 = current_css_set_refcount_read, | 
 | 5576 | 	}, | 
 | 5577 |  | 
 | 5578 | 	{ | 
| Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5579 | 		.name = "current_css_set_cg_links", | 
 | 5580 | 		.read_seq_string = current_css_set_cg_links_read, | 
 | 5581 | 	}, | 
 | 5582 |  | 
 | 5583 | 	{ | 
 | 5584 | 		.name = "cgroup_css_links", | 
 | 5585 | 		.read_seq_string = cgroup_css_links_read, | 
 | 5586 | 	}, | 
 | 5587 |  | 
 | 5588 | 	{ | 
| Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5589 | 		.name = "releasable", | 
 | 5590 | 		.read_u64 = releasable_read, | 
 | 5591 | 	}, | 
| Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5592 |  | 
| Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5593 | 	{ }	/* terminate */ | 
 | 5594 | }; | 
| Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5595 |  | 
 | 5596 | struct cgroup_subsys debug_subsys = { | 
 | 5597 | 	.name = "debug", | 
| Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5598 | 	.css_alloc = debug_css_alloc, | 
 | 5599 | 	.css_free = debug_css_free, | 
| Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5600 | 	.subsys_id = debug_subsys_id, | 
| Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5601 | 	.base_cftypes = debug_files, | 
| Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5602 | }; | 
 | 5603 | #endif /* CONFIG_CGROUP_DEBUG */ |