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