Re: [PATCH 3/3] x86/ftrace: Do not jump to direct code in created trampolines

From: Peter Zijlstra
Date: Wed Apr 22 2020 - 16:08:41 EST


On Wed, Apr 22, 2020 at 12:25:42PM -0400, Steven Rostedt wrote:

> @@ -367,6 +371,17 @@ create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size)
> if (WARN_ON(ret < 0))
> goto fail;
>
> + /* No need to test direct calls on created trampolines */
> + if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
> + /* NOP the jnz 1f; but make sure it's a 2 byte jnz */
> + ip = trampoline + (jmp_offset - start_offset);
> + if (WARN_ON(*(char *)ip != 0x75))
> + goto fail;
> + ret = probe_kernel_read(ip, ideal_nops[2], 2);

Now you're just being silly, are you really, actually worried you can't
read ideal_nops[] ?

> + if (ret < 0)
> + goto fail;
> + }
> +
> /*
> * The address of the ftrace_ops that is used for this trampoline
> * is stored at the end of the trampoline. This will be used to
> diff --git a/arch/x86/kernel/ftrace_64.S b/arch/x86/kernel/ftrace_64.S
> index 0882758d165a..f72ef157feb3 100644
> --- a/arch/x86/kernel/ftrace_64.S
> +++ b/arch/x86/kernel/ftrace_64.S
> @@ -241,6 +241,7 @@ SYM_INNER_LABEL(ftrace_regs_call, SYM_L_GLOBAL)
> */
> movq ORIG_RAX(%rsp), %rax
> testq %rax, %rax
> +SYM_INNER_LABEL(ftrace_regs_caller_jmp, SYM_L_GLOBAL)
> jnz 1f
>

I you worry about performance, it would make more sense to do something
like so:

SYM_INNER_LABEL(ftrace_regs_caller_from, SYM_L_GLOBAL)
movq ORIG_RAX(%rsp), %rax
testq %rax, %rax
jnz 1f
SYM_INNER_LABEL(ftrace_regs_caller_to, SYM_L_GLOBAL)

if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
ip = trampoline + (ftrace_regs_caller_from - start_offset);
((u8[])ip)[0] = JMP8_INSN_OPCODE;
((u8[])ip)[1] = ftrace_regs_caller_to - ftrace_regs_caller_from - JMP8_INSN_SIZE;
}

Or nop the whole range, but it's like 10 bytes so I'm not sure that's
actually faster.