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=1796382967; b=RRFnZFKVquqI70YSp8eU2z+ubPX2F18IIuHj/7g7TBE7hY2s7TRn6lTt1vJaOCFYUHmL DRC7WM8qkvcY9XUPgC9PUT5ujn2OTQS1Xued0JEKKikuzV+z7BAWVKIbM1B/g2PK7pgbb vXXRBe6gYBiJyteBKEsE0mmvZKXDR7li39nay/Ozyl+9aMwBaInJop+AiWnGBbTb5+w8p n+gMtVtOaq/+MzTDRKxUTcUhjHlP8cAELjxUSqhInOL8VEEbek73erOxuXKVQ3tmUInLH Einok6oP7QTx9H4RbjVIw8smwunszmz17WkpIyTRkuQ1ks2l9oEOdu7rX4ev98U8tRw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796382967; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=sgQVotQikcn3zaFbGVZq8c3ibsILT+WhKUXyms8F86U=; b=4tvwWFG3dPIuk/gBW1N/DhFsf6wYn7L5kzPKpH2zcQT/zYOWUYua4wJqt6rIY4YwxqnJ 1JCpI8zUwIm87TZwJb4/gJcRupZDoxWmOZYaqLGK1PeIIM9uSuhwNf1/2hb59q6CweoHC RnjpjgmvdWtBtFKko12lslnYC1W1EkGi3FGrEE7d+GDJFhyF4pmZQhZiGfOF5vDceyreR 1GKVm0dkLRbP5XDKhUmQdNH9xCa0fWboscBMAtqn8E4NReXZ7ZXv1KJAGJ59RLG5X2D5u oeRw60CgI5W35Ds6p5HsgFapBpAFb2is3RaehGkhJdGAYvA3Dvx6VaRBzTDkQUDvw9w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=WrTCLCdg; 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 AEFB0606A322; Fri, 4 Dec 2026 11:16:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=WrTCLCdg Received: by smtp.kernel.org (Postfix) with ESMTPSA id 339E61F000C2; Fri, 4 Dec 2026 11:16:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796382967; bh=sgQVotQikcn3zaFbGVZq8c3ibsILT+WhKUXyms8F86U=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=WrTCLCdg8u8aoCLsTqne/NWgABFd6eLnPSK8wVDz/PolCd8iDwG1q01IiXAd77A5D 5+aqL71A39/Tw2+bDqsdz08SJMy+VTsBmxsP1GG8gVhDGSEoTb1l4q7t1z6/XeG2UP /sANKet0E93sZcD/YhLLcHoOtboG09SZy5iN2vS4= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, "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.18 668/703] mm/vma: undo userfaultfd unmap preparation when unmapping fails Date: Fri, 4 Dec 2026 19:09:27 +0900 Message-ID: <20261204100645.566965347@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100627.522266145@linuxfoundation.org> References: <20261204100627.522266145@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.18-stable review patch. If anyone has any objections, please let me know. ------------------ 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 userfaultfd_unmap_abort() into fs/userfaultfd.c and its test stub into vma_internal.h, and kept the older may_expand_vm(map->vm_flags) form in __mmap_setup(). ] Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- fs/userfaultfd.c | 19 +++++++++++++++++++ include/linux/userfaultfd_k.h | 5 +++++ mm/vma.c | 28 +++++++++++++++++++++++----- tools/testing/vma/vma_internal.h | 4 ++++ 4 files changed, 51 insertions(+), 5 deletions(-) --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -863,6 +863,25 @@ void userfaultfd_unmap_complete(struct m } } +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; --- a/include/linux/userfaultfd_k.h +++ b/include/linux/userfaultfd_k.h @@ -271,6 +271,7 @@ extern int userfaultfd_unmap_prep(struct 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); @@ -397,6 +398,10 @@ static inline void userfaultfd_unmap_com { } +static inline void userfaultfd_unmap_abort(struct list_head *uf) +{ +} + static inline bool uffd_disable_fault_around(struct vm_area_struct *vma) { return false; --- a/mm/vma.c +++ b/mm/vma.c @@ -1592,9 +1592,18 @@ int do_vmi_align_munmap(struct vma_itera 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; @@ -1604,12 +1613,15 @@ int do_vmi_align_munmap(struct vma_itera 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; } @@ -2473,7 +2485,7 @@ static int __mmap_setup(struct mmap_stat if (error) { /* On error VMAs will already have been reattached. */ vms->nr_pages = 0; - return error; + goto abort_unmaps; } map->next = vms->next; @@ -2483,8 +2495,10 @@ static int __mmap_setup(struct mmap_stat } /* Check against address space limit. */ - if (!may_expand_vm(map->mm, map->vm_flags, map->pglen - vms->nr_pages)) - return -ENOMEM; + if (!may_expand_vm(map->mm, map->vm_flags, map->pglen - vms->nr_pages)) { + error = -ENOMEM; + goto abort_unmaps; + } /* Private writable mapping: check memory availability. */ if (accountable_mapping(map->file, map->vm_flags)) { @@ -2494,7 +2508,7 @@ static int __mmap_setup(struct mmap_stat if (uncharged) { error = security_vm_enough_memory_mm(map->mm, uncharged); if (error) - return error; + goto abort_unmaps; } /* Our new mapping owns the entire charge now. */ @@ -2512,6 +2526,10 @@ static int __mmap_setup(struct mmap_stat vms_clean_up_area(vms, &map->mas_detach); return 0; + +abort_unmaps: + userfaultfd_unmap_abort(uf); + return error; } --- a/tools/testing/vma/vma_internal.h +++ b/tools/testing/vma/vma_internal.h @@ -1274,6 +1274,10 @@ static inline void userfaultfd_unmap_com { } +static inline void userfaultfd_unmap_abort(struct list_head *uf) +{ +} + # define ACCESS_PRIVATE(p, member) ((p)->member) static inline bool mm_flags_test(int flag, const struct mm_struct *mm)