From: "David Hildenbrand (Arm)" <david@kernel.org>
To: Daniel Wade <danjwade95@gmail.com>, 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: Re: [PATCH] mm/khugepaged: mark a collapsed mTHP dirty
Date: Tue, 17 Nov 2026 09:13:09 +0100 [thread overview]
Message-ID: <dfbf361e-8125-4f8e-9567-65e80996e2d4@kernel.org> (raw)
In-Reply-To: <20261117075246.61336-1-danjwade95@gmail.com>
On 11/17/26 08:52, Daniel Wade wrote:
> 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>
> ---
I have a different patch on my queue here that fixes this as well I think. Please review.
commit b835defd5b9b0fb8cd89997fd4a11c27b28bbef7
Author: David Hildenbrand (Arm) <david@kernel.org>
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")
Reported-by: Chris Mason <mason@kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
diff --git a/mm/memory.c b/mm/memory.c
index 24144892c2803..8b4a62722a0b1 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -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);
--
Cheers,
David
prev parent reply other threads:[~2026-11-17 8:13 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-17 7:52 Daniel Wade
2026-11-17 8:13 ` David Hildenbrand (Arm) [this message]
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=dfbf361e-8125-4f8e-9567-65e80996e2d4@kernel.org \
--to=david@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=danjwade95@gmail.com \
--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
all inboxes | Powered by JetHome®