From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796339140; b=AxQAX/IjnusvT8AzDIKGj68y/PS5fvAJss9UCuctFOhVds8P05w0Wbz9LcLH2fliVm3b ToCPTW7Y+7OLgTJSYMpea8zDVOfJPyWLQJMR6rZ67AvhOzOp6uQBMgg3U/+Xzx5LDdrRk 8w32/chtyZhKGN93KlTl4QXTD2cTEdAcZ8UOXobLSOO+JuDLtTyQhdtIxCgnxh1TIzV1b +lHbUVKu6q8vUFj5dioiQ9tfnO9Sc9ZXZOwpzFV9I65nlV2Zku9vOFwltDvc+dLkeHt2a Y8HkFKUn5IA9+xi775h2CJxF7Jfjq0RyWjSvE1EU0q7PAmdppoPLL8L9tAAksArDklg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796339140; h=Received:DKIM-Signature:Date:Message-ID:From:To:Cc:Subject: In-Reply-To:References:MIME-Version:Content-Type: Content-Transfer-Encoding; bh=qzTi6p/Ev3eULRiPVpbkLlP1ESunS+HSrZ6HOCccZH8=; b=yPmiWjXsTobTWE1REWYC4S4iniHldxlUuQlgHTVN7lNUwms4wQnF4/tfByF4gTSXz4fl +AndypYdqyjB/5aRFcubgj2DBgJSC8QuKw8b4xRmMztye2Qd6p6c9JmpBYYgRKiSQrFxh NI26P0B/5LunWqBnWue05ZlwWWyX8TbdfZ9M4/PNGTjYv2bMpxZ7T04A9s59QrNeOLbVg Pio19y2sEOhEkruEBfSjsrgGCtuxXp02RqQNurHW5+LL7nmTh6kktmGFGPCCiVfVbEiLa 50cwTwvI7QbfBgLxnSnoi3WZKtw/cNUyRqk3fmaEYrcE6U28XWa81eR4mkMFwPACNgw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=c2xMpQwn; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A2DFE606A32D; Thu, 3 Dec 2026 23:05:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=c2xMpQwn Received: by smtp.kernel.org (Postfix) with ESMTPSA id 55BEA1F000C2; Thu, 3 Dec 2026 23:05:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796339140; bh=qzTi6p/Ev3eULRiPVpbkLlP1ESunS+HSrZ6HOCccZH8=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=c2xMpQwnqkk811XdjVWZF5Y7ddt59zAtqsQi7H5+aw9s/WjY6+fNOPGVlyeg8VWa+ iMZllfHS0epGn1Gds1LuNDefaTvhf1gVIo/+X/5Mn3S+4hH6gvaQc7/Xksx6G1Fc0E jdfQP2c+xy2wZKh9c360Yol5Sb8j9o25SMz37izCZywS+Oow8z1gbVyPL8LhV9H+tb UgbF74UUHy7MO3vTWjEUBTZZXGQIwVcSWYGa/zLvTvMLISo6BM7TTZrBzqHW11R8xT OiKzKK0aCKhv4rEydwd2LgNO0RW7Pwg4Tul1ArYZiw0ZpP/+AjnE7CJCBE1uB88jb/ 0go+nX3Lwxk2Q== Date: Thu, 03 Dec 2026 13:05:39 -1000 Message-ID: <0ce53b3fedd19e14e295499ac9cd2186@kernel.org> From: Tejun Heo To: Hu Xinyao Cc: Johannes Weiner , Michal Koutny , cgroups@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] cgroup: Honor css refcnt on online_css() failure in css_create() In-Reply-To: References: X-Mailing-List: cgroups@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit 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