From: Andrew Morton <akpm@linux-foundation.org>
To: "Mike Rapoport (Microsoft)" <rppt@kernel.org>
Cc: David Hildenbrand <david@kernel.org>,
Peter Xu <peterx@redhat.com>,
Suren Baghdasaryan <surenb@google.com>,
linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
linux-mm@kvack.org, Sashiko <sashiko-bot@kernel.org>
Subject: Re: [PATCH v2 4/4] selftests/mm: free the test context when uffd_test_ctx_init() fails
Date: Sat, 5 Dec 2026 09:09:49 -0800 [thread overview]
Message-ID: <20261205090949.5bae6070d8a30e7da4617535@linux-foundation.org> (raw)
In-Reply-To: <20261203-fixes-uffd-sashiko-v2-4-308ce557a7b6@kernel.org>
On Thu, 03 Dec 2026 19:15:24 +0200 "Mike Rapoport (Microsoft)" <rppt@kernel.org> 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@linux-foundation.org>
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 <akpm@linux-foundation.org>
Cc: Mike Rapoport (Microsoft) <rppt@kernel.org>
Cc: David Hildenbrand <david@kernel.org>
Cc: Peter Xu <peterx@redhat.com>
Cc: Suren Baghdasaryan <surenb@google.com>
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);
_
prev parent reply other threads:[~2026-12-05 17:09 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 17:15 [PATCH v2 0/4] userfaultfd: fixes for sashiko reports Mike Rapoport (Microsoft)
2026-12-03 17:15 ` [PATCH v2 1/4] userfaultfd: write-lock the VMA before clearing its uffd state Mike Rapoport (Microsoft)
2026-12-04 9:27 ` Jose A. Perez de Azpillaga
2026-12-03 17:15 ` [PATCH v2 2/4] userfaultfd: mark zero pages moved by UFFDIO_MOVE soft-dirty Mike Rapoport (Microsoft)
2026-12-04 9:31 ` Jose A. Perez de Azpillaga
2026-12-03 17:15 ` [PATCH v2 3/4] userfaultfd: mark THPs " Mike Rapoport (Microsoft)
2026-12-04 9:32 ` Jose A. Perez de Azpillaga
2026-12-03 17:15 ` [PATCH v2 4/4] selftests/mm: free the test context when uffd_test_ctx_init() fails Mike Rapoport (Microsoft)
2026-12-04 9:39 ` Jose A. Perez de Azpillaga
2026-12-05 17:09 ` Andrew Morton [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261205090949.5bae6070d8a30e7da4617535@linux-foundation.org \
--to=akpm@linux-foundation.org \
--cc=david@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=peterx@redhat.com \
--cc=rppt@kernel.org \
--cc=sashiko-bot@kernel.org \
--cc=surenb@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®