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
  2026-11-17  8:55 ` David Hildenbrand (Arm)
  0 siblings, 1 reply; 2+ messages 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] 2+ messages in thread

* Re: [PATCH] mm/memory: mark unshare copies dirty to fix MADV_FREE data loss
  2026-11-16 11:12 [PATCH] mm/memory: mark unshare copies dirty to fix MADV_FREE data loss Daniel Wade
@ 2026-11-17  8:55 ` David Hildenbrand (Arm)
  0 siblings, 0 replies; 2+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-17  8:55 UTC (permalink / raw)
  To: Daniel Wade, Andrew Morton
  Cc: Lorenzo Stoakes, Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
	Suren Baghdasaryan, Michal Hocko, stable, linux-mm, linux-kernel

On 11/16/26 12:12, Daniel Wade wrote:
> 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.

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

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

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

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:

diff --git a/mm/memory.c b/mm/memory.c
index ec3d9d99ccee2..22106e016da91 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -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:

diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index eb913acb03b74..020bee9b47e93 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -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
diff --git a/mm/madvise.c b/mm/madvise.c
index 0214efd2a014f..9be519a72e856 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -753,38 +753,37 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,
 			}
 		}
 
-		if (folio_test_swapcache(folio) || folio_test_dirty(folio)) {
-			if (!folio_trylock(folio))
-				continue;
-			/*
-			 * KSM folios can gain new mappings at any time, so we must
-			 * not clear their dirty flag.
-			 */
-			if (folio_test_ksm(folio)) {
-				folio_unlock(folio);
-				continue;
-			}
-
-			/*
-			 * If we have a large folio at this point, we know it is
-			 * fully mapped so if its mapcount is the same as its
-			 * number of pages, it must be exclusive.
-			 */
-			if (folio_mapcount(folio) != folio_nr_pages(folio)) {
-				folio_unlock(folio);
-				continue;
-			}
+		if (folio_mapcount(folio) != folio_nr_pages(folio))
+			continue;
+		if (!folio_trylock(folio))
+			continue;
+		/*
+		 * KSM folios can gain new mappings at any time, so we must
+		 * not clear their dirty flag.
+		 */
+		if (folio_test_ksm(folio)) {
+			folio_unlock(folio);
+			continue;
+		}
 
-			if (folio_test_swapcache(folio) &&
-			    !folio_free_swap(folio)) {
-				folio_unlock(folio);
-				continue;
-			}
+		/*
+		 * If we have a large folio at this point, we know it is
+		 * fully mapped so if its mapcount is the same as its
+		 * number of pages, it must be exclusive.
+		 */
+		if (folio_mapcount(folio) != folio_nr_pages(folio)) {
+			folio_unlock(folio);
+			continue;
+		}
 
-			folio_clear_dirty(folio);
+		if (folio_test_swapcache(folio) && !folio_free_swap(folio)) {
 			folio_unlock(folio);
+			continue;
 		}
 
+		folio_clear_dirty(folio);
+		folio_unlock(folio);
+
 		if (pte_young(ptent) || pte_dirty(ptent)) {
 			clear_young_dirty_ptes(vma, addr, pte, nr, cydp_flags);
 			tlb_remove_tlb_entries(tlb, pte, nr, addr);


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

-- 
Cheers,

David

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

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

Thread overview: 2+ messages (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
2026-11-17  8:55 ` 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®