static struct cftype cgroup_legacy_base_files[];
 
 static int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask);
+static void cgroup_lock_and_drain_offline(struct cgroup *cgrp);
 static void css_task_iter_advance(struct css_task_iter *it);
 static int cgroup_destroy_locked(struct cgroup *cgrp);
 static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
 /**
  * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
  * @kn: the kernfs_node being serviced
+ * @drain_offline: perform offline draining on the cgroup
  *
  * This helper is to be used by a cgroup kernfs method currently servicing
  * @kn.  It breaks the active protection, performs cgroup locking and
  * verifies that the associated cgroup is alive.  Returns the cgroup if
  * alive; otherwise, %NULL.  A successful return should be undone by a
- * matching cgroup_kn_unlock() invocation.
+ * matching cgroup_kn_unlock() invocation.  If @drain_offline is %true, the
+ * cgroup is drained of offlining csses before return.
  *
  * Any cgroup kernfs method implementation which requires locking the
  * associated cgroup should use this helper.  It avoids nesting cgroup
  * locking under kernfs active protection and allows all kernfs operations
  * including self-removal.
  */
-static struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn)
+static struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn,
+                                         bool drain_offline)
 {
        struct cgroup *cgrp;
 
                return NULL;
        kernfs_break_active_protection(kn);
 
-       mutex_lock(&cgroup_mutex);
+       if (drain_offline)
+               cgroup_lock_and_drain_offline(cgrp);
+       else
+               mutex_lock(&cgroup_mutex);
 
        if (!cgroup_is_dead(cgrp))
                return cgrp;
        if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
                return -EINVAL;
 
-       cgrp = cgroup_kn_lock_live(of->kn);
+       cgrp = cgroup_kn_lock_live(of->kn, false);
        if (!cgrp)
                return -ENODEV;
 
 
        BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
 
-       cgrp = cgroup_kn_lock_live(of->kn);
+       cgrp = cgroup_kn_lock_live(of->kn, false);
        if (!cgrp)
                return -ENODEV;
        spin_lock(&release_agent_path_lock);
 }
 
 /**
- * cgroup_drain_offline - wait for previously offlined csses to go away
+ * cgroup_lock_and_drain_offline - lock cgroup_mutex and drain offlined csses
  * @cgrp: root of the target subtree
  *
  * Because css offlining is asynchronous, userland may try to re-enable a
- * controller while the previous css is still around.  This function drains
- * the previous css instances of @cgrp's subtree.
- *
- * Must be called with cgroup_mutex held.  Returns %false if there were no
- * dying css instances.  Returns %true if there were one or more and this
- * function waited.  On %true return, cgroup_mutex has been dropped and
- * re-acquired inbetween which anything could have happened.  The caller
- * typically would have to start over.
+ * controller while the previous css is still around.  This function grabs
+ * cgroup_mutex and drains the previous css instances of @cgrp's subtree.
  */
-static bool cgroup_drain_offline(struct cgroup *cgrp)
+static void cgroup_lock_and_drain_offline(struct cgroup *cgrp)
+       __acquires(&cgroup_mutex)
 {
        struct cgroup *dsct;
        struct cgroup_subsys_state *d_css;
        struct cgroup_subsys *ss;
        int ssid;
 
-       lockdep_assert_held(&cgroup_mutex);
+restart:
+       mutex_lock(&cgroup_mutex);
 
        cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
                for_each_subsys(ss, ssid) {
                        mutex_unlock(&cgroup_mutex);
                        schedule();
                        finish_wait(&dsct->offline_waitq, &wait);
-                       mutex_lock(&cgroup_mutex);
 
                        cgroup_put(dsct);
-                       return true;
+                       goto restart;
                }
        }
-
-       return false;
 }
 
 /**
                for_each_subsys(ss, ssid) {
                        struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
 
+                       WARN_ON_ONCE(css && percpu_ref_is_dying(&css->refcnt));
+
                        if (!(cgroup_ss_mask(dsct) & (1 << ss->id)))
                                continue;
 
                for_each_subsys(ss, ssid) {
                        struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
 
+                       WARN_ON_ONCE(css && percpu_ref_is_dying(&css->refcnt));
+
                        if (!css)
                                continue;
 
                        return -EINVAL;
        }
 
-       cgrp = cgroup_kn_lock_live(of->kn);
+       cgrp = cgroup_kn_lock_live(of->kn, true);
        if (!cgrp)
                return -ENODEV;
 
                goto out_unlock;
        }
 
-       if (cgroup_drain_offline(cgrp)) {
-               cgroup_kn_unlock(of->kn);
-               return restart_syscall();
-       }
-
        /* save and update control masks and prepare csses */
        cgroup_save_control(cgrp);
 
        if (strchr(name, '\n'))
                return -EINVAL;
 
-       parent = cgroup_kn_lock_live(parent_kn);
+       parent = cgroup_kn_lock_live(parent_kn, false);
        if (!parent)
                return -ENODEV;
 
        struct cgroup *cgrp;
        int ret = 0;
 
-       cgrp = cgroup_kn_lock_live(kn);
+       cgrp = cgroup_kn_lock_live(kn, false);
        if (!cgrp)
                return 0;