From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 330AC423E87; Sat, 3 Oct 2026 12:55:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791032118; cv=none; b=MHrhaHa5oVoxGdKFrFkzFuoyC/O6jCE72z8nTaPN2gCaYIeN4RF7TVEMOkGbeHVxdjDTzyWY3RFNsJZ5iPdmbZm7U8XcTTXBKGHfUCp3AP7b3P1cM6L0EzktsL72tKr9L2om31afUEMSWUt1l2kn26782d6TBptiRbal+rKWCU8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791032118; c=relaxed/simple; bh=wf2qnnNZyt63f6bgPwSW7gAoSrPCsJ0FRmCZEeRXJac=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=jtpBsCEHb69dAxIr3FSMYA1CjA2x8V9U1wDyof8Sc/5YMndORkdf2mdVqEqolQ34Y1RQvtcohWDtrjaDEuV2ctBq+5HWEnJhO0fEl4pGvyUmFEeQwaUrQoIFJyDFGzFTrYRlcAKBRGG8XKslk/iCAyW7+My9RFgizCP0r9HLeGs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DhoT0l/o; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="DhoT0l/o" Received: by smtp.kernel.org (Postfix) with ESMTPS id A745AC4AF15; Sat, 3 Oct 2026 12:55:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1791032117; bh=wf2qnnNZyt63f6bgPwSW7gAoSrPCsJ0FRmCZEeRXJac=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=DhoT0l/oF7p4P+fPi7y6cIq/099YTs0IZ5ri/gSoMa4vgMBZT83mIqzhJQ9TkiET7 UtHwwhya5Hnf3fOXZnbkDRl0LO+MSoV0wN1EYXy4WoRf4HNHUEM2ClWzqFOg+llI03 38Tw4Tv4KwQr0R352B59Glrx3tkz/x9YwGOLTiRwNrHd/M7gnEXiMH++wBzVuVifyJ OlQclRSYi4bCJFkEpEftkS4JVUHwnpnub8YR3AUb0oLOiVdPvJt4ASDJfQWTbLSq9F pEuwA74nlyWXuQ+PyIrqVKCHlaaDyUyhCdW6M/kj0ksQ5LlYPXhoMMoHfiqrVMJuQA HZ9UWLbLIC64w== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8AEB6CA5FE9; Sat, 3 Oct 2026 12:55:17 +0000 (UTC) From: Kairui Song via B4 Relay Date: Sat, 03 Oct 2026 20:55:14 +0800 Subject: [PATCH RFC v3 03/17] mm/memcg: add folio-based lruvec live helper Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261003-mglru-fg-v3-3-cbd4546a5bd9@tencent.com> References: <20261003-mglru-fg-v3-0-cbd4546a5bd9@tencent.com> In-Reply-To: <20261003-mglru-fg-v3-0-cbd4546a5bd9@tencent.com> To: linux-mm@kvack.org Cc: Andrew Morton , Johannes Weiner , Muchun Song , Qi Zheng , Ying Huang , Chris Li , Baoquan He , Nico Pache , Usama Arif , Michal Hocko , Roman Gushchin , Shakeel Butt , David Hildenbrand , Lorenzo Stoakes , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , Vlastimil Babka , Suren Baghdasaryan , Kemeng Shi , Nhat Pham , Youngjun Park , Zi Yan , Gregory Price , "Matthew Wilcox (Oracle)" , Baolin Wang , Ryan Roberts , Dev Jain , Lance Yang , Hugh Dickins , SeongJae Park , David Rientjes , Yu Zhao , Vernon Yang , Zicheng Wang , Chen Ridong , Tal Zussman , Kairui Song , Xiang Liu , Ehab Ababneh , zhaozhengzhuo , Lian Wang , Kunwu Chan , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, Kairui Song X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1791032114; l=1978; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=INmIp582Dn7nky+glPCA6hAuYR4TWw3FCmBj9GXz/2U=; b=oORF7caj8BINg9CX51BXlXyhC2/uK2Go8JqcZ2ZYKXWd/N359aK8Lru/BdtBdL6lwEvMpxDKH LCvoXtg1oVBCYBQEoSnSybH31YH1asP6qLVdIy5LT97H97HZA87X1Qp X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= X-Endpoint-Received: by B4 Relay for kasong@tencent.com/kasong-sign-tencent with auth_id=562 X-Original-From: Kairui Song Reply-To: kasong@tencent.com From: Kairui Song Add a helper that resolves a stable lruvec for a folio under RCU without taking the lruvec lock. It takes a folio directly so the lruvec lookup happens inside the RCU read-side critical section, which a lruvec-based interface cannot guarantee. No functional change. Signed-off-by: Kairui Song --- include/linux/memcontrol.h | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index bdc5dc925c1d..d0a9229e8cd7 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -1535,6 +1535,45 @@ static inline void lruvec_lock_irq(struct lruvec *lruvec) spin_lock_irq(&lruvec->lru_lock); } +/** + * folio_lruvec_live_get - get a live lruvec for a folio under RCU + * @folio: the folio + * + * Computes @folio's lruvec and walks up to the nearest live ancestor + * if the folio's memcg is dying. Paired with folio_lruvec_live_put(). + * The result may be stale: RCU keeps it alive but does not pin @folio + * to it. That is fine as the counters are fixed up on reparenting. + * + * Return: the live lruvec, with rcu_read_lock held. + */ +static inline struct lruvec *folio_lruvec_live_get(struct folio *folio) +{ +#ifdef CONFIG_MEMCG + struct lruvec *lruvec; + struct pglist_data *pgdat; + struct mem_cgroup *memcg; + + rcu_read_lock(); + lruvec = folio_lruvec(folio); + pgdat = lruvec_pgdat(lruvec); + memcg = lruvec_memcg(lruvec); + while (unlikely(memcg && css_is_dying(&memcg->css))) { + memcg = parent_mem_cgroup(memcg); + lruvec = mem_cgroup_lruvec(memcg, pgdat); + } + return lruvec; +#else + return folio_lruvec(folio); +#endif +} + +static inline void folio_lruvec_live_put(struct lruvec *lruvec) +{ +#ifdef CONFIG_MEMCG + rcu_read_unlock(); +#endif +} + static inline struct lruvec *lruvec_live_lock_irq(struct lruvec *lruvec) { #ifdef CONFIG_MEMCG -- 2.55.0