ARM: Use invalid opcode also for warnings
cover ARM: Use invalid opcode also for warnings
ARM is already using an invalid opcode for BUG(). WARN_ON() an its variants are not optimised this way. The series here massages the BUG() implementation until __WARN_FLAGS() can make use of it. Here is comparison build of the defconfig with and without the series:
| text data bss filename | 19678700 10728606 407364 vmlinux | 19434880 10814198 407748 vmlinux-patched
text -238KiB, data +84KiB, bss +384
| 19680276 10699150 408132 vmlinux-!DEBUG_BUGVERBOSE | 19443392 10735644 408516 vmlinux-patched-!DEBUG_BUGVERBOSE text -231KiB, data +36KiB, +384
In general the text savings are greater than the increment in data.
---
Sebastian Andrzej Siewior (11):
ARM: BUG: Move the assembly out of __BUG() macro
ARM: BUG: Spent each entry in __BUG__LINE() its own line
ARM: BUG: Move the section right after the opcode
ARM: BUG: Move file/ line encoding into a custom macro
ARM: BUG: Move __bug_table creation to __BUG_ENTRY_GENERIC() macro
ARM: BUG: Move the __BUG() out of CONFIG_DEBUG_BUGVERBOSE
ARM: BUG: Pass a flags argument to __BUG__LINE()
ARM: BUG: Move BUG() into the do-while loop
ARM: traps: Move the BUG handling from die() to do_undefinstr()
ARM: traps: Add handling for warnings to handle_bug_opcode()
ARM: BUG: Add WARN support via __WARN_FLAGS()
arch/arm/include/asm/bug.h | 61 ++++++++++++++++++++++++++++------------------ arch/arm/kernel/traps.c | 40 +++++++++++++++++++++++++----- 2 files changed, 71 insertions(+), 30 deletions(-)
---
base-commit: 4eeccbed21e50c19f97be9d325511f3de6343f2d
change-id: 20261004-arm_warning-3ac1e2aea65f
Best regards,
Signature · 1 line
Sebastian Andrzej Siewior <[email protected]>
PATCH 1/11 ARM: BUG: Move the assembly out of __BUG() macro
Move the assembly line from within the __BUG() macro into its own define. The quotation marks and the string escape can be removed and instead the resulting __BUG__LINE() macro can be wrapped into __stringify() to achieve the same.
The BUG_INSTR() expects a string context. This can be replaced with ___asm_opcode_to_mem_arm()/_thumb16() macro. Wrap this into BUG_ENCODING() macro.
--- arch/arm/include/asm/bug.h | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-)
arch/arm/include/asm/bug.h +15 -10
@@ -14,9 +14,11 @@#ifdef CONFIG_THUMB2_KERNEL #define BUG_INSTR_VALUE 0xde02#define BUG_INSTR (__value) __inst_thumb16(__value) +#define BUG_ENCODING () .short ___asm_opcode_to_mem_thumb16(BUG_INSTR_VALUE)#else #define BUG_INSTR_VALUE 0xe7f001f2#define BUG_INSTR (__value) __inst_arm(__value) +#define BUG_ENCODING () .long ___asm_opcode_to_mem_arm(BUG_INSTR_VALUE)#endif
@@ -25,24 +27,27 @@#ifdef CONFIG_DEBUG_BUGVERBOSE +#define __BUG__LINE (__file, __line, __value) \ + 1: ; \ + BUG_ENCODING(); \ + .pushsection .rodata.str, "aMS", %progbits, 1; \ + 2: .asciz __file; \ + .popsection; \ + .pushsection __bug_table,"aw"; \ + .align 2; \ + 3: .word 1b, 2b; \ + .hword __line , 0; \ + .popsection; +/* * The extra indirection is to ensure that the __FILE__ string comes through * OK. Many version of gcc do not support the asm %c parameter which would be * preferable to this unpleasantness. We use mergeable string sections to * avoid multiple copies of the string appearing in the kernel image. */ -#define __BUG (__file, __line, __value) \ do { \ - asm volatile("1:\t" BUG_INSTR(__value) "\n" \ - ".pushsection .rodata.str, \"aMS\", %progbits, 1\n" \ - "2:\t.asciz " #__file "\n" \ - ".popsection\n" \ - ".pushsection __bug_table,\"aw\"\n" \ - ".align 2\n" \ - "3:\t.word 1b, 2b\n" \ - "\t.hword " #__line ", 0\n" \ - ".popsection"); \ + asm volatile(__stringify(__BUG__LINE(__file, __line, __value))); \ unreachable(); \ } while (0)
Signature · 1 line
2.55.0
PATCH 2/11 ARM: BUG: Spent each entry in __BUG__LINE() its own line
In reply to Sebastian Andrzej SiewiorAfter the "3:" label the individual entries for the struct bug_entry follow.
Move the entries into their own line which makes re-grouping later simpler.
--- arch/arm/include/asm/bug.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
arch/arm/include/asm/bug.h +5 -2
@@ -35,8 +35,11 @@ .popsection; \ .pushsection __bug_table,"aw"; \ .align 2; \ - 3: .word 1b, 2b; \ - .hword __line , 0; \ + 3: \ + .word 1b; \ + .word 2b; \ + .hword __line; \ + .hword 0; \ .popsection;/*
Signature · 1 line
2.55.0
PATCH 3/11 ARM: BUG: Move the section right after the opcode
In reply to Sebastian Andrzej SiewiorMove the references to the opcode right after the opcode. This requires to push the section __bug_table first and keep it open. The .rodata.str section will nest into it but this is not a problem.
This simplifies the splitting later on.
--- arch/arm/include/asm/bug.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
arch/arm/include/asm/bug.h +3 -3
@@ -30,13 +30,13 @@#define __BUG__LINE (__file, __line, __value) \ 1: ; \ BUG_ENCODING(); \ + .pushsection __bug_table,"aw"; \ + .align 2; \ + .word 1b; \ .pushsection .rodata.str, "aMS", %progbits, 1; \ 2: .asciz __file; \ .popsection; \ - .pushsection __bug_table,"aw"; \ - .align 2; \ 3: \ - .word 1b; \ .word 2b; \ .hword __line; \ .hword 0; \
Signature · 1 line
2.55.0
PATCH 4/11 ARM: BUG: Move file/ line encoding into a custom macro
In reply to Sebastian Andrzej SiewiorMove the filename encoding and the line number into a custom macro named _BUG_VERBOSE_LINE(). This makes it simpler to add this entries depending on CONFIG_DEBUG_BUGVERBOSE.
--- arch/arm/include/asm/bug.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
arch/arm/include/asm/bug.h +9 -6
@@ -27,18 +27,21 @@#ifdef CONFIG_DEBUG_BUGVERBOSE -#define __BUG__LINE (__file, __line, __value) \ - 1: ; \ - BUG_ENCODING(); \ - .pushsection __bug_table,"aw"; \ - .align 2; \ - .word 1b; \ +#define _BUG_VERBOSE_LINE (__file, __line) \ .pushsection .rodata.str, "aMS", %progbits, 1; \ 2: .asciz __file; \ .popsection; \ 3: \ .word 2b; \ .hword __line; \ + +#define __BUG__LINE (__file, __line, __value) \ + 1: ; \ + BUG_ENCODING(); \ + .pushsection __bug_table,"aw"; \ + .align 2; \ + .word 1b; \ + _BUG_VERBOSE_LINE(__file, __line); \ .hword 0; \ .popsection;
Signature · 1 line
2.55.0
PATCH 5/11 ARM: BUG: Move __bug_table creation to __BUG_ENTRY_GENERIC() macro
In reply to Sebastian Andrzej SiewiorMove the creation of the __bug_table section into a custom macro __BUG_ENTRY_GENERIC() and make it depend on CONFIG_GENERIC_BUG. This is preparatory work to make the creation of BUG-opcode and the additional tables depend on CONFIG_GENERIC_BUG instead CONFIG_DEBUG_BUGVERBOSE.
--- arch/arm/include/asm/bug.h | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)
arch/arm/include/asm/bug.h +11 -3
@@ -35,9 +35,8 @@ .word 2b; \ .hword __line; \ -#define __BUG__LINE (__file, __line, __value) \ - 1: ; \ - BUG_ENCODING(); \ +#ifdef CONFIG_GENERIC_BUG +#define __BUG_ENTRY_GENERIC (__file, __line, __value) \ .pushsection __bug_table,"aw"; \ .align 2; \ .word 1b; \
@@ -45,6 +44,15 @@ .hword 0; \ .popsection; +#else +#define __BUG_ENTRY_GENERIC (__file, __line, __value) +#endif + +#define __BUG__LINE (__file, __line, __value) \ + 1: ; \ + BUG_ENCODING(); \ + __BUG_ENTRY_GENERIC(__file, __line, __value); +/* * The extra indirection is to ensure that the __FILE__ string comes through * OK. Many version of gcc do not support the asm %c parameter which would be
Signature · 1 line
2.55.0
PATCH 6/11 ARM: BUG: Move the __BUG() out of CONFIG_DEBUG_BUGVERBOSE
In reply to Sebastian Andrzej SiewiorMove the __BUG() macro out of the CONFIG_DEBUG_BUGVERBOSE block and provide an empty _BUG_VERBOSE_LINE() macro for the !CONFIG_DEBUG_BUGVERBOSE case.
This allows the removal of the other __BUG implementation (in the old !CONFIG_DEBUG_BUGVERBOSE case). Remove BUG_INSTR which has no users now.
The side effect is that the entries in __bug_table are still added but without FILE and LINE.
--- arch/arm/include/asm/bug.h | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-)
arch/arm/include/asm/bug.h +4 -12
@@ -13,11 +13,9 @@*/ #ifdef CONFIG_THUMB2_KERNEL #define BUG_INSTR_VALUE 0xde02 -#define BUG_INSTR (__value) __inst_thumb16(__value)#define BUG_ENCODING () .short ___asm_opcode_to_mem_thumb16(BUG_INSTR_VALUE)#else #define BUG_INSTR_VALUE 0xe7f001f2 -#define BUG_INSTR (__value) __inst_arm(__value)#define BUG_ENCODING () .long ___asm_opcode_to_mem_arm(BUG_INSTR_VALUE)#endif
@@ -26,7 +24,6 @@#define _BUG (file, line, value) __BUG(file, line, value)#ifdef CONFIG_DEBUG_BUGVERBOSE -#define _BUG_VERBOSE_LINE (__file, __line) \ .pushsection .rodata.str, "aMS", %progbits, 1; \ 2: .asciz __file; \
@@ -35,6 +32,10 @@ .word 2b; \ .hword __line; \ +#else +#define _BUG_VERBOSE_LINE (__file, __line) +#endif +#ifdef CONFIG_GENERIC_BUG #define __BUG_ENTRY_GENERIC (__file, __line, __value) \ .pushsection __bug_table,"aw"; \
@@ -65,15 +66,6 @@ do { \unreachable (); \ } while (0) -#else - -#define __BUG (__file, __line, __value) \ -do { \ - asm volatile(BUG_INSTR(__value) "\n"); \ - unreachable(); \ -} while (0) -#endif /* CONFIG_DEBUG_BUGVERBOSE */ -#define HAVE_ARCH_BUG #include <asm-generic/bug.h>
Signature · 1 line
2.55.0
PATCH 7/11 ARM: BUG: Pass a flags argument to __BUG__LINE()
In reply to Sebastian Andrzej SiewiorSince the hard coding of the BUG-opcode, the value argument remained unused. Repurpose that argument as flags from struct bug_entry. For now this is set to 0.
--- arch/arm/include/asm/bug.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
arch/arm/include/asm/bug.h +9 -9
@@ -20,8 +20,8 @@#endif -#define BUG () _BUG(__FILE__, __LINE__, BUG_INSTR_VALUE) -#define _BUG (file, line, value) __BUG(file, line, value) +#define BUG () _BUG(__FILE__, __LINE__) +#define _BUG (file, line) __BUG(file, line)#ifdef CONFIG_DEBUG_BUGVERBOSE #define _BUG_VERBOSE_LINE (__file, __line) \
@@ -37,22 +37,22 @@#endif #ifdef CONFIG_GENERIC_BUG -#define __BUG_ENTRY_GENERIC (__file, __line, __value) \ +#define __BUG_ENTRY_GENERIC (__file, __line, __flags) \ .pushsection __bug_table,"aw"; \ .align 2; \ .word 1b; \ _BUG_VERBOSE_LINE(__file, __line); \ - .hword 0; \ + .hword __flags; \ .popsection;#else -#define __BUG_ENTRY_GENERIC (__file, __line, __value) +#define __BUG_ENTRY_GENERIC (__file, __line, __flags)#endif -#define __BUG__LINE (__file, __line, __value) \ +#define __BUG__LINE (__file, __line, __flags) \ 1: ; \ BUG_ENCODING(); \ - __BUG_ENTRY_GENERIC(__file, __line, __value); + __BUG_ENTRY_GENERIC(__file, __line, __flags);/* * The extra indirection is to ensure that the __FILE__ string comes through
@@ -60,9 +60,9 @@* preferable to this unpleasantness. We use mergeable string sections to * avoid multiple copies of the string appearing in the kernel image. */ -#define __BUG (__file, __line, __value) \ +#define __BUG (__file, __line) \ do { \ - asm volatile(__stringify(__BUG__LINE(__file, __line, __value))); \ + asm volatile(__stringify(__BUG__LINE(__file, __line, 0))); \ unreachable(); \ } while (0)
Signature · 1 line
2.55.0
PATCH 8/11 ARM: BUG: Move BUG() into the do-while loop
In reply to Sebastian Andrzej SiewiorMake the BUG() macro use the do-while loop and make the __BUG_FLAGS() use the extra indirection. This shift makes it possible to use __BUG_FLAGS() macro without having the unreachable() macro.
--- arch/arm/include/asm/bug.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
arch/arm/include/asm/bug.h +4 -5
@@ -19,9 +19,8 @@#define BUG_ENCODING () .long ___asm_opcode_to_mem_arm(BUG_INSTR_VALUE)#endif - -#define BUG () _BUG(__FILE__, __LINE__) -#define _BUG (file, line) __BUG(file, line) +#define __BUG_FLAGS (__file, __line, __flags) __BUG_FLAGS_(__file, __line, __flags) +#define __BUG_FLAGS_ (__file, __line, __flags) __BUG__LINE(__file, __line, __flags)#ifdef CONFIG_DEBUG_BUGVERBOSE #define _BUG_VERBOSE_LINE (__file, __line) \
@@ -60,9 +59,9 @@* preferable to this unpleasantness. We use mergeable string sections to * avoid multiple copies of the string appearing in the kernel image. */ -#define __BUG (__file, __line) \ +#define BUG () \ do { \ - asm volatile(__stringify(__BUG__LINE(__file, __line, 0))); \ + asm volatile(__stringify(__BUG_FLAGS(__FILE__, __LINE__, 0))); \ unreachable(); \ } while (0)
Signature · 1 line
2.55.0
PATCH 9/11 ARM: traps: Move the BUG handling from die() to do_undefinstr()
In reply to Sebastian Andrzej SiewiorThe BUG() macro uses an opcode which is treated as an invalid opcode by the ARM core. There is no dedicated handling for this. Instead, should the core step onto an invalid opcode then the do_undefinstr() will eventually end in die(). At this point, the kernel will crash and stop invoking opcodes.
Move the BUG handling earlier to do_undefinstr() in the kernel block. This allows a different handling if a BUG-entry is found.
--- arch/arm/kernel/traps.c | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-)
arch/arm/kernel/traps.c +26 -6
@@ -355,14 +355,10 @@ static void oops_end(unsigned long flags, struct pt_regs *regs, int signr)*/ void die (const char *str,struct pt_regs *regs,int err) { -enum bug_trap_type bug_type =BUG_TRAP_TYPE_NONE ; -unsigned long flags =oops_begin (); +unsigned long flags;int sig =SIGSEGV ; -if (!user_mode (regs)) - bug_type =report_bug (regs->ARM_pc, regs); -if (bug_type !=BUG_TRAP_TYPE_NONE ) - str ="Oops - BUG" ; + flags =oops_begin ();if (__die (str, err, regs)) sig =0 ;
@@ -442,6 +438,26 @@ int call_undef_hook(struct pt_regs *regs, unsigned int instr)return fn ?fn (regs, instr) :1 ; } +static int handle_bug_opcode (unsigned int instr,struct pt_regs *regs) +{ +enum bug_trap_type bug_type; +unsigned long flags; +int sig =SIGSEGV ; + +if (instr !=BUG_INSTR_VALUE ) +return 0 ; + + bug_type =report_bug (regs->ARM_pc, regs); +if (bug_type ==BUG_TRAP_TYPE_NONE ) +return 0 ; + + flags =oops_begin (); +if (__die ("Oops - BUG" ,0 , regs)) + sig =0 ; +oops_end (flags, regs, sig); +return 1 ; +} + asmlinkage void do_undefinstr(struct pt_regs *regs) {unsigned int instr;
@@ -461,6 +477,10 @@ asmlinkage void do_undefinstr(struct pt_regs *regs) } else#endif instr =__mem_to_opcode_arm (*(u32 *) pc); + +if (handle_bug_opcode (instr, regs)) +return ; + }else if (thumb_mode (regs)) {if (get_user (instr, (u16 __user *)pc)) goto die_sig;
Signature · 1 line
2.55.0
PATCH 10/11 ARM: traps: Add handling for warnings to handle_bug_opcode()
In reply to Sebastian Andrzej SiewiorShould a WARNing be encoded with a BUG opcode then it is needed to forwarded the PC so the CPU can continue after the invalid opcode.
--- arch/arm/kernel/traps.c | 8 ++++++++ 1 file changed, 8 insertions(+)
arch/arm/kernel/traps.c +8 -0
@@ -451,6 +451,14 @@ static int handle_bug_opcode(unsigned int instr, struct pt_regs *regs)if (bug_type ==BUG_TRAP_TYPE_NONE )return 0 ; +if (bug_type ==BUG_TRAP_TYPE_WARN ) { +if (IS_ENABLED (CONFIG_THUMB2_KERNEL )) + regs->ARM_pc +=2 ; +else + regs->ARM_pc +=4 ; +return 1 ; + } + flags =oops_begin ();if (__die ("Oops - BUG" ,0 , regs)) sig =0 ;
Signature · 1 line
2.55.0
PATCH 11/11 ARM: BUG: Add WARN support via __WARN_FLAGS()
In reply to Sebastian Andrzej Siewiorthe recent rework makes it possible to add support for WARN_ON() and friends by defining __WARN_FLAGS(). The __BUG_FLAGS() macro needs to pass BUGFLAG_WARNING as option to distinguish it from BUG() entries.
--- arch/arm/include/asm/bug.h | 3 +++ 1 file changed, 3 insertions(+)
arch/arm/include/asm/bug.h +3 -0
@@ -65,6 +65,9 @@ do { \unreachable (); \ } while (0) +#define __WARN_FLAGS (cond_str, flags) \ + asm volatile(__stringify(__BUG_FLAGS(__FILE__, __LINE__, BUGFLAG_WARNING|(flags)))) +#define HAVE_ARCH_BUG #include <asm-generic/bug.h>
Signature · 1 line
2.55.0