Re: [PATCH 1/2] mm/gup: make mlock() and MAP_POPULATE handle VM_FAULT_RETRY properly
From: Harrison Crosse
Date: Wed Nov 18 2026 - 18:59:39 EST
> Let me take care of this (or find someone more familiar with that code
> that can help).
Thanks for taking this over David!
Harrison
On Wed, Nov 18, 2026 at 6:25 PM David Hildenbrand (Arm)
<david@xxxxxxxxxx> wrote:
>
> On 11/18/26 22:56, Harrison Crosse via B4 Relay wrote:
> > From: Harrison Crosse <harrison@xxxxxxxxxx>
> >
> > If every read of a page of a file mapping fails, mmap(MAP_POPULATE)
> > and mlock() of that mapping do not return: the task spins in
> > __mm_populate(), submitting a new read of the page on each iteration,
> > until it gets a fatal signal. Touching the same page gets SIGBUS, and
> > madvise(MADV_POPULATE_READ) fails with -EFAULT.
> >
> > The first fault on the page reaches filemap_fault() with
> > FAULT_FLAG_ALLOW_RETRY and without FAULT_FLAG_TRIED. filemap_fault()
> > releases the mmap_lock to read the page and, because it released the
> > lock, returns VM_FAULT_RETRY before it looks at the result of the read.
> > It cannot report the error then: a fault that has released the lock
> > returns VM_FAULT_RETRY or VM_FAULT_COMPLETED, and neither carries an
> > error. faultin_page() turns VM_FAULT_RETRY into -EBUSY, so
> > __get_user_pages() stops at that page without an error, and
> > __mm_populate() calls populate_vma_page_range() again at that address,
> > again without FOLL_TRIED. The page fault handlers and
> > __get_user_pages_locked() retry with FAULT_FLAG_TRIED instead, and on
> > that attempt filemap_fault() reads the page with the lock held and
> > returns VM_FAULT_SIGBUS if the read fails.
> >
> > Commit 631426ba1d45 ("mm/madvise: make MADV_POPULATE_(READ|WRITE) handle
> > VM_FAULT_RETRY properly") fixed the same loop for MADV_POPULATE_* by
> > calling __get_user_pages_locked(). That does not fit here:
> > __get_user_pages_locked() retakes the mmap_lock itself and carries on
> > with the GUP flags it was given, while populate_vma_page_range()
> > derives those flags from the VMA that __mm_populate() looks up again,
> > and may skip, after each unlock. Retrying with a NULL locked instead
> > would drop FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_KILLABLE, so a
> > userfaultfd range would fail rather than wait, and folio lock waits
> > would not be killable.
> >
> > Instead, have __mm_populate() remember the address of the page whose
> > fault released the mmap_lock. When the next iteration starts at that
> > address, fault in that page alone with FOLL_TRIED, as
> > __get_user_pages_locked() does, then continue with the rest of the
> > range as before. The retry reads the page with the mmap_lock held, as
> > filemap_fault() did for a page that was not uptodate before v5.1, and
> > as the other GUP retries do. With a persistent read error the retry
> > fails with -EFAULT: mlock() returns -ENOMEM
> > (__mlock_posix_error_return() maps -EFAULT to it), and MAP_POPULATE,
> > which ignores populate errors, returns the mapping. A fatal signal
> > still ends the loop, with -EINTR from __get_user_pages() or, if it
> > interrupts the FOLL_TRIED read, with -EFAULT. The other callers of
> > populate_vma_page_range() pass a NULL locked, so they cannot get
> > VM_FAULT_RETRY and are unchanged.
> >
> > Reproduced with ext4 on dm-linear over a loop device, with the eight
> > sectors of one page of a file mapped to the dm "error" target, on arm64
> > (Debian's 6.12.116 and v7.4-rc3) and x86_64 (v7.4-rc3): mmap(MAP_POPULATE)
> > and mlock() of the file were still running in the kernel after 5 seconds
> > and ended when sent SIGKILL. On 6.12.116, the block layer tracepoints
> > showed about 280,000 bios a second being submitted and failed.
> >
> > The analysis, the patch and this changelog were prepared with the help
> > of an AI coding assistant (Claude).
>
> Yes, that's rather obvious from looking at it, thanks for documenting that :)
>
> Thanks for the report!
>
> [...]
>
> > + *
> > + * filemap_fault() reports a read error only when
> > + * FAULT_FLAG_TRIED is set: on a first attempt it releases the
> > + * mmap_lock for the read and returns VM_FAULT_RETRY whatever
> > + * the result. So when the last call released the lock at
> > + * nstart, retry that page with FOLL_TRIED, and only that page,
> > + * so that later pages still get a first attempt that releases
> > + * the lock for the read. Compare the address because nstart
> > + * moves past that page if it is no longer mapped, or its VMA
> > + * is skipped, when the lock is retaken.
> > */
> > - ret = populate_vma_page_range(vma, nstart, nend, &locked);
> > + tried = retry && nstart == retry_addr;
> > + ret = __populate_vma_page_range(vma, nstart,
> > + tried ? nstart + PAGE_SIZE : nend,
> > + &locked, tried);
>
> Yeah, that's quite the ugly code. Let me take care of this (or find someone more
> familiar with that code that can help).
>
> @Yeoreum, do you have some spare cycles?
>
>
> --
> Cheers,
>
> David