From: Menglong Dong <menglong.dong@linux.dev>
To: Menglong Dong <menglong8.dong@gmail.com>, Jiri Olsa <olsajiri@gmail.com>
Cc: ast@kernel.org, rostedt@goodmis.org, daniel@iogearbox.net,
john.fastabend@gmail.com, andrii@kernel.org,
martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org,
yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me,
haoluo@google.com, mhiramat@kernel.org, mark.rutland@arm.com,
mathieu.desnoyers@efficios.com, jiang.biao@linux.dev,
bpf@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-trace-kernel@vger.kernel.org
Subject: Re: [PATCH bpf-next v3 2/6] x86/ftrace: implement DYNAMIC_FTRACE_WITH_JMP
Date: Wed, 19 Nov 2025 09:05:05 +0800 [thread overview]
Message-ID: <5056669.31r3eYUQgx@7950hx> (raw)
In-Reply-To: <aRzs1GGLCm5svW5_@krava>
On 2025/11/19 06:01, Jiri Olsa wrote:
> On Tue, Nov 18, 2025 at 08:36:30PM +0800, Menglong Dong wrote:
> > 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>
> > ---
> > arch/x86/Kconfig | 1 +
> > arch/x86/kernel/ftrace.c | 7 ++++++-
> > arch/x86/kernel/ftrace_64.S | 12 +++++++++++-
> > 3 files changed, 18 insertions(+), 2 deletions(-)
> >
> > diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
> > index fa3b616af03a..462250a20311 100644
> > --- a/arch/x86/Kconfig
> > +++ b/arch/x86/Kconfig
> > @@ -230,6 +230,7 @@ config X86
> > select HAVE_DYNAMIC_FTRACE_WITH_ARGS if X86_64
> > select HAVE_FTRACE_REGS_HAVING_PT_REGS if X86_64
> > select HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
> > + select HAVE_DYNAMIC_FTRACE_WITH_JMP if X86_64
> > select HAVE_SAMPLE_FTRACE_DIRECT if X86_64
> > select HAVE_SAMPLE_FTRACE_DIRECT_MULTI if X86_64
> > select HAVE_EBPF_JIT
> > diff --git a/arch/x86/kernel/ftrace.c b/arch/x86/kernel/ftrace.c
> > index 4450acec9390..0543b57f54ee 100644
> > --- a/arch/x86/kernel/ftrace.c
> > +++ b/arch/x86/kernel/ftrace.c
> > @@ -74,7 +74,12 @@ static const char *ftrace_call_replace(unsigned long ip, unsigned long addr)
> > * No need to translate into a callthunk. The trampoline does
> > * the depth accounting itself.
> > */
> > - return text_gen_insn(CALL_INSN_OPCODE, (void *)ip, (void *)addr);
> > + if (ftrace_is_jmp(addr)) {
> > + addr = ftrace_jmp_get(addr);
> > + return text_gen_insn(JMP32_INSN_OPCODE, (void *)ip, (void *)addr);
> > + } else {
> > + return text_gen_insn(CALL_INSN_OPCODE, (void *)ip, (void *)addr);
> > + }
> > }
> >
> > static int ftrace_verify_code(unsigned long ip, const char *old_code)
> > diff --git a/arch/x86/kernel/ftrace_64.S b/arch/x86/kernel/ftrace_64.S
> > index 823dbdd0eb41..a132608265f6 100644
> > --- a/arch/x86/kernel/ftrace_64.S
> > +++ b/arch/x86/kernel/ftrace_64.S
> > @@ -285,8 +285,18 @@ SYM_INNER_LABEL(ftrace_regs_caller_end, SYM_L_GLOBAL)
> > ANNOTATE_NOENDBR
> > RET
> >
> > +1:
> > + testb $1, %al
> > + jz 2f
> > + andq $0xfffffffffffffffe, %rax
> > + movq %rax, MCOUNT_REG_SIZE+8(%rsp)
> > + restore_mcount_regs
> > + /* Restore flags */
> > + popfq
> > + RET
>
> is this hunk the reason for the 0x1 jmp-bit you set in the address?
Exactly!
>
> I wonder if we introduced new flag in dyn_ftrace::flags for this,
> then we'd need to have extra ftrace trampoline for jmp ftrace_ops
We don't introduce new dyn_ftrace::flags. I tried to introduce
FTRACE_FL_JMP and FTRACE_FL_JMP_EN for this propose before I
added the jmp-bit to the address. It's hard to do it this way.
First, we need to introduce a ftrace_regs_jmp_caller, which will
be used for the "jmp" mode. However, it's difficult when we need
to change a call_address to jmp_address in __modify_ftrace_direct(),
as it will change the "entry->direct" directly. And maybe we need
reconstruct the direct call to implement it this way.
I were almost giving up before I thought the jmp-bit, which allow
us the update the address from call mode to jmp mode atomically.
Thanks!
Menglong Dong
>
> jirka
>
>
next prev parent reply other threads:[~2025-11-19 1:05 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-18 12:36 [PATCH bpf-next v3 0/6] bpf trampoline support "jmp" mode Menglong Dong
2025-11-18 12:36 ` [PATCH bpf-next v3 1/6] ftrace: introduce FTRACE_OPS_FL_JMP Menglong Dong
2025-11-18 13:25 ` bot+bpf-ci
2025-11-18 13:51 ` Steven Rostedt
2025-11-18 12:36 ` [PATCH bpf-next v3 2/6] x86/ftrace: implement DYNAMIC_FTRACE_WITH_JMP Menglong Dong
2025-11-18 22:01 ` Jiri Olsa
2025-11-19 1:05 ` Menglong Dong [this message]
2025-11-18 12:36 ` [PATCH bpf-next v3 3/6] bpf: fix the usage of BPF_TRAMP_F_SKIP_FRAME Menglong Dong
2025-12-18 14:45 ` Andreas Schwab
2025-12-18 15:05 ` Menglong Dong
2025-12-19 2:22 ` Menglong Dong
2025-12-19 11:41 ` Andreas Schwab
2025-12-19 12:27 ` Menglong Dong
2025-12-19 13:31 ` Menglong Dong
2025-12-19 13:48 ` Andreas Schwab
2025-12-19 14:04 ` Menglong Dong
2025-12-19 14:50 ` Andreas Schwab
2025-12-19 14:54 ` Menglong Dong
2025-11-18 12:36 ` [PATCH bpf-next v3 4/6] bpf,x86: adjust the "jmp" mode for bpf trampoline Menglong Dong
2025-11-18 12:36 ` [PATCH bpf-next v3 5/6] bpf: specify the old and new poke_type for bpf_arch_text_poke Menglong Dong
2025-11-18 12:36 ` [PATCH bpf-next v3 6/6] bpf: implement "jmp" mode for trampoline Menglong Dong
2025-11-19 0:59 ` Alexei Starovoitov
2025-11-19 1:03 ` Steven Rostedt
2025-11-22 2:37 ` Alexei Starovoitov
2025-11-24 14:50 ` Steven Rostedt
2025-11-19 0:28 ` [PATCH bpf-next v3 0/6] bpf trampoline support "jmp" mode Alexei Starovoitov
2025-11-19 2:47 ` Menglong Dong
2025-11-19 2:55 ` Leon Hwang
2025-11-19 12:36 ` Xu Kuohai
2025-11-20 2:07 ` Leon Hwang
2025-11-20 3:24 ` Xu Kuohai
2025-11-24 18:00 ` patchwork-bot+netdevbpf
2026-04-02 6:12 ` Leon Hwang
2026-04-02 7:51 ` Menglong Dong
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5056669.31r3eYUQgx@7950hx \
--to=menglong.dong@linux.dev \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=haoluo@google.com \
--cc=jiang.biao@linux.dev \
--cc=john.fastabend@gmail.com \
--cc=kpsingh@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=martin.lau@linux.dev \
--cc=mathieu.desnoyers@efficios.com \
--cc=menglong8.dong@gmail.com \
--cc=mhiramat@kernel.org \
--cc=olsajiri@gmail.com \
--cc=rostedt@goodmis.org \
--cc=sdf@fomichev.me \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox