Skip to contentSkip to conversation

mm/hugetlb_cma: support percentage-based hugetlb_cma reservation

PATCH Superseded Sourav Panda 4 participants 7 messages Jun 28 – Jul 29

Subsystems (3)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 mm/hugetlb_cma: support percentage-based hugetlb_cma reservation

Currently, hugetlb_cma reservation only supports absolute sizes (e.g., hugetlb_cma=2G or hugetlb_cma=0:1G,1:1G). This can be restrictive in heterogeneous environments or when deploying common kernel command lines across machines with different memory capacities.

Add support for percentage-based hugetlb_cma reservation (e.g., hugetlb_cma=20% or hugetlb_cma=0:20%,1:10%).

The percentage is calculated against the total memory (for global settings) or against the node-specific memory (for node-specific settings) using memblock APIs during early boot.

---

v2:
- Use mul_u64_u32_div() instead of mult_frac() to avoid 64-bit division on 32-bit architectures (Reported by kernel test robot).
- Fix physical memory size truncation on 32-bit PAE by using phys_addr_t in memblock_node_memory_size() (Sashiko).
- Fix boot parameter override logic to respect standard last-one-wins precedence by clearing opposite values during parsing and clearing node-specific values when global values are parsed.

 .../admin-guide/kernel-parameters.txt         |  7 +-
 mm/hugetlb_cma.c                              | 96 ++++++++++++++++++-
 2 files changed, 97 insertions(+), 6 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +5 -2
index 5a05b48d1684..846940ce2858 100644
@@ -2064,8 +2064,11 @@ Kernel parameters
 	hugetlb_cma=	[HW,CMA,EARLY] The size of a CMA area used for allocation
 			of gigantic hugepages. Or using node format, the size
 			of a CMA area per node can be specified.
-			Format: nn[KMGTPE] or (node format)
-				<node>:nn[KMGTPE][,<node>:nn[KMGTPE]]
+			The size can be an absolute value (e.g., 2G) or a
+			percentage of the total memory or node memory (e.g., 20%).
+			Format: nn[KMGTPE] or nn% or (node format)
+				<node>:nn[KMGTPE][,<node>:nn[KMGTPE]] or
+				<node>:nn%[,<node>:nn%]
 
 			Reserve a CMA area of given size and allocate gigantic
 			hugepages using the CMA allocator. If enabled, the
mm/hugetlb_cma.c +92 -4

133 lines not on this page · raw message

Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
maintainer
#1 lore raw
Quoted 11 lines · On Sun, 28 Jun 2026 19:01:55 +0000 Sourav Panda <[email protected]> wrote:

Currently, hugetlb_cma reservation only supports absolute sizes (e.g., hugetlb_cma=2G or hugetlb_cma=0:1G,1:1G). This can be restrictive in heterogeneous environments or when deploying common kernel command lines across machines with different memory capacities.

Add support for percentage-based hugetlb_cma reservation (e.g., hugetlb_cma=20% or hugetlb_cma=0:20%,1:10%).

The percentage is calculated against the total memory (for global settings) or against the node-specific memory (for node-specific settings) using memblock APIs during early boot.

Thanks, I'll queue this for testing and additional review.

There are a couple of pr_infos in this code, to give people an understanding of how the kernel has sized these things. Would it make sense to enhance those, to help our operator to understand/confirm what effect the chosen percentage configuration had?

Quoted 3 lines

Robot found issues in the v1 patch. This development-time info isn't something which we (at least, I) normally include in the permanent kernel record.

#2 lore raw
Quoted 8 lines · Andrew Morton
Quoted 5 lines · On Sun, 28 Jun 2026 19:01:55 +0000 Sourav Panda <[email protected]> wrote:

Currently, hugetlb_cma reservation only supports absolute sizes (e.g., hugetlb_cma=2G or hugetlb_cma=0:1G,1:1G). This can be restrictive in heterogeneous environments or when deploying common kernel command lines across machines with different memory capacities.

Quoted 9 lines
Quoted 6 lines

Add support for percentage-based hugetlb_cma reservation (e.g., hugetlb_cma=20% or hugetlb_cma=0:20%,1:10%).

The percentage is calculated against the total memory (for global settings) or against the node-specific memory (for node-specific settings) using memblock APIs during early boot.

Thanks, I'll queue this for testing and additional review.

Thank you very much!

Quoted 6 lines

There are a couple of pr_infos in this code, to give people an understanding of how the kernel has sized these things. Would it make sense to enhance those, to help our operator to understand/confirm what effect the chosen percentage configuration had?

Absolutely, will add them!

Quoted 10 lines
Quoted 4 lines

Closes:

https://lore.kernel.org/oe-kbuild-all/[email protected]/

Robot found issues in the v1 patch. This development-time info isn't something which we (at least, I) normally include in the permanent kernel record.

Gotcha! Thanks for the feedback :)

#3 lore raw
In reply to Sourav Panda
Quoted 175 lines · On Sun, 28 Jun 2026 19:01:55 +0000 Sourav Panda <[email protected]> wrote:

Currently, hugetlb_cma reservation only supports absolute sizes (e.g., hugetlb_cma=2G or hugetlb_cma=0:1G,1:1G). This can be restrictive in heterogeneous environments or when deploying common kernel command lines across machines with different memory capacities.

Add support for percentage-based hugetlb_cma reservation (e.g., hugetlb_cma=20% or hugetlb_cma=0:20%,1:10%).

The percentage is calculated against the total memory (for global settings) or against the node-specific memory (for node-specific settings) using memblock APIs during early boot.

---

v2:
- Use mul_u64_u32_div() instead of mult_frac() to avoid 64-bit division on 32-bit architectures (Reported by kernel test robot).
- Fix physical memory size truncation on 32-bit PAE by using phys_addr_t in memblock_node_memory_size() (Sashiko).
- Fix boot parameter override logic to respect standard last-one-wins precedence by clearing opposite values during parsing and clearing node-specific values when global values are parsed.

 .../admin-guide/kernel-parameters.txt         |  7 +-
 mm/hugetlb_cma.c                              | 96 ++++++++++++++++++-
 2 files changed, 97 insertions(+), 6 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +5 -2
index 5a05b48d1684..846940ce2858 100644
@@ -2064,8 +2064,11 @@ Kernel parameters
 	hugetlb_cma=	[HW,CMA,EARLY] The size of a CMA area used for allocation
 			of gigantic hugepages. Or using node format, the size
 			of a CMA area per node can be specified.
-			Format: nn[KMGTPE] or (node format)
-				<node>:nn[KMGTPE][,<node>:nn[KMGTPE]]
+			The size can be an absolute value (e.g., 2G) or a
+			percentage of the total memory or node memory (e.g., 20%).
+			Format: nn[KMGTPE] or nn% or (node format)
+				<node>:nn[KMGTPE][,<node>:nn[KMGTPE]] or
+				<node>:nn%[,<node>:nn%]
 
 			Reserve a CMA area of given size and allocate gigantic
 			hugepages using the CMA allocator. If enabled, the
mm/hugetlb_cma.c +84 -4
index 7693ccefd0c6..684ffa3537d2 100644
@@ -9,6 +9,9 @@
 #include <asm/setup.h>
 
 #include <linux/hugetlb.h>
+#include <linux/memblock.h>
+#include <linux/math.h>
+#include <linux/math64.h>
 #include "internal.h"
 #include "hugetlb_cma.h"
 
@@ -18,6 +21,28 @@ static unsigned long hugetlb_cma_size_in_node[MAX_NUMNODES] __initdata;
 static bool hugetlb_cma_only __ro_after_init;
 static unsigned long hugetlb_cma_size __ro_after_init;
 
+static unsigned int hugetlb_cma_percent __initdata;
+static unsigned int hugetlb_cma_percent_in_node[MAX_NUMNODES] __initdata;
+
+#ifdef CONFIG_NUMA
+static phys_addr_t __init memblock_node_memory_size(int nid)
+{
+	struct memblock_region *reg;
+	phys_addr_t size = 0;
+
+	for_each_mem_region(reg) {
+		if (reg->nid == nid)
+			size += reg->size;
+	}
+	return size;
+}
+#else
+static phys_addr_t __init memblock_node_memory_size(int nid)
+{
+	return memblock_phys_mem_size();
+}
+#endif
+
 void hugetlb_cma_free_frozen_folio(struct folio *folio)
 {
 	WARN_ON_ONCE(!cma_release_frozen(hugetlb_cma[folio_nid(folio)],
@@ -100,14 +125,28 @@ static int __init cmdline_parse_hugetlb_cma(char *p)
 			break;
 
 		if (s[count] == ':') {
+			char *next;
+
 			if (tmp >= MAX_NUMNODES)
 				break;
 			nid = array_index_nospec(tmp, MAX_NUMNODES);
 
 			s += count + 1;
-			tmp = memparse(s, &s);
-			hugetlb_cma_size_in_node[nid] = tmp;
-			hugetlb_cma_size += tmp;
+			tmp = memparse(s, &next);
+			if (*next == '%') {
+				if (tmp > 100) {
+					pr_warn("hugetlb_cma: invalid percentage %lu for node %d\n",
+						tmp, nid);
+					break;
+				}
+				hugetlb_cma_percent_in_node[nid] = tmp;
+				hugetlb_cma_size_in_node[nid] = 0;
+				s = next + 1;
+			} else {
+				hugetlb_cma_size_in_node[nid] = tmp;
+				hugetlb_cma_percent_in_node[nid] = 0;
+				s = next;
+			}
 
 			/*
 			 * Skip the separator if have one, otherwise
@@ -118,7 +157,28 @@ static int __init cmdline_parse_hugetlb_cma(char *p)
 			else
 				break;
 		} else {
-			hugetlb_cma_size = memparse(p, &p);
+			char *next;
+
+			tmp = memparse(p, &next);
+			if (*next == '%') {
+				if (tmp > 100) {
+					pr_warn("hugetlb_cma: invalid percentage %lu\n", tmp);
+				} else {
+					hugetlb_cma_percent = tmp;
+					hugetlb_cma_size = 0;
+					for (nid = 0; nid < MAX_NUMNODES; nid++) {
+						hugetlb_cma_size_in_node[nid] = 0;
+						hugetlb_cma_percent_in_node[nid] = 0;
+					}
+				}
+			} else {
+				hugetlb_cma_size = tmp;
+				hugetlb_cma_percent = 0;
+				for (nid = 0; nid < MAX_NUMNODES; nid++) {
+					hugetlb_cma_size_in_node[nid] = 0;
+					hugetlb_cma_percent_in_node[nid] = 0;
+				}
+			}
 			break;
 		}
 	}
@@ -144,8 +204,36 @@ void __init hugetlb_cma_reserve(void)
 {
 	unsigned long size, reserved, per_node, order;
 	bool node_specific_cma_alloc = false;
+	bool has_node_specific_param = false;
 	int nid;
 
+	for (nid = 0; nid < MAX_NUMNODES; nid++) {
+		if (hugetlb_cma_size_in_node[nid] || hugetlb_cma_percent_in_node[nid]) {
+			has_node_specific_param = true;
+			break;
+		}
+	}
+
+	if (has_node_specific_param) {
+		hugetlb_cma_size = 0;
+		for (nid = 0; nid < MAX_NUMNODES; nid++) {
+			if (hugetlb_cma_percent_in_node[nid]) {
+				phys_addr_t node_gfp_mem = memblock_node_memory_size(nid);
+				u64 s;
+
+				s = mul_u64_u32_div((u64)node_gfp_mem,
+						    hugetlb_cma_percent_in_node[nid],
+						    100);
+
+				hugetlb_cma_size_in_node[nid] = s;

mm/hugetlb_cma.c void __init hugetlb_cma_reserve(void) · line 238

Quoted 10 lines
mm/hugetlb_cma.c +8 -0
+			}
+			hugetlb_cma_size += hugetlb_cma_size_in_node[nid];
+		}
+	} else if (hugetlb_cma_percent) {
+		hugetlb_cma_size = mul_u64_u32_div((u64)memblock_phys_mem_size(),
+						   hugetlb_cma_percent, 100);
+	}
+
 	if (!hugetlb_cma_size)
 		return;

Can this round percentage-derived sizes to the gigantic page size before the existing alignment checks run?

For example, on an x86_64 machine with 64 GiB of memory, hugetlb_cma=20% computes 12.8 GiB. The later hugetlb_cma_reserve() alignment check requires the total to be a multiple of the gigantic_page_size, so the documented 20% example is rejected and the reservation is disabled?

Quoted 5 lines
Signature · 2 lines
-- 
2.55.0.rc0.799.gd6f94ed593-goog
#4 lore raw
Quoted 192 lines · Usama Arif
Quoted 175 lines · On Sun, 28 Jun 2026 19:01:55 +0000 Sourav Panda <[email protected]> wrote:

Currently, hugetlb_cma reservation only supports absolute sizes (e.g., hugetlb_cma=2G or hugetlb_cma=0:1G,1:1G). This can be restrictive in heterogeneous environments or when deploying common kernel command lines across machines with different memory capacities.

Add support for percentage-based hugetlb_cma reservation (e.g., hugetlb_cma=20% or hugetlb_cma=0:20%,1:10%).

The percentage is calculated against the total memory (for global settings) or against the node-specific memory (for node-specific settings) using memblock APIs during early boot.

---

v2:
- Use mul_u64_u32_div() instead of mult_frac() to avoid 64-bit division on 32-bit architectures (Reported by kernel test robot).
- Fix physical memory size truncation on 32-bit PAE by using phys_addr_t in memblock_node_memory_size() (Sashiko).
- Fix boot parameter override logic to respect standard last-one-wins precedence by clearing opposite values during parsing and clearing node-specific values when global values are parsed.

 .../admin-guide/kernel-parameters.txt         |  7 +-
 mm/hugetlb_cma.c                              | 96 ++++++++++++++++++-
 2 files changed, 97 insertions(+), 6 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +5 -2
index 5a05b48d1684..846940ce2858 100644
@@ -2064,8 +2064,11 @@ Kernel parameters
      hugetlb_cma=    [HW,CMA,EARLY] The size of a CMA area used for allocation
                      of gigantic hugepages. Or using node format, the size
                      of a CMA area per node can be specified.
-                     Format: nn[KMGTPE] or (node format)
-                             <node>:nn[KMGTPE][,<node>:nn[KMGTPE]]
+                     The size can be an absolute value (e.g., 2G) or a
+                     percentage of the total memory or node memory (e.g., 20%).
+                     Format: nn[KMGTPE] or nn% or (node format)
+                             <node>:nn[KMGTPE][,<node>:nn[KMGTPE]] or
+                             <node>:nn%[,<node>:nn%]
 
                      Reserve a CMA area of given size and allocate gigantic
                      hugepages using the CMA allocator. If enabled, the
mm/hugetlb_cma.c +84 -4
index 7693ccefd0c6..684ffa3537d2 100644
@@ -9,6 +9,9 @@
 #include <asm/setup.h>
 
 #include <linux/hugetlb.h>
+#include <linux/memblock.h>
+#include <linux/math.h>
+#include <linux/math64.h>
 #include "internal.h"
 #include "hugetlb_cma.h"
 
@@ -18,6 +21,28 @@ static unsigned long hugetlb_cma_size_in_node[MAX_NUMNODES] __initdata;
 static bool hugetlb_cma_only __ro_after_init;
 static unsigned long hugetlb_cma_size __ro_after_init;
 
+static unsigned int hugetlb_cma_percent __initdata;
+static unsigned int hugetlb_cma_percent_in_node[MAX_NUMNODES] __initdata;
+
+#ifdef CONFIG_NUMA
+static phys_addr_t __init memblock_node_memory_size(int nid)
+{
+     struct memblock_region *reg;
+     phys_addr_t size = 0;
+
+     for_each_mem_region(reg) {
+             if (reg->nid == nid)
+                     size += reg->size;
+     }
+     return size;
+}
+#else
+static phys_addr_t __init memblock_node_memory_size(int nid)
+{
+     return memblock_phys_mem_size();
+}
+#endif
+
 void hugetlb_cma_free_frozen_folio(struct folio *folio)
 {
      WARN_ON_ONCE(!cma_release_frozen(hugetlb_cma[folio_nid(folio)],
@@ -100,14 +125,28 @@ static int __init cmdline_parse_hugetlb_cma(char *p)
                      break;
 
              if (s[count] == ':') {
+                     char *next;
+
                      if (tmp >= MAX_NUMNODES)
                              break;
                      nid = array_index_nospec(tmp, MAX_NUMNODES);
 
                      s += count + 1;
-                     tmp = memparse(s, &s);
-                     hugetlb_cma_size_in_node[nid] = tmp;
-                     hugetlb_cma_size += tmp;
+                     tmp = memparse(s, &next);
+                     if (*next == '%') {
+                             if (tmp > 100) {
+                                     pr_warn("hugetlb_cma: invalid percentage %lu for node %d\n",
+                                             tmp, nid);
+                                     break;
+                             }
+                             hugetlb_cma_percent_in_node[nid] = tmp;
+                             hugetlb_cma_size_in_node[nid] = 0;
+                             s = next + 1;
+                     } else {
+                             hugetlb_cma_size_in_node[nid] = tmp;
+                             hugetlb_cma_percent_in_node[nid] = 0;
+                             s = next;
+                     }
 
                      /*
                       * Skip the separator if have one, otherwise
@@ -118,7 +157,28 @@ static int __init cmdline_parse_hugetlb_cma(char *p)
                      else
                              break;
              } else {
-                     hugetlb_cma_size = memparse(p, &p);
+                     char *next;
+
+                     tmp = memparse(p, &next);
+                     if (*next == '%') {
+                             if (tmp > 100) {
+                                     pr_warn("hugetlb_cma: invalid percentage %lu\n", tmp);
+                             } else {
+                                     hugetlb_cma_percent = tmp;
+                                     hugetlb_cma_size = 0;
+                                     for (nid = 0; nid < MAX_NUMNODES; nid++) {
+                                             hugetlb_cma_size_in_node[nid] = 0;
+                                             hugetlb_cma_percent_in_node[nid] = 0;
+                                     }
+                             }
+                     } else {
+                             hugetlb_cma_size = tmp;
+                             hugetlb_cma_percent = 0;
+                             for (nid = 0; nid < MAX_NUMNODES; nid++) {
+                                     hugetlb_cma_size_in_node[nid] = 0;
+                                     hugetlb_cma_percent_in_node[nid] = 0;
+                             }
+                     }
                      break;
              }
      }
@@ -144,8 +204,36 @@ void __init hugetlb_cma_reserve(void)
 {
      unsigned long size, reserved, per_node, order;
      bool node_specific_cma_alloc = false;
+     bool has_node_specific_param = false;
      int nid;
 
+     for (nid = 0; nid < MAX_NUMNODES; nid++) {
+             if (hugetlb_cma_size_in_node[nid] || hugetlb_cma_percent_in_node[nid]) {
+                     has_node_specific_param = true;
+                     break;
+             }
+     }
+
+     if (has_node_specific_param) {
+             hugetlb_cma_size = 0;
+             for (nid = 0; nid < MAX_NUMNODES; nid++) {
+                     if (hugetlb_cma_percent_in_node[nid]) {
+                             phys_addr_t node_gfp_mem = memblock_node_memory_size(nid);
+                             u64 s;
+
+                             s = mul_u64_u32_div((u64)node_gfp_mem,
+                                                 hugetlb_cma_percent_in_node[nid],
+                                                 100);
+
+                             hugetlb_cma_size_in_node[nid] = s;

mm/hugetlb_cma.c void __init hugetlb_cma_reserve(void) · line 238

Quoted 10 lines
mm/hugetlb_cma.c +8 -0
+                     }
+                     hugetlb_cma_size += hugetlb_cma_size_in_node[nid];
+             }
+     } else if (hugetlb_cma_percent) {
+             hugetlb_cma_size = mul_u64_u32_div((u64)memblock_phys_mem_size(),
+                                                hugetlb_cma_percent, 100);
+     }
+
      if (!hugetlb_cma_size)
              return;

Can this round percentage-derived sizes to the gigantic page size before the existing alignment checks run?

Quoted 5 lines

For example, on an x86_64 machine with 64 GiB of memory, hugetlb_cma=20% computes 12.8 GiB. The later hugetlb_cma_reserve() alignment check requires the total to be a multiple of the gigantic_page_size, so the documented 20% example is rejected and the reservation is disabled?

Hi Usama,

Good catch :) and thanks for reviewing!

So right after this block, we get the order =
arch_hugetlb_cma_order(); and carry out a few checks.

I am thinking we can apply ALIGN_DOWN(hugetlb_cma_size, PAGE_SIZE
<< order) right after the VM_WARN_ON(order <= MAX_PAGE_ORDER);

Something like this:

        VM_WARN_ON(order <= MAX_PAGE_ORDER);

        if (hugetlb_cma_percent) {
            hugetlb_cma_size = ALIGN_DOWN(hugetlb_cma_size, PAGE_SIZE << order);
        } else if (has_node_specific_param) {
            // (Same logic applied in the per-node loop)

Thanks again!

Quoted 5 lines
Quoted 5 lines
mm/hugetlb_cma.c +0 -1
--

2.55.0.rc0.799.gd6f94ed593-goog

#5 lore raw
In reply to Sourav Panda
Quoted 18 lines · Sourav Panda

Currently, hugetlb_cma reservation only supports absolute sizes (e.g., hugetlb_cma=2G or hugetlb_cma=0:1G,1:1G). This can be restrictive in heterogeneous environments or when deploying common kernel command lines across machines with different memory capacities.

Add support for percentage-based hugetlb_cma reservation (e.g., hugetlb_cma=20% or hugetlb_cma=0:20%,1:10%).

The percentage is calculated against the total memory (for global settings) or against the node-specific memory (for node-specific settings) using memblock APIs during early boot.

---

Quoted 7 lines

v2:
- Use mul_u64_u32_div() instead of mult_frac() to avoid 64-bit division on 32-bit architectures (Reported by kernel test robot).
- Fix physical memory size truncation on 32-bit PAE by using phys_addr_t in memblock_node_memory_size() (Sashiko).
- Fix boot parameter override logic to respect standard last-one-wins precedence by clearing opposite values during parsing and clearing node-specific values when global values are parsed.

 .../admin-guide/kernel-parameters.txt         |  7 +-
 mm/hugetlb_cma.c                              | 96 ++++++++++++++++++-
 2 files changed, 97 insertions(+), 6 deletions(-)

What's the status of this? Reading the comments I assume a v3?

Signature · 3 lines
Cheers,

David
#6 lore raw
Quoted 28 lines · David Hildenbrand (Arm)
Quoted 18 lines · Sourav Panda

Currently, hugetlb_cma reservation only supports absolute sizes (e.g., hugetlb_cma=2G or hugetlb_cma=0:1G,1:1G). This can be restrictive in heterogeneous environments or when deploying common kernel command lines across machines with different memory capacities.

Add support for percentage-based hugetlb_cma reservation (e.g., hugetlb_cma=20% or hugetlb_cma=0:20%,1:10%).

The percentage is calculated against the total memory (for global settings) or against the node-specific memory (for node-specific settings) using memblock APIs during early boot.

---

Quoted 7 lines

v2:
- Use mul_u64_u32_div() instead of mult_frac() to avoid 64-bit division on 32-bit architectures (Reported by kernel test robot).
- Fix physical memory size truncation on 32-bit PAE by using phys_addr_t in memblock_node_memory_size() (Sashiko).
- Fix boot parameter override logic to respect standard last-one-wins precedence by clearing opposite values during parsing and clearing node-specific values when global values are parsed.

 .../admin-guide/kernel-parameters.txt         |  7 +-
 mm/hugetlb_cma.c                              | 96 ++++++++++++++++++-
 2 files changed, 97 insertions(+), 6 deletions(-)
Quoted 1 line

What's the status of this? Reading the comments I assume a v3?

Thanks David for checking.

Just sent v3: https://lore.kernel.org/linux-mm/[email protected]/

Quoted 5 lines

--
Cheers,

David