]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blobdiff - arch/x86/kernel/ftrace.c
x86/ftrace: Use alternative RET encoding
[mirror_ubuntu-jammy-kernel.git] / arch / x86 / kernel / ftrace.c
index 1b3ce3b4a2a2faf94ec7044261f33483502f9e05..6bb479ce1ae4af48019f8418d4941631ac99453b 100644 (file)
@@ -308,7 +308,7 @@ union ftrace_op_code_union {
        } __attribute__((packed));
 };
 
-#define RET_SIZE               1
+#define RET_SIZE               (IS_ENABLED(CONFIG_RETPOLINE) ? 5 : 1 + IS_ENABLED(CONFIG_SLS))
 
 static unsigned long
 create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size)
@@ -321,12 +321,12 @@ create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size)
        unsigned long offset;
        unsigned long npages;
        unsigned long size;
-       unsigned long retq;
        unsigned long *ptr;
        void *trampoline;
        void *ip;
        /* 48 8b 15 <offset> is movq <offset>(%rip), %rdx */
        unsigned const char op_ref[] = { 0x48, 0x8b, 0x15 };
+       unsigned const char retq[] = { RET_INSN_OPCODE, INT3_INSN_OPCODE };
        union ftrace_op_code_union op_ptr;
        int ret;
 
@@ -364,12 +364,10 @@ create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size)
                goto fail;
 
        ip = trampoline + size;
-
-       /* The trampoline ends with ret(q) */
-       retq = (unsigned long)ftrace_stub;
-       ret = copy_from_kernel_nofault(ip, (void *)retq, RET_SIZE);
-       if (WARN_ON(ret < 0))
-               goto fail;
+       if (cpu_feature_enabled(X86_FEATURE_RETHUNK))
+               memcpy(ip, text_gen_insn(JMP32_INSN_OPCODE, ip, &__x86_return_thunk), JMP32_INSN_SIZE);
+       else
+               memcpy(ip, retq, sizeof(retq));
 
        /* No need to test direct calls on created trampolines */
        if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {