Re: [PATCH v2] libbpf: Cleanup the kprobe_event on failed add_kprobe_event_legacy()
From: Chuang W
Date: Thu Jun 23 2022 - 02:09:58 EST
Hi, John. Thanks, I will resubmit V3 soon, and supplement the commit
information.
On Thu, Jun 23, 2022 at 11:37 AM John Fastabend
<john.fastabend@xxxxxxxxx> wrote:
>
> Chuang W wrote:
> > Before the 0bc11ed5ab60 commit ("kprobes: Allow kprobes coexist with
> > livepatch"), in a scenario where livepatch and kprobe coexist on the
> > same function entry, the creation of kprobe_event using
> > add_kprobe_event_legacy() will be successful, at the same time as a
> > trace event (e.g. /debugfs/tracing/events/kprobe/XX) will exist, but
> > perf_event_open() will return an error because both livepatch and kprobe
> > use FTRACE_OPS_FL_IPMODIFY.
> >
> > With this patch, whenever an error is returned after
> > add_kprobe_event_legacy(), this ensures that the created kprobe_event is
> > cleaned.
> >
> > Signed-off-by: Chuang W <nashuiliang@xxxxxxxxx>
> > Signed-off-by: Jingren Zhou <zhoujingren@xxxxxxxxxxxxxx>
> > ---
> > tools/lib/bpf/libbpf.c | 12 +++++++++---
> > 1 file changed, 9 insertions(+), 3 deletions(-)
>
> I think we want to improve the commit message otherwise I'm sure we will
> stumble on this in the future and from just above its tricky to follow.
> I would suggest almost verbatim the description you gave in reply to
> my question. Just cut'n'pasting your text together with minor edit
> glue,
>
> "
> The legacy kprobe API (i.e. tracefs API) has two steps:
>
> 1) register_kprobe
>
> $ echo 'p:mykprobe XXX' > /sys/kernel/debug/tracing/kprobe_events
>
> This will create a trace event of mykprobe and register a disable
> kprobe that waits to be activated.
>
> 2) enable_kprobe
>
> 2.1) using syscall perf_event_open as the following code,
> perf_event_kprobe_open_legacy (file: tools/lib/bpf/libbpf.c):
> ---
> attr.type = PERF_TYPE_TRACEPOINT;
> pfd = syscall(__NR_perf_event_open, &attr,
> pid < 0 ? -1 : pid, /* pid */
> pid == -1 ? 0 : -1, /* cpu */
> -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
> ---
>
> In the implementation code of perf_event_open, enable_kprobe() will be executed.
>
> 2.2) using shell
>
> $ echo 1 > /sys/kernel/debug/tracing/events/kprobes/mykprobe/enable
>
> As with perf_event_open, enable_kprobe() will also be executed.
>
> When using the same function XXX, kprobe and livepatch cannot coexist,
> that is, step 2) will return an error (ref: arm_kprobe_ftrace()),
> however, step 1) is ok! The new kprobe API (i.e. perf kprobe API)
> aggregates register_kprobe and enable_kprobe, internally fixes the
> issue on failed enable_kprobe.
>
> To fix: before the 0bc11ed5ab60 commit ("kprobes: Allow kprobes coexist with
> livepatch"), in a scenario where livepatch and kprobe coexist on the
> same function entry, the creation of kprobe_event using
> add_kprobe_event_legacy() will be successful, at the same time as a
> trace event (e.g. /debugfs/tracing/events/kprobe/XX) will exist, but
> perf_event_open() will return an error because both livepatch and kprobe
> use FTRACE_OPS_FL_IPMODIFY.
>
> With this patch, whenever an error is returned after
> add_kprobe_event_legacy(), this ensures that the created kprobe_event is
> cleaned.
> "
>
> Thanks,
> John