From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796318139; b=TpVlj2X6uluRIJSYKL+NolLmoFzArQmE6rD15zb9jdVE5mQbB2KByCttYLcwvItYd0rp c3mlgvPfJXZdbWR4lTQFFIg+qP/RE0py9ffGn3qRsj6jHc8snMW1MuuMblzFzpX7SdAaS qq1jK/Q0ahu88p/dLsYbE563Nn6zToKI3G8M3Ik8PrDkcG474/kbuimwfFuKPAf49zNKk PlRnBeWHTeWOUiSBSCd6ZDFxQ1MAvMDlE5na3Dd9uFuorXxo0+YiZ30OdJxXjfiFKIy2b zw6+aE0irOy62vd3Nwip0e0BfV51wMzcs5JyLbVmmIqjhjK8cl16AcEWquXWsHX2xyg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796318139; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer; bh=Q+Q/nu2uw2ms0oOiZUeD4KfaNAj0b1YISAS62drZMRo=; b=TGpu4zKaS2jpMzK9xp3Ii0wqcyobJPz/LpuKKoA1il3C1nLenRS7UD1L4n5XUzzca1qG fchoELHfNRHZMkqWBwhEwTbgd7qx+3uoqCFo3Yz72wlEeeyRR1nSQAaqeJiNiJ3fDcRrB 4TC91kaOuYT5VIbSQnLVmhnohE/H4FhaBWu5u3JSr4nbWrDLoaWYb2mfJW6xizb3npa/q 7bZzVWP3Dm6UqR2FmQi/7s+65QBr/yfFkcY0ygtiEPNnC6gv+qAgKHIdQGGFhjwjRPPzS 0XS5utxW209C0+6bnNB0a+IwOUGBu1Ms4RqfCBq4AH0ETcpAOHgKoMdX2Cf6h+HN0zQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=AvdqWo8+; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1DEFB606A688; Thu, 3 Dec 2026 17:15:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=AvdqWo8+ Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5E5A41F0093B; Thu, 3 Dec 2026 17:15:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796318138; bh=Q+Q/nu2uw2ms0oOiZUeD4KfaNAj0b1YISAS62drZMRo=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=AvdqWo8+5gyjtC7XYwNSiuByF/WmeahC/m5ePn/HgpVMKvqeFZwAFAMJXtmzeOrYa /6FpBoHGR1OjyQdedHmFELrZlg2g0eJoYMqXDy33KoxMR7102EVvK7siYHKR6IyOWT SU71xViw9uj8HT7cN8oFSWDnWWKjVgrUfXUxI8VS0zveOHlwb9Kyy1pETea0SEii6N wVStt/2m/OymTpH35cDxuOBY++/w5/9QDy6AyWiAvIhedG0Xi8GFRsYpSl2FuwVNR3 JgXU9ikmE61w7qe9W6HG0iOY/WIrfpjlypg9hyTm1obq2yVWKcIUzWWPUmEvx+fRlq ExcPE237kieYQ== From: "Mike Rapoport (Microsoft)" Date: Thu, 03 Dec 2026 19:15:24 +0200 Subject: [PATCH v2 4/4] selftests/mm: free the test context when uffd_test_ctx_init() fails X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261203-fixes-uffd-sashiko-v2-4-308ce557a7b6@kernel.org> References: <20261203-fixes-uffd-sashiko-v2-0-308ce557a7b6@kernel.org> In-Reply-To: <20261203-fixes-uffd-sashiko-v2-0-308ce557a7b6@kernel.org> To: Andrew Morton , David Hildenbrand Cc: Mike Rapoport , Peter Xu , Suren Baghdasaryan , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, Sashiko X-Mailer: b4 0.16.1-dev 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 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 | 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