From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796156386; b=YpLWyZ8kqQl6sYnL3vi1J10+1BiPngL4y/LnFfZfdw9RPfIf9likP9qevuC6uP9Wgu5p tslU7WxiftVU2/7NNRvQ1hctKe2py7phslpZHjkW3LkAct8/sZtleeAcSCmzwEEGKmcbf XgjrPgdcL9MLJHQAopu2jRmMbDY7+t+vvc0S3ylKmobK2PvD9oNwEGtif6T92BzVRQaAi Tffb/ZuwF2Cr5jXl86YV65tCpSu+PXUmFTJ/qXSLzH6I8UrSqh+ZdmfqQG+GeML/Oz6tf 6r65faQyh6UKk5EZGJnJEkS3QgbKjXyg81l/95e6h9Iqksr+hmoABqCukKf2hgu9aCw== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796156386; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=d+/THulD3ffcc7482FU1Hrb59IfXkJ0+UwmyX2pjA5E=; b=3XM8SWSyU/43O3HGzFvcpuORn/oIFGZOMagU1N8f7z2adUqs080tn5CceTZnyUBMqzys LMOSdR8NRyavRGE807oJiTBxdI2UelwtehenNS0eqqpH2vCV9DPl1pf94chf3uRLt/Vbf vBMvZrGcnqzVFtteHRPAy6VWk1h3QBhAuio7P9zmquUl+BANkWYoxFXQpbFKBuycfxjyF tMqlnLCVksMdjEHmrM9CX0yuiwFv21RMRos1vFkOzanNRoM/+XxUnaTUdq1qYjWmg9Vtb h5QjPGD/x4Vcwe8Q8qvvqp5VS559kjpGAPJFh8TgZazGQYTcJQGlGQnVDhgjblPv0gg== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=nsau0ho9; arc=pass header.oldest-pass=0 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 967DF606A697; Tue, 1 Dec 2026 20:19:46 +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=nsau0ho9 Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1796156386; b=bl/WZET5w4V/XBBqi2/Kw/7AI1TqVrccX61ZHXjgRacC4wFVNxoZyUHUbwD6YHkV3zqW EQtCsW1HTFkl7bkKQRpXF6Ukp18mpVEayDU29teo4F6t5SN6iPkFsKQUhmPR4vdNnQRTg /3QXgU5cj10l8AoyH1P4BMNtDECqVMy0U4tBJsh3Wyf5hOCGgKazsVnYt/g5lWhG+WoJN qWhC5Otstc6uyjS0ZhHlVDu6wsVXDGgxLBOKOdMErzylu5sLWE1eRN2tjPjOM/sav1Ko2 oFOBffB8huvWabnJQcn7rClYbNHDATliFV4aj+1WgVrLYaxIIAQct9oFNdyPpQOc3Xg== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796156386; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=d+/THulD3ffcc7482FU1Hrb59IfXkJ0+UwmyX2pjA5E=; b=KtTM/n3uSSToyLUSJVmHvKGAL22OBTRQUaPWcOzg87aGnWgnH2dMESg5SWDzKjDfpK3Z qit0oGgU5wk/0+GBO8Yq5C+7B8sRDrTp1lA6R4EoxkVCOLP/wjI/q1HEkAjGMfHsErtoM J9nTjtyEOfKCOmOUcDeZVYZXpN/RBgILfaYUUbdnLwMERPUukg1aXTyTbsFGIgqTXGPZ7 QVqG8W5GVISFXCl5PYN4RofhOlH4mhNxdaLE+hS2MHfIuZc0Pe4cqi6+YbLoYMeWo4LLt HwFQh7EenQqZTUdFHb1epUJIZYZ41QA9FVUz1XwXp6jAR542P19vYTKakDAtNf7lsOQ== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id 3241E1F00899; Tue, 1 Dec 2026 20:19:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796156386; bh=d+/THulD3ffcc7482FU1Hrb59IfXkJ0+UwmyX2pjA5E=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=nsau0ho9WHUE3mF6WCv4eyblTJ16NDR6e484xdeyudjeh/t9HUPt2zeJs21nbaQO4 N+vP+b6F648ShxGCxicosMMtRtt/Hv4PYh2TaF0C041Sq+gTVxv6XREgdB4gvAeGq0 9IUGl5NG5Zqw9VBF7YvfLiaWo19ru/axyu3gdhdehGv7/oPXzK6HKL7KGIllqDVh0y 4nTCEalGPSWF1v/u4hLTAXvmYJWFDjNz8afNDoL2mABjcmQ9UQW2tU1c5nWCVqPImZ WmRWa7c5Pq7KOUq+6GLyXV1mTXoC0JrV6XN+V9uJNtJBCPqI03RHA3V0VL3sRo/enE rWEQbYVo7LVUg== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id C5B444F0BB; Tue, 1 Dec 2026 20:19:21 +0000 (UTC) From: Kairui Song via B4 Relay Date: Wed, 02 Dec 2026 04:18:50 +0800 Subject: [PATCH v7 10/19] mm/mglru, gup: mark folios referenced via a fast helper X-Mailing-List: linux-kernel@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="utf-8" Message-Id: <20261202-mglru-fg-v7-10-5965a96d86d0@tencent.com> References: <20261202-mglru-fg-v7-0-5965a96d86d0@tencent.com> In-Reply-To: <20261202-mglru-fg-v7-0-5965a96d86d0@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 , "Liam R. Howlett" , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , Vlastimil Babka , Suren Baghdasaryan , Nhat Pham , Youngjun Park , Zi Yan , Gregory Price , "Matthew Wilcox (Oracle)" , Baolin Wang , Ryan Roberts , Dev Jain , Lance Yang , Hugh Dickins , SJ Park , David Rientjes , Yu Zhao , Vernon Yang , Zicheng Wang , Tal Zussman , Kairui Song , Xiang Liu , Ehab Ababneh , Zhao Zhenzhuo , Lian Wang , Joanne Koong , Kunwu Chan , Hui Zhu , Maxim Muzafarov , Tianyi Chen , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, damon@lists.linux.dev, linux-doc@vger.kernel.org, Kairui Song , Ridong Chen X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796156357; l=5939; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=Wy+3pP+FafsAGVPcSELRFZ94CElt6T7HVh4OKQaqEWY=; b=AJY12ae6+3FBtnnor9asxk3/PAP/gZNUdr//a5OMx0uKznvYR+xI/CojCyWIesvDhOsgp5NoC /hUIw4bSIC4Co5mw3N+7XytsazklU0xGd2GtT+PqmnrS4ThzXoZytV8 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 Content-Transfer-Encoding: 7bit From: Kairui Song Introduce folio_inc_lru_refs_fast(): raise refs from 0 to LRU_REFS_REFERENCED via cmpxchg, or just set PG_referenced for the classical LRU as before. No counter update or promotion is needed, nonzero MGLRU refs are left unchanged. The classical path is taken for a folio that is off the gen lists while MGLRU is off, not on lru_gen_enabled() alone: while MGLRU is being turned off, a folio can still be on a gen list after the static key flips, and setting PG_referenced there would bump its refs count. That check uses lru_get_gen_flags(), so move the helper out of the CONFIG_LRU_GEN block, it is now built with the option off, where LRU_GEN_WIDTH is 0 and LRU_GEN_MASK is zero. Keep the decoded value in an int so the subtraction of the offset-by-one encoding cannot fold to ULONG_MAX before it happens to avoid -Woverflow error with CONFIG_WERROR. This helper only aims to be fast, it exists so the gup fast path can stay a fast path: folio_set_referenced() would corrupt the MGLRU refs count, of which PG_referenced is bit 0, and folio_inc_lru_refs() is too heavy, while this one only raises the count to LRU_REFS_REFERENCED and never moves the folio to another generation, so a cmpxchg is all it needs. The new helper doesn't account for refs above LRU_REFS_REFERENCED but that's fine for now as GUP never used that or anything similar for CLRU either. Convert the gup fast-path sites. No behavior change for classical LRU, no regression for MGLRU either. Just prepare for the following changes by providing a helper that follows the new convention. Tested-by: Zhao Zhenzhuo Reviewed-by: Kunwu Chan Signed-off-by: Kairui Song --- include/linux/mm_inline.h | 63 +++++++++++++++++++++++++++++++++++------------ mm/gup.c | 6 ++--- 2 files changed, 50 insertions(+), 19 deletions(-) diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h index e88229aeb641..dde3fc6e7c0c 100644 --- a/include/linux/mm_inline.h +++ b/include/linux/mm_inline.h @@ -127,6 +127,22 @@ static __always_inline enum lru_list folio_lru_list(const struct folio *folio) #define MIN_SEQ_UNBOUNDED 0UL #define MAX_SEQ_UNBOUNDED ULONG_MAX +/** + * lru_get_gen_flags - Return the LRU generation number from folio flags. + * @flags: folio flags + * + * Returns: A number between 0 and (MAX_NR_GENS - 1), inclusive. Returns + * -1 if the flags indicate the folio is off the list (e.g., isolated). + */ +static inline int lru_get_gen_flags(unsigned long flags) +{ + int gen = (int)((flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF); + + /* The gen is stored offset by one, so zero means off-list */ + VM_WARN_ON_ONCE(gen > MAX_NR_GENS); + return gen - 1; +} + #ifdef CONFIG_LRU_GEN static inline bool lru_gen_switching(void) @@ -190,22 +206,6 @@ static inline void lru_set_gen_flags(unsigned long *flags, int gen) *flags |= (gen + 1UL) << LRU_GEN_PGOFF; } -/** - * lru_get_gen_flags - Return the LRU generation number from folio flags. - * @flags: folio flags - * - * Returns: A number between 0 and (MAX_NR_GENS - 1), inclusive. Returns - * -1 if the flags indicate the folio is off the list (e.g., isolated). - */ -static inline int lru_get_gen_flags(unsigned long flags) -{ - int gen = ((flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1; - - /* Exclude the legal -1 from the unsigned MAX_NR_GENS comparison */ - VM_WARN_ON_ONCE(gen != -1 && gen >= MAX_NR_GENS); - return gen; -} - static inline int folio_lru_gen(const struct folio *folio) { return lru_get_gen_flags(READ_ONCE(*const_folio_flags(folio, 0))); @@ -493,6 +493,37 @@ static inline bool lru_gen_del_folio(struct lruvec *lruvec, struct folio *folio, } #endif /* CONFIG_LRU_GEN */ +/** + * folio_inc_lru_refs_fast - Bump folio refs count without promotion. + * @folio: the folio + * + * Raise refs from 0 to LRU_REFS_REFERENCED and leave hotter folios + * untouched. For a folio off the gen lists with MGLRU off, a plain + * PG_referenced set. This replaces folio_set_referenced(), which corrupts + * the MGLRU refs count, for paths that must stay cheap like GUP-fast: no + * lruvec lookup, accounting or promotion. Use folio_mark_accessed() + * everywhere else. + */ +static __always_inline void folio_inc_lru_refs_fast(struct folio *folio) +{ + unsigned long new_flags, old_flags = READ_ONCE(*folio_flags(folio, 0)); + unsigned int refs; + bool has_gen; + + do { + has_gen = lru_get_gen_flags(old_flags) >= 0; + refs = lru_get_refs_flags(old_flags); + if (has_gen || lru_gen_enabled()) { + if (refs) + break; + } else if (refs & LRU_REFS_REFERENCED) { + break; + } + new_flags = old_flags; + lru_set_refs_flags(&new_flags, refs | LRU_REFS_REFERENCED); + } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags)); +} + static __always_inline void lruvec_add_folio(struct lruvec *lruvec, struct folio *folio) { diff --git a/mm/gup.c b/mm/gup.c index 7605da0e06d8..da04bebfff43 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -2921,7 +2921,7 @@ static unsigned long gup_fast_pte_range(pmd_t pmd, pmd_t *pmdp, gup_put_folio(folio, 1, flags); goto pte_unmap; } - folio_set_referenced(folio); + folio_inc_lru_refs_fast(folio); pages[nr_pages] = page; nr_pages++; } while (ptep++, addr += PAGE_SIZE, addr != end); @@ -2990,7 +2990,7 @@ static unsigned long gup_fast_pmd_leaf(pmd_t orig, pmd_t *pmdp, for (i = 0; i < nr_pages; i++) *(pages++) = page++; - folio_set_referenced(folio); + folio_inc_lru_refs_fast(folio); return nr_pages; } @@ -3032,7 +3032,7 @@ static unsigned long gup_fast_pud_leaf(pud_t orig, pud_t *pudp, for (i = 0; i < nr_pages; i++) *(pages++) = page++; - folio_set_referenced(folio); + folio_inc_lru_refs_fast(folio); return nr_pages; } -- 2.56.1