summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJianhui Zhou <jianhuizzzzz@gmail.com>2026-06-01 16:26:09 +0800
committerAndrew Morton <akpm@linux-foundation.org>2026-07-05 16:23:12 -0700
commitf435e5acc3ed67e8ed4187bd479e6fd1983ac823 (patch)
tree67aad6f4df5ae08444c9383d273f6902245a9e1a
parent49c2b25b7f15a44b7d0ab67cef07a138fab2bce3 (diff)
downloadlinux-next-f435e5acc3ed67e8ed4187bd479e6fd1983ac823.tar.gz
linux-next-f435e5acc3ed67e8ed4187bd479e6fd1983ac823.zip
mm/userfaultfd: clear uffd-wp PTE state when re-registering without WP
UFFDIO_REGISTER can be issued on a range that is already registered in the same userfaultfd context, replacing the VMA's userfaultfd tracking mode. For example, a range can be registered with UFFDIO_REGISTER_MODE_WP and later re-registered with UFFDIO_REGISTER_MODE_MISSING. When the second registration removes VM_UFFD_WP, the VMA flags are updated but existing uffd-wp state in page-table entries is left behind. That stale state can survive in swap PTEs. On swapin, do_swap_page() restores _PAGE_UFFD_WP from the swap PTE and can then install a writable PTE, triggering page_table_check: pte_uffd_wp(pte) && pte_write(pte) Handle removal of WP mode through UFFDIO_REGISTER the same way as UFFDIO_UNREGISTER: resolve the per-PTE uffd-wp state before dropping VM_UFFD_WP from the VMA. Also make the same-context fast path require an exact UFFD mode match. The old subset check treats MISSING|WP -> MISSING as a no-op, even though WP mode is being removed. Link: https://lore.kernel.org/20260601082609.170076-1-jianhuizzzzz@gmail.com Fixes: f45ec5ff16a7 ("userfaultfd: wp: support swap and page migration") Signed-off-by: Jianhui Zhou <jianhuizzzzz@gmail.com> Reported-by: syzbot+18d274a59b87cf80e86d@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=18d274a59b87cf80e86d Cc: Mike Rapoport <rppt@kernel.org> Cc: Peter Xu <peterx@redhat.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
-rw-r--r--mm/userfaultfd.c10
1 files changed, 9 insertions, 1 deletions
diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
index c3adedaaf7d5..33ef42d5a04e 100644
--- a/mm/userfaultfd.c
+++ b/mm/userfaultfd.c
@@ -2238,13 +2238,21 @@ static int userfaultfd_register_range(struct userfaultfd_ctx *ctx,
* userfaultfd and with the right tracking mode too.
*/
if (vma->vm_userfaultfd_ctx.ctx == ctx &&
- vma_test_all_mask(vma, vma_flags))
+ (vma->vm_flags & __VM_UFFD_FLAGS) == vm_flags)
goto skip;
if (vma->vm_start > start)
start = vma->vm_start;
vma_end = min(end, vma->vm_end);
+ /*
+ * Re-registering into the same userfaultfd can remove WP mode.
+ * Clear any per-PTE uffd-wp state before dropping VM_UFFD_WP,
+ * matching the UFFDIO_UNREGISTER cleanup semantics.
+ */
+ if (userfaultfd_wp(vma) && !(vm_flags & VM_UFFD_WP))
+ uffd_wp_range(vma, start, vma_end - start, false);
+
new_vma_flags = vma->flags;
vma_flags_clear_mask(&new_vma_flags, __VMA_UFFD_FLAGS);
vma_flags_set_mask(&new_vma_flags, vma_flags);