Re: [RFC PATCH 0/2] SKSM: Synchronous Kernel Samepage Merging

From: Mathieu Desnoyers
Date: Fri Feb 28 2025 - 16:49:57 EST


On 2025-02-28 16:45, David Hildenbrand wrote:
On 28.02.25 22:38, Mathieu Desnoyers wrote:
On 2025-02-28 10:10, David Hildenbrand wrote:
[...]
For example, QEMU will mark all guest memory is mergeable using MADV, to
limit the deduplicaton to guest RAM only.


On a related note, I think the madvise(2) documentation is inaccurate.

It states:

         MADV_MERGEABLE (since Linux 2.6.32)
                Enable  Kernel Samepage Merging (KSM) for the pages in the range
                specified by addr and length. [...]

AFAIU, based on code review of ksm_madvise(), this is not strictly true.

The KSM implementation enables KSM for pages in the entire vma containing the range.
So if it so happens that two mmap areas with identical protection flags are merged,
both will be considered mergeable by KSM as soon as at least one page from any of
those areas is made mergeable.

I *think* it does what is documented. In madvise_vma_behavior(), ksm_madvise() will update "new_flags".

Then we call madvise_update_vma() to split the VMA if required and set new_flags only on the split VMA. The handling is similar to other MADV operations that end up modifying vm_flags.

If I am missing something and this is indeed broken, we should definitely write a selftest for it and fix it.


You are correct, I missed that part. Thanks for the clarification!

Mathieu



--
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com