From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (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 B09383B1B3 for ; Sun, 28 Jun 2026 19:01:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782673319; cv=none; b=e2AfNBzY6OfM2mxqVW10eofibr7ufdOOE0sy3uPhxbpLx79qk9Y+VudbznCMHHWdw8HXh4kIZaJZ89UmgrAPKD2hboAH6zBwcDZK+KDFXWMZbG9+DP6r1y7tDKjNcFVNRuGQFlLBCdvK6iRMG4lVhXLjhZcNxXwfUOVMX1mcUkE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782673319; c=relaxed/simple; bh=FWw+CbRCqBoVmoUzwngCawN24Ofn/AxPrf/0Afx9jHc=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=rU+7G+hIud5gczCcOn44l4eCFjcUE3bxG92rxbQfHMjxox0cVzG3iEhtAr19MbJL8wKZKFkcaEjB5l2jEfe+skuXPoZE2UpY2Sxv4OkkPt1y4LDCiOzfj8Qs5RV9BpdLMvUSQPxJMo+QbA1F6xZtaPjNiK/DZMiuv+pngg5c01k= 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=n0VuzMV1; arc=none smtp.client-ip=209.85.215.201 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="n0VuzMV1" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-c9151bf6ce7so2366504a12.3 for ; Sun, 28 Jun 2026 12:01:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1782673317; x=1783278117; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=fHT/VH/FCGpteAy70lN8IuW/FU0GRl1Lj7eDs34tWTw=; b=n0VuzMV1Jsa/v1DDS92CdbYo8BBAxBjO2k03ESHsTov9WgI/v//o8FNYjxTSatAW7G iow95yOqGZ1HljVwxiIIwHubE1G1s8W0U7PwF2YgBbMqB9RkdR501T7iHG6f0rAbFGC7 ZoozbR40AaHCcWsfMsx+lM2IyaWw5uwr5y+o88kZf2ENOSdTO3hgBTgx9EQkKh4JXMkM pXVL4TENDcqI0G6btIw3miqXMgxajGrCt+YrF8ejyUbw82pB7ztAR5MweoJHf6vjNyZ7 X3Y/13EQWmOcfcCUVzdzwrL3Q6jgUIvo8ZIYMvxUL9JKn4HQDTUBxnglyv4D5S+nfSqp M4Rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782673317; x=1783278117; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=fHT/VH/FCGpteAy70lN8IuW/FU0GRl1Lj7eDs34tWTw=; b=pTaXzEO3UruHu/ZrCtRJ1J5hF6Z63YeaNyYHNz4JhDXIAWO94el4wRV84OOf27BMHT aH1aNUOjwIFFlC/2EQef2PoMA9ipQci+vV7wHpP82RTFtL0oYoyJRCP05FznyXcEZage jy0KrejAjSGYwEGAB/5lJSRz6Kv7XBzhoWiowgoTshA9TLb9j95RKbB8f4I9PurayYjy k5/CQ1E5HhSR0DhHWfjombWX79yCz/KlOteBK4ggF35AtP2Bl3Vdriuis7a7wUdMnU6P gHWJA+3mJy+/Q4ZdnXqRaGH7Pv8f0BzwvB12nm8mI319Ja8SGkBQYXfkrmxtD19860xV LtTg== X-Forwarded-Encrypted: i=1; AHgh+RpsqjHwK8gdPXzz9hQ3Zzldq+kkIo3WNBCJo3jv9jNC144DnPVlr7cRabR3ZMhnrFzv5M2XpCkUCxt0iMM=@vger.kernel.org X-Gm-Message-State: AOJu0Yxz9oy09mkufoSNXicYo40jVEdmZeSvDZbGQxTDiibJ5FC/6JzF 2RckSphGcJsTjTe75380VsDr560x29FwDwuqcjBtTNrbIbii2WkNdGgTPK6+gsZWap2qP2+J5jd RWDLqpnpBYAj8d+WwfHj0/pDNeA== X-Received: from pfbcj24.prod.google.com ([2002:a05:6a00:2998:b0:845:c378:7950]) (user=souravpanda job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:6c8e:b0:846:2f5b:c00 with SMTP id d2e1a72fcca58-8462f5b0d5fmr2493452b3a.54.1782673316699; Sun, 28 Jun 2026 12:01:56 -0700 (PDT) Date: Sun, 28 Jun 2026 19:01:55 +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.rc0.799.gd6f94ed593-goog Message-ID: <20260628190155.3655895-1-souravpanda@google.com> Subject: [PATCH v2] 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, 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 Reported-by: kernel test robot Closes: https://lore.kernel.org/oe-kbuild-all/202606262023.IKUrn01I-lkp@intel.com/ --- Link: https://lore.kernel.org/linux-mm/20260625215900.2151690-1-souravpanda@google.com/ [v1] 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(-) 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..684ffa3537d2 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,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; + } + 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; -- 2.55.0.rc0.799.gd6f94ed593-goog