Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

x86/ftrace: Implement DYNAMIC_FTRACE_WITH_JMP

Implement the DYNAMIC_FTRACE_WITH_JMP for x86_64. In ftrace_call_replace,
we will use JMP32_INSN_OPCODE instead of CALL_INSN_OPCODE if the address
should use "jmp".

Meanwhile, adjust the direct call in the ftrace_regs_caller. The RSB is
balanced in the "jmp" mode. Take the function "foo" for example:

original_caller:
call foo -> foo:
call fentry -> fentry:
[do ftrace callbacks ]
move tramp_addr to stack
RET -> tramp_addr
tramp_addr:
[..]
call foo_body -> foo_body:
[..]
RET -> back to tramp_addr
[..]
RET -> back to original_caller

Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
Acked-by: Steven Rostedt (Google) <rostedt@goodmis.org>
Link: https://lore.kernel.org/r/20251118123639.688444-3-dongml2@chinatelecom.cn
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Menglong Dong and committed by
Alexei Starovoitov
0c3772a8 25e4e356

+18 -2
+1
arch/x86/Kconfig
··· 230 230 select HAVE_DYNAMIC_FTRACE_WITH_ARGS if X86_64 231 231 select HAVE_FTRACE_REGS_HAVING_PT_REGS if X86_64 232 232 select HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS 233 + select HAVE_DYNAMIC_FTRACE_WITH_JMP if X86_64 233 234 select HAVE_SAMPLE_FTRACE_DIRECT if X86_64 234 235 select HAVE_SAMPLE_FTRACE_DIRECT_MULTI if X86_64 235 236 select HAVE_EBPF_JIT
+6 -1
arch/x86/kernel/ftrace.c
··· 74 74 * No need to translate into a callthunk. The trampoline does 75 75 * the depth accounting itself. 76 76 */ 77 - return text_gen_insn(CALL_INSN_OPCODE, (void *)ip, (void *)addr); 77 + if (ftrace_is_jmp(addr)) { 78 + addr = ftrace_jmp_get(addr); 79 + return text_gen_insn(JMP32_INSN_OPCODE, (void *)ip, (void *)addr); 80 + } else { 81 + return text_gen_insn(CALL_INSN_OPCODE, (void *)ip, (void *)addr); 82 + } 78 83 } 79 84 80 85 static int ftrace_verify_code(unsigned long ip, const char *old_code)
+11 -1
arch/x86/kernel/ftrace_64.S
··· 285 285 ANNOTATE_NOENDBR 286 286 RET 287 287 288 + 1: 289 + testb $1, %al 290 + jz 2f 291 + andq $0xfffffffffffffffe, %rax 292 + movq %rax, MCOUNT_REG_SIZE+8(%rsp) 293 + restore_mcount_regs 294 + /* Restore flags */ 295 + popfq 296 + RET 297 + 288 298 /* Swap the flags with orig_rax */ 289 - 1: movq MCOUNT_REG_SIZE(%rsp), %rdi 299 + 2: movq MCOUNT_REG_SIZE(%rsp), %rdi 290 300 movq %rdi, MCOUNT_REG_SIZE-8(%rsp) 291 301 movq %rax, MCOUNT_REG_SIZE(%rsp) 292 302