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