Re: [PATCH v2 27/31] mm/ksm: don't convert a shared anon folio into a KSM folio

From: Rik van Riel

Date: Fri Dec 04 2026 - 23:03:11 EST


On Fri, 2026-12-04 at 13:50 +0100, David Hildenbrand (Arm) wrote:
> 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.


> 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>

Safe from simultaneous fork because dup_mm() takes
the mmap_lock for write, while ksmd takes the mmap_lock
for read.

Reviewed-by: Rik van Riel <riel@xxxxxxxxxxx>

--
All Rights Reversed.