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