From: menglong.dong@linux.dev
To: Peter Zijlstra <peterz@infradead.org>
Cc: Alexei Starovoitov <alexei.starovoitov@gmail.com>,
Jiri Olsa <jolsa@kernel.org>,
Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
X86 ML <x86@kernel.org>, "H. Peter Anvin" <hpa@zytor.com>,
Kees Cook <kees@kernel.org>,
Sami Tolvanen <samitolvanen@google.com>,
Mike Rapoport <rppt@kernel.org>,
Andy Lutomirski <luto@kernel.org>,
Masami Hiramatsu <mhiramat@kernel.org>,
Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
LKML <linux-kernel@vger.kernel.org>, bpf <bpf@vger.kernel.org>
Subject: Re: [PATCH] x86/ibt: make is_endbr() notrace
Date: Mon, 22 Sep 2025 15:13:38 +0800 [thread overview]
Message-ID: <6196970.lOV4Wx5bFT@7940hx> (raw)
In-Reply-To: <20250922065248.GO3245006@noisy.programming.kicks-ass.net>
On 2025/9/22 14:52 Peter Zijlstra <peterz@infradead.org> write:
> On Fri, Sep 19, 2025 at 09:13:15AM +0800, Menglong Dong wrote:
>
> > Ok, let me describe the problem in deetail.
> >
> > First of all, it has nothing to do with kprobe. The bpf program of type
> > kprobe-multi based on fprobe, and fprobe base on fgraph. So it's all
> > about the ftrace, which means __fentry__.
>
> Well, that's not confusing at all. Something called kprobe-multi not
> being related to kprobes :-(
>
> > Second, let me explain the recur detection of the kprobe-multi. Let's
> > take the is_endbr() for example. When it is hooked by the bpf program
> > of type kretprobe-multi, following calling chain will happen:
> >
> > is_endbr -> __ftrace_return_to_handler -> fprobe_return ->
> > kprobe_multi_link_exit_handler -> ftrace_get_entry_ip ->
> > arch_ftrace_get_symaddr -> is_endbr
> >
> > Look, is_endbr() is called again during the ftrace handler, so it will
> > trigger the ftrace handler(__ftrace_return_to_handler) again, which
> > causes recurrence.
>
> Right.
>
> > Such recurrence can be detected. In kprobe_multi_link_prog_run(),
> > the percpu various "bpf_prog_active" will be increased by 1 before we
> > run the bpf progs, and decrease by 1 after the bpf progs finish. If the
> > kprobe_multi_link_prog_run() is triggered again during bpf progs run,
> > it will check if bpf_prog_active is zero, and return directly if it is not.
> > Therefore, recurrence can't happen within the "bpf_prog_active" protection.
>
> As I think Masami already said, the problem is the layer. You're trying
> to fix an ftrace problem at the bpf layer.
Yeah, I see. And Masami has already posted a series for this
problem in:
https://lore.kernel.org/bpf/175852291163.307379.14414635977719513326.stgit@devnote2/
>
> > However, the calling to is_endbr() is not within that scope, which makes
> > the recurrence happen.
>
> Sorta, I'm still sketchy on the whole kprobe-multi thing.
>
> Anyway, I don't mind making is_endbr() invisible to tracing, that might
> just have security benefits too. But I think first the ftrace folks need
> to figure out how to best kill that recursion, because I don't think
> is_endbr is particularly special here.
So, does this patch seem useful after all?
OK, I'll send a V2 base on your following suggestion.
Thanks!
Menglong Dong
>
> It is just one more function that can emit a __fentry__ site.
>
> Anyway, something like the below would do:
>
> Note that without making __is_endbr() __always_inline, you run the risk
> of the compiler being retarded (they often are in the face of
> KASAN/UBSAN like) and deciding to out-of-line that function, resulting
> in yet another __fentry__ site.
>
> An added advantage of noinstr is that it is validated by objtool to
> never call to !noinstr code. As such, you can be sure there is no
> instrumentation in it.
>
> (the below hasn't been near a compiler)
>
> ---
> arch/x86/include/asm/ibt.h | 2 +-
> arch/x86/kernel/alternative.c | 2 +-
> 2 files changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/arch/x86/include/asm/ibt.h b/arch/x86/include/asm/ibt.h
> index 5e45d6424722..54937a527042 100644
> --- a/arch/x86/include/asm/ibt.h
> +++ b/arch/x86/include/asm/ibt.h
> @@ -65,7 +65,7 @@ static __always_inline __attribute_const__ u32 gen_endbr_poison(void)
> return 0xd6401f0f; /* nopl -42(%rax) */
> }
>
> -static inline bool __is_endbr(u32 val)
> +static __always_inline bool __is_endbr(u32 val)
> {
> if (val == gen_endbr_poison())
> return true;
> diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
> index 69fb818df2ee..f791e7abd466 100644
> --- a/arch/x86/kernel/alternative.c
> +++ b/arch/x86/kernel/alternative.c
> @@ -1108,7 +1108,7 @@ void __init_or_module noinline apply_returns(s32 *start, s32 *end) { }
>
> #ifdef CONFIG_X86_KERNEL_IBT
>
> -__noendbr bool is_endbr(u32 *val)
> +__noendbr noinstr bool is_endbr(u32 *val)
> {
> u32 endbr;
>
>
>
next prev parent reply other threads:[~2025-09-22 7:13 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-18 12:09 [PATCH] x86/ibt: make is_endbr() notrace Menglong Dong
2025-09-18 13:05 ` Peter Zijlstra
2025-09-18 13:32 ` Menglong Dong
2025-09-18 16:02 ` Alexei Starovoitov
2025-09-18 16:59 ` Peter Zijlstra
2025-09-18 17:53 ` Alexei Starovoitov
2025-09-19 1:13 ` Menglong Dong
2025-09-22 6:52 ` Peter Zijlstra
2025-09-22 7:13 ` menglong.dong [this message]
2025-09-22 7:19 ` Peter Zijlstra
2025-09-22 7:21 ` Menglong Dong
2025-09-22 6:36 ` Peter Zijlstra
2025-09-18 16:56 ` Peter Zijlstra
2025-09-19 12:35 ` Masami Hiramatsu
2025-09-19 8:52 ` Masami Hiramatsu
2025-09-19 8:58 ` Menglong Dong
2025-09-19 12:32 ` Masami Hiramatsu
2025-09-19 11:57 ` [PATCH] tracing: fgraph: Protect return handler from recursion loop Masami Hiramatsu (Google)
2025-09-19 15:27 ` Steven Rostedt
2025-09-20 7:45 ` Jiri Olsa
2025-09-22 6:16 ` Masami Hiramatsu
2025-09-22 13:38 ` Jiri Olsa
2025-09-22 14:42 ` Steven Rostedt
2025-09-22 19:45 ` Jiri Olsa
2025-09-21 4:05 ` Masami Hiramatsu
2025-09-21 22:52 ` Steven Rostedt
2025-09-24 22:58 ` Masami Hiramatsu
2025-09-20 13:39 ` Menglong Dong
2025-09-21 4:06 ` Masami Hiramatsu
2025-09-21 23:00 ` Steven Rostedt
2025-09-24 22:59 ` Masami Hiramatsu
2025-09-22 5:19 ` Masami Hiramatsu
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=6196970.lOV4Wx5bFT@7940hx \
--to=menglong.dong@linux.dev \
--cc=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bp@alien8.de \
--cc=bpf@vger.kernel.org \
--cc=dave.hansen@linux.intel.com \
--cc=hpa@zytor.com \
--cc=jolsa@kernel.org \
--cc=kees@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@kernel.org \
--cc=mhiramat@kernel.org \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=rppt@kernel.org \
--cc=samitolvanen@google.com \
--cc=tglx@linutronix.de \
--cc=x86@kernel.org \
/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