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