* [PATCH v2 0/4] userfaultfd: fixes for sashiko reports
@ 2026-12-03 17:15 Mike Rapoport (Microsoft)
2026-12-03 17:15 ` [PATCH v2 1/4] userfaultfd: write-lock the VMA before clearing its uffd state Mike Rapoport (Microsoft)
` (3 more replies)
0 siblings, 4 replies; 15+ messages in thread
From: Mike Rapoport (Microsoft) @ 2026-12-03 17:15 UTC (permalink / raw)
To: Andrew Morton, David Hildenbrand
Cc: Mike Rapoport, Peter Xu, Suren Baghdasaryan, linux-kernel,
linux-kselftest, linux-mm, Sashiko
These are fixes for userfaultfd bugs reported by sashiko, plus a THP
variant of one of them found while looking at the reports.
The first patch closes a race between closing a userfaultfd and uffd
operations of another userfaultfd of the same mm that can leave a
writable PTE with the uffd marker bit set.
The next two make UFFDIO_MOVE mark moved zero pages and THPs soft-dirty,
like it does for other PTEs and for the huge zero page.
The last one makes uffd-unit-tests free the test context when its setup
fails.
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
---
Changes in v2:
- address sashiko findings in selftest
- Link to v1: https://patch.msgid.link/20261202-fixes-uffd-sashiko-v1-0-a6f5a874334a@kernel.org
---
Mike Rapoport (Microsoft) (4):
userfaultfd: write-lock the VMA before clearing its uffd state
userfaultfd: mark zero pages moved by UFFDIO_MOVE soft-dirty
userfaultfd: mark THPs moved by UFFDIO_MOVE soft-dirty
selftests/mm: free the test context when uffd_test_ctx_init() fails
mm/huge_memory.c | 1 +
mm/userfaultfd.c | 5 +++++
tools/testing/selftests/mm/uffd-common.c | 27 +++++++++++++++++++++++----
3 files changed, 29 insertions(+), 4 deletions(-)
---
base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
change-id: 20261202-fixes-uffd-sashiko-fb173c1557eb
--
Sincerely yours,
Mike.
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH v2 1/4] userfaultfd: write-lock the VMA before clearing its uffd state 2026-12-03 17:15 [PATCH v2 0/4] userfaultfd: fixes for sashiko reports Mike Rapoport (Microsoft) @ 2026-12-03 17:15 ` Mike Rapoport (Microsoft) 2026-12-04 7:04 ` + userfaultfd-write-lock-the-vma-before-clearing-its-uffd-state.patch added to mm-new branch Andrew Morton 2026-12-04 9:27 ` [PATCH v2 1/4] userfaultfd: write-lock the VMA before clearing its uffd state Jose A. Perez de Azpillaga 2026-12-03 17:15 ` [PATCH v2 2/4] userfaultfd: mark zero pages moved by UFFDIO_MOVE soft-dirty Mike Rapoport (Microsoft) ` (2 subsequent siblings) 3 siblings, 2 replies; 15+ messages in thread From: Mike Rapoport (Microsoft) @ 2026-12-03 17:15 UTC (permalink / raw) To: Andrew Morton, David Hildenbrand Cc: Mike Rapoport, Peter Xu, Suren Baghdasaryan, linux-kernel, linux-kselftest, linux-mm, Sashiko UFFDIO_COPY and other uffd operations run under the per-VMA lock. They check that the VMA is registered with a userfaultfd, but not that it is registered with the userfaultfd that issued the operation. When a userfaultfd is closed, userfaultfd_release_all() clears the uffd state of its VMAs without write-locking them. Another userfaultfd of the same mm can then install a write-protected PTE in a part of the VMA that userfaultfd_clear_ptes() has already walked. The PTE keeps its uffd bit after the VMA is unregistered, and a later write fault makes it writable with the uffd bit still set. close(uffd1) UFFDIO_COPY(uffd2, MODE_WP) userfaultfd_release_all() mmap_write_lock() userfaultfd_clear_vma() userfaultfd_clear_ptes() change_protection() // walks past addr lock_vma_under_rcu() validate_dst_vma() // installs PTE with the uffd bit // at addr vma_end_read() userfaultfd_reset_ctx() vma_start_write() mmap_write_unlock() write fault at addr do_wp_page() // the PTE becomes writable with the uffd bit set UFFDIO_UNREGISTER is not affected since commit 35f7ecd55ce7 ("mm/userfaultfd: write-lock the VMA before waking waiters when unregistering"). Write-lock the VMA in userfaultfd_clear_ptes() before walking it. Reported-by: Sashiko <sashiko-bot@kernel.org> Closes: https://sashiko.dev/bug/linux-5cddfd13-146a-47fc-97f8-feca4b2339b4 Fixes: c88033efe9a3 ("mm/userfaultfd: reset ptes when close() for wr-protected ones") Reviewed-by: Suren Baghdasaryan <surenb@google.com> Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org> --- mm/userfaultfd.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 65f7399e79b65..772adff8883d3 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -2216,6 +2216,9 @@ static void userfaultfd_clear_ptes(struct vm_area_struct *vma, if (!userfaultfd_protected(vma)) return; + /* keep uffd operations under the per-VMA lock away from the walk */ + vma_start_write(vma); + if (userfaultfd_wp(vma)) mm_cp_flags |= MM_CP_UFFD_WP_RESOLVE; if (userfaultfd_rwp(vma)) -- 2.53.0 ^ permalink raw reply related [flat|nested] 15+ messages in thread
* + userfaultfd-write-lock-the-vma-before-clearing-its-uffd-state.patch added to mm-new branch 2026-12-03 17:15 ` [PATCH v2 1/4] userfaultfd: write-lock the VMA before clearing its uffd state Mike Rapoport (Microsoft) @ 2026-12-04 7:04 ` Andrew Morton 2026-12-04 9:27 ` [PATCH v2 1/4] userfaultfd: write-lock the VMA before clearing its uffd state Jose A. Perez de Azpillaga 1 sibling, 0 replies; 15+ messages in thread From: Andrew Morton @ 2026-12-04 7:04 UTC (permalink / raw) To: mm-commits, surenb, peterx, david, rppt, akpm The patch titled Subject: userfaultfd: write-lock the VMA before clearing its uffd state has been added to the -mm mm-new branch. Its filename is userfaultfd-write-lock-the-vma-before-clearing-its-uffd-state.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/userfaultfd-write-lock-the-vma-before-clearing-its-uffd-state.patch This patch will later appear in the mm-new branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Note, mm-new is a provisional staging ground for work-in-progress patches, and acceptance into mm-new is a notification for others take notice and to finish up reviews. Please do not hesitate to respond to review feedback and post updated versions to replace or incrementally fixup patches in mm-new. The mm-new branch of mm.git is not included in linux-next If a few days of testing in mm-new is successful, the patch will me moved into mm.git's mm-unstable branch, which is included in linux-next Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there most days ------------------------------------------------------ From: "Mike Rapoport (Microsoft)" <rppt@kernel.org> Subject: userfaultfd: write-lock the VMA before clearing its uffd state Date: Thu, 03 Dec 2026 19:15:21 +0200 Patch series "userfaultfd: fixes for sashiko reports", v2. These are fixes for userfaultfd bugs reported by sashiko, plus a THP variant of one of them found while looking at the reports. The first patch closes a race between closing a userfaultfd and uffd operations of another userfaultfd of the same mm that can leave a writable PTE with the uffd marker bit set. The next two make UFFDIO_MOVE mark moved zero pages and THPs soft-dirty, like it does for other PTEs and for the huge zero page. The last one makes uffd-unit-tests free the test context when its setup fails. This patch (of 4): UFFDIO_COPY and other uffd operations run under the per-VMA lock. They check that the VMA is registered with a userfaultfd, but not that it is registered with the userfaultfd that issued the operation. When a userfaultfd is closed, userfaultfd_release_all() clears the uffd state of its VMAs without write-locking them. Another userfaultfd of the same mm can then install a write-protected PTE in a part of the VMA that userfaultfd_clear_ptes() has already walked. The PTE keeps its uffd bit after the VMA is unregistered, and a later write fault makes it writable with the uffd bit still set. close(uffd1) UFFDIO_COPY(uffd2, MODE_WP) userfaultfd_release_all() mmap_write_lock() userfaultfd_clear_vma() userfaultfd_clear_ptes() change_protection() // walks past addr lock_vma_under_rcu() validate_dst_vma() // installs PTE with the uffd bit // at addr vma_end_read() userfaultfd_reset_ctx() vma_start_write() mmap_write_unlock() write fault at addr do_wp_page() // the PTE becomes writable with the uffd bit set UFFDIO_UNREGISTER is not affected since commit 35f7ecd55ce7 ("mm/userfaultfd: write-lock the VMA before waking waiters when unregistering"). Write-lock the VMA in userfaultfd_clear_ptes() before walking it. Link: https://lore.kernel.org/20261203-fixes-uffd-sashiko-v2-0-308ce557a7b6@kernel.org Link: https://patch.msgid.link/20261203-fixes-uffd-sashiko-v2-1-308ce557a7b6@kernel.org Fixes: c88033efe9a3 ("mm/userfaultfd: reset ptes when close() for wr-protected ones") Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Reported-by: Sashiko <sashiko-bot@kernel.org> Closes: https://sashiko.dev/bug/linux-5cddfd13-146a-47fc-97f8-feca4b2339b4 Reviewed-by: Suren Baghdasaryan <surenb@google.com> Cc: David Hildenbrand <david@kernel.org> Cc: Peter Xu <peterx@redhat.com> mm/userfaultfd.c | 3 +++ 1 file changed, 3 insertions(+) --- a/mm/userfaultfd.c~userfaultfd-write-lock-the-vma-before-clearing-its-uffd-state +++ a/mm/userfaultfd.c @@ -2221,6 +2221,9 @@ static void userfaultfd_clear_ptes(struc if (!userfaultfd_protected(vma)) return; + /* keep uffd operations under the per-VMA lock away from the walk */ + vma_start_write(vma); + if (userfaultfd_wp(vma)) mm_cp_flags |= MM_CP_UFFD_WP_RESOLVE; if (userfaultfd_rwp(vma)) _ Patches currently in -mm which might be from rppt@kernel.org are mm-cma-simplify-cma_declare_contiguous_multi.patch mm-fake-numa-fix-leak-of-the-distance-table-copy-on-errors.patch mm-secretmem-disable-memfd_secret-if-secretmem_init-fails.patch mm-cma-fix-handling-of-free-ranges-that-cross-4g-for-multi-range-areas.patch userfaultfd-write-lock-the-vma-before-clearing-its-uffd-state.patch userfaultfd-mark-zero-pages-moved-by-uffdio_move-soft-dirty.patch userfaultfd-mark-thps-moved-by-uffdio_move-soft-dirty.patch selftests-mm-free-the-test-context-when-uffd_test_ctx_init-fails.patch hibernation-make-swsusp_page-helpers-static.patch hibernation-ensure-secretmem-pages-dont-reach-a-snapshot.patch hibernate-handle-potential-errors-in-hibernate_mapunmap_page.patch hibernation-kfence-explicitly-map-unmap-kfence-pages.patch hibernation-make-hibernation-unavailable-when-debug_pagealloc-is-on.patch ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2 1/4] userfaultfd: write-lock the VMA before clearing its uffd state 2026-12-03 17:15 ` [PATCH v2 1/4] userfaultfd: write-lock the VMA before clearing its uffd state Mike Rapoport (Microsoft) 2026-12-04 7:04 ` + userfaultfd-write-lock-the-vma-before-clearing-its-uffd-state.patch added to mm-new branch Andrew Morton @ 2026-12-04 9:27 ` Jose A. Perez de Azpillaga 1 sibling, 0 replies; 15+ messages in thread From: Jose A. Perez de Azpillaga @ 2026-12-04 9:27 UTC (permalink / raw) To: Mike Rapoport (Microsoft), Andrew Morton, David Hildenbrand Cc: Peter Xu, Suren Baghdasaryan, linux-kernel, linux-kselftest, linux-mm, Sashiko On 12/3/26 6:15 PM, Mike Rapoport (Microsoft) wrote: > UFFDIO_COPY and other uffd operations run under the per-VMA lock. They > check that the VMA is registered with a userfaultfd, but not that it is > registered with the userfaultfd that issued the operation. > > When a userfaultfd is closed, userfaultfd_release_all() clears the uffd > state of its VMAs without write-locking them. Another userfaultfd of the > same mm can then install a write-protected PTE in a part of the VMA that > userfaultfd_clear_ptes() has already walked. The PTE keeps its uffd bit > after the VMA is unregistered, and a later write fault makes it writable > with the uffd bit still set. > > close(uffd1) UFFDIO_COPY(uffd2, MODE_WP) > > userfaultfd_release_all() > mmap_write_lock() > userfaultfd_clear_vma() > userfaultfd_clear_ptes() > change_protection() > // walks past addr > lock_vma_under_rcu() > validate_dst_vma() > // installs PTE with the uffd bit > // at addr > vma_end_read() > userfaultfd_reset_ctx() > vma_start_write() > mmap_write_unlock() > > write fault at addr > do_wp_page() > // the PTE becomes writable with the uffd bit set > > UFFDIO_UNREGISTER is not affected since > commit 35f7ecd55ce7 ("mm/userfaultfd: write-lock the VMA before waking > waiters when unregistering"). > > Write-lock the VMA in userfaultfd_clear_ptes() before walking it. > > Reported-by: Sashiko <sashiko-bot@kernel.org> > Closes: https://sashiko.dev/bug/linux-5cddfd13-146a-47fc-97f8-feca4b2339b4 > Fixes: c88033efe9a3 ("mm/userfaultfd: reset ptes when close() for wr-protected ones") > Reviewed-by: Suren Baghdasaryan <surenb@google.com> > Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org> LGTM: Reviewed-by: Jose A. Perez de Azpillaga <azpijr@gmail.com> -- cheers, jose a. p-a ^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH v2 2/4] userfaultfd: mark zero pages moved by UFFDIO_MOVE soft-dirty 2026-12-03 17:15 [PATCH v2 0/4] userfaultfd: fixes for sashiko reports Mike Rapoport (Microsoft) 2026-12-03 17:15 ` [PATCH v2 1/4] userfaultfd: write-lock the VMA before clearing its uffd state Mike Rapoport (Microsoft) @ 2026-12-03 17:15 ` Mike Rapoport (Microsoft) 2026-12-04 7:04 ` + userfaultfd-mark-zero-pages-moved-by-uffdio_move-soft-dirty.patch added to mm-new branch Andrew Morton 2026-12-04 9:31 ` [PATCH v2 2/4] userfaultfd: mark zero pages moved by UFFDIO_MOVE soft-dirty Jose A. Perez de Azpillaga 2026-12-03 17:15 ` [PATCH v2 3/4] userfaultfd: mark THPs " Mike Rapoport (Microsoft) 2026-12-03 17:15 ` [PATCH v2 4/4] selftests/mm: free the test context when uffd_test_ctx_init() fails Mike Rapoport (Microsoft) 3 siblings, 2 replies; 15+ messages in thread From: Mike Rapoport (Microsoft) @ 2026-12-03 17:15 UTC (permalink / raw) To: Andrew Morton, David Hildenbrand Cc: Mike Rapoport, Peter Xu, Suren Baghdasaryan, linux-kernel, linux-kselftest, linux-mm, Sashiko Commit 75cb1cca2c88 ("mm: userfaultfd: correct dirty flags set for both present and swap pte") made UFFDIO_MOVE set the soft-dirty bit on moved PTEs, like mremap() does, so that soft-dirty trackers such as CRIU notice the move. It missed move_zeropage_pte(), so a zero page moved by UFFDIO_MOVE is not marked soft-dirty. Mark the moved zero page PTE soft-dirty as well. Reported-by: Sashiko <sashiko-bot@kernel.org> Closes: https://sashiko.dev/bug/linux-0c1c66e4-231d-4567-a40c-42243a875a5d Fixes: eb1521dad8f3 ("userfaultfd: handle zeropage moves by UFFDIO_MOVE") Reviewed-by: David Hildenbrand (Arm) <david@kernel.org> Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org> --- mm/userfaultfd.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 772adff8883d3..6c5e5b0c7d784 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -1433,6 +1433,8 @@ static int move_zeropage_pte(struct mm_struct *mm, zero_pte = pte_mkspecial(pfn_pte(zero_pfn(dst_addr), dst_vma->vm_page_prot)); + if (pgtable_supports_soft_dirty()) + zero_pte = pte_mksoft_dirty(zero_pte); /* Re-arm RWP on the moved PTE if dst_vma is RWP-registered. */ if (userfaultfd_rwp(dst_vma)) { -- 2.53.0 ^ permalink raw reply related [flat|nested] 15+ messages in thread
* + userfaultfd-mark-zero-pages-moved-by-uffdio_move-soft-dirty.patch added to mm-new branch 2026-12-03 17:15 ` [PATCH v2 2/4] userfaultfd: mark zero pages moved by UFFDIO_MOVE soft-dirty Mike Rapoport (Microsoft) @ 2026-12-04 7:04 ` Andrew Morton 2026-12-04 9:31 ` [PATCH v2 2/4] userfaultfd: mark zero pages moved by UFFDIO_MOVE soft-dirty Jose A. Perez de Azpillaga 1 sibling, 0 replies; 15+ messages in thread From: Andrew Morton @ 2026-12-04 7:04 UTC (permalink / raw) To: mm-commits, surenb, peterx, david, rppt, akpm The patch titled Subject: userfaultfd: mark zero pages moved by UFFDIO_MOVE soft-dirty has been added to the -mm mm-new branch. Its filename is userfaultfd-mark-zero-pages-moved-by-uffdio_move-soft-dirty.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/userfaultfd-mark-zero-pages-moved-by-uffdio_move-soft-dirty.patch This patch will later appear in the mm-new branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Note, mm-new is a provisional staging ground for work-in-progress patches, and acceptance into mm-new is a notification for others take notice and to finish up reviews. Please do not hesitate to respond to review feedback and post updated versions to replace or incrementally fixup patches in mm-new. The mm-new branch of mm.git is not included in linux-next If a few days of testing in mm-new is successful, the patch will me moved into mm.git's mm-unstable branch, which is included in linux-next Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there most days ------------------------------------------------------ From: "Mike Rapoport (Microsoft)" <rppt@kernel.org> Subject: userfaultfd: mark zero pages moved by UFFDIO_MOVE soft-dirty Date: Thu, 03 Dec 2026 19:15:22 +0200 Commit 75cb1cca2c88 ("mm: userfaultfd: correct dirty flags set for both present and swap pte") made UFFDIO_MOVE set the soft-dirty bit on moved PTEs, like mremap() does, so that soft-dirty trackers such as CRIU notice the move. It missed move_zeropage_pte(), so a zero page moved by UFFDIO_MOVE is not marked soft-dirty. Mark the moved zero page PTE soft-dirty as well. Link: https://patch.msgid.link/20261203-fixes-uffd-sashiko-v2-2-308ce557a7b6@kernel.org Fixes: eb1521dad8f3 ("userfaultfd: handle zeropage moves by UFFDIO_MOVE") Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Reported-by: Sashiko <sashiko-bot@kernel.org> Closes: https://sashiko.dev/bug/linux-0c1c66e4-231d-4567-a40c-42243a875a5d Reviewed-by: David Hildenbrand (Arm) <david@kernel.org> Cc: Peter Xu <peterx@redhat.com> Cc: Suren Baghdasaryan <surenb@google.com> mm/userfaultfd.c | 2 ++ 1 file changed, 2 insertions(+) --- a/mm/userfaultfd.c~userfaultfd-mark-zero-pages-moved-by-uffdio_move-soft-dirty +++ a/mm/userfaultfd.c @@ -1426,6 +1426,8 @@ static int move_zeropage_pte(struct mm_s zero_pte = pte_mkspecial(pfn_pte(zero_pfn(dst_addr), dst_vma->vm_page_prot)); + if (pgtable_supports_soft_dirty()) + zero_pte = pte_mksoft_dirty(zero_pte); /* Re-arm RWP on the moved PTE if dst_vma is RWP-registered. */ if (userfaultfd_rwp(dst_vma)) { _ Patches currently in -mm which might be from rppt@kernel.org are mm-cma-simplify-cma_declare_contiguous_multi.patch mm-fake-numa-fix-leak-of-the-distance-table-copy-on-errors.patch mm-secretmem-disable-memfd_secret-if-secretmem_init-fails.patch mm-cma-fix-handling-of-free-ranges-that-cross-4g-for-multi-range-areas.patch userfaultfd-write-lock-the-vma-before-clearing-its-uffd-state.patch userfaultfd-mark-zero-pages-moved-by-uffdio_move-soft-dirty.patch userfaultfd-mark-thps-moved-by-uffdio_move-soft-dirty.patch selftests-mm-free-the-test-context-when-uffd_test_ctx_init-fails.patch hibernation-make-swsusp_page-helpers-static.patch hibernation-ensure-secretmem-pages-dont-reach-a-snapshot.patch hibernate-handle-potential-errors-in-hibernate_mapunmap_page.patch hibernation-kfence-explicitly-map-unmap-kfence-pages.patch hibernation-make-hibernation-unavailable-when-debug_pagealloc-is-on.patch ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2 2/4] userfaultfd: mark zero pages moved by UFFDIO_MOVE soft-dirty 2026-12-03 17:15 ` [PATCH v2 2/4] userfaultfd: mark zero pages moved by UFFDIO_MOVE soft-dirty Mike Rapoport (Microsoft) 2026-12-04 7:04 ` + userfaultfd-mark-zero-pages-moved-by-uffdio_move-soft-dirty.patch added to mm-new branch Andrew Morton @ 2026-12-04 9:31 ` Jose A. Perez de Azpillaga 1 sibling, 0 replies; 15+ messages in thread From: Jose A. Perez de Azpillaga @ 2026-12-04 9:31 UTC (permalink / raw) To: Mike Rapoport (Microsoft), Andrew Morton, David Hildenbrand Cc: Peter Xu, Suren Baghdasaryan, linux-kernel, linux-kselftest, linux-mm, Sashiko On 12/3/26 6:15 PM, Mike Rapoport (Microsoft) wrote: > Commit 75cb1cca2c88 ("mm: userfaultfd: correct dirty flags set for both > present and swap pte") made UFFDIO_MOVE set the soft-dirty bit on moved > PTEs, like mremap() does, so that soft-dirty trackers such as CRIU notice > the move. > > It missed move_zeropage_pte(), so a zero page moved by UFFDIO_MOVE is not > marked soft-dirty. > > Mark the moved zero page PTE soft-dirty as well. > > Reported-by: Sashiko <sashiko-bot@kernel.org> > Closes: https://sashiko.dev/bug/linux-0c1c66e4-231d-4567-a40c-42243a875a5d > Fixes: eb1521dad8f3 ("userfaultfd: handle zeropage moves by UFFDIO_MOVE") > Reviewed-by: David Hildenbrand (Arm) <david@kernel.org> > Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org> LGTM: Reviewed-by: Jose A. Perez de Azpillaga <azpijr@gmail.com> -- cheers, jose a. p-a ^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH v2 3/4] userfaultfd: mark THPs moved by UFFDIO_MOVE soft-dirty 2026-12-03 17:15 [PATCH v2 0/4] userfaultfd: fixes for sashiko reports Mike Rapoport (Microsoft) 2026-12-03 17:15 ` [PATCH v2 1/4] userfaultfd: write-lock the VMA before clearing its uffd state Mike Rapoport (Microsoft) 2026-12-03 17:15 ` [PATCH v2 2/4] userfaultfd: mark zero pages moved by UFFDIO_MOVE soft-dirty Mike Rapoport (Microsoft) @ 2026-12-03 17:15 ` Mike Rapoport (Microsoft) 2026-12-04 7:04 ` + userfaultfd-mark-thps-moved-by-uffdio_move-soft-dirty.patch added to mm-new branch Andrew Morton 2026-12-04 9:32 ` [PATCH v2 3/4] userfaultfd: mark THPs moved by UFFDIO_MOVE soft-dirty Jose A. Perez de Azpillaga 2026-12-03 17:15 ` [PATCH v2 4/4] selftests/mm: free the test context when uffd_test_ctx_init() fails Mike Rapoport (Microsoft) 3 siblings, 2 replies; 15+ messages in thread From: Mike Rapoport (Microsoft) @ 2026-12-03 17:15 UTC (permalink / raw) To: Andrew Morton, David Hildenbrand Cc: Mike Rapoport, Peter Xu, Suren Baghdasaryan, linux-kernel, linux-kselftest, linux-mm UFFDIO_MOVE marks moved PTEs and a moved huge zero page soft-dirty, like mremap() does. A THP is the exception. move_pages_huge_pmd() creates the destination PMD from scratch and does not mark it soft-dirty, so soft-dirty trackers such as CRIU miss the move. Mark the moved THP PMD soft-dirty with move_soft_dirty_pmd(), like mremap() does for PMDs. Fixes: adef440691ba ("userfaultfd: UFFDIO_MOVE uABI") Reviewed-by: David Hildenbrand (Arm) <david@kernel.org> Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org> --- mm/huge_memory.c | 1 + 1 file changed, 1 insertion(+) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index d065b26a94dfa..f0cb8b0e469f4 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -3014,6 +3014,7 @@ int move_pages_huge_pmd(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, pm _dst_pmd = folio_mk_pmd(src_folio, dst_vma->vm_page_prot); /* Follow mremap() behavior and treat the entry dirty after the move */ _dst_pmd = pmd_mkwrite(pmd_mkdirty(_dst_pmd), dst_vma); + _dst_pmd = move_soft_dirty_pmd(_dst_pmd); } else { src_pmdval = pmdp_huge_clear_flush(src_vma, src_addr, src_pmd); _dst_pmd = move_soft_dirty_pmd(src_pmdval); -- 2.53.0 ^ permalink raw reply related [flat|nested] 15+ messages in thread
* + userfaultfd-mark-thps-moved-by-uffdio_move-soft-dirty.patch added to mm-new branch 2026-12-03 17:15 ` [PATCH v2 3/4] userfaultfd: mark THPs " Mike Rapoport (Microsoft) @ 2026-12-04 7:04 ` Andrew Morton 2026-12-04 9:32 ` [PATCH v2 3/4] userfaultfd: mark THPs moved by UFFDIO_MOVE soft-dirty Jose A. Perez de Azpillaga 1 sibling, 0 replies; 15+ messages in thread From: Andrew Morton @ 2026-12-04 7:04 UTC (permalink / raw) To: mm-commits, surenb, peterx, david, rppt, akpm The patch titled Subject: userfaultfd: mark THPs moved by UFFDIO_MOVE soft-dirty has been added to the -mm mm-new branch. Its filename is userfaultfd-mark-thps-moved-by-uffdio_move-soft-dirty.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/userfaultfd-mark-thps-moved-by-uffdio_move-soft-dirty.patch This patch will later appear in the mm-new branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Note, mm-new is a provisional staging ground for work-in-progress patches, and acceptance into mm-new is a notification for others take notice and to finish up reviews. Please do not hesitate to respond to review feedback and post updated versions to replace or incrementally fixup patches in mm-new. The mm-new branch of mm.git is not included in linux-next If a few days of testing in mm-new is successful, the patch will me moved into mm.git's mm-unstable branch, which is included in linux-next Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there most days ------------------------------------------------------ From: "Mike Rapoport (Microsoft)" <rppt@kernel.org> Subject: userfaultfd: mark THPs moved by UFFDIO_MOVE soft-dirty Date: Thu, 03 Dec 2026 19:15:23 +0200 UFFDIO_MOVE marks moved PTEs and a moved huge zero page soft-dirty, like mremap() does. A THP is the exception. move_pages_huge_pmd() creates the destination PMD from scratch and does not mark it soft-dirty, so soft-dirty trackers such as CRIU miss the move. Mark the moved THP PMD soft-dirty with move_soft_dirty_pmd(), like mremap() does for PMDs. Link: https://patch.msgid.link/20261203-fixes-uffd-sashiko-v2-3-308ce557a7b6@kernel.org Fixes: adef440691ba ("userfaultfd: UFFDIO_MOVE uABI") Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Reviewed-by: David Hildenbrand (Arm) <david@kernel.org> Cc: Peter Xu <peterx@redhat.com> Cc: Suren Baghdasaryan <surenb@google.com> mm/huge_memory.c | 1 + 1 file changed, 1 insertion(+) --- a/mm/huge_memory.c~userfaultfd-mark-thps-moved-by-uffdio_move-soft-dirty +++ a/mm/huge_memory.c @@ -2983,6 +2983,7 @@ int move_pages_huge_pmd(struct mm_struct _dst_pmd = folio_mk_pmd(src_folio, dst_vma->vm_page_prot); /* Follow mremap() behavior and treat the entry dirty after the move */ _dst_pmd = pmd_mkwrite(pmd_mkdirty(_dst_pmd), dst_vma); + _dst_pmd = move_soft_dirty_pmd(_dst_pmd); } else { pmdp_huge_clear_flush(src_vma, src_addr, src_pmd); _dst_pmd = folio_mk_pmd(huge_zero_folio, dst_vma->vm_page_prot); _ Patches currently in -mm which might be from rppt@kernel.org are mm-cma-simplify-cma_declare_contiguous_multi.patch mm-fake-numa-fix-leak-of-the-distance-table-copy-on-errors.patch mm-secretmem-disable-memfd_secret-if-secretmem_init-fails.patch mm-cma-fix-handling-of-free-ranges-that-cross-4g-for-multi-range-areas.patch userfaultfd-write-lock-the-vma-before-clearing-its-uffd-state.patch userfaultfd-mark-zero-pages-moved-by-uffdio_move-soft-dirty.patch userfaultfd-mark-thps-moved-by-uffdio_move-soft-dirty.patch selftests-mm-free-the-test-context-when-uffd_test_ctx_init-fails.patch hibernation-make-swsusp_page-helpers-static.patch hibernation-ensure-secretmem-pages-dont-reach-a-snapshot.patch hibernate-handle-potential-errors-in-hibernate_mapunmap_page.patch hibernation-kfence-explicitly-map-unmap-kfence-pages.patch hibernation-make-hibernation-unavailable-when-debug_pagealloc-is-on.patch ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2 3/4] userfaultfd: mark THPs moved by UFFDIO_MOVE soft-dirty 2026-12-03 17:15 ` [PATCH v2 3/4] userfaultfd: mark THPs " Mike Rapoport (Microsoft) 2026-12-04 7:04 ` + userfaultfd-mark-thps-moved-by-uffdio_move-soft-dirty.patch added to mm-new branch Andrew Morton @ 2026-12-04 9:32 ` Jose A. Perez de Azpillaga 1 sibling, 0 replies; 15+ messages in thread From: Jose A. Perez de Azpillaga @ 2026-12-04 9:32 UTC (permalink / raw) To: Mike Rapoport (Microsoft), Andrew Morton, David Hildenbrand Cc: Peter Xu, Suren Baghdasaryan, linux-kernel, linux-kselftest, linux-mm On 12/3/26 6:15 PM, Mike Rapoport (Microsoft) wrote: > UFFDIO_MOVE marks moved PTEs and a moved huge zero page soft-dirty, like > mremap() does. > > A THP is the exception. move_pages_huge_pmd() creates the destination PMD > from scratch and does not mark it soft-dirty, so soft-dirty trackers such > as CRIU miss the move. > > Mark the moved THP PMD soft-dirty with move_soft_dirty_pmd(), like > mremap() does for PMDs. > > Fixes: adef440691ba ("userfaultfd: UFFDIO_MOVE uABI") > Reviewed-by: David Hildenbrand (Arm) <david@kernel.org> > Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org> LGTM: Reviewed-by: Jose A. Perez de Azpillaga <azpijr@gmail.com> -- cheers, jose a. p-a ^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH v2 4/4] selftests/mm: free the test context when uffd_test_ctx_init() fails 2026-12-03 17:15 [PATCH v2 0/4] userfaultfd: fixes for sashiko reports Mike Rapoport (Microsoft) ` (2 preceding siblings ...) 2026-12-03 17:15 ` [PATCH v2 3/4] userfaultfd: mark THPs " Mike Rapoport (Microsoft) @ 2026-12-03 17:15 ` Mike Rapoport (Microsoft) 2026-12-04 7:04 ` + selftests-mm-free-the-test-context-when-uffd_test_ctx_init-fails.patch added to mm-new branch Andrew Morton ` (3 more replies) 3 siblings, 4 replies; 15+ messages in thread From: Mike Rapoport (Microsoft) @ 2026-12-03 17:15 UTC (permalink / raw) To: Andrew Morton, David Hildenbrand Cc: Mike Rapoport, Peter Xu, Suren Baghdasaryan, linux-kernel, linux-kselftest, linux-mm, Sashiko uffd_test_ctx_init() returns on errors without unmapping the test areas it has already allocated, and userfaultfd_open() leaves the userfaultfd open when UFFDIO_API fails. uffd-unit-tests then skips the test and goes on to the next one in the same process, so everything allocated for the failed test stays around until the end of the run. For hugetlb, the leaked areas keep their huge pages reserved, and the following hugetlb tests can fail to allocate their areas and get skipped with a misleading "memory allocation failed". Unmap the areas allocated so far when uffd_test_ctx_init() fails and close the userfaultfd when userfaultfd_open() fails. Reported-by: Sashiko <sashiko-bot@kernel.org> Closes: https://sashiko.dev/bug/linux-3a91f55a-ede8-4950-bbda-cd0b03f9076a Fixes: 1c8d39fa7b63 ("selftests/mm: call uffd_test_ctx_clear at the end of the test") Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org> --- tools/testing/selftests/mm/uffd-common.c | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/mm/uffd-common.c b/tools/testing/selftests/mm/uffd-common.c index a76851aced4d0..82af2b2964b23 100644 --- a/tools/testing/selftests/mm/uffd-common.c +++ b/tools/testing/selftests/mm/uffd-common.c @@ -269,12 +269,17 @@ int userfaultfd_open(uffd_global_test_opts_t *gopts, uint64_t *features) uffdio_api.features = *features; if (ioctl(gopts->uffd, UFFDIO_API, &uffdio_api)) /* Probably lack of CAP_PTRACE? */ - return -1; + goto err_close; if (uffdio_api.api != UFFD_API) err("UFFDIO_API error: %" PRIu64, (uint64_t)uffdio_api.api); *features = uffdio_api.features; return 0; + +err_close: + close(gopts->uffd); + gopts->uffd = -1; + return -1; } static inline void munmap_area(uffd_global_test_opts_t *gopts, void **area) @@ -333,24 +338,30 @@ int uffd_test_ctx_init(uffd_global_test_opts_t *gopts, uint64_t features, const } ret = uffd_test_ops->allocate_area(gopts, (void **) &gopts->area_src, true); - ret |= uffd_test_ops->allocate_area(gopts, (void **) &gopts->area_dst, false); if (ret) { if (errmsg) *errmsg = "memory allocation failed"; return ret; } + ret = uffd_test_ops->allocate_area(gopts, (void **) &gopts->area_dst, false); + if (ret) { + if (errmsg) + *errmsg = "memory allocation failed"; + goto err_unmap_src; + } + if (uffd_test_case_ops && uffd_test_case_ops->post_alloc) { ret = uffd_test_case_ops->post_alloc(gopts, errmsg); if (ret) - return ret; + goto err_unmap_dst; } ret = userfaultfd_open(gopts, &features); if (ret) { if (errmsg) *errmsg = "possible lack of privilege"; - return ret; + goto err_unmap_dst; } gopts->count_verify = malloc(gopts->nr_pages * sizeof(unsigned long long)); @@ -399,6 +410,14 @@ int uffd_test_ctx_init(uffd_global_test_opts_t *gopts, uint64_t features, const err("pipe"); return 0; + +err_unmap_dst: + munmap_area(gopts, (void **)&gopts->area_dst); + munmap_area(gopts, (void **)&gopts->area_dst_alias); +err_unmap_src: + munmap_area(gopts, (void **)&gopts->area_src); + munmap_area(gopts, (void **)&gopts->area_src_alias); + return ret; } void wp_range(int ufd, __u64 start, __u64 len, bool wp) -- 2.53.0 ^ permalink raw reply related [flat|nested] 15+ messages in thread
* + selftests-mm-free-the-test-context-when-uffd_test_ctx_init-fails.patch added to mm-new branch 2026-12-03 17:15 ` [PATCH v2 4/4] selftests/mm: free the test context when uffd_test_ctx_init() fails Mike Rapoport (Microsoft) @ 2026-12-04 7:04 ` Andrew Morton 2026-12-04 9:39 ` [PATCH v2 4/4] selftests/mm: free the test context when uffd_test_ctx_init() fails Jose A. Perez de Azpillaga ` (2 subsequent siblings) 3 siblings, 0 replies; 15+ messages in thread From: Andrew Morton @ 2026-12-04 7:04 UTC (permalink / raw) To: mm-commits, surenb, peterx, david, rppt, akpm The patch titled Subject: selftests/mm: free the test context when uffd_test_ctx_init() fails has been added to the -mm mm-new branch. Its filename is selftests-mm-free-the-test-context-when-uffd_test_ctx_init-fails.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/selftests-mm-free-the-test-context-when-uffd_test_ctx_init-fails.patch This patch will later appear in the mm-new branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Note, mm-new is a provisional staging ground for work-in-progress patches, and acceptance into mm-new is a notification for others take notice and to finish up reviews. Please do not hesitate to respond to review feedback and post updated versions to replace or incrementally fixup patches in mm-new. The mm-new branch of mm.git is not included in linux-next If a few days of testing in mm-new is successful, the patch will me moved into mm.git's mm-unstable branch, which is included in linux-next Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there most days ------------------------------------------------------ From: "Mike Rapoport (Microsoft)" <rppt@kernel.org> Subject: selftests/mm: free the test context when uffd_test_ctx_init() fails Date: Thu, 03 Dec 2026 19:15:24 +0200 uffd_test_ctx_init() returns on errors without unmapping the test areas it has already allocated, and userfaultfd_open() leaves the userfaultfd open when UFFDIO_API fails. uffd-unit-tests then skips the test and goes on to the next one in the same process, so everything allocated for the failed test stays around until the end of the run. For hugetlb, the leaked areas keep their huge pages reserved, and the following hugetlb tests can fail to allocate their areas and get skipped with a misleading "memory allocation failed". Unmap the areas allocated so far when uffd_test_ctx_init() fails and close the userfaultfd when userfaultfd_open() fails. Link: https://patch.msgid.link/20261203-fixes-uffd-sashiko-v2-4-308ce557a7b6@kernel.org Fixes: 1c8d39fa7b63 ("selftests/mm: call uffd_test_ctx_clear at the end of the test") Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Reported-by: Sashiko <sashiko-bot@kernel.org> Closes: https://sashiko.dev/bug/linux-3a91f55a-ede8-4950-bbda-cd0b03f9076a Cc: David Hildenbrand <david@kernel.org> Cc: Peter Xu <peterx@redhat.com> Cc: Suren Baghdasaryan <surenb@google.com> tools/testing/selftests/mm/uffd-common.c | 27 +++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) --- a/tools/testing/selftests/mm/uffd-common.c~selftests-mm-free-the-test-context-when-uffd_test_ctx_init-fails +++ a/tools/testing/selftests/mm/uffd-common.c @@ -269,12 +269,17 @@ int userfaultfd_open(uffd_global_test_op uffdio_api.features = *features; if (ioctl(gopts->uffd, UFFDIO_API, &uffdio_api)) /* Probably lack of CAP_PTRACE? */ - return -1; + goto err_close; if (uffdio_api.api != UFFD_API) err("UFFDIO_API error: %" PRIu64, (uint64_t)uffdio_api.api); *features = uffdio_api.features; return 0; + +err_close: + close(gopts->uffd); + gopts->uffd = -1; + return -1; } static inline void munmap_area(uffd_global_test_opts_t *gopts, void **area) @@ -333,24 +338,30 @@ int uffd_test_ctx_init(uffd_global_test_ } ret = uffd_test_ops->allocate_area(gopts, (void **) &gopts->area_src, true); - ret |= uffd_test_ops->allocate_area(gopts, (void **) &gopts->area_dst, false); if (ret) { if (errmsg) *errmsg = "memory allocation failed"; return ret; } + ret = uffd_test_ops->allocate_area(gopts, (void **) &gopts->area_dst, false); + if (ret) { + if (errmsg) + *errmsg = "memory allocation failed"; + goto err_unmap_src; + } + if (uffd_test_case_ops && uffd_test_case_ops->post_alloc) { ret = uffd_test_case_ops->post_alloc(gopts, errmsg); if (ret) - return ret; + goto err_unmap_dst; } ret = userfaultfd_open(gopts, &features); if (ret) { if (errmsg) *errmsg = "possible lack of privilege"; - return ret; + goto err_unmap_dst; } gopts->count_verify = malloc(gopts->nr_pages * sizeof(unsigned long long)); @@ -399,6 +410,14 @@ int uffd_test_ctx_init(uffd_global_test_ err("pipe"); return 0; + +err_unmap_dst: + munmap_area(gopts, (void **)&gopts->area_dst); + munmap_area(gopts, (void **)&gopts->area_dst_alias); +err_unmap_src: + munmap_area(gopts, (void **)&gopts->area_src); + munmap_area(gopts, (void **)&gopts->area_src_alias); + return ret; } void wp_range(int ufd, __u64 start, __u64 len, bool wp) _ Patches currently in -mm which might be from rppt@kernel.org are mm-cma-simplify-cma_declare_contiguous_multi.patch mm-fake-numa-fix-leak-of-the-distance-table-copy-on-errors.patch mm-secretmem-disable-memfd_secret-if-secretmem_init-fails.patch mm-cma-fix-handling-of-free-ranges-that-cross-4g-for-multi-range-areas.patch userfaultfd-write-lock-the-vma-before-clearing-its-uffd-state.patch userfaultfd-mark-zero-pages-moved-by-uffdio_move-soft-dirty.patch userfaultfd-mark-thps-moved-by-uffdio_move-soft-dirty.patch selftests-mm-free-the-test-context-when-uffd_test_ctx_init-fails.patch hibernation-make-swsusp_page-helpers-static.patch hibernation-ensure-secretmem-pages-dont-reach-a-snapshot.patch hibernate-handle-potential-errors-in-hibernate_mapunmap_page.patch hibernation-kfence-explicitly-map-unmap-kfence-pages.patch hibernation-make-hibernation-unavailable-when-debug_pagealloc-is-on.patch ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2 4/4] selftests/mm: free the test context when uffd_test_ctx_init() fails 2026-12-03 17:15 ` [PATCH v2 4/4] selftests/mm: free the test context when uffd_test_ctx_init() fails Mike Rapoport (Microsoft) 2026-12-04 7:04 ` + selftests-mm-free-the-test-context-when-uffd_test_ctx_init-fails.patch added to mm-new branch Andrew Morton @ 2026-12-04 9:39 ` Jose A. Perez de Azpillaga 2026-12-05 17:09 ` Andrew Morton 2026-12-05 17:09 ` + selftests-mm-free-the-test-context-when-uffd_test_ctx_init-fails-fix.patch added to mm-new branch Andrew Morton 3 siblings, 0 replies; 15+ messages in thread From: Jose A. Perez de Azpillaga @ 2026-12-04 9:39 UTC (permalink / raw) To: Mike Rapoport (Microsoft), Andrew Morton, David Hildenbrand Cc: Peter Xu, Suren Baghdasaryan, linux-kernel, linux-kselftest, linux-mm, Sashiko On 12/3/26 6:15 PM, Mike Rapoport (Microsoft) wrote: > uffd_test_ctx_init() returns on errors without unmapping the test areas > it has already allocated, and userfaultfd_open() leaves the userfaultfd > open when UFFDIO_API fails. uffd-unit-tests then skips the test and goes > on to the next one in the same process, so everything allocated for the > failed test stays around until the end of the run. > > For hugetlb, the leaked areas keep their huge pages reserved, and the > following hugetlb tests can fail to allocate their areas and get skipped > with a misleading "memory allocation failed". > > Unmap the areas allocated so far when uffd_test_ctx_init() fails and > close the userfaultfd when userfaultfd_open() fails. > > Reported-by: Sashiko <sashiko-bot@kernel.org> > Closes: https://sashiko.dev/bug/linux-3a91f55a-ede8-4950-bbda-cd0b03f9076a > Fixes: 1c8d39fa7b63 ("selftests/mm: call uffd_test_ctx_clear at the end of the test") > Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org> LGTM: Reviewed-by: Jose A. Perez de Azpillaga <azpijr@gmail.com> -- cheers, jose a. p-a ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2 4/4] selftests/mm: free the test context when uffd_test_ctx_init() fails 2026-12-03 17:15 ` [PATCH v2 4/4] selftests/mm: free the test context when uffd_test_ctx_init() fails Mike Rapoport (Microsoft) 2026-12-04 7:04 ` + selftests-mm-free-the-test-context-when-uffd_test_ctx_init-fails.patch added to mm-new branch Andrew Morton 2026-12-04 9:39 ` [PATCH v2 4/4] selftests/mm: free the test context when uffd_test_ctx_init() fails Jose A. Perez de Azpillaga @ 2026-12-05 17:09 ` Andrew Morton 2026-12-05 17:09 ` + selftests-mm-free-the-test-context-when-uffd_test_ctx_init-fails-fix.patch added to mm-new branch Andrew Morton 3 siblings, 0 replies; 15+ messages in thread From: Andrew Morton @ 2026-12-05 17:09 UTC (permalink / raw) To: Mike Rapoport (Microsoft) Cc: David Hildenbrand, Peter Xu, Suren Baghdasaryan, linux-kernel, linux-kselftest, linux-mm, Sashiko On Thu, 03 Dec 2026 19:15:24 +0200 "Mike Rapoport (Microsoft)" <rppt@kernel.org> wrote: > uffd_test_ctx_init() returns on errors without unmapping the test areas > it has already allocated, and userfaultfd_open() leaves the userfaultfd > open when UFFDIO_API fails. uffd-unit-tests then skips the test and goes > on to the next one in the same process, so everything allocated for the > failed test stays around until the end of the run. > > For hugetlb, the leaked areas keep their huge pages reserved, and the > following hugetlb tests can fail to allocate their areas and get skipped > with a misleading "memory allocation failed". > > Unmap the areas allocated so far when uffd_test_ctx_init() fails and > close the userfaultfd when userfaultfd_open() fails. testbot hit a leak after fault-injecting an mmap failure. How does this -fix look? From: Andrew Morton <akpm@linux-foundation.org> Subject: selftests/mm: release partial hugetlb area on allocation failure Date: Sat, 05 Dec 2026 08:53:51 -0800 hugetlb_allocate_area() creates a memfd before mapping the test area. If the first mmap() fails, the memfd stays open. If mapping a shared alias fails, the first area also remains mapped. An error allocating the source area returns directly from uffd_test_ctx_init(), bypassing the cleanup added by the preceding patch. A skipped test can therefore leave huge pages reserved for later tests. Close the memfd on both failures, unmap the first area when its alias fails, and enter err_unmap_src for source allocation errors. Preserve the original errno across cleanup. Fault injection of the alias mmap() left one live mapping before this change and none afterward. The first mmap() failure and subsequent normal cases were also checked. The userfaultfd selftests passed 122/122 with CONFIG_USERFAULTFD=y, CONFIG_HUGETLB_PAGE=y and CONFIG_HUGETLBFS=y. CONFIG_CPA_DEBUG was disabled in the test kernel. Link: https://lore.kernel.org/r/20261203-fixes-uffd-sashiko-v2-4-308ce557a7b6@kernel.org Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Cc: Mike Rapoport (Microsoft) <rppt@kernel.org> Cc: David Hildenbrand <david@kernel.org> Cc: Peter Xu <peterx@redhat.com> Cc: Suren Baghdasaryan <surenb@google.com> tools/testing/selftests/mm/uffd-common.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) --- a/tools/testing/selftests/mm/uffd-common.c~selftests-mm-free-the-test-context-when-uffd_test_ctx_init-fails-fix +++ a/tools/testing/selftests/mm/uffd-common.c @@ -90,21 +90,29 @@ static int hugetlb_allocate_area(uffd_gl void *area_alias = NULL; char **alloc_area_alias; int mem_fd = uffd_mem_fd_create(size * 2, true); + int ret; *alloc_area = mmap(NULL, size, PROT_READ | PROT_WRITE, (gopts->map_shared ? MAP_SHARED : MAP_PRIVATE) | (is_src ? 0 : MAP_NORESERVE), mem_fd, offset); if (*alloc_area == MAP_FAILED) { + ret = -errno; *alloc_area = NULL; - return -errno; + close(mem_fd); + return ret; } if (gopts->map_shared) { area_alias = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, offset); - if (area_alias == MAP_FAILED) - return -errno; + if (area_alias == MAP_FAILED) { + ret = -errno; + munmap(*alloc_area, size); + *alloc_area = NULL; + close(mem_fd); + return ret; + } } if (is_src) { @@ -341,7 +349,7 @@ int uffd_test_ctx_init(uffd_global_test_ if (ret) { if (errmsg) *errmsg = "memory allocation failed"; - return ret; + goto err_unmap_src; } ret = uffd_test_ops->allocate_area(gopts, (void **) &gopts->area_dst, false); _ ^ permalink raw reply [flat|nested] 15+ messages in thread
* + selftests-mm-free-the-test-context-when-uffd_test_ctx_init-fails-fix.patch added to mm-new branch 2026-12-03 17:15 ` [PATCH v2 4/4] selftests/mm: free the test context when uffd_test_ctx_init() fails Mike Rapoport (Microsoft) ` (2 preceding siblings ...) 2026-12-05 17:09 ` Andrew Morton @ 2026-12-05 17:09 ` Andrew Morton 3 siblings, 0 replies; 15+ messages in thread From: Andrew Morton @ 2026-12-05 17:09 UTC (permalink / raw) To: mm-commits, surenb, rppt, peterx, david, akpm The patch titled Subject: selftests/mm: release partial hugetlb area on allocation failure has been added to the -mm mm-new branch. Its filename is selftests-mm-free-the-test-context-when-uffd_test_ctx_init-fails-fix.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/selftests-mm-free-the-test-context-when-uffd_test_ctx_init-fails-fix.patch This patch will later appear in the mm-new branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Note, mm-new is a provisional staging ground for work-in-progress patches, and acceptance into mm-new is a notification for others take notice and to finish up reviews. Please do not hesitate to respond to review feedback and post updated versions to replace or incrementally fixup patches in mm-new. The mm-new branch of mm.git is not included in linux-next If a few days of testing in mm-new is successful, the patch will me moved into mm.git's mm-unstable branch, which is included in linux-next Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there most days ------------------------------------------------------ From: Andrew Morton <akpm@linux-foundation.org> Subject: selftests/mm: release partial hugetlb area on allocation failure Date: Sat, 05 Dec 2026 08:53:51 -0800 hugetlb_allocate_area() creates a memfd before mapping the test area. If the first mmap() fails, the memfd stays open. If mapping a shared alias fails, the first area also remains mapped. An error allocating the source area returns directly from uffd_test_ctx_init(), bypassing the cleanup added by the preceding patch. A skipped test can therefore leave huge pages reserved for later tests. Close the memfd on both failures, unmap the first area when its alias fails, and enter err_unmap_src for source allocation errors. Preserve the original errno across cleanup. Fault injection of the alias mmap() left one live mapping before this change and none afterward. The first mmap() failure and subsequent normal cases were also checked. The userfaultfd selftests passed 122/122 with CONFIG_USERFAULTFD=y, CONFIG_HUGETLB_PAGE=y and CONFIG_HUGETLBFS=y. CONFIG_CPA_DEBUG was disabled in the test kernel. Link: https://lore.kernel.org/r/20261203-fixes-uffd-sashiko-v2-4-308ce557a7b6@kernel.org Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Cc: Mike Rapoport (Microsoft) <rppt@kernel.org> Cc: David Hildenbrand <david@kernel.org> Cc: Peter Xu <peterx@redhat.com> Cc: Suren Baghdasaryan <surenb@google.com> tools/testing/selftests/mm/uffd-common.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) --- a/tools/testing/selftests/mm/uffd-common.c~selftests-mm-free-the-test-context-when-uffd_test_ctx_init-fails-fix +++ a/tools/testing/selftests/mm/uffd-common.c @@ -90,21 +90,29 @@ static int hugetlb_allocate_area(uffd_gl void *area_alias = NULL; char **alloc_area_alias; int mem_fd = uffd_mem_fd_create(size * 2, true); + int ret; *alloc_area = mmap(NULL, size, PROT_READ | PROT_WRITE, (gopts->map_shared ? MAP_SHARED : MAP_PRIVATE) | (is_src ? 0 : MAP_NORESERVE), mem_fd, offset); if (*alloc_area == MAP_FAILED) { + ret = -errno; *alloc_area = NULL; - return -errno; + close(mem_fd); + return ret; } if (gopts->map_shared) { area_alias = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, offset); - if (area_alias == MAP_FAILED) - return -errno; + if (area_alias == MAP_FAILED) { + ret = -errno; + munmap(*alloc_area, size); + *alloc_area = NULL; + close(mem_fd); + return ret; + } } if (is_src) { @@ -341,7 +349,7 @@ int uffd_test_ctx_init(uffd_global_test_ if (ret) { if (errmsg) *errmsg = "memory allocation failed"; - return ret; + goto err_unmap_src; } ret = uffd_test_ops->allocate_area(gopts, (void **) &gopts->area_dst, false); _ Patches currently in -mm which might be from akpm@linux-foundation.org are kselftest-mm-rename-read_pmd_pagesize-to-pmd_psize-fix.patch selftests-mm-check-madv_cold-and-madv_pageout-on-partly-advised-thps-fix.patch selftests-mm-retain-move-pmd-coverage-and-verify-huge-pmds.patch lib-test_xarray-check-unused-spare-node-after-range-creation.patch selftests-mm-check-that-fsync-reports-hard-poisoned-dirty-page-cache.patch selftests-mm-memory-failure-truncate-poisoned-fuse-page-cache-before-teardown.patch selftests-mm-memory-failure-skip-poisoned-dirty-page-cache-on-nilfs2.patch selftest-mm-memory-failure-keep-page-cache-files-linked-until-cleanup.patch selftest-mm-memory-failure-keep-page-cache-files-linked-until-cleanup-fix.patch selftests-mm-memory-failure-skip-hard-dirty-page-cache-test-on-cephfs.patch selftests-mm-memory-failure-close-dirty-page-cache-files-before-unlinking-them.patch selftests-mm-memory-failure-extend-page-cache-test-files-with-pwrite.patch selftests-mm-memory-failure-write-the-whole-page-cache-test-file-for-omfs.patch selftests-mm-memory-failure-accept-eio-from-close-after-poisoning-dirty-data.patch alloc_tag-introduce-ioctls-to-toggle-allocation-tracepoints-fix-fix.patch alloc_tag-introduce-ioctls-to-toggle-allocation-tracepoints-fix.patch mm-vmscan-scope-freeze-writeback-skip-to-the-reclaim-target.patch mm-vmscan-scope-freeze-writeback-skip-to-the-reclaim-target-fix.patch mm-hmm-refuse-secretmem-vmas-in-hmm_range_fault-fix.patch hugetlbfs-dont-overflow-the-end-of-the-unmap-range-on-32-bit-fix.patch selftests-mm-free-the-test-context-when-uffd_test_ctx_init-fails-fix.patch ipc-msg-clamp-negative-msg_info-usage-counts.patch squashfs-fix-leaked-page-locks-when-page-actor-allocation-fails-fix.patch ocfs2-skip-network-flush-when-no-domain-handlers-are-registered.patch selftests-ptrace-check-that-ptrace_set_syscall_info-rejects-invalid-error-values-fix.patch lib-atomic64-separate-hashed-locks-by-smp-cache-line.patch nvdimm-btt-separate-map-locks-by-smp-cache-line.patch percpu_ref-replace-the-global-switch-lock-with-hashed-locks-fix.patch afs-do-not-put-error-pointer-after-address-lookup-failure.patch drivers-media-v4l2-core-v4l2-vp9c-reduce-inlining.patch ^ permalink raw reply [flat|nested] 15+ messages in thread
end of thread, other threads:[~2026-12-05 17:09 UTC | newest] Thread overview: 15+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-12-03 17:15 [PATCH v2 0/4] userfaultfd: fixes for sashiko reports Mike Rapoport (Microsoft) 2026-12-03 17:15 ` [PATCH v2 1/4] userfaultfd: write-lock the VMA before clearing its uffd state Mike Rapoport (Microsoft) 2026-12-04 7:04 ` + userfaultfd-write-lock-the-vma-before-clearing-its-uffd-state.patch added to mm-new branch Andrew Morton 2026-12-04 9:27 ` [PATCH v2 1/4] userfaultfd: write-lock the VMA before clearing its uffd state Jose A. Perez de Azpillaga 2026-12-03 17:15 ` [PATCH v2 2/4] userfaultfd: mark zero pages moved by UFFDIO_MOVE soft-dirty Mike Rapoport (Microsoft) 2026-12-04 7:04 ` + userfaultfd-mark-zero-pages-moved-by-uffdio_move-soft-dirty.patch added to mm-new branch Andrew Morton 2026-12-04 9:31 ` [PATCH v2 2/4] userfaultfd: mark zero pages moved by UFFDIO_MOVE soft-dirty Jose A. Perez de Azpillaga 2026-12-03 17:15 ` [PATCH v2 3/4] userfaultfd: mark THPs " Mike Rapoport (Microsoft) 2026-12-04 7:04 ` + userfaultfd-mark-thps-moved-by-uffdio_move-soft-dirty.patch added to mm-new branch Andrew Morton 2026-12-04 9:32 ` [PATCH v2 3/4] userfaultfd: mark THPs moved by UFFDIO_MOVE soft-dirty Jose A. Perez de Azpillaga 2026-12-03 17:15 ` [PATCH v2 4/4] selftests/mm: free the test context when uffd_test_ctx_init() fails Mike Rapoport (Microsoft) 2026-12-04 7:04 ` + selftests-mm-free-the-test-context-when-uffd_test_ctx_init-fails.patch added to mm-new branch Andrew Morton 2026-12-04 9:39 ` [PATCH v2 4/4] selftests/mm: free the test context when uffd_test_ctx_init() fails Jose A. Perez de Azpillaga 2026-12-05 17:09 ` Andrew Morton 2026-12-05 17:09 ` + selftests-mm-free-the-test-context-when-uffd_test_ctx_init-fails-fix.patch added to mm-new branch Andrew Morton
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.