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