Skip to contentSkip to conversation

Improve WARN_ON_ONCE() output by adding the condition string

PATCH In review Ingo Molnar 4 participants 16 messages Mar 26, 2025 – Apr 1, 2025

Subsystems (10)
x86 PowerPC RISC-V arm64 +6 more subsystems
lore
Share & tools
lore
#0 lore raw

cover Improve WARN_ON_ONCE() output by adding the condition string

This series improves the current WARN_ON_ONCE() output from:

  WARN_ON_ONCE(idx < 0 && ptr);
  ...
  WARNING: CPU: 0 PID: 0 at kernel/sched/core.c:8511 sched_init+0x20/0x410
  CPU: 0 UID: 0 PID: 0 Comm: swapper Not tainted 6.14.0-01616-g94d7af2844aa #4 PREEMPT(undef)

to (on x86):

  WARNING: [idx < 0 && ptr] kernel/sched/core.c:8511 sched_init+0x20/0x410
  CPU: 0 UID: 0 PID: 0 Comm: swapper Not tainted 6.14.0-01616-g94d7af2844aa #4 PREEMPT(undef)

and on non-x86 architectures (the CPU/PID fields in the WARNING line are skipped):

  WARNING: kernel/sched/core.c:8511 sched_init+0x20/0x410
  CPU: 0 UID: 0 PID: 0 Comm: swapper Not tainted 6.14.0-01616-g94d7af2844aa #4 PREEMPT(undef)

The motivation is the SCHED_WARN_ON() primitive that got removed in this merge window:

f7d2728cc032 ("sched/debug: Change SCHED_WARN_ON() to WARN_ON_ONCE()")

... which produced more informative debug output, as it included the WARN_ON_ONCE() condition string - at the expense of worse code generation.

This series, based on Linus's latest Git tree, merges the code generation benefits of modern WARN_ON_ONCE() bug_entry architecture code with the expanded information content of SCHED_WARN_ON().

The cost is about +100K more .data on a defconfig kernel, and no runtime code generation impact:

       text       data        bss         dec        hex    filename
   29523998    7926322    1389904    38840224    250a7a0    vmlinux.x86.defconfig.before
   29523998    8024626    1389904    38938528    25227a0    vmlinue.x86.defconfig.after

The series was build and boot tested on x86, with an expectation for it to work on other architectures (with no testing at the moment to back up that expectation).

Thanks,

Ingo

================>
Ingo Molnar (5):
bugs/core: Extend __WARN_FLAGS() with the 'cond_str' parameter
bugs/core: Pass down the condition string of WARN_ON_ONCE(cond) warnings to __WARN_FLAGS() bugs/x86: Extend _BUG_FLAGS() with the 'cond_str' parameter
bugs/x86: Augment warnings output by concatenating 'cond_str' with the regular __FILE__ string in _BUG_FLAGS() bugs/core: Do not print CPU and PID values in__warn() output

 arch/arm64/include/asm/bug.h     |  2 +-
 arch/loongarch/include/asm/bug.h |  2 +-
 arch/parisc/include/asm/bug.h    |  4 ++--
 arch/powerpc/include/asm/bug.h   |  2 +-
 arch/riscv/include/asm/bug.h     |  2 +-
 arch/s390/include/asm/bug.h      |  2 +-
 arch/sh/include/asm/bug.h        |  2 +-
 arch/x86/include/asm/bug.h       | 14 +++++++-------
 include/asm-generic/bug.h        |  7 ++++---
 kernel/panic.c                   |  7 ++-----
 10 files changed, 21 insertions(+), 23 deletions(-)
Signature · 1 line
2.45.2
#1 lore raw

PATCH 1/5 bugs/core: Extend __WARN_FLAGS() with the 'cond_str' parameter

Push it down into every architecture that defines __WARN_FLAGS().

Don't pass anything substantial down yet, just propagate the new parameter with empty strings, without generating it or using it.

---
 arch/arm64/include/asm/bug.h     | 2 +-
 arch/loongarch/include/asm/bug.h | 2 +-
 arch/parisc/include/asm/bug.h    | 4 ++--
 arch/powerpc/include/asm/bug.h   | 2 +-
 arch/riscv/include/asm/bug.h     | 2 +-
 arch/s390/include/asm/bug.h      | 2 +-
 arch/sh/include/asm/bug.h        | 2 +-
 arch/x86/include/asm/bug.h       | 2 +-
 include/asm-generic/bug.h        | 7 ++++---
 9 files changed, 13 insertions(+), 12 deletions(-)
arch/arm64/include/asm/bug.h +1 -1
index 28be048db3f6..bceeaec21fb9 100644
@@ -19,7 +19,7 @@
 	unreachable();					\
 } while (0)
 
-#define __WARN_FLAGS(flags) __BUG_FLAGS(BUGFLAG_WARNING|(flags))
+#define __WARN_FLAGS(cond_str, flags) __BUG_FLAGS(BUGFLAG_WARNING|(flags))
 
 #define HAVE_ARCH_BUG
 
arch/loongarch/include/asm/bug.h +1 -1
index f6f254f2c5db..51c2cb98d728 100644
@@ -42,7 +42,7 @@
 	asm_inline volatile (__stringify(ASM_BUG_FLAGS(flags))		\
 			     extra);
 
-#define __WARN_FLAGS(flags)					\
+#define __WARN_FLAGS(cond_str, flags)				\
 do {								\
 	instrumentation_begin();				\
 	__BUG_FLAGS(BUGFLAG_WARNING|(flags), ANNOTATE_REACHABLE(10001b));\
arch/parisc/include/asm/bug.h +2 -2
index 833555f74ffa..1a87cf80ec3c 100644
@@ -50,7 +50,7 @@
 #endif
 
 #ifdef CONFIG_DEBUG_BUGVERBOSE
-#define __WARN_FLAGS(flags)						\
+#define __WARN_FLAGS(cond_str, flags)					\
 	do {								\
 		asm volatile("\n"					\
 			     "1:\t" PARISC_BUG_BREAK_ASM "\n"		\
@@ -66,7 +66,7 @@
 			     "i" (sizeof(struct bug_entry)) );		\
 	} while(0)
 #else
-#define __WARN_FLAGS(flags)						\
+#define __WARN_FLAGS(cond_str, flags)					\
 	do {								\
 		asm volatile("\n"					\
 			     "1:\t" PARISC_BUG_BREAK_ASM "\n"		\
arch/powerpc/include/asm/bug.h +1 -1
index 1db485aacbd9..34d39ec79720 100644
@@ -72,7 +72,7 @@
 } while (0)
 #define HAVE_ARCH_BUG
 
-#define __WARN_FLAGS(flags) BUG_ENTRY("twi 31, 0, 0", BUGFLAG_WARNING | (flags))
+#define __WARN_FLAGS(cond_str, flags) BUG_ENTRY("twi 31, 0, 0", BUGFLAG_WARNING | (flags))
 
 #ifdef CONFIG_PPC64
 #define BUG_ON(x) do {						\
arch/riscv/include/asm/bug.h +1 -1
index 1aaea81fb141..b22ee4d2c882 100644
@@ -76,7 +76,7 @@ do {								\
 	unreachable();						\
 } while (0)
 
-#define __WARN_FLAGS(flags) __BUG_FLAGS(BUGFLAG_WARNING|(flags))
+#define __WARN_FLAGS(cond_str, flags) __BUG_FLAGS(BUGFLAG_WARNING|(flags))
 
 #define HAVE_ARCH_BUG
 
arch/s390/include/asm/bug.h +1 -1
index c500d45fb465..ef3e495ec1e3 100644
@@ -46,7 +46,7 @@
 	unreachable();					\
 } while (0)
 
-#define __WARN_FLAGS(flags) do {			\
+#define __WARN_FLAGS(cond_str, flags) do {		\
 	__EMIT_BUG(BUGFLAG_WARNING|(flags));		\
 } while (0)
 
arch/sh/include/asm/bug.h +1 -1
index 05a485c4fabc..834c621ab249 100644
@@ -52,7 +52,7 @@ do {							\
 	unreachable();					\
 } while (0)
 
-#define __WARN_FLAGS(flags)				\
+#define __WARN_FLAGS(cond_str, flags)			\
 do {							\
 	__asm__ __volatile__ (				\
 		"1:\t.short %O0\n"			\
arch/x86/include/asm/bug.h +1 -1
index f0e9acf72547..413b86b876d9 100644
@@ -92,7 +92,7 @@ do {								\
  * were to trigger, we'd rather wreck the machine in an attempt to get the
  * message out than not know about it.
  */
-#define __WARN_FLAGS(flags)					\
+#define __WARN_FLAGS(cond_str, flags)				\
 do {								\
 	__auto_type __flags = BUGFLAG_WARNING|(flags);		\
 	instrumentation_begin();				\
include/asm-generic/bug.h +4 -3
index 387720933973..af76e4a04b16 100644
@@ -100,17 +100,18 @@ extern __printf(1, 2) void __warn_printk(const char *fmt, ...);
 		instrumentation_end();					\
 	} while (0)
 #else
-#define __WARN()		__WARN_FLAGS(BUGFLAG_TAINT(TAINT_WARN))
+#define __WARN()		__WARN_FLAGS("", BUGFLAG_TAINT(TAINT_WARN))
 #define __WARN_printf(taint, arg...) do {				\
 		instrumentation_begin();				\
 		__warn_printk(arg);					\
-		__WARN_FLAGS(BUGFLAG_NO_CUT_HERE | BUGFLAG_TAINT(taint));\
+		__WARN_FLAGS("", BUGFLAG_NO_CUT_HERE | BUGFLAG_TAINT(taint));\
 		instrumentation_end();					\
 	} while (0)
 #define WARN_ON_ONCE(condition) ({				\
 	int __ret_warn_on = !!(condition);			\
 	if (unlikely(__ret_warn_on))				\
-		__WARN_FLAGS(BUGFLAG_ONCE |			\
+		__WARN_FLAGS("",				\
+			     BUGFLAG_ONCE |			\
 			     BUGFLAG_TAINT(TAINT_WARN));	\
 	unlikely(__ret_warn_on);				\
 })
Signature · 1 line
2.45.2
#2 lore raw

PATCH 2/5 bugs/core: Pass down the condition string of WARN_ON_ONCE(cond) warnings to __WARN_FLAGS()

In reply to Ingo Molnar

This will allow architecture code to print out this information.

The format of the string is '[condition]', for example:

  WARN_ON_ONCE(idx < 0 && ptr);

Will get the '[idx < 0 && ptr]' string literal passed down as 'cond_str' in __WARN_FLAGS().

---
 include/asm-generic/bug.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/asm-generic/bug.h +1 -1
index af76e4a04b16..c8e7126bc26e 100644
@@ -110,7 +110,7 @@ extern __printf(1, 2) void __warn_printk(const char *fmt, ...);
 #define WARN_ON_ONCE(condition) ({				\
 	int __ret_warn_on = !!(condition);			\
 	if (unlikely(__ret_warn_on))				\
-		__WARN_FLAGS("",				\
+		__WARN_FLAGS("["#condition"] ",			\
 			     BUGFLAG_ONCE |			\
 			     BUGFLAG_TAINT(TAINT_WARN));	\
 	unlikely(__ret_warn_on);				\
Signature · 1 line
2.45.2
#3 lore raw

PATCH 3/5 bugs/x86: Extend _BUG_FLAGS() with the 'cond_str' parameter

In reply to Ingo Molnar

Just pass down the parameter, don't do anything with it yet.

---
 arch/x86/include/asm/bug.h | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
arch/x86/include/asm/bug.h +5 -5
index 413b86b876d9..aff1c6b7a7f3 100644
@@ -39,7 +39,7 @@
 
 #ifdef CONFIG_DEBUG_BUGVERBOSE
 
-#define _BUG_FLAGS(ins, flags, extra)					\
+#define _BUG_FLAGS(cond_str, ins, flags, extra)				\
 do {									\
 	asm_inline volatile("1:\t" ins "\n"				\
 		     ".pushsection __bug_table,\"aw\"\n"		\
@@ -57,7 +57,7 @@ do {									\
 
 #else /* !CONFIG_DEBUG_BUGVERBOSE */
 
-#define _BUG_FLAGS(ins, flags, extra)					\
+#define _BUG_FLAGS(cond_str, ins, flags, extra)				\
 do {									\
 	asm_inline volatile("1:\t" ins "\n"				\
 		     ".pushsection __bug_table,\"aw\"\n"		\
@@ -74,7 +74,7 @@ do {									\
 
 #else
 
-#define _BUG_FLAGS(ins, flags, extra)  asm volatile(ins)
+#define _BUG_FLAGS(cond_str, ins, flags, extra)  asm volatile(ins)
 
 #endif /* CONFIG_GENERIC_BUG */
 
@@ -82,7 +82,7 @@ do {									\
 #define BUG()							\
 do {								\
 	instrumentation_begin();				\
-	_BUG_FLAGS(ASM_UD2, 0, "");				\
+	_BUG_FLAGS("", ASM_UD2, 0, "");				\
 	__builtin_unreachable();				\
 } while (0)
 
@@ -96,7 +96,7 @@ do {								\
 do {								\
 	__auto_type __flags = BUGFLAG_WARNING|(flags);		\
 	instrumentation_begin();				\
-	_BUG_FLAGS(ASM_UD2, __flags, ANNOTATE_REACHABLE(1b));	\
+	_BUG_FLAGS(cond_str, ASM_UD2, __flags, ANNOTATE_REACHABLE(1b)); \
 	instrumentation_end();					\
 } while (0)
 
Signature · 1 line
2.45.2
#4 lore raw

PATCH 4/5 bugs/x86: Augment warnings output by concatenating 'cond_str' with the regular __FILE__ string in _BUG_FLAGS()

In reply to Ingo Molnar

This allows the reuse of the UD2 based 'struct bug_entry' low-overhead _BUG_FLAGS() implementation and string-printing backend, without having to add a new field.

An example:

If we have the following WARN_ON_ONCE() in kernel/sched/core.c:

	WARN_ON_ONCE(idx < 0 && ptr);

Then previously _BUG_FLAGS() would store this string in bug_entry::file:

"kernel/sched/core.c"

After this patch, it would store and print:

	"[idx < 0 && ptr] kernel/sched/core.c"

Which is an extended string that will be printed in warnings.

---
 arch/x86/include/asm/bug.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/bug.h +1 -1
index aff1c6b7a7f3..e966199c8ef7 100644
@@ -50,7 +50,7 @@ do {									\
 		     "\t.org 2b+%c3\n"					\
 		     ".popsection\n"					\
 		     extra						\
-		     : : "i" (__FILE__), "i" (__LINE__),		\
+		     : : "i" (cond_str __FILE__), "i" (__LINE__),		\
 			 "i" (flags),					\
 			 "i" (sizeof(struct bug_entry)));		\
 } while (0)
Signature · 1 line
2.45.2
maintainer
#5 lore raw

Re: [PATCH 4/5] bugs/x86: Augment warnings output by concatenating 'cond_str' with the regular __FILE__ string in _BUG_FLAGS()

Quoted 30 lines · Ingo Molnar

This allows the reuse of the UD2 based 'struct bug_entry' low-overhead _BUG_FLAGS() implementation and string-printing backend, without having to add a new field.

An example:

If we have the following WARN_ON_ONCE() in kernel/sched/core.c:

	WARN_ON_ONCE(idx < 0 && ptr);

Then previously _BUG_FLAGS() would store this string in bug_entry::file:

"kernel/sched/core.c"

After this patch, it would store and print:

	"[idx < 0 && ptr] kernel/sched/core.c"

Which is an extended string that will be printed in warnings.

---
 arch/x86/include/asm/bug.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/bug.h +0 -0
index aff1c6b7a7f3..e966199c8ef7 100644

arch/x86/include/asm/bug.h do { \ · line 56

Quoted 8 lines
arch/x86/include/asm/bug.h +1 -1
@@ -50,7 +50,7 @@ do {									\
 		     "\t.org 2b+%c3\n"					\
 		     ".popsection\n"					\
 		     extra						\
-		     : : "i" (__FILE__), "i" (__LINE__),		\
+		     : : "i" (cond_str __FILE__), "i" (__LINE__),		\
 			 "i" (flags),					\
 			 "i" (sizeof(struct bug_entry)));		\
 } while (0)

Sneaky :-) Do we want to touch up all the other archs? I mean, you already touched them anyway earlier in the series in order to push this argument through.

#6 lore raw

[COMBO PATCH 6/5] bugs/arch: Wire in the 'cond_str' string to the WARN/BUG output machinery of PowerPC, LoongArch, S390, RISC-V, PA-RISC and SH

Quoted 35 lines · Peter Zijlstra
Quoted 34 lines · Ingo Molnar

This allows the reuse of the UD2 based 'struct bug_entry' low-overhead _BUG_FLAGS() implementation and string-printing backend, without having to add a new field.

An example:

If we have the following WARN_ON_ONCE() in kernel/sched/core.c:

	WARN_ON_ONCE(idx < 0 && ptr);

Then previously _BUG_FLAGS() would store this string in bug_entry::file:

"kernel/sched/core.c"

After this patch, it would store and print:

	"[idx < 0 && ptr] kernel/sched/core.c"

Which is an extended string that will be printed in warnings.

---
 arch/x86/include/asm/bug.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/bug.h +0 -1
index aff1c6b7a7f3..e966199c8ef7 100644
@@ -50,7 +50,7 @@ do {									\
 		     "\t.org 2b+%c3\n"					\
 		     ".popsection\n"					\
 		     extra						\
-		     : : "i" (__FILE__), "i" (__LINE__),		\
Quoted 8 lines

arch/x86/include/asm/bug.h do { \ · line 56

Quoted 4 lines
arch/x86/include/asm/bug.h +1 -0
+		     : : "i" (cond_str __FILE__), "i" (__LINE__),		\
 			 "i" (flags),					\
 			 "i" (sizeof(struct bug_entry)));		\
 } while (0)

Sneaky :-) Do we want to touch up all the other archs? I mean, you already touched them anyway earlier in the series in order to push this argument through.

Sneaky how you make it sound so simple ;-)

... some time later:

c9bb718f4d8a bugs/sh: Use 'cond_str' in __WARN_FLAGS() 6fd6983325f7 bugs/parisc: Use 'cond_str' in __WARN_FLAGS() cc6f8cdc5438 bugs/riscv: Pass in 'cond_str' to __BUG_FLAGS() and use it b2becbe8b469 bugs/s390: Pass in 'cond_str' to __EMIT_BUG() and use it 8d1deb72c07f bugs/LoongArch: Pass in 'cond_str' to __BUG_ENTRY() and use it f4a1a3f7f1bb bugs/powerpc: Pass in 'cond_str' to BUG_ENTRY() and use it

There were like 5 separate variants of how architectures make use of __WARN_FLAGS(), and the 6 patches above are totally untested. Combo patch below.

Thanks,

Ingo

============================>

 arch/loongarch/include/asm/bug.h | 22 +++++++++++-----------
 arch/parisc/include/asm/bug.h    |  2 +-
 arch/powerpc/include/asm/bug.h   | 12 ++++++------
 arch/riscv/include/asm/bug.h     | 10 +++++-----
 arch/s390/include/asm/bug.h      | 10 +++++-----
 arch/sh/include/asm/bug.h        |  2 +-
 6 files changed, 29 insertions(+), 29 deletions(-)
arch/loongarch/include/asm/bug.h +11 -11
index 51c2cb98d728..b8b4d9f569c1 100644
@@ -20,39 +20,39 @@
 #endif
 
 #ifndef CONFIG_GENERIC_BUG
-#define __BUG_ENTRY(flags)
+#define __BUG_ENTRY(cond_str, flags)
 #else
-#define __BUG_ENTRY(flags) 					\
+#define __BUG_ENTRY(cond_str, flags)				\
 		.pushsection __bug_table, "aw";			\
 		.align 2;					\
 	10000:	.long 10001f - .;				\
-		_BUGVERBOSE_LOCATION(__FILE__, __LINE__)	\
-		.short flags; 					\
+		_BUGVERBOSE_LOCATION(cond_str __FILE__, __LINE__) \
+		.short flags;					\
 		.popsection;					\
 	10001:
 #endif
 
-#define ASM_BUG_FLAGS(flags)					\
-	__BUG_ENTRY(flags)					\
+#define ASM_BUG_FLAGS(cond_str, flags)				\
+	__BUG_ENTRY(cond_str, flags)				\
 	break		BRK_BUG;
 
-#define ASM_BUG()	ASM_BUG_FLAGS(0)
+#define ASM_BUG()	ASM_BUG_FLAGS("", 0)
 
-#define __BUG_FLAGS(flags, extra)					\
-	asm_inline volatile (__stringify(ASM_BUG_FLAGS(flags))		\
+#define __BUG_FLAGS(cond_str, flags, extra)					\
+	asm_inline volatile (__stringify(ASM_BUG_FLAGS(cond_str, flags))	\
 			     extra);
 
 #define __WARN_FLAGS(cond_str, flags)				\
 do {								\
 	instrumentation_begin();				\
-	__BUG_FLAGS(BUGFLAG_WARNING|(flags), ANNOTATE_REACHABLE(10001b));\
+	__BUG_FLAGS(cond_str, BUGFLAG_WARNING|(flags), ANNOTATE_REACHABLE(10001b));\
 	instrumentation_end();					\
 } while (0)
 
 #define BUG()							\
 do {								\
 	instrumentation_begin();				\
-	__BUG_FLAGS(0, "");					\
+	__BUG_FLAGS("", 0, "");					\
 	unreachable();						\
 } while (0)
 
arch/parisc/include/asm/bug.h +1 -1
index 1a87cf80ec3c..2d14d6cf21f3 100644
@@ -61,7 +61,7 @@
 			     "\t.short %1, %2\n"			\
 			     "\t.blockz %3-2*4-2*2\n"			\
 			     "\t.popsection"				\
-			     : : "i" (__FILE__), "i" (__LINE__),	\
+			     : : "i" (cond_str __FILE__), "i" (__LINE__), \
 			     "i" (BUGFLAG_WARNING|(flags)),		\
 			     "i" (sizeof(struct bug_entry)) );		\
 	} while(0)
arch/powerpc/include/asm/bug.h +6 -6
index 34d39ec79720..a2120fbedd09 100644
@@ -51,11 +51,11 @@
 	".previous\n"
 #endif
 
-#define BUG_ENTRY(insn, flags, ...)			\
+#define BUG_ENTRY(cond_str, insn, flags, ...)		\
 	__asm__ __volatile__(				\
 		"1:	" insn "\n"			\
 		_EMIT_BUG_ENTRY				\
-		: : "i" (__FILE__), "i" (__LINE__),	\
+		: : "i" (cond_str __FILE__), "i" (__LINE__),	\
 		  "i" (flags),				\
 		  "i" (sizeof(struct bug_entry)),	\
 		  ##__VA_ARGS__)
@@ -67,12 +67,12 @@
  */
 
 #define BUG() do {						\
-	BUG_ENTRY("twi 31, 0, 0", 0);				\
+	BUG_ENTRY("", "twi 31, 0, 0", 0);			\
 	unreachable();						\
 } while (0)
 #define HAVE_ARCH_BUG
 
-#define __WARN_FLAGS(cond_str, flags) BUG_ENTRY("twi 31, 0, 0", BUGFLAG_WARNING | (flags))
+#define __WARN_FLAGS(cond_str, flags) BUG_ENTRY(cond_str, "twi 31, 0, 0", BUGFLAG_WARNING | (flags))
 
 #ifdef CONFIG_PPC64
 #define BUG_ON(x) do {						\
@@ -80,7 +80,7 @@
 		if (x)						\
 			BUG();					\
 	} else {						\
-		BUG_ENTRY(PPC_TLNEI " %4, 0", 0, "r" ((__force long)(x)));	\
+		BUG_ENTRY(#x, PPC_TLNEI " %4, 0", 0, "r" ((__force long)(x)));	\
 	}							\
 } while (0)
 
@@ -90,7 +90,7 @@
 		if (__ret_warn_on)				\
 			__WARN();				\
 	} else {						\
-		BUG_ENTRY(PPC_TLNEI " %4, 0",			\
+		BUG_ENTRY(#x, PPC_TLNEI " %4, 0",			\
 			  BUGFLAG_WARNING | BUGFLAG_TAINT(TAINT_WARN),	\
 			  "r" (__ret_warn_on));	\
 	}							\
arch/riscv/include/asm/bug.h +5 -5
index b22ee4d2c882..6278523dd2d1 100644
@@ -50,7 +50,7 @@ typedef u32 bug_insn_t;
 #endif
 
 #ifdef CONFIG_GENERIC_BUG
-#define __BUG_FLAGS(flags)					\
+#define __BUG_FLAGS(cond_str, flags)				\
 do {								\
 	__asm__ __volatile__ (					\
 		"1:\n\t"					\
@@ -61,22 +61,22 @@ do {								\
 			".org 2b + %3\n\t"                      \
 			".popsection"				\
 		:						\
-		: "i" (__FILE__), "i" (__LINE__),		\
+		: "i" (cond_str __FILE__), "i" (__LINE__),	\
 		  "i" (flags),					\
 		  "i" (sizeof(struct bug_entry)));              \
 } while (0)
 #else /* CONFIG_GENERIC_BUG */
-#define __BUG_FLAGS(flags) do {					\
+#define __BUG_FLAGS(cond_str, flags) do {			\
 	__asm__ __volatile__ ("ebreak\n");			\
 } while (0)
 #endif /* CONFIG_GENERIC_BUG */
 
 #define BUG() do {						\
-	__BUG_FLAGS(0);						\
+	__BUG_FLAGS("", 0);					\
 	unreachable();						\
 } while (0)
 
-#define __WARN_FLAGS(cond_str, flags) __BUG_FLAGS(BUGFLAG_WARNING|(flags))
+#define __WARN_FLAGS(cond_str, flags) __BUG_FLAGS(cond_str, BUGFLAG_WARNING|(flags))
 
 #define HAVE_ARCH_BUG
 
arch/s390/include/asm/bug.h +5 -5
index ef3e495ec1e3..e3d839517c17 100644
@@ -8,7 +8,7 @@
 
 #ifdef CONFIG_DEBUG_BUGVERBOSE
 
-#define __EMIT_BUG(x) do {					\
+#define __EMIT_BUG(cond_str, x) do {				\
 	asm_inline volatile(					\
 		"0:	mc	0,0\n"				\
 		".section .rodata.str,\"aMS\",@progbits,1\n"	\
@@ -20,14 +20,14 @@
 		"	.short	%0,%1\n"			\
 		"	.org	2b+%2\n"			\
 		".previous\n"					\
-		: : "i" (__LINE__),				\
+		: : "i" (cond_str __LINE__),			\
 		    "i" (x),					\
 		    "i" (sizeof(struct bug_entry)));		\
 } while (0)
 
 #else /* CONFIG_DEBUG_BUGVERBOSE */
 
-#define __EMIT_BUG(x) do {					\
+#define __EMIT_BUG(cond_str, x) do {				\
 	asm_inline volatile(					\
 		"0:	mc	0,0\n"				\
 		".section __bug_table,\"aw\"\n"			\
@@ -42,12 +42,12 @@
 #endif /* CONFIG_DEBUG_BUGVERBOSE */
 
 #define BUG() do {					\
-	__EMIT_BUG(0);					\
+	__EMIT_BUG("", 0);				\
 	unreachable();					\
 } while (0)
 
 #define __WARN_FLAGS(cond_str, flags) do {		\
-	__EMIT_BUG(BUGFLAG_WARNING|(flags));		\
+	__EMIT_BUG(cond_str, BUGFLAG_WARNING|(flags));	\
 } while (0)
 
 #define WARN_ON(x) ({					\
arch/sh/include/asm/bug.h +1 -1
index 834c621ab249..20d5220bf452 100644
@@ -59,7 +59,7 @@ do {							\
 		 _EMIT_BUG_ENTRY			\
 		 :					\
 		 : "n" (TRAPA_BUG_OPCODE),		\
-		   "i" (__FILE__),			\
+		   "i" (cond_str __FILE__),		\
 		   "i" (__LINE__),			\
 		   "i" (BUGFLAG_WARNING|(flags)),	\
 		   "i" (sizeof(struct bug_entry)));	\
#7 lore raw

Re: [COMBO PATCH 6/5] bugs/arch: Wire in the 'cond_str' string to the WARN/BUG output machinery of PowerPC, LoongArch, S390, RISC-V, PA-RISC and SH

Quoted 37 lines · Ingo Molnar

... some time later:

b2becbe8b469 bugs/s390: Pass in 'cond_str' to __EMIT_BUG() and use it

arch/s390/include/asm/bug.h +3 -4
@@ -8,7 +8,7 @@
 
 #ifdef CONFIG_DEBUG_BUGVERBOSE
 
-#define __EMIT_BUG(x) do {					\
+#define __EMIT_BUG(cond_str, x) do {				\
 	asm_inline volatile(					\
 		"0:	mc	0,0\n"				\
 		".section .rodata.str,\"aMS\",@progbits,1\n"	\
@@ -20,14 +20,14 @@
 		"	.short	%0,%1\n"			\
 		"	.org	2b+%2\n"			\
 		".previous\n"					\
-		: : "i" (__LINE__),				\
+		: : "i" (cond_str __LINE__),			\
 		    "i" (x),					\
 		    "i" (sizeof(struct bug_entry)));		\
 } while (0)
 
 #else /* CONFIG_DEBUG_BUGVERBOSE */
 
-#define __EMIT_BUG(x) do {					\
+#define __EMIT_BUG(cond_str, x) do {				\
 	asm_inline volatile(					\
 		"0:	mc	0,0\n"				\
 		".section __bug_table,\"aw\"\n"			\
@@ -42,12 +42,12 @@
 #endif /* CONFIG_DEBUG_BUGVERBOSE */
 
 #define BUG() do {					\
-	__EMIT_BUG(0);					\

arch/s390/include/asm/bug.h line 53

Quoted 10 lines
arch/s390/include/asm/bug.h +2 -1
+	__EMIT_BUG("", 0);				\
 	unreachable();					\
 } while (0)
 
 #define __WARN_FLAGS(cond_str, flags) do {		\
-	__EMIT_BUG(BUGFLAG_WARNING|(flags));		\
+	__EMIT_BUG(cond_str, BUGFLAG_WARNING|(flags));	\
 } while (0)
 
 #define WARN_ON(x) ({					\

So the fix below makes it build on S390, but it won't link:

kernel/exit.o:(__bug_table+0x8): relocation truncated to fit: R_390_16 against .rodata.str1.2' kernel/exit.o:(__bug_table+0x14): relocation truncated to fit: R_390_16 against .rodata.str1.2' kernel/exit.o:(__bug_table+0x20): relocation truncated to fit: R_390_16 against .rodata.str1.2' kernel/exit.o:(__bug_table+0x2c): relocation truncated to fit: R_390_16 against .rodata.str1.2' kernel/exit.o:(__bug_table+0x38): relocation truncated to fit: R_390_16 against .rodata.str1.2' kernel/exit.o:(__bug_table+0x44): relocation truncated to fit: R_390_16 against .rodata.str1.2' kernel/exit.o:(__bug_table+0x50): relocation truncated to fit: R_390_16 against .rodata.str1.2' init/main.o:(__bug_table+0x8): relocation truncated to fit: R_390_16 against .rodata.str1.2' init/main.o:(__bug_table+0x14): relocation truncated to fit: R_390_16 against .rodata.str1.2' init/main.o:(__bug_table+0x20): relocation truncated to fit: R_390_16 against .rodata.str1.2' init/main.o:(__bug_table+0x2c): additional relocation overflows omitted from the output

From the _16 name my rough guess is that the larger string table has overflown 16 bits (64k)?

At which point I'd much rather go back to plan-A: pass in cond_str to __WARN_FLAGS(), and wash my hands and leave architectures to make use of them as they wish to. :-)

Thanks,

Ingo

=====================>

 arch/s390/include/asm/bug.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/s390/include/asm/bug.h +1 -1
index e3d839517c17..7eb9b44f78cb 100644
@@ -20,7 +20,7 @@
 		"	.short	%0,%1\n"			\
 		"	.org	2b+%2\n"			\
 		".previous\n"					\
-		: : "i" (cond_str __LINE__),			\
+		: : "i" (__stringify(cond_str __LINE__)),	\
 		    "i" (x),					\
 		    "i" (sizeof(struct bug_entry)));		\
 } while (0)
#8 lore raw

Re: [PATCH 4/5] bugs/x86: Augment warnings output by concatenating 'cond_str' with the regular __FILE__ string in _BUG_FLAGS()

In reply to Peter Zijlstra
Quoted 26 lines · Peter Zijlstra
Quoted 25 lines · Ingo Molnar

This allows the reuse of the UD2 based 'struct bug_entry' low-overhead _BUG_FLAGS() implementation and string-printing backend, without having to add a new field.

An example:

If we have the following WARN_ON_ONCE() in kernel/sched/core.c:

	WARN_ON_ONCE(idx < 0 && ptr);

Then previously _BUG_FLAGS() would store this string in bug_entry::file:

"kernel/sched/core.c"

After this patch, it would store and print:

	"[idx < 0 && ptr] kernel/sched/core.c"

Which is an extended string that will be printed in warnings.

---
 arch/x86/include/asm/bug.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines

arch/x86/include/asm/bug.h do { \ · line 56

Quoted 13 lines
arch/x86/include/asm/bug.h +1 -1
index aff1c6b7a7f3..e966199c8ef7 100644
@@ -50,7 +50,7 @@ do {									\
 		     "\t.org 2b+%c3\n"					\
 		     ".popsection\n"					\
 		     extra						\
-		     : : "i" (__FILE__), "i" (__LINE__),		\
+		     : : "i" (cond_str __FILE__), "i" (__LINE__),		\
 			 "i" (flags),					\
 			 "i" (sizeof(struct bug_entry)));		\
 } while (0)

Sneaky :-)

BTW., any reason why we go all the trouble with the bug_entry::line u16 number, instead of storing it in the bug_entry::file string with a :__LINE__ postfix or so?

Using 4 bytes doesn't even save any RAM, given that the average line position number within the kernel is around 3 bytes:

 $ for N in $(git grep -lE 'WARN_ON|BUG_ON|WARN\(|BUG\(' -- '*.[ch]'); do echo -n $(($(cat $N | wc -l)/2)) | wc -c; done | sort -n | uniq -c
     10 1
   1209 2
   6645 3
   1582 4
     10 5

( This is the histogram of the length of average line numbers within
the kernel's ~9,400 .[ch] source code files that are using these facilities. )

So concatenation would save on complexity, IMHO, and it would give us flexibility as well, if we passed in the string from higher layers. We wouldn't have to change architecture level code at all for this series for example.

Not to mention that some files within the kernel are beyond the 16-bit limit already, 38K to 222K lines of code:

starship:~/tip> wc -l drivers/gpu/drm/amd/include/asic_reg/dcn/dcn_3_2_0_sh_mask.h 222,948 drivers/gpu/drm/amd/include/asic_reg/dcn/dcn_3_2_0_sh_mask.h

starship:~/tip> wc -l crypto/testmgr.h
38,897 crypto/testmgr.h

So u16 line numbers are also a (minor) breakage waiting to happen.

Thanks,

Ingo

maintainer
#9 lore raw

Re: [PATCH 4/5] bugs/x86: Augment warnings output by concatenating 'cond_str' with the regular __FILE__ string in _BUG_FLAGS()

Quoted 16 lines · Ingo Molnar
Quoted 7 lines
Quoted 7 lines
arch/x86/include/asm/bug.h +0 -0
index aff1c6b7a7f3..e966199c8ef7 100644
@@ -50,7 +50,7 @@ do {									\
 		     "\t.org 2b+%c3\n"					\
 		     ".popsection\n"					\
Quoted 8 lines

arch/x86/include/asm/bug.h do { \ · line 56

Quoted 6 lines
arch/x86/include/asm/bug.h +1 -1
 		     extra						\
-		     : : "i" (__FILE__), "i" (__LINE__),		\
+		     : : "i" (cond_str __FILE__), "i" (__LINE__),		\
 			 "i" (flags),					\
 			 "i" (sizeof(struct bug_entry)));		\
 } while (0)

Sneaky :-)

Quoted 3 lines

BTW., any reason why we go all the trouble with the bug_entry::line u16 number, instead of storing it in the bug_entry::file string with a :__LINE__ postfix or so?

I have no clue; this is well before my time. But yes, that seems a viable option indeed.

#10 lore raw

Re: [PATCH 4/5] bugs/x86: Augment warnings output by concatenating 'cond_str' with the regular __FILE__ string in _BUG_FLAGS()

In reply to Ingo Molnar
Quoted 4 lines · Ingo Molnar

BTW., any reason why we go all the trouble with the bug_entry::line u16 number, instead of storing it in the bug_entry::file string with a :__LINE__ postfix or so?

The compiler will happily share the same storage for identical strings, so that was an issue: re-using the same memory for the same filename being repeated multiple times.

That obviously doesn't work anyway once you add the warning string to it, so that makes that whole argument go away.

Linus

#11 lore raw

Re: [PATCH 4/5] bugs/x86: Augment warnings output by concatenating 'cond_str' with the regular __FILE__ string in _BUG_FLAGS()

Quoted 9 lines · Linus Torvalds
Quoted 4 lines · Ingo Molnar

BTW., any reason why we go all the trouble with the bug_entry::line u16 number, instead of storing it in the bug_entry::file string with a :__LINE__ postfix or so?

The compiler will happily share the same storage for identical strings, so that was an issue: re-using the same memory for the same filename being repeated multiple times.

ohhh ... TIL.

Quoted 2 lines

That obviously doesn't work anyway once you add the warning string to it, so that makes that whole argument go away.

Yeah. Explains the +100K increase in .data as well, which was more than what I expected.

Thanks,

Ingo

#12 lore raw

PATCH 5/5 bugs/core: Do not print CPU and PID values in__warn() output

In reply to Ingo Molnar

For most warnings it's repeated anyway a few lines later, as part of the register dump:

  WARNING: CPU: 0 PID: 0 at kernel/sched/core.c:8511 sched_init+0x20/0x410
           ^^^^^^^^^^^^^
  Modules linked in:
  CPU: 0 UID: 0 PID: 0 Comm: swapper Not tainted 6.14.0-01616-g94d7af2844aa #4 PREEMPT(undef)
  ^^^^^^^^^^^^^^^^^^

In fact the later dump is richer, as it includes the 'comm' line as well.

As a side effect, this makes some space for the 'file' field to be augmented with extra information (the condition string in particular).

---
 kernel/panic.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
kernel/panic.c +2 -5
index d8635d5cecb2..3101c21ca39f 100644
@@ -725,12 +725,9 @@ void __warn(const char *file, int line, void *caller, unsigned taint,
 	disable_trace_on_warning();
 
 	if (file)
-		pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS\n",
-			raw_smp_processor_id(), current->pid, file, line,
-			caller);
+		pr_warn("WARNING: %s:%d at %pS\n", file, line, caller);
 	else
-		pr_warn("WARNING: CPU: %d PID: %d at %pS\n",
-			raw_smp_processor_id(), current->pid, caller);
+		pr_warn("WARNING: at %pS\n", caller);
 
 #pragma GCC diagnostic push
 #ifndef __clang__
Signature · 1 line
2.45.2
maintainer
#13 lore raw

Re: [PATCH 5/5] bugs/core: Do not print CPU and PID values in__warn() output

Quoted 25 lines · Ingo Molnar

For most warnings it's repeated anyway a few lines later, as part of the register dump:

  WARNING: CPU: 0 PID: 0 at kernel/sched/core.c:8511 sched_init+0x20/0x410
           ^^^^^^^^^^^^^
  Modules linked in:
  CPU: 0 UID: 0 PID: 0 Comm: swapper Not tainted 6.14.0-01616-g94d7af2844aa #4 PREEMPT(undef)
  ^^^^^^^^^^^^^^^^^^

In fact the later dump is richer, as it includes the 'comm' line as well.

As a side effect, this makes some space for the 'file' field to be augmented with extra information (the condition string in particular).

---
 kernel/panic.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
kernel/panic.c +0 -0
index d8635d5cecb2..3101c21ca39f 100644
@@ -725,12 +725,9 @@ void __warn(const char *file, int line, void *caller, unsigned taint,
 	disable_trace_on_warning();

kernel/panic.c void __warn(const char *file, int line, void *caller, unsigned taint, · line 730

Quoted 10 lines
kernel/panic.c +2 -5
 
 	if (file)
-		pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS\n",
-			raw_smp_processor_id(), current->pid, file, line,
-			caller);
+		pr_warn("WARNING: %s:%d at %pS\n", file, line, caller);
 	else
-		pr_warn("WARNING: CPU: %d PID: %d at %pS\n",
-			raw_smp_processor_id(), current->pid, caller);
+		pr_warn("WARNING: at %pS\n", caller);

Over the years I've had concurrent WARNs interleaved on the console. This duplicate information would allow untangling some of that.

#14 lore raw

Re: [PATCH 5/5] bugs/core: Do not print CPU and PID values in__warn() output

Quoted 27 lines · Peter Zijlstra
Quoted 26 lines · Ingo Molnar

For most warnings it's repeated anyway a few lines later, as part of the register dump:

  WARNING: CPU: 0 PID: 0 at kernel/sched/core.c:8511 sched_init+0x20/0x410
           ^^^^^^^^^^^^^
  Modules linked in:
  CPU: 0 UID: 0 PID: 0 Comm: swapper Not tainted 6.14.0-01616-g94d7af2844aa #4 PREEMPT(undef)
  ^^^^^^^^^^^^^^^^^^

In fact the later dump is richer, as it includes the 'comm' line as well.

As a side effect, this makes some space for the 'file' field to be augmented with extra information (the condition string in particular).

---
 kernel/panic.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
kernel/panic.c +0 -0
index d8635d5cecb2..3101c21ca39f 100644
@@ -725,12 +725,9 @@ void __warn(const char *file, int line, void *caller, unsigned taint,
 	disable_trace_on_warning();
 
Quoted 12 lines

kernel/panic.c void __warn(const char *file, int line, void *caller, unsigned taint, · line 730

Quoted 9 lines
kernel/panic.c +2 -5
 	if (file)
-		pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS\n",
-			raw_smp_processor_id(), current->pid, file, line,
-			caller);
+		pr_warn("WARNING: %s:%d at %pS\n", file, line, caller);
 	else
-		pr_warn("WARNING: CPU: %d PID: %d at %pS\n",
-			raw_smp_processor_id(), current->pid, caller);
+		pr_warn("WARNING: at %pS\n", caller);

Over the years I've had concurrent WARNs interleaved on the console. This duplicate information would allow untangling some of that.

Fair enough.

With patch #1 below:

- Reintroduced CPU and PID

- Added a '->comm[]' to the warning line: in practice it's often more valuable than a PID...

- Re-ordered the fields of information by value: higher value to lower value.

Here's an overview of the output variants (-v3 is the latestest):

-vanilla: WARNING: CPU: 0 PID: 0 at kernel/sched/core.c:8511 sched_init+0x20/0x410

 -v1:      WARNING: CPU: 0 PID: 0 at [ptr == 0 && 1] kernel/sched/core.c:8511 sched_init+0x20/0x410
 -v2:      WARNING: [ptr == 0 && 1] kernel/sched/core.c:8511 at sched_init+0x20/0x410
 -v3:      WARNING: [ptr == 0 && 1] kernel/sched/core.c:8511 at sched_init+0x20/0x410 CPU#0: swapper/0

I think I like -v3: a poor guy's single line trace entry in essence.

I suppose we'll have some worst-case situations with really complex conditions bloating the output when they trigger, but a quick git grep suggests that most WARN_ON_ONCE() instances are sane, in kernel/ at least.

Side note, found some WARN_ON_ONCE() weirdnesses:

  kernel/sched/syscalls.c:        WARN_ON_ONCE(sched_setscheduler_nocheck(p, SCHED_FIFO, &sp) != 0);
  kernel/sched/syscalls.c:        WARN_ON_ONCE(sched_setscheduler_nocheck(p, SCHED_FIFO, &sp) != 0);
  kernel/sched/syscalls.c:        WARN_ON_ONCE(sched_setattr_nocheck(p, &attr) != 0);

I don't think WARN_ON()s with complex side effects are considered kernel META these days, right? The pattern in patch #2 below is longer, but less surprising, IMHO.

Thanks,

Ingo

---
 kernel/panic.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
kernel/panic.c +9 -4
index 01746e89a7a1..6478dd70ec6d 100644
@@ -724,10 +724,15 @@ void __warn(const char *file, int line, void *caller, unsigned taint,
 
 	disable_trace_on_warning();
 
-	if (file)
-		pr_warn("WARNING: %s:%d at %pS\n", file, line, caller);
-	else
-		pr_warn("WARNING: at %pS\n", caller);
+	if (file) {
+		pr_warn("WARNING: %s:%d at %pS CPU#%d: %s/%d\n",
+			file, line, caller,
+			raw_smp_processor_id(), current->comm, current->pid);
+	} else {
+		pr_warn("WARNING: at %pS CPU#%d: %s/%d\n",
+			caller,
+			raw_smp_processor_id(), current->comm, current->pid);
+	}
 
 #pragma GCC diagnostic push
 #ifndef __clang__
 kernel/sched/syscalls.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
kernel/sched/syscalls.c +4 -1
index c326de1344fb..1a91231d3ad8 100644
@@ -846,7 +846,10 @@ int sched_setscheduler_nocheck(struct task_struct *p, int policy,
 void sched_set_fifo(struct task_struct *p)
 {
 	struct sched_param sp = { .sched_priority = MAX_RT_PRIO / 2 };
-	WARN_ON_ONCE(sched_setscheduler_nocheck(p, SCHED_FIFO, &sp) != 0);
+	int err;
+
+	err = sched_setscheduler_nocheck(p, SCHED_FIFO, &sp);
+	WARN_ON_ONCE(err);
 }
 EXPORT_SYMBOL_GPL(sched_set_fifo);
 
#15 lore raw
In reply to Ingo Molnar
Quoted 7 lines · Ingo Molnar

The cost is about +100K more .data on a defconfig kernel, and no runtime code generation impact:

       text       data        bss         dec        hex    filename
   29523998    7926322    1389904    38840224    250a7a0    vmlinux.x86.defconfig.before
   29523998    8024626    1389904    38938528    25227a0    vmlinue.x86.defconfig.after

That's quite a lot. I don't suppose the condition strings themselves are responsible for most of that; how much is due to the __FILE__ strings now no longer being deduplicated/shared between WARN instances in same file? How much harder would it be to add a new cond_str member to bug_entry, and how would the numbers look then?

Rasmus