Re: [PATCH v2 05/35] mm/hugetlb: report reservations that we actually added from hugetlb_reserve_pages()

From: Jose A. Perez de Azpillaga

Date: Fri Dec 04 2026 - 05:30:51 EST


On 12/3/26 12:37 PM, David Hildenbrand (Arm) wrote:
> memfd_alloc_folio() uses hugetlb_reserve_pages() to reserve the index
> of the folio it is about to allocate. If allocating the folio or adding
> it to the page cache fails, it undoes that reservation, but only if
> hugetlb_reserve_pages() reported that it actually performed the
> reservation.
>
> If the index was already reserved, for example through an mmap() of the
> same file, memfd_alloc_folio() must not undo that reservation.
>
> However, if hugetlb_reserve_pages() races with another reservation for
> the same index, region_add_limited() can reserve fewer pages than
> region_chg() counted. hugetlb_reserve_pages() will then hand back the
> surplus charges, but still report the number of pages that region_chg()
> counted.
>
> Consequently, after racing with hugetlbfs_file_mmap() reserving the same
> index, memfd_alloc_folio() will undo the reservation of the mmap() caller
> on error, and a later fault at that index can fail with SIGBUS.
>
> Reproduced by artificially delaying hugetlb_reserve_pages() after
> region_chg() during IOMMU_IOAS_MAP_FILE of a hugetlb memfd, while another
> process mmap()s the memfd, and failing the page cache insertion with
> fault injection. Once all unreserved huge pages are in use, the mmap()
> caller gets SIGBUS on fault.
>
> Let's fix it by only reporting the reservations that we actually added.
> Other callers are not affected, as they only care about error vs.
> success, not the actual number of reserved pages.
>
> This has low urgency, as it requires an allocation failure after racing
> with an mmap() of the same file.
>
> Fixes: 717cf9357325 ("mm/memfd: reserve hugetlb folios before allocation")
> Reported-by: Sashiko <sashiko-bot@xxxxxxxxxx>
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>

LGTM:

Reviewed-by: Jose A. Perez de Azpillaga <azpijr@xxxxxxxxx>

--
cheers,
jose a. p-a