]> git.karo-electronics.de Git - linux-beck.git/blobdiff - kernel/cgroup.c
cgroup: link all cgroup_subsys_states in their sibling lists
[linux-beck.git] / kernel / cgroup.c
index cb5864e36f990a7f9a62eb48225e226e8c67fbab..dcb06e181ce4f6178a02bd82e3bc6d898faf533b 100644 (file)
@@ -176,10 +176,12 @@ static int need_forkexit_callback __read_mostly;
 static struct cftype cgroup_base_files[];
 
 static void cgroup_put(struct cgroup *cgrp);
+static bool cgroup_has_live_children(struct cgroup *cgrp);
 static int rebind_subsystems(struct cgroup_root *dst_root,
                             unsigned int ss_mask);
 static int cgroup_destroy_locked(struct cgroup *cgrp);
 static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss);
+static void css_release(struct percpu_ref *ref);
 static void kill_css(struct cgroup_subsys_state *css);
 static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
                              bool is_add);
@@ -216,6 +218,15 @@ static void cgroup_idr_remove(struct idr *idr, int id)
        spin_unlock_bh(&cgroup_idr_lock);
 }
 
+static struct cgroup *cgroup_parent(struct cgroup *cgrp)
+{
+       struct cgroup_subsys_state *parent_css = cgrp->self.parent;
+
+       if (parent_css)
+               return container_of(parent_css, struct cgroup, self);
+       return NULL;
+}
+
 /**
  * cgroup_css - obtain a cgroup's css for the specified subsystem
  * @cgrp: the cgroup of interest
@@ -258,9 +269,9 @@ static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
        if (!(cgrp->root->subsys_mask & (1 << ss->id)))
                return NULL;
 
-       while (cgrp->parent &&
-              !(cgrp->parent->child_subsys_mask & (1 << ss->id)))
-               cgrp = cgrp->parent;
+       while (cgroup_parent(cgrp) &&
+              !(cgroup_parent(cgrp)->child_subsys_mask & (1 << ss->id)))
+               cgrp = cgroup_parent(cgrp);
 
        return cgroup_css(cgrp, ss);
 }
@@ -305,7 +316,7 @@ bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
        while (cgrp) {
                if (cgrp == ancestor)
                        return true;
-               cgrp = cgrp->parent;
+               cgrp = cgroup_parent(cgrp);
        }
        return false;
 }
@@ -367,7 +378,7 @@ static int notify_on_release(const struct cgroup *cgrp)
 
 /* iterate over child cgrps, lock should be held throughout iteration */
 #define cgroup_for_each_live_child(child, cgrp)                                \
-       list_for_each_entry((child), &(cgrp)->children, sibling)        \
+       list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
                if (({ lockdep_assert_held(&cgroup_mutex);              \
                       cgroup_is_dead(child); }))                       \
                        ;                                               \
@@ -452,7 +463,7 @@ static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
 
                if (cgrp->populated_kn)
                        kernfs_notify(cgrp->populated_kn);
-               cgrp = cgrp->parent;
+               cgrp = cgroup_parent(cgrp);
        } while (cgrp);
 }
 
@@ -859,7 +870,7 @@ static void cgroup_destroy_root(struct cgroup_root *root)
        mutex_lock(&cgroup_mutex);
 
        BUG_ON(atomic_read(&root->nr_cgrps));
-       BUG_ON(!list_empty(&cgrp->children));
+       BUG_ON(!list_empty(&cgrp->self.children));
 
        /* Rebind all subsystems back to the default hierarchy */
        rebind_subsystems(&cgrp_dfl_root, root->subsys_mask);
@@ -1008,62 +1019,15 @@ static umode_t cgroup_file_mode(const struct cftype *cft)
        return mode;
 }
 
-static void cgroup_free_fn(struct work_struct *work)
-{
-       struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
-
-       atomic_dec(&cgrp->root->nr_cgrps);
-       cgroup_pidlist_destroy_all(cgrp);
-
-       if (cgrp->parent) {
-               /*
-                * We get a ref to the parent, and put the ref when this
-                * cgroup is being freed, so it's guaranteed that the
-                * parent won't be destroyed before its children.
-                */
-               cgroup_put(cgrp->parent);
-               kernfs_put(cgrp->kn);
-               kfree(cgrp);
-       } else {
-               /*
-                * This is root cgroup's refcnt reaching zero, which
-                * indicates that the root should be released.
-                */
-               cgroup_destroy_root(cgrp->root);
-       }
-}
-
-static void cgroup_free_rcu(struct rcu_head *head)
-{
-       struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
-
-       INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
-       queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
-}
-
 static void cgroup_get(struct cgroup *cgrp)
 {
        WARN_ON_ONCE(cgroup_is_dead(cgrp));
-       WARN_ON_ONCE(atomic_read(&cgrp->refcnt) <= 0);
-       atomic_inc(&cgrp->refcnt);
+       css_get(&cgrp->self);
 }
 
 static void cgroup_put(struct cgroup *cgrp)
 {
-       if (!atomic_dec_and_test(&cgrp->refcnt))
-               return;
-       if (WARN_ON_ONCE(cgrp->parent && !cgroup_is_dead(cgrp)))
-               return;
-
-       /* delete this cgroup from parent->children */
-       mutex_lock(&cgroup_mutex);
-       list_del_rcu(&cgrp->sibling);
-       mutex_unlock(&cgroup_mutex);
-
-       cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
-       cgrp->id = -1;
-
-       call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
+       css_put(&cgrp->self);
 }
 
 /**
@@ -1468,7 +1432,7 @@ static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
        }
 
        /* remounting is not allowed for populated hierarchies */
-       if (!list_empty(&root->cgrp.children)) {
+       if (!list_empty(&root->cgrp.self.children)) {
                ret = -EBUSY;
                goto out_unlock;
        }
@@ -1548,9 +1512,8 @@ static void init_cgroup_housekeeping(struct cgroup *cgrp)
        struct cgroup_subsys *ss;
        int ssid;
 
-       atomic_set(&cgrp->refcnt, 1);
-       INIT_LIST_HEAD(&cgrp->sibling);
-       INIT_LIST_HEAD(&cgrp->children);
+       INIT_LIST_HEAD(&cgrp->self.sibling);
+       INIT_LIST_HEAD(&cgrp->self.children);
        INIT_LIST_HEAD(&cgrp->cset_links);
        INIT_LIST_HEAD(&cgrp->release_list);
        INIT_LIST_HEAD(&cgrp->pidlists);
@@ -1597,6 +1560,10 @@ static int cgroup_setup_root(struct cgroup_root *root, unsigned int ss_mask)
                goto out;
        root_cgrp->id = ret;
 
+       ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release);
+       if (ret)
+               goto out;
+
        /*
         * We're accessing css_set_count without locking css_set_rwsem here,
         * but that's OK - it can only be increased by someone holding
@@ -1605,11 +1572,11 @@ static int cgroup_setup_root(struct cgroup_root *root, unsigned int ss_mask)
         */
        ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
        if (ret)
-               goto out;
+               goto cancel_ref;
 
        ret = cgroup_init_root_id(root);
        if (ret)
-               goto out;
+               goto cancel_ref;
 
        root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops,
                                           KERNFS_ROOT_CREATE_DEACTIVATED,
@@ -1645,7 +1612,7 @@ static int cgroup_setup_root(struct cgroup_root *root, unsigned int ss_mask)
                link_css_set(&tmp_links, cset, root_cgrp);
        up_write(&css_set_rwsem);
 
-       BUG_ON(!list_empty(&root_cgrp->children));
+       BUG_ON(!list_empty(&root_cgrp->self.children));
        BUG_ON(atomic_read(&root->nr_cgrps) != 1);
 
        kernfs_activate(root_cgrp->kn);
@@ -1657,6 +1624,8 @@ destroy_root:
        root->kf_root = NULL;
 exit_root_id:
        cgroup_exit_root_id(root);
+cancel_ref:
+       percpu_ref_cancel_init(&root_cgrp->self.refcnt);
 out:
        free_cgrp_cset_links(&tmp_links);
        return ret;
@@ -1735,13 +1704,14 @@ static struct dentry *cgroup_mount(struct file_system_type *fs_type,
                }
 
                /*
-                * 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.
+                * A root's lifetime is governed by its root cgroup.
+                * tryget_live failure 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->cgrp.refcnt)) {
+               if (!percpu_ref_tryget_live(&root->cgrp.self.refcnt)) {
                        mutex_unlock(&cgroup_mutex);
                        msleep(10);
                        ret = restart_syscall();
@@ -1794,7 +1764,16 @@ static void cgroup_kill_sb(struct super_block *sb)
        struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
        struct cgroup_root *root = cgroup_root_from_kf(kf_root);
 
-       cgroup_put(&root->cgrp);
+       /*
+        * If @root doesn't have any mounts or children, start killing it.
+        * This prevents new mounts by disabling percpu_ref_tryget_live().
+        * cgroup_mount() may wait for @root's release.
+        */
+       if (cgroup_has_live_children(&root->cgrp))
+               cgroup_put(&root->cgrp);
+       else
+               percpu_ref_kill(&root->cgrp.self.refcnt);
+
        kernfs_kill_sb(sb);
 }
 
@@ -2048,7 +2027,7 @@ static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp,
         * Except for the root, child_subsys_mask must be zero for a cgroup
         * with tasks so that child cgroups don't compete against tasks.
         */
-       if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && dst_cgrp->parent &&
+       if (dst_cgrp && cgroup_on_dfl(dst_cgrp) && cgroup_parent(dst_cgrp) &&
            dst_cgrp->child_subsys_mask)
                return -EBUSY;
 
@@ -2457,7 +2436,7 @@ static int cgroup_controllers_show(struct seq_file *seq, void *v)
 {
        struct cgroup *cgrp = seq_css(seq)->cgroup;
 
-       cgroup_print_ss_mask(seq, cgrp->parent->child_subsys_mask);
+       cgroup_print_ss_mask(seq, cgroup_parent(cgrp)->child_subsys_mask);
        return 0;
 }
 
@@ -2640,8 +2619,8 @@ static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
 
                        /* unavailable or not enabled on the parent? */
                        if (!(cgrp_dfl_root.subsys_mask & (1 << ssid)) ||
-                           (cgrp->parent &&
-                            !(cgrp->parent->child_subsys_mask & (1 << ssid)))) {
+                           (cgroup_parent(cgrp) &&
+                            !(cgroup_parent(cgrp)->child_subsys_mask & (1 << ssid)))) {
                                ret = -ENOENT;
                                goto out_unlock;
                        }
@@ -2670,7 +2649,7 @@ static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
         * Except for the root, child_subsys_mask must be zero for a cgroup
         * with tasks so that child cgroups don't compete against tasks.
         */
-       if (enable && cgrp->parent && !list_empty(&cgrp->cset_links)) {
+       if (enable && cgroup_parent(cgrp) && !list_empty(&cgrp->cset_links)) {
                ret = -EBUSY;
                goto out_unlock;
        }
@@ -2928,9 +2907,9 @@ static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
                        continue;
                if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
                        continue;
-               if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
+               if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp))
                        continue;
-               if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
+               if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp))
                        continue;
 
                if (is_add) {
@@ -3149,11 +3128,11 @@ css_next_child(struct cgroup_subsys_state *pos_css,
         * cgroup is removed or iteration and removal race.
         */
        if (!pos) {
-               next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
+               next = list_entry_rcu(cgrp->self.children.next, struct cgroup, self.sibling);
        } else if (likely(!cgroup_is_dead(pos))) {
-               next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
+               next = list_entry_rcu(pos->self.sibling.next, struct cgroup, self.sibling);
        } else {
-               list_for_each_entry_rcu(next, &cgrp->children, sibling)
+               list_for_each_entry_rcu(next, &cgrp->self.children, self.sibling)
                        if (next->serial_nr > pos->serial_nr)
                                break;
        }
@@ -3163,12 +3142,12 @@ css_next_child(struct cgroup_subsys_state *pos_css,
         * the next sibling; however, it might have @ss disabled.  If so,
         * fast-forward to the next enabled one.
         */
-       while (&next->sibling != &cgrp->children) {
+       while (&next->self.sibling != &cgrp->self.children) {
                struct cgroup_subsys_state *next_css = cgroup_css(next, parent_css->ss);
 
                if (next_css)
                        return next_css;
-               next = list_entry_rcu(next->sibling.next, struct cgroup, sibling);
+               next = list_entry_rcu(next->self.sibling.next, struct cgroup, self.sibling);
        }
        return NULL;
 }
@@ -3206,10 +3185,10 @@ css_next_descendant_pre(struct cgroup_subsys_state *pos,
 
        /* no child, visit my or the closest ancestor's next sibling */
        while (pos != root) {
-               next = css_next_child(pos, css_parent(pos));
+               next = css_next_child(pos, pos->parent);
                if (next)
                        return next;
-               pos = css_parent(pos);
+               pos = pos->parent;
        }
 
        return NULL;
@@ -3291,12 +3270,12 @@ css_next_descendant_post(struct cgroup_subsys_state *pos,
                return NULL;
 
        /* if there's an unvisited sibling, visit its leftmost descendant */
-       next = css_next_child(pos, css_parent(pos));
+       next = css_next_child(pos, pos->parent);
        if (next)
                return css_leftmost_descendant(next);
 
        /* no sibling left, visit parent */
-       return css_parent(pos);
+       return pos->parent;
 }
 
 static bool cgroup_has_live_children(struct cgroup *cgrp)
@@ -3304,7 +3283,7 @@ static bool cgroup_has_live_children(struct cgroup *cgrp)
        struct cgroup *child;
 
        rcu_read_lock();
-       list_for_each_entry_rcu(child, &cgrp->children, sibling) {
+       list_for_each_entry_rcu(child, &cgrp->self.children, self.sibling) {
                if (!cgroup_is_dead(child)) {
                        rcu_read_unlock();
                        return true;
@@ -4110,11 +4089,37 @@ static void css_free_work_fn(struct work_struct *work)
                container_of(work, struct cgroup_subsys_state, destroy_work);
        struct cgroup *cgrp = css->cgroup;
 
-       if (css->parent)
-               css_put(css->parent);
+       if (css->ss) {
+               /* css free path */
+               if (css->parent)
+                       css_put(css->parent);
 
-       css->ss->css_free(css);
-       cgroup_put(cgrp);
+               css->ss->css_free(css);
+               cgroup_put(cgrp);
+       } else {
+               /* cgroup free path */
+               atomic_dec(&cgrp->root->nr_cgrps);
+               cgroup_pidlist_destroy_all(cgrp);
+
+               if (cgroup_parent(cgrp)) {
+                       /*
+                        * We get a ref to the parent, and put the ref when
+                        * this cgroup is being freed, so it's guaranteed
+                        * that the parent won't be destroyed before its
+                        * children.
+                        */
+                       cgroup_put(cgroup_parent(cgrp));
+                       kernfs_put(cgrp->kn);
+                       kfree(cgrp);
+               } else {
+                       /*
+                        * This is root cgroup's refcnt reaching zero,
+                        * which indicates that the root should be
+                        * released.
+                        */
+                       cgroup_destroy_root(cgrp->root);
+               }
+       }
 }
 
 static void css_free_rcu_fn(struct rcu_head *rcu_head)
@@ -4131,8 +4136,22 @@ static void css_release_work_fn(struct work_struct *work)
        struct cgroup_subsys_state *css =
                container_of(work, struct cgroup_subsys_state, destroy_work);
        struct cgroup_subsys *ss = css->ss;
+       struct cgroup *cgrp = css->cgroup;
 
-       cgroup_idr_remove(&ss->css_idr, css->id);
+       mutex_lock(&cgroup_mutex);
+
+       list_del_rcu(&css->sibling);
+
+       if (ss) {
+               /* css release path */
+               cgroup_idr_remove(&ss->css_idr, css->id);
+       } else {
+               /* cgroup release path */
+               cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
+               cgrp->id = -1;
+       }
+
+       mutex_unlock(&cgroup_mutex);
 
        call_rcu(&css->rcu_head, css_free_rcu_fn);
 }
@@ -4151,12 +4170,14 @@ static void init_and_link_css(struct cgroup_subsys_state *css,
 {
        cgroup_get(cgrp);
 
+       memset(css, 0, sizeof(*css));
        css->cgroup = cgrp;
        css->ss = ss;
-       css->flags = 0;
+       INIT_LIST_HEAD(&css->sibling);
+       INIT_LIST_HEAD(&css->children);
 
-       if (cgrp->parent) {
-               css->parent = cgroup_css(cgrp->parent, ss);
+       if (cgroup_parent(cgrp)) {
+               css->parent = cgroup_css(cgroup_parent(cgrp), ss);
                css_get(css->parent);
        }
 
@@ -4210,13 +4231,14 @@ static void offline_css(struct cgroup_subsys_state *css)
  */
 static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
 {
-       struct cgroup *parent = cgrp->parent;
+       struct cgroup *parent = cgroup_parent(cgrp);
+       struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss);
        struct cgroup_subsys_state *css;
        int err;
 
        lockdep_assert_held(&cgroup_mutex);
 
-       css = ss->css_alloc(cgroup_css(parent, ss));
+       css = ss->css_alloc(parent_css);
        if (IS_ERR(css))
                return PTR_ERR(css);
 
@@ -4236,14 +4258,15 @@ static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
                goto err_free_id;
 
        /* @css is ready to be brought online now, make it visible */
+       list_add_tail_rcu(&css->sibling, &parent_css->children);
        cgroup_idr_replace(&ss->css_idr, css, css->id);
 
        err = online_css(css);
        if (err)
-               goto err_clear_dir;
+               goto err_list_del;
 
        if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
-           parent->parent) {
+           cgroup_parent(parent)) {
                pr_warn("%s (%d) created nested cgroup for controller \"%s\" which has incomplete hierarchy support. Nested cgroups may change behavior in the future.\n",
                        current->comm, current->pid, ss->name);
                if (!strcmp(ss->name, "memory"))
@@ -4253,7 +4276,8 @@ static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
 
        return 0;
 
-err_clear_dir:
+err_list_del:
+       list_del_rcu(&css->sibling);
        cgroup_clear_dir(css->cgroup, 1 << css->ss->id);
 err_free_id:
        cgroup_idr_remove(&ss->css_idr, css->id);
@@ -4285,6 +4309,10 @@ static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
                goto out_unlock;
        }
 
+       ret = percpu_ref_init(&cgrp->self.refcnt, css_release);
+       if (ret)
+               goto out_free_cgrp;
+
        /*
         * Temporarily set the pointer to NULL, so idr_find() won't return
         * a half-baked cgroup.
@@ -4292,12 +4320,11 @@ static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
        cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_NOWAIT);
        if (cgrp->id < 0) {
                ret = -ENOMEM;
-               goto out_free_cgrp;
+               goto out_cancel_ref;
        }
 
        init_cgroup_housekeeping(cgrp);
 
-       cgrp->parent = parent;
        cgrp->self.parent = &parent->self;
        cgrp->root = root;
 
@@ -4324,7 +4351,7 @@ static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
        cgrp->serial_nr = cgroup_serial_nr_next++;
 
        /* allocation complete, commit to creation */
-       list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
+       list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
        atomic_inc(&root->nr_cgrps);
        cgroup_get(parent);
 
@@ -4365,6 +4392,8 @@ static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
 
 out_free_id:
        cgroup_idr_remove(&root->cgroup_idr, cgrp->id);
+out_cancel_ref:
+       percpu_ref_cancel_init(&cgrp->self.refcnt);
 out_free_cgrp:
        kfree(cgrp);
 out_unlock:
@@ -4485,9 +4514,9 @@ static int cgroup_destroy_locked(struct cgroup *cgrp)
                return -EBUSY;
 
        /*
-        * Make sure there's no live children.  We can't test ->children
-        * emptiness as dead children linger on it while being destroyed;
-        * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
+        * Make sure there's no live children.  We can't test emptiness of
+        * ->self.children as dead children linger on it while being
+        * drained; otherwise, "rmdir parent/child parent" may fail.
         */
        if (cgroup_has_live_children(cgrp))
                return -EBUSY;
@@ -4517,11 +4546,11 @@ static int cgroup_destroy_locked(struct cgroup *cgrp)
         */
        kernfs_remove(cgrp->kn);
 
-       set_bit(CGRP_RELEASABLE, &cgrp->parent->flags);
-       check_for_release(cgrp->parent);
+       set_bit(CGRP_RELEASABLE, &cgroup_parent(cgrp)->flags);
+       check_for_release(cgroup_parent(cgrp));
 
        /* put the base reference */
-       cgroup_put(cgrp);
+       percpu_ref_kill(&cgrp->self.refcnt);
 
        return 0;
 };
@@ -4579,11 +4608,17 @@ static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
        /* We don't handle early failures gracefully */
        BUG_ON(IS_ERR(css));
        init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
+
+       /*
+        * Root csses are never destroyed and we can't initialize
+        * percpu_ref during early init.  Disable refcnting.
+        */
+       css->flags |= CSS_NO_REF;
+
        if (early) {
                /* allocation can't be done safely during early init */
                css->id = 1;
        } else {
-               BUG_ON(percpu_ref_init(&css->refcnt, css_release));
                css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
                BUG_ON(css->id < 0);
        }
@@ -4622,6 +4657,8 @@ int __init cgroup_init_early(void)
        int i;
 
        init_cgroup_root(&cgrp_dfl_root, &opts);
+       cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF;
+
        RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
 
        for_each_subsys(ss, i) {
@@ -4670,7 +4707,6 @@ int __init cgroup_init(void)
                        struct cgroup_subsys_state *css =
                                init_css_set.subsys[ss->id];
 
-                       BUG_ON(percpu_ref_init(&css->refcnt, css_release));
                        css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
                                                   GFP_KERNEL);
                        BUG_ON(css->id < 0);