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