Re: [PATCH 28/32] mm/ksm: don't convert a shared anon folio into a KSM folio
From: xu . xin
Date: Fri Dec 04 2026 - 01:33:11 EST
> So far we were under the assumption that only VM_MERGEABLE VMAs may
> contain KSM folios, because we unmerge when clearing VM_MERGEABLE, and
> KSM only merges within VM_MERGEABLE VMAs. Various infrastructure (e.g.,
> rmap walks) relies on related properties: for example that a KSM folio
> is always mapped at rmap_item->address, also in child processes.
>
> We ignored one scenario: turning a COW-shared folio into a KSM folio in
> one process will make all processes mapping it suddenly see a KSM
> folio, even if VM_MERGEABLE is not set for them.
>
> Consequently, rmap walks can miss such folio mappings, resulting in
> the KSM folio no longer being reclaimable or migratable.
>
> Worse, if such a process calls mremap() while the kernel is migrating a
> KSM folio, mremap() moves the migration entry that try_to_migrate()
> installed, and remove_migration_ptes() will not find it at its new
> address. Once migration frees the source folio, the moved PTE still
> holds a migration entry that refers to the freed, and possibly already
> reused, folio: the next access through that PTE will hang, or worse.
>
> Further, if ksmd scans the child, rmap walks will not even find the
> parent's VMA: the child's rmap_item references the child's anon_vma,
> which the parent's VMA is not linked into.
>
> Let's fix it all by refusing to convert a folio into a KSM folio if it
> might be shared with another mm. When merging two folios that are not
> KSM folios yet, try both directions: as long as one folio is not shared,
> we can merge.
Hi David, two quick thoughts:
1. Dose refusing to merge two CoW-shared folios might reduce merging
efficiency in some workloads (e.g., QEMU)? I' wondering if we have another
approach instead of blocking it, could we store the original folio's
folio_anon_vma() (and linear_page_index) in rmap_item under the folio lock
and drop the addr check in rmap_walk_ksm()? That would allow rmap_walk_ksm()
to find the parent's VMA even without VM_MERGEABLE or after mremap().
Or does exposing KSM folios to non-VM_MERGEABLE VMAs still break too many
other mm assumptions?
2. If blocking two CoW-shared merges is the only viable option, it's deserved to
explicitly document this limitation in Documentation/admin-guide/mm/ksm.rst.
>
> Shared folios can still be merged into an existing KSM folio, the shared
> zeropage or an identical folio that is not shared. Only identical folios
> that are all shared will no longer be merged, which should be fine for
> the known use cases.
>
> Note that other processes might only hold a swap entry for the folio,
> and map it after swapping it in. As the previous commit only copies KSM
> folios at swap-in into VMAs without VM_MERGEABLE, also refuse to convert
> a folio that is in the swap cache, unless we can free the swap entry.
>
> This has medium urgency, as an unprivileged process can trigger a
> use-after-free if KSM is enabled.
>
> Fixes: 80e148226028 ("ksm: share anon page without allocating")
> Fixes: 5beb49305251 ("mm: change anon_vma linking to fix multi-process server scalability issue")
> Cc: stable@xxxxxxxxxxxxxxx
> Reported-by: Chris Mason <mason@xxxxxxxxxx>
> Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
> ---
> mm/ksm.c | 47 ++++++++++++++++++++++++++++++++++++++++++-----
> 1 file changed, 42 insertions(+), 5 deletions(-)
>