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 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) --- 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