From: Masami Hiramatsu (Google) <mhiramat@kernel.org>
To: Florent Revest <revest@chromium.org>
Cc: Alexei Starovoitov <alexei.starovoitov@gmail.com>,
Steven Rostedt <rostedt@goodmis.org>,
linux-trace-kernel@vger.kernel.org,
LKML <linux-kernel@vger.kernel.org>,
Martin KaFai Lau <martin.lau@linux.dev>,
bpf <bpf@vger.kernel.org>, Sven Schnelle <svens@linux.ibm.com>,
Alexei Starovoitov <ast@kernel.org>, Jiri Olsa <jolsa@kernel.org>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Alan Maguire <alan.maguire@oracle.com>,
Mark Rutland <mark.rutland@arm.com>,
Peter Zijlstra <peterz@infradead.org>,
Thomas Gleixner <tglx@linutronix.de>,
Peter Zijlstra <peterz@infradead.org>
Subject: Re: [PATCH v4 6/9] tracing/fprobe: Enable fprobe events with CONFIG_DYNAMIC_FTRACE_WITH_ARGS
Date: Sat, 26 Aug 2023 12:38:38 +0900 [thread overview]
Message-ID: <20230826123838.610b3fe09b9fa1aab75f158d@kernel.org> (raw)
In-Reply-To: <CABRcYmLcTBey7QY9Ln3aVvJPV7weeTR0FA6DOU3_QObuAM8_Zg@mail.gmail.com>
(Cc: Peter)
On Fri, 25 Aug 2023 18:12:07 +0200
Florent Revest <revest@chromium.org> wrote:
> On Wed, Aug 23, 2023 at 5:16 PM Masami Hiramatsu (Google)
> <mhiramat@kernel.org> wrote:
> >
> > diff --git a/kernel/trace/trace_fprobe.c b/kernel/trace/trace_fprobe.c
> > index c60d0d9f1a95..90ad28260a9f 100644
> > --- a/kernel/trace/trace_fprobe.c
> > +++ b/kernel/trace/trace_fprobe.c
> > +#else /* CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS && !CONFIG_HAVE_PT_REGS_TO_FTRACE_REGS_CAST */
> > +
> > +/* Since fprobe handlers can be nested, pt_regs buffer need to be a stack */
> > +#define PERF_FPROBE_REGS_MAX 4
> > +
> > +struct pt_regs_stack {
> > + struct pt_regs regs[PERF_FPROBE_REGS_MAX];
> > + int idx;
> > +};
> > +
> > +static DEFINE_PER_CPU(struct pt_regs_stack, perf_fprobe_regs);
> > +
> > +static __always_inline
> > +struct pt_regs *perf_fprobe_partial_regs(struct ftrace_regs *fregs)
> > +{
> > + struct pt_regs_stack *stack = this_cpu_ptr(&perf_fprobe_regs);
> > + struct pt_regs *regs;
> > +
> > + if (stack->idx < PERF_FPROBE_REGS_MAX) {
> > + regs = stack->regs[stack->idx++];
>
> This is missing an &:
> regs = &stack->regs[stack->idx++];
Oops, good point. I'm curious it didin't cause compile error...
(I thought I built it on arm64)
>
> > + return ftrace_partial_regs(fregs, regs);
>
> I think this is incorrect on arm64 and will likely cause very subtle
> failure modes down the line on other architectures too. The problem on
> arm64 is that Perf calls "user_mode(regs)" somewhere down the line,
> that macro tries to read the "pstate" register, which is not populated
> in ftrace_regs, so it's not copied into a "partial" pt_regs either and
> Perf can take wrong decisions based on that.
I think we can assure the ftrace_regs is always !user_mode() so in that case
ftrace_partial_regs() should fill the 'pstate' register as kernel mode.
>
> I already mentioned this problem in the past:
> - in the third answer block of:
> https://lore.kernel.org/all/CABRcYmJjtVq-330ktqTAUiNO1=yG_aHd0xz=c550O5C7QP++UA@mail.gmail.com/
> - in the fourth answer block of:
> https://lore.kernel.org/all/CABRcYm+esb8J2O1v6=C+h+HSa5NxraPUgo63w7-iZj0CXbpusg@mail.gmail.com/
>
Oops, sorry I missed that. And I basically agreed that we need a special
care for perf. Let me reply it.
> It is quite possible that other architectures at some point introduce
> a light ftrace "args" trampoline that misses one of the registers
> expected by Perf because they don't realize that this trampoline calls
> fprobe which calls Perf which has specific registers expectations.
Agreed.
>
> We got the green light from Alexei to use ftrace_partial_regs for "BPF
> mutli_kprobe" because these BPF programs can gracefully deal with
> sparse pt_regs but I think a similar conversation needs to happen with
> the Perf folks.
Indeed. Who is the best person to involve, Peterz? (but I think
we need arm64 PMU part maintainer to talk)
>
> ----
>
> On a side-note, a subtle difference between ftrace_partial_regs with
> and without HAVE_PT_REGS_TO_FTRACE_REGS_CAST is that one does a copy
> and the other does not. If a subsystem receives a partial regs under
> HAVE_PT_REGS_TO_FTRACE_REGS_CAST, it can modify register fields and
> the modified values will be restored by the ftrace trampoline. Without
> HAVE_PT_REGS_TO_FTRACE_REGS_CAST, only the copy will be modified and
> ftrace won't restore them. I think the least we can do is to document
> thoroughly the guarantees of the ftrace_partial_regs API: users
> shouldn't rely on modifying the resulting regs because depending on
> the architecture this could do different things. People shouldn't rely
> on any register that isn't covered by one of the ftrace_regs_get_*
> helpers because it can be unpopulated on some architectures. I believe
> this is the case for BPF multi_kprobe but not for Perf.
I agree with the documentation requirement, but since the fprobe official
interface becomes ftrace_regs, user naturally expects it is not pt_regs.
The problem is that the perf's case. Since the perf is natively only
support pt_regs (and there is no reason to support ftrace_regs, yes).
Hmm, I will recheck how the perf events on trace-event is implementd.
Thank you,
>
> > + }
> > + return NULL;
> > +}
> > +
> > +static __always_inline void perf_fprobe_return_regs(struct pt_regs *regs)
> > +{
> > + struct pt_regs_stack *stack = this_cpu_ptr(&perf_fprobe_regs);
> > +
> > + if (WARN_ON_ONCE(regs != stack->regs[stack->idx]))
>
> This is missing an & too:
> if (WARN_ON_ONCE(regs != &stack->regs[stack->idx]))
>
>
>
>
> > + return;
> > +
> > + --stack->idx;
> > +}
> > +
> > +#endif /* !CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS || CONFIG_HAVE_PT_REGS_TO_FTRACE_REGS_CAST */
--
Masami Hiramatsu (Google) <mhiramat@kernel.org>
next prev parent reply other threads:[~2023-08-26 12:22 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-23 15:15 [PATCH v4 0/9] bpf: fprobe: rethook: Use ftrace_regs instead of pt_regs Masami Hiramatsu (Google)
2023-08-23 15:15 ` [PATCH v4 1/9] Documentation: probes: Add a new ret_ip callback parameter Masami Hiramatsu (Google)
2023-08-25 16:11 ` Florent Revest
2023-08-23 15:15 ` [PATCH v4 2/9] fprobe: Use fprobe_regs in fprobe entry handler Masami Hiramatsu (Google)
2023-08-25 16:11 ` Florent Revest
2023-08-23 15:16 ` [PATCH v4 3/9] tracing: Expose ftrace_regs regardless of CONFIG_FUNCTION_TRACER Masami Hiramatsu (Google)
2023-08-23 15:16 ` [PATCH v4 4/9] fprobe: rethook: Use ftrace_regs in fprobe exit handler and rethook Masami Hiramatsu (Google)
2023-08-25 16:12 ` Florent Revest
2023-09-04 13:40 ` Masami Hiramatsu
2023-09-05 7:17 ` Sven Schnelle
2023-09-05 13:36 ` Masami Hiramatsu
2023-09-05 16:30 ` Steven Rostedt
2023-09-06 0:06 ` Masami Hiramatsu
2023-09-06 6:49 ` Sven Schnelle
2023-09-09 14:24 ` Masami Hiramatsu
2023-09-11 7:55 ` Sven Schnelle
2023-09-11 14:15 ` Masami Hiramatsu
2023-08-23 15:16 ` [PATCH v4 5/9] ftrace: Add ftrace_partial_regs() for converting ftrace_regs to pt_regs Masami Hiramatsu (Google)
2023-08-25 21:49 ` Andrii Nakryiko
2023-08-26 1:56 ` Masami Hiramatsu
2023-09-05 19:50 ` Andrii Nakryiko
2023-09-06 0:28 ` Masami Hiramatsu
2023-09-08 22:56 ` Andrii Nakryiko
2023-08-23 15:16 ` [PATCH v4 6/9] tracing/fprobe: Enable fprobe events with CONFIG_DYNAMIC_FTRACE_WITH_ARGS Masami Hiramatsu (Google)
2023-08-25 16:12 ` Florent Revest
2023-08-26 3:38 ` Masami Hiramatsu [this message]
2023-08-30 7:20 ` Masami Hiramatsu
2023-08-23 15:16 ` [PATCH v4 7/9] bpf: Enable kprobe_multi feature if CONFIG_FPROBE is enabled Masami Hiramatsu (Google)
2023-08-23 15:16 ` [PATCH v4 8/9] Documentations: probes: Update fprobe document to use ftrace_regs Masami Hiramatsu (Google)
2023-08-23 15:17 ` [PATCH v4 9/9] Documentation: tracing: Add a note about argument and retval access Masami Hiramatsu (Google)
2023-08-25 16:12 ` Florent Revest
2023-08-25 16:11 ` [PATCH v4 0/9] bpf: fprobe: rethook: Use ftrace_regs instead of pt_regs Florent Revest
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=20230826123838.610b3fe09b9fa1aab75f158d@kernel.org \
--to=mhiramat@kernel.org \
--cc=acme@kernel.org \
--cc=alan.maguire@oracle.com \
--cc=alexei.starovoitov@gmail.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=jolsa@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=peterz@infradead.org \
--cc=revest@chromium.org \
--cc=rostedt@goodmis.org \
--cc=svens@linux.ibm.com \
--cc=tglx@linutronix.de \
/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