From: David Hildenbrand <david@redhat.com>
To: Barry Song <21cnbao@gmail.com>,
akpm@linux-foundation.org, linux-mm@kvack.org
Cc: linux-kernel@vger.kernel.org, Barry Song <v-songbaohua@oppo.com>,
Peter Xu <peterx@redhat.com>,
Suren Baghdasaryan <surenb@google.com>,
Lokesh Gidra <lokeshgidra@google.com>
Subject: Re: [PATCH RFC] mm: userfaultfd: correct dirty flags set for both present and swap pte
Date: Thu, 8 May 2025 11:24:35 +0200 [thread overview]
Message-ID: <0e00e429-01d8-4504-8238-565e1bca5ad3@redhat.com> (raw)
In-Reply-To: <20250508090735.39756-1-21cnbao@gmail.com>
On 08.05.25 11:07, Barry Song wrote:
> From: Barry Song <v-songbaohua@oppo.com>
>
> 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 <david@redhat.com>
> Closes: https://lore.kernel.org/linux-mm/02f14ee1-923f-47e3-a994-4950afb9afcc@redhat.com/
> Cc: Peter Xu <peterx@redhat.com>
> Cc: Suren Baghdasaryan <surenb@google.com>
> Cc: Lokesh Gidra <lokeshgidra@google.com>
> Signed-off-by: Barry Song <v-songbaohua@oppo.com>
> ---
> mm/userfaultfd.c | 12 ++++++++++--
> 1 file changed, 10 insertions(+), 2 deletions(-)
>
> diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
> index e8ce92dc105f..bc473ad21202 100644
> --- a/mm/userfaultfd.c
> +++ b/mm/userfaultfd.c
> @@ -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);
Yeah, I think that should be the right thing to do.
--
Cheers,
David / dhildenb
next prev parent reply other threads:[~2025-05-08 9:24 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-08 9:07 Barry Song
2025-05-08 9:24 ` David Hildenbrand [this message]
2025-05-08 15:23 ` Peter Xu
2025-05-08 15:27 ` Lokesh Gidra
2025-05-08 21:36 ` Barry Song
2025-05-08 22:00 ` Lokesh Gidra
2025-05-08 15:33 ` Suren Baghdasaryan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=0e00e429-01d8-4504-8238-565e1bca5ad3@redhat.com \
--to=david@redhat.com \
--cc=21cnbao@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lokeshgidra@google.com \
--cc=peterx@redhat.com \
--cc=surenb@google.com \
--cc=v-songbaohua@oppo.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox