[PATCH 5.15 094/245] x86/alternative: Make custom return thunk unconditional

Greg Kroah-Hartman
2024-02-27T14:06:32Z

From: Peter Zijlstra <peterz@infradead.org>

Upstream commit: 095b8303f3835c68ac4a8b6d754ca1c3b6230711

There is infrastructure to rewrite return thunks to point to any
random thunk one desires, unwrap that from CALL_THUNKS, which up to
now was the sole user of that.

[ bp: Make the thunks visible on 32-bit and add ifdeffery for the
32-bit builds. ]

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Link: https://lore.kernel.org/r/20230814121148.775293785@infradead.org
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
arch/x86/include/asm/nospec-branch.h | 8 ++++----
arch/x86/kernel/alternative.c | 4 ----
arch/x86/kernel/cpu/bugs.c | 2 ++
3 files changed, 6 insertions(+), 8 deletions(-)

--- a/arch/x86/include/asm/nospec-branch.h
+++ b/arch/x86/include/asm/nospec-branch.h
@@ -190,7 +190,11 @@
_ASM_PTR " 999b\n\t" \
".popsection\n\t"

+#ifdef CONFIG_RETHUNK
extern void __x86_return_thunk(void);
+#else
+static inline void __x86_return_thunk(void) {}
+#endif

extern void retbleed_return_thunk(void);
extern void srso_return_thunk(void);
@@ -203,11 +207,7 @@ extern void srso_alias_untrain_ret(void)
extern void entry_untrain_ret(void);
extern void entry_ibpb(void);

-#ifdef CONFIG_CALL_THUNKS
extern void (*x86_return_thunk)(void);
-#else
-#define x86_return_thunk (&__x86_return_thunk)
-#endif

#ifdef CONFIG_RETPOLINE

--- a/arch/x86/kernel/alternative.c
+++ b/arch/x86/kernel/alternative.c
@@ -522,10 +522,6 @@ void __init_or_module noinline apply_ret

#ifdef CONFIG_RETHUNK

-#ifdef CONFIG_CALL_THUNKS
-void (*x86_return_thunk)(void) __ro_after_init = &__x86_return_thunk;
-#endif
-
/*
* Rewrite the compiler generated return thunk tail-calls.
*
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -62,6 +62,8 @@ EXPORT_SYMBOL_GPL(x86_pred_cmd);

static DEFINE_MUTEX(spec_ctrl_mutex);

+void (*x86_return_thunk)(void) __ro_after_init = &__x86_return_thunk;
+
/* Update SPEC_CTRL MSR and its cached copy unconditionally */
static void update_spec_ctrl(u64 val)
{