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