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