Re: [PATCH] mm/ksm: do not inherit ksm_zero_pages counter or PTE markers on fork
From: Xu Xin (vivo)
Date: Tue Nov 17 2026 - 22:10:53 EST
> On 11/17/26 09:18, xu.xin@xxxxxxxxx wrote:
> > From: "Xu Xin (vivo)" <xu.xin@xxxxxxxxx>
>
> Hi,
>
> thanks for the report.
Hi David,
Thanks for the feedback!
>
> >
> > As documented in Documentation/admin-guide/mm/ksm.rst, ksm_zero_pages
> > accounts only for zero pages actually placed by KSM. However, the
> > current implementation wrongly accounts for forked zero-page PTEs as
> > KSM's contribution, inflating both the child's per-mm counter and the
> > global ksm_zero_pages counter.
>
> 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?
> >
> > Originally, commit e2942062e01d ("ksm: count all zero pages placed by
> > KSM") marked KSM-placed zero-page PTEs with the dirty bit for unmap
> > accounting, but forgot to clear this marker during fork(). Later, to
> > prevent ksm_might_unmap_zero_page() from underflowing the counters on
> > child exit[1], commit 4d6fc29f3634 ("mm/ksm: fix incorrect KSM counter
> > handling in mm_struct during fork") kept the child's inherited per-mm
> > counter and added it to the global counter.
> >
> > Instead of inflating the counters, fix the root cause by resetting the
> > child's mm->ksm_zero_pages to 0 in ksm_fork() and clearing the dirty
> > bit marker via pte_mkclean() when copying a KSM zero PTE in
> > __copy_present_ptes(). With the marker cleared,
> > ksm_might_unmap_zero_page() will not decrement the counters on child
> > exit, MADV_UNMERGEABLE in the child will not unnecessarily unshare
> > inherited zero pages, and the global counter remains untouched.
> >
> > [1] https://lore.kernel.org/all/53a7bf3d-843d-4e19-b0a3-cc6852fe72c1@xxxxxxxxxx/
> >
> > Fixes: 4d6fc29f3634 ("mm/ksm: fix incorrect KSM counter handling in mm_struct during fork")
> > Cc: stable@xxxxxxxxxxxxxxx
> > Signed-off-by: Xu Xin (vivo) <xu.xin@xxxxxxxxx>
> > ---
> > include/linux/ksm.h | 8 +++++---
> > mm/memory.c | 8 ++++++++
> > 2 files changed, 13 insertions(+), 3 deletions(-)
> >
> > diff --git a/include/linux/ksm.h b/include/linux/ksm.h
> > index 93793ca6bcc4..3c6db19d825e 100644
> > --- a/include/linux/ksm.h
> > +++ b/include/linux/ksm.h
> > @@ -56,11 +56,13 @@ static inline void ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
> > {
> > /* Adding mm to ksm is best effort on fork. */
> > if (mm_flags_test(MMF_VM_MERGEABLE, oldmm)) {
> > - long nr_ksm_zero_pages = atomic_long_read(&mm->ksm_zero_pages);
> > -
> > + /*
> > + * Child's inherited zero-page PTEs are unmarked on copy and
> > + * were not placed by KSM in the child, so reset its counter.
> > + */
> > mm->ksm_merging_pages = 0;
> > mm->ksm_rmap_items = 0;
> > - atomic_long_add(nr_ksm_zero_pages, &ksm_zero_pages);
>
> So I think there is definitely a BUG int he existing code when we skip copying
> some VMAs during fork. We should have instead adjusted the counter as we
> duplicate them into the child process.