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 | * |
| 7 | * Copyright notices from the original cpuset code: |
| 8 | * -------------------------------------------------- |
| 9 | * Copyright (C) 2003 BULL SA. |
| 10 | * Copyright (C) 2004-2006 Silicon Graphics, Inc. |
| 11 | * |
| 12 | * Portions derived from Patrick Mochel's sysfs code. |
| 13 | * sysfs is Copyright (c) 2001-3 Patrick Mochel |
| 14 | * |
| 15 | * 2003-10-10 Written by Simon Derr. |
| 16 | * 2003-10-22 Updates by Stephen Hemminger. |
| 17 | * 2004 May-July Rework by Paul Jackson. |
| 18 | * --------------------------------------------------- |
| 19 | * |
| 20 | * This file is subject to the terms and conditions of the GNU General Public |
| 21 | * License. See the file COPYING in the main directory of the Linux |
| 22 | * distribution for more details. |
| 23 | */ |
| 24 | |
| 25 | #include <linux/cgroup.h> |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 26 | #include <linux/ctype.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 27 | #include <linux/errno.h> |
| 28 | #include <linux/fs.h> |
| 29 | #include <linux/kernel.h> |
| 30 | #include <linux/list.h> |
| 31 | #include <linux/mm.h> |
| 32 | #include <linux/mutex.h> |
| 33 | #include <linux/mount.h> |
| 34 | #include <linux/pagemap.h> |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 35 | #include <linux/proc_fs.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 36 | #include <linux/rcupdate.h> |
| 37 | #include <linux/sched.h> |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 38 | #include <linux/backing-dev.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 39 | #include <linux/seq_file.h> |
| 40 | #include <linux/slab.h> |
| 41 | #include <linux/magic.h> |
| 42 | #include <linux/spinlock.h> |
| 43 | #include <linux/string.h> |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 44 | #include <linux/sort.h> |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 45 | #include <linux/kmod.h> |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 46 | #include <linux/delayacct.h> |
| 47 | #include <linux/cgroupstats.h> |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 48 | #include <linux/hash.h> |
Al Viro | 3f8206d | 2008-07-26 03:46:43 -0400 | [diff] [blame] | 49 | #include <linux/namei.h> |
Alessio Igor Bogani | 337eb00 | 2009-05-12 15:10:54 +0200 | [diff] [blame] | 50 | #include <linux/smp_lock.h> |
Li Zefan | 096b7fe | 2009-07-29 15:04:04 -0700 | [diff] [blame] | 51 | #include <linux/pid_namespace.h> |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 52 | #include <linux/idr.h> |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 53 | #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */ |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 54 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 55 | #include <asm/atomic.h> |
| 56 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 57 | static DEFINE_MUTEX(cgroup_mutex); |
| 58 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 59 | /* Generate an array of cgroup subsystem pointers */ |
| 60 | #define SUBSYS(_x) &_x ## _subsys, |
| 61 | |
| 62 | static struct cgroup_subsys *subsys[] = { |
| 63 | #include <linux/cgroup_subsys.h> |
| 64 | }; |
| 65 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 66 | #define MAX_CGROUP_ROOT_NAMELEN 64 |
| 67 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 68 | /* |
| 69 | * A cgroupfs_root represents the root of a cgroup hierarchy, |
| 70 | * and may be associated with a superblock to form an active |
| 71 | * hierarchy |
| 72 | */ |
| 73 | struct cgroupfs_root { |
| 74 | struct super_block *sb; |
| 75 | |
| 76 | /* |
| 77 | * The bitmask of subsystems intended to be attached to this |
| 78 | * hierarchy |
| 79 | */ |
| 80 | unsigned long subsys_bits; |
| 81 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 82 | /* Unique id for this hierarchy. */ |
| 83 | int hierarchy_id; |
| 84 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 85 | /* The bitmask of subsystems currently attached to this hierarchy */ |
| 86 | unsigned long actual_subsys_bits; |
| 87 | |
| 88 | /* A list running through the attached subsystems */ |
| 89 | struct list_head subsys_list; |
| 90 | |
| 91 | /* The root cgroup for this hierarchy */ |
| 92 | struct cgroup top_cgroup; |
| 93 | |
| 94 | /* Tracks how many cgroups are currently defined in hierarchy.*/ |
| 95 | int number_of_cgroups; |
| 96 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 97 | /* A list running through the active hierarchies */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 98 | struct list_head root_list; |
| 99 | |
| 100 | /* Hierarchy-specific flags */ |
| 101 | unsigned long flags; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 102 | |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 103 | /* The path to use for release notifications. */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 104 | char release_agent_path[PATH_MAX]; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 105 | |
| 106 | /* The name for this hierarchy - may be empty */ |
| 107 | char name[MAX_CGROUP_ROOT_NAMELEN]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 110 | /* |
| 111 | * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the |
| 112 | * subsystems that are otherwise unattached - it never has more than a |
| 113 | * single cgroup, and all tasks are part of that cgroup. |
| 114 | */ |
| 115 | static struct cgroupfs_root rootnode; |
| 116 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 117 | /* |
| 118 | * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when |
| 119 | * cgroup_subsys->use_id != 0. |
| 120 | */ |
| 121 | #define CSS_ID_MAX (65535) |
| 122 | struct css_id { |
| 123 | /* |
| 124 | * The css to which this ID points. This pointer is set to valid value |
| 125 | * after cgroup is populated. If cgroup is removed, this will be NULL. |
| 126 | * This pointer is expected to be RCU-safe because destroy() |
| 127 | * is called after synchronize_rcu(). But for safe use, css_is_removed() |
| 128 | * css_tryget() should be used for avoiding race. |
| 129 | */ |
| 130 | struct cgroup_subsys_state *css; |
| 131 | /* |
| 132 | * ID of this css. |
| 133 | */ |
| 134 | unsigned short id; |
| 135 | /* |
| 136 | * Depth in hierarchy which this ID belongs to. |
| 137 | */ |
| 138 | unsigned short depth; |
| 139 | /* |
| 140 | * ID is freed by RCU. (and lookup routine is RCU safe.) |
| 141 | */ |
| 142 | struct rcu_head rcu_head; |
| 143 | /* |
| 144 | * Hierarchy of CSS ID belongs to. |
| 145 | */ |
| 146 | unsigned short stack[0]; /* Array of Length (depth+1) */ |
| 147 | }; |
| 148 | |
| 149 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 150 | /* The list of hierarchy roots */ |
| 151 | |
| 152 | static LIST_HEAD(roots); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 153 | static int root_count; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 154 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 155 | static DEFINE_IDA(hierarchy_ida); |
| 156 | static int next_hierarchy_id; |
| 157 | static DEFINE_SPINLOCK(hierarchy_id_lock); |
| 158 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 159 | /* dummytop is a shorthand for the dummy hierarchy's top cgroup */ |
| 160 | #define dummytop (&rootnode.top_cgroup) |
| 161 | |
| 162 | /* This flag indicates whether tasks in the fork and exit paths should |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 163 | * check for fork/exit handlers to call. This avoids us having to do |
| 164 | * extra work in the fork/exit path if none of the subsystems need to |
| 165 | * be called. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 166 | */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 167 | static int need_forkexit_callback __read_mostly; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 168 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 169 | /* convenient tests for these bits */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 170 | inline int cgroup_is_removed(const struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 171 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 172 | return test_bit(CGRP_REMOVED, &cgrp->flags); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /* bits in struct cgroupfs_root flags field */ |
| 176 | enum { |
| 177 | ROOT_NOPREFIX, /* mounted subsystems have no named prefix */ |
| 178 | }; |
| 179 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 180 | static int cgroup_is_releasable(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 181 | { |
| 182 | const int bits = |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 183 | (1 << CGRP_RELEASABLE) | |
| 184 | (1 << CGRP_NOTIFY_ON_RELEASE); |
| 185 | return (cgrp->flags & bits) == bits; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 188 | static int notify_on_release(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 189 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 190 | return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 193 | /* |
| 194 | * for_each_subsys() allows you to iterate on each subsystem attached to |
| 195 | * an active hierarchy |
| 196 | */ |
| 197 | #define for_each_subsys(_root, _ss) \ |
| 198 | list_for_each_entry(_ss, &_root->subsys_list, sibling) |
| 199 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 200 | /* for_each_active_root() allows you to iterate across the active hierarchies */ |
| 201 | #define for_each_active_root(_root) \ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 202 | list_for_each_entry(_root, &roots, root_list) |
| 203 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 204 | /* the list of cgroups eligible for automatic release. Protected by |
| 205 | * release_list_lock */ |
| 206 | static LIST_HEAD(release_list); |
| 207 | static DEFINE_SPINLOCK(release_list_lock); |
| 208 | static void cgroup_release_agent(struct work_struct *work); |
| 209 | static DECLARE_WORK(release_agent_work, cgroup_release_agent); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 210 | static void check_for_release(struct cgroup *cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 211 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 212 | /* Link structure for associating css_set objects with cgroups */ |
| 213 | struct cg_cgroup_link { |
| 214 | /* |
| 215 | * List running through cg_cgroup_links associated with a |
| 216 | * cgroup, anchored on cgroup->css_sets |
| 217 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 218 | struct list_head cgrp_link_list; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 219 | struct cgroup *cgrp; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 220 | /* |
| 221 | * List running through cg_cgroup_links pointing at a |
| 222 | * single css_set object, anchored on css_set->cg_links |
| 223 | */ |
| 224 | struct list_head cg_link_list; |
| 225 | struct css_set *cg; |
| 226 | }; |
| 227 | |
| 228 | /* The default css_set - used by init and its children prior to any |
| 229 | * hierarchies being mounted. It contains a pointer to the root state |
| 230 | * for each subsystem. Also used to anchor the list of css_sets. Not |
| 231 | * reference-counted, to improve performance when child cgroups |
| 232 | * haven't been created. |
| 233 | */ |
| 234 | |
| 235 | static struct css_set init_css_set; |
| 236 | static struct cg_cgroup_link init_css_set_link; |
| 237 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 238 | static int cgroup_subsys_init_idr(struct cgroup_subsys *ss); |
| 239 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 240 | /* css_set_lock protects the list of css_set objects, and the |
| 241 | * chain of tasks off each css_set. Nests outside task->alloc_lock |
| 242 | * due to cgroup_iter_start() */ |
| 243 | static DEFINE_RWLOCK(css_set_lock); |
| 244 | static int css_set_count; |
| 245 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 246 | /* |
| 247 | * hash table for cgroup groups. This improves the performance to find |
| 248 | * an existing css_set. This hash doesn't (currently) take into |
| 249 | * account cgroups in empty hierarchies. |
| 250 | */ |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 251 | #define CSS_SET_HASH_BITS 7 |
| 252 | #define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS) |
| 253 | static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE]; |
| 254 | |
| 255 | static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[]) |
| 256 | { |
| 257 | int i; |
| 258 | int index; |
| 259 | unsigned long tmp = 0UL; |
| 260 | |
| 261 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) |
| 262 | tmp += (unsigned long)css[i]; |
| 263 | tmp = (tmp >> 16) ^ tmp; |
| 264 | |
| 265 | index = hash_long(tmp, CSS_SET_HASH_BITS); |
| 266 | |
| 267 | return &css_set_table[index]; |
| 268 | } |
| 269 | |
Ben Blum | c378369 | 2009-09-23 15:56:29 -0700 | [diff] [blame] | 270 | static void free_css_set_rcu(struct rcu_head *obj) |
| 271 | { |
| 272 | struct css_set *cg = container_of(obj, struct css_set, rcu_head); |
| 273 | kfree(cg); |
| 274 | } |
| 275 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 276 | /* We don't maintain the lists running through each css_set to its |
| 277 | * task until after the first call to cgroup_iter_start(). This |
| 278 | * reduces the fork()/exit() overhead for people who have cgroups |
| 279 | * compiled into their kernel but not actually in use */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 280 | static int use_task_css_set_links __read_mostly; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 281 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 282 | static void __put_css_set(struct css_set *cg, int taskexit) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 283 | { |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 284 | struct cg_cgroup_link *link; |
| 285 | struct cg_cgroup_link *saved_link; |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 286 | /* |
| 287 | * Ensure that the refcount doesn't hit zero while any readers |
| 288 | * can see it. Similar to atomic_dec_and_lock(), but for an |
| 289 | * rwlock |
| 290 | */ |
| 291 | if (atomic_add_unless(&cg->refcount, -1, 1)) |
| 292 | return; |
| 293 | write_lock(&css_set_lock); |
| 294 | if (!atomic_dec_and_test(&cg->refcount)) { |
| 295 | write_unlock(&css_set_lock); |
| 296 | return; |
| 297 | } |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 298 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 299 | /* This css_set is dead. unlink it and release cgroup refcounts */ |
| 300 | hlist_del(&cg->hlist); |
| 301 | css_set_count--; |
| 302 | |
| 303 | list_for_each_entry_safe(link, saved_link, &cg->cg_links, |
| 304 | cg_link_list) { |
| 305 | struct cgroup *cgrp = link->cgrp; |
| 306 | list_del(&link->cg_link_list); |
| 307 | list_del(&link->cgrp_link_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 308 | if (atomic_dec_and_test(&cgrp->count) && |
| 309 | notify_on_release(cgrp)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 310 | if (taskexit) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 311 | set_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 312 | check_for_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 313 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 314 | |
| 315 | kfree(link); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 316 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 317 | |
| 318 | write_unlock(&css_set_lock); |
Ben Blum | c378369 | 2009-09-23 15:56:29 -0700 | [diff] [blame] | 319 | call_rcu(&cg->rcu_head, free_css_set_rcu); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | /* |
| 323 | * refcounted get/put for css_set objects |
| 324 | */ |
| 325 | static inline void get_css_set(struct css_set *cg) |
| 326 | { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 327 | atomic_inc(&cg->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | static inline void put_css_set(struct css_set *cg) |
| 331 | { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 332 | __put_css_set(cg, 0); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 335 | static inline void put_css_set_taskexit(struct css_set *cg) |
| 336 | { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 337 | __put_css_set(cg, 1); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 340 | /* |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 341 | * compare_css_sets - helper function for find_existing_css_set(). |
| 342 | * @cg: candidate css_set being tested |
| 343 | * @old_cg: existing css_set for a task |
| 344 | * @new_cgrp: cgroup that's being entered by the task |
| 345 | * @template: desired set of css pointers in css_set (pre-calculated) |
| 346 | * |
| 347 | * Returns true if "cg" matches "old_cg" except for the hierarchy |
| 348 | * which "new_cgrp" belongs to, for which it should match "new_cgrp". |
| 349 | */ |
| 350 | static bool compare_css_sets(struct css_set *cg, |
| 351 | struct css_set *old_cg, |
| 352 | struct cgroup *new_cgrp, |
| 353 | struct cgroup_subsys_state *template[]) |
| 354 | { |
| 355 | struct list_head *l1, *l2; |
| 356 | |
| 357 | if (memcmp(template, cg->subsys, sizeof(cg->subsys))) { |
| 358 | /* Not all subsystems matched */ |
| 359 | return false; |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | * Compare cgroup pointers in order to distinguish between |
| 364 | * different cgroups in heirarchies with no subsystems. We |
| 365 | * could get by with just this check alone (and skip the |
| 366 | * memcmp above) but on most setups the memcmp check will |
| 367 | * avoid the need for this more expensive check on almost all |
| 368 | * candidates. |
| 369 | */ |
| 370 | |
| 371 | l1 = &cg->cg_links; |
| 372 | l2 = &old_cg->cg_links; |
| 373 | while (1) { |
| 374 | struct cg_cgroup_link *cgl1, *cgl2; |
| 375 | struct cgroup *cg1, *cg2; |
| 376 | |
| 377 | l1 = l1->next; |
| 378 | l2 = l2->next; |
| 379 | /* See if we reached the end - both lists are equal length. */ |
| 380 | if (l1 == &cg->cg_links) { |
| 381 | BUG_ON(l2 != &old_cg->cg_links); |
| 382 | break; |
| 383 | } else { |
| 384 | BUG_ON(l2 == &old_cg->cg_links); |
| 385 | } |
| 386 | /* Locate the cgroups associated with these links. */ |
| 387 | cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list); |
| 388 | cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list); |
| 389 | cg1 = cgl1->cgrp; |
| 390 | cg2 = cgl2->cgrp; |
| 391 | /* Hierarchies should be linked in the same order. */ |
| 392 | BUG_ON(cg1->root != cg2->root); |
| 393 | |
| 394 | /* |
| 395 | * If this hierarchy is the hierarchy of the cgroup |
| 396 | * that's changing, then we need to check that this |
| 397 | * css_set points to the new cgroup; if it's any other |
| 398 | * hierarchy, then this css_set should point to the |
| 399 | * same cgroup as the old css_set. |
| 400 | */ |
| 401 | if (cg1->root == new_cgrp->root) { |
| 402 | if (cg1 != new_cgrp) |
| 403 | return false; |
| 404 | } else { |
| 405 | if (cg1 != cg2) |
| 406 | return false; |
| 407 | } |
| 408 | } |
| 409 | return true; |
| 410 | } |
| 411 | |
| 412 | /* |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 413 | * find_existing_css_set() is a helper for |
| 414 | * find_css_set(), and checks to see whether an existing |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 415 | * css_set is suitable. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 416 | * |
| 417 | * oldcg: the cgroup group that we're using before the cgroup |
| 418 | * transition |
| 419 | * |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 420 | * cgrp: the cgroup that we're moving into |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 421 | * |
| 422 | * template: location in which to build the desired set of subsystem |
| 423 | * state objects for the new cgroup group |
| 424 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 425 | static struct css_set *find_existing_css_set( |
| 426 | struct css_set *oldcg, |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 427 | struct cgroup *cgrp, |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 428 | struct cgroup_subsys_state *template[]) |
| 429 | { |
| 430 | int i; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 431 | struct cgroupfs_root *root = cgrp->root; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 432 | struct hlist_head *hhead; |
| 433 | struct hlist_node *node; |
| 434 | struct css_set *cg; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 435 | |
| 436 | /* Built the set of subsystem state objects that we want to |
| 437 | * see in the new css_set */ |
| 438 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Li Zefan | 8d53d55 | 2008-02-23 15:24:11 -0800 | [diff] [blame] | 439 | if (root->subsys_bits & (1UL << i)) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 440 | /* Subsystem is in this hierarchy. So we want |
| 441 | * the subsystem state from the new |
| 442 | * cgroup */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 443 | template[i] = cgrp->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 444 | } else { |
| 445 | /* Subsystem is not in this hierarchy, so we |
| 446 | * don't want to change the subsystem state */ |
| 447 | template[i] = oldcg->subsys[i]; |
| 448 | } |
| 449 | } |
| 450 | |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 451 | hhead = css_set_hash(template); |
| 452 | hlist_for_each_entry(cg, node, hhead, hlist) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 453 | if (!compare_css_sets(cg, oldcg, cgrp, template)) |
| 454 | continue; |
| 455 | |
| 456 | /* This css_set matches what we need */ |
| 457 | return cg; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 458 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 459 | |
| 460 | /* No existing cgroup group matched */ |
| 461 | return NULL; |
| 462 | } |
| 463 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 464 | static void free_cg_links(struct list_head *tmp) |
| 465 | { |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 466 | struct cg_cgroup_link *link; |
| 467 | struct cg_cgroup_link *saved_link; |
| 468 | |
| 469 | list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 470 | list_del(&link->cgrp_link_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 471 | kfree(link); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | /* |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 476 | * allocate_cg_links() allocates "count" cg_cgroup_link structures |
| 477 | * and chains them on tmp through their cgrp_link_list fields. Returns 0 on |
| 478 | * success or a negative error |
| 479 | */ |
| 480 | static int allocate_cg_links(int count, struct list_head *tmp) |
| 481 | { |
| 482 | struct cg_cgroup_link *link; |
| 483 | int i; |
| 484 | INIT_LIST_HEAD(tmp); |
| 485 | for (i = 0; i < count; i++) { |
| 486 | link = kmalloc(sizeof(*link), GFP_KERNEL); |
| 487 | if (!link) { |
| 488 | free_cg_links(tmp); |
| 489 | return -ENOMEM; |
| 490 | } |
| 491 | list_add(&link->cgrp_link_list, tmp); |
| 492 | } |
| 493 | return 0; |
| 494 | } |
| 495 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 496 | /** |
| 497 | * link_css_set - a helper function to link a css_set to a cgroup |
| 498 | * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links() |
| 499 | * @cg: the css_set to be linked |
| 500 | * @cgrp: the destination cgroup |
| 501 | */ |
| 502 | static void link_css_set(struct list_head *tmp_cg_links, |
| 503 | struct css_set *cg, struct cgroup *cgrp) |
| 504 | { |
| 505 | struct cg_cgroup_link *link; |
| 506 | |
| 507 | BUG_ON(list_empty(tmp_cg_links)); |
| 508 | link = list_first_entry(tmp_cg_links, struct cg_cgroup_link, |
| 509 | cgrp_link_list); |
| 510 | link->cg = cg; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 511 | link->cgrp = cgrp; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 512 | atomic_inc(&cgrp->count); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 513 | list_move(&link->cgrp_link_list, &cgrp->css_sets); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 514 | /* |
| 515 | * Always add links to the tail of the list so that the list |
| 516 | * is sorted by order of hierarchy creation |
| 517 | */ |
| 518 | list_add_tail(&link->cg_link_list, &cg->cg_links); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 519 | } |
| 520 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 521 | /* |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 522 | * find_css_set() takes an existing cgroup group and a |
| 523 | * cgroup object, and returns a css_set object that's |
| 524 | * equivalent to the old group, but with the given cgroup |
| 525 | * substituted into the appropriate hierarchy. Must be called with |
| 526 | * cgroup_mutex held |
| 527 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 528 | static struct css_set *find_css_set( |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 529 | struct css_set *oldcg, struct cgroup *cgrp) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 530 | { |
| 531 | struct css_set *res; |
| 532 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 533 | |
| 534 | struct list_head tmp_cg_links; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 535 | |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 536 | struct hlist_head *hhead; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 537 | struct cg_cgroup_link *link; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 538 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 539 | /* First see if we already have a cgroup group that matches |
| 540 | * the desired set */ |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 541 | read_lock(&css_set_lock); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 542 | res = find_existing_css_set(oldcg, cgrp, template); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 543 | if (res) |
| 544 | get_css_set(res); |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 545 | read_unlock(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 546 | |
| 547 | if (res) |
| 548 | return res; |
| 549 | |
| 550 | res = kmalloc(sizeof(*res), GFP_KERNEL); |
| 551 | if (!res) |
| 552 | return NULL; |
| 553 | |
| 554 | /* Allocate all the cg_cgroup_link objects that we'll need */ |
| 555 | if (allocate_cg_links(root_count, &tmp_cg_links) < 0) { |
| 556 | kfree(res); |
| 557 | return NULL; |
| 558 | } |
| 559 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 560 | atomic_set(&res->refcount, 1); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 561 | INIT_LIST_HEAD(&res->cg_links); |
| 562 | INIT_LIST_HEAD(&res->tasks); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 563 | INIT_HLIST_NODE(&res->hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 564 | |
| 565 | /* Copy the set of subsystem state objects generated in |
| 566 | * find_existing_css_set() */ |
| 567 | memcpy(res->subsys, template, sizeof(res->subsys)); |
| 568 | |
| 569 | write_lock(&css_set_lock); |
| 570 | /* Add reference counts and links from the new css_set. */ |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 571 | list_for_each_entry(link, &oldcg->cg_links, cg_link_list) { |
| 572 | struct cgroup *c = link->cgrp; |
| 573 | if (c->root == cgrp->root) |
| 574 | c = cgrp; |
| 575 | link_css_set(&tmp_cg_links, res, c); |
| 576 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 577 | |
| 578 | BUG_ON(!list_empty(&tmp_cg_links)); |
| 579 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 580 | css_set_count++; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 581 | |
| 582 | /* Add this cgroup group to the hash table */ |
| 583 | hhead = css_set_hash(res->subsys); |
| 584 | hlist_add_head(&res->hlist, hhead); |
| 585 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 586 | write_unlock(&css_set_lock); |
| 587 | |
| 588 | return res; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 589 | } |
| 590 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 591 | /* |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 592 | * Return the cgroup for "task" from the given hierarchy. Must be |
| 593 | * called with cgroup_mutex held. |
| 594 | */ |
| 595 | static struct cgroup *task_cgroup_from_root(struct task_struct *task, |
| 596 | struct cgroupfs_root *root) |
| 597 | { |
| 598 | struct css_set *css; |
| 599 | struct cgroup *res = NULL; |
| 600 | |
| 601 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 602 | read_lock(&css_set_lock); |
| 603 | /* |
| 604 | * No need to lock the task - since we hold cgroup_mutex the |
| 605 | * task can't change groups, so the only thing that can happen |
| 606 | * is that it exits and its css is set back to init_css_set. |
| 607 | */ |
| 608 | css = task->cgroups; |
| 609 | if (css == &init_css_set) { |
| 610 | res = &root->top_cgroup; |
| 611 | } else { |
| 612 | struct cg_cgroup_link *link; |
| 613 | list_for_each_entry(link, &css->cg_links, cg_link_list) { |
| 614 | struct cgroup *c = link->cgrp; |
| 615 | if (c->root == root) { |
| 616 | res = c; |
| 617 | break; |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | read_unlock(&css_set_lock); |
| 622 | BUG_ON(!res); |
| 623 | return res; |
| 624 | } |
| 625 | |
| 626 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 627 | * There is one global cgroup mutex. We also require taking |
| 628 | * task_lock() when dereferencing a task's cgroup subsys pointers. |
| 629 | * See "The task_lock() exception", at the end of this comment. |
| 630 | * |
| 631 | * A task must hold cgroup_mutex to modify cgroups. |
| 632 | * |
| 633 | * Any task can increment and decrement the count field without lock. |
| 634 | * So in general, code holding cgroup_mutex can't rely on the count |
| 635 | * field not changing. However, if the count goes to zero, then only |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 636 | * cgroup_attach_task() can increment it again. Because a count of zero |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 637 | * means that no tasks are currently attached, therefore there is no |
| 638 | * way a task attached to that cgroup can fork (the other way to |
| 639 | * increment the count). So code holding cgroup_mutex can safely |
| 640 | * assume that if the count is zero, it will stay zero. Similarly, if |
| 641 | * a task holds cgroup_mutex on a cgroup with zero count, it |
| 642 | * knows that the cgroup won't be removed, as cgroup_rmdir() |
| 643 | * needs that mutex. |
| 644 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 645 | * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't |
| 646 | * (usually) take cgroup_mutex. These are the two most performance |
| 647 | * critical pieces of code here. The exception occurs on cgroup_exit(), |
| 648 | * when a task in a notify_on_release cgroup exits. Then cgroup_mutex |
| 649 | * 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] | 650 | * to the release agent with the name of the cgroup (path relative to |
| 651 | * the root of cgroup file system) as the argument. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 652 | * |
| 653 | * A cgroup can only be deleted if both its 'count' of using tasks |
| 654 | * is zero, and its list of 'children' cgroups is empty. Since all |
| 655 | * tasks in the system use _some_ cgroup, and since there is always at |
| 656 | * least one task in the system (init, pid == 1), therefore, top_cgroup |
| 657 | * always has either children cgroups and/or using tasks. So we don't |
| 658 | * need a special hack to ensure that top_cgroup cannot be deleted. |
| 659 | * |
| 660 | * The task_lock() exception |
| 661 | * |
| 662 | * The need for this exception arises from the action of |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 663 | * cgroup_attach_task(), which overwrites one tasks cgroup pointer with |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 664 | * another. It does so using cgroup_mutex, however there are |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 665 | * several performance critical places that need to reference |
| 666 | * task->cgroup without the expense of grabbing a system global |
| 667 | * mutex. Therefore except as noted below, when dereferencing or, as |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 668 | * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 669 | * task_lock(), which acts on a spinlock (task->alloc_lock) already in |
| 670 | * the task_struct routinely used for such matters. |
| 671 | * |
| 672 | * P.S. One more locking exception. RCU is used to guard the |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 673 | * update of a tasks cgroup pointer by cgroup_attach_task() |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 674 | */ |
| 675 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 676 | /** |
| 677 | * cgroup_lock - lock out any changes to cgroup structures |
| 678 | * |
| 679 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 680 | void cgroup_lock(void) |
| 681 | { |
| 682 | mutex_lock(&cgroup_mutex); |
| 683 | } |
| 684 | |
| 685 | /** |
| 686 | * cgroup_unlock - release lock on cgroup changes |
| 687 | * |
| 688 | * Undo the lock taken in a previous cgroup_lock() call. |
| 689 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 690 | void cgroup_unlock(void) |
| 691 | { |
| 692 | mutex_unlock(&cgroup_mutex); |
| 693 | } |
| 694 | |
| 695 | /* |
| 696 | * A couple of forward declarations required, due to cyclic reference loop: |
| 697 | * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir -> |
| 698 | * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations |
| 699 | * -> cgroup_mkdir. |
| 700 | */ |
| 701 | |
| 702 | static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode); |
| 703 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 704 | static int cgroup_populate_dir(struct cgroup *cgrp); |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 705 | static const struct inode_operations cgroup_dir_inode_operations; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 706 | static const struct file_operations proc_cgroupstats_operations; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 707 | |
| 708 | static struct backing_dev_info cgroup_backing_dev_info = { |
Jens Axboe | d993831 | 2009-06-12 14:45:52 +0200 | [diff] [blame] | 709 | .name = "cgroup", |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 710 | .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK, |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 711 | }; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 712 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 713 | static int alloc_css_id(struct cgroup_subsys *ss, |
| 714 | struct cgroup *parent, struct cgroup *child); |
| 715 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 716 | static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb) |
| 717 | { |
| 718 | struct inode *inode = new_inode(sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 719 | |
| 720 | if (inode) { |
| 721 | inode->i_mode = mode; |
David Howells | 76aac0e | 2008-11-14 10:39:12 +1100 | [diff] [blame] | 722 | inode->i_uid = current_fsuid(); |
| 723 | inode->i_gid = current_fsgid(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 724 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 725 | inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info; |
| 726 | } |
| 727 | return inode; |
| 728 | } |
| 729 | |
KAMEZAWA Hiroyuki | 4fca88c | 2008-02-07 00:14:27 -0800 | [diff] [blame] | 730 | /* |
| 731 | * Call subsys's pre_destroy handler. |
| 732 | * This is called before css refcnt check. |
| 733 | */ |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 734 | static int cgroup_call_pre_destroy(struct cgroup *cgrp) |
KAMEZAWA Hiroyuki | 4fca88c | 2008-02-07 00:14:27 -0800 | [diff] [blame] | 735 | { |
| 736 | struct cgroup_subsys *ss; |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 737 | int ret = 0; |
| 738 | |
KAMEZAWA Hiroyuki | 4fca88c | 2008-02-07 00:14:27 -0800 | [diff] [blame] | 739 | for_each_subsys(cgrp->root, ss) |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 740 | if (ss->pre_destroy) { |
| 741 | ret = ss->pre_destroy(ss, cgrp); |
| 742 | if (ret) |
| 743 | break; |
| 744 | } |
| 745 | return ret; |
KAMEZAWA Hiroyuki | 4fca88c | 2008-02-07 00:14:27 -0800 | [diff] [blame] | 746 | } |
| 747 | |
Paul Menage | a47295e | 2009-01-07 18:07:44 -0800 | [diff] [blame] | 748 | static void free_cgroup_rcu(struct rcu_head *obj) |
| 749 | { |
| 750 | struct cgroup *cgrp = container_of(obj, struct cgroup, rcu_head); |
| 751 | |
| 752 | kfree(cgrp); |
| 753 | } |
| 754 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 755 | static void cgroup_diput(struct dentry *dentry, struct inode *inode) |
| 756 | { |
| 757 | /* is dentry a directory ? if so, kfree() associated cgroup */ |
| 758 | if (S_ISDIR(inode->i_mode)) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 759 | struct cgroup *cgrp = dentry->d_fsdata; |
Paul Menage | 8dc4f3e | 2008-02-07 00:13:45 -0800 | [diff] [blame] | 760 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 761 | BUG_ON(!(cgroup_is_removed(cgrp))); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 762 | /* It's possible for external users to be holding css |
| 763 | * reference counts on a cgroup; css_put() needs to |
| 764 | * be able to access the cgroup after decrementing |
| 765 | * the reference count in order to know if it needs to |
| 766 | * queue the cgroup to be handled by the release |
| 767 | * agent */ |
| 768 | synchronize_rcu(); |
Paul Menage | 8dc4f3e | 2008-02-07 00:13:45 -0800 | [diff] [blame] | 769 | |
| 770 | mutex_lock(&cgroup_mutex); |
| 771 | /* |
| 772 | * Release the subsystem state objects. |
| 773 | */ |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 774 | for_each_subsys(cgrp->root, ss) |
| 775 | ss->destroy(ss, cgrp); |
Paul Menage | 8dc4f3e | 2008-02-07 00:13:45 -0800 | [diff] [blame] | 776 | |
| 777 | cgrp->root->number_of_cgroups--; |
| 778 | mutex_unlock(&cgroup_mutex); |
| 779 | |
Paul Menage | a47295e | 2009-01-07 18:07:44 -0800 | [diff] [blame] | 780 | /* |
| 781 | * Drop the active superblock reference that we took when we |
| 782 | * created the cgroup |
| 783 | */ |
Paul Menage | 8dc4f3e | 2008-02-07 00:13:45 -0800 | [diff] [blame] | 784 | deactivate_super(cgrp->root->sb); |
| 785 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 786 | /* |
| 787 | * if we're getting rid of the cgroup, refcount should ensure |
| 788 | * that there are no pidlists left. |
| 789 | */ |
| 790 | BUG_ON(!list_empty(&cgrp->pidlists)); |
| 791 | |
Paul Menage | a47295e | 2009-01-07 18:07:44 -0800 | [diff] [blame] | 792 | call_rcu(&cgrp->rcu_head, free_cgroup_rcu); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 793 | } |
| 794 | iput(inode); |
| 795 | } |
| 796 | |
| 797 | static void remove_dir(struct dentry *d) |
| 798 | { |
| 799 | struct dentry *parent = dget(d->d_parent); |
| 800 | |
| 801 | d_delete(d); |
| 802 | simple_rmdir(parent->d_inode, d); |
| 803 | dput(parent); |
| 804 | } |
| 805 | |
| 806 | static void cgroup_clear_directory(struct dentry *dentry) |
| 807 | { |
| 808 | struct list_head *node; |
| 809 | |
| 810 | BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex)); |
| 811 | spin_lock(&dcache_lock); |
| 812 | node = dentry->d_subdirs.next; |
| 813 | while (node != &dentry->d_subdirs) { |
| 814 | struct dentry *d = list_entry(node, struct dentry, d_u.d_child); |
| 815 | list_del_init(node); |
| 816 | if (d->d_inode) { |
| 817 | /* This should never be called on a cgroup |
| 818 | * directory with child cgroups */ |
| 819 | BUG_ON(d->d_inode->i_mode & S_IFDIR); |
| 820 | d = dget_locked(d); |
| 821 | spin_unlock(&dcache_lock); |
| 822 | d_delete(d); |
| 823 | simple_unlink(dentry->d_inode, d); |
| 824 | dput(d); |
| 825 | spin_lock(&dcache_lock); |
| 826 | } |
| 827 | node = dentry->d_subdirs.next; |
| 828 | } |
| 829 | spin_unlock(&dcache_lock); |
| 830 | } |
| 831 | |
| 832 | /* |
| 833 | * NOTE : the dentry must have been dget()'ed |
| 834 | */ |
| 835 | static void cgroup_d_remove_dir(struct dentry *dentry) |
| 836 | { |
| 837 | cgroup_clear_directory(dentry); |
| 838 | |
| 839 | spin_lock(&dcache_lock); |
| 840 | list_del_init(&dentry->d_u.d_child); |
| 841 | spin_unlock(&dcache_lock); |
| 842 | remove_dir(dentry); |
| 843 | } |
| 844 | |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 845 | /* |
| 846 | * A queue for waiters to do rmdir() cgroup. A tasks will sleep when |
| 847 | * cgroup->count == 0 && list_empty(&cgroup->children) && subsys has some |
| 848 | * reference to css->refcnt. In general, this refcnt is expected to goes down |
| 849 | * to zero, soon. |
| 850 | * |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 851 | * 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] | 852 | */ |
| 853 | DECLARE_WAIT_QUEUE_HEAD(cgroup_rmdir_waitq); |
| 854 | |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 855 | static void cgroup_wakeup_rmdir_waiter(struct cgroup *cgrp) |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 856 | { |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 857 | if (unlikely(test_and_clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags))) |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 858 | wake_up_all(&cgroup_rmdir_waitq); |
| 859 | } |
| 860 | |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 861 | void cgroup_exclude_rmdir(struct cgroup_subsys_state *css) |
| 862 | { |
| 863 | css_get(css); |
| 864 | } |
| 865 | |
| 866 | void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state *css) |
| 867 | { |
| 868 | cgroup_wakeup_rmdir_waiter(css->cgroup); |
| 869 | css_put(css); |
| 870 | } |
| 871 | |
| 872 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 873 | static int rebind_subsystems(struct cgroupfs_root *root, |
| 874 | unsigned long final_bits) |
| 875 | { |
| 876 | unsigned long added_bits, removed_bits; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 877 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 878 | int i; |
| 879 | |
| 880 | removed_bits = root->actual_subsys_bits & ~final_bits; |
| 881 | added_bits = final_bits & ~root->actual_subsys_bits; |
| 882 | /* Check that any added subsystems are currently free */ |
| 883 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Li Zefan | 8d53d55 | 2008-02-23 15:24:11 -0800 | [diff] [blame] | 884 | unsigned long bit = 1UL << i; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 885 | struct cgroup_subsys *ss = subsys[i]; |
| 886 | if (!(bit & added_bits)) |
| 887 | continue; |
| 888 | if (ss->root != &rootnode) { |
| 889 | /* Subsystem isn't free */ |
| 890 | return -EBUSY; |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | /* Currently we don't handle adding/removing subsystems when |
| 895 | * any child cgroups exist. This is theoretically supportable |
| 896 | * but involves complex error handling, so it's being left until |
| 897 | * later */ |
Paul Menage | 307257c | 2008-12-15 13:54:22 -0800 | [diff] [blame] | 898 | if (root->number_of_cgroups > 1) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 899 | return -EBUSY; |
| 900 | |
| 901 | /* Process each subsystem */ |
| 902 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 903 | struct cgroup_subsys *ss = subsys[i]; |
| 904 | unsigned long bit = 1UL << i; |
| 905 | if (bit & added_bits) { |
| 906 | /* We're binding this subsystem to this hierarchy */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 907 | BUG_ON(cgrp->subsys[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 908 | BUG_ON(!dummytop->subsys[i]); |
| 909 | BUG_ON(dummytop->subsys[i]->cgroup != dummytop); |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 910 | mutex_lock(&ss->hierarchy_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 911 | cgrp->subsys[i] = dummytop->subsys[i]; |
| 912 | cgrp->subsys[i]->cgroup = cgrp; |
Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 913 | list_move(&ss->sibling, &root->subsys_list); |
Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 914 | ss->root = root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 915 | if (ss->bind) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 916 | ss->bind(ss, cgrp); |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 917 | mutex_unlock(&ss->hierarchy_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 918 | } else if (bit & removed_bits) { |
| 919 | /* We're removing this subsystem */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 920 | BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]); |
| 921 | BUG_ON(cgrp->subsys[i]->cgroup != cgrp); |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 922 | mutex_lock(&ss->hierarchy_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 923 | if (ss->bind) |
| 924 | ss->bind(ss, dummytop); |
| 925 | dummytop->subsys[i]->cgroup = dummytop; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 926 | cgrp->subsys[i] = NULL; |
Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 927 | subsys[i]->root = &rootnode; |
Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 928 | list_move(&ss->sibling, &rootnode.subsys_list); |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 929 | mutex_unlock(&ss->hierarchy_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 930 | } else if (bit & final_bits) { |
| 931 | /* Subsystem state should already exist */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 932 | BUG_ON(!cgrp->subsys[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 933 | } else { |
| 934 | /* Subsystem state shouldn't exist */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 935 | BUG_ON(cgrp->subsys[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 936 | } |
| 937 | } |
| 938 | root->subsys_bits = root->actual_subsys_bits = final_bits; |
| 939 | synchronize_rcu(); |
| 940 | |
| 941 | return 0; |
| 942 | } |
| 943 | |
| 944 | static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs) |
| 945 | { |
| 946 | struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info; |
| 947 | struct cgroup_subsys *ss; |
| 948 | |
| 949 | mutex_lock(&cgroup_mutex); |
| 950 | for_each_subsys(root, ss) |
| 951 | seq_printf(seq, ",%s", ss->name); |
| 952 | if (test_bit(ROOT_NOPREFIX, &root->flags)) |
| 953 | seq_puts(seq, ",noprefix"); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 954 | if (strlen(root->release_agent_path)) |
| 955 | seq_printf(seq, ",release_agent=%s", root->release_agent_path); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 956 | if (strlen(root->name)) |
| 957 | seq_printf(seq, ",name=%s", root->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 958 | mutex_unlock(&cgroup_mutex); |
| 959 | return 0; |
| 960 | } |
| 961 | |
| 962 | struct cgroup_sb_opts { |
| 963 | unsigned long subsys_bits; |
| 964 | unsigned long flags; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 965 | char *release_agent; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 966 | char *name; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 967 | /* User explicitly requested empty subsystem */ |
| 968 | bool none; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 969 | |
| 970 | struct cgroupfs_root *new_root; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 971 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 972 | }; |
| 973 | |
| 974 | /* Convert a hierarchy specifier into a bitmask of subsystems and |
| 975 | * flags. */ |
| 976 | static int parse_cgroupfs_options(char *data, |
| 977 | struct cgroup_sb_opts *opts) |
| 978 | { |
| 979 | char *token, *o = data ?: "all"; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 980 | unsigned long mask = (unsigned long)-1; |
| 981 | |
| 982 | #ifdef CONFIG_CPUSETS |
| 983 | mask = ~(1UL << cpuset_subsys_id); |
| 984 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 985 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 986 | memset(opts, 0, sizeof(*opts)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 987 | |
| 988 | while ((token = strsep(&o, ",")) != NULL) { |
| 989 | if (!*token) |
| 990 | return -EINVAL; |
| 991 | if (!strcmp(token, "all")) { |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 992 | /* Add all non-disabled subsystems */ |
| 993 | int i; |
| 994 | opts->subsys_bits = 0; |
| 995 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 996 | struct cgroup_subsys *ss = subsys[i]; |
| 997 | if (!ss->disabled) |
| 998 | opts->subsys_bits |= 1ul << i; |
| 999 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1000 | } else if (!strcmp(token, "none")) { |
| 1001 | /* Explicitly have no subsystems */ |
| 1002 | opts->none = true; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1003 | } else if (!strcmp(token, "noprefix")) { |
| 1004 | set_bit(ROOT_NOPREFIX, &opts->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1005 | } else if (!strncmp(token, "release_agent=", 14)) { |
| 1006 | /* Specifying two release agents is forbidden */ |
| 1007 | if (opts->release_agent) |
| 1008 | return -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1009 | opts->release_agent = |
| 1010 | kstrndup(token + 14, PATH_MAX, GFP_KERNEL); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1011 | if (!opts->release_agent) |
| 1012 | return -ENOMEM; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1013 | } else if (!strncmp(token, "name=", 5)) { |
| 1014 | int i; |
| 1015 | const char *name = token + 5; |
| 1016 | /* Can't specify an empty name */ |
| 1017 | if (!strlen(name)) |
| 1018 | return -EINVAL; |
| 1019 | /* Must match [\w.-]+ */ |
| 1020 | for (i = 0; i < strlen(name); i++) { |
| 1021 | char c = name[i]; |
| 1022 | if (isalnum(c)) |
| 1023 | continue; |
| 1024 | if ((c == '.') || (c == '-') || (c == '_')) |
| 1025 | continue; |
| 1026 | return -EINVAL; |
| 1027 | } |
| 1028 | /* Specifying two names is forbidden */ |
| 1029 | if (opts->name) |
| 1030 | return -EINVAL; |
| 1031 | opts->name = kstrndup(name, |
| 1032 | MAX_CGROUP_ROOT_NAMELEN, |
| 1033 | GFP_KERNEL); |
| 1034 | if (!opts->name) |
| 1035 | return -ENOMEM; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1036 | } else { |
| 1037 | struct cgroup_subsys *ss; |
| 1038 | int i; |
| 1039 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 1040 | ss = subsys[i]; |
| 1041 | if (!strcmp(token, ss->name)) { |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 1042 | if (!ss->disabled) |
| 1043 | set_bit(i, &opts->subsys_bits); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1044 | break; |
| 1045 | } |
| 1046 | } |
| 1047 | if (i == CGROUP_SUBSYS_COUNT) |
| 1048 | return -ENOENT; |
| 1049 | } |
| 1050 | } |
| 1051 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1052 | /* Consistency checks */ |
| 1053 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1054 | /* |
| 1055 | * Option noprefix was introduced just for backward compatibility |
| 1056 | * with the old cpuset, so we allow noprefix only if mounting just |
| 1057 | * the cpuset subsystem. |
| 1058 | */ |
| 1059 | if (test_bit(ROOT_NOPREFIX, &opts->flags) && |
| 1060 | (opts->subsys_bits & mask)) |
| 1061 | return -EINVAL; |
| 1062 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1063 | |
| 1064 | /* Can't specify "none" and some subsystems */ |
| 1065 | if (opts->subsys_bits && opts->none) |
| 1066 | return -EINVAL; |
| 1067 | |
| 1068 | /* |
| 1069 | * We either have to specify by name or by subsystems. (So all |
| 1070 | * empty hierarchies must have a name). |
| 1071 | */ |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1072 | if (!opts->subsys_bits && !opts->name) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1073 | return -EINVAL; |
| 1074 | |
| 1075 | return 0; |
| 1076 | } |
| 1077 | |
| 1078 | static int cgroup_remount(struct super_block *sb, int *flags, char *data) |
| 1079 | { |
| 1080 | int ret = 0; |
| 1081 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1082 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1083 | struct cgroup_sb_opts opts; |
| 1084 | |
Alessio Igor Bogani | 337eb00 | 2009-05-12 15:10:54 +0200 | [diff] [blame] | 1085 | lock_kernel(); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1086 | mutex_lock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1087 | mutex_lock(&cgroup_mutex); |
| 1088 | |
| 1089 | /* See what subsystems are wanted */ |
| 1090 | ret = parse_cgroupfs_options(data, &opts); |
| 1091 | if (ret) |
| 1092 | goto out_unlock; |
| 1093 | |
| 1094 | /* Don't allow flags to change at remount */ |
| 1095 | if (opts.flags != root->flags) { |
| 1096 | ret = -EINVAL; |
| 1097 | goto out_unlock; |
| 1098 | } |
| 1099 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1100 | /* Don't allow name to change at remount */ |
| 1101 | if (opts.name && strcmp(opts.name, root->name)) { |
| 1102 | ret = -EINVAL; |
| 1103 | goto out_unlock; |
| 1104 | } |
| 1105 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1106 | ret = rebind_subsystems(root, opts.subsys_bits); |
Li Zefan | 0670e08 | 2009-04-02 16:57:30 -0700 | [diff] [blame] | 1107 | if (ret) |
| 1108 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1109 | |
| 1110 | /* (re)populate subsystem files */ |
Li Zefan | 0670e08 | 2009-04-02 16:57:30 -0700 | [diff] [blame] | 1111 | cgroup_populate_dir(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1112 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1113 | if (opts.release_agent) |
| 1114 | strcpy(root->release_agent_path, opts.release_agent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1115 | out_unlock: |
Jesper Juhl | 66bdc9c | 2009-04-02 16:57:27 -0700 | [diff] [blame] | 1116 | kfree(opts.release_agent); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1117 | kfree(opts.name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1118 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1119 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Alessio Igor Bogani | 337eb00 | 2009-05-12 15:10:54 +0200 | [diff] [blame] | 1120 | unlock_kernel(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1121 | return ret; |
| 1122 | } |
| 1123 | |
Alexey Dobriyan | b87221d | 2009-09-21 17:01:09 -0700 | [diff] [blame] | 1124 | static const struct super_operations cgroup_ops = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1125 | .statfs = simple_statfs, |
| 1126 | .drop_inode = generic_delete_inode, |
| 1127 | .show_options = cgroup_show_options, |
| 1128 | .remount_fs = cgroup_remount, |
| 1129 | }; |
| 1130 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1131 | static void init_cgroup_housekeeping(struct cgroup *cgrp) |
| 1132 | { |
| 1133 | INIT_LIST_HEAD(&cgrp->sibling); |
| 1134 | INIT_LIST_HEAD(&cgrp->children); |
| 1135 | INIT_LIST_HEAD(&cgrp->css_sets); |
| 1136 | INIT_LIST_HEAD(&cgrp->release_list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1137 | INIT_LIST_HEAD(&cgrp->pidlists); |
| 1138 | mutex_init(&cgrp->pidlist_mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1139 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1140 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1141 | static void init_cgroup_root(struct cgroupfs_root *root) |
| 1142 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1143 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1144 | INIT_LIST_HEAD(&root->subsys_list); |
| 1145 | INIT_LIST_HEAD(&root->root_list); |
| 1146 | root->number_of_cgroups = 1; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1147 | cgrp->root = root; |
| 1148 | cgrp->top_cgroup = cgrp; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1149 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1150 | } |
| 1151 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1152 | static bool init_root_id(struct cgroupfs_root *root) |
| 1153 | { |
| 1154 | int ret = 0; |
| 1155 | |
| 1156 | do { |
| 1157 | if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL)) |
| 1158 | return false; |
| 1159 | spin_lock(&hierarchy_id_lock); |
| 1160 | /* Try to allocate the next unused ID */ |
| 1161 | ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id, |
| 1162 | &root->hierarchy_id); |
| 1163 | if (ret == -ENOSPC) |
| 1164 | /* Try again starting from 0 */ |
| 1165 | ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id); |
| 1166 | if (!ret) { |
| 1167 | next_hierarchy_id = root->hierarchy_id + 1; |
| 1168 | } else if (ret != -EAGAIN) { |
| 1169 | /* Can only get here if the 31-bit IDR is full ... */ |
| 1170 | BUG_ON(ret); |
| 1171 | } |
| 1172 | spin_unlock(&hierarchy_id_lock); |
| 1173 | } while (ret); |
| 1174 | return true; |
| 1175 | } |
| 1176 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1177 | static int cgroup_test_super(struct super_block *sb, void *data) |
| 1178 | { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1179 | struct cgroup_sb_opts *opts = data; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1180 | struct cgroupfs_root *root = sb->s_fs_info; |
| 1181 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1182 | /* If we asked for a name then it must match */ |
| 1183 | if (opts->name && strcmp(opts->name, root->name)) |
| 1184 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1185 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1186 | /* |
| 1187 | * If we asked for subsystems (or explicitly for no |
| 1188 | * subsystems) then they must match |
| 1189 | */ |
| 1190 | if ((opts->subsys_bits || opts->none) |
| 1191 | && (opts->subsys_bits != root->subsys_bits)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1192 | return 0; |
| 1193 | |
| 1194 | return 1; |
| 1195 | } |
| 1196 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1197 | static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts) |
| 1198 | { |
| 1199 | struct cgroupfs_root *root; |
| 1200 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1201 | if (!opts->subsys_bits && !opts->none) |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1202 | return NULL; |
| 1203 | |
| 1204 | root = kzalloc(sizeof(*root), GFP_KERNEL); |
| 1205 | if (!root) |
| 1206 | return ERR_PTR(-ENOMEM); |
| 1207 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1208 | if (!init_root_id(root)) { |
| 1209 | kfree(root); |
| 1210 | return ERR_PTR(-ENOMEM); |
| 1211 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1212 | init_cgroup_root(root); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1213 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1214 | root->subsys_bits = opts->subsys_bits; |
| 1215 | root->flags = opts->flags; |
| 1216 | if (opts->release_agent) |
| 1217 | strcpy(root->release_agent_path, opts->release_agent); |
| 1218 | if (opts->name) |
| 1219 | strcpy(root->name, opts->name); |
| 1220 | return root; |
| 1221 | } |
| 1222 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1223 | static void cgroup_drop_root(struct cgroupfs_root *root) |
| 1224 | { |
| 1225 | if (!root) |
| 1226 | return; |
| 1227 | |
| 1228 | BUG_ON(!root->hierarchy_id); |
| 1229 | spin_lock(&hierarchy_id_lock); |
| 1230 | ida_remove(&hierarchy_ida, root->hierarchy_id); |
| 1231 | spin_unlock(&hierarchy_id_lock); |
| 1232 | kfree(root); |
| 1233 | } |
| 1234 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1235 | static int cgroup_set_super(struct super_block *sb, void *data) |
| 1236 | { |
| 1237 | int ret; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1238 | struct cgroup_sb_opts *opts = data; |
| 1239 | |
| 1240 | /* If we don't have a new root, we can't set up a new sb */ |
| 1241 | if (!opts->new_root) |
| 1242 | return -EINVAL; |
| 1243 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1244 | BUG_ON(!opts->subsys_bits && !opts->none); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1245 | |
| 1246 | ret = set_anon_super(sb, NULL); |
| 1247 | if (ret) |
| 1248 | return ret; |
| 1249 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1250 | sb->s_fs_info = opts->new_root; |
| 1251 | opts->new_root->sb = sb; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1252 | |
| 1253 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 1254 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 1255 | sb->s_magic = CGROUP_SUPER_MAGIC; |
| 1256 | sb->s_op = &cgroup_ops; |
| 1257 | |
| 1258 | return 0; |
| 1259 | } |
| 1260 | |
| 1261 | static int cgroup_get_rootdir(struct super_block *sb) |
| 1262 | { |
| 1263 | struct inode *inode = |
| 1264 | cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb); |
| 1265 | struct dentry *dentry; |
| 1266 | |
| 1267 | if (!inode) |
| 1268 | return -ENOMEM; |
| 1269 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1270 | inode->i_fop = &simple_dir_operations; |
| 1271 | inode->i_op = &cgroup_dir_inode_operations; |
| 1272 | /* directories start off with i_nlink == 2 (for "." entry) */ |
| 1273 | inc_nlink(inode); |
| 1274 | dentry = d_alloc_root(inode); |
| 1275 | if (!dentry) { |
| 1276 | iput(inode); |
| 1277 | return -ENOMEM; |
| 1278 | } |
| 1279 | sb->s_root = dentry; |
| 1280 | return 0; |
| 1281 | } |
| 1282 | |
| 1283 | static int cgroup_get_sb(struct file_system_type *fs_type, |
| 1284 | int flags, const char *unused_dev_name, |
| 1285 | void *data, struct vfsmount *mnt) |
| 1286 | { |
| 1287 | struct cgroup_sb_opts opts; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1288 | struct cgroupfs_root *root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1289 | int ret = 0; |
| 1290 | struct super_block *sb; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1291 | struct cgroupfs_root *new_root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1292 | |
| 1293 | /* First find the desired set of subsystems */ |
| 1294 | ret = parse_cgroupfs_options(data, &opts); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1295 | if (ret) |
| 1296 | goto out_err; |
| 1297 | |
| 1298 | /* |
| 1299 | * Allocate a new cgroup root. We may not need it if we're |
| 1300 | * reusing an existing hierarchy. |
| 1301 | */ |
| 1302 | new_root = cgroup_root_from_opts(&opts); |
| 1303 | if (IS_ERR(new_root)) { |
| 1304 | ret = PTR_ERR(new_root); |
| 1305 | goto out_err; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1306 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1307 | opts.new_root = new_root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1308 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1309 | /* Locate an existing or new sb for this hierarchy */ |
| 1310 | sb = sget(fs_type, cgroup_test_super, cgroup_set_super, &opts); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1311 | if (IS_ERR(sb)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1312 | ret = PTR_ERR(sb); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1313 | cgroup_drop_root(opts.new_root); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1314 | goto out_err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1315 | } |
| 1316 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1317 | root = sb->s_fs_info; |
| 1318 | BUG_ON(!root); |
| 1319 | if (root == opts.new_root) { |
| 1320 | /* We used the new root structure, so this is a new hierarchy */ |
| 1321 | struct list_head tmp_cg_links; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1322 | struct cgroup *root_cgrp = &root->top_cgroup; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1323 | struct inode *inode; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1324 | struct cgroupfs_root *existing_root; |
Li Zefan | 28fd5df | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 1325 | int i; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1326 | |
| 1327 | BUG_ON(sb->s_root != NULL); |
| 1328 | |
| 1329 | ret = cgroup_get_rootdir(sb); |
| 1330 | if (ret) |
| 1331 | goto drop_new_super; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1332 | inode = sb->s_root->d_inode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1333 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1334 | mutex_lock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1335 | mutex_lock(&cgroup_mutex); |
| 1336 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1337 | if (strlen(root->name)) { |
| 1338 | /* Check for name clashes with existing mounts */ |
| 1339 | for_each_active_root(existing_root) { |
| 1340 | if (!strcmp(existing_root->name, root->name)) { |
| 1341 | ret = -EBUSY; |
| 1342 | mutex_unlock(&cgroup_mutex); |
| 1343 | mutex_unlock(&inode->i_mutex); |
| 1344 | goto drop_new_super; |
| 1345 | } |
| 1346 | } |
| 1347 | } |
| 1348 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1349 | /* |
| 1350 | * We're accessing css_set_count without locking |
| 1351 | * css_set_lock here, but that's OK - it can only be |
| 1352 | * increased by someone holding cgroup_lock, and |
| 1353 | * that's us. The worst that can happen is that we |
| 1354 | * have some link structures left over |
| 1355 | */ |
| 1356 | ret = allocate_cg_links(css_set_count, &tmp_cg_links); |
| 1357 | if (ret) { |
| 1358 | mutex_unlock(&cgroup_mutex); |
| 1359 | mutex_unlock(&inode->i_mutex); |
| 1360 | goto drop_new_super; |
| 1361 | } |
| 1362 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1363 | ret = rebind_subsystems(root, root->subsys_bits); |
| 1364 | if (ret == -EBUSY) { |
| 1365 | mutex_unlock(&cgroup_mutex); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1366 | mutex_unlock(&inode->i_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1367 | free_cg_links(&tmp_cg_links); |
| 1368 | goto drop_new_super; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | /* EBUSY should be the only error here */ |
| 1372 | BUG_ON(ret); |
| 1373 | |
| 1374 | list_add(&root->root_list, &roots); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1375 | root_count++; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1376 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1377 | sb->s_root->d_fsdata = root_cgrp; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1378 | root->top_cgroup.dentry = sb->s_root; |
| 1379 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1380 | /* Link the top cgroup in this hierarchy into all |
| 1381 | * the css_set objects */ |
| 1382 | write_lock(&css_set_lock); |
Li Zefan | 28fd5df | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 1383 | for (i = 0; i < CSS_SET_TABLE_SIZE; i++) { |
| 1384 | struct hlist_head *hhead = &css_set_table[i]; |
| 1385 | struct hlist_node *node; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1386 | struct css_set *cg; |
Li Zefan | 28fd5df | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 1387 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1388 | hlist_for_each_entry(cg, node, hhead, hlist) |
| 1389 | link_css_set(&tmp_cg_links, cg, root_cgrp); |
Li Zefan | 28fd5df | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 1390 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1391 | write_unlock(&css_set_lock); |
| 1392 | |
| 1393 | free_cg_links(&tmp_cg_links); |
| 1394 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1395 | BUG_ON(!list_empty(&root_cgrp->sibling)); |
| 1396 | BUG_ON(!list_empty(&root_cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1397 | BUG_ON(root->number_of_cgroups != 1); |
| 1398 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1399 | cgroup_populate_dir(root_cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1400 | mutex_unlock(&cgroup_mutex); |
Xiaotian Feng | 34f77a9 | 2009-09-23 15:56:18 -0700 | [diff] [blame] | 1401 | mutex_unlock(&inode->i_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1402 | } else { |
| 1403 | /* |
| 1404 | * We re-used an existing hierarchy - the new root (if |
| 1405 | * any) is not needed |
| 1406 | */ |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1407 | cgroup_drop_root(opts.new_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1408 | } |
| 1409 | |
Sukadev Bhattiprolu | a3ec947 | 2009-03-04 12:06:34 -0800 | [diff] [blame] | 1410 | simple_set_mnt(mnt, sb); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1411 | kfree(opts.release_agent); |
| 1412 | kfree(opts.name); |
Sukadev Bhattiprolu | a3ec947 | 2009-03-04 12:06:34 -0800 | [diff] [blame] | 1413 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1414 | |
| 1415 | drop_new_super: |
Al Viro | 6f5bbff | 2009-05-06 01:34:22 -0400 | [diff] [blame] | 1416 | deactivate_locked_super(sb); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1417 | out_err: |
| 1418 | kfree(opts.release_agent); |
| 1419 | kfree(opts.name); |
| 1420 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1421 | return ret; |
| 1422 | } |
| 1423 | |
| 1424 | static void cgroup_kill_sb(struct super_block *sb) { |
| 1425 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1426 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1427 | int ret; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1428 | struct cg_cgroup_link *link; |
| 1429 | struct cg_cgroup_link *saved_link; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1430 | |
| 1431 | BUG_ON(!root); |
| 1432 | |
| 1433 | BUG_ON(root->number_of_cgroups != 1); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1434 | BUG_ON(!list_empty(&cgrp->children)); |
| 1435 | BUG_ON(!list_empty(&cgrp->sibling)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1436 | |
| 1437 | mutex_lock(&cgroup_mutex); |
| 1438 | |
| 1439 | /* Rebind all subsystems back to the default hierarchy */ |
| 1440 | ret = rebind_subsystems(root, 0); |
| 1441 | /* Shouldn't be able to fail ... */ |
| 1442 | BUG_ON(ret); |
| 1443 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1444 | /* |
| 1445 | * Release all the links from css_sets to this hierarchy's |
| 1446 | * root cgroup |
| 1447 | */ |
| 1448 | write_lock(&css_set_lock); |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1449 | |
| 1450 | list_for_each_entry_safe(link, saved_link, &cgrp->css_sets, |
| 1451 | cgrp_link_list) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1452 | list_del(&link->cg_link_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1453 | list_del(&link->cgrp_link_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1454 | kfree(link); |
| 1455 | } |
| 1456 | write_unlock(&css_set_lock); |
| 1457 | |
Paul Menage | 839ec54 | 2009-01-29 14:25:22 -0800 | [diff] [blame] | 1458 | if (!list_empty(&root->root_list)) { |
| 1459 | list_del(&root->root_list); |
| 1460 | root_count--; |
| 1461 | } |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 1462 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1463 | mutex_unlock(&cgroup_mutex); |
| 1464 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1465 | kill_litter_super(sb); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1466 | cgroup_drop_root(root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | static struct file_system_type cgroup_fs_type = { |
| 1470 | .name = "cgroup", |
| 1471 | .get_sb = cgroup_get_sb, |
| 1472 | .kill_sb = cgroup_kill_sb, |
| 1473 | }; |
| 1474 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1475 | static inline struct cgroup *__d_cgrp(struct dentry *dentry) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1476 | { |
| 1477 | return dentry->d_fsdata; |
| 1478 | } |
| 1479 | |
| 1480 | static inline struct cftype *__d_cft(struct dentry *dentry) |
| 1481 | { |
| 1482 | return dentry->d_fsdata; |
| 1483 | } |
| 1484 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1485 | /** |
| 1486 | * cgroup_path - generate the path of a cgroup |
| 1487 | * @cgrp: the cgroup in question |
| 1488 | * @buf: the buffer to write the path into |
| 1489 | * @buflen: the length of the buffer |
| 1490 | * |
Paul Menage | a47295e | 2009-01-07 18:07:44 -0800 | [diff] [blame] | 1491 | * Called with cgroup_mutex held or else with an RCU-protected cgroup |
| 1492 | * reference. Writes path of cgroup into buf. Returns 0 on success, |
| 1493 | * -errno on error. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1494 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1495 | int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1496 | { |
| 1497 | char *start; |
Paul Menage | a47295e | 2009-01-07 18:07:44 -0800 | [diff] [blame] | 1498 | struct dentry *dentry = rcu_dereference(cgrp->dentry); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1499 | |
Paul Menage | a47295e | 2009-01-07 18:07:44 -0800 | [diff] [blame] | 1500 | if (!dentry || cgrp == dummytop) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1501 | /* |
| 1502 | * Inactive subsystems have no dentry for their root |
| 1503 | * cgroup |
| 1504 | */ |
| 1505 | strcpy(buf, "/"); |
| 1506 | return 0; |
| 1507 | } |
| 1508 | |
| 1509 | start = buf + buflen; |
| 1510 | |
| 1511 | *--start = '\0'; |
| 1512 | for (;;) { |
Paul Menage | a47295e | 2009-01-07 18:07:44 -0800 | [diff] [blame] | 1513 | int len = dentry->d_name.len; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1514 | if ((start -= len) < buf) |
| 1515 | return -ENAMETOOLONG; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1516 | memcpy(start, cgrp->dentry->d_name.name, len); |
| 1517 | cgrp = cgrp->parent; |
| 1518 | if (!cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1519 | break; |
Paul Menage | a47295e | 2009-01-07 18:07:44 -0800 | [diff] [blame] | 1520 | dentry = rcu_dereference(cgrp->dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1521 | if (!cgrp->parent) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1522 | continue; |
| 1523 | if (--start < buf) |
| 1524 | return -ENAMETOOLONG; |
| 1525 | *start = '/'; |
| 1526 | } |
| 1527 | memmove(buf, start, buf + buflen - start); |
| 1528 | return 0; |
| 1529 | } |
| 1530 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1531 | /** |
| 1532 | * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp' |
| 1533 | * @cgrp: the cgroup the task is attaching to |
| 1534 | * @tsk: the task to be attached |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1535 | * |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1536 | * Call holding cgroup_mutex. May take task_lock of |
| 1537 | * the task 'tsk' during call. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1538 | */ |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 1539 | int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1540 | { |
| 1541 | int retval = 0; |
| 1542 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1543 | struct cgroup *oldcgrp; |
Lai Jiangshan | 77efecd | 2009-01-07 18:07:39 -0800 | [diff] [blame] | 1544 | struct css_set *cg; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1545 | struct css_set *newcg; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1546 | struct cgroupfs_root *root = cgrp->root; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1547 | |
| 1548 | /* Nothing to do if the task is already in that cgroup */ |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1549 | oldcgrp = task_cgroup_from_root(tsk, root); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1550 | if (cgrp == oldcgrp) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1551 | return 0; |
| 1552 | |
| 1553 | for_each_subsys(root, ss) { |
| 1554 | if (ss->can_attach) { |
Ben Blum | be367d0 | 2009-09-23 15:56:31 -0700 | [diff] [blame] | 1555 | retval = ss->can_attach(ss, cgrp, tsk, false); |
Paul Jackson | e18f631 | 2008-02-07 00:13:44 -0800 | [diff] [blame] | 1556 | if (retval) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1557 | return retval; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1558 | } |
| 1559 | } |
| 1560 | |
Lai Jiangshan | 77efecd | 2009-01-07 18:07:39 -0800 | [diff] [blame] | 1561 | task_lock(tsk); |
| 1562 | cg = tsk->cgroups; |
| 1563 | get_css_set(cg); |
| 1564 | task_unlock(tsk); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1565 | /* |
| 1566 | * Locate or allocate a new css_set for this task, |
| 1567 | * based on its final set of cgroups |
| 1568 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1569 | newcg = find_css_set(cg, cgrp); |
Lai Jiangshan | 77efecd | 2009-01-07 18:07:39 -0800 | [diff] [blame] | 1570 | put_css_set(cg); |
Paul Jackson | e18f631 | 2008-02-07 00:13:44 -0800 | [diff] [blame] | 1571 | if (!newcg) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1572 | return -ENOMEM; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1573 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1574 | task_lock(tsk); |
| 1575 | if (tsk->flags & PF_EXITING) { |
| 1576 | task_unlock(tsk); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1577 | put_css_set(newcg); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1578 | return -ESRCH; |
| 1579 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1580 | rcu_assign_pointer(tsk->cgroups, newcg); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1581 | task_unlock(tsk); |
| 1582 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1583 | /* Update the css_set linked lists if we're using them */ |
| 1584 | write_lock(&css_set_lock); |
| 1585 | if (!list_empty(&tsk->cg_list)) { |
| 1586 | list_del(&tsk->cg_list); |
| 1587 | list_add(&tsk->cg_list, &newcg->tasks); |
| 1588 | } |
| 1589 | write_unlock(&css_set_lock); |
| 1590 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1591 | for_each_subsys(root, ss) { |
Paul Jackson | e18f631 | 2008-02-07 00:13:44 -0800 | [diff] [blame] | 1592 | if (ss->attach) |
Ben Blum | be367d0 | 2009-09-23 15:56:31 -0700 | [diff] [blame] | 1593 | ss->attach(ss, cgrp, oldcgrp, tsk, false); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1594 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1595 | set_bit(CGRP_RELEASABLE, &oldcgrp->flags); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1596 | synchronize_rcu(); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1597 | put_css_set(cg); |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 1598 | |
| 1599 | /* |
| 1600 | * wake up rmdir() waiter. the rmdir should fail since the cgroup |
| 1601 | * is no longer empty. |
| 1602 | */ |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 1603 | cgroup_wakeup_rmdir_waiter(cgrp); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1604 | return 0; |
| 1605 | } |
| 1606 | |
| 1607 | /* |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 1608 | * Attach task with pid 'pid' to cgroup 'cgrp'. Call with cgroup_mutex |
| 1609 | * held. May take task_lock of task |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1610 | */ |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 1611 | static int attach_task_by_pid(struct cgroup *cgrp, u64 pid) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1612 | { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1613 | struct task_struct *tsk; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1614 | const struct cred *cred = current_cred(), *tcred; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1615 | int ret; |
| 1616 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1617 | if (pid) { |
| 1618 | rcu_read_lock(); |
Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 1619 | tsk = find_task_by_vpid(pid); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1620 | if (!tsk || tsk->flags & PF_EXITING) { |
| 1621 | rcu_read_unlock(); |
| 1622 | return -ESRCH; |
| 1623 | } |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1624 | |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1625 | tcred = __task_cred(tsk); |
| 1626 | if (cred->euid && |
| 1627 | cred->euid != tcred->uid && |
| 1628 | cred->euid != tcred->suid) { |
| 1629 | rcu_read_unlock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1630 | return -EACCES; |
| 1631 | } |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1632 | get_task_struct(tsk); |
| 1633 | rcu_read_unlock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1634 | } else { |
| 1635 | tsk = current; |
| 1636 | get_task_struct(tsk); |
| 1637 | } |
| 1638 | |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 1639 | ret = cgroup_attach_task(cgrp, tsk); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1640 | put_task_struct(tsk); |
| 1641 | return ret; |
| 1642 | } |
| 1643 | |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 1644 | static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid) |
| 1645 | { |
| 1646 | int ret; |
| 1647 | if (!cgroup_lock_live_group(cgrp)) |
| 1648 | return -ENODEV; |
| 1649 | ret = attach_task_by_pid(cgrp, pid); |
| 1650 | cgroup_unlock(); |
| 1651 | return ret; |
| 1652 | } |
| 1653 | |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 1654 | /** |
| 1655 | * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive. |
| 1656 | * @cgrp: the cgroup to be checked for liveness |
| 1657 | * |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 1658 | * On success, returns true; the lock should be later released with |
| 1659 | * cgroup_unlock(). On failure returns false with no lock held. |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 1660 | */ |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 1661 | bool cgroup_lock_live_group(struct cgroup *cgrp) |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 1662 | { |
| 1663 | mutex_lock(&cgroup_mutex); |
| 1664 | if (cgroup_is_removed(cgrp)) { |
| 1665 | mutex_unlock(&cgroup_mutex); |
| 1666 | return false; |
| 1667 | } |
| 1668 | return true; |
| 1669 | } |
| 1670 | |
| 1671 | static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft, |
| 1672 | const char *buffer) |
| 1673 | { |
| 1674 | BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX); |
| 1675 | if (!cgroup_lock_live_group(cgrp)) |
| 1676 | return -ENODEV; |
| 1677 | strcpy(cgrp->root->release_agent_path, buffer); |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 1678 | cgroup_unlock(); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 1679 | return 0; |
| 1680 | } |
| 1681 | |
| 1682 | static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft, |
| 1683 | struct seq_file *seq) |
| 1684 | { |
| 1685 | if (!cgroup_lock_live_group(cgrp)) |
| 1686 | return -ENODEV; |
| 1687 | seq_puts(seq, cgrp->root->release_agent_path); |
| 1688 | seq_putc(seq, '\n'); |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 1689 | cgroup_unlock(); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 1690 | return 0; |
| 1691 | } |
| 1692 | |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 1693 | /* A buffer size big enough for numbers or short strings */ |
| 1694 | #define CGROUP_LOCAL_BUFFER_SIZE 64 |
| 1695 | |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 1696 | static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft, |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 1697 | struct file *file, |
| 1698 | const char __user *userbuf, |
| 1699 | size_t nbytes, loff_t *unused_ppos) |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 1700 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 1701 | char buffer[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 1702 | int retval = 0; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 1703 | char *end; |
| 1704 | |
| 1705 | if (!nbytes) |
| 1706 | return -EINVAL; |
| 1707 | if (nbytes >= sizeof(buffer)) |
| 1708 | return -E2BIG; |
| 1709 | if (copy_from_user(buffer, userbuf, nbytes)) |
| 1710 | return -EFAULT; |
| 1711 | |
| 1712 | buffer[nbytes] = 0; /* nul-terminate */ |
Paul Menage | b7269df | 2008-04-29 00:59:59 -0700 | [diff] [blame] | 1713 | strstrip(buffer); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 1714 | if (cft->write_u64) { |
| 1715 | u64 val = simple_strtoull(buffer, &end, 0); |
| 1716 | if (*end) |
| 1717 | return -EINVAL; |
| 1718 | retval = cft->write_u64(cgrp, cft, val); |
| 1719 | } else { |
| 1720 | s64 val = simple_strtoll(buffer, &end, 0); |
| 1721 | if (*end) |
| 1722 | return -EINVAL; |
| 1723 | retval = cft->write_s64(cgrp, cft, val); |
| 1724 | } |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 1725 | if (!retval) |
| 1726 | retval = nbytes; |
| 1727 | return retval; |
| 1728 | } |
| 1729 | |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 1730 | static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft, |
| 1731 | struct file *file, |
| 1732 | const char __user *userbuf, |
| 1733 | size_t nbytes, loff_t *unused_ppos) |
| 1734 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 1735 | char local_buffer[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 1736 | int retval = 0; |
| 1737 | size_t max_bytes = cft->max_write_len; |
| 1738 | char *buffer = local_buffer; |
| 1739 | |
| 1740 | if (!max_bytes) |
| 1741 | max_bytes = sizeof(local_buffer) - 1; |
| 1742 | if (nbytes >= max_bytes) |
| 1743 | return -E2BIG; |
| 1744 | /* Allocate a dynamic buffer if we need one */ |
| 1745 | if (nbytes >= sizeof(local_buffer)) { |
| 1746 | buffer = kmalloc(nbytes + 1, GFP_KERNEL); |
| 1747 | if (buffer == NULL) |
| 1748 | return -ENOMEM; |
| 1749 | } |
Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 1750 | if (nbytes && copy_from_user(buffer, userbuf, nbytes)) { |
| 1751 | retval = -EFAULT; |
| 1752 | goto out; |
| 1753 | } |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 1754 | |
| 1755 | buffer[nbytes] = 0; /* nul-terminate */ |
| 1756 | strstrip(buffer); |
| 1757 | retval = cft->write_string(cgrp, cft, buffer); |
| 1758 | if (!retval) |
| 1759 | retval = nbytes; |
Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 1760 | out: |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 1761 | if (buffer != local_buffer) |
| 1762 | kfree(buffer); |
| 1763 | return retval; |
| 1764 | } |
| 1765 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1766 | static ssize_t cgroup_file_write(struct file *file, const char __user *buf, |
| 1767 | size_t nbytes, loff_t *ppos) |
| 1768 | { |
| 1769 | struct cftype *cft = __d_cft(file->f_dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1770 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1771 | |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 1772 | if (cgroup_is_removed(cgrp)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1773 | return -ENODEV; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 1774 | if (cft->write) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1775 | return cft->write(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 1776 | if (cft->write_u64 || cft->write_s64) |
| 1777 | return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 1778 | if (cft->write_string) |
| 1779 | return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos); |
Pavel Emelyanov | d447ea2 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 1780 | if (cft->trigger) { |
| 1781 | int ret = cft->trigger(cgrp, (unsigned int)cft->private); |
| 1782 | return ret ? ret : nbytes; |
| 1783 | } |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 1784 | return -EINVAL; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1785 | } |
| 1786 | |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 1787 | static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft, |
| 1788 | struct file *file, |
| 1789 | char __user *buf, size_t nbytes, |
| 1790 | loff_t *ppos) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1791 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 1792 | char tmp[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 1793 | u64 val = cft->read_u64(cgrp, cft); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1794 | int len = sprintf(tmp, "%llu\n", (unsigned long long) val); |
| 1795 | |
| 1796 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
| 1797 | } |
| 1798 | |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 1799 | static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft, |
| 1800 | struct file *file, |
| 1801 | char __user *buf, size_t nbytes, |
| 1802 | loff_t *ppos) |
| 1803 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 1804 | char tmp[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 1805 | s64 val = cft->read_s64(cgrp, cft); |
| 1806 | int len = sprintf(tmp, "%lld\n", (long long) val); |
| 1807 | |
| 1808 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
| 1809 | } |
| 1810 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1811 | static ssize_t cgroup_file_read(struct file *file, char __user *buf, |
| 1812 | size_t nbytes, loff_t *ppos) |
| 1813 | { |
| 1814 | struct cftype *cft = __d_cft(file->f_dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1815 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1816 | |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 1817 | if (cgroup_is_removed(cgrp)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1818 | return -ENODEV; |
| 1819 | |
| 1820 | if (cft->read) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1821 | return cft->read(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 1822 | if (cft->read_u64) |
| 1823 | return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 1824 | if (cft->read_s64) |
| 1825 | return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1826 | return -EINVAL; |
| 1827 | } |
| 1828 | |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 1829 | /* |
| 1830 | * seqfile ops/methods for returning structured data. Currently just |
| 1831 | * supports string->u64 maps, but can be extended in future. |
| 1832 | */ |
| 1833 | |
| 1834 | struct cgroup_seqfile_state { |
| 1835 | struct cftype *cft; |
| 1836 | struct cgroup *cgroup; |
| 1837 | }; |
| 1838 | |
| 1839 | static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value) |
| 1840 | { |
| 1841 | struct seq_file *sf = cb->state; |
| 1842 | return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value); |
| 1843 | } |
| 1844 | |
| 1845 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) |
| 1846 | { |
| 1847 | struct cgroup_seqfile_state *state = m->private; |
| 1848 | struct cftype *cft = state->cft; |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 1849 | if (cft->read_map) { |
| 1850 | struct cgroup_map_cb cb = { |
| 1851 | .fill = cgroup_map_add, |
| 1852 | .state = m, |
| 1853 | }; |
| 1854 | return cft->read_map(state->cgroup, cft, &cb); |
| 1855 | } |
| 1856 | return cft->read_seq_string(state->cgroup, cft, m); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 1857 | } |
| 1858 | |
Adrian Bunk | 96930a6 | 2008-07-25 19:46:21 -0700 | [diff] [blame] | 1859 | static int cgroup_seqfile_release(struct inode *inode, struct file *file) |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 1860 | { |
| 1861 | struct seq_file *seq = file->private_data; |
| 1862 | kfree(seq->private); |
| 1863 | return single_release(inode, file); |
| 1864 | } |
| 1865 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 1866 | static const struct file_operations cgroup_seqfile_operations = { |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 1867 | .read = seq_read, |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 1868 | .write = cgroup_file_write, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 1869 | .llseek = seq_lseek, |
| 1870 | .release = cgroup_seqfile_release, |
| 1871 | }; |
| 1872 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1873 | static int cgroup_file_open(struct inode *inode, struct file *file) |
| 1874 | { |
| 1875 | int err; |
| 1876 | struct cftype *cft; |
| 1877 | |
| 1878 | err = generic_file_open(inode, file); |
| 1879 | if (err) |
| 1880 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1881 | cft = __d_cft(file->f_dentry); |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 1882 | |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 1883 | if (cft->read_map || cft->read_seq_string) { |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 1884 | struct cgroup_seqfile_state *state = |
| 1885 | kzalloc(sizeof(*state), GFP_USER); |
| 1886 | if (!state) |
| 1887 | return -ENOMEM; |
| 1888 | state->cft = cft; |
| 1889 | state->cgroup = __d_cgrp(file->f_dentry->d_parent); |
| 1890 | file->f_op = &cgroup_seqfile_operations; |
| 1891 | err = single_open(file, cgroup_seqfile_show, state); |
| 1892 | if (err < 0) |
| 1893 | kfree(state); |
| 1894 | } else if (cft->open) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1895 | err = cft->open(inode, file); |
| 1896 | else |
| 1897 | err = 0; |
| 1898 | |
| 1899 | return err; |
| 1900 | } |
| 1901 | |
| 1902 | static int cgroup_file_release(struct inode *inode, struct file *file) |
| 1903 | { |
| 1904 | struct cftype *cft = __d_cft(file->f_dentry); |
| 1905 | if (cft->release) |
| 1906 | return cft->release(inode, file); |
| 1907 | return 0; |
| 1908 | } |
| 1909 | |
| 1910 | /* |
| 1911 | * cgroup_rename - Only allow simple rename of directories in place. |
| 1912 | */ |
| 1913 | static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 1914 | struct inode *new_dir, struct dentry *new_dentry) |
| 1915 | { |
| 1916 | if (!S_ISDIR(old_dentry->d_inode->i_mode)) |
| 1917 | return -ENOTDIR; |
| 1918 | if (new_dentry->d_inode) |
| 1919 | return -EEXIST; |
| 1920 | if (old_dir != new_dir) |
| 1921 | return -EIO; |
| 1922 | return simple_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 1923 | } |
| 1924 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 1925 | static const struct file_operations cgroup_file_operations = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1926 | .read = cgroup_file_read, |
| 1927 | .write = cgroup_file_write, |
| 1928 | .llseek = generic_file_llseek, |
| 1929 | .open = cgroup_file_open, |
| 1930 | .release = cgroup_file_release, |
| 1931 | }; |
| 1932 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 1933 | static const struct inode_operations cgroup_dir_inode_operations = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1934 | .lookup = simple_lookup, |
| 1935 | .mkdir = cgroup_mkdir, |
| 1936 | .rmdir = cgroup_rmdir, |
| 1937 | .rename = cgroup_rename, |
| 1938 | }; |
| 1939 | |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 1940 | static int cgroup_create_file(struct dentry *dentry, mode_t mode, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1941 | struct super_block *sb) |
| 1942 | { |
Al Viro | 3ba13d1 | 2009-02-20 06:02:22 +0000 | [diff] [blame] | 1943 | static const struct dentry_operations cgroup_dops = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1944 | .d_iput = cgroup_diput, |
| 1945 | }; |
| 1946 | |
| 1947 | struct inode *inode; |
| 1948 | |
| 1949 | if (!dentry) |
| 1950 | return -ENOENT; |
| 1951 | if (dentry->d_inode) |
| 1952 | return -EEXIST; |
| 1953 | |
| 1954 | inode = cgroup_new_inode(mode, sb); |
| 1955 | if (!inode) |
| 1956 | return -ENOMEM; |
| 1957 | |
| 1958 | if (S_ISDIR(mode)) { |
| 1959 | inode->i_op = &cgroup_dir_inode_operations; |
| 1960 | inode->i_fop = &simple_dir_operations; |
| 1961 | |
| 1962 | /* start off with i_nlink == 2 (for "." entry) */ |
| 1963 | inc_nlink(inode); |
| 1964 | |
| 1965 | /* start with the directory inode held, so that we can |
| 1966 | * populate it without racing with another mkdir */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1967 | mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1968 | } else if (S_ISREG(mode)) { |
| 1969 | inode->i_size = 0; |
| 1970 | inode->i_fop = &cgroup_file_operations; |
| 1971 | } |
| 1972 | dentry->d_op = &cgroup_dops; |
| 1973 | d_instantiate(dentry, inode); |
| 1974 | dget(dentry); /* Extra count - pin the dentry in core */ |
| 1975 | return 0; |
| 1976 | } |
| 1977 | |
| 1978 | /* |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1979 | * cgroup_create_dir - create a directory for an object. |
| 1980 | * @cgrp: the cgroup we create the directory for. It must have a valid |
| 1981 | * ->parent field. And we are going to fill its ->dentry field. |
| 1982 | * @dentry: dentry of the new cgroup |
| 1983 | * @mode: mode to set on new directory. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1984 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1985 | static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry, |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 1986 | mode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1987 | { |
| 1988 | struct dentry *parent; |
| 1989 | int error = 0; |
| 1990 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1991 | parent = cgrp->parent->dentry; |
| 1992 | error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1993 | if (!error) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1994 | dentry->d_fsdata = cgrp; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1995 | inc_nlink(parent->d_inode); |
Paul Menage | a47295e | 2009-01-07 18:07:44 -0800 | [diff] [blame] | 1996 | rcu_assign_pointer(cgrp->dentry, dentry); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1997 | dget(dentry); |
| 1998 | } |
| 1999 | dput(dentry); |
| 2000 | |
| 2001 | return error; |
| 2002 | } |
| 2003 | |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2004 | /** |
| 2005 | * cgroup_file_mode - deduce file mode of a control file |
| 2006 | * @cft: the control file in question |
| 2007 | * |
| 2008 | * returns cft->mode if ->mode is not 0 |
| 2009 | * returns S_IRUGO|S_IWUSR if it has both a read and a write handler |
| 2010 | * returns S_IRUGO if it has only a read handler |
| 2011 | * returns S_IWUSR if it has only a write hander |
| 2012 | */ |
| 2013 | static mode_t cgroup_file_mode(const struct cftype *cft) |
| 2014 | { |
| 2015 | mode_t mode = 0; |
| 2016 | |
| 2017 | if (cft->mode) |
| 2018 | return cft->mode; |
| 2019 | |
| 2020 | if (cft->read || cft->read_u64 || cft->read_s64 || |
| 2021 | cft->read_map || cft->read_seq_string) |
| 2022 | mode |= S_IRUGO; |
| 2023 | |
| 2024 | if (cft->write || cft->write_u64 || cft->write_s64 || |
| 2025 | cft->write_string || cft->trigger) |
| 2026 | mode |= S_IWUSR; |
| 2027 | |
| 2028 | return mode; |
| 2029 | } |
| 2030 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2031 | int cgroup_add_file(struct cgroup *cgrp, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2032 | struct cgroup_subsys *subsys, |
| 2033 | const struct cftype *cft) |
| 2034 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2035 | struct dentry *dir = cgrp->dentry; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2036 | struct dentry *dentry; |
| 2037 | int error; |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2038 | mode_t mode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2039 | |
| 2040 | char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 }; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2041 | if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2042 | strcpy(name, subsys->name); |
| 2043 | strcat(name, "."); |
| 2044 | } |
| 2045 | strcat(name, cft->name); |
| 2046 | BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex)); |
| 2047 | dentry = lookup_one_len(name, dir, strlen(name)); |
| 2048 | if (!IS_ERR(dentry)) { |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2049 | mode = cgroup_file_mode(cft); |
| 2050 | error = cgroup_create_file(dentry, mode | S_IFREG, |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2051 | cgrp->root->sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2052 | if (!error) |
| 2053 | dentry->d_fsdata = (void *)cft; |
| 2054 | dput(dentry); |
| 2055 | } else |
| 2056 | error = PTR_ERR(dentry); |
| 2057 | return error; |
| 2058 | } |
| 2059 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2060 | int cgroup_add_files(struct cgroup *cgrp, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2061 | struct cgroup_subsys *subsys, |
| 2062 | const struct cftype cft[], |
| 2063 | int count) |
| 2064 | { |
| 2065 | int i, err; |
| 2066 | for (i = 0; i < count; i++) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2067 | err = cgroup_add_file(cgrp, subsys, &cft[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2068 | if (err) |
| 2069 | return err; |
| 2070 | } |
| 2071 | return 0; |
| 2072 | } |
| 2073 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2074 | /** |
| 2075 | * cgroup_task_count - count the number of tasks in a cgroup. |
| 2076 | * @cgrp: the cgroup in question |
| 2077 | * |
| 2078 | * Return the number of tasks in the cgroup. |
| 2079 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2080 | int cgroup_task_count(const struct cgroup *cgrp) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2081 | { |
| 2082 | int count = 0; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 2083 | struct cg_cgroup_link *link; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2084 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2085 | read_lock(&css_set_lock); |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 2086 | list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 2087 | count += atomic_read(&link->cg->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2088 | } |
| 2089 | read_unlock(&css_set_lock); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2090 | return count; |
| 2091 | } |
| 2092 | |
| 2093 | /* |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2094 | * Advance a list_head iterator. The iterator should be positioned at |
| 2095 | * the start of a css_set |
| 2096 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2097 | static void cgroup_advance_iter(struct cgroup *cgrp, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 2098 | struct cgroup_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2099 | { |
| 2100 | struct list_head *l = it->cg_link; |
| 2101 | struct cg_cgroup_link *link; |
| 2102 | struct css_set *cg; |
| 2103 | |
| 2104 | /* Advance to the next non-empty css_set */ |
| 2105 | do { |
| 2106 | l = l->next; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2107 | if (l == &cgrp->css_sets) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2108 | it->cg_link = NULL; |
| 2109 | return; |
| 2110 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2111 | link = list_entry(l, struct cg_cgroup_link, cgrp_link_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2112 | cg = link->cg; |
| 2113 | } while (list_empty(&cg->tasks)); |
| 2114 | it->cg_link = l; |
| 2115 | it->task = cg->tasks.next; |
| 2116 | } |
| 2117 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2118 | /* |
| 2119 | * To reduce the fork() overhead for systems that are not actually |
| 2120 | * using their cgroups capability, we don't maintain the lists running |
| 2121 | * through each css_set to its tasks until we see the list actually |
| 2122 | * used - in other words after the first call to cgroup_iter_start(). |
| 2123 | * |
| 2124 | * The tasklist_lock is not held here, as do_each_thread() and |
| 2125 | * while_each_thread() are protected by RCU. |
| 2126 | */ |
Adrian Bunk | 3df91fe | 2008-04-29 00:59:54 -0700 | [diff] [blame] | 2127 | static void cgroup_enable_task_cg_lists(void) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2128 | { |
| 2129 | struct task_struct *p, *g; |
| 2130 | write_lock(&css_set_lock); |
| 2131 | use_task_css_set_links = 1; |
| 2132 | do_each_thread(g, p) { |
| 2133 | task_lock(p); |
Li Zefan | 0e04388 | 2008-04-17 11:37:15 +0800 | [diff] [blame] | 2134 | /* |
| 2135 | * We should check if the process is exiting, otherwise |
| 2136 | * it will race with cgroup_exit() in that the list |
| 2137 | * entry won't be deleted though the process has exited. |
| 2138 | */ |
| 2139 | if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list)) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2140 | list_add(&p->cg_list, &p->cgroups->tasks); |
| 2141 | task_unlock(p); |
| 2142 | } while_each_thread(g, p); |
| 2143 | write_unlock(&css_set_lock); |
| 2144 | } |
| 2145 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2146 | void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2147 | { |
| 2148 | /* |
| 2149 | * The first time anyone tries to iterate across a cgroup, |
| 2150 | * we need to enable the list linking each css_set to its |
| 2151 | * tasks, and fix up all existing tasks. |
| 2152 | */ |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2153 | if (!use_task_css_set_links) |
| 2154 | cgroup_enable_task_cg_lists(); |
| 2155 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2156 | read_lock(&css_set_lock); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2157 | it->cg_link = &cgrp->css_sets; |
| 2158 | cgroup_advance_iter(cgrp, it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2159 | } |
| 2160 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2161 | struct task_struct *cgroup_iter_next(struct cgroup *cgrp, |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2162 | struct cgroup_iter *it) |
| 2163 | { |
| 2164 | struct task_struct *res; |
| 2165 | struct list_head *l = it->task; |
Lai Jiangshan | 2019f63 | 2009-01-07 18:07:36 -0800 | [diff] [blame] | 2166 | struct cg_cgroup_link *link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2167 | |
| 2168 | /* If the iterator cg is NULL, we have no tasks */ |
| 2169 | if (!it->cg_link) |
| 2170 | return NULL; |
| 2171 | res = list_entry(l, struct task_struct, cg_list); |
| 2172 | /* Advance iterator to find next entry */ |
| 2173 | l = l->next; |
Lai Jiangshan | 2019f63 | 2009-01-07 18:07:36 -0800 | [diff] [blame] | 2174 | link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list); |
| 2175 | if (l == &link->cg->tasks) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2176 | /* We reached the end of this task list - move on to |
| 2177 | * the next cg_cgroup_link */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2178 | cgroup_advance_iter(cgrp, it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2179 | } else { |
| 2180 | it->task = l; |
| 2181 | } |
| 2182 | return res; |
| 2183 | } |
| 2184 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2185 | void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2186 | { |
| 2187 | read_unlock(&css_set_lock); |
| 2188 | } |
| 2189 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2190 | static inline int started_after_time(struct task_struct *t1, |
| 2191 | struct timespec *time, |
| 2192 | struct task_struct *t2) |
| 2193 | { |
| 2194 | int start_diff = timespec_compare(&t1->start_time, time); |
| 2195 | if (start_diff > 0) { |
| 2196 | return 1; |
| 2197 | } else if (start_diff < 0) { |
| 2198 | return 0; |
| 2199 | } else { |
| 2200 | /* |
| 2201 | * Arbitrarily, if two processes started at the same |
| 2202 | * time, we'll say that the lower pointer value |
| 2203 | * started first. Note that t2 may have exited by now |
| 2204 | * so this may not be a valid pointer any longer, but |
| 2205 | * that's fine - it still serves to distinguish |
| 2206 | * between two tasks started (effectively) simultaneously. |
| 2207 | */ |
| 2208 | return t1 > t2; |
| 2209 | } |
| 2210 | } |
| 2211 | |
| 2212 | /* |
| 2213 | * This function is a callback from heap_insert() and is used to order |
| 2214 | * the heap. |
| 2215 | * In this case we order the heap in descending task start time. |
| 2216 | */ |
| 2217 | static inline int started_after(void *p1, void *p2) |
| 2218 | { |
| 2219 | struct task_struct *t1 = p1; |
| 2220 | struct task_struct *t2 = p2; |
| 2221 | return started_after_time(t1, &t2->start_time, t2); |
| 2222 | } |
| 2223 | |
| 2224 | /** |
| 2225 | * cgroup_scan_tasks - iterate though all the tasks in a cgroup |
| 2226 | * @scan: struct cgroup_scanner containing arguments for the scan |
| 2227 | * |
| 2228 | * Arguments include pointers to callback functions test_task() and |
| 2229 | * process_task(). |
| 2230 | * Iterate through all the tasks in a cgroup, calling test_task() for each, |
| 2231 | * and if it returns true, call process_task() for it also. |
| 2232 | * The test_task pointer may be NULL, meaning always true (select all tasks). |
| 2233 | * Effectively duplicates cgroup_iter_{start,next,end}() |
| 2234 | * but does not lock css_set_lock for the call to process_task(). |
| 2235 | * The struct cgroup_scanner may be embedded in any structure of the caller's |
| 2236 | * creation. |
| 2237 | * It is guaranteed that process_task() will act on every task that |
| 2238 | * is a member of the cgroup for the duration of this call. This |
| 2239 | * function may or may not call process_task() for tasks that exit |
| 2240 | * or move to a different cgroup during the call, or are forked or |
| 2241 | * move into the cgroup during the call. |
| 2242 | * |
| 2243 | * Note that test_task() may be called with locks held, and may in some |
| 2244 | * situations be called multiple times for the same task, so it should |
| 2245 | * be cheap. |
| 2246 | * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been |
| 2247 | * pre-allocated and will be used for heap operations (and its "gt" member will |
| 2248 | * be overwritten), else a temporary heap will be used (allocation of which |
| 2249 | * may cause this function to fail). |
| 2250 | */ |
| 2251 | int cgroup_scan_tasks(struct cgroup_scanner *scan) |
| 2252 | { |
| 2253 | int retval, i; |
| 2254 | struct cgroup_iter it; |
| 2255 | struct task_struct *p, *dropped; |
| 2256 | /* Never dereference latest_task, since it's not refcounted */ |
| 2257 | struct task_struct *latest_task = NULL; |
| 2258 | struct ptr_heap tmp_heap; |
| 2259 | struct ptr_heap *heap; |
| 2260 | struct timespec latest_time = { 0, 0 }; |
| 2261 | |
| 2262 | if (scan->heap) { |
| 2263 | /* The caller supplied our heap and pre-allocated its memory */ |
| 2264 | heap = scan->heap; |
| 2265 | heap->gt = &started_after; |
| 2266 | } else { |
| 2267 | /* We need to allocate our own heap memory */ |
| 2268 | heap = &tmp_heap; |
| 2269 | retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after); |
| 2270 | if (retval) |
| 2271 | /* cannot allocate the heap */ |
| 2272 | return retval; |
| 2273 | } |
| 2274 | |
| 2275 | again: |
| 2276 | /* |
| 2277 | * Scan tasks in the cgroup, using the scanner's "test_task" callback |
| 2278 | * to determine which are of interest, and using the scanner's |
| 2279 | * "process_task" callback to process any of them that need an update. |
| 2280 | * Since we don't want to hold any locks during the task updates, |
| 2281 | * gather tasks to be processed in a heap structure. |
| 2282 | * The heap is sorted by descending task start time. |
| 2283 | * If the statically-sized heap fills up, we overflow tasks that |
| 2284 | * started later, and in future iterations only consider tasks that |
| 2285 | * started after the latest task in the previous pass. This |
| 2286 | * guarantees forward progress and that we don't miss any tasks. |
| 2287 | */ |
| 2288 | heap->size = 0; |
| 2289 | cgroup_iter_start(scan->cg, &it); |
| 2290 | while ((p = cgroup_iter_next(scan->cg, &it))) { |
| 2291 | /* |
| 2292 | * Only affect tasks that qualify per the caller's callback, |
| 2293 | * if he provided one |
| 2294 | */ |
| 2295 | if (scan->test_task && !scan->test_task(p, scan)) |
| 2296 | continue; |
| 2297 | /* |
| 2298 | * Only process tasks that started after the last task |
| 2299 | * we processed |
| 2300 | */ |
| 2301 | if (!started_after_time(p, &latest_time, latest_task)) |
| 2302 | continue; |
| 2303 | dropped = heap_insert(heap, p); |
| 2304 | if (dropped == NULL) { |
| 2305 | /* |
| 2306 | * The new task was inserted; the heap wasn't |
| 2307 | * previously full |
| 2308 | */ |
| 2309 | get_task_struct(p); |
| 2310 | } else if (dropped != p) { |
| 2311 | /* |
| 2312 | * The new task was inserted, and pushed out a |
| 2313 | * different task |
| 2314 | */ |
| 2315 | get_task_struct(p); |
| 2316 | put_task_struct(dropped); |
| 2317 | } |
| 2318 | /* |
| 2319 | * Else the new task was newer than anything already in |
| 2320 | * the heap and wasn't inserted |
| 2321 | */ |
| 2322 | } |
| 2323 | cgroup_iter_end(scan->cg, &it); |
| 2324 | |
| 2325 | if (heap->size) { |
| 2326 | for (i = 0; i < heap->size; i++) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 2327 | struct task_struct *q = heap->ptrs[i]; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2328 | if (i == 0) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 2329 | latest_time = q->start_time; |
| 2330 | latest_task = q; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2331 | } |
| 2332 | /* Process the task per the caller's callback */ |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 2333 | scan->process_task(q, scan); |
| 2334 | put_task_struct(q); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2335 | } |
| 2336 | /* |
| 2337 | * If we had to process any tasks at all, scan again |
| 2338 | * in case some of them were in the middle of forking |
| 2339 | * children that didn't get processed. |
| 2340 | * Not the most efficient way to do it, but it avoids |
| 2341 | * having to take callback_mutex in the fork path |
| 2342 | */ |
| 2343 | goto again; |
| 2344 | } |
| 2345 | if (heap == &tmp_heap) |
| 2346 | heap_free(&tmp_heap); |
| 2347 | return 0; |
| 2348 | } |
| 2349 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2350 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2351 | * Stuff for reading the 'tasks'/'procs' files. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2352 | * |
| 2353 | * Reading this file can return large amounts of data if a cgroup has |
| 2354 | * *lots* of attached tasks. So it may need several calls to read(), |
| 2355 | * but we cannot guarantee that the information we produce is correct |
| 2356 | * unless we produce it entirely atomically. |
| 2357 | * |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2358 | */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2359 | |
| 2360 | /* |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 2361 | * The following two functions "fix" the issue where there are more pids |
| 2362 | * than kmalloc will give memory for; in such cases, we use vmalloc/vfree. |
| 2363 | * TODO: replace with a kernel-wide solution to this problem |
| 2364 | */ |
| 2365 | #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2)) |
| 2366 | static void *pidlist_allocate(int count) |
| 2367 | { |
| 2368 | if (PIDLIST_TOO_LARGE(count)) |
| 2369 | return vmalloc(count * sizeof(pid_t)); |
| 2370 | else |
| 2371 | return kmalloc(count * sizeof(pid_t), GFP_KERNEL); |
| 2372 | } |
| 2373 | static void pidlist_free(void *p) |
| 2374 | { |
| 2375 | if (is_vmalloc_addr(p)) |
| 2376 | vfree(p); |
| 2377 | else |
| 2378 | kfree(p); |
| 2379 | } |
| 2380 | static void *pidlist_resize(void *p, int newcount) |
| 2381 | { |
| 2382 | void *newlist; |
| 2383 | /* note: if new alloc fails, old p will still be valid either way */ |
| 2384 | if (is_vmalloc_addr(p)) { |
| 2385 | newlist = vmalloc(newcount * sizeof(pid_t)); |
| 2386 | if (!newlist) |
| 2387 | return NULL; |
| 2388 | memcpy(newlist, p, newcount * sizeof(pid_t)); |
| 2389 | vfree(p); |
| 2390 | } else { |
| 2391 | newlist = krealloc(p, newcount * sizeof(pid_t), GFP_KERNEL); |
| 2392 | } |
| 2393 | return newlist; |
| 2394 | } |
| 2395 | |
| 2396 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2397 | * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries |
| 2398 | * If the new stripped list is sufficiently smaller and there's enough memory |
| 2399 | * to allocate a new buffer, will let go of the unneeded memory. Returns the |
| 2400 | * number of unique elements. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2401 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2402 | /* is the size difference enough that we should re-allocate the array? */ |
| 2403 | #define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new)) |
| 2404 | static int pidlist_uniq(pid_t **p, int length) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2405 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2406 | int src, dest = 1; |
| 2407 | pid_t *list = *p; |
| 2408 | pid_t *newlist; |
| 2409 | |
| 2410 | /* |
| 2411 | * we presume the 0th element is unique, so i starts at 1. trivial |
| 2412 | * edge cases first; no work needs to be done for either |
| 2413 | */ |
| 2414 | if (length == 0 || length == 1) |
| 2415 | return length; |
| 2416 | /* src and dest walk down the list; dest counts unique elements */ |
| 2417 | for (src = 1; src < length; src++) { |
| 2418 | /* find next unique element */ |
| 2419 | while (list[src] == list[src-1]) { |
| 2420 | src++; |
| 2421 | if (src == length) |
| 2422 | goto after; |
| 2423 | } |
| 2424 | /* dest always points to where the next unique element goes */ |
| 2425 | list[dest] = list[src]; |
| 2426 | dest++; |
| 2427 | } |
| 2428 | after: |
| 2429 | /* |
| 2430 | * if the length difference is large enough, we want to allocate a |
| 2431 | * smaller buffer to save memory. if this fails due to out of memory, |
| 2432 | * we'll just stay with what we've got. |
| 2433 | */ |
| 2434 | if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) { |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 2435 | newlist = pidlist_resize(list, dest); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2436 | if (newlist) |
| 2437 | *p = newlist; |
| 2438 | } |
| 2439 | return dest; |
| 2440 | } |
| 2441 | |
| 2442 | static int cmppid(const void *a, const void *b) |
| 2443 | { |
| 2444 | return *(pid_t *)a - *(pid_t *)b; |
| 2445 | } |
| 2446 | |
| 2447 | /* |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 2448 | * find the appropriate pidlist for our purpose (given procs vs tasks) |
| 2449 | * returns with the lock on that pidlist already held, and takes care |
| 2450 | * of the use count, or returns NULL with no locks held if we're out of |
| 2451 | * memory. |
| 2452 | */ |
| 2453 | static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp, |
| 2454 | enum cgroup_filetype type) |
| 2455 | { |
| 2456 | struct cgroup_pidlist *l; |
| 2457 | /* don't need task_nsproxy() if we're looking at ourself */ |
| 2458 | struct pid_namespace *ns = get_pid_ns(current->nsproxy->pid_ns); |
| 2459 | /* |
| 2460 | * We can't drop the pidlist_mutex before taking the l->mutex in case |
| 2461 | * the last ref-holder is trying to remove l from the list at the same |
| 2462 | * time. Holding the pidlist_mutex precludes somebody taking whichever |
| 2463 | * list we find out from under us - compare release_pid_array(). |
| 2464 | */ |
| 2465 | mutex_lock(&cgrp->pidlist_mutex); |
| 2466 | list_for_each_entry(l, &cgrp->pidlists, links) { |
| 2467 | if (l->key.type == type && l->key.ns == ns) { |
| 2468 | /* found a matching list - drop the extra refcount */ |
| 2469 | put_pid_ns(ns); |
| 2470 | /* make sure l doesn't vanish out from under us */ |
| 2471 | down_write(&l->mutex); |
| 2472 | mutex_unlock(&cgrp->pidlist_mutex); |
| 2473 | l->use_count++; |
| 2474 | return l; |
| 2475 | } |
| 2476 | } |
| 2477 | /* entry not found; create a new one */ |
| 2478 | l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL); |
| 2479 | if (!l) { |
| 2480 | mutex_unlock(&cgrp->pidlist_mutex); |
| 2481 | put_pid_ns(ns); |
| 2482 | return l; |
| 2483 | } |
| 2484 | init_rwsem(&l->mutex); |
| 2485 | down_write(&l->mutex); |
| 2486 | l->key.type = type; |
| 2487 | l->key.ns = ns; |
| 2488 | l->use_count = 0; /* don't increment here */ |
| 2489 | l->list = NULL; |
| 2490 | l->owner = cgrp; |
| 2491 | list_add(&l->links, &cgrp->pidlists); |
| 2492 | mutex_unlock(&cgrp->pidlist_mutex); |
| 2493 | return l; |
| 2494 | } |
| 2495 | |
| 2496 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2497 | * Load a cgroup's pidarray with either procs' tgids or tasks' pids |
| 2498 | */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 2499 | static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, |
| 2500 | struct cgroup_pidlist **lp) |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2501 | { |
| 2502 | pid_t *array; |
| 2503 | int length; |
| 2504 | int pid, n = 0; /* used for populating the array */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2505 | struct cgroup_iter it; |
| 2506 | struct task_struct *tsk; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2507 | struct cgroup_pidlist *l; |
| 2508 | |
| 2509 | /* |
| 2510 | * If cgroup gets more users after we read count, we won't have |
| 2511 | * enough space - tough. This race is indistinguishable to the |
| 2512 | * caller from the case that the additional cgroup users didn't |
| 2513 | * show up until sometime later on. |
| 2514 | */ |
| 2515 | length = cgroup_task_count(cgrp); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 2516 | array = pidlist_allocate(length); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2517 | if (!array) |
| 2518 | return -ENOMEM; |
| 2519 | /* now, populate the array */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2520 | cgroup_iter_start(cgrp, &it); |
| 2521 | while ((tsk = cgroup_iter_next(cgrp, &it))) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2522 | if (unlikely(n == length)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2523 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2524 | /* get tgid or pid for procs or tasks file respectively */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 2525 | if (type == CGROUP_FILE_PROCS) |
| 2526 | pid = task_tgid_vnr(tsk); |
| 2527 | else |
| 2528 | pid = task_pid_vnr(tsk); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2529 | if (pid > 0) /* make sure to only use valid results */ |
| 2530 | array[n++] = pid; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2531 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2532 | cgroup_iter_end(cgrp, &it); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2533 | length = n; |
| 2534 | /* now sort & (if procs) strip out duplicates */ |
| 2535 | sort(array, length, sizeof(pid_t), cmppid, NULL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 2536 | if (type == CGROUP_FILE_PROCS) |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2537 | length = pidlist_uniq(&array, length); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 2538 | l = cgroup_pidlist_find(cgrp, type); |
| 2539 | if (!l) { |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 2540 | pidlist_free(array); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 2541 | return -ENOMEM; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2542 | } |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 2543 | /* store array, freeing old if necessary - lock already held */ |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 2544 | pidlist_free(l->list); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2545 | l->list = array; |
| 2546 | l->length = length; |
| 2547 | l->use_count++; |
| 2548 | up_write(&l->mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 2549 | *lp = l; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2550 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2551 | } |
| 2552 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 2553 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2554 | * cgroupstats_build - build and fill cgroupstats |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 2555 | * @stats: cgroupstats to fill information into |
| 2556 | * @dentry: A dentry entry belonging to the cgroup for which stats have |
| 2557 | * been requested. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2558 | * |
| 2559 | * Build and fill cgroupstats so that taskstats can export it to user |
| 2560 | * space. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 2561 | */ |
| 2562 | int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) |
| 2563 | { |
| 2564 | int ret = -EINVAL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2565 | struct cgroup *cgrp; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 2566 | struct cgroup_iter it; |
| 2567 | struct task_struct *tsk; |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 2568 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 2569 | /* |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 2570 | * Validate dentry by checking the superblock operations, |
| 2571 | * and make sure it's a directory. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 2572 | */ |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 2573 | if (dentry->d_sb->s_op != &cgroup_ops || |
| 2574 | !S_ISDIR(dentry->d_inode->i_mode)) |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 2575 | goto err; |
| 2576 | |
| 2577 | ret = 0; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2578 | cgrp = dentry->d_fsdata; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 2579 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2580 | cgroup_iter_start(cgrp, &it); |
| 2581 | while ((tsk = cgroup_iter_next(cgrp, &it))) { |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 2582 | switch (tsk->state) { |
| 2583 | case TASK_RUNNING: |
| 2584 | stats->nr_running++; |
| 2585 | break; |
| 2586 | case TASK_INTERRUPTIBLE: |
| 2587 | stats->nr_sleeping++; |
| 2588 | break; |
| 2589 | case TASK_UNINTERRUPTIBLE: |
| 2590 | stats->nr_uninterruptible++; |
| 2591 | break; |
| 2592 | case TASK_STOPPED: |
| 2593 | stats->nr_stopped++; |
| 2594 | break; |
| 2595 | default: |
| 2596 | if (delayacct_is_task_waiting_on_io(tsk)) |
| 2597 | stats->nr_io_wait++; |
| 2598 | break; |
| 2599 | } |
| 2600 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2601 | cgroup_iter_end(cgrp, &it); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 2602 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 2603 | err: |
| 2604 | return ret; |
| 2605 | } |
| 2606 | |
Paul Menage | 8f3ff20 | 2009-09-23 15:56:25 -0700 | [diff] [blame] | 2607 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2608 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2609 | * 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] | 2610 | * 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] | 2611 | * in the cgroup->l->list array. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2612 | */ |
| 2613 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2614 | static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2615 | { |
| 2616 | /* |
| 2617 | * Initially we receive a position value that corresponds to |
| 2618 | * one more than the last pid shown (or 0 on the first call or |
| 2619 | * after a seek to the start). Use a binary-search to find the |
| 2620 | * next pid to display, if any |
| 2621 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2622 | struct cgroup_pidlist *l = s->private; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2623 | int index = 0, pid = *pos; |
| 2624 | int *iter; |
| 2625 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2626 | down_read(&l->mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2627 | if (pid) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2628 | int end = l->length; |
Stephen Rothwell | 2077776 | 2008-10-21 16:11:20 +1100 | [diff] [blame] | 2629 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2630 | while (index < end) { |
| 2631 | int mid = (index + end) / 2; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2632 | if (l->list[mid] == pid) { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2633 | index = mid; |
| 2634 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2635 | } else if (l->list[mid] <= pid) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2636 | index = mid + 1; |
| 2637 | else |
| 2638 | end = mid; |
| 2639 | } |
| 2640 | } |
| 2641 | /* If we're off the end of the array, we're done */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2642 | if (index >= l->length) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2643 | return NULL; |
| 2644 | /* Update the abstract position to be the actual pid that we found */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2645 | iter = l->list + index; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2646 | *pos = *iter; |
| 2647 | return iter; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2648 | } |
| 2649 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2650 | static void cgroup_pidlist_stop(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2651 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2652 | struct cgroup_pidlist *l = s->private; |
| 2653 | up_read(&l->mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2654 | } |
| 2655 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2656 | 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] | 2657 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2658 | struct cgroup_pidlist *l = s->private; |
| 2659 | pid_t *p = v; |
| 2660 | pid_t *end = l->list + l->length; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2661 | /* |
| 2662 | * Advance to the next pid in the array. If this goes off the |
| 2663 | * end, we're done |
| 2664 | */ |
| 2665 | p++; |
| 2666 | if (p >= end) { |
| 2667 | return NULL; |
| 2668 | } else { |
| 2669 | *pos = *p; |
| 2670 | return p; |
| 2671 | } |
| 2672 | } |
| 2673 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2674 | static int cgroup_pidlist_show(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2675 | { |
| 2676 | return seq_printf(s, "%d\n", *(int *)v); |
| 2677 | } |
| 2678 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2679 | /* |
| 2680 | * seq_operations functions for iterating on pidlists through seq_file - |
| 2681 | * independent of whether it's tasks or procs |
| 2682 | */ |
| 2683 | static const struct seq_operations cgroup_pidlist_seq_operations = { |
| 2684 | .start = cgroup_pidlist_start, |
| 2685 | .stop = cgroup_pidlist_stop, |
| 2686 | .next = cgroup_pidlist_next, |
| 2687 | .show = cgroup_pidlist_show, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2688 | }; |
| 2689 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2690 | static void cgroup_release_pid_array(struct cgroup_pidlist *l) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2691 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 2692 | /* |
| 2693 | * the case where we're the last user of this particular pidlist will |
| 2694 | * have us remove it from the cgroup's list, which entails taking the |
| 2695 | * mutex. since in pidlist_find the pidlist->lock depends on cgroup-> |
| 2696 | * pidlist_mutex, we have to take pidlist_mutex first. |
| 2697 | */ |
| 2698 | mutex_lock(&l->owner->pidlist_mutex); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2699 | down_write(&l->mutex); |
| 2700 | BUG_ON(!l->use_count); |
| 2701 | if (!--l->use_count) { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 2702 | /* we're the last user if refcount is 0; remove and free */ |
| 2703 | list_del(&l->links); |
| 2704 | mutex_unlock(&l->owner->pidlist_mutex); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 2705 | pidlist_free(l->list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 2706 | put_pid_ns(l->key.ns); |
| 2707 | up_write(&l->mutex); |
| 2708 | kfree(l); |
| 2709 | return; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2710 | } |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 2711 | mutex_unlock(&l->owner->pidlist_mutex); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2712 | up_write(&l->mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2713 | } |
| 2714 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2715 | static int cgroup_pidlist_release(struct inode *inode, struct file *file) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2716 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2717 | struct cgroup_pidlist *l; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2718 | if (!(file->f_mode & FMODE_READ)) |
| 2719 | return 0; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2720 | /* |
| 2721 | * the seq_file will only be initialized if the file was opened for |
| 2722 | * reading; hence we check if it's not null only in that case. |
| 2723 | */ |
| 2724 | l = ((struct seq_file *)file->private_data)->private; |
| 2725 | cgroup_release_pid_array(l); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2726 | return seq_release(inode, file); |
| 2727 | } |
| 2728 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2729 | static const struct file_operations cgroup_pidlist_operations = { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2730 | .read = seq_read, |
| 2731 | .llseek = seq_lseek, |
| 2732 | .write = cgroup_file_write, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2733 | .release = cgroup_pidlist_release, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2734 | }; |
| 2735 | |
| 2736 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2737 | * The following functions handle opens on a file that displays a pidlist |
| 2738 | * (tasks or procs). Prepare an array of the process/thread IDs of whoever's |
| 2739 | * in the cgroup. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2740 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2741 | /* helper function for the two below it */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 2742 | static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2743 | { |
| 2744 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 2745 | struct cgroup_pidlist *l; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2746 | int retval; |
| 2747 | |
| 2748 | /* Nothing to do for write-only files */ |
| 2749 | if (!(file->f_mode & FMODE_READ)) |
| 2750 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2751 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2752 | /* have the array populated */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 2753 | retval = pidlist_array_load(cgrp, type, &l); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2754 | if (retval) |
| 2755 | return retval; |
| 2756 | /* configure file information */ |
| 2757 | file->f_op = &cgroup_pidlist_operations; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2758 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2759 | retval = seq_open(file, &cgroup_pidlist_seq_operations); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2760 | if (retval) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2761 | cgroup_release_pid_array(l); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2762 | return retval; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2763 | } |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2764 | ((struct seq_file *)file->private_data)->private = l; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2765 | return 0; |
| 2766 | } |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2767 | static int cgroup_tasks_open(struct inode *unused, struct file *file) |
| 2768 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 2769 | return cgroup_pidlist_open(file, CGROUP_FILE_TASKS); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2770 | } |
| 2771 | static int cgroup_procs_open(struct inode *unused, struct file *file) |
| 2772 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 2773 | return cgroup_pidlist_open(file, CGROUP_FILE_PROCS); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2774 | } |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2775 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2776 | static u64 cgroup_read_notify_on_release(struct cgroup *cgrp, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2777 | struct cftype *cft) |
| 2778 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2779 | return notify_on_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2780 | } |
| 2781 | |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2782 | static int cgroup_write_notify_on_release(struct cgroup *cgrp, |
| 2783 | struct cftype *cft, |
| 2784 | u64 val) |
| 2785 | { |
| 2786 | clear_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 2787 | if (val) |
| 2788 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 2789 | else |
| 2790 | clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 2791 | return 0; |
| 2792 | } |
| 2793 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2794 | /* |
| 2795 | * for the common functions, 'private' gives the type of file |
| 2796 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2797 | /* for hysterical raisins, we can't put this on the older files */ |
| 2798 | #define CGROUP_FILE_GENERIC_PREFIX "cgroup." |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2799 | static struct cftype files[] = { |
| 2800 | { |
| 2801 | .name = "tasks", |
| 2802 | .open = cgroup_tasks_open, |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2803 | .write_u64 = cgroup_tasks_write, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2804 | .release = cgroup_pidlist_release, |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2805 | .mode = S_IRUGO | S_IWUSR, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2806 | }, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2807 | { |
| 2808 | .name = CGROUP_FILE_GENERIC_PREFIX "procs", |
| 2809 | .open = cgroup_procs_open, |
| 2810 | /* .write_u64 = cgroup_procs_write, TODO */ |
| 2811 | .release = cgroup_pidlist_release, |
| 2812 | .mode = S_IRUGO, |
| 2813 | }, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2814 | { |
| 2815 | .name = "notify_on_release", |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2816 | .read_u64 = cgroup_read_notify_on_release, |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2817 | .write_u64 = cgroup_write_notify_on_release, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2818 | }, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2819 | }; |
| 2820 | |
| 2821 | static struct cftype cft_release_agent = { |
| 2822 | .name = "release_agent", |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2823 | .read_seq_string = cgroup_release_agent_show, |
| 2824 | .write_string = cgroup_release_agent_write, |
| 2825 | .max_write_len = PATH_MAX, |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2826 | }; |
| 2827 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2828 | static int cgroup_populate_dir(struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2829 | { |
| 2830 | int err; |
| 2831 | struct cgroup_subsys *ss; |
| 2832 | |
| 2833 | /* First clear out any existing files */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2834 | cgroup_clear_directory(cgrp->dentry); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2835 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2836 | err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files)); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2837 | if (err < 0) |
| 2838 | return err; |
| 2839 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2840 | if (cgrp == cgrp->top_cgroup) { |
| 2841 | if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2842 | return err; |
| 2843 | } |
| 2844 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2845 | for_each_subsys(cgrp->root, ss) { |
| 2846 | if (ss->populate && (err = ss->populate(ss, cgrp)) < 0) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2847 | return err; |
| 2848 | } |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 2849 | /* This cgroup is ready now */ |
| 2850 | for_each_subsys(cgrp->root, ss) { |
| 2851 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 2852 | /* |
| 2853 | * Update id->css pointer and make this css visible from |
| 2854 | * CSS ID functions. This pointer will be dereferened |
| 2855 | * from RCU-read-side without locks. |
| 2856 | */ |
| 2857 | if (css->id) |
| 2858 | rcu_assign_pointer(css->id->css, css); |
| 2859 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2860 | |
| 2861 | return 0; |
| 2862 | } |
| 2863 | |
| 2864 | static void init_cgroup_css(struct cgroup_subsys_state *css, |
| 2865 | struct cgroup_subsys *ss, |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2866 | struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2867 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2868 | css->cgroup = cgrp; |
Paul Menage | e7c5ec9 | 2009-01-07 18:08:38 -0800 | [diff] [blame] | 2869 | atomic_set(&css->refcnt, 1); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2870 | css->flags = 0; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 2871 | css->id = NULL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2872 | if (cgrp == dummytop) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2873 | set_bit(CSS_ROOT, &css->flags); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2874 | BUG_ON(cgrp->subsys[ss->subsys_id]); |
| 2875 | cgrp->subsys[ss->subsys_id] = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2876 | } |
| 2877 | |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 2878 | static void cgroup_lock_hierarchy(struct cgroupfs_root *root) |
| 2879 | { |
| 2880 | /* We need to take each hierarchy_mutex in a consistent order */ |
| 2881 | int i; |
| 2882 | |
| 2883 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 2884 | struct cgroup_subsys *ss = subsys[i]; |
| 2885 | if (ss->root == root) |
Li Zefan | cfebe56 | 2009-02-11 13:04:36 -0800 | [diff] [blame] | 2886 | mutex_lock(&ss->hierarchy_mutex); |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 2887 | } |
| 2888 | } |
| 2889 | |
| 2890 | static void cgroup_unlock_hierarchy(struct cgroupfs_root *root) |
| 2891 | { |
| 2892 | int i; |
| 2893 | |
| 2894 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 2895 | struct cgroup_subsys *ss = subsys[i]; |
| 2896 | if (ss->root == root) |
| 2897 | mutex_unlock(&ss->hierarchy_mutex); |
| 2898 | } |
| 2899 | } |
| 2900 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2901 | /* |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2902 | * cgroup_create - create a cgroup |
| 2903 | * @parent: cgroup that will be parent of the new cgroup |
| 2904 | * @dentry: dentry of the new cgroup |
| 2905 | * @mode: mode to set on new inode |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2906 | * |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2907 | * Must be called with the mutex on the parent inode held |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2908 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2909 | static long cgroup_create(struct cgroup *parent, struct dentry *dentry, |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2910 | mode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2911 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2912 | struct cgroup *cgrp; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2913 | struct cgroupfs_root *root = parent->root; |
| 2914 | int err = 0; |
| 2915 | struct cgroup_subsys *ss; |
| 2916 | struct super_block *sb = root->sb; |
| 2917 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2918 | cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL); |
| 2919 | if (!cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2920 | return -ENOMEM; |
| 2921 | |
| 2922 | /* Grab a reference on the superblock so the hierarchy doesn't |
| 2923 | * get deleted on unmount if there are child cgroups. This |
| 2924 | * can be done outside cgroup_mutex, since the sb can't |
| 2925 | * disappear while someone has an open control file on the |
| 2926 | * fs */ |
| 2927 | atomic_inc(&sb->s_active); |
| 2928 | |
| 2929 | mutex_lock(&cgroup_mutex); |
| 2930 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2931 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2932 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2933 | cgrp->parent = parent; |
| 2934 | cgrp->root = parent->root; |
| 2935 | cgrp->top_cgroup = parent->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2936 | |
Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 2937 | if (notify_on_release(parent)) |
| 2938 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 2939 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2940 | for_each_subsys(root, ss) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2941 | struct cgroup_subsys_state *css = ss->create(ss, cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2942 | if (IS_ERR(css)) { |
| 2943 | err = PTR_ERR(css); |
| 2944 | goto err_destroy; |
| 2945 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2946 | init_cgroup_css(css, ss, cgrp); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 2947 | if (ss->use_id) |
| 2948 | if (alloc_css_id(ss, parent, cgrp)) |
| 2949 | goto err_destroy; |
| 2950 | /* At error, ->destroy() callback has to free assigned ID. */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2951 | } |
| 2952 | |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 2953 | cgroup_lock_hierarchy(root); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2954 | list_add(&cgrp->sibling, &cgrp->parent->children); |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 2955 | cgroup_unlock_hierarchy(root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2956 | root->number_of_cgroups++; |
| 2957 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2958 | err = cgroup_create_dir(cgrp, dentry, mode); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2959 | if (err < 0) |
| 2960 | goto err_remove; |
| 2961 | |
| 2962 | /* The cgroup directory was pre-locked for us */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2963 | BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2964 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2965 | err = cgroup_populate_dir(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2966 | /* If err < 0, we have a half-filled directory - oh well ;) */ |
| 2967 | |
| 2968 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2969 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2970 | |
| 2971 | return 0; |
| 2972 | |
| 2973 | err_remove: |
| 2974 | |
KAMEZAWA Hiroyuki | baef99a | 2009-01-29 14:25:10 -0800 | [diff] [blame] | 2975 | cgroup_lock_hierarchy(root); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2976 | list_del(&cgrp->sibling); |
KAMEZAWA Hiroyuki | baef99a | 2009-01-29 14:25:10 -0800 | [diff] [blame] | 2977 | cgroup_unlock_hierarchy(root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2978 | root->number_of_cgroups--; |
| 2979 | |
| 2980 | err_destroy: |
| 2981 | |
| 2982 | for_each_subsys(root, ss) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2983 | if (cgrp->subsys[ss->subsys_id]) |
| 2984 | ss->destroy(ss, cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2985 | } |
| 2986 | |
| 2987 | mutex_unlock(&cgroup_mutex); |
| 2988 | |
| 2989 | /* Release the reference count that we took on the superblock */ |
| 2990 | deactivate_super(sb); |
| 2991 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2992 | kfree(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2993 | return err; |
| 2994 | } |
| 2995 | |
| 2996 | static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 2997 | { |
| 2998 | struct cgroup *c_parent = dentry->d_parent->d_fsdata; |
| 2999 | |
| 3000 | /* the vfs holds inode->i_mutex already */ |
| 3001 | return cgroup_create(c_parent, dentry, mode | S_IFDIR); |
| 3002 | } |
| 3003 | |
Li Zefan | 55b6fd0 | 2008-07-29 22:33:20 -0700 | [diff] [blame] | 3004 | static int cgroup_has_css_refs(struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3005 | { |
| 3006 | /* Check the reference count on each subsystem. Since we |
| 3007 | * already established that there are no tasks in the |
Paul Menage | e7c5ec9 | 2009-01-07 18:08:38 -0800 | [diff] [blame] | 3008 | * cgroup, if the css refcount is also 1, then there should |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3009 | * be no outstanding references, so the subsystem is safe to |
| 3010 | * destroy. We scan across all subsystems rather than using |
| 3011 | * the per-hierarchy linked list of mounted subsystems since |
| 3012 | * we can be called via check_for_release() with no |
| 3013 | * synchronization other than RCU, and the subsystem linked |
| 3014 | * list isn't RCU-safe */ |
| 3015 | int i; |
| 3016 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 3017 | struct cgroup_subsys *ss = subsys[i]; |
| 3018 | struct cgroup_subsys_state *css; |
| 3019 | /* Skip subsystems not in this hierarchy */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3020 | if (ss->root != cgrp->root) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3021 | continue; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3022 | css = cgrp->subsys[ss->subsys_id]; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3023 | /* When called from check_for_release() it's possible |
| 3024 | * that by this point the cgroup has been removed |
| 3025 | * and the css deleted. But a false-positive doesn't |
| 3026 | * matter, since it can only happen if the cgroup |
| 3027 | * has been deleted and hence no longer needs the |
| 3028 | * release agent to be called anyway. */ |
Paul Menage | e7c5ec9 | 2009-01-07 18:08:38 -0800 | [diff] [blame] | 3029 | if (css && (atomic_read(&css->refcnt) > 1)) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3030 | return 1; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3031 | } |
| 3032 | return 0; |
| 3033 | } |
| 3034 | |
Paul Menage | e7c5ec9 | 2009-01-07 18:08:38 -0800 | [diff] [blame] | 3035 | /* |
| 3036 | * Atomically mark all (or else none) of the cgroup's CSS objects as |
| 3037 | * CSS_REMOVED. Return true on success, or false if the cgroup has |
| 3038 | * busy subsystems. Call with cgroup_mutex held |
| 3039 | */ |
| 3040 | |
| 3041 | static int cgroup_clear_css_refs(struct cgroup *cgrp) |
| 3042 | { |
| 3043 | struct cgroup_subsys *ss; |
| 3044 | unsigned long flags; |
| 3045 | bool failed = false; |
| 3046 | local_irq_save(flags); |
| 3047 | for_each_subsys(cgrp->root, ss) { |
| 3048 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 3049 | int refcnt; |
Paul Menage | 804b3c2 | 2009-01-29 14:25:21 -0800 | [diff] [blame] | 3050 | while (1) { |
Paul Menage | e7c5ec9 | 2009-01-07 18:08:38 -0800 | [diff] [blame] | 3051 | /* We can only remove a CSS with a refcnt==1 */ |
| 3052 | refcnt = atomic_read(&css->refcnt); |
| 3053 | if (refcnt > 1) { |
| 3054 | failed = true; |
| 3055 | goto done; |
| 3056 | } |
| 3057 | BUG_ON(!refcnt); |
| 3058 | /* |
| 3059 | * Drop the refcnt to 0 while we check other |
| 3060 | * subsystems. This will cause any racing |
| 3061 | * css_tryget() to spin until we set the |
| 3062 | * CSS_REMOVED bits or abort |
| 3063 | */ |
Paul Menage | 804b3c2 | 2009-01-29 14:25:21 -0800 | [diff] [blame] | 3064 | if (atomic_cmpxchg(&css->refcnt, refcnt, 0) == refcnt) |
| 3065 | break; |
| 3066 | cpu_relax(); |
| 3067 | } |
Paul Menage | e7c5ec9 | 2009-01-07 18:08:38 -0800 | [diff] [blame] | 3068 | } |
| 3069 | done: |
| 3070 | for_each_subsys(cgrp->root, ss) { |
| 3071 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 3072 | if (failed) { |
| 3073 | /* |
| 3074 | * Restore old refcnt if we previously managed |
| 3075 | * to clear it from 1 to 0 |
| 3076 | */ |
| 3077 | if (!atomic_read(&css->refcnt)) |
| 3078 | atomic_set(&css->refcnt, 1); |
| 3079 | } else { |
| 3080 | /* Commit the fact that the CSS is removed */ |
| 3081 | set_bit(CSS_REMOVED, &css->flags); |
| 3082 | } |
| 3083 | } |
| 3084 | local_irq_restore(flags); |
| 3085 | return !failed; |
| 3086 | } |
| 3087 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3088 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry) |
| 3089 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3090 | struct cgroup *cgrp = dentry->d_fsdata; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3091 | struct dentry *d; |
| 3092 | struct cgroup *parent; |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 3093 | DEFINE_WAIT(wait); |
| 3094 | int ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3095 | |
| 3096 | /* the vfs holds both inode->i_mutex already */ |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 3097 | again: |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3098 | mutex_lock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3099 | if (atomic_read(&cgrp->count) != 0) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3100 | mutex_unlock(&cgroup_mutex); |
| 3101 | return -EBUSY; |
| 3102 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3103 | if (!list_empty(&cgrp->children)) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3104 | mutex_unlock(&cgroup_mutex); |
| 3105 | return -EBUSY; |
| 3106 | } |
KAMEZAWA Hiroyuki | 3fa59df | 2008-11-19 15:36:34 -0800 | [diff] [blame] | 3107 | mutex_unlock(&cgroup_mutex); |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3108 | |
KAMEZAWA Hiroyuki | 4fca88c | 2008-02-07 00:14:27 -0800 | [diff] [blame] | 3109 | /* |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 3110 | * In general, subsystem has no css->refcnt after pre_destroy(). But |
| 3111 | * in racy cases, subsystem may have to get css->refcnt after |
| 3112 | * pre_destroy() and it makes rmdir return with -EBUSY. This sometimes |
| 3113 | * make rmdir return -EBUSY too often. To avoid that, we use waitqueue |
| 3114 | * for cgroup's rmdir. CGRP_WAIT_ON_RMDIR is for synchronizing rmdir |
| 3115 | * and subsystem's reference count handling. Please see css_get/put |
| 3116 | * and css_tryget() and cgroup_wakeup_rmdir_waiter() implementation. |
| 3117 | */ |
| 3118 | set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags); |
| 3119 | |
| 3120 | /* |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3121 | * Call pre_destroy handlers of subsys. Notify subsystems |
| 3122 | * that rmdir() request comes. |
KAMEZAWA Hiroyuki | 4fca88c | 2008-02-07 00:14:27 -0800 | [diff] [blame] | 3123 | */ |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 3124 | ret = cgroup_call_pre_destroy(cgrp); |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 3125 | if (ret) { |
| 3126 | clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags); |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 3127 | return ret; |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 3128 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3129 | |
KAMEZAWA Hiroyuki | 3fa59df | 2008-11-19 15:36:34 -0800 | [diff] [blame] | 3130 | mutex_lock(&cgroup_mutex); |
| 3131 | parent = cgrp->parent; |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 3132 | if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) { |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 3133 | clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3134 | mutex_unlock(&cgroup_mutex); |
| 3135 | return -EBUSY; |
| 3136 | } |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 3137 | prepare_to_wait(&cgroup_rmdir_waitq, &wait, TASK_INTERRUPTIBLE); |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 3138 | if (!cgroup_clear_css_refs(cgrp)) { |
| 3139 | mutex_unlock(&cgroup_mutex); |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 3140 | /* |
| 3141 | * Because someone may call cgroup_wakeup_rmdir_waiter() before |
| 3142 | * prepare_to_wait(), we need to check this flag. |
| 3143 | */ |
| 3144 | if (test_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags)) |
| 3145 | schedule(); |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 3146 | finish_wait(&cgroup_rmdir_waitq, &wait); |
| 3147 | clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags); |
| 3148 | if (signal_pending(current)) |
| 3149 | return -EINTR; |
| 3150 | goto again; |
| 3151 | } |
| 3152 | /* NO css_tryget() can success after here. */ |
| 3153 | finish_wait(&cgroup_rmdir_waitq, &wait); |
| 3154 | clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3155 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3156 | spin_lock(&release_list_lock); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3157 | set_bit(CGRP_REMOVED, &cgrp->flags); |
| 3158 | if (!list_empty(&cgrp->release_list)) |
| 3159 | list_del(&cgrp->release_list); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3160 | spin_unlock(&release_list_lock); |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 3161 | |
| 3162 | cgroup_lock_hierarchy(cgrp->root); |
| 3163 | /* delete this cgroup from parent->children */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3164 | list_del(&cgrp->sibling); |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 3165 | cgroup_unlock_hierarchy(cgrp->root); |
| 3166 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3167 | spin_lock(&cgrp->dentry->d_lock); |
| 3168 | d = dget(cgrp->dentry); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3169 | spin_unlock(&d->d_lock); |
| 3170 | |
| 3171 | cgroup_d_remove_dir(d); |
| 3172 | dput(d); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3173 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3174 | set_bit(CGRP_RELEASABLE, &parent->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3175 | check_for_release(parent); |
| 3176 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3177 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3178 | return 0; |
| 3179 | } |
| 3180 | |
Li Zefan | 06a1192 | 2008-04-29 01:00:07 -0700 | [diff] [blame] | 3181 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3182 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3183 | struct cgroup_subsys_state *css; |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 3184 | |
| 3185 | printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3186 | |
| 3187 | /* Create the top cgroup state for this subsystem */ |
Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 3188 | list_add(&ss->sibling, &rootnode.subsys_list); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3189 | ss->root = &rootnode; |
| 3190 | css = ss->create(ss, dummytop); |
| 3191 | /* We don't handle early failures gracefully */ |
| 3192 | BUG_ON(IS_ERR(css)); |
| 3193 | init_cgroup_css(css, ss, dummytop); |
| 3194 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 3195 | /* Update the init_css_set to contain a subsys |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3196 | * pointer to this state - since the subsystem is |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 3197 | * newly registered, all tasks and hence the |
| 3198 | * init_css_set is in the subsystem's top cgroup. */ |
| 3199 | init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3200 | |
| 3201 | need_forkexit_callback |= ss->fork || ss->exit; |
| 3202 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 3203 | /* At system boot, before all subsystems have been |
| 3204 | * registered, no tasks have been forked, so we don't |
| 3205 | * need to invoke fork callbacks here. */ |
| 3206 | BUG_ON(!list_empty(&init_task.tasks)); |
| 3207 | |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 3208 | mutex_init(&ss->hierarchy_mutex); |
Li Zefan | cfebe56 | 2009-02-11 13:04:36 -0800 | [diff] [blame] | 3209 | lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3210 | ss->active = 1; |
| 3211 | } |
| 3212 | |
| 3213 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3214 | * cgroup_init_early - cgroup initialization at system boot |
| 3215 | * |
| 3216 | * Initialize cgroups at system boot, and initialize any |
| 3217 | * subsystems that request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3218 | */ |
| 3219 | int __init cgroup_init_early(void) |
| 3220 | { |
| 3221 | int i; |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 3222 | atomic_set(&init_css_set.refcount, 1); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3223 | INIT_LIST_HEAD(&init_css_set.cg_links); |
| 3224 | INIT_LIST_HEAD(&init_css_set.tasks); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 3225 | INIT_HLIST_NODE(&init_css_set.hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3226 | css_set_count = 1; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3227 | init_cgroup_root(&rootnode); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3228 | root_count = 1; |
| 3229 | init_task.cgroups = &init_css_set; |
| 3230 | |
| 3231 | init_css_set_link.cg = &init_css_set; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 3232 | init_css_set_link.cgrp = dummytop; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3233 | list_add(&init_css_set_link.cgrp_link_list, |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3234 | &rootnode.top_cgroup.css_sets); |
| 3235 | list_add(&init_css_set_link.cg_link_list, |
| 3236 | &init_css_set.cg_links); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3237 | |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 3238 | for (i = 0; i < CSS_SET_TABLE_SIZE; i++) |
| 3239 | INIT_HLIST_HEAD(&css_set_table[i]); |
| 3240 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3241 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 3242 | struct cgroup_subsys *ss = subsys[i]; |
| 3243 | |
| 3244 | BUG_ON(!ss->name); |
| 3245 | BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN); |
| 3246 | BUG_ON(!ss->create); |
| 3247 | BUG_ON(!ss->destroy); |
| 3248 | if (ss->subsys_id != i) { |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 3249 | printk(KERN_ERR "cgroup: Subsys %s id == %d\n", |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3250 | ss->name, ss->subsys_id); |
| 3251 | BUG(); |
| 3252 | } |
| 3253 | |
| 3254 | if (ss->early_init) |
| 3255 | cgroup_init_subsys(ss); |
| 3256 | } |
| 3257 | return 0; |
| 3258 | } |
| 3259 | |
| 3260 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3261 | * cgroup_init - cgroup initialization |
| 3262 | * |
| 3263 | * Register cgroup filesystem and /proc file, and initialize |
| 3264 | * any subsystems that didn't request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3265 | */ |
| 3266 | int __init cgroup_init(void) |
| 3267 | { |
| 3268 | int err; |
| 3269 | int i; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 3270 | struct hlist_head *hhead; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 3271 | |
| 3272 | err = bdi_init(&cgroup_backing_dev_info); |
| 3273 | if (err) |
| 3274 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3275 | |
| 3276 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 3277 | struct cgroup_subsys *ss = subsys[i]; |
| 3278 | if (!ss->early_init) |
| 3279 | cgroup_init_subsys(ss); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 3280 | if (ss->use_id) |
| 3281 | cgroup_subsys_init_idr(ss); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3282 | } |
| 3283 | |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 3284 | /* Add init_css_set to the hash table */ |
| 3285 | hhead = css_set_hash(init_css_set.subsys); |
| 3286 | hlist_add_head(&init_css_set.hlist, hhead); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 3287 | BUG_ON(!init_root_id(&rootnode)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3288 | err = register_filesystem(&cgroup_fs_type); |
| 3289 | if (err < 0) |
| 3290 | goto out; |
| 3291 | |
Li Zefan | 46ae220 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 3292 | proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 3293 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3294 | out: |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 3295 | if (err) |
| 3296 | bdi_destroy(&cgroup_backing_dev_info); |
| 3297 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3298 | return err; |
| 3299 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 3300 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 3301 | /* |
| 3302 | * proc_cgroup_show() |
| 3303 | * - Print task's cgroup paths into seq_file, one line for each hierarchy |
| 3304 | * - Used for /proc/<pid>/cgroup. |
| 3305 | * - No need to task_lock(tsk) on this tsk->cgroup reference, as it |
| 3306 | * doesn't really matter if tsk->cgroup changes after we read it, |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 3307 | * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 3308 | * anyway. No need to check that tsk->cgroup != NULL, thanks to |
| 3309 | * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks |
| 3310 | * cgroup to top_cgroup. |
| 3311 | */ |
| 3312 | |
| 3313 | /* TODO: Use a proper seq_file iterator */ |
| 3314 | static int proc_cgroup_show(struct seq_file *m, void *v) |
| 3315 | { |
| 3316 | struct pid *pid; |
| 3317 | struct task_struct *tsk; |
| 3318 | char *buf; |
| 3319 | int retval; |
| 3320 | struct cgroupfs_root *root; |
| 3321 | |
| 3322 | retval = -ENOMEM; |
| 3323 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 3324 | if (!buf) |
| 3325 | goto out; |
| 3326 | |
| 3327 | retval = -ESRCH; |
| 3328 | pid = m->private; |
| 3329 | tsk = get_pid_task(pid, PIDTYPE_PID); |
| 3330 | if (!tsk) |
| 3331 | goto out_free; |
| 3332 | |
| 3333 | retval = 0; |
| 3334 | |
| 3335 | mutex_lock(&cgroup_mutex); |
| 3336 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 3337 | for_each_active_root(root) { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 3338 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3339 | struct cgroup *cgrp; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 3340 | int count = 0; |
| 3341 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 3342 | seq_printf(m, "%d:", root->hierarchy_id); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 3343 | for_each_subsys(root, ss) |
| 3344 | seq_printf(m, "%s%s", count++ ? "," : "", ss->name); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 3345 | if (strlen(root->name)) |
| 3346 | seq_printf(m, "%sname=%s", count ? "," : "", |
| 3347 | root->name); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 3348 | seq_putc(m, ':'); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 3349 | cgrp = task_cgroup_from_root(tsk, root); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3350 | retval = cgroup_path(cgrp, buf, PAGE_SIZE); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 3351 | if (retval < 0) |
| 3352 | goto out_unlock; |
| 3353 | seq_puts(m, buf); |
| 3354 | seq_putc(m, '\n'); |
| 3355 | } |
| 3356 | |
| 3357 | out_unlock: |
| 3358 | mutex_unlock(&cgroup_mutex); |
| 3359 | put_task_struct(tsk); |
| 3360 | out_free: |
| 3361 | kfree(buf); |
| 3362 | out: |
| 3363 | return retval; |
| 3364 | } |
| 3365 | |
| 3366 | static int cgroup_open(struct inode *inode, struct file *file) |
| 3367 | { |
| 3368 | struct pid *pid = PROC_I(inode)->pid; |
| 3369 | return single_open(file, proc_cgroup_show, pid); |
| 3370 | } |
| 3371 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 3372 | const struct file_operations proc_cgroup_operations = { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 3373 | .open = cgroup_open, |
| 3374 | .read = seq_read, |
| 3375 | .llseek = seq_lseek, |
| 3376 | .release = single_release, |
| 3377 | }; |
| 3378 | |
| 3379 | /* Display information about each subsystem and each hierarchy */ |
| 3380 | static int proc_cgroupstats_show(struct seq_file *m, void *v) |
| 3381 | { |
| 3382 | int i; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 3383 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 3384 | seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n"); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 3385 | mutex_lock(&cgroup_mutex); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 3386 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 3387 | struct cgroup_subsys *ss = subsys[i]; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 3388 | seq_printf(m, "%s\t%d\t%d\t%d\n", |
| 3389 | ss->name, ss->root->hierarchy_id, |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 3390 | ss->root->number_of_cgroups, !ss->disabled); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 3391 | } |
| 3392 | mutex_unlock(&cgroup_mutex); |
| 3393 | return 0; |
| 3394 | } |
| 3395 | |
| 3396 | static int cgroupstats_open(struct inode *inode, struct file *file) |
| 3397 | { |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 3398 | return single_open(file, proc_cgroupstats_show, NULL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 3399 | } |
| 3400 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 3401 | static const struct file_operations proc_cgroupstats_operations = { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 3402 | .open = cgroupstats_open, |
| 3403 | .read = seq_read, |
| 3404 | .llseek = seq_lseek, |
| 3405 | .release = single_release, |
| 3406 | }; |
| 3407 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 3408 | /** |
| 3409 | * cgroup_fork - attach newly forked task to its parents cgroup. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3410 | * @child: pointer to task_struct of forking parent process. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 3411 | * |
| 3412 | * Description: A task inherits its parent's cgroup at fork(). |
| 3413 | * |
| 3414 | * A pointer to the shared css_set was automatically copied in |
| 3415 | * fork.c by dup_task_struct(). However, we ignore that copy, since |
| 3416 | * 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] | 3417 | * might no longer be a valid cgroup pointer. cgroup_attach_task() might |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3418 | * have already changed current->cgroups, allowing the previously |
| 3419 | * referenced cgroup group to be removed and freed. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 3420 | * |
| 3421 | * At the point that cgroup_fork() is called, 'current' is the parent |
| 3422 | * task, and the passed argument 'child' points to the child task. |
| 3423 | */ |
| 3424 | void cgroup_fork(struct task_struct *child) |
| 3425 | { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3426 | task_lock(current); |
| 3427 | child->cgroups = current->cgroups; |
| 3428 | get_css_set(child->cgroups); |
| 3429 | task_unlock(current); |
| 3430 | INIT_LIST_HEAD(&child->cg_list); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 3431 | } |
| 3432 | |
| 3433 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3434 | * cgroup_fork_callbacks - run fork callbacks |
| 3435 | * @child: the new task |
| 3436 | * |
| 3437 | * Called on a new task very soon before adding it to the |
| 3438 | * tasklist. No need to take any locks since no-one can |
| 3439 | * be operating on this task. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 3440 | */ |
| 3441 | void cgroup_fork_callbacks(struct task_struct *child) |
| 3442 | { |
| 3443 | if (need_forkexit_callback) { |
| 3444 | int i; |
| 3445 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 3446 | struct cgroup_subsys *ss = subsys[i]; |
| 3447 | if (ss->fork) |
| 3448 | ss->fork(ss, child); |
| 3449 | } |
| 3450 | } |
| 3451 | } |
| 3452 | |
| 3453 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3454 | * cgroup_post_fork - called on a new task after adding it to the task list |
| 3455 | * @child: the task in question |
| 3456 | * |
| 3457 | * Adds the task to the list running through its css_set if necessary. |
| 3458 | * Has to be after the task is visible on the task list in case we race |
| 3459 | * with the first call to cgroup_iter_start() - to guarantee that the |
| 3460 | * new task ends up on its list. |
| 3461 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3462 | void cgroup_post_fork(struct task_struct *child) |
| 3463 | { |
| 3464 | if (use_task_css_set_links) { |
| 3465 | write_lock(&css_set_lock); |
Lai Jiangshan | b12b533 | 2009-01-07 18:07:36 -0800 | [diff] [blame] | 3466 | task_lock(child); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3467 | if (list_empty(&child->cg_list)) |
| 3468 | list_add(&child->cg_list, &child->cgroups->tasks); |
Lai Jiangshan | b12b533 | 2009-01-07 18:07:36 -0800 | [diff] [blame] | 3469 | task_unlock(child); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3470 | write_unlock(&css_set_lock); |
| 3471 | } |
| 3472 | } |
| 3473 | /** |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 3474 | * cgroup_exit - detach cgroup from exiting task |
| 3475 | * @tsk: pointer to task_struct of exiting process |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3476 | * @run_callback: run exit callbacks? |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 3477 | * |
| 3478 | * Description: Detach cgroup from @tsk and release it. |
| 3479 | * |
| 3480 | * Note that cgroups marked notify_on_release force every task in |
| 3481 | * them to take the global cgroup_mutex mutex when exiting. |
| 3482 | * This could impact scaling on very large systems. Be reluctant to |
| 3483 | * use notify_on_release cgroups where very high task exit scaling |
| 3484 | * is required on large systems. |
| 3485 | * |
| 3486 | * the_top_cgroup_hack: |
| 3487 | * |
| 3488 | * Set the exiting tasks cgroup to the root cgroup (top_cgroup). |
| 3489 | * |
| 3490 | * We call cgroup_exit() while the task is still competent to |
| 3491 | * handle notify_on_release(), then leave the task attached to the |
| 3492 | * root cgroup in each hierarchy for the remainder of its exit. |
| 3493 | * |
| 3494 | * To do this properly, we would increment the reference count on |
| 3495 | * top_cgroup, and near the very end of the kernel/exit.c do_exit() |
| 3496 | * code we would add a second cgroup function call, to drop that |
| 3497 | * reference. This would just create an unnecessary hot spot on |
| 3498 | * the top_cgroup reference count, to no avail. |
| 3499 | * |
| 3500 | * Normally, holding a reference to a cgroup without bumping its |
| 3501 | * count is unsafe. The cgroup could go away, or someone could |
| 3502 | * attach us to a different cgroup, decrementing the count on |
| 3503 | * the first cgroup that we never incremented. But in this case, |
| 3504 | * 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] | 3505 | * which wards off any cgroup_attach_task() attempts, or task is a failed |
| 3506 | * fork, never visible to cgroup_attach_task. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 3507 | */ |
| 3508 | void cgroup_exit(struct task_struct *tsk, int run_callbacks) |
| 3509 | { |
| 3510 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3511 | struct css_set *cg; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 3512 | |
| 3513 | if (run_callbacks && need_forkexit_callback) { |
| 3514 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 3515 | struct cgroup_subsys *ss = subsys[i]; |
| 3516 | if (ss->exit) |
| 3517 | ss->exit(ss, tsk); |
| 3518 | } |
| 3519 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3520 | |
| 3521 | /* |
| 3522 | * Unlink from the css_set task list if necessary. |
| 3523 | * Optimistically check cg_list before taking |
| 3524 | * css_set_lock |
| 3525 | */ |
| 3526 | if (!list_empty(&tsk->cg_list)) { |
| 3527 | write_lock(&css_set_lock); |
| 3528 | if (!list_empty(&tsk->cg_list)) |
| 3529 | list_del(&tsk->cg_list); |
| 3530 | write_unlock(&css_set_lock); |
| 3531 | } |
| 3532 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 3533 | /* Reassign the task to the init_css_set. */ |
| 3534 | task_lock(tsk); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3535 | cg = tsk->cgroups; |
| 3536 | tsk->cgroups = &init_css_set; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 3537 | task_unlock(tsk); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3538 | if (cg) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3539 | put_css_set_taskexit(cg); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 3540 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 3541 | |
| 3542 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3543 | * cgroup_clone - clone the cgroup the given subsystem is attached to |
| 3544 | * @tsk: the task to be moved |
| 3545 | * @subsys: the given subsystem |
Serge E. Hallyn | e885dcd | 2008-07-25 01:47:06 -0700 | [diff] [blame] | 3546 | * @nodename: the name for the new cgroup |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3547 | * |
| 3548 | * Duplicate the current cgroup in the hierarchy that the given |
| 3549 | * subsystem is attached to, and move this task into the new |
| 3550 | * child. |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 3551 | */ |
Serge E. Hallyn | e885dcd | 2008-07-25 01:47:06 -0700 | [diff] [blame] | 3552 | int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys, |
| 3553 | char *nodename) |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 3554 | { |
| 3555 | struct dentry *dentry; |
| 3556 | int ret = 0; |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 3557 | struct cgroup *parent, *child; |
| 3558 | struct inode *inode; |
| 3559 | struct css_set *cg; |
| 3560 | struct cgroupfs_root *root; |
| 3561 | struct cgroup_subsys *ss; |
| 3562 | |
| 3563 | /* We shouldn't be called by an unregistered subsystem */ |
| 3564 | BUG_ON(!subsys->active); |
| 3565 | |
| 3566 | /* First figure out what hierarchy and cgroup we're dealing |
| 3567 | * with, and pin them so we can drop cgroup_mutex */ |
| 3568 | mutex_lock(&cgroup_mutex); |
| 3569 | again: |
| 3570 | root = subsys->root; |
| 3571 | if (root == &rootnode) { |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 3572 | mutex_unlock(&cgroup_mutex); |
| 3573 | return 0; |
| 3574 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 3575 | |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 3576 | /* Pin the hierarchy */ |
Li Zefan | 1404f06 | 2009-01-29 14:25:21 -0800 | [diff] [blame] | 3577 | if (!atomic_inc_not_zero(&root->sb->s_active)) { |
Li Zefan | 7b574b7 | 2009-01-04 12:00:45 -0800 | [diff] [blame] | 3578 | /* We race with the final deactivate_super() */ |
| 3579 | mutex_unlock(&cgroup_mutex); |
| 3580 | return 0; |
| 3581 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 3582 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3583 | /* Keep the cgroup alive */ |
Li Zefan | 1404f06 | 2009-01-29 14:25:21 -0800 | [diff] [blame] | 3584 | task_lock(tsk); |
| 3585 | parent = task_cgroup(tsk, subsys->subsys_id); |
| 3586 | cg = tsk->cgroups; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3587 | get_css_set(cg); |
Lai Jiangshan | 104cbd5 | 2009-01-07 18:07:38 -0800 | [diff] [blame] | 3588 | task_unlock(tsk); |
Li Zefan | 1404f06 | 2009-01-29 14:25:21 -0800 | [diff] [blame] | 3589 | |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 3590 | mutex_unlock(&cgroup_mutex); |
| 3591 | |
| 3592 | /* Now do the VFS work to create a cgroup */ |
| 3593 | inode = parent->dentry->d_inode; |
| 3594 | |
| 3595 | /* Hold the parent directory mutex across this operation to |
| 3596 | * stop anyone else deleting the new cgroup */ |
| 3597 | mutex_lock(&inode->i_mutex); |
| 3598 | dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename)); |
| 3599 | if (IS_ERR(dentry)) { |
| 3600 | printk(KERN_INFO |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 3601 | "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename, |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 3602 | PTR_ERR(dentry)); |
| 3603 | ret = PTR_ERR(dentry); |
| 3604 | goto out_release; |
| 3605 | } |
| 3606 | |
| 3607 | /* Create the cgroup directory, which also creates the cgroup */ |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 3608 | ret = vfs_mkdir(inode, dentry, 0755); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3609 | child = __d_cgrp(dentry); |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 3610 | dput(dentry); |
| 3611 | if (ret) { |
| 3612 | printk(KERN_INFO |
| 3613 | "Failed to create cgroup %s: %d\n", nodename, |
| 3614 | ret); |
| 3615 | goto out_release; |
| 3616 | } |
| 3617 | |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 3618 | /* The cgroup now exists. Retake cgroup_mutex and check |
| 3619 | * that we're still in the same state that we thought we |
| 3620 | * were. */ |
| 3621 | mutex_lock(&cgroup_mutex); |
| 3622 | if ((root != subsys->root) || |
| 3623 | (parent != task_cgroup(tsk, subsys->subsys_id))) { |
| 3624 | /* Aargh, we raced ... */ |
| 3625 | mutex_unlock(&inode->i_mutex); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3626 | put_css_set(cg); |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 3627 | |
Li Zefan | 1404f06 | 2009-01-29 14:25:21 -0800 | [diff] [blame] | 3628 | deactivate_super(root->sb); |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 3629 | /* The cgroup is still accessible in the VFS, but |
| 3630 | * we're not going to try to rmdir() it at this |
| 3631 | * point. */ |
| 3632 | printk(KERN_INFO |
| 3633 | "Race in cgroup_clone() - leaking cgroup %s\n", |
| 3634 | nodename); |
| 3635 | goto again; |
| 3636 | } |
| 3637 | |
| 3638 | /* do any required auto-setup */ |
| 3639 | for_each_subsys(root, ss) { |
| 3640 | if (ss->post_clone) |
| 3641 | ss->post_clone(ss, child); |
| 3642 | } |
| 3643 | |
| 3644 | /* All seems fine. Finish by moving the task into the new cgroup */ |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 3645 | ret = cgroup_attach_task(child, tsk); |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 3646 | mutex_unlock(&cgroup_mutex); |
| 3647 | |
| 3648 | out_release: |
| 3649 | mutex_unlock(&inode->i_mutex); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3650 | |
| 3651 | mutex_lock(&cgroup_mutex); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3652 | put_css_set(cg); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3653 | mutex_unlock(&cgroup_mutex); |
Li Zefan | 1404f06 | 2009-01-29 14:25:21 -0800 | [diff] [blame] | 3654 | deactivate_super(root->sb); |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 3655 | return ret; |
| 3656 | } |
| 3657 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3658 | /** |
Grzegorz Nosek | 313e924 | 2009-04-02 16:57:23 -0700 | [diff] [blame] | 3659 | * 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] | 3660 | * @cgrp: the cgroup in question |
Grzegorz Nosek | 313e924 | 2009-04-02 16:57:23 -0700 | [diff] [blame] | 3661 | * @task: the task in question |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3662 | * |
Grzegorz Nosek | 313e924 | 2009-04-02 16:57:23 -0700 | [diff] [blame] | 3663 | * See if @cgrp is a descendant of @task's cgroup in the appropriate |
| 3664 | * hierarchy. |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 3665 | * |
| 3666 | * If we are sending in dummytop, then presumably we are creating |
| 3667 | * the top cgroup in the subsystem. |
| 3668 | * |
| 3669 | * Called only by the ns (nsproxy) cgroup. |
| 3670 | */ |
Grzegorz Nosek | 313e924 | 2009-04-02 16:57:23 -0700 | [diff] [blame] | 3671 | int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task) |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 3672 | { |
| 3673 | int ret; |
| 3674 | struct cgroup *target; |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 3675 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3676 | if (cgrp == dummytop) |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 3677 | return 1; |
| 3678 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 3679 | target = task_cgroup_from_root(task, cgrp->root); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3680 | while (cgrp != target && cgrp!= cgrp->top_cgroup) |
| 3681 | cgrp = cgrp->parent; |
| 3682 | ret = (cgrp == target); |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 3683 | return ret; |
| 3684 | } |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3685 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3686 | static void check_for_release(struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3687 | { |
| 3688 | /* All of these checks rely on RCU to keep the cgroup |
| 3689 | * structure alive */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3690 | if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count) |
| 3691 | && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3692 | /* Control Group is currently removeable. If it's not |
| 3693 | * already queued for a userspace notification, queue |
| 3694 | * it now */ |
| 3695 | int need_schedule_work = 0; |
| 3696 | spin_lock(&release_list_lock); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3697 | if (!cgroup_is_removed(cgrp) && |
| 3698 | list_empty(&cgrp->release_list)) { |
| 3699 | list_add(&cgrp->release_list, &release_list); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3700 | need_schedule_work = 1; |
| 3701 | } |
| 3702 | spin_unlock(&release_list_lock); |
| 3703 | if (need_schedule_work) |
| 3704 | schedule_work(&release_agent_work); |
| 3705 | } |
| 3706 | } |
| 3707 | |
| 3708 | void __css_put(struct cgroup_subsys_state *css) |
| 3709 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3710 | struct cgroup *cgrp = css->cgroup; |
KAMEZAWA Hiroyuki | 3dece83 | 2009-10-01 15:44:09 -0700 | [diff] [blame^] | 3711 | int val; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3712 | rcu_read_lock(); |
KAMEZAWA Hiroyuki | 3dece83 | 2009-10-01 15:44:09 -0700 | [diff] [blame^] | 3713 | val = atomic_dec_return(&css->refcnt); |
| 3714 | if (val == 1) { |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 3715 | if (notify_on_release(cgrp)) { |
| 3716 | set_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 3717 | check_for_release(cgrp); |
| 3718 | } |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 3719 | cgroup_wakeup_rmdir_waiter(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3720 | } |
| 3721 | rcu_read_unlock(); |
KAMEZAWA Hiroyuki | 3dece83 | 2009-10-01 15:44:09 -0700 | [diff] [blame^] | 3722 | WARN_ON_ONCE(val < 1); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3723 | } |
| 3724 | |
| 3725 | /* |
| 3726 | * Notify userspace when a cgroup is released, by running the |
| 3727 | * configured release agent with the name of the cgroup (path |
| 3728 | * relative to the root of cgroup file system) as the argument. |
| 3729 | * |
| 3730 | * Most likely, this user command will try to rmdir this cgroup. |
| 3731 | * |
| 3732 | * This races with the possibility that some other task will be |
| 3733 | * attached to this cgroup before it is removed, or that some other |
| 3734 | * user task will 'mkdir' a child cgroup of this cgroup. That's ok. |
| 3735 | * The presumed 'rmdir' will fail quietly if this cgroup is no longer |
| 3736 | * unused, and this cgroup will be reprieved from its death sentence, |
| 3737 | * to continue to serve a useful existence. Next time it's released, |
| 3738 | * we will get notified again, if it still has 'notify_on_release' set. |
| 3739 | * |
| 3740 | * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which |
| 3741 | * means only wait until the task is successfully execve()'d. The |
| 3742 | * separate release agent task is forked by call_usermodehelper(), |
| 3743 | * then control in this thread returns here, without waiting for the |
| 3744 | * release agent task. We don't bother to wait because the caller of |
| 3745 | * this routine has no use for the exit status of the release agent |
| 3746 | * task, so no sense holding our caller up for that. |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3747 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3748 | static void cgroup_release_agent(struct work_struct *work) |
| 3749 | { |
| 3750 | BUG_ON(work != &release_agent_work); |
| 3751 | mutex_lock(&cgroup_mutex); |
| 3752 | spin_lock(&release_list_lock); |
| 3753 | while (!list_empty(&release_list)) { |
| 3754 | char *argv[3], *envp[3]; |
| 3755 | int i; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 3756 | char *pathbuf = NULL, *agentbuf = NULL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3757 | struct cgroup *cgrp = list_entry(release_list.next, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3758 | struct cgroup, |
| 3759 | release_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3760 | list_del_init(&cgrp->release_list); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3761 | spin_unlock(&release_list_lock); |
| 3762 | pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 3763 | if (!pathbuf) |
| 3764 | goto continue_free; |
| 3765 | if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0) |
| 3766 | goto continue_free; |
| 3767 | agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL); |
| 3768 | if (!agentbuf) |
| 3769 | goto continue_free; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3770 | |
| 3771 | i = 0; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 3772 | argv[i++] = agentbuf; |
| 3773 | argv[i++] = pathbuf; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3774 | argv[i] = NULL; |
| 3775 | |
| 3776 | i = 0; |
| 3777 | /* minimal command environment */ |
| 3778 | envp[i++] = "HOME=/"; |
| 3779 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
| 3780 | envp[i] = NULL; |
| 3781 | |
| 3782 | /* Drop the lock while we invoke the usermode helper, |
| 3783 | * since the exec could involve hitting disk and hence |
| 3784 | * be a slow process */ |
| 3785 | mutex_unlock(&cgroup_mutex); |
| 3786 | call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3787 | mutex_lock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 3788 | continue_free: |
| 3789 | kfree(pathbuf); |
| 3790 | kfree(agentbuf); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3791 | spin_lock(&release_list_lock); |
| 3792 | } |
| 3793 | spin_unlock(&release_list_lock); |
| 3794 | mutex_unlock(&cgroup_mutex); |
| 3795 | } |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 3796 | |
| 3797 | static int __init cgroup_disable(char *str) |
| 3798 | { |
| 3799 | int i; |
| 3800 | char *token; |
| 3801 | |
| 3802 | while ((token = strsep(&str, ",")) != NULL) { |
| 3803 | if (!*token) |
| 3804 | continue; |
| 3805 | |
| 3806 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 3807 | struct cgroup_subsys *ss = subsys[i]; |
| 3808 | |
| 3809 | if (!strcmp(token, ss->name)) { |
| 3810 | ss->disabled = 1; |
| 3811 | printk(KERN_INFO "Disabling %s control group" |
| 3812 | " subsystem\n", ss->name); |
| 3813 | break; |
| 3814 | } |
| 3815 | } |
| 3816 | } |
| 3817 | return 1; |
| 3818 | } |
| 3819 | __setup("cgroup_disable=", cgroup_disable); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 3820 | |
| 3821 | /* |
| 3822 | * Functons for CSS ID. |
| 3823 | */ |
| 3824 | |
| 3825 | /* |
| 3826 | *To get ID other than 0, this should be called when !cgroup_is_removed(). |
| 3827 | */ |
| 3828 | unsigned short css_id(struct cgroup_subsys_state *css) |
| 3829 | { |
| 3830 | struct css_id *cssid = rcu_dereference(css->id); |
| 3831 | |
| 3832 | if (cssid) |
| 3833 | return cssid->id; |
| 3834 | return 0; |
| 3835 | } |
| 3836 | |
| 3837 | unsigned short css_depth(struct cgroup_subsys_state *css) |
| 3838 | { |
| 3839 | struct css_id *cssid = rcu_dereference(css->id); |
| 3840 | |
| 3841 | if (cssid) |
| 3842 | return cssid->depth; |
| 3843 | return 0; |
| 3844 | } |
| 3845 | |
| 3846 | bool css_is_ancestor(struct cgroup_subsys_state *child, |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 3847 | const struct cgroup_subsys_state *root) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 3848 | { |
| 3849 | struct css_id *child_id = rcu_dereference(child->id); |
| 3850 | struct css_id *root_id = rcu_dereference(root->id); |
| 3851 | |
| 3852 | if (!child_id || !root_id || (child_id->depth < root_id->depth)) |
| 3853 | return false; |
| 3854 | return child_id->stack[root_id->depth] == root_id->id; |
| 3855 | } |
| 3856 | |
| 3857 | static void __free_css_id_cb(struct rcu_head *head) |
| 3858 | { |
| 3859 | struct css_id *id; |
| 3860 | |
| 3861 | id = container_of(head, struct css_id, rcu_head); |
| 3862 | kfree(id); |
| 3863 | } |
| 3864 | |
| 3865 | void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css) |
| 3866 | { |
| 3867 | struct css_id *id = css->id; |
| 3868 | /* When this is called before css_id initialization, id can be NULL */ |
| 3869 | if (!id) |
| 3870 | return; |
| 3871 | |
| 3872 | BUG_ON(!ss->use_id); |
| 3873 | |
| 3874 | rcu_assign_pointer(id->css, NULL); |
| 3875 | rcu_assign_pointer(css->id, NULL); |
| 3876 | spin_lock(&ss->id_lock); |
| 3877 | idr_remove(&ss->idr, id->id); |
| 3878 | spin_unlock(&ss->id_lock); |
| 3879 | call_rcu(&id->rcu_head, __free_css_id_cb); |
| 3880 | } |
| 3881 | |
| 3882 | /* |
| 3883 | * This is called by init or create(). Then, calls to this function are |
| 3884 | * always serialized (By cgroup_mutex() at create()). |
| 3885 | */ |
| 3886 | |
| 3887 | static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth) |
| 3888 | { |
| 3889 | struct css_id *newid; |
| 3890 | int myid, error, size; |
| 3891 | |
| 3892 | BUG_ON(!ss->use_id); |
| 3893 | |
| 3894 | size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1); |
| 3895 | newid = kzalloc(size, GFP_KERNEL); |
| 3896 | if (!newid) |
| 3897 | return ERR_PTR(-ENOMEM); |
| 3898 | /* get id */ |
| 3899 | if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) { |
| 3900 | error = -ENOMEM; |
| 3901 | goto err_out; |
| 3902 | } |
| 3903 | spin_lock(&ss->id_lock); |
| 3904 | /* Don't use 0. allocates an ID of 1-65535 */ |
| 3905 | error = idr_get_new_above(&ss->idr, newid, 1, &myid); |
| 3906 | spin_unlock(&ss->id_lock); |
| 3907 | |
| 3908 | /* Returns error when there are no free spaces for new ID.*/ |
| 3909 | if (error) { |
| 3910 | error = -ENOSPC; |
| 3911 | goto err_out; |
| 3912 | } |
| 3913 | if (myid > CSS_ID_MAX) |
| 3914 | goto remove_idr; |
| 3915 | |
| 3916 | newid->id = myid; |
| 3917 | newid->depth = depth; |
| 3918 | return newid; |
| 3919 | remove_idr: |
| 3920 | error = -ENOSPC; |
| 3921 | spin_lock(&ss->id_lock); |
| 3922 | idr_remove(&ss->idr, myid); |
| 3923 | spin_unlock(&ss->id_lock); |
| 3924 | err_out: |
| 3925 | kfree(newid); |
| 3926 | return ERR_PTR(error); |
| 3927 | |
| 3928 | } |
| 3929 | |
| 3930 | static int __init cgroup_subsys_init_idr(struct cgroup_subsys *ss) |
| 3931 | { |
| 3932 | struct css_id *newid; |
| 3933 | struct cgroup_subsys_state *rootcss; |
| 3934 | |
| 3935 | spin_lock_init(&ss->id_lock); |
| 3936 | idr_init(&ss->idr); |
| 3937 | |
| 3938 | rootcss = init_css_set.subsys[ss->subsys_id]; |
| 3939 | newid = get_new_cssid(ss, 0); |
| 3940 | if (IS_ERR(newid)) |
| 3941 | return PTR_ERR(newid); |
| 3942 | |
| 3943 | newid->stack[0] = newid->id; |
| 3944 | newid->css = rootcss; |
| 3945 | rootcss->id = newid; |
| 3946 | return 0; |
| 3947 | } |
| 3948 | |
| 3949 | static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent, |
| 3950 | struct cgroup *child) |
| 3951 | { |
| 3952 | int subsys_id, i, depth = 0; |
| 3953 | struct cgroup_subsys_state *parent_css, *child_css; |
| 3954 | struct css_id *child_id, *parent_id = NULL; |
| 3955 | |
| 3956 | subsys_id = ss->subsys_id; |
| 3957 | parent_css = parent->subsys[subsys_id]; |
| 3958 | child_css = child->subsys[subsys_id]; |
| 3959 | depth = css_depth(parent_css) + 1; |
| 3960 | parent_id = parent_css->id; |
| 3961 | |
| 3962 | child_id = get_new_cssid(ss, depth); |
| 3963 | if (IS_ERR(child_id)) |
| 3964 | return PTR_ERR(child_id); |
| 3965 | |
| 3966 | for (i = 0; i < depth; i++) |
| 3967 | child_id->stack[i] = parent_id->stack[i]; |
| 3968 | child_id->stack[depth] = child_id->id; |
| 3969 | /* |
| 3970 | * child_id->css pointer will be set after this cgroup is available |
| 3971 | * see cgroup_populate_dir() |
| 3972 | */ |
| 3973 | rcu_assign_pointer(child_css->id, child_id); |
| 3974 | |
| 3975 | return 0; |
| 3976 | } |
| 3977 | |
| 3978 | /** |
| 3979 | * css_lookup - lookup css by id |
| 3980 | * @ss: cgroup subsys to be looked into. |
| 3981 | * @id: the id |
| 3982 | * |
| 3983 | * Returns pointer to cgroup_subsys_state if there is valid one with id. |
| 3984 | * NULL if not. Should be called under rcu_read_lock() |
| 3985 | */ |
| 3986 | struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id) |
| 3987 | { |
| 3988 | struct css_id *cssid = NULL; |
| 3989 | |
| 3990 | BUG_ON(!ss->use_id); |
| 3991 | cssid = idr_find(&ss->idr, id); |
| 3992 | |
| 3993 | if (unlikely(!cssid)) |
| 3994 | return NULL; |
| 3995 | |
| 3996 | return rcu_dereference(cssid->css); |
| 3997 | } |
| 3998 | |
| 3999 | /** |
| 4000 | * css_get_next - lookup next cgroup under specified hierarchy. |
| 4001 | * @ss: pointer to subsystem |
| 4002 | * @id: current position of iteration. |
| 4003 | * @root: pointer to css. search tree under this. |
| 4004 | * @foundid: position of found object. |
| 4005 | * |
| 4006 | * Search next css under the specified hierarchy of rootid. Calling under |
| 4007 | * rcu_read_lock() is necessary. Returns NULL if it reaches the end. |
| 4008 | */ |
| 4009 | struct cgroup_subsys_state * |
| 4010 | css_get_next(struct cgroup_subsys *ss, int id, |
| 4011 | struct cgroup_subsys_state *root, int *foundid) |
| 4012 | { |
| 4013 | struct cgroup_subsys_state *ret = NULL; |
| 4014 | struct css_id *tmp; |
| 4015 | int tmpid; |
| 4016 | int rootid = css_id(root); |
| 4017 | int depth = css_depth(root); |
| 4018 | |
| 4019 | if (!rootid) |
| 4020 | return NULL; |
| 4021 | |
| 4022 | BUG_ON(!ss->use_id); |
| 4023 | /* fill start point for scan */ |
| 4024 | tmpid = id; |
| 4025 | while (1) { |
| 4026 | /* |
| 4027 | * scan next entry from bitmap(tree), tmpid is updated after |
| 4028 | * idr_get_next(). |
| 4029 | */ |
| 4030 | spin_lock(&ss->id_lock); |
| 4031 | tmp = idr_get_next(&ss->idr, &tmpid); |
| 4032 | spin_unlock(&ss->id_lock); |
| 4033 | |
| 4034 | if (!tmp) |
| 4035 | break; |
| 4036 | if (tmp->depth >= depth && tmp->stack[depth] == rootid) { |
| 4037 | ret = rcu_dereference(tmp->css); |
| 4038 | if (ret) { |
| 4039 | *foundid = tmpid; |
| 4040 | break; |
| 4041 | } |
| 4042 | } |
| 4043 | /* continue to scan from next id */ |
| 4044 | tmpid = tmpid + 1; |
| 4045 | } |
| 4046 | return ret; |
| 4047 | } |
| 4048 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4049 | #ifdef CONFIG_CGROUP_DEBUG |
| 4050 | static struct cgroup_subsys_state *debug_create(struct cgroup_subsys *ss, |
| 4051 | struct cgroup *cont) |
| 4052 | { |
| 4053 | struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL); |
| 4054 | |
| 4055 | if (!css) |
| 4056 | return ERR_PTR(-ENOMEM); |
| 4057 | |
| 4058 | return css; |
| 4059 | } |
| 4060 | |
| 4061 | static void debug_destroy(struct cgroup_subsys *ss, struct cgroup *cont) |
| 4062 | { |
| 4063 | kfree(cont->subsys[debug_subsys_id]); |
| 4064 | } |
| 4065 | |
| 4066 | static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft) |
| 4067 | { |
| 4068 | return atomic_read(&cont->count); |
| 4069 | } |
| 4070 | |
| 4071 | static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft) |
| 4072 | { |
| 4073 | return cgroup_task_count(cont); |
| 4074 | } |
| 4075 | |
| 4076 | static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft) |
| 4077 | { |
| 4078 | return (u64)(unsigned long)current->cgroups; |
| 4079 | } |
| 4080 | |
| 4081 | static u64 current_css_set_refcount_read(struct cgroup *cont, |
| 4082 | struct cftype *cft) |
| 4083 | { |
| 4084 | u64 count; |
| 4085 | |
| 4086 | rcu_read_lock(); |
| 4087 | count = atomic_read(¤t->cgroups->refcount); |
| 4088 | rcu_read_unlock(); |
| 4089 | return count; |
| 4090 | } |
| 4091 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4092 | static int current_css_set_cg_links_read(struct cgroup *cont, |
| 4093 | struct cftype *cft, |
| 4094 | struct seq_file *seq) |
| 4095 | { |
| 4096 | struct cg_cgroup_link *link; |
| 4097 | struct css_set *cg; |
| 4098 | |
| 4099 | read_lock(&css_set_lock); |
| 4100 | rcu_read_lock(); |
| 4101 | cg = rcu_dereference(current->cgroups); |
| 4102 | list_for_each_entry(link, &cg->cg_links, cg_link_list) { |
| 4103 | struct cgroup *c = link->cgrp; |
| 4104 | const char *name; |
| 4105 | |
| 4106 | if (c->dentry) |
| 4107 | name = c->dentry->d_name.name; |
| 4108 | else |
| 4109 | name = "?"; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4110 | seq_printf(seq, "Root %d group %s\n", |
| 4111 | c->root->hierarchy_id, name); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4112 | } |
| 4113 | rcu_read_unlock(); |
| 4114 | read_unlock(&css_set_lock); |
| 4115 | return 0; |
| 4116 | } |
| 4117 | |
| 4118 | #define MAX_TASKS_SHOWN_PER_CSS 25 |
| 4119 | static int cgroup_css_links_read(struct cgroup *cont, |
| 4120 | struct cftype *cft, |
| 4121 | struct seq_file *seq) |
| 4122 | { |
| 4123 | struct cg_cgroup_link *link; |
| 4124 | |
| 4125 | read_lock(&css_set_lock); |
| 4126 | list_for_each_entry(link, &cont->css_sets, cgrp_link_list) { |
| 4127 | struct css_set *cg = link->cg; |
| 4128 | struct task_struct *task; |
| 4129 | int count = 0; |
| 4130 | seq_printf(seq, "css_set %p\n", cg); |
| 4131 | list_for_each_entry(task, &cg->tasks, cg_list) { |
| 4132 | if (count++ > MAX_TASKS_SHOWN_PER_CSS) { |
| 4133 | seq_puts(seq, " ...\n"); |
| 4134 | break; |
| 4135 | } else { |
| 4136 | seq_printf(seq, " task %d\n", |
| 4137 | task_pid_vnr(task)); |
| 4138 | } |
| 4139 | } |
| 4140 | } |
| 4141 | read_unlock(&css_set_lock); |
| 4142 | return 0; |
| 4143 | } |
| 4144 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4145 | static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft) |
| 4146 | { |
| 4147 | return test_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 4148 | } |
| 4149 | |
| 4150 | static struct cftype debug_files[] = { |
| 4151 | { |
| 4152 | .name = "cgroup_refcount", |
| 4153 | .read_u64 = cgroup_refcount_read, |
| 4154 | }, |
| 4155 | { |
| 4156 | .name = "taskcount", |
| 4157 | .read_u64 = debug_taskcount_read, |
| 4158 | }, |
| 4159 | |
| 4160 | { |
| 4161 | .name = "current_css_set", |
| 4162 | .read_u64 = current_css_set_read, |
| 4163 | }, |
| 4164 | |
| 4165 | { |
| 4166 | .name = "current_css_set_refcount", |
| 4167 | .read_u64 = current_css_set_refcount_read, |
| 4168 | }, |
| 4169 | |
| 4170 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4171 | .name = "current_css_set_cg_links", |
| 4172 | .read_seq_string = current_css_set_cg_links_read, |
| 4173 | }, |
| 4174 | |
| 4175 | { |
| 4176 | .name = "cgroup_css_links", |
| 4177 | .read_seq_string = cgroup_css_links_read, |
| 4178 | }, |
| 4179 | |
| 4180 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4181 | .name = "releasable", |
| 4182 | .read_u64 = releasable_read, |
| 4183 | }, |
| 4184 | }; |
| 4185 | |
| 4186 | static int debug_populate(struct cgroup_subsys *ss, struct cgroup *cont) |
| 4187 | { |
| 4188 | return cgroup_add_files(cont, ss, debug_files, |
| 4189 | ARRAY_SIZE(debug_files)); |
| 4190 | } |
| 4191 | |
| 4192 | struct cgroup_subsys debug_subsys = { |
| 4193 | .name = "debug", |
| 4194 | .create = debug_create, |
| 4195 | .destroy = debug_destroy, |
| 4196 | .populate = debug_populate, |
| 4197 | .subsys_id = debug_subsys_id, |
| 4198 | }; |
| 4199 | #endif /* CONFIG_CGROUP_DEBUG */ |