[PATCH 0/3] mm: fix soft-dirty false positives in mprotect() of large folios

From: Shivank Garg

Date: Wed Nov 25 2026 - 08:33:28 EST


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@xxxxxxx
[2] https://lore.kernel.org/20260813-migrate-rmap-batch-v2-0-3c5424c555c7@xxxxxxx

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