RE: [PATCH] libbpf: Remove kprobe_event on failed kprobe_open_legacy
From: John Fastabend
Date: Sat Jun 18 2022 - 00:14:04 EST
Chuang W wrote:
> In a scenario where livepatch and aggrprobe coexist, the creating
> kprobe_event using tracefs API will succeed, a trace event (e.g.
> /debugfs/tracing/events/kprobe/XX) will exist, but perf_event_open()
> will return an error.
This seems a bit strange from API side. I'm not really familiar with
livepatch, but I guess this is UAPI now so fixing add_kprobe_event_legacy
to fail is not an option?
>
> 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(-)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 0781fae58a06..d0a36350e22a 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -10809,10 +10809,11 @@ static int perf_event_kprobe_open_legacy(const char *probe_name, bool retprobe,
> }
> type = determine_kprobe_perf_type_legacy(probe_name, retprobe);
> if (type < 0) {
> + err = type;
> pr_warn("failed to determine legacy kprobe event id for '%s+0x%zx': %s\n",
> kfunc_name, offset,
> - libbpf_strerror_r(type, errmsg, sizeof(errmsg)));
> - return type;
> + libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
> + goto clear_kprobe_event;
> }
> attr.size = sizeof(attr);
> attr.config = type;
> @@ -10826,9 +10827,14 @@ static int perf_event_kprobe_open_legacy(const char *probe_name, bool retprobe,
> err = -errno;
> pr_warn("legacy kprobe perf_event_open() failed: %s\n",
> libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
> - return err;
> + goto clear_kprobe_event;
> }
> return pfd;
> +
> +clear_kprobe_event:
> + /* Clear the newly added kprobe_event */
> + remove_kprobe_event_legacy(probe_name, retprobe);
> + return err;
> }
>
> struct bpf_link *
> --
> 2.34.1
>