Commit e4adceef authored by Andrii Nakryiko's avatar Andrii Nakryiko

Merge branch 'cleanup the legacy probe_event on failed scenario'

Chuang Wang says:

====================
A potential scenario, when an error is returned after
add_uprobe_event_legacy() in perf_event_uprobe_open_legacy(), or
bpf_program__attach_perf_event_opts() in
bpf_program__attach_uprobe_opts() returns an error, the uprobe_event
that was previously created is not cleaned.

At the same time, the legacy kprobe_event also have similar problems.

With these patches, whenever an error is returned, it ensures that
the created kprobe_event/uprobe_event is cleaned.

V1 -> v3:

- add detail commits
- call remove_kprobe_event_legacy() on failed bpf_program__attach_perf_event_opts()

v3 -> v4:

- cleanup the legacy kprobe_event on failed add/attach_event
====================
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
parents f6b9f6d5 2655144f
...@@ -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);
...@@ -10238,9 +10248,10 @@ static int perf_event_uprobe_open_legacy(const char *probe_name, bool retprobe, ...@@ -10238,9 +10248,10 @@ static int perf_event_uprobe_open_legacy(const char *probe_name, bool retprobe,
} }
type = determine_uprobe_perf_type_legacy(probe_name, retprobe); type = determine_uprobe_perf_type_legacy(probe_name, retprobe);
if (type < 0) { if (type < 0) {
err = type;
pr_warn("failed to determine legacy uprobe event id for %s:0x%zx: %d\n", pr_warn("failed to determine legacy uprobe event id for %s:0x%zx: %d\n",
binary_path, offset, err); binary_path, offset, err);
return type; goto err_clean_legacy;
} }
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, sizeof(attr));
...@@ -10255,9 +10266,14 @@ static int perf_event_uprobe_open_legacy(const char *probe_name, bool retprobe, ...@@ -10255,9 +10266,14 @@ static int perf_event_uprobe_open_legacy(const char *probe_name, bool retprobe,
if (pfd < 0) { if (pfd < 0) {
err = -errno; err = -errno;
pr_warn("legacy uprobe perf_event_open() failed: %d\n", err); pr_warn("legacy uprobe perf_event_open() failed: %d\n", err);
return err; goto err_clean_legacy;
} }
return pfd; return pfd;
err_clean_legacy:
/* Clear the newly added legacy uprobe_event */
remove_uprobe_event_legacy(probe_name, retprobe);
return err;
} }
/* Return next ELF section of sh_type after scn, or first of that type if scn is NULL. */ /* Return next ELF section of sh_type after scn, or first of that type if scn is NULL. */
...@@ -10591,7 +10607,7 @@ bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid, ...@@ -10591,7 +10607,7 @@ bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
prog->name, retprobe ? "uretprobe" : "uprobe", prog->name, retprobe ? "uretprobe" : "uprobe",
binary_path, func_offset, binary_path, func_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);
...@@ -10601,10 +10617,13 @@ bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid, ...@@ -10601,10 +10617,13 @@ bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
perf_link->legacy_is_retprobe = retprobe; perf_link->legacy_is_retprobe = retprobe;
} }
return link; return link;
err_clean_legacy:
if (legacy)
remove_uprobe_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);
} }
/* Format of u[ret]probe section definition supporting auto-attach: /* Format of u[ret]probe section definition supporting auto-attach:
......
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