From: Daniel Wade <danjwade95@gmail.com>
To: David Hildenbrand <david@kernel.org>, Zi Yan <ziy@nvidia.com>,
Andrew Morton <akpm@linux-foundation.org>
Cc: Baolin Wang <baolin.wang@linux.alibaba.com>,
"Liam R . Howlett" <liam@infradead.org>,
Nico Pache <nico.pache@linux.dev>,
Ryan Roberts <ryan.roberts@arm.com>, Dev Jain <dev.jain@arm.com>,
Barry Song <baohua@kernel.org>, Lance Yang <lance.yang@linux.dev>,
Usama Arif <usama.arif@linux.dev>,
Kiryl Shutsemau <kas@kernel.org>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org
Subject: [PATCH] mm/khugepaged: mark a collapsed mTHP dirty
Date: Tue, 17 Nov 2026 18:22:46 +1030 [thread overview]
Message-ID: <20261117075246.61336-1-danjwade95@gmail.com> (raw)
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")
Cc: stable@vger.kernel.org
Signed-off-by: Daniel Wade <danjwade95@gmail.com>
---
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(+)
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 4bbe319..8a81cab 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -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)
next reply other threads:[~2026-11-17 7:53 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-17 7:52 Daniel Wade [this message]
2026-11-17 8:13 ` [PATCH] mm/khugepaged: mark a collapsed mTHP dirty David Hildenbrand (Arm)
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=20261117075246.61336-1-danjwade95@gmail.com \
--to=danjwade95@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=david@kernel.org \
--cc=dev.jain@arm.com \
--cc=kas@kernel.org \
--cc=lance.yang@linux.dev \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=nico.pache@linux.dev \
--cc=ryan.roberts@arm.com \
--cc=stable@vger.kernel.org \
--cc=usama.arif@linux.dev \
--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