Re: [PATCH v2 03/35] mm/hugetlb: restore the reservation when racing with another reservation
From: Jose A. Perez de Azpillaga
Date: Fri Dec 04 2026 - 05:21:17 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, alloc_hugetlb_folio() can race with
> hugetlb_reserve_pages() reserving that index, for example, because
> another process mmap()s the same file without MAP_NORESERVE.
>
> vma_commit_reservation() will then find the racing reservation in the
> reserve map after allocating the folio, and we consume that reservation
> instead: we return our subpool charge, drop a global reservation and
> uncharge the cgroup reservation of the folio. However, we don't set
> HPG_restore_reserve on the folio.
>
> If we then hit an error before the folio gets added to the page cache,
> restore_reserve_on_error() assumes that alloc_hugetlb_folio() performed
> the reservation in the reserve map and removes it, and free_huge_folio()
> doesn't restore the global reservation. The reservation of the racing
> mmap() is then gone, and a later fault at that index can fail with
> SIGBUS.
>
> Reproduced by artificially delaying alloc_hugetlb_folio() right before
> vma_commit_reservation() during a failing UFFDIO_COPY, while another
> process mmap()s the same file: once all unreserved huge pages are in
> use, that process gets SIGBUS when touching its page.
>
> Let's fix it by setting HPG_restore_reserve, so the error path keeps the
> reservation in the reserve map and free_huge_folio() restores the global
> reservation. On success, adding the folio to the page cache clears the
> flag.
>
> This has low urgency, as it requires an error after racing with another
> mapping reserving the same index.
>
> Fixes: 33039678c8da ("mm/hugetlb: handle races in alloc_huge_page and hugetlb_reserve_pages")
> 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