mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] mm/ksm: keep soft-dirty and uffd-wp when merging a page
@ 2026-10-18 12:29 Samarth Narang
  2026-10-19  1:02 ` Lance Yang
  0 siblings, 1 reply; 2+ messages in thread
From: Samarth Narang @ 2026-10-18 12:29 UTC (permalink / raw)
  To: Andrew Morton, David Hildenbrand
  Cc: Xu Xin, Chengming Zhou, Peter Xu, Kiryl Shutsemau, Mike Rapoport,
	Lance Yang, linux-mm, linux-kernel, Samarth Narang, stable

replace_page() maps the KSM page with a PTE built from scratch, so the
soft-dirty and uffd bits of the PTE it replaces are lost. Due to this, a
write before the merge no longer shows as soft-dirty, the next write to
a uffd-wp page raises no write-protect fault, and because the page is
mapped with the VMA's normal permissions, on a uffd-rwp VMA the next
access raises no RWP fault.

Copy the soft-dirty and uffd bits from the original PTE to the new PTE,
and restore PAGE_NONE for RWP VMAs, as try_to_map_unused_to_zeropage()
does.

Fixes: 292924b26024 ("userfaultfd: wp: apply _PAGE_UFFD_WP bit")
Fixes: 0f8975ec4db2 ("mm: soft-dirty bits for user memory changes tracking")
Fixes: 763f43865fc4 ("mm: preserve RWP marker across PTE rewrites")
Cc: stable@vger.kernel.org
Assisted-by: LLM
Signed-off-by: Samarth Narang <samarth.colleges@gmail.com>
---
 mm/ksm.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/mm/ksm.c b/mm/ksm.c
index dbfb8f98b189..b0d0555aa1b5 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -34,6 +34,7 @@
 #include <linux/memory.h>
 #include <linux/mmu_notifier.h>
 #include <linux/swap.h>
+#include <linux/userfaultfd_k.h>
 #include <linux/ksm.h>
 #include <linux/hashtable.h>
 #include <linux/freezer.h>
@@ -1463,6 +1464,18 @@ static int replace_page(struct vm_area_struct *vma, struct page *page,
 		dec_mm_counter(mm, MM_ANONPAGES);
 	}
 
+	/*
+	 * The old PTE may carry soft-dirty and uffd-wp state that tracks writes
+	 * to this address. Make sure to preserve these bits in the new PTE.
+	 * On a uffd-rwp VMA, also restore PAGE_NONE.
+	 */
+	if (pte_soft_dirty(orig_pte))
+		newpte = pte_mksoft_dirty(newpte);
+	if (pte_uffd(orig_pte))
+		newpte = pte_mkuffd(newpte);
+	if (pte_uffd(orig_pte) && userfaultfd_rwp(vma))
+		newpte = pte_modify(newpte, PAGE_NONE);
+
 	flush_cache_page(vma, addr, pte_pfn(ptep_get(ptep)));
 	/*
 	 * No need to notify as we are replacing a read only page with another

base-commit: 48fe11c8d0f1e4e59426a3606f20ac1130b6d8dc
-- 
2.53.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] mm/ksm: keep soft-dirty and uffd-wp when merging a page
  2026-10-18 12:29 [PATCH] mm/ksm: keep soft-dirty and uffd-wp when merging a page Samarth Narang
@ 2026-10-19  1:02 ` Lance Yang
  0 siblings, 0 replies; 2+ messages in thread
From: Lance Yang @ 2026-10-19  1:02 UTC (permalink / raw)
  To: samarth.colleges
  Cc: akpm, david, xu.xin, chengming.zhou, peterx, kas, rppt,
	lance.yang, linux-mm, linux-kernel, stable


On Sun, Oct 18, 2026 at 05:29:56AM -0700, Samarth Narang wrote:
>replace_page() maps the KSM page with a PTE built from scratch, so the
>soft-dirty and uffd bits of the PTE it replaces are lost. Due to this, a
>write before the merge no longer shows as soft-dirty, the next write to
>a uffd-wp page raises no write-protect fault, and because the page is
>mapped with the VMA's normal permissions, on a uffd-rwp VMA the next
>access raises no RWP fault.
>
>Copy the soft-dirty and uffd bits from the original PTE to the new PTE,
>and restore PAGE_NONE for RWP VMAs, as try_to_map_unused_to_zeropage()
>does.
>
>Fixes: 292924b26024 ("userfaultfd: wp: apply _PAGE_UFFD_WP bit")
>Fixes: 0f8975ec4db2 ("mm: soft-dirty bits for user memory changes tracking")
>Fixes: 763f43865fc4 ("mm: preserve RWP marker across PTE rewrites")

Shouldn't we use these two instead?

Fixes: 63b2d4174c4a ("userfaultfd: wp: add the writeprotect API to userfaultfd ioctl")
Fixes: 2d427bb01639 ("mm/userfaultfd: add RWP fault delivery and expose UFFDIO_REGISTER_MODE_RWP")

At 292924b26024, WP registration still returns -EINVAL. RWP registration
isn't enabled at 763f43865fc4 either.

Fixes: 0f8975ec4db2 ("mm: soft-dirty bits for user memory changes tracking")

As for soft-dirty, the tag looks fine to me :)

>Cc: stable@vger.kernel.org
>Assisted-by: LLM
>Signed-off-by: Samarth Narang <samarth.colleges@gmail.com>
>---
> mm/ksm.c | 13 +++++++++++++
> 1 file changed, 13 insertions(+)
>
>diff --git a/mm/ksm.c b/mm/ksm.c
>index dbfb8f98b189..b0d0555aa1b5 100644
>--- a/mm/ksm.c
>+++ b/mm/ksm.c
>@@ -34,6 +34,7 @@
> #include <linux/memory.h>
> #include <linux/mmu_notifier.h>
> #include <linux/swap.h>
>+#include <linux/userfaultfd_k.h>
> #include <linux/ksm.h>
> #include <linux/hashtable.h>
> #include <linux/freezer.h>
>@@ -1463,6 +1464,18 @@ static int replace_page(struct vm_area_struct *vma, struct page *page,
> 		dec_mm_counter(mm, MM_ANONPAGES);
> 	}
> 
>+	/*
>+	 * The old PTE may carry soft-dirty and uffd-wp state that tracks writes
>+	 * to this address. Make sure to preserve these bits in the new PTE.
>+	 * On a uffd-rwp VMA, also restore PAGE_NONE.
>+	 */

Nit: the uffd bit also carries RWP state, so "tracks writes" doesn't quite
cover it.

Maybe:

/*
 * Preserve soft-dirty and uffd bits when replacing the page.
 * Restore PAGE_NONE for RWP-protected PTEs.
 */

Otherwise, LGTM, thanks!

Tested-by: Lance Yang <lance.yang@linux.dev>

Cheers, Lance

>+	if (pte_soft_dirty(orig_pte))
>+		newpte = pte_mksoft_dirty(newpte);
>+	if (pte_uffd(orig_pte))
>+		newpte = pte_mkuffd(newpte);
>+	if (pte_uffd(orig_pte) && userfaultfd_rwp(vma))
>+		newpte = pte_modify(newpte, PAGE_NONE);
>+
> 	flush_cache_page(vma, addr, pte_pfn(ptep_get(ptep)));
> 	/*
> 	 * No need to notify as we are replacing a read only page with another
>
>base-commit: 48fe11c8d0f1e4e59426a3606f20ac1130b6d8dc
>-- 
>2.53.0
>
>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-10-19  1:02 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-18 12:29 [PATCH] mm/ksm: keep soft-dirty and uffd-wp when merging a page Samarth Narang
2026-10-19  1:02 ` Lance Yang

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®