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