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 83ADF43B3EA; Mon, 24 Aug 2026 15:26:32 +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=1787585192; cv=none; b=dY9m2zweEFk9MGrJ+V6t+pRC7Bp/zzSq1VEVMNI7S+Dfr9N1eNe9DJ50M1e1DIaASjU7bAwAFCU/NsgXOg7+eSgqPSoOPT/KGfzx/0rbW0T2lgAtMM/oewKVQx9rqZAc7CtFx8ztDm+IssXJtLAopJ99uHpyizvTaXQZiDFBvT4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787585192; c=relaxed/simple; bh=P+17iUIaKozZUszrRwBPon4mP56KzLJ6WpxOwu3K73M=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=bg0eHc8nDhwd16Vkw1JcD/iiaB+rNjP8nXpobLD+RWvg/hM4ER8OeYI8SQxoF+KE3rj4YRQCdAXWRC7qrgXbW1Yx9zO1k0EvqkWpOny//UgSBF7s48WechAn9JqSHIVhVvuRxuZaZhiQpz7IBX7BlYpz25Zjink4VwRCx97dE6U= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=evkVSOi/; 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="evkVSOi/" Received: by smtp.kernel.org (Postfix) with ESMTPS id 0181CC2BD00; Mon, 24 Aug 2026 15:26:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1787585192; bh=P+17iUIaKozZUszrRwBPon4mP56KzLJ6WpxOwu3K73M=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=evkVSOi/AASgqhx0m0pslJmESHDQjSwT2XZ5pV4+3mjA4X+KiInpvYmecFJhALIbS ghsOjUBgDXcgO130CF/TYN21VMOCSLCcHoohwcE8K/NZQcorYMpnF4hErX3gfwuejo YCuNPm9Aq/8c4qqwNhZGY5FfvG6LYI1n0lpMd9KObZ4IWvEE1kHg2ofh8IHU/CBzoQ KumQ4PKXKFDiKP7b7QR4VUN3G+7X6wtDGyjLTsag1D2l8xTTfqrhIkqkKuIUtcMlkT yjG7KbW9n8iAzYgFdLhioROxcv8fCBtrFez5xXn8GwebSK6/X4zB9HDO1dR2KnVjCB vO6iAX2jHYTUw== 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 DB982C61DB4; Mon, 24 Aug 2026 15:26:31 +0000 (UTC) From: Kairui Song via B4 Relay Date: Mon, 24 Aug 2026 23:26:30 +0800 Subject: [PATCH v2 3/6] mm/migrate: copy all referenced state via folio_migrate_lru_refs 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: <20260824-mglru-flags-cleanup-v2-3-0104132114ae@tencent.com> References: <20260824-mglru-flags-cleanup-v2-0-0104132114ae@tencent.com> In-Reply-To: <20260824-mglru-flags-cleanup-v2-0-0104132114ae@tencent.com> To: linux-mm@kvack.org Cc: Andrew Morton , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , Baoquan He , Shakeel Butt , Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , Chris Li , Baolin Wang , Ridong Chen , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Yu Zhao , Zi Yan , Qi Zheng , cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Kairui Song , Kairui Song X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1787585189; l=3250; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=cPJ+z22jDdcCM3atcpqaDtz2zW4ZvNzKe4hDfgAQsCE=; b=yVbj2v8HH/FoZaiXjkE0G1SM5F9E68EtrLhFxk6egxlIGeT+4fZ0IJPVD9g/sfre3W6ZuzyaO qhoBG9o1nzMBvRIH/Nl/0QiyoXI6itIZ8RvOPdhjdo3dIHFQ9qA9Un2 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 folio_migrate_flags() copies PG_referenced separately from the MGLRU refs counter, which folio_migrate_refs() transfers. Yet under MGLRU, PG_referenced and the refs counter bits together describe the referenced status of a folio. Consolidate the two: rename folio_migrate_refs() to folio_migrate_lru_refs() and let it copy the complete referenced status, i.e., the MGLRU refs count including PG_referenced, or just PG_referenced for the active/inactive LRU. Drop the open-coded PG_referenced copy so the referenced status is transferred in one place. No behavior change is intended: under the active/inactive LRU the extra bits are unused, so operating on them is a noop. Reviewed-by: Baoquan He Signed-off-by: Kairui Song --- include/linux/mm_inline.h | 20 +++++++++++++++----- mm/migrate.c | 6 +++--- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h index edfaf2661812..7f91a89b5ba3 100644 --- a/include/linux/mm_inline.h +++ b/include/linux/mm_inline.h @@ -365,11 +365,19 @@ static inline bool lru_gen_del_folio(struct lruvec *lruvec, struct folio *folio, return true; } -static inline void folio_migrate_refs(struct folio *new, const struct folio *old) +/** + * 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) { - unsigned long refs = READ_ONCE(old->flags.f) & LRU_REFS_MASK; - - set_mask_bits(&new->flags.f, LRU_REFS_MASK, refs); + BUILD_BUG_ON(LRU_REFS_MASK & BIT(PG_referenced)); + folio_set_lru_refs(new, folio_lru_refs(old)); } #else /* !CONFIG_LRU_GEN */ @@ -398,8 +406,10 @@ static inline bool lru_gen_del_folio(struct lruvec *lruvec, struct folio *folio, return false; } -static inline void folio_migrate_refs(struct folio *new, const struct folio *old) +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 */ diff --git a/mm/migrate.c b/mm/migrate.c index 15b45832bcfa..96eda490ba43 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -776,8 +776,6 @@ void folio_migrate_flags(struct folio *newfolio, struct folio *folio) { int cpupid; - if (folio_test_referenced(folio)) - folio_set_referenced(newfolio); if (folio_test_uptodate(folio)) folio_mark_uptodate(newfolio); if (folio_test_clear_active(folio)) { @@ -807,7 +805,9 @@ void folio_migrate_flags(struct folio *newfolio, struct folio *folio) if (folio_test_idle(folio)) folio_set_idle(newfolio); - folio_migrate_refs(newfolio, folio); + /* Copy the reference state, including PG_referenced */ + folio_migrate_lru_refs(newfolio, folio); + /* * Copy NUMA information to the new page, to prevent over-eager * future migrations of this same page. -- 2.55.0