The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH v2] cgroup: Honor css refcnt on online_css() failure in css_create()
@ 2026-12-02  3:30 Hu Xinyao
  2026-12-03 23:05 ` Tejun Heo
  0 siblings, 1 reply; 2+ messages in thread
From: Hu Xinyao @ 2026-12-02  3:30 UTC (permalink / raw)
  To: Tejun Heo, Johannes Weiner, Michal Koutny
  Cc: cgroups, linux-kernel, Hu Xinyao

css_create() intentionally links css into parent_css->children and
publishes it in ss->css_idr before invoking online_css(css), so a css
is visible to iteration and ID lookups before and while coming online.

Commit b00c52dae6d9 ("cgroup: remove redundant cleanup in css_create")
removed percpu_ref_exit() and cgroup_idr_remove() from err_list_del,
leaving online_css() failure to unlink css->sibling and schedule
css_free_rwork_fn() directly without killing css->refcnt or clearing
ss->css_idr. Concurrent RCU readers using css_from_id() or child
iteration followed by css_tryget() can acquire a reference on the
failed css while css_free_rwork_fn() calls percpu_ref_exit() and frees
css via ss->css_free().

Kill css->refcnt with percpu_ref_kill() when online_css() fails so
teardown proceeds through css_release_work_fn() and css_free_rwork_fn()
only after all active references drop, clearing ss->css_idr and
unlinking css->sibling with CSS_RELEASED set. Because a failed css
from css_create() was never online and did not pass through kill_css(),
guard css_rstat_flush(), ss->css_released(), and nr_dying_subsys
decrements in css_release_work_fn() with (css->flags & CSS_DYING).

Tested in QEMU with CONFIG_KASAN=y, CONFIG_PROVE_LOCKING=y,
CONFIG_DEBUG_LIST=y, and CONFIG_FAILSLAB=y injecting -ENOMEM into
mem_cgroup_css_online() during cgroup mkdir and subtree_control updates
under concurrent cgroup.stat reads, with nr_dying_subsys counters
remaining at 0 and no kernel warnings.

Fixes: b00c52dae6d9 ("cgroup: remove redundant cleanup in css_create")
Suggested-by: Tejun Heo <tj@kernel.org>
Cc: stable@vger.kernel.org
Signed-off-by: Hu Xinyao <huxinyao@kylinsec.com.cn>
---
v2:
  - Keep cgroup_idr_replace() before online_css() and instead kill
    css->refcnt on online_css() failure so teardown honors active
    references via css_release_work_fn(), guarding online/dying-only
    cleanup with CSS_DYING (Tejun Heo).
  - Link to v1: https://lore.kernel.org/r/25ABE1D5225B64B1+1796116222.35099.6566-huxinyao@kylinsec.com.cn
 kernel/cgroup/cgroup.c | 47 ++++++++++++++++++++++--------------------
 1 file changed, 25 insertions(+), 22 deletions(-)

diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index f6df192..2982eb8 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -5772,27 +5772,30 @@ static void css_release_work_fn(struct work_struct *work)
 	list_del_rcu(&css->sibling);
 
 	if (!css_is_self(css)) {
-		struct cgroup *parent_cgrp;
+		cgroup_idr_replace(&ss->css_idr, NULL, css->id);
 
-		css_rstat_flush(css);
+		if (css->flags & CSS_DYING) {
+			struct cgroup *parent_cgrp;
 
-		cgroup_idr_replace(&ss->css_idr, NULL, css->id);
-		if (ss->css_released)
-			ss->css_released(css);
+			css_rstat_flush(css);
+			if (ss->css_released)
+				ss->css_released(css);
 
-		cgrp->nr_dying_subsys[ss->id]--;
-		/*
-		 * When a css is released and ready to be freed, its
-		 * nr_descendants must be zero. However, the corresponding
-		 * cgrp->nr_dying_subsys[ss->id] may not be 0 if a subsystem
-		 * is activated and deactivated multiple times with one or
-		 * more of its previous activation leaving behind dying csses.
-		 */
-		WARN_ON_ONCE(css->nr_descendants);
-		parent_cgrp = cgroup_parent(cgrp);
-		while (parent_cgrp) {
-			parent_cgrp->nr_dying_subsys[ss->id]--;
-			parent_cgrp = cgroup_parent(parent_cgrp);
+			cgrp->nr_dying_subsys[ss->id]--;
+			/*
+			 * When a css is released and ready to be freed, its
+			 * nr_descendants must be zero. However, the
+			 * corresponding cgrp->nr_dying_subsys[ss->id] may not
+			 * be 0 if a subsystem is activated and deactivated
+			 * multiple times with one or more of its previous
+			 * activation leaving behind dying csses.
+			 */
+			WARN_ON_ONCE(css->nr_descendants);
+			parent_cgrp = cgroup_parent(cgrp);
+			while (parent_cgrp) {
+				parent_cgrp->nr_dying_subsys[ss->id]--;
+				parent_cgrp = cgroup_parent(parent_cgrp);
+			}
 		}
 	} else {
 		struct cgroup *tcgrp;
@@ -5982,13 +5985,13 @@ static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
 	cgroup_idr_replace(&ss->css_idr, css, css->id);
 
 	err = online_css(css);
-	if (err)
-		goto err_list_del;
+	if (err) {
+		percpu_ref_kill(&css->refcnt);
+		return ERR_PTR(err);
+	}
 
 	return css;
 
-err_list_del:
-	list_del_rcu(&css->sibling);
 err_free_css:
 	INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn);
 	queue_rcu_work(cgroup_free_wq, &css->destroy_rwork);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] cgroup: Honor css refcnt on online_css() failure in css_create()
  2026-12-02  3:30 [PATCH v2] cgroup: Honor css refcnt on online_css() failure in css_create() Hu Xinyao
@ 2026-12-03 23:05 ` Tejun Heo
  0 siblings, 0 replies; 2+ messages in thread
From: Tejun Heo @ 2026-12-03 23:05 UTC (permalink / raw)
  To: Hu Xinyao; +Cc: Johannes Weiner, Michal Koutny, cgroups, linux-kernel

Hello, Xinyao.

The following is a Claude-generated review.

On Wed, Dec 02, 2026 at 12:30:03PM +0900, Hu Xinyao wrote:
> Fixes: b00c52dae6d9 ("cgroup: remove redundant cleanup in css_create")

The failure path was already freeing the css without honoring its refcnt
before this commit.

> -		css_rstat_flush(css);
> +		if (css->flags & CSS_DYING) {
> +			struct cgroup *parent_cgrp;
>
> -		cgroup_idr_replace(&ss->css_idr, NULL, css->id);
> -		if (ss->css_released)
> -			ss->css_released(css);
> +			css_rstat_flush(css);
> +			if (ss->css_released)
> +				ss->css_released(css);

Skipping ->css_released() is needed for cpu but looks wrong for memcg.
mem_cgroup_iter() saves the memcg it returns in iter->position without
holding a reference, and mem_cgroup_css_released() is what clears it,
through invalidate_reclaim_iterators(). mem_cgroup_iter() doesn't skip a
css which isn't online, so the position can be left on a memcg whose
->css_online() failed. The memcg is then freed with the position still
pointing to it.

From reading the code, cgroup1 soft limit reclaim can leave the position
there. mem_cgroup_soft_reclaim() breaks out of its loop when
soft_limit_excess() becomes zero, whichever memcg the iterator is on.
shrink_node_memcgs() can't, because a memcg with no usage is always below
min there and the walk moves past it.

The use-after-free was confirmed with a debug patch which fails
mem_cgroup_css_online() and leaves the parent's iterator on the failing
memcg. A later write to the parent's memory.reclaim triggers:

  BUG: KASAN: slab-use-after-free in css_next_descendant_pre+0x97/0x190
  Read of size 8 at addr ffff888103c68db8 by task bash/1441
  ...
   css_next_descendant_pre+0x97/0x190
   mem_cgroup_iter+0x1d1/0x440
   shrink_node+0xe5a/0x23b0
   do_try_to_free_pages+0x52a/0x1400
   try_to_free_mem_cgroup_pages+0x1e5/0x4e0
   user_proactive_reclaim+0x2b7/0x490

The same happens without this patch, so it isn't a regression. The soft
limit reclaim path hasn't been reproduced. However, CSS_DYING can't serve
both ->css_released() users. sched_release_group() must not run for a
task_group which was never online, and mem_cgroup_css_released() has to
run.

> +	if (err) {
> +		percpu_ref_kill(&css->refcnt);
> +		return ERR_PTR(err);
> +	}

->css_offline() never runs for this css, so a reference which is dropped
only from there now pins it. inode_switch_wbs() finds the memcg css with
css_from_id() and css_tryget(), and cgwb_create() takes a reference which
is held until the wb is killed. wb_memcg_offline() is what kills it on the
memcg side.

This needs a stale memcg ID which got reused by the failing css, so it
should be rare. With wb_get_create() forced on the failing memcg from a
debug patch, the parent's nr_dying_descendants stays at 1. Before this
patch, the memcg was freed with the wb still on its cgwb_list.

Similarly, shrink_worker() can leave zswap_next_shrink on the failed
memcg, which then stays pinned until shrink_worker() runs again, because
zswap_memcg_offline_cleanup() never runs for it. This one hasn't been
reproduced.

Thanks.

--
tejun

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-03 23:05 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02  3:30 [PATCH v2] cgroup: Honor css refcnt on online_css() failure in css_create() Hu Xinyao
2026-12-03 23:05 ` Tejun Heo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox