Re: [PATCH v3 0/3] mm: hugetlb: fix CMA-related issues

From: David Hildenbrand (Arm)

Date: Wed Nov 18 2026 - 10:25:02 EST


On 11/17/26 07:55, Qi Zheng wrote:
> Hi David,
>
> On 11/16/26 8:07 PM, David Hildenbrand (Arm) wrote:
>>
>> Hi,
>>
>> sorry somehow I missed your mail.
>>
>>
>>
>> Ugh, what a hacky mess people created.
>>
>>
>> That would be cleanest. Would it break existing setups?
>
> Yes, this requires an existing user to explicitly configure a 32M memory
> pool.
>
> But in practice there shouldn't be such a user today, otherwise the
> misalignment issue would have surfaced long ago, wouldn't it?
>
>>
>>
>> Another quick-fix idea could be to always allocate larger folios first.
>>
>> That way we should avoid any holes, right>
>
> Ah, this looks feasible. How about the following?
>
> (If that looks good, this could go as a stable-first fix.)
>
> diff --git a/mm/hugetlb.c b/mm/hugetlb.c
> index 6fba92b0f6749..32e65ce347c90 100644
> --- a/mm/hugetlb.c
> +++ b/mm/hugetlb.c
> @@ -4556,6 +4556,22 @@ void __init hugetlb_bootmem_set_nodes(void)
> node_set(nid, hugetlb_bootmem_nodes);
> }
>
> +/* Return the largest early-CMA hstate below the given order. */
> +static struct hstate * __init
> +hugetlb_next_early_cma_hstate(unsigned int order)
> +{
> + struct hstate *h, *next = NULL;
> +
> + for_each_hstate(h) {
> + if (!hugetlb_early_cma(h) || huge_page_order(h) >= order)
> + continue;
> + if (!next || huge_page_order(h) > huge_page_order(next))
> + next = h;
> + }
> +
> + return next;
> +}
> +
> void __init hugetlb_bootmem_alloc(void)
> {
> struct hstate *h;
> @@ -4571,9 +4587,19 @@ void __init hugetlb_bootmem_alloc(void)
> for_each_hstate(h) {
> h->next_nid_to_alloc = first_online_node;
>
> - if (hstate_is_gigantic(h))
> + if (hstate_is_gigantic(h) && !hugetlb_early_cma(h))
> hugetlb_hstate_alloc_pages(h);
> }
> +
> + /*
> + * CMA ranges are aligned to the largest supported size. Allocate larger
> + * sizes first to keep the early reservation cursor aligned without gaps.
> + */
> + h = hugetlb_next_early_cma_hstate(UINT_MAX);
> + while (h) {
> + hugetlb_hstate_alloc_pages(h);
> + h = hugetlb_next_early_cma_hstate(huge_page_order(h));
> + }
> }
>
> /*

I think this should do. Best to send out a proper patch.

--
Cheers,

David