All of lore.kernel.org
 help / color / mirror / Atom feed
From: Tejun Heo <tj@kernel.org>
To: Hu Xinyao <huxinyao@kylinsec.com.cn>
Cc: Johannes Weiner <hannes@cmpxchg.org>,
	Michal Koutny <mkoutny@suse.com>,
	cgroups@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] cgroup: Honor css refcnt on online_css() failure in css_create()
Date: Thu, 03 Dec 2026 13:05:39 -1000	[thread overview]
Message-ID: <0ce53b3fedd19e14e295499ac9cd2186@kernel.org> (raw)
In-Reply-To: <C3CCEE8A9F4802A9+1796182203.53583-1-huxinyao@kylinsec.com.cn>

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

      reply	other threads:[~2026-12-03 23:05 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0ce53b3fedd19e14e295499ac9cd2186@kernel.org \
    --to=tj@kernel.org \
    --cc=cgroups@vger.kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=huxinyao@kylinsec.com.cn \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mkoutny@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.