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; 7+ 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] 7+ messages in thread

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

Thread overview: 7+ 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)
2026-11-25 14:16   ` Garg, Shivank
2026-11-25 15:42   ` Paul Gofman

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®