mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] mm/memory: mark unshare copies dirty to fix MADV_FREE data loss
@ 2026-11-17 10:06 Daniel Wade
  2026-11-17 10:16 ` David Hildenbrand (Arm)
  0 siblings, 1 reply; 2+ messages in thread
From: Daniel Wade @ 2026-11-17 10:06 UTC (permalink / raw)
  To: David Hildenbrand, Andrew Morton
  Cc: Lorenzo Stoakes, Liam R . Howlett, Vlastimil Babka,
	Mike Rapoport, Suren Baghdasaryan, Michal Hocko, stable,
	linux-mm, linux-kernel

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-17 10:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-17 10:06 [PATCH v2] mm/memory: mark unshare copies dirty to fix MADV_FREE data loss Daniel Wade
2026-11-17 10:16 ` David Hildenbrand (Arm)

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®