Re: [PATCH 28/32] mm/ksm: don't convert a shared anon folio into a KSM folio
From: David Hildenbrand (Arm)
Date: Fri Dec 04 2026 - 06:07:15 EST
On 12/4/26 07:32, xu.xin@xxxxxxxxx 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. 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:
Hi,
thanks for taking a look!
>
> 1. Dose refusing to merge two CoW-shared folios might reduce merging
> efficiency in some workloads (e.g., QEMU)?
At least not for QEMU. QEMU doesn't really share its guest memory with any
children, and fork() is pretty rare in QEMU nowadays.
QEMU sets QEMU_MADV_DONTFORK, unless for qtest (which KSM doesn't really apply to).
> 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().
No, having KSM folios in processes without VM_MERGABLE is the real violation,
and we shouldn't hack around that.
In this fix I keep it working as long as you don't have two pairs of COW-shared
pages. If something is already COW-shared, it's effectively deduplicated already
using a different mechanism.
So really only what's disallowed is converting a COW-shared page into a KSM page.
>
> 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.
I think this was just one (of multiple, I'm afraid) major oversights by the
authors of KVM.
Let me see if I can come up with something for the documentation that doesn't
sound too specialized.
--
Cheers,
David