userfaultfd: clear the inherited uffd bit in move_swap_pte()
Subsystems (5)
v2 cover userfaultfd: clear the inherited uffd bit in move_swap_pte()
UFFDIO_MOVE on a swapped-out page installs the source PTE at the destination unchanged, so a uffd bit set on the source lands in a destination VMA that was never registered for write protection, and nothing clears it afterwards. Patch 1 clears the bit unless the destination is RWP-registered, which is what the present-page and zeropage move paths already do. Patch 2 adds the test that catches it.
v1: https://lore.kernel.org/all/[email protected]/
Changes in v2:
- patch 1: clear the bit unconditionally (Kiryl Shutsemau)
- patch 1: rewrite the changelog for readability (Mike Rapoport)
- add Assisted-by: LLM (Mike Rapoport)
x86_64 defconfig plus USERFAULTFD, TRANSPARENT_HUGEPAGE, GUP_TEST and a swap device, under QEMU, base 17e7b8eacf4c:
uffd-unit-tests before after move-swap-wp on anon not ok ok the other 114 unit tests ok ok uffd-wp-mremap, 38 tests ok ok
pagemap bit 57 at the destination before after swapped page, dst not armed set clear swapped page, dst WP-armed set clear swapped page, dst RWP-armed set set resident page, dst WP-armed clear clear
MADV_COLLAPSE over 2 MB at dst EINVAL 0
Donggeun Yoo (2):
userfaultfd: clear the inherited uffd bit in move_swap_pte() selftests/mm: add a test for UFFDIO_MOVE of a write-protected swap
entry
mm/userfaultfd.c | 1 + tools/testing/selftests/mm/uffd-unit-tests.c | 61 ++++++++++++++++++++ 2 files changed, 62 insertions(+)
Signature · 1 line
2.53.0
PATCH v2 1/2 userfaultfd: clear the inherited uffd bit in move_swap_pte()
UFFDIO_MOVE on a swapped-out page installs the source PTE at the destination unchanged, so a uffd bit set on the source lands in a destination VMA that was never registered for write protection. It can't be unset there, and THP collapse can't happen either, because a swap entry with the uffd bit set makes the scan bail.
I don't think it's intentional because for an unswapped page the bit doesn't come along, and I don't see why being swapped out should change that.
Clear the uffd bit on the moved swap entry unless the destination is RWP-registered.
--- mm/userfaultfd.c | 1 + 1 file changed, 1 insertion(+)
mm/userfaultfd.c +1 -0
@@ -1449,6 +1449,7 @@ 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);if (pgtable_supports_soft_dirty ()) orig_src_pte =pte_swp_mksoft_dirty (orig_src_pte); + orig_src_pte =pte_swp_clear_uffd (orig_src_pte);/* Re-arm RWP on the moved swap entry if dst_vma is RWP-registered. */ if (userfaultfd_rwp (dst_vma)) orig_src_pte =pte_swp_mkuffd (orig_src_pte);
Signature · 1 line
2.53.0
Andrew Morton
maintainerQuoted 6 lines · On Fri, 25 Sep 2026 13:29:06 +0900 Donggeun Yoo <[email protected]>…
UFFDIO_MOVEon a swapped-out page installs the source PTE at the destination unchanged, so a uffd bit set on the source lands in a destination VMA that was never registered for write protection. It can't be unset there, and THP collapse can't happen either, because a swap entry with the uffd bit set makes the scan bail.
Quoted 6 lines
I don't think it's intentional because for an unswapped page the bit doesn't come along, and I don't see why being swapped out should change that.
Clear the uffd bit on the moved swap entry unless the destination is RWP-registered.
Thanks, but...
When fixing a bug, please always fully describe the userspace-visible effects of that bug.
Quoted 2 lines
Fixes adef440691ba ("userfaultfd: UFFDIO_MOVE uABI")Cc <[email protected]>
Especially when proposing a backport.
Please review the first half-page ofDocumentation/process/stable-kernel-rules.rst
I don't mean "be reluctant to backport". I mean "when proposing a backport, explain *why*".
Quoted 1 line
Assisted-by LLM
Great! Please add a prompt to LLM's .md ensuring that future changelogs always provide this information. Please also share that update with the rest of your organization.
David Hildenbrand (Arm)
maintainerQuoted 12 lines · Andrew Morton
Quoted 10 lines · On Fri, 25 Sep 2026 13:29:06 +0900 Donggeun Yoo <[email protected]>…
UFFDIO_MOVEon a swapped-out page installs the source PTE at the destination unchanged, so a uffd bit set on the source lands in a destination VMA that was never registered for write protection. It can't be unset there, and THP collapse can't happen either, because a swap entry with the uffd bit set makes the scan bail.I don't think it's intentional because for an unswapped page the bit doesn't come along, and I don't see why being swapped out should change that.
Quoted 7 lines
Quoted 2 lines
Clear the uffd bit on the moved swap entry unless the destination is RWP-registered.
Thanks, but...
When fixing a bug, please always fully describe the userspace-visible effects of that bug.
It would also be helpful to describe if this was actually hit in practice so far. IOW, how relevant is this in practice.
Will this trigger a kernel splat later?
Someone would have to use UFFD_WP/UFFD_RWP with UFFDIO_MOVE, and have UFFD_WP/UFFD_RWP disabled on the destination vma ... and then work on a swap entry.
Code itself LGTM
Signature · 3 lines
Cheers, David
Quoted 2 lines · David Hildenbrand (Arm)
It would also be helpful to describe if this was actually hit in practice so far. IOW, how relevant is this in practice.
Not that I know of. An LLM found it reading the code, and I couldn't find a report of it on linux-mm or lkml.
Quoted 1 line
Will this trigger a kernel splat later?
Yes, with CONFIG_PAGE_TABLE_CHECK. When the moved page is faulted in at the destination, do_swap_page() copies the uffd bit to the present PTE and, because the destination isn't WP-registered, also makes it writable. A read fault is enough:
WARNING: mm/page_table_check.c:202 at __page_table_check_ptes_set+0x185/0x1e0 Call Trace: set_ptes+0x67/0xc0 do_swap_page+0x990/0xfe0 __handle_mm_fault+0x7d0/0xeb0 handle_mm_fault+0x9c/0x250 do_user_addr_fault+0x207/0x650 exc_page_fault+0x65/0x150 asm_exc_page_fault+0x26/0x30
That's on 7.3-rc4 in QEMU, from a WP- or RWP-armed source; with this patch applied it doesn't fire. I'll put it in the v3 changelog.
Thanks,
Donggeun
Quoted 2 lines · Andrew Morton
When fixing a bug, please always fully describe the userspace-visible effects of that bug.
Done in v3:
https://lore.kernel.org/all/[email protected]/
Quoted 2 lines
Great! Please add a prompt to LLM's .md ensuring that future changelogs always provide this information.
Added.
Thanks,
Donggeun
PATCH v2 2/2 selftests/mm: add a test for UFFDIO_MOVE of a write-protected swap entry
In reply to Donggeun YooMove a swapped-out page out of a write-protected area into a destination registered for missing faults only, and read pagemap bit 57 at the destination. The destination was never write-protected, so the bit must be clear.
--- tools/testing/selftests/mm/uffd-unit-tests.c | 61 ++++++++++++++++++++ 1 file changed, 61 insertions(+)
tools/testing/selftests/mm/uffd-unit-tests.c +61 -0
@@ -2037,6 +2037,59 @@ static void uffd_move_pmd_split_test(uffd_global_test_opts_t *gopts, uffd_test_a uffd_move_pmd_handle_fault); } +/* +* Moving a swapped-out page out of a write-protected area must not carry the +* uffd-wp bit into a destination that is not write-protect registered: such a +* bit is never cleared afterwards, so pagemap keeps reporting the page as +* uffd-tracked. +* +* Needs a swap device; skipped if MADV_PAGEOUT cannot evict the page. +*/ +static void uffd_move_swap_wp_test (uffd_global_test_opts_t *gopts, +uffd_test_args_t *targs) +{ +unsigned long page_size = gopts->page_size; +struct uffdio_move move = { }; +int pagemap_fd, i; + +if (uffd_register (gopts->uffd, gopts->area_src, page_size, + false, true, false)) +err ("register src failure" ); +if (uffd_register (gopts->uffd, gopts->area_dst, page_size, + true, false, false)) +err ("register dst failure" ); + +wp_range (gopts->uffd, (unsigned long )gopts->area_src, page_size, true); + + pagemap_fd =pagemap_open (); +for (i =0 ; i <100 ; i++) { +if (madvise (gopts->area_src, page_size,MADV_PAGEOUT )) +err ("MADV_PAGEOUT" ); +if (pagemap_is_swapped (pagemap_fd, gopts->area_src)) +break ; +usleep (10000 ); + } +if (!pagemap_is_swapped (pagemap_fd, gopts->area_src)) { +uffd_test_skip ("MADV_PAGEOUT did not swap the page; is swap enabled?" ); + goto out; + } + + move.dst = (unsigned long )gopts->area_dst; + move.src = (unsigned long )gopts->area_src; + move.len = page_size; +if (ioctl (gopts->uffd,UFFDIO_MOVE , &move)) +err ("UFFDIO_MOVE" ); + +if (pagemap_get_entry (pagemap_fd, gopts->area_dst) &PM_UFFD_WP ) +uffd_test_fail ("uffd-wp bit moved into an area that is not write-protected" ); +else +uffd_test_pass (); +out: +close (pagemap_fd); +uffd_unregister (gopts->uffd, gopts->area_src, page_size); +uffd_unregister (gopts->uffd, gopts->area_dst, page_size); +} +static bool uffdio_verify_results (const char *name,int ret,int error,long result) {
@@ -2359,6 +2412,14 @@ uffd_test_case_t uffd_tests[] = { .uffd_feature_required = UFFD_FEATURE_MOVE, .test_case_ops = &uffd_move_test_pmd_case_ops, }, + { + .name = "move-swap-wp", + .uffd_fn = uffd_move_swap_wp_test, + .mem_targets = MEM_ANON, + .uffd_feature_required = UFFD_FEATURE_MOVE | + UFFD_FEATURE_PAGEFAULT_FLAG_WP, + .test_case_ops = &uffd_move_test_case_ops, + }, { .name = "wp-fork", .uffd_fn = uffd_wp_fork_test,
Signature · 1 line
2.53.0
David Hildenbrand (Arm)
maintainerRe: [PATCH v2 2/2] selftests/mm: add a test for UFFDIO_MOVE of a write-protected swap entry
Quoted 29 lines · Donggeun Yoo
Move a swapped-out page out of a write-protected area into a destination registered for missing faults only, and read pagemap bit 57 at the destination. The destination was never write-protected, so the bit must be clear.
Assisted-by LLMSigned-off-by Donggeun Yoo <[email protected]>--- tools/testing/selftests/mm/uffd-unit-tests.c | 61 ++++++++++++++++++++ 1 file changed, 61 insertions(+)tools/testing/selftests/mm/uffd-unit-tests.c +10
-0@@ -2037,6 +2037,59 @@ static void uffd_move_pmd_split_test(uffd_global_test_opts_t *gopts, uffd_test_a uffd_move_pmd_handle_fault); } +/* +* Moving a swapped-out page out of a write-protected area must not carry the +* uffd-wp bit into a destination that is not write-protect registered: such a +* bit is never cleared afterwards, so pagemap keeps reporting the page as +* uffd-tracked. +* +* Needs a swap device; skipped if MADV_PAGEOUT cannot evict the page. +*/ +static void uffd_move_swap_wp_test (uffd_global_test_opts_t *gopts, +uffd_test_args_t *targs)
tools/testing/selftests/mm/uffd-unit-tests.c static void uffd_move_pmd_split_test(uffd_global_test_opts_t *gopts, uffd_test_a · line 2059
Quoted 10 lines
tools/testing/selftests/mm/uffd-unit-tests.c +10
-0+{ +unsigned long page_size = gopts->page_size; +struct uffdio_move move = { }; +int pagemap_fd, i; + +if (uffd_register (gopts->uffd, gopts->area_src, page_size, + false, true, false)) +err ("register src failure" ); +if (uffd_register (gopts->uffd, gopts->area_dst, page_size, + true, false, false))
Side note: this binary encoding of features is horrible.
tools/testing/selftests/mm/uffd-unit-tests.c static void uffd_move_pmd_split_test(uffd_global_test_opts_t *gopts, uffd_test_a · line 2071
Quoted 12 lines
tools/testing/selftests/mm/uffd-unit-tests.c +12
-0+err ("register dst failure" ); + +wp_range (gopts->uffd, (unsigned long )gopts->area_src, page_size, true); + + pagemap_fd =pagemap_open (); +for (i =0 ; i <100 ; i++) { +if (madvise (gopts->area_src, page_size,MADV_PAGEOUT )) +err ("MADV_PAGEOUT" ); +if (pagemap_is_swapped (pagemap_fd, gopts->area_src)) +break ; +usleep (10000 ); + }
Is this loop really required? I thought the page should just be unmapped from the page table. It might still be in the swapcache and under writeback, but that's irrelevant.
Quoted 26 lines
tools/testing/selftests/mm/uffd-unit-tests.c +21
-0+if (!pagemap_is_swapped (pagemap_fd, gopts->area_src)) { +uffd_test_skip ("MADV_PAGEOUT did not swap the page; is swap enabled?" ); + goto out; + } + + move.dst = (unsigned long )gopts->area_dst; + move.src = (unsigned long )gopts->area_src; + move.len = page_size; +if (ioctl (gopts->uffd,UFFDIO_MOVE , &move)) +err ("UFFDIO_MOVE" ); + +if (pagemap_get_entry (pagemap_fd, gopts->area_dst) &PM_UFFD_WP ) +uffd_test_fail ("uffd-wp bit moved into an area that is not write-protected" ); +else +uffd_test_pass (); +out: +close (pagemap_fd); +uffd_unregister (gopts->uffd, gopts->area_src, page_size); +uffd_unregister (gopts->uffd, gopts->area_dst, page_size); +} +static bool uffdio_verify_results (const char *name,int ret,int error,long result) {@@ -2359,6 +2412,14 @@ uffd_test_case_t uffd_tests[] = { .uffd_feature_required = UFFD_FEATURE_MOVE,
tools/testing/selftests/mm/uffd-unit-tests.c uffd_test_case_t uffd_tests[] = { · line 2422
Quoted 10 lines
tools/testing/selftests/mm/uffd-unit-tests.c +8
-0.test_case_ops = &uffd_move_test_pmd_case_ops, }, + { + .name = "move-swap-wp", + .uffd_fn = uffd_move_swap_wp_test, + .mem_targets = MEM_ANON, + .uffd_feature_required =UFFD_FEATURE_MOVE | + UFFD_FEATURE_PAGEFAULT_FLAG_WP, + .test_case_ops = &uffd_move_test_case_ops, + },
Do we also want to check wrp? I'd assume it suffers from the same problem (did not check carefully though)
Signature · 3 lines
Cheers, David
Re: [PATCH v2 2/2] selftests/mm: add a test for UFFDIO_MOVE of a write-protected swap entry
Quoted 3 lines · David Hildenbrand (Arm)
Is this loop really required? I thought the page should just be unmapped from the page table. It might still be in the swapcache and under writeback, but that's irrelevant.
No, one call was enough in every run I did. v3 calls it once and skips if the page isn't swapped out.
Quoted 2 lines
Do we also want to check wrp? I'd assume it suffers from the same problem (did not check carefully though)
Yes, it does. v3 adds a move-swap-rwp case, which fails without patch 1 and passes with it.
Thanks,
Donggeun