[PATCH v2 04/35] mm/hugetlb: return the reservation to the subpool on error
From: David Hildenbrand (Arm)
Date: Thu Dec 03 2026 - 06:38:58 EST
When we fault in a folio in a shared hugetlb mapping at an index that is
not reserved yet on a min_size= mount, alloc_hugetlb_folio() can take the
reservation from the subpool minimum, and we perform the 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() is supposed to undo the reservation in the
reserve map, and free_huge_folio() is supposed to return the reservation
to the subpool, like we do for CoW mappings already.
However, as the folio consumed a global reservation, it has
HPG_restore_reserve set. So restore_reserve_on_error() keeps the
reservation in the reserve map, and free_huge_folio() restores the global
reservation instead of returning it to the subpool.
Consequently, the reservation from the subpool minimum is stuck at that
index until truncation. As vma_commit_reservation() does not charge the
hugetlb cgroup reservation, a later fault at that index will consume it
without a cgroup charge.
Reproduced with a failing UFFDIO_COPY on a min_size= mount, followed by
a fault at the same index, inside a hugetlb cgroup:
hugetlb.2MB.rsvd.current: 0 instead of 2097152
Let's fix it by clearing HPG_restore_reserve in shared mappings as well.
On success, adding the folio to the page cache clears the flag.
Note that we have to clear the flag before vma_commit_reservation()
now, such that the race handling can set it again when we raced with
another reservation.
This has low urgency, as it only affects the subpool minimum and the
hugetlb cgroup reservation accounting on min_size= mounts.
Fixes: 1c5ecae3a93f ("hugetlbfs: add minimum size accounting to subpools")
Reported-by: Sashiko <sashiko-bot@xxxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
---
Documentation/mm/hugetlbfs_reserv.rst | 4 ++--
mm/hugetlb.c | 24 +++++++++++++++---------
2 files changed, 17 insertions(+), 11 deletions(-)
diff --git a/Documentation/mm/hugetlbfs_reserv.rst b/Documentation/mm/hugetlbfs_reserv.rst
index 4711029bd067d..7c13f79603eee 100644
--- a/Documentation/mm/hugetlbfs_reserv.rst
+++ b/Documentation/mm/hugetlbfs_reserv.rst
@@ -257,8 +257,8 @@ compensate. See the section
:ref:`Reservation Map Helper Routines <resv_map_helpers>` for more
information on these routines.
-For CoW mappings, alloc_hugetlb_folio() calls
-folio_clear_hugetlb_restore_reserve() if the reservation came from the subpool.
+alloc_hugetlb_folio() calls folio_clear_hugetlb_restore_reserve() if the
+reservation came from the subpool.
This makes free_huge_folio() return the reservation to the subpool on error.
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index fbe79ae4c0f3c..025aae6d0b72e 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -2722,7 +2722,9 @@ static long vma_del_reservation(struct hstate *h,
* In case 2, simply undo reserve map modifications done by alloc_hugetlb_folio.
*
* For CoW mappings, allocations using a subpool reservation leave the
- * flag clear and the reserve map unchanged. free_huge_folio returns the
+ * flag clear and the reserve map unchanged. The same applies to shared
+ * mappings, except that case 2 removes the reservation from the reserve map,
+ * unless updating the reserve map fails. free_huge_folio returns the
* reservation to the subpool.
*/
void restore_reserve_on_error(struct hstate *h, struct vm_area_struct *vma,
@@ -3200,6 +3202,18 @@ static struct folio *__alloc_hugetlb_folio(struct vm_area_struct *vma,
hugetlb_set_folio_subpool(folio, spool);
+ /*
+ * On error, let folio freeing code return the reservation to the
+ * subpool, unless we consumed a reservation from the reserve map.
+ *
+ * In private mappings, the reserve map might not exist or already
+ * account for another folio at this index. In shared mappings,
+ * restore_reserve_on_error() will remove the reservation we are about
+ * to add to the reserve map, unless updating the reserve map fails.
+ */
+ if (map_chg != MAP_CHG_REUSE)
+ folio_clear_hugetlb_restore_reserve(folio);
+
if (map_chg != MAP_CHG_ENFORCED) {
/* commit() is only needed if the map_chg is not enforced */
retval = vma_commit_reservation(h, vma, addr);
@@ -3230,14 +3244,6 @@ static struct folio *__alloc_hugetlb_folio(struct vm_area_struct *vma,
}
}
- /*
- * Return subpool reservations to the subpool on error. A CoW mapping
- * might not have a reserve map, or it might already account for
- * another folio at this index.
- */
- if (map_chg != MAP_CHG_REUSE && vma_is_cow_mapping(vma))
- folio_clear_hugetlb_restore_reserve(folio);
-
return folio;
out_subpool_put:
--
2.56.1