* [PATCH] mm: hugetlb: allocate larger early CMA pages first
@ 2026-11-20 7:47 Qi Zheng
2026-11-20 8:55 ` David Hildenbrand (Arm)
2026-11-20 9:04 ` Muchun Song
0 siblings, 2 replies; 4+ messages in thread
From: Qi Zheng @ 2026-11-20 7:47 UTC (permalink / raw)
To: muchun.song, osalvador, david, akpm, fvdl
Cc: linux-mm, linux-kernel, Qi Zheng, stable
From: Qi Zheng <zhengqi.arch@bytedance.com>
With hugetlb_cma_only=1, early HugeTLB CMA allocations follow hstate
registration order, which can depend on the order of hugepagesz= and
default_hugepagesz= parameters. cma_reserve_early() advances early_pfn by
the requested size without aligning the start of each allocation.
On arm64 with 4 KiB base pages, 32 MiB and 1 GiB pages share a CMA pool
aligned to 1 GiB. In a 2 GiB CMA range starting at 4 GiB, allocating a
32 MiB page followed by a 1 GiB page places the latter at 4 GiB + 32 MiB,
violating its required 1 GiB physical alignment.
Simply rounding early_pfn up before the 1 GiB allocation would leave a
992 MiB gap and advance the cursor to 6 GiB. cma_activate_area() marks the
entire prefix below early_pfn as allocated and only frees pageblocks after
it. The gap would remain reserved with its bitmap bits set, and releasing
the two HugeTLB folios would not reclaim it, permanently losing 992 MiB
of usable CMA capacity. An alignment-only fix would therefore also need
to track and reclaim such gaps.
The same alignment issue can affect non-LPAR PowerPC64 hash MMU systems
supporting 16 MiB and 16 GiB pages, with 64 KiB base pages and
CONFIG_ARCH_FORCE_MAX_ORDER=7. The 16 MiB size then has order 8 and is
also gigantic, sharing the 16 GiB-aligned CMA pool.
Allocate early CMA hstates in descending order. Each range starts aligned
to the largest supported size, so this preserves alignment for subsequent
smaller allocations without creating gaps.
Fixes: d2d786714080 ("mm/hugetlb: enable bootmem allocation from CMA areas")
Cc: stable@vger.kernel.org
Suggested-by: David Hildenbrand (Arm) <david@kernel.org>
Signed-off-by: Qi Zheng <zhengqi.arch@bytedance.com>
---
mm/hugetlb.c | 28 +++++++++++++++++++++++++++-
1 file changed, 27 insertions(+), 1 deletion(-)
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 4a448c5909d36..d69a9532ad5ef 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -4600,6 +4600,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;
@@ -4615,9 +4631,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(MAX_FOLIO_ORDER + 1);
+ while (h) {
+ hugetlb_hstate_alloc_pages(h);
+ h = hugetlb_next_early_cma_hstate(huge_page_order(h));
+ }
}
/*
--
2.54.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] mm: hugetlb: allocate larger early CMA pages first
2026-11-20 7:47 [PATCH] mm: hugetlb: allocate larger early CMA pages first Qi Zheng
@ 2026-11-20 8:55 ` David Hildenbrand (Arm)
2026-11-20 8:59 ` Qi Zheng
2026-11-20 9:04 ` Muchun Song
1 sibling, 1 reply; 4+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-20 8:55 UTC (permalink / raw)
To: Qi Zheng, muchun.song, osalvador, akpm, fvdl
Cc: linux-mm, linux-kernel, Qi Zheng, stable
On 11/20/26 08:47, Qi Zheng wrote:
> From: Qi Zheng <zhengqi.arch@bytedance.com>
>
> With hugetlb_cma_only=1, early HugeTLB CMA allocations follow hstate
> registration order, which can depend on the order of hugepagesz= and
> default_hugepagesz= parameters. cma_reserve_early() advances early_pfn by
> the requested size without aligning the start of each allocation.
>
> On arm64 with 4 KiB base pages, 32 MiB and 1 GiB pages share a CMA pool
> aligned to 1 GiB. In a 2 GiB CMA range starting at 4 GiB, allocating a
> 32 MiB page followed by a 1 GiB page places the latter at 4 GiB + 32 MiB,
> violating its required 1 GiB physical alignment.
>
> Simply rounding early_pfn up before the 1 GiB allocation would leave a
> 992 MiB gap and advance the cursor to 6 GiB. cma_activate_area() marks the
> entire prefix below early_pfn as allocated and only frees pageblocks after
> it. The gap would remain reserved with its bitmap bits set, and releasing
> the two HugeTLB folios would not reclaim it, permanently losing 992 MiB
> of usable CMA capacity. An alignment-only fix would therefore also need
> to track and reclaim such gaps.
>
> The same alignment issue can affect non-LPAR PowerPC64 hash MMU systems
> supporting 16 MiB and 16 GiB pages, with 64 KiB base pages and
> CONFIG_ARCH_FORCE_MAX_ORDER=7. The 16 MiB size then has order 8 and is
> also gigantic, sharing the 16 GiB-aligned CMA pool.
>
> Allocate early CMA hstates in descending order. Each range starts aligned
> to the largest supported size, so this preserves alignment for subsequent
> smaller allocations without creating gaps.
>
> Fixes: d2d786714080 ("mm/hugetlb: enable bootmem allocation from CMA areas")
> Cc: stable@vger.kernel.org
> Suggested-by: David Hildenbrand (Arm) <david@kernel.org>
> Signed-off-by: Qi Zheng <zhengqi.arch@bytedance.com>
> ---
> mm/hugetlb.c | 28 +++++++++++++++++++++++++++-
> 1 file changed, 27 insertions(+), 1 deletion(-)
>
> diff --git a/mm/hugetlb.c b/mm/hugetlb.c
> index 4a448c5909d36..d69a9532ad5ef 100644
> --- a/mm/hugetlb.c
> +++ b/mm/hugetlb.c
> @@ -4600,6 +4600,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;
> @@ -4615,9 +4631,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(MAX_FOLIO_ORDER + 1);
> + while (h) {
> + hugetlb_hstate_alloc_pages(h);
> + h = hugetlb_next_early_cma_hstate(huge_page_order(h));
Was thinking that this could be written in a neater way, but it really can't.
Best we could do is issue a single hugetlb_next_early_cma_hstate().
order = MAX_ORDER - 1;
while (h = hugetlb_next_early_cma_hstate(order)) {
hugetlb_hstate_alloc_pages(h);
order = huge_page_order(h);
}
Thanks!
Reviewed-by: David Hildenbrand (Arm) <david@kernel.org>
--
Cheers,
David
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] mm: hugetlb: allocate larger early CMA pages first
2026-11-20 8:55 ` David Hildenbrand (Arm)
@ 2026-11-20 8:59 ` Qi Zheng
0 siblings, 0 replies; 4+ messages in thread
From: Qi Zheng @ 2026-11-20 8:59 UTC (permalink / raw)
To: David Hildenbrand (Arm), muchun.song, osalvador, akpm, fvdl, Qi Zheng
Cc: linux-mm, linux-kernel, stable
On 11/20/26 4:55 PM, David Hildenbrand (Arm) wrote:
> On 11/20/26 08:47, Qi Zheng wrote:
>> From: Qi Zheng <zhengqi.arch@bytedance.com>
>>
>> With hugetlb_cma_only=1, early HugeTLB CMA allocations follow hstate
>> registration order, which can depend on the order of hugepagesz= and
>> default_hugepagesz= parameters. cma_reserve_early() advances early_pfn by
>> the requested size without aligning the start of each allocation.
>>
>> On arm64 with 4 KiB base pages, 32 MiB and 1 GiB pages share a CMA pool
>> aligned to 1 GiB. In a 2 GiB CMA range starting at 4 GiB, allocating a
>> 32 MiB page followed by a 1 GiB page places the latter at 4 GiB + 32 MiB,
>> violating its required 1 GiB physical alignment.
>>
>> Simply rounding early_pfn up before the 1 GiB allocation would leave a
>> 992 MiB gap and advance the cursor to 6 GiB. cma_activate_area() marks the
>> entire prefix below early_pfn as allocated and only frees pageblocks after
>> it. The gap would remain reserved with its bitmap bits set, and releasing
>> the two HugeTLB folios would not reclaim it, permanently losing 992 MiB
>> of usable CMA capacity. An alignment-only fix would therefore also need
>> to track and reclaim such gaps.
>>
>> The same alignment issue can affect non-LPAR PowerPC64 hash MMU systems
>> supporting 16 MiB and 16 GiB pages, with 64 KiB base pages and
>> CONFIG_ARCH_FORCE_MAX_ORDER=7. The 16 MiB size then has order 8 and is
>> also gigantic, sharing the 16 GiB-aligned CMA pool.
>>
>> Allocate early CMA hstates in descending order. Each range starts aligned
>> to the largest supported size, so this preserves alignment for subsequent
>> smaller allocations without creating gaps.
>>
>> Fixes: d2d786714080 ("mm/hugetlb: enable bootmem allocation from CMA areas")
>> Cc: stable@vger.kernel.org
>> Suggested-by: David Hildenbrand (Arm) <david@kernel.org>
>> Signed-off-by: Qi Zheng <zhengqi.arch@bytedance.com>
>> ---
>> mm/hugetlb.c | 28 +++++++++++++++++++++++++++-
>> 1 file changed, 27 insertions(+), 1 deletion(-)
>>
>> diff --git a/mm/hugetlb.c b/mm/hugetlb.c
>> index 4a448c5909d36..d69a9532ad5ef 100644
>> --- a/mm/hugetlb.c
>> +++ b/mm/hugetlb.c
>> @@ -4600,6 +4600,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;
>> @@ -4615,9 +4631,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(MAX_FOLIO_ORDER + 1);
>> + while (h) {
>> + hugetlb_hstate_alloc_pages(h);
>> + h = hugetlb_next_early_cma_hstate(huge_page_order(h));
>
> Was thinking that this could be written in a neater way, but it really can't.
> Best we could do is issue a single hugetlb_next_early_cma_hstate().
>
> order = MAX_ORDER - 1;
> while (h = hugetlb_next_early_cma_hstate(order)) {
> hugetlb_hstate_alloc_pages(h);
> order = huge_page_order(h);
> }
Yeah, that is indeed simpler — will rework it and send v2.
>
> Thanks!
>
> Reviewed-by: David Hildenbrand (Arm) <david@kernel.org>
Thanks!
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] mm: hugetlb: allocate larger early CMA pages first
2026-11-20 7:47 [PATCH] mm: hugetlb: allocate larger early CMA pages first Qi Zheng
2026-11-20 8:55 ` David Hildenbrand (Arm)
@ 2026-11-20 9:04 ` Muchun Song
1 sibling, 0 replies; 4+ messages in thread
From: Muchun Song @ 2026-11-20 9:04 UTC (permalink / raw)
To: Qi Zheng
Cc: osalvador, david, akpm, fvdl, linux-mm, linux-kernel, Qi Zheng, stable
> On Nov 20, 2026, at 15:47, Qi Zheng <qi.zheng@linux.dev> wrote:
>
> From: Qi Zheng <zhengqi.arch@bytedance.com>
>
> With hugetlb_cma_only=1, early HugeTLB CMA allocations follow hstate
> registration order, which can depend on the order of hugepagesz= and
> default_hugepagesz= parameters. cma_reserve_early() advances early_pfn by
> the requested size without aligning the start of each allocation.
>
> On arm64 with 4 KiB base pages, 32 MiB and 1 GiB pages share a CMA pool
> aligned to 1 GiB. In a 2 GiB CMA range starting at 4 GiB, allocating a
> 32 MiB page followed by a 1 GiB page places the latter at 4 GiB + 32 MiB,
> violating its required 1 GiB physical alignment.
>
> Simply rounding early_pfn up before the 1 GiB allocation would leave a
> 992 MiB gap and advance the cursor to 6 GiB. cma_activate_area() marks the
> entire prefix below early_pfn as allocated and only frees pageblocks after
> it. The gap would remain reserved with its bitmap bits set, and releasing
> the two HugeTLB folios would not reclaim it, permanently losing 992 MiB
> of usable CMA capacity. An alignment-only fix would therefore also need
> to track and reclaim such gaps.
>
> The same alignment issue can affect non-LPAR PowerPC64 hash MMU systems
> supporting 16 MiB and 16 GiB pages, with 64 KiB base pages and
> CONFIG_ARCH_FORCE_MAX_ORDER=7. The 16 MiB size then has order 8 and is
> also gigantic, sharing the 16 GiB-aligned CMA pool.
>
> Allocate early CMA hstates in descending order. Each range starts aligned
> to the largest supported size, so this preserves alignment for subsequent
> smaller allocations without creating gaps.
>
> Fixes: d2d786714080 ("mm/hugetlb: enable bootmem allocation from CMA areas")
> Cc: stable@vger.kernel.org
> Suggested-by: David Hildenbrand (Arm) <david@kernel.org>
> Signed-off-by: Qi Zheng <zhengqi.arch@bytedance.com>
Acked-by: Muchun Song (ByteDance) <muchun.song@linux.dev>
Thanks.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-20 9:04 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-20 7:47 [PATCH] mm: hugetlb: allocate larger early CMA pages first Qi Zheng
2026-11-20 8:55 ` David Hildenbrand (Arm)
2026-11-20 8:59 ` Qi Zheng
2026-11-20 9:04 ` Muchun Song
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®