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=1796490590; b=oLd65HGjqKClQO3mOZbGhZI8Vh5wCKf1hZ6BytWGy0jma0LJqFB5rGDY6U88MvhAeSvx DRQGF0rv7s/wfVdV1QnU1dIFaElRhqrfdfmcWQdwS4zJMFu4MhNdHD24/UnHOp/eOkvLz ESMMYvHOXQFGwRSvEWzaOQw8ULROIbE8BvQMr/GwaXCN6poSV98ONfxXf+s4qYkTGg4Ed YYJZW+xaPWulY9XUvTKsu1HSa0/B5Out8blLh6fFH7vARXOhgqhUTt2ml0YdARV+uzr9C 1tUrIUHhwNjj9Y+HbJeb557IRBAIxHcxQaynekG3Dl7q18eda5xzfwPgQch754NaaLQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796490590; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-Id: In-Reply-To:References:X-Mailer:Mime-Version:Content-Type: Content-Transfer-Encoding; bh=c/91TLgKE4x0sGksy1k+f+3iWvvX8cFeWqvxhQb8OX4=; b=rDyIE8Mtx0jN4md5XITSZ31pPNrd0O7BdgpiTYdIrzVNxy5hsJ0mw/JHK6/BygeCxHma PCbMpt9GRk8k/eDOp0SA5zoVoXOojUas2UaEWcyVVsNqwUbunb1NGq3nf0x8MkmtRAtGJ mWfLBYUkY3pqxXPMDlEh+hm5WBXoC/2r7cdb8nzp0wIxymt1EneOIPBwVw4NnSzjwL/IC esZyBWUtTEMir17i9x23fN72DW0DTpCcM1RCi7uSZlZTNJ3pOt3sLAJ0DhyMZ07CxgTzL qbTn3AYj2p0RH0vu5WnJDFa1ProBaQ7EC+qQ0uc8HvekIjo5YRYUQ9uqkYqeKGtFyHQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=NInK5WxZ; 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 5DC89606A31F; Sat, 5 Dec 2026 17:09:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=NInK5WxZ Received: by smtp.kernel.org (Postfix) with ESMTPSA id AE3721F0092B; Sat, 5 Dec 2026 17:09:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796490589; bh=c/91TLgKE4x0sGksy1k+f+3iWvvX8cFeWqvxhQb8OX4=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=NInK5WxZL1oG3nsXOAX7YFNeQshALfnrfT/dZei74eJWpA53pLwLvnY2gvSLf0NUV p/qlrBh1ygnWchagIUm5opmwN4hYSeFXvDO7zkW1cv6gy4YHfi3J6EEWDqcFyoVpGg R4XZgeG9TTVpbXJGVhYSfEbjPanZlvYy1yUfF5ss= Date: Sat, 5 Dec 2026 09:09:49 -0800 From: Andrew Morton To: "Mike Rapoport (Microsoft)" Cc: David Hildenbrand , Peter Xu , Suren Baghdasaryan , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, Sashiko Subject: Re: [PATCH v2 4/4] selftests/mm: free the test context when uffd_test_ctx_init() fails Message-Id: <20261205090949.5bae6070d8a30e7da4617535@linux-foundation.org> In-Reply-To: <20261203-fixes-uffd-sashiko-v2-4-308ce557a7b6@kernel.org> References: <20261203-fixes-uffd-sashiko-v2-0-308ce557a7b6@kernel.org> <20261203-fixes-uffd-sashiko-v2-4-308ce557a7b6@kernel.org> X-Mailer: Sylpheed 3.8.0beta1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) 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=US-ASCII Content-Transfer-Encoding: 7bit On Thu, 03 Dec 2026 19:15:24 +0200 "Mike Rapoport (Microsoft)" 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 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@kernel.org Signed-off-by: Andrew Morton Cc: Mike Rapoport (Microsoft) Cc: David Hildenbrand Cc: Peter Xu Cc: Suren Baghdasaryan 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); _