[PATCH v2 03/35] mm/hugetlb: restore the reservation when racing with another reservation

From: David Hildenbrand (Arm)

Date: Thu Dec 03 2026 - 06:38:40 EST


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>
---
mm/hugetlb.c | 5 +++++
1 file changed, 5 insertions(+)

diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index bf2265bc6eb71..fbe79ae4c0f3c 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -3222,6 +3222,11 @@ static struct folio *__alloc_hugetlb_folio(struct vm_area_struct *vma,
hugetlb_cgroup_uncharge_folio_rsvd(
hstate_index(h), pages_per_huge_page(h), folio);
spin_unlock_irq(&hugetlb_lock);
+ /*
+ * We consumed that existing reservation and must
+ * restore it when freeing the folio due to errors.
+ */
+ folio_set_hugetlb_restore_reserve(folio);
}
}


--
2.56.1