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 4E4AF606A333 for ; Fri, 4 Dec 2026 01:54:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=Vp06BP7n Received: by kanga.kvack.org (Postfix) id 55C1F6B05B1; Thu, 3 Dec 2026 20:54:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 533426B05B2; Thu, 3 Dec 2026 20:54:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3D3986B05B3; Thu, 3 Dec 2026 20:54:44 -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 1A4C56B05B1 for ; Thu, 3 Dec 2026 20:54:44 -0500 (EST) Received: from smtpin25.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 931EF160227 for ; Fri, 4 Dec 2026 01:54:43 +0000 (UTC) X-FDA: 85504631646.25.A8B7037 Received: from mta1.migadu.com (out-219.mta1.migadu.com [95.215.58.219]) by imf28.hostedemail.com (Postfix) with ESMTP id 71BADC0002 for ; Fri, 4 Dec 2026 01:54:41 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Vp06BP7n; spf=pass (imf28.hostedemail.com: domain of xu.xin@linux.dev designates 95.215.58.219 as permitted sender) smtp.mailfrom=xu.xin@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796349281; b=mINYWy3H3cXPwLzDz/lh5i7+ixjnAtBLRea5wMo2ZhJcMmQPhlMZ4tAYHD9/8Gr/mCsFJ4 Ac2YcLxsVPc4c+XnlOJxOk3UWDemTTwulsMn5Teuk3BDrueWn2flQMF1GNrVAinfIXav6x Wqt1qa3OI+IMsgeP2LKGYOQClPR3Ito= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796349281; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=9PjY/5RMyrNKZQRguJMDb31EBn1Sa+deXTkstOHUiYE=; b=nf2a6LZJpMz1SUArsY6FoAX+D+uYztL7B/w28V2cm0/Dia6f809v/xlLaEOCAA3YqQx513 JQRYHh2nDy6PoXdRsoKEeXKr4H6cWyx+NHrSwtBQZA78VYtpT9wb2BkbmmX+KUs9nK61SB xpwT0HgN3M4sOg+Q7vmS8uaSdcISnkk= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Vp06BP7n; spf=pass (imf28.hostedemail.com: domain of xu.xin@linux.dev designates 95.215.58.219 as permitted sender) smtp.mailfrom=xu.xin@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Envelope-To: linux-mm@kvack.org DKIM-Signature: a=rsa-sha256; bh=zduADO8axO4fg97fXISpr57BUvFk8ZtWBfPvXONi9IE=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796349279; v=1; x=1796954079; b=Vp06BP7nushDVRcKolx4oeZGu0AO0FeQJG7RGgOJQFVZEcFxjnxWkCmY5zf6itR4OkYxCLmV s+78h9vRsDKweSwW2R70EaStpqSKNVW79kG7e3vf4828jlP3N14EJicNphLnfThI6WM61ggtOp8 yL+G4ok1bHGhVyyjYA0QBID0= X-Envelope-To: linux-mm@kvack.org Received: by mta10.migadu.com with ESMTPS id ccc2c8f51ef38293; Fri, 04 Dec 2026 01:54:38 +0000 X-Mizu-Trace-ID: ccc2c8f51ef38293 X-Migadu-Flow: FLOW_OUT From: "Xu Xin (vivo)" To: david@kernel.org Cc: Felix.Kuehling@amd.com, James.Bottomley@HansenPartnership.com, airlied@redhat.com, akpm@linux-foundation.org, alex.sierra@amd.com, alex@shazbot.org, apopple@nvidia.com, ast@kernel.org, axelrasmussen@google.com, balbirs@nvidia.com, baohua@kernel.org, baolin.wang@linux.alibaba.com, baoquan.he@linux.dev, bigeasy@linutronix.de, byungchul@sk.com, chengming.zhou@linux.dev, cl@gentwo.org, clrkwllms@kernel.org, dave.jiang@intel.com, dave@stgolabs.net, dev.jain@arm.com, djbw@kernel.org, gorcunov@gmail.com, gourry@gourry.net, hagen@jauu.net, hao.li@linux.dev, harry@kernel.org, hughd@google.com, jack@suse.cz, jannh@google.com, jgg@ziepe.ca, jhubbard@nvidia.com, joshua.hahnjy@gmail.com, kas@kernel.org, kasong@tencent.com, kraxel@redhat.com, lance.yang@linux.dev, liam@infradead.org, linmiaohe@huawei.com, linux-cxl@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-rt-devel@lists.linux.dev, ljs@kernel.org, manfred@colorfullife.com, mason@kernel.org, matthew.brost@intel.com, mgorman@suse.de, mhocko@suse.com, muchun.song@linux.dev, nico.pache@linux.dev, osalvador@suse.de, peterx@redhat.com, qi.zheng@linux.dev, rakie.kim@sk.com, riel@surriel.com, rientjes@google.com, roman.gushchin@linux.dev, rostedt@goodmis.org, rppt@kernel.org, ryan.roberts@arm.com, shakeel.butt@linux.dev, shivankg@amd.com, shy828301@gmail.com, sj@kernel.org, song@kernel.org, stevensd@chromium.org, surenb@google.com, usama.arif@linux.dev, vbabka@kernel.org, vivek.kasireddy@intel.com, wangkefeng.wang@huawei.com, weixugc@google.com, willy@infradead.org, xu.xin@linux.dev, ying.huang@linux.alibaba.com, yuanchu@google.com, ziy@nvidia.com, zokeefe@google.com Subject: Re: [PATCH 29/32] mm/ksm: use a migration counter to detect KSM folio migration Date: Fri, 4 Dec 2026 09:54:09 +0800 Message-Id: <20261204015409.492628-1-xu.xin@linux.dev> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261129-fixes_core_3-v1-29-f66094116801@kernel.org> References: <20261129-fixes_core_3-v1-29-f66094116801@kernel.org> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 71BADC0002 X-Stat-Signature: uns3ime7exsbecbh843mter1uxejrxz1 X-Spam-Status: No, score=-19.0 required=3.0 tests=BAYES_00,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-HE-Tag: 1796349281-775751 X-HE-Meta: U2FsdGVkX1/MnX/MygO83yhXQ61XmInOcgoqGB7OJpSVYFiRGtkEsGvfunyAV5L1O8vbHRbm1LDxqotI5bG+Ilftrjlpkmk9nI0Xztx2UA6moD5XPSAZPF2lRoLh2JK7JG9GQ3keFXrzih857cLSsGfCOsSFdRB+T9K3tkgOCnwaQrcmCfYheghbj5G19flKStlNQgFw6k34/tNWW0yTqY2oBXRmpQzmiImAeB/5RC2eVmF3ZHs5LsCGQim7rKjH0k3ahG31We/8AtLmCv8EqP1KhFfu49kB9Fj4neA+g4cf6oTwt+YI6w3k5RAoTJDwOCbYA9fLYEjwj91foAO6nZbUZFVm9kpct2Q7lUilohfeMoFpx3VfnY5aBphrgmNtWuMJGZtW/Jlq61EFXo0IOW68cv/Mbri2+KjHrHD7lkJW47sJPoGhoi1zcjAwwyJhq/EBCckwk0187dI+4+JWaQqZZgf3jMUJhRADToZutsBW41xIsdiHU4N3b7XRHrCwSvrTC5rE1arexyWgLixbXzc2kfBfcTJTCHZxU94CMYtBeM6fbr2jnI24gq30OE8an4+hGFM5SAo+l7N+FkGgrHB6Q8LMBGxuGZngPSI8swb4NFq+Fgmmxf6rnhp5OXDLU4HVSuvLGaR3qclbUV7vtxPyr++gGOKeuhQkKpse3SjZJ1F6nOBQRY973wxqPGIqJJs/V52KP4K0jhIgvUBsCYxEpAh2DzJ+sLJmZxP4LznRDezZReyfgfuSsV6qW+NUTSPkn3HDnQFbnAsDUCTdFy046VmMB5yEPTp3wnUgWW6+N7rk7SN83Y4xVOmrXVHnyq8+i9V/X9HWr0bmK2CGdU6vn3Rszqeotim0POmNIda/HKgABAtZ0pY5sXO/CQNMxcofluIXApfkf9XZPKBD84FRFr3woDeW8u2keIC1Q7waPoWUUx6K+84TtzJVwi5mFybouZGYaAbWkhpAIlJ z6doIb1O Zw2YGQ/S/ioUa6upRezVAFB4bTCe14gFkfpytaMwTKsoMt8GNdiViwuo/MZO8+b+zWVSo2fK4ynBj/HTq7SjdnfRF/MOmjIqyWI6ivsMJXKWlip6d2/5NjACRVv5YG6LXjvG9fHPI7m+H7m2LvW3pWZQImRn9K/mfBE85Vn7YPH9ZEivvbQ/8oeUKiYCfLhvYANCWinQHCiaiwm1lhSBeYJBZHoOcHFt/YUNr0Y6b3jg2kSeq5W+Xqt37I5aCfcCApdFnv7yQCg/oQYLAfqwbZ+tSFAYNN+rAdJz3sRdWL59J6LpXeVHAzUYs/qMtIBXj7xjI6azDeygWkbCpj8RLkiP0LHdninRYCqXq+7R+ip3wlyi4cbM9KCOU7PXPlXjj79zdt/4I+Qm4Heifoah48JGF4XwgB5MKVE//G09xiPBZ0tVzzWoSRksBmADNeo/734UFLxH9LWWdbnU2ZYzFrN7ztDWVzW0YbDvgFhNJAW56P1Pyv374ftKDDSh4TVP6Okw6PjNxst1dKWWR+bNiS+zNo8CVxn7VLuqb 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. Yes, It's very unlikely to happen and I have never met it at least. > > Fixes: c8d6553b9580 ("ksm: make KSM page migration possible") > Reported-by: Chris Mason > Signed-off-by: David Hildenbrand (Arm) > --- > mm/ksm.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++----------------- > 1 file changed, 66 insertions(+), 23 deletions(-) > Reviewed-by: Xu Xin (vivo) > 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 >