From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.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 178342F12AC for ; Thu, 25 Jun 2026 21:59:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782424744; cv=none; b=kXeebKvoUMzC+zwzTJz2mi3ilv9ZJ0YSQsxSgsNx7ZotABNXzVj3zw3IWwO7lZcj9Gjob6gaE5Bygfv2235OXNBGGFdrhiHbtwgP1w1uxBJYB/v1C6r9uvuwnPq2REfRQaxQ4b0vDvcIM1tL1eTwI9kagneopcJDJj/E6DfMNHM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782424744; c=relaxed/simple; bh=RN79o8C1UBe65cHqEJrCm5p2bOJfKQXp9d4NRnnFyk0=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=hLN8S9MOtvz9O47iZywYb9MlHis1DzAwA3ZZxJ8M8caFIBV+4oag28MkXZc1vFgki6MqeiAk10zTc+NOwgoCcpLqAdjcLmPd9DHDvEIaqmVDKosJAkFfGAQo22dIKHpBPHIPHk406rFhIfYIXNKZtaoopPCsgD9mK1cMJN7aWT0= 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=YnSvm5rl; arc=none smtp.client-ip=209.85.210.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="YnSvm5rl" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-845317fa7e6so334413b3a.3 for ; Thu, 25 Jun 2026 14:59:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1782424742; x=1783029542; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=d2PbcE6YHaWKPSTOJuBDMS6/xM75stA7GMJ8h7+fVys=; b=YnSvm5rlwqvZgoPr46Z4tF2a0WnURPJHYK1JaDsdKHqQvFDL4jY6N3vfu3rWtOcafS ScmjtYNYOad92dJRyUJzt02SWQ9qu/TRArjg9pWYhmZCZkFcGZuDAiJK9VGozaUWa2Bg uz7gyfsBOQzQmQpxNESMXUtRn+EOMM5Ww4pgCNQZAv4qfWgDEXNz0wWb3YRBjfOpFdex mo+hGmT7k9TWwl81bMdl1ulCc9dv9XrQX7Q/4XueRiuQ0sq+PC1Jm8ewLOPvkfGf5n+X pG96Db8TeCpaVwbMhyCSfODXl2tnXJMLOexk2f+AHRhh7F/sB7Nv4h2YVneGy7PGvaBS qK4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782424742; x=1783029542; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=d2PbcE6YHaWKPSTOJuBDMS6/xM75stA7GMJ8h7+fVys=; b=XjJDzBCOsZR2ExGpN8Geya3BWBlxezH5kT1nJI046bf3VYh2I5iw+XDtfz2hjDrcVP xLffapkyMZG8Ujrk5XtY8G0lWYx5YdaDb4HJRof0cf/C84MbPxWsp+B1cRC/6q+oHMjS WIZ/ZInOz6sooRg54VAi7oOahZ7m8Nn/ibMSnSxVuWHHEKIwSn/aK1+wvEkiWjxtazut GYHbXD2QYswtlgiHZ2vmqVLuCScbsbgRwTVxQOc5ZKt55g7qih24sddniM5JKR7hrnes vrH4TVVECIOjxZT/hrbcW8HVAdC0I+7P5RgJPw5q/4WWoy+z0zKOWtJT1Ky0xae93o/r sczA== X-Forwarded-Encrypted: i=1; AFNElJ8oiwdZZH+nNGOPoAUgZ/NChMRAIXRi/djYu6F4+rUs1J/4NNB0O2tOBWTIIXs3up2T78BYbgRZ9xWC3l4=@vger.kernel.org X-Gm-Message-State: AOJu0YxdXTfo4hKtFcjZSzGaPU3CbF6L8KZdzKXDux7eZey6Wj3bzWLR 0pbNnsO+raCS9uB3ZSEoQapeCcKRywd9bCYvmwP6AMc1VYt0WbAGY/VcpsZtWhKAnLG8bN1BgyT sEh7ceY/ESrXZbCnMEhzAFY3rlg== X-Received: from pfwz36.prod.google.com ([2002:a05:6a00:1da4:b0:845:aa6d:92ea]) (user=souravpanda job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2989:b0:842:708f:39a0 with SMTP id d2e1a72fcca58-845b39e15ecmr4983540b3a.8.1782424742033; Thu, 25 Jun 2026 14:59:02 -0700 (PDT) Date: Thu, 25 Jun 2026 21:59:00 +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: <20260625215900.2151690-1-souravpanda@google.com> Subject: [PATCH v1] 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, 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 --- .../admin-guide/kernel-parameters.txt | 7 +- mm/hugetlb_cma.c | 81 ++++++++++++++++++- 2 files changed, 82 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..b4f8bbe4c62d 100644 --- a/mm/hugetlb_cma.c +++ b/mm/hugetlb_cma.c @@ -9,6 +9,8 @@ #include #include +#include +#include #include "internal.h" #include "hugetlb_cma.h" @@ -18,6 +20,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 unsigned long __init memblock_node_memory_size(int nid) +{ + struct memblock_region *reg; + unsigned long size = 0; + + for_each_mem_region(reg) { + if (reg->nid == nid) + size += reg->size; + } + return size; +} +#else +static unsigned long __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 +124,27 @@ 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; + s = next + 1; + } else { + hugetlb_cma_size_in_node[nid] = tmp; + hugetlb_cma_size += tmp; + s = next; + } /* * Skip the separator if have one, otherwise @@ -118,7 +155,17 @@ 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; + } else { + hugetlb_cma_size = tmp; + } break; } } @@ -144,8 +191,34 @@ 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) { + for (nid = 0; nid < MAX_NUMNODES; nid++) { + if (hugetlb_cma_percent_in_node[nid]) { + unsigned long node_gfp_mem = memblock_node_memory_size(nid); + unsigned long s; + + s = mult_frac(node_gfp_mem, + hugetlb_cma_percent_in_node[nid], + 100); + + hugetlb_cma_size_in_node[nid] = s; + hugetlb_cma_size += s; + } + } + } else if (hugetlb_cma_percent) { + hugetlb_cma_size = mult_frac(memblock_phys_mem_size(), hugetlb_cma_percent, 100); + } + if (!hugetlb_cma_size) return; -- 2.55.0.rc0.799.gd6f94ed593-goog