mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] mm/memory: mark unshare copies dirty to fix MADV_FREE data loss
@ 2026-11-16 11:12 Daniel Wade
  0 siblings, 0 replies; only message in thread
From: Daniel Wade @ 2026-11-16 11:12 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

MADV_FREE in one process can make reclaim free an anonymous page that
another process still maps and never gave up, which then reads zeroes.
It happens with a page copied by an unshare fault and shared by a later
fork().

MADV_FREE and lazyfree reclaim rely on an unwritten rule: an anonymous
folio whose content a mapping still needs has a dirty PTE in that
mapping, PG_dirty set, or a swap cache entry. madvise_free_pte_range()
only checks for other mappers on a folio that is already dirty or in the
swap cache. Any other folio it covers is marked lazyfree once the
caller's PTEs are clean, even when other processes map it, and
try_to_unmap_one() then drops those clean PTEs and frees the folio.

The copy wp_page_copy() makes for FAULT_FLAG_UNSHARE breaks the rule:
pte_mkdirty() is applied only for write faults, so the copy has a clean
PTE, no PG_dirty and no swap cache entry. Unshare faults come from R/O
pins on non-exclusive anonymous pages, for example process_vm_readv() on
a page shared with a child, or KSM unmerging one. If the process forks
after the pin is gone, MADV_FREE in either process frees the page under
the other.

Mark the copy dirty with folio_set_dirty(), as ksm_might_need_to_copy()
does for its copy. pte_mkdirty() is not suitable: on x86 it also sets the
soft-dirty bit, which this path carries over from the old PTE.

Side effect: smaps and numa_maps now count an unshare copy as
Private_Dirty or Shared_Dirty rather than _Clean. The PTE is unchanged.

Fixes: c89357e27f20 ("mm: support GUP-triggered unsharing of anonymous pages")
Cc: stable@vger.kernel.org
Signed-off-by: Daniel Wade <danjwade95@gmail.com>
---
Tested on mainline b3dfbde62cf1; mm/memory.c and mm/madvise.c are identical
in v7.4-rc3. x86-64, KASAN and PROVE_LOCKING, 2 vCPUs under KVM, no swap,
as uid 65534, with only this patch applied.

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: "test page in the child (unshare copy): present 0, 4096 of 4096
  bytes wrong"; an ordinary shared page freed the same way stays intact.
- with: the child's page is intact, the control page still discarded.
Same two results on v6.12.117, there with folio_set_dirty() placed earlier
in the same function. No KASAN or lockdep report in any run.

Two other ways to reach the same state are known and unchanged here:
khugepaged collapse to a sub-PMD folio in a non-writable VMA (covered by
"mm/khugepaged: mark a collapsed mTHP dirty"), and a swap-cache-skipping
read fault in the stable trees that still skip it. Checking for other
mappers in madvise_free_pte_range() covers all three and passed the same
tests, but changes MADV_FREE for every shared clean page.

 mm/memory.c |    8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/mm/memory.c b/mm/memory.c
index ec3d9d99ccee..49f6dc92c020 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -4130,6 +4130,14 @@ 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)) {
+			/*
+			 * The PTE stays clean, and pte_mkdirty() would also
+			 * set soft-dirty, which is carried over below. Mark
+			 * the folio dirty instead: MADV_FREE and lazyfree
+			 * reclaim may drop a clean anon folio that has no
+			 * swap entry, even one others still map.
+			 */
+			folio_set_dirty(new_folio);
 			if (pte_soft_dirty(vmf->orig_pte))
 				entry = pte_mksoft_dirty(entry);
 			if (pte_uffd(vmf->orig_pte)) {

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-16 11:13 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-16 11:12 [PATCH] mm/memory: mark unshare copies dirty to fix MADV_FREE data loss Daniel Wade

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®