Skip to contentSkip to conversation

mm/khugepaged: mark a collapsed mTHP dirty

PATCH In review Daniel Wade 2 participants 4 messages Nov 17

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/khugepaged: mark a collapsed mTHP dirty

When khugepaged collapses an anonymous range into a folio below PMD size, collapse_huge_page() copies the pages in, but map_anon_folio_pte_nopf() marks the PTEs dirty only when the VMA has VM_WRITE and nothing marks the folio itself dirty. After mprotect(PROT_READ) the folio therefore holds the only copy of the data while looking clean, so MADV_FREE in one mapping marks it lazyfree, reclaim frees it, and another process that maps it reads zeroes. Mark the folio dirty before mapping it.

Found by static analysis with an AI coding assistant and reproduced with a test program on an unmodified kernel.

Fixes e22f2fe72c74 ("mm/khugepaged: generalize collapse_huge_page for mTHP collapse")

---
Claude (claude-mythos-5-2) found the bug and wrote the fix, this description and the test program, which is available on request.

Tested on b3dfbde62cf1 with KASAN and lockdep: as uid 65534, write 64 KiB, mprotect(PROT_READ), wait for khugepaged, fork, MADV_FREE in the parent. Unpatched the child reads 65536 wrong bytes; patched it is intact.

 mm/khugepaged.c |    5 +++++
 1 file changed, 5 insertions(+)
mm/khugepaged.c +5 -0
index 4bbe319..8a81cab 100644
@@ -1422,6 +1422,11 @@ static enum scan_result collapse_huge_page(struct mm_struct *mm,
 		if (pte_ptl != pmd_ptl)
 			spin_lock_nested(pte_ptl, SINGLE_DEPTH_NESTING);
 		pmd_populate(mm, pmd, pmd_pgtable(_pmd));
+		/*
+		 * The PTEs are clean in a read-only VMA, but the folio holds
+		 * copied data: mark it dirty so reclaim cannot drop it.
+		 */
+		folio_set_dirty(folio);
 		map_anon_folio_pte_nopf(folio, pte, vma, start_addr,
 					  /*uffd_wp=*/ false);
 		if (pte_ptl != pmd_ptl)
#1 lore raw
Quoted 9 lines · Daniel Wade

When khugepaged collapses an anonymous range into a folio below PMD size, collapse_huge_page() copies the pages in, but map_anon_folio_pte_nopf() marks the PTEs dirty only when the VMA has VM_WRITE and nothing marks the folio itself dirty. After mprotect(PROT_READ) the folio therefore holds the only copy of the data while looking clean, so MADV_FREE in one mapping marks it lazyfree, reclaim frees it, and another process that maps it reads zeroes. Mark the folio dirty before mapping it.

Quoted 7 lines

Found by static analysis with an AI coding assistant and reproduced with a test program on an unmodified kernel.

Fixes e22f2fe72c74 ("mm/khugepaged: generalize collapse_huge_page for mTHP collapse")

---

I have a different patch on my queue here that fixes this as well I think. Please review.

commit b835defd5b9b0fb8cd89997fd4a11c27b28bbef7

Author: David Hildenbrand (Arm) <[email protected]>
Date:   Wed Nov 11 21:09:26 2026 +0100

mm/khugepaged: do not lose soft-dirty in an mTHP collapse

An mTHP collapse can lose soft-dirty PTE bits if pages were written and the VMA was then made read-only.

Collapse does not preserve the soft-dirty bits of the old PTEs, so the new entries must be marked soft-dirty. map_anon_folio_pte_nopf() relies on pte_mkdirty() to do that, but only calls it if the VMA has VM_WRITE.

In map_anon_folio_pte_nopf(), call pte_mkdirty() unconditionally, just like map_anon_folio_pmd_nopf() already does with pmd_mkdirty().

Consequently, we always mark these PTEs dirty (including soft-dirty) but only set them writable if the VMA has VM_WRITE.

This essentially reverts Hugh's commit 1ac0cb5d0e22 ("mm: fix anonymous dirtying") that back then touched do_anonymous_page() to avoid forcing clean anonymous pages through swap instead of discarding them. This no longer applies as shrink_folio_list() allocates swap for ordinary anonymous folios and unconditionally marks them dirty before unmapping, regardless of PTE dirty state. Keeping their PTEs clean does not make them discardable.

This now also marks fresh anonymous PTEs dirty and soft-dirty in read-only VMAs when page faults allocate real pages.

This has low urgency.

Fixes e22f2fe72c74 ("mm/khugepaged: generalize collapse_huge_page for mTHP collapse")
mm/memory.c +1 -2
index 24144892c2803..8b4a62722a0b1 100644
@@ -5518,8 +5518,7 @@ void map_anon_folio_pte_nopf(struct folio *folio, pte_t *pte,
 
        entry = pte_sw_mkyoung(entry);
 
-       if (vma->vm_flags & VM_WRITE)
-               entry = pte_mkwrite(pte_mkdirty(entry), vma);
+       entry = maybe_mkwrite(pte_mkdirty(entry), vma);
        if (uffd_wp)
                entry = pte_mkuffd(entry);
 
Signature · 3 lines
Cheers,

David
author Reviewed-by
#2 lore raw
Quoted 1 line · On Tue, 17 Nov 2026 09:13:09 +0100 "David Hildenbrand (Arm)"…

I have a different patch on my queue here that fixes this as well I think. Please review.

Yep had a look and yours does fix it.

It's more than soft-dirty though. If the folio gets dropped while another process still maps it that process reads zeroes, I had 64K of a child's memory come back wrong. So might need a stable backport.

Dropping mine, yours obsoletes it.

#3 lore raw
Quoted 10 lines · Daniel Wade
Quoted 1 line · On Tue, 17 Nov 2026 09:13:09 +0100 "David Hildenbrand (Arm)"…

I have a different patch on my queue here that fixes this as well I think. Please review.

Yep had a look and yours does fix it.

It's more than soft-dirty though. If the folio gets dropped while another process still maps it that process reads zeroes, I had 64K of a child's memory come back wrong. So might need a stable backport.

Yes, I'll add you reported-by and clarify in the patch description.

I expected nobody really runs into this I practice (odd combination of things), but CC stable makes sense.

Quoted 4 lines

Dropping mine, yours obsoletes it.

Thanks, I just sent out a proper patch.

Signature · 3 lines
Cheers,

David