Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Yury Norov <ynorov@nvidia.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	David Hildenbrand <david@kernel.org>,
	Gregory Price <gourry@gourry.net>, Zi Yan <ziy@nvidia.com>,
	Joshua Hahn <joshua.hahnjy@gmail.com>,
	Rakie Kim <rakie.kim@sk.com>,
	Ying Huang <ying.huang@linux.alibaba.com>,
	Muchun Song <muchun.song@linux.dev>,
	Oscar Salvador <osalvador@suse.de>,
	Johannes Weiner <hannes@cmpxchg.org>,
	Kairui Song <kasong@tencent.com>,
	Thomas Bogendoerfer <tsbogend@alpha.franken.de>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	Len Brown <lenb@kernel.org>, Jens Axboe <axboe@kernel.dk>,
	Peter Zijlstra <peterz@infradead.org>,
	Mel Gorman <mgorman@suse.de>,
	Steven Rostedt <rostedt@goodmis.org>,
	Masami Hiramatsu <mhiramat@kernel.org>,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	linux-mips@vger.kernel.org, linux-acpi@vger.kernel.org,
	linux-block@vger.kernel.org, linux-trace-kernel@vger.kernel.org,
	"Liam R. Howlett" <liam@infradead.org>,
	Axel Rasmussen <axelrasmussen@google.com>,
	Baolin Wang <baolin.wang@linux.alibaba.com>,
	Baoquan He <baoquan.he@linux.dev>, Barry Song <baohua@kernel.org>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Lorenzo Stoakes <ljs@kernel.org>, Michal Hocko <mhocko@suse.com>,
	Mike Rapoport <rppt@kernel.org>, Qi Zheng <qi.zheng@linux.dev>,
	Shakeel Butt <shakeel.butt@linux.dev>,
	Suren Baghdasaryan <surenb@google.com>,
	Vlastimil Babka <vbabka@kernel.org>, Wei Xu <weixugc@google.com>,
	Yuanchu Xie <yuanchu@google.com>,
	Yury Norov <yury.norov@gmail.com>
Subject: Re: [PATCH 00/11] nodemask: switch to runtime nr_node_ids
Date: Sun, 29 Nov 2026 17:05:02 -0500	[thread overview]
Message-ID: <awyhjqBjtnHdsNby@yury> (raw)
In-Reply-To: <20261128202010.18f36e612d57eda194a6c534@linux-foundation.org>

On Sat, Nov 28, 2026 at 08:20:10PM -0800, Andrew Morton wrote:
> On Sat, 28 Nov 2026 22:01:46 -0500 Yury Norov <ynorov@nvidia.com> wrote:
> 
> > I would suggest another experiment. Let's on the system boot print a
> > message like:
> > 
> >     The kernel is configured to support for up to 1024 NUMA nodes, while
> >     the actual number of nodes on this system is 2. This leaves 99.8% of
> >     the allocated resources unused. Please consider reconfiguring.
> > 
> > We can show it, say, at 90% rate or higher, and wait until this will start
> > showing up in the people's boot logs. :)
> 
> I love it - you're a troll after my own heart.
> 
> I guess we could try it as a standalone thing with lots of convincing
> justifications.  Are you able to estimate the cost of these wasted
> resources?

Yes, let me think about it for a few days, and I'll figure something
out.
 
> otoh, I'm sure our downstream have considered all this and have good
> reasons for these configurations so we should find out what those
> reasons are before adding shouty things to dmesg,
 
I agree with Linus' comment on this.

> ditto NR_CPUS?
> 
> Who                         NR_CPUS
> Mainline default                  64
> RHEL                           8192
> SUSE                           8192
> Oracle UEK                     8192
> Ubuntu                         8192
> Debian                         8192
> Fedora                         8192
> AWS (Amazon Linux)             8192
> Google (COS)                    512
> Microsoft (Azure Linux)        8192
> Meta                        unknown
> Tencent (CubeSandbox)           128
> 
> Seems that as long as the machine gets nr_cpu_ids reasonably correct,
> the main cost here is one bit/cpu in the task_struct: 1k.  Which is not
> insignificant.

In aec6487e994d2, Ingo says each supported CPU eats ~8KB. It's 10 years
old measurements, though. I'll recalculate it myself.

I think, the best solution for distros would be providing whatever
kernel they want (but please better than now), then once loaded build
it against localmodconfig, or similar. 

I would like at least to have such an option.

Our local{yes,mod}config doesn't trim NR_CPUS and MAX_NUMNODES to the
actual values, though. The attached patch teaches it to do that.

Thanks,
Yury

From 44c8589f38d5eb73b0eec64fff7f3602e3ff50cd Mon Sep 17 00:00:00 2001
From: Yury Norov <ynorov@nvidia.com>
Date: Sun, 29 Nov 2026 16:13:32 -0500
Subject: [PATCH] kconfig: trim CPU and NUMA limits in local configuration
 targets

localmodconfig and localyesconfig trim module selections but retain the
CPU and NUMA limits inherited from their base configuration. Distribution
configurations can reserve much more capacity than the local machine needs.

Add a conf --localconfig option and use it from both targets.

Skip topology adjustments when LSMOD is supplied, since the module list
may describe another machine.

Signed-off-by: Yury Norov <ynorov@nvidia.com>
---
 Documentation/admin-guide/README.rst |   7 +-
 scripts/kconfig/Makefile             |   8 +-
 scripts/kconfig/conf.c               | 144 +++++++++++++++++++++++++++
 3 files changed, 153 insertions(+), 6 deletions(-)

diff --git a/Documentation/admin-guide/README.rst b/Documentation/admin-guide/README.rst
index 77fec1de6dc8..973facd8197c 100644
--- a/Documentation/admin-guide/README.rst
+++ b/Documentation/admin-guide/README.rst
@@ -201,9 +201,10 @@ Configuring the kernel
      "make randconfig"  Create a ./.config file by setting symbol
                         values to random values.
 
-     "make localmodconfig" Create a config based on current config and
-                           loaded modules (lsmod). Disables any module
-                           option that is not needed for the loaded modules.
+     "make localmodconfig" Create a config based on current config, possible
+                           number of CPUs and NUMA nodes, and loaded modules
+                           (lsmod). Disables any module option that is not
+                           needed for the loaded modules.
 
                            To create a localmodconfig for another machine,
                            store the lsmod of that machine into a file
diff --git a/scripts/kconfig/Makefile b/scripts/kconfig/Makefile
index 5baf1c44ffa2..1f79c5bf988f 100644
--- a/scripts/kconfig/Makefile
+++ b/scripts/kconfig/Makefile
@@ -55,18 +55,20 @@ endef
 
 $(foreach c, config menuconfig nconfig gconfig xconfig, $(eval $(call config_rule,$(c))))
 
+# A supplied module list may describe another machine's topology.
+localconfig-flag := $(if $(filter undefined,$(origin LSMOD)),--localconfig)
+
 PHONY += localmodconfig localyesconfig
 localyesconfig localmodconfig: $(obj)/conf
 	$(Q)$(PERL) $(src)/streamline_config.pl --$@ $(srctree) $(Kconfig) > .tmp.config
 	$(Q)if [ -f .config ]; then 				\
-		cmp -s .tmp.config .config ||			\
 		(mv -f .config .config.old.1;			\
 		 mv -f .tmp.config .config;			\
-		 $< $(silent) --oldconfig $(Kconfig);		\
+		 $< $(silent) $(localconfig-flag) --oldconfig $(Kconfig); \
 		 mv -f .config.old.1 .config.old)		\
 	else							\
 		mv -f .tmp.config .config;			\
-		$< $(silent) --oldconfig $(Kconfig);		\
+		$< $(silent) $(localconfig-flag) --oldconfig $(Kconfig); \
 	fi
 	$(Q)rm -f .tmp.config
 
diff --git a/scripts/kconfig/conf.c b/scripts/kconfig/conf.c
index 31c6fd25e6ec..000e6014af58 100644
--- a/scripts/kconfig/conf.c
+++ b/scripts/kconfig/conf.c
@@ -636,9 +636,132 @@ static void check_conf(struct menu *menu)
 		check_conf(child);
 }
 
+static int lastnum(const unsigned char *str, size_t len)
+{
+	const unsigned char *start, *end = str + len;
+	unsigned int ret = 0, pos = 1, d;
+
+	if (end > str && end[-1] == '\n')
+		end--;
+
+	for (start = end; start > str && isdigit(start[-1]); start--, pos *= 10) {
+		d = start[-1] - '0';
+		ret += d * pos;
+	}
+
+	if (start == end || (start > str && start[-1] != ',' && start[-1] != '-'))
+		return -1;
+
+	return ret;
+}
+
+static int local_capacity(const char *path)
+{
+	FILE *fp;
+	unsigned char *buf = NULL;
+	size_t size = 0;
+	ssize_t len;
+	int highest = -1;
+
+	fp = fopen(path, "r");
+	if (!fp)
+		return 0;
+
+	len = getline((char **)&buf, &size, fp);
+	if (len >= 0)
+		highest = lastnum(buf, len);
+
+	free(buf);
+	fclose(fp);
+	return highest + 1;
+}
+
+static unsigned int ceil_log2(unsigned int value)
+{
+	if (value <= 1)
+		return 0;
+
+	return sizeof(value) * CHAR_BIT - __builtin_clz(value - 1);
+}
+
+static void conf_local_maxnumnodes(void)
+{
+	struct symbol *numa, *nodes_shift;
+	struct property *range;
+	unsigned int nodes, shift;
+	unsigned long min;
+	char buf[32];
+
+	numa = sym_find("NUMA");
+	if (!numa)
+		return;
+
+	nodes = local_capacity("/sys/devices/system/node/possible");
+	if (!nodes) {
+		fprintf(stderr, "Cannot get number of NUMA nodes\n");
+		return;
+	}
+
+	sym_calc_value(numa);
+	if (nodes == 1) {
+		sym_set_tristate_value(numa, no);
+		return;
+	}
+
+	nodes_shift = sym_find("NODES_SHIFT");
+	if (!nodes_shift || sym_get_tristate_value(numa) != yes)
+		return;
+
+	sym_calc_value(nodes_shift);
+	shift = ceil_log2(nodes);
+	range = sym_get_range_prop(nodes_shift);
+	if (range) {
+		min = strtoul(sym_get_string_value(range->expr->left.sym),
+			      NULL, 10);
+		if (shift < min)
+			shift = min;
+	}
+	snprintf(buf, sizeof(buf), "%u", shift);
+	if (!sym_set_string_value(nodes_shift, buf))
+		fprintf(stderr, "Cannot set NODES_SHIFT to local capacity %u\n", shift);
+}
+
+static void conf_local_nr_cpus(void)
+{
+	struct symbol *smp, *nr_cpus;
+	unsigned int cpus;
+	char buf[32];
+
+	smp = sym_find("SMP");
+	if (!smp)
+		return;
+
+	cpus = local_capacity("/sys/devices/system/cpu/possible");
+	if (!cpus) {
+		fprintf(stderr, "Cannot get number of CPUs\n");
+		return;
+	}
+
+	sym_calc_value(smp);
+	if (cpus == 1) {
+		sym_set_tristate_value(smp, no);
+		return;
+	}
+
+	nr_cpus = sym_find("NR_CPUS");
+
+	if (nr_cpus && sym_get_tristate_value(smp) == yes) {
+		sym_calc_value(nr_cpus);
+		snprintf(buf, sizeof(buf), "%u", cpus);
+		if (!sym_set_string_value(nr_cpus, buf))
+			fprintf(stderr, "Cannot set NR_CPUS to local capacity %u\n", cpus);
+	}
+}
+
 static const struct option long_opts[] = {
 	{"help",          no_argument,       NULL,            'h'},
 	{"silent",        no_argument,       NULL,            's'},
+	{"localconfig",   no_argument,       NULL,            'l'},
 	{"oldaskconfig",  no_argument,       &input_mode_opt, oldaskconfig},
 	{"oldconfig",     no_argument,       &input_mode_opt, oldconfig},
 	{"syncconfig",    no_argument,       &input_mode_opt, syncconfig},
@@ -665,6 +788,7 @@ static void conf_usage(const char *progname)
 	printf("Generic options:\n");
 	printf("  -h, --help              Print this message and exit.\n");
 	printf("  -s, --silent            Do not print log.\n");
+	printf("      --localconfig       Limit CPUs and NUMA nodes to local hardware.\n");
 	printf("\n");
 	printf("Mode options:\n");
 	printf("  --listnewconfig         List new options\n");
@@ -696,6 +820,7 @@ int main(int ac, char **av)
 	int opt;
 	const char *name, *defconfig_file = NULL /* gcc uninit */;
 	int no_conf_write = 0;
+	bool localconfig = false;
 
 	tty_stdio = isatty(0) && isatty(1);
 
@@ -708,6 +833,9 @@ int main(int ac, char **av)
 		case 's':
 			conf_set_message_callback(NULL);
 			break;
+		case 'l':
+			localconfig = true;
+			break;
 		case 0:
 			input_mode = input_mode_opt;
 			switch (input_mode) {
@@ -805,6 +933,22 @@ int main(int ac, char **av)
 	if (conf_errors())
 		exit(1);
 
+	if (localconfig) {
+		struct symbol *maxsmp;
+
+		/* MAXSMP hides the CPU and node limits and forces their maxima. */
+		maxsmp = sym_find("MAXSMP");
+		if (maxsmp)
+			sym_set_tristate_value(maxsmp, no);
+
+		/*
+		 * Preserve the order to make sure that Kconfig doesn't reject
+		 * disabling SMP if NUMA is still enabled.
+		 */
+		conf_local_maxnumnodes();
+		conf_local_nr_cpus();
+	}
+
 	if (sync_kconfig) {
 		name = getenv("KCONFIG_NOSILENTUPDATE");
 		if (name && *name) {
-- 
2.53.0



      parent reply	other threads:[~2026-11-29 22:05 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 23:01 [PATCH 00/11] nodemask: switch to runtime nr_node_ids Yury Norov
2026-11-27 23:01 ` [PATCH 01/11] nodemask: add 'small' and 'large' compile-time sizes Yury Norov
2026-11-27 23:01 ` [PATCH 02/11] nodemask: pass nodemask length to node search functions Yury Norov
2026-11-27 23:01 ` [PATCH 03/11] mm/mempolicy: reject migrate_pages source IDs beyond nr_node_ids Yury Norov
2026-11-27 23:01 ` [PATCH 04/11] mm/mempolicy: preserve full-width userspace nodemasks Yury Norov
2026-11-27 23:01 ` [PATCH 05/11] mm: switch to runtime nodemask length Yury Norov
2026-11-27 23:01 ` [PATCH 06/11] MIPS: IP27: switch nmi_dump " Yury Norov
2026-11-27 23:01 ` [PATCH 07/11] ACPI: NUMA: switch acpi_map_pxm_to_node " Yury Norov
2026-11-27 23:01 ` [PATCH 08/11] mtip32xx: switch next_online_node() " Yury Norov
2026-11-27 23:01 ` [PATCH 09/11] nodemask: add nodes_copy_and_extend() Yury Norov
2026-11-27 23:01 ` [PATCH 10/11] sched/numa: bound cpuset trace masks by nr_node_ids Yury Norov
2026-11-28 23:02   ` sashiko-bot
2026-11-27 23:01 ` [PATCH 11/11] nodemask: add cpumask-style operation size bounds Yury Norov
2026-11-28 23:40 ` [PATCH 00/11] nodemask: switch to runtime nr_node_ids Andrew Morton
2026-11-29  3:01   ` Yury Norov
2026-11-29  4:20     ` Andrew Morton
2026-11-29  4:21       ` Linus Torvalds
2026-11-29 22:05       ` Yury Norov [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=awyhjqBjtnHdsNby@yury \
    --to=ynorov@nvidia.com \
    --cc=akpm@linux-foundation.org \
    --cc=axboe@kernel.dk \
    --cc=axelrasmussen@google.com \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=baoquan.he@linux.dev \
    --cc=david@kernel.org \
    --cc=gourry@gourry.net \
    --cc=hannes@cmpxchg.org \
    --cc=joshua.hahnjy@gmail.com \
    --cc=kasong@tencent.com \
    --cc=lenb@kernel.org \
    --cc=liam@infradead.org \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mips@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=ljs@kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mgorman@suse.de \
    --cc=mhiramat@kernel.org \
    --cc=mhocko@suse.com \
    --cc=muchun.song@linux.dev \
    --cc=osalvador@suse.de \
    --cc=peterz@infradead.org \
    --cc=qi.zheng@linux.dev \
    --cc=rafael@kernel.org \
    --cc=rakie.kim@sk.com \
    --cc=rostedt@goodmis.org \
    --cc=rppt@kernel.org \
    --cc=shakeel.butt@linux.dev \
    --cc=surenb@google.com \
    --cc=torvalds@linux-foundation.org \
    --cc=tsbogend@alpha.franken.de \
    --cc=vbabka@kernel.org \
    --cc=weixugc@google.com \
    --cc=ying.huang@linux.alibaba.com \
    --cc=yuanchu@google.com \
    --cc=yury.norov@gmail.com \
    --cc=ziy@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox