From nobody Tue Sep 29 14:53:52 2026 Received: from mail-pj1-f72.google.com (mail-pj1-f72.google.com [209.85.216.72]) (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 104243A1A56 for ; Fri, 7 Aug 2026 04:00:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.72 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786075207; cv=none; b=B01gPrM/8XPX1Ci14OFc3TzXuk8Ag2bQfkClb45ompLL3SMSxVMhC5jksZueagwu6FVXSnhzsaGDaTVn0WyTnxP+95Bn8xPl3/c6KbslR0/wzncY+TTzzAlZ3NXlhUJGoCQlirWykWG2gsE9/jq4yeyPdSEpM2wRBGErDiDzK5Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786075207; c=relaxed/simple; bh=1GVSJ5+PTW0POb8DbU/mALtDAqrFM6TJ1rUbhtC0lS4=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=Py+8bu2rqahGtOaQ0NcBo6bMp5ReaYJpoXgJStvnV6sx8xuZUA23cMXQNsS63b1Zb7oxknEXOHbMp0LtKqDBpqc/mmJREpcgT+hBtk/TLmH43mVtThHwIjuZU2rpSYP5grj4NWfKUgyRgXTjZp+ZzBAxP9C5kFu+1dkrtgc/55U= 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=G6DyLMtT; arc=none smtp.client-ip=209.85.216.72 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="G6DyLMtT" Received: by mail-pj1-f72.google.com with SMTP id 98e67ed59e1d1-388b404eaa4so3778318a91.0 for ; Thu, 06 Aug 2026 21:00:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786075205; x=1786680005; 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=vtTQetELCW0NUYuImjfKHiXf/lqR62/Atju4/hLtEqA=; b=G6DyLMtTrxQe0JPi57POdcCsSn6VW6bNMSMhcNENI4prEtkfVER8+i34QObyNRaMEc 2S+Es8fAwurN2byl4ZmN//7bUvSoCvy3ZBA2QEuSsuiMHNpSKvB/3I405i9wB49/Qaz7 HE5hFBzkRNlzrtqPOEUXRhgQryybr6Itsn22xw3IrptC0LdnrqPrln7AD7aFn9PqB4yx /xvq9nzAvXedms6bHDdK3U+/r6amFERTfJMzGRoP5N/oeY3duoi4RzRxBV9Lj1FxktY4 toa0jnMeuXLKQl4Lxp1fgwcRZgw9bLwqBjJtQRWrFn+IpSqg+k5f0BO9nC4JGNXLjSJ5 vtOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786075205; x=1786680005; 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=vtTQetELCW0NUYuImjfKHiXf/lqR62/Atju4/hLtEqA=; b=kIcXcKgoKqyyNnro4feWFvusZmIymO+65D99aKfaZ1HGPQd+xrOg5DLtYyjXrtQ0bR u3KSTrmTvnA9p11q8iSpk6GgQGwF0hMwo6LccmX1Nv8QoI3LTgCSMztfr2epjUBJDCzc /PuxYrZMNDFV+Fcehka9Gf8GQ5dR9MNImTck1fqULsfu6pWoJCYk3Jb78DcPS/VHB6/A d5ljvjF5rw1GQjq15PS7327Uw1mKq3R0z9ctUnt5fGJYtvV0NDBFI2lf0vJA/j7YpE8+ GvJ4n4ysZoGuulELh7zoXaL2uyH6lUrahqOo5TF6v6VBPg/21R5P7q5GggdjE4uqEhRQ eNPw== X-Forwarded-Encrypted: i=1; AHgh+RpssQ8A7kYeGfCdFkosq5RGonAyMo1Ma4TEa6uqhSoFaB9xxX16uvxL45i0VX887oZND3On4zWgVrXegBY=@vger.kernel.org X-Gm-Message-State: AOJu0Yy/s7EihBGPN96/YLtxHv/THLhgw+mqa9o81W57mmISaoc0fwGF tBA1CUVhNDHycDGbAWoHeZphn4symf5n6Dm4l61a5D7RdQ0PkBThDVzTb6dL+0E9uUPiZPy9lOe k6wEi8n4soTLrVousKamW9mSu+A== X-Received: from pjxx4.prod.google.com ([2002:a17:90b:58c4:b0:37d:f041:9678]) (user=souravpanda job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:d64f:b0:38f:caa2:5721 with SMTP id 98e67ed59e1d1-3903c5dc500mr20296068a91.17.1786075204667; Thu, 06 Aug 2026 21:00:04 -0700 (PDT) Date: Fri, 7 Aug 2026 04:00:03 +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.679.g6767b8d81c-goog Message-ID: <20260807040003.2156630-1-souravpanda@google.com> Subject: [PATCH v5] 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently, hugetlb_cma reservation only supports absolute sizes (e.g., hugetlb_cma=3D2G or hugetlb_cma=3D0: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=3D20% or hugetlb_cma=3D0: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 Acked-by: Usama Arif --- Link: https://lore.kernel.org/linux-mm/20260806085651.4068433-1-souravpanda= @google.com/ [v4] 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] v5: - Document in kernel-parameters.txt that percentage-derived sizes are round= ed down to a multiple of the gigantic hugepage size and may become zero (Us= ama Arif). - Add Acked-by: Usama Arif . v4: - Clear global hugetlb_cma_percent and hugetlb_cma_size when node-specific = parameters are parsed so the last command line configuration wins (Usama Ar= if). - Warn when ALIGN_DOWN rounds down a non-zero percentage-derived reservatio= n size to 0 MiB (Usama Arif). v3: - Enhance pr_info logs to explicitly show percentage-derived sizes per Andr= ew 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 | 10 +- mm/hugetlb_cma.c | 141 +++++++++++++++++- 2 files changed, 141 insertions(+), 10 deletions(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentatio= n/admin-guide/kernel-parameters.txt index 5a05b48d1684..160c414e0f30 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -2064,8 +2064,14 @@ Kernel parameters hugetlb_cma=3D [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%). + 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) + :nn[KMGTPE][,:nn[KMGTPE]] or + :nn%[,:nn%] =20 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 =20 #include +#include +#include +#include #include "internal.h" #include "hugetlb_cma.h" =20 @@ -18,6 +21,28 @@ static unsigned long hugetlb_cma_size_in_node[MAX_NUMNOD= ES] __initdata; static bool hugetlb_cma_only __ro_after_init; static unsigned long hugetlb_cma_size __ro_after_init; =20 +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 =3D 0; + + for_each_mem_region(reg) { + if (reg->nid =3D=3D nid) + size +=3D 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; =20 if (s[count] =3D=3D ':') { + char *next; + if (tmp >=3D MAX_NUMNODES) break; nid =3D array_index_nospec(tmp, MAX_NUMNODES); =20 + hugetlb_cma_size =3D 0; + hugetlb_cma_percent =3D 0; + s +=3D count + 1; - tmp =3D memparse(s, &s); - hugetlb_cma_size_in_node[nid] =3D tmp; - hugetlb_cma_size +=3D tmp; + tmp =3D memparse(s, &next); + if (*next =3D=3D '%') { + if (tmp > 100) { + pr_warn("hugetlb_cma: invalid percentage %lu for node %d\n", + tmp, nid); + break; + } + hugetlb_cma_percent_in_node[nid] =3D tmp; + hugetlb_cma_size_in_node[nid] =3D 0; + s =3D next + 1; + } else { + hugetlb_cma_size_in_node[nid] =3D tmp; + hugetlb_cma_percent_in_node[nid] =3D 0; + s =3D next; + } =20 /* * 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 =3D memparse(p, &p); + char *next; + + tmp =3D memparse(p, &next); + if (*next =3D=3D '%') { + if (tmp > 100) { + pr_warn("hugetlb_cma: invalid percentage %lu\n", tmp); + } else { + hugetlb_cma_percent =3D tmp; + hugetlb_cma_size =3D 0; + for (nid =3D 0; nid < MAX_NUMNODES; nid++) { + hugetlb_cma_size_in_node[nid] =3D 0; + hugetlb_cma_percent_in_node[nid] =3D 0; + } + } + } else { + hugetlb_cma_size =3D tmp; + hugetlb_cma_percent =3D 0; + for (nid =3D 0; nid < MAX_NUMNODES; nid++) { + hugetlb_cma_size_in_node[nid] =3D 0; + hugetlb_cma_percent_in_node[nid] =3D 0; + } + } break; } } @@ -144,8 +207,36 @@ void __init hugetlb_cma_reserve(void) { unsigned long size, reserved, per_node, order; bool node_specific_cma_alloc =3D false; + bool has_node_specific_param =3D false; int nid; =20 + for (nid =3D 0; nid < MAX_NUMNODES; nid++) { + if (hugetlb_cma_size_in_node[nid] || hugetlb_cma_percent_in_node[nid]) { + has_node_specific_param =3D true; + break; + } + } + + if (has_node_specific_param) { + hugetlb_cma_size =3D 0; + for (nid =3D 0; nid < MAX_NUMNODES; nid++) { + if (hugetlb_cma_percent_in_node[nid]) { + phys_addr_t node_gfp_mem =3D memblock_node_memory_size(nid); + u64 s; + + s =3D mul_u64_u32_div((u64)node_gfp_mem, + hugetlb_cma_percent_in_node[nid], + 100); + + hugetlb_cma_size_in_node[nid] =3D s; + } + hugetlb_cma_size +=3D hugetlb_cma_size_in_node[nid]; + } + } else if (hugetlb_cma_percent) { + hugetlb_cma_size =3D mul_u64_u32_div((u64)memblock_phys_mem_size(), + hugetlb_cma_percent, 100); + } + if (!hugetlb_cma_size) return; =20 @@ -163,6 +254,32 @@ void __init hugetlb_cma_reserve(void) */ VM_WARN_ON(order <=3D MAX_PAGE_ORDER); =20 + if (hugetlb_cma_percent) { + unsigned long orig_size =3D hugetlb_cma_size; + + hugetlb_cma_size =3D 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 =3D 0; + for (nid =3D 0; nid < MAX_NUMNODES; nid++) { + if (hugetlb_cma_percent_in_node[nid]) { + unsigned long orig_size =3D hugetlb_cma_size_in_node[nid]; + + hugetlb_cma_size_in_node[nid] =3D + 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 +=3D hugetlb_cma_size_in_node[nid]; + } + } + hugetlb_bootmem_set_nodes(); =20 for (nid =3D 0; nid < MAX_NUMNODES; nid++) { @@ -205,8 +322,12 @@ void __init hugetlb_cma_reserve(void) per_node =3D DIV_ROUND_UP(hugetlb_cma_size, nodes_weight(hugetlb_bootmem_nodes)); per_node =3D 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); } =20 reserved =3D 0; @@ -241,8 +362,12 @@ void __init hugetlb_cma_reserve(void) } =20 reserved +=3D 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); =20 if (reserved >=3D hugetlb_cma_size) break; --=20 2.55.0.679.g6767b8d81c-goog