From: "David Hildenbrand (Arm)" <david@kernel.org>
To: "Xu Xin (vivo)" <xu.xin@linux.dev>
Cc: akpm@linux-foundation.org, chengming.zhou@linux.dev,
donettom@linux.ibm.com, linux-kernel@vger.kernel.org,
linux-mm@kvack.org, stable@vger.kernel.org
Subject: Re: [PATCH v2 1/3] mm/ksm: do not inherit ksm_zero_pages counter or PTE markers on fork
Date: Sat, 21 Nov 2026 13:58:21 +0100 [thread overview]
Message-ID: <60b9b869-e1a0-40fd-a77c-821ac16f6bca@kernel.org> (raw)
In-Reply-To: <20261120074359.41019-1-xu.xin@linux.dev>
On 11/20/26 08: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 might_clear_ksm_zero_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@redhat.com/
>
> Fixes: 4d6fc29f3634 ("mm/ksm: fix incorrect KSM counter handling in mm_struct during fork")
> Cc: stable@vger.kernel.org
> Signed-off-by: Xu Xin (vivo) <xu.xin@linux.dev>
> ---
> Documentation/admin-guide/mm/ksm.rst | 7 ++++++-
> include/linux/ksm.h | 18 +++++++++++++++---
> mm/memory.c | 2 ++
> 3 files changed, 23 insertions(+), 4 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..62fa8ca803ed 100644
> --- a/include/linux/ksm.h
> +++ b/include/linux/ksm.h
> @@ -32,6 +32,15 @@ void __ksm_exit(struct mm_struct *mm);
> */
> #define is_ksm_zero_pte(pte) (is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte))
>
> +/* Clear KSM's marker on zero-page PTEs (e.g., on fork). */
This function would be better called "ksm_fork_pte"
Drop this comment and instead ...
> +static inline pte_t might_clear_ksm_zero_pte(pte_t pte)
> +{
Explain here *why* we are doing this.
> + if (is_ksm_zero_pte(pte))
> + return pte_mkclean(pte);> +
But I'd move the check into the caller and only clear here.
IOW, the following on the caller site:
diff --git a/mm/memory.c b/mm/memory.c
index ad5511b2e949a..ee4545a181598 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -1184,8 +1184,11 @@ copy_present_ptes(struct vm_area_struct *dst_vma, struct
vm_area_struct *src_vma
int err, nr;
page = vm_normal_page(src_vma, addr, pte);
- if (unlikely(!page))
+ if (unlikely(!page)) {
+ if (unlikely(is_ksm_zero_pte(pte)))
+ pte = ksm_fork_pte(pte);
goto copy_pte;
+ }
folio = page_folio(page);
--
Cheers,
David
next prev parent reply other threads:[~2026-11-21 12:58 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 7:42 [PATCH v2 0/3] mm/ksm: fix ksm_zero_pages accounting " Xu Xin (vivo)
2026-11-20 7:43 ` [PATCH v2 1/3] mm/ksm: do not inherit ksm_zero_pages counter or PTE markers " Xu Xin (vivo)
2026-11-21 12:58 ` David Hildenbrand (Arm) [this message]
2026-11-20 7:44 ` [PATCH v2 2/3] selftests/mm: fix false-positive KSM fork test by dropping init_global_file_handles() Xu Xin (vivo)
2026-11-20 7:44 ` [PATCH v2 3/3] selftests/mm: verify ksm_zero_pages is not inherited on fork Xu Xin (vivo)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=60b9b869-e1a0-40fd-a77c-821ac16f6bca@kernel.org \
--to=david@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=chengming.zhou@linux.dev \
--cc=donettom@linux.ibm.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=stable@vger.kernel.org \
--cc=xu.xin@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®