* [PATCH 0/4] userfaultfd: fixes for sashiko reports
@ 2026-12-02 18:20 Mike Rapoport (Microsoft)
2026-12-02 18:20 ` [PATCH 1/4] userfaultfd: write-lock the VMA before clearing its uffd state Mike Rapoport (Microsoft)
` (3 more replies)
0 siblings, 4 replies; 6+ messages in thread
From: Mike Rapoport (Microsoft) @ 2026-12-02 18:20 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>
---
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 | 11 ++++++++---
3 files changed, 14 insertions(+), 3 deletions(-)
---
base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
change-id: 20261202-fixes-uffd-sashiko-fb173c1557eb
--
Sincerely yours,
Mike.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 1/4] userfaultfd: write-lock the VMA before clearing its uffd state
2026-12-02 18:20 [PATCH 0/4] userfaultfd: fixes for sashiko reports Mike Rapoport (Microsoft)
@ 2026-12-02 18:20 ` Mike Rapoport (Microsoft)
2026-12-02 18:20 ` [PATCH 2/4] userfaultfd: mark zero pages moved by UFFDIO_MOVE soft-dirty Mike Rapoport (Microsoft)
` (2 subsequent siblings)
3 siblings, 0 replies; 6+ messages in thread
From: Mike Rapoport (Microsoft) @ 2026-12-02 18:20 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")
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] 6+ messages in thread
* [PATCH 2/4] userfaultfd: mark zero pages moved by UFFDIO_MOVE soft-dirty
2026-12-02 18:20 [PATCH 0/4] userfaultfd: fixes for sashiko reports Mike Rapoport (Microsoft)
2026-12-02 18:20 ` [PATCH 1/4] userfaultfd: write-lock the VMA before clearing its uffd state Mike Rapoport (Microsoft)
@ 2026-12-02 18:20 ` Mike Rapoport (Microsoft)
2026-12-02 18:20 ` [PATCH 3/4] userfaultfd: mark THPs " Mike Rapoport (Microsoft)
2026-12-02 18:20 ` [PATCH 4/4] selftests/mm: free the test context when uffd_test_ctx_init() fails Mike Rapoport (Microsoft)
3 siblings, 0 replies; 6+ messages in thread
From: Mike Rapoport (Microsoft) @ 2026-12-02 18:20 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")
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] 6+ messages in thread
* [PATCH 3/4] userfaultfd: mark THPs moved by UFFDIO_MOVE soft-dirty
2026-12-02 18:20 [PATCH 0/4] userfaultfd: fixes for sashiko reports Mike Rapoport (Microsoft)
2026-12-02 18:20 ` [PATCH 1/4] userfaultfd: write-lock the VMA before clearing its uffd state Mike Rapoport (Microsoft)
2026-12-02 18:20 ` [PATCH 2/4] userfaultfd: mark zero pages moved by UFFDIO_MOVE soft-dirty Mike Rapoport (Microsoft)
@ 2026-12-02 18:20 ` Mike Rapoport (Microsoft)
2026-12-02 18:20 ` [PATCH 4/4] selftests/mm: free the test context when uffd_test_ctx_init() fails Mike Rapoport (Microsoft)
3 siblings, 0 replies; 6+ messages in thread
From: Mike Rapoport (Microsoft) @ 2026-12-02 18:20 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")
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] 6+ messages in thread
* [PATCH 4/4] selftests/mm: free the test context when uffd_test_ctx_init() fails
2026-12-02 18:20 [PATCH 0/4] userfaultfd: fixes for sashiko reports Mike Rapoport (Microsoft)
` (2 preceding siblings ...)
2026-12-02 18:20 ` [PATCH 3/4] userfaultfd: mark THPs " Mike Rapoport (Microsoft)
@ 2026-12-02 18:20 ` Mike Rapoport (Microsoft)
2026-12-02 19:18 ` sashiko-bot
3 siblings, 1 reply; 6+ messages in thread
From: Mike Rapoport (Microsoft) @ 2026-12-02 18:20 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 or closing the userfaultfd. 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".
Call uffd_test_ctx_clear() when uffd_test_ctx_init() fails, and
initialize gopts->uffd to -1 so that uffd_test_ctx_clear() doesn't close
fd 0 when the userfaultfd is not opened yet.
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 | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/tools/testing/selftests/mm/uffd-common.c b/tools/testing/selftests/mm/uffd-common.c
index a76851aced4d0..284cd8a9150d5 100644
--- a/tools/testing/selftests/mm/uffd-common.c
+++ b/tools/testing/selftests/mm/uffd-common.c
@@ -325,6 +325,7 @@ int uffd_test_ctx_init(uffd_global_test_opts_t *gopts, uint64_t features, const
gopts->area_src_alias = NULL;
gopts->area_dst_alias = NULL;
gopts->area_remap = NULL;
+ gopts->uffd = -1;
if (uffd_test_case_ops && uffd_test_case_ops->pre_alloc) {
ret = uffd_test_case_ops->pre_alloc(gopts, errmsg);
@@ -337,20 +338,20 @@ int uffd_test_ctx_init(uffd_global_test_opts_t *gopts, uint64_t features, const
if (ret) {
if (errmsg)
*errmsg = "memory allocation failed";
- return ret;
+ goto err_clear;
}
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_clear;
}
ret = userfaultfd_open(gopts, &features);
if (ret) {
if (errmsg)
*errmsg = "possible lack of privilege";
- return ret;
+ goto err_clear;
}
gopts->count_verify = malloc(gopts->nr_pages * sizeof(unsigned long long));
@@ -399,6 +400,10 @@ int uffd_test_ctx_init(uffd_global_test_opts_t *gopts, uint64_t features, const
err("pipe");
return 0;
+
+err_clear:
+ uffd_test_ctx_clear(gopts);
+ return ret;
}
void wp_range(int ufd, __u64 start, __u64 len, bool wp)
--
2.53.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH 4/4] selftests/mm: free the test context when uffd_test_ctx_init() fails
2026-12-02 18:20 ` [PATCH 4/4] selftests/mm: free the test context when uffd_test_ctx_init() fails Mike Rapoport (Microsoft)
@ 2026-12-02 19:18 ` sashiko-bot
0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-12-02 19:18 UTC (permalink / raw)
To: Mike Rapoport (Microsoft), Andrew Morton, David Hildenbrand
Cc: Peter Xu, Suren Baghdasaryan, linux-mm
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] selftests/mm: uninitialized gopts fields used in
uffd_test_ctx_clear()
--
commit 8ee91369181d6477837e66884f6a29b9ebb0157e
Author: Mike Rapoport (Microsoft) <rppt@kernel.org>
selftests/mm: free the test context when uffd_test_ctx_init() fails
This commit cleans up the test context via uffd_test_ctx_clear() when
uffd_test_ctx_init() fails and initializes gopts->uffd to -1.
> diff --git a/tools/testing/selftests/mm/uffd-common.c b/tools/testing/selftests/mm/uffd-common.c
> index a76851aced4d0..284cd8a9150d5 100644
> --- a/tools/testing/selftests/mm/uffd-common.c
> +++ b/tools/testing/selftests/mm/uffd-common.c
> @@ -325,6 +325,7 @@ int uffd_test_ctx_init(uffd_global_test_opts_t *gopts, uint64_t features, const
> gopts->area_src_alias = NULL;
> gopts->area_dst_alias = NULL;
> gopts->area_remap = NULL;
> + gopts->uffd = -1;
>
> if (uffd_test_case_ops && uffd_test_case_ops->pre_alloc) {
> ret = uffd_test_case_ops->pre_alloc(gopts, errmsg);
> @@ -337,20 +338,20 @@ int uffd_test_ctx_init(uffd_global_test_opts_t *gopts, uint64_t features, const
> if (ret) {
> if (errmsg)
> *errmsg = "memory allocation failed";
> - return ret;
> + goto err_clear;
> }
>
> 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_clear;
> }
>
> ret = userfaultfd_open(gopts, &features);
> if (ret) {
> if (errmsg)
> *errmsg = "possible lack of privilege";
> - return ret;
> + goto err_clear;
> }
>
> gopts->count_verify = malloc(gopts->nr_pages * sizeof(unsigned long long));
> @@ -399,6 +400,10 @@ int uffd_test_ctx_init(uffd_global_test_opts_t *gopts, uint64_t features, const
> err("pipe");
>
> return 0;
> +
> +err_clear:
> + uffd_test_ctx_clear(gopts);
> + return ret;
> }
[Severity: Low]
Can jumping to err_clear cause uffd_test_ctx_clear() to use uninitialized
gopts->count_verify, gopts->pipefd, gopts->area_src, and gopts->area_dst
pointers?
In uffd-stress.c, main() allocates gopts with malloc() without zeroing it.
If allocate_area(), post_alloc(), or userfaultfd_open() fails,
uffd_test_ctx_clear() runs before gopts->count_verify and gopts->pipefd
are assigned (and shmem_allocate_area() can also fail before assigning
*alloc_area), passing garbage pointers to close(), free(), and munmap().
Should uffd_test_ctx_init() also initialize those four fields to NULL at
entry alongside gopts->uffd = -1?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202-fixes-uffd-sashiko-v1-0-a6f5a874334a@kernel.org?part=4
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-12-02 19:18 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 18:20 [PATCH 0/4] userfaultfd: fixes for sashiko reports Mike Rapoport (Microsoft)
2026-12-02 18:20 ` [PATCH 1/4] userfaultfd: write-lock the VMA before clearing its uffd state Mike Rapoport (Microsoft)
2026-12-02 18:20 ` [PATCH 2/4] userfaultfd: mark zero pages moved by UFFDIO_MOVE soft-dirty Mike Rapoport (Microsoft)
2026-12-02 18:20 ` [PATCH 3/4] userfaultfd: mark THPs " Mike Rapoport (Microsoft)
2026-12-02 18:20 ` [PATCH 4/4] selftests/mm: free the test context when uffd_test_ctx_init() fails Mike Rapoport (Microsoft)
2026-12-02 19:18 ` sashiko-bot
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.