if (!buf)
                goto out;
 
-       cgroup_lock();
+       rcu_read_lock();
        spin_lock_irq(&css_set_lock);
 
        for_each_root(root) {
                if (root == &cgrp_dfl_root && !READ_ONCE(cgrp_dfl_visible))
                        continue;
 
+               cgrp = task_cgroup_from_root(tsk, root);
+               /* The root has already been unmounted. */
+               if (!cgrp)
+                       continue;
+
                seq_printf(m, "%d:", root->hierarchy_id);
                if (root != &cgrp_dfl_root)
                        for_each_subsys(ss, ssid)
                        seq_printf(m, "%sname=%s", count ? "," : "",
                                   root->name);
                seq_putc(m, ':');
-
-               cgrp = task_cgroup_from_root(tsk, root);
-
                /*
                 * On traditional hierarchies, all zombie tasks show up as
                 * belonging to the root cgroup.  On the default hierarchy,
        retval = 0;
 out_unlock:
        spin_unlock_irq(&css_set_lock);
-       cgroup_unlock();
+       rcu_read_unlock();
        kfree(buf);
 out:
        return retval;