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 - 07:44:32 EST
On 12/4/26 12:06, David Hildenbrand (Arm) wrote:
> 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.
>
I ended up with:
diff --git a/Documentation/admin-guide/mm/ksm.rst b/Documentation/admin-guide/mm/ksm.rst
index 8968ae3f55c0a..079db28bfc5b4 100644
--- a/Documentation/admin-guide/mm/ksm.rst
+++ b/Documentation/admin-guide/mm/ksm.rst
@@ -27,6 +27,9 @@ KSM's merged pages were originally locked into kernel memory, but can now
be swapped out just like other user pages (but sharing is broken when they
are swapped back in: ksmd must rediscover their identity and merge again).
+Note that KSM might not merge identical pages if each of them is already
+shared between processes as a result of fork().
+
Controlling KSM with madvise
============================
As announced, I'll resend this series in a bit.
--
Cheers,
David