From nobody Mon Feb 9 10:11:57 2026 Received: from out-181.mta0.migadu.com (out-181.mta0.migadu.com [91.218.175.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8E229395D91; Wed, 14 Jan 2026 11:33:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768390431; cv=none; b=lLJxgJSwc8LnzAJI6w87YNA5AWESg21oX2v3nq+efpQ/jQ4/vvwEMIpkCzyOmWeRAy/iK926+t7BkEE1pgjeNnWny7nM5VRYHT5xNvwtlqR226Xt4GN/Jpz+8etLhEQqyYne2f+vqjU2y09ueYJTxmFcmNJp1BTGkjPJeOoNxpQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768390431; c=relaxed/simple; bh=19iguOSN0FR3hJYW5Hpa1N4k1E4Ug7weBVONE8ZSDjM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ygg1RPGOYVr94GUreWMHGxjqvVVLWej1i2O3W3hNOkMTkM4u2zi5U++hpBtaEIqHdOMbG1ObLGjn9y8Y/6AgkyGdJO1kDdw75kjoUMHxMa88Gsajuii0Gj75dPxJrBuoWFEzBJxuZjylmFURjkwYizSHQnfFMlK3ES3snYYUYsA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=OGrepS5E; arc=none smtp.client-ip=91.218.175.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="OGrepS5E" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1768390428; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wCcB0UGPb0CSzzkeBaE2CAurOVf6PC++ahaCYPhogr8=; b=OGrepS5EADanSDbnfcHAHrI+1eYigZjnEBbgUYBgJdtCBcfId+muqORTBBvjhGmTBycGVz SB0/6P5mAPv56UDMtFN3er1Iz8WqARaPjEdd6X4Mir7G7xmNi3r/m67Mqb7d1FTfrKBXFG lKIcziY6h/BpCZGiRqLLKPO8jFCNZZE= From: Qi Zheng To: hannes@cmpxchg.org, hughd@google.com, mhocko@suse.com, roman.gushchin@linux.dev, shakeel.butt@linux.dev, muchun.song@linux.dev, david@kernel.org, lorenzo.stoakes@oracle.com, ziy@nvidia.com, harry.yoo@oracle.com, yosry.ahmed@linux.dev, imran.f.khan@oracle.com, kamalesh.babulal@oracle.com, axelrasmussen@google.com, yuanchu@google.com, weixugc@google.com, chenridong@huaweicloud.com, mkoutny@suse.com, akpm@linux-foundation.org, hamzamahfooz@linux.microsoft.com, apais@linux.microsoft.com, lance.yang@linux.dev Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, Muchun Song , Qi Zheng Subject: [PATCH v3 11/30] mm: memcontrol: prevent memory cgroup release in count_memcg_folio_events() Date: Wed, 14 Jan 2026 19:32:38 +0800 Message-ID: <1a9b3ffaa06c5e72a6853e3513aa6564164ed45a.1768389889.git.zhengqi.arch@bytedance.com> In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" From: Muchun Song In the near future, a folio will no longer pin its corresponding memory cgroup. To ensure safety, it will only be appropriate to hold the rcu read lock or acquire a reference to the memory cgroup returned by folio_memcg(), thereby preventing it from being released. In the current patch, the rcu read lock is employed to safeguard against the release of the memory cgroup in count_memcg_folio_events(). This serves as a preparatory measure for the reparenting of the LRU pages. Signed-off-by: Muchun Song Signed-off-by: Qi Zheng Reviewed-by: Harry Yoo Acked-by: Johannes Weiner Acked-by: Shakeel Butt --- include/linux/memcontrol.h | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 6b987f7089ca4..f1556759d0d3f 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -976,10 +976,15 @@ void count_memcg_events(struct mem_cgroup *memcg, enu= m vm_event_item idx, static inline void count_memcg_folio_events(struct folio *folio, enum vm_event_item idx, unsigned long nr) { - struct mem_cgroup *memcg =3D folio_memcg(folio); + struct mem_cgroup *memcg; =20 - if (memcg) - count_memcg_events(memcg, idx, nr); + if (!folio_memcg_charged(folio)) + return; + + rcu_read_lock(); + memcg =3D folio_memcg(folio); + count_memcg_events(memcg, idx, nr); + rcu_read_unlock(); } =20 static inline void count_memcg_events_mm(struct mm_struct *mm, --=20 2.20.1