From: sashiko-bot@kernel.org
To: "David Hildenbrand (Arm)" <david@kernel.org>,
"Hugh Dickins" <hughd@google.com>,
"Baolin Wang" <baolin.wang@linux.alibaba.com>,
"Andrew Morton" <akpm@linux-foundation.org>,
"Mike Rapoport" <rppt@kernel.org>, "Peter Xu" <peterx@redhat.com>,
"Michal Hocko" <mhocko@suse.com>,
"Kiryl Shutsemau" <kas@kernel.org>,
"Manfred Spraul" <manfred@colorfullife.com>,
"Davidlohr Bueso" <dave@stgolabs.net>,
"Cyrill Gorcunov" <gorcunov@gmail.com>, "Zi Yan" <ziy@nvidia.com>,
"Liam R. Howlett" <liam@infradead.org>,
"Nico Pache" <nico.pache@linux.dev>,
"Ryan Roberts" <ryan.roberts@arm.com>,
"Dev Jain" <dev.jain@arm.com>, "Barry Song" <baohua@kernel.org>,
"Lance Yang" <lance.yang@linux.dev>,
"Usama Arif" <usama.arif@linux.dev>,
"Lorenzo Stoakes" <ljs@kernel.org>,
"Rik van Riel" <riel@surriel.com>,
"Vlastimil Babka" <vbabka@kernel.org>,
"Harry Yoo" <harry@kernel.org>, "Jann Horn" <jannh@google.com>,
"Yang Shi" <shy828301@gmail.com>,
"Zach O'Keefe" <zokeefe@google.com>, "Song Liu" <song@kernel.org>,
"David Stevens" <stevensd@chromium.org>,
"Kefeng Wang" <wangkefeng.wang@huawei.com>,
"Alex Williamson" <alex@shazbot.org>,
"Suren Baghdasaryan" <surenb@google.com>,
"Dave Jiang" <dave.jiang@intel.com>,
"Matthew Wilcox" <willy@infradead.org>,
"Miaohe Lin" <linmiaohe@huawei.com>,
"David Rientjes" <rientjes@google.com>,
"Shakeel Butt" <shakeel.butt@linux.dev>,
"Jan Kara" <jack@suse.cz>, "Mel Gorman" <mgorman@suse.de>,
"Jason Gunthorpe" <jgg@ziepe.ca>,
"John Hubbard" <jhubbard@nvidia.com>,
"Gerd Hoffmann" <kraxel@redhat.com>,
"Vivek Kasireddy" <vivek.kasireddy@intel.com>,
"Dave Airlie" <airlied@redhat.com>,
"Matthew Brost" <matthew.brost@intel.com>,
"Joshua Hahn" <joshua.hahnjy@gmail.com>,
"Byungchul Park" <byungchul@sk.com>,
"Gregory Price" <gourry@gourry.net>,
"Ying Huang" <ying.huang@linux.alibaba.com>,
"Alistair Popple" <apopple@nvidia.com>,
"Hagen Paul Pfeifer" <hagen@jauu.net>,
"James Bottomley" <James.Bottomley@HansenPartnership.com>,
"Felix Kuehling" <Felix.Kuehling@amd.com>,
"Alex Sierra" <alex.sierra@amd.com>,
"Balbir Singh" <balbirs@nvidia.com>,
"Kairui Song" <kasong@tencent.com>,
"Qi Zheng" <qi.zheng@linux.dev>,
"Axel Rasmussen" <axelrasmussen@google.com>,
"Yuanchu Xie" <yuanchu@google.com>, "Wei Xu" <weixugc@google.com>,
"Baoquan He" <baoquan.he@linux.dev>,
"Oscar Salvador" <osalvador@suse.de>,
"Muchun Song" <muchun.song@linux.dev>,
"Dan Williams" <djbw@kernel.org>, "Rakie Kim" <rakie.kim@sk.com>,
"Hao Li" <hao.li@linux.dev>, "Christoph Lameter" <cl@gentwo.org>,
"Roman Gushchin" <roman.gushchin@linux.dev>,
"Sebastian Andrzej Siewior" <bigeasy@linutronix.de>,
"Clark Williams" <clrkwllms@kernel.org>,
"Steven Rostedt" <rostedt@goodmis.org>,
"Alexei Starovoitov" <ast@kernel.org>,
"Xu Xin" <xu.xin@linux.dev>,
"Chengming Zhou" <chengming.zhou@linux.dev>,
"Shivank Garg" <shivankg@amd.com>
Cc: linux-mm@kvack.org, linux-cxl@vger.kernel.org,
linux-rt-devel@lists.linux.dev, "SJ Park" <sj@kernel.org>,
"Chris Mason" <mason@kernel.org>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 28/31] mm/ksm: use a migration counter to detect KSM folio migration
Date: Sat, 05 Dec 2026 13:00:02 +0000 [thread overview]
Message-ID: <sashiko-outbox-251541@kernel.org> (raw)
In-Reply-To: <20261204-fixes_core_3-v2-28-9aad65727632@kernel.org>
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] mm/ksm: torn 64-bit kpfn_seq in ksm_get_folio() breaks 32-bit
SMP safety
--
commit 121b2ee344a6ed62766dc4e36462c4e689247791
Author: David Hildenbrand (Arm) <david@kernel.org>
mm/ksm: use a migration counter to detect KSM folio migration
This commit converts stable_node->kpfn into a u64 kpfn_seq field that
packs a migration counter into the upper bits so ksm_get_folio() can
detect a KSM folio that migrated away and back to the same PFN.
> diff --git a/mm/ksm.c b/mm/ksm.c
> index fe7b3b60540db..33c270aae3285 100644
> --- a/mm/ksm.c
> +++ b/mm/ksm.c
[ ... ]
> @@ -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;
[Severity: Medium]
Can torn 64-bit accesses to stable_node->kpfn_seq on 32-bit SMP still
cause ksm_get_folio() to free a live stable_node?
CONFIG_KSM only depends on MMU, so it can be enabled on 32-bit SMP
kernels where 64-bit READ_ONCE() and WRITE_ONCE() split into two 32-bit
accesses (lo for the PFN, hi for the migration counter). With a
low-word-first access order, two migrations back to the same PFN can
produce the same torn value on both reads:
CPU0 (ksm_get_folio) CPU1 (folio_migrate_ksm)
/* kpfn_seq = (hi=0, lo=A) */
READ_ONCE(kpfn_seq): lo = A
migrate A -> B:
WRITE_ONCE: lo = B, hi = 1
smp_wmb()
folio_set_stable_node(A, NULL)
READ_ONCE(kpfn_seq): hi = 1
/* torn kpfn_seq = (1, A) */
folio->mapping != expected
goto stale; smp_rmb()
migrate B -> A:
folio(A)->mapping = expected
WRITE_ONCE: lo = A /* (1, A) */
READ_ONCE(kpfn_seq) -> (1, A)
/* matches torn kpfn_seq */
remove_node_from_stable_tree()
free_stable_node()
WRITE_ONCE: hi = 2 <-- write-after-free
This leaves the live mapped folio at A pointing to the freed stable_node,
causing a write-after-free when CPU1 finishes the store and a
use-after-free in callers such as rmap_walk_ksm().
On weakly-ordered 32-bit SMP, ksm_kpfn_pfn() also discards the upper
32-bit word when computing the folio address, so the address dependency
orders only lo before READ_ONCE(folio->mapping); a delayed load of hi
can observe hi = 2 from the second migration and match the stale re-read.
Using atomic64_t (atomic64_read() / atomic64_set()) for kpfn_seq, or
restricting CONFIG_KSM to depend on 64BIT, would avoid torn 64-bit
accesses on 32-bit with no overhead on 64-bit.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204-fixes_core_3-v2-0-9aad65727632@kernel.org?part=28
next prev parent reply other threads:[~2026-12-05 13:00 UTC|newest]
Thread overview: 57+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-04 12:49 [PATCH v2 00/31] mm: various (urgent?) fixes, part 3 David Hildenbrand (Arm)
2026-12-04 12:49 ` [PATCH v2 01/31] mm/shmem: count only the swapped pages in the requested range David Hildenbrand (Arm)
2026-12-04 12:49 ` [PATCH v2 02/31] mm/shmem: don't skip parts of a split swap entry when punching holes David Hildenbrand (Arm)
2026-12-04 12:49 ` [PATCH v2 03/31] mm/shmem: reject userfaultfd fills during hole punch David Hildenbrand (Arm)
2026-12-04 12:49 ` [PATCH v2 04/31] mm/shmem: fix commit limit bypass with very large shmem sizes David Hildenbrand (Arm)
2026-12-04 12:49 ` [PATCH v2 05/31] mm/shmem: don't pin inodes with eviction held off in the huge shrinker David Hildenbrand (Arm)
2026-12-04 12:49 ` [PATCH v2 06/31] mm/shmem: seal the size of SysV and shared anon files David Hildenbrand (Arm)
2026-12-04 12:49 ` [PATCH v2 07/31] mm/khugepaged: check the VMA before walking page tables when collapsing David Hildenbrand (Arm)
2026-12-04 12:49 ` [PATCH v2 08/31] mm/khugepaged: flush file after unlocking folios in collapse_file() David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 09/31] mm/khugepaged: check for uffd outside the i_pages lock David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 10/31] mm/khugepaged: don't wait for userfaultfd David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 11/31] mm/khugepaged: store new folio before adding it to LRU David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 12/31] mm/khugepaged: check PMD support with the folios locked David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 13/31] mm/khugepaged: stop MADV_COLLAPSE on a fatal signal David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 14/31] mm/huge_memory: split huge zero PMDs on unshare faults David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 15/31] mm/huge_memory: don't map huge PFN entries if the arch restricts PFNs David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 16/31] mm/memory: don't clear a PUD leaf as bad in a zap David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 17/31] mm/memory: drop the swap device ref before do_wp_page() David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 18/31] mm: consistently check MMF_UNSTABLE when populating in CoW mappings David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 19/31] mm/gup: check for fatal signals in memfd_pin_folios() David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 20/31] mm/migrate_device: don't collect secretmem pages David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 21/31] mm/migrate_device: mark folios leaving device memory dirty David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 22/31] mm: taint device sections before setting subsections valid David Hildenbrand (Arm)
2026-12-04 17:53 ` Rik van Riel
2026-12-04 12:50 ` [PATCH v2 23/31] mm/mempolicy: check access under exec_update_lock David Hildenbrand (Arm)
2026-12-05 3:02 ` Rik van Riel
2026-12-05 12:59 ` sashiko-bot
2026-12-05 13:25 ` Oleg Nesterov
2026-12-05 16:26 ` David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 24/31] mm/mempolicy: reject an empty set of allowed nodes David Hildenbrand (Arm)
2026-12-05 3:08 ` Rik van Riel
2026-12-04 12:50 ` [PATCH v2 25/31] mm/ksm: look up the folio again in cmp_and_merge_page() David Hildenbrand (Arm)
2026-12-05 3:14 ` Rik van Riel
2026-12-05 12:59 ` sashiko-bot
2026-12-05 16:31 ` David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 26/31] mm/ksm: copy KSM folios at swap-in without VM_MERGEABLE David Hildenbrand (Arm)
2026-12-04 13:00 ` Lorenzo Stoakes (ARM)
2026-12-04 13:01 ` David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 27/31] mm/ksm: don't convert a shared anon folio into a KSM folio David Hildenbrand (Arm)
2026-12-05 4:00 ` Rik van Riel
2026-12-05 12:59 ` sashiko-bot
2026-12-05 16:48 ` David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 28/31] mm/ksm: use a migration counter to detect KSM folio migration David Hildenbrand (Arm)
2026-12-05 13:00 ` sashiko-bot [this message]
2026-12-05 17:24 ` David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 29/31] mm/migrate: fix KSM oddity by checking expected refcount without mappings David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 30/31] mm/migrate_device: don't collect KSM folios David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 31/31] mm/migrate: use consistent folio locking logic David Hildenbrand (Arm)
2026-12-05 4:14 ` Rik van Riel
2026-12-05 17:27 ` (subset) [PATCH v2 00/31] mm: various (urgent?) fixes, part 3 David Hildenbrand
2026-12-05 17:29 ` David Hildenbrand
2026-12-05 17:29 ` David Hildenbrand
2026-12-05 17:31 ` David Hildenbrand
2026-12-05 17:32 ` David Hildenbrand
2026-12-05 17:34 ` David Hildenbrand
2026-12-05 17:34 ` David Hildenbrand
2026-12-05 17:38 ` David Hildenbrand
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-251541@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=Felix.Kuehling@amd.com \
--cc=James.Bottomley@HansenPartnership.com \
--cc=airlied@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=alex.sierra@amd.com \
--cc=alex@shazbot.org \
--cc=apopple@nvidia.com \
--cc=ast@kernel.org \
--cc=axelrasmussen@google.com \
--cc=balbirs@nvidia.com \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=baoquan.he@linux.dev \
--cc=bigeasy@linutronix.de \
--cc=byungchul@sk.com \
--cc=chengming.zhou@linux.dev \
--cc=cl@gentwo.org \
--cc=clrkwllms@kernel.org \
--cc=dave.jiang@intel.com \
--cc=dave@stgolabs.net \
--cc=david@kernel.org \
--cc=dev.jain@arm.com \
--cc=djbw@kernel.org \
--cc=gorcunov@gmail.com \
--cc=gourry@gourry.net \
--cc=hagen@jauu.net \
--cc=hao.li@linux.dev \
--cc=harry@kernel.org \
--cc=hughd@google.com \
--cc=jack@suse.cz \
--cc=jannh@google.com \
--cc=jgg@ziepe.ca \
--cc=jhubbard@nvidia.com \
--cc=joshua.hahnjy@gmail.com \
--cc=kas@kernel.org \
--cc=kasong@tencent.com \
--cc=kraxel@redhat.com \
--cc=lance.yang@linux.dev \
--cc=liam@infradead.org \
--cc=linmiaohe@huawei.com \
--cc=linux-cxl@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-rt-devel@lists.linux.dev \
--cc=ljs@kernel.org \
--cc=manfred@colorfullife.com \
--cc=mason@kernel.org \
--cc=matthew.brost@intel.com \
--cc=mgorman@suse.de \
--cc=mhocko@suse.com \
--cc=muchun.song@linux.dev \
--cc=nico.pache@linux.dev \
--cc=osalvador@suse.de \
--cc=peterx@redhat.com \
--cc=qi.zheng@linux.dev \
--cc=rakie.kim@sk.com \
--cc=riel@surriel.com \
--cc=rientjes@google.com \
--cc=roman.gushchin@linux.dev \
--cc=rostedt@goodmis.org \
--cc=rppt@kernel.org \
--cc=ryan.roberts@arm.com \
--cc=sashiko-reviews@lists.linux.dev \
--cc=shakeel.butt@linux.dev \
--cc=shivankg@amd.com \
--cc=shy828301@gmail.com \
--cc=sj@kernel.org \
--cc=song@kernel.org \
--cc=stevensd@chromium.org \
--cc=surenb@google.com \
--cc=usama.arif@linux.dev \
--cc=vbabka@kernel.org \
--cc=vivek.kasireddy@intel.com \
--cc=wangkefeng.wang@huawei.com \
--cc=weixugc@google.com \
--cc=willy@infradead.org \
--cc=xu.xin@linux.dev \
--cc=ying.huang@linux.alibaba.com \
--cc=yuanchu@google.com \
--cc=ziy@nvidia.com \
--cc=zokeefe@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.