Skip to contentSkip to conversation

kconfig: fix extra output from savedefconfig on out-of-range defaults

PATCH In review Julian Braha 3 participants 8 messages Aug 30 – Sep 17

Subsystems (3)
lore
Share & tools
lore

Julian Braha

author reviewer
#0 lore raw

PATCH 1/1 kconfig: fix extra output from savedefconfig on out-of-range defaults

The Kconfig interpreter currently allows defaults that are outside of the range bounds.

In these cases, the 'sym_validate_range' function will adjust the default value to the nearest range bound. For example, see this example:

config A
int
range 1 2
default 16

Here, since the default value of 16 is greater than the bounds, the effective default value gets adjusted down to the upper bound, 2.

However, 'savedefconfig' writes non-default values, and without being aware of the automatic adjustment to the range bound, it would write: A=2

This limitation is also documented in a comment: "The following fails to handle the situation where a default value is further limited by the valid range."

To resolve this, let's factor out the default-range adjustment logic from the existing 'sym_validate_range' function into its own 'sym_get_near_range_bound' function for 'savedefconfig' to use too, so that it compares against the effective value.

Adds tests, accordingly.

---
 scripts/kconfig/symbol.c                      | 39 ++++++++----
 .../kconfig/tests/savedefconfig_range/Kconfig | 60 +++++++++++++++++++
 .../tests/savedefconfig_range/__init__.py     |  8 +++
 .../kconfig/tests/savedefconfig_range/config  |  7 +++
 .../savedefconfig_range/expected_defconfig    |  0
 5 files changed, 102 insertions(+), 12 deletions(-)
 create mode 100644 scripts/kconfig/tests/savedefconfig_range/Kconfig
 create mode 100644 scripts/kconfig/tests/savedefconfig_range/__init__.py
 create mode 100644 scripts/kconfig/tests/savedefconfig_range/config
 create mode 100644 scripts/kconfig/tests/savedefconfig_range/expected_defconfig
scripts/kconfig/symbol.c +27 -12
index 2d1c021fa395..72aaae9da4b1 100644
@@ -158,7 +158,12 @@ static long long sym_get_range_val(struct symbol *sym, int base)
 	return strtoll(sym->curr.val, NULL, base);
 }
 
-static void sym_validate_range(struct symbol *sym)
+/*
+ * Return the nearest range bound for an out-of-range default value.
+ * Return NULL if the value is valid or the symbol has no active range.
+ */
+static struct symbol *sym_get_near_range_bound(struct symbol *sym,
+					  const char *value)
 {
 	struct property *prop;
 	struct symbol *range_sym;
@@ -173,21 +178,31 @@ static void sym_validate_range(struct symbol *sym)
 		base = 16;
 		break;
 	default:
-		return;
+		return NULL;
 	}
 	prop = sym_get_range_prop(sym);
 	if (!prop)
-		return;
-	val = strtoll(sym->curr.val, NULL, base);
+		return NULL;
+	val = strtoll(value, NULL, base);
 	range_sym = prop->expr->left.sym;
 	val2 = sym_get_range_val(range_sym, base);
 	if (val >= val2) {
 		range_sym = prop->expr->right.sym;
 		val2 = sym_get_range_val(range_sym, base);
 		if (val <= val2)
-			return;
+			return NULL;
 	}
-	sym->curr.val = range_sym->curr.val;
+
+	return range_sym;
+}
+
+static void sym_validate_range(struct symbol *sym)
+{
+	struct symbol *range_sym;
+
+	range_sym = sym_get_near_range_bound(sym, sym->curr.val);
+	if (range_sym)
+		sym->curr.val = range_sym->curr.val;
 }
 
 static void sym_set_changed(struct symbol *sym)
@@ -832,7 +847,7 @@ bool sym_set_string_value(struct symbol *sym, const char *newval)
 const char *sym_get_string_default(struct symbol *sym)
 {
 	struct property *prop;
-	struct symbol *ds;
+	struct symbol *ds, *range_sym;
 	const char *str = "";
 	tristate val;
 
@@ -850,11 +865,6 @@ const char *sym_get_string_default(struct symbol *sym)
 			val = EXPR_AND(expr_calc_value(prop->expr), prop->visible.tri);
 			break;
 		default:
-			/*
-			 * The following fails to handle the situation
-			 * where a default value is further limited by
-			 * the valid range.
-			 */
 			ds = prop_get_symbol(prop);
 			if (ds != NULL) {
 				sym_calc_value(ds);
@@ -898,6 +908,11 @@ const char *sym_get_string_default(struct symbol *sym)
 	default:
 		break;
 	}
+
+	range_sym = sym_get_near_range_bound(sym, str);
+	if (range_sym)
+		str = range_sym->curr.val;
+
 	return str;
 }
 
scripts/kconfig/tests/savedefconfig_range/Kconfig new file +60 -0

60 lines not on this page · raw message

scripts/kconfig/tests/savedefconfig_range/__init__.py new file +8 -0
new file mode 100644
index 000000000000..961454be732c
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0
+"""Test savedefconfig with numerical defaults outside of ranges."""
+
+
+def test(conf):
+    assert conf._run_conf('--savedefconfig=defconfig', dot_config='config',
+                          out_file='defconfig') == 0
+    assert conf.config_matches('expected_defconfig')
scripts/kconfig/tests/savedefconfig_range/config new file +7 -0
new file mode 100644
index 000000000000..d939cfe5fd34
@@ -0,0 +1,7 @@
+CONFIG_INT_DEFAULT_ABOVE_RANGE=1
+CONFIG_INT_DEFAULT_BELOW_RANGE=4
+CONFIG_INT_DYNAMIC_RANGE=3
+CONFIG_HEX_DEFAULT_ABOVE_RANGE=0x20
+CONFIG_INT_IMPLICIT_DEFAULT_ZERO=1
+CONFIG_INT_CONDITIONAL_RANGE=2
+CONFIG_INT_CONDITIONAL_DEFAULT=2
scripts/kconfig/tests/savedefconfig_range/expected_defconfig new file +0 -0
new file mode 100644
index 000000000000..e69de29bb2d1
Signature · 1 line
2.55.0
Tested-by on v1 1/1
#1 lore raw

Hi Julian,

Thanks for your patch!

Quoted 11 lines · Julian Braha

The Kconfig interpreter currently allows defaults that are outside of the range bounds.

In these cases, the 'sym_validate_range' function will adjust the default value to the nearest range bound. For example, see this example:

config A
int
range 1 2
default 16

Quoted 4 lines

Here, since the default value of 16 is greater than the bounds, the effective default value gets adjusted down to the upper bound, 2.

However, 'savedefconfig' writes non-default values, and without being

s/non-default/non-adjusted/?

Quoted 11 lines

aware of the automatic adjustment to the range bound, it would write: A=2

This limitation is also documented in a comment: "The following fails to handle the situation where a default value is further limited by the valid range."

To resolve this, let's factor out the default-range adjustment logic from the existing 'sym_validate_range' function into its own 'sym_get_near_range_bound' function for 'savedefconfig' to use too, so that it compares against the effective value.

Quoted 7 lines

Adds tests, accordingly.

Thanks, this fixes the issue, and would let us revert commit ab74edaeb1ae7c71 ("erofs: Fix EROFS_FS_ZIP_LZMA_DEFAULT_MAX_STREAMS default logic") in v7.3-rc1.

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#2 lore raw
Quoted 38 lines · Geert Uytterhoeven
Quoted 11 lines · Julian Braha

The Kconfig interpreter currently allows defaults that are outside of the range bounds.

In these cases, the 'sym_validate_range' function will adjust the default value to the nearest range bound. For example, see this example:

config A
int
range 1 2
default 16

Quoted 4 lines

Here, since the default value of 16 is greater than the bounds, the effective default value gets adjusted down to the upper bound, 2.

However, 'savedefconfig' writes non-default values, and without being

s/non-default/non-adjusted/?

Quoted 11 lines

aware of the automatic adjustment to the range bound, it would write: A=2

This limitation is also documented in a comment: "The following fails to handle the situation where a default value is further limited by the valid range."

To resolve this, let's factor out the default-range adjustment logic from the existing 'sym_validate_range' function into its own 'sym_get_near_range_bound' function for 'savedefconfig' to use too, so that it compares against the effective value.

Quoted 7 lines

Adds tests, accordingly.

Quoted 5 lines

Thanks, this fixes the issue, and would let us revert commit ab74edaeb1ae7c71 ("erofs: Fix EROFS_FS_ZIP_LZMA_DEFAULT_MAX_STREAMS default logic") in v7.3-rc1.

FTR, this will impact all defconfigs that restrict SERIAL_8250_NR_UARTS to a value lower than 4:

    config SERIAL_8250_NR_UARTS
            int "Maximum number of 8250/16550 serial ports"
            depends on SERIAL_8250
            default "4"

    config SERIAL_8250_RUNTIME_UARTS
            int "Number of 8250/16550 serial ports to register at runtime"
            depends on SERIAL_8250
            range 0 SERIAL_8250_NR_UARTS
            default "4"

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
author reviewer
#3 lore raw
CC Arnd, Christophe
Quoted 13 lines · Geert Uytterhoeven

FTR, this will impact all defconfigs that restrict SERIAL_8250_NR_UARTS to a value lower than 4:

    config SERIAL_8250_NR_UARTS
            int "Maximum number of 8250/16550 serial ports"
            depends on SERIAL_8250
            default "4"

    config SERIAL_8250_RUNTIME_UARTS
            int "Number of 8250/16550 serial ports to register at runtime"
            depends on SERIAL_8250
            range 0 SERIAL_8250_NR_UARTS
            default "4"

I suspected that there could be some cases where these out-of-range defaults were actually accidents and causing bugs, so I added a check with my SMT solver [1], which also detected these two (powerpc-only):

config DATA_SHIFT

    int "Data shift" if DATA_SHIFT_BOOL
    default 24 if STRICT_KERNEL_RWX && PPC64
    range 17 28 if (STRICT_KERNEL_RWX || DEBUG_PAGEALLOC || KFENCE) &&
PPC_BOOK3S_32
    range 14 23 if (STRICT_KERNEL_RWX || DEBUG_PAGEALLOC || KFENCE) &&
PPC_8xx
    range 20 24 if (STRICT_KERNEL_RWX || DEBUG_PAGEALLOC || KFENCE) &&
PPC_85xx
    default 22 if STRICT_KERNEL_RWX && PPC_BOOK3S_32
    default 18 if (DEBUG_PAGEALLOC || KFENCE) && PPC_BOOK3S_32
    default 23 if (STRICT_KERNEL_RWX || DEBUG_PAGEALLOC || KFENCE) &&
PPC_8xx && \
      (PIN_TLB_DATA || PIN_TLB_TEXT)
    default 19 if (STRICT_KERNEL_RWX || DEBUG_PAGEALLOC || KFENCE) &&
PPC_8xx
    default 24 if STRICT_KERNEL_RWX && PPC_85xx
    default PAGE_SHIFT
  config MODULES_SIZE
    int "Size of modules/execmem area (In Mbytes)" if MODULES_SIZE_BOOL
    range 1 256 if EXECMEM
    default 64 if EXECMEM && PPC_BOOK3S_32
    default 32 if EXECMEM && PPC_8xx
    default 0

The DATA_SHIFT case may be bug, where it should be 12, but incorrectly adjusts to 20 when:
PPC_85xx=y

DEBUG_PAGEALLOC=y

STRICT_KERNEL_RWX=n

PAGE_SHIFT=12

The MODULES_SIZE case seems harmless since the only systems using that option are PPC_BOOK3S_32 and PPC_8xx.

Anyway, this change to savedefconfig would also affect those 2.

[1] https://github.com/julianbraha/kconfirm/tree/smt

- Julian Braha

maintainer Reviewed-by on v1 1/1
#4 lore raw
In reply to Julian Braha
Quoted 11 lines

The Kconfig interpreter currently allows defaults that are outside of the range bounds.

In these cases, the 'sym_validate_range' function will adjust the default value to the nearest range bound. For example, see this example:

config A
int
range 1 2
default 16

Quoted 5 lines

Here, since the default value of 16 is greater than the bounds, the effective default value gets adjusted down to the upper bound, 2.

However, 'savedefconfig' writes non-default values, and without being aware of the automatic adjustment to the range bound, it would write: A=2

Should that 'A=2' be 'A=16'?

Quoted 9 lines

This limitation is also documented in a comment: "The following fails to handle the situation where a default value is further limited by the valid range."

To resolve this, let's factor out the default-range adjustment logic from the existing 'sym_validate_range' function into its own 'sym_get_near_range_bound' function for 'savedefconfig' to use too, so that it compares against the effective value.

Quoted 7 lines

Adds tests, accordingly.

Thanks for the patch!

Given this is a long standing issue, it should probably go via kbuild-next for 7.4, especially in case this results in problems like noted downthread. I would revert commit ab74edaeb1ae ("erofs: Fix EROFS_FS_ZIP_LZMA_DEFAULT_MAX_STREAMS default logic") on top of that.

I will wait a little bit for the folks CC'd downthread to reply to those instances impacted by this change before applying this.

Signature · 2 lines
Cheers,
Nathan
author reviewer
#5 lore raw
Quoted 11 lines · Nathan Chancellor
Quoted 11 lines

The Kconfig interpreter currently allows defaults that are outside of the range bounds.

In these cases, the 'sym_validate_range' function will adjust the default value to the nearest range bound. For example, see this example:

config A
int
range 1 2
default 16

Quoted 7 lines
Quoted 5 lines

Here, since the default value of 16 is greater than the bounds, the effective default value gets adjusted down to the upper bound, 2.

However, 'savedefconfig' writes non-default values, and without being aware of the automatic adjustment to the range bound, it would write: A=2

Should that 'A=2' be 'A=16'?

No typo, this example is adapted from the original report where it needlessly writes the adjusted default. In the original it was 1, here it's 2. [1]

Quoted 22 lines
Quoted 10 lines

This limitation is also documented in a comment: "The following fails to handle the situation where a default value is further limited by the valid range."

To resolve this, let's factor out the default-range adjustment logic from the existing 'sym_validate_range' function into its own 'sym_get_near_range_bound' function for 'savedefconfig' to use too, so that it compares against the effective value.

Quoted 7 lines

Adds tests, accordingly.

Thanks for the patch!

Quoted 8 lines

Given this is a long standing issue, it should probably go via kbuild-next for 7.4, especially in case this results in problems like noted downthread. I would revert commit ab74edaeb1ae ("erofs: Fix EROFS_FS_ZIP_LZMA_DEFAULT_MAX_STREAMS default logic") on top of that.

I will wait a little bit for the folks CC'd downthread to reply to those instances impacted by this change before applying this.

Makes sense to me, and thanks for reviewing!

[1]
https://lore.kernel.org/lkml/c480ed6b8bf38822263e2c5b7cf32b28600f212d.1787219898.git.geert+renesas@glider.be/

- Julian Braha

#6 lore raw
Quoted 3 lines · Julian Braha

No typo, this example is adapted from the original report where it needlessly writes the adjusted default. In the original it was 1, here it's 2. [1]

Oh, right, this is about savedefconfig, I am not sure what I was thinking when I wrote that.

Signature · 2 lines
Cheers,
Nathan
#7 lore raw
In reply to Julian Braha
Quoted 1 line · Julian Braha

kconfig: fix extra output from savedefconfig on out-of-range defaults

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/kbuild/linux.git kbuild-next-unstable

Thanks!

[1/1] kconfig: fix extra output from savedefconfig on out-of-range defaults

      https://git.kernel.org/kbuild/c/dbe1ae2304a3b

I also reverted the workaround that necessitated this change:

https://git.kernel.org/kbuild/c/a2a402bd5e5a4fa466d6034e70b18b6ac22f873d

Please look out for regression or issue reports or other follow up comments, as they may result in the patch/series getting dropped or reverted. Patches applied to an "unstable" branch are accepted pending wider testing in -next and any post-commit review; they will generally be moved to the main branch in a week if no issues are found.

Best regards,

Signature · 2 lines
Cheers,
Nathan