mm/memory: mark unshare copies dirty to fix MADV_FREE data loss
Subsystems (3)
PATCH v2 1/1 mm/memory: mark unshare copies dirty to fix MADV_FREE data loss
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.
---
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/[email protected]/
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(+)
mm/memory.c +3 -0
@@ -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))
On 11/17/26 11:06, Daniel Wade wrote:
Please don't send out stuff that fast, especially when the discussion is not over yet.
Quoted 9 lines
wp_page_copy()only callspte_mkdirty()for write faults, so the copy it makes forFAULT_FLAG_UNSHAREcomes 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_FREEin one of them marks the folio lazyfree and reclaim frees it. The other process then reads zeroes.
Quoted 5 lines
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_mapscount an unshare copy asPrivate_DirtyorShared_Dirtyrather than_Clean.
numa_maps still doesn't expose Private_Dirty or Shared_Dirty. So this should probably be be
Side effect: smaps counts an unshare copy as Private_Dirty or Shared_Dirty, and numa_maps indicates it as dirty.
Quoted 26 lines
Fixes c89357e27f20 ("mm: support GUP-triggered unsharing of anonymous pages")Suggested-by David Hildenbrand (Arm) <[email protected]>Signed-off-by Daniel Wade <[email protected]>---
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/[email protected]/Tested on mainline
b3dfbde62cf1, x86-64 with KASAN andPROVE_LOCKING, as uid 65534. The test gives an unshare copy to a child, has the parentMADV_FREEit 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(+)mm/memory.c +0
-0@@ -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);
mm/memory.c static vm_fault_t wp_page_copy(struct vm_fault *vmf) · line 4140
Quoted 10 lines
mm/memory.c +3
-0entry =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))
Apart from that LGTM
I'll wait for more review before picking this up.
Signature · 3 lines
Cheers, David
Quoted 10 lines · Daniel Wade
wp_page_copy()only callspte_mkdirty()for write faults, so the copy it makes forFAULT_FLAG_UNSHAREcomes 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_FREEin one of them marks the folio lazyfree and reclaim frees it. The other process then reads zeroes.[...]
Applied, thanks!
tree: mm/core.git
branch: for-next-fixes
Applied patches will appear soon in mm-next, followed by linux-next.
Please report any outstanding bugs that were missed during review, so we can either drop the patch series or decide how to fix it up.
It is encouraged to provide Acked-bys, Reviewed-bys, and Tested-bys even though the patches have already been applied. If possible, patch trailers will be updated.
Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. Further, this is not a guarantee that the patches will go upstream. If in doubt, please check the listed branch.
[1/1] mm/memory: mark unshare copies dirty to fix MADV_FREE data loss
https://git.kernel.org/mm/core/c/1000fb7d
Signature · 3 lines
Cheers, David