Re: [PATCH v2 28/31] mm/ksm: use a migration counter to detect KSM folio migration
From: sashiko-bot
Date: Sat Dec 05 2026 - 08:00:14 EST
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@xxxxxxxxxx>
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@xxxxxxxxxx?part=28