mm: userfaultfd: correct dirty flags set for both present and swap pte
Subsystems (2)
RFC PATCH 1/1 mm: userfaultfd: correct dirty flags set for both present and swap pte
From: Barry Song <[email protected]>
As David pointed out, what truly matters for mremap and userfaultfd move operations is the soft dirty bit. The current comment and implementation—which always sets the dirty bit for present PTEs and fails to set the soft dirty bit for swap PTEs—are incorrect. This patch updates the behavior to correctly set the soft dirty bit for both present and swap PTEs in accordance with mremap.
--- mm/userfaultfd.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
mm/userfaultfd.c +10 -2
@@ -1064,8 +1064,13 @@ static int move_present_pte(struct mm_struct *mm, src_folio->index =linear_page_index (dst_vma, dst_addr); orig_dst_pte =folio_mk_pte (src_folio, dst_vma->vm_page_prot); -/* Follow mremap() behavior and treat the entry dirty after the move */ - orig_dst_pte =pte_mkwrite (pte_mkdirty (orig_dst_pte), dst_vma); +/* Set soft dirty bit so userspace can notice the pte was moved */ +#ifdef CONFIG_MEM_SOFT_DIRTY + orig_dst_pte =pte_mksoft_dirty (orig_dst_pte); +#endif +if (pte_dirty (orig_src_pte)) + orig_dst_pte =pte_mkdirty (orig_dst_pte); + orig_dst_pte =pte_mkwrite (orig_dst_pte, dst_vma);set_pte_at (mm, dst_addr, dst_pte, orig_dst_pte); out:
@@ -1100,6 +1105,9 @@ static int move_swap_pte(struct mm_struct *mm, struct vm_area_struct *dst_vma, } orig_src_pte =ptep_get_and_clear (mm, src_addr, src_pte); +#ifdef CONFIG_MEM_SOFT_DIRTY + orig_src_pte =pte_swp_mksoft_dirty (orig_src_pte); +#endif set_pte_at (mm, dst_addr, dst_pte, orig_src_pte);double_pt_unlock (dst_ptl, src_ptl);
Signature · 1 line
2.39.3 (Apple Git-146)
Quoted 40 lines · Barry Song
From: Barry Song <[email protected]>
As David pointed out, what truly matters for mremap and userfaultfd move operations is the soft dirty bit. The current comment and implementation—which always sets the dirty bit for present PTEs and fails to set the soft dirty bit for swap PTEs—are incorrect. This patch updates the behavior to correctly set the soft dirty bit for both present and swap PTEs in accordance with mremap.
Reported-by David Hildenbrand <[email protected]>Cc Peter Xu <[email protected]>Cc Suren Baghdasaryan <[email protected]>Cc Lokesh Gidra <[email protected]>Signed-off-by Barry Song <[email protected]>--- mm/userfaultfd.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)mm/userfaultfd.c +7
-2@@ -1064,8 +1064,13 @@ static int move_present_pte(struct mm_struct *mm, src_folio->index =linear_page_index (dst_vma, dst_addr); orig_dst_pte =folio_mk_pte (src_folio, dst_vma->vm_page_prot); -/* Follow mremap() behavior and treat the entry dirty after the move */ - orig_dst_pte =pte_mkwrite (pte_mkdirty (orig_dst_pte), dst_vma); +/* Set soft dirty bit so userspace can notice the pte was moved */ +#ifdef CONFIG_MEM_SOFT_DIRTY + orig_dst_pte =pte_mksoft_dirty (orig_dst_pte); +#endif +if (pte_dirty (orig_src_pte)) + orig_dst_pte =pte_mkdirty (orig_dst_pte); + orig_dst_pte =pte_mkwrite (orig_dst_pte, dst_vma);set_pte_at (mm, dst_addr, dst_pte, orig_dst_pte); out:
mm/userfaultfd.c static int move_swap_pte(struct mm_struct *mm, struct vm_area_struct *dst_vma, · line 1112
Quoted 8 lines
mm/userfaultfd.c +3
-0@@ -1100,6 +1105,9 @@ static int move_swap_pte(struct mm_struct *mm, struct vm_area_struct *dst_vma, } orig_src_pte =ptep_get_and_clear (mm, src_addr, src_pte); +#ifdef CONFIG_MEM_SOFT_DIRTY + orig_src_pte =pte_swp_mksoft_dirty (orig_src_pte); +#endif set_pte_at (mm, dst_addr, dst_pte, orig_src_pte);double_pt_unlock (dst_ptl, src_ptl);
Yeah, I think that should be the right thing to do.
Signature · 3 lines
Cheers, David / dhildenb
Quoted 9 lines · Barry Song
From: Barry Song <[email protected]>
As David pointed out, what truly matters for mremap and userfaultfd move operations is the soft dirty bit. The current comment and implementation—which always sets the dirty bit for present PTEs and fails to set the soft dirty bit for swap PTEs—are incorrect. This patch updates the behavior to correctly set the soft dirty bit for both present and swap PTEs in accordance with mremap.
Quoted 6 lines
Reported-by David Hildenbrand <[email protected]>Cc Peter Xu <[email protected]>Cc Suren Baghdasaryan <[email protected]>Cc Lokesh Gidra <[email protected]>Signed-off-by Barry Song <[email protected]>
Signature · 1 line
Peter Xu
Thanks Barry for fixing this.
Quoted 8 lines · Peter Xu
Quoted 6 lines · Barry Song
From: Barry Song <[email protected]>
As David pointed out, what truly matters for mremap and userfaultfd move operations is the soft dirty bit. The current comment and implementation—which always sets the dirty bit for present PTEs and fails to set the soft dirty bit for swap PTEs—are incorrect.
Can you please briefly describe the consequences of not setting the soft-dirty bit? I'm wondering if it needs to be backported as a fix?
Quoted 15 lines
Quoted 9 lines
This patch updates the behavior to correctly set the soft dirty bit for both present and swap PTEs in accordance with mremap.
Reported-by David Hildenbrand <[email protected]>Cc Peter Xu <[email protected]>Cc Suren Baghdasaryan <[email protected]>Cc Lokesh Gidra <[email protected]>Signed-off-by Barry Song <[email protected]>Acked-by Peter Xu <[email protected]>--
Peter Xu
Quoted 8 lines · Lokesh Gidra
Thanks Barry for fixing this.
Quoted 4 lines · Peter Xu
Quoted 2 lines · Barry Song
From: Barry Song <[email protected]>
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
As David pointed out, what truly matters for mremap and userfaultfd move operations is the soft dirty bit. The current comment and implementation—which always sets the dirty bit for present PTEs and fails to set the soft dirty bit for swap PTEs—are incorrect.
Can you please briefly describe the consequences of not setting the soft-dirty bit? I'm wondering if it needs to be backported as a fix?
As I understand it, this could break features like Checkpoint-Restore in Userspace (CRIU), which relies on tracking memory changes to create incremental dumps. While Android may not currently have a real-world use case for this, it would still be beneficial to backport the fix in a general way.
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
This patch updates the behavior to correctly set the soft dirty bit for both present and swap PTEs in accordance with mremap.
Reported-by David Hildenbrand <[email protected]>Cc Peter Xu <[email protected]>Cc Suren Baghdasaryan <[email protected]>Cc Lokesh Gidra <[email protected]>Signed-off-by Barry Song <[email protected]>
Quoted 4 lines
Quoted 4 lines
Acked-by Peter Xu <[email protected]>--
Peter Xu
Thanks
Barry
Quoted 15 lines · Barry Song
Quoted 13 lines · Lokesh Gidra
Thanks Barry for fixing this.
Quoted 8 lines · Peter Xu
Quoted 6 lines · Barry Song
From: Barry Song <[email protected]>
As David pointed out, what truly matters for mremap and userfaultfd move operations is the soft dirty bit. The current comment and implementation—which always sets the dirty bit for present PTEs and fails to set the soft dirty bit for swap PTEs—are incorrect.
Quoted 9 lines
Quoted 2 lines
Can you please briefly describe the consequences of not setting the soft-dirty bit? I'm wondering if it needs to be backported as a fix?
As I understand it, this could break features like Checkpoint-Restore in Userspace (CRIU), which relies on tracking memory changes to create incremental dumps. While Android may not currently have a real-world use case for this, it would still be beneficial to backport the fix in a general way.
Makes sense. Thanks for clarifying.
Quoted 18 lines
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
This patch updates the behavior to correctly set the soft dirty bit for both present and swap PTEs in accordance with mremap.
Reported-by David Hildenbrand <[email protected]>Cc Peter Xu <[email protected]>Cc Suren Baghdasaryan <[email protected]>Cc Lokesh Gidra <[email protected]>Signed-off-by Barry Song <[email protected]>Quoted 4 lines
Quoted 4 lines
Acked-by Peter Xu <[email protected]>--
Peter XuThanks
Barry
Suren Baghdasaryan
Reviewed-byQuoted 11 lines · Peter Xu
Quoted 9 lines · Barry Song
From: Barry Song <[email protected]>
As David pointed out, what truly matters for mremap and userfaultfd move operations is the soft dirty bit. The current comment and implementation—which always sets the dirty bit for present PTEs and fails to set the soft dirty bit for swap PTEs—are incorrect. This patch updates the behavior to correctly set the soft dirty bit for both present and swap PTEs in accordance with mremap.
Quoted 8 lines
Quoted 6 lines
Reported-by David Hildenbrand <[email protected]>Cc Peter Xu <[email protected]>Cc Suren Baghdasaryan <[email protected]>Cc Lokesh Gidra <[email protected]>Signed-off-by Barry Song <[email protected]>Acked-by Peter Xu <[email protected]>
The function was introduced in 6.8. We should probably CC stable as well.
Quoted 4 lines
--
Peter Xu