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

Archive Download this file



interactive