[PATCH] kconfig: trim CPU and NUMA limits in local configuration targets

From: Yury Norov

Date: Sun Nov 29 2026 - 16:13:32 EST


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@xxxxxxxxxx>
---
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