Commit 80940293 authored by Chuang Wang's avatar Chuang Wang Committed by Andrii Nakryiko

libbpf: Cleanup the legacy kprobe_event on failed add/attach_event()

Before the 0bc11ed5 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/XXX) will exist, but
perf_event_open() will return an error because both livepatch and kprobe
use FTRACE_OPS_FL_IPMODIFY. As follows:

1) add a livepatch

$ insmod livepatch-XXX.ko

2) add a kprobe using tracefs API (i.e. add_kprobe_event_legacy)

$ echo 'p:mykprobe XXX' > /sys/kernel/debug/tracing/kprobe_events

3) enable this kprobe (i.e. sys_perf_event_open)

This will return an error, -EBUSY.

On Andrii Nakryiko's comment, few error paths in
bpf_program__attach_kprobe_opts() that should need to call
remove_kprobe_event_legacy().

With this patch, whenever an error is returned after
add_kprobe_event_legacy() or bpf_program__attach_perf_event_opts(), this
ensures that the created kprobe_event is cleaned.
Signed-off-by: default avatarChuang Wang <nashuiliang@gmail.com>
Signed-off-by: default avatarJingren Zhou <zhoujingren@didiglobal.com>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20220629151848.65587-2-nashuiliang@gmail.com
parent f6b9f6d5
...@@ -9877,10 +9877,11 @@ static int perf_event_kprobe_open_legacy(const char *probe_name, bool retprobe, ...@@ -9877,10 +9877,11 @@ static int perf_event_kprobe_open_legacy(const char *probe_name, bool retprobe,
} }
type = determine_kprobe_perf_type_legacy(probe_name, retprobe); type = determine_kprobe_perf_type_legacy(probe_name, retprobe);
if (type < 0) { if (type < 0) {
err = type;
pr_warn("failed to determine legacy kprobe event id for '%s+0x%zx': %s\n", pr_warn("failed to determine legacy kprobe event id for '%s+0x%zx': %s\n",
kfunc_name, offset, kfunc_name, offset,
libbpf_strerror_r(type, errmsg, sizeof(errmsg))); libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
return type; goto err_clean_legacy;
} }
attr.size = sizeof(attr); attr.size = sizeof(attr);
attr.config = type; attr.config = type;
...@@ -9894,9 +9895,14 @@ static int perf_event_kprobe_open_legacy(const char *probe_name, bool retprobe, ...@@ -9894,9 +9895,14 @@ static int perf_event_kprobe_open_legacy(const char *probe_name, bool retprobe,
err = -errno; err = -errno;
pr_warn("legacy kprobe perf_event_open() failed: %s\n", pr_warn("legacy kprobe perf_event_open() failed: %s\n",
libbpf_strerror_r(err, errmsg, sizeof(errmsg))); libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
return err; goto err_clean_legacy;
} }
return pfd; return pfd;
err_clean_legacy:
/* Clear the newly added legacy kprobe_event */
remove_kprobe_event_legacy(probe_name, retprobe);
return err;
} }
struct bpf_link * struct bpf_link *
...@@ -9953,7 +9959,7 @@ bpf_program__attach_kprobe_opts(const struct bpf_program *prog, ...@@ -9953,7 +9959,7 @@ bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
prog->name, retprobe ? "kretprobe" : "kprobe", prog->name, retprobe ? "kretprobe" : "kprobe",
func_name, offset, func_name, offset,
libbpf_strerror_r(err, errmsg, sizeof(errmsg))); libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
goto err_out; goto err_clean_legacy;
} }
if (legacy) { if (legacy) {
struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link); struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link);
...@@ -9964,6 +9970,10 @@ bpf_program__attach_kprobe_opts(const struct bpf_program *prog, ...@@ -9964,6 +9970,10 @@ bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
} }
return link; return link;
err_clean_legacy:
if (legacy)
remove_kprobe_event_legacy(legacy_probe, retprobe);
err_out: err_out:
free(legacy_probe); free(legacy_probe);
return libbpf_err_ptr(err); return libbpf_err_ptr(err);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment