Re: [PATCH v2 02/35] mm/hugetlb: restore the reservation if vma_needs_reservation() fails
From: Jose A. Perez de Azpillaga
Date: Fri Dec 04 2026 - 05:18:14 EST
On 12/3/26 12:37 PM, David Hildenbrand (Arm) wrote:
> When we fault in a folio in a shared hugetlb mapping at an index that is
> not reserved yet, for example because the mapping was created with
> MAP_NORESERVE, alloc_hugetlb_folio() is responsible for performing the
> hugetlb reservation for the folio's index in the reserve map.
>
> If something goes wrong before adding the folio to the page cache,
> restore_reserve_on_error() will call vma_needs_reservation(), which can
> fail to allocate a file_region in corner cases.
>
> In that case, we will leave the reserve map unchanged and free the folio
> without HPG_restore_reserve. Consequently, nothing backs the reservation.
> The same happens on a min_size= mount if alloc_hugetlb_folio() took the
> reservation from the subpool minimum: the folio then has
> HPG_restore_reserve, but we clear it when vma_needs_reservation() fails.
>
> When we later try again to allocate+populate the page cache, we will
> consume the reservation for that index in the reserve map, although we
> never accounted for a reservation. This will underflow resv_huge_pages
> and the subpool usage.
>
> Reproduced both cases with fault-injection, failing the file_region
> allocation in restore_reserve_on_error() when the first UFFDIO_COPY
> attempt into a MAP_SHARED|MAP_NORESERVE mapping fails:
>
> (1) memfd, after the retry:
>
> HugePages_Rsvd: 0 -> 18446744073709551615
>
> (2) hugetlbfs mount with min_size=, after unmounting:
>
> HugePages_Rsvd: 0 -> 18446744073709551615
>
> Let's fix it by setting or keeping HPG_restore_reserve for folios in
> shared mappings in this unlikely error case, such that free_huge_folio()
> will restore the reservation.
>
> Restructure the code to make it at least a little bit easier to
> understand.
>
> This has low urgency, as the file_region allocation is very unlikely to
> fail.
>
> Fixes: 846be08578ed ("mm/hugetlb: expand restore_reserve_on_error functionality")
> 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