mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/3] mm: fix soft-dirty false positives in mprotect() of large folios
@ 2026-11-25 13:32 Shivank Garg
  2026-11-25 13:32 ` [PATCH 1/3] mm: move the soft-dirty fallbacks up in pgtable.h Shivank Garg
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Shivank Garg @ 2026-11-25 13:32 UTC (permalink / raw)
  To: Andrew Morton, David Hildenbrand, Lorenzo Stoakes,
	Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
	Suren Baghdasaryan, Michal Hocko, Dev Jain, Zi Yan, Baolin Wang,
	Nico Pache, Ryan Roberts, Barry Song, Lance Yang, Usama Arif,
	Kiryl Shutsemau, Peter Xu, Shuah Khan, Michał Mirosław,
	Andrei Vagin, Paul Gofman, Bharata B Rao, Daniel Wade, Yin Tirui,
	Muhammad Usama Anjum
  Cc: linux-mm, linux-kselftest, linux-kernel, Shivank Garg

mprotect() handles the PTEs of a large folio as one batch, and marks the
whole batch dirty with pte_mkdirty() if any of its PTEs is dirty.  On
x86, powerpc, riscv and s390, pte_mkdirty() also sets the soft-dirty
bit, so after clear_refs, mprotect() marks unwritten pages soft-dirty.
With 64K mTHP on x86-64, all 240 unwritten pages of a 1MiB region are
reported soft-dirty; with my fix, none are.

I fix this issue similar to how commit 055267feaecc ("mm/migrate:
preserve exact soft-dirty state") fixed remove_migration_pte() and add
a selftest.
On Zen5, an mprotect() loop over 256MiB is 2-4% slower with 16K to 1M
mTHP and unchanged with 4K pages.  Patch 3 adds a selftest.

I first hit this while stress testing [1] and [2] with LLM-generated
test cases: both build swap or migration entries from
get_and_clear_full_ptes(), and their soft-dirty tests failed on x86.
Looking into it, I found that mprotect() batching, already in mainline,
has the same pattern and reproduces the problem.

Two other places have the same pattern, but I left them out since
over-indicating soft-dirty is sometimes considered fine:

 - get_and_clear_full_ptes(): when reclaim of a lazyfree large folio
   fails because the folio was written to, try_to_unmap_one() restores
   the PTEs from the merged PTE, and all of them become soft-dirty.
 - remove_migration_pmd(): pmd_mkdirty() marks a migrated PMD THP
   soft-dirty.

Are they worth fixing?

[1] https://lore.kernel.org/20261123053543.295467-1-dev.jain@arm.com
[2] https://lore.kernel.org/20260813-migrate-rmap-batch-v2-0-3c5424c555c7@amd.com

---
Shivank Garg (3):
      mm: move the soft-dirty fallbacks up in pgtable.h
      mm: don't let modify_prot_start_ptes() set soft-dirty on clean PTEs
      selftests/mm: soft-dirty: check that mprotect() keeps PTE-mapped THPs clean

 include/linux/pgtable.h                 | 188 ++++++++++++++++----------------
 tools/testing/selftests/mm/soft-dirty.c |  48 +++++++-
 2 files changed, 143 insertions(+), 93 deletions(-)
---
base-commit: 98f6b507621822477db7ec7cef8519de0dd243c4
base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/mm/core.git for-next
change-id: 20261124-b4-mm-soft-dirty-fixes-68c578e1dfbd

Best regards,
--  
Shivank Garg <shivankg@amd.com>


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

* [PATCH 1/3] mm: move the soft-dirty fallbacks up in pgtable.h
  2026-11-25 13:32 [PATCH 0/3] mm: fix soft-dirty false positives in mprotect() of large folios Shivank Garg
@ 2026-11-25 13:32 ` Shivank Garg
  2026-11-25 13:32 ` [PATCH 2/3] mm: don't let modify_prot_start_ptes() set soft-dirty on clean PTEs Shivank Garg
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Shivank Garg @ 2026-11-25 13:32 UTC (permalink / raw)
  To: Andrew Morton, David Hildenbrand, Lorenzo Stoakes,
	Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
	Suren Baghdasaryan, Michal Hocko, Dev Jain, Zi Yan, Baolin Wang,
	Nico Pache, Ryan Roberts, Barry Song, Lance Yang, Usama Arif,
	Kiryl Shutsemau, Peter Xu, Shuah Khan, Michał Mirosław,
	Andrei Vagin, Paul Gofman, Bharata B Rao, Daniel Wade, Yin Tirui,
	Muhammad Usama Anjum
  Cc: linux-mm, linux-kselftest, linux-kernel, Shivank Garg

The fallbacks for pte_soft_dirty() and friends on architectures without
CONFIG_HAVE_ARCH_SOFT_DIRTY are defined near the end of pgtable.h, after
the batched PTE helper modify_prot_start_ptes().

Move them, together with pgtable_supports_soft_dirty(), up next to the
other generic PTE and PMD accessors, so that the next patch can use them
in that helper.

No functional change.

Signed-off-by: Shivank Garg <shivankg@amd.com>
---
 include/linux/pgtable.h | 182 ++++++++++++++++++++++++------------------------
 1 file changed, 91 insertions(+), 91 deletions(-)

diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h
index 66d369870a74..b1db7aec5362 100644
--- a/include/linux/pgtable.h
+++ b/include/linux/pgtable.h
@@ -194,6 +194,97 @@ static inline int pmd_dirty(pmd_t pmd)
 }
 #endif
 
+/*
+ * Some platforms can customize the PTE soft-dirty bit making it unavailable
+ * even if the architecture provides the resource.
+ * Adding this API allows architectures to add their own checks for the
+ * devices on which the kernel is running.
+ * Note: When overriding it, please make sure the CONFIG_MEM_SOFT_DIRTY
+ * is part of this macro.
+ */
+#ifndef pgtable_supports_soft_dirty
+#define pgtable_supports_soft_dirty()	IS_ENABLED(CONFIG_MEM_SOFT_DIRTY)
+#endif
+
+#ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY
+#ifndef CONFIG_ARCH_HAS_PMD_SOFTLEAVES
+static inline pmd_t pmd_swp_mksoft_dirty(pmd_t pmd)
+{
+	return pmd;
+}
+
+static inline int pmd_swp_soft_dirty(pmd_t pmd)
+{
+	return 0;
+}
+
+static inline pmd_t pmd_swp_clear_soft_dirty(pmd_t pmd)
+{
+	return pmd;
+}
+#endif
+#else /* !CONFIG_HAVE_ARCH_SOFT_DIRTY */
+static inline int pte_soft_dirty(pte_t pte)
+{
+	return 0;
+}
+
+static inline int pmd_soft_dirty(pmd_t pmd)
+{
+	return 0;
+}
+
+static inline pte_t pte_mksoft_dirty(pte_t pte)
+{
+	return pte;
+}
+
+static inline pmd_t pmd_mksoft_dirty(pmd_t pmd)
+{
+	return pmd;
+}
+
+static inline pte_t pte_clear_soft_dirty(pte_t pte)
+{
+	return pte;
+}
+
+static inline pmd_t pmd_clear_soft_dirty(pmd_t pmd)
+{
+	return pmd;
+}
+
+static inline pte_t pte_swp_mksoft_dirty(pte_t pte)
+{
+	return pte;
+}
+
+static inline int pte_swp_soft_dirty(pte_t pte)
+{
+	return 0;
+}
+
+static inline pte_t pte_swp_clear_soft_dirty(pte_t pte)
+{
+	return pte;
+}
+
+static inline pmd_t pmd_swp_mksoft_dirty(pmd_t pmd)
+{
+	return pmd;
+}
+
+static inline int pmd_swp_soft_dirty(pmd_t pmd)
+{
+	return 0;
+}
+
+static inline pmd_t pmd_swp_clear_soft_dirty(pmd_t pmd)
+{
+	return pmd;
+}
+#endif
+
 /*
  * A facility to provide lazy MMU batching.  This allows PTE updates and
  * page invalidations to be delayed until a call to leave lazy MMU mode
@@ -1850,97 +1941,6 @@ static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot)
 #define arch_start_context_switch(prev)	do {} while (0)
 #endif
 
-/*
- * Some platforms can customize the PTE soft-dirty bit making it unavailable
- * even if the architecture provides the resource.
- * Adding this API allows architectures to add their own checks for the
- * devices on which the kernel is running.
- * Note: When overriding it, please make sure the CONFIG_MEM_SOFT_DIRTY
- * is part of this macro.
- */
-#ifndef pgtable_supports_soft_dirty
-#define pgtable_supports_soft_dirty()	IS_ENABLED(CONFIG_MEM_SOFT_DIRTY)
-#endif
-
-#ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY
-#ifndef CONFIG_ARCH_HAS_PMD_SOFTLEAVES
-static inline pmd_t pmd_swp_mksoft_dirty(pmd_t pmd)
-{
-	return pmd;
-}
-
-static inline int pmd_swp_soft_dirty(pmd_t pmd)
-{
-	return 0;
-}
-
-static inline pmd_t pmd_swp_clear_soft_dirty(pmd_t pmd)
-{
-	return pmd;
-}
-#endif
-#else /* !CONFIG_HAVE_ARCH_SOFT_DIRTY */
-static inline int pte_soft_dirty(pte_t pte)
-{
-	return 0;
-}
-
-static inline int pmd_soft_dirty(pmd_t pmd)
-{
-	return 0;
-}
-
-static inline pte_t pte_mksoft_dirty(pte_t pte)
-{
-	return pte;
-}
-
-static inline pmd_t pmd_mksoft_dirty(pmd_t pmd)
-{
-	return pmd;
-}
-
-static inline pte_t pte_clear_soft_dirty(pte_t pte)
-{
-	return pte;
-}
-
-static inline pmd_t pmd_clear_soft_dirty(pmd_t pmd)
-{
-	return pmd;
-}
-
-static inline pte_t pte_swp_mksoft_dirty(pte_t pte)
-{
-	return pte;
-}
-
-static inline int pte_swp_soft_dirty(pte_t pte)
-{
-	return 0;
-}
-
-static inline pte_t pte_swp_clear_soft_dirty(pte_t pte)
-{
-	return pte;
-}
-
-static inline pmd_t pmd_swp_mksoft_dirty(pmd_t pmd)
-{
-	return pmd;
-}
-
-static inline int pmd_swp_soft_dirty(pmd_t pmd)
-{
-	return 0;
-}
-
-static inline pmd_t pmd_swp_clear_soft_dirty(pmd_t pmd)
-{
-	return pmd;
-}
-#endif
-
 #ifndef __HAVE_PFNMAP_TRACKING
 /*
  * Interfaces that can be used by architecture code to keep track of

-- 
2.53.0


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

* [PATCH 2/3] mm: don't let modify_prot_start_ptes() set soft-dirty on clean PTEs
  2026-11-25 13:32 [PATCH 0/3] mm: fix soft-dirty false positives in mprotect() of large folios Shivank Garg
  2026-11-25 13:32 ` [PATCH 1/3] mm: move the soft-dirty fallbacks up in pgtable.h Shivank Garg
@ 2026-11-25 13:32 ` Shivank Garg
  2026-11-25 13:32 ` [PATCH 3/3] selftests/mm: soft-dirty: check that mprotect() keeps PTE-mapped THPs clean Shivank Garg
  2026-11-25 13:43 ` [PATCH 0/3] mm: fix soft-dirty false positives in mprotect() of large folios David Hildenbrand (Arm)
  3 siblings, 0 replies; 5+ messages in thread
From: Shivank Garg @ 2026-11-25 13:32 UTC (permalink / raw)
  To: Andrew Morton, David Hildenbrand, Lorenzo Stoakes,
	Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
	Suren Baghdasaryan, Michal Hocko, Dev Jain, Zi Yan, Baolin Wang,
	Nico Pache, Ryan Roberts, Barry Song, Lance Yang, Usama Arif,
	Kiryl Shutsemau, Peter Xu, Shuah Khan, Michał Mirosław,
	Andrei Vagin, Paul Gofman, Bharata B Rao, Daniel Wade, Yin Tirui,
	Muhammad Usama Anjum
  Cc: linux-mm, linux-kselftest, linux-kernel, Shivank Garg

Since commit cac1db8c3aad ("mm: optimize mprotect() by PTE batching"),
mprotect() of large folios marks pages soft-dirty that were not written
since the soft-dirty bits were cleared.

modify_prot_start_ptes() folds the dirty bits of a batch into the
returned PTE with pte_mkdirty(), which also sets the soft-dirty bit on
architectures that support it.  If any PTE of the batch is hardware
dirty, change_pte_range() then marks the whole batch soft-dirty.
clear_refs keeps the hardware dirty bit, so after clear_refs, mprotect()
makes nearly every page of a populated region look written, which can
mislead tools like CRIU into copying it again.

Reproducer, with 64K mTHP enabled:

 1. mmap() a 64K-aligned 1MiB anonymous region and populate it
 2. write "4" to /proc/self/clear_refs
 3. write to the first page of each 64K folio
 4. mprotect(PROT_READ) the region
 5. read bit 55 of /proc/self/pagemap for each page

Only the 16 written pages should be soft-dirty, but on x86-64 all 256
are.

Instead, mark the returned PTE soft-dirty if any PTE of the batch is
soft-dirty, and clear the bit otherwise, undoing the soft-dirty bit
that pte_mkdirty() set.

Fixes: cac1db8c3aad ("mm: optimize mprotect() by PTE batching")
Signed-off-by: Shivank Garg <shivankg@amd.com>
---
 include/linux/pgtable.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h
index b1db7aec5362..bbba2cc71096 100644
--- a/include/linux/pgtable.h
+++ b/include/linux/pgtable.h
@@ -1785,8 +1785,10 @@ static inline pte_t modify_prot_start_ptes(struct vm_area_struct *vma,
 		unsigned long addr, pte_t *ptep, unsigned int nr)
 {
 	pte_t pte, tmp_pte;
+	bool soft_dirty;
 
 	pte = ptep_modify_prot_start(vma, addr, ptep);
+	soft_dirty = pte_soft_dirty(pte);
 	while (--nr) {
 		ptep++;
 		addr += PAGE_SIZE;
@@ -1795,8 +1797,10 @@ static inline pte_t modify_prot_start_ptes(struct vm_area_struct *vma,
 			pte = pte_mkdirty(pte);
 		if (pte_young(tmp_pte))
 			pte = pte_mkyoung(pte);
+		soft_dirty |= pte_soft_dirty(tmp_pte);
 	}
-	return pte;
+	/* pte_mkdirty() also sets the soft-dirty bit. */
+	return soft_dirty ? pte_mksoft_dirty(pte) : pte_clear_soft_dirty(pte);
 }
 #endif
 

-- 
2.53.0


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

* [PATCH 3/3] selftests/mm: soft-dirty: check that mprotect() keeps PTE-mapped THPs clean
  2026-11-25 13:32 [PATCH 0/3] mm: fix soft-dirty false positives in mprotect() of large folios Shivank Garg
  2026-11-25 13:32 ` [PATCH 1/3] mm: move the soft-dirty fallbacks up in pgtable.h Shivank Garg
  2026-11-25 13:32 ` [PATCH 2/3] mm: don't let modify_prot_start_ptes() set soft-dirty on clean PTEs Shivank Garg
@ 2026-11-25 13:32 ` Shivank Garg
  2026-11-25 13:43 ` [PATCH 0/3] mm: fix soft-dirty false positives in mprotect() of large folios David Hildenbrand (Arm)
  3 siblings, 0 replies; 5+ messages in thread
From: Shivank Garg @ 2026-11-25 13:32 UTC (permalink / raw)
  To: Andrew Morton, David Hildenbrand, Lorenzo Stoakes,
	Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
	Suren Baghdasaryan, Michal Hocko, Dev Jain, Zi Yan, Baolin Wang,
	Nico Pache, Ryan Roberts, Barry Song, Lance Yang, Usama Arif,
	Kiryl Shutsemau, Peter Xu, Shuah Khan, Michał Mirosław,
	Andrei Vagin, Paul Gofman, Bharata B Rao, Daniel Wade, Yin Tirui,
	Muhammad Usama Anjum
  Cc: linux-mm, linux-kselftest, linux-kernel, Shivank Garg

Add a test that clears the soft-dirty bits of a dirty, PTE-mapped THP,
mprotect()s it without writing to it, and checks that pagemap reports no
page as soft-dirty.  mprotect() batches the PTEs of the large folio, so
this covers modify_prot_start_ptes().

On x86-64, the test fails without the previous patch.  It is skipped
without THP.

Signed-off-by: Shivank Garg <shivankg@amd.com>
---
 tools/testing/selftests/mm/soft-dirty.c | 48 ++++++++++++++++++++++++++++++++-
 1 file changed, 47 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/mm/soft-dirty.c b/tools/testing/selftests/mm/soft-dirty.c
index 7f649b673355..1764e285f125 100644
--- a/tools/testing/selftests/mm/soft-dirty.c
+++ b/tools/testing/selftests/mm/soft-dirty.c
@@ -310,6 +310,51 @@ static void test_merge(int pagemap_fd, int pagesize)
 	munmap(map, 2 * pagesize);
 }
 
+/*
+ * Clear the soft-dirty bits of a dirty PTE-mapped THP, then mprotect() it
+ * without writing to it. mprotect() batches the PTEs of the large folio
+ * and must not mark them soft-dirty.
+ */
+static void test_thp_mprotect(int pagemap_fd, int pagesize)
+{
+	size_t hpage_len = read_pmd_pagesize();
+	size_t len = hpage_len - pagesize;
+	unsigned long nr = 0;
+	size_t offs;
+	char *map;
+
+	if (!thp_is_enabled() || !hpage_len) {
+		ksft_test_result_skip("Test %s no THP\n", __func__);
+		return;
+	}
+
+	map = memalign(hpage_len, hpage_len);
+	if (!map)
+		ksft_exit_fail_msg("memalign failed\n");
+	if (madvise(map, hpage_len, MADV_HUGEPAGE))
+		ksft_exit_fail_msg("madvise failed\n");
+	memset(map, 1, hpage_len);
+
+	if (!check_huge_anon(map, hpage_len, 1, hpage_len)) {
+		ksft_test_result_skip("Test %s no THP\n", __func__);
+		goto out;
+	}
+
+	/* Zapping the last page PTE-maps the rest of the THP. */
+	madvise(map + len, pagesize, MADV_DONTNEED);
+	clear_softdirty();
+	mprotect(map, len, PROT_READ);
+
+	for (offs = 0; offs < len; offs += pagesize)
+		nr += pagemap_is_softdirty(pagemap_fd, map + offs);
+	ksft_test_result(!nr, "Test %s soft-dirty clear after mprotect\n",
+			 __func__);
+
+	mprotect(map, len, PROT_READ | PROT_WRITE);
+out:
+	free(map);
+}
+
 static void test_mprotect_anon(int pagemap_fd, int pagesize)
 {
 	test_mprotect(pagemap_fd, pagesize, true);
@@ -330,7 +375,7 @@ int main(int argc, char **argv)
 	if (!softdirty_supported())
 		ksft_exit_skip("soft-dirty is not support\n");
 
-	ksft_set_plan(19);
+	ksft_set_plan(20);
 	pagemap_fd = open(PAGEMAP_FILE_PATH, O_RDONLY);
 	if (pagemap_fd < 0)
 		ksft_exit_fail_msg("Failed to open %s\n", PAGEMAP_FILE_PATH);
@@ -343,6 +388,7 @@ int main(int argc, char **argv)
 	test_mprotect_anon(pagemap_fd, pagesize);
 	test_mprotect_file(pagemap_fd, pagesize);
 	test_merge(pagemap_fd, pagesize);
+	test_thp_mprotect(pagemap_fd, pagesize);
 
 	close(pagemap_fd);
 

-- 
2.53.0


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

* Re: [PATCH 0/3] mm: fix soft-dirty false positives in mprotect() of large folios
  2026-11-25 13:32 [PATCH 0/3] mm: fix soft-dirty false positives in mprotect() of large folios Shivank Garg
                   ` (2 preceding siblings ...)
  2026-11-25 13:32 ` [PATCH 3/3] selftests/mm: soft-dirty: check that mprotect() keeps PTE-mapped THPs clean Shivank Garg
@ 2026-11-25 13:43 ` David Hildenbrand (Arm)
  3 siblings, 0 replies; 5+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-25 13:43 UTC (permalink / raw)
  To: Shivank Garg, Andrew Morton, Lorenzo Stoakes, Liam R. Howlett,
	Vlastimil Babka, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
	Dev Jain, Zi Yan, Baolin Wang, Nico Pache, Ryan Roberts,
	Barry Song, Lance Yang, Usama Arif, Kiryl Shutsemau, Peter Xu,
	Shuah Khan, Michał Mirosław, Andrei Vagin, Paul Gofman,
	Bharata B Rao, Daniel Wade, Yin Tirui, Muhammad Usama Anjum
  Cc: linux-mm, linux-kselftest, linux-kernel

On 11/25/26 14:32, Shivank Garg wrote:
> mprotect() handles the PTEs of a large folio as one batch, and marks the
> whole batch dirty with pte_mkdirty() if any of its PTEs is dirty.  On
> x86, powerpc, riscv and s390, pte_mkdirty() also sets the soft-dirty
> bit, so after clear_refs, mprotect() marks unwritten pages soft-dirty.
> With 64K mTHP on x86-64, all 240 unwritten pages of a 1MiB region are
> reported soft-dirty; with my fix, none are.

Why do we care? Just over-indicate soft-dirty. It's valid, and the whole feature
is full of holes and buggy as hell.

-- 
Cheers,

David

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

end of thread, other threads:[~2026-11-25 13:43 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-25 13:32 [PATCH 0/3] mm: fix soft-dirty false positives in mprotect() of large folios Shivank Garg
2026-11-25 13:32 ` [PATCH 1/3] mm: move the soft-dirty fallbacks up in pgtable.h Shivank Garg
2026-11-25 13:32 ` [PATCH 2/3] mm: don't let modify_prot_start_ptes() set soft-dirty on clean PTEs Shivank Garg
2026-11-25 13:32 ` [PATCH 3/3] selftests/mm: soft-dirty: check that mprotect() keeps PTE-mapped THPs clean Shivank Garg
2026-11-25 13:43 ` [PATCH 0/3] mm: fix soft-dirty false positives in mprotect() of large folios David Hildenbrand (Arm)

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®