Re: [PATCH v2 4/4] selftests/mm: free the test context when uffd_test_ctx_init() fails
From: Andrew Morton
Date: Sat Dec 05 2026 - 12:09:53 EST
On Thu, 03 Dec 2026 19:15:24 +0200 "Mike Rapoport (Microsoft)" <rppt@xxxxxxxxxx> 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@xxxxxxxxxxxxxxxxxxxx>
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@xxxxxxxxxx
Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Cc: Mike Rapoport (Microsoft) <rppt@xxxxxxxxxx>
Cc: David Hildenbrand <david@xxxxxxxxxx>
Cc: Peter Xu <peterx@xxxxxxxxxx>
Cc: Suren Baghdasaryan <surenb@xxxxxxxxxx>
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);
_