x86: WARN() hackery
Subsystems (6)
Peter Zijlstra
maintainercover x86: WARN() hackery
Hi,
Slightly less mad this time :-)
The primary purpose of all this is to get the WARN() printk() and __warn() calls into the same context. Notably the current state is that WARN() ends up doing printk() in-place, then takes an exception and has the exception do the __warn().
The problem with all this is the ONCE logic. Normal WARN_ON_ONCE() (without the printk) has the ONCE logic in the exception (__report_bug()). But because WARN() essentially results in two distinct actions (printk + trap) this cannot work. With the result that additional ONCE logic is sprinkled around for each such site.
Current proposals look to make this worse by adding KUnit checks for all this, including regular WARN. Making the per-instance code overhead even worse.
As such, by moving the printk() into the exception, and having the exception (__report_bug() in fact) do everything, we get rid of the external ONCE logic and provide a cental place for additional conditions without need to litter all the instances.
Peter Zijlstra
maintainerPATCH 1/11 x86: Provide assembly __bug_table helpers
Rework the __bug_table helpers such that usage from assembly becomes possible.
--- arch/x86/include/asm/bug.h | 54 ++++++++++++++++++++------------------------- 1 file changed, 24 insertions(+), 30 deletions(-)
arch/x86/include/asm/bug.h +24 -30
@@ -32,46 +32,40 @@#ifdef CONFIG_GENERIC_BUG #ifdef CONFIG_X86_32 -# define __BUG_REL (val) ".long " __stringify(val) +#define ASM_BUG_REL (val) .long val#else -# define __BUG_REL (val) ".long " __stringify(val) " - ." +#define ASM_BUG_REL (val) .long val - .#endif #ifdef CONFIG_DEBUG_BUGVERBOSE +#define ASM_BUGTABLE_VERBOSE (file, line) \ + ASM_BUG_REL(file) ; \ + .word line +#define ASM_BUGTABLE_VERBOSE_SIZE 6/* sizeof(file) + sizeof(line) */ +#else +#define ASM_BUGTABLE_VERBOSE (file, line) +#define ASM_BUGTABLE_VERBOSE_SIZE 0 +#endif -#define _BUG_FLAGS (ins, flags, extra) \ -do { \ - asm_inline volatile("1:\t" ins "\n" \ - ".pushsection __bug_table,\"aw\"\n" \ - "2:\t" __BUG_REL(1b) "\t# bug_entry::bug_addr\n" \ - "\t" __BUG_REL(%c0) "\t# bug_entry::file\n" \ - "\t.word %c1" "\t# bug_entry::line\n" \ - "\t.word %c2" "\t# bug_entry::flags\n" \ - "\t.org 2b+%c3\n" \ - ".popsection\n" \ - extra \ - : : "i" (__FILE__), "i" (__LINE__), \ - "i" (flags), \ - "i" (sizeof(struct bug_entry))); \ -} while (0) +#define ASM_BUGTABLE_BASE_SIZE 6/* sizeof(bug_addr) + sizeof(flags) */ -#else /* !CONFIG_DEBUG_BUGVERBOSE */ +#define ASM_BUGTABLE_FLAGS (at, file, line, flags) \ + .pushsection __bug_table, "aw" ; \ + 123: ASM_BUG_REL(at) ; \ + ASM_BUGTABLE_VERBOSE(file, line) ; \ + .word flags ; \ + .org 123b + ASM_BUGTABLE_BASE_SIZE + ASM_BUGTABLE_VERBOSE_SIZE ;\ + .popsection -#define _BUG_FLAGS (ins, flags, extra) \ +#define _BUG_FLAGS (insn, flags, extra) \ do { \ - asm_inline volatile("1:\t" ins "\n" \ - ".pushsection __bug_table,\"aw\"\n" \ - "2:\t" __BUG_REL(1b) "\t# bug_entry::bug_addr\n" \ - "\t.word %c0" "\t# bug_entry::flags\n" \ - "\t.org 2b+%c1\n" \ - ".popsection\n" \ - extra \ - : : "i" (flags), \ - "i" (sizeof(struct bug_entry))); \ + asm_inline volatile("1:\t" insn "\n" \ + __stringify(ASM_BUGTABLE_FLAGS(1b, %c[file], %c[line], %c[fl])) "\n" \ + extra \ + : : [file] "i" (__FILE__), [line] "i" (__LINE__), \ + [fl] "i" (flags)); \ } while (0) -#endif /* CONFIG_DEBUG_BUGVERBOSE */ -#else #define _BUG_FLAGS (ins, flags, extra) asm volatile(ins)
Peter Zijlstra
maintainerPATCH 2/11 bug: Add BUG_FORMAT infrastructure
In reply to Peter ZijlstraAdd 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 longreturn 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(udisable_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 */
Peter Zijlstra
maintainerPATCH 3/11 bug: Clean up CONFIG_GENERIC_BUG_RELATIVE_POINTERS
In reply to Peter ZijlstraThree 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;
Peter Zijlstra
maintainerPATCH 4/11 bug: Add BUG_FORMAT_ARGS infrastructure
In reply to Peter ZijlstraAdd 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 longreturn 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) {
Peter Zijlstra
maintainerPATCH 5/11 bug: Add report_bug_entry()
In reply to Peter ZijlstraAdd 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_entrystruct 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 *fmprintk ("%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(ureturn 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;
Peter Zijlstra
maintainerPATCH 6/11 bug: Allow architectures to provide __WARN_printf()
In reply to Peter ZijlstraInstead of providing __WARN_FLAGS(), allow an architecture to provide __WARN_printf(), which allows for optimizing WARN(), rather than WARN_ON().
--- include/asm-generic/bug.h | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)
include/asm-generic/bug.h +13 -8
@@ -94,14 +94,7 @@ 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 +#if defined(__WARN_FLAGS) && !defined(__WARN_printf)#define __WARN () __WARN_FLAGS(BUGFLAG_TAINT(TAINT_WARN)) #define __WARN_printf(taint, arg...) do { \ instrumentation_begin(); \
@@ -118,6 +111,18 @@ extern __printf(1, 2) void __warn_printk })#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
Peter Zijlstra
maintainerPATCH 7/11 x86_64/bug: Add BUG_FORMAT basics
In reply to Peter ZijlstraOpt-in to BUG_FORMAT for x86_64, adjust the BUGTABLE helper and for now, just store NULL pointers.
--- arch/x86/include/asm/bug.h | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-)
arch/x86/include/asm/bug.h +17 -4
@@ -47,22 +47,35 @@#define ASM_BUGTABLE_VERBOSE_SIZE 0#endif +#ifdef CONFIG_X86_64 +#define HAVE_ARCH_BUG_FORMAT +#define ASM_BUGTABLE_FORMAT (format) \ + ASM_BUG_REL(format) +#define ASM_BUGTABLE_FORMAT_SIZE 4/* sizeof(format) */ +#else +#define ASM_BUGTABLE_FORMAT (format) +#define ASM_BUGTABLE_FORMAT_SIZE 0 +#endif +#define ASM_BUGTABLE_BASE_SIZE 6/* sizeof(bug_addr) + sizeof(flags) */ -#define ASM_BUGTABLE_FLAGS (at, file, line, flags) \ +#define ASM_BUGTABLE_FLAGS (at, format, file, line, flags) \ .pushsection __bug_table, "aw" ; \ 123: ASM_BUG_REL(at) ; \ + ASM_BUGTABLE_FORMAT(format) ; \ ASM_BUGTABLE_VERBOSE(file, line) ; \ .word flags ; \ - .org 123b + ASM_BUGTABLE_BASE_SIZE + ASM_BUGTABLE_VERBOSE_SIZE ;\ + .org 123b + ASM_BUGTABLE_BASE_SIZE + ASM_BUGTABLE_FORMAT_SIZE + ASM_BUGTABLE_VERBOSE_SIZE ; \ .popsection#define _BUG_FLAGS (insn, flags, extra) \ do { \ asm_inline volatile("1:\t" insn "\n" \ - __stringify(ASM_BUGTABLE_FLAGS(1b, %c[file], %c[line], %c[fl])) "\n" \ + __stringify(ASM_BUGTABLE_FLAGS(1b, %c[fmt], %c[file], %c[line], %c[fl])) "\n" \ extra \ - : : [file] "i" (__FILE__), [line] "i" (__LINE__), \ + : : [fmt] "i" (NULL), \ + [file] "i" (__FILE__), \ + [line] "i" (__LINE__), \ [fl] "i" (flags)); \ } while (0)
Peter Zijlstra
maintainerPATCH 8/11 x86_64/bug: Implement __WARN_printf()
In reply to Peter ZijlstraThe 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
This will be adressed in a follow up patch.
--- arch/x86/entry/entry.S | 8 +++ arch/x86/include/asm/bug.h | 47 +++++++++++++++++++ arch/x86/kernel/traps.c | 107 ++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 152 insertions(+), 10 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 +47 -0
65 lines not on this page · raw message
arch/x86/kernel/traps.c +97 -10
144 lines not on this page · raw message
Peter Zijlstra
maintainerRe: [PATCH 08/11] x86_64/bug: Implement __WARN_printf()
Quoted 24 lines · Peter Zijlstra
arch/x86/kernel/traps.c +12
-1@@ -156,10 +166,21 @@ __always_inline int decode_bug(unsignedif (X86_MODRM_MOD (v) !=3 &&X86_MODRM_RM (v) ==4 ) addr++;/* SIB */ + reg =X86_MODRM_REG (v) +8 *!!X86_REX_R (rex); + rm =X86_MODRM_RM (v) +8 *!!X86_REX_B (rex); +/* Decode immediate, if present */ switch (X86_MODRM_MOD (v)) {case 0 :if (X86_MODRM_RM (v) ==5 ) - addr +=4 ;/* RIP + disp32 */ + addr +=4 ;/* RIP + disp32 */ + +if (rm ==0 )/* (%eax) */ + type =BUG_UD1_UBSAN ; + +if (rm ==1 ) {/* (%ecx) */ + *imm = reg; + type =BUG_UD1_WARN ; + }break ;case 1 : *imm = *(s8 *)addr;
arch/x86/kernel/traps.c __always_inline int decode_bug(unsigned · line 201
Quoted 7 lines
arch/x86/kernel/traps.c +2
-2@@ -176,12 +197,73 @@ __always_inline int decode_bug(unsigned/* record instruction length */ *len = addr - start; -if (X86_MODRM_REG (v) ==0 )/* EAX */ -return BUG_UD1_UBSAN ; +return type; +}
Oh, this breaks the UBSAN case, it must also set UD1_UBSAN for mod 1 and
2. I'll fix.
Peter Zijlstra
maintainerPATCH 9/11 x86/bug: Implement WARN_ONCE()
In reply to Peter ZijlstraImplement WARN_ONCE like WARN using BUGFLAG_ONCE.
--- arch/x86/include/asm/bug.h | 14 ++++++++++++++ include/asm-generic/bug.h | 2 ++ 2 files changed, 16 insertions(+)
arch/x86/include/asm/bug.h +14 -0
@@ -152,6 +152,20 @@ 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; \ +}) + +/* +* Make sure WARN_ONCE() arguments get expanded before trying to count them. +*/ +#define WARN_ONCE(cond, format...) __WARN_ONCE(cond, format) +#endif /* HAVE_ARCH_BUG_FORMAT */ #include <asm-generic/bug.h>
include/asm-generic/bug.h +2 -0
@@ -155,8 +155,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)
Peter Zijlstra
maintainerPATCH 10/11 x86: Clean up default rethunk warning
In reply to Peter ZijlstraReplace the funny __warn_thunk thing with a more regular WARN_ONCE() and simplify the ifdeffery.
Notably this avoids RET from having recursive RETs (once from the thunk and once from the C function) -- recurive RET makes my head hurt for no good reason.
--- arch/x86/entry/entry.S | 4 ---- arch/x86/include/asm/nospec-branch.h | 2 -- arch/x86/kernel/cpu/bugs.c | 5 ----- arch/x86/lib/retpoline.S | 22 +++++++++++++++------- 4 files changed, 15 insertions(+), 18 deletions(-)
arch/x86/entry/entry.S +0 -4
@@ -13,8 +13,6 @@ #include <asm/cpufeatures.h> #include <asm/nospec-branch.h> -#include "calling.h" - .pushsection .noinstr.text, "ax" /* Clobbers AX, CX, DX */
@@ -61,8 +59,6 @@ EXPORT_SYMBOL_GPL(mds_verw_sel); .popsection -THUNK warn_thunk_thunk, __warn_thunk - /* * Clang's implementation of TLS stack cookies requires the variable in * question to be a TLS variable. If the variable happens to be defined as an
arch/x86/include/asm/nospec-branch.h +0 -2
@@ -386,8 +386,6 @@ extern void clear_bhb_loop(void);extern void (*x86_return_thunk)(void ); -extern void __warn_thunk (void ); -#ifdef CONFIG_MITIGATION_CALL_DEPTH_TRACKING extern void call_depth_return_thunk (void );
arch/x86/kernel/cpu/bugs.c +0 -5
@@ -3415,8 +3415,3 @@ ssize_t cpu_show_indirect_target_selecti return cpu_show_common(dev, attr, buf, X86_BUG_ITS); }#endif - -void __warn_thunk (void ) -{ -WARN_ONCE (1 ,"Unpatched return thunk in use. This should not happen!\n" ); -}
arch/x86/lib/retpoline.S +15 -7
@@ -12,6 +12,7 @@ #include <asm/percpu.h> #include <asm/frame.h> #include <asm/nops.h> +#include <asm/bug.h> .section .text..__x86.indirect_thunk
@@ -416,6 +417,19 @@ EXPORT_SYMBOL(its_return_thunk) #endif /* CONFIG_MITIGATION_ITS */ + .pushsection .rodata.str1.1 +.Lwarn: + .string "Unpatched return thunk in use.\n" + .popsection + +/* + * Helper that will trip WARN_ONCE() after alternatives have ran. + */ +#define ALT_WARN_RETHUNK \ + ANNOTATE_REACHABLE ; \ + 1: ALTERNATIVE "", "ud2", X86_FEATURE_ALWAYS ; \ + ASM_BUGTABLE_FLAGS(1b, .Lwarn, 0, 0, BUGFLAG_WARNING | BUGFLAG_ONCE ) + /* * This function name is magical and is used by -mfunction-return=thunk-extern * for the compiler to generate JMPs to it.
@@ -432,15 +446,9 @@ EXPORT_SYMBOL(its_return_thunk) SYM_CODE_START(__x86_return_thunk) UNWIND_HINT_FUNC ANNOTATE_NOENDBR -#if defined(CONFIG_MITIGATION_UNRET_ENTRY) || \ - defined(CONFIG_MITIGATION_SRSO) || \ - defined(CONFIG_MITIGATION_CALL_DEPTH_TRACKING) - ALTERNATIVE __stringify(ANNOTATE_UNRET_SAFE; ret), \ - "jmp warn_thunk_thunk", X86_FEATURE_ALWAYS -#else + ALT_WARN_RETHUNK ANNOTATE_UNRET_SAFE ret -#endif int3 SYM_CODE_END(__x86_return_thunk) SYM_PIC_ALIAS(__x86_return_thunk)
Peter Zijlstra
maintainerPATCH 11/11 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
@@ -114,9 +114,12 @@ do { \ #ifdef HAVE_ARCH_BUG_FORMAT #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;
@@ -145,7 +148,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, 0x2estatic 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>
@@ -209,6 +210,9 @@ static inline unsigned long pt_regs_val( }#ifdef HAVE_ARCH_BUG_FORMAT +DEFINE_STATIC_CALL (WARN_trap, __WARN_trap); +EXPORT_STATIC_CALL_TRAMP (WARN_trap); +/* * Create a va_list from an exception context. */
Quoted 2 lines · Peter Zijlstra
Slightly less mad this time :-)
Looks a lot better to me - at least by looking at the patches.
So I didn't actually apply the series to test out what the end result looks like, but from just the patches this looks much nicer. Thanks,
Linus
Hi,
Quoted 13 lines · Peter Zijlstra
Slightly less mad this time :-)
The primary purpose of all this is to get the
WARN()printk()and__warn()calls into the same context. Notably the current state is thatWARN()ends up doingprintk()in-place, then takes an exception and has the exception do the__warn().The problem with all this is the ONCE logic. Normal
WARN_ON_ONCE()(without the printk) has the ONCE logic in the exception (__report_bug()). But becauseWARN()essentially results in two distinct actions (printk + trap) this cannot work. With the result that additional ONCE logic is sprinkled around for each such site.
Quoted 8 lines
Current proposals look to make this worse by adding KUnit checks for all this, including regular WARN. Making the per-instance code overhead even worse.
As such, by moving the
printk()into the exception, and having the exception (__report_bug()in fact) do everything, we get rid of the external ONCE logic and provide a cental place for additional conditions without need to litter all the instances.
Thanks a lot for working on that. What is the status of this patch? It looks like Linus was happy with it, and I understood that you felt it was a blocker for the kunit warning work we'd really like to get merged at some point.
Thanks again,
Maxime
Peter Zijlstra
maintainerQuoted 25 lines · Maxime Ripard
Hi,
Quoted 13 lines · Peter Zijlstra
Slightly less mad this time :-)
The primary purpose of all this is to get the
WARN()printk()and__warn()calls into the same context. Notably the current state is thatWARN()ends up doingprintk()in-place, then takes an exception and has the exception do the__warn().The problem with all this is the ONCE logic. Normal
WARN_ON_ONCE()(without the printk) has the ONCE logic in the exception (__report_bug()). But becauseWARN()essentially results in two distinct actions (printk + trap) this cannot work. With the result that additional ONCE logic is sprinkled around for each such site.Quoted 8 lines
Current proposals look to make this worse by adding KUnit checks for all this, including regular WARN. Making the per-instance code overhead even worse.
As such, by moving the
printk()into the exception, and having the exception (__report_bug()in fact) do everything, we get rid of the external ONCE logic and provide a cental place for additional conditions without need to litter all the instances.
Quoted 4 lines
Thanks a lot for working on that. What is the status of this patch? It looks like Linus was happy with it, and I understood that you felt it was a blocker for the kunit warning work we'd really like to get merged at some point.
I talked to Mark Rutland about arm64 support, to see if the things I did are generic enough for other architectures to support, or if there's anything I need to change.
He said he was going to have a poke, but it appears he's not had time yet.
Once he's happy, I think we can move this forward. Obviously I have to rebase on top of the things Ingo stuck in meanwhile, but that shouldn't be too hard.
Quoted 35 lines · Peter Zijlstra
Quoted 25 lines · Maxime Ripard
Hi,
Quoted 13 lines · Peter Zijlstra
Slightly less mad this time :-)
The primary purpose of all this is to get the
WARN()printk()and__warn()calls into the same context. Notably the current state is thatWARN()ends up doingprintk()in-place, then takes an exception and has the exception do the__warn().The problem with all this is the ONCE logic. Normal
WARN_ON_ONCE()(without the printk) has the ONCE logic in the exception (__report_bug()). But becauseWARN()essentially results in two distinct actions (printk + trap) this cannot work. With the result that additional ONCE logic is sprinkled around for each such site.Quoted 8 lines
Current proposals look to make this worse by adding KUnit checks for all this, including regular WARN. Making the per-instance code overhead even worse.
As such, by moving the
printk()into the exception, and having the exception (__report_bug()in fact) do everything, we get rid of the external ONCE logic and provide a cental place for additional conditions without need to litter all the instances.Quoted 4 lines
Thanks a lot for working on that. What is the status of this patch? It looks like Linus was happy with it, and I understood that you felt it was a blocker for the kunit warning work we'd really like to get merged at some point.
I talked to Mark Rutland about arm64 support, to see if the things I did are generic enough for other architectures to support, or if there's anything I need to change.
Quoted 6 lines
He said he was going to have a poke, but it appears he's not had time yet.
Once he's happy, I think we can move this forward. Obviously I have to rebase on top of the things Ingo stuck in meanwhile, but that shouldn't be too hard.
Ack, thanks for the update :)
Maxime