Skip to contentSkip to conversation

x86: WARN() hackery

PATCH In review Peter Zijlstra 2 participants 22 messages Nov 10, 2025 – Nov 26, 2025

Subsystems (5)
lore
Share & tools
lore

Peter Zijlstra

author maintainer
#0 lore raw

v2 cover x86: WARN() hackery

Hi!

I was going through my patch cabinet the other day and found these patches.

Mark was going to see if he could make ARM64 play ball to see if the generic bits really are that. But clearly he's forgotten about things just as much as I had.

We should probably just merge these, as they improbe the code-gen for WARN() significantly on x86 and then I'll help Mark out for ARM64 when he gets around to it and needs a hand.

Freshly rebased and somewhat tested.

As a quick reminder, this makes WARN(), which has a vararg printf like arguments work with the #UD based WARN infra by hiding the format in the bug_table (its a constant after all) and replacing this first argument with the address of the bug-table entry while making the actual function that's called a UD1 instruction.

The exception handler then create a va_list from pt_regs and vprintk() does the rest.

Also here:

git://git.kernel.org/pub/scm/linux/kernel/git/peterz/queue.git x86/bugs

v1: https://lkml.kernel.org/r/[email protected]

author maintainer
#1 lore raw

PATCH v2 1/12 x86: Rework __bug_table helpers

Rework the __bug_table helpers such that extension becomes easier.

---
 arch/x86/include/asm/bug.h |   37 ++++++++++++++++++++-----------------
 1 file changed, 20 insertions(+), 17 deletions(-)
arch/x86/include/asm/bug.h +20 -17
@@ -37,39 +37,42 @@
 #ifdef CONFIG_GENERIC_BUG
 
 #ifdef CONFIG_X86_32
-# define __BUG_REL(val)	".long " val
+#define __BUG_REL(val)		".long " val
 #else
-# define __BUG_REL(val)	".long " val " - ."
+#define __BUG_REL(val)		".long " val " - ."
 #endif
 
 #ifdef CONFIG_DEBUG_BUGVERBOSE
-#define __BUG_ENTRY(file, line, flags)					\
-	"2:\t" __BUG_REL("1b") "\t# bug_entry::bug_addr\n"		\
+#define __BUG_ENTRY_VERBOSE(file, line)					\
 	"\t" __BUG_REL(file)   "\t# bug_entry::file\n"			\
-	"\t.word " line        "\t# bug_entry::line\n"			\
-	"\t.word " flags       "\t# bug_entry::flags\n"
+	"\t.word " line        "\t# bug_entry::line\n"
 #else
-#define __BUG_ENTRY(file, line, flags)					\
-	"2:\t" __BUG_REL("1b") "\t# bug_entry::bug_addr\n"		\
-	"\t.word " flags       "\t# bug_entry::flags\n"
+#define __BUG_ENTRY_VERBOSE(file, line)
 #endif
 
+#define __BUG_ENTRY(file, line, flags)					\
+	__BUG_REL("1b")		"\t# bug_entry::bug_addr\n"		\
+	__BUG_ENTRY_VERBOSE(file, line)					\
+	"\t.word " flags	"\t# bug_entry::flags\n"
+
 #define _BUG_FLAGS_ASM(ins, file, line, flags, size, extra)		\
 	"1:\t" ins "\n"							\
-	".pushsection __bug_table,\"aw\"\n"				\
+	".pushsection __bug_table,\"aw\"\n\t"				\
 	ANNOTATE_DATA_SPECIAL						\
+	"2:\n\t"							\
 	__BUG_ENTRY(file, line, flags)					\
 	"\t.org 2b + " size "\n"					\
 	".popsection\n"							\
 	extra
 
-#define _BUG_FLAGS(cond_str, ins, flags, extra)						\
-do {											\
-	asm_inline volatile(_BUG_FLAGS_ASM(ins, "%c0",					\
-					   "%c1", "%c2", "%c3", extra)			\
-		     : : "i" (WARN_CONDITION_STR(cond_str) __FILE__), "i" (__LINE__),	\
-			 "i" (flags),							\
-			 "i" (sizeof(struct bug_entry)));				\
+#define _BUG_FLAGS(cond_str, ins, flags, extra)				\
+do {									\
+	asm_inline volatile(_BUG_FLAGS_ASM(ins, "%c0",			\
+					   "%c1", "%c2", "%c3", extra)	\
+		   : : "i" (WARN_CONDITION_STR(cond_str) __FILE__),	\
+		       "i" (__LINE__),					\
+		       "i" (flags),					\
+		       "i" (sizeof(struct bug_entry)));			\
 } while (0)
 
 #define ARCH_WARN_ASM(file, line, flags, size)				\
author maintainer
#2 lore raw

PATCH v2 2/12 bug: Add BUG_FORMAT infrastructure

In reply to Peter Zijlstra

Add BUG_FORMAT; an architecture opt-in feature that allows adding the WARN_printf() format string to the bug_entry table.

---
 include/asm-generic/bug.h |    7 +++++++
 lib/bug.c                 |   39 ++++++++++++++++++++++++++++++++-------
 2 files changed, 39 insertions(+), 7 deletions(-)
include/asm-generic/bug.h +7 -0
@@ -36,6 +36,13 @@ struct bug_entry {
 #else
 	signed int	bug_addr_disp;
 #endif
+#ifdef HAVE_ARCH_BUG_FORMAT
+#ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
+	const char	*format;
+#else
+	signed int	format_disp;
+#endif
+#endif
 #ifdef CONFIG_DEBUG_BUGVERBOSE
 #ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
 	const char	*file;
lib/bug.c +32 -7
@@ -139,6 +139,19 @@ void bug_get_file_line(struct bug_entry
 #endif
 }
 
+static const char *bug_get_format(struct bug_entry *bug)
+{
+	const char *format = NULL;
+#ifdef HAVE_ARCH_BUG_FORMAT
+#ifdef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
+	format = (const char *)&bug->format_disp + bug->format_disp;
+#else
+	format = bug->format;
+#endif
+#endif
+	return format;
+}
+
 struct bug_entry *find_bug(unsigned long bugaddr)
 {
 	struct bug_entry *bug;
@@ -150,11 +163,19 @@ struct bug_entry *find_bug(unsigned long
 	return module_find_bug(bugaddr);
 }
 
+static void __warn_printf(const char *fmt)
+{
+	if (!fmt)
+		return;
+
+	printk("%s", fmt);
+}
+
 static enum bug_trap_type __report_bug(unsigned long bugaddr, struct pt_regs *regs)
 {
-	struct bug_entry *bug;
-	const char *file;
-	unsigned line, warning, once, done;
+	bool warning, once, done, no_cut, has_args;
+	const char *file, *fmt;
+	unsigned line;
 
 	if (!is_valid_bugaddr(bugaddr))
 		return BUG_TRAP_TYPE_NONE;
@@ -166,10 +187,12 @@ static enum bug_trap_type __report_bug(u
 	disable_trace_on_warning();
 
 	bug_get_file_line(bug, &file, &line);
+	fmt = bug_get_format(bug);
 
-	warning = (bug->flags & BUGFLAG_WARNING) != 0;
-	once = (bug->flags & BUGFLAG_ONCE) != 0;
-	done = (bug->flags & BUGFLAG_DONE) != 0;
+	warning  = bug->flags & BUGFLAG_WARNING;
+	once     = bug->flags & BUGFLAG_ONCE;
+	done     = bug->flags & BUGFLAG_DONE;
+	no_cut   = bug->flags & BUGFLAG_NO_CUT_HERE;
 
 	if (warning && once) {
 		if (done)
@@ -187,8 +210,10 @@ static enum bug_trap_type __report_bug(u
 	 * "cut here" line now. WARN() issues its own "cut here" before the
 	 * extra debugging message it writes before triggering the handler.
 	 */
-	if ((bug->flags & BUGFLAG_NO_CUT_HERE) == 0)
+	if (!no_cut) {
 		printk(KERN_DEFAULT CUT_HERE);
+		__warn_printf(fmt);
+	}
 
 	if (warning) {
 		/* this is a WARN_ON rather than BUG/BUG_ON */
author maintainer
#3 lore raw

PATCH v2 3/12 bug: Clean up CONFIG_GENERIC_BUG_RELATIVE_POINTERS

In reply to Peter Zijlstra

Three repeated CONFIG_GENERIC_BUG_RELATIVE_POINTERS #ifdefs right after one another yields unreadable code. Add a helper.

---
 include/asm-generic/bug.h |   22 ++++++++--------------
 1 file changed, 8 insertions(+), 14 deletions(-)
include/asm-generic/bug.h +8 -14
@@ -30,26 +30,20 @@ void __warn(const char *file, int line,
 
 #ifdef CONFIG_BUG
 
-#ifdef CONFIG_GENERIC_BUG
-struct bug_entry {
 #ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
-	unsigned long	bug_addr;
+#define BUG_REL(type, name) type name
 #else
-	signed int	bug_addr_disp;
+#define BUG_REL(type, name) signed int name##_disp
 #endif
+
+#ifdef CONFIG_GENERIC_BUG
+struct bug_entry {
+	BUG_REL(unsigned long, bug_addr);
 #ifdef HAVE_ARCH_BUG_FORMAT
-#ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
-	const char	*format;
-#else
-	signed int	format_disp;
-#endif
+	BUG_REL(const char *, format);
 #endif
 #ifdef CONFIG_DEBUG_BUGVERBOSE
-#ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
-	const char	*file;
-#else
-	signed int	file_disp;
-#endif
+	BUG_REL(const char *, file);
 	unsigned short	line;
 #endif
 	unsigned short	flags;
author maintainer
#4 lore raw

PATCH v2 4/12 bug: Add BUG_FORMAT_ARGS infrastructure

In reply to Peter Zijlstra

Add BUG_FORMAT_ARGS; when an architecture is able to provide a va_list given pt_regs, use this to print format arguments.

---
 include/asm-generic/bug.h |    1 +
 lib/bug.c                 |   17 +++++++++++++++--
 2 files changed, 16 insertions(+), 2 deletions(-)
include/asm-generic/bug.h +1 -0
@@ -13,6 +13,7 @@
 #define BUGFLAG_ONCE		(1 << 1)
 #define BUGFLAG_DONE		(1 << 2)
 #define BUGFLAG_NO_CUT_HERE	(1 << 3)	/* CUT_HERE already sent */
+#define BUGFLAG_ARGS		(1 << 4)
 #define BUGFLAG_TAINT(taint)	((taint) << 8)
 #define BUG_GET_TAINT(bug)	((bug)->flags >> 8)
 #endif
lib/bug.c +15 -2
@@ -163,11 +163,23 @@ struct bug_entry *find_bug(unsigned long
 	return module_find_bug(bugaddr);
 }
 
-static void __warn_printf(const char *fmt)
+static void __warn_printf(const char *fmt, struct pt_regs *regs)
 {
 	if (!fmt)
 		return;
 
+#ifdef HAVE_ARCH_BUG_FORMAT_ARGS
+	if (regs) {
+		struct arch_va_list _args;
+		va_list *args = __warn_args(&_args, regs);
+
+		if (args) {
+			vprintk(fmt, *args);
+			return;
+		}
+	}
+#endif
+
 	printk("%s", fmt);
 }
 
@@ -193,6 +205,7 @@ static enum bug_trap_type __report_bug(u
 	once     = bug->flags & BUGFLAG_ONCE;
 	done     = bug->flags & BUGFLAG_DONE;
 	no_cut   = bug->flags & BUGFLAG_NO_CUT_HERE;
+	has_args = bug->flags & BUGFLAG_ARGS;
 
 	if (warning && once) {
 		if (done)
@@ -212,7 +225,7 @@ static enum bug_trap_type __report_bug(u
 	 */
 	if (!no_cut) {
 		printk(KERN_DEFAULT CUT_HERE);
-		__warn_printf(fmt);
+		__warn_printf(fmt, has_args ? regs : NULL);
 	}
 
 	if (warning) {
author maintainer
#5 lore raw

PATCH v2 5/12 bug: Add report_bug_entry()

In reply to Peter Zijlstra

Add a report_bug() variant where the bug_entry is already known. This is useful when the exception instruction is not instantiated per-site. But instead has a single instance. In such a case the bug_entry address might be passed along in a known register or something.

---
 include/linux/bug.h |    1 +
 lib/bug.c           |   32 +++++++++++++++++++++++---------
 2 files changed, 24 insertions(+), 9 deletions(-)
include/linux/bug.h +1 -0
@@ -42,6 +42,7 @@ void bug_get_file_line(struct bug_entry
 struct bug_entry *find_bug(unsigned long bugaddr);
 
 enum bug_trap_type report_bug(unsigned long bug_addr, struct pt_regs *regs);
+enum bug_trap_type report_bug_entry(struct bug_entry *bug, struct pt_regs *regs);
 
 /* These are defined by the architecture */
 int is_valid_bugaddr(unsigned long addr);
lib/bug.c +22 -8
@@ -183,18 +183,20 @@ static void __warn_printf(const char *fm
 	printk("%s", fmt);
 }
 
-static enum bug_trap_type __report_bug(unsigned long bugaddr, struct pt_regs *regs)
+static enum bug_trap_type __report_bug(struct bug_entry *bug, unsigned long bugaddr, struct pt_regs *regs)
 {
 	bool warning, once, done, no_cut, has_args;
 	const char *file, *fmt;
 	unsigned line;
 
-	if (!is_valid_bugaddr(bugaddr))
-		return BUG_TRAP_TYPE_NONE;
-
-	bug = find_bug(bugaddr);
-	if (!bug)
-		return BUG_TRAP_TYPE_NONE;
+	if (!bug) {
+		if (!is_valid_bugaddr(bugaddr))
+			return BUG_TRAP_TYPE_NONE;
+
+		bug = find_bug(bugaddr);
+		if (!bug)
+			return BUG_TRAP_TYPE_NONE;
+	}
 
 	disable_trace_on_warning();
 
@@ -244,13 +246,25 @@ static enum bug_trap_type __report_bug(u
 	return BUG_TRAP_TYPE_BUG;
 }
 
+enum bug_trap_type report_bug_entry(struct bug_entry *bug, struct pt_regs *regs)
+{
+	enum bug_trap_type ret;
+	bool rcu = false;
+
+	rcu = warn_rcu_enter();
+	ret = __report_bug(bug, 0, regs);
+	warn_rcu_exit(rcu);
+
+	return ret;
+}
+
 enum bug_trap_type report_bug(unsigned long bugaddr, struct pt_regs *regs)
 {
 	enum bug_trap_type ret;
 	bool rcu = false;
 
 	rcu = warn_rcu_enter();
-	ret = __report_bug(bugaddr, regs);
+	ret = __report_bug(NULL, bugaddr, regs);
 	warn_rcu_exit(rcu);
 
 	return ret;
author maintainer
#6 lore raw

PATCH v2 6/12 bug: Implement WARN_ON() using __WARN_FLAGS()

In reply to Peter Zijlstra

This completes 3bc3c9c3ab6d ("bugs/core: Pass down the condition string of WARN_ON_ONCE(cond) warnings to __WARN_FLAGS()") and makes WARN_ON() and WARN_ON_ONCE() behaviour consistent.

---
 include/asm-generic/bug.h |   24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)
include/asm-generic/bug.h +17 -7
@@ -109,19 +109,29 @@ extern __printf(1, 2) void __warn_printk
 	} while (0)
 #else
 #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));\
 		instrumentation_end();					\
 	} while (0)
-#define WARN_ON_ONCE(condition) ({				\
-	int __ret_warn_on = !!(condition);			\
-	if (unlikely(__ret_warn_on))				\
-		__WARN_FLAGS("["#condition"] ",			\
-			     BUGFLAG_ONCE |			\
-			     BUGFLAG_TAINT(TAINT_WARN));	\
-	unlikely(__ret_warn_on);				\
+
+#define WARN_ON(condition) ({						\
+	int __ret_warn_on = !!(condition);				\
+	if (unlikely(__ret_warn_on))					\
+		__WARN_FLAGS("["#condition"] ",				\
+			     BUGFLAG_TAINT(TAINT_WARN));		\
+	unlikely(__ret_warn_on);					\
+})
+
+#define WARN_ON_ONCE(condition) ({					\
+	int __ret_warn_on = !!(condition);				\
+	if (unlikely(__ret_warn_on))					\
+		__WARN_FLAGS("["#condition"] ",				\
+			     BUGFLAG_ONCE |				\
+			     BUGFLAG_TAINT(TAINT_WARN));		\
+	unlikely(__ret_warn_on);					\
 })
 #endif
 
author maintainer
#7 lore raw

PATCH v2 7/12 bug: Allow architectures to provide __WARN_printf()

In reply to Peter Zijlstra

In addition to providing __WARN_FLAGS(), allow an architecture to also provide __WARN_printf().

---
 include/asm-generic/bug.h |   37 ++++++++++++++++++++++---------------
 1 file changed, 22 insertions(+), 15 deletions(-)
include/asm-generic/bug.h +22 -15
@@ -100,23 +100,9 @@ void warn_slowpath_fmt(const char *file,
 		       const char *fmt, ...);
 extern __printf(1, 2) void __warn_printk(const char *fmt, ...);
 
-#ifndef __WARN_FLAGS
-#define __WARN()		__WARN_printf(TAINT_WARN, NULL)
-#define __WARN_printf(taint, arg...) do {				\
-		instrumentation_begin();				\
-		warn_slowpath_fmt(__FILE__, __LINE__, taint, arg);	\
-		instrumentation_end();					\
-	} while (0)
-#else
+#ifdef __WARN_FLAGS
 #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));\
-		instrumentation_end();					\
-	} while (0)
-
 #define WARN_ON(condition) ({						\
 	int __ret_warn_on = !!(condition);				\
 	if (unlikely(__ret_warn_on))					\
@@ -135,6 +121,27 @@ extern __printf(1, 2) void __warn_printk
 })
 #endif
 
+#if defined(__WARN_FLAGS) && !defined(__WARN_printf)
+#define __WARN_printf(taint, arg...) do {				\
+		instrumentation_begin();				\
+		__warn_printk(arg);					\
+		__WARN_FLAGS("", BUGFLAG_NO_CUT_HERE | BUGFLAG_TAINT(taint));\
+		instrumentation_end();					\
+	} while (0)
+#endif
+
+#ifndef __WARN_printf
+#define __WARN_printf(taint, arg...) do {				\
+		instrumentation_begin();				\
+		warn_slowpath_fmt(__FILE__, __LINE__, taint, arg);	\
+		instrumentation_end();					\
+	} while (0)
+#endif
+
+#ifndef __WARN
+#define __WARN()		__WARN_printf(TAINT_WARN, NULL)
+#endif
+
 /* used internally by panic.c */
 
 #ifndef WARN_ON
author maintainer
#8 lore raw

PATCH v2 8/12 x86/bug: Add BUG_FORMAT basics

In reply to Peter Zijlstra

Opt-in to BUG_FORMAT for x86_64, adjust the BUGTABLE helper and for now, just store NULL pointers.

---
 arch/x86/include/asm/bug.h |   31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)
arch/x86/include/asm/bug.h +21 -10
@@ -50,33 +50,44 @@
 #define __BUG_ENTRY_VERBOSE(file, line)
 #endif
 
-#define __BUG_ENTRY(file, line, flags)					\
+#if defined(CONFIG_X86_64) || defined(CONFIG_DEBUG_BUGVERBOSE_DETAILED)
+#define HAVE_ARCH_BUG_FORMAT
+#define __BUG_ENTRY_FORMAT(format)					\
+	"\t" __BUG_REL(format)	"\t# bug_entry::format\n"
+#else
+#define __BUG_ENTRY_FORMAT(format)
+#endif
+
+#define __BUG_ENTRY(format, file, line, flags)				\
 	__BUG_REL("1b")		"\t# bug_entry::bug_addr\n"		\
+	__BUG_ENTRY_FORMAT(format)					\
 	__BUG_ENTRY_VERBOSE(file, line)					\
 	"\t.word " flags	"\t# bug_entry::flags\n"
 
-#define _BUG_FLAGS_ASM(ins, file, line, flags, size, extra)		\
+#define _BUG_FLAGS_ASM(ins, format, file, line, flags, size, extra)	\
 	"1:\t" ins "\n"							\
 	".pushsection __bug_table,\"aw\"\n\t"				\
 	ANNOTATE_DATA_SPECIAL						\
 	"2:\n\t"							\
-	__BUG_ENTRY(file, line, flags)					\
+	__BUG_ENTRY(format, file, line, flags)				\
 	"\t.org 2b + " size "\n"					\
 	".popsection\n"							\
 	extra
 
 #define _BUG_FLAGS(cond_str, ins, flags, extra)				\
 do {									\
-	asm_inline volatile(_BUG_FLAGS_ASM(ins, "%c0",			\
-					   "%c1", "%c2", "%c3", extra)	\
-		   : : "i" (WARN_CONDITION_STR(cond_str) __FILE__),	\
-		       "i" (__LINE__),					\
-		       "i" (flags),					\
-		       "i" (sizeof(struct bug_entry)));			\
+	asm_inline volatile(_BUG_FLAGS_ASM(ins, "%c[fmt]", "%c[file]",	\
+					   "%c[line]", "%c[fl]",	\
+					   "%c[size]", extra)		\
+		   : : [fmt] "i" (NULL),				\
+		       [file] "i" (WARN_CONDITION_STR(cond_str) __FILE__), \
+		       [line] "i" (__LINE__),				\
+		       [fl] "i" (flags),				\
+		       [size] "i" (sizeof(struct bug_entry)));		\
 } while (0)
 
 #define ARCH_WARN_ASM(file, line, flags, size)				\
-	_BUG_FLAGS_ASM(ASM_UD2, file, line, flags, size, "")
+	_BUG_FLAGS_ASM(ASM_UD2, "NULL", file, line, flags, size, "")
 
 #else
 
author maintainer
#9 lore raw

Re: [PATCH v2 08/12] x86/bug: Add BUG_FORMAT basics

Quoted 43 lines · Peter Zijlstra

Opt-in to BUG_FORMAT for x86_64, adjust the BUGTABLE helper and for now, just store NULL pointers.

---
 arch/x86/include/asm/bug.h |   31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)
arch/x86/include/asm/bug.h +12 -3
@@ -50,33 +50,44 @@
 #define __BUG_ENTRY_VERBOSE(file, line)
 #endif
 
-#define __BUG_ENTRY(file, line, flags)					\
+#if defined(CONFIG_X86_64) || defined(CONFIG_DEBUG_BUGVERBOSE_DETAILED)
+#define HAVE_ARCH_BUG_FORMAT
+#define __BUG_ENTRY_FORMAT(format)					\
+	"\t" __BUG_REL(format)	"\t# bug_entry::format\n"
+#else
+#define __BUG_ENTRY_FORMAT(format)
+#endif
+
+#define __BUG_ENTRY(format, file, line, flags)				\
 	__BUG_REL("1b")		"\t# bug_entry::bug_addr\n"		\
+	__BUG_ENTRY_FORMAT(format)					\
 	__BUG_ENTRY_VERBOSE(file, line)					\
 	"\t.word " flags	"\t# bug_entry::flags\n"
 
-#define _BUG_FLAGS_ASM(ins, file, line, flags, size, extra)		\
+#define _BUG_FLAGS_ASM(ins, format, file, line, flags, size, extra)	\
 	"1:\t" ins "\n"							\
 	".pushsection __bug_table,\"aw\"\n\t"				\
 	ANNOTATE_DATA_SPECIAL						\
 	"2:\n\t"							\
-	__BUG_ENTRY(file, line, flags)					\
+	__BUG_ENTRY(format, file, line, flags)				\
 	"\t.org 2b + " size "\n"					\
 	".popsection\n"							\
 	extra
 
 #define _BUG_FLAGS(cond_str, ins, flags, extra)				\
 do {									\

arch/x86/include/asm/bug.h line 82

Quoted 10 lines
arch/x86/include/asm/bug.h +4 -6
-	asm_inline volatile(_BUG_FLAGS_ASM(ins, "%c0",			\
-					   "%c1", "%c2", "%c3", extra)	\
-		   : : "i" (WARN_CONDITION_STR(cond_str) __FILE__),	\
-		       "i" (__LINE__),					\
-		       "i" (flags),					\
-		       "i" (sizeof(struct bug_entry)));			\
+	asm_inline volatile(_BUG_FLAGS_ASM(ins, "%c[fmt]", "%c[file]",	\
+					   "%c[line]", "%c[fl]",	\
+					   "%c[size]", extra)		\
+		   : : [fmt] "i" (NULL),				\

This doesn't work right with KASLR on -- and I hadn't noticed because most of my machines have nokaslr because of debugability :/

When we relocate the kernel, everything shifts by kaslr_offset(), and that works just fine when both the __bug_table and the target string is shifted, because then the relative position is the same and so the relocation keeps working.

However, when the target is the absolute value 0, this breaks, because 0 isn't shifted by kaslr_offset() but the __bug_table itself is.

So the relative entry:

	.long 0 - .

and its inverse:

	format = (const char *)&bug->format_disp + bug->format_disp;

then end up at kaslr_offset() and things are sad.

The relative entry has a SHN_UNDEF relocation, which is ignored by the relocs tool.

How is this supposed to be fixed?

Quoted 14 lines
arch/x86/include/asm/bug.h +5 -1
+		       [file] "i" (WARN_CONDITION_STR(cond_str) __FILE__), \
+		       [line] "i" (__LINE__),				\
+		       [fl] "i" (flags),				\
+		       [size] "i" (sizeof(struct bug_entry)));		\
 } while (0)
 
 #define ARCH_WARN_ASM(file, line, flags, size)				\
-	_BUG_FLAGS_ASM(ASM_UD2, file, line, flags, size, "")
+	_BUG_FLAGS_ASM(ASM_UD2, "NULL", file, line, flags, size, "")
 
 #else
 
author maintainer
#10 lore raw

Re: [PATCH v2 08/12] x86/bug: Add BUG_FORMAT basics

Quoted 72 lines · Peter Zijlstra
Quoted 43 lines · Peter Zijlstra

Opt-in to BUG_FORMAT for x86_64, adjust the BUGTABLE helper and for now, just store NULL pointers.

---
 arch/x86/include/asm/bug.h |   31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)
arch/x86/include/asm/bug.h +12 -3
@@ -50,33 +50,44 @@
 #define __BUG_ENTRY_VERBOSE(file, line)
 #endif
 
-#define __BUG_ENTRY(file, line, flags)					\
+#if defined(CONFIG_X86_64) || defined(CONFIG_DEBUG_BUGVERBOSE_DETAILED)
+#define HAVE_ARCH_BUG_FORMAT
+#define __BUG_ENTRY_FORMAT(format)					\
+	"\t" __BUG_REL(format)	"\t# bug_entry::format\n"
+#else
+#define __BUG_ENTRY_FORMAT(format)
+#endif
+
+#define __BUG_ENTRY(format, file, line, flags)				\
 	__BUG_REL("1b")		"\t# bug_entry::bug_addr\n"		\
+	__BUG_ENTRY_FORMAT(format)					\
 	__BUG_ENTRY_VERBOSE(file, line)					\
 	"\t.word " flags	"\t# bug_entry::flags\n"
 
-#define _BUG_FLAGS_ASM(ins, file, line, flags, size, extra)		\
+#define _BUG_FLAGS_ASM(ins, format, file, line, flags, size, extra)	\
 	"1:\t" ins "\n"							\
 	".pushsection __bug_table,\"aw\"\n\t"				\
 	ANNOTATE_DATA_SPECIAL						\
 	"2:\n\t"							\
-	__BUG_ENTRY(file, line, flags)					\
+	__BUG_ENTRY(format, file, line, flags)				\
 	"\t.org 2b + " size "\n"					\
 	".popsection\n"							\
 	extra
 
 #define _BUG_FLAGS(cond_str, ins, flags, extra)				\
 do {									\

arch/x86/include/asm/bug.h line 82

Quoted 10 lines
arch/x86/include/asm/bug.h +4 -6
-	asm_inline volatile(_BUG_FLAGS_ASM(ins, "%c0",			\
-					   "%c1", "%c2", "%c3", extra)	\
-		   : : "i" (WARN_CONDITION_STR(cond_str) __FILE__),	\
-		       "i" (__LINE__),					\
-		       "i" (flags),					\
-		       "i" (sizeof(struct bug_entry)));			\
+	asm_inline volatile(_BUG_FLAGS_ASM(ins, "%c[fmt]", "%c[file]",	\
+					   "%c[line]", "%c[fl]",	\
+					   "%c[size]", extra)		\
+		   : : [fmt] "i" (NULL),				\

This doesn't work right with KASLR on -- and I hadn't noticed because most of my machines have nokaslr because of debugability :/

When we relocate the kernel, everything shifts by kaslr_offset(), and that works just fine when both the __bug_table and the target string is shifted, because then the relative position is the same and so the relocation keeps working.

However, when the target is the absolute value 0, this breaks, because 0 isn't shifted by kaslr_offset() but the __bug_table itself is.

So the relative entry:

	.long 0 - .

and its inverse:

Quoted 8 lines
	format = (const char *)&bug->format_disp + bug->format_disp;

then end up at kaslr_offset() and things are sad.

The relative entry has a SHN_UNDEF relocation, which is ignored by the relocs tool.

How is this supposed to be fixed?

This seems to work. Is this something we can all live with? It feels a bit like a hack, but there doesn't appear to be anything better at hand.

---

lib/bug.c +5 -0
index 581a66b88c5c..8b470cc70afc 100644
@@ -48,6 +48,7 @@
 #include <linux/rculist.h>
 #include <linux/ftrace.h>
 #include <linux/context_tracking.h>
+#include <asm/setup.h>
 
 extern struct bug_entry __start___bug_table[], __stop___bug_table[];
 
@@ -145,6 +146,10 @@ static const char *bug_get_format(struct bug_entry *bug)
 #ifdef HAVE_ARCH_BUG_FORMAT
 #ifdef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
 	format = (const char *)&bug->format_disp + bug->format_disp;
+#ifdef CONFIG_RANDOMIZE_BASE
+	if ((unsigned long)format == kaslr_offset())
+		format = NULL;
+#endif
 #else
 	format = bug->format;
 #endif
author maintainer
#11 lore raw

Re: [PATCH v2 08/12] x86/bug: Add BUG_FORMAT basics

Quoted 26 lines · Peter Zijlstra
Quoted 26 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+	asm_inline volatile(_BUG_FLAGS_ASM(ins, "%c[fmt]", "%c[file]",	\
+					   "%c[line]", "%c[fl]",	\
+					   "%c[size]", extra)		\
+		   : : [fmt] "i" (NULL),				\

This doesn't work right with KASLR on -- and I hadn't noticed because most of my machines have nokaslr because of debugability :/

When we relocate the kernel, everything shifts by kaslr_offset(), and that works just fine when both the __bug_table and the target string is shifted, because then the relative position is the same and so the relocation keeps working.

However, when the target is the absolute value 0, this breaks, because 0 isn't shifted by kaslr_offset() but the __bug_table itself is.

So the relative entry:

	.long 0 - .

and its inverse:

	format = (const char *)&bug->format_disp + bug->format_disp;

then end up at kaslr_offset() and things are sad.

Quoted 7 lines
Quoted 4 lines

The relative entry has a SHN_UNDEF relocation, which is ignored by the relocs tool.

How is this supposed to be fixed?

This seems to work. Is this something we can all live with? It feels a bit like a hack, but there doesn't appear to be anything better at hand.

Ard came up with this glorious hack :-)

---

arch/x86/include/asm/bug.h +13 -9
index d0a96ff5c02c..812ec8932734 100644
@@ -5,6 +5,7 @@
 #include <linux/stringify.h>
 #include <linux/instrumentation.h>
 #include <linux/objtool.h>
+#include <linux/compiler.h>
 #include <asm/asm.h>
 
 #ifndef __ASSEMBLY__
@@ -59,7 +60,7 @@ extern void __WARN_trap(struct bug_entry *bug, ...);
 #if defined(CONFIG_X86_64) || defined(CONFIG_DEBUG_BUGVERBOSE_DETAILED)
 #define HAVE_ARCH_BUG_FORMAT
 #define __BUG_ENTRY_FORMAT(format)					\
-	"\t" __BUG_REL(format)	"\t# bug_entry::format\n"
+	"3:\t" __BUG_REL(format)	"\t# bug_entry::format\n"
 #else
 #define __BUG_ENTRY_FORMAT(format)
 #endif
@@ -84,18 +85,20 @@ extern void __WARN_trap(struct bug_entry *bug, ...);
 	extra
 
 #ifdef CONFIG_DEBUG_BUGVERBOSE_DETAILED
-#define WARN_CONDITION_STR(cond_str) cond_str
+#define WARN_CONDITION_STR(cond_str, null_str) cond_str
 #else
-#define WARN_CONDITION_STR(cond_str) NULL
+#define WARN_CONDITION_STR(cond_str, null_str) null_str
 #endif
 
-#define _BUG_FLAGS(cond_str, ins, flags, extra)				\
+#define _BUG_FLAGS(cond_str, ins, flags, extra, id)			\
 do {									\
+	extern typeof(cond_str) id;					\
 	asm_inline volatile("1:\t" ins "\n"				\
 			    _BUG_FLAGS_ASM("%c[fmt]", "%c[file]",	\
 					   "%c[line]", "%c[fl]",	\
 					   "%c[size]", extra)		\
-		   : : [fmt] "i" (WARN_CONDITION_STR(cond_str)),	\
+			    ".set " __stringify(id) ", 3b\n"		\
+		   : : [fmt] "i" (WARN_CONDITION_STR(cond_str, id)),	\
 		       [file] "i" (__FILE__),				\
 		       [line] "i" (__LINE__),				\
 		       [fl] "i" (flags),				\
@@ -104,11 +107,11 @@ do {									\
 
 #define ARCH_WARN_ASM(file, line, flags, size)				\
 	"1:\t " ASM_UD2 "\n"						\
-	_BUG_FLAGS_ASM("0", file, line, flags, size, "")
+	_BUG_FLAGS_ASM("3b", file, line, flags, size, "")
 
 #else
 
-#define _BUG_FLAGS(cond_str, ins, flags, extra)  asm volatile(ins)
+#define _BUG_FLAGS(cond_str, ins, flags, extra, id)  asm volatile(ins)
 
 #endif /* CONFIG_GENERIC_BUG */
 
@@ -116,7 +119,7 @@ do {									\
 #define BUG()							\
 do {								\
 	instrumentation_begin();				\
-	_BUG_FLAGS("", ASM_UD2, 0, "");				\
+	_BUG_FLAGS("", ASM_UD2, 0, "", __UNIQUE_ID(bug));	\
 	__builtin_unreachable();				\
 } while (0)
 
@@ -133,7 +136,8 @@ do {								\
 do {									\
 	__auto_type __flags = BUGFLAG_WARNING|(flags);			\
 	instrumentation_begin();					\
-	_BUG_FLAGS(cond_str, ASM_UD2, __flags, ARCH_WARN_REACHABLE);	\
+	_BUG_FLAGS(cond_str, ASM_UD2, __flags, ARCH_WARN_REACHABLE,	\
+		   __UNIQUE_ID(warn));					\
 	instrumentation_end();						\
 } while (0)
 
lib/bug.c +2 -1
index 581a66b88c5c..9a598b7dfc11 100644
@@ -144,7 +145,8 @@ static const char *bug_get_format(struct bug_entry *bug)
 	const char *format = NULL;
 #ifdef HAVE_ARCH_BUG_FORMAT
 #ifdef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
-	format = (const char *)&bug->format_disp + bug->format_disp;
+	if (bug->format_disp)
+		format = (const char *)&bug->format_disp + bug->format_disp;
 #else
 	format = bug->format;
 #endif
#12 lore raw

Re: [PATCH v2 08/12] x86/bug: Add BUG_FORMAT basics

Quoted 2 lines · Peter Zijlstra

Ard came up with this glorious hack :-)

"glorious".

Hmm. I _really_ don't love how it now uses that odd label across macros, it's not screaming "obvious" to me.

How bad would it be to just not use a NULL pointer at all, and instead make the "no format" be "empty format"?

(Slightly related thing: could we perhaps just make x86-32 also use the relative format, and get rid of some of the horrid #ifdef's in the x86 <asm/bug.h> implementation? Because that "__BUG_REL" thing is not a thing of beauty either).

Linus

author maintainer
#13 lore raw

Re: [PATCH v2 08/12] x86/bug: Add BUG_FORMAT basics

Quoted 8 lines · Linus Torvalds
Quoted 2 lines · Peter Zijlstra

Ard came up with this glorious hack :-)

"glorious".

Hmm. I _really_ don't love how it now uses that odd label across macros, it's not screaming "obvious" to me.

True.

Quoted 2 lines

How bad would it be to just not use a NULL pointer at all, and instead make the "no format" be "empty format"?

Certainly, how does this look?

I verified it works on normal C code and that it compiles and the object data looks right for Rust code.

---

arch/x86/include/asm/bug.h +6 -2
index d0a96ff5c02c..4b5ab56903bb 100644
@@ -86,7 +86,7 @@ extern void __WARN_trap(struct bug_entry *bug, ...);
 #ifdef CONFIG_DEBUG_BUGVERBOSE_DETAILED
 #define WARN_CONDITION_STR(cond_str) cond_str
 #else
-#define WARN_CONDITION_STR(cond_str) NULL
+#define WARN_CONDITION_STR(cond_str) ""
 #endif
 
 #define _BUG_FLAGS(cond_str, ins, flags, extra)				\
@@ -103,8 +103,12 @@ do {									\
 } while (0)
 
 #define ARCH_WARN_ASM(file, line, flags, size)				\
+	".pushsection .rodata.str1.1, \"aMS\", @progbits, 1\n"		\
+	"99:\n"								\
+	"\t.string \"\"\n"						\
+	".popsection\n"							\
 	"1:\t " ASM_UD2 "\n"						\
-	_BUG_FLAGS_ASM("0", file, line, flags, size, "")
+	_BUG_FLAGS_ASM("99b", file, line, flags, size, "")
 
 #else
 
lib/bug.c +11 -1
index 581a66b88c5c..edd9041f89f3 100644
@@ -144,7 +144,17 @@ static const char *bug_get_format(struct bug_entry *bug)
 	const char *format = NULL;
 #ifdef HAVE_ARCH_BUG_FORMAT
 #ifdef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
-	format = (const char *)&bug->format_disp + bug->format_disp;
+	/*
+	 * Allow an architecture to:
+	 *  - relative encode NULL (difficult vs KASLR);
+	 *  - use a literal 0 (there are no valid objects inside
+	 *    the __bug_table itself to refer to after all);
+	 *  - use an empty string.
+	 */
+	if (bug->format_disp)
+		format = (const char *)&bug->format_disp + bug->format_disp;
+	if (format && format[0] == '\0')
+		format = NULL;
 #else
 	format = bug->format;
 #endif
author maintainer
#14 lore raw

Re: [PATCH v2 08/12] x86/bug: Add BUG_FORMAT basics

In reply to Linus Torvalds
Quoted 4 lines · Linus Torvalds

(Slightly related thing: could we perhaps just make x86-32 also use the relative format, and get rid of some of the horrid #ifdef's in the x86 <asm/bug.h> implementation? Because that "__BUG_REL" thing is not a thing of beauty either).

This builds and appears to work. I'll slap a changelog on and stick on top of those other patches.

arch/x86/Kconfig +1 -1
@@ -381,7 +381,7 @@ config GENERIC_CSUM
 config GENERIC_BUG
 	def_bool y
 	depends on BUG
-	select GENERIC_BUG_RELATIVE_POINTERS if X86_64
+	select GENERIC_BUG_RELATIVE_POINTERS
 
 config GENERIC_BUG_RELATIVE_POINTERS
 	bool
arch/x86/include/asm/bug.h +4 -10
@@ -42,16 +42,10 @@ extern void __WARN_trap(struct bug_entry
 
 #ifdef CONFIG_GENERIC_BUG
 
-#ifdef CONFIG_X86_32
-#define __BUG_REL(val)		".long " val
-#else
-#define __BUG_REL(val)		".long " val " - ."
-#endif
-
 #ifdef CONFIG_DEBUG_BUGVERBOSE
 #define __BUG_ENTRY_VERBOSE(file, line)					\
-	"\t" __BUG_REL(file)   "\t# bug_entry::file\n"			\
-	"\t.word " line        "\t# bug_entry::line\n"
+	"\t.long " file " - .\t# bug_entry::file\n"			\
+	"\t.word " line     "\t# bug_entry::line\n"
 #else
 #define __BUG_ENTRY_VERBOSE(file, line)
 #endif
@@ -59,7 +53,7 @@ extern void __WARN_trap(struct bug_entry
 #if defined(CONFIG_X86_64) || defined(CONFIG_DEBUG_BUGVERBOSE_DETAILED)
 #define HAVE_ARCH_BUG_FORMAT
 #define __BUG_ENTRY_FORMAT(format)					\
-	"\t" __BUG_REL(format)	"\t# bug_entry::format\n"
+	"\t.long " format " - .\t# bug_entry::format\n"
 #else
 #define __BUG_ENTRY_FORMAT(format)
 #endif
@@ -69,7 +63,7 @@ extern void __WARN_trap(struct bug_entry
 #endif
 
 #define __BUG_ENTRY(format, file, line, flags)				\
-	__BUG_REL("1b")		"\t# bug_entry::bug_addr\n"		\
+	"\t.long 1b - ."	"\t# bug_entry::bug_addr\n"		\
 	__BUG_ENTRY_FORMAT(format)					\
 	__BUG_ENTRY_VERBOSE(file, line)					\
 	"\t.word " flags	"\t# bug_entry::flags\n"
author maintainer
#15 lore raw

PATCH v2 9/12 x86/bug: Use BUG_FORMAT for DEBUG_BUGVERBOSE_DETAILED

In reply to Peter Zijlstra

Since we have an explicit format string, use it for the condition string instead of frobbing it in the file string.

---
 arch/x86/include/asm/bug.h |   10 ++++++++--
 include/asm-generic/bug.h  |    8 +++++---
 2 files changed, 13 insertions(+), 5 deletions(-)
arch/x86/include/asm/bug.h +8 -2
@@ -74,13 +74,19 @@
 	".popsection\n"							\
 	extra
 
+#ifdef CONFIG_DEBUG_BUGVERBOSE_DETAILED
+#define WARN_CONDITION_STR(cond_str) cond_str
+#else
+#define WARN_CONDITION_STR(cond_str) NULL
+#endif
+
 #define _BUG_FLAGS(cond_str, ins, flags, extra)				\
 do {									\
 	asm_inline volatile(_BUG_FLAGS_ASM(ins, "%c[fmt]", "%c[file]",	\
 					   "%c[line]", "%c[fl]",	\
 					   "%c[size]", extra)		\
-		   : : [fmt] "i" (NULL),				\
-		       [file] "i" (WARN_CONDITION_STR(cond_str) __FILE__), \
+		   : : [fmt] "i" (WARN_CONDITION_STR(cond_str)),	\
+		       [file] "i" (__FILE__),				\
 		       [line] "i" (__LINE__),				\
 		       [fl] "i" (flags),				\
 		       [size] "i" (sizeof(struct bug_entry)));		\
include/asm-generic/bug.h +5 -3
@@ -18,11 +18,13 @@
 #define BUG_GET_TAINT(bug)	((bug)->flags >> 8)
 #endif
 
+#ifndef WARN_CONDITION_STR
 #ifdef CONFIG_DEBUG_BUGVERBOSE_DETAILED
-# define WARN_CONDITION_STR(cond_str) cond_str
+# define WARN_CONDITION_STR(cond_str) "[" cond_str "] "
 #else
 # define WARN_CONDITION_STR(cond_str)
 #endif
+#endif /* WARN_CONDITION_STR */
 
 #ifndef __ASSEMBLY__
 #include <linux/panic.h>
@@ -106,7 +108,7 @@ extern __printf(1, 2) void __warn_printk
 #define WARN_ON(condition) ({						\
 	int __ret_warn_on = !!(condition);				\
 	if (unlikely(__ret_warn_on))					\
-		__WARN_FLAGS("["#condition"] ",				\
+		__WARN_FLAGS(#condition,				\
 			     BUGFLAG_TAINT(TAINT_WARN));		\
 	unlikely(__ret_warn_on);					\
 })
@@ -114,7 +116,7 @@ extern __printf(1, 2) void __warn_printk
 #define WARN_ON_ONCE(condition) ({					\
 	int __ret_warn_on = !!(condition);				\
 	if (unlikely(__ret_warn_on))					\
-		__WARN_FLAGS("["#condition"] ",				\
+		__WARN_FLAGS(#condition,				\
 			     BUGFLAG_ONCE |				\
 			     BUGFLAG_TAINT(TAINT_WARN));		\
 	unlikely(__ret_warn_on);					\
author maintainer
#16 lore raw

PATCH v2 10/12 x86_64/bug: Implement __WARN_printf()

In reply to Peter Zijlstra

The basic idea is to have __WARN_printf() be a vararg function such that the compiler can do the optimal calling convention for us. This function body will be a #UD and then set up a va_list in the exception from pt_regs.

But because the trap will be in a called function, the bug_entry must be passed in. Have that be the first argument, with the format tucked away inside the bug_entry.

The comments should clarify the real fun details.

The big downside is that all WARNs will now show:

 RIP: 0010:__WARN_trap:+0

One possible solution is to simply discard the top frame when unwinding. A follow up patch takes care of this slightly differently by abusing the x86 static_call implementation.

---
 arch/x86/entry/entry.S     |    8 +++
 arch/x86/include/asm/bug.h |   60 +++++++++++++++++++++--
 arch/x86/kernel/traps.c    |  115 ++++++++++++++++++++++++++++++++++++++++-----
 3 files changed, 168 insertions(+), 15 deletions(-)
arch/x86/entry/entry.S +8 -0
@@ -32,6 +32,14 @@ SYM_FUNC_END(write_ibpb)
 /* For KVM */
 EXPORT_SYMBOL_GPL(write_ibpb);
 
+SYM_FUNC_START(__WARN_trap)
+	ANNOTATE_NOENDBR
+	ANNOTATE_REACHABLE
+	ud1 (%ecx), %_ASM_ARG1
+	RET
+SYM_FUNC_END(__WARN_trap)
+EXPORT_SYMBOL(__WARN_trap)
+
 .popsection
 
 /*
arch/x86/include/asm/bug.h +56 -4

96 lines not on this page · raw message

arch/x86/kernel/traps.c +104 -11

165 lines not on this page · raw message

author maintainer
#17 lore raw

Re: [PATCH v2 10/12] x86_64/bug: Implement __WARN_printf()

arch/x86/kernel/traps.c __always_inline int is_valid_bugaddr(uns · line 109

Quoted 13 lines · Peter Zijlstra

#endif /* _ASM_X86_BUG_H */

arch/x86/kernel/traps.c +3 -1
@@ -102,25 +102,37 @@ __always_inline int is_valid_bugaddr(uns
  * UBSan{0}:     67 0f b9 00             ud1    (%eax),%eax
  * UBSan{10}:    67 0f b9 40 10          ud1    0x10(%eax),%eax
  * static_call:  0f b9 cc                ud1    %esp,%ecx
+ * __WARN_trap:  67 48 0f b9 39          ud1    (%ecx),%reg
  *
- * Notably UBSAN uses EAX, static_call uses ECX.
+ * Notable, since __WARN_trap can use all registers, the distinction between
+ * UD1 users is through R/M.
  */

Maciej; you were working on making KASAN emit UD1 instructions, right? Where are you with those patches and are we conflicting on the encoding?

/me goes find the emails.. Yeah, I suggested UD1 /1, specifically:

	ud1 xx(%ecx), %ecx

and that does conflict. How about I switch to using:

	ud1 (%edx), %reg

for this?

author maintainer
#18 lore raw

PATCH v2 11/12 x86/bug: Implement WARN_ONCE()

In reply to Peter Zijlstra

Implement WARN_ONCE like WARN using BUGFLAG_ONCE.

---
 arch/x86/include/asm/bug.h |    9 +++++++++
 include/asm-generic/bug.h  |    2 ++
 2 files changed, 11 insertions(+)
arch/x86/include/asm/bug.h +9 -0
@@ -176,6 +176,15 @@ do {									\
 #define __WARN_printf(taint, fmt, arg...) \
 	__WARN_print_arg(BUGFLAG_TAINT(taint), fmt, ## arg)
 
+#define WARN_ONCE(cond, format, arg...) ({				\
+	int __ret_warn_on = !!(cond);					\
+	if (unlikely(__ret_warn_on)) {					\
+		__WARN_print_arg(BUGFLAG_ONCE|BUGFLAG_TAINT(TAINT_WARN),\
+				format, ## arg);			\
+	}								\
+	__ret_warn_on;							\
+})
+
 #endif /* HAVE_ARCH_BUG_FORMAT_ARGS */
 
 #include <asm-generic/bug.h>
include/asm-generic/bug.h +2 -0
@@ -176,8 +176,10 @@ extern __printf(1, 2) void __warn_printk
 	DO_ONCE_LITE_IF(condition, WARN_ON, 1)
 #endif
 
+#ifndef WARN_ONCE
 #define WARN_ONCE(condition, format...)				\
 	DO_ONCE_LITE_IF(condition, WARN, 1, format)
+#endif
 
 #define WARN_TAINT_ONCE(condition, taint, format...)		\
 	DO_ONCE_LITE_IF(condition, WARN_TAINT, 1, taint, format)
author maintainer
#19 lore raw

PATCH v2 12/12 x86_64/bug: Inline the UD1

In reply to Peter Zijlstra

(Ab)use the static_call infrastructure to convert all:

call __WARN_trap

instances into the desired:

ud1 (%ecx), %rdi

eliminating the CALL/RET, but more importantly, fixing the fact that all WARNs will have:

  RIP: 0010:__WARN_trap+0

Basically, by making it a static_call trampoline call, objtool will collect the callsites, and then the inline rewrite will hit the special case and replace the code with the magic instruction.

---
 arch/x86/include/asm/bug.h    |    5 ++++-
 arch/x86/kernel/static_call.c |   13 +++++++++++--
 arch/x86/kernel/traps.c       |    4 ++++
 3 files changed, 19 insertions(+), 3 deletions(-)
arch/x86/include/asm/bug.h +4 -1
@@ -137,9 +137,12 @@ do {									\
 #ifdef HAVE_ARCH_BUG_FORMAT_ARGS
 
 #ifndef __ASSEMBLY__
+#include <linux/static_call_types.h>
 struct bug_entry;
 extern void __WARN_trap(struct bug_entry *bug, ...);
 
+DECLARE_STATIC_CALL(WARN_trap, __WARN_trap);
+
 struct pt_regs;
 struct sysv_va_list { /* from AMD64 System V ABI */
 	unsigned int gp_offset;
@@ -171,7 +174,7 @@ extern void *__warn_args(struct arch_va_
 #define __WARN_print_arg(flags, format, arg...)				\
 do {									\
 	int __flags = (flags) | BUGFLAG_WARNING | BUGFLAG_ARGS ;	\
-	__WARN_trap(__WARN_bug_entry(__flags, format), ## arg);		\
+	static_call_mod(WARN_trap)(__WARN_bug_entry(__flags, format), ## arg); \
 	asm (""); /* inhibit tail-call optimization */			\
 } while (0)
 
arch/x86/kernel/static_call.c +11 -2
@@ -26,6 +26,11 @@ static const u8 xor5rax[] = { 0x2e, 0x2e
 
 static const u8 retinsn[] = { RET_INSN_OPCODE, 0xcc, 0xcc, 0xcc, 0xcc };
 
+/*
+ * ud1    (%ecx),%rdi -- see __WARN_trap() / decode_bug()
+ */
+static const u8 warninsn[] = { 0x67, 0x48, 0x0f, 0xb9, 0x39 };
+
 static u8 __is_Jcc(u8 *insn) /* Jcc.d32 */
 {
 	u8 ret = 0;
@@ -69,7 +74,10 @@ static void __ref __static_call_transfor
 			emulate = code;
 			code = &xor5rax;
 		}
-
+		if (func == &__WARN_trap) {
+			emulate = code;
+			code = &warninsn;
+		}
 		break;
 
 	case NOP:
@@ -128,7 +136,8 @@ static void __static_call_validate(u8 *i
 	} else {
 		if (opcode == CALL_INSN_OPCODE ||
 		    !memcmp(insn, x86_nops[5], 5) ||
-		    !memcmp(insn, xor5rax, 5))
+		    !memcmp(insn, xor5rax, 5) ||
+		    !memcmp(insn, warninsn, 5))
 			return;
 	}
 
arch/x86/kernel/traps.c +4 -0
@@ -31,6 +31,7 @@
 #include <linux/kexec.h>
 #include <linux/sched.h>
 #include <linux/sched/task_stack.h>
+#include <linux/static_call.h>
 #include <linux/timer.h>
 #include <linux/init.h>
 #include <linux/bug.h>
@@ -215,6 +216,9 @@ static inline unsigned long pt_regs_val(
 }
 
 #ifdef HAVE_ARCH_BUG_FORMAT_ARGS
+DEFINE_STATIC_CALL(WARN_trap, __WARN_trap);
+EXPORT_STATIC_CALL_TRAMP(WARN_trap);
+
 /*
  * Create a va_list from an exception context.
  */
#20 lore raw
In reply to Peter Zijlstra
Quoted 3 lines · Peter Zijlstra

We should probably just merge these, as they improve the code-gen for WARN() significantly on x86

Yes.. And I'd actually like to see some example code generation in the commit messages just to make that "improve the code-gen" more explicit.

I know I saw some originally (or at least we discussed it), but it's been long enough that my bird-brain has long since forgotten..

It looks like this series is on top of the BUGVERBOSE_DETAILED updates (that I like but don't have in my tree yet - I think it's all scheduled for 6.19). So I just scanned the patches visually and they all look fine, I was just left wanting a more concrete "look, we used to do this, now we generate _this_ instead".

Linus

author maintainer
#21 lore raw
Quoted 8 lines · Linus Torvalds
Quoted 3 lines · Peter Zijlstra

We should probably just merge these, as they improve the code-gen for WARN() significantly on x86

Yes.. And I'd actually like to see some example code generation in the commit messages just to make that "improve the code-gen" more explicit.

Damn, I actually thought to include some and then I totally forgot.

So lets see:

        if (WARN_ONCE(preempt_count() != 2*PREEMPT_DISABLE_OFFSET,
                      "corrupted preempt_count: %s/%d/0x%x\n",
                      current->comm, current->pid, preempt_count()))

#define WARN_ONCE(condition, format...) \
DO_ONCE_LITE_IF(condition, WARN, 1, format)

#define WARN(condition, format...) ({                                   \
        int __ret_warn_on = !!(condition);                              \
        if (unlikely(__ret_warn_on))                                    \
                __WARN_printf(TAINT_WARN, format);                      \
        unlikely(__ret_warn_on);                                        \

})

#define __WARN_printf(taint, arg...) do {                               \
                instrumentation_begin();                                \
                __warn_printk(arg);                                     \
                __WARN_FLAGS("", BUGFLAG_NO_CUT_HERE | BUGFLAG_TAINT(taint));\
                instrumentation_end();                                  \
        } while (0)

Turns into this majestic pile:

# ../kernel/sched/core.c:5093: if (WARN_ONCE(preempt_count() != 2*PREEMPT_DISABLE_OFFSET,

        cmpl    $2, %ecx        #, _7
        jne     .L1472  #,

...

.L1472:
# ../kernel/sched/core.c:5093: if (WARN_ONCE(preempt_count() != 2*PREEMPT_DISABLE_OFFSET,

        cmpb    $0, __already_done.11(%rip)     #, __already_done
        je      .L1513  #,

...

.L1513:
# ../kernel/sched/core.c:5093: if (WARN_ONCE(preempt_count() != 2*PREEMPT_DISABLE_OFFSET,

        movb    $1, __already_done.11(%rip)     #, __already_done

# ../kernel/sched/core.c:5093: if (WARN_ONCE(preempt_count() != 2*PREEMPT_DISABLE_OFFSET,

# 0 "" 2
# ../kernel/sched/core.c:5093: if (WARN_ONCE(preempt_count() != 2*PREEMPT_DISABLE_OFFSET, #NO_APP

        movl    1424(%r14), %edx        # _15->pid, _15->pid
        leaq    1912(%r14), %rsi        #, _17
        movq    $.LC43, %rdi    #,
        call    __warn_printk   #

# ../kernel/sched/core.c:5093: if (WARN_ONCE(preempt_count() != 2*PREEMPT_DISABLE_OFFSET,

# 0 "" 2
# 5093 "../kernel/sched/core.c" 1

        1:       .byte 0x0f, 0x0b ;

.pushsection __bug_table,"aw"
912:
.pushsection .discard.annotate_data,"M", @progbits, 8 .long 912b - .
.long 1
.popsection
2: .long 1b - . # bug_entry::bug_addr
.long .LC1 - . # bug_entry::file #
.word 5093 # bug_entry::line #
.word 2313 # bug_entry::flags #
.org 2b + 12 #
.popsection
.pushsection .discard.annotate_insn,"M", @progbits, 8
.long 1b - .
.long 8 # ANNOTYPE_REACHABLE
.popsection

While afterwards it looks like so:

# ../kernel/sched/core.c:5093: if (WARN_ONCE(preempt_count() != 2*PREEMPT_DISABLE_OFFSET,

        cmpl    $2, %ecx        #, _7
        jne     .L1442  #,

...

.L1442:
# ../kernel/sched/core.c:5093: if (WARN_ONCE(preempt_count() != 2*PREEMPT_DISABLE_OFFSET, #APP
# 5093 "../kernel/sched/core.c" 1

        lea (2f)(%rip), %rdi    # bug
1:
.pushsection __bug_table,"aw"
        912:
        .pushsection .discard.annotate_data,"M", @progbits, 8
        .long 912b - .
        .long 1
        .popsection
        2:
        .long 1b - .    # bug_entry::bug_addr
        .long .LC43 - . # bug_entry::format     #
        .long .LC1 - .  # bug_entry::file       #
        .word 5093      # bug_entry::line       #
        .word 2323      # bug_entry::flags      #
        .org 2b + 16    #

.popsection

# 0 "" 2
#NO_APP

        movl    1424(%r14), %edx        # pretmp_19->pid, pretmp_19->pid
# ../kernel/sched/core.c:5093:  if (WARN_ONCE(preempt_count() != 2*PREEMPT_DISABLE_OFFSET,
        leaq    1912(%r14), %rsi        #, _13

# ../kernel/sched/core.c:5093: if (WARN_ONCE(preempt_count() != 2*PREEMPT_DISABLE_OFFSET,
call __SCT__WARN_trap #

Where at runtime we patch that "call __SCT__WARN_trap" to read like:

	ud1 (%ecx), %rdi

The whole DO_ONCE_LITE_IF/__already_done thing is gone (replaced with BUGFLAG_ONCE) and the external __warn_prink() call is also gone, now inside the exception handler through the pt_regs->va_list magic.

I'll see if I can clean that up somewhat and stick in the changelog.