Re: [PATCH] mm/vma: undo userfaultfd unmap preparation when unmapping fails

From: Lorenzo Stoakes (ARM)

Date: Tue Dec 01 2026 - 09:57:01 EST


On Sun, Nov 01, 2026 at 10:24:02AM +0200, Mike Rapoport (Microsoft) wrote:
> 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.

Great... :)

>
> 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.
>
> Fixes: 65ac132027a88 ("userfaultfd: fix regression in userfaultfd_unmap_prep()")
> Reported-by: Noah Weinberger <NoahWeinberger2003@xxxxxxxxx>
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Mike Rapoport (Microsoft) <rppt@xxxxxxxxxx>

Ahem. I see this got merged already without any input from VMA people :)

The AI slop wave is making the workload nearly unmanageable, so it's easy
to miss stuff, you are welcome to nag though.

This gets fixed next cycle when the VMA tree becomes independent, but let's
try to avoid this for the remainder of the cycle please!

I wonder if we could have avoided propagating this terrible linked list
stuff here. But ah well, too late.

Maybe not possible for a backport but would have been good to explore at
least!

--
Cheers, Lorenzo