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=1796360231; b=BfxCMLGVX+PiVPykbIt+PK7iT+2nR0YrH79oXEHIqoErfK80eT+VvMjiRzhQ/DAkWTLF cuT7ach39ojMlOIp2Va0skLVuaAWcKMQVXGqXZthhX9oBR2CDGWvbrmpAdW4OHsDVulm+ /LKXB8Z4eXACFdYOs4316vMh4Q3u91xuVvphtMNK6DPpBrAjP8FjRg6c/E+p97PZI4kzU Vu20SeMrT/G7s0Xv1PO3/qFDBbD41owpywAxPBFRhFAbtQBgQDsNzyas+C6U4wb4fW8qh mYztO2CoT9DKZuJJP87i9BIglPZUMwHergz/841DWXEtzyXKpHCZ1dgZCNHfBMmGNxQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796360231; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=5djg98k6J2L5YzafGgoQC4+QahXh7w1U73heUy5DavI=; b=MR1sXTrxyozvaazIbyfYYIpfisN09Bnri5JRmUaCl+iUQ8i60SPV65iIj7PGzJm4aotw yvJzjsx+sAwSanUU9mr/f8LvsqhVv7cgFJv13OHme1g62QBblIwyCxnZssL+F8zsKiZED +4NpCOuuyPhrRfuPavkkUgjWQq7pYKQZ5eaEARyc7QcaVjJdx9fz8lAJjaF6nKFUli30N dUNuqppk+FbLyjCcZOemweZ4wPPhUWvhJJEivqr3IteBHik/uVp1shs8xUcf7hBLJ7rJZ BXM6A40r1Pw5BYXPH/AWG2MZFmPIf0dUMlAwsUM730x11D+6EwzCyj08yTxEvMjxrxQ== 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=gvcgDo9C; 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 6ABEA606A322 for ; Fri, 4 Dec 2026 04:57:11 +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=gvcgDo9C Received: by smtp.kernel.org (Postfix) with ESMTPSA id A1EF81F000C2; Fri, 4 Dec 2026 04:57:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796360231; bh=5djg98k6J2L5YzafGgoQC4+QahXh7w1U73heUy5DavI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=gvcgDo9Ckii3unWKABphlRsBi6Tpwwkk9tK6gHb3IyLoiFJPiFI5PYcDLqsQHbYCG riiJRz1NZnbyVjpkRVflSyt5VZ3T/c7wmipnqZ/USMV+haR9YZiht0LQ3e1abvFtPk DZiY1GobwdlwbZBccVX9e/ogr5q5BB6s2C6yAl8iUuk5mqgYZp5i+W9eOEfqULaz7X J+zxTpHjH5AfwOW3LDjIcZpTco7N4XDE+dCfKzIXiQzY+ztwqqr9O0fhhEDshJ5Qwp V5fmkr03LQuiP9K9TaDf1qvG8H8zqcf6SnNrCTB9AOftpT0JDEgy3zObk5y3kZVuKn 5R7JPn+7+Jh6A== From: Sasha Levin To: stable@vger.kernel.org Cc: "Mike Rapoport (Microsoft)" , Andrew Morton , Noah Weinberger , Tal Zussman , "Liam R. Howlett" , Lorenzo Stoakes , David Hildenbrand , Jann Horn , Pedro Falcato , Peter Xu , Vlastimil Babka , Sasha Levin Subject: [PATCH 6.12.y] mm/vma: undo userfaultfd unmap preparation when unmapping fails Date: Thu, 3 Dec 2026 23:57:08 -0500 Message-ID: <20261204044656.2606352-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <2026120246-headed-thirsty-da7d@gregkh> References: <2026120246-headed-thirsty-da7d@gregkh> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: "Mike Rapoport (Microsoft)" [ Upstream commit f987d9f38a46216d45326b7a61c619c574b45837 ] vms_gather_munmap_vmas() collects VMAs to unmap and calls userfaultfd_unmap_prep() for each of these VMAs. This takes a reference to the userfaultfd context, raises ctx->mmap_changing and queues an UFFD_EVENT_UNMAP on the caller's list. If gathering fails half way, e.g. because one of the VMAs is sealed, or the operation fails after the VMAs were gathered, the original mappings stay intact but the queued events remain on the list. Depending on the caller they are either delivered although nothing was unmapped (munmap(), mremap(), mmap(MAP_FIXED)), or silently dropped (brk()). In the latter case the userfaultfd context and the mm_struct it pins are leaked and mmap_changing is never lowered, so UFFDIO_COPY, UFFDIO_ZEROPAGE and all other ioctls that check mmap_changing fail with -EAGAIN forever. With mseal() this is trivially triggered by an unprivileged process. Collect the events in do_vmi_align_munmap() on a local list and splice it to the caller's list only when the unmap completes, because mremap() may already have events for earlier unmaps on that list. Drop the pending events with a new userfaultfd_unmap_abort() helper when do_vmi_align_munmap() or __mmap_setup() fail. Link: https://lore.kernel.org/20261101-uffd-unmap-abort-v1-1-1588d7a7c61a@kernel.org Fixes: 65ac132027a8 ("userfaultfd: fix regression in userfaultfd_unmap_prep()") Signed-off-by: Mike Rapoport (Microsoft) Signed-off-by: Andrew Morton Reported-by: Noah Weinberger Reviewed-by: Tal Zussman Tested-by: Tal Zussman Cc: Liam R. Howlett Cc: Lorenzo Stoakes Cc: David Hildenbrand Cc: Jann Horn Cc: Pedro Falcato Cc: Peter Xu Cc: Vlastimil Babka Cc: [ sashal: Moved the UFFD unmap-abort calls from upstream's __mmap_setup() failure paths into __mmap_region(), calling abort only when clear_ptes is still set, plus a return error; to avoid fallthrough. ] Signed-off-by: Sasha Levin --- fs/userfaultfd.c | 19 +++++++++++++++++++ include/linux/userfaultfd_k.h | 5 +++++ mm/mmap.c | 4 ++++ mm/vma.c | 14 +++++++++++++- tools/testing/vma/vma_internal.h | 4 ++++ 5 files changed, 45 insertions(+), 1 deletion(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 77b295cb39906..3f836108047a8 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -857,6 +857,25 @@ void userfaultfd_unmap_complete(struct mm_struct *mm, struct list_head *uf) } } +void userfaultfd_unmap_abort(struct list_head *uf) +{ + struct userfaultfd_unmap_ctx *unmap_ctx, *n; + + if (!uf) + return; + + list_for_each_entry_safe(unmap_ctx, n, uf, list) { + struct userfaultfd_ctx *ctx = unmap_ctx->ctx; + + atomic_dec(&ctx->mmap_changing); + VM_WARN_ON_ONCE(atomic_read(&ctx->mmap_changing) < 0); + userfaultfd_ctx_put(ctx); + + list_del(&unmap_ctx->list); + kfree(unmap_ctx); + } +} + static int userfaultfd_release(struct inode *inode, struct file *file) { struct userfaultfd_ctx *ctx = file->private_data; diff --git a/include/linux/userfaultfd_k.h b/include/linux/userfaultfd_k.h index 630a46b62dd81..a8b42e03b09ec 100644 --- a/include/linux/userfaultfd_k.h +++ b/include/linux/userfaultfd_k.h @@ -279,6 +279,7 @@ extern int userfaultfd_unmap_prep(struct vm_area_struct *vma, unsigned long start, unsigned long end, struct list_head *uf); extern void userfaultfd_unmap_complete(struct mm_struct *mm, struct list_head *uf); +void userfaultfd_unmap_abort(struct list_head *uf); extern bool userfaultfd_wp_unpopulated(struct vm_area_struct *vma); extern bool userfaultfd_wp_async(struct vm_area_struct *vma); @@ -401,6 +402,10 @@ static inline void userfaultfd_unmap_complete(struct mm_struct *mm, { } +static inline void userfaultfd_unmap_abort(struct list_head *uf) +{ +} + static inline bool uffd_disable_fault_around(struct vm_area_struct *vma) { return false; diff --git a/mm/mmap.c b/mm/mmap.c index e2c51c6ed83d2..388d0ab2c4907 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1595,8 +1595,12 @@ static unsigned long __mmap_region(struct file *file, unsigned long addr, vm_unacct_memory(charged); abort_munmap: + if (vms.clear_ptes) + userfaultfd_unmap_abort(uf); vms_abort_munmap_vmas(&vms, &mas_detach); + return error; gather_failed: + userfaultfd_unmap_abort(uf); return error; } diff --git a/mm/vma.c b/mm/vma.c index 6f50712eb9b6d..4d2854cebef57 100644 --- a/mm/vma.c +++ b/mm/vma.c @@ -1364,9 +1364,18 @@ int do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, mt_init_flags(&mt_detach, vmi->mas.tree->ma_flags & MT_FLAGS_LOCK_MASK); mt_on_stack(mt_detach); struct vma_munmap_struct vms; + struct list_head *uf_unmaps; + LIST_HEAD(uf_new_unmaps); int error; - init_vma_munmap(&vms, vmi, vma, start, end, uf, unlock); + /* + * @uf may already hold events of earlier successful unmaps, e.g. in + * mremap(). + * Collect new events on a separate list so they can be dropped if this + * unmap fails, and splice them into @uf on success. + */ + uf_unmaps = uf ? &uf_new_unmaps : NULL; + init_vma_munmap(&vms, vmi, vma, start, end, uf_unmaps, unlock); error = vms_gather_munmap_vmas(&vms, &mas_detach); if (error) goto gather_failed; @@ -1376,12 +1385,15 @@ int do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, goto clear_tree_failed; /* Point of no return */ + if (uf) + list_splice_tail(&uf_new_unmaps, uf); vms_complete_munmap_vmas(&vms, &mas_detach); return 0; clear_tree_failed: reattach_vmas(&mas_detach); gather_failed: + userfaultfd_unmap_abort(&uf_new_unmaps); validate_mm(mm); return error; } diff --git a/tools/testing/vma/vma_internal.h b/tools/testing/vma/vma_internal.h index 213e40afa8ee3..a472f1b1c4fb2 100644 --- a/tools/testing/vma/vma_internal.h +++ b/tools/testing/vma/vma_internal.h @@ -834,6 +834,10 @@ static inline int userfaultfd_unmap_prep(struct vm_area_struct *vma, return 0; } +static inline void userfaultfd_unmap_abort(struct list_head *) +{ +} + static inline void mmap_write_downgrade(struct mm_struct *) { } -- 2.53.0