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

From: David Hildenbrand (Arm)

Date: Mon Nov 23 2026 - 11:38:22 EST


On 11/23/26 04:43, Xu Xin (vivo) wrote:
> 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.
>
> 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 KSM
> zero-page marker via ksm_fork_pte() when copying present PTEs 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
> Tested-by: Karl Mehltretter <kmehltretter@xxxxxxxxx>
> Signed-off-by: Xu Xin (vivo) <xu.xin@xxxxxxxxx>
> ---
> Changes in v3:
> - Rename might_clear_ksm_zero_pte() to ksm_fork_pte() and move the
> is_ksm_zero_pte() check into copy_present_ptes() under the !page
> branch, as suggested by David Hildenbrand.
> - Expand the comment above ksm_fork_pte() to explain why the marker is
> cleared on fork.
> - Add Tested-by tag from Karl Mehltretter.
>
> Documentation/admin-guide/mm/ksm.rst | 7 ++++++-
> include/linux/ksm.h | 22 +++++++++++++++++++---
> mm/memory.c | 5 ++++-
> 3 files changed, 29 insertions(+), 5 deletions(-)
>
> diff --git a/Documentation/admin-guide/mm/ksm.rst b/Documentation/admin-guide/mm/ksm.rst
> index c329ca747b8c..8968ae3f55c0 100644
> --- a/Documentation/admin-guide/mm/ksm.rst
> +++ b/Documentation/admin-guide/mm/ksm.rst
> @@ -214,7 +214,12 @@ stable_node_dups
> number of duplicated KSM pages
> ksm_zero_pages
> how many zero pages that are still mapped into processes were mapped by
> - KSM when deduplicating.
> + KSM when deduplicating. Note: on fork(), the child process does not
> + inherit the ``ksm_zero_pages`` counter, and any inherited KSM-placed
> + zero-page mappings are converted into ordinary shared zero-page
> + mappings. Consequently, these mappings are neither accounted for in
> + the child's counter nor unshared when ``MADV_UNMERGEABLE`` is called
> + in the child.
>
> When ``use_zero_pages`` is/was enabled, the sum of ``pages_sharing`` +
> ``ksm_zero_pages`` represents the actual number of pages saved by KSM.
> diff --git a/include/linux/ksm.h b/include/linux/ksm.h
> index 596c04ca7326..f29aec4496d0 100644
> --- a/include/linux/ksm.h
> +++ b/include/linux/ksm.h
> @@ -32,6 +32,17 @@ void __ksm_exit(struct mm_struct *mm);
> */
> #define is_ksm_zero_pte(pte) (is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte))
>
> +/*
> + * Since the inherited mappings on fork are not expected to be accounted for
> + * in the child's ksm_zero_pages counter (reset in ksm_fork()), we must
> + * clear KSM's dirty-bit marker on KSM-placed zero-page PTEs to avoid
> + * underflowing the counters on child exit.
> + */
> +static inline pte_t ksm_fork_pte(pte_t pte)
> +{

Instead of the long function comment, I'd just have here instead:

/*
* Strip KSM information from the PTE for the child to simplify
* ksm_zero_pages accounting, and to indicate each deduplicated zeropage
* only once towards the user.
*/

I can fix that up when applying.

--
Cheers,

David