[PATCH v2 4/4] selftests/mm: free the test context when uffd_test_ctx_init() fails

From: Mike Rapoport (Microsoft)

Date: Thu Dec 03 2026 - 12:15:51 EST


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@xxxxxxxxxx>
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@xxxxxxxxxx>
---
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