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