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=1796156395; b=Nr7/7TnuIW3R6HDuWcJfaWqDUbBURpqOBmdSFeneXSr6nMCzJgu2EY973HUTYD/fB3hX refyLxxLgKGf1/UD/1YDfIJWNqicPLOTVXzjbWheDf9CuX9CT9XjU/6NSS2lFLKDrq/Bp Jdsferv/XPzmaY8mVX+fFZslgD5Sp7DohwmH9bmgxi71APabpB4T5Jbs0s/atvSyRyWQ0 pCB1zALBVtB/pkm7rhN+eZQCTKNTn4MIZG0K50BaS09Ms6NzwWajbxAolKf6DxB2gDARM RX6+Ph6oCmJJ/HcjYLlSUVLhdZdmQ0FhFyKS8yDOcfLFXLwuSIPHqNjacDgp79m5CJA== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796156395; 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=Cgbozr/c+1l7uQ01JWUVFcuMuL/XNwtPtfVwC/yVWkg=; b=nWMPjTu8QBFlomMFydXH3MecLy7tmou+blFQGa1EIX6jjHYCmMadtmVQUHZGKon8WaWo A7uA7V0td8OXCyP+E7ljBS8QUHP6F7B+qVTcA8/4JYlb2KWrH9lY3/Et+R3eXPeufhdCZ +feYaS8fco47NpSmIa8SFTKRWMQotWQnKNRKGJseXcqLKVpD8IN1oYVmys1o8xG97Jodd BN0s9GSMSlq6KIcq1sa5BY8MPvLcar5nVs7hbXo4yaG3xD6DeFuiOyby8tmDgwpL/mx+O 2YwSS7pYfZZTeHIqUdUnMTLCnare39xLrhBRvw0yW5gPQMu/HQMIzvmUE4R9svwh3QA== 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=FeQBDkQq; 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 4D70E606A327; Tue, 1 Dec 2026 20:19:55 +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=FeQBDkQq 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=1796156395; b=czliP9TFYHXW3PF/yKQZYKATm/+giNHF5yzmwQ1/m2BoQ5RzRgL42elP8hVXgqWkTAl5 wu2+M946jOOrmmouCMI9AaLo9kbCjbWLxlAoNcqmWvAAuSPkVkEdqUA9bPSqRmXUgEWUb rRvE5ellgcMN70/0z7sPvlacmzuFGWIKOtrWE5BwA9yTnZCxAlu+0KEuUiddhSzcIhnsu 3nVZesVvxeB+ADtHgU8qV+sDr4ZNEY26lyySeqI6j6rbRey9U4lPQQ/m3F6PGwo86byHQ g5Spc6dRhdOnhebhE4l9yK57KIaJEG8ACliSLHv5wWiS3fCpks8AHnQ8mm9YJwxWXhQ== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796156395; 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=Cgbozr/c+1l7uQ01JWUVFcuMuL/XNwtPtfVwC/yVWkg=; b=KqsdWE0y76yuiTpX7+wOydQD4VQ45y/5jhmjsI+niG4xXmaN79JJOTuJmcnY28+rylC4 sxypT/AHzHpLwNr+1mQ/DO8wTH8Y/pvaSjC8QeL8HAnfbN6H++x/8ozB5GVOudrgo0gun EwT5US/2sQqSf0ogK10rkDCsuVCjTBFkOAbg8mHeL4ZqpIVyN24SRQ1HVNgxTTDWou8h0 rp/eCysSknx//T1aZFyFeOmTUhJ13+bshm4ZDxjUIN9I5od0xog4NWt0BzU78KpCYDStO rcdc+wWTHrniW76H+MLqwLevNs8mGd7d+bnX8ANAattltpaLOAOnMb7tTWk3etOxw7Q== 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 8D0461F00A17; Tue, 1 Dec 2026 20:19:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796156395; bh=Cgbozr/c+1l7uQ01JWUVFcuMuL/XNwtPtfVwC/yVWkg=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=FeQBDkQqsIJi3by6qY/rJKsOaF1MUraxHedwyb1rXqPnKDJX2I8s6OEafx+H6Rfli ibIiv42ZvQlIYWyRB1KRIincVPEEL+GfPG9aA9utU2AupBL7zepZqSidL/lRkLFGrd fsXB4/v+OT+GqORi3LumCL13hUW8REM4MHB/HVvY5cFOqfg/BJoyUdpHnVSHmhMXIZ KOWoYAol8X7vrp+NrK3qxvgI0EBDJIQF9lHMdbQu1LLAVe7hdALnP+efYfo7ScdiVY Cf5UXCT8SV+nSmEA835h8Hqw4IeREpmGPclvpuXuvPA5Q+DclaD2ybwnHV3znGgCZb NQsCPjlyrtgcw== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 7B6574F0B3; Tue, 1 Dec 2026 20:19:21 +0000 (UTC) From: Kairui Song via B4 Relay Date: Wed, 02 Dec 2026 04:18:48 +0800 Subject: [PATCH v7 08/19] mm/mglru: make folio lru referenced times count a generic API 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-8-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=4890; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=ttFmOeEr1izsLZLNvxXWvLR9wQw0aYtQRd/khYmN3v4=; b=I/FaXDGjy+Udpb9uwkPQwSl4F3w9JQ2g+MVEzZ/5TmvavZtA1yck/TGnc9rsySxe5pM1xUc1N P7tNZ+om7EjD9WZxnhz8lEbfevm4Yz+muzPkBNGmmm5JZn2Y90O6WPy 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 Pure code shuffle, no behavior change. Make the folio LRU refs helpers available regardless of CONFIG_LRU_GEN. Without MGLRU the extra bits are never set, so they are inert. Move the lru_gen_* helpers into the CONFIG_LRU_GEN section. This prepares for unifying the API for checking folio referenced and workingset status. folio_migrate_lru_refs() now migrates the complete refs count, including PG_workingset (bit 1 of the encoding), bitwise identical to the direct copy dropped from folio_migrate_flags(). lru_set_refs_flags() skips the nonexistent mask bits when LRU_REFS_WIDTH collapses to 0, preserving the two independent flags for !CONFIG_LRU_GEN builds. Tested-by: Zhao Zhenzhuo Reviewed-by: Kunwu Chan Tested-by: Andrew Morton Tested-by: Wang Lian (ByteDance) Tested-by: Tianyi Chen Signed-off-by: Kairui Song --- include/linux/mm_inline.h | 55 ++++++++++++++++++++++++----------------------- mm/migrate.c | 2 -- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h index 3cdad5765b58..1749cbddf125 100644 --- a/include/linux/mm_inline.h +++ b/include/linux/mm_inline.h @@ -206,6 +206,12 @@ static inline int lru_get_gen_flags(unsigned long flags) return gen; } +static inline int folio_lru_gen(const struct folio *folio) +{ + return lru_get_gen_flags(READ_ONCE(*const_folio_flags(folio, 0))); +} +#endif + /** * lru_set_refs_flags - Set the LRU referenced count to folio flags. * @flags: pointer to the folio flags @@ -228,7 +234,8 @@ static inline void lru_set_refs_flags(unsigned long *flags, unsigned int refs) *flags |= BIT(PG_referenced); if (refs & BIT(1)) *flags |= BIT(PG_workingset); - *flags |= ((unsigned long)refs >> 2) << LRU_REFS_PGOFF; + if (LRU_REFS_WIDTH) + *flags |= ((unsigned long)refs >> 2) << LRU_REFS_PGOFF; } /** @@ -274,13 +281,31 @@ static inline void folio_set_lru_refs(struct folio *folio, unsigned int refs) } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags)); } +#ifdef CONFIG_LRU_GEN void folio_inc_lru_refs(struct folio *folio, unsigned int flags); +#else +static inline void folio_inc_lru_refs(struct folio *folio, unsigned int flags) +{ + /* Should not be called with !CONFIG_LRU_GEN */ + WARN_ON_ONCE(1); +} +#endif -static inline int folio_lru_gen(const struct folio *folio) +/** + * folio_migrate_lru_refs - copy the reference state to a new folio + * @new: the destination folio + * @old: the source folio + * + * Transfer the reference state to @new during migration: the MGLRU + * refs count, or PG_referenced and PG_workingset (bits 0-1 of the + * encoding) for the active/inactive LRU. + */ +static inline void folio_migrate_lru_refs(struct folio *new, const struct folio *old) { - return lru_get_gen_flags(READ_ONCE(*const_folio_flags(folio, 0))); + folio_set_lru_refs(new, folio_lru_refs(old)); } +#ifdef CONFIG_LRU_GEN static inline bool lru_gen_is_old(struct lruvec *lruvec, int type, int gen) { unsigned long min_seq = READ_ONCE(lruvec->lrugen.min_seq[type]); @@ -438,20 +463,6 @@ static inline bool lru_gen_del_folio(struct lruvec *lruvec, struct folio *folio, return true; } - -/** - * folio_migrate_lru_refs - copy the reference state to a new folio - * @new: the destination folio - * @old: the source folio - * - * Transfer the reference state to @new during migration: the MGLRU - * refs count, including PG_referenced, or just PG_referenced for the - * active/inactive LRU. - */ -static inline void folio_migrate_lru_refs(struct folio *new, const struct folio *old) -{ - folio_set_lru_refs(new, folio_lru_refs(old)); -} #else /* !CONFIG_LRU_GEN */ static inline bool lru_gen_enabled(void) @@ -480,16 +491,6 @@ static inline bool lru_gen_del_folio(struct lruvec *lruvec, struct folio *folio, { return false; } - -static inline void folio_inc_lru_refs(struct folio *folio, unsigned int flags) -{ -} - -static inline void folio_migrate_lru_refs(struct folio *new, const struct folio *old) -{ - if (folio_test_referenced(old)) - folio_set_referenced(new); -} #endif /* CONFIG_LRU_GEN */ static __always_inline diff --git a/mm/migrate.c b/mm/migrate.c index 96b66a272379..f59dd50b3f22 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -807,8 +807,6 @@ void folio_migrate_flags(struct folio *newfolio, struct folio *folio) folio_set_active(newfolio); } else if (folio_test_clear_unevictable(folio)) folio_set_unevictable(newfolio); - if (folio_test_workingset(folio)) - folio_set_workingset(newfolio); if (folio_test_checked(folio)) folio_set_checked(newfolio); /* -- 2.56.1