From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7E5A2606A32F for ; Fri, 4 Dec 2026 12:59:01 +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=ibkU4TmL Received: by kanga.kvack.org (Postfix) id 9AEA26B05B8; Fri, 4 Dec 2026 07:59:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9862D6B05DF; Fri, 4 Dec 2026 07:59:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 89BE76B05E0; Fri, 4 Dec 2026 07:59:00 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 61A976B05B8 for ; Fri, 4 Dec 2026 07:59:00 -0500 (EST) Received: from smtpin27.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay07.hostedemail.com (Postfix) with ESMTP id F1ECC160385 for ; Fri, 4 Dec 2026 12:58:58 +0000 (UTC) X-FDA: 85506305556.27.8979AB5 Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by imf13.hostedemail.com (Postfix) with ESMTP id 3D57120004 for ; Fri, 4 Dec 2026 12:58:57 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=ibkU4TmL; spf=pass (imf13.hostedemail.com: domain of david@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=david@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796389137; b=Uw1z7eRtiuAyt3VpypoxRfXAwE5008lo/90+Vn+N43HR4PBVIzyUjJFsv2s6SIY83qQi8K u/6YJR/fLgAIKWFtzIvy/dbfMe10Atlj71uVHPHqp+t0AG9piyzHXmh5MI2GZVimLluqsz vUaPSS/d5oeZXC65hzSjXGCFvHydwg0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796389137; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=WqCPgDINdDPRc87PjfPl3NlB0dngLfGGdqjwvn7uJRM=; b=hBvjNMJkB7vFeeiR4AdokVmJJKV4mz+sshEFI+7xDUTGWPeNR66+p0wcK3krL06C11l0dC ak21OqgbqXoyuFlmhsdyeY68saCYGWTmLEC9DOYA+WR6Md7zFtpBrYh9b1d7RatOKlQztD EOQSIqGbkgAec4WOvqOOn9tDc1CJ84M= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=ibkU4TmL; spf=pass (imf13.hostedemail.com: domain of david@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=david@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id A62EA633D0; Fri, 4 Dec 2026 12:58:56 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EAE8F1F00939; Fri, 4 Dec 2026 12:58:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796389136; bh=WqCPgDINdDPRc87PjfPl3NlB0dngLfGGdqjwvn7uJRM=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=ibkU4TmLbjerekTJvkJjVYPEV8ZrZ0Ajp9vNg+EwVqYmhZRAySGFGeiSQ962Af6xB LL/FnRnltnZVPj1sNtmnu92Mq1qk/zxEa6I82puZBoENUlcIxVZpJXcXZLc4iuI2Rg Uq4QYUqCbwJS03MuOlck8HF+cKrb+e8cMAFcRvnLnac3pMoMH6OqEPaa66TMQJevKv PeGQ852MletHvBnzsmF9phj1CGvWFmDzKGpn+jw44ULdwXJB+uY0dpe5gnV+nzV1xg tEq0GbC01mpIBS86bXTVDkgSr4T37iYvNLOTu1s3KOwzJ1PCQmNtc3upndtC6rHUMC r3GhpA1NOqI/Q== From: "David Hildenbrand (Arm)" Date: Fri, 04 Dec 2026 13:50:19 +0100 Subject: [PATCH v2 28/31] mm/ksm: use a migration counter to detect KSM folio migration X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261204-fixes_core_3-v2-28-9aad65727632@kernel.org> References: <20261204-fixes_core_3-v2-0-9aad65727632@kernel.org> In-Reply-To: <20261204-fixes_core_3-v2-0-9aad65727632@kernel.org> To: Hugh Dickins , Baolin Wang , Andrew Morton , Mike Rapoport , Peter Xu , Michal Hocko , Kiryl Shutsemau , Manfred Spraul , Davidlohr Bueso , Cyrill Gorcunov , Zi Yan , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Usama Arif , Lorenzo Stoakes , Rik van Riel , Vlastimil Babka , Harry Yoo , Jann Horn , Yang Shi , Zach O'Keefe , Song Liu , David Stevens , Kefeng Wang , Alex Williamson , Suren Baghdasaryan , Dave Jiang , Matthew Wilcox , Miaohe Lin , David Rientjes , Shakeel Butt , Jan Kara , Mel Gorman , Jason Gunthorpe , John Hubbard , Gerd Hoffmann , Vivek Kasireddy , Dave Airlie , Matthew Brost , Joshua Hahn , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple , Hagen Paul Pfeifer , James Bottomley , Felix Kuehling , Alex Sierra , Balbir Singh , Kairui Song , Qi Zheng , Axel Rasmussen , Yuanchu Xie , Wei Xu , Baoquan He , Oscar Salvador , Muchun Song , Dan Williams , Rakie Kim , Hao Li , Christoph Lameter , Roman Gushchin , Sebastian Andrzej Siewior , Clark Williams , Steven Rostedt , Alexei Starovoitov , Xu Xin , Chengming Zhou , Shivank Garg Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-cxl@vger.kernel.org, linux-rt-devel@lists.linux.dev, SJ Park , "David Hildenbrand (Arm)" , Chris Mason X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=10494; i=david@kernel.org; h=from:subject:message-id; bh=wKZfMdhQEbep8L61GwbREENp6/NkB0YR6+MoEvp1byk=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLKFtjNK1H/y/lXw7y7T8l8TFD1KjMU8Lghv+T5J/ojOD cNyxe1TO0pZGMQ4GGTFFFmCgu8bvHy5rrbajY8ZZg4rE8gQBi5OAZiI8i1Ghr08+uYah53rp3w1 aFy98Pg627SGkOJl0zkVtzVu6mMp42dkOHXceXeVol/NDP2d17XvKnY9XXpHIvgDX9bBLbuYTmV ncwMA X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A X-Stat-Signature: 3ekg7gffm8mrg9p8cai95nxcrznbpu3h X-Rspam-User: X-Spam-Status: No, score=-19.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Rspamd-Queue-Id: 3D57120004 X-Rspamd-Server: rspam01 X-HE-Tag: 1796389137-635307 X-HE-Meta: U2FsdGVkX1/O178A4S9YM9aurkBFX0e6aoBgKWaUBtJ2RLC8PmhOLAdXK6BaElv7h+ilF4Uhysso9h5TKvlnoM3yX2JQ1m9U3SdGgk7pqTTOibWSnfFPoym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sVqGotQamb1hcp6IBIHiV31mLxerEU2BIDoBkTafWcsMmaE0fZwKwftU4iH+tmvnSwCxUjdSOSmtkr0v2PGgVkGIYNi7R3Lc7ALcM7+wTbdZpLVOZu3UjcoOujdapu3P3dflE9frs7YRkpZsj21Wrw7eZjcHfiSD0dHvoXFm9HHr6Shq81ZMBf36xrRabgo3TqOjMJMGP9+0Vj1dTgJFQEhrPpJVK0G+SlHUzbIg6kRP3ukwGE6mHqh6fMClPRV5LoK+PjRvPReNh5cv8InT34vbbDft4jVyAdrDITYfNZashLUHGTQldPvviDoQwt0z/T2ufhr/OVNMQxL8UkxlF0awBQWpO+MOwRXujXPIapjwwHx4FmHH7KyxyHxAd89c7VrwsfLk+4Kv1O4dAhS5 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 ksm_get_folio() holds no reference on the folio referenced by stable_node->kpfn. If the folio at stable_node->kpfn is no longer our KSM folio, it was either freed or migrated. When freed, we can drop the stable node; when migrated, we have to look again for the migrated folio. However, a second migration might have migrated the KSM folio back to the very same page in the meantime: stable_node->kpfn would be effectively unchanged and the KSM folio is still alive. Consequently, we might remove the stable node of a KSM folio that is still mapped, resulting in a use-after-free of the stable node, for example, in rmap_walk_ksm(). Let's fix it by storing a migration counter in the upper bits of stable_node->kpfn, incremented on each migration of the KSM folio. The existing check whether the value changed will then also detect a KSM folio that migrated back, and a freed KSM folio will still be detected as such. As a pfn never needs more than 64 - PAGE_SHIFT bits, convert stable_node->kpfn to a u64, and use the remaining PAGE_SHIFT bits (at least 12) for the counter. A wrap-around would require 4096 migrations of the KSM folio while ksmd is between two reads in ksm_get_folio(): not relevant in practice, because even two migrations back and forth would be hard to hit. This change doesn't grow the size of a stable node on 64-bit, which is what we care about. We don't care about 32-bit and likely we should make KSM 64-bit-only soon. Note that on 32-bit, the pfn will always reside in the lower 32 bits, and every update changes the upper 32 bits, which hold the counter. So even if we read a value torn by a concurrent update (2x32-bit accesses), the pfn will be valid, and due to the existing barriers, the value cannot look unchanged after a migration. Rename stable_node->kpfn to stable_node->kpfn_seq to catch all users, and add helpers to access the pfn. This has low urgency, as it should be extremely hard to hit in practice. As this was not reproducible and wasn't reported so far, and the change is not trivial to backport, let's not CC stable. Fixes: c8d6553b9580 ("ksm: make KSM page migration possible") Reported-by: Chris Mason Tested-by: Gregory Price (Meta) Reviewed-by: Gregory Price (Meta) Reviewed-by: Xu Xin (vivo) Signed-off-by: David Hildenbrand (Arm) --- mm/ksm.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 66 insertions(+), 23 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index fe7b3b60540db..33c270aae3285 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -152,10 +152,11 @@ struct ksm_scan { * @hlist_dup: linked into the stable_node->hlist with a stable_node chain * @list: linked into migrate_nodes, pending placement in the proper node tree * @hlist: hlist head of rmap_items using this ksm page - * @kpfn: page frame number of this ksm page (perhaps temporarily on wrong nid) + * @kpfn_seq: page frame number of this ksm page (perhaps temporarily on wrong + * nid), with a migration counter in the upper bits * @chain_prune_time: time of the last full garbage collection * @rmap_hlist_len: number of rmap_item entries in hlist or STABLE_NODE_CHAIN - * @nid: NUMA node id of stable tree in which linked (may not match kpfn) + * @nid: NUMA node id of stable tree in which linked (may not match the pfn) */ struct ksm_stable_node { union { @@ -170,7 +171,7 @@ struct ksm_stable_node { }; struct hlist_head hlist; union { - unsigned long kpfn; + u64 kpfn_seq; unsigned long chain_prune_time; }; /* @@ -844,6 +845,39 @@ static struct page *get_mergeable_page(struct ksm_rmap_item *rmap_item) return page; } +/* + * The upper bits of stable_node->kpfn_seq count migrations of the ksm page, so + * ksm_get_folio() can detect a ksm page that migrated back to the same pfn. + */ +#define KSM_KPFN_SEQ_SHIFT (64 - PAGE_SHIFT) +#define KSM_KPFN_PFN_MASK ((1ULL << KSM_KPFN_SEQ_SHIFT) - 1) + +static inline unsigned long ksm_kpfn_pfn(u64 kpfn_seq) +{ + return kpfn_seq & KSM_KPFN_PFN_MASK; +} + +static inline unsigned long stable_node_pfn(const struct ksm_stable_node *node) +{ + return ksm_kpfn_pfn(READ_ONCE(node->kpfn_seq)); +} + +static inline void stable_node_init_pfn(struct ksm_stable_node *node, + unsigned long pfn) +{ + node->kpfn_seq = pfn; +} + +static inline void stable_node_migrate_pfn(struct ksm_stable_node *node, + unsigned long pfn) +{ + u64 kpfn_seq = node->kpfn_seq; + + kpfn_seq &= ~KSM_KPFN_PFN_MASK; + kpfn_seq += 1ULL << KSM_KPFN_SEQ_SHIFT; + WRITE_ONCE(node->kpfn_seq, kpfn_seq | pfn); +} + /* * This helper is used for getting right index into array of tree roots. * When merge_across_nodes knob is set to 1, there are only two rb-trees for @@ -855,6 +889,11 @@ static inline int get_kpfn_nid(unsigned long kpfn) return ksm_merge_across_nodes ? 0 : NUMA(pfn_to_nid(kpfn)); } +static inline bool stable_node_nid_consistent(const struct ksm_stable_node *node) +{ + return get_kpfn_nid(stable_node_pfn(node)) == NUMA(node->nid); +} + static struct ksm_stable_node *alloc_stable_node_chain(struct ksm_stable_node *dup, struct rb_root *root) { @@ -906,7 +945,8 @@ static void remove_node_from_stable_tree(struct ksm_stable_node *stable_node) hlist_for_each_entry(rmap_item, &stable_node->hlist, hlist) { if (rmap_item->hlist.next) { ksm_pages_sharing--; - trace_ksm_remove_rmap_item(stable_node->kpfn, rmap_item, rmap_item->mm); + trace_ksm_remove_rmap_item(stable_node_pfn(stable_node), + rmap_item, rmap_item->mm); } else { ksm_pages_shared--; } @@ -932,7 +972,7 @@ static void remove_node_from_stable_tree(struct ksm_stable_node *stable_node) BUILD_BUG_ON(STABLE_NODE_DUP_HEAD <= &migrate_nodes); BUILD_BUG_ON(STABLE_NODE_DUP_HEAD >= &migrate_nodes + 1); - trace_ksm_remove_ksm_page(stable_node->kpfn); + trace_ksm_remove_ksm_page(stable_node_pfn(stable_node)); if (stable_node->head == &migrate_nodes) list_del(&stable_node->list); else @@ -969,13 +1009,13 @@ static struct folio *ksm_get_folio(struct ksm_stable_node *stable_node, { struct folio *folio; void *expected_mapping; - unsigned long kpfn; + u64 kpfn_seq; expected_mapping = (void *)((unsigned long)stable_node | FOLIO_MAPPING_KSM); again: - kpfn = READ_ONCE(stable_node->kpfn); /* Address dependency. */ - folio = pfn_folio(kpfn); + kpfn_seq = READ_ONCE(stable_node->kpfn_seq); /* Address dependency. */ + folio = pfn_folio(ksm_kpfn_pfn(kpfn_seq)); if (READ_ONCE(folio->mapping) != expected_mapping) goto stale; @@ -1031,10 +1071,12 @@ static struct folio *ksm_get_folio(struct ksm_stable_node *stable_node, * We come here from above when folio->mapping or the swapcache flag * suggests that the node is stale; but it might be under migration. * We need smp_rmb(), matching the smp_wmb() in folio_migrate_ksm(), - * before checking whether node->kpfn has been changed. + * before checking whether node->kpfn_seq has been changed. Due to the + * migration counter, a ksm page that migrated back to the same pfn + * changes node->kpfn_seq as well. */ smp_rmb(); - if (READ_ONCE(stable_node->kpfn) != kpfn) + if (READ_ONCE(stable_node->kpfn_seq) != kpfn_seq) goto again; remove_node_from_stable_tree(stable_node); return NULL; @@ -1984,8 +2026,7 @@ static struct folio *stable_tree_search(struct page *page) goto again; folio_unlock(tree_folio); - if (get_kpfn_nid(stable_node_dup->kpfn) != - NUMA(stable_node_dup->nid)) { + if (!stable_node_nid_consistent(stable_node_dup)) { folio_put(tree_folio); goto replace; } @@ -2149,7 +2190,7 @@ static struct ksm_stable_node *stable_tree_insert(struct folio *kfolio) return NULL; INIT_HLIST_HEAD(&stable_node_dup->hlist); - stable_node_dup->kpfn = kpfn; + stable_node_init_pfn(stable_node_dup, kpfn); stable_node_dup->rmap_hlist_len = 0; DO_NUMA(stable_node_dup->nid = nid); if (!need_chain) { @@ -2316,8 +2357,7 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite stable_node = folio_stable_node(folio); if (stable_node) { if (stable_node->head != &migrate_nodes && - get_kpfn_nid(READ_ONCE(stable_node->kpfn)) != - NUMA(stable_node->nid)) { + !stable_node_nid_consistent(stable_node)) { stable_node_dup_del(stable_node); stable_node->head = &migrate_nodes; list_add(&stable_node->list, stable_node->head); @@ -3416,11 +3456,12 @@ void folio_migrate_ksm(struct folio *newfolio, struct folio *folio) stable_node = folio_stable_node(folio); if (stable_node) { - VM_BUG_ON_FOLIO(stable_node->kpfn != folio_pfn(folio), folio); - WRITE_ONCE(stable_node->kpfn, folio_pfn(newfolio)); + VM_BUG_ON_FOLIO(stable_node_pfn(stable_node) != + folio_pfn(folio), folio); + stable_node_migrate_pfn(stable_node, folio_pfn(newfolio)); /* * newfolio->mapping was set in advance; now we need smp_wmb() - * to make sure that the new stable_node->kpfn is visible + * to make sure that the new stable_node->kpfn_seq is visible * to ksm_get_folio() before it can see that folio->mapping * has gone stale (or that the swapcache flag has been cleared). */ @@ -3445,11 +3486,12 @@ static bool stable_node_dup_remove_range(struct ksm_stable_node *stable_node, unsigned long start_pfn, unsigned long end_pfn) { - if (stable_node->kpfn >= start_pfn && - stable_node->kpfn < end_pfn) { + const unsigned long pfn = stable_node_pfn(stable_node); + + if (pfn >= start_pfn && pfn < end_pfn) { /* * Don't ksm_get_folio, page has already gone: - * which is why we keep kpfn instead of page* + * which is why we keep the pfn instead of page* */ remove_node_from_stable_tree(stable_node); return true; @@ -3505,8 +3547,9 @@ static void ksm_check_stable_tree(unsigned long start_pfn, } } list_for_each_entry_safe(stable_node, next, &migrate_nodes, list) { - if (stable_node->kpfn >= start_pfn && - stable_node->kpfn < end_pfn) + const unsigned long pfn = stable_node_pfn(stable_node); + + if (pfn >= start_pfn && pfn < end_pfn) remove_node_from_stable_tree(stable_node); cond_resched(); } -- 2.56.1