Skip to contentSkip to conversation

mm: hugetlb: add CMA support for PMD-sized page

PATCH Superseded Qi Zheng 6 participants 24 messages Oct 16 – Oct 20

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Qi Zheng

author
#0 lore raw

cover mm: hugetlb: add CMA support for PMD-sized page

From: Qi Zheng <[email protected]>

Hi all,

Allocating PMD-sized HugeTLB pages at runtime can become slow or fail as physical memory becomes fragmented. Reserving HugeTLB pages at boot avoids this problem, but keeps unused pages unavailable to ordinary allocations.

Live upgrades are one example: they need additional HugeTLB pages only during the upgrade. HugeTLB already supports CMA for gigantic pages, but reserving a CMA pool for 1 GiB pages can fail when physical KASLR and other early reservations leave too few suitable 1 GiB-aligned ranges. At runtime, 1 GiB allocations can also fail when pages cannot be migrated out of a suitable contiguous range.

Extend CMA support to PMD-sized pages so their backing memory can be reserved at boot while remaining usable for movable allocations until needed. This relaxes the reservation alignment requirement and reduces the contiguous range required per allocation.

The series adds an optional @<page-size> suffix to hugetlb_cma and a separate PMD-sized pool. For example, on x86-64:

hugetlb_cma=4G hugetlb_cma=1G@2M

This reserves 4G for the existing gigantic-page pool and 1G for the PMD-sized pool. Existing unsuffixed parameters and gigantic-page pool sharing are preserved. The new size follows the architecture's PMD size rather than being fixed at 2M.

The preparatory patches introduce per-pool state and prevent recursive CMA allocation during migration before enabling PMD-sized allocation.

Thanks,
Qi

Qi Zheng (5):
mm: hugetlb: add explicit page size to hugetlb_cma
mm: hugetlb: group CMA pool and reservation state
mm: hugetlb: track CMA reservations by page order
mm: hugetlb: avoid recursive CMA allocation during migration
mm: hugetlb: enable CMA allocation for PMD-sized pages

 .../admin-guide/kernel-parameters.txt         |  39 ++-
 include/linux/hugetlb.h                       |   4 +-
 mm/hugetlb.c                                  |  80 +++--
 mm/hugetlb_cma.c                              | 328 ++++++++++++------
 mm/hugetlb_cma.h                              |  11 +-
 mm/mempolicy.c                                |   2 +-
 mm/migrate.c                                  |   3 +-
 7 files changed, 306 insertions(+), 161 deletions(-)

base-commit: b810335a0699d1810683796b0e3ffe8827ddfe6c

Signature · 1 line
2.54.0
author
#1 lore raw

PATCH 1/5 mm: hugetlb: add explicit page size to hugetlb_cma

From: Qi Zheng <[email protected]>

Accept an optional @<page-size> suffix for hugetlb_cma. Initially accept only the architecture's existing CMA page size. Preserve the behavior of unsuffixed arguments and the existing hugepagesz/hugepages options.

Validate the suffix before applying the existing capacity, percentage and node-list parser.

Save the arguments during early parsing and process them at reservation time, after architecture-specific page-size information has been initialized.

Assisted-by LLM
---
 mm/hugetlb_cma.c | 43 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 42 insertions(+), 1 deletion(-)
mm/hugetlb_cma.c +42 -1
index 95fd2d190f0d2..a17d32fa5ebb3 100644
@@ -25,6 +25,9 @@ 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;
 
+static char hugetlb_cma_cmdline[COMMAND_LINE_SIZE] __initdata;
+static unsigned int hugetlb_cma_cmdline_len __initdata;
+
 #ifdef CONFIG_NUMA
 static phys_addr_t __init memblock_node_memory_size(int nid)
 {
@@ -119,11 +122,29 @@ void * __init hugetlb_cma_alloc_bootmem(struct hstate *h, int nid, bool node_exa
 	return NULL;
 }
 
-static int __init cmdline_parse_hugetlb_cma(char *p)
+static int __init hugetlb_cma_parse(char *p)
 {
 	int nid, count = 0;
 	unsigned long tmp;
 	char *s = p;
+	char *suffix = strchr(p, '@');
+
+	if (suffix) {
+		unsigned int order = arch_hugetlb_cma_order();
+		unsigned long long page_size;
+		char *end;
+
+		page_size = memparse(suffix + 1, &end);
+		if (suffix == p || !page_size || *end) {
+			pr_warn("hugetlb_cma: invalid parameter %s\n", p);
+			return -EINVAL;
+		}
+		if (!order || page_size != (PAGE_SIZE << order) ||
+		    !arch_hugetlb_valid_size(page_size)) {
+			pr_warn("hugetlb_cma: unsupported page size in %s\n", p);
+			return -EINVAL;
+		}
+	}
 
 	while (*s) {
 		if (sscanf(s, "%lu%n", &tmp, &count) != 1)
@@ -194,6 +215,20 @@ static int __init cmdline_parse_hugetlb_cma(char *p)
 	return 0;
 }
 
+static int __init cmdline_parse_hugetlb_cma(char *p)
+{
+	size_t len;
+
+	if (!p)
+		return -EINVAL;
+	len = strlen(p) + 1;
+	if (len > sizeof(hugetlb_cma_cmdline) - hugetlb_cma_cmdline_len)
+		return -EINVAL;
+	memcpy(hugetlb_cma_cmdline + hugetlb_cma_cmdline_len, p, len);
+	hugetlb_cma_cmdline_len += len;
+
+	return 0;
+}
 early_param("hugetlb_cma", cmdline_parse_hugetlb_cma);
 
 static int __init cmdline_parse_hugetlb_cma_only(char *p)
@@ -214,6 +249,12 @@ void __init hugetlb_cma_reserve(void)
 	bool node_specific_cma_alloc = false;
 	bool has_node_specific_param = false;
 	int nid;
+	char *p;
+
+	for (p = hugetlb_cma_cmdline;
+	     p < hugetlb_cma_cmdline + hugetlb_cma_cmdline_len;
+	     p += strlen(p) + 1)
+		hugetlb_cma_parse(p);
 
 	for (nid = 0; nid < MAX_NUMNODES; nid++) {
 		if (hugetlb_cma_size_in_node[nid] || hugetlb_cma_percent_in_node[nid]) {
Signature · 1 line
2.54.0
author
#3 lore raw

PATCH 2/5 mm: hugetlb: group CMA pool and reservation state

In reply to Qi Zheng

From: Qi Zheng <[email protected]>

The HugeTLB CMA areas, total size, per-node sizes and percentages are stored in separate singleton variables. Group the runtime areas and size into a pool, and the temporary reservation inputs into a separate configuration structure.

Keep one instance of each structure and preserve the existing parsing, reservation and allocation behavior. This separates the mechanical state conversion from adding support for multiple page orders.

Assisted-by LLM
---
 mm/hugetlb_cma.c | 163 ++++++++++++++++++++++++++---------------------
 1 file changed, 89 insertions(+), 74 deletions(-)
mm/hugetlb_cma.c +89 -74
index a17d32fa5ebb3..4f27eb242dc3b 100644
@@ -16,14 +16,21 @@
 #include "internal.h"
 #include "hugetlb_cma.h"
 
+struct hugetlb_cma_pool {
+	struct cma *cma[MAX_NUMNODES];
+	unsigned long size;
+};
 
-static struct cma *hugetlb_cma[MAX_NUMNODES] __ro_after_init;
-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;
+struct hugetlb_cma_config {
+	unsigned long size_in_node[MAX_NUMNODES];
+	unsigned int percent;
+	unsigned int percent_in_node[MAX_NUMNODES];
+};
+
+static struct hugetlb_cma_pool hugetlb_cma __ro_after_init;
+static struct hugetlb_cma_config hugetlb_cma_config __initdata;
 
-static unsigned int hugetlb_cma_percent __initdata;
-static unsigned int hugetlb_cma_percent_in_node[MAX_NUMNODES] __initdata;
+static bool hugetlb_cma_only __ro_after_init;
 
 static char hugetlb_cma_cmdline[COMMAND_LINE_SIZE] __initdata;
 static unsigned int hugetlb_cma_cmdline_len __initdata;
@@ -49,7 +56,9 @@ static phys_addr_t __init memblock_node_memory_size(int nid)
 
 void hugetlb_cma_free_frozen_folio(struct folio *folio)
 {
-	WARN_ON_ONCE(!cma_release_frozen(hugetlb_cma[folio_nid(folio)],
+	struct hugetlb_cma_pool *pool = &hugetlb_cma;
+
+	WARN_ON_ONCE(!cma_release_frozen(pool->cma[folio_nid(folio)],
 					 &folio->page, folio_nr_pages(folio)));
 }
 
@@ -61,8 +70,9 @@ struct folio *hugetlb_cma_alloc_frozen_folio(int order, gfp_t gfp_mask,
 	struct page *page = NULL;
 	const nodemask_t *nmask;
 	unsigned int cpuset_mems_cookie;
+	struct hugetlb_cma_pool *pool = &hugetlb_cma;
 
-	if (!hugetlb_cma_size)
+	if (!pool->size)
 		return NULL;
 
 retry_cpuset:
@@ -73,15 +83,15 @@ struct folio *hugetlb_cma_alloc_frozen_folio(int order, gfp_t gfp_mask,
 		nmask = nodemask;
 	}
 
-	if (hugetlb_cma[nid] && node_isset(nid, *nmask))
-		page = cma_alloc_frozen_compound(hugetlb_cma[nid], order);
+	if (pool->cma[nid] && node_isset(nid, *nmask))
+		page = cma_alloc_frozen_compound(pool->cma[nid], order);
 
 	if (!page && !(gfp_mask & __GFP_THISNODE)) {
 		for_each_node_mask(node, *nmask) {
-			if (node == nid || !hugetlb_cma[node])
+			if (node == nid || !pool->cma[node])
 				continue;
 
-			page = cma_alloc_frozen_compound(hugetlb_cma[node], order);
+			page = cma_alloc_frozen_compound(pool->cma[node], order);
 			if (page)
 				break;
 		}
@@ -101,17 +111,18 @@ struct folio *hugetlb_cma_alloc_frozen_folio(int order, gfp_t gfp_mask,
 
 void * __init hugetlb_cma_alloc_bootmem(struct hstate *h, int nid, bool node_exact)
 {
+	struct hugetlb_cma_pool *pool = &hugetlb_cma;
 	struct cma *cma;
 	void *m;
 	int node;
 
-	cma = hugetlb_cma[nid];
+	cma = pool->cma[nid];
 	m = cma_reserve_early(cma, huge_page_size(h));
 	if (m || node_exact)
 		return m;
 
 	for_each_node_mask(node, hugetlb_bootmem_nodes) {
-		cma = hugetlb_cma[node];
+		cma = pool->cma[node];
 		if (!cma || node == nid)
 			continue;
 		m = cma_reserve_early(cma, huge_page_size(h));
@@ -124,6 +135,8 @@ void * __init hugetlb_cma_alloc_bootmem(struct hstate *h, int nid, bool node_exa
 
 static int __init hugetlb_cma_parse(char *p)
 {
+	struct hugetlb_cma_pool *pool = &hugetlb_cma;
+	struct hugetlb_cma_config *config = &hugetlb_cma_config;
 	int nid, count = 0;
 	unsigned long tmp;
 	char *s = p;
@@ -157,8 +170,8 @@ static int __init hugetlb_cma_parse(char *p)
 				break;
 			nid = array_index_nospec(tmp, MAX_NUMNODES);
 
-			hugetlb_cma_size = 0;
-			hugetlb_cma_percent = 0;
+			pool->size = 0;
+			config->percent = 0;
 
 			s += count + 1;
 			tmp = memparse(s, &next);
@@ -168,12 +181,12 @@ static int __init hugetlb_cma_parse(char *p)
 						tmp, nid);
 					break;
 				}
-				hugetlb_cma_percent_in_node[nid] = tmp;
-				hugetlb_cma_size_in_node[nid] = 0;
+				config->percent_in_node[nid] = tmp;
+				config->size_in_node[nid] = 0;
 				s = next + 1;
 			} else {
-				hugetlb_cma_size_in_node[nid] = tmp;
-				hugetlb_cma_percent_in_node[nid] = 0;
+				config->size_in_node[nid] = tmp;
+				config->percent_in_node[nid] = 0;
 				s = next;
 			}
 
@@ -193,19 +206,19 @@ static int __init hugetlb_cma_parse(char *p)
 				if (tmp > 100) {
 					pr_warn("hugetlb_cma: invalid percentage %lu\n", tmp);
 				} else {
-					hugetlb_cma_percent = tmp;
-					hugetlb_cma_size = 0;
+					config->percent = tmp;
+					pool->size = 0;
 					for (nid = 0; nid < MAX_NUMNODES; nid++) {
-						hugetlb_cma_size_in_node[nid] = 0;
-						hugetlb_cma_percent_in_node[nid] = 0;
+						config->size_in_node[nid] = 0;
+						config->percent_in_node[nid] = 0;
 					}
 				}
 			} else {
-				hugetlb_cma_size = tmp;
-				hugetlb_cma_percent = 0;
+				pool->size = tmp;
+				config->percent = 0;
 				for (nid = 0; nid < MAX_NUMNODES; nid++) {
-					hugetlb_cma_size_in_node[nid] = 0;
-					hugetlb_cma_percent_in_node[nid] = 0;
+					config->size_in_node[nid] = 0;
+					config->percent_in_node[nid] = 0;
 				}
 			}
 			break;
@@ -245,6 +258,8 @@ unsigned int __weak arch_hugetlb_cma_order(void)
 
 void __init hugetlb_cma_reserve(void)
 {
+	struct hugetlb_cma_pool *pool = &hugetlb_cma;
+	struct hugetlb_cma_config *config = &hugetlb_cma_config;
 	unsigned long size, reserved, per_node, order, gigantic_page_size;
 	bool node_specific_cma_alloc = false;
 	bool has_node_specific_param = false;
@@ -257,33 +272,33 @@ void __init hugetlb_cma_reserve(void)
 		hugetlb_cma_parse(p);
 
 	for (nid = 0; nid < MAX_NUMNODES; nid++) {
-		if (hugetlb_cma_size_in_node[nid] || hugetlb_cma_percent_in_node[nid]) {
+		if (config->size_in_node[nid] || config->percent_in_node[nid]) {
 			has_node_specific_param = true;
 			break;
 		}
 	}
 
 	if (has_node_specific_param) {
-		hugetlb_cma_size = 0;
+		pool->size = 0;
 		for (nid = 0; nid < MAX_NUMNODES; nid++) {
-			if (hugetlb_cma_percent_in_node[nid]) {
+			if (config->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],
+						    config->percent_in_node[nid],
 						    100);
 
-				hugetlb_cma_size_in_node[nid] = s;
+				config->size_in_node[nid] = s;
 			}
-			hugetlb_cma_size += hugetlb_cma_size_in_node[nid];
+			pool->size += config->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);
+	} else if (config->percent) {
+		pool->size = mul_u64_u32_div((u64)memblock_phys_mem_size(),
+						   config->percent, 100);
 	}
 
-	if (!hugetlb_cma_size)
+	if (!pool->size)
 		return;
 
 	order = arch_hugetlb_cma_order();
@@ -301,36 +316,36 @@ void __init hugetlb_cma_reserve(void)
 	VM_WARN_ON(order <= MAX_PAGE_ORDER);
 	gigantic_page_size = PAGE_SIZE << order;
 
-	if (hugetlb_cma_percent) {
-		unsigned long orig_size = hugetlb_cma_size;
+	if (config->percent) {
+		unsigned long orig_size = pool->size;
 
-		hugetlb_cma_size = ALIGN_DOWN(hugetlb_cma_size, PAGE_SIZE << order);
-		if (orig_size && !hugetlb_cma_size)
+		pool->size = ALIGN_DOWN(pool->size, PAGE_SIZE << order);
+		if (orig_size && !pool->size)
 			pr_warn("hugetlb_cma: reservation size rounded down to 0 from %lu MiB (%u%%)\n",
-				orig_size / SZ_1M, hugetlb_cma_percent);
+				orig_size / SZ_1M, config->percent);
 	} else if (has_node_specific_param) {
-		hugetlb_cma_size = 0;
+		pool->size = 0;
 		for (nid = 0; nid < MAX_NUMNODES; nid++) {
-			if (hugetlb_cma_percent_in_node[nid]) {
-				unsigned long orig_size = hugetlb_cma_size_in_node[nid];
+			if (config->percent_in_node[nid]) {
+				unsigned long orig_size = config->size_in_node[nid];
 
-				hugetlb_cma_size_in_node[nid] =
-					ALIGN_DOWN(hugetlb_cma_size_in_node[nid],
+				config->size_in_node[nid] =
+					ALIGN_DOWN(config->size_in_node[nid],
 						   PAGE_SIZE << order);
-				if (orig_size && !hugetlb_cma_size_in_node[nid])
+				if (orig_size && !config->size_in_node[nid])
 					pr_warn("hugetlb_cma: reservation size rounded down to 0 from %lu MiB (%u%%) on node %d\n",
 						orig_size / SZ_1M,
-						hugetlb_cma_percent_in_node[nid],
+						config->percent_in_node[nid],
 						nid);
 			}
-			hugetlb_cma_size += hugetlb_cma_size_in_node[nid];
+			pool->size += config->size_in_node[nid];
 		}
 	}
 
 	hugetlb_bootmem_set_nodes();
 
 	for (nid = 0; nid < MAX_NUMNODES; nid++) {
-		size = hugetlb_cma_size_in_node[nid];
+		size = config->size_in_node[nid];
 		if (size == 0)
 			continue;
 
@@ -344,18 +359,18 @@ void __init hugetlb_cma_reserve(void)
 			continue;
 		}
 
-		hugetlb_cma_size -= size;
-		hugetlb_cma_size_in_node[nid] = 0;
+		pool->size -= size;
+		config->size_in_node[nid] = 0;
 	}
 
 	/* Validate the CMA size again in case some invalid nodes specified. */
-	if (!hugetlb_cma_size)
+	if (!pool->size)
 		return;
 
-	if (!IS_ALIGNED(hugetlb_cma_size, gigantic_page_size)) {
+	if (!IS_ALIGNED(pool->size, gigantic_page_size)) {
 		pr_warn("hugetlb_cma: cma area must be a multiple of %lu MiB\n",
 			gigantic_page_size / SZ_1M);
-		hugetlb_cma_size = 0;
+		pool->size = 0;
 		return;
 	}
 
@@ -364,16 +379,16 @@ void __init hugetlb_cma_reserve(void)
 		 * If 3 GB area is requested on a machine with 4 numa nodes,
 		 * let's allocate 1 GB on first three nodes and ignore the last one.
 		 */
-		per_node = DIV_ROUND_UP(hugetlb_cma_size,
+		per_node = DIV_ROUND_UP(pool->size,
 					nodes_weight(hugetlb_bootmem_nodes));
 		per_node = round_up(per_node, gigantic_page_size);
-		if (hugetlb_cma_percent)
+		if (config->percent)
 			pr_info("hugetlb_cma: reserve %lu MiB (%u%%), up to %lu MiB per node\n",
-				hugetlb_cma_size / SZ_1M, hugetlb_cma_percent,
+				pool->size / SZ_1M, config->percent,
 				per_node / SZ_1M);
 		else
 			pr_info("hugetlb_cma: reserve %lu MiB, up to %lu MiB per node\n",
-				hugetlb_cma_size / SZ_1M, per_node / SZ_1M);
+				pool->size / SZ_1M, per_node / SZ_1M);
 	}
 
 	reserved = 0;
@@ -382,12 +397,12 @@ void __init hugetlb_cma_reserve(void)
 		char name[CMA_MAX_NAME];
 
 		if (node_specific_cma_alloc) {
-			if (hugetlb_cma_size_in_node[nid] == 0)
+			if (config->size_in_node[nid] == 0)
 				continue;
 
-			size = hugetlb_cma_size_in_node[nid];
+			size = config->size_in_node[nid];
 		} else {
-			size = min(per_node, hugetlb_cma_size - reserved);
+			size = min(per_node, pool->size - reserved);
 		}
 
 		snprintf(name, sizeof(name), "hugetlb%d", nid);
@@ -398,33 +413,33 @@ void __init hugetlb_cma_reserve(void)
 		 */
 		res = cma_declare_contiguous_multi(size, gigantic_page_size,
 					HUGETLB_PAGE_ORDER, name,
-					&hugetlb_cma[nid], nid);
-		if (res || !cma_validate_zones(hugetlb_cma[nid])) {
+					&pool->cma[nid], nid);
+		if (res || !cma_validate_zones(pool->cma[nid])) {
 			pr_warn("hugetlb_cma: %s: err %d, node %d\n",
 				res ? "reservation failed" : "reserved area spans zones",
 				res, nid);
-			hugetlb_cma[nid] = NULL;
+			pool->cma[nid] = NULL;
 			continue;
 		}
 
 		reserved += size;
-		if (hugetlb_cma_percent_in_node[nid])
+		if (config->percent_in_node[nid])
 			pr_info("hugetlb_cma: reserved %lu MiB (%u%%) on node %d\n",
-				size / SZ_1M, hugetlb_cma_percent_in_node[nid], nid);
+				size / SZ_1M, config->percent_in_node[nid], nid);
 		else
 			pr_info("hugetlb_cma: reserved %lu MiB on node %d\n",
 				size / SZ_1M, nid);
 
-		if (reserved >= hugetlb_cma_size)
+		if (reserved >= pool->size)
 			break;
 	}
 
 	if (!reserved)
 		/*
-		 * hugetlb_cma_size is used to determine if allocations from
+		 * pool->size is used to determine if allocations from
 		 * cma are possible.  Set to zero if no cma regions are set up.
 		 */
-		hugetlb_cma_size = 0;
+		pool->size = 0;
 }
 
 bool hugetlb_cma_exclusive_alloc(void)
@@ -434,12 +449,12 @@ bool hugetlb_cma_exclusive_alloc(void)
 
 unsigned long __init hugetlb_cma_total_size(void)
 {
-	return hugetlb_cma_size;
+	return hugetlb_cma.size;
 }
 
 void __init hugetlb_cma_validate_params(void)
 {
-	if (!hugetlb_cma_size)
+	if (!hugetlb_cma.size)
 		hugetlb_cma_only = false;
 }
 
Signature · 1 line
2.54.0
author
#5 lore raw

PATCH 3/5 mm: hugetlb: track CMA reservations by page order

In reply to Qi Zheng

From: Qi Zheng <[email protected]>

Prepare for a separate CMA pool for PMD-sized HugeTLB pages by organizing CMA areas and reservation parameters into pools identified by page order.

Select the corresponding pool for allocation, CMA-only checks and boot-time allocation decisions. Release folios through the CMA area that owns their physical memory, including after demotion.

Record each pool's successfully reserved capacity. Remove hugetlb_cma_validate_params(), since the per-pool CMA-only check already handles missing or empty pools.

Continue accepting only the architecture's existing CMA page size and preserve the default pool's use by all gigantic sizes. PMD-sized pool support will be added in a subsequent patch.

Assisted-by LLM
---
 mm/hugetlb.c     |   6 +-
 mm/hugetlb_cma.c | 154 ++++++++++++++++++++++++++++++++---------------
 mm/hugetlb_cma.h |  11 ++--
 3 files changed, 112 insertions(+), 59 deletions(-)
mm/hugetlb.c +2 -4
index 76f4d2a5e0206..9d569eae852c5 100644
@@ -1368,7 +1368,7 @@ static struct folio *alloc_gigantic_frozen_folio(int order, gfp_t gfp_mask,
 	if (folio)
 		return folio;
 
-	if (hugetlb_cma_exclusive_alloc())
+	if (hugetlb_cma_exclusive_alloc(order))
 		return NULL;
 
 	folio = (struct folio *)alloc_contig_frozen_pages(1 << order, gfp_mask,
@@ -3635,7 +3635,7 @@ static void __init hugetlb_hstate_alloc_pages(struct hstate *h)
 	 * Skip gigantic hugepages allocation if early CMA
 	 * reservations are not available.
 	 */
-	if (hstate_is_gigantic(h) && hugetlb_cma_total_size() &&
+	if (hstate_is_gigantic(h) && hugetlb_cma_size(h) &&
 	    !hugetlb_early_cma(h)) {
 		pr_warn_once("hugetlb_cma is enabled, skip boot time allocation\n");
 		return;
@@ -4338,8 +4338,6 @@ static __init void hugetlb_parse_params(void)
 
 		hcp->setup(hcp->val);
 	}
-
-	hugetlb_cma_validate_params();
 }
 
 /*
mm/hugetlb_cma.c +104 -50

287 lines not on this page · raw message

mm/hugetlb_cma.h +6 -5
index 730b2b4965b64..3d829258f6728 100644
@@ -9,9 +9,9 @@ void hugetlb_cma_free_frozen_folio(struct folio *folio);
 struct folio *hugetlb_cma_alloc_frozen_folio(int order, gfp_t gfp_mask,
 				      int nid, nodemask_t *nodemask);
 void *hugetlb_cma_alloc_bootmem(struct hstate *h, int nid, bool node_exact);
-bool hugetlb_cma_exclusive_alloc(void);
+bool hugetlb_cma_exclusive_alloc(unsigned int order);
+unsigned long hugetlb_cma_size(struct hstate *h);
 unsigned long hugetlb_cma_total_size(void);
-void hugetlb_cma_validate_params(void);
 bool hugetlb_early_cma(struct hstate *h);
 #else
 static inline void hugetlb_cma_free_frozen_folio(struct folio *folio)
@@ -30,18 +30,19 @@ static inline void *hugetlb_cma_alloc_bootmem(struct hstate *h, int nid,
 	return NULL;
 }
 
-static inline bool hugetlb_cma_exclusive_alloc(void)
+static inline bool hugetlb_cma_exclusive_alloc(unsigned int order)
 {
 	return false;
 }
 
-static inline unsigned long hugetlb_cma_total_size(void)
+static inline unsigned long hugetlb_cma_size(struct hstate *h)
 {
 	return 0;
 }
 
-static inline void hugetlb_cma_validate_params(void)
+static inline unsigned long hugetlb_cma_total_size(void)
 {
+	return 0;
 }
 
 static inline bool hugetlb_early_cma(struct hstate *h)
Signature · 1 line
2.54.0
author
#8 lore raw

PATCH 4/5 mm: hugetlb: avoid recursive CMA allocation during migration

In reply to Qi Zheng

From: Qi Zheng <[email protected]>

Contiguous allocation can migrate allocated HugeTLB folios or replace free HugeTLB folios while holding a CMA allocation mutex. Allocating a replacement through CMA would recurse into that allocation path once non-gigantic HugeTLB pages can use CMA.

Add a fresh allocation helper that bypasses CMA, and use it for free folio replacement. Pass an explicit allow_cma argument through the migration allocation API and disable it for MR_CONTIG_RANGE. Honor hugetlb_cma_only by failing fresh allocation when the corresponding size requires CMA.

The existing gigantic migration restriction is unchanged.

Assisted-by LLM
---
 include/linux/hugetlb.h |  4 ++--
 mm/hugetlb.c            | 45 ++++++++++++++++++++++++++++++++---------
 mm/mempolicy.c          |  2 +-
 mm/migrate.c            |  3 ++-
 4 files changed, 41 insertions(+), 13 deletions(-)
include/linux/hugetlb.h +2 -2
index 5029c72418631..38612f7b3478f 100644
@@ -736,7 +736,7 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,
 				unsigned long addr, bool cow_from_owner);
 struct folio *alloc_hugetlb_folio_nodemask(struct hstate *h, int preferred_nid,
 				nodemask_t *nmask, gfp_t gfp_mask,
-				bool allow_alloc_fallback);
+				bool allow_alloc_fallback, bool allow_cma);
 struct folio *alloc_hugetlb_folio_reserve(struct hstate *h, int preferred_nid,
 					  nodemask_t *nmask, gfp_t gfp_mask);
 
@@ -1172,7 +1172,7 @@ alloc_hugetlb_folio_reserve(struct hstate *h, int preferred_nid,
 static inline struct folio *
 alloc_hugetlb_folio_nodemask(struct hstate *h, int preferred_nid,
 			nodemask_t *nmask, gfp_t gfp_mask,
-			bool allow_alloc_fallback)
+			bool allow_alloc_fallback, bool allow_cma)
 {
 	return NULL;
 }
mm/hugetlb.c +36 -9

93 lines not on this page · raw message

mm/mempolicy.c +1 -1
index 70298fded1b4a..b59b8c73b8ce3 100644
@@ -1471,7 +1471,7 @@ static struct folio *alloc_migration_target_by_mpol(struct folio *src,
 		gfp = htlb_alloc_mask(h);
 		nodemask = policy_nodemask(gfp, pol, ilx, &nid);
 		return alloc_hugetlb_folio_nodemask(h, nid, nodemask, gfp,
-				htlb_allow_alloc_fallback(MR_MEMPOLICY_MBIND));
+				htlb_allow_alloc_fallback(MR_MEMPOLICY_MBIND), true);
 	}
 
 	if (folio_test_large(src))
mm/migrate.c +2 -1
index 7bdcdb57652f8..670556bf5de2c 100644
@@ -2213,7 +2213,8 @@ struct folio *alloc_migration_target(struct folio *src, unsigned long private)
 		gfp_mask = htlb_modify_alloc_mask(h, gfp_mask);
 		return alloc_hugetlb_folio_nodemask(h, nid,
 						mtc->nmask, gfp_mask,
-						htlb_allow_alloc_fallback(mtc->reason));
+						htlb_allow_alloc_fallback(mtc->reason),
+						mtc->reason != MR_CONTIG_RANGE);
 	}
 
 	if (folio_test_large(src)) {
Signature · 1 line
2.54.0
author
#11 lore raw

PATCH 5/5 mm: hugetlb: enable CMA allocation for PMD-sized pages

In reply to Qi Zheng

From: Qi Zheng <[email protected]>

Allow hugetlb_cma=<capacity>@<page-size> to select the architecture's PMD size in addition to its existing CMA size. PMD pages are 2M on x86-64 and 2M, 32M or 512M on arm64 with 4K, 16K or 64K base pages.

Give PMD pages their own pool without changing the original pool's sharing among other gigantic sizes. On arm64 with 4K base pages, 32M and 1G pages continue to use the default 1G-aligned pool. Unsuffixed arguments and an explicit default size still configure that same pool.

PMD requests use only the PMD pool, even when their order is gigantic. Other gigantic requests use only the original pool. Apply this mapping consistently to allocation, CMA-only policy and boot-time accounting.

Move CMA allocation into the common fresh HugeTLB allocation path so non-gigantic pages can use CMA. Align reservations to both the selected page size and CMA's minimum alignment, and use a bitmap granularity no larger than the selected order. Keep the original CMA area names and give PMD areas distinct names. Document the syntax and pool selection.

Assisted-by LLM
---
 .../admin-guide/kernel-parameters.txt         | 39 ++++++++++++-------
 mm/hugetlb.c                                  | 31 +++++++--------
 mm/hugetlb_cma.c                              | 26 ++++++-------
 3 files changed, 52 insertions(+), 44 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +25 -14
index e3f25e805fea6..71cd431f07fe2 100644
@@ -2065,31 +2065,42 @@ Kernel parameters
 			Note that this parameter only applies to non-gigantic huge pages.
 
 	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.
+			of hugepages. Or using node format, the size of a CMA area
+			per node can be specified.
 			The size can be an absolute value (e.g., 2G) or a
 			percentage of the total memory or node memory (e.g., 20%).
 			Percentage-derived sizes are rounded down to a multiple of
-			the architecture's gigantic hugepage size and may become
-			zero.
-			Format: nn[KMGTPE] or nn% or (node format)
-				<node>:nn[KMGTPE][,<node>:nn[KMGTPE]] or
-				<node>:nn%[,<node>:nn%]
-
-			The size must be a multiple of the gigantic page size.
+			the larger of the selected hugepage size and CMA's minimum
+			alignment, and may become zero.
+			Format: nn[KMGTPE][@<page-size>] or nn%[@<page-size>]
+				or (node format)
+				<node>:nn[KMGTPE][,<node>:nn[KMGTPE]][@<page-size>] or
+				<node>:nn%[,<node>:nn%][@<page-size>]
+
+			An optional @<page-size> suffix selects the architecture's
+			default CMA hugepage size or a supported PMD size. If
+			omitted, the default is used. For node format, append
+			the suffix after the entire node list. Repeat the
+			parameter to configure the two pools independently.
+
+			The size must be aligned to both the selected hugepage
+			size and CMA's minimum alignment.
 			When using node format, this applies to each per-node size.
 			Missaligned values are dropped with a warning.
 
-			Reserve a CMA area of given size and allocate gigantic
-			hugepages using the CMA allocator. If enabled, the
-			boot-time allocation of gigantic hugepages is skipped.
+			Reserve a CMA area of given size and allocate hugepages
+			using the CMA allocator. For gigantic page sizes with a
+			reserved CMA pool, normal boot-time allocation is skipped.
+			With hugetlb_cma_only=1, early CMA allocation is used where
+			supported.
+
 
 	hugetlb_cma_only=
 			[HW,CMA,EARLY] When allocating new HugeTLB pages, only
 			try to allocate from the CMA areas.
 
-			This option does nothing if hugetlb_cma= is not also
-			specified.
+			This option only affects page sizes with a CMA pool
+			reserved by hugetlb_cma=.
 
 	hugetlb_free_vmemmap=
 			[KNL] Requires CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP
mm/hugetlb.c +14 -17
index 218fbaee6f7e5..8c00fd9d0658d 100644
@@ -1362,18 +1362,8 @@ static struct folio *dequeue_hugetlb_folio(struct hstate *h, gfp_t gfp_mask,
 static struct folio *alloc_gigantic_frozen_folio(int order, gfp_t gfp_mask,
 		int nid, nodemask_t *nodemask)
 {
-	struct folio *folio;
-
-	folio = hugetlb_cma_alloc_frozen_folio(order, gfp_mask, nid, nodemask);
-	if (folio)
-		return folio;
-
-	if (hugetlb_cma_exclusive_alloc(order))
-		return NULL;
-
-	folio = (struct folio *)alloc_contig_frozen_pages(1 << order, gfp_mask,
-							  nid, nodemask);
-	return folio;
+	return (struct folio *)alloc_contig_frozen_pages(1 << order, gfp_mask,
+							 nid, nodemask);
 }
 #else /* !CONFIG_ARCH_HAS_GIGANTIC_PAGE || !CONFIG_CONTIG_ALLOC */
 static struct folio *alloc_gigantic_frozen_folio(int order, gfp_t gfp_mask, int nid,
@@ -1849,11 +1839,18 @@ static struct folio *only_alloc_fresh_hugetlb_folio(struct hstate *h,
 	if (nid == NUMA_NO_NODE)
 		nid = numa_mem_id();
 
-	if (order_is_gigantic(order))
-		folio = alloc_gigantic_frozen_folio(order, gfp_mask, nid, nmask);
-	else
-		folio = alloc_buddy_frozen_folio(order, gfp_mask, nid, nmask,
-						 node_alloc_noretry);
+	folio = hugetlb_cma_alloc_frozen_folio(order, gfp_mask, nid, nmask);
+	if (!folio) {
+		if (hugetlb_cma_exclusive_alloc(order))
+			return NULL;
+
+		if (order_is_gigantic(order))
+			folio = alloc_gigantic_frozen_folio(order, gfp_mask, nid, nmask);
+		else
+			folio = alloc_buddy_frozen_folio(order, gfp_mask, nid, nmask,
+							 node_alloc_noretry);
+	}
+
 	if (folio)
 		init_new_hugetlb_folio(folio);
 	return folio;
mm/hugetlb_cma.c +13 -13
index 9f1dfe6c2af67..9dd376979e345 100644
@@ -42,8 +42,8 @@ static struct hugetlb_cma_pool *hugetlb_cma_pool(unsigned int order)
 {
 	unsigned int i;
 
-	/* Gigantic sizes share the architecture's original CMA pool. */
-	if (order_is_gigantic(order))
+	/* PMD pages use their own pool; other gigantic sizes share. */
+	if (order != PMD_SHIFT - PAGE_SHIFT && order_is_gigantic(order))
 		order = hugetlb_cma_default_order;
 
 	for (i = 0; i < hugetlb_cma_nr; i++) {
@@ -177,11 +177,13 @@ static int __init hugetlb_cma_parse(char *p)
 			pr_warn("hugetlb_cma: invalid parameter %s\n", p);
 			return -EINVAL;
 		}
-		if (!order || page_size != (PAGE_SIZE << order) ||
+		if ((page_size != PMD_SIZE &&
+		     page_size != (PAGE_SIZE << order)) ||
 		    !arch_hugetlb_valid_size(page_size)) {
 			pr_warn("hugetlb_cma: unsupported page size in %s\n", p);
 			return -EINVAL;
 		}
+		order = ilog2(page_size) - PAGE_SHIFT;
 	}
 
 	if (!order) {
@@ -340,14 +342,7 @@ static void __init hugetlb_cma_reserve_pool(struct hugetlb_cma_pool *pool,
 	if (!pool->size)
 		return;
 
-	/*
-	 * HugeTLB CMA reservation is required for gigantic
-	 * huge pages which could not be allocated via the
-	 * page allocator. Just warn if there is any change
-	 * breaking this assumption.
-	 */
-	VM_WARN_ON(order <= MAX_PAGE_ORDER);
-	hugepage_size = PAGE_SIZE << order;
+	hugepage_size = max(PAGE_SIZE << order, CMA_MIN_ALIGNMENT_BYTES);
 
 	if (config->percent) {
 		unsigned long orig_size = pool->size;
@@ -438,14 +433,19 @@ static void __init hugetlb_cma_reserve_pool(struct hugetlb_cma_pool *pool,
 			size = min(per_node, pool->size - reserved);
 		}
 
-		snprintf(name, sizeof(name), "hugetlb%d", nid);
+		if (order == hugetlb_cma_default_order)
+			snprintf(name, sizeof(name), "hugetlb%d", nid);
+		else
+			snprintf(name, sizeof(name), "hugetlb-%lukB-%d",
+				 (PAGE_SIZE << order) / SZ_1K, nid);
+
 		/*
 		 * Note that 'order per bit' is based on smallest size that
 		 * may be returned to CMA allocator in the case of
 		 * huge page demotion.
 		 */
 		res = cma_declare_contiguous_multi(size, hugepage_size,
-					HUGETLB_PAGE_ORDER, name,
+					min_t(unsigned int, order, HUGETLB_PAGE_ORDER), name,
 					&pool->cma[nid], nid);
 		if (res || !cma_validate_zones(pool->cma[nid])) {
 			pr_warn("hugetlb_cma: %s: err %d, node %d\n",
Signature · 1 line
2.54.0
Reviewed-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] mm: hugetlb: add explicit page size to hugetlb_cma

In reply to Qi Zheng
Quoted 9 lines · Qi Zheng

From: Qi Zheng <[email protected]>

Accept an optional @<page-size> suffix for hugetlb_cma. Initially accept only the architecture's existing CMA page size. Preserve the behavior of unsuffixed arguments and the existing hugepagesz/hugepages options.

Validate the suffix before applying the existing capacity, percentage and node-list parser.

Quoted 6 lines

Save the arguments during early parsing and process them at reservation time, after architecture-specific page-size information has been initialized.

Assisted-by LLM

this LGTM so:

Signature · 2 lines
cheers,
jose a. p-a
Reviewed-by on v1 2/5
#4 lore raw

Re: [PATCH 2/5] mm: hugetlb: group CMA pool and reservation state

In reply to Qi Zheng
Quoted 7 lines · Qi Zheng

From: Qi Zheng <[email protected]>

The HugeTLB CMA areas, total size, per-node sizes and percentages are stored in separate singleton variables. Group the runtime areas and size into a pool, and the temporary reservation inputs into a separate configuration structure.

Quoted 6 lines

Keep one instance of each structure and preserve the existing parsing, reservation and allocation behavior. This separates the mechanical state conversion from adding support for multiple page orders.

Assisted-by LLM

this LGTM too so:

Signature · 2 lines
cheers,
jose a. p-a
#6 lore raw

Re: [PATCH 3/5] mm: hugetlb: track CMA reservations by page order

In reply to Qi Zheng
Quoted 19 lines · Qi Zheng

From: Qi Zheng <[email protected]>

Prepare for a separate CMA pool for PMD-sized HugeTLB pages by organizing CMA areas and reservation parameters into pools identified by page order.

Select the corresponding pool for allocation, CMA-only checks and boot-time allocation decisions. Release folios through the CMA area that owns their physical memory, including after demotion.

Record each pool's successfully reserved capacity. Remove hugetlb_cma_validate_params(), since the per-pool CMA-only check already handles missing or empty pools.

Continue accepting only the architecture's existing CMA page size and preserve the default pool's use by all gigantic sizes. PMD-sized pool support will be added in a subsequent patch.

Assisted-by LLM

[snip]

mm/hugetlb_cma.c struct hugetlb_cma_config { · line 33

Quoted 3 lines
mm/hugetlb_cma.c +3 -0
+static struct hugetlb_cma_pool hugetlb_cma[HUGE_MAX_HSTATE] __ro_after_init;
+static struct hugetlb_cma_config hugetlb_cma_config[HUGE_MAX_HSTATE] __initdata;
+static unsigned int hugetlb_cma_nr __ro_after_init;

the commit message prepares for one more pool, the PMD one, and hugetlb_cma_pool() folds every gigantic order into the default pool. would a fixed pair work instead of an order-keyed array?

Signature · 2 lines
cheers,
jose a. p-a
#9 lore raw

Re: [PATCH 4/5] mm: hugetlb: avoid recursive CMA allocation during migration

In reply to Qi Zheng
Quoted 16 lines · Qi Zheng

From: Qi Zheng <[email protected]>

Contiguous allocation can migrate allocated HugeTLB folios or replace free HugeTLB folios while holding a CMA allocation mutex. Allocating a replacement through CMA would recurse into that allocation path once non-gigantic HugeTLB pages can use CMA.

Add a fresh allocation helper that bypasses CMA, and use it for free folio replacement. Pass an explicit allow_cma argument through the migration allocation API and disable it for MR_CONTIG_RANGE. Honor hugetlb_cma_only by failing fresh allocation when the corresponding size requires CMA.

The existing gigantic migration restriction is unchanged.

Assisted-by LLM

[snip]

Quoted 4 lines

/*
- * Common helper to allocate a fresh hugetlb folio. All specific allocators
- * should use this function to get new hugetlb folio
+ * Common helper to allocate a fresh hugetlb folio.

couldn't allow_cma be passed down to alloc_fresh_hugetlb_folio() instead of adding a second copy of it, so this line can stay?

Signature · 2 lines
cheers,
jose a. p-a
#12 lore raw

Re: [PATCH 5/5] mm: hugetlb: enable CMA allocation for PMD-sized pages

In reply to Qi Zheng
Quoted 23 lines · Qi Zheng

From: Qi Zheng <[email protected]>

Allow hugetlb_cma=<capacity>@<page-size> to select the architecture's PMD size in addition to its existing CMA size. PMD pages are 2M on x86-64 and 2M, 32M or 512M on arm64 with 4K, 16K or 64K base pages.

Give PMD pages their own pool without changing the original pool's sharing among other gigantic sizes. On arm64 with 4K base pages, 32M and 1G pages continue to use the default 1G-aligned pool. Unsuffixed arguments and an explicit default size still configure that same pool.

PMD requests use only the PMD pool, even when their order is gigantic. Other gigantic requests use only the original pool. Apply this mapping consistently to allocation, CMA-only policy and boot-time accounting.

Move CMA allocation into the common fresh HugeTLB allocation path so non-gigantic pages can use CMA. Align reservations to both the selected page size and CMA's minimum alignment, and use a bitmap granularity no larger than the selected order. Keep the original CMA area names and give PMD areas distinct names. Document the syntax and pool selection.

Assisted-by LLM

[snip]

mm/hugetlb.c static struct folio *only_alloc_fresh_hugetlb_folio(struct hstate *h, · line 1843

Quoted 2 lines
mm/hugetlb.c +2 -0
+	folio = hugetlb_cma_alloc_frozen_folio(order, gfp_mask, nid, nmask);
+	if (!folio) {

boot-time hugepages= goes through here too, so hugepages=128 with hugetlb_cma=512M@2M already uses half of the pool at boot. would buddy first and cma as fallback make more sense for PMD?

[snip]

mm/hugetlb_cma.c static struct hugetlb_cma_pool *hugetlb_cma_pool(unsigned int order) · line 47

Quoted 2 lines
mm/hugetlb_cma.c +1 -0
+	if (order != PMD_SHIFT - PAGE_SHIFT && order_is_gigantic(order))
 		order = hugetlb_cma_default_order;

doesn't this take gigantic PMD-order pages out of the default pool even when no PMD pool is set? would trying the exact order first and then falling back to the default pool work?

I tested it on qemu and saw no issues, but I'm hesitant about the points above. I can give a Tested-by tag on the next version :)

Signature · 2 lines
cheers,
jose a. p-a
maintainer
#16 lore raw
In reply to Qi Zheng
Quoted 13 lines · Qi Zheng

From: Qi Zheng <[email protected]>

Hi all,

Allocating PMD-sized HugeTLB pages at runtime can become slow or fail as physical memory becomes fragmented. Reserving HugeTLB pages at boot avoids this problem, but keeps unused pages unavailable to ordinary allocations.

Quoted 6 lines

Live upgrades are one example: they need additional HugeTLB pages only during the upgrade. HugeTLB already supports CMA for gigantic pages, but reserving a CMA pool for 1 GiB pages can fail when physical KASLR and other early reservations leave too few suitable 1 GiB-aligned ranges. At runtime, 1 GiB allocations can also fail when pages cannot be migrated out of a suitable contiguous range.

Yes, that's our use case at ByteDance. Allocating a 1GB HugeTLB page from the CMA pool is more prone to failure than allocating a 2MB one. To improve the probability of successful allocation, we typically configure the CMA size to be larger than our actual requirement, keeping a certain margin to handle cases where temporary kernel operations (such as DMA) prevent successful migration. For 1G HugeTLB pages, since the margin must be an integer multiple of 1G, the extra memory configured as headroom is also relatively large, making it less flexible than 2M pages.

Quoted 10 lines

Extend CMA support to PMD-sized pages so their backing memory can be reserved at boot while remaining usable for movable allocations until needed. This relaxes the reservation alignment requirement and reduces the contiguous range required per allocation.

The series adds an optional @<page-size> suffix to hugetlb_cma and a separate PMD-sized pool. For example, on x86-64:

hugetlb_cma=4G hugetlb_cma=1G@2M

Different sizes of gigantic pages already share a single HugeTLB CMA pool. Given that, why not let 2M pages also reuse this same pool? Would that make the overall code implementation simpler? And if it does make things simpler, should we go ahead and do it first?

Thanks,
Muchun

Quoted 33 lines

This reserves 4G for the existing gigantic-page pool and 1G for the PMD-sized pool. Existing unsuffixed parameters and gigantic-page pool sharing are preserved. The new size follows the architecture's PMD size rather than being fixed at 2M.

The preparatory patches introduce per-pool state and prevent recursive CMA allocation during migration before enabling PMD-sized allocation.

Thanks,
Qi

Qi Zheng (5):
mm: hugetlb: add explicit page size to hugetlb_cma
mm: hugetlb: group CMA pool and reservation state
mm: hugetlb: track CMA reservations by page order
mm: hugetlb: avoid recursive CMA allocation during migration
mm: hugetlb: enable CMA allocation for PMD-sized pages

.../admin-guide/kernel-parameters.txt         |  39 ++-
include/linux/hugetlb.h                       |   4 +-
mm/hugetlb.c                                  |  80 +++--
mm/hugetlb_cma.c                              | 328 ++++++++++++------
mm/hugetlb_cma.h                              |  11 +-
mm/mempolicy.c                                |   2 +-
mm/migrate.c                                  |   3 +-
7 files changed, 306 insertions(+), 161 deletions(-)

base-commit: b810335a0699d1810683796b0e3ffe8827ddfe6c

Signature · 1 line
2.54.0
#7 lore raw

Re: [PATCH 3/5] mm: hugetlb: track CMA reservations by page order

In reply to Jose A. Perez de Azpillaga

Hi Jose,

Quoted 23 lines · Jose A. Perez de Azpillaga
Quoted 19 lines · Qi Zheng

From: Qi Zheng <[email protected]>

Prepare for a separate CMA pool for PMD-sized HugeTLB pages by organizing CMA areas and reservation parameters into pools identified by page order.

Select the corresponding pool for allocation, CMA-only checks and boot-time allocation decisions. Release folios through the CMA area that owns their physical memory, including after demotion.

Record each pool's successfully reserved capacity. Remove hugetlb_cma_validate_params(), since the per-pool CMA-only check already handles missing or empty pools.

Continue accepting only the architecture's existing CMA page size and preserve the default pool's use by all gigantic sizes. PMD-sized pool support will be added in a subsequent patch.

Assisted-by LLM

[snip]

Quoted 7 lines

mm/hugetlb_cma.c struct hugetlb_cma_config { · line 33

Quoted 3 lines
mm/hugetlb_cma.c +3 -0
+static struct hugetlb_cma_pool hugetlb_cma[HUGE_MAX_HSTATE] __ro_after_init;
+static struct hugetlb_cma_config hugetlb_cma_config[HUGE_MAX_HSTATE] __initdata;
+static unsigned int hugetlb_cma_nr __ro_after_init;

the commit message prepares for one more pool, the PMD one, and hugetlb_cma_pool() folds every gigantic order into the default pool. would a fixed pair work instead of an order-keyed array?

Do you mean keeping two fixed pools, like this:

enum {
HUGETLB_CMA_DEFAULT,
HUGETLB_CMA_PMD,
HUGETLB_CMA_POOLS,
};

If so, I agree. That is sufficient for this series and simpler than an order-keyed array.

Thanks,
Qi

#10 lore raw

Re: [PATCH 4/5] mm: hugetlb: avoid recursive CMA allocation during migration

In reply to Jose A. Perez de Azpillaga
Quoted 20 lines · Jose A. Perez de Azpillaga
Quoted 16 lines · Qi Zheng

From: Qi Zheng <[email protected]>

Contiguous allocation can migrate allocated HugeTLB folios or replace free HugeTLB folios while holding a CMA allocation mutex. Allocating a replacement through CMA would recurse into that allocation path once non-gigantic HugeTLB pages can use CMA.

Add a fresh allocation helper that bypasses CMA, and use it for free folio replacement. Pass an explicit allow_cma argument through the migration allocation API and disable it for MR_CONTIG_RANGE. Honor hugetlb_cma_only by failing fresh allocation when the corresponding size requires CMA.

The existing gigantic migration restriction is unchanged.

Assisted-by LLM

[snip]

Quoted 7 lines
Quoted 4 lines

/*
- * Common helper to allocate a fresh hugetlb folio. All specific allocators
- * should use this function to get new hugetlb folio
+ * Common helper to allocate a fresh hugetlb folio.

couldn't allow_cma be passed down to alloc_fresh_hugetlb_folio() instead of adding a second copy of it, so this line can stay?

make sense, will do.

Thanks,
Qi

#13 lore raw

Re: [PATCH 5/5] mm: hugetlb: enable CMA allocation for PMD-sized pages

In reply to Jose A. Perez de Azpillaga
Quoted 25 lines · Jose A. Perez de Azpillaga
Quoted 15 lines · Qi Zheng

From: Qi Zheng <[email protected]>

Allow hugetlb_cma=<capacity>@<page-size> to select the architecture's PMD size in addition to its existing CMA size. PMD pages are 2M on x86-64 and 2M, 32M or 512M on arm64 with 4K, 16K or 64K base pages.

Give PMD pages their own pool without changing the original pool's sharing among other gigantic sizes. On arm64 with 4K base pages, 32M and 1G pages continue to use the default 1G-aligned pool. Unsuffixed arguments and an explicit default size still configure that same pool.

PMD requests use only the PMD pool, even when their order is gigantic. Other gigantic requests use only the original pool. Apply this mapping consistently to allocation, CMA-only policy and boot-time accounting.

Quoted 8 lines

Move CMA allocation into the common fresh HugeTLB allocation path so non-gigantic pages can use CMA. Align reservations to both the selected page size and CMA's minimum alignment, and use a bitmap granularity no larger than the selected order. Keep the original CMA area names and give PMD areas distinct names. Document the syntax and pool selection.

Assisted-by LLM
Quoted 8 lines

[snip]

mm/hugetlb.c static struct folio *only_alloc_fresh_hugetlb_folio(struct hstate *h, · line 1843

Quoted 2 lines
mm/hugetlb.c +2 -0
+	folio = hugetlb_cma_alloc_frozen_folio(order, gfp_mask, nid, nmask);
+	if (!folio) {

boot-time hugepages= goes through here too, so hugepages=128 with hugetlb_cma=512M@2M already uses half of the pool at boot. would buddy first and cma as fallback make more sense for PMD?

That makes sense.

Buddy-first can reduce CMA migration and leave more CMA capacity for later allocations.

I'll use buddy first and CMA as a fallback for non-gigantic PMD pages, while keeping hugetlb_cma_only=1 restricted to CMA and preserving the existing gigantic allocation path.

Quoted 9 lines

[snip]

mm/hugetlb_cma.c static struct hugetlb_cma_pool *hugetlb_cma_pool(unsigned int order) · line 47

Quoted 2 lines
mm/hugetlb_cma.c +1 -0
+	if (order != PMD_SHIFT - PAGE_SHIFT && order_is_gigantic(order))
  		order = hugetlb_cma_default_order;

doesn't this take gigantic PMD-order pages out of the default pool even when no PMD pool is set? would trying the exact order first and then falling back to the default pool work?

Good point. Perhaps we should use the following selector with the two fixed pools:

static struct hugetlb_cma_pool *hugetlb_cma_pool(unsigned int order)

{
	struct hugetlb_cma_pool *pmd_pool = &hugetlb_cma[HUGETLB_CMA_PMD];

	if (order == PMD_SHIFT - PAGE_SHIFT && pmd_pool->size)
		return pmd_pool;

	if (order_is_gigantic(order))
		return &hugetlb_cma[HUGETLB_CMA_DEFAULT];

	return NULL;
}
Quoted 3 lines

I tested it on qemu and saw no issues, but I'm hesitant about the points above. I can give a Tested-by tag on the next version :)

Thanks! I'll send out a v2 as soon as possible.

#17 lore raw
In reply to Muchun Song

Hi Muchun,

Quoted 38 lines · Muchun Song
Quoted 13 lines · Qi Zheng

From: Qi Zheng <[email protected]>

Hi all,

Allocating PMD-sized HugeTLB pages at runtime can become slow or fail as physical memory becomes fragmented. Reserving HugeTLB pages at boot avoids this problem, but keeps unused pages unavailable to ordinary allocations.

Quoted 6 lines

Live upgrades are one example: they need additional HugeTLB pages only during the upgrade. HugeTLB already supports CMA for gigantic pages, but reserving a CMA pool for 1 GiB pages can fail when physical KASLR and other early reservations leave too few suitable 1 GiB-aligned ranges. At runtime, 1 GiB allocations can also fail when pages cannot be migrated out of a suitable contiguous range.

Yes, that's our use case at ByteDance. Allocating a 1GB HugeTLB page from the CMA pool is more prone to failure than allocating a 2MB one. To improve the probability of successful allocation, we typically configure the CMA size to be larger than our actual requirement, keeping a certain margin to handle cases where temporary kernel operations (such as DMA) prevent successful migration. For 1G HugeTLB pages, since the margin must be an integer multiple of 1G, the extra memory configured as headroom is also relatively large, making it less flexible than 2M pages.

Quoted 9 lines

Extend CMA support to PMD-sized pages so their backing memory can be reserved at boot while remaining usable for movable allocations until needed. This relaxes the reservation alignment requirement and reduces the contiguous range required per allocation.

The series adds an optional @<page-size> suffix to hugetlb_cma and a separate PMD-sized pool. For example, on x86-64:

Quoted 6 lines
Quoted 1 line

hugetlb_cma=4G hugetlb_cma=1G@2M

Different sizes of gigantic pages already share a single HugeTLB CMA pool. Given that, why not let 2M pages also reuse this same pool? Would that make the overall code implementation simpler? And if it does make things simpler, should we go ahead and do it first?

Sharing the pool would simplify the implementation. My concern is that small PMD allocations could fragment the default pool and prevent later gigantic allocations.

32M allocations can already cause this, but 2M allocations can block the same number of 1G-aligned ranges with much less allocated memory.

Thanks,
Qi

Quoted 37 lines

Thanks,
Muchun

Quoted 33 lines

This reserves 4G for the existing gigantic-page pool and 1G for the PMD-sized pool. Existing unsuffixed parameters and gigantic-page pool sharing are preserved. The new size follows the architecture's PMD size rather than being fixed at 2M.

The preparatory patches introduce per-pool state and prevent recursive CMA allocation during migration before enabling PMD-sized allocation.

Thanks,
Qi

Qi Zheng (5):
mm: hugetlb: add explicit page size to hugetlb_cma
mm: hugetlb: group CMA pool and reservation state
mm: hugetlb: track CMA reservations by page order
mm: hugetlb: avoid recursive CMA allocation during migration
mm: hugetlb: enable CMA allocation for PMD-sized pages

.../admin-guide/kernel-parameters.txt         |  39 ++-
include/linux/hugetlb.h                       |   4 +-
mm/hugetlb.c                                  |  80 +++--
mm/hugetlb_cma.c                              | 328 ++++++++++++------
mm/hugetlb_cma.h                              |  11 +-
mm/mempolicy.c                                |   2 +-
mm/migrate.c                                  |   3 +-
7 files changed, 306 insertions(+), 161 deletions(-)

base-commit: b810335a0699d1810683796b0e3ffe8827ddfe6c

Signature · 1 line
2.54.0
maintainer
#18 lore raw
Quoted 47 lines · Qi Zheng

Hi Muchun,

Quoted 40 lines · Muchun Song
Quoted 13 lines · Qi Zheng

From: Qi Zheng <[email protected]>

Hi all,

Allocating PMD-sized HugeTLB pages at runtime can become slow or fail as physical memory becomes fragmented. Reserving HugeTLB pages at boot avoids this problem, but keeps unused pages unavailable to ordinary allocations.

Quoted 6 lines

Live upgrades are one example: they need additional HugeTLB pages only during the upgrade. HugeTLB already supports CMA for gigantic pages, but reserving a CMA pool for 1 GiB pages can fail when physical KASLR and other early reservations leave too few suitable 1 GiB-aligned ranges. At runtime, 1 GiB allocations can also fail when pages cannot be migrated out of a suitable contiguous range.

Yes, that's our use case at ByteDance. Allocating a 1GB HugeTLB page from the CMA pool is more prone to failure than allocating a 2MB one. To improve the probability of successful allocation, we typically configure the CMA size to be larger than our actual requirement, keeping a certain margin to handle cases where temporary kernel operations (such as DMA) prevent successful migration. For 1G HugeTLB pages, since the margin must be an integer multiple of 1G, the extra memory configured as headroom is also relatively large, making it less flexible than 2M pages.

Quoted 10 lines

Extend CMA support to PMD-sized pages so their backing memory can be reserved at boot while remaining usable for movable allocations until needed. This relaxes the reservation alignment requirement and reduces the contiguous range required per allocation.

The series adds an optional @<page-size> suffix to hugetlb_cma and a separate PMD-sized pool. For example, on x86-64:

hugetlb_cma=4G hugetlb_cma=1G@2M

Quoted 8 lines
Quoted 4 lines

Different sizes of gigantic pages already share a single HugeTLB CMA pool. Given that, why not let 2M pages also reuse this same pool? Would that make the overall code implementation simpler? And if it does make things simpler, should we go ahead and do it first?

Sharing the pool would simplify the implementation. My concern is that small PMD allocations could fragment the default pool and prevent later gigantic allocations.

Yes. However, the premise is that there has to be a mixed-use scenario. I'm not sure if this kind of mixed usage is a common scenario.

Of course, we don't have much experience to draw on, because on most architectures there is generally only one kind of gigantic page, and prior to this, mixed-use scenarios could not arise.

Therefore, I think that before the need arises, a simpler design is easier to maintain.

Thanks,
Muchun

Quoted 43 lines

32M allocations can already cause this, but 2M allocations can block the same number of 1G-aligned ranges with much less allocated memory.

Thanks,
Qi

Quoted 36 lines

Thanks,
Muchun

Quoted 32 lines

This reserves 4G for the existing gigantic-page pool and 1G for the PMD-sized pool. Existing unsuffixed parameters and gigantic-page pool sharing are preserved. The new size follows the architecture's PMD size rather than being fixed at 2M.

The preparatory patches introduce per-pool state and prevent recursive CMA allocation during migration before enabling PMD-sized allocation.

Thanks,
Qi

Qi Zheng (5):
mm: hugetlb: add explicit page size to hugetlb_cma
mm: hugetlb: group CMA pool and reservation state
mm: hugetlb: track CMA reservations by page order
mm: hugetlb: avoid recursive CMA allocation during migration
mm: hugetlb: enable CMA allocation for PMD-sized pages

.../admin-guide/kernel-parameters.txt         |  39 ++-
include/linux/hugetlb.h                       |   4 +-
mm/hugetlb.c                                  |  80 +++--
mm/hugetlb_cma.c                              | 328 ++++++++++++------
mm/hugetlb_cma.h                              |  11 +-
mm/mempolicy.c                                |   2 +-
mm/migrate.c                                  |   3 +-
7 files changed, 306 insertions(+), 161 deletions(-)

base-commit: b810335a0699d1810683796b0e3ffe8827ddfe6c

Signature · 1 line
2.54.0
#19 lore raw
Quoted 59 lines · Muchun Song
Quoted 47 lines · Qi Zheng

Hi Muchun,

Quoted 40 lines · Muchun Song
Quoted 13 lines · Qi Zheng

From: Qi Zheng <[email protected]>

Hi all,

Allocating PMD-sized HugeTLB pages at runtime can become slow or fail as physical memory becomes fragmented. Reserving HugeTLB pages at boot avoids this problem, but keeps unused pages unavailable to ordinary allocations.

Quoted 6 lines

Live upgrades are one example: they need additional HugeTLB pages only during the upgrade. HugeTLB already supports CMA for gigantic pages, but reserving a CMA pool for 1 GiB pages can fail when physical KASLR and other early reservations leave too few suitable 1 GiB-aligned ranges. At runtime, 1 GiB allocations can also fail when pages cannot be migrated out of a suitable contiguous range.

Yes, that's our use case at ByteDance. Allocating a 1GB HugeTLB page from the CMA pool is more prone to failure than allocating a 2MB one. To improve the probability of successful allocation, we typically configure the CMA size to be larger than our actual requirement, keeping a certain margin to handle cases where temporary kernel operations (such as DMA) prevent successful migration. For 1G HugeTLB pages, since the margin must be an integer multiple of 1G, the extra memory configured as headroom is also relatively large, making it less flexible than 2M pages.

Quoted 10 lines

Extend CMA support to PMD-sized pages so their backing memory can be reserved at boot while remaining usable for movable allocations until needed. This relaxes the reservation alignment requirement and reduces the contiguous range required per allocation.

The series adds an optional @<page-size> suffix to hugetlb_cma and a separate PMD-sized pool. For example, on x86-64:

hugetlb_cma=4G hugetlb_cma=1G@2M

Quoted 8 lines
Quoted 4 lines

Different sizes of gigantic pages already share a single HugeTLB CMA pool. Given that, why not let 2M pages also reuse this same pool? Would that make the overall code implementation simpler? And if it does make things simpler, should we go ahead and do it first?

Sharing the pool would simplify the implementation. My concern is that small PMD allocations could fragment the default pool and prevent later gigantic allocations.

Yes. However, the premise is that there has to be a mixed-use scenario. I'm not sure if this kind of mixed usage is a common scenario.

Quoted 6 lines

Of course, we don't have much experience to draw on, because on most architectures there is generally only one kind of gigantic page, and prior to this, mixed-use scenarios could not arise.

Therefore, I think that before the need arises, a simpler design is easier to maintain.

wouldn't a shared pool still be sized and aligned for 1G? then the 2M headroom stays a multiple of 1G and the 1G-aligned reservation can still fail, which is what the series is trying to avoid.

Signature · 2 lines
cheers,
jose a. p-a
maintainer
#20 lore raw
Quoted 68 lines · Jose A. Perez de Azpillaga
Quoted 63 lines · Muchun Song
Quoted 47 lines · Qi Zheng

Hi Muchun,

Quoted 40 lines · Muchun Song
Quoted 13 lines · Qi Zheng

From: Qi Zheng <[email protected]>

Hi all,

Allocating PMD-sized HugeTLB pages at runtime can become slow or fail as physical memory becomes fragmented. Reserving HugeTLB pages at boot avoids this problem, but keeps unused pages unavailable to ordinary allocations.

Quoted 6 lines

Live upgrades are one example: they need additional HugeTLB pages only during the upgrade. HugeTLB already supports CMA for gigantic pages, but reserving a CMA pool for 1 GiB pages can fail when physical KASLR and other early reservations leave too few suitable 1 GiB-aligned ranges. At runtime, 1 GiB allocations can also fail when pages cannot be migrated out of a suitable contiguous range.

Yes, that's our use case at ByteDance. Allocating a 1GB HugeTLB page from the CMA pool is more prone to failure than allocating a 2MB one. To improve the probability of successful allocation, we typically configure the CMA size to be larger than our actual requirement, keeping a certain margin to handle cases where temporary kernel operations (such as DMA) prevent successful migration. For 1G HugeTLB pages, since the margin must be an integer multiple of 1G, the extra memory configured as headroom is also relatively large, making it less flexible than 2M pages.

Quoted 10 lines

Extend CMA support to PMD-sized pages so their backing memory can be reserved at boot while remaining usable for movable allocations until needed. This relaxes the reservation alignment requirement and reduces the contiguous range required per allocation.

The series adds an optional @<page-size> suffix to hugetlb_cma and a separate PMD-sized pool. For example, on x86-64:

hugetlb_cma=4G hugetlb_cma=1G@2M

Quoted 8 lines
Quoted 4 lines

Different sizes of gigantic pages already share a single HugeTLB CMA pool. Given that, why not let 2M pages also reuse this same pool? Would that make the overall code implementation simpler? And if it does make things simpler, should we go ahead and do it first?

Sharing the pool would simplify the implementation. My concern is that small PMD allocations could fragment the default pool and prevent later gigantic allocations.

Yes. However, the premise is that there has to be a mixed-use scenario. I'm not sure if this kind of mixed usage is a common scenario.

Of course, we don't have much experience to draw on, because on most architectures there is generally only one kind of gigantic page, and prior to this, mixed-use scenarios could not arise.

Quoted 6 lines
Quoted 2 lines

Therefore, I think that before the need arises, a simpler design is easier to maintain.

wouldn't a shared pool still be sized and aligned for 1G? then the 2M headroom stays a multiple of 1G and the 1G-aligned reservation can still fail, which is what the series is trying to avoid.

If we decide to use a shared pool, I think the current alignment strategy should be adapted as well, it should be changed to align with the minimal acquirement, like 2M.

For example, if a user reserves 1G+2M through hugetlb_cma, then the memory reserved by the pool is also 1G+2M, except that the starting address needs to be aligned to the largest gigantic page (1G). Therefore, this pool is suitable for both 1GB and 2MB allocation. The only problem I see with a shared pool is that it may not support mixed-use scenarios very well.

What do you think?

Thanks,
Muchun

Quoted 4 lines
Signature · 3 lines
-- 
cheers,
jose a. p-a
#21 lore raw
Quoted 79 lines · Muchun Song
Quoted 68 lines · Jose A. Perez de Azpillaga
Quoted 63 lines · Muchun Song
Quoted 47 lines · Qi Zheng

Hi Muchun,

Quoted 40 lines · Muchun Song
Quoted 13 lines · Qi Zheng

From: Qi Zheng <[email protected]>

Hi all,

Allocating PMD-sized HugeTLB pages at runtime can become slow or fail as physical memory becomes fragmented. Reserving HugeTLB pages at boot avoids this problem, but keeps unused pages unavailable to ordinary allocations.

Quoted 6 lines

Live upgrades are one example: they need additional HugeTLB pages only during the upgrade. HugeTLB already supports CMA for gigantic pages, but reserving a CMA pool for 1 GiB pages can fail when physical KASLR and other early reservations leave too few suitable 1 GiB-aligned ranges. At runtime, 1 GiB allocations can also fail when pages cannot be migrated out of a suitable contiguous range.

Yes, that's our use case at ByteDance. Allocating a 1GB HugeTLB page from the CMA pool is more prone to failure than allocating a 2MB one. To improve the probability of successful allocation, we typically configure the CMA size to be larger than our actual requirement, keeping a certain margin to handle cases where temporary kernel operations (such as DMA) prevent successful migration. For 1G HugeTLB pages, since the margin must be an integer multiple of 1G, the extra memory configured as headroom is also relatively large, making it less flexible than 2M pages.

Quoted 10 lines

Extend CMA support to PMD-sized pages so their backing memory can be reserved at boot while remaining usable for movable allocations until needed. This relaxes the reservation alignment requirement and reduces the contiguous range required per allocation.

The series adds an optional @<page-size> suffix to hugetlb_cma and a separate PMD-sized pool. For example, on x86-64:

hugetlb_cma=4G hugetlb_cma=1G@2M

Quoted 8 lines
Quoted 4 lines

Different sizes of gigantic pages already share a single HugeTLB CMA pool. Given that, why not let 2M pages also reuse this same pool? Would that make the overall code implementation simpler? And if it does make things simpler, should we go ahead and do it first?

Sharing the pool would simplify the implementation. My concern is that small PMD allocations could fragment the default pool and prevent later gigantic allocations.

Yes. However, the premise is that there has to be a mixed-use scenario. I'm not sure if this kind of mixed usage is a common scenario.

Of course, we don't have much experience to draw on, because on most architectures there is generally only one kind of gigantic page, and prior to this, mixed-use scenarios could not arise.

Quoted 6 lines
Quoted 2 lines

Therefore, I think that before the need arises, a simpler design is easier to maintain.

wouldn't a shared pool still be sized and aligned for 1G? then the 2M headroom stays a multiple of 1G and the 1G-aligned reservation can still fail, which is what the series is trying to avoid.

If we decide to use a shared pool, I think the current alignment strategy should be adapted as well, it should be changed to align with the minimal acquirement, like 2M.

Quoted 7 lines

For example, if a user reserves 1G+2M through hugetlb_cma, then the memory reserved by the pool is also 1G+2M, except that the starting address needs to be aligned to the largest gigantic page (1G). Therefore, this pool is suitable for both 1GB and 2MB allocation. The only problem I see with a shared pool is that it may not support mixed-use scenarios very well.

What do you think?

makes sense to me. existing hugetlb_cma= users with WM pages would get mixed use without asking for it though.

Signature · 2 lines
cheers,
jose a. p-a
#22 lore raw
Quoted 19 lines · Jose A. Perez de Azpillaga
Quoted 18 lines · Muchun Song
Quoted 7 lines

wouldn't a shared pool still be sized and aligned for 1G? then the 2M headroom stays a multiple of 1G and the 1G-aligned reservation can still fail, which is what the series is trying to avoid.

If we decide to use a shared pool, I think the current alignment strategy should be adapted as well, it should be changed to align with the minimal acquirement, like 2M.

For example, if a user reserves 1G+2M through hugetlb_cma, then the memory reserved by the pool is also 1G+2M, except that the starting address needs to be aligned to the largest gigantic page (1G). Therefore, this pool is suitable for both 1GB and 2MB allocation. The only problem I see with a shared pool is that it may not support mixed-use scenarios very well.

Quoted 4 lines
Quoted 1 line

What do you think?

makes sense to me. existing hugetlb_cma= users with WM pages would get mixed use without asking for it though.

Yeah, my intuition tells me that having 1 allocation size per CMA area might be cleaner.

Assume you allocate 512 2 MiB hugetlb folios. You'd think that you would then be able to allocate 1 GiB hugetlb folio less. But that's not guaranteed, as CMA might just fallback to other areas fairly easily.

If someone wants to convert from 1 GiB to 512 x 2MiB, I think the demotion interface allows for that.

Signature · 3 lines
Cheers,

David
#23 lore raw
Quoted 20 lines · David Hildenbrand (Arm)
Quoted 19 lines · Jose A. Perez de Azpillaga
Quoted 18 lines · Muchun Song
Quoted 7 lines

wouldn't a shared pool still be sized and aligned for 1G? then the 2M headroom stays a multiple of 1G and the 1G-aligned reservation can still fail, which is what the series is trying to avoid.

If we decide to use a shared pool, I think the current alignment strategy should be adapted as well, it should be changed to align with the minimal acquirement, like 2M.

For example, if a user reserves 1G+2M through hugetlb_cma, then the memory reserved by the pool is also 1G+2M, except that the starting address needs to be aligned to the largest gigantic page (1G). Therefore, this pool is suitable for both 1GB and 2MB allocation. The only problem I see with a shared pool is that it may not support mixed-use scenarios very well.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

What do you think?

makes sense to me. existing hugetlb_cma= users with WM pages would get mixed use without asking for it though.

Yeah, my intuition tells me that having 1 allocation size per CMA area might be cleaner.

Agree.

Quoted 7 lines

Assume you allocate 512 2 MiB hugetlb folios. You'd think that you would then be able to allocate 1 GiB hugetlb folio less. But that's not guaranteed, as CMA might just fallback to other areas fairly easily.

If someone wants to convert from 1 GiB to 512 x 2MiB, I think the demotion interface allows for that.

For this series, I'd like to use a separate CMA pool for the newly supported PMD-sized pages, while keeping the existing sharing among gigantic sizes unchanged.

If there are no objections, I'll update v2.

Thanks,
Qi

#14 lore raw

Re: [PATCH 5/5] mm: hugetlb: enable CMA allocation for PMD-sized pages

In reply to Qi Zheng
Quoted 1 line · Qi Zheng

From: Qi Zheng <[email protected]>

[...]

Quoted 26 lines
mm/hugetlb.c +2 -12
index 218fbaee6f7e5..8c00fd9d0658d 100644
@@ -1362,18 +1362,8 @@ static struct folio *dequeue_hugetlb_folio(struct hstate *h, gfp_t gfp_mask,
 static struct folio *alloc_gigantic_frozen_folio(int order, gfp_t gfp_mask,
 		int nid, nodemask_t *nodemask)
 {
-	struct folio *folio;
-
-	folio = hugetlb_cma_alloc_frozen_folio(order, gfp_mask, nid, nodemask);
-	if (folio)
-		return folio;
-
-	if (hugetlb_cma_exclusive_alloc(order))
-		return NULL;
-
-	folio = (struct folio *)alloc_contig_frozen_pages(1 << order, gfp_mask,
-							  nid, nodemask);
-	return folio;
+	return (struct folio *)alloc_contig_frozen_pages(1 << order, gfp_mask,
+							 nid, nodemask);
 }
 #else /* !CONFIG_ARCH_HAS_GIGANTIC_PAGE || !CONFIG_CONTIG_ALLOC */
 static struct folio *alloc_gigantic_frozen_folio(int order, gfp_t gfp_mask, int nid,

mm/hugetlb.c static struct folio *only_alloc_fresh_hugetlb_folio(struct hstate *h, · line 1843

Quoted 10 lines
mm/hugetlb.c +2 -5
@@ -1849,11 +1839,18 @@ static struct folio *only_alloc_fresh_hugetlb_folio(struct hstate *h,
 	if (nid == NUMA_NO_NODE)
 		nid = numa_mem_id();
 
-	if (order_is_gigantic(order))
-		folio = alloc_gigantic_frozen_folio(order, gfp_mask, nid, nmask);
-	else
-		folio = alloc_buddy_frozen_folio(order, gfp_mask, nid, nmask,
-						 node_alloc_noretry);
+	folio = hugetlb_cma_alloc_frozen_folio(order, gfp_mask, nid, nmask);
+	if (!folio) {

Emm ... what about PF_MEMALLOC_PIN here?

If a FOLL_LONGTERM migration out of CMA needs a fresh hugetlb folio, this can allocate another CMA folio as the target.

With HVO disabled, the old folio is freed back to CMA and can be picked again again again.

I guess we'd keep migrating between CMA folios without ever completing the pin ... even if pinnable buddy memory is available, no?

[...]

Cheers, Lance

author
#15 lore raw

Re: [PATCH 5/5] mm: hugetlb: enable CMA allocation for PMD-sized pages

Hi Lance,

Quoted 43 lines · Lance Yang
Quoted 1 line · Qi Zheng

From: Qi Zheng <[email protected]>

[...]

Quoted 26 lines
mm/hugetlb.c +2 -12
index 218fbaee6f7e5..8c00fd9d0658d 100644
@@ -1362,18 +1362,8 @@ static struct folio *dequeue_hugetlb_folio(struct hstate *h, gfp_t gfp_mask,
 static struct folio *alloc_gigantic_frozen_folio(int order, gfp_t gfp_mask,
 		int nid, nodemask_t *nodemask)
 {
-	struct folio *folio;
-
-	folio = hugetlb_cma_alloc_frozen_folio(order, gfp_mask, nid, nodemask);
-	if (folio)
-		return folio;
-
-	if (hugetlb_cma_exclusive_alloc(order))
-		return NULL;
-
-	folio = (struct folio *)alloc_contig_frozen_pages(1 << order, gfp_mask,
-							  nid, nodemask);
-	return folio;
+	return (struct folio *)alloc_contig_frozen_pages(1 << order, gfp_mask,
+							 nid, nodemask);
 }
 #else /* !CONFIG_ARCH_HAS_GIGANTIC_PAGE || !CONFIG_CONTIG_ALLOC */
 static struct folio *alloc_gigantic_frozen_folio(int order, gfp_t gfp_mask, int nid,

mm/hugetlb.c static struct folio *only_alloc_fresh_hugetlb_folio(struct hstate *h, · line 1843

Quoted 10 lines
mm/hugetlb.c +2 -5
@@ -1849,11 +1839,18 @@ static struct folio *only_alloc_fresh_hugetlb_folio(struct hstate *h,
 	if (nid == NUMA_NO_NODE)
 		nid = numa_mem_id();
 
-	if (order_is_gigantic(order))
-		folio = alloc_gigantic_frozen_folio(order, gfp_mask, nid, nmask);
-	else
-		folio = alloc_buddy_frozen_folio(order, gfp_mask, nid, nmask,
-						 node_alloc_noretry);
+	folio = hugetlb_cma_alloc_frozen_folio(order, gfp_mask, nid, nmask);
+	if (!folio) {

Emm ... what about PF_MEMALLOC_PIN here?

Quoted 8 lines

If a FOLL_LONGTERM migration out of CMA needs a fresh hugetlb folio, this can allocate another CMA folio as the target.

With HVO disabled, the old folio is freed back to CMA and can be picked again again again.

I guess we'd keep migrating between CMA folios without ever completing the pin ... even if pinnable buddy memory is available, no?

Yes, you are right. A fresh migration target can come from CMA again, causing repeated migration without completing the pin, even when pinnable buddy memory is available.

The same missing PF_MEMALLOC_PIN check also affects 1 GiB CMA allocations, so I had planned to fix it separately.

Thanks,
Qi

Quoted 4 lines

[...]

Cheers, Lance