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