cgroup: fix spurious lockdep warning in cgroup_exit()
[pandora-kernel.git] / kernel / cgroup.c
index 771d1b8..60fd6f1 100644 (file)
@@ -52,7 +52,6 @@
 #include <linux/pid_namespace.h>
 #include <linux/idr.h>
 #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
-#include <linux/flex_array.h> /* used in cgroup_attach_task */
 #include <linux/kthread.h>
 #include <linux/delay.h>
 
@@ -81,12 +80,21 @@ static DEFINE_MUTEX(cgroup_tree_mutex);
 /*
  * cgroup_mutex is the master lock.  Any modification to cgroup or its
  * hierarchy must be performed while holding it.
+ *
+ * css_set_rwsem protects task->cgroups pointer, the list of css_set
+ * objects, and the chain of tasks off each css_set.
+ *
+ * These locks are exported if CONFIG_PROVE_RCU so that accessors in
+ * cgroup.h can use them for lockdep annotations.
  */
 #ifdef CONFIG_PROVE_RCU
 DEFINE_MUTEX(cgroup_mutex);
-EXPORT_SYMBOL_GPL(cgroup_mutex);       /* only for lockdep */
+DECLARE_RWSEM(css_set_rwsem);
+EXPORT_SYMBOL_GPL(cgroup_mutex);
+EXPORT_SYMBOL_GPL(css_set_rwsem);
 #else
 static DEFINE_MUTEX(cgroup_mutex);
+static DECLARE_RWSEM(css_set_rwsem);
 #endif
 
 /*
@@ -130,14 +138,17 @@ static const char *cgroup_subsys_name[] = {
 #undef SUBSYS
 
 /*
- * The dummy hierarchy, reserved for the subsystems that are otherwise
+ * The default hierarchy, reserved for the subsystems that are otherwise
  * unattached - it never has more than a single cgroup, and all tasks are
  * part of that cgroup.
  */
-static struct cgroupfs_root cgroup_dummy_root;
+struct cgroup_root cgrp_dfl_root;
 
-/* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
-static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup;
+/*
+ * The default hierarchy always exists but is hidden until mounted for the
+ * first time.  This is for backward compatibility.
+ */
+static bool cgrp_dfl_root_visible;
 
 /* The list of hierarchy roots */
 
@@ -167,8 +178,8 @@ static int need_forkexit_callback __read_mostly;
 static struct cftype cgroup_base_files[];
 
 static void cgroup_put(struct cgroup *cgrp);
-static int rebind_subsystems(struct cgroupfs_root *root,
-                            unsigned long added_mask, unsigned removed_mask);
+static int rebind_subsystems(struct cgroup_root *dst_root,
+                            unsigned long ss_mask);
 static void cgroup_destroy_css_killed(struct cgroup *cgrp);
 static int cgroup_destroy_locked(struct cgroup *cgrp);
 static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
@@ -281,8 +292,8 @@ static int notify_on_release(const struct cgroup *cgrp)
        for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT &&                \
             (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
 
-/* iterate across the active hierarchies */
-#define for_each_active_root(root)                                     \
+/* iterate across the hierarchies */
+#define for_each_root(root)                                            \
        list_for_each_entry((root), &cgroup_roots, root_list)
 
 /**
@@ -330,22 +341,23 @@ struct cgrp_cset_link {
        struct list_head        cgrp_link;
 };
 
-/* The default css_set - used by init and its children prior to any
+/*
+ * The default css_set - used by init and its children prior to any
  * hierarchies being mounted. It contains a pointer to the root state
  * for each subsystem. Also used to anchor the list of css_sets. Not
  * reference-counted, to improve performance when child cgroups
  * haven't been created.
  */
+static struct css_set init_css_set = {
+       .refcount               = ATOMIC_INIT(1),
+       .cgrp_links             = LIST_HEAD_INIT(init_css_set.cgrp_links),
+       .tasks                  = LIST_HEAD_INIT(init_css_set.tasks),
+       .mg_tasks               = LIST_HEAD_INIT(init_css_set.mg_tasks),
+       .mg_preload_node        = LIST_HEAD_INIT(init_css_set.mg_preload_node),
+       .mg_node                = LIST_HEAD_INIT(init_css_set.mg_node),
+};
 
-static struct css_set init_css_set;
-static struct cgrp_cset_link init_cgrp_cset_link;
-
-/*
- * css_set_rwsem protects the list of css_set objects, and the chain of
- * tasks off each css_set.
- */
-static DECLARE_RWSEM(css_set_rwsem);
-static int css_set_count;
+static int css_set_count       = 1;    /* 1 for init_css_set */
 
 /*
  * hash table for cgroup groups. This improves the performance to find
@@ -505,7 +517,7 @@ static struct css_set *find_existing_css_set(struct css_set *old_cset,
                                        struct cgroup *cgrp,
                                        struct cgroup_subsys_state *template[])
 {
-       struct cgroupfs_root *root = cgrp->root;
+       struct cgroup_root *root = cgrp->root;
        struct cgroup_subsys *ss;
        struct css_set *cset;
        unsigned long key;
@@ -517,7 +529,7 @@ static struct css_set *find_existing_css_set(struct css_set *old_cset,
         * won't change, so no need for locking.
         */
        for_each_subsys(ss, i) {
-               if (root->subsys_mask & (1UL << i)) {
+               if (root->cgrp.subsys_mask & (1UL << i)) {
                        /* Subsystem is in this hierarchy. So we want
                         * the subsystem state from the new
                         * cgroup */
@@ -644,6 +656,9 @@ static struct css_set *find_css_set(struct css_set *old_cset,
        atomic_set(&cset->refcount, 1);
        INIT_LIST_HEAD(&cset->cgrp_links);
        INIT_LIST_HEAD(&cset->tasks);
+       INIT_LIST_HEAD(&cset->mg_tasks);
+       INIT_LIST_HEAD(&cset->mg_preload_node);
+       INIT_LIST_HEAD(&cset->mg_node);
        INIT_HLIST_NODE(&cset->hlist);
 
        /* Copy the set of subsystem state objects generated in
@@ -673,21 +688,20 @@ static struct css_set *find_css_set(struct css_set *old_cset,
        return cset;
 }
 
-static struct cgroupfs_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
+static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
 {
-       struct cgroup *top_cgrp = kf_root->kn->priv;
+       struct cgroup *root_cgrp = kf_root->kn->priv;
 
-       return top_cgrp->root;
+       return root_cgrp->root;
 }
 
-static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
+static int cgroup_init_root_id(struct cgroup_root *root)
 {
        int id;
 
        lockdep_assert_held(&cgroup_mutex);
 
-       id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
-                             GFP_KERNEL);
+       id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
        if (id < 0)
                return id;
 
@@ -695,7 +709,7 @@ static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
        return 0;
 }
 
-static void cgroup_exit_root_id(struct cgroupfs_root *root)
+static void cgroup_exit_root_id(struct cgroup_root *root)
 {
        lockdep_assert_held(&cgroup_mutex);
 
@@ -705,7 +719,7 @@ static void cgroup_exit_root_id(struct cgroupfs_root *root)
        }
 }
 
-static void cgroup_free_root(struct cgroupfs_root *root)
+static void cgroup_free_root(struct cgroup_root *root)
 {
        if (root) {
                /* hierarhcy ID shoulid already have been released */
@@ -716,9 +730,9 @@ static void cgroup_free_root(struct cgroupfs_root *root)
        }
 }
 
-static void cgroup_destroy_root(struct cgroupfs_root *root)
+static void cgroup_destroy_root(struct cgroup_root *root)
 {
-       struct cgroup *cgrp = &root->top_cgroup;
+       struct cgroup *cgrp = &root->cgrp;
        struct cgrp_cset_link *link, *tmp_link;
 
        mutex_lock(&cgroup_tree_mutex);
@@ -728,7 +742,7 @@ static void cgroup_destroy_root(struct cgroupfs_root *root)
        BUG_ON(!list_empty(&cgrp->children));
 
        /* Rebind all subsystems back to the default hierarchy */
-       WARN_ON(rebind_subsystems(root, 0, root->subsys_mask));
+       rebind_subsystems(&cgrp_dfl_root, cgrp->subsys_mask);
 
        /*
         * Release all the links from cset_links to this hierarchy's
@@ -757,27 +771,17 @@ static void cgroup_destroy_root(struct cgroupfs_root *root)
        cgroup_free_root(root);
 }
 
-/*
- * Return the cgroup for "task" from the given hierarchy. Must be
- * called with cgroup_mutex and css_set_rwsem held.
- */
-static struct cgroup *task_cgroup_from_root(struct task_struct *task,
-                                           struct cgroupfs_root *root)
+/* look up cgroup associated with given css_set on the specified hierarchy */
+static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
+                                           struct cgroup_root *root)
 {
-       struct css_set *cset;
        struct cgroup *res = NULL;
 
        lockdep_assert_held(&cgroup_mutex);
        lockdep_assert_held(&css_set_rwsem);
 
-       /*
-        * No need to lock the task - since we hold cgroup_mutex the
-        * task can't change groups, so the only thing that can happen
-        * is that it exits and its css is set back to init_css_set.
-        */
-       cset = task_css_set(task);
        if (cset == &init_css_set) {
-               res = &root->top_cgroup;
+               res = &root->cgrp;
        } else {
                struct cgrp_cset_link *link;
 
@@ -796,10 +800,21 @@ static struct cgroup *task_cgroup_from_root(struct task_struct *task,
 }
 
 /*
- * There is one global cgroup mutex. We also require taking
- * task_lock() when dereferencing a task's cgroup subsys pointers.
- * See "The task_lock() exception", at the end of this comment.
- *
+ * Return the cgroup for "task" from the given hierarchy. Must be
+ * called with cgroup_mutex and css_set_rwsem held.
+ */
+static struct cgroup *task_cgroup_from_root(struct task_struct *task,
+                                           struct cgroup_root *root)
+{
+       /*
+        * No need to lock the task - since we hold cgroup_mutex the
+        * task can't change groups, so the only thing that can happen
+        * is that it exits and its css is set back to init_css_set.
+        */
+       return cset_cgroup_from_root(task_css_set(task), root);
+}
+
+/*
  * A task must hold cgroup_mutex to modify cgroups.
  *
  * Any task can increment and decrement the count field without lock.
@@ -825,21 +840,9 @@ static struct cgroup *task_cgroup_from_root(struct task_struct *task,
  * A cgroup can only be deleted if both its 'count' of using tasks
  * is zero, and its list of 'children' cgroups is empty.  Since all
  * tasks in the system use _some_ cgroup, and since there is always at
- * least one task in the system (init, pid == 1), therefore, top_cgroup
+ * least one task in the system (init, pid == 1), therefore, root cgroup
  * always has either children cgroups and/or using tasks.  So we don't
- * need a special hack to ensure that top_cgroup cannot be deleted.
- *
- *     The task_lock() exception
- *
- * The need for this exception arises from the action of
- * cgroup_attach_task(), which overwrites one task's cgroup pointer with
- * another.  It does so using cgroup_mutex, however there are
- * several performance critical places that need to reference
- * task->cgroup without the expense of grabbing a system global
- * mutex.  Therefore except as noted below, when dereferencing or, as
- * in cgroup_attach_task(), modifying a task's cgroup pointer we use
- * task_lock(), which acts on a spinlock (task->alloc_lock) already in
- * the task_struct routinely used for such matters.
+ * need a special hack to ensure that root cgroup cannot be deleted.
  *
  * P.S.  One more locking exception.  RCU is used to guard the
  * update of a tasks cgroup pointer by cgroup_attach_task()
@@ -905,7 +908,7 @@ static void cgroup_free_fn(struct work_struct *work)
                kfree(cgrp);
        } else {
                /*
-                * This is top cgroup's refcnt reaching zero, which
+                * This is root cgroup's refcnt reaching zero, which
                 * indicates that the root should be released.
                 */
                cgroup_destroy_root(cgrp->root);
@@ -976,81 +979,97 @@ static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
        }
 }
 
-static int rebind_subsystems(struct cgroupfs_root *root,
-                            unsigned long added_mask, unsigned removed_mask)
+static int rebind_subsystems(struct cgroup_root *dst_root,
+                            unsigned long ss_mask)
 {
-       struct cgroup *cgrp = &root->top_cgroup;
        struct cgroup_subsys *ss;
-       int i, ret;
+       int ssid, ret;
 
        lockdep_assert_held(&cgroup_tree_mutex);
        lockdep_assert_held(&cgroup_mutex);
 
-       /* Check that any added subsystems are currently free */
-       for_each_subsys(ss, i)
-               if ((added_mask & (1 << i)) && ss->root != &cgroup_dummy_root)
+       for_each_subsys(ss, ssid) {
+               if (!(ss_mask & (1 << ssid)))
+                       continue;
+
+               /* if @ss is on the dummy_root, we can always move it */
+               if (ss->root == &cgrp_dfl_root)
+                       continue;
+
+               /* if @ss has non-root cgroups attached to it, can't move */
+               if (!list_empty(&ss->root->cgrp.children))
                        return -EBUSY;
 
-       ret = cgroup_populate_dir(cgrp, added_mask);
-       if (ret)
-               return ret;
+               /* can't move between two non-dummy roots either */
+               if (dst_root != &cgrp_dfl_root)
+                       return -EBUSY;
+       }
+
+       ret = cgroup_populate_dir(&dst_root->cgrp, ss_mask);
+       if (ret) {
+               if (dst_root != &cgrp_dfl_root)
+                       return ret;
+
+               /*
+                * Rebinding back to the default root is not allowed to
+                * fail.  Using both default and non-default roots should
+                * be rare.  Moving subsystems back and forth even more so.
+                * Just warn about it and continue.
+                */
+               if (cgrp_dfl_root_visible) {
+                       pr_warning("cgroup: failed to create files (%d) while rebinding 0x%lx to default root\n",
+                                  ret, ss_mask);
+                       pr_warning("cgroup: you may retry by moving them to a different hierarchy and unbinding\n");
+               }
+       }
 
        /*
         * Nothing can fail from this point on.  Remove files for the
         * removed subsystems and rebind each subsystem.
         */
        mutex_unlock(&cgroup_mutex);
-       cgroup_clear_dir(cgrp, removed_mask);
+       for_each_subsys(ss, ssid)
+               if (ss_mask & (1 << ssid))
+                       cgroup_clear_dir(&ss->root->cgrp, 1 << ssid);
        mutex_lock(&cgroup_mutex);
 
-       for_each_subsys(ss, i) {
-               unsigned long bit = 1UL << i;
-
-               if (bit & added_mask) {
-                       /* We're binding this subsystem to this hierarchy */
-                       BUG_ON(cgroup_css(cgrp, ss));
-                       BUG_ON(!cgroup_css(cgroup_dummy_top, ss));
-                       BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top);
+       for_each_subsys(ss, ssid) {
+               struct cgroup_root *src_root;
+               struct cgroup_subsys_state *css;
 
-                       rcu_assign_pointer(cgrp->subsys[i],
-                                          cgroup_css(cgroup_dummy_top, ss));
-                       cgroup_css(cgrp, ss)->cgroup = cgrp;
+               if (!(ss_mask & (1 << ssid)))
+                       continue;
 
-                       ss->root = root;
-                       if (ss->bind)
-                               ss->bind(cgroup_css(cgrp, ss));
+               src_root = ss->root;
+               css = cgroup_css(&src_root->cgrp, ss);
 
-                       /* refcount was already taken, and we're keeping it */
-                       root->subsys_mask |= bit;
-               } else if (bit & removed_mask) {
-                       /* We're removing this subsystem */
-                       BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss));
-                       BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp);
+               WARN_ON(!css || cgroup_css(&dst_root->cgrp, ss));
 
-                       if (ss->bind)
-                               ss->bind(cgroup_css(cgroup_dummy_top, ss));
+               RCU_INIT_POINTER(src_root->cgrp.subsys[ssid], NULL);
+               rcu_assign_pointer(dst_root->cgrp.subsys[ssid], css);
+               ss->root = dst_root;
+               css->cgroup = &dst_root->cgrp;
 
-                       cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top;
-                       RCU_INIT_POINTER(cgrp->subsys[i], NULL);
+               src_root->cgrp.subsys_mask &= ~(1 << ssid);
+               dst_root->cgrp.subsys_mask |= 1 << ssid;
 
-                       cgroup_subsys[i]->root = &cgroup_dummy_root;
-                       root->subsys_mask &= ~bit;
-               }
+               if (ss->bind)
+                       ss->bind(css);
        }
 
-       kernfs_activate(cgrp->kn);
+       kernfs_activate(dst_root->cgrp.kn);
        return 0;
 }
 
 static int cgroup_show_options(struct seq_file *seq,
                               struct kernfs_root *kf_root)
 {
-       struct cgroupfs_root *root = cgroup_root_from_kf(kf_root);
+       struct cgroup_root *root = cgroup_root_from_kf(kf_root);
        struct cgroup_subsys *ss;
        int ssid;
 
        for_each_subsys(ss, ssid)
-               if (root->subsys_mask & (1 << ssid))
+               if (root->cgrp.subsys_mask & (1 << ssid))
                        seq_printf(seq, ",%s", ss->name);
        if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
                seq_puts(seq, ",sane_behavior");
@@ -1064,7 +1083,7 @@ static int cgroup_show_options(struct seq_file *seq,
                seq_printf(seq, ",release_agent=%s", root->release_agent_path);
        spin_unlock(&release_agent_path_lock);
 
-       if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
+       if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
                seq_puts(seq, ",clone_children");
        if (strlen(root->name))
                seq_printf(seq, ",name=%s", root->name);
@@ -1188,16 +1207,6 @@ static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
                        return -ENOENT;
        }
 
-       /*
-        * If the 'all' option was specified select all the subsystems,
-        * otherwise if 'none', 'name=' and a subsystem name options
-        * were not specified, let's default to 'all'
-        */
-       if (all_ss || (!one_ss && !opts->none && !opts->name))
-               for_each_subsys(ss, i)
-                       if (!ss->disabled)
-                               set_bit(i, &opts->subsys_mask);
-
        /* Consistency checks */
 
        if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
@@ -1209,6 +1218,23 @@ static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
                        pr_err("cgroup: sane_behavior: noprefix, xattr, clone_children, release_agent and name are not allowed\n");
                        return -EINVAL;
                }
+       } else {
+               /*
+                * If the 'all' option was specified select all the
+                * subsystems, otherwise if 'none', 'name=' and a subsystem
+                * name options were not specified, let's default to 'all'
+                */
+               if (all_ss || (!one_ss && !opts->none && !opts->name))
+                       for_each_subsys(ss, i)
+                               if (!ss->disabled)
+                                       set_bit(i, &opts->subsys_mask);
+
+               /*
+                * We either have to specify by name or by subsystems. (So
+                * all empty hierarchies must have a name).
+                */
+               if (!opts->subsys_mask && !opts->name)
+                       return -EINVAL;
        }
 
        /*
@@ -1224,20 +1250,13 @@ static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
        if (opts->subsys_mask && opts->none)
                return -EINVAL;
 
-       /*
-        * We either have to specify by name or by subsystems. (So all
-        * empty hierarchies must have a name).
-        */
-       if (!opts->subsys_mask && !opts->name)
-               return -EINVAL;
-
        return 0;
 }
 
 static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
 {
        int ret = 0;
-       struct cgroupfs_root *root = cgroup_root_from_kf(kf_root);
+       struct cgroup_root *root = cgroup_root_from_kf(kf_root);
        struct cgroup_sb_opts opts;
        unsigned long added_mask, removed_mask;
 
@@ -1254,12 +1273,12 @@ static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
        if (ret)
                goto out_unlock;
 
-       if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
+       if (opts.subsys_mask != root->cgrp.subsys_mask || opts.release_agent)
                pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
                           task_tgid_nr(current), current->comm);
 
-       added_mask = opts.subsys_mask & ~root->subsys_mask;
-       removed_mask = root->subsys_mask & ~opts.subsys_mask;
+       added_mask = opts.subsys_mask & ~root->cgrp.subsys_mask;
+       removed_mask = root->cgrp.subsys_mask & ~opts.subsys_mask;
 
        /* Don't allow flags or name to change at remount */
        if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
@@ -1272,15 +1291,17 @@ static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
        }
 
        /* remounting is not allowed for populated hierarchies */
-       if (!list_empty(&root->top_cgroup.children)) {
+       if (!list_empty(&root->cgrp.children)) {
                ret = -EBUSY;
                goto out_unlock;
        }
 
-       ret = rebind_subsystems(root, added_mask, removed_mask);
+       ret = rebind_subsystems(root, added_mask);
        if (ret)
                goto out_unlock;
 
+       rebind_subsystems(&cgrp_dfl_root, removed_mask);
+
        if (opts.release_agent) {
                spin_lock(&release_agent_path_lock);
                strcpy(root->release_agent_path, opts.release_agent);
@@ -1322,8 +1343,6 @@ static void cgroup_enable_task_cg_lists(void)
         */
        read_lock(&tasklist_lock);
        do_each_thread(g, p) {
-               task_lock(p);
-
                WARN_ON_ONCE(!list_empty(&p->cg_list) ||
                             task_css_set(p) != &init_css_set);
 
@@ -1331,11 +1350,17 @@ static void cgroup_enable_task_cg_lists(void)
                 * We should check if the process is exiting, otherwise
                 * it will race with cgroup_exit() in that the list
                 * entry won't be deleted though the process has exited.
+                * Do it while holding siglock so that we don't end up
+                * racing against cgroup_exit().
                 */
-               if (!(p->flags & PF_EXITING))
-                       list_add(&p->cg_list, &task_css_set(p)->tasks);
+               spin_lock_irq(&p->sighand->siglock);
+               if (!(p->flags & PF_EXITING)) {
+                       struct css_set *cset = task_css_set(p);
 
-               task_unlock(p);
+                       list_add(&p->cg_list, &cset->tasks);
+                       get_css_set(cset);
+               }
+               spin_unlock_irq(&p->sighand->siglock);
        } while_each_thread(g, p);
        read_unlock(&tasklist_lock);
 out_unlock:
@@ -1354,29 +1379,16 @@ static void init_cgroup_housekeeping(struct cgroup *cgrp)
        cgrp->dummy_css.cgroup = cgrp;
 }
 
-static void init_cgroup_root(struct cgroupfs_root *root)
+static void init_cgroup_root(struct cgroup_root *root,
+                            struct cgroup_sb_opts *opts)
 {
-       struct cgroup *cgrp = &root->top_cgroup;
+       struct cgroup *cgrp = &root->cgrp;
 
        INIT_LIST_HEAD(&root->root_list);
        atomic_set(&root->nr_cgrps, 1);
        cgrp->root = root;
        init_cgroup_housekeeping(cgrp);
        idr_init(&root->cgroup_idr);
-}
-
-static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
-{
-       struct cgroupfs_root *root;
-
-       if (!opts->subsys_mask && !opts->none)
-               return ERR_PTR(-EINVAL);
-
-       root = kzalloc(sizeof(*root), GFP_KERNEL);
-       if (!root)
-               return ERR_PTR(-ENOMEM);
-
-       init_cgroup_root(root);
 
        root->flags = opts->flags;
        if (opts->release_agent)
@@ -1384,14 +1396,13 @@ static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
        if (opts->name)
                strcpy(root->name, opts->name);
        if (opts->cpuset_clone_children)
-               set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
-       return root;
+               set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
 }
 
-static int cgroup_setup_root(struct cgroupfs_root *root, unsigned long ss_mask)
+static int cgroup_setup_root(struct cgroup_root *root, unsigned long ss_mask)
 {
        LIST_HEAD(tmp_links);
-       struct cgroup *root_cgrp = &root->top_cgroup;
+       struct cgroup *root_cgrp = &root->cgrp;
        struct css_set *cset;
        int i, ret;
 
@@ -1413,8 +1424,7 @@ static int cgroup_setup_root(struct cgroupfs_root *root, unsigned long ss_mask)
        if (ret)
                goto out;
 
-       /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
-       ret = cgroup_init_root_id(root, 2, 0);
+       ret = cgroup_init_root_id(root);
        if (ret)
                goto out;
 
@@ -1431,7 +1441,7 @@ static int cgroup_setup_root(struct cgroupfs_root *root, unsigned long ss_mask)
        if (ret)
                goto destroy_root;
 
-       ret = rebind_subsystems(root, ss_mask, 0);
+       ret = rebind_subsystems(root, ss_mask);
        if (ret)
                goto destroy_root;
 
@@ -1444,7 +1454,7 @@ static int cgroup_setup_root(struct cgroupfs_root *root, unsigned long ss_mask)
        cgroup_root_count++;
 
        /*
-        * Link the top cgroup in this hierarchy into all the css_set
+        * Link the root cgroup in this hierarchy into all the css_set
         * objects.
         */
        down_write(&css_set_rwsem);
@@ -1473,7 +1483,7 @@ static struct dentry *cgroup_mount(struct file_system_type *fs_type,
                         int flags, const char *unused_dev_name,
                         void *data)
 {
-       struct cgroupfs_root *root;
+       struct cgroup_root *root;
        struct cgroup_sb_opts opts;
        struct dentry *dentry;
        int ret;
@@ -1494,9 +1504,20 @@ retry:
                goto out_unlock;
 
        /* look for a matching existing root */
-       for_each_active_root(root) {
+       if (!opts.subsys_mask && !opts.none && !opts.name) {
+               cgrp_dfl_root_visible = true;
+               root = &cgrp_dfl_root;
+               cgroup_get(&root->cgrp);
+               ret = 0;
+               goto out_unlock;
+       }
+
+       for_each_root(root) {
                bool name_match = false;
 
+               if (root == &cgrp_dfl_root)
+                       continue;
+
                /*
                 * If we asked for a name then it must match.  Also, if
                 * name matches but sybsys_mask doesn't, we should fail.
@@ -1513,7 +1534,7 @@ retry:
                 * subsystems) then they must match.
                 */
                if ((opts.subsys_mask || opts.none) &&
-                   (opts.subsys_mask != root->subsys_mask)) {
+                   (opts.subsys_mask != root->cgrp.subsys_mask)) {
                        if (!name_match)
                                continue;
                        ret = -EBUSY;
@@ -1531,13 +1552,13 @@ retry:
                }
 
                /*
-                * A root's lifetime is governed by its top cgroup.  Zero
+                * A root's lifetime is governed by its root cgroup.  Zero
                 * ref indicate that the root is being destroyed.  Wait for
                 * destruction to complete so that the subsystems are free.
                 * We can use wait_queue for the wait but this path is
                 * super cold.  Let's just sleep for a bit and retry.
                 */
-               if (!atomic_inc_not_zero(&root->top_cgroup.refcnt)) {
+               if (!atomic_inc_not_zero(&root->cgrp.refcnt)) {
                        mutex_unlock(&cgroup_mutex);
                        mutex_unlock(&cgroup_tree_mutex);
                        kfree(opts.release_agent);
@@ -1550,13 +1571,24 @@ retry:
                goto out_unlock;
        }
 
-       /* no such thing, create a new one */
-       root = cgroup_root_from_opts(&opts);
-       if (IS_ERR(root)) {
-               ret = PTR_ERR(root);
+       /*
+        * No such thing, create a new one.  name= matching without subsys
+        * specification is allowed for already existing hierarchies but we
+        * can't create new one without subsys specification.
+        */
+       if (!opts.subsys_mask && !opts.none) {
+               ret = -EINVAL;
                goto out_unlock;
        }
 
+       root = kzalloc(sizeof(*root), GFP_KERNEL);
+       if (!root) {
+               ret = -ENOMEM;
+               goto out_unlock;
+       }
+
+       init_cgroup_root(root, &opts);
+
        ret = cgroup_setup_root(root, opts.subsys_mask);
        if (ret)
                cgroup_free_root(root);
@@ -1573,16 +1605,16 @@ out_unlock:
 
        dentry = kernfs_mount(fs_type, flags, root->kf_root);
        if (IS_ERR(dentry))
-               cgroup_put(&root->top_cgroup);
+               cgroup_put(&root->cgrp);
        return dentry;
 }
 
 static void cgroup_kill_sb(struct super_block *sb)
 {
        struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
-       struct cgroupfs_root *root = cgroup_root_from_kf(kf_root);
+       struct cgroup_root *root = cgroup_root_from_kf(kf_root);
 
-       cgroup_put(&root->top_cgroup);
+       cgroup_put(&root->cgrp);
        kernfs_kill_sb(sb);
 }
 
@@ -1609,7 +1641,7 @@ static struct kobject *cgroup_kobj;
  */
 char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
 {
-       struct cgroupfs_root *root;
+       struct cgroup_root *root;
        struct cgroup *cgrp;
        int hierarchy_id = 1;
        char *path = NULL;
@@ -1634,20 +1666,26 @@ char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
 }
 EXPORT_SYMBOL_GPL(task_cgroup_path);
 
-/*
- * Control Group taskset
- */
-struct task_and_cgroup {
-       struct task_struct      *task;
-       struct cgroup           *cgrp;
-       struct css_set          *cset;
-};
-
+/* used to track tasks and other necessary states during migration */
 struct cgroup_taskset {
-       struct task_and_cgroup  single;
-       struct flex_array       *tc_array;
-       int                     tc_array_len;
-       int                     idx;
+       /* the src and dst cset list running through cset->mg_node */
+       struct list_head        src_csets;
+       struct list_head        dst_csets;
+
+       /*
+        * Fields for cgroup_taskset_*() iteration.
+        *
+        * Before migration is committed, the target migration tasks are on
+        * ->mg_tasks of the csets on ->src_csets.  After, on ->mg_tasks of
+        * the csets on ->dst_csets.  ->csets point to either ->src_csets
+        * or ->dst_csets depending on whether migration is committed.
+        *
+        * ->cur_csets and ->cur_task point to the current task position
+        * during iteration.
+        */
+       struct list_head        *csets;
+       struct css_set          *cur_cset;
+       struct task_struct      *cur_task;
 };
 
 /**
@@ -1658,12 +1696,10 @@ struct cgroup_taskset {
  */
 struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
 {
-       if (tset->tc_array) {
-               tset->idx = 0;
-               return cgroup_taskset_next(tset);
-       } else {
-               return tset->single.task;
-       }
+       tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
+       tset->cur_task = NULL;
+
+       return cgroup_taskset_next(tset);
 }
 
 /**
@@ -1675,13 +1711,27 @@ struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
  */
 struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
 {
-       struct task_and_cgroup *tc;
+       struct css_set *cset = tset->cur_cset;
+       struct task_struct *task = tset->cur_task;
 
-       if (!tset->tc_array || tset->idx >= tset->tc_array_len)
-               return NULL;
+       while (&cset->mg_node != tset->csets) {
+               if (!task)
+                       task = list_first_entry(&cset->mg_tasks,
+                                               struct task_struct, cg_list);
+               else
+                       task = list_next_entry(task, cg_list);
+
+               if (&task->cg_list != &cset->mg_tasks) {
+                       tset->cur_cset = cset;
+                       tset->cur_task = task;
+                       return task;
+               }
 
-       tc = flex_array_get(tset->tc_array, tset->idx++);
-       return tc->task;
+               cset = list_next_entry(cset, mg_node);
+               task = NULL;
+       }
+
+       return NULL;
 }
 
 /**
@@ -1709,11 +1759,16 @@ static void cgroup_task_migrate(struct cgroup *old_cgrp,
        WARN_ON_ONCE(tsk->flags & PF_EXITING);
        old_cset = task_css_set(tsk);
 
-       task_lock(tsk);
+       get_css_set(new_cset);
        rcu_assign_pointer(tsk->cgroups, new_cset);
-       task_unlock(tsk);
 
-       list_move(&tsk->cg_list, &new_cset->tasks);
+       /*
+        * Use move_tail so that cgroup_taskset_first() still returns the
+        * leader after migration.  This works because cgroup_migrate()
+        * ensures that the dst_cset of the leader is the first on the
+        * tset's dst_csets list.
+        */
+       list_move_tail(&tsk->cg_list, &new_cset->mg_tasks);
 
        /*
         * We just gained a reference on old_cset by taking it from the
@@ -1725,95 +1780,191 @@ static void cgroup_task_migrate(struct cgroup *old_cgrp,
 }
 
 /**
- * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
- * @cgrp: the cgroup to attach to
- * @leader: the task or the leader of the threadgroup to be attached
- * @threadgroup: attach the whole threadgroup?
+ * cgroup_migrate_finish - cleanup after attach
+ * @preloaded_csets: list of preloaded css_sets
  *
- * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
- * task_lock of @tsk or each thread in the threadgroup individually in turn.
+ * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst().  See
+ * those functions for details.
  */
-static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *leader,
-                             bool threadgroup)
+static void cgroup_migrate_finish(struct list_head *preloaded_csets)
 {
-       int ret, i, group_size;
-       struct cgroupfs_root *root = cgrp->root;
-       struct cgroup_subsys_state *css, *failed_css = NULL;
-       /* threadgroup list cursor and array */
-       struct task_struct *task;
-       struct task_and_cgroup *tc;
-       struct flex_array *group;
-       struct cgroup_taskset tset = { };
+       struct css_set *cset, *tmp_cset;
 
-       /*
-        * step 0: in order to do expensive, possibly blocking operations for
-        * every thread, we cannot iterate the thread group list, since it needs
-        * rcu or tasklist locked. instead, build an array of all threads in the
-        * group - group_rwsem prevents new threads from appearing, and if
-        * threads exit, this will just be an over-estimate.
-        */
-       if (threadgroup)
-               group_size = get_nr_threads(leader);
-       else
-               group_size = 1;
-       /* flex_array supports very large thread-groups better than kmalloc. */
-       group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
-       if (!group)
-               return -ENOMEM;
-       /* pre-allocate to guarantee space while iterating in rcu read-side. */
-       ret = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
-       if (ret)
-               goto out_free_group_list;
+       lockdep_assert_held(&cgroup_mutex);
+
+       down_write(&css_set_rwsem);
+       list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) {
+               cset->mg_src_cgrp = NULL;
+               cset->mg_dst_cset = NULL;
+               list_del_init(&cset->mg_preload_node);
+               put_css_set_locked(cset, false);
+       }
+       up_write(&css_set_rwsem);
+}
+
+/**
+ * cgroup_migrate_add_src - add a migration source css_set
+ * @src_cset: the source css_set to add
+ * @dst_cgrp: the destination cgroup
+ * @preloaded_csets: list of preloaded css_sets
+ *
+ * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp.  Pin
+ * @src_cset and add it to @preloaded_csets, which should later be cleaned
+ * up by cgroup_migrate_finish().
+ *
+ * This function may be called without holding threadgroup_lock even if the
+ * target is a process.  Threads may be created and destroyed but as long
+ * as cgroup_mutex is not dropped, no new css_set can be put into play and
+ * the preloaded css_sets are guaranteed to cover all migrations.
+ */
+static void cgroup_migrate_add_src(struct css_set *src_cset,
+                                  struct cgroup *dst_cgrp,
+                                  struct list_head *preloaded_csets)
+{
+       struct cgroup *src_cgrp;
+
+       lockdep_assert_held(&cgroup_mutex);
+       lockdep_assert_held(&css_set_rwsem);
+
+       src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
+
+       /* nothing to do if this cset already belongs to the cgroup */
+       if (src_cgrp == dst_cgrp)
+               return;
+
+       if (!list_empty(&src_cset->mg_preload_node))
+               return;
+
+       WARN_ON(src_cset->mg_src_cgrp);
+       WARN_ON(!list_empty(&src_cset->mg_tasks));
+       WARN_ON(!list_empty(&src_cset->mg_node));
+
+       src_cset->mg_src_cgrp = src_cgrp;
+       get_css_set(src_cset);
+       list_add(&src_cset->mg_preload_node, preloaded_csets);
+}
+
+/**
+ * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
+ * @dst_cgrp: the destination cgroup
+ * @preloaded_csets: list of preloaded source css_sets
+ *
+ * Tasks are about to be moved to @dst_cgrp and all the source css_sets
+ * have been preloaded to @preloaded_csets.  This function looks up and
+ * pins all destination css_sets, links each to its source, and put them on
+ * @preloaded_csets.
+ *
+ * This function must be called after cgroup_migrate_add_src() has been
+ * called on each migration source css_set.  After migration is performed
+ * using cgroup_migrate(), cgroup_migrate_finish() must be called on
+ * @preloaded_csets.
+ */
+static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
+                                     struct list_head *preloaded_csets)
+{
+       LIST_HEAD(csets);
+       struct css_set *src_cset;
+
+       lockdep_assert_held(&cgroup_mutex);
+
+       /* look up the dst cset for each src cset and link it to src */
+       list_for_each_entry(src_cset, preloaded_csets, mg_preload_node) {
+               struct css_set *dst_cset;
+
+               dst_cset = find_css_set(src_cset, dst_cgrp);
+               if (!dst_cset)
+                       goto err;
+
+               WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
+               src_cset->mg_dst_cset = dst_cset;
+
+               if (list_empty(&dst_cset->mg_preload_node))
+                       list_add(&dst_cset->mg_preload_node, &csets);
+               else
+                       put_css_set(dst_cset, false);
+       }
+
+       list_splice(&csets, preloaded_csets);
+       return 0;
+err:
+       cgroup_migrate_finish(&csets);
+       return -ENOMEM;
+}
+
+/**
+ * cgroup_migrate - migrate a process or task to a cgroup
+ * @cgrp: the destination cgroup
+ * @leader: the leader of the process or the task to migrate
+ * @threadgroup: whether @leader points to the whole process or a single task
+ *
+ * Migrate a process or task denoted by @leader to @cgrp.  If migrating a
+ * process, the caller must be holding threadgroup_lock of @leader.  The
+ * caller is also responsible for invoking cgroup_migrate_add_src() and
+ * cgroup_migrate_prepare_dst() on the targets before invoking this
+ * function and following up with cgroup_migrate_finish().
+ *
+ * As long as a controller's ->can_attach() doesn't fail, this function is
+ * guaranteed to succeed.  This means that, excluding ->can_attach()
+ * failure, when migrating multiple targets, the success or failure can be
+ * decided for all targets by invoking group_migrate_prepare_dst() before
+ * actually starting migrating.
+ */
+static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader,
+                         bool threadgroup)
+{
+       struct cgroup_taskset tset = {
+               .src_csets      = LIST_HEAD_INIT(tset.src_csets),
+               .dst_csets      = LIST_HEAD_INIT(tset.dst_csets),
+               .csets          = &tset.src_csets,
+       };
+       struct cgroup_subsys_state *css, *failed_css = NULL;
+       struct css_set *cset, *tmp_cset;
+       struct task_struct *task, *tmp_task;
+       int i, ret;
 
-       i = 0;
        /*
         * Prevent freeing of tasks while we take a snapshot. Tasks that are
         * already PF_EXITING could be freed from underneath us unless we
         * take an rcu_read_lock.
         */
-       down_read(&css_set_rwsem);
+       down_write(&css_set_rwsem);
        rcu_read_lock();
        task = leader;
        do {
-               struct task_and_cgroup ent;
-
                /* @task either already exited or can't exit until the end */
                if (task->flags & PF_EXITING)
                        goto next;
 
-               /* as per above, nr_threads may decrease, but not increase. */
-               BUG_ON(i >= group_size);
-               ent.task = task;
-               ent.cgrp = task_cgroup_from_root(task, root);
-               /* nothing to do if this task is already in the cgroup */
-               if (ent.cgrp == cgrp)
+               /* leave @task alone if post_fork() hasn't linked it yet */
+               if (list_empty(&task->cg_list))
+                       goto next;
+
+               cset = task_css_set(task);
+               if (!cset->mg_src_cgrp)
                        goto next;
+
                /*
-                * saying GFP_ATOMIC has no effect here because we did prealloc
-                * earlier, but it's good form to communicate our expectations.
+                * cgroup_taskset_first() must always return the leader.
+                * Take care to avoid disturbing the ordering.
                 */
-               ret = flex_array_put(group, i, &ent, GFP_ATOMIC);
-               BUG_ON(ret != 0);
-               i++;
+               list_move_tail(&task->cg_list, &cset->mg_tasks);
+               if (list_empty(&cset->mg_node))
+                       list_add_tail(&cset->mg_node, &tset.src_csets);
+               if (list_empty(&cset->mg_dst_cset->mg_node))
+                       list_move_tail(&cset->mg_dst_cset->mg_node,
+                                      &tset.dst_csets);
        next:
                if (!threadgroup)
                        break;
        } while_each_thread(leader, task);
        rcu_read_unlock();
-       up_read(&css_set_rwsem);
-       /* remember the number of threads in the array for later. */
-       group_size = i;
-       tset.tc_array = group;
-       tset.tc_array_len = group_size;
+       up_write(&css_set_rwsem);
 
        /* methods shouldn't be called if no task is actually migrating */
-       ret = 0;
-       if (!group_size)
-               goto out_free_group_list;
+       if (list_empty(&tset.src_csets))
+               return 0;
 
-       /*
-        * step 1: check that we can legitimately attach to the cgroup.
-        */
+       /* check that we can legitimately attach to the cgroup */
        for_each_css(css, i, cgrp) {
                if (css->ss->can_attach) {
                        ret = css->ss->can_attach(css, &tset);
@@ -1825,72 +1976,91 @@ static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *leader,
        }
 
        /*
-        * step 2: make sure css_sets exist for all threads to be migrated.
-        * we use find_css_set, which allocates a new one if necessary.
-        */
-       for (i = 0; i < group_size; i++) {
-               struct css_set *old_cset;
-
-               tc = flex_array_get(group, i);
-               old_cset = task_css_set(tc->task);
-               tc->cset = find_css_set(old_cset, cgrp);
-               if (!tc->cset) {
-                       ret = -ENOMEM;
-                       goto out_put_css_set_refs;
-               }
-       }
-
-       /*
-        * step 3: now that we're guaranteed success wrt the css_sets,
-        * proceed to move all tasks to the new cgroup.  There are no
-        * failure cases after here, so this is the commit point.
+        * Now that we're guaranteed success, proceed to move all tasks to
+        * the new cgroup.  There are no failure cases after here, so this
+        * is the commit point.
         */
        down_write(&css_set_rwsem);
-       for (i = 0; i < group_size; i++) {
-               tc = flex_array_get(group, i);
-               cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
+       list_for_each_entry(cset, &tset.src_csets, mg_node) {
+               list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list)
+                       cgroup_task_migrate(cset->mg_src_cgrp, task,
+                                           cset->mg_dst_cset);
        }
        up_write(&css_set_rwsem);
-       /* nothing is sensitive to fork() after this point. */
 
        /*
-        * step 4: do subsystem attach callbacks.
+        * Migration is committed, all target tasks are now on dst_csets.
+        * Nothing is sensitive to fork() after this point.  Notify
+        * controllers that migration is complete.
         */
+       tset.csets = &tset.dst_csets;
+
        for_each_css(css, i, cgrp)
                if (css->ss->attach)
                        css->ss->attach(css, &tset);
 
-       /*
-        * step 5: success! and cleanup
-        */
        ret = 0;
-out_put_css_set_refs:
-       if (ret) {
-               for (i = 0; i < group_size; i++) {
-                       tc = flex_array_get(group, i);
-                       if (!tc->cset)
-                               break;
-                       put_css_set(tc->cset, false);
-               }
-       }
+       goto out_release_tset;
+
 out_cancel_attach:
-       if (ret) {
-               for_each_css(css, i, cgrp) {
-                       if (css == failed_css)
-                               break;
-                       if (css->ss->cancel_attach)
-                               css->ss->cancel_attach(css, &tset);
-               }
+       for_each_css(css, i, cgrp) {
+               if (css == failed_css)
+                       break;
+               if (css->ss->cancel_attach)
+                       css->ss->cancel_attach(css, &tset);
+       }
+out_release_tset:
+       down_write(&css_set_rwsem);
+       list_splice_init(&tset.dst_csets, &tset.src_csets);
+       list_for_each_entry_safe(cset, tmp_cset, &tset.src_csets, mg_node) {
+               list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
+               list_del_init(&cset->mg_node);
        }
-out_free_group_list:
-       flex_array_free(group);
+       up_write(&css_set_rwsem);
+       return ret;
+}
+
+/**
+ * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
+ * @dst_cgrp: the cgroup to attach to
+ * @leader: the task or the leader of the threadgroup to be attached
+ * @threadgroup: attach the whole threadgroup?
+ *
+ * Call holding cgroup_mutex and threadgroup_lock of @leader.
+ */
+static int cgroup_attach_task(struct cgroup *dst_cgrp,
+                             struct task_struct *leader, bool threadgroup)
+{
+       LIST_HEAD(preloaded_csets);
+       struct task_struct *task;
+       int ret;
+
+       /* look up all src csets */
+       down_read(&css_set_rwsem);
+       rcu_read_lock();
+       task = leader;
+       do {
+               cgroup_migrate_add_src(task_css_set(task), dst_cgrp,
+                                      &preloaded_csets);
+               if (!threadgroup)
+                       break;
+       } while_each_thread(leader, task);
+       rcu_read_unlock();
+       up_read(&css_set_rwsem);
+
+       /* prepare dst csets and commit */
+       ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets);
+       if (!ret)
+               ret = cgroup_migrate(dst_cgrp, leader, threadgroup);
+
+       cgroup_migrate_finish(&preloaded_csets);
        return ret;
 }
 
 /*
  * Find the task_struct of the task to attach by vpid and pass it along to the
  * function to attach either it or all tasks in its threadgroup. Will lock
- * cgroup_mutex and threadgroup; may take task_lock of task.
+ * cgroup_mutex and threadgroup.
  */
 static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
 {
@@ -1975,13 +2145,16 @@ out_unlock_cgroup:
  */
 int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
 {
-       struct cgroupfs_root *root;
+       struct cgroup_root *root;
        int retval = 0;
 
        mutex_lock(&cgroup_mutex);
-       for_each_active_root(root) {
+       for_each_root(root) {
                struct cgroup *from_cgrp;
 
+               if (root == &cgrp_dfl_root)
+                       continue;
+
                down_read(&css_set_rwsem);
                from_cgrp = task_cgroup_from_root(from, root);
                up_read(&css_set_rwsem);
@@ -2009,9 +2182,9 @@ static int cgroup_procs_write(struct cgroup_subsys_state *css,
 }
 
 static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
-                                     struct cftype *cft, const char *buffer)
+                                     struct cftype *cft, char *buffer)
 {
-       struct cgroupfs_root *root = css->cgroup->root;
+       struct cgroup_root *root = css->cgroup->root;
 
        BUILD_BUG_ON(sizeof(root->release_agent_path) < PATH_MAX);
        if (!cgroup_lock_live_group(css->cgroup))
@@ -2151,6 +2324,14 @@ static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
        if (cgroup_sane_behavior(cgrp))
                return -EPERM;
 
+       /*
+        * We're gonna grab cgroup_tree_mutex which nests outside kernfs
+        * active_ref.  kernfs_rename() doesn't require active_ref
+        * protection.  Break them before grabbing cgroup_tree_mutex.
+        */
+       kernfs_break_active_protection(new_parent);
+       kernfs_break_active_protection(kn);
+
        mutex_lock(&cgroup_tree_mutex);
        mutex_lock(&cgroup_mutex);
 
@@ -2158,6 +2339,9 @@ static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
 
        mutex_unlock(&cgroup_mutex);
        mutex_unlock(&cgroup_tree_mutex);
+
+       kernfs_unbreak_active_protection(kn);
+       kernfs_unbreak_active_protection(new_parent);
        return ret;
 }
 
@@ -2197,6 +2381,8 @@ static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
 
        for (cft = cfts; cft->name[0] != '\0'; cft++) {
                /* does cft->flags tell us to skip this file on @cgrp? */
+               if ((cft->flags & CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
+                       continue;
                if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
                        continue;
                if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
@@ -2222,14 +2408,14 @@ static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
 {
        LIST_HEAD(pending);
        struct cgroup_subsys *ss = cfts[0].ss;
-       struct cgroup *root = &ss->root->top_cgroup;
+       struct cgroup *root = &ss->root->cgrp;
        struct cgroup_subsys_state *css;
        int ret = 0;
 
        lockdep_assert_held(&cgroup_tree_mutex);
 
        /* don't bother if @ss isn't attached */
-       if (ss->root == &cgroup_dummy_root)
+       if (ss->root == &cgrp_dfl_root)
                return 0;
 
        /* add/rm files for all cgroups created before */
@@ -2586,9 +2772,14 @@ static void css_advance_task_iter(struct css_task_iter *it)
                }
                link = list_entry(l, struct cgrp_cset_link, cset_link);
                cset = link->cset;
-       } while (list_empty(&cset->tasks));
+       } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks));
+
        it->cset_link = l;
-       it->task = cset->tasks.next;
+
+       if (!list_empty(&cset->tasks))
+               it->task = cset->tasks.next;
+       else
+               it->task = cset->mg_tasks.next;
 }
 
 /**
@@ -2632,24 +2823,29 @@ struct task_struct *css_task_iter_next(struct css_task_iter *it)
 {
        struct task_struct *res;
        struct list_head *l = it->task;
-       struct cgrp_cset_link *link;
+       struct cgrp_cset_link *link = list_entry(it->cset_link,
+                                       struct cgrp_cset_link, cset_link);
 
        /* If the iterator cg is NULL, we have no tasks */
        if (!it->cset_link)
                return NULL;
        res = list_entry(l, struct task_struct, cg_list);
-       /* Advance iterator to find next entry */
+
+       /*
+        * Advance iterator to find next entry.  cset->tasks is consumed
+        * first and then ->mg_tasks.  After ->mg_tasks, we move onto the
+        * next cset.
+        */
        l = l->next;
-       link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
-       if (l == &link->cset->tasks) {
-               /*
-                * We reached the end of this task list - move on to the
-                * next cgrp_cset_link.
-                */
+
+       if (l == &link->cset->tasks)
+               l = link->cset->mg_tasks.next;
+
+       if (l == &link->cset->mg_tasks)
                css_advance_task_iter(it);
-       } else {
+       else
                it->task = l;
-       }
+
        return res;
 }
 
@@ -2669,13 +2865,37 @@ void css_task_iter_end(struct css_task_iter *it)
  * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
  * @to: cgroup to which the tasks will be moved
  * @from: cgroup in which the tasks currently reside
+ *
+ * Locking rules between cgroup_post_fork() and the migration path
+ * guarantee that, if a task is forking while being migrated, the new child
+ * is guaranteed to be either visible in the source cgroup after the
+ * parent's migration is complete or put into the target cgroup.  No task
+ * can slip out of migration through forking.
  */
 int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
 {
+       LIST_HEAD(preloaded_csets);
+       struct cgrp_cset_link *link;
        struct css_task_iter it;
        struct task_struct *task;
-       int ret = 0;
+       int ret;
+
+       mutex_lock(&cgroup_mutex);
+
+       /* all tasks in @from are being moved, all csets are source */
+       down_read(&css_set_rwsem);
+       list_for_each_entry(link, &from->cset_links, cset_link)
+               cgroup_migrate_add_src(link->cset, to, &preloaded_csets);
+       up_read(&css_set_rwsem);
 
+       ret = cgroup_migrate_prepare_dst(to, &preloaded_csets);
+       if (ret)
+               goto out_err;
+
+       /*
+        * Migrate tasks one-by-one until @form is empty.  This fails iff
+        * ->can_attach() fails.
+        */
        do {
                css_task_iter_start(&from->dummy_css, &it);
                task = css_task_iter_next(&it);
@@ -2684,13 +2904,13 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
                css_task_iter_end(&it);
 
                if (task) {
-                       mutex_lock(&cgroup_mutex);
-                       ret = cgroup_attach_task(to, task, false);
-                       mutex_unlock(&cgroup_mutex);
+                       ret = cgroup_migrate(to, task, false);
                        put_task_struct(task);
                }
        } while (task && !ret);
-
+out_err:
+       cgroup_migrate_finish(&preloaded_csets);
+       mutex_unlock(&cgroup_mutex);
        return ret;
 }
 
@@ -3325,7 +3545,7 @@ static void css_release(struct percpu_ref *ref)
        struct cgroup_subsys_state *css =
                container_of(ref, struct cgroup_subsys_state, refcnt);
 
-       rcu_assign_pointer(css->cgroup->subsys[css->ss->id], NULL);
+       RCU_INIT_POINTER(css->cgroup->subsys[css->ss->id], NULL);
        call_rcu(&css->rcu_head, css_free_rcu_fn);
 }
 
@@ -3420,6 +3640,8 @@ static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
        cgroup_get(cgrp);
        css_get(css->parent);
 
+       cgrp->subsys_mask |= 1 << ss->id;
+
        if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
            parent->parent) {
                pr_warning("cgroup: %s (%d) created nested cgroup for controller \"%s\" which has incomplete hierarchy support. Nested cgroups may change behavior in the future.\n",
@@ -3447,11 +3669,18 @@ static long cgroup_create(struct cgroup *parent, const char *name,
                          umode_t mode)
 {
        struct cgroup *cgrp;
-       struct cgroupfs_root *root = parent->root;
+       struct cgroup_root *root = parent->root;
        int ssid, err;
        struct cgroup_subsys *ss;
        struct kernfs_node *kn;
 
+       /*
+        * XXX: The default hierarchy isn't fully implemented yet.  Block
+        * !root cgroup creation on it for now.
+        */
+       if (root == &cgrp_dfl_root)
+               return -EINVAL;
+
        /* allocate the cgroup and its ID, 0 is reserved for the root */
        cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
        if (!cgrp)
@@ -3526,7 +3755,7 @@ static long cgroup_create(struct cgroup *parent, const char *name,
 
        /* let's create and online css's */
        for_each_subsys(ss, ssid) {
-               if (root->subsys_mask & (1 << ssid)) {
+               if (root->cgrp.subsys_mask & (1 << ssid)) {
                        err = create_css(cgrp, ss);
                        if (err)
                                goto err_destroy;
@@ -3560,8 +3789,22 @@ static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
                        umode_t mode)
 {
        struct cgroup *parent = parent_kn->priv;
+       int ret;
+
+       /*
+        * cgroup_create() grabs cgroup_tree_mutex which nests outside
+        * kernfs active_ref and cgroup_create() already synchronizes
+        * properly against removal through cgroup_lock_live_group().
+        * Break it before calling cgroup_create().
+        */
+       cgroup_get(parent);
+       kernfs_break_active_protection(parent_kn);
+
+       ret = cgroup_create(parent, name, mode);
 
-       return cgroup_create(parent, name, mode);
+       kernfs_unbreak_active_protection(parent_kn);
+       cgroup_put(parent);
+       return ret;
 }
 
 /*
@@ -3614,17 +3857,10 @@ static void css_killed_ref_fn(struct percpu_ref *ref)
        queue_work(cgroup_destroy_wq, &css->destroy_work);
 }
 
-/**
- * kill_css - destroy a css
- * @css: css to destroy
- *
- * This function initiates destruction of @css by removing cgroup interface
- * files and putting its base reference.  ->css_offline() will be invoked
- * asynchronously once css_tryget() is guaranteed to fail and when the
- * reference count reaches zero, @css will be released.
- */
-static void kill_css(struct cgroup_subsys_state *css)
+static void __kill_css(struct cgroup_subsys_state *css)
 {
+       lockdep_assert_held(&cgroup_tree_mutex);
+
        /*
         * This must happen before css is disassociated with its cgroup.
         * See seq_css() for details.
@@ -3650,6 +3886,28 @@ static void kill_css(struct cgroup_subsys_state *css)
        percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
 }
 
+/**
+ * kill_css - destroy a css
+ * @css: css to destroy
+ *
+ * This function initiates destruction of @css by removing cgroup interface
+ * files and putting its base reference.  ->css_offline() will be invoked
+ * asynchronously once css_tryget() is guaranteed to fail and when the
+ * reference count reaches zero, @css will be released.
+ */
+static void kill_css(struct cgroup_subsys_state *css)
+{
+       struct cgroup *cgrp = css->cgroup;
+
+       lockdep_assert_held(&cgroup_tree_mutex);
+
+       /* if already killed, noop */
+       if (cgrp->subsys_mask & (1 << css->ss->id)) {
+               cgrp->subsys_mask &= ~(1 << css->ss->id);
+               __kill_css(css);
+       }
+}
+
 /**
  * cgroup_destroy_locked - the first stage of cgroup destruction
  * @cgrp: cgroup to be destroyed
@@ -3711,6 +3969,15 @@ static int cgroup_destroy_locked(struct cgroup *cgrp)
        if (!empty)
                return -EBUSY;
 
+       /*
+        * Mark @cgrp dead.  This prevents further task migration and child
+        * creation by disabling cgroup_lock_live_group().  Note that
+        * CGRP_DEAD assertion is depended upon by css_next_child() to
+        * resume iteration after dropping RCU read lock.  See
+        * css_next_child() for details.
+        */
+       set_bit(CGRP_DEAD, &cgrp->flags);
+
        /*
         * Initiate massacre of all css's.  cgroup_destroy_css_killed()
         * will be invoked to perform the rest of destruction once the
@@ -3722,15 +3989,6 @@ static int cgroup_destroy_locked(struct cgroup *cgrp)
                kill_css(css);
        mutex_lock(&cgroup_mutex);
 
-       /*
-        * Mark @cgrp dead.  This prevents further task migration and child
-        * creation by disabling cgroup_lock_live_group().  Note that
-        * CGRP_DEAD assertion is depended upon by css_next_child() to
-        * resume iteration after dropping RCU read lock.  See
-        * css_next_child() for details.
-        */
-       set_bit(CGRP_DEAD, &cgrp->flags);
-
        /* CGRP_DEAD is set, remove from ->release_list for the last time */
        raw_spin_lock(&release_list_lock);
        if (!list_empty(&cgrp->release_list))
@@ -3840,17 +4098,17 @@ static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
 
        INIT_LIST_HEAD(&ss->cfts);
 
-       /* Create the top cgroup state for this subsystem */
-       ss->root = &cgroup_dummy_root;
-       css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
+       /* Create the root cgroup state for this subsystem */
+       ss->root = &cgrp_dfl_root;
+       css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
        /* We don't handle early failures gracefully */
        BUG_ON(IS_ERR(css));
-       init_css(css, ss, cgroup_dummy_top);
+       init_css(css, ss, &cgrp_dfl_root.cgrp);
 
        /* Update the init_css_set to contain a subsys
         * pointer to this state - since the subsystem is
         * newly registered, all tasks and hence the
-        * init_css_set is in the subsystem's top cgroup. */
+        * init_css_set is in the subsystem's root cgroup. */
        init_css_set.subsys[ss->id] = css;
 
        need_forkexit_callback |= ss->fork || ss->exit;
@@ -3862,6 +4120,8 @@ static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
 
        BUG_ON(online_css(css));
 
+       cgrp_dfl_root.cgrp.subsys_mask |= 1 << ss->id;
+
        mutex_unlock(&cgroup_mutex);
        mutex_unlock(&cgroup_tree_mutex);
 }
@@ -3874,23 +4134,14 @@ static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
  */
 int __init cgroup_init_early(void)
 {
+       static struct cgroup_sb_opts __initdata opts =
+               { .flags = CGRP_ROOT_SANE_BEHAVIOR };
        struct cgroup_subsys *ss;
        int i;
 
-       atomic_set(&init_css_set.refcount, 1);
-       INIT_LIST_HEAD(&init_css_set.cgrp_links);
-       INIT_LIST_HEAD(&init_css_set.tasks);
-       INIT_HLIST_NODE(&init_css_set.hlist);
-       css_set_count = 1;
-       init_cgroup_root(&cgroup_dummy_root);
-       cgroup_root_count = 1;
+       init_cgroup_root(&cgrp_dfl_root, &opts);
        RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
 
-       init_cgrp_cset_link.cset = &init_css_set;
-       init_cgrp_cset_link.cgrp = cgroup_dummy_top;
-       list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
-       list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
-
        for_each_subsys(ss, i) {
                WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
                     "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
@@ -3918,11 +4169,23 @@ int __init cgroup_init(void)
 {
        struct cgroup_subsys *ss;
        unsigned long key;
-       int i, err;
+       int ssid, err;
 
        BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
 
-       for_each_subsys(ss, i) {
+       mutex_lock(&cgroup_tree_mutex);
+       mutex_lock(&cgroup_mutex);
+
+       /* Add init_css_set to the hash table */
+       key = css_set_hash(init_css_set.subsys);
+       hash_add(css_set_table, &init_css_set.hlist, key);
+
+       BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
+
+       mutex_unlock(&cgroup_mutex);
+       mutex_unlock(&cgroup_tree_mutex);
+
+       for_each_subsys(ss, ssid) {
                if (!ss->early_init)
                        cgroup_init_subsys(ss);
 
@@ -3934,21 +4197,6 @@ int __init cgroup_init(void)
                        WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes));
        }
 
-       /* allocate id for the dummy hierarchy */
-       mutex_lock(&cgroup_mutex);
-
-       /* Add init_css_set to the hash table */
-       key = css_set_hash(init_css_set.subsys);
-       hash_add(css_set_table, &init_css_set.hlist, key);
-
-       BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
-
-       err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
-                       0, 1, GFP_KERNEL);
-       BUG_ON(err < 0);
-
-       mutex_unlock(&cgroup_mutex);
-
        cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
        if (!cgroup_kobj)
                return -ENOMEM;
@@ -3968,16 +4216,12 @@ static int __init cgroup_wq_init(void)
        /*
         * There isn't much point in executing destruction path in
         * parallel.  Good chunk is serialized with cgroup_mutex anyway.
-        *
-        * XXX: Must be ordered to make sure parent is offlined after
-        * children.  The ordering requirement is for memcg where a
-        * parent's offline may wait for a child's leading to deadlock.  In
-        * the long term, this should be fixed from memcg side.
+        * Use 1 for @max_active.
         *
         * We would prefer to do this in cgroup_init() above, but that
         * is called before init_workqueues(): so leave this until after.
         */
-       cgroup_destroy_wq = alloc_ordered_workqueue("cgroup_destroy", 0);
+       cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
        BUG_ON(!cgroup_destroy_wq);
 
        /*
@@ -3996,12 +4240,6 @@ core_initcall(cgroup_wq_init);
  * proc_cgroup_show()
  *  - Print task's cgroup paths into seq_file, one line for each hierarchy
  *  - Used for /proc/<pid>/cgroup.
- *  - No need to task_lock(tsk) on this tsk->cgroup reference, as it
- *    doesn't really matter if tsk->cgroup changes after we read it,
- *    and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
- *    anyway.  No need to check that tsk->cgroup != NULL, thanks to
- *    the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
- *    cgroup to top_cgroup.
  */
 
 /* TODO: Use a proper seq_file iterator */
@@ -4011,7 +4249,7 @@ int proc_cgroup_show(struct seq_file *m, void *v)
        struct task_struct *tsk;
        char *buf, *path;
        int retval;
-       struct cgroupfs_root *root;
+       struct cgroup_root *root;
 
        retval = -ENOMEM;
        buf = kmalloc(PATH_MAX, GFP_KERNEL);
@@ -4029,14 +4267,17 @@ int proc_cgroup_show(struct seq_file *m, void *v)
        mutex_lock(&cgroup_mutex);
        down_read(&css_set_rwsem);
 
-       for_each_active_root(root) {
+       for_each_root(root) {
                struct cgroup_subsys *ss;
                struct cgroup *cgrp;
                int ssid, count = 0;
 
+               if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible)
+                       continue;
+
                seq_printf(m, "%d:", root->hierarchy_id);
                for_each_subsys(ss, ssid)
-                       if (root->subsys_mask & (1 << ssid))
+                       if (root->cgrp.subsys_mask & (1 << ssid))
                                seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
                if (strlen(root->name))
                        seq_printf(m, "%sname=%s", count ? "," : "",
@@ -4098,27 +4339,16 @@ static const struct file_operations proc_cgroupstats_operations = {
 };
 
 /**
- * cgroup_fork - attach newly forked task to its parents cgroup.
+ * cgroup_fork - initialize cgroup related fields during copy_process()
  * @child: pointer to task_struct of forking parent process.
  *
- * Description: A task inherits its parent's cgroup at fork().
- *
- * A pointer to the shared css_set was automatically copied in
- * fork.c by dup_task_struct().  However, we ignore that copy, since
- * it was not made under the protection of RCU or cgroup_mutex, so
- * might no longer be a valid cgroup pointer.  cgroup_attach_task() might
- * have already changed current->cgroups, allowing the previously
- * referenced cgroup group to be removed and freed.
- *
- * At the point that cgroup_fork() is called, 'current' is the parent
- * task, and the passed argument 'child' points to the child task.
+ * A task is associated with the init_css_set until cgroup_post_fork()
+ * attaches it to the parent's css_set.  Empty cg_list indicates that
+ * @child isn't holding reference to its css_set.
  */
 void cgroup_fork(struct task_struct *child)
 {
-       task_lock(current);
-       get_css_set(task_css_set(current));
-       child->cgroups = current->cgroups;
-       task_unlock(current);
+       RCU_INIT_POINTER(child->cgroups, &init_css_set);
        INIT_LIST_HEAD(&child->cg_list);
 }
 
@@ -4138,22 +4368,36 @@ void cgroup_post_fork(struct task_struct *child)
        int i;
 
        /*
-        * use_task_css_set_links is set to 1 before we walk the tasklist
-        * under the tasklist_lock and we read it here after we added the child
-        * to the tasklist under the tasklist_lock as well. If the child wasn't
-        * yet in the tasklist when we walked through it from
-        * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
-        * should be visible now due to the paired locking and barriers implied
-        * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
-        * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
-        * lock on fork.
+        * This may race against cgroup_enable_task_cg_links().  As that
+        * function sets use_task_css_set_links before grabbing
+        * tasklist_lock and we just went through tasklist_lock to add
+        * @child, it's guaranteed that either we see the set
+        * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
+        * @child during its iteration.
+        *
+        * If we won the race, @child is associated with %current's
+        * css_set.  Grabbing css_set_rwsem guarantees both that the
+        * association is stable, and, on completion of the parent's
+        * migration, @child is visible in the source of migration or
+        * already in the destination cgroup.  This guarantee is necessary
+        * when implementing operations which need to migrate all tasks of
+        * a cgroup to another.
+        *
+        * Note that if we lose to cgroup_enable_task_cg_links(), @child
+        * will remain in init_css_set.  This is safe because all tasks are
+        * in the init_css_set before cg_links is enabled and there's no
+        * operation which transfers all tasks out of init_css_set.
         */
        if (use_task_css_set_links) {
+               struct css_set *cset;
+
                down_write(&css_set_rwsem);
-               task_lock(child);
-               if (list_empty(&child->cg_list))
-                       list_add(&child->cg_list, &task_css_set(child)->tasks);
-               task_unlock(child);
+               cset = task_css_set(current);
+               if (list_empty(&child->cg_list)) {
+                       rcu_assign_pointer(child->cgroups, cset);
+                       list_add(&child->cg_list, &cset->tasks);
+                       get_css_set(cset);
+               }
                up_write(&css_set_rwsem);
        }
 
@@ -4182,47 +4426,32 @@ void cgroup_post_fork(struct task_struct *child)
  * use notify_on_release cgroups where very high task exit scaling
  * is required on large systems.
  *
- * the_top_cgroup_hack:
- *
- *    Set the exiting tasks cgroup to the root cgroup (top_cgroup).
- *
- *    We call cgroup_exit() while the task is still competent to
- *    handle notify_on_release(), then leave the task attached to the
- *    root cgroup in each hierarchy for the remainder of its exit.
- *
- *    To do this properly, we would increment the reference count on
- *    top_cgroup, and near the very end of the kernel/exit.c do_exit()
- *    code we would add a second cgroup function call, to drop that
- *    reference.  This would just create an unnecessary hot spot on
- *    the top_cgroup reference count, to no avail.
- *
- *    Normally, holding a reference to a cgroup without bumping its
- *    count is unsafe.   The cgroup could go away, or someone could
- *    attach us to a different cgroup, decrementing the count on
- *    the first cgroup that we never incremented.  But in this case,
- *    top_cgroup isn't going away, and either task has PF_EXITING set,
- *    which wards off any cgroup_attach_task() attempts, or task is a failed
- *    fork, never visible to cgroup_attach_task.
+ * We set the exiting tasks cgroup to the root cgroup (top_cgroup).  We
+ * call cgroup_exit() while the task is still competent to handle
+ * notify_on_release(), then leave the task attached to the root cgroup in
+ * each hierarchy for the remainder of its exit.  No need to bother with
+ * init_css_set refcnting.  init_css_set never goes away and we can't race
+ * with migration path - PF_EXITING is visible to migration path.
  */
 void cgroup_exit(struct task_struct *tsk, int run_callbacks)
 {
        struct cgroup_subsys *ss;
        struct css_set *cset;
+       bool put_cset = false;
        int i;
 
        /*
-        * Unlink from the css_set task list if necessary.  Optimistically
-        * check cg_list before taking css_set_rwsem.
+        * Unlink from @tsk from its css_set.  As migration path can't race
+        * with us, we can check cg_list without grabbing css_set_rwsem.
         */
        if (!list_empty(&tsk->cg_list)) {
                down_write(&css_set_rwsem);
-               if (!list_empty(&tsk->cg_list))
-                       list_del_init(&tsk->cg_list);
+               list_del_init(&tsk->cg_list);
                up_write(&css_set_rwsem);
+               put_cset = true;
        }
 
        /* Reassign the task to the init_css_set. */
-       task_lock(tsk);
        cset = task_css_set(tsk);
        RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
 
@@ -4237,9 +4466,9 @@ void cgroup_exit(struct task_struct *tsk, int run_callbacks)
                        }
                }
        }
-       task_unlock(tsk);
 
-       put_css_set(cset, true);
+       if (put_cset)
+               put_css_set(cset, true);
 }
 
 static void check_for_release(struct cgroup *cgrp)
@@ -4475,15 +4704,10 @@ static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
        cset = rcu_dereference(current->cgroups);
        list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
                struct cgroup *c = link->cgrp;
-               const char *name = "?";
-
-               if (c != cgroup_dummy_top) {
-                       cgroup_name(c, name_buf, NAME_MAX + 1);
-                       name = name_buf;
-               }
 
+               cgroup_name(c, name_buf, NAME_MAX + 1);
                seq_printf(seq, "Root %d group %s\n",
-                          c->root->hierarchy_id, name);
+                          c->root->hierarchy_id, name_buf);
        }
        rcu_read_unlock();
        up_read(&css_set_rwsem);
@@ -4502,16 +4726,23 @@ static int cgroup_css_links_read(struct seq_file *seq, void *v)
                struct css_set *cset = link->cset;
                struct task_struct *task;
                int count = 0;
+
                seq_printf(seq, "css_set %p\n", cset);
+
                list_for_each_entry(task, &cset->tasks, cg_list) {
-                       if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
-                               seq_puts(seq, "  ...\n");
-                               break;
-                       } else {
-                               seq_printf(seq, "  task %d\n",
-                                          task_pid_vnr(task));
-                       }
+                       if (count++ > MAX_TASKS_SHOWN_PER_CSS)
+                               goto overflow;
+                       seq_printf(seq, "  task %d\n", task_pid_vnr(task));
+               }
+
+               list_for_each_entry(task, &cset->mg_tasks, cg_list) {
+                       if (count++ > MAX_TASKS_SHOWN_PER_CSS)
+                               goto overflow;
+                       seq_printf(seq, "  task %d\n", task_pid_vnr(task));
                }
+               continue;
+       overflow:
+               seq_puts(seq, "  ...\n");
        }
        up_read(&css_set_rwsem);
        return 0;