All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4 0/3] cleanup the legacy probe_event on failed scenario
@ 2022-06-29 15:18 Chuang Wang
  2022-06-29 15:18 ` [PATCH v4 1/3] libbpf: cleanup the legacy kprobe_event on failed add/attach_event() Chuang Wang
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Chuang Wang @ 2022-06-29 15:18 UTC (permalink / raw)
  Cc: Chuang Wang, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, netdev, bpf

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

Chuang Wang (3):
  libbpf: cleanup the legacy kprobe_event on failed add/attach_event()
  libbpf: fix wrong variable used in perf_event_uprobe_open_legacy()
  libbpf: cleanup the legacy uprobe_event on failed add/attach_event()

 tools/lib/bpf/libbpf.c | 37 +++++++++++++++++++++++++++++--------
 1 file changed, 29 insertions(+), 8 deletions(-)

-- 
2.34.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v4 1/3] libbpf: cleanup the legacy kprobe_event on failed add/attach_event()
  2022-06-29 15:18 [PATCH v4 0/3] cleanup the legacy probe_event on failed scenario Chuang Wang
@ 2022-06-29 15:18 ` Chuang Wang
  2022-06-29 15:18 ` [PATCH v4 2/3] libbpf: fix wrong variable used in perf_event_uprobe_open_legacy() Chuang Wang
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Chuang Wang @ 2022-06-29 15:18 UTC (permalink / raw)
  Cc: Chuang Wang, Jingren Zhou, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, netdev, bpf, linux-kernel

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/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: Chuang Wang <nashuiliang@gmail.com>
Signed-off-by: Jingren Zhou <zhoujingren@didiglobal.com>
---
 tools/lib/bpf/libbpf.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index e994797bcd48..8a33a52e01a5 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -9868,10 +9868,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 err_clean_legacy;
 	}
 	attr.size = sizeof(attr);
 	attr.config = type;
@@ -9885,9 +9886,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 err_clean_legacy;
 	}
 	return pfd;
+
+err_clean_legacy:
+	/* Clear the newly added legacy kprobe_event */
+	remove_kprobe_event_legacy(probe_name, retprobe);
+	return err;
 }
 
 struct bpf_link *
@@ -9944,7 +9950,7 @@ bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
 			prog->name, retprobe ? "kretprobe" : "kprobe",
 			func_name, offset,
 			libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
-		goto err_out;
+		goto err_clean_legacy;
 	}
 	if (legacy) {
 		struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link);
@@ -9955,6 +9961,11 @@ bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
 	}
 
 	return link;
+
+err_clean_legacy:
+	if (legacy)
+		remove_kprobe_event_legacy(legacy_probe, retprobe);
+
 err_out:
 	free(legacy_probe);
 	return libbpf_err_ptr(err);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v4 2/3] libbpf: fix wrong variable used in perf_event_uprobe_open_legacy()
  2022-06-29 15:18 [PATCH v4 0/3] cleanup the legacy probe_event on failed scenario Chuang Wang
  2022-06-29 15:18 ` [PATCH v4 1/3] libbpf: cleanup the legacy kprobe_event on failed add/attach_event() Chuang Wang
@ 2022-06-29 15:18 ` Chuang Wang
  2022-06-29 15:18 ` [PATCH v4 3/3] libbpf: cleanup the legacy uprobe_event on failed add/attach_event() Chuang Wang
  2022-07-06  4:30 ` [PATCH v4 0/3] cleanup the legacy probe_event on failed scenario patchwork-bot+netdevbpf
  3 siblings, 0 replies; 5+ messages in thread
From: Chuang Wang @ 2022-06-29 15:18 UTC (permalink / raw)
  Cc: Chuang Wang, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, netdev, bpf, linux-kernel

Use "type" as opposed to "err" in pr_warn() after
determine_uprobe_perf_type_legacy() returns an error.

Signed-off-by: Chuang Wang <nashuiliang@gmail.com>
---
 tools/lib/bpf/libbpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 8a33a52e01a5..159b69d8b941 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -10241,7 +10241,7 @@ static int perf_event_uprobe_open_legacy(const char *probe_name, bool retprobe,
 	type = determine_uprobe_perf_type_legacy(probe_name, retprobe);
 	if (type < 0) {
 		pr_warn("failed to determine legacy uprobe event id for %s:0x%zx: %d\n",
-			binary_path, offset, err);
+			binary_path, offset, type);
 		return type;
 	}
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v4 3/3] libbpf: cleanup the legacy uprobe_event on failed add/attach_event()
  2022-06-29 15:18 [PATCH v4 0/3] cleanup the legacy probe_event on failed scenario Chuang Wang
  2022-06-29 15:18 ` [PATCH v4 1/3] libbpf: cleanup the legacy kprobe_event on failed add/attach_event() Chuang Wang
  2022-06-29 15:18 ` [PATCH v4 2/3] libbpf: fix wrong variable used in perf_event_uprobe_open_legacy() Chuang Wang
@ 2022-06-29 15:18 ` Chuang Wang
  2022-07-06  4:30 ` [PATCH v4 0/3] cleanup the legacy probe_event on failed scenario patchwork-bot+netdevbpf
  3 siblings, 0 replies; 5+ messages in thread
From: Chuang Wang @ 2022-06-29 15:18 UTC (permalink / raw)
  Cc: Chuang Wang, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, netdev, bpf, linux-kernel

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.

So, with this patch, when an error is returned, fix this by adding
remove_uprobe_event_legacy()

Signed-off-by: Chuang Wang <nashuiliang@gmail.com>
---
 tools/lib/bpf/libbpf.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 159b69d8b941..5aa6033a0284 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -10240,9 +10240,10 @@ static int perf_event_uprobe_open_legacy(const char *probe_name, bool retprobe,
 	}
 	type = determine_uprobe_perf_type_legacy(probe_name, retprobe);
 	if (type < 0) {
+		err = type;
 		pr_warn("failed to determine legacy uprobe event id for %s:0x%zx: %d\n",
-			binary_path, offset, type);
-		return type;
+			binary_path, offset, err);
+		goto err_clean_legacy;
 	}
 
 	memset(&attr, 0, sizeof(attr));
@@ -10257,9 +10258,14 @@ static int perf_event_uprobe_open_legacy(const char *probe_name, bool retprobe,
 	if (pfd < 0) {
 		err = -errno;
 		pr_warn("legacy uprobe perf_event_open() failed: %d\n", err);
-		return err;
+		goto err_clean_legacy;
 	}
 	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. */
@@ -10593,7 +10599,7 @@ bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
 			prog->name, retprobe ? "uretprobe" : "uprobe",
 			binary_path, func_offset,
 			libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
-		goto err_out;
+		goto err_clean_legacy;
 	}
 	if (legacy) {
 		struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link);
@@ -10603,10 +10609,14 @@ bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
 		perf_link->legacy_is_retprobe = retprobe;
 	}
 	return link;
+
+err_clean_legacy:
+	if (legacy)
+		remove_uprobe_event_legacy(legacy_probe, retprobe);
+
 err_out:
 	free(legacy_probe);
 	return libbpf_err_ptr(err);
-
 }
 
 /* Format of u[ret]probe section definition supporting auto-attach:
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v4 0/3] cleanup the legacy probe_event on failed scenario
  2022-06-29 15:18 [PATCH v4 0/3] cleanup the legacy probe_event on failed scenario Chuang Wang
                   ` (2 preceding siblings ...)
  2022-06-29 15:18 ` [PATCH v4 3/3] libbpf: cleanup the legacy uprobe_event on failed add/attach_event() Chuang Wang
@ 2022-07-06  4:30 ` patchwork-bot+netdevbpf
  3 siblings, 0 replies; 5+ messages in thread
From: patchwork-bot+netdevbpf @ 2022-07-06  4:30 UTC (permalink / raw)
  To: Chuang Wang
  Cc: ast, daniel, andrii, kafai, songliubraving, yhs, john.fastabend,
	kpsingh, netdev, bpf

Hello:

This series was applied to bpf/bpf-next.git (master)
by Andrii Nakryiko <andrii@kernel.org>:

On Wed, 29 Jun 2022 23:18:44 +0800 you wrote:
> 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.
> 
> [...]

Here is the summary with links:
  - [v4,1/3] libbpf: cleanup the legacy kprobe_event on failed add/attach_event()
    https://git.kernel.org/bpf/bpf-next/c/8094029330a2
  - [v4,2/3] libbpf: fix wrong variable used in perf_event_uprobe_open_legacy()
    https://git.kernel.org/bpf/bpf-next/c/5666fc997ccb
  - [v4,3/3] libbpf: cleanup the legacy uprobe_event on failed add/attach_event()
    https://git.kernel.org/bpf/bpf-next/c/2655144fb49b

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-07-06  4:30 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-29 15:18 [PATCH v4 0/3] cleanup the legacy probe_event on failed scenario Chuang Wang
2022-06-29 15:18 ` [PATCH v4 1/3] libbpf: cleanup the legacy kprobe_event on failed add/attach_event() Chuang Wang
2022-06-29 15:18 ` [PATCH v4 2/3] libbpf: fix wrong variable used in perf_event_uprobe_open_legacy() Chuang Wang
2022-06-29 15:18 ` [PATCH v4 3/3] libbpf: cleanup the legacy uprobe_event on failed add/attach_event() Chuang Wang
2022-07-06  4:30 ` [PATCH v4 0/3] cleanup the legacy probe_event on failed scenario patchwork-bot+netdevbpf

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.