]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - kernel/cgroup.c
sched: Use lockdep-based checking on rcu_dereference()
[mirror_ubuntu-zesty-kernel.git] / kernel / cgroup.c
1 /*
2 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
7 * Copyright notices from the original cpuset code:
8 * --------------------------------------------------
9 * Copyright (C) 2003 BULL SA.
10 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
11 *
12 * Portions derived from Patrick Mochel's sysfs code.
13 * sysfs is Copyright (c) 2001-3 Patrick Mochel
14 *
15 * 2003-10-10 Written by Simon Derr.
16 * 2003-10-22 Updates by Stephen Hemminger.
17 * 2004 May-July Rework by Paul Jackson.
18 * ---------------------------------------------------
19 *
20 * This file is subject to the terms and conditions of the GNU General Public
21 * License. See the file COPYING in the main directory of the Linux
22 * distribution for more details.
23 */
24
25 #include <linux/cgroup.h>
26 #include <linux/ctype.h>
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>
35 #include <linux/proc_fs.h>
36 #include <linux/rcupdate.h>
37 #include <linux/sched.h>
38 #include <linux/backing-dev.h>
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>
44 #include <linux/sort.h>
45 #include <linux/kmod.h>
46 #include <linux/delayacct.h>
47 #include <linux/cgroupstats.h>
48 #include <linux/hash.h>
49 #include <linux/namei.h>
50 #include <linux/smp_lock.h>
51 #include <linux/pid_namespace.h>
52 #include <linux/idr.h>
53 #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
54
55 #include <asm/atomic.h>
56
57 static DEFINE_MUTEX(cgroup_mutex);
58
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
66 #define MAX_CGROUP_ROOT_NAMELEN 64
67
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
82 /* Unique id for this hierarchy. */
83 int hierarchy_id;
84
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
97 /* A list running through the active hierarchies */
98 struct list_head root_list;
99
100 /* Hierarchy-specific flags */
101 unsigned long flags;
102
103 /* The path to use for release notifications. */
104 char release_agent_path[PATH_MAX];
105
106 /* The name for this hierarchy - may be empty */
107 char name[MAX_CGROUP_ROOT_NAMELEN];
108 };
109
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
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
150 /* The list of hierarchy roots */
151
152 static LIST_HEAD(roots);
153 static int root_count;
154
155 static DEFINE_IDA(hierarchy_ida);
156 static int next_hierarchy_id;
157 static DEFINE_SPINLOCK(hierarchy_id_lock);
158
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
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.
166 */
167 static int need_forkexit_callback __read_mostly;
168
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
183 /* convenient tests for these bits */
184 inline int cgroup_is_removed(const struct cgroup *cgrp)
185 {
186 return test_bit(CGRP_REMOVED, &cgrp->flags);
187 }
188
189 /* bits in struct cgroupfs_root flags field */
190 enum {
191 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
192 };
193
194 static int cgroup_is_releasable(const struct cgroup *cgrp)
195 {
196 const int bits =
197 (1 << CGRP_RELEASABLE) |
198 (1 << CGRP_NOTIFY_ON_RELEASE);
199 return (cgrp->flags & bits) == bits;
200 }
201
202 static int notify_on_release(const struct cgroup *cgrp)
203 {
204 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
205 }
206
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
214 /* for_each_active_root() allows you to iterate across the active hierarchies */
215 #define for_each_active_root(_root) \
216 list_for_each_entry(_root, &roots, root_list)
217
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);
224 static void check_for_release(struct cgroup *cgrp);
225
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 */
232 struct list_head cgrp_link_list;
233 struct cgroup *cgrp;
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
252 static int cgroup_subsys_init_idr(struct cgroup_subsys *ss);
253
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
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 */
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
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
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 */
294 static int use_task_css_set_links __read_mostly;
295
296 static void __put_css_set(struct css_set *cg, int taskexit)
297 {
298 struct cg_cgroup_link *link;
299 struct cg_cgroup_link *saved_link;
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 }
312
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);
322 if (atomic_dec_and_test(&cgrp->count) &&
323 notify_on_release(cgrp)) {
324 if (taskexit)
325 set_bit(CGRP_RELEASABLE, &cgrp->flags);
326 check_for_release(cgrp);
327 }
328
329 kfree(link);
330 }
331
332 write_unlock(&css_set_lock);
333 call_rcu(&cg->rcu_head, free_css_set_rcu);
334 }
335
336 /*
337 * refcounted get/put for css_set objects
338 */
339 static inline void get_css_set(struct css_set *cg)
340 {
341 atomic_inc(&cg->refcount);
342 }
343
344 static inline void put_css_set(struct css_set *cg)
345 {
346 __put_css_set(cg, 0);
347 }
348
349 static inline void put_css_set_taskexit(struct css_set *cg)
350 {
351 __put_css_set(cg, 1);
352 }
353
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
426 /*
427 * find_existing_css_set() is a helper for
428 * find_css_set(), and checks to see whether an existing
429 * css_set is suitable.
430 *
431 * oldcg: the cgroup group that we're using before the cgroup
432 * transition
433 *
434 * cgrp: the cgroup that we're moving into
435 *
436 * template: location in which to build the desired set of subsystem
437 * state objects for the new cgroup group
438 */
439 static struct css_set *find_existing_css_set(
440 struct css_set *oldcg,
441 struct cgroup *cgrp,
442 struct cgroup_subsys_state *template[])
443 {
444 int i;
445 struct cgroupfs_root *root = cgrp->root;
446 struct hlist_head *hhead;
447 struct hlist_node *node;
448 struct css_set *cg;
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++) {
453 if (root->subsys_bits & (1UL << i)) {
454 /* Subsystem is in this hierarchy. So we want
455 * the subsystem state from the new
456 * cgroup */
457 template[i] = cgrp->subsys[i];
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
465 hhead = css_set_hash(template);
466 hlist_for_each_entry(cg, node, hhead, hlist) {
467 if (!compare_css_sets(cg, oldcg, cgrp, template))
468 continue;
469
470 /* This css_set matches what we need */
471 return cg;
472 }
473
474 /* No existing cgroup group matched */
475 return NULL;
476 }
477
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
489 /*
490 * allocate_cg_links() allocates "count" cg_cgroup_link structures
491 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
492 * success or a negative error
493 */
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) {
502 free_cg_links(tmp);
503 return -ENOMEM;
504 }
505 list_add(&link->cgrp_link_list, tmp);
506 }
507 return 0;
508 }
509
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;
525 link->cgrp = cgrp;
526 atomic_inc(&cgrp->count);
527 list_move(&link->cgrp_link_list, &cgrp->css_sets);
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);
533 }
534
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 */
542 static struct css_set *find_css_set(
543 struct css_set *oldcg, struct cgroup *cgrp)
544 {
545 struct css_set *res;
546 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
547
548 struct list_head tmp_cg_links;
549
550 struct hlist_head *hhead;
551 struct cg_cgroup_link *link;
552
553 /* First see if we already have a cgroup group that matches
554 * the desired set */
555 read_lock(&css_set_lock);
556 res = find_existing_css_set(oldcg, cgrp, template);
557 if (res)
558 get_css_set(res);
559 read_unlock(&css_set_lock);
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
574 atomic_set(&res->refcount, 1);
575 INIT_LIST_HEAD(&res->cg_links);
576 INIT_LIST_HEAD(&res->tasks);
577 INIT_HLIST_NODE(&res->hlist);
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. */
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 }
591
592 BUG_ON(!list_empty(&tmp_cg_links));
593
594 css_set_count++;
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
600 write_unlock(&css_set_lock);
601
602 return res;
603 }
604
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
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
650 * cgroup_attach_task() can increment it again. Because a count of zero
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 *
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
664 * to the release agent with the name of the cgroup (path relative to
665 * the root of cgroup file system) as the argument.
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
677 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
678 * another. It does so using cgroup_mutex, however there are
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
682 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
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
687 * update of a tasks cgroup pointer by cgroup_attach_task()
688 */
689
690 /**
691 * cgroup_lock - lock out any changes to cgroup structures
692 *
693 */
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 */
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);
718 static int cgroup_populate_dir(struct cgroup *cgrp);
719 static const struct inode_operations cgroup_dir_inode_operations;
720 static const struct file_operations proc_cgroupstats_operations;
721
722 static struct backing_dev_info cgroup_backing_dev_info = {
723 .name = "cgroup",
724 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
725 };
726
727 static int alloc_css_id(struct cgroup_subsys *ss,
728 struct cgroup *parent, struct cgroup *child);
729
730 static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
731 {
732 struct inode *inode = new_inode(sb);
733
734 if (inode) {
735 inode->i_mode = mode;
736 inode->i_uid = current_fsuid();
737 inode->i_gid = current_fsgid();
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
744 /*
745 * Call subsys's pre_destroy handler.
746 * This is called before css refcnt check.
747 */
748 static int cgroup_call_pre_destroy(struct cgroup *cgrp)
749 {
750 struct cgroup_subsys *ss;
751 int ret = 0;
752
753 for_each_subsys(cgrp->root, ss)
754 if (ss->pre_destroy) {
755 ret = ss->pre_destroy(ss, cgrp);
756 if (ret)
757 break;
758 }
759 return ret;
760 }
761
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
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)) {
773 struct cgroup *cgrp = dentry->d_fsdata;
774 struct cgroup_subsys *ss;
775 BUG_ON(!(cgroup_is_removed(cgrp)));
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();
783
784 mutex_lock(&cgroup_mutex);
785 /*
786 * Release the subsystem state objects.
787 */
788 for_each_subsys(cgrp->root, ss)
789 ss->destroy(ss, cgrp);
790
791 cgrp->root->number_of_cgroups--;
792 mutex_unlock(&cgroup_mutex);
793
794 /*
795 * Drop the active superblock reference that we took when we
796 * created the cgroup
797 */
798 deactivate_super(cgrp->root->sb);
799
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
806 call_rcu(&cgrp->rcu_head, free_cgroup_rcu);
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
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 *
865 * CGRP_WAIT_ON_RMDIR flag is set under cgroup's inode->i_mutex;
866 */
867 DECLARE_WAIT_QUEUE_HEAD(cgroup_rmdir_waitq);
868
869 static void cgroup_wakeup_rmdir_waiter(struct cgroup *cgrp)
870 {
871 if (unlikely(test_and_clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags)))
872 wake_up_all(&cgroup_rmdir_waitq);
873 }
874
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
887 static int rebind_subsystems(struct cgroupfs_root *root,
888 unsigned long final_bits)
889 {
890 unsigned long added_bits, removed_bits;
891 struct cgroup *cgrp = &root->top_cgroup;
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++) {
898 unsigned long bit = 1UL << i;
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 */
912 if (root->number_of_cgroups > 1)
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 */
921 BUG_ON(cgrp->subsys[i]);
922 BUG_ON(!dummytop->subsys[i]);
923 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
924 mutex_lock(&ss->hierarchy_mutex);
925 cgrp->subsys[i] = dummytop->subsys[i];
926 cgrp->subsys[i]->cgroup = cgrp;
927 list_move(&ss->sibling, &root->subsys_list);
928 ss->root = root;
929 if (ss->bind)
930 ss->bind(ss, cgrp);
931 mutex_unlock(&ss->hierarchy_mutex);
932 } else if (bit & removed_bits) {
933 /* We're removing this subsystem */
934 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
935 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
936 mutex_lock(&ss->hierarchy_mutex);
937 if (ss->bind)
938 ss->bind(ss, dummytop);
939 dummytop->subsys[i]->cgroup = dummytop;
940 cgrp->subsys[i] = NULL;
941 subsys[i]->root = &rootnode;
942 list_move(&ss->sibling, &rootnode.subsys_list);
943 mutex_unlock(&ss->hierarchy_mutex);
944 } else if (bit & final_bits) {
945 /* Subsystem state should already exist */
946 BUG_ON(!cgrp->subsys[i]);
947 } else {
948 /* Subsystem state shouldn't exist */
949 BUG_ON(cgrp->subsys[i]);
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");
968 if (strlen(root->release_agent_path))
969 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
970 if (strlen(root->name))
971 seq_printf(seq, ",name=%s", root->name);
972 mutex_unlock(&cgroup_mutex);
973 return 0;
974 }
975
976 struct cgroup_sb_opts {
977 unsigned long subsys_bits;
978 unsigned long flags;
979 char *release_agent;
980 char *name;
981 /* User explicitly requested empty subsystem */
982 bool none;
983
984 struct cgroupfs_root *new_root;
985
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";
994 unsigned long mask = (unsigned long)-1;
995
996 #ifdef CONFIG_CPUSETS
997 mask = ~(1UL << cpuset_subsys_id);
998 #endif
999
1000 memset(opts, 0, sizeof(*opts));
1001
1002 while ((token = strsep(&o, ",")) != NULL) {
1003 if (!*token)
1004 return -EINVAL;
1005 if (!strcmp(token, "all")) {
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 }
1014 } else if (!strcmp(token, "none")) {
1015 /* Explicitly have no subsystems */
1016 opts->none = true;
1017 } else if (!strcmp(token, "noprefix")) {
1018 set_bit(ROOT_NOPREFIX, &opts->flags);
1019 } else if (!strncmp(token, "release_agent=", 14)) {
1020 /* Specifying two release agents is forbidden */
1021 if (opts->release_agent)
1022 return -EINVAL;
1023 opts->release_agent =
1024 kstrndup(token + 14, PATH_MAX, GFP_KERNEL);
1025 if (!opts->release_agent)
1026 return -ENOMEM;
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;
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)) {
1056 if (!ss->disabled)
1057 set_bit(i, &opts->subsys_bits);
1058 break;
1059 }
1060 }
1061 if (i == CGROUP_SUBSYS_COUNT)
1062 return -ENOENT;
1063 }
1064 }
1065
1066 /* Consistency checks */
1067
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
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 */
1086 if (!opts->subsys_bits && !opts->name)
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;
1096 struct cgroup *cgrp = &root->top_cgroup;
1097 struct cgroup_sb_opts opts;
1098
1099 lock_kernel();
1100 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
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
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
1120 ret = rebind_subsystems(root, opts.subsys_bits);
1121 if (ret)
1122 goto out_unlock;
1123
1124 /* (re)populate subsystem files */
1125 cgroup_populate_dir(cgrp);
1126
1127 if (opts.release_agent)
1128 strcpy(root->release_agent_path, opts.release_agent);
1129 out_unlock:
1130 kfree(opts.release_agent);
1131 kfree(opts.name);
1132 mutex_unlock(&cgroup_mutex);
1133 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
1134 unlock_kernel();
1135 return ret;
1136 }
1137
1138 static const struct super_operations cgroup_ops = {
1139 .statfs = simple_statfs,
1140 .drop_inode = generic_delete_inode,
1141 .show_options = cgroup_show_options,
1142 .remount_fs = cgroup_remount,
1143 };
1144
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);
1151 INIT_LIST_HEAD(&cgrp->pidlists);
1152 mutex_init(&cgrp->pidlist_mutex);
1153 }
1154
1155 static void init_cgroup_root(struct cgroupfs_root *root)
1156 {
1157 struct cgroup *cgrp = &root->top_cgroup;
1158 INIT_LIST_HEAD(&root->subsys_list);
1159 INIT_LIST_HEAD(&root->root_list);
1160 root->number_of_cgroups = 1;
1161 cgrp->root = root;
1162 cgrp->top_cgroup = cgrp;
1163 init_cgroup_housekeeping(cgrp);
1164 }
1165
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
1191 static int cgroup_test_super(struct super_block *sb, void *data)
1192 {
1193 struct cgroup_sb_opts *opts = data;
1194 struct cgroupfs_root *root = sb->s_fs_info;
1195
1196 /* If we asked for a name then it must match */
1197 if (opts->name && strcmp(opts->name, root->name))
1198 return 0;
1199
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))
1206 return 0;
1207
1208 return 1;
1209 }
1210
1211 static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1212 {
1213 struct cgroupfs_root *root;
1214
1215 if (!opts->subsys_bits && !opts->none)
1216 return NULL;
1217
1218 root = kzalloc(sizeof(*root), GFP_KERNEL);
1219 if (!root)
1220 return ERR_PTR(-ENOMEM);
1221
1222 if (!init_root_id(root)) {
1223 kfree(root);
1224 return ERR_PTR(-ENOMEM);
1225 }
1226 init_cgroup_root(root);
1227
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
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
1249 static int cgroup_set_super(struct super_block *sb, void *data)
1250 {
1251 int ret;
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
1258 BUG_ON(!opts->subsys_bits && !opts->none);
1259
1260 ret = set_anon_super(sb, NULL);
1261 if (ret)
1262 return ret;
1263
1264 sb->s_fs_info = opts->new_root;
1265 opts->new_root->sb = sb;
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
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;
1302 struct cgroupfs_root *root;
1303 int ret = 0;
1304 struct super_block *sb;
1305 struct cgroupfs_root *new_root;
1306
1307 /* First find the desired set of subsystems */
1308 ret = parse_cgroupfs_options(data, &opts);
1309 if (ret)
1310 goto out_err;
1311
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;
1320 }
1321 opts.new_root = new_root;
1322
1323 /* Locate an existing or new sb for this hierarchy */
1324 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, &opts);
1325 if (IS_ERR(sb)) {
1326 ret = PTR_ERR(sb);
1327 cgroup_drop_root(opts.new_root);
1328 goto out_err;
1329 }
1330
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;
1336 struct cgroup *root_cgrp = &root->top_cgroup;
1337 struct inode *inode;
1338 struct cgroupfs_root *existing_root;
1339 int i;
1340
1341 BUG_ON(sb->s_root != NULL);
1342
1343 ret = cgroup_get_rootdir(sb);
1344 if (ret)
1345 goto drop_new_super;
1346 inode = sb->s_root->d_inode;
1347
1348 mutex_lock(&inode->i_mutex);
1349 mutex_lock(&cgroup_mutex);
1350
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
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
1377 ret = rebind_subsystems(root, root->subsys_bits);
1378 if (ret == -EBUSY) {
1379 mutex_unlock(&cgroup_mutex);
1380 mutex_unlock(&inode->i_mutex);
1381 free_cg_links(&tmp_cg_links);
1382 goto drop_new_super;
1383 }
1384
1385 /* EBUSY should be the only error here */
1386 BUG_ON(ret);
1387
1388 list_add(&root->root_list, &roots);
1389 root_count++;
1390
1391 sb->s_root->d_fsdata = root_cgrp;
1392 root->top_cgroup.dentry = sb->s_root;
1393
1394 /* Link the top cgroup in this hierarchy into all
1395 * the css_set objects */
1396 write_lock(&css_set_lock);
1397 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1398 struct hlist_head *hhead = &css_set_table[i];
1399 struct hlist_node *node;
1400 struct css_set *cg;
1401
1402 hlist_for_each_entry(cg, node, hhead, hlist)
1403 link_css_set(&tmp_cg_links, cg, root_cgrp);
1404 }
1405 write_unlock(&css_set_lock);
1406
1407 free_cg_links(&tmp_cg_links);
1408
1409 BUG_ON(!list_empty(&root_cgrp->sibling));
1410 BUG_ON(!list_empty(&root_cgrp->children));
1411 BUG_ON(root->number_of_cgroups != 1);
1412
1413 cgroup_populate_dir(root_cgrp);
1414 mutex_unlock(&cgroup_mutex);
1415 mutex_unlock(&inode->i_mutex);
1416 } else {
1417 /*
1418 * We re-used an existing hierarchy - the new root (if
1419 * any) is not needed
1420 */
1421 cgroup_drop_root(opts.new_root);
1422 }
1423
1424 simple_set_mnt(mnt, sb);
1425 kfree(opts.release_agent);
1426 kfree(opts.name);
1427 return 0;
1428
1429 drop_new_super:
1430 deactivate_locked_super(sb);
1431 out_err:
1432 kfree(opts.release_agent);
1433 kfree(opts.name);
1434
1435 return ret;
1436 }
1437
1438 static void cgroup_kill_sb(struct super_block *sb) {
1439 struct cgroupfs_root *root = sb->s_fs_info;
1440 struct cgroup *cgrp = &root->top_cgroup;
1441 int ret;
1442 struct cg_cgroup_link *link;
1443 struct cg_cgroup_link *saved_link;
1444
1445 BUG_ON(!root);
1446
1447 BUG_ON(root->number_of_cgroups != 1);
1448 BUG_ON(!list_empty(&cgrp->children));
1449 BUG_ON(!list_empty(&cgrp->sibling));
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
1458 /*
1459 * Release all the links from css_sets to this hierarchy's
1460 * root cgroup
1461 */
1462 write_lock(&css_set_lock);
1463
1464 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1465 cgrp_link_list) {
1466 list_del(&link->cg_link_list);
1467 list_del(&link->cgrp_link_list);
1468 kfree(link);
1469 }
1470 write_unlock(&css_set_lock);
1471
1472 if (!list_empty(&root->root_list)) {
1473 list_del(&root->root_list);
1474 root_count--;
1475 }
1476
1477 mutex_unlock(&cgroup_mutex);
1478
1479 kill_litter_super(sb);
1480 cgroup_drop_root(root);
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
1489 static inline struct cgroup *__d_cgrp(struct dentry *dentry)
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
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 *
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.
1508 */
1509 int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
1510 {
1511 char *start;
1512 struct dentry *dentry = rcu_dereference(cgrp->dentry);
1513
1514 if (!dentry || cgrp == dummytop) {
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 (;;) {
1527 int len = dentry->d_name.len;
1528 if ((start -= len) < buf)
1529 return -ENAMETOOLONG;
1530 memcpy(start, cgrp->dentry->d_name.name, len);
1531 cgrp = cgrp->parent;
1532 if (!cgrp)
1533 break;
1534 dentry = rcu_dereference(cgrp->dentry);
1535 if (!cgrp->parent)
1536 continue;
1537 if (--start < buf)
1538 return -ENAMETOOLONG;
1539 *start = '/';
1540 }
1541 memmove(buf, start, buf + buflen - start);
1542 return 0;
1543 }
1544
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
1549 *
1550 * Call holding cgroup_mutex. May take task_lock of
1551 * the task 'tsk' during call.
1552 */
1553 int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
1554 {
1555 int retval = 0;
1556 struct cgroup_subsys *ss;
1557 struct cgroup *oldcgrp;
1558 struct css_set *cg;
1559 struct css_set *newcg;
1560 struct cgroupfs_root *root = cgrp->root;
1561
1562 /* Nothing to do if the task is already in that cgroup */
1563 oldcgrp = task_cgroup_from_root(tsk, root);
1564 if (cgrp == oldcgrp)
1565 return 0;
1566
1567 for_each_subsys(root, ss) {
1568 if (ss->can_attach) {
1569 retval = ss->can_attach(ss, cgrp, tsk, false);
1570 if (retval)
1571 return retval;
1572 }
1573 }
1574
1575 task_lock(tsk);
1576 cg = tsk->cgroups;
1577 get_css_set(cg);
1578 task_unlock(tsk);
1579 /*
1580 * Locate or allocate a new css_set for this task,
1581 * based on its final set of cgroups
1582 */
1583 newcg = find_css_set(cg, cgrp);
1584 put_css_set(cg);
1585 if (!newcg)
1586 return -ENOMEM;
1587
1588 task_lock(tsk);
1589 if (tsk->flags & PF_EXITING) {
1590 task_unlock(tsk);
1591 put_css_set(newcg);
1592 return -ESRCH;
1593 }
1594 rcu_assign_pointer(tsk->cgroups, newcg);
1595 task_unlock(tsk);
1596
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
1605 for_each_subsys(root, ss) {
1606 if (ss->attach)
1607 ss->attach(ss, cgrp, oldcgrp, tsk, false);
1608 }
1609 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
1610 synchronize_rcu();
1611 put_css_set(cg);
1612
1613 /*
1614 * wake up rmdir() waiter. the rmdir should fail since the cgroup
1615 * is no longer empty.
1616 */
1617 cgroup_wakeup_rmdir_waiter(cgrp);
1618 return 0;
1619 }
1620
1621 /*
1622 * Attach task with pid 'pid' to cgroup 'cgrp'. Call with cgroup_mutex
1623 * held. May take task_lock of task
1624 */
1625 static int attach_task_by_pid(struct cgroup *cgrp, u64 pid)
1626 {
1627 struct task_struct *tsk;
1628 const struct cred *cred = current_cred(), *tcred;
1629 int ret;
1630
1631 if (pid) {
1632 rcu_read_lock();
1633 tsk = find_task_by_vpid(pid);
1634 if (!tsk || tsk->flags & PF_EXITING) {
1635 rcu_read_unlock();
1636 return -ESRCH;
1637 }
1638
1639 tcred = __task_cred(tsk);
1640 if (cred->euid &&
1641 cred->euid != tcred->uid &&
1642 cred->euid != tcred->suid) {
1643 rcu_read_unlock();
1644 return -EACCES;
1645 }
1646 get_task_struct(tsk);
1647 rcu_read_unlock();
1648 } else {
1649 tsk = current;
1650 get_task_struct(tsk);
1651 }
1652
1653 ret = cgroup_attach_task(cgrp, tsk);
1654 put_task_struct(tsk);
1655 return ret;
1656 }
1657
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
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 *
1672 * On success, returns true; the lock should be later released with
1673 * cgroup_unlock(). On failure returns false with no lock held.
1674 */
1675 bool cgroup_lock_live_group(struct cgroup *cgrp)
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);
1692 cgroup_unlock();
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');
1703 cgroup_unlock();
1704 return 0;
1705 }
1706
1707 /* A buffer size big enough for numbers or short strings */
1708 #define CGROUP_LOCAL_BUFFER_SIZE 64
1709
1710 static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
1711 struct file *file,
1712 const char __user *userbuf,
1713 size_t nbytes, loff_t *unused_ppos)
1714 {
1715 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
1716 int retval = 0;
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 */
1727 if (cft->write_u64) {
1728 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
1729 if (*end)
1730 return -EINVAL;
1731 retval = cft->write_u64(cgrp, cft, val);
1732 } else {
1733 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
1734 if (*end)
1735 return -EINVAL;
1736 retval = cft->write_s64(cgrp, cft, val);
1737 }
1738 if (!retval)
1739 retval = nbytes;
1740 return retval;
1741 }
1742
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 {
1748 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
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 }
1763 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
1764 retval = -EFAULT;
1765 goto out;
1766 }
1767
1768 buffer[nbytes] = 0; /* nul-terminate */
1769 retval = cft->write_string(cgrp, cft, strstrip(buffer));
1770 if (!retval)
1771 retval = nbytes;
1772 out:
1773 if (buffer != local_buffer)
1774 kfree(buffer);
1775 return retval;
1776 }
1777
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);
1782 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
1783
1784 if (cgroup_is_removed(cgrp))
1785 return -ENODEV;
1786 if (cft->write)
1787 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
1788 if (cft->write_u64 || cft->write_s64)
1789 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
1790 if (cft->write_string)
1791 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
1792 if (cft->trigger) {
1793 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
1794 return ret ? ret : nbytes;
1795 }
1796 return -EINVAL;
1797 }
1798
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)
1803 {
1804 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
1805 u64 val = cft->read_u64(cgrp, cft);
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
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 {
1816 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
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
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);
1827 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
1828
1829 if (cgroup_is_removed(cgrp))
1830 return -ENODEV;
1831
1832 if (cft->read)
1833 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
1834 if (cft->read_u64)
1835 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
1836 if (cft->read_s64)
1837 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
1838 return -EINVAL;
1839 }
1840
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;
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);
1869 }
1870
1871 static int cgroup_seqfile_release(struct inode *inode, struct file *file)
1872 {
1873 struct seq_file *seq = file->private_data;
1874 kfree(seq->private);
1875 return single_release(inode, file);
1876 }
1877
1878 static const struct file_operations cgroup_seqfile_operations = {
1879 .read = seq_read,
1880 .write = cgroup_file_write,
1881 .llseek = seq_lseek,
1882 .release = cgroup_seqfile_release,
1883 };
1884
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;
1893 cft = __d_cft(file->f_dentry);
1894
1895 if (cft->read_map || cft->read_seq_string) {
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)
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
1937 static const struct file_operations cgroup_file_operations = {
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
1945 static const struct inode_operations cgroup_dir_inode_operations = {
1946 .lookup = simple_lookup,
1947 .mkdir = cgroup_mkdir,
1948 .rmdir = cgroup_rmdir,
1949 .rename = cgroup_rename,
1950 };
1951
1952 static int cgroup_create_file(struct dentry *dentry, mode_t mode,
1953 struct super_block *sb)
1954 {
1955 static const struct dentry_operations cgroup_dops = {
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 */
1979 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
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 /*
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.
1996 */
1997 static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
1998 mode_t mode)
1999 {
2000 struct dentry *parent;
2001 int error = 0;
2002
2003 parent = cgrp->parent->dentry;
2004 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
2005 if (!error) {
2006 dentry->d_fsdata = cgrp;
2007 inc_nlink(parent->d_inode);
2008 rcu_assign_pointer(cgrp->dentry, dentry);
2009 dget(dentry);
2010 }
2011 dput(dentry);
2012
2013 return error;
2014 }
2015
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
2043 int cgroup_add_file(struct cgroup *cgrp,
2044 struct cgroup_subsys *subsys,
2045 const struct cftype *cft)
2046 {
2047 struct dentry *dir = cgrp->dentry;
2048 struct dentry *dentry;
2049 int error;
2050 mode_t mode;
2051
2052 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
2053 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
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)) {
2061 mode = cgroup_file_mode(cft);
2062 error = cgroup_create_file(dentry, mode | S_IFREG,
2063 cgrp->root->sb);
2064 if (!error)
2065 dentry->d_fsdata = (void *)cft;
2066 dput(dentry);
2067 } else
2068 error = PTR_ERR(dentry);
2069 return error;
2070 }
2071
2072 int cgroup_add_files(struct cgroup *cgrp,
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++) {
2079 err = cgroup_add_file(cgrp, subsys, &cft[i]);
2080 if (err)
2081 return err;
2082 }
2083 return 0;
2084 }
2085
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 */
2092 int cgroup_task_count(const struct cgroup *cgrp)
2093 {
2094 int count = 0;
2095 struct cg_cgroup_link *link;
2096
2097 read_lock(&css_set_lock);
2098 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
2099 count += atomic_read(&link->cg->refcount);
2100 }
2101 read_unlock(&css_set_lock);
2102 return count;
2103 }
2104
2105 /*
2106 * Advance a list_head iterator. The iterator should be positioned at
2107 * the start of a css_set
2108 */
2109 static void cgroup_advance_iter(struct cgroup *cgrp,
2110 struct cgroup_iter *it)
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;
2119 if (l == &cgrp->css_sets) {
2120 it->cg_link = NULL;
2121 return;
2122 }
2123 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
2124 cg = link->cg;
2125 } while (list_empty(&cg->tasks));
2126 it->cg_link = l;
2127 it->task = cg->tasks.next;
2128 }
2129
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 */
2139 static void cgroup_enable_task_cg_lists(void)
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);
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))
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
2158 void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
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 */
2165 if (!use_task_css_set_links)
2166 cgroup_enable_task_cg_lists();
2167
2168 read_lock(&css_set_lock);
2169 it->cg_link = &cgrp->css_sets;
2170 cgroup_advance_iter(cgrp, it);
2171 }
2172
2173 struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
2174 struct cgroup_iter *it)
2175 {
2176 struct task_struct *res;
2177 struct list_head *l = it->task;
2178 struct cg_cgroup_link *link;
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;
2186 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
2187 if (l == &link->cg->tasks) {
2188 /* We reached the end of this task list - move on to
2189 * the next cg_cgroup_link */
2190 cgroup_advance_iter(cgrp, it);
2191 } else {
2192 it->task = l;
2193 }
2194 return res;
2195 }
2196
2197 void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
2198 {
2199 read_unlock(&css_set_lock);
2200 }
2201
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++) {
2339 struct task_struct *q = heap->ptrs[i];
2340 if (i == 0) {
2341 latest_time = q->start_time;
2342 latest_task = q;
2343 }
2344 /* Process the task per the caller's callback */
2345 scan->process_task(q, scan);
2346 put_task_struct(q);
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
2362 /*
2363 * Stuff for reading the 'tasks'/'procs' files.
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 *
2370 */
2371
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
2408 /*
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.
2413 */
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)
2417 {
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)) {
2447 newlist = pidlist_resize(list, dest);
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
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);
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
2507 /*
2508 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
2509 */
2510 static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
2511 struct cgroup_pidlist **lp)
2512 {
2513 pid_t *array;
2514 int length;
2515 int pid, n = 0; /* used for populating the array */
2516 struct cgroup_iter it;
2517 struct task_struct *tsk;
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);
2527 array = pidlist_allocate(length);
2528 if (!array)
2529 return -ENOMEM;
2530 /* now, populate the array */
2531 cgroup_iter_start(cgrp, &it);
2532 while ((tsk = cgroup_iter_next(cgrp, &it))) {
2533 if (unlikely(n == length))
2534 break;
2535 /* get tgid or pid for procs or tasks file respectively */
2536 if (type == CGROUP_FILE_PROCS)
2537 pid = task_tgid_vnr(tsk);
2538 else
2539 pid = task_pid_vnr(tsk);
2540 if (pid > 0) /* make sure to only use valid results */
2541 array[n++] = pid;
2542 }
2543 cgroup_iter_end(cgrp, &it);
2544 length = n;
2545 /* now sort & (if procs) strip out duplicates */
2546 sort(array, length, sizeof(pid_t), cmppid, NULL);
2547 if (type == CGROUP_FILE_PROCS)
2548 length = pidlist_uniq(&array, length);
2549 l = cgroup_pidlist_find(cgrp, type);
2550 if (!l) {
2551 pidlist_free(array);
2552 return -ENOMEM;
2553 }
2554 /* store array, freeing old if necessary - lock already held */
2555 pidlist_free(l->list);
2556 l->list = array;
2557 l->length = length;
2558 l->use_count++;
2559 up_write(&l->mutex);
2560 *lp = l;
2561 return 0;
2562 }
2563
2564 /**
2565 * cgroupstats_build - build and fill cgroupstats
2566 * @stats: cgroupstats to fill information into
2567 * @dentry: A dentry entry belonging to the cgroup for which stats have
2568 * been requested.
2569 *
2570 * Build and fill cgroupstats so that taskstats can export it to user
2571 * space.
2572 */
2573 int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
2574 {
2575 int ret = -EINVAL;
2576 struct cgroup *cgrp;
2577 struct cgroup_iter it;
2578 struct task_struct *tsk;
2579
2580 /*
2581 * Validate dentry by checking the superblock operations,
2582 * and make sure it's a directory.
2583 */
2584 if (dentry->d_sb->s_op != &cgroup_ops ||
2585 !S_ISDIR(dentry->d_inode->i_mode))
2586 goto err;
2587
2588 ret = 0;
2589 cgrp = dentry->d_fsdata;
2590
2591 cgroup_iter_start(cgrp, &it);
2592 while ((tsk = cgroup_iter_next(cgrp, &it))) {
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 }
2612 cgroup_iter_end(cgrp, &it);
2613
2614 err:
2615 return ret;
2616 }
2617
2618
2619 /*
2620 * seq_file methods for the tasks/procs files. The seq_file position is the
2621 * next pid to display; the seq_file iterator is a pointer to the pid
2622 * in the cgroup->l->list array.
2623 */
2624
2625 static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
2626 {
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 */
2633 struct cgroup_pidlist *l = s->private;
2634 int index = 0, pid = *pos;
2635 int *iter;
2636
2637 down_read(&l->mutex);
2638 if (pid) {
2639 int end = l->length;
2640
2641 while (index < end) {
2642 int mid = (index + end) / 2;
2643 if (l->list[mid] == pid) {
2644 index = mid;
2645 break;
2646 } else if (l->list[mid] <= pid)
2647 index = mid + 1;
2648 else
2649 end = mid;
2650 }
2651 }
2652 /* If we're off the end of the array, we're done */
2653 if (index >= l->length)
2654 return NULL;
2655 /* Update the abstract position to be the actual pid that we found */
2656 iter = l->list + index;
2657 *pos = *iter;
2658 return iter;
2659 }
2660
2661 static void cgroup_pidlist_stop(struct seq_file *s, void *v)
2662 {
2663 struct cgroup_pidlist *l = s->private;
2664 up_read(&l->mutex);
2665 }
2666
2667 static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
2668 {
2669 struct cgroup_pidlist *l = s->private;
2670 pid_t *p = v;
2671 pid_t *end = l->list + l->length;
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
2685 static int cgroup_pidlist_show(struct seq_file *s, void *v)
2686 {
2687 return seq_printf(s, "%d\n", *(int *)v);
2688 }
2689
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,
2699 };
2700
2701 static void cgroup_release_pid_array(struct cgroup_pidlist *l)
2702 {
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);
2710 down_write(&l->mutex);
2711 BUG_ON(!l->use_count);
2712 if (!--l->use_count) {
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);
2716 pidlist_free(l->list);
2717 put_pid_ns(l->key.ns);
2718 up_write(&l->mutex);
2719 kfree(l);
2720 return;
2721 }
2722 mutex_unlock(&l->owner->pidlist_mutex);
2723 up_write(&l->mutex);
2724 }
2725
2726 static int cgroup_pidlist_release(struct inode *inode, struct file *file)
2727 {
2728 struct cgroup_pidlist *l;
2729 if (!(file->f_mode & FMODE_READ))
2730 return 0;
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);
2737 return seq_release(inode, file);
2738 }
2739
2740 static const struct file_operations cgroup_pidlist_operations = {
2741 .read = seq_read,
2742 .llseek = seq_lseek,
2743 .write = cgroup_file_write,
2744 .release = cgroup_pidlist_release,
2745 };
2746
2747 /*
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.
2751 */
2752 /* helper function for the two below it */
2753 static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
2754 {
2755 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
2756 struct cgroup_pidlist *l;
2757 int retval;
2758
2759 /* Nothing to do for write-only files */
2760 if (!(file->f_mode & FMODE_READ))
2761 return 0;
2762
2763 /* have the array populated */
2764 retval = pidlist_array_load(cgrp, type, &l);
2765 if (retval)
2766 return retval;
2767 /* configure file information */
2768 file->f_op = &cgroup_pidlist_operations;
2769
2770 retval = seq_open(file, &cgroup_pidlist_seq_operations);
2771 if (retval) {
2772 cgroup_release_pid_array(l);
2773 return retval;
2774 }
2775 ((struct seq_file *)file->private_data)->private = l;
2776 return 0;
2777 }
2778 static int cgroup_tasks_open(struct inode *unused, struct file *file)
2779 {
2780 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
2781 }
2782 static int cgroup_procs_open(struct inode *unused, struct file *file)
2783 {
2784 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
2785 }
2786
2787 static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
2788 struct cftype *cft)
2789 {
2790 return notify_on_release(cgrp);
2791 }
2792
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
2805 /*
2806 * for the common functions, 'private' gives the type of file
2807 */
2808 /* for hysterical raisins, we can't put this on the older files */
2809 #define CGROUP_FILE_GENERIC_PREFIX "cgroup."
2810 static struct cftype files[] = {
2811 {
2812 .name = "tasks",
2813 .open = cgroup_tasks_open,
2814 .write_u64 = cgroup_tasks_write,
2815 .release = cgroup_pidlist_release,
2816 .mode = S_IRUGO | S_IWUSR,
2817 },
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 },
2825 {
2826 .name = "notify_on_release",
2827 .read_u64 = cgroup_read_notify_on_release,
2828 .write_u64 = cgroup_write_notify_on_release,
2829 },
2830 };
2831
2832 static struct cftype cft_release_agent = {
2833 .name = "release_agent",
2834 .read_seq_string = cgroup_release_agent_show,
2835 .write_string = cgroup_release_agent_write,
2836 .max_write_len = PATH_MAX,
2837 };
2838
2839 static int cgroup_populate_dir(struct cgroup *cgrp)
2840 {
2841 int err;
2842 struct cgroup_subsys *ss;
2843
2844 /* First clear out any existing files */
2845 cgroup_clear_directory(cgrp->dentry);
2846
2847 err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
2848 if (err < 0)
2849 return err;
2850
2851 if (cgrp == cgrp->top_cgroup) {
2852 if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
2853 return err;
2854 }
2855
2856 for_each_subsys(cgrp->root, ss) {
2857 if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
2858 return err;
2859 }
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 }
2871
2872 return 0;
2873 }
2874
2875 static void init_cgroup_css(struct cgroup_subsys_state *css,
2876 struct cgroup_subsys *ss,
2877 struct cgroup *cgrp)
2878 {
2879 css->cgroup = cgrp;
2880 atomic_set(&css->refcnt, 1);
2881 css->flags = 0;
2882 css->id = NULL;
2883 if (cgrp == dummytop)
2884 set_bit(CSS_ROOT, &css->flags);
2885 BUG_ON(cgrp->subsys[ss->subsys_id]);
2886 cgrp->subsys[ss->subsys_id] = css;
2887 }
2888
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)
2897 mutex_lock(&ss->hierarchy_mutex);
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
2912 /*
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
2917 *
2918 * Must be called with the mutex on the parent inode held
2919 */
2920 static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
2921 mode_t mode)
2922 {
2923 struct cgroup *cgrp;
2924 struct cgroupfs_root *root = parent->root;
2925 int err = 0;
2926 struct cgroup_subsys *ss;
2927 struct super_block *sb = root->sb;
2928
2929 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
2930 if (!cgrp)
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
2942 init_cgroup_housekeeping(cgrp);
2943
2944 cgrp->parent = parent;
2945 cgrp->root = parent->root;
2946 cgrp->top_cgroup = parent->top_cgroup;
2947
2948 if (notify_on_release(parent))
2949 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2950
2951 for_each_subsys(root, ss) {
2952 struct cgroup_subsys_state *css = ss->create(ss, cgrp);
2953
2954 if (IS_ERR(css)) {
2955 err = PTR_ERR(css);
2956 goto err_destroy;
2957 }
2958 init_cgroup_css(css, ss, cgrp);
2959 if (ss->use_id) {
2960 err = alloc_css_id(ss, parent, cgrp);
2961 if (err)
2962 goto err_destroy;
2963 }
2964 /* At error, ->destroy() callback has to free assigned ID. */
2965 }
2966
2967 cgroup_lock_hierarchy(root);
2968 list_add(&cgrp->sibling, &cgrp->parent->children);
2969 cgroup_unlock_hierarchy(root);
2970 root->number_of_cgroups++;
2971
2972 err = cgroup_create_dir(cgrp, dentry, mode);
2973 if (err < 0)
2974 goto err_remove;
2975
2976 /* The cgroup directory was pre-locked for us */
2977 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
2978
2979 err = cgroup_populate_dir(cgrp);
2980 /* If err < 0, we have a half-filled directory - oh well ;) */
2981
2982 mutex_unlock(&cgroup_mutex);
2983 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
2984
2985 return 0;
2986
2987 err_remove:
2988
2989 cgroup_lock_hierarchy(root);
2990 list_del(&cgrp->sibling);
2991 cgroup_unlock_hierarchy(root);
2992 root->number_of_cgroups--;
2993
2994 err_destroy:
2995
2996 for_each_subsys(root, ss) {
2997 if (cgrp->subsys[ss->subsys_id])
2998 ss->destroy(ss, cgrp);
2999 }
3000
3001 mutex_unlock(&cgroup_mutex);
3002
3003 /* Release the reference count that we took on the superblock */
3004 deactivate_super(sb);
3005
3006 kfree(cgrp);
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
3018 static int cgroup_has_css_refs(struct cgroup *cgrp)
3019 {
3020 /* Check the reference count on each subsystem. Since we
3021 * already established that there are no tasks in the
3022 * cgroup, if the css refcount is also 1, then there should
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 */
3034 if (ss->root != cgrp->root)
3035 continue;
3036 css = cgrp->subsys[ss->subsys_id];
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. */
3043 if (css && (atomic_read(&css->refcnt) > 1))
3044 return 1;
3045 }
3046 return 0;
3047 }
3048
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;
3064 while (1) {
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 */
3078 if (atomic_cmpxchg(&css->refcnt, refcnt, 0) == refcnt)
3079 break;
3080 cpu_relax();
3081 }
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
3102 static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
3103 {
3104 struct cgroup *cgrp = dentry->d_fsdata;
3105 struct dentry *d;
3106 struct cgroup *parent;
3107 DEFINE_WAIT(wait);
3108 int ret;
3109
3110 /* the vfs holds both inode->i_mutex already */
3111 again:
3112 mutex_lock(&cgroup_mutex);
3113 if (atomic_read(&cgrp->count) != 0) {
3114 mutex_unlock(&cgroup_mutex);
3115 return -EBUSY;
3116 }
3117 if (!list_empty(&cgrp->children)) {
3118 mutex_unlock(&cgroup_mutex);
3119 return -EBUSY;
3120 }
3121 mutex_unlock(&cgroup_mutex);
3122
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
3134 /*
3135 * Call pre_destroy handlers of subsys. Notify subsystems
3136 * that rmdir() request comes.
3137 */
3138 ret = cgroup_call_pre_destroy(cgrp);
3139 if (ret) {
3140 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
3141 return ret;
3142 }
3143
3144 mutex_lock(&cgroup_mutex);
3145 parent = cgrp->parent;
3146 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) {
3147 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
3148 mutex_unlock(&cgroup_mutex);
3149 return -EBUSY;
3150 }
3151 prepare_to_wait(&cgroup_rmdir_waitq, &wait, TASK_INTERRUPTIBLE);
3152 if (!cgroup_clear_css_refs(cgrp)) {
3153 mutex_unlock(&cgroup_mutex);
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();
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);
3169
3170 spin_lock(&release_list_lock);
3171 set_bit(CGRP_REMOVED, &cgrp->flags);
3172 if (!list_empty(&cgrp->release_list))
3173 list_del(&cgrp->release_list);
3174 spin_unlock(&release_list_lock);
3175
3176 cgroup_lock_hierarchy(cgrp->root);
3177 /* delete this cgroup from parent->children */
3178 list_del(&cgrp->sibling);
3179 cgroup_unlock_hierarchy(cgrp->root);
3180
3181 spin_lock(&cgrp->dentry->d_lock);
3182 d = dget(cgrp->dentry);
3183 spin_unlock(&d->d_lock);
3184
3185 cgroup_d_remove_dir(d);
3186 dput(d);
3187
3188 set_bit(CGRP_RELEASABLE, &parent->flags);
3189 check_for_release(parent);
3190
3191 mutex_unlock(&cgroup_mutex);
3192 return 0;
3193 }
3194
3195 static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
3196 {
3197 struct cgroup_subsys_state *css;
3198
3199 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
3200
3201 /* Create the top cgroup state for this subsystem */
3202 list_add(&ss->sibling, &rootnode.subsys_list);
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
3209 /* Update the init_css_set to contain a subsys
3210 * pointer to this state - since the subsystem is
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];
3214
3215 need_forkexit_callback |= ss->fork || ss->exit;
3216
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
3222 mutex_init(&ss->hierarchy_mutex);
3223 lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
3224 ss->active = 1;
3225 }
3226
3227 /**
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.
3232 */
3233 int __init cgroup_init_early(void)
3234 {
3235 int i;
3236 atomic_set(&init_css_set.refcount, 1);
3237 INIT_LIST_HEAD(&init_css_set.cg_links);
3238 INIT_LIST_HEAD(&init_css_set.tasks);
3239 INIT_HLIST_NODE(&init_css_set.hlist);
3240 css_set_count = 1;
3241 init_cgroup_root(&rootnode);
3242 root_count = 1;
3243 init_task.cgroups = &init_css_set;
3244
3245 init_css_set_link.cg = &init_css_set;
3246 init_css_set_link.cgrp = dummytop;
3247 list_add(&init_css_set_link.cgrp_link_list,
3248 &rootnode.top_cgroup.css_sets);
3249 list_add(&init_css_set_link.cg_link_list,
3250 &init_css_set.cg_links);
3251
3252 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
3253 INIT_HLIST_HEAD(&css_set_table[i]);
3254
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) {
3263 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
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 /**
3275 * cgroup_init - cgroup initialization
3276 *
3277 * Register cgroup filesystem and /proc file, and initialize
3278 * any subsystems that didn't request early init.
3279 */
3280 int __init cgroup_init(void)
3281 {
3282 int err;
3283 int i;
3284 struct hlist_head *hhead;
3285
3286 err = bdi_init(&cgroup_backing_dev_info);
3287 if (err)
3288 return err;
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);
3294 if (ss->use_id)
3295 cgroup_subsys_init_idr(ss);
3296 }
3297
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);
3301 BUG_ON(!init_root_id(&rootnode));
3302 err = register_filesystem(&cgroup_fs_type);
3303 if (err < 0)
3304 goto out;
3305
3306 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
3307
3308 out:
3309 if (err)
3310 bdi_destroy(&cgroup_backing_dev_info);
3311
3312 return err;
3313 }
3314
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,
3321 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
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
3351 for_each_active_root(root) {
3352 struct cgroup_subsys *ss;
3353 struct cgroup *cgrp;
3354 int count = 0;
3355
3356 seq_printf(m, "%d:", root->hierarchy_id);
3357 for_each_subsys(root, ss)
3358 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
3359 if (strlen(root->name))
3360 seq_printf(m, "%sname=%s", count ? "," : "",
3361 root->name);
3362 seq_putc(m, ':');
3363 cgrp = task_cgroup_from_root(tsk, root);
3364 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
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
3386 const struct file_operations proc_cgroup_operations = {
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;
3397
3398 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
3399 mutex_lock(&cgroup_mutex);
3400 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3401 struct cgroup_subsys *ss = subsys[i];
3402 seq_printf(m, "%s\t%d\t%d\t%d\n",
3403 ss->name, ss->root->hierarchy_id,
3404 ss->root->number_of_cgroups, !ss->disabled);
3405 }
3406 mutex_unlock(&cgroup_mutex);
3407 return 0;
3408 }
3409
3410 static int cgroupstats_open(struct inode *inode, struct file *file)
3411 {
3412 return single_open(file, proc_cgroupstats_show, NULL);
3413 }
3414
3415 static const struct file_operations proc_cgroupstats_operations = {
3416 .open = cgroupstats_open,
3417 .read = seq_read,
3418 .llseek = seq_lseek,
3419 .release = single_release,
3420 };
3421
3422 /**
3423 * cgroup_fork - attach newly forked task to its parents cgroup.
3424 * @child: pointer to task_struct of forking parent process.
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
3431 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
3432 * have already changed current->cgroups, allowing the previously
3433 * referenced cgroup group to be removed and freed.
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 {
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);
3445 }
3446
3447 /**
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.
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
3467 /**
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 */
3476 void cgroup_post_fork(struct task_struct *child)
3477 {
3478 if (use_task_css_set_links) {
3479 write_lock(&css_set_lock);
3480 task_lock(child);
3481 if (list_empty(&child->cg_list))
3482 list_add(&child->cg_list, &child->cgroups->tasks);
3483 task_unlock(child);
3484 write_unlock(&css_set_lock);
3485 }
3486 }
3487 /**
3488 * cgroup_exit - detach cgroup from exiting task
3489 * @tsk: pointer to task_struct of exiting process
3490 * @run_callback: run exit callbacks?
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,
3519 * which wards off any cgroup_attach_task() attempts, or task is a failed
3520 * fork, never visible to cgroup_attach_task.
3521 */
3522 void cgroup_exit(struct task_struct *tsk, int run_callbacks)
3523 {
3524 int i;
3525 struct css_set *cg;
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 }
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
3547 /* Reassign the task to the init_css_set. */
3548 task_lock(tsk);
3549 cg = tsk->cgroups;
3550 tsk->cgroups = &init_css_set;
3551 task_unlock(tsk);
3552 if (cg)
3553 put_css_set_taskexit(cg);
3554 }
3555
3556 /**
3557 * cgroup_clone - clone the cgroup the given subsystem is attached to
3558 * @tsk: the task to be moved
3559 * @subsys: the given subsystem
3560 * @nodename: the name for the new cgroup
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.
3565 */
3566 int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys,
3567 char *nodename)
3568 {
3569 struct dentry *dentry;
3570 int ret = 0;
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) {
3586 mutex_unlock(&cgroup_mutex);
3587 return 0;
3588 }
3589
3590 /* Pin the hierarchy */
3591 if (!atomic_inc_not_zero(&root->sb->s_active)) {
3592 /* We race with the final deactivate_super() */
3593 mutex_unlock(&cgroup_mutex);
3594 return 0;
3595 }
3596
3597 /* Keep the cgroup alive */
3598 task_lock(tsk);
3599 parent = task_cgroup(tsk, subsys->subsys_id);
3600 cg = tsk->cgroups;
3601 get_css_set(cg);
3602 task_unlock(tsk);
3603
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
3615 "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename,
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 */
3622 ret = vfs_mkdir(inode, dentry, 0755);
3623 child = __d_cgrp(dentry);
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
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);
3640 put_css_set(cg);
3641
3642 deactivate_super(root->sb);
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 */
3659 ret = cgroup_attach_task(child, tsk);
3660 mutex_unlock(&cgroup_mutex);
3661
3662 out_release:
3663 mutex_unlock(&inode->i_mutex);
3664
3665 mutex_lock(&cgroup_mutex);
3666 put_css_set(cg);
3667 mutex_unlock(&cgroup_mutex);
3668 deactivate_super(root->sb);
3669 return ret;
3670 }
3671
3672 /**
3673 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
3674 * @cgrp: the cgroup in question
3675 * @task: the task in question
3676 *
3677 * See if @cgrp is a descendant of @task's cgroup in the appropriate
3678 * hierarchy.
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 */
3685 int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
3686 {
3687 int ret;
3688 struct cgroup *target;
3689
3690 if (cgrp == dummytop)
3691 return 1;
3692
3693 target = task_cgroup_from_root(task, cgrp->root);
3694 while (cgrp != target && cgrp!= cgrp->top_cgroup)
3695 cgrp = cgrp->parent;
3696 ret = (cgrp == target);
3697 return ret;
3698 }
3699
3700 static void check_for_release(struct cgroup *cgrp)
3701 {
3702 /* All of these checks rely on RCU to keep the cgroup
3703 * structure alive */
3704 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
3705 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
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);
3711 if (!cgroup_is_removed(cgrp) &&
3712 list_empty(&cgrp->release_list)) {
3713 list_add(&cgrp->release_list, &release_list);
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 {
3724 struct cgroup *cgrp = css->cgroup;
3725 int val;
3726 rcu_read_lock();
3727 val = atomic_dec_return(&css->refcnt);
3728 if (val == 1) {
3729 if (notify_on_release(cgrp)) {
3730 set_bit(CGRP_RELEASABLE, &cgrp->flags);
3731 check_for_release(cgrp);
3732 }
3733 cgroup_wakeup_rmdir_waiter(cgrp);
3734 }
3735 rcu_read_unlock();
3736 WARN_ON_ONCE(val < 1);
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.
3761 */
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;
3770 char *pathbuf = NULL, *agentbuf = NULL;
3771 struct cgroup *cgrp = list_entry(release_list.next,
3772 struct cgroup,
3773 release_list);
3774 list_del_init(&cgrp->release_list);
3775 spin_unlock(&release_list_lock);
3776 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
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;
3784
3785 i = 0;
3786 argv[i++] = agentbuf;
3787 argv[i++] = pathbuf;
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);
3801 mutex_lock(&cgroup_mutex);
3802 continue_free:
3803 kfree(pathbuf);
3804 kfree(agentbuf);
3805 spin_lock(&release_list_lock);
3806 }
3807 spin_unlock(&release_list_lock);
3808 mutex_unlock(&cgroup_mutex);
3809 }
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);
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,
3861 const struct cgroup_subsys_state *root)
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
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(&current->cgroups->refcount);
4102 rcu_read_unlock();
4103 return count;
4104 }
4105
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 = "?";
4124 seq_printf(seq, "Root %d group %s\n",
4125 c->root->hierarchy_id, name);
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
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
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
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 */