Re: [PATCH] mm/ksm: do not inherit ksm_zero_pages counter or PTE markers on fork

From: David Hildenbrand (Arm)

Date: Thu Nov 19 2026 - 12:18:45 EST


>>
>> But ... they were placed by KSM?
>>
>> Just like any other KSM folios deduplicated before fork and then shared with the
>> child?
>
> I see your point, but in practice, people evaluating use_zero_pages=1 often
> look at ksm_zero_pages to gauge how much memory KSM actually saved. Since
> forked pages already share the same physical frame via COW even without KSM,
> counting them across many forked children can give a somewhat misleading
> impression of KSM's actual savings.
>
> Also, ksm_merging_pages right next to it is already reset to 0 on fork (even
> when regular KSM folios are shared with the child), so resetting ksm_zero_pages
> keeps the two counters consistent.
>
>>
>> So won't there be an inconsistency when MADV_UNMERGEABLE?
>>
>
> As for MADV_UNMERGEABLE, leaving the child's mapping as a normal read-only shared
> zero page is safe and avoids extra allocations. Plus it naturally avoids the
> skipped-VMA (MADV_DONTFORK) accounting bug you spotted, and does so
> without extra per-PTE overhead.
>
> What do you think?

We should then definitely document the behavioral change, and how ksm_zero_pages
interacts with fork.

For clearing the dirty bit we should have a dedicated helper, such the handling
is hidden like in is_ksm_zero_pte().


So clean up that code (reducing the code comment drastically) and properly
document all that for ksm_zero_pages.

--
Cheers,

David