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