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 Cc: stable@vger.kernel.org Signed-off-by: David Hildenbrand (Arm) --- mm/hugetlb.c | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index f46c85fc0cd0b..bf2265bc6eb71 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -2730,21 +2730,27 @@ void restore_reserve_on_error(struct hstate *h, struct vm_area_struct *vma, { long rc = vma_needs_reservation(h, vma, address); - if (folio_test_hugetlb_restore_reserve(folio)) { - if (unlikely(rc < 0)) - /* - * Rare out of memory condition in reserve map - * manipulation. Clear hugetlb_restore_reserve so - * that global reserve count will not be incremented - * by free_huge_folio. This will make it appear - * as though the reservation for this folio was - * consumed. This may prevent the task from - * faulting in the folio at a later time. This - * is better than inconsistent global huge page - * accounting of reserve counts. - */ + if (unlikely(rc < 0)) { + /* + * Rare out of memory condition in reserve map manipulation. + * + * In shared mappings, the reserve map has the reservation for + * this index, so let folio freeing code restore it. + * + * Otherwise, don't restore the reservation, making it appear + * as though it was consumed. This may prevent the task from + * faulting in the folio at a later time, but is better than + * inconsistent global huge page accounting of reserve counts. + */ + if (vma->vm_flags & VM_MAYSHARE) + folio_set_hugetlb_restore_reserve(folio); + else folio_clear_hugetlb_restore_reserve(folio); - else if (rc) + return; + } + + if (folio_test_hugetlb_restore_reserve(folio)) { + if (rc) (void)vma_add_reservation(h, vma, address); else vma_end_reservation(h, vma, address); @@ -2769,11 +2775,6 @@ void restore_reserve_on_error(struct hstate *h, struct vm_area_struct *vma, * on a subsequent allocation. */ folio_set_hugetlb_restore_reserve(folio); - } else if (rc < 0) { - /* - * Region cache allocation can fail even if an entry exists. - * Leave the flag and reserve map unchanged. - */ } else { /* * No reservation present, do nothing -- 2.56.1