Skip to contentSkip to conversation

kconfig: warn on dead default

PATCH In review Julian Braha 4 participants 7 messages Jun 6 – Jul 7

Subsystems (2)
lore
Share & tools
lore

Julian Braha

author reviewer
#0 lore raw

PATCH 1/1 kconfig: warn on dead default

The dead default check was originally introduced with kconfirm:
https://lore.kernel.org/all/[email protected]/

While I'm still working on that tool, it's not yet ready for inclusion into the tree. I am currently waiting for common distro packagers to package the parsing library before submitting the next RFC iteration.

However, the dead default check is more impactful than the other checks:
all 4 dead defaults that were detected should not have been dead and could cause misconfiguration bugs. But fortunately, these were just for kunit tests. The 3 patches to fix them have all since been merged:
commit aef656a0e6c0 ("powerpc: fix dead default for GUEST_STATE_BUFFER_TEST") commit 30cc5e2ad826 ("s390/Kconfig: Cleanup defaults for selftests") commit df75430515c3 ("drm: fix dead default for DRM_TTM_KUNIT_TEST")

We can actually check for dead defaults while evaluating Kconfig, which should be even more effective at preventing future instances than keeping it in a static checker.

Note that this patch will only trigger a warning when the default values are different, in other words, pure duplicate defaults won't cause a warning, as they are simply redundant.

---
 scripts/kconfig/menu.c                        | 22 +++++++++-
 .../kconfig/tests/warn_dead_default/Kconfig   | 40 +++++++++++++++++++
 .../tests/warn_dead_default/__init__.py       |  8 ++++
 .../tests/warn_dead_default/expected_stderr   |  4 ++
 4 files changed, 73 insertions(+), 1 deletion(-)
 create mode 100644 scripts/kconfig/tests/warn_dead_default/Kconfig
 create mode 100644 scripts/kconfig/tests/warn_dead_default/__init__.py
 create mode 100644 scripts/kconfig/tests/warn_dead_default/expected_stderr
scripts/kconfig/menu.c +21 -1
index b2d8d4e11e07..8c280292f9cd 100644
@@ -242,13 +242,33 @@ static int menu_validate_number(struct symbol *sym, struct symbol *sym2)
 
 static void sym_check_prop(struct symbol *sym)
 {
-	struct property *prop;
+	struct property *prev, *prop;
 	struct symbol *sym2;
 	char *use;
 
 	for (prop = sym->prop; prop; prop = prop->next) {
 		switch (prop->type) {
 		case P_DEFAULT:
+			for_all_defaults(sym, prev) {
+				if (prev == prop)
+					break;
+				if (expr_is_yes(prev->visible.expr)) {
+					if (!expr_eq(prev->expr, prop->expr))
+						prop_warn(prop,
+							"default for '%s' is unreachable: earlier default at %s:%d is unconditional",
+							sym->name ? sym->name : "<choice>",
+							prev->filename, prev->lineno);
+					break;
+				}
+				if (expr_eq(prev->visible.expr, prop->visible.expr)) {
+					if (!expr_eq(prev->expr, prop->expr))
+						prop_warn(prop,
+							"default for '%s' has the same condition as the earlier default at %s:%d",
+							sym->name ? sym->name : "<choice>",
+							prev->filename, prev->lineno);
+					break;
+				}
+			}
 			if ((sym->type == S_STRING || sym->type == S_INT || sym->type == S_HEX) &&
 			    prop->expr->type != E_SYMBOL)
 				prop_warn(prop,
scripts/kconfig/tests/warn_dead_default/Kconfig new file +40 -0
new file mode 100644
index 000000000000..adf421d73dbd
@@ -0,0 +1,40 @@
+# SPDX-License-Identifier: GPL-2.0
+
+config A
+	bool
+
+config B
+	bool
+
+config UNCONDITIONAL
+	int
+	default 1
+	default 2
+
+config CONDITIONAL
+	int
+	default 1 if A
+	default 2 if A
+	default 3 if B
+
+config CONDITIONAL_COMMUTATIVE
+	int
+	default 1 if A && B
+	default 2 if B && A
+
+config CONTROL
+	int
+	default 1 if A
+	default 2 if B
+	default 3
+
+choice
+	prompt "test choice"
+	default C
+	default D
+
+	config C
+		bool "C"
+	config D
+		bool "D"
+endchoice
scripts/kconfig/tests/warn_dead_default/__init__.py new file +8 -0
new file mode 100644
index 000000000000..911b30ce19fe
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0
+"""
+Test detection of dead defaults (different defaults that can never be active).
+"""
+
+def test(conf):
+    assert conf.olddefconfig() == 0
+    assert conf.stderr_contains('expected_stderr')
scripts/kconfig/tests/warn_dead_default/expected_stderr new file +4 -0
new file mode 100644
index 000000000000..baa20bf33910
@@ -0,0 +1,4 @@
+Kconfig:12:warning: default for 'UNCONDITIONAL' is unreachable: earlier default at Kconfig:11 is unconditional
+Kconfig:17:warning: default for 'CONDITIONAL' has the same condition as the earlier default at Kconfig:16
+Kconfig:23:warning: default for 'CONDITIONAL_COMMUTATIVE' has the same condition as the earlier default at Kconfig:22
+Kconfig:34:warning: default for '<choice>' is unreachable: earlier default at Kconfig:33 is unconditional
Signature · 1 line
2.53.0
#1 lore raw

Hi Julian.

Quoted 15 lines · Julian Braha

The dead default check was originally introduced with kconfirm:
https://lore.kernel.org/all/[email protected]/

While I'm still working on that tool, it's not yet ready for inclusion into the tree. I am currently waiting for common distro packagers to package the parsing library before submitting the next RFC iteration.

However, the dead default check is more impactful than the other checks:
all 4 dead defaults that were detected should not have been dead and could cause misconfiguration bugs. But fortunately, these were just for kunit tests. The 3 patches to fix them have all since been merged:
commit aef656a0e6c0 ("powerpc: fix dead default for GUEST_STATE_BUFFER_TEST") commit 30cc5e2ad826 ("s390/Kconfig: Cleanup defaults for selftests") commit df75430515c3 ("drm: fix dead default for DRM_TTM_KUNIT_TEST")

Quoted 7 lines

We can actually check for dead defaults while evaluating Kconfig, which should be even more effective at preventing future instances than keeping it in a static checker.

Note that this patch will only trigger a warning when the default values are different, in other words, pure duplicate defaults won't cause a warning, as they are simply redundant.

A drive-by comment below.

Quoted 17 lines
---
 scripts/kconfig/menu.c                        | 22 +++++++++-
 .../kconfig/tests/warn_dead_default/Kconfig   | 40 +++++++++++++++++++
 .../tests/warn_dead_default/__init__.py       |  8 ++++
 .../tests/warn_dead_default/expected_stderr   |  4 ++
 4 files changed, 73 insertions(+), 1 deletion(-)
 create mode 100644 scripts/kconfig/tests/warn_dead_default/Kconfig
 create mode 100644 scripts/kconfig/tests/warn_dead_default/__init__.py
 create mode 100644 scripts/kconfig/tests/warn_dead_default/expected_stderr
scripts/kconfig/menu.c +0 -0
index b2d8d4e11e07..8c280292f9cd 100644

scripts/kconfig/menu.c static int menu_validate_number(struct symbol *sym, struct symbol *sym2) · line 249

Quoted 9 lines
scripts/kconfig/menu.c +1 -1
@@ -242,13 +242,33 @@ static int menu_validate_number(struct symbol *sym, struct symbol *sym2)
 
 static void sym_check_prop(struct symbol *sym)
 {
-	struct property *prop;
+	struct property *prev, *prop;
 	struct symbol *sym2;
 	char *use;
 
 	for (prop = sym->prop; prop; prop = prop->next) {

This part iterates through all properties of sym.

scripts/kconfig/menu.c static int menu_validate_number(struct symbol *sym, struct symbol *sym2) · line 252

Quoted 3 lines
scripts/kconfig/menu.c +1 -0
 		switch (prop->type) {
 		case P_DEFAULT:
+			for_all_defaults(sym, prev) {

And this part iterates through all properties of sym and will match only P_DEFAULT.

I looks redundant with the two loops were we for the latter knows it is a P_DEFAULT.

Quoted 12 lines
scripts/kconfig/menu.c +12 -0
+				if (prev == prop)
+					break;
+				if (expr_is_yes(prev->visible.expr)) {
+					if (!expr_eq(prev->expr, prop->expr))
+						prop_warn(prop,
+							"default for '%s' is unreachable: earlier default at %s:%d is unconditional",
+							sym->name ? sym->name : "<choice>",
+							prev->filename, prev->lineno);
+					break;
+				}
+				if (expr_eq(prev->visible.expr, prop->visible.expr)) {
+					if (!expr_eq(prev->expr, prop->expr))

scripts/kconfig/menu.c static int menu_validate_number(struct symbol *sym, struct symbol *sym2) · line 274

Quoted 10 lines
scripts/kconfig/menu.c +7 -0
+						prop_warn(prop,
+							"default for '%s' has the same condition as the earlier default at %s:%d",
+							sym->name ? sym->name : "<choice>",
+							prev->filename, prev->lineno);
+					break;
+				}
+			}
 			if ((sym->type == S_STRING || sym->type == S_INT || sym->type == S_HEX) &&
 			    prop->expr->type != E_SYMBOL)
 				prop_warn(prop,

Sam

author reviewer
#2 lore raw
Quoted 2 lines · Sam Ravnborg

I looks redundant with the two loops were we for the latter knows it is a P_DEFAULT.

Hi Sam,

It may look redundant, but we do need to check each default we encounter in the

```
for (prop = sym->prop; prop; prop = prop->next) {
```

loop against the previous defaults to know if we've encountered a duplicate default condition.

I guess the other approach would be to copy each default as we encounter them into a hashtable and check against that, instead? Let me know if you have another, preferred approach.

- Julian Braha

#3 lore raw

Hi Julian.

Quoted 11 lines · Julian Braha
Quoted 2 lines · Sam Ravnborg

I looks redundant with the two loops were we for the latter knows it is a P_DEFAULT.

Hi Sam,

It may look redundant, but we do need to check each default we encounter in the

```
for (prop = sym->prop; prop; prop = prop->next) {
```
Quoted 6 lines

loop against the previous defaults to know if we've encountered a duplicate default condition.

I guess the other approach would be to copy each default as we encounter them into a hashtable and check against that, instead? Let me know if you have another, preferred approach.

It was just a drive-by comment, keep it as simple as possible.

Sam

#4 lore raw
In reply to Julian Braha

Hi Julian,

Quoted 19 lines · Julian Braha

The dead default check was originally introduced with kconfirm:
https://lore.kernel.org/all/[email protected]/

While I'm still working on that tool, it's not yet ready for inclusion into the tree. I am currently waiting for common distro packagers to package the parsing library before submitting the next RFC iteration.

However, the dead default check is more impactful than the other checks:
all 4 dead defaults that were detected should not have been dead and could cause misconfiguration bugs. But fortunately, these were just for kunit tests. The 3 patches to fix them have all since been merged:
commit aef656a0e6c0 ("powerpc: fix dead default for GUEST_STATE_BUFFER_TEST") commit 30cc5e2ad826 ("s390/Kconfig: Cleanup defaults for selftests") commit df75430515c3 ("drm: fix dead default for DRM_TTM_KUNIT_TEST")

We can actually check for dead defaults while evaluating Kconfig, which should be even more effective at preventing future instances than keeping it in a static checker.

Quoted 5 lines

Note that this patch will only trigger a warning when the default values are different, in other words, pure duplicate defaults won't cause a warning, as they are simply redundant.

Nicolas pushed this into -next, where it fires several times for me. I have not looked at these closely yet, I just wanted to get the report out so that more eyes could be on it.

ARCH=powerpc allmodconfig:

arch/powerpc/platforms/Kconfig.cputype:269:warning: default for 'TUNE_CPU' has the same condition as the earlier default at arch/powerpc/platforms/Kconfig.cputype:268 arch/powerpc/platforms/Kconfig.cputype:270:warning: default for 'TUNE_CPU' has the same condition as the earlier default at arch/powerpc/platforms/Kconfig.cputype:268 lib/Kconfig.debug:1083:warning: default for 'PANIC_TIMEOUT' is unreachable: earlier default at arch/powerpc/Kconfig:350 is unconditional arch/Kconfig:1194:warning: default for 'PGTABLE_LEVELS' is unreachable: earlier default at arch/powerpc/Kconfig:482 is unconditional

ARCH=s390 allmodconfig:

arch/Kconfig:1194:warning: default for 'PGTABLE_LEVELS' is unreachable: earlier default at arch/s390/Kconfig:295 is unconditional lib/Kconfig:372:warning: default for 'HAS_IOMEM' is unreachable: earlier default at arch/s390/Kconfig:760 is unconditional

When building with clang-23, which supports '-fms-anonymous-struct':

init/Kconfig:978:warning: default for 'CC_MS_EXTENSIONS' is unreachable: earlier default at init/Kconfig:977 is unconditional

I think the init/Kconfig and arch/powerpc/platforms/Kconfig.cputype ones are intentional due to how cc-option works but I know those might be hard to silence because they are already evaluated by this stage if I understand correctly? Not sure about the other ones (apologies if they are already known or have fixes).

Signature · 2 lines
Cheers,
Nathan
maintainer
#5 lore raw
Quoted 46 lines · Nathan Chancellor

Hi Julian,

Quoted 19 lines · Julian Braha

The dead default check was originally introduced with kconfirm:
https://lore.kernel.org/all/[email protected]/

While I'm still working on that tool, it's not yet ready for inclusion into the tree. I am currently waiting for common distro packagers to package the parsing library before submitting the next RFC iteration.

However, the dead default check is more impactful than the other checks:
all 4 dead defaults that were detected should not have been dead and could cause misconfiguration bugs. But fortunately, these were just for kunit tests. The 3 patches to fix them have all since been merged:
commit aef656a0e6c0 ("powerpc: fix dead default for GUEST_STATE_BUFFER_TEST") commit 30cc5e2ad826 ("s390/Kconfig: Cleanup defaults for selftests") commit df75430515c3 ("drm: fix dead default for DRM_TTM_KUNIT_TEST")

We can actually check for dead defaults while evaluating Kconfig, which should be even more effective at preventing future instances than keeping it in a static checker.

Quoted 5 lines

Note that this patch will only trigger a warning when the default values are different, in other words, pure duplicate defaults won't cause a warning, as they are simply redundant.

Nicolas pushed this into -next, where it fires several times for me. I have not looked at these closely yet, I just wanted to get the report out so that more eyes could be on it.

ARCH=powerpc allmodconfig:

arch/powerpc/platforms/Kconfig.cputype:269:warning: default for 'TUNE_CPU' has the same condition as the earlier default at arch/powerpc/platforms/Kconfig.cputype:268 arch/powerpc/platforms/Kconfig.cputype:270:warning: default for 'TUNE_CPU' has the same condition as the earlier default at arch/powerpc/platforms/Kconfig.cputype:268 lib/Kconfig.debug:1083:warning: default for 'PANIC_TIMEOUT' is unreachable: earlier default at arch/powerpc/Kconfig:350 is unconditional arch/Kconfig:1194:warning: default for 'PGTABLE_LEVELS' is unreachable: earlier default at arch/powerpc/Kconfig:482 is unconditional

ARCH=s390 allmodconfig:

arch/Kconfig:1194:warning: default for 'PGTABLE_LEVELS' is unreachable: earlier default at arch/s390/Kconfig:295 is unconditional lib/Kconfig:372:warning: default for 'HAS_IOMEM' is unreachable: earlier default at arch/s390/Kconfig:760 is unconditional

When building with clang-23, which supports '-fms-anonymous-struct':

Quoted 7 lines

init/Kconfig:978:warning: default for 'CC_MS_EXTENSIONS' is unreachable: earlier default at init/Kconfig:977 is unconditional

I think the init/Kconfig and arch/powerpc/platforms/Kconfig.cputype ones are intentional due to how cc-option works but I know those might be hard to silence because they are already evaluated by this stage if I understand correctly? Not sure about the other ones (apologies if they are already known or have fixes).

thanks for the report. I'll remove the patch from kbuild-for-next again as there is not obvious immediate solution visible to me right now.

Signature · 1 line
Nicolas
author reviewer
#6 lore raw
In reply to Nathan Chancellor

Hi Nathan,

Quoted 3 lines · Nathan Chancellor

Nicolas pushed this into -next, where it fires several times for me. I have not looked at these closely yet, I just wanted to get the report out so that more eyes could be on it.

Thank you for sharing, I've taken a close look.

Quoted 11 lines

ARCH=powerpc allmodconfig:

arch/powerpc/platforms/Kconfig.cputype:269:warning: default for 'TUNE_CPU' has the same condition as the earlier default at arch/powerpc/platforms/Kconfig.cputype:268 arch/powerpc/platforms/Kconfig.cputype:270:warning: default for 'TUNE_CPU' has the same condition as the earlier default at arch/powerpc/platforms/Kconfig.cputype:268 lib/Kconfig.debug:1083:warning: default for 'PANIC_TIMEOUT' is unreachable: earlier default at arch/powerpc/Kconfig:350 is unconditional arch/Kconfig:1194:warning: default for 'PGTABLE_LEVELS' is unreachable: earlier default at arch/powerpc/Kconfig:482 is unconditional

ARCH=s390 allmodconfig:

arch/Kconfig:1194:warning: default for 'PGTABLE_LEVELS' is unreachable: earlier default at arch/s390/Kconfig:295 is unconditional lib/Kconfig:372:warning: default for 'HAS_IOMEM' is unreachable: earlier default at arch/s390/Kconfig:760 is unconditional

It seems to me that all of these instances are probably intended by the architectures to override the global default.

Personally, I find this redefine-and-override style confusing as it requires the reader to think about the order of imports in kconfig. I'll throw them in my kconfig cleanup pile, and see what the relevant maintainers think of my version.

Quoted 9 lines

When building with clang-23, which supports '-fms-anonymous-struct':

init/Kconfig:978:warning: default for 'CC_MS_EXTENSIONS' is unreachable: earlier default at init/Kconfig:977 is unconditional

I think the init/Kconfig and arch/powerpc/platforms/Kconfig.cputype ones are intentional due to how cc-option works but I know those might be hard to silence because they are already evaluated by this stage if I understand correctly? Not sure about the other ones (apologies if they are already known or have fixes).

Yeah, let's leave these alone.

Best not to merge this. Thank you all for taking a look.

- Julian Braha