Skip to contentSkip to conversation

x86: ASM based __bug_table and rethunks

RFC PATCH In review Peter Zijlstra 3 participants 12 messages Oct 7, 2024 – Nov 4, 2024

Subsystems (3)
lore
Share & tools
lore

Peter Zijlstra

author maintainer
#0 lore raw

cover x86: ASM based __bug_table and rethunks

Hi,

I ran into the __warn_thunk_thunk_thunk_thunk thing and urgh.

Make it a 'simple' WARN_ON_ONCE(). And since all of x86_64 has the same rewrite rules, simplify the ifdeffery madness too.

author maintainer
#1 lore raw

RFC PATCH 1/2 x86: Provide assembly __bug_table helpers

Rework the __bug_table helpers such that usage from assembly becomes possible.

---
 arch/x86/include/asm/bug.h |   50 ++++++++++++++++++---------------------------
 1 file changed, 21 insertions(+), 29 deletions(-)
arch/x86/include/asm/bug.h +21 -29
@@ -28,46 +28,38 @@
 #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
+#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)
-
-#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 + 6 + ASM_BUGTABLE_VERBOSE_SIZE ;			\
+	.popsection
 
 #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.word %c0"        "\t# bug_entry::flags\n"	\
-		     "\t.org 2b+%c1\n"					\
-		     ".popsection\n"					\
-		     extra						\
-		     : : "i" (flags),					\
-			 "i" (sizeof(struct bug_entry)));		\
+	    __stringify(ASM_BUGTABLE_FLAGS(1b, %c0, %c1, %c2)) "\n"	\
+			    extra					\
+		     : : "i" (__FILE__), "i" (__LINE__),		\
+			 "i" (flags));					\
 } while (0)
 
-#endif /* CONFIG_DEBUG_BUGVERBOSE */
-
 #else
 
 #define _BUG_FLAGS(ins, flags, extra)  asm volatile(ins)
maintainer Acked-by on v1 1/2
#2 lore raw

Re: [RFC][PATCH 1/2] x86: Provide assembly __bug_table helpers

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

Quoted 6 lines · Peter Zijlstra
arch/x86/include/asm/bug.h +6 -0
+#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 + 6 + ASM_BUGTABLE_VERBOSE_SIZE ;			\
	    ^

should be a tab?

Nice cleanup!

Signature · 1 line
Josh
maintainer
#3 lore raw

Re: [RFC][PATCH 1/2] x86: Provide assembly __bug_table helpers

In reply to Peter Zijlstra

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

Quoted 6 lines · Peter Zijlstra
arch/x86/include/asm/bug.h +6 -0
+#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 + 6 + ASM_BUGTABLE_VERBOSE_SIZE ;			\

I'm guessing this second 6 is the sizeof(long + word) of the second two?

I.e., in here:

asm __inline volatile("1:\t" ".byte 0x0f, 0x0b" "\n"

		      ".pushsection __bug_table, \"aw\" ;
		       123:
		       		.long 1b - . ;
				.long %c0 - . ;
				^^^^^^^^^^^^^
				.word %c1 ;
				.word %c2 ;
				^^^^^^^^^^


				.org 123b + 6 + 6 ;
			.popsection" "\n" ""

Can we add a define for it too instead of a naked 6?

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

Quoted 5 lines
arch/x86/include/asm/bug.h +1 -0
+	.popsection
 
 #define _BUG_FLAGS(ins, flags, extra)					\
 do {									\
 	asm_inline volatile("1:\t" ins "\n"				\

s/ins/insn/ while at it.

The usual abbreviation for an "instruction".

Quoted 13 lines
arch/x86/include/asm/bug.h +4 -8
-		     ".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)));		\
+	    __stringify(ASM_BUGTABLE_FLAGS(1b, %c0, %c1, %c2)) "\n"	\
+			    extra					\
+		     : : "i" (__FILE__), "i" (__LINE__),		\
+			 "i" (flags));					\
 } while (0)
Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
author maintainer
#4 lore raw

RFC PATCH 2/2 x86: Clean up default rethunk warning

In reply to Peter Zijlstra

Replace the funny __warn_thunk thing with a more regular WARN_ON_ONCE(), and simplify the ifdeffery.

Notably this avoids RET from having recursive RETs (once from the thunk and once from the C function) -- recursive RET makes my head hurt for no good reason.

---
 arch/x86/entry/entry.S               |    3 ---
 arch/x86/include/asm/nospec-branch.h |    2 --
 arch/x86/kernel/cpu/bugs.c           |    5 -----
 arch/x86/lib/retpoline.S             |   20 ++++++++++++--------
 4 files changed, 12 insertions(+), 18 deletions(-)
arch/x86/entry/entry.S +0 -3
@@ -10,8 +10,6 @@
 #include <asm/segment.h>
 #include <asm/cache.h>
 
-#include "calling.h"
-
 .pushsection .noinstr.text, "ax"
 
 SYM_FUNC_START(entry_ibpb)
@@ -45,4 +43,3 @@ EXPORT_SYMBOL_GPL(mds_verw_sel);
 
 .popsection
 
-THUNK warn_thunk_thunk, __warn_thunk
arch/x86/include/asm/nospec-branch.h +0 -2
@@ -387,8 +387,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
@@ -3025,8 +3025,3 @@ ssize_t cpu_show_reg_file_data_sampling(
 	return cpu_show_common(dev, attr, buf, X86_BUG_RFDS);
 }
 #endif
-
-void __warn_thunk(void)
-{
-	WARN_ONCE(1, "Unpatched return thunk in use. This should not happen!\n");
-}
arch/x86/lib/retpoline.S +12 -8
@@ -12,9 +12,14 @@
 #include <asm/percpu.h>
 #include <asm/frame.h>
 #include <asm/nops.h>
+#include <asm/bug.h>
 
-	.section .text..__x86.indirect_thunk
+#define WARN_ONCE							\
+	1: ALTERNATIVE "", "ud2", X86_FEATURE_ALWAYS ;			\
+	ASM_BUGTABLE_FLAGS(1b, 0, 0, BUGFLAG_WARNING | BUGFLAG_ONCE) ;	\
+	REACHABLE
 
+	.section .text..__x86.indirect_thunk
 
 .macro POLINE reg
 	ANNOTATE_INTRA_FUNCTION_CALL
@@ -382,16 +387,15 @@ SYM_FUNC_END(call_depth_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
+
+#ifdef CONFIG_X86_64
+	WARN_ONCE
+#endif
+
 	ANNOTATE_UNRET_SAFE
 	ret
-#endif
 	int3
+
 SYM_CODE_END(__x86_return_thunk)
 EXPORT_SYMBOL(__x86_return_thunk)
 
maintainer
#5 lore raw

Re: [RFC][PATCH 2/2] x86: Clean up default rethunk warning

Quoted 6 lines · Peter Zijlstra

Replace the funny __warn_thunk thing with a more regular WARN_ON_ONCE(), and simplify the ifdeffery.

Notably this avoids RET from having recursive RETs (once from the thunk and once from the C function) -- recursive RET makes my head hurt for no good reason.

This could use an explanation for why the ifdefs can be removed and why the alternative can be removed.

arch/x86/lib/retpoline.S line 20

Quoted 4 lines
arch/x86/lib/retpoline.S +4 -0
+#define WARN_ONCE							\
+	1: ALTERNATIVE "", "ud2", X86_FEATURE_ALWAYS ;			\
+	ASM_BUGTABLE_FLAGS(1b, 0, 0, BUGFLAG_WARNING | BUGFLAG_ONCE) ;	\
+	REACHABLE

Can we not use __FILE__ and __LINE__ here? Also why not put this in asm/bug.h?

arch/x86/lib/retpoline.S SYM_FUNC_END(call_depth_return_thunk) · line 393

Quoted 13 lines

SYM_CODE_START(__x86_return_thunk)

arch/x86/lib/retpoline.S +4 -6
 	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
+
+#ifdef CONFIG_X86_64
+	WARN_ONCE
+#endif

Isn't this return thunk used before apply_returns()? How does that not trigger the warning?

Signature · 1 line
Josh
author maintainer
#6 lore raw

Re: [RFC][PATCH 2/2] x86: Clean up default rethunk warning

Quoted 10 lines · Josh Poimboeuf
Quoted 6 lines · Peter Zijlstra

Replace the funny __warn_thunk thing with a more regular WARN_ON_ONCE(), and simplify the ifdeffery.

Notably this avoids RET from having recursive RETs (once from the thunk and once from the C function) -- recursive RET makes my head hurt for no good reason.

This could use an explanation for why the ifdefs can be removed and why the alternative can be removed.

The alternative is in the WARN_ONCE now.

Quoted 6 lines

arch/x86/lib/retpoline.S line 20

Quoted 4 lines
arch/x86/lib/retpoline.S +4 -0
+#define WARN_ONCE							\
+	1: ALTERNATIVE "", "ud2", X86_FEATURE_ALWAYS ;			\
+	ASM_BUGTABLE_FLAGS(1b, 0, 0, BUGFLAG_WARNING | BUGFLAG_ONCE) ;	\
+	REACHABLE

Can we not use __FILE__ and __LINE__ here?

Because for asm, __FILE__ is spelled like:

#ifdef CONFIG_DEBUG_BUGVERBOSE
.pushsection .rodata.str1.1, "aMS",@progbits,1
.LC0:

	.string __FILE__
.popsection
#endif
	1: ALTERNATIVE "", "ud2", X86_FEATURE_ALWAYS
	ASM_BUGTABLE_FLAGS(1b, LC0b, __LINE__, BUGFLAG_WARNING | BUGFLAG_ONCE)
	REACHABLE

And I didn't feel the whole thing was worth the trouble, if NULL bug will only print the symbol name and that should be clear enough.

Quoted 1 line

Also why not put this in asm/bug.h?

Because the ALTERNATIVE..

Quoted 8 lines
Quoted 8 lines

SYM_CODE_START(__x86_return_thunk)

arch/x86/lib/retpoline.S +0 -5
 	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
Quoted 8 lines

arch/x86/lib/retpoline.S SYM_FUNC_END(call_depth_return_thunk) · line 393

Quoted 5 lines
arch/x86/lib/retpoline.S +4 -1
-#else
+
+#ifdef CONFIG_X86_64
+	WARN_ONCE
+#endif

Isn't this return thunk used before apply_returns()? How does that not trigger the warning?

You missed the ALTERNATIVE I squirreled away in the WARN thing :-)

maintainer
#7 lore raw

Re: [RFC][PATCH 2/2] x86: Clean up default rethunk warning

Quoted 5 lines · Peter Zijlstra
Quoted 4 lines · Josh Poimboeuf
Quoted 3 lines · Peter Zijlstra

Replace the funny __warn_thunk thing with a more regular WARN_ON_ONCE(), and simplify the ifdeffery.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Notably this avoids RET from having recursive RETs (once from the thunk and once from the C function) -- recursive RET makes my head hurt for no good reason.

This could use an explanation for why the ifdefs can be removed and why the alternative can be removed.

The alternative is in the WARN_ONCE now.

Ah, sneaky... It should really be called WARN_ONCE_AFTER_ALTERNATIVES or something.

Signature · 1 line
Josh
author maintainer
#8 lore raw

Re: [RFC][PATCH 2/2] x86: Clean up default rethunk warning

Quoted 10 lines · Josh Poimboeuf
Quoted 9 lines · Peter Zijlstra
Quoted 8 lines · Josh Poimboeuf
Quoted 6 lines · Peter Zijlstra

Replace the funny __warn_thunk thing with a more regular WARN_ON_ONCE(), and simplify the ifdeffery.

Notably this avoids RET from having recursive RETs (once from the thunk and once from the C function) -- recursive RET makes my head hurt for no good reason.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

This could use an explanation for why the ifdefs can be removed and why the alternative can be removed.

The alternative is in the WARN_ONCE now.

Ah, sneaky... It should really be called WARN_ONCE_AFTER_ALTERNATIVES or something.

Yeah, I suppose it should.

maintainer
#9 lore raw

Re: [RFC][PATCH 2/2] x86: Clean up default rethunk warning

In reply to Peter Zijlstra

arch/x86/lib/retpoline.S line 17

Quoted 2 lines · Peter Zijlstra
arch/x86/lib/retpoline.S +1 -1
-	.section .text..__x86.indirect_thunk
+#define WARN_ONCE							\

This should be in the asm section of arch/x86/include/asm/bug.h so that other asm code can use it. It will come in handy...

arch/x86/lib/retpoline.S line 18

Quoted 1 line
arch/x86/lib/retpoline.S +1 -0
+	1: ALTERNATIVE "", "ud2", X86_FEATURE_ALWAYS ;			\

... but uff, you can't because of this ALTERNATIVE. This is a conditional WARN_ONCE. Yuck.

I guess ALT_WARN_ONCE or so...

Quoted 11 lines
arch/x86/lib/retpoline.S +3 -0
+	ASM_BUGTABLE_FLAGS(1b, 0, 0, BUGFLAG_WARNING | BUGFLAG_ONCE) ;	\
+	REACHABLE
 
+	.section .text..__x86.indirect_thunk
 
 .macro POLINE reg
 	ANNOTATE_INTRA_FUNCTION_CALL
@@ -382,16 +387,15 @@ SYM_FUNC_END(call_depth_return_thunk)
 SYM_CODE_START(__x86_return_thunk)
 	UNWIND_HINT_FUNC
 	ANNOTATE_NOENDBR

arch/x86/lib/retpoline.S SYM_FUNC_END(call_depth_return_thunk) · line 393

Quoted 10 lines
arch/x86/lib/retpoline.S +4 -6
-#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
+
+#ifdef CONFIG_X86_64
+	WARN_ONCE
+#endif

And you can add an empty 32-bit WARN_ONCE macro so that we don't have this ifdeffery here where ifdeffery gives the last drop of making this file totally unreadable...

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
author maintainer
#10 lore raw

Re: [RFC][PATCH 2/2] x86: Clean up default rethunk warning

Quoted 7 lines · Borislav Petkov

arch/x86/lib/retpoline.S line 17

Quoted 2 lines · Peter Zijlstra
arch/x86/lib/retpoline.S +1 -1
-	.section .text..__x86.indirect_thunk
+#define WARN_ONCE							\

This should be in the asm section of arch/x86/include/asm/bug.h so that other asm code can use it. It will come in handy...

Quoted 6 lines

arch/x86/lib/retpoline.S line 18

Quoted 1 line
arch/x86/lib/retpoline.S +1 -0
+	1: ALTERNATIVE "", "ud2", X86_FEATURE_ALWAYS ;			\

... but uff, you can't because of this ALTERNATIVE. This is a conditional WARN_ONCE. Yuck.

I guess ALT_WARN_ONCE or so...

Yeah, Josh already said similar things.

Quoted 17 lines
Quoted 17 lines
arch/x86/lib/retpoline.S +3 -6
+	ASM_BUGTABLE_FLAGS(1b, 0, 0, BUGFLAG_WARNING | BUGFLAG_ONCE) ;	\
+	REACHABLE
 
+	.section .text..__x86.indirect_thunk
 
 .macro POLINE reg
 	ANNOTATE_INTRA_FUNCTION_CALL
@@ -382,16 +387,15 @@ SYM_FUNC_END(call_depth_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
Quoted 8 lines

arch/x86/lib/retpoline.S SYM_FUNC_END(call_depth_return_thunk) · line 393

Quoted 4 lines
arch/x86/lib/retpoline.S +4 -0
+
+#ifdef CONFIG_X86_64
+	WARN_ONCE
+#endif

And you can add an empty 32-bit WARN_ONCE macro so that we don't have this ifdeffery here where ifdeffery gives the last drop of making this file totally unreadable...

I just realized all the rethunk crap is 64bit only anyway. So it don't matter.

But the reason I did this is that we never rewrite thunk calls on 32bit (really, we should just strip all mitigation shit from it and leave it to rot).

maintainer
#11 lore raw

Re: [RFC][PATCH 2/2] x86: Clean up default rethunk warning

Quoted 6 lines · Peter Zijlstra

I just realized all the rethunk crap is 64bit only anyway. So it don't matter.

But the reason I did this is that we never rewrite thunk calls on 32bit (really, we should just strip all mitigation shit from it and leave it to rot).

Perfectly fine with me.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette