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