Since commit 93618edf7538 ("cgroup: Defer css percpu_ref kill on rmdir until cgroup is depopulated"), rmdir can remove a cgroup while an exiting task is still linked to it. The task is unlinked later, in cgroup_task_dead(). This breaks the freezer counts: - rmdir takes the cgroup out of the ancestors' nr_frozen_descendants only if it is frozen, and a cgroup with an exiting task is not. When the task is unlinked, cgroup_update_frozen() marks the dead cgroup frozen and counts it again in every ancestor. The count stays one too high, so a freezing ancestor reports frozen=1 too early or never. - If the removed cgroup was the parent's last unfrozen descendant, the parent is not checked again and stays at frozen=0. Clear CSS_ONLINE, fix the ancestors' counts and check the parent again in one css_set_lock section, and make cgroup_update_frozen() skip dead cgroups. cgroup_is_dead() can now run with only css_set_lock held, while other bits of the same flags word change under cgroup_mutex, so use READ_ONCE() and WRITE_ONCE() for CSS_ONLINE. The parent's check needs the frozen state to cover descendants, so a backport needs "cgroup/freezer: check own tasks and descendants for the frozen state" first. Reproduced in a VM: freeze G/P, kill the only task in G/P/C while its exit is held on an fsfreeze'd filesystem, remove G/P/C, and let the task finish. G/P, empty and freezing, reports frozen=0, and a later freeze of G never completes. This patch fixes both. Fixes: 93618edf7538 ("cgroup: Defer css percpu_ref kill on rmdir until cgroup is depopulated") Cc: stable@vger.kernel.org # v7.0+, needs "cgroup/freezer: check own tasks and descendants for the frozen state" Signed-off-by: Shakeel Butt --- include/linux/cgroup.h | 2 +- kernel/cgroup/cgroup.c | 33 +++++++++++++++++++-------------- kernel/cgroup/freezer.c | 8 ++++++++ 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h index f3c65991acac..7279613d6804 100644 --- a/include/linux/cgroup.h +++ b/include/linux/cgroup.h @@ -462,7 +462,7 @@ static inline bool css_is_self(struct cgroup_subsys_state *css) static inline bool cgroup_is_dead(const struct cgroup *cgrp) { - return !(cgrp->self.flags & CSS_ONLINE); + return !(READ_ONCE(cgrp->self.flags) & CSS_ONLINE); } static inline void cgroup_get(struct cgroup *cgrp) diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index 0fcd2c1b0da9..3972cf413797 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -6426,25 +6426,17 @@ static int cgroup_destroy_locked(struct cgroup *cgrp) * further task migration and child creation by disabling * cgroup_kn_lock_live(). The latter makes the csets ignored by * the migration path. + * + * Exiting tasks can stay linked to @cgrp until cgroup_task_dead(). + * Take @cgrp out of its ancestors' descendant counts in the same + * css_set_lock section, so that cgroup_update_frozen() for those + * tasks sees @cgrp dead and leaves the counts alone. */ - cgrp->self.flags &= ~CSS_ONLINE; - spin_lock_irq(&css_set_lock); + WRITE_ONCE(cgrp->self.flags, cgrp->self.flags & ~CSS_ONLINE); list_for_each_entry(link, &cgrp->cset_links, cset_link) link->cset->dead = true; - spin_unlock_irq(&css_set_lock); - - for_each_css(css, ssid, cgrp) - kill_css_sync(css); - - /* clear and remove @cgrp dir, @cgrp has an extra ref on its kn */ - css_clear_dir(&cgrp->self); - kernfs_remove(cgrp->kn); - - if (cgroup_is_threaded(cgrp)) - parent->nr_threaded_children--; - spin_lock_irq(&css_set_lock); for (tcgrp = parent; tcgrp; tcgrp = cgroup_parent(tcgrp)) { tcgrp->nr_descendants--; tcgrp->nr_dying_descendants++; @@ -6455,8 +6447,21 @@ static int cgroup_destroy_locked(struct cgroup *cgrp) if (test_bit(CGRP_FROZEN, &cgrp->flags)) tcgrp->freezer.nr_frozen_descendants--; } + + /* @cgrp may have been the last unfrozen descendant of a freezing parent */ + cgroup_update_frozen(parent); spin_unlock_irq(&css_set_lock); + for_each_css(css, ssid, cgrp) + kill_css_sync(css); + + /* clear and remove @cgrp dir, @cgrp has an extra ref on its kn */ + css_clear_dir(&cgrp->self); + kernfs_remove(cgrp->kn); + + if (cgroup_is_threaded(cgrp)) + parent->nr_threaded_children--; + cgroup1_check_for_release(parent); ret = blocking_notifier_call_chain(&cgroup_lifetime_notifier, diff --git a/kernel/cgroup/freezer.c b/kernel/cgroup/freezer.c index 6ecb42084ecc..a85f8897ffa2 100644 --- a/kernel/cgroup/freezer.c +++ b/kernel/cgroup/freezer.c @@ -53,6 +53,14 @@ void cgroup_update_frozen(struct cgroup *cgrp) lockdep_assert_held(&css_set_lock); + /* + * An exiting task can stay linked to a cgroup after rmdir. + * cgroup_destroy_locked() took the cgroup out of its ancestors' counts + * when it cleared CSS_ONLINE. Leave its state alone from then on. + */ + if (cgroup_is_dead(cgrp)) + return; + do { bool frozen; -- 2.53.1-Meta