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