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