mm/userfaultfd: recheck WP registration in the hugetlb UFFDIO_COPY retry
Subsystems (5)
v2 cover mm/userfaultfd: recheck WP registration in the hugetlb UFFDIO_COPY retry
The urgent fixes made hugetlb_fault() tolerate uffd bits left in VMAs that are no longer registered for them, and asked for follow-ups that remove the sources [1]. This is one: a hugetlb UFFDIO_COPY_MODE_WP copy that has to fault in its source page doesn't recheck the WP registration before retrying, so re-registering the range without WP in that window leaves a uffd-wp PTE behind.
Patch 1 adds the missing check. Patch 2 adds a uffd-unit-tests case for it, which fails on hugetlb without patch 1.
The other source 6/10 of [1] mentions, UFFDIO_COPY in MODE_WP racing with UFFDIO_UNREGISTER [2], is closed by "mm/userfaultfd: write-lock the VMA before waking waiters when unregistering" [3].
Based on mm-unstable.
Tested on x86_64 in QEMU.
[1] https://lore.kernel.org/[email protected]
[2] https://lore.kernel.org/[email protected]
[3] https://lore.kernel.org/[email protected] (v1, now 23/47 in v3)
Changes in v2:
- 1/2: drop the leftover sentence and the non-hugetlb reference from the commit message, add Mike's Reviewed-by
- 2/2: "all memory types" in the commit message, rename copy to result
- rebase on mm-unstable
v1: https://lore.kernel.org/[email protected]
Jose A. Perez de Azpillaga (2):
mm/userfaultfd: recheck WP registration in the hugetlb UFFDIO_COPY
retry
selftests/mm: add a uffd test for re-registering during UFFDIO_COPY
mm/userfaultfd.c | 4 + tools/testing/selftests/mm/uffd-unit-tests.c | 105 +++++++++++++++++++ 2 files changed, 109 insertions(+)
base-commit: 34f88a114888c9dba49e45f2b0c2a3e83c4c871c
Signature · 1 line
2.56.0
PATCH v2 1/2 mm/userfaultfd: recheck WP registration in the hugetlb UFFDIO_COPY retry
mfill_atomic_hugetlb() drops its locks to copy the source page with page faults enabled and then looks the VMA up again. The retry doesn't check that the VMA is still registered for WP, so if the range is re-registered without WP in that window, a UFFDIO_COPY_MODE_WP copy installs a uffd-wp PTE in a VMA without VM_UFFD_WP.
Recheck VM_UFFD_WP for UFFDIO_COPY_MODE_WP copies after looking the VMA up again, and fail with -EINVAL if it is gone.
---
Changes in v2:
- drop the leftover sentence and the non-hugetlb reference from the commit message
- add Mike's Reviewed-by
v1: https://lore.kernel.org/[email protected]
mm/userfaultfd.c | 4 ++++ 1 file changed, 4 insertions(+)
mm/userfaultfd.c +4 -0
@@ -810,6 +810,10 @@ static __always_inline ssize_t mfill_atomic_hugetlb(if (vma_hpagesize !=vma_kernel_pagesize (dst_vma)) goto out_unlock_vma; +/* The range may have been re-registered without WP meanwhile. */ +if ((flags &MFILL_ATOMIC_WP ) && !(dst_vma->vm_flags &VM_UFFD_WP )) + goto out_unlock_vma; +/* * If memory mappings are changing because of non-cooperative * operation (e.g. mremap) running in parallel, bail out and
Signature · 1 line
2.56.0
PATCH v2 2/2 selftests/mm: add a uffd test for re-registering during UFFDIO_COPY
In reply to Jose A. Perez de AzpillagaRe-register the destination without WP while a UFFDIO_COPY_MODE_WP copy waits for its source page, then check that the copy fails with -EINVAL and leaves no uffd-wp bit behind. This covers all memory types.
---
Changes in v2:
- "all memory types" in the commit message
- rename copy to result
v1: https://lore.kernel.org/[email protected]
tools/testing/selftests/mm/uffd-unit-tests.c | 105 +++++++++++++++++++ 1 file changed, 105 insertions(+)
tools/testing/selftests/mm/uffd-unit-tests.c +105 -0
@@ -375,6 +375,104 @@ static void uffd_wp_unpopulated_test(uffd_global_test_opts_t *gopts, uffd_test_auffd_test_pass (); } +struct wp_copy_args { +uffd_global_test_opts_t *gopts; +char *src; +__s64 result; +}; + +static void *wp_copy_thread (void *data) +{ +struct wp_copy_args *args = data; +struct uffdio_copy copy = { + .dst = (unsigned long )args->gopts->area_dst, + .src = (unsigned long )args->src, + .len = args->gopts->page_size, + .mode =UFFDIO_COPY_MODE_WP , + }; + +ioctl (args->gopts->uffd,UFFDIO_COPY , ©); + args->result = copy.copy; + +return NULL ; +} + +/* +* UFFDIO_COPY drops its locks to fault in the source page. Re-register the +* destination without WP while it waits there: the MODE_WP copy must then +* fail instead of leaving a uffd-wp bit in a VMA that no longer has WP. +*/ +static void uffd_wp_copy_reregister_test (uffd_global_test_opts_t *gopts, +uffd_test_args_t *args) +{ +struct uffdio_api api = { .api =UFFD_API }; +struct wp_copy_args copy_args = { .gopts = gopts }; +unsigned long len = gopts->page_size; +struct uffdio_zeropage zeropage; +struct uffd_msg msg; +struct pollfd pfd; +pthread_t thread; +uint64_t value; +int pagemap_fd; +int src_uffd; + +/* The source fault comes from the kernel, so no UFFD_USER_MODE_ONLY */ + src_uffd =uffd_open (O_CLOEXEC ); +if (src_uffd <0 ) { +uffd_test_skip ("cannot open a kernel-mode userfaultfd" ); +return ; + } +if (ioctl (src_uffd,UFFDIO_API , &api)) +err ("UFFDIO_API failed" ); + + copy_args.src =mmap (NULL , len,PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +if (copy_args.src ==MAP_FAILED ) +err ("mmap() failed" ); + +if (uffd_register (src_uffd, copy_args.src, len, true, false, false)) +err ("register src failed" ); +if (uffd_register (gopts->uffd, gopts->area_dst, len, true, true, false)) +err ("register dst failed" ); + +if (pthread_create (&thread,NULL , wp_copy_thread, ©_args)) +err ("pthread_create() failed" ); + +/* Wait until UFFDIO_COPY blocks on the source page */ + pfd = (struct pollfd ){ .fd = src_uffd, .events =POLLIN }; +if (poll (&pfd,1 ,10000 ) !=1 ) +err ("no fault on the source page" ); +if (read (src_uffd, &msg,sizeof (msg)) !=sizeof (msg)) +err ("read() failed" ); + +if (uffd_unregister (gopts->uffd, gopts->area_dst, len)) +err ("unregister dst failed" ); +if (uffd_register (gopts->uffd, gopts->area_dst, len, true, false, false)) +err ("re-register dst failed" ); + + zeropage.range.start = (unsigned long )copy_args.src; + zeropage.range.len = len; + zeropage.mode =0 ; +if (ioctl (src_uffd,UFFDIO_ZEROPAGE , &zeropage)) +err ("UFFDIO_ZEROPAGE failed" ); + +if (pthread_join (thread,NULL )) +err ("pthread_join() failed" ); + +if (copy_args.result != -EINVAL ) +err ("UFFDIO_COPY returned %lld, expected %d" , + (long long )copy_args.result, -EINVAL ); + + pagemap_fd =pagemap_open (); + value =pagemap_get_entry (pagemap_fd, gopts->area_dst); +pagemap_check_wp (value, false); +close (pagemap_fd); + +munmap (copy_args.src, len); +close (src_uffd); +uffd_test_pass (); +} +static void uffd_wp_fork_test_common (uffd_global_test_opts_t *gopts,uffd_test_args_t *args,bool with_event) {
@@ -2403,6 +2501,13 @@ uffd_test_case_t uffd_tests[] = { .uffd_feature_required = UFFD_FEATURE_PAGEFAULT_FLAG_WP | UFFD_FEATURE_WP_UNPOPULATED, }, + { + .name = "wp-copy-reregister", + .uffd_fn = uffd_wp_copy_reregister_test, + .mem_targets = MEM_ALL, + .uffd_feature_required = UFFD_FEATURE_PAGEFAULT_FLAG_WP | + UFFD_FEATURE_WP_HUGETLBFS_SHMEM, + }, { .name = "minor", .uffd_fn = uffd_minor_test,
Signature · 1 line
2.56.0
Re: [PATCH v2 2/2] selftests/mm: add a uffd test for re-registering during UFFDIO_COPY
Quoted 5 lines · Jose A. Perez de Azpillaga
Re-register the destination without WP while a
UFFDIO_COPY_MODE_WPcopy waits for its source page, then check that the copy fails with-EINVALand leaves no uffd-wp bit behind. This covers all memory types.Signed-off-by Jose A. Perez de Azpillaga <[email protected]>
Quoted 136 lines
---
Changes in v2:
- "all memory types" in the commit message
- rename copy to resultv1: https://lore.kernel.org/[email protected]
tools/testing/selftests/mm/uffd-unit-tests.c | 105 +++++++++++++++++++ 1 file changed, 105 insertions(+)tools/testing/selftests/mm/uffd-unit-tests.c +105
-0@@ -375,6 +375,104 @@ static void uffd_wp_unpopulated_test(uffd_global_test_opts_t *gopts, uffd_test_auffd_test_pass (); } +struct wp_copy_args { +uffd_global_test_opts_t *gopts; +char *src; +__s64 result; +}; + +static void *wp_copy_thread (void *data) +{ +struct wp_copy_args *args = data; +struct uffdio_copy copy = { + .dst = (unsigned long )args->gopts->area_dst, + .src = (unsigned long )args->src, + .len = args->gopts->page_size, + .mode =UFFDIO_COPY_MODE_WP , + }; + +ioctl (args->gopts->uffd,UFFDIO_COPY , ©); + args->result = copy.copy; + +return NULL ; +} + +/* +* UFFDIO_COPY drops its locks to fault in the source page. Re-register the +* destination without WP while it waits there: the MODE_WP copy must then +* fail instead of leaving a uffd-wp bit in a VMA that no longer has WP. +*/ +static void uffd_wp_copy_reregister_test (uffd_global_test_opts_t *gopts, +uffd_test_args_t *args) +{ +struct uffdio_api api = { .api =UFFD_API }; +struct wp_copy_args copy_args = { .gopts = gopts }; +unsigned long len = gopts->page_size; +struct uffdio_zeropage zeropage; +struct uffd_msg msg; +struct pollfd pfd; +pthread_t thread; +uint64_t value; +int pagemap_fd; +int src_uffd; + +/* The source fault comes from the kernel, so no UFFD_USER_MODE_ONLY */ + src_uffd =uffd_open (O_CLOEXEC ); +if (src_uffd <0 ) { +uffd_test_skip ("cannot open a kernel-mode userfaultfd" ); +return ; + } +if (ioctl (src_uffd,UFFDIO_API , &api)) +err ("UFFDIO_API failed" ); + + copy_args.src =mmap (NULL , len,PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +if (copy_args.src ==MAP_FAILED ) +err ("mmap() failed" ); + +if (uffd_register (src_uffd, copy_args.src, len, true, false, false)) +err ("register src failed" ); +if (uffd_register (gopts->uffd, gopts->area_dst, len, true, true, false)) +err ("register dst failed" ); + +if (pthread_create (&thread,NULL , wp_copy_thread, ©_args)) +err ("pthread_create() failed" ); + +/* Wait until UFFDIO_COPY blocks on the source page */ + pfd = (struct pollfd ){ .fd = src_uffd, .events =POLLIN }; +if (poll (&pfd,1 ,10000 ) !=1 ) +err ("no fault on the source page" ); +if (read (src_uffd, &msg,sizeof (msg)) !=sizeof (msg)) +err ("read() failed" ); + +if (uffd_unregister (gopts->uffd, gopts->area_dst, len)) +err ("unregister dst failed" ); +if (uffd_register (gopts->uffd, gopts->area_dst, len, true, false, false)) +err ("re-register dst failed" ); + + zeropage.range.start = (unsigned long )copy_args.src; + zeropage.range.len = len; + zeropage.mode =0 ; +if (ioctl (src_uffd,UFFDIO_ZEROPAGE , &zeropage)) +err ("UFFDIO_ZEROPAGE failed" ); + +if (pthread_join (thread,NULL )) +err ("pthread_join() failed" ); + +if (copy_args.result != -EINVAL ) +err ("UFFDIO_COPY returned %lld, expected %d" , + (long long )copy_args.result, -EINVAL ); + + pagemap_fd =pagemap_open (); + value =pagemap_get_entry (pagemap_fd, gopts->area_dst); +pagemap_check_wp (value, false); +close (pagemap_fd); + +munmap (copy_args.src, len); +close (src_uffd); +uffd_test_pass (); +} +static void uffd_wp_fork_test_common (uffd_global_test_opts_t *gopts,uffd_test_args_t *args,bool with_event) {@@ -2403,6 +2501,13 @@ uffd_test_case_t uffd_tests[] = { .uffd_feature_required = UFFD_FEATURE_PAGEFAULT_FLAG_WP | UFFD_FEATURE_WP_UNPOPULATED, }, + { + .name = "wp-copy-reregister", + .uffd_fn = uffd_wp_copy_reregister_test, + .mem_targets = MEM_ALL, + .uffd_feature_required = UFFD_FEATURE_PAGEFAULT_FLAG_WP | + UFFD_FEATURE_WP_HUGETLBFS_SHMEM, + }, { .name = "minor", .uffd_fn = uffd_minor_test,Signature · 1 line
2.56.0
Signature · 2 lines
Sincerely yours, Mike.
[email protected]
AI reviewRe: [PATCH v2 2/2] selftests/mm: add a uffd test for re-registering during UFFDIO_COPY
In reply to Jose A. Perez de AzpillagaThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] selftests/mm: poll() returns POLLERR in uffd_wp_copy_reregister_test() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] selftests/mm: poll() returns POLLERR in uffd_wp_copy_reregister_test() --
commit 58286e73577bfa5312c30b5cdc10c8a436006bfb
Author: Jose A. Perez de Azpillaga <[email protected]>
selftests/mm: add a uffd test for re-registering during UFFDIO_COPY
This commit adds the wp-copy-reregister selftest to verify that re-registering a destination VMA without write-protection while UFFDIO_COPY with UFFDIO_COPY_MODE_WP is waiting on a source page fault fails with -EINVAL and does not leave a uffd-wp bit behind.
Quoted 5 lines
tools/testing/selftests/mm/uffd-unit-tests.c +0
-0@@ -375,6 +375,104 @@ static void uffd_wp_unpopulated_test(uffd_global_test_opts_t *gopts, uffd_test_a
[ ... ]
Quoted 32 lines
tools/testing/selftests/mm/uffd-unit-tests.c +32
-0+static void uffd_wp_copy_reregister_test (uffd_global_test_opts_t *gopts, +uffd_test_args_t *args) +{ +struct uffdio_api api = { .api =UFFD_API }; +struct wp_copy_args copy_args = { .gopts = gopts }; +unsigned long len = gopts->page_size; +struct uffdio_zeropage zeropage; +struct uffd_msg msg; +struct pollfd pfd; +pthread_t thread; +uint64_t value; +int pagemap_fd; +int src_uffd; + +/* The source fault comes from the kernel, so no UFFD_USER_MODE_ONLY */ + src_uffd =uffd_open (O_CLOEXEC ); +if (src_uffd <0 ) { +uffd_test_skip ("cannot open a kernel-mode userfaultfd" ); +return ; + } +if (ioctl (src_uffd,UFFDIO_API , &api)) +err ("UFFDIO_API failed" ); + + copy_args.src =mmap (NULL , len,PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +if (copy_args.src ==MAP_FAILED ) +err ("mmap() failed" ); + +if (uffd_register (src_uffd, copy_args.src, len, true, false, false)) +err ("register src failed" ); +if (uffd_register (gopts->uffd, gopts->area_dst, len, true, true, false)) +err ("register dst failed" );
tools/testing/selftests/mm/uffd-unit-tests.c static void uffd_wp_unpopulated_test(uffd_global_test_opts_t *gopts, uffd_test_a · line 446
Quoted 10 lines
tools/testing/selftests/mm/uffd-unit-tests.c +10
-0+ +if (pthread_create (&thread,NULL , wp_copy_thread, ©_args)) +err ("pthread_create() failed" ); + +/* Wait until UFFDIO_COPY blocks on the source page */ + pfd = (struct pollfd ){ .fd = src_uffd, .events =POLLIN }; +if (poll (&pfd,1 ,10000 ) !=1 ) +err ("no fault on the source page" ); +if (read (src_uffd, &msg,sizeof (msg)) !=sizeof (msg)) +err ("read() failed" );
[Severity: Medium]
In uffd_wp_copy_reregister_test(), src_uffd is opened with uffd_open(O_CLOEXEC) without O_NONBLOCK (unlike UFFD_FLAGS, which includes O_CLOEXEC | O_NONBLOCK).
In the kernel, mm/userfaultfd.c:userfaultfd_poll() requires O_NONBLOCK to be set on the file descriptor:
if (unlikely(!(file->f_flags & O_NONBLOCK))) return EPOLLERR;
Because POLLERR is returned in pfd.revents regardless of pfd.events, poll(&pfd, 1, 10000) immediately returns 1 with pfd.revents == POLLERR without waiting for a page fault. The 10-second timeout is bypassed, and if no fault occurs on the source page, the subsequent read() will block indefinitely.
Should src_uffd be opened with O_CLOEXEC | O_NONBLOCK (or UFFD_FLAGS & ~UFFD_USER_MODE_ONLY), and should pfd.revents also be checked for POLLIN?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Andrew Morton
maintainerQuoted 7 lines · On Mon, 2 Nov 2026 09:11:29 +0100 "Jose A. Perez de Azpillaga"…
The urgent fixes made
hugetlb_fault()tolerate uffd bits left in VMAs that are no longer registered for them, and asked for follow-ups that remove the sources [1]. This is one: a hugetlbUFFDIO_COPY_MODE_WPcopy that has to fault in its source page doesn't recheck the WP registration before retrying, so re-registering the range without WP in that window leaves a uffd-wp PTE behind.
Quoted 6 lines
Patch 1 adds the missing check. Patch 2 adds a uffd-unit-tests case for it, which fails on hugetlb without patch 1.
The other source 6/10 of [1] mentions,
UFFDIO_COPYinMODE_WPracing withUFFDIO_UNREGISTER[2], is closed by "mm/userfaultfd: write-lock the VMA before waking waiters when unregistering" [3].
Thanks.
Sashiko isn't entirely happy:
https://sashiko.dev/#/patchset/[email protected]
Also, for those who believe uffd is now defect free:
: Separately, mwriteprotect_range() checks the WP flag but not which : userfaultfd owns the VMA. With two userfaultfds in one mm, unprotecting : B's range through A can clear B's WP but wake A's queues, leaving B's : writer asleep. mrwprotect_range() has the same ownership-check omission. : This is older and deserves a separate fix.