From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f70.google.com (mail-pj1-f70.google.com [209.85.216.70]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2817E420471 for ; Thu, 6 Aug 2026 08:56:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.70 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786006615; cv=none; b=bqggzcM7Sg6kK3YluVryEwEYnbEE1nmUGmzfHs984L5tBUT8dTd7Qqd0tkv3xPtK+M3OXap4qziykv9Pe+/MCZ6Yffeyowrb+mDkhyU1/KnSo5yAggyMbTs1udO2QSpCAHpp+SX6flUKx9bGHe4Ibw/qqlEjH4J6L8e5mKcr9Yg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786006615; c=relaxed/simple; bh=lzAzv3D8bvXFQ6MiLXlIRVMWk3AbZIynINEVVyyvY/w=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=FfbZKI5bRKCi1opemLilbAolffPRA5rFPzghUwKnxEIxpJgl46vZRJc4eFawVzCu+XssMyHzTOXMwBlq4d6rKWhEXLUFaR0ltCiGSD1peTZ0XwmmOTd7vi1cjxPUPqwx4iff4YlSkCISlP5t8GzBqt0Qs1fb1seF14UZZ44LYhA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--souravpanda.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=khG1X4h+; arc=none smtp.client-ip=209.85.216.70 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--souravpanda.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="khG1X4h+" Received: by mail-pj1-f70.google.com with SMTP id 98e67ed59e1d1-38f5ac7354dso2846496a91.1 for ; Thu, 06 Aug 2026 01:56:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786006613; x=1786611413; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:mime-version:date:from :to:cc:subject:date:message-id:reply-to:content-type; bh=f2Kd5S4Tu7Fwb1K9owR83WOTCIf8JouRZAzl2QC9oZc=; b=khG1X4h+ClP3uBbp+b6ZoIzExY+dQ7fRQoyq4jj2pWQA1YsWjmwB66qQEq9IAjc0h9 h0ZvOdn9Barn+uKcOg7MMO2kzcILX215PTzQyvCR9SOmHIYJ7xZRTeyLDHDTEmTYiDGn wO1FEiMA5yk3YDAFB15in91JFs6GsVOGMOFgJPTeIkXl52aRxlKP+oL8HodWrZsl5iFs 5NM0gqxlV8N6ZWH5+8Ulf+qOldyLLlKa7FlxP37WPhTffoWffIsfYgoUszQq9VOTTfgj L4rcD+RzTJSVw1/KflPLS18Ul43mrGebJenAkYVKEN2thuCsYvt81SUQdJB6/wqHhPqu YNSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786006613; x=1786611413; h=content-type:cc:to:from:subject:message-id:mime-version:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=f2Kd5S4Tu7Fwb1K9owR83WOTCIf8JouRZAzl2QC9oZc=; b=Bs23ZSd/23TFctvYzOAZ5ENDGpzCA7MWT8r39TjTJrRvNobiMrVwqlVFzqKSu8k7GC MK8ltqi8uOolnDm5dmS11CZxZH+etuLO9ljpCroNuU2Gd/QTjflM79cojwJgac9cWGnG 4IX47ItbL+bfV2SIVi77dW9LfswOx1I0YG9E7HFiKJevLyPPE3mQ2tfgBQndkDAfju1T ZLYJ0dzA/olGuQelOEDawxeBzJPtTOtp9w9Yma1DnutbrgQx9lpiiUZDDWFWEE8Gic6N Lj1K3BkG1p4xlrGZ13atIt8yNMLVnOOpXKwBs6eejywLjuNgKDO0wlKtZjD8+uWy0ZqF dPCQ== X-Forwarded-Encrypted: i=1; AHgh+RowLwPUOBDjLa6VeogUE7XgfC48bG/vZtWKg7fdzAa9C6KkHhUD7XX1PpGNN/HisPgVqYxQhl2STHfuFqE=@vger.kernel.org X-Gm-Message-State: AOJu0YwHDox24OJwczJwTT0aBtdh0sz8343h5pqlsxvPI5PJbprRXUJd kbNfjxfKmFp9JzyvNZgRyTXI+oMCWSWPU27RogcdSUgNlssZ91wTFELaRjkzFUNi2DvraLuchkT NYcYHI3q2qdR55/m4xrW75qk2Zg== X-Received: from pjyj17.prod.google.com ([2002:a17:90a:e611:b0:38e:3cd6:1866]) (user=souravpanda job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:53c5:b0:381:bc4c:da5b with SMTP id 98e67ed59e1d1-3903c61098cmr14798023a91.18.1786006612810; Thu, 06 Aug 2026 01:56:52 -0700 (PDT) Date: Thu, 6 Aug 2026 08:56:51 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.55.0.629.g250fe7f194-goog Message-ID: <20260806085651.4068433-1-souravpanda@google.com> Subject: [PATCH v4] mm/hugetlb_cma: support percentage-based hugetlb_cma reservation From: Sourav Panda To: muchun.song@linux.dev, osalvador@suse.de, akpm@linux-foundation.org Cc: david@kernel.org, usama.arif@linux.dev, surenb@google.com, fvdl@google.com, gthelen@google.com, rientjes@google.com, souravpanda@google.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" 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. Signed-off-by: Sourav Panda --- Link: https://lore.kernel.org/linux-mm/20260729163148.3271755-1-souravpanda@google.com/ [v3] Link: https://lore.kernel.org/linux-mm/20260628190155.3655895-1-souravpanda@google.com/ [v2] Link: https://lore.kernel.org/linux-mm/20260625215900.2151690-1-souravpanda@google.com/ [v1] v4: - Clear global hugetlb_cma_percent and hugetlb_cma_size when node-specific parameters are parsed so the last command line configuration wins (Usama Arif). - Warn when ALIGN_DOWN rounds down a non-zero percentage-derived reservation size to 0 MiB (Usama Arif). v3: - Enhance pr_info logs to explicitly show percentage-derived sizes per Andrew Morton"s suggestions. - Explicitly align percentage-derived sizes down to the gigantic page size to prevent allocation failures (Usama Arif). .../admin-guide/kernel-parameters.txt | 7 +- mm/hugetlb_cma.c | 141 +++++++++++++++++- 2 files changed, 138 insertions(+), 10 deletions(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt index 5a05b48d1684..846940ce2858 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -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) - :nn[KMGTPE][,: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) + :nn[KMGTPE][,:nn[KMGTPE]] or + :nn%[,:nn%] Reserve a CMA area of given size and allocate gigantic hugepages using the CMA allocator. If enabled, the diff --git a/mm/hugetlb_cma.c b/mm/hugetlb_cma.c index 7693ccefd0c6..2ca014a0a3b8 100644 --- a/mm/hugetlb_cma.c +++ b/mm/hugetlb_cma.c @@ -9,6 +9,9 @@ #include #include +#include +#include +#include #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,31 @@ 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); + hugetlb_cma_size = 0; + hugetlb_cma_percent = 0; + 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 +160,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 +207,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; + } + 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; @@ -163,6 +254,32 @@ void __init hugetlb_cma_reserve(void) */ VM_WARN_ON(order <= MAX_PAGE_ORDER); + if (hugetlb_cma_percent) { + unsigned long orig_size = hugetlb_cma_size; + + hugetlb_cma_size = ALIGN_DOWN(hugetlb_cma_size, PAGE_SIZE << order); + if (orig_size && !hugetlb_cma_size) + pr_warn("hugetlb_cma: reservation size rounded down to 0 from %lu MiB (%u%%)\n", + orig_size / SZ_1M, hugetlb_cma_percent); + } else if (has_node_specific_param) { + hugetlb_cma_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]; + + hugetlb_cma_size_in_node[nid] = + ALIGN_DOWN(hugetlb_cma_size_in_node[nid], + PAGE_SIZE << order); + if (orig_size && !hugetlb_cma_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], + nid); + } + hugetlb_cma_size += hugetlb_cma_size_in_node[nid]; + } + } + hugetlb_bootmem_set_nodes(); for (nid = 0; nid < MAX_NUMNODES; nid++) { @@ -205,8 +322,12 @@ void __init hugetlb_cma_reserve(void) per_node = DIV_ROUND_UP(hugetlb_cma_size, nodes_weight(hugetlb_bootmem_nodes)); per_node = round_up(per_node, PAGE_SIZE << order); - pr_info("hugetlb_cma: reserve %lu MiB, up to %lu MiB per node\n", - hugetlb_cma_size / SZ_1M, per_node / SZ_1M); + if (hugetlb_cma_percent) + pr_info("hugetlb_cma: reserve %lu MiB (%u%%), up to %lu MiB per node\n", + hugetlb_cma_size / SZ_1M, hugetlb_cma_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); } reserved = 0; @@ -241,8 +362,12 @@ void __init hugetlb_cma_reserve(void) } reserved += size; - pr_info("hugetlb_cma: reserved %lu MiB on node %d\n", - size / SZ_1M, nid); + if (hugetlb_cma_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); + else + pr_info("hugetlb_cma: reserved %lu MiB on node %d\n", + size / SZ_1M, nid); if (reserved >= hugetlb_cma_size) break; -- 2.55.0.571.g244d577d93-goog