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