mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "David Hildenbrand (Arm)" <david@kernel.org>
To: xu.xin@linux.dev
Cc: Donet Tom <donettom@linux.ibm.com>,
	Chengming Zhou <chengming.zhou@linux.dev>,
	Andrew Morton <akpm@linux-foundation.org>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org
Subject: Re: [PATCH] mm/ksm: do not inherit ksm_zero_pages counter or PTE markers on fork
Date: Tue, 17 Nov 2026 10:51:02 +0100	[thread overview]
Message-ID: <e971c3f2-e8cf-4eb0-ae25-e7e4d9ea02ee@kernel.org> (raw)
In-Reply-To: <20261117081821.7211-1-xu.xin@linux.dev>

On 11/17/26 09:18, xu.xin@linux.dev wrote:
> From: "Xu Xin (vivo)" <xu.xin@linux.dev>

Hi,

thanks for the report.

> 
> 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?

So won't there be an inconsistency when MADV_UNMERGEABLE?

> 
> 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@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>
> ---
>  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.

> +		atomic_long_set(&mm->ksm_zero_pages, 0);
>  		__ksm_enter(mm);
>  	}
>  }
> diff --git a/mm/memory.c b/mm/memory.c
> index cf075fcec3c5..7dce96af2a51 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -1160,6 +1160,14 @@ static __always_inline void __copy_present_ptes(struct vm_area_struct *dst_vma,
>  		pte = pte_mkclean(pte);
>  	pte = pte_mkold(pte);
>  
> +	/*
> +	 * Clear the dirty-bit marker on KSM-placed zero-page PTEs in the
> +	 * child so inherited mappings are not accounted against the child or
> +	 * unshared by MADV_UNMERGEABLE.  Special PTEs always have nr == 1.
> +	 */
> +	if (is_ksm_zero_pte(pte))
> +		pte = pte_mkclean(pte);
> +
>  	set_ptes(dst_vma->vm_mm, addr, dst_pte, pte, nr);
>  }
>  


-- 
Cheers,

David

      reply	other threads:[~2026-11-17  9:51 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17  8:18 xu.xin
2026-11-17  9:51 ` David Hildenbrand (Arm) [this message]

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=e971c3f2-e8cf-4eb0-ae25-e7e4d9ea02ee@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®