Re: [PATCH v2] cgroup: Honor css refcnt on online_css() failure in css_create()

From: Tejun Heo

Date: Thu Dec 03 2026 - 18:05:45 EST


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