]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
x86/returnthunk: Allow different return thunks
authorPeter Zijlstra <peterz@infradead.org>
Thu, 15 Sep 2022 11:11:25 +0000 (13:11 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 1 Mar 2024 12:16:49 +0000 (13:16 +0100)
Upstream commit: 770ae1b709528a6a173b5c7b183818ee9b45e376

In preparation for call depth tracking on Intel SKL CPUs, make it possible
to patch in a SKL specific return thunk.

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lore.kernel.org/r/20220915111147.680469665@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
arch/x86/kernel/alternative.c
arch/x86/kernel/cpu/bugs.c
arch/x86/kernel/ftrace.c
arch/x86/kernel/static_call.c
arch/x86/net/bpf_jit_comp.c

index 7b4782249a9250737c8abc517677fb5e56523844..142444a4f94df7db30c70fa06f56d86db365044c 100644 (file)
@@ -207,6 +207,12 @@ 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
 
 typedef u8 retpoline_thunk_t[RETPOLINE_THUNK_SIZE];
index 9e0a3daa838c23423233945dd682d3fed91f43cc..4d3715f6aaab3ed366ce9c5cb1902607292162f9 100644 (file)
@@ -676,6 +676,11 @@ void __init_or_module noinline apply_retpolines(s32 *start, s32 *end)
 }
 
 #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.
  *
@@ -691,14 +696,18 @@ static int patch_return(void *addr, struct insn *insn, u8 *bytes)
 {
        int i = 0;
 
-       if (cpu_feature_enabled(X86_FEATURE_RETHUNK))
-               return -1;
+       if (cpu_feature_enabled(X86_FEATURE_RETHUNK)) {
+               if (x86_return_thunk == __x86_return_thunk)
+                       return -1;
 
-       bytes[i++] = RET_INSN_OPCODE;
+               i = JMP32_INSN_SIZE;
+               __text_gen_insn(bytes, JMP32_INSN_OPCODE, addr, x86_return_thunk, i);
+       } else {
+               bytes[i++] = RET_INSN_OPCODE;
+       }
 
        for (; i < insn->length;)
                bytes[i++] = INT3_INSN_OPCODE;
-
        return i;
 }
 
index d9fda0b6eb19e2920c0c06c956b1884fae3ccc5b..e33b9ef56b21de0e9e44b3e9c287bc20635436e2 100644 (file)
@@ -61,7 +61,9 @@ EXPORT_SYMBOL_GPL(x86_pred_cmd);
 
 static DEFINE_MUTEX(spec_ctrl_mutex);
 
+#ifdef CONFIG_CALL_THUNKS
 void (*x86_return_thunk)(void) __ro_after_init = &__x86_return_thunk;
+#endif
 
 /* Update SPEC_CTRL MSR and its cached copy unconditionally */
 static void update_spec_ctrl(u64 val)
index 76a94e0ad87334fd47c9831d6ac9c11770efe3d8..46447877b59419984581a294b637fb99c8c781c4 100644 (file)
@@ -368,7 +368,7 @@ create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size)
 
        ip = trampoline + size;
        if (cpu_feature_enabled(X86_FEATURE_RETHUNK))
-               __text_gen_insn(ip, JMP32_INSN_OPCODE, ip, &__x86_return_thunk, JMP32_INSN_SIZE);
+               __text_gen_insn(ip, JMP32_INSN_OPCODE, ip, x86_return_thunk, JMP32_INSN_SIZE);
        else
                memcpy(ip, retq, sizeof(retq));
 
index 759b986b7f033f49a1e4130f08710cda8ceefd97..273e9b77b7302615b27c86fde33e2ede0e7e2695 100644 (file)
@@ -41,7 +41,7 @@ static void __ref __static_call_transform(void *insn, enum insn_type type,
 
        case RET:
                if (cpu_feature_enabled(X86_FEATURE_RETHUNK))
-                       code = text_gen_insn(JMP32_INSN_OPCODE, insn, &__x86_return_thunk);
+                       code = text_gen_insn(JMP32_INSN_OPCODE, insn, x86_return_thunk);
                else
                        code = &retinsn;
                break;
index 8e3c3d8916dd217d7d586da0734f64d785a0d482..d7d592c0929835338302146dad95248016a36808 100644 (file)
@@ -405,7 +405,7 @@ static void emit_return(u8 **pprog, u8 *ip)
        int cnt = 0;
 
        if (cpu_feature_enabled(X86_FEATURE_RETHUNK)) {
-               emit_jump(&prog, &__x86_return_thunk, ip);
+               emit_jump(&prog, x86_return_thunk, ip);
        } else {
                EMIT1(0xC3);            /* ret */
                if (IS_ENABLED(CONFIG_SLS))