Skip to content

Commit 1e688dd

Browse files
chleroympe
authored andcommitted
powerpc/bug: Provide better flexibility to WARN_ON/__WARN_FLAGS() with asm goto
Using asm goto in __WARN_FLAGS() and WARN_ON() allows more flexibility to GCC. For that add an entry to the exception table so that program_check_exception() knowns where to resume execution after a WARNING. Here are two exemples. The first one is done on PPC32 (which benefits from the previous patch), the second is on PPC64. unsigned long test(struct pt_regs *regs) { int ret; WARN_ON(regs->msr & MSR_PR); return regs->gpr[3]; } unsigned long test9w(unsigned long a, unsigned long b) { if (WARN_ON(!b)) return 0; return a / b; } Before the patch: 000003a8 <test>: 3a8: 81 23 00 84 lwz r9,132(r3) 3ac: 71 29 40 00 andi. r9,r9,16384 3b0: 40 82 00 0c bne 3bc <test+0x14> 3b4: 80 63 00 0c lwz r3,12(r3) 3b8: 4e 80 00 20 blr 3bc: 0f e0 00 00 twui r0,0 3c0: 80 63 00 0c lwz r3,12(r3) 3c4: 4e 80 00 20 blr 0000000000000bf0 <.test9w>: bf0: 7c 89 00 74 cntlzd r9,r4 bf4: 79 29 d1 82 rldicl r9,r9,58,6 bf8: 0b 09 00 00 tdnei r9,0 bfc: 2c 24 00 00 cmpdi r4,0 c00: 41 82 00 0c beq c0c <.test9w+0x1c> c04: 7c 63 23 92 divdu r3,r3,r4 c08: 4e 80 00 20 blr c0c: 38 60 00 00 li r3,0 c10: 4e 80 00 20 blr After the patch: 000003a8 <test>: 3a8: 81 23 00 84 lwz r9,132(r3) 3ac: 71 29 40 00 andi. r9,r9,16384 3b0: 40 82 00 0c bne 3bc <test+0x14> 3b4: 80 63 00 0c lwz r3,12(r3) 3b8: 4e 80 00 20 blr 3bc: 0f e0 00 00 twui r0,0 0000000000000c50 <.test9w>: c50: 7c 89 00 74 cntlzd r9,r4 c54: 79 29 d1 82 rldicl r9,r9,58,6 c58: 0b 09 00 00 tdnei r9,0 c5c: 7c 63 23 92 divdu r3,r3,r4 c60: 4e 80 00 20 blr c70: 38 60 00 00 li r3,0 c74: 4e 80 00 20 blr In the first exemple, we see GCC doesn't need to duplicate what happens after the trap. In the second exemple, we see that GCC doesn't need to emit a test and a branch in the likely path in addition to the trap. We've got some WARN_ON() in .softirqentry.text section so it needs to be added in the OTHER_TEXT_SECTIONS in modpost.c Signed-off-by: Christophe Leroy <christophe.leroy@csgroup.eu> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au> Link: https://lore.kernel.org/r/389962b1b702e3c78d169e59bcfac56282889173.1618331882.git.christophe.leroy@csgroup.eu
1 parent db87a71 commit 1e688dd

9 files changed

Lines changed: 72 additions & 25 deletions

File tree

‎arch/powerpc/include/asm/book3s/64/kup.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
/* Prevent access to userspace using any key values */
9191
LOAD_REG_IMMEDIATE(\gpr2, AMR_KUAP_BLOCKED)
9292
999: tdne \gpr1, \gpr2
93-
EMIT_BUG_ENTRY 999b, __FILE__, __LINE__, (BUGFLAG_WARNING | BUGFLAG_ONCE)
93+
EMIT_WARN_ENTRY 999b, __FILE__, __LINE__, (BUGFLAG_WARNING | BUGFLAG_ONCE)
9494
END_MMU_FTR_SECTION_NESTED_IFSET(MMU_FTR_BOOK3S_KUAP, 67)
9595
#endif
9696
.endm

‎arch/powerpc/include/asm/bug.h‎

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#ifdef __KERNEL__
55

66
#include <asm/asm-compat.h>
7+
#include <asm/extable.h>
78

89
#ifdef CONFIG_BUG
910

@@ -30,6 +31,11 @@
3031
.endm
3132
#endif /* verbose */
3233

34+
.macro EMIT_WARN_ENTRY addr,file,line,flags
35+
EX_TABLE(\addr,\addr+4)
36+
EMIT_BUG_ENTRY \addr,\file,\line,\flags
37+
.endm
38+
3339
#else /* !__ASSEMBLY__ */
3440
/* _EMIT_BUG_ENTRY expects args %0,%1,%2,%3 to be FILE, LINE, flags and
3541
sizeof(struct bug_entry), respectively */
@@ -58,6 +64,16 @@
5864
"i" (sizeof(struct bug_entry)), \
5965
##__VA_ARGS__)
6066

67+
#define WARN_ENTRY(insn, flags, label, ...) \
68+
asm_volatile_goto( \
69+
"1: " insn "\n" \
70+
EX_TABLE(1b, %l[label]) \
71+
_EMIT_BUG_ENTRY \
72+
: : "i" (__FILE__), "i" (__LINE__), \
73+
"i" (flags), \
74+
"i" (sizeof(struct bug_entry)), \
75+
##__VA_ARGS__ : : label)
76+
6177
/*
6278
* BUG_ON() and WARN_ON() do their best to cooperate with compile-time
6379
* optimisations. However depending on the complexity of the condition
@@ -70,7 +86,15 @@
7086
} while (0)
7187
#define HAVE_ARCH_BUG
7288

73-
#define __WARN_FLAGS(flags) BUG_ENTRY("twi 31, 0, 0", BUGFLAG_WARNING | (flags))
89+
#define __WARN_FLAGS(flags) do { \
90+
__label__ __label_warn_on; \
91+
\
92+
WARN_ENTRY("twi 31, 0, 0", BUGFLAG_WARNING | (flags), __label_warn_on); \
93+
unreachable(); \
94+
\
95+
__label_warn_on: \
96+
break; \
97+
} while (0)
7498

7599
#ifdef CONFIG_PPC64
76100
#define BUG_ON(x) do { \
@@ -83,15 +107,24 @@
83107
} while (0)
84108

85109
#define WARN_ON(x) ({ \
86-
int __ret_warn_on = !!(x); \
87-
if (__builtin_constant_p(__ret_warn_on)) { \
88-
if (__ret_warn_on) \
110+
bool __ret_warn_on = false; \
111+
do { \
112+
if (__builtin_constant_p((x))) { \
113+
if (!(x)) \
114+
break; \
89115
__WARN(); \
90-
} else { \
91-
BUG_ENTRY(PPC_TLNEI " %4, 0", \
92-
BUGFLAG_WARNING | BUGFLAG_TAINT(TAINT_WARN), \
93-
"r" (__ret_warn_on)); \
94-
} \
116+
__ret_warn_on = true; \
117+
} else { \
118+
__label__ __label_warn_on; \
119+
\
120+
WARN_ENTRY(PPC_TLNEI " %4, 0", \
121+
BUGFLAG_WARNING | BUGFLAG_TAINT(TAINT_WARN), \
122+
__label_warn_on, "r" (x)); \
123+
break; \
124+
__label_warn_on: \
125+
__ret_warn_on = true; \
126+
} \
127+
} while (0); \
95128
unlikely(__ret_warn_on); \
96129
})
97130

@@ -104,8 +137,11 @@
104137
#ifdef __ASSEMBLY__
105138
.macro EMIT_BUG_ENTRY addr,file,line,flags
106139
.endm
140+
.macro EMIT_WARN_ENTRY addr,file,line,flags
141+
.endm
107142
#else /* !__ASSEMBLY__ */
108143
#define _EMIT_BUG_ENTRY
144+
#define _EMIT_WARN_ENTRY
109145
#endif
110146
#endif /* CONFIG_BUG */
111147

‎arch/powerpc/include/asm/extable.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
#define ARCH_HAS_RELATIVE_EXTABLE
1919

20+
#ifndef __ASSEMBLY__
21+
2022
struct exception_table_entry {
2123
int insn;
2224
int fixup;
@@ -28,3 +30,15 @@ static inline unsigned long extable_fixup(const struct exception_table_entry *x)
2830
}
2931

3032
#endif
33+
34+
/*
35+
* Helper macro for exception table entries
36+
*/
37+
#define EX_TABLE(_fault, _target) \
38+
stringify_in_c(.section __ex_table,"a";)\
39+
stringify_in_c(.balign 4;) \
40+
stringify_in_c(.long (_fault) - . ;) \
41+
stringify_in_c(.long (_target) - . ;) \
42+
stringify_in_c(.previous)
43+
44+
#endif

‎arch/powerpc/include/asm/ppc_asm.h‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <asm/ppc-opcode.h>
1111
#include <asm/firmware.h>
1212
#include <asm/feature-fixups.h>
13+
#include <asm/extable.h>
1314

1415
#ifdef __ASSEMBLY__
1516

@@ -752,16 +753,6 @@ END_FTR_SECTION_NESTED(CPU_FTR_CELL_TB_BUG, CPU_FTR_CELL_TB_BUG, 96)
752753

753754
#endif /* __ASSEMBLY__ */
754755

755-
/*
756-
* Helper macro for exception table entries
757-
*/
758-
#define EX_TABLE(_fault, _target) \
759-
stringify_in_c(.section __ex_table,"a";)\
760-
stringify_in_c(.balign 4;) \
761-
stringify_in_c(.long (_fault) - . ;) \
762-
stringify_in_c(.long (_target) - . ;) \
763-
stringify_in_c(.previous)
764-
765756
#define SOFT_MASK_TABLE(_start, _end) \
766757
stringify_in_c(.section __soft_mask_table,"a";)\
767758
stringify_in_c(.balign 8;) \

‎arch/powerpc/kernel/entry_64.S‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ _GLOBAL(enter_rtas)
309309
*/
310310
lbz r0,PACAIRQSOFTMASK(r13)
311311
1: tdeqi r0,IRQS_ENABLED
312-
EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,BUGFLAG_WARNING
312+
EMIT_WARN_ENTRY 1b,__FILE__,__LINE__,BUGFLAG_WARNING
313313
#endif
314314

315315
/* Hard-disable interrupts */

‎arch/powerpc/kernel/misc_32.S‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ _GLOBAL(copy_page)
237237
addi r3,r3,-4
238238

239239
0: twnei r5, 0 /* WARN if r3 is not cache aligned */
240-
EMIT_BUG_ENTRY 0b,__FILE__,__LINE__, BUGFLAG_WARNING
240+
EMIT_WARN_ENTRY 0b,__FILE__,__LINE__, BUGFLAG_WARNING
241241

242242
addi r4,r4,-4
243243

‎arch/powerpc/kernel/traps.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,8 +1477,13 @@ static void do_program_check(struct pt_regs *regs)
14771477

14781478
if (!(regs->msr & MSR_PR) && /* not user-mode */
14791479
report_bug(bugaddr, regs) == BUG_TRAP_TYPE_WARN) {
1480-
regs_add_return_ip(regs, 4);
1481-
return;
1480+
const struct exception_table_entry *entry;
1481+
1482+
entry = search_exception_tables(bugaddr);
1483+
if (entry) {
1484+
regs_set_return_ip(regs, extable_fixup(entry) + regs->nip - bugaddr);
1485+
return;
1486+
}
14821487
}
14831488
_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
14841489
return;

‎scripts/mod/modpost.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ static void check_section(const char *modname, struct elf_info *elf,
931931
".kprobes.text", ".cpuidle.text", ".noinstr.text"
932932
#define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
933933
".fixup", ".entry.text", ".exception.text", ".text.*", \
934-
".coldtext"
934+
".coldtext", ".softirqentry.text"
935935

936936
#define INIT_SECTIONS ".init.*"
937937
#define MEM_INIT_SECTIONS ".meminit.*"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../../../arch/powerpc/include/asm/extable.h

0 commit comments

Comments
 (0)