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> |
| 26 | #include <linux/errno.h> |
| 27 | #include <linux/fs.h> |
| 28 | #include <linux/kernel.h> |
| 29 | #include <linux/list.h> |
| 30 | #include <linux/mm.h> |
| 31 | #include <linux/mutex.h> |
| 32 | #include <linux/mount.h> |
| 33 | #include <linux/pagemap.h> |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 34 | #include <linux/proc_fs.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 35 | #include <linux/rcupdate.h> |
| 36 | #include <linux/sched.h> |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 37 | #include <linux/backing-dev.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 38 | #include <linux/seq_file.h> |
| 39 | #include <linux/slab.h> |
| 40 | #include <linux/magic.h> |
| 41 | #include <linux/spinlock.h> |
| 42 | #include <linux/string.h> |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 43 | #include <linux/sort.h> |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 44 | #include <linux/kmod.h> |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 45 | #include <linux/delayacct.h> |
| 46 | #include <linux/cgroupstats.h> |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 47 | #include <linux/hash.h> |
Al Viro | 3f8206d | 2008-07-26 03:46:43 -0400 | [diff] [blame] | 48 | #include <linux/namei.h> |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 49 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 50 | #include <asm/atomic.h> |
| 51 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 52 | static DEFINE_MUTEX(cgroup_mutex); |
| 53 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 54 | /* Generate an array of cgroup subsystem pointers */ |
| 55 | #define SUBSYS(_x) &_x ## _subsys, |
| 56 | |
| 57 | static struct cgroup_subsys *subsys[] = { |
| 58 | #include <linux/cgroup_subsys.h> |
| 59 | }; |
| 60 | |
| 61 | /* |
| 62 | * A cgroupfs_root represents the root of a cgroup hierarchy, |
| 63 | * and may be associated with a superblock to form an active |
| 64 | * hierarchy |
| 65 | */ |
| 66 | struct cgroupfs_root { |
| 67 | struct super_block *sb; |
| 68 | |
| 69 | /* |
| 70 | * The bitmask of subsystems intended to be attached to this |
| 71 | * hierarchy |
| 72 | */ |
| 73 | unsigned long subsys_bits; |
| 74 | |
| 75 | /* The bitmask of subsystems currently attached to this hierarchy */ |
| 76 | unsigned long actual_subsys_bits; |
| 77 | |
| 78 | /* A list running through the attached subsystems */ |
| 79 | struct list_head subsys_list; |
| 80 | |
| 81 | /* The root cgroup for this hierarchy */ |
| 82 | struct cgroup top_cgroup; |
| 83 | |
| 84 | /* Tracks how many cgroups are currently defined in hierarchy.*/ |
| 85 | int number_of_cgroups; |
| 86 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame^] | 87 | /* A list running through the active hierarchies */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 88 | struct list_head root_list; |
| 89 | |
| 90 | /* Hierarchy-specific flags */ |
| 91 | unsigned long flags; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 92 | |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 93 | /* The path to use for release notifications. */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 94 | char release_agent_path[PATH_MAX]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | |
| 98 | /* |
| 99 | * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the |
| 100 | * subsystems that are otherwise unattached - it never has more than a |
| 101 | * single cgroup, and all tasks are part of that cgroup. |
| 102 | */ |
| 103 | static struct cgroupfs_root rootnode; |
| 104 | |
| 105 | /* The list of hierarchy roots */ |
| 106 | |
| 107 | static LIST_HEAD(roots); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 108 | static int root_count; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 109 | |
| 110 | /* dummytop is a shorthand for the dummy hierarchy's top cgroup */ |
| 111 | #define dummytop (&rootnode.top_cgroup) |
| 112 | |
| 113 | /* This flag indicates whether tasks in the fork and exit paths should |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 114 | * check for fork/exit handlers to call. This avoids us having to do |
| 115 | * extra work in the fork/exit path if none of the subsystems need to |
| 116 | * be called. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 117 | */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 118 | static int need_forkexit_callback __read_mostly; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 119 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 120 | /* convenient tests for these bits */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 121 | inline int cgroup_is_removed(const struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 122 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 123 | return test_bit(CGRP_REMOVED, &cgrp->flags); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /* bits in struct cgroupfs_root flags field */ |
| 127 | enum { |
| 128 | ROOT_NOPREFIX, /* mounted subsystems have no named prefix */ |
| 129 | }; |
| 130 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 131 | static int cgroup_is_releasable(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 132 | { |
| 133 | const int bits = |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 134 | (1 << CGRP_RELEASABLE) | |
| 135 | (1 << CGRP_NOTIFY_ON_RELEASE); |
| 136 | return (cgrp->flags & bits) == bits; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 139 | static int notify_on_release(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 140 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 141 | return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 144 | /* |
| 145 | * for_each_subsys() allows you to iterate on each subsystem attached to |
| 146 | * an active hierarchy |
| 147 | */ |
| 148 | #define for_each_subsys(_root, _ss) \ |
| 149 | list_for_each_entry(_ss, &_root->subsys_list, sibling) |
| 150 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame^] | 151 | /* for_each_active_root() allows you to iterate across the active hierarchies */ |
| 152 | #define for_each_active_root(_root) \ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 153 | list_for_each_entry(_root, &roots, root_list) |
| 154 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 155 | /* the list of cgroups eligible for automatic release. Protected by |
| 156 | * release_list_lock */ |
| 157 | static LIST_HEAD(release_list); |
| 158 | static DEFINE_SPINLOCK(release_list_lock); |
| 159 | static void cgroup_release_agent(struct work_struct *work); |
| 160 | static DECLARE_WORK(release_agent_work, cgroup_release_agent); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 161 | static void check_for_release(struct cgroup *cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 162 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 163 | /* Link structure for associating css_set objects with cgroups */ |
| 164 | struct cg_cgroup_link { |
| 165 | /* |
| 166 | * List running through cg_cgroup_links associated with a |
| 167 | * cgroup, anchored on cgroup->css_sets |
| 168 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 169 | struct list_head cgrp_link_list; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 170 | /* |
| 171 | * List running through cg_cgroup_links pointing at a |
| 172 | * single css_set object, anchored on css_set->cg_links |
| 173 | */ |
| 174 | struct list_head cg_link_list; |
| 175 | struct css_set *cg; |
| 176 | }; |
| 177 | |
| 178 | /* The default css_set - used by init and its children prior to any |
| 179 | * hierarchies being mounted. It contains a pointer to the root state |
| 180 | * for each subsystem. Also used to anchor the list of css_sets. Not |
| 181 | * reference-counted, to improve performance when child cgroups |
| 182 | * haven't been created. |
| 183 | */ |
| 184 | |
| 185 | static struct css_set init_css_set; |
| 186 | static struct cg_cgroup_link init_css_set_link; |
| 187 | |
| 188 | /* css_set_lock protects the list of css_set objects, and the |
| 189 | * chain of tasks off each css_set. Nests outside task->alloc_lock |
| 190 | * due to cgroup_iter_start() */ |
| 191 | static DEFINE_RWLOCK(css_set_lock); |
| 192 | static int css_set_count; |
| 193 | |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 194 | /* hash table for cgroup groups. This improves the performance to |
| 195 | * find an existing css_set */ |
| 196 | #define CSS_SET_HASH_BITS 7 |
| 197 | #define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS) |
| 198 | static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE]; |
| 199 | |
| 200 | static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[]) |
| 201 | { |
| 202 | int i; |
| 203 | int index; |
| 204 | unsigned long tmp = 0UL; |
| 205 | |
| 206 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) |
| 207 | tmp += (unsigned long)css[i]; |
| 208 | tmp = (tmp >> 16) ^ tmp; |
| 209 | |
| 210 | index = hash_long(tmp, CSS_SET_HASH_BITS); |
| 211 | |
| 212 | return &css_set_table[index]; |
| 213 | } |
| 214 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 215 | /* We don't maintain the lists running through each css_set to its |
| 216 | * task until after the first call to cgroup_iter_start(). This |
| 217 | * reduces the fork()/exit() overhead for people who have cgroups |
| 218 | * compiled into their kernel but not actually in use */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 219 | static int use_task_css_set_links __read_mostly; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 220 | |
| 221 | /* When we create or destroy a css_set, the operation simply |
| 222 | * takes/releases a reference count on all the cgroups referenced |
| 223 | * by subsystems in this css_set. This can end up multiple-counting |
| 224 | * some cgroups, but that's OK - the ref-count is just a |
| 225 | * busy/not-busy indicator; ensuring that we only count each cgroup |
| 226 | * once would require taking a global lock to ensure that no |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 227 | * subsystems moved between hierarchies while we were doing so. |
| 228 | * |
| 229 | * Possible TODO: decide at boot time based on the number of |
| 230 | * registered subsystems and the number of CPUs or NUMA nodes whether |
| 231 | * it's better for performance to ref-count every subsystem, or to |
| 232 | * take a global lock and only add one ref count to each hierarchy. |
| 233 | */ |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 234 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 235 | /* |
| 236 | * unlink a css_set from the list and free it |
| 237 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 238 | static void unlink_css_set(struct css_set *cg) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 239 | { |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 240 | struct cg_cgroup_link *link; |
| 241 | struct cg_cgroup_link *saved_link; |
| 242 | |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 243 | hlist_del(&cg->hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 244 | css_set_count--; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 245 | |
| 246 | list_for_each_entry_safe(link, saved_link, &cg->cg_links, |
| 247 | cg_link_list) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 248 | list_del(&link->cg_link_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 249 | list_del(&link->cgrp_link_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 250 | kfree(link); |
| 251 | } |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 254 | static void __put_css_set(struct css_set *cg, int taskexit) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 255 | { |
| 256 | int i; |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 257 | /* |
| 258 | * Ensure that the refcount doesn't hit zero while any readers |
| 259 | * can see it. Similar to atomic_dec_and_lock(), but for an |
| 260 | * rwlock |
| 261 | */ |
| 262 | if (atomic_add_unless(&cg->refcount, -1, 1)) |
| 263 | return; |
| 264 | write_lock(&css_set_lock); |
| 265 | if (!atomic_dec_and_test(&cg->refcount)) { |
| 266 | write_unlock(&css_set_lock); |
| 267 | return; |
| 268 | } |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 269 | unlink_css_set(cg); |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 270 | write_unlock(&css_set_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 271 | |
| 272 | rcu_read_lock(); |
| 273 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 274 | struct cgroup *cgrp = cg->subsys[i]->cgroup; |
| 275 | if (atomic_dec_and_test(&cgrp->count) && |
| 276 | notify_on_release(cgrp)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 277 | if (taskexit) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 278 | set_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 279 | check_for_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | rcu_read_unlock(); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 283 | kfree(cg); |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | * refcounted get/put for css_set objects |
| 288 | */ |
| 289 | static inline void get_css_set(struct css_set *cg) |
| 290 | { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 291 | atomic_inc(&cg->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | static inline void put_css_set(struct css_set *cg) |
| 295 | { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 296 | __put_css_set(cg, 0); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 299 | static inline void put_css_set_taskexit(struct css_set *cg) |
| 300 | { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 301 | __put_css_set(cg, 1); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 304 | /* |
| 305 | * find_existing_css_set() is a helper for |
| 306 | * find_css_set(), and checks to see whether an existing |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 307 | * css_set is suitable. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 308 | * |
| 309 | * oldcg: the cgroup group that we're using before the cgroup |
| 310 | * transition |
| 311 | * |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 312 | * cgrp: the cgroup that we're moving into |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 313 | * |
| 314 | * template: location in which to build the desired set of subsystem |
| 315 | * state objects for the new cgroup group |
| 316 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 317 | static struct css_set *find_existing_css_set( |
| 318 | struct css_set *oldcg, |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 319 | struct cgroup *cgrp, |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 320 | struct cgroup_subsys_state *template[]) |
| 321 | { |
| 322 | int i; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 323 | struct cgroupfs_root *root = cgrp->root; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 324 | struct hlist_head *hhead; |
| 325 | struct hlist_node *node; |
| 326 | struct css_set *cg; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 327 | |
| 328 | /* Built the set of subsystem state objects that we want to |
| 329 | * see in the new css_set */ |
| 330 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Li Zefan | 8d53d55 | 2008-02-23 15:24:11 -0800 | [diff] [blame] | 331 | if (root->subsys_bits & (1UL << i)) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 332 | /* Subsystem is in this hierarchy. So we want |
| 333 | * the subsystem state from the new |
| 334 | * cgroup */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 335 | template[i] = cgrp->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 336 | } else { |
| 337 | /* Subsystem is not in this hierarchy, so we |
| 338 | * don't want to change the subsystem state */ |
| 339 | template[i] = oldcg->subsys[i]; |
| 340 | } |
| 341 | } |
| 342 | |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 343 | hhead = css_set_hash(template); |
| 344 | hlist_for_each_entry(cg, node, hhead, hlist) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 345 | if (!memcmp(template, cg->subsys, sizeof(cg->subsys))) { |
| 346 | /* All subsystems matched */ |
| 347 | return cg; |
| 348 | } |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 349 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 350 | |
| 351 | /* No existing cgroup group matched */ |
| 352 | return NULL; |
| 353 | } |
| 354 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 355 | static void free_cg_links(struct list_head *tmp) |
| 356 | { |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 357 | struct cg_cgroup_link *link; |
| 358 | struct cg_cgroup_link *saved_link; |
| 359 | |
| 360 | list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 361 | list_del(&link->cgrp_link_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 362 | kfree(link); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | /* |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 367 | * allocate_cg_links() allocates "count" cg_cgroup_link structures |
| 368 | * and chains them on tmp through their cgrp_link_list fields. Returns 0 on |
| 369 | * success or a negative error |
| 370 | */ |
| 371 | static int allocate_cg_links(int count, struct list_head *tmp) |
| 372 | { |
| 373 | struct cg_cgroup_link *link; |
| 374 | int i; |
| 375 | INIT_LIST_HEAD(tmp); |
| 376 | for (i = 0; i < count; i++) { |
| 377 | link = kmalloc(sizeof(*link), GFP_KERNEL); |
| 378 | if (!link) { |
| 379 | free_cg_links(tmp); |
| 380 | return -ENOMEM; |
| 381 | } |
| 382 | list_add(&link->cgrp_link_list, tmp); |
| 383 | } |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | /* |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 388 | * find_css_set() takes an existing cgroup group and a |
| 389 | * cgroup object, and returns a css_set object that's |
| 390 | * equivalent to the old group, but with the given cgroup |
| 391 | * substituted into the appropriate hierarchy. Must be called with |
| 392 | * cgroup_mutex held |
| 393 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 394 | static struct css_set *find_css_set( |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 395 | struct css_set *oldcg, struct cgroup *cgrp) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 396 | { |
| 397 | struct css_set *res; |
| 398 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT]; |
| 399 | int i; |
| 400 | |
| 401 | struct list_head tmp_cg_links; |
| 402 | struct cg_cgroup_link *link; |
| 403 | |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 404 | struct hlist_head *hhead; |
| 405 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 406 | /* First see if we already have a cgroup group that matches |
| 407 | * the desired set */ |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 408 | read_lock(&css_set_lock); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 409 | res = find_existing_css_set(oldcg, cgrp, template); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 410 | if (res) |
| 411 | get_css_set(res); |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 412 | read_unlock(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 413 | |
| 414 | if (res) |
| 415 | return res; |
| 416 | |
| 417 | res = kmalloc(sizeof(*res), GFP_KERNEL); |
| 418 | if (!res) |
| 419 | return NULL; |
| 420 | |
| 421 | /* Allocate all the cg_cgroup_link objects that we'll need */ |
| 422 | if (allocate_cg_links(root_count, &tmp_cg_links) < 0) { |
| 423 | kfree(res); |
| 424 | return NULL; |
| 425 | } |
| 426 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 427 | atomic_set(&res->refcount, 1); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 428 | INIT_LIST_HEAD(&res->cg_links); |
| 429 | INIT_LIST_HEAD(&res->tasks); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 430 | INIT_HLIST_NODE(&res->hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 431 | |
| 432 | /* Copy the set of subsystem state objects generated in |
| 433 | * find_existing_css_set() */ |
| 434 | memcpy(res->subsys, template, sizeof(res->subsys)); |
| 435 | |
| 436 | write_lock(&css_set_lock); |
| 437 | /* Add reference counts and links from the new css_set. */ |
| 438 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 439 | struct cgroup *cgrp = res->subsys[i]->cgroup; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 440 | struct cgroup_subsys *ss = subsys[i]; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 441 | atomic_inc(&cgrp->count); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 442 | /* |
| 443 | * We want to add a link once per cgroup, so we |
| 444 | * only do it for the first subsystem in each |
| 445 | * hierarchy |
| 446 | */ |
| 447 | if (ss->root->subsys_list.next == &ss->sibling) { |
| 448 | BUG_ON(list_empty(&tmp_cg_links)); |
| 449 | link = list_entry(tmp_cg_links.next, |
| 450 | struct cg_cgroup_link, |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 451 | cgrp_link_list); |
| 452 | list_del(&link->cgrp_link_list); |
| 453 | list_add(&link->cgrp_link_list, &cgrp->css_sets); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 454 | link->cg = res; |
| 455 | list_add(&link->cg_link_list, &res->cg_links); |
| 456 | } |
| 457 | } |
| 458 | if (list_empty(&rootnode.subsys_list)) { |
| 459 | link = list_entry(tmp_cg_links.next, |
| 460 | struct cg_cgroup_link, |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 461 | cgrp_link_list); |
| 462 | list_del(&link->cgrp_link_list); |
| 463 | list_add(&link->cgrp_link_list, &dummytop->css_sets); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 464 | link->cg = res; |
| 465 | list_add(&link->cg_link_list, &res->cg_links); |
| 466 | } |
| 467 | |
| 468 | BUG_ON(!list_empty(&tmp_cg_links)); |
| 469 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 470 | css_set_count++; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 471 | |
| 472 | /* Add this cgroup group to the hash table */ |
| 473 | hhead = css_set_hash(res->subsys); |
| 474 | hlist_add_head(&res->hlist, hhead); |
| 475 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 476 | write_unlock(&css_set_lock); |
| 477 | |
| 478 | return res; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 481 | /* |
| 482 | * There is one global cgroup mutex. We also require taking |
| 483 | * task_lock() when dereferencing a task's cgroup subsys pointers. |
| 484 | * See "The task_lock() exception", at the end of this comment. |
| 485 | * |
| 486 | * A task must hold cgroup_mutex to modify cgroups. |
| 487 | * |
| 488 | * Any task can increment and decrement the count field without lock. |
| 489 | * So in general, code holding cgroup_mutex can't rely on the count |
| 490 | * field not changing. However, if the count goes to zero, then only |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 491 | * cgroup_attach_task() can increment it again. Because a count of zero |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 492 | * means that no tasks are currently attached, therefore there is no |
| 493 | * way a task attached to that cgroup can fork (the other way to |
| 494 | * increment the count). So code holding cgroup_mutex can safely |
| 495 | * assume that if the count is zero, it will stay zero. Similarly, if |
| 496 | * a task holds cgroup_mutex on a cgroup with zero count, it |
| 497 | * knows that the cgroup won't be removed, as cgroup_rmdir() |
| 498 | * needs that mutex. |
| 499 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 500 | * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't |
| 501 | * (usually) take cgroup_mutex. These are the two most performance |
| 502 | * critical pieces of code here. The exception occurs on cgroup_exit(), |
| 503 | * when a task in a notify_on_release cgroup exits. Then cgroup_mutex |
| 504 | * 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] | 505 | * to the release agent with the name of the cgroup (path relative to |
| 506 | * the root of cgroup file system) as the argument. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 507 | * |
| 508 | * A cgroup can only be deleted if both its 'count' of using tasks |
| 509 | * is zero, and its list of 'children' cgroups is empty. Since all |
| 510 | * tasks in the system use _some_ cgroup, and since there is always at |
| 511 | * least one task in the system (init, pid == 1), therefore, top_cgroup |
| 512 | * always has either children cgroups and/or using tasks. So we don't |
| 513 | * need a special hack to ensure that top_cgroup cannot be deleted. |
| 514 | * |
| 515 | * The task_lock() exception |
| 516 | * |
| 517 | * The need for this exception arises from the action of |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 518 | * cgroup_attach_task(), which overwrites one tasks cgroup pointer with |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 519 | * another. It does so using cgroup_mutex, however there are |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 520 | * several performance critical places that need to reference |
| 521 | * task->cgroup without the expense of grabbing a system global |
| 522 | * mutex. Therefore except as noted below, when dereferencing or, as |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 523 | * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 524 | * task_lock(), which acts on a spinlock (task->alloc_lock) already in |
| 525 | * the task_struct routinely used for such matters. |
| 526 | * |
| 527 | * P.S. One more locking exception. RCU is used to guard the |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 528 | * update of a tasks cgroup pointer by cgroup_attach_task() |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 529 | */ |
| 530 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 531 | /** |
| 532 | * cgroup_lock - lock out any changes to cgroup structures |
| 533 | * |
| 534 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 535 | void cgroup_lock(void) |
| 536 | { |
| 537 | mutex_lock(&cgroup_mutex); |
| 538 | } |
| 539 | |
| 540 | /** |
| 541 | * cgroup_unlock - release lock on cgroup changes |
| 542 | * |
| 543 | * Undo the lock taken in a previous cgroup_lock() call. |
| 544 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 545 | void cgroup_unlock(void) |
| 546 | { |
| 547 | mutex_unlock(&cgroup_mutex); |
| 548 | } |
| 549 | |
| 550 | /* |
| 551 | * A couple of forward declarations required, due to cyclic reference loop: |
| 552 | * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir -> |
| 553 | * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations |
| 554 | * -> cgroup_mkdir. |
| 555 | */ |
| 556 | |
| 557 | static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode); |
| 558 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 559 | static int cgroup_populate_dir(struct cgroup *cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 560 | static struct inode_operations cgroup_dir_inode_operations; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 561 | static struct file_operations proc_cgroupstats_operations; |
| 562 | |
| 563 | static struct backing_dev_info cgroup_backing_dev_info = { |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 564 | .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK, |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 565 | }; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 566 | |
| 567 | static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb) |
| 568 | { |
| 569 | struct inode *inode = new_inode(sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 570 | |
| 571 | if (inode) { |
| 572 | inode->i_mode = mode; |
David Howells | 76aac0e | 2008-11-14 10:39:12 +1100 | [diff] [blame] | 573 | inode->i_uid = current_fsuid(); |
| 574 | inode->i_gid = current_fsgid(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 575 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 576 | inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info; |
| 577 | } |
| 578 | return inode; |
| 579 | } |
| 580 | |
KAMEZAWA Hiroyuki | 4fca88c | 2008-02-07 00:14:27 -0800 | [diff] [blame] | 581 | /* |
| 582 | * Call subsys's pre_destroy handler. |
| 583 | * This is called before css refcnt check. |
| 584 | */ |
KAMEZAWA Hiroyuki | 4fca88c | 2008-02-07 00:14:27 -0800 | [diff] [blame] | 585 | static void cgroup_call_pre_destroy(struct cgroup *cgrp) |
| 586 | { |
| 587 | struct cgroup_subsys *ss; |
| 588 | for_each_subsys(cgrp->root, ss) |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 589 | if (ss->pre_destroy) |
KAMEZAWA Hiroyuki | 4fca88c | 2008-02-07 00:14:27 -0800 | [diff] [blame] | 590 | ss->pre_destroy(ss, cgrp); |
| 591 | return; |
| 592 | } |
| 593 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 594 | static void cgroup_diput(struct dentry *dentry, struct inode *inode) |
| 595 | { |
| 596 | /* is dentry a directory ? if so, kfree() associated cgroup */ |
| 597 | if (S_ISDIR(inode->i_mode)) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 598 | struct cgroup *cgrp = dentry->d_fsdata; |
Paul Menage | 8dc4f3e | 2008-02-07 00:13:45 -0800 | [diff] [blame] | 599 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 600 | BUG_ON(!(cgroup_is_removed(cgrp))); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 601 | /* It's possible for external users to be holding css |
| 602 | * reference counts on a cgroup; css_put() needs to |
| 603 | * be able to access the cgroup after decrementing |
| 604 | * the reference count in order to know if it needs to |
| 605 | * queue the cgroup to be handled by the release |
| 606 | * agent */ |
| 607 | synchronize_rcu(); |
Paul Menage | 8dc4f3e | 2008-02-07 00:13:45 -0800 | [diff] [blame] | 608 | |
| 609 | mutex_lock(&cgroup_mutex); |
| 610 | /* |
| 611 | * Release the subsystem state objects. |
| 612 | */ |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 613 | for_each_subsys(cgrp->root, ss) |
| 614 | ss->destroy(ss, cgrp); |
Paul Menage | 8dc4f3e | 2008-02-07 00:13:45 -0800 | [diff] [blame] | 615 | |
| 616 | cgrp->root->number_of_cgroups--; |
| 617 | mutex_unlock(&cgroup_mutex); |
| 618 | |
| 619 | /* Drop the active superblock reference that we took when we |
| 620 | * created the cgroup */ |
| 621 | deactivate_super(cgrp->root->sb); |
| 622 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 623 | kfree(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 624 | } |
| 625 | iput(inode); |
| 626 | } |
| 627 | |
| 628 | static void remove_dir(struct dentry *d) |
| 629 | { |
| 630 | struct dentry *parent = dget(d->d_parent); |
| 631 | |
| 632 | d_delete(d); |
| 633 | simple_rmdir(parent->d_inode, d); |
| 634 | dput(parent); |
| 635 | } |
| 636 | |
| 637 | static void cgroup_clear_directory(struct dentry *dentry) |
| 638 | { |
| 639 | struct list_head *node; |
| 640 | |
| 641 | BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex)); |
| 642 | spin_lock(&dcache_lock); |
| 643 | node = dentry->d_subdirs.next; |
| 644 | while (node != &dentry->d_subdirs) { |
| 645 | struct dentry *d = list_entry(node, struct dentry, d_u.d_child); |
| 646 | list_del_init(node); |
| 647 | if (d->d_inode) { |
| 648 | /* This should never be called on a cgroup |
| 649 | * directory with child cgroups */ |
| 650 | BUG_ON(d->d_inode->i_mode & S_IFDIR); |
| 651 | d = dget_locked(d); |
| 652 | spin_unlock(&dcache_lock); |
| 653 | d_delete(d); |
| 654 | simple_unlink(dentry->d_inode, d); |
| 655 | dput(d); |
| 656 | spin_lock(&dcache_lock); |
| 657 | } |
| 658 | node = dentry->d_subdirs.next; |
| 659 | } |
| 660 | spin_unlock(&dcache_lock); |
| 661 | } |
| 662 | |
| 663 | /* |
| 664 | * NOTE : the dentry must have been dget()'ed |
| 665 | */ |
| 666 | static void cgroup_d_remove_dir(struct dentry *dentry) |
| 667 | { |
| 668 | cgroup_clear_directory(dentry); |
| 669 | |
| 670 | spin_lock(&dcache_lock); |
| 671 | list_del_init(&dentry->d_u.d_child); |
| 672 | spin_unlock(&dcache_lock); |
| 673 | remove_dir(dentry); |
| 674 | } |
| 675 | |
| 676 | static int rebind_subsystems(struct cgroupfs_root *root, |
| 677 | unsigned long final_bits) |
| 678 | { |
| 679 | unsigned long added_bits, removed_bits; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 680 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 681 | int i; |
| 682 | |
| 683 | removed_bits = root->actual_subsys_bits & ~final_bits; |
| 684 | added_bits = final_bits & ~root->actual_subsys_bits; |
| 685 | /* Check that any added subsystems are currently free */ |
| 686 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Li Zefan | 8d53d55 | 2008-02-23 15:24:11 -0800 | [diff] [blame] | 687 | unsigned long bit = 1UL << i; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 688 | struct cgroup_subsys *ss = subsys[i]; |
| 689 | if (!(bit & added_bits)) |
| 690 | continue; |
| 691 | if (ss->root != &rootnode) { |
| 692 | /* Subsystem isn't free */ |
| 693 | return -EBUSY; |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | /* Currently we don't handle adding/removing subsystems when |
| 698 | * any child cgroups exist. This is theoretically supportable |
| 699 | * but involves complex error handling, so it's being left until |
| 700 | * later */ |
Paul Menage | 307257c | 2008-12-15 13:54:22 -0800 | [diff] [blame] | 701 | if (root->number_of_cgroups > 1) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 702 | return -EBUSY; |
| 703 | |
| 704 | /* Process each subsystem */ |
| 705 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 706 | struct cgroup_subsys *ss = subsys[i]; |
| 707 | unsigned long bit = 1UL << i; |
| 708 | if (bit & added_bits) { |
| 709 | /* We're binding this subsystem to this hierarchy */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 710 | BUG_ON(cgrp->subsys[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 711 | BUG_ON(!dummytop->subsys[i]); |
| 712 | BUG_ON(dummytop->subsys[i]->cgroup != dummytop); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 713 | cgrp->subsys[i] = dummytop->subsys[i]; |
| 714 | cgrp->subsys[i]->cgroup = cgrp; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 715 | list_add(&ss->sibling, &root->subsys_list); |
Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 716 | ss->root = root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 717 | if (ss->bind) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 718 | ss->bind(ss, cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 719 | |
| 720 | } else if (bit & removed_bits) { |
| 721 | /* We're removing this subsystem */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 722 | BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]); |
| 723 | BUG_ON(cgrp->subsys[i]->cgroup != cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 724 | if (ss->bind) |
| 725 | ss->bind(ss, dummytop); |
| 726 | dummytop->subsys[i]->cgroup = dummytop; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 727 | cgrp->subsys[i] = NULL; |
Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 728 | subsys[i]->root = &rootnode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 729 | list_del(&ss->sibling); |
| 730 | } else if (bit & final_bits) { |
| 731 | /* Subsystem state should already exist */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 732 | BUG_ON(!cgrp->subsys[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 733 | } else { |
| 734 | /* Subsystem state shouldn't exist */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 735 | BUG_ON(cgrp->subsys[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 736 | } |
| 737 | } |
| 738 | root->subsys_bits = root->actual_subsys_bits = final_bits; |
| 739 | synchronize_rcu(); |
| 740 | |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs) |
| 745 | { |
| 746 | struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info; |
| 747 | struct cgroup_subsys *ss; |
| 748 | |
| 749 | mutex_lock(&cgroup_mutex); |
| 750 | for_each_subsys(root, ss) |
| 751 | seq_printf(seq, ",%s", ss->name); |
| 752 | if (test_bit(ROOT_NOPREFIX, &root->flags)) |
| 753 | seq_puts(seq, ",noprefix"); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 754 | if (strlen(root->release_agent_path)) |
| 755 | seq_printf(seq, ",release_agent=%s", root->release_agent_path); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 756 | mutex_unlock(&cgroup_mutex); |
| 757 | return 0; |
| 758 | } |
| 759 | |
| 760 | struct cgroup_sb_opts { |
| 761 | unsigned long subsys_bits; |
| 762 | unsigned long flags; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 763 | char *release_agent; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 764 | }; |
| 765 | |
| 766 | /* Convert a hierarchy specifier into a bitmask of subsystems and |
| 767 | * flags. */ |
| 768 | static int parse_cgroupfs_options(char *data, |
| 769 | struct cgroup_sb_opts *opts) |
| 770 | { |
| 771 | char *token, *o = data ?: "all"; |
| 772 | |
| 773 | opts->subsys_bits = 0; |
| 774 | opts->flags = 0; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 775 | opts->release_agent = NULL; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 776 | |
| 777 | while ((token = strsep(&o, ",")) != NULL) { |
| 778 | if (!*token) |
| 779 | return -EINVAL; |
| 780 | if (!strcmp(token, "all")) { |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 781 | /* Add all non-disabled subsystems */ |
| 782 | int i; |
| 783 | opts->subsys_bits = 0; |
| 784 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 785 | struct cgroup_subsys *ss = subsys[i]; |
| 786 | if (!ss->disabled) |
| 787 | opts->subsys_bits |= 1ul << i; |
| 788 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 789 | } else if (!strcmp(token, "noprefix")) { |
| 790 | set_bit(ROOT_NOPREFIX, &opts->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 791 | } else if (!strncmp(token, "release_agent=", 14)) { |
| 792 | /* Specifying two release agents is forbidden */ |
| 793 | if (opts->release_agent) |
| 794 | return -EINVAL; |
| 795 | opts->release_agent = kzalloc(PATH_MAX, GFP_KERNEL); |
| 796 | if (!opts->release_agent) |
| 797 | return -ENOMEM; |
| 798 | strncpy(opts->release_agent, token + 14, PATH_MAX - 1); |
| 799 | opts->release_agent[PATH_MAX - 1] = 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 800 | } else { |
| 801 | struct cgroup_subsys *ss; |
| 802 | int i; |
| 803 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 804 | ss = subsys[i]; |
| 805 | if (!strcmp(token, ss->name)) { |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 806 | if (!ss->disabled) |
| 807 | set_bit(i, &opts->subsys_bits); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 808 | break; |
| 809 | } |
| 810 | } |
| 811 | if (i == CGROUP_SUBSYS_COUNT) |
| 812 | return -ENOENT; |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | /* We can't have an empty hierarchy */ |
| 817 | if (!opts->subsys_bits) |
| 818 | return -EINVAL; |
| 819 | |
| 820 | return 0; |
| 821 | } |
| 822 | |
| 823 | static int cgroup_remount(struct super_block *sb, int *flags, char *data) |
| 824 | { |
| 825 | int ret = 0; |
| 826 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 827 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 828 | struct cgroup_sb_opts opts; |
| 829 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 830 | mutex_lock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 831 | mutex_lock(&cgroup_mutex); |
| 832 | |
| 833 | /* See what subsystems are wanted */ |
| 834 | ret = parse_cgroupfs_options(data, &opts); |
| 835 | if (ret) |
| 836 | goto out_unlock; |
| 837 | |
| 838 | /* Don't allow flags to change at remount */ |
| 839 | if (opts.flags != root->flags) { |
| 840 | ret = -EINVAL; |
| 841 | goto out_unlock; |
| 842 | } |
| 843 | |
| 844 | ret = rebind_subsystems(root, opts.subsys_bits); |
| 845 | |
| 846 | /* (re)populate subsystem files */ |
| 847 | if (!ret) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 848 | cgroup_populate_dir(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 849 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 850 | if (opts.release_agent) |
| 851 | strcpy(root->release_agent_path, opts.release_agent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 852 | out_unlock: |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 853 | if (opts.release_agent) |
| 854 | kfree(opts.release_agent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 855 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 856 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 857 | return ret; |
| 858 | } |
| 859 | |
| 860 | static struct super_operations cgroup_ops = { |
| 861 | .statfs = simple_statfs, |
| 862 | .drop_inode = generic_delete_inode, |
| 863 | .show_options = cgroup_show_options, |
| 864 | .remount_fs = cgroup_remount, |
| 865 | }; |
| 866 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 867 | static void init_cgroup_housekeeping(struct cgroup *cgrp) |
| 868 | { |
| 869 | INIT_LIST_HEAD(&cgrp->sibling); |
| 870 | INIT_LIST_HEAD(&cgrp->children); |
| 871 | INIT_LIST_HEAD(&cgrp->css_sets); |
| 872 | INIT_LIST_HEAD(&cgrp->release_list); |
| 873 | init_rwsem(&cgrp->pids_mutex); |
| 874 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 875 | static void init_cgroup_root(struct cgroupfs_root *root) |
| 876 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 877 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 878 | INIT_LIST_HEAD(&root->subsys_list); |
| 879 | INIT_LIST_HEAD(&root->root_list); |
| 880 | root->number_of_cgroups = 1; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 881 | cgrp->root = root; |
| 882 | cgrp->top_cgroup = cgrp; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 883 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | static int cgroup_test_super(struct super_block *sb, void *data) |
| 887 | { |
| 888 | struct cgroupfs_root *new = data; |
| 889 | struct cgroupfs_root *root = sb->s_fs_info; |
| 890 | |
| 891 | /* First check subsystems */ |
| 892 | if (new->subsys_bits != root->subsys_bits) |
| 893 | return 0; |
| 894 | |
| 895 | /* Next check flags */ |
| 896 | if (new->flags != root->flags) |
| 897 | return 0; |
| 898 | |
| 899 | return 1; |
| 900 | } |
| 901 | |
| 902 | static int cgroup_set_super(struct super_block *sb, void *data) |
| 903 | { |
| 904 | int ret; |
| 905 | struct cgroupfs_root *root = data; |
| 906 | |
| 907 | ret = set_anon_super(sb, NULL); |
| 908 | if (ret) |
| 909 | return ret; |
| 910 | |
| 911 | sb->s_fs_info = root; |
| 912 | root->sb = sb; |
| 913 | |
| 914 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 915 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 916 | sb->s_magic = CGROUP_SUPER_MAGIC; |
| 917 | sb->s_op = &cgroup_ops; |
| 918 | |
| 919 | return 0; |
| 920 | } |
| 921 | |
| 922 | static int cgroup_get_rootdir(struct super_block *sb) |
| 923 | { |
| 924 | struct inode *inode = |
| 925 | cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb); |
| 926 | struct dentry *dentry; |
| 927 | |
| 928 | if (!inode) |
| 929 | return -ENOMEM; |
| 930 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 931 | inode->i_fop = &simple_dir_operations; |
| 932 | inode->i_op = &cgroup_dir_inode_operations; |
| 933 | /* directories start off with i_nlink == 2 (for "." entry) */ |
| 934 | inc_nlink(inode); |
| 935 | dentry = d_alloc_root(inode); |
| 936 | if (!dentry) { |
| 937 | iput(inode); |
| 938 | return -ENOMEM; |
| 939 | } |
| 940 | sb->s_root = dentry; |
| 941 | return 0; |
| 942 | } |
| 943 | |
| 944 | static int cgroup_get_sb(struct file_system_type *fs_type, |
| 945 | int flags, const char *unused_dev_name, |
| 946 | void *data, struct vfsmount *mnt) |
| 947 | { |
| 948 | struct cgroup_sb_opts opts; |
| 949 | int ret = 0; |
| 950 | struct super_block *sb; |
| 951 | struct cgroupfs_root *root; |
Li Zefan | 28fd5df | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 952 | struct list_head tmp_cg_links; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 953 | |
| 954 | /* First find the desired set of subsystems */ |
| 955 | ret = parse_cgroupfs_options(data, &opts); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 956 | if (ret) { |
| 957 | if (opts.release_agent) |
| 958 | kfree(opts.release_agent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 959 | return ret; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 960 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 961 | |
| 962 | root = kzalloc(sizeof(*root), GFP_KERNEL); |
Li Zefan | f777073 | 2008-02-23 15:24:10 -0800 | [diff] [blame] | 963 | if (!root) { |
| 964 | if (opts.release_agent) |
| 965 | kfree(opts.release_agent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 966 | return -ENOMEM; |
Li Zefan | f777073 | 2008-02-23 15:24:10 -0800 | [diff] [blame] | 967 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 968 | |
| 969 | init_cgroup_root(root); |
| 970 | root->subsys_bits = opts.subsys_bits; |
| 971 | root->flags = opts.flags; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 972 | if (opts.release_agent) { |
| 973 | strcpy(root->release_agent_path, opts.release_agent); |
| 974 | kfree(opts.release_agent); |
| 975 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 976 | |
| 977 | sb = sget(fs_type, cgroup_test_super, cgroup_set_super, root); |
| 978 | |
| 979 | if (IS_ERR(sb)) { |
| 980 | kfree(root); |
| 981 | return PTR_ERR(sb); |
| 982 | } |
| 983 | |
| 984 | if (sb->s_fs_info != root) { |
| 985 | /* Reusing an existing superblock */ |
| 986 | BUG_ON(sb->s_root == NULL); |
| 987 | kfree(root); |
| 988 | root = NULL; |
| 989 | } else { |
| 990 | /* New superblock */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 991 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 992 | struct inode *inode; |
Li Zefan | 28fd5df | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 993 | int i; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 994 | |
| 995 | BUG_ON(sb->s_root != NULL); |
| 996 | |
| 997 | ret = cgroup_get_rootdir(sb); |
| 998 | if (ret) |
| 999 | goto drop_new_super; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1000 | inode = sb->s_root->d_inode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1001 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1002 | mutex_lock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1003 | mutex_lock(&cgroup_mutex); |
| 1004 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1005 | /* |
| 1006 | * We're accessing css_set_count without locking |
| 1007 | * css_set_lock here, but that's OK - it can only be |
| 1008 | * increased by someone holding cgroup_lock, and |
| 1009 | * that's us. The worst that can happen is that we |
| 1010 | * have some link structures left over |
| 1011 | */ |
| 1012 | ret = allocate_cg_links(css_set_count, &tmp_cg_links); |
| 1013 | if (ret) { |
| 1014 | mutex_unlock(&cgroup_mutex); |
| 1015 | mutex_unlock(&inode->i_mutex); |
| 1016 | goto drop_new_super; |
| 1017 | } |
| 1018 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1019 | ret = rebind_subsystems(root, root->subsys_bits); |
| 1020 | if (ret == -EBUSY) { |
| 1021 | mutex_unlock(&cgroup_mutex); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1022 | mutex_unlock(&inode->i_mutex); |
Li Zefan | 20ca9b3 | 2008-12-23 13:57:14 -0800 | [diff] [blame] | 1023 | goto free_cg_links; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | /* EBUSY should be the only error here */ |
| 1027 | BUG_ON(ret); |
| 1028 | |
| 1029 | list_add(&root->root_list, &roots); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1030 | root_count++; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1031 | |
| 1032 | sb->s_root->d_fsdata = &root->top_cgroup; |
| 1033 | root->top_cgroup.dentry = sb->s_root; |
| 1034 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1035 | /* Link the top cgroup in this hierarchy into all |
| 1036 | * the css_set objects */ |
| 1037 | write_lock(&css_set_lock); |
Li Zefan | 28fd5df | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 1038 | for (i = 0; i < CSS_SET_TABLE_SIZE; i++) { |
| 1039 | struct hlist_head *hhead = &css_set_table[i]; |
| 1040 | struct hlist_node *node; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1041 | struct css_set *cg; |
Li Zefan | 28fd5df | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 1042 | |
| 1043 | hlist_for_each_entry(cg, node, hhead, hlist) { |
| 1044 | struct cg_cgroup_link *link; |
| 1045 | |
| 1046 | BUG_ON(list_empty(&tmp_cg_links)); |
| 1047 | link = list_entry(tmp_cg_links.next, |
| 1048 | struct cg_cgroup_link, |
| 1049 | cgrp_link_list); |
| 1050 | list_del(&link->cgrp_link_list); |
| 1051 | link->cg = cg; |
| 1052 | list_add(&link->cgrp_link_list, |
| 1053 | &root->top_cgroup.css_sets); |
| 1054 | list_add(&link->cg_link_list, &cg->cg_links); |
| 1055 | } |
| 1056 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1057 | write_unlock(&css_set_lock); |
| 1058 | |
| 1059 | free_cg_links(&tmp_cg_links); |
| 1060 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1061 | BUG_ON(!list_empty(&cgrp->sibling)); |
| 1062 | BUG_ON(!list_empty(&cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1063 | BUG_ON(root->number_of_cgroups != 1); |
| 1064 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1065 | cgroup_populate_dir(cgrp); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1066 | mutex_unlock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1067 | mutex_unlock(&cgroup_mutex); |
| 1068 | } |
| 1069 | |
| 1070 | return simple_set_mnt(mnt, sb); |
| 1071 | |
Li Zefan | 20ca9b3 | 2008-12-23 13:57:14 -0800 | [diff] [blame] | 1072 | free_cg_links: |
| 1073 | free_cg_links(&tmp_cg_links); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1074 | drop_new_super: |
| 1075 | up_write(&sb->s_umount); |
| 1076 | deactivate_super(sb); |
| 1077 | return ret; |
| 1078 | } |
| 1079 | |
| 1080 | static void cgroup_kill_sb(struct super_block *sb) { |
| 1081 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1082 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1083 | int ret; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1084 | struct cg_cgroup_link *link; |
| 1085 | struct cg_cgroup_link *saved_link; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1086 | |
| 1087 | BUG_ON(!root); |
| 1088 | |
| 1089 | BUG_ON(root->number_of_cgroups != 1); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1090 | BUG_ON(!list_empty(&cgrp->children)); |
| 1091 | BUG_ON(!list_empty(&cgrp->sibling)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1092 | |
| 1093 | mutex_lock(&cgroup_mutex); |
| 1094 | |
| 1095 | /* Rebind all subsystems back to the default hierarchy */ |
| 1096 | ret = rebind_subsystems(root, 0); |
| 1097 | /* Shouldn't be able to fail ... */ |
| 1098 | BUG_ON(ret); |
| 1099 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1100 | /* |
| 1101 | * Release all the links from css_sets to this hierarchy's |
| 1102 | * root cgroup |
| 1103 | */ |
| 1104 | write_lock(&css_set_lock); |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1105 | |
| 1106 | list_for_each_entry_safe(link, saved_link, &cgrp->css_sets, |
| 1107 | cgrp_link_list) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1108 | list_del(&link->cg_link_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1109 | list_del(&link->cgrp_link_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1110 | kfree(link); |
| 1111 | } |
| 1112 | write_unlock(&css_set_lock); |
| 1113 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame^] | 1114 | list_del(&root->root_list); |
| 1115 | root_count--; |
| 1116 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1117 | mutex_unlock(&cgroup_mutex); |
| 1118 | |
| 1119 | kfree(root); |
| 1120 | kill_litter_super(sb); |
| 1121 | } |
| 1122 | |
| 1123 | static struct file_system_type cgroup_fs_type = { |
| 1124 | .name = "cgroup", |
| 1125 | .get_sb = cgroup_get_sb, |
| 1126 | .kill_sb = cgroup_kill_sb, |
| 1127 | }; |
| 1128 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1129 | static inline struct cgroup *__d_cgrp(struct dentry *dentry) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1130 | { |
| 1131 | return dentry->d_fsdata; |
| 1132 | } |
| 1133 | |
| 1134 | static inline struct cftype *__d_cft(struct dentry *dentry) |
| 1135 | { |
| 1136 | return dentry->d_fsdata; |
| 1137 | } |
| 1138 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1139 | /** |
| 1140 | * cgroup_path - generate the path of a cgroup |
| 1141 | * @cgrp: the cgroup in question |
| 1142 | * @buf: the buffer to write the path into |
| 1143 | * @buflen: the length of the buffer |
| 1144 | * |
| 1145 | * Called with cgroup_mutex held. Writes path of cgroup into buf. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1146 | * Returns 0 on success, -errno on error. |
| 1147 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1148 | int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1149 | { |
| 1150 | char *start; |
| 1151 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1152 | if (cgrp == dummytop) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1153 | /* |
| 1154 | * Inactive subsystems have no dentry for their root |
| 1155 | * cgroup |
| 1156 | */ |
| 1157 | strcpy(buf, "/"); |
| 1158 | return 0; |
| 1159 | } |
| 1160 | |
| 1161 | start = buf + buflen; |
| 1162 | |
| 1163 | *--start = '\0'; |
| 1164 | for (;;) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1165 | int len = cgrp->dentry->d_name.len; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1166 | if ((start -= len) < buf) |
| 1167 | return -ENAMETOOLONG; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1168 | memcpy(start, cgrp->dentry->d_name.name, len); |
| 1169 | cgrp = cgrp->parent; |
| 1170 | if (!cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1171 | break; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1172 | if (!cgrp->parent) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1173 | continue; |
| 1174 | if (--start < buf) |
| 1175 | return -ENAMETOOLONG; |
| 1176 | *start = '/'; |
| 1177 | } |
| 1178 | memmove(buf, start, buf + buflen - start); |
| 1179 | return 0; |
| 1180 | } |
| 1181 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1182 | /* |
| 1183 | * Return the first subsystem attached to a cgroup's hierarchy, and |
| 1184 | * its subsystem id. |
| 1185 | */ |
| 1186 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1187 | static void get_first_subsys(const struct cgroup *cgrp, |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1188 | struct cgroup_subsys_state **css, int *subsys_id) |
| 1189 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1190 | const struct cgroupfs_root *root = cgrp->root; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1191 | const struct cgroup_subsys *test_ss; |
| 1192 | BUG_ON(list_empty(&root->subsys_list)); |
| 1193 | test_ss = list_entry(root->subsys_list.next, |
| 1194 | struct cgroup_subsys, sibling); |
| 1195 | if (css) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1196 | *css = cgrp->subsys[test_ss->subsys_id]; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1197 | BUG_ON(!*css); |
| 1198 | } |
| 1199 | if (subsys_id) |
| 1200 | *subsys_id = test_ss->subsys_id; |
| 1201 | } |
| 1202 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1203 | /** |
| 1204 | * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp' |
| 1205 | * @cgrp: the cgroup the task is attaching to |
| 1206 | * @tsk: the task to be attached |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1207 | * |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1208 | * Call holding cgroup_mutex. May take task_lock of |
| 1209 | * the task 'tsk' during call. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1210 | */ |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 1211 | int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1212 | { |
| 1213 | int retval = 0; |
| 1214 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1215 | struct cgroup *oldcgrp; |
Lai Jiangshan | 77efecd | 2009-01-07 18:07:39 -0800 | [diff] [blame] | 1216 | struct css_set *cg; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1217 | struct css_set *newcg; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1218 | struct cgroupfs_root *root = cgrp->root; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1219 | int subsys_id; |
| 1220 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1221 | get_first_subsys(cgrp, NULL, &subsys_id); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1222 | |
| 1223 | /* Nothing to do if the task is already in that cgroup */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1224 | oldcgrp = task_cgroup(tsk, subsys_id); |
| 1225 | if (cgrp == oldcgrp) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1226 | return 0; |
| 1227 | |
| 1228 | for_each_subsys(root, ss) { |
| 1229 | if (ss->can_attach) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1230 | retval = ss->can_attach(ss, cgrp, tsk); |
Paul Jackson | e18f631 | 2008-02-07 00:13:44 -0800 | [diff] [blame] | 1231 | if (retval) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1232 | return retval; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1233 | } |
| 1234 | } |
| 1235 | |
Lai Jiangshan | 77efecd | 2009-01-07 18:07:39 -0800 | [diff] [blame] | 1236 | task_lock(tsk); |
| 1237 | cg = tsk->cgroups; |
| 1238 | get_css_set(cg); |
| 1239 | task_unlock(tsk); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1240 | /* |
| 1241 | * Locate or allocate a new css_set for this task, |
| 1242 | * based on its final set of cgroups |
| 1243 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1244 | newcg = find_css_set(cg, cgrp); |
Lai Jiangshan | 77efecd | 2009-01-07 18:07:39 -0800 | [diff] [blame] | 1245 | put_css_set(cg); |
Paul Jackson | e18f631 | 2008-02-07 00:13:44 -0800 | [diff] [blame] | 1246 | if (!newcg) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1247 | return -ENOMEM; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1248 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1249 | task_lock(tsk); |
| 1250 | if (tsk->flags & PF_EXITING) { |
| 1251 | task_unlock(tsk); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1252 | put_css_set(newcg); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1253 | return -ESRCH; |
| 1254 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1255 | rcu_assign_pointer(tsk->cgroups, newcg); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1256 | task_unlock(tsk); |
| 1257 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1258 | /* Update the css_set linked lists if we're using them */ |
| 1259 | write_lock(&css_set_lock); |
| 1260 | if (!list_empty(&tsk->cg_list)) { |
| 1261 | list_del(&tsk->cg_list); |
| 1262 | list_add(&tsk->cg_list, &newcg->tasks); |
| 1263 | } |
| 1264 | write_unlock(&css_set_lock); |
| 1265 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1266 | for_each_subsys(root, ss) { |
Paul Jackson | e18f631 | 2008-02-07 00:13:44 -0800 | [diff] [blame] | 1267 | if (ss->attach) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1268 | ss->attach(ss, cgrp, oldcgrp, tsk); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1269 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1270 | set_bit(CGRP_RELEASABLE, &oldcgrp->flags); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1271 | synchronize_rcu(); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1272 | put_css_set(cg); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1273 | return 0; |
| 1274 | } |
| 1275 | |
| 1276 | /* |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 1277 | * Attach task with pid 'pid' to cgroup 'cgrp'. Call with cgroup_mutex |
| 1278 | * held. May take task_lock of task |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1279 | */ |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 1280 | static int attach_task_by_pid(struct cgroup *cgrp, u64 pid) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1281 | { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1282 | struct task_struct *tsk; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1283 | const struct cred *cred = current_cred(), *tcred; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1284 | int ret; |
| 1285 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1286 | if (pid) { |
| 1287 | rcu_read_lock(); |
Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 1288 | tsk = find_task_by_vpid(pid); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1289 | if (!tsk || tsk->flags & PF_EXITING) { |
| 1290 | rcu_read_unlock(); |
| 1291 | return -ESRCH; |
| 1292 | } |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1293 | |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1294 | tcred = __task_cred(tsk); |
| 1295 | if (cred->euid && |
| 1296 | cred->euid != tcred->uid && |
| 1297 | cred->euid != tcred->suid) { |
| 1298 | rcu_read_unlock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1299 | return -EACCES; |
| 1300 | } |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1301 | get_task_struct(tsk); |
| 1302 | rcu_read_unlock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1303 | } else { |
| 1304 | tsk = current; |
| 1305 | get_task_struct(tsk); |
| 1306 | } |
| 1307 | |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 1308 | ret = cgroup_attach_task(cgrp, tsk); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1309 | put_task_struct(tsk); |
| 1310 | return ret; |
| 1311 | } |
| 1312 | |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 1313 | static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid) |
| 1314 | { |
| 1315 | int ret; |
| 1316 | if (!cgroup_lock_live_group(cgrp)) |
| 1317 | return -ENODEV; |
| 1318 | ret = attach_task_by_pid(cgrp, pid); |
| 1319 | cgroup_unlock(); |
| 1320 | return ret; |
| 1321 | } |
| 1322 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1323 | /* The various types of files and directories in a cgroup file system */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1324 | enum cgroup_filetype { |
| 1325 | FILE_ROOT, |
| 1326 | FILE_DIR, |
| 1327 | FILE_TASKLIST, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1328 | FILE_NOTIFY_ON_RELEASE, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1329 | FILE_RELEASE_AGENT, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1330 | }; |
| 1331 | |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 1332 | /** |
| 1333 | * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive. |
| 1334 | * @cgrp: the cgroup to be checked for liveness |
| 1335 | * |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 1336 | * On success, returns true; the lock should be later released with |
| 1337 | * cgroup_unlock(). On failure returns false with no lock held. |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 1338 | */ |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 1339 | bool cgroup_lock_live_group(struct cgroup *cgrp) |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 1340 | { |
| 1341 | mutex_lock(&cgroup_mutex); |
| 1342 | if (cgroup_is_removed(cgrp)) { |
| 1343 | mutex_unlock(&cgroup_mutex); |
| 1344 | return false; |
| 1345 | } |
| 1346 | return true; |
| 1347 | } |
| 1348 | |
| 1349 | static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft, |
| 1350 | const char *buffer) |
| 1351 | { |
| 1352 | BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX); |
| 1353 | if (!cgroup_lock_live_group(cgrp)) |
| 1354 | return -ENODEV; |
| 1355 | strcpy(cgrp->root->release_agent_path, buffer); |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 1356 | cgroup_unlock(); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 1357 | return 0; |
| 1358 | } |
| 1359 | |
| 1360 | static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft, |
| 1361 | struct seq_file *seq) |
| 1362 | { |
| 1363 | if (!cgroup_lock_live_group(cgrp)) |
| 1364 | return -ENODEV; |
| 1365 | seq_puts(seq, cgrp->root->release_agent_path); |
| 1366 | seq_putc(seq, '\n'); |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 1367 | cgroup_unlock(); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 1368 | return 0; |
| 1369 | } |
| 1370 | |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 1371 | /* A buffer size big enough for numbers or short strings */ |
| 1372 | #define CGROUP_LOCAL_BUFFER_SIZE 64 |
| 1373 | |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 1374 | static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft, |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 1375 | struct file *file, |
| 1376 | const char __user *userbuf, |
| 1377 | size_t nbytes, loff_t *unused_ppos) |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 1378 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 1379 | char buffer[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 1380 | int retval = 0; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 1381 | char *end; |
| 1382 | |
| 1383 | if (!nbytes) |
| 1384 | return -EINVAL; |
| 1385 | if (nbytes >= sizeof(buffer)) |
| 1386 | return -E2BIG; |
| 1387 | if (copy_from_user(buffer, userbuf, nbytes)) |
| 1388 | return -EFAULT; |
| 1389 | |
| 1390 | buffer[nbytes] = 0; /* nul-terminate */ |
Paul Menage | b7269df | 2008-04-29 00:59:59 -0700 | [diff] [blame] | 1391 | strstrip(buffer); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 1392 | if (cft->write_u64) { |
| 1393 | u64 val = simple_strtoull(buffer, &end, 0); |
| 1394 | if (*end) |
| 1395 | return -EINVAL; |
| 1396 | retval = cft->write_u64(cgrp, cft, val); |
| 1397 | } else { |
| 1398 | s64 val = simple_strtoll(buffer, &end, 0); |
| 1399 | if (*end) |
| 1400 | return -EINVAL; |
| 1401 | retval = cft->write_s64(cgrp, cft, val); |
| 1402 | } |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 1403 | if (!retval) |
| 1404 | retval = nbytes; |
| 1405 | return retval; |
| 1406 | } |
| 1407 | |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 1408 | static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft, |
| 1409 | struct file *file, |
| 1410 | const char __user *userbuf, |
| 1411 | size_t nbytes, loff_t *unused_ppos) |
| 1412 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 1413 | char local_buffer[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 1414 | int retval = 0; |
| 1415 | size_t max_bytes = cft->max_write_len; |
| 1416 | char *buffer = local_buffer; |
| 1417 | |
| 1418 | if (!max_bytes) |
| 1419 | max_bytes = sizeof(local_buffer) - 1; |
| 1420 | if (nbytes >= max_bytes) |
| 1421 | return -E2BIG; |
| 1422 | /* Allocate a dynamic buffer if we need one */ |
| 1423 | if (nbytes >= sizeof(local_buffer)) { |
| 1424 | buffer = kmalloc(nbytes + 1, GFP_KERNEL); |
| 1425 | if (buffer == NULL) |
| 1426 | return -ENOMEM; |
| 1427 | } |
Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 1428 | if (nbytes && copy_from_user(buffer, userbuf, nbytes)) { |
| 1429 | retval = -EFAULT; |
| 1430 | goto out; |
| 1431 | } |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 1432 | |
| 1433 | buffer[nbytes] = 0; /* nul-terminate */ |
| 1434 | strstrip(buffer); |
| 1435 | retval = cft->write_string(cgrp, cft, buffer); |
| 1436 | if (!retval) |
| 1437 | retval = nbytes; |
Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 1438 | out: |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 1439 | if (buffer != local_buffer) |
| 1440 | kfree(buffer); |
| 1441 | return retval; |
| 1442 | } |
| 1443 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1444 | static ssize_t cgroup_file_write(struct file *file, const char __user *buf, |
| 1445 | size_t nbytes, loff_t *ppos) |
| 1446 | { |
| 1447 | struct cftype *cft = __d_cft(file->f_dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1448 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1449 | |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 1450 | if (cgroup_is_removed(cgrp)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1451 | return -ENODEV; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 1452 | if (cft->write) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1453 | return cft->write(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 1454 | if (cft->write_u64 || cft->write_s64) |
| 1455 | return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 1456 | if (cft->write_string) |
| 1457 | return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos); |
Pavel Emelyanov | d447ea2 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 1458 | if (cft->trigger) { |
| 1459 | int ret = cft->trigger(cgrp, (unsigned int)cft->private); |
| 1460 | return ret ? ret : nbytes; |
| 1461 | } |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 1462 | return -EINVAL; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1463 | } |
| 1464 | |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 1465 | static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft, |
| 1466 | struct file *file, |
| 1467 | char __user *buf, size_t nbytes, |
| 1468 | loff_t *ppos) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1469 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 1470 | char tmp[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 1471 | u64 val = cft->read_u64(cgrp, cft); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1472 | int len = sprintf(tmp, "%llu\n", (unsigned long long) val); |
| 1473 | |
| 1474 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
| 1475 | } |
| 1476 | |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 1477 | static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft, |
| 1478 | struct file *file, |
| 1479 | char __user *buf, size_t nbytes, |
| 1480 | loff_t *ppos) |
| 1481 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 1482 | char tmp[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 1483 | s64 val = cft->read_s64(cgrp, cft); |
| 1484 | int len = sprintf(tmp, "%lld\n", (long long) val); |
| 1485 | |
| 1486 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
| 1487 | } |
| 1488 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1489 | static ssize_t cgroup_file_read(struct file *file, char __user *buf, |
| 1490 | size_t nbytes, loff_t *ppos) |
| 1491 | { |
| 1492 | struct cftype *cft = __d_cft(file->f_dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1493 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1494 | |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 1495 | if (cgroup_is_removed(cgrp)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1496 | return -ENODEV; |
| 1497 | |
| 1498 | if (cft->read) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1499 | return cft->read(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 1500 | if (cft->read_u64) |
| 1501 | return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 1502 | if (cft->read_s64) |
| 1503 | return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1504 | return -EINVAL; |
| 1505 | } |
| 1506 | |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 1507 | /* |
| 1508 | * seqfile ops/methods for returning structured data. Currently just |
| 1509 | * supports string->u64 maps, but can be extended in future. |
| 1510 | */ |
| 1511 | |
| 1512 | struct cgroup_seqfile_state { |
| 1513 | struct cftype *cft; |
| 1514 | struct cgroup *cgroup; |
| 1515 | }; |
| 1516 | |
| 1517 | static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value) |
| 1518 | { |
| 1519 | struct seq_file *sf = cb->state; |
| 1520 | return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value); |
| 1521 | } |
| 1522 | |
| 1523 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) |
| 1524 | { |
| 1525 | struct cgroup_seqfile_state *state = m->private; |
| 1526 | struct cftype *cft = state->cft; |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 1527 | if (cft->read_map) { |
| 1528 | struct cgroup_map_cb cb = { |
| 1529 | .fill = cgroup_map_add, |
| 1530 | .state = m, |
| 1531 | }; |
| 1532 | return cft->read_map(state->cgroup, cft, &cb); |
| 1533 | } |
| 1534 | return cft->read_seq_string(state->cgroup, cft, m); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 1535 | } |
| 1536 | |
Adrian Bunk | 96930a6 | 2008-07-25 19:46:21 -0700 | [diff] [blame] | 1537 | static int cgroup_seqfile_release(struct inode *inode, struct file *file) |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 1538 | { |
| 1539 | struct seq_file *seq = file->private_data; |
| 1540 | kfree(seq->private); |
| 1541 | return single_release(inode, file); |
| 1542 | } |
| 1543 | |
| 1544 | static struct file_operations cgroup_seqfile_operations = { |
| 1545 | .read = seq_read, |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 1546 | .write = cgroup_file_write, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 1547 | .llseek = seq_lseek, |
| 1548 | .release = cgroup_seqfile_release, |
| 1549 | }; |
| 1550 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1551 | static int cgroup_file_open(struct inode *inode, struct file *file) |
| 1552 | { |
| 1553 | int err; |
| 1554 | struct cftype *cft; |
| 1555 | |
| 1556 | err = generic_file_open(inode, file); |
| 1557 | if (err) |
| 1558 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1559 | cft = __d_cft(file->f_dentry); |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 1560 | |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 1561 | if (cft->read_map || cft->read_seq_string) { |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 1562 | struct cgroup_seqfile_state *state = |
| 1563 | kzalloc(sizeof(*state), GFP_USER); |
| 1564 | if (!state) |
| 1565 | return -ENOMEM; |
| 1566 | state->cft = cft; |
| 1567 | state->cgroup = __d_cgrp(file->f_dentry->d_parent); |
| 1568 | file->f_op = &cgroup_seqfile_operations; |
| 1569 | err = single_open(file, cgroup_seqfile_show, state); |
| 1570 | if (err < 0) |
| 1571 | kfree(state); |
| 1572 | } else if (cft->open) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1573 | err = cft->open(inode, file); |
| 1574 | else |
| 1575 | err = 0; |
| 1576 | |
| 1577 | return err; |
| 1578 | } |
| 1579 | |
| 1580 | static int cgroup_file_release(struct inode *inode, struct file *file) |
| 1581 | { |
| 1582 | struct cftype *cft = __d_cft(file->f_dentry); |
| 1583 | if (cft->release) |
| 1584 | return cft->release(inode, file); |
| 1585 | return 0; |
| 1586 | } |
| 1587 | |
| 1588 | /* |
| 1589 | * cgroup_rename - Only allow simple rename of directories in place. |
| 1590 | */ |
| 1591 | static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 1592 | struct inode *new_dir, struct dentry *new_dentry) |
| 1593 | { |
| 1594 | if (!S_ISDIR(old_dentry->d_inode->i_mode)) |
| 1595 | return -ENOTDIR; |
| 1596 | if (new_dentry->d_inode) |
| 1597 | return -EEXIST; |
| 1598 | if (old_dir != new_dir) |
| 1599 | return -EIO; |
| 1600 | return simple_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 1601 | } |
| 1602 | |
| 1603 | static struct file_operations cgroup_file_operations = { |
| 1604 | .read = cgroup_file_read, |
| 1605 | .write = cgroup_file_write, |
| 1606 | .llseek = generic_file_llseek, |
| 1607 | .open = cgroup_file_open, |
| 1608 | .release = cgroup_file_release, |
| 1609 | }; |
| 1610 | |
| 1611 | static struct inode_operations cgroup_dir_inode_operations = { |
| 1612 | .lookup = simple_lookup, |
| 1613 | .mkdir = cgroup_mkdir, |
| 1614 | .rmdir = cgroup_rmdir, |
| 1615 | .rename = cgroup_rename, |
| 1616 | }; |
| 1617 | |
| 1618 | static int cgroup_create_file(struct dentry *dentry, int mode, |
| 1619 | struct super_block *sb) |
| 1620 | { |
| 1621 | static struct dentry_operations cgroup_dops = { |
| 1622 | .d_iput = cgroup_diput, |
| 1623 | }; |
| 1624 | |
| 1625 | struct inode *inode; |
| 1626 | |
| 1627 | if (!dentry) |
| 1628 | return -ENOENT; |
| 1629 | if (dentry->d_inode) |
| 1630 | return -EEXIST; |
| 1631 | |
| 1632 | inode = cgroup_new_inode(mode, sb); |
| 1633 | if (!inode) |
| 1634 | return -ENOMEM; |
| 1635 | |
| 1636 | if (S_ISDIR(mode)) { |
| 1637 | inode->i_op = &cgroup_dir_inode_operations; |
| 1638 | inode->i_fop = &simple_dir_operations; |
| 1639 | |
| 1640 | /* start off with i_nlink == 2 (for "." entry) */ |
| 1641 | inc_nlink(inode); |
| 1642 | |
| 1643 | /* start with the directory inode held, so that we can |
| 1644 | * populate it without racing with another mkdir */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1645 | mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1646 | } else if (S_ISREG(mode)) { |
| 1647 | inode->i_size = 0; |
| 1648 | inode->i_fop = &cgroup_file_operations; |
| 1649 | } |
| 1650 | dentry->d_op = &cgroup_dops; |
| 1651 | d_instantiate(dentry, inode); |
| 1652 | dget(dentry); /* Extra count - pin the dentry in core */ |
| 1653 | return 0; |
| 1654 | } |
| 1655 | |
| 1656 | /* |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1657 | * cgroup_create_dir - create a directory for an object. |
| 1658 | * @cgrp: the cgroup we create the directory for. It must have a valid |
| 1659 | * ->parent field. And we are going to fill its ->dentry field. |
| 1660 | * @dentry: dentry of the new cgroup |
| 1661 | * @mode: mode to set on new directory. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1662 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1663 | static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1664 | int mode) |
| 1665 | { |
| 1666 | struct dentry *parent; |
| 1667 | int error = 0; |
| 1668 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1669 | parent = cgrp->parent->dentry; |
| 1670 | error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1671 | if (!error) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1672 | dentry->d_fsdata = cgrp; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1673 | inc_nlink(parent->d_inode); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1674 | cgrp->dentry = dentry; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1675 | dget(dentry); |
| 1676 | } |
| 1677 | dput(dentry); |
| 1678 | |
| 1679 | return error; |
| 1680 | } |
| 1681 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1682 | int cgroup_add_file(struct cgroup *cgrp, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1683 | struct cgroup_subsys *subsys, |
| 1684 | const struct cftype *cft) |
| 1685 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1686 | struct dentry *dir = cgrp->dentry; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1687 | struct dentry *dentry; |
| 1688 | int error; |
| 1689 | |
| 1690 | char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 }; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1691 | if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1692 | strcpy(name, subsys->name); |
| 1693 | strcat(name, "."); |
| 1694 | } |
| 1695 | strcat(name, cft->name); |
| 1696 | BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex)); |
| 1697 | dentry = lookup_one_len(name, dir, strlen(name)); |
| 1698 | if (!IS_ERR(dentry)) { |
| 1699 | error = cgroup_create_file(dentry, 0644 | S_IFREG, |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1700 | cgrp->root->sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1701 | if (!error) |
| 1702 | dentry->d_fsdata = (void *)cft; |
| 1703 | dput(dentry); |
| 1704 | } else |
| 1705 | error = PTR_ERR(dentry); |
| 1706 | return error; |
| 1707 | } |
| 1708 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1709 | int cgroup_add_files(struct cgroup *cgrp, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1710 | struct cgroup_subsys *subsys, |
| 1711 | const struct cftype cft[], |
| 1712 | int count) |
| 1713 | { |
| 1714 | int i, err; |
| 1715 | for (i = 0; i < count; i++) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1716 | err = cgroup_add_file(cgrp, subsys, &cft[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1717 | if (err) |
| 1718 | return err; |
| 1719 | } |
| 1720 | return 0; |
| 1721 | } |
| 1722 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1723 | /** |
| 1724 | * cgroup_task_count - count the number of tasks in a cgroup. |
| 1725 | * @cgrp: the cgroup in question |
| 1726 | * |
| 1727 | * Return the number of tasks in the cgroup. |
| 1728 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1729 | int cgroup_task_count(const struct cgroup *cgrp) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1730 | { |
| 1731 | int count = 0; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1732 | struct cg_cgroup_link *link; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1733 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1734 | read_lock(&css_set_lock); |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1735 | list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 1736 | count += atomic_read(&link->cg->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1737 | } |
| 1738 | read_unlock(&css_set_lock); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1739 | return count; |
| 1740 | } |
| 1741 | |
| 1742 | /* |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1743 | * Advance a list_head iterator. The iterator should be positioned at |
| 1744 | * the start of a css_set |
| 1745 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1746 | static void cgroup_advance_iter(struct cgroup *cgrp, |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1747 | struct cgroup_iter *it) |
| 1748 | { |
| 1749 | struct list_head *l = it->cg_link; |
| 1750 | struct cg_cgroup_link *link; |
| 1751 | struct css_set *cg; |
| 1752 | |
| 1753 | /* Advance to the next non-empty css_set */ |
| 1754 | do { |
| 1755 | l = l->next; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1756 | if (l == &cgrp->css_sets) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1757 | it->cg_link = NULL; |
| 1758 | return; |
| 1759 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1760 | link = list_entry(l, struct cg_cgroup_link, cgrp_link_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1761 | cg = link->cg; |
| 1762 | } while (list_empty(&cg->tasks)); |
| 1763 | it->cg_link = l; |
| 1764 | it->task = cg->tasks.next; |
| 1765 | } |
| 1766 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 1767 | /* |
| 1768 | * To reduce the fork() overhead for systems that are not actually |
| 1769 | * using their cgroups capability, we don't maintain the lists running |
| 1770 | * through each css_set to its tasks until we see the list actually |
| 1771 | * used - in other words after the first call to cgroup_iter_start(). |
| 1772 | * |
| 1773 | * The tasklist_lock is not held here, as do_each_thread() and |
| 1774 | * while_each_thread() are protected by RCU. |
| 1775 | */ |
Adrian Bunk | 3df91fe | 2008-04-29 00:59:54 -0700 | [diff] [blame] | 1776 | static void cgroup_enable_task_cg_lists(void) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 1777 | { |
| 1778 | struct task_struct *p, *g; |
| 1779 | write_lock(&css_set_lock); |
| 1780 | use_task_css_set_links = 1; |
| 1781 | do_each_thread(g, p) { |
| 1782 | task_lock(p); |
Li Zefan | 0e04388 | 2008-04-17 11:37:15 +0800 | [diff] [blame] | 1783 | /* |
| 1784 | * We should check if the process is exiting, otherwise |
| 1785 | * it will race with cgroup_exit() in that the list |
| 1786 | * entry won't be deleted though the process has exited. |
| 1787 | */ |
| 1788 | if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list)) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 1789 | list_add(&p->cg_list, &p->cgroups->tasks); |
| 1790 | task_unlock(p); |
| 1791 | } while_each_thread(g, p); |
| 1792 | write_unlock(&css_set_lock); |
| 1793 | } |
| 1794 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1795 | void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1796 | { |
| 1797 | /* |
| 1798 | * The first time anyone tries to iterate across a cgroup, |
| 1799 | * we need to enable the list linking each css_set to its |
| 1800 | * tasks, and fix up all existing tasks. |
| 1801 | */ |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 1802 | if (!use_task_css_set_links) |
| 1803 | cgroup_enable_task_cg_lists(); |
| 1804 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1805 | read_lock(&css_set_lock); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1806 | it->cg_link = &cgrp->css_sets; |
| 1807 | cgroup_advance_iter(cgrp, it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1808 | } |
| 1809 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1810 | struct task_struct *cgroup_iter_next(struct cgroup *cgrp, |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1811 | struct cgroup_iter *it) |
| 1812 | { |
| 1813 | struct task_struct *res; |
| 1814 | struct list_head *l = it->task; |
Lai Jiangshan | 2019f63 | 2009-01-07 18:07:36 -0800 | [diff] [blame] | 1815 | struct cg_cgroup_link *link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1816 | |
| 1817 | /* If the iterator cg is NULL, we have no tasks */ |
| 1818 | if (!it->cg_link) |
| 1819 | return NULL; |
| 1820 | res = list_entry(l, struct task_struct, cg_list); |
| 1821 | /* Advance iterator to find next entry */ |
| 1822 | l = l->next; |
Lai Jiangshan | 2019f63 | 2009-01-07 18:07:36 -0800 | [diff] [blame] | 1823 | link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list); |
| 1824 | if (l == &link->cg->tasks) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1825 | /* We reached the end of this task list - move on to |
| 1826 | * the next cg_cgroup_link */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1827 | cgroup_advance_iter(cgrp, it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1828 | } else { |
| 1829 | it->task = l; |
| 1830 | } |
| 1831 | return res; |
| 1832 | } |
| 1833 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1834 | void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1835 | { |
| 1836 | read_unlock(&css_set_lock); |
| 1837 | } |
| 1838 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 1839 | static inline int started_after_time(struct task_struct *t1, |
| 1840 | struct timespec *time, |
| 1841 | struct task_struct *t2) |
| 1842 | { |
| 1843 | int start_diff = timespec_compare(&t1->start_time, time); |
| 1844 | if (start_diff > 0) { |
| 1845 | return 1; |
| 1846 | } else if (start_diff < 0) { |
| 1847 | return 0; |
| 1848 | } else { |
| 1849 | /* |
| 1850 | * Arbitrarily, if two processes started at the same |
| 1851 | * time, we'll say that the lower pointer value |
| 1852 | * started first. Note that t2 may have exited by now |
| 1853 | * so this may not be a valid pointer any longer, but |
| 1854 | * that's fine - it still serves to distinguish |
| 1855 | * between two tasks started (effectively) simultaneously. |
| 1856 | */ |
| 1857 | return t1 > t2; |
| 1858 | } |
| 1859 | } |
| 1860 | |
| 1861 | /* |
| 1862 | * This function is a callback from heap_insert() and is used to order |
| 1863 | * the heap. |
| 1864 | * In this case we order the heap in descending task start time. |
| 1865 | */ |
| 1866 | static inline int started_after(void *p1, void *p2) |
| 1867 | { |
| 1868 | struct task_struct *t1 = p1; |
| 1869 | struct task_struct *t2 = p2; |
| 1870 | return started_after_time(t1, &t2->start_time, t2); |
| 1871 | } |
| 1872 | |
| 1873 | /** |
| 1874 | * cgroup_scan_tasks - iterate though all the tasks in a cgroup |
| 1875 | * @scan: struct cgroup_scanner containing arguments for the scan |
| 1876 | * |
| 1877 | * Arguments include pointers to callback functions test_task() and |
| 1878 | * process_task(). |
| 1879 | * Iterate through all the tasks in a cgroup, calling test_task() for each, |
| 1880 | * and if it returns true, call process_task() for it also. |
| 1881 | * The test_task pointer may be NULL, meaning always true (select all tasks). |
| 1882 | * Effectively duplicates cgroup_iter_{start,next,end}() |
| 1883 | * but does not lock css_set_lock for the call to process_task(). |
| 1884 | * The struct cgroup_scanner may be embedded in any structure of the caller's |
| 1885 | * creation. |
| 1886 | * It is guaranteed that process_task() will act on every task that |
| 1887 | * is a member of the cgroup for the duration of this call. This |
| 1888 | * function may or may not call process_task() for tasks that exit |
| 1889 | * or move to a different cgroup during the call, or are forked or |
| 1890 | * move into the cgroup during the call. |
| 1891 | * |
| 1892 | * Note that test_task() may be called with locks held, and may in some |
| 1893 | * situations be called multiple times for the same task, so it should |
| 1894 | * be cheap. |
| 1895 | * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been |
| 1896 | * pre-allocated and will be used for heap operations (and its "gt" member will |
| 1897 | * be overwritten), else a temporary heap will be used (allocation of which |
| 1898 | * may cause this function to fail). |
| 1899 | */ |
| 1900 | int cgroup_scan_tasks(struct cgroup_scanner *scan) |
| 1901 | { |
| 1902 | int retval, i; |
| 1903 | struct cgroup_iter it; |
| 1904 | struct task_struct *p, *dropped; |
| 1905 | /* Never dereference latest_task, since it's not refcounted */ |
| 1906 | struct task_struct *latest_task = NULL; |
| 1907 | struct ptr_heap tmp_heap; |
| 1908 | struct ptr_heap *heap; |
| 1909 | struct timespec latest_time = { 0, 0 }; |
| 1910 | |
| 1911 | if (scan->heap) { |
| 1912 | /* The caller supplied our heap and pre-allocated its memory */ |
| 1913 | heap = scan->heap; |
| 1914 | heap->gt = &started_after; |
| 1915 | } else { |
| 1916 | /* We need to allocate our own heap memory */ |
| 1917 | heap = &tmp_heap; |
| 1918 | retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after); |
| 1919 | if (retval) |
| 1920 | /* cannot allocate the heap */ |
| 1921 | return retval; |
| 1922 | } |
| 1923 | |
| 1924 | again: |
| 1925 | /* |
| 1926 | * Scan tasks in the cgroup, using the scanner's "test_task" callback |
| 1927 | * to determine which are of interest, and using the scanner's |
| 1928 | * "process_task" callback to process any of them that need an update. |
| 1929 | * Since we don't want to hold any locks during the task updates, |
| 1930 | * gather tasks to be processed in a heap structure. |
| 1931 | * The heap is sorted by descending task start time. |
| 1932 | * If the statically-sized heap fills up, we overflow tasks that |
| 1933 | * started later, and in future iterations only consider tasks that |
| 1934 | * started after the latest task in the previous pass. This |
| 1935 | * guarantees forward progress and that we don't miss any tasks. |
| 1936 | */ |
| 1937 | heap->size = 0; |
| 1938 | cgroup_iter_start(scan->cg, &it); |
| 1939 | while ((p = cgroup_iter_next(scan->cg, &it))) { |
| 1940 | /* |
| 1941 | * Only affect tasks that qualify per the caller's callback, |
| 1942 | * if he provided one |
| 1943 | */ |
| 1944 | if (scan->test_task && !scan->test_task(p, scan)) |
| 1945 | continue; |
| 1946 | /* |
| 1947 | * Only process tasks that started after the last task |
| 1948 | * we processed |
| 1949 | */ |
| 1950 | if (!started_after_time(p, &latest_time, latest_task)) |
| 1951 | continue; |
| 1952 | dropped = heap_insert(heap, p); |
| 1953 | if (dropped == NULL) { |
| 1954 | /* |
| 1955 | * The new task was inserted; the heap wasn't |
| 1956 | * previously full |
| 1957 | */ |
| 1958 | get_task_struct(p); |
| 1959 | } else if (dropped != p) { |
| 1960 | /* |
| 1961 | * The new task was inserted, and pushed out a |
| 1962 | * different task |
| 1963 | */ |
| 1964 | get_task_struct(p); |
| 1965 | put_task_struct(dropped); |
| 1966 | } |
| 1967 | /* |
| 1968 | * Else the new task was newer than anything already in |
| 1969 | * the heap and wasn't inserted |
| 1970 | */ |
| 1971 | } |
| 1972 | cgroup_iter_end(scan->cg, &it); |
| 1973 | |
| 1974 | if (heap->size) { |
| 1975 | for (i = 0; i < heap->size; i++) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 1976 | struct task_struct *q = heap->ptrs[i]; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 1977 | if (i == 0) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 1978 | latest_time = q->start_time; |
| 1979 | latest_task = q; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 1980 | } |
| 1981 | /* Process the task per the caller's callback */ |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 1982 | scan->process_task(q, scan); |
| 1983 | put_task_struct(q); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 1984 | } |
| 1985 | /* |
| 1986 | * If we had to process any tasks at all, scan again |
| 1987 | * in case some of them were in the middle of forking |
| 1988 | * children that didn't get processed. |
| 1989 | * Not the most efficient way to do it, but it avoids |
| 1990 | * having to take callback_mutex in the fork path |
| 1991 | */ |
| 1992 | goto again; |
| 1993 | } |
| 1994 | if (heap == &tmp_heap) |
| 1995 | heap_free(&tmp_heap); |
| 1996 | return 0; |
| 1997 | } |
| 1998 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1999 | /* |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2000 | * Stuff for reading the 'tasks' file. |
| 2001 | * |
| 2002 | * Reading this file can return large amounts of data if a cgroup has |
| 2003 | * *lots* of attached tasks. So it may need several calls to read(), |
| 2004 | * but we cannot guarantee that the information we produce is correct |
| 2005 | * unless we produce it entirely atomically. |
| 2006 | * |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2007 | */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2008 | |
| 2009 | /* |
| 2010 | * Load into 'pidarray' up to 'npids' of the tasks using cgroup |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2011 | * 'cgrp'. Return actual number of pids loaded. No need to |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2012 | * task_lock(p) when reading out p->cgroup, since we're in an RCU |
| 2013 | * read section, so the css_set can't go away, and is |
| 2014 | * immutable after creation. |
| 2015 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2016 | static int pid_array_load(pid_t *pidarray, int npids, struct cgroup *cgrp) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2017 | { |
| 2018 | int n = 0; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2019 | struct cgroup_iter it; |
| 2020 | struct task_struct *tsk; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2021 | cgroup_iter_start(cgrp, &it); |
| 2022 | while ((tsk = cgroup_iter_next(cgrp, &it))) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2023 | if (unlikely(n == npids)) |
| 2024 | break; |
Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2025 | pidarray[n++] = task_pid_vnr(tsk); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2026 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2027 | cgroup_iter_end(cgrp, &it); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2028 | return n; |
| 2029 | } |
| 2030 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 2031 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2032 | * cgroupstats_build - build and fill cgroupstats |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 2033 | * @stats: cgroupstats to fill information into |
| 2034 | * @dentry: A dentry entry belonging to the cgroup for which stats have |
| 2035 | * been requested. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2036 | * |
| 2037 | * Build and fill cgroupstats so that taskstats can export it to user |
| 2038 | * space. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 2039 | */ |
| 2040 | int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) |
| 2041 | { |
| 2042 | int ret = -EINVAL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2043 | struct cgroup *cgrp; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 2044 | struct cgroup_iter it; |
| 2045 | struct task_struct *tsk; |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 2046 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 2047 | /* |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 2048 | * Validate dentry by checking the superblock operations, |
| 2049 | * and make sure it's a directory. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 2050 | */ |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 2051 | if (dentry->d_sb->s_op != &cgroup_ops || |
| 2052 | !S_ISDIR(dentry->d_inode->i_mode)) |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 2053 | goto err; |
| 2054 | |
| 2055 | ret = 0; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2056 | cgrp = dentry->d_fsdata; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 2057 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2058 | cgroup_iter_start(cgrp, &it); |
| 2059 | while ((tsk = cgroup_iter_next(cgrp, &it))) { |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 2060 | switch (tsk->state) { |
| 2061 | case TASK_RUNNING: |
| 2062 | stats->nr_running++; |
| 2063 | break; |
| 2064 | case TASK_INTERRUPTIBLE: |
| 2065 | stats->nr_sleeping++; |
| 2066 | break; |
| 2067 | case TASK_UNINTERRUPTIBLE: |
| 2068 | stats->nr_uninterruptible++; |
| 2069 | break; |
| 2070 | case TASK_STOPPED: |
| 2071 | stats->nr_stopped++; |
| 2072 | break; |
| 2073 | default: |
| 2074 | if (delayacct_is_task_waiting_on_io(tsk)) |
| 2075 | stats->nr_io_wait++; |
| 2076 | break; |
| 2077 | } |
| 2078 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2079 | cgroup_iter_end(cgrp, &it); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 2080 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 2081 | err: |
| 2082 | return ret; |
| 2083 | } |
| 2084 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2085 | static int cmppid(const void *a, const void *b) |
| 2086 | { |
| 2087 | return *(pid_t *)a - *(pid_t *)b; |
| 2088 | } |
| 2089 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2090 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2091 | /* |
| 2092 | * seq_file methods for the "tasks" file. The seq_file position is the |
| 2093 | * next pid to display; the seq_file iterator is a pointer to the pid |
| 2094 | * in the cgroup->tasks_pids array. |
| 2095 | */ |
| 2096 | |
| 2097 | static void *cgroup_tasks_start(struct seq_file *s, loff_t *pos) |
| 2098 | { |
| 2099 | /* |
| 2100 | * Initially we receive a position value that corresponds to |
| 2101 | * one more than the last pid shown (or 0 on the first call or |
| 2102 | * after a seek to the start). Use a binary-search to find the |
| 2103 | * next pid to display, if any |
| 2104 | */ |
| 2105 | struct cgroup *cgrp = s->private; |
| 2106 | int index = 0, pid = *pos; |
| 2107 | int *iter; |
| 2108 | |
| 2109 | down_read(&cgrp->pids_mutex); |
| 2110 | if (pid) { |
| 2111 | int end = cgrp->pids_length; |
Stephen Rothwell | 2077776 | 2008-10-21 16:11:20 +1100 | [diff] [blame] | 2112 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2113 | while (index < end) { |
| 2114 | int mid = (index + end) / 2; |
| 2115 | if (cgrp->tasks_pids[mid] == pid) { |
| 2116 | index = mid; |
| 2117 | break; |
| 2118 | } else if (cgrp->tasks_pids[mid] <= pid) |
| 2119 | index = mid + 1; |
| 2120 | else |
| 2121 | end = mid; |
| 2122 | } |
| 2123 | } |
| 2124 | /* If we're off the end of the array, we're done */ |
| 2125 | if (index >= cgrp->pids_length) |
| 2126 | return NULL; |
| 2127 | /* Update the abstract position to be the actual pid that we found */ |
| 2128 | iter = cgrp->tasks_pids + index; |
| 2129 | *pos = *iter; |
| 2130 | return iter; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2131 | } |
| 2132 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2133 | static void cgroup_tasks_stop(struct seq_file *s, void *v) |
| 2134 | { |
| 2135 | struct cgroup *cgrp = s->private; |
| 2136 | up_read(&cgrp->pids_mutex); |
| 2137 | } |
| 2138 | |
| 2139 | static void *cgroup_tasks_next(struct seq_file *s, void *v, loff_t *pos) |
| 2140 | { |
| 2141 | struct cgroup *cgrp = s->private; |
| 2142 | int *p = v; |
| 2143 | int *end = cgrp->tasks_pids + cgrp->pids_length; |
| 2144 | |
| 2145 | /* |
| 2146 | * Advance to the next pid in the array. If this goes off the |
| 2147 | * end, we're done |
| 2148 | */ |
| 2149 | p++; |
| 2150 | if (p >= end) { |
| 2151 | return NULL; |
| 2152 | } else { |
| 2153 | *pos = *p; |
| 2154 | return p; |
| 2155 | } |
| 2156 | } |
| 2157 | |
| 2158 | static int cgroup_tasks_show(struct seq_file *s, void *v) |
| 2159 | { |
| 2160 | return seq_printf(s, "%d\n", *(int *)v); |
| 2161 | } |
| 2162 | |
| 2163 | static struct seq_operations cgroup_tasks_seq_operations = { |
| 2164 | .start = cgroup_tasks_start, |
| 2165 | .stop = cgroup_tasks_stop, |
| 2166 | .next = cgroup_tasks_next, |
| 2167 | .show = cgroup_tasks_show, |
| 2168 | }; |
| 2169 | |
| 2170 | static void release_cgroup_pid_array(struct cgroup *cgrp) |
| 2171 | { |
| 2172 | down_write(&cgrp->pids_mutex); |
| 2173 | BUG_ON(!cgrp->pids_use_count); |
| 2174 | if (!--cgrp->pids_use_count) { |
| 2175 | kfree(cgrp->tasks_pids); |
| 2176 | cgrp->tasks_pids = NULL; |
| 2177 | cgrp->pids_length = 0; |
| 2178 | } |
| 2179 | up_write(&cgrp->pids_mutex); |
| 2180 | } |
| 2181 | |
| 2182 | static int cgroup_tasks_release(struct inode *inode, struct file *file) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2183 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2184 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2185 | |
| 2186 | if (!(file->f_mode & FMODE_READ)) |
| 2187 | return 0; |
| 2188 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2189 | release_cgroup_pid_array(cgrp); |
| 2190 | return seq_release(inode, file); |
| 2191 | } |
| 2192 | |
| 2193 | static struct file_operations cgroup_tasks_operations = { |
| 2194 | .read = seq_read, |
| 2195 | .llseek = seq_lseek, |
| 2196 | .write = cgroup_file_write, |
| 2197 | .release = cgroup_tasks_release, |
| 2198 | }; |
| 2199 | |
| 2200 | /* |
| 2201 | * Handle an open on 'tasks' file. Prepare an array containing the |
| 2202 | * process id's of tasks currently attached to the cgroup being opened. |
| 2203 | */ |
| 2204 | |
| 2205 | static int cgroup_tasks_open(struct inode *unused, struct file *file) |
| 2206 | { |
| 2207 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
| 2208 | pid_t *pidarray; |
| 2209 | int npids; |
| 2210 | int retval; |
| 2211 | |
| 2212 | /* Nothing to do for write-only files */ |
| 2213 | if (!(file->f_mode & FMODE_READ)) |
| 2214 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2215 | |
| 2216 | /* |
| 2217 | * If cgroup gets more users after we read count, we won't have |
| 2218 | * enough space - tough. This race is indistinguishable to the |
| 2219 | * caller from the case that the additional cgroup users didn't |
| 2220 | * show up until sometime later on. |
| 2221 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2222 | npids = cgroup_task_count(cgrp); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2223 | pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL); |
| 2224 | if (!pidarray) |
| 2225 | return -ENOMEM; |
| 2226 | npids = pid_array_load(pidarray, npids, cgrp); |
| 2227 | sort(pidarray, npids, sizeof(pid_t), cmppid, NULL); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2228 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2229 | /* |
| 2230 | * Store the array in the cgroup, freeing the old |
| 2231 | * array if necessary |
| 2232 | */ |
| 2233 | down_write(&cgrp->pids_mutex); |
| 2234 | kfree(cgrp->tasks_pids); |
| 2235 | cgrp->tasks_pids = pidarray; |
| 2236 | cgrp->pids_length = npids; |
| 2237 | cgrp->pids_use_count++; |
| 2238 | up_write(&cgrp->pids_mutex); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2239 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2240 | file->f_op = &cgroup_tasks_operations; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2241 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2242 | retval = seq_open(file, &cgroup_tasks_seq_operations); |
| 2243 | if (retval) { |
| 2244 | release_cgroup_pid_array(cgrp); |
| 2245 | return retval; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2246 | } |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2247 | ((struct seq_file *)file->private_data)->private = cgrp; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2248 | return 0; |
| 2249 | } |
| 2250 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2251 | static u64 cgroup_read_notify_on_release(struct cgroup *cgrp, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2252 | struct cftype *cft) |
| 2253 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2254 | return notify_on_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2255 | } |
| 2256 | |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2257 | static int cgroup_write_notify_on_release(struct cgroup *cgrp, |
| 2258 | struct cftype *cft, |
| 2259 | u64 val) |
| 2260 | { |
| 2261 | clear_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 2262 | if (val) |
| 2263 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 2264 | else |
| 2265 | clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 2266 | return 0; |
| 2267 | } |
| 2268 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2269 | /* |
| 2270 | * for the common functions, 'private' gives the type of file |
| 2271 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2272 | static struct cftype files[] = { |
| 2273 | { |
| 2274 | .name = "tasks", |
| 2275 | .open = cgroup_tasks_open, |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2276 | .write_u64 = cgroup_tasks_write, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2277 | .release = cgroup_tasks_release, |
| 2278 | .private = FILE_TASKLIST, |
| 2279 | }, |
| 2280 | |
| 2281 | { |
| 2282 | .name = "notify_on_release", |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2283 | .read_u64 = cgroup_read_notify_on_release, |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2284 | .write_u64 = cgroup_write_notify_on_release, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2285 | .private = FILE_NOTIFY_ON_RELEASE, |
| 2286 | }, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2287 | }; |
| 2288 | |
| 2289 | static struct cftype cft_release_agent = { |
| 2290 | .name = "release_agent", |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2291 | .read_seq_string = cgroup_release_agent_show, |
| 2292 | .write_string = cgroup_release_agent_write, |
| 2293 | .max_write_len = PATH_MAX, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2294 | .private = FILE_RELEASE_AGENT, |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2295 | }; |
| 2296 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2297 | static int cgroup_populate_dir(struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2298 | { |
| 2299 | int err; |
| 2300 | struct cgroup_subsys *ss; |
| 2301 | |
| 2302 | /* First clear out any existing files */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2303 | cgroup_clear_directory(cgrp->dentry); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2304 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2305 | err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files)); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2306 | if (err < 0) |
| 2307 | return err; |
| 2308 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2309 | if (cgrp == cgrp->top_cgroup) { |
| 2310 | if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2311 | return err; |
| 2312 | } |
| 2313 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2314 | for_each_subsys(cgrp->root, ss) { |
| 2315 | if (ss->populate && (err = ss->populate(ss, cgrp)) < 0) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2316 | return err; |
| 2317 | } |
| 2318 | |
| 2319 | return 0; |
| 2320 | } |
| 2321 | |
| 2322 | static void init_cgroup_css(struct cgroup_subsys_state *css, |
| 2323 | struct cgroup_subsys *ss, |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2324 | struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2325 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2326 | css->cgroup = cgrp; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2327 | atomic_set(&css->refcnt, 0); |
| 2328 | css->flags = 0; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2329 | if (cgrp == dummytop) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2330 | set_bit(CSS_ROOT, &css->flags); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2331 | BUG_ON(cgrp->subsys[ss->subsys_id]); |
| 2332 | cgrp->subsys[ss->subsys_id] = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2333 | } |
| 2334 | |
| 2335 | /* |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2336 | * cgroup_create - create a cgroup |
| 2337 | * @parent: cgroup that will be parent of the new cgroup |
| 2338 | * @dentry: dentry of the new cgroup |
| 2339 | * @mode: mode to set on new inode |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2340 | * |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2341 | * Must be called with the mutex on the parent inode held |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2342 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2343 | static long cgroup_create(struct cgroup *parent, struct dentry *dentry, |
| 2344 | int mode) |
| 2345 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2346 | struct cgroup *cgrp; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2347 | struct cgroupfs_root *root = parent->root; |
| 2348 | int err = 0; |
| 2349 | struct cgroup_subsys *ss; |
| 2350 | struct super_block *sb = root->sb; |
| 2351 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2352 | cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL); |
| 2353 | if (!cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2354 | return -ENOMEM; |
| 2355 | |
| 2356 | /* Grab a reference on the superblock so the hierarchy doesn't |
| 2357 | * get deleted on unmount if there are child cgroups. This |
| 2358 | * can be done outside cgroup_mutex, since the sb can't |
| 2359 | * disappear while someone has an open control file on the |
| 2360 | * fs */ |
| 2361 | atomic_inc(&sb->s_active); |
| 2362 | |
| 2363 | mutex_lock(&cgroup_mutex); |
| 2364 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 2365 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2366 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2367 | cgrp->parent = parent; |
| 2368 | cgrp->root = parent->root; |
| 2369 | cgrp->top_cgroup = parent->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2370 | |
Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 2371 | if (notify_on_release(parent)) |
| 2372 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 2373 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2374 | for_each_subsys(root, ss) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2375 | struct cgroup_subsys_state *css = ss->create(ss, cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2376 | if (IS_ERR(css)) { |
| 2377 | err = PTR_ERR(css); |
| 2378 | goto err_destroy; |
| 2379 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2380 | init_cgroup_css(css, ss, cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2381 | } |
| 2382 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2383 | list_add(&cgrp->sibling, &cgrp->parent->children); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2384 | root->number_of_cgroups++; |
| 2385 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2386 | err = cgroup_create_dir(cgrp, dentry, mode); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2387 | if (err < 0) |
| 2388 | goto err_remove; |
| 2389 | |
| 2390 | /* The cgroup directory was pre-locked for us */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2391 | BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2392 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2393 | err = cgroup_populate_dir(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2394 | /* If err < 0, we have a half-filled directory - oh well ;) */ |
| 2395 | |
| 2396 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2397 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2398 | |
| 2399 | return 0; |
| 2400 | |
| 2401 | err_remove: |
| 2402 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2403 | list_del(&cgrp->sibling); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2404 | root->number_of_cgroups--; |
| 2405 | |
| 2406 | err_destroy: |
| 2407 | |
| 2408 | for_each_subsys(root, ss) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2409 | if (cgrp->subsys[ss->subsys_id]) |
| 2410 | ss->destroy(ss, cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2411 | } |
| 2412 | |
| 2413 | mutex_unlock(&cgroup_mutex); |
| 2414 | |
| 2415 | /* Release the reference count that we took on the superblock */ |
| 2416 | deactivate_super(sb); |
| 2417 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2418 | kfree(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2419 | return err; |
| 2420 | } |
| 2421 | |
| 2422 | static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 2423 | { |
| 2424 | struct cgroup *c_parent = dentry->d_parent->d_fsdata; |
| 2425 | |
| 2426 | /* the vfs holds inode->i_mutex already */ |
| 2427 | return cgroup_create(c_parent, dentry, mode | S_IFDIR); |
| 2428 | } |
| 2429 | |
Li Zefan | 55b6fd0 | 2008-07-29 22:33:20 -0700 | [diff] [blame] | 2430 | static int cgroup_has_css_refs(struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2431 | { |
| 2432 | /* Check the reference count on each subsystem. Since we |
| 2433 | * already established that there are no tasks in the |
| 2434 | * cgroup, if the css refcount is also 0, then there should |
| 2435 | * be no outstanding references, so the subsystem is safe to |
| 2436 | * destroy. We scan across all subsystems rather than using |
| 2437 | * the per-hierarchy linked list of mounted subsystems since |
| 2438 | * we can be called via check_for_release() with no |
| 2439 | * synchronization other than RCU, and the subsystem linked |
| 2440 | * list isn't RCU-safe */ |
| 2441 | int i; |
| 2442 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 2443 | struct cgroup_subsys *ss = subsys[i]; |
| 2444 | struct cgroup_subsys_state *css; |
| 2445 | /* Skip subsystems not in this hierarchy */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2446 | if (ss->root != cgrp->root) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2447 | continue; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2448 | css = cgrp->subsys[ss->subsys_id]; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2449 | /* When called from check_for_release() it's possible |
| 2450 | * that by this point the cgroup has been removed |
| 2451 | * and the css deleted. But a false-positive doesn't |
| 2452 | * matter, since it can only happen if the cgroup |
| 2453 | * has been deleted and hence no longer needs the |
| 2454 | * release agent to be called anyway. */ |
Paul Jackson | e18f631 | 2008-02-07 00:13:44 -0800 | [diff] [blame] | 2455 | if (css && atomic_read(&css->refcnt)) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2456 | return 1; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2457 | } |
| 2458 | return 0; |
| 2459 | } |
| 2460 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2461 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry) |
| 2462 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2463 | struct cgroup *cgrp = dentry->d_fsdata; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2464 | struct dentry *d; |
| 2465 | struct cgroup *parent; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2466 | |
| 2467 | /* the vfs holds both inode->i_mutex already */ |
| 2468 | |
| 2469 | mutex_lock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2470 | if (atomic_read(&cgrp->count) != 0) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2471 | mutex_unlock(&cgroup_mutex); |
| 2472 | return -EBUSY; |
| 2473 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2474 | if (!list_empty(&cgrp->children)) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2475 | mutex_unlock(&cgroup_mutex); |
| 2476 | return -EBUSY; |
| 2477 | } |
KAMEZAWA Hiroyuki | 3fa59df | 2008-11-19 15:36:34 -0800 | [diff] [blame] | 2478 | mutex_unlock(&cgroup_mutex); |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2479 | |
KAMEZAWA Hiroyuki | 4fca88c | 2008-02-07 00:14:27 -0800 | [diff] [blame] | 2480 | /* |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2481 | * Call pre_destroy handlers of subsys. Notify subsystems |
| 2482 | * that rmdir() request comes. |
KAMEZAWA Hiroyuki | 4fca88c | 2008-02-07 00:14:27 -0800 | [diff] [blame] | 2483 | */ |
| 2484 | cgroup_call_pre_destroy(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2485 | |
KAMEZAWA Hiroyuki | 3fa59df | 2008-11-19 15:36:34 -0800 | [diff] [blame] | 2486 | mutex_lock(&cgroup_mutex); |
| 2487 | parent = cgrp->parent; |
KAMEZAWA Hiroyuki | 3fa59df | 2008-11-19 15:36:34 -0800 | [diff] [blame] | 2488 | |
| 2489 | if (atomic_read(&cgrp->count) |
| 2490 | || !list_empty(&cgrp->children) |
| 2491 | || cgroup_has_css_refs(cgrp)) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2492 | mutex_unlock(&cgroup_mutex); |
| 2493 | return -EBUSY; |
| 2494 | } |
| 2495 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2496 | spin_lock(&release_list_lock); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2497 | set_bit(CGRP_REMOVED, &cgrp->flags); |
| 2498 | if (!list_empty(&cgrp->release_list)) |
| 2499 | list_del(&cgrp->release_list); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2500 | spin_unlock(&release_list_lock); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2501 | /* delete my sibling from parent->children */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2502 | list_del(&cgrp->sibling); |
| 2503 | spin_lock(&cgrp->dentry->d_lock); |
| 2504 | d = dget(cgrp->dentry); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2505 | spin_unlock(&d->d_lock); |
| 2506 | |
| 2507 | cgroup_d_remove_dir(d); |
| 2508 | dput(d); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2509 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2510 | set_bit(CGRP_RELEASABLE, &parent->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2511 | check_for_release(parent); |
| 2512 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2513 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2514 | return 0; |
| 2515 | } |
| 2516 | |
Li Zefan | 06a1192 | 2008-04-29 01:00:07 -0700 | [diff] [blame] | 2517 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2518 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2519 | struct cgroup_subsys_state *css; |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 2520 | |
| 2521 | printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2522 | |
| 2523 | /* Create the top cgroup state for this subsystem */ |
| 2524 | ss->root = &rootnode; |
| 2525 | css = ss->create(ss, dummytop); |
| 2526 | /* We don't handle early failures gracefully */ |
| 2527 | BUG_ON(IS_ERR(css)); |
| 2528 | init_cgroup_css(css, ss, dummytop); |
| 2529 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 2530 | /* Update the init_css_set to contain a subsys |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2531 | * pointer to this state - since the subsystem is |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 2532 | * newly registered, all tasks and hence the |
| 2533 | * init_css_set is in the subsystem's top cgroup. */ |
| 2534 | init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2535 | |
| 2536 | need_forkexit_callback |= ss->fork || ss->exit; |
| 2537 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 2538 | /* At system boot, before all subsystems have been |
| 2539 | * registered, no tasks have been forked, so we don't |
| 2540 | * need to invoke fork callbacks here. */ |
| 2541 | BUG_ON(!list_empty(&init_task.tasks)); |
| 2542 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2543 | ss->active = 1; |
| 2544 | } |
| 2545 | |
| 2546 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2547 | * cgroup_init_early - cgroup initialization at system boot |
| 2548 | * |
| 2549 | * Initialize cgroups at system boot, and initialize any |
| 2550 | * subsystems that request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2551 | */ |
| 2552 | int __init cgroup_init_early(void) |
| 2553 | { |
| 2554 | int i; |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 2555 | atomic_set(&init_css_set.refcount, 1); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2556 | INIT_LIST_HEAD(&init_css_set.cg_links); |
| 2557 | INIT_LIST_HEAD(&init_css_set.tasks); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 2558 | INIT_HLIST_NODE(&init_css_set.hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2559 | css_set_count = 1; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2560 | init_cgroup_root(&rootnode); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2561 | root_count = 1; |
| 2562 | init_task.cgroups = &init_css_set; |
| 2563 | |
| 2564 | init_css_set_link.cg = &init_css_set; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2565 | list_add(&init_css_set_link.cgrp_link_list, |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2566 | &rootnode.top_cgroup.css_sets); |
| 2567 | list_add(&init_css_set_link.cg_link_list, |
| 2568 | &init_css_set.cg_links); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2569 | |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 2570 | for (i = 0; i < CSS_SET_TABLE_SIZE; i++) |
| 2571 | INIT_HLIST_HEAD(&css_set_table[i]); |
| 2572 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2573 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 2574 | struct cgroup_subsys *ss = subsys[i]; |
| 2575 | |
| 2576 | BUG_ON(!ss->name); |
| 2577 | BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN); |
| 2578 | BUG_ON(!ss->create); |
| 2579 | BUG_ON(!ss->destroy); |
| 2580 | if (ss->subsys_id != i) { |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 2581 | printk(KERN_ERR "cgroup: Subsys %s id == %d\n", |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2582 | ss->name, ss->subsys_id); |
| 2583 | BUG(); |
| 2584 | } |
| 2585 | |
| 2586 | if (ss->early_init) |
| 2587 | cgroup_init_subsys(ss); |
| 2588 | } |
| 2589 | return 0; |
| 2590 | } |
| 2591 | |
| 2592 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2593 | * cgroup_init - cgroup initialization |
| 2594 | * |
| 2595 | * Register cgroup filesystem and /proc file, and initialize |
| 2596 | * any subsystems that didn't request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2597 | */ |
| 2598 | int __init cgroup_init(void) |
| 2599 | { |
| 2600 | int err; |
| 2601 | int i; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 2602 | struct hlist_head *hhead; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 2603 | |
| 2604 | err = bdi_init(&cgroup_backing_dev_info); |
| 2605 | if (err) |
| 2606 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2607 | |
| 2608 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 2609 | struct cgroup_subsys *ss = subsys[i]; |
| 2610 | if (!ss->early_init) |
| 2611 | cgroup_init_subsys(ss); |
| 2612 | } |
| 2613 | |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 2614 | /* Add init_css_set to the hash table */ |
| 2615 | hhead = css_set_hash(init_css_set.subsys); |
| 2616 | hlist_add_head(&init_css_set.hlist, hhead); |
| 2617 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2618 | err = register_filesystem(&cgroup_fs_type); |
| 2619 | if (err < 0) |
| 2620 | goto out; |
| 2621 | |
Li Zefan | 46ae220 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 2622 | proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 2623 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2624 | out: |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 2625 | if (err) |
| 2626 | bdi_destroy(&cgroup_backing_dev_info); |
| 2627 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2628 | return err; |
| 2629 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2630 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 2631 | /* |
| 2632 | * proc_cgroup_show() |
| 2633 | * - Print task's cgroup paths into seq_file, one line for each hierarchy |
| 2634 | * - Used for /proc/<pid>/cgroup. |
| 2635 | * - No need to task_lock(tsk) on this tsk->cgroup reference, as it |
| 2636 | * doesn't really matter if tsk->cgroup changes after we read it, |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 2637 | * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 2638 | * anyway. No need to check that tsk->cgroup != NULL, thanks to |
| 2639 | * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks |
| 2640 | * cgroup to top_cgroup. |
| 2641 | */ |
| 2642 | |
| 2643 | /* TODO: Use a proper seq_file iterator */ |
| 2644 | static int proc_cgroup_show(struct seq_file *m, void *v) |
| 2645 | { |
| 2646 | struct pid *pid; |
| 2647 | struct task_struct *tsk; |
| 2648 | char *buf; |
| 2649 | int retval; |
| 2650 | struct cgroupfs_root *root; |
| 2651 | |
| 2652 | retval = -ENOMEM; |
| 2653 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 2654 | if (!buf) |
| 2655 | goto out; |
| 2656 | |
| 2657 | retval = -ESRCH; |
| 2658 | pid = m->private; |
| 2659 | tsk = get_pid_task(pid, PIDTYPE_PID); |
| 2660 | if (!tsk) |
| 2661 | goto out_free; |
| 2662 | |
| 2663 | retval = 0; |
| 2664 | |
| 2665 | mutex_lock(&cgroup_mutex); |
| 2666 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame^] | 2667 | for_each_active_root(root) { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 2668 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2669 | struct cgroup *cgrp; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 2670 | int subsys_id; |
| 2671 | int count = 0; |
| 2672 | |
Paul Menage | b6c3006 | 2008-04-10 21:29:16 -0700 | [diff] [blame] | 2673 | seq_printf(m, "%lu:", root->subsys_bits); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 2674 | for_each_subsys(root, ss) |
| 2675 | seq_printf(m, "%s%s", count++ ? "," : "", ss->name); |
| 2676 | seq_putc(m, ':'); |
| 2677 | get_first_subsys(&root->top_cgroup, NULL, &subsys_id); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2678 | cgrp = task_cgroup(tsk, subsys_id); |
| 2679 | retval = cgroup_path(cgrp, buf, PAGE_SIZE); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 2680 | if (retval < 0) |
| 2681 | goto out_unlock; |
| 2682 | seq_puts(m, buf); |
| 2683 | seq_putc(m, '\n'); |
| 2684 | } |
| 2685 | |
| 2686 | out_unlock: |
| 2687 | mutex_unlock(&cgroup_mutex); |
| 2688 | put_task_struct(tsk); |
| 2689 | out_free: |
| 2690 | kfree(buf); |
| 2691 | out: |
| 2692 | return retval; |
| 2693 | } |
| 2694 | |
| 2695 | static int cgroup_open(struct inode *inode, struct file *file) |
| 2696 | { |
| 2697 | struct pid *pid = PROC_I(inode)->pid; |
| 2698 | return single_open(file, proc_cgroup_show, pid); |
| 2699 | } |
| 2700 | |
| 2701 | struct file_operations proc_cgroup_operations = { |
| 2702 | .open = cgroup_open, |
| 2703 | .read = seq_read, |
| 2704 | .llseek = seq_lseek, |
| 2705 | .release = single_release, |
| 2706 | }; |
| 2707 | |
| 2708 | /* Display information about each subsystem and each hierarchy */ |
| 2709 | static int proc_cgroupstats_show(struct seq_file *m, void *v) |
| 2710 | { |
| 2711 | int i; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 2712 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 2713 | seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n"); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 2714 | mutex_lock(&cgroup_mutex); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 2715 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 2716 | struct cgroup_subsys *ss = subsys[i]; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 2717 | seq_printf(m, "%s\t%lu\t%d\t%d\n", |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2718 | ss->name, ss->root->subsys_bits, |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 2719 | ss->root->number_of_cgroups, !ss->disabled); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 2720 | } |
| 2721 | mutex_unlock(&cgroup_mutex); |
| 2722 | return 0; |
| 2723 | } |
| 2724 | |
| 2725 | static int cgroupstats_open(struct inode *inode, struct file *file) |
| 2726 | { |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 2727 | return single_open(file, proc_cgroupstats_show, NULL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 2728 | } |
| 2729 | |
| 2730 | static struct file_operations proc_cgroupstats_operations = { |
| 2731 | .open = cgroupstats_open, |
| 2732 | .read = seq_read, |
| 2733 | .llseek = seq_lseek, |
| 2734 | .release = single_release, |
| 2735 | }; |
| 2736 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2737 | /** |
| 2738 | * cgroup_fork - attach newly forked task to its parents cgroup. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2739 | * @child: pointer to task_struct of forking parent process. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2740 | * |
| 2741 | * Description: A task inherits its parent's cgroup at fork(). |
| 2742 | * |
| 2743 | * A pointer to the shared css_set was automatically copied in |
| 2744 | * fork.c by dup_task_struct(). However, we ignore that copy, since |
| 2745 | * 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] | 2746 | * might no longer be a valid cgroup pointer. cgroup_attach_task() might |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2747 | * have already changed current->cgroups, allowing the previously |
| 2748 | * referenced cgroup group to be removed and freed. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2749 | * |
| 2750 | * At the point that cgroup_fork() is called, 'current' is the parent |
| 2751 | * task, and the passed argument 'child' points to the child task. |
| 2752 | */ |
| 2753 | void cgroup_fork(struct task_struct *child) |
| 2754 | { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2755 | task_lock(current); |
| 2756 | child->cgroups = current->cgroups; |
| 2757 | get_css_set(child->cgroups); |
| 2758 | task_unlock(current); |
| 2759 | INIT_LIST_HEAD(&child->cg_list); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2760 | } |
| 2761 | |
| 2762 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2763 | * cgroup_fork_callbacks - run fork callbacks |
| 2764 | * @child: the new task |
| 2765 | * |
| 2766 | * Called on a new task very soon before adding it to the |
| 2767 | * tasklist. No need to take any locks since no-one can |
| 2768 | * be operating on this task. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2769 | */ |
| 2770 | void cgroup_fork_callbacks(struct task_struct *child) |
| 2771 | { |
| 2772 | if (need_forkexit_callback) { |
| 2773 | int i; |
| 2774 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 2775 | struct cgroup_subsys *ss = subsys[i]; |
| 2776 | if (ss->fork) |
| 2777 | ss->fork(ss, child); |
| 2778 | } |
| 2779 | } |
| 2780 | } |
| 2781 | |
| 2782 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2783 | * cgroup_post_fork - called on a new task after adding it to the task list |
| 2784 | * @child: the task in question |
| 2785 | * |
| 2786 | * Adds the task to the list running through its css_set if necessary. |
| 2787 | * Has to be after the task is visible on the task list in case we race |
| 2788 | * with the first call to cgroup_iter_start() - to guarantee that the |
| 2789 | * new task ends up on its list. |
| 2790 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2791 | void cgroup_post_fork(struct task_struct *child) |
| 2792 | { |
| 2793 | if (use_task_css_set_links) { |
| 2794 | write_lock(&css_set_lock); |
Lai Jiangshan | b12b533 | 2009-01-07 18:07:36 -0800 | [diff] [blame] | 2795 | task_lock(child); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2796 | if (list_empty(&child->cg_list)) |
| 2797 | list_add(&child->cg_list, &child->cgroups->tasks); |
Lai Jiangshan | b12b533 | 2009-01-07 18:07:36 -0800 | [diff] [blame] | 2798 | task_unlock(child); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2799 | write_unlock(&css_set_lock); |
| 2800 | } |
| 2801 | } |
| 2802 | /** |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2803 | * cgroup_exit - detach cgroup from exiting task |
| 2804 | * @tsk: pointer to task_struct of exiting process |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2805 | * @run_callback: run exit callbacks? |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2806 | * |
| 2807 | * Description: Detach cgroup from @tsk and release it. |
| 2808 | * |
| 2809 | * Note that cgroups marked notify_on_release force every task in |
| 2810 | * them to take the global cgroup_mutex mutex when exiting. |
| 2811 | * This could impact scaling on very large systems. Be reluctant to |
| 2812 | * use notify_on_release cgroups where very high task exit scaling |
| 2813 | * is required on large systems. |
| 2814 | * |
| 2815 | * the_top_cgroup_hack: |
| 2816 | * |
| 2817 | * Set the exiting tasks cgroup to the root cgroup (top_cgroup). |
| 2818 | * |
| 2819 | * We call cgroup_exit() while the task is still competent to |
| 2820 | * handle notify_on_release(), then leave the task attached to the |
| 2821 | * root cgroup in each hierarchy for the remainder of its exit. |
| 2822 | * |
| 2823 | * To do this properly, we would increment the reference count on |
| 2824 | * top_cgroup, and near the very end of the kernel/exit.c do_exit() |
| 2825 | * code we would add a second cgroup function call, to drop that |
| 2826 | * reference. This would just create an unnecessary hot spot on |
| 2827 | * the top_cgroup reference count, to no avail. |
| 2828 | * |
| 2829 | * Normally, holding a reference to a cgroup without bumping its |
| 2830 | * count is unsafe. The cgroup could go away, or someone could |
| 2831 | * attach us to a different cgroup, decrementing the count on |
| 2832 | * the first cgroup that we never incremented. But in this case, |
| 2833 | * 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] | 2834 | * which wards off any cgroup_attach_task() attempts, or task is a failed |
| 2835 | * fork, never visible to cgroup_attach_task. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2836 | */ |
| 2837 | void cgroup_exit(struct task_struct *tsk, int run_callbacks) |
| 2838 | { |
| 2839 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2840 | struct css_set *cg; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2841 | |
| 2842 | if (run_callbacks && need_forkexit_callback) { |
| 2843 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 2844 | struct cgroup_subsys *ss = subsys[i]; |
| 2845 | if (ss->exit) |
| 2846 | ss->exit(ss, tsk); |
| 2847 | } |
| 2848 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2849 | |
| 2850 | /* |
| 2851 | * Unlink from the css_set task list if necessary. |
| 2852 | * Optimistically check cg_list before taking |
| 2853 | * css_set_lock |
| 2854 | */ |
| 2855 | if (!list_empty(&tsk->cg_list)) { |
| 2856 | write_lock(&css_set_lock); |
| 2857 | if (!list_empty(&tsk->cg_list)) |
| 2858 | list_del(&tsk->cg_list); |
| 2859 | write_unlock(&css_set_lock); |
| 2860 | } |
| 2861 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2862 | /* Reassign the task to the init_css_set. */ |
| 2863 | task_lock(tsk); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2864 | cg = tsk->cgroups; |
| 2865 | tsk->cgroups = &init_css_set; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2866 | task_unlock(tsk); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2867 | if (cg) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2868 | put_css_set_taskexit(cg); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2869 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 2870 | |
| 2871 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2872 | * cgroup_clone - clone the cgroup the given subsystem is attached to |
| 2873 | * @tsk: the task to be moved |
| 2874 | * @subsys: the given subsystem |
Serge E. Hallyn | e885dcd | 2008-07-25 01:47:06 -0700 | [diff] [blame] | 2875 | * @nodename: the name for the new cgroup |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2876 | * |
| 2877 | * Duplicate the current cgroup in the hierarchy that the given |
| 2878 | * subsystem is attached to, and move this task into the new |
| 2879 | * child. |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 2880 | */ |
Serge E. Hallyn | e885dcd | 2008-07-25 01:47:06 -0700 | [diff] [blame] | 2881 | int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys, |
| 2882 | char *nodename) |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 2883 | { |
| 2884 | struct dentry *dentry; |
| 2885 | int ret = 0; |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 2886 | struct cgroup *parent, *child; |
| 2887 | struct inode *inode; |
| 2888 | struct css_set *cg; |
| 2889 | struct cgroupfs_root *root; |
| 2890 | struct cgroup_subsys *ss; |
| 2891 | |
| 2892 | /* We shouldn't be called by an unregistered subsystem */ |
| 2893 | BUG_ON(!subsys->active); |
| 2894 | |
| 2895 | /* First figure out what hierarchy and cgroup we're dealing |
| 2896 | * with, and pin them so we can drop cgroup_mutex */ |
| 2897 | mutex_lock(&cgroup_mutex); |
| 2898 | again: |
| 2899 | root = subsys->root; |
| 2900 | if (root == &rootnode) { |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 2901 | mutex_unlock(&cgroup_mutex); |
| 2902 | return 0; |
| 2903 | } |
Lai Jiangshan | 104cbd5 | 2009-01-07 18:07:38 -0800 | [diff] [blame] | 2904 | task_lock(tsk); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2905 | cg = tsk->cgroups; |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 2906 | parent = task_cgroup(tsk, subsys->subsys_id); |
| 2907 | |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 2908 | /* Pin the hierarchy */ |
Li Zefan | 7b574b7 | 2009-01-04 12:00:45 -0800 | [diff] [blame] | 2909 | if (!atomic_inc_not_zero(&parent->root->sb->s_active)) { |
| 2910 | /* We race with the final deactivate_super() */ |
| 2911 | mutex_unlock(&cgroup_mutex); |
| 2912 | return 0; |
| 2913 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 2914 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2915 | /* Keep the cgroup alive */ |
| 2916 | get_css_set(cg); |
Lai Jiangshan | 104cbd5 | 2009-01-07 18:07:38 -0800 | [diff] [blame] | 2917 | task_unlock(tsk); |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 2918 | mutex_unlock(&cgroup_mutex); |
| 2919 | |
| 2920 | /* Now do the VFS work to create a cgroup */ |
| 2921 | inode = parent->dentry->d_inode; |
| 2922 | |
| 2923 | /* Hold the parent directory mutex across this operation to |
| 2924 | * stop anyone else deleting the new cgroup */ |
| 2925 | mutex_lock(&inode->i_mutex); |
| 2926 | dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename)); |
| 2927 | if (IS_ERR(dentry)) { |
| 2928 | printk(KERN_INFO |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 2929 | "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename, |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 2930 | PTR_ERR(dentry)); |
| 2931 | ret = PTR_ERR(dentry); |
| 2932 | goto out_release; |
| 2933 | } |
| 2934 | |
| 2935 | /* Create the cgroup directory, which also creates the cgroup */ |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2936 | ret = vfs_mkdir(inode, dentry, 0755); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2937 | child = __d_cgrp(dentry); |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 2938 | dput(dentry); |
| 2939 | if (ret) { |
| 2940 | printk(KERN_INFO |
| 2941 | "Failed to create cgroup %s: %d\n", nodename, |
| 2942 | ret); |
| 2943 | goto out_release; |
| 2944 | } |
| 2945 | |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 2946 | /* The cgroup now exists. Retake cgroup_mutex and check |
| 2947 | * that we're still in the same state that we thought we |
| 2948 | * were. */ |
| 2949 | mutex_lock(&cgroup_mutex); |
| 2950 | if ((root != subsys->root) || |
| 2951 | (parent != task_cgroup(tsk, subsys->subsys_id))) { |
| 2952 | /* Aargh, we raced ... */ |
| 2953 | mutex_unlock(&inode->i_mutex); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2954 | put_css_set(cg); |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 2955 | |
| 2956 | deactivate_super(parent->root->sb); |
| 2957 | /* The cgroup is still accessible in the VFS, but |
| 2958 | * we're not going to try to rmdir() it at this |
| 2959 | * point. */ |
| 2960 | printk(KERN_INFO |
| 2961 | "Race in cgroup_clone() - leaking cgroup %s\n", |
| 2962 | nodename); |
| 2963 | goto again; |
| 2964 | } |
| 2965 | |
| 2966 | /* do any required auto-setup */ |
| 2967 | for_each_subsys(root, ss) { |
| 2968 | if (ss->post_clone) |
| 2969 | ss->post_clone(ss, child); |
| 2970 | } |
| 2971 | |
| 2972 | /* All seems fine. Finish by moving the task into the new cgroup */ |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 2973 | ret = cgroup_attach_task(child, tsk); |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 2974 | mutex_unlock(&cgroup_mutex); |
| 2975 | |
| 2976 | out_release: |
| 2977 | mutex_unlock(&inode->i_mutex); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2978 | |
| 2979 | mutex_lock(&cgroup_mutex); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2980 | put_css_set(cg); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2981 | mutex_unlock(&cgroup_mutex); |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 2982 | deactivate_super(parent->root->sb); |
| 2983 | return ret; |
| 2984 | } |
| 2985 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2986 | /** |
| 2987 | * cgroup_is_descendant - see if @cgrp is a descendant of current task's cgrp |
| 2988 | * @cgrp: the cgroup in question |
| 2989 | * |
| 2990 | * See if @cgrp is a descendant of the current task's cgroup in |
| 2991 | * the appropriate hierarchy. |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 2992 | * |
| 2993 | * If we are sending in dummytop, then presumably we are creating |
| 2994 | * the top cgroup in the subsystem. |
| 2995 | * |
| 2996 | * Called only by the ns (nsproxy) cgroup. |
| 2997 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2998 | int cgroup_is_descendant(const struct cgroup *cgrp) |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 2999 | { |
| 3000 | int ret; |
| 3001 | struct cgroup *target; |
| 3002 | int subsys_id; |
| 3003 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3004 | if (cgrp == dummytop) |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 3005 | return 1; |
| 3006 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3007 | get_first_subsys(cgrp, NULL, &subsys_id); |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 3008 | target = task_cgroup(current, subsys_id); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3009 | while (cgrp != target && cgrp!= cgrp->top_cgroup) |
| 3010 | cgrp = cgrp->parent; |
| 3011 | ret = (cgrp == target); |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 3012 | return ret; |
| 3013 | } |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3014 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3015 | static void check_for_release(struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3016 | { |
| 3017 | /* All of these checks rely on RCU to keep the cgroup |
| 3018 | * structure alive */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3019 | if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count) |
| 3020 | && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3021 | /* Control Group is currently removeable. If it's not |
| 3022 | * already queued for a userspace notification, queue |
| 3023 | * it now */ |
| 3024 | int need_schedule_work = 0; |
| 3025 | spin_lock(&release_list_lock); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3026 | if (!cgroup_is_removed(cgrp) && |
| 3027 | list_empty(&cgrp->release_list)) { |
| 3028 | list_add(&cgrp->release_list, &release_list); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3029 | need_schedule_work = 1; |
| 3030 | } |
| 3031 | spin_unlock(&release_list_lock); |
| 3032 | if (need_schedule_work) |
| 3033 | schedule_work(&release_agent_work); |
| 3034 | } |
| 3035 | } |
| 3036 | |
| 3037 | void __css_put(struct cgroup_subsys_state *css) |
| 3038 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3039 | struct cgroup *cgrp = css->cgroup; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3040 | rcu_read_lock(); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3041 | if (atomic_dec_and_test(&css->refcnt) && notify_on_release(cgrp)) { |
| 3042 | set_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 3043 | check_for_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3044 | } |
| 3045 | rcu_read_unlock(); |
| 3046 | } |
| 3047 | |
| 3048 | /* |
| 3049 | * Notify userspace when a cgroup is released, by running the |
| 3050 | * configured release agent with the name of the cgroup (path |
| 3051 | * relative to the root of cgroup file system) as the argument. |
| 3052 | * |
| 3053 | * Most likely, this user command will try to rmdir this cgroup. |
| 3054 | * |
| 3055 | * This races with the possibility that some other task will be |
| 3056 | * attached to this cgroup before it is removed, or that some other |
| 3057 | * user task will 'mkdir' a child cgroup of this cgroup. That's ok. |
| 3058 | * The presumed 'rmdir' will fail quietly if this cgroup is no longer |
| 3059 | * unused, and this cgroup will be reprieved from its death sentence, |
| 3060 | * to continue to serve a useful existence. Next time it's released, |
| 3061 | * we will get notified again, if it still has 'notify_on_release' set. |
| 3062 | * |
| 3063 | * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which |
| 3064 | * means only wait until the task is successfully execve()'d. The |
| 3065 | * separate release agent task is forked by call_usermodehelper(), |
| 3066 | * then control in this thread returns here, without waiting for the |
| 3067 | * release agent task. We don't bother to wait because the caller of |
| 3068 | * this routine has no use for the exit status of the release agent |
| 3069 | * task, so no sense holding our caller up for that. |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3070 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3071 | static void cgroup_release_agent(struct work_struct *work) |
| 3072 | { |
| 3073 | BUG_ON(work != &release_agent_work); |
| 3074 | mutex_lock(&cgroup_mutex); |
| 3075 | spin_lock(&release_list_lock); |
| 3076 | while (!list_empty(&release_list)) { |
| 3077 | char *argv[3], *envp[3]; |
| 3078 | int i; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 3079 | char *pathbuf = NULL, *agentbuf = NULL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3080 | struct cgroup *cgrp = list_entry(release_list.next, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3081 | struct cgroup, |
| 3082 | release_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3083 | list_del_init(&cgrp->release_list); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3084 | spin_unlock(&release_list_lock); |
| 3085 | pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 3086 | if (!pathbuf) |
| 3087 | goto continue_free; |
| 3088 | if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0) |
| 3089 | goto continue_free; |
| 3090 | agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL); |
| 3091 | if (!agentbuf) |
| 3092 | goto continue_free; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3093 | |
| 3094 | i = 0; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 3095 | argv[i++] = agentbuf; |
| 3096 | argv[i++] = pathbuf; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3097 | argv[i] = NULL; |
| 3098 | |
| 3099 | i = 0; |
| 3100 | /* minimal command environment */ |
| 3101 | envp[i++] = "HOME=/"; |
| 3102 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
| 3103 | envp[i] = NULL; |
| 3104 | |
| 3105 | /* Drop the lock while we invoke the usermode helper, |
| 3106 | * since the exec could involve hitting disk and hence |
| 3107 | * be a slow process */ |
| 3108 | mutex_unlock(&cgroup_mutex); |
| 3109 | call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3110 | mutex_lock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 3111 | continue_free: |
| 3112 | kfree(pathbuf); |
| 3113 | kfree(agentbuf); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3114 | spin_lock(&release_list_lock); |
| 3115 | } |
| 3116 | spin_unlock(&release_list_lock); |
| 3117 | mutex_unlock(&cgroup_mutex); |
| 3118 | } |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 3119 | |
| 3120 | static int __init cgroup_disable(char *str) |
| 3121 | { |
| 3122 | int i; |
| 3123 | char *token; |
| 3124 | |
| 3125 | while ((token = strsep(&str, ",")) != NULL) { |
| 3126 | if (!*token) |
| 3127 | continue; |
| 3128 | |
| 3129 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 3130 | struct cgroup_subsys *ss = subsys[i]; |
| 3131 | |
| 3132 | if (!strcmp(token, ss->name)) { |
| 3133 | ss->disabled = 1; |
| 3134 | printk(KERN_INFO "Disabling %s control group" |
| 3135 | " subsystem\n", ss->name); |
| 3136 | break; |
| 3137 | } |
| 3138 | } |
| 3139 | } |
| 3140 | return 1; |
| 3141 | } |
| 3142 | __setup("cgroup_disable=", cgroup_disable); |