Skip to contentSkip to conversation

mm/memory: mark unshare copies dirty to fix MADV_FREE data loss

PATCH Superseded Daniel Wade 2 participants 3 messages Nov 16 – Nov 17

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/memory: mark unshare copies dirty to fix MADV_FREE data loss

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

---
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(+)
mm/memory.c +8 -0
index ec3d9d99ccee..49f6dc92c020 100644
@@ -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)) {
#1 lore raw
Quoted 4 lines · Daniel Wade

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

Thanks for the report.

Quoted 17 lines

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.

Quoted 5 lines

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

numa_maps does not show any such data. Did you mean smaps_rollup ?

Quoted 37 lines

Private_Dirty or Shared_Dirty rather than _Clean. The PTE is unchanged.

Fixes c89357e27f20 ("mm: support GUP-triggered unsharing of anonymous pages")

---
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(+)
mm/memory.c +1 -0
index ec3d9d99ccee..49f6dc92c020 100644
@@ -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)) {
+			/*

mm/memory.c static vm_fault_t wp_page_copy(struct vm_fault *vmf) · line 4143

Quoted 10 lines
mm/memory.c +7 -0
+			 * 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)) {

We should always prefer setting the pte dirty, not the folio, as the latter needs atomics. If that sets the soft-dirty bit, we can clear that if required, So something cheaper would be:

mm/memory.c +3 -0
index ec3d9d99ccee2..22106e016da91 100644
@@ -4130,8 +4130,16 @@ 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))

That would be acceptable as a quick fix.

But I am wondering which rule we actually want to follow in the future.

folio_add_new_anon_rmap() always sets __folio_set_swapbacked().

In vmscan, we run into

if (folio_test_anon(folio) && folio_test_swapbacked(folio) &&

    !folio_test_swapcache(folio)) {

	...
	folio_mark_dirty(folio);
}

That's what traditionally meant: even if the pte+folio is not dirty, we will dirty it once during reclaim.

So if we go down the path here, that means, any pte has to be dirty when it starts in a process. I think we mostly follow that logic already.

But the real problem is that MADV_FREE messes with that logic as it operates on shared folios. I don't think it should have ever done that.

A more future proof solution would be to only allow MADV_FREE if we are the exclusive folio owner, checked under folio lock. In madvise_free_huge_pmd() we almost do the correct thing: we would just have to recheck under folio lock.

So something less shaky in the future could be:

mm/huge_memory.c +8 -4
index eb913acb03b74..020bee9b47e93 100644
@@ -2437,15 +2437,19 @@ bool madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
 	if (folio_is_zone_device(folio))
 		goto out;
 
+	if (folio_maybe_mapped_shared(folio))
+		goto out;
+	if (!folio_trylock(folio))
+		goto out;
+
 	/*
 	 * If other processes are mapping this folio, we couldn't discard
 	 * the folio unless they all do MADV_FREE so let's skip the folio.
 	 */
-	if (folio_maybe_mapped_shared(folio))
-		goto out;
-
-	if (!folio_trylock(folio))
+	if (folio_maybe_mapped_shared(folio) || folio_test_swapcache(folio)) {
+		folio_unlock(folio);
 		goto out;
+	}
 
 	/*
 	 * If user want to discard part-pages of THP, split it so MADV_FREE
mm/madvise.c +26 -27

64 lines not on this page · raw message

But that means that folios we cannot trylock cannot be marked lazyfree. Well, the PTE path would now match the PMD path.

Signature · 3 lines
Cheers,

David
#2 lore raw
Quoted 2 lines · On Tue, 17 Nov 2026 09:55:24 +0100 "David Hildenbrand (Arm)"…

We should always prefer setting the pte dirty, not the folio, as the latter needs atomics.

Will do a v2 with your version, thanks.

Quoted 1 line

numa_maps does not show any such data. Did you mean smaps_rollup ?

It does, gather_stats() ors the pte bit with folio_test_dirty(). So I kept it.

Quoted 2 lines

A more future proof solution would be to only allow MADV_FREE if we are the exclusive folio owner, checked under folio lock.

Makes sense to me, it kills the whole class instead of one producer at a time.

One thing though, that takes the folio lock on every folio. Right now a clean exclusive one doesn't take it at all. If the lock is busy you skip the folio, so jemalloc freeing a big range would leave some of it behind.