From: Shivank Garg <shivankg@amd.com>
To: "Andrew Morton" <akpm@linux-foundation.org>,
"David Hildenbrand" <david@kernel.org>,
"Lorenzo Stoakes" <ljs@kernel.org>,
"Liam R. Howlett" <liam@infradead.org>,
"Vlastimil Babka" <vbabka@kernel.org>,
"Mike Rapoport" <rppt@kernel.org>,
"Suren Baghdasaryan" <surenb@google.com>,
"Michal Hocko" <mhocko@suse.com>, "Dev Jain" <dev.jain@arm.com>,
"Zi Yan" <ziy@nvidia.com>,
"Baolin Wang" <baolin.wang@linux.alibaba.com>,
"Nico Pache" <nico.pache@linux.dev>,
"Ryan Roberts" <ryan.roberts@arm.com>,
"Barry Song" <baohua@kernel.org>,
"Lance Yang" <lance.yang@linux.dev>,
"Usama Arif" <usama.arif@linux.dev>,
"Kiryl Shutsemau" <kas@kernel.org>,
"Peter Xu" <peterx@redhat.com>, "Shuah Khan" <shuah@kernel.org>,
"Michał Mirosław" <emmir@google.com>,
"Andrei Vagin" <avagin@gmail.com>,
"Paul Gofman" <pgofman@codeweavers.com>,
"Bharata B Rao" <bharata@amd.com>,
"Daniel Wade" <danjwade95@gmail.com>,
"Yin Tirui" <yintirui@gmail.com>,
"Muhammad Usama Anjum" <usama.anjum@arm.com>
Cc: <linux-mm@kvack.org>, <linux-kselftest@vger.kernel.org>,
<linux-kernel@vger.kernel.org>, Shivank Garg <shivankg@amd.com>
Subject: [PATCH 2/3] mm: don't let modify_prot_start_ptes() set soft-dirty on clean PTEs
Date: Wed, 25 Nov 2026 13:32:28 +0000 [thread overview]
Message-ID: <20261125-b4-mm-soft-dirty-fixes-v1-2-538e3cdb618c@amd.com> (raw)
In-Reply-To: <20261125-b4-mm-soft-dirty-fixes-v1-0-538e3cdb618c@amd.com>
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
next prev parent reply other threads:[~2026-11-25 13:33 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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
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=20261125-b4-mm-soft-dirty-fixes-v1-2-538e3cdb618c@amd.com \
--to=shivankg@amd.com \
--cc=akpm@linux-foundation.org \
--cc=avagin@gmail.com \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=bharata@amd.com \
--cc=danjwade95@gmail.com \
--cc=david@kernel.org \
--cc=dev.jain@arm.com \
--cc=emmir@google.com \
--cc=kas@kernel.org \
--cc=lance.yang@linux.dev \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=mhocko@suse.com \
--cc=nico.pache@linux.dev \
--cc=peterx@redhat.com \
--cc=pgofman@codeweavers.com \
--cc=rppt@kernel.org \
--cc=ryan.roberts@arm.com \
--cc=shuah@kernel.org \
--cc=surenb@google.com \
--cc=usama.anjum@arm.com \
--cc=usama.arif@linux.dev \
--cc=vbabka@kernel.org \
--cc=yintirui@gmail.com \
--cc=ziy@nvidia.com \
/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®