From: Daniel Wade <danjwade95@gmail.com>
To: David Hildenbrand <david@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>
Cc: 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>,
stable@vger.kernel.org, linux-mm@kvack.org,
linux-kernel@vger.kernel.org
Subject: [PATCH v2] mm/memory: mark unshare copies dirty to fix MADV_FREE data loss
Date: Tue, 17 Nov 2026 20:36:22 +1030 [thread overview]
Message-ID: <20261117100622.807-1-danjwade95@gmail.com> (raw)
wp_page_copy() only calls pte_mkdirty() for write faults, so the copy it
makes for FAULT_FLAG_UNSHARE comes out clean. Nothing marks the folio
dirty either and there is no swap entry.
Unshare faults come from R/O GUP pins, like process_vm_readv() on a page
shared with a child. If the process forks after the pin is gone,
MADV_FREE in one of them marks the folio lazyfree and reclaim frees it.
The other process then reads zeroes.
Mark the copy's PTE dirty. pte_mkdirty() also sets soft-dirty on x86, so
take soft-dirty from the old PTE in both directions.
Side effect: smaps and numa_maps count an unshare copy as Private_Dirty
or Shared_Dirty rather than _Clean.
Fixes: c89357e27f20 ("mm: support GUP-triggered unsharing of anonymous pages")
Suggested-by: David Hildenbrand (Arm) <david@kernel.org>
Cc: stable@vger.kernel.org
Signed-off-by: Daniel Wade <danjwade95@gmail.com>
---
v2: set the PTE dirty instead of the folio, as David suggested. No atomic,
and the dirty bit then also survives in the other mappings.
v1: https://lore.kernel.org/all/20261116111245.28871-1-danjwade95@gmail.com/
Tested on mainline b3dfbde62cf1, x86-64 with KASAN and PROVE_LOCKING, as
uid 65534. The test gives an unshare copy to a child, has the parent
MADV_FREE it and a control page it alone maps, then waits for reclaim.
Without the patch all 4096 bytes come back wrong in the child; with it the
child's page is intact and the control page is still discarded. Same on
v6.12.117.
mm/memory.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/mm/memory.c b/mm/memory.c
index ec3d9d99ccee..113b305854d5 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -4130,8 +4130,11 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf)
entry = folio_mk_pte(new_folio, vma->vm_page_prot);
entry = pte_sw_mkyoung(entry);
if (unlikely(unshare)) {
+ entry = pte_mkdirty(entry);
if (pte_soft_dirty(vmf->orig_pte))
entry = pte_mksoft_dirty(entry);
+ else
+ entry = pte_clear_soft_dirty(entry);
if (pte_uffd(vmf->orig_pte)) {
entry = pte_mkuffd(entry);
if (userfaultfd_rwp(vma))
next reply other threads:[~2026-11-17 10:06 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-17 10:06 Daniel Wade [this message]
2026-11-17 10:16 ` 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=20261117100622.807-1-danjwade95@gmail.com \
--to=danjwade95@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=david@kernel.org \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=mhocko@suse.com \
--cc=rppt@kernel.org \
--cc=stable@vger.kernel.org \
--cc=surenb@google.com \
--cc=vbabka@kernel.org \
/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®