Re: [PATCH 13/15] mm/hugetlb: fix reservation leak on min_size= mounts

From: David Hildenbrand (Arm)

Date: Mon Nov 16 2026 - 11:10:57 EST


On 11/14/26 18:59, Gregory Price (Meta) wrote:
> On 2026-10-28 21:13 +0100, David Hildenbrand (Arm) wrote:
>> From: Chris Mason <mason@xxxxxxxxxx>
>>
>> Failed allocations for CoW mappings can leak reservations on hugetlbfs
>> mounts with min_size= and no size=. The leaked reservations survive
>> unmount.
>>
>> HPG_restore_reserve makes free_huge_folio() restore the global
>> reservation without returning it to the subpool. This assumes the
>> reserve map holds the reservation. However, a CoW mapping may have no
>> reserve map, or the map may already account for another folio at the
>> same index.
>>
>> Commit 58db7c5fbe7d ("mm/hugetlb: fix avoid_reserve to allow taking
>> folio from subpool") allowed owner CoW allocations to consume subpool
>> reservations. Commit 51e1de00acdb ("mm/hugetlb: simplify
>> vma_has_reserves()") later extended this to CoW mappings without a
>> reserve map.
>>
>> For example, with a subpool minimum of one page and a
>> MAP_PRIVATE|MAP_NORESERVE mapping:
>>
>> subpool global
>> rsv_hpages resv_huge_pages
>> 1. Mount 1 1
>> 2. Allocate using subpool reserve 0 0
>> 3. Free on error with flag set 0 1
>> 4. Allocate without a reservation 0 1
>> 5. Free and return to subpool 1 2
>>
>> Call folio_clear_hugetlb_restore_reserve() for CoW mappings when
>> map_chg != MAP_CHG_REUSE, meaning no reserve-map reservation was consumed.
>> This lets free_huge_folio() return subpool reservations to the subpool
>> on error.
>>
>> Do not set the flag again if region-cache allocation fails during
>> rollback: that would undo the flag clearing and skip returning the
>> reservation to the subpool. This also fixes the older case where setting
>> the flag invents a reservation for an unreserved allocation for a CoW
>> mapping.
>>
>> The doc still mentions PagePrivate at various places: future work should
>> clean that up.
>>
>> Fix generated by an LLM and sent by Chris Mason in private.
>>
>> Fixes: 846be08578ed ("mm/hugetlb: expand restore_reserve_on_error functionality")
>> Fixes: 58db7c5fbe7d ("mm/hugetlb: fix avoid_reserve to allow taking folio from subpool")
>> Cc: stable@xxxxxxxxxxxxxxx
>> Signed-off-by: Chris Mason <mason@xxxxxxxxxx>
>> [ david: simplify comments and improve description; updated Fixes ]
>> Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
>
> Commit message is still hard to read :| (or maybe HugeTLB is just awful
> and no amount of massaging the message will ever help this).

It's just absolutely horrible stuff.

>
> Maybe note - this is an accounting error only, the pages do not leak.

Ack, I will add this.

>
> Was able to reproduce the issue with a UFFDIO_COPY the provides an invalid
> invalid source address (src=0).
>
> 1. Create the pool and mapping (hugetlbfs w/ pagesize=2M,min_size=2M)
> 2. invalid UFFDIO_COPY (kernel allocates, fails the, rolls back)
> 3. Close uffd, cause a normal fault
> 4. Unmap and truncate the file
> 5. Remove the file and unmount
>
> Base Patch
> step 2: total=2 free=2 rsvd=1 total=2 free=2 rsvd=1
> step 3: total=2 free=2 rsvd=2 total=2 free=2 rsvd=1*
> step 5: total=2 free=2 rsvd=1 total=2 free=2 rsvd=0*
>
> Result looks correct to me.
> The code hurts my brain but looks reasonable.
>
> Tested-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>
> Reviewed-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>

Thanks!

--
Cheers,

David