Skip to contentSkip to conversation

mm/userfaultfd: clear uffd-wp PTE state when re-registering without WP

PATCH In review Jianhui Zhou 3 participants 4 messages Jun 1 – Aug 9

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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.

Fixes f45ec5ff16a7 ("userfaultfd: wp: support swap and page migration")
Reported-by [email protected]
---
 mm/userfaultfd.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
mm/userfaultfd.c +9 -1
index 180bad42fc79..dc0b3eba768b 100644
@@ -2153,13 +2153,21 @@ 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);

base-commit: e43ffb69e0438cddd72aaa30898b4dc446f664f8

Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 14 lines · On Mon, 1 Jun 2026 16:26:09 +0800 Jianhui Zhou <[email protected]> wrote:

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)
Quoted 7 lines

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.

Thanks.

Quoted 3 lines
Fixes f45ec5ff16a7 ("userfaultfd: wp: support swap and page migration")
Reported-by [email protected]

So we have a WARN which can be triggered by unprivileged userspace.

The patch arrived during -rc6 when people were reviewing and testing material for the next merge window, rather than looking at new changes.

Quoted 15 lines
mm/userfaultfd.c +2 -1
@@ -2153,13 +2153,21 @@ 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);
 
+		/*

mm/userfaultfd.c int userfaultfd_register_range(struct userfaultfd_ctx *ctx, · line 2173

Quoted 10 lines
mm/userfaultfd.c +7 -0
+		 * 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);

I'll queue this for 7.3-rc1, with a cc:stable. f45ec5ff16a7 was 6 years ago, so there's no rush here.

I don't know if this is the best fix, so I'll await reviewer input before taking it any further.

maintainer
#2 lore raw
In reply to Jianhui Zhou
Quoted 6 lines · Jianhui Zhou

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.

Quoted 7 lines

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)

Please explain where the check is triggered and what are the effects.

Quoted 21 lines

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.

Fixes f45ec5ff16a7 ("userfaultfd: wp: support swap and page migration")
Reported-by [email protected]
---
 mm/userfaultfd.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
mm/userfaultfd.c +0 -0
index 180bad42fc79..dc0b3eba768b 100644

mm/userfaultfd.c int userfaultfd_register_range(struct userfaultfd_ctx *ctx, · line 2156

Quoted 5 lines
mm/userfaultfd.c +1 -1
@@ -2153,13 +2153,21 @@ 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)

Please use new VMA flag APIs.

Quoted 22 lines
mm/userfaultfd.c +8 -0
 			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);

base-commit: e43ffb69e0438cddd72aaa30898b4dc446f664f8

Signature · 1 line
2.43.0
Signature · 2 lines
Sincerely yours,
Mike.
maintainer
#3 lore raw
In reply to Jianhui Zhou
Quoted 14 lines · Jianhui Zhou

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)
Quoted 7 lines

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.

The recent RWP changes prevent re-registering to the same uffd with a mode that clears VM_UFFD_WP or VM_UFFD_RWP. See commit 6eab8f2cc646 ("userfaultfd: add UFFDIO_REGISTER_MODE_RWP and UFFDIO_RWPROTECT plumbing") in the mm tree.

Andrew, can you please drop this patch from mm-unstable?

Quoted 7 lines
Fixes f45ec5ff16a7 ("userfaultfd: wp: support swap and page migration")
Reported-by [email protected]
---
 mm/userfaultfd.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
Signature · 2 lines
Sincerely yours,
Mike.