diff mbox series

[PATCHv2,bpf-next,8/9] selftests/bpf: Add uprobe session recursive test

Message ID 20240701164115.723677-9-jolsa@kernel.org (mailing list archive)
State Superseded
Headers show
Series uprobe, bpf: Add session support | expand

Commit Message

Jiri Olsa July 1, 2024, 4:41 p.m. UTC
Adding uprobe session test that verifies the cookie value is stored
properly when single uprobe-ed function is executed recursively.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
 .../bpf/prog_tests/uprobe_multi_test.c        | 57 +++++++++++++++++++
 .../progs/uprobe_multi_session_recursive.c    | 44 ++++++++++++++
 2 files changed, 101 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/uprobe_multi_session_recursive.c

Comments

Andrii Nakryiko July 2, 2024, 10:01 p.m. UTC | #1
On Mon, Jul 1, 2024 at 9:43 AM Jiri Olsa <jolsa@kernel.org> wrote:
>
> Adding uprobe session test that verifies the cookie value is stored
> properly when single uprobe-ed function is executed recursively.
>
> Signed-off-by: Jiri Olsa <jolsa@kernel.org>
> ---
>  .../bpf/prog_tests/uprobe_multi_test.c        | 57 +++++++++++++++++++
>  .../progs/uprobe_multi_session_recursive.c    | 44 ++++++++++++++
>  2 files changed, 101 insertions(+)
>  create mode 100644 tools/testing/selftests/bpf/progs/uprobe_multi_session_recursive.c
>

Nice!

Acked-by: Andrii Nakryiko <andrii@kernel.org>


[...]

> +static void test_session_recursive_skel_api(void)
> +{
> +       struct uprobe_multi_session_recursive *skel = NULL;
> +       int i, err;
> +
> +       skel = uprobe_multi_session_recursive__open_and_load();
> +       if (!ASSERT_OK_PTR(skel, "uprobe_multi_session_recursive__open_and_load"))
> +               goto cleanup;
> +
> +       skel->bss->pid = getpid();
> +
> +       err = uprobe_multi_session_recursive__attach(skel);
> +       if (!ASSERT_OK(err, "uprobe_multi_session_recursive__attach"))
> +               goto cleanup;
> +
> +       for (i = 0; i < ARRAY_SIZE(skel->bss->test_uprobe_cookie_entry); i++)
> +               skel->bss->test_uprobe_cookie_entry[i] = i + 1;
> +
> +       uprobe_session_recursive(5);
> +
> +       /*

nit: unnecessary empty comment line

> +        *                                         entry uprobe:
> +        * uprobe_session_recursive(5) {             *cookie = 1, return 0
> +        *   uprobe_session_recursive(4) {           *cookie = 2, return 1
> +        *     uprobe_session_recursive(3) {         *cookie = 3, return 0
> +        *       uprobe_session_recursive(2) {       *cookie = 4, return 1
> +        *         uprobe_session_recursive(1) {     *cookie = 5, return 0
> +        *           uprobe_session_recursive(0) {   *cookie = 6, return 1
> +        *                                          return uprobe:
> +        *           } i = 0                          not executed
> +        *         } i = 1                            test_uprobe_cookie_return[0] = 5
> +        *       } i = 2                              not executed
> +        *     } i = 3                                test_uprobe_cookie_return[1] = 3
> +        *   } i = 4                                  not executed
> +        * } i = 5                                    test_uprobe_cookie_return[2] = 1
> +        */
> +

[...]
Jiri Olsa July 3, 2024, 5:16 p.m. UTC | #2
On Tue, Jul 02, 2024 at 03:01:35PM -0700, Andrii Nakryiko wrote:
> On Mon, Jul 1, 2024 at 9:43 AM Jiri Olsa <jolsa@kernel.org> wrote:
> >
> > Adding uprobe session test that verifies the cookie value is stored
> > properly when single uprobe-ed function is executed recursively.
> >
> > Signed-off-by: Jiri Olsa <jolsa@kernel.org>
> > ---
> >  .../bpf/prog_tests/uprobe_multi_test.c        | 57 +++++++++++++++++++
> >  .../progs/uprobe_multi_session_recursive.c    | 44 ++++++++++++++
> >  2 files changed, 101 insertions(+)
> >  create mode 100644 tools/testing/selftests/bpf/progs/uprobe_multi_session_recursive.c
> >
> 
> Nice!
> 
> Acked-by: Andrii Nakryiko <andrii@kernel.org>
> 
> 
> [...]
> 
> > +static void test_session_recursive_skel_api(void)
> > +{
> > +       struct uprobe_multi_session_recursive *skel = NULL;
> > +       int i, err;
> > +
> > +       skel = uprobe_multi_session_recursive__open_and_load();
> > +       if (!ASSERT_OK_PTR(skel, "uprobe_multi_session_recursive__open_and_load"))
> > +               goto cleanup;
> > +
> > +       skel->bss->pid = getpid();
> > +
> > +       err = uprobe_multi_session_recursive__attach(skel);
> > +       if (!ASSERT_OK(err, "uprobe_multi_session_recursive__attach"))
> > +               goto cleanup;
> > +
> > +       for (i = 0; i < ARRAY_SIZE(skel->bss->test_uprobe_cookie_entry); i++)
> > +               skel->bss->test_uprobe_cookie_entry[i] = i + 1;
> > +
> > +       uprobe_session_recursive(5);
> > +
> > +       /*
> 
> nit: unnecessary empty comment line

ok

jirka

> 
> > +        *                                         entry uprobe:
> > +        * uprobe_session_recursive(5) {             *cookie = 1, return 0
> > +        *   uprobe_session_recursive(4) {           *cookie = 2, return 1
> > +        *     uprobe_session_recursive(3) {         *cookie = 3, return 0
> > +        *       uprobe_session_recursive(2) {       *cookie = 4, return 1
> > +        *         uprobe_session_recursive(1) {     *cookie = 5, return 0
> > +        *           uprobe_session_recursive(0) {   *cookie = 6, return 1
> > +        *                                          return uprobe:
> > +        *           } i = 0                          not executed
> > +        *         } i = 1                            test_uprobe_cookie_return[0] = 5
> > +        *       } i = 2                              not executed
> > +        *     } i = 3                                test_uprobe_cookie_return[1] = 3
> > +        *   } i = 4                                  not executed
> > +        * } i = 5                                    test_uprobe_cookie_return[2] = 1
> > +        */
> > +
> 
> [...]
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
index d5f78fc61013..b521590fdbb9 100644
--- a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
+++ b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
@@ -8,6 +8,7 @@ 
 #include "uprobe_multi_usdt.skel.h"
 #include "uprobe_multi_session.skel.h"
 #include "uprobe_multi_session_cookie.skel.h"
+#include "uprobe_multi_session_recursive.skel.h"
 #include "bpf/libbpf_internal.h"
 #include "testing_helpers.h"
 #include "../sdt.h"
@@ -34,6 +35,12 @@  noinline void usdt_trigger(void)
 	STAP_PROBE(test, pid_filter_usdt);
 }
 
+noinline void uprobe_session_recursive(int i)
+{
+	if (i)
+		uprobe_session_recursive(i - 1);
+}
+
 struct child {
 	int go[2];
 	int c2p[2]; /* child -> parent channel */
@@ -684,6 +691,54 @@  static void test_session_cookie_skel_api(void)
 	uprobe_multi_session_cookie__destroy(skel);
 }
 
+static void test_session_recursive_skel_api(void)
+{
+	struct uprobe_multi_session_recursive *skel = NULL;
+	int i, err;
+
+	skel = uprobe_multi_session_recursive__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "uprobe_multi_session_recursive__open_and_load"))
+		goto cleanup;
+
+	skel->bss->pid = getpid();
+
+	err = uprobe_multi_session_recursive__attach(skel);
+	if (!ASSERT_OK(err, "uprobe_multi_session_recursive__attach"))
+		goto cleanup;
+
+	for (i = 0; i < ARRAY_SIZE(skel->bss->test_uprobe_cookie_entry); i++)
+		skel->bss->test_uprobe_cookie_entry[i] = i + 1;
+
+	uprobe_session_recursive(5);
+
+	/*
+	 *                                         entry uprobe:
+	 * uprobe_session_recursive(5) {             *cookie = 1, return 0
+	 *   uprobe_session_recursive(4) {           *cookie = 2, return 1
+	 *     uprobe_session_recursive(3) {         *cookie = 3, return 0
+	 *       uprobe_session_recursive(2) {       *cookie = 4, return 1
+	 *         uprobe_session_recursive(1) {     *cookie = 5, return 0
+	 *           uprobe_session_recursive(0) {   *cookie = 6, return 1
+	 *                                          return uprobe:
+	 *           } i = 0                          not executed
+	 *         } i = 1                            test_uprobe_cookie_return[0] = 5
+	 *       } i = 2                              not executed
+	 *     } i = 3                                test_uprobe_cookie_return[1] = 3
+	 *   } i = 4                                  not executed
+	 * } i = 5                                    test_uprobe_cookie_return[2] = 1
+	 */
+
+	ASSERT_EQ(skel->bss->idx_entry, 6, "idx_entry");
+	ASSERT_EQ(skel->bss->idx_return, 3, "idx_return");
+
+	ASSERT_EQ(skel->bss->test_uprobe_cookie_return[0], 5, "test_uprobe_cookie_return[0]");
+	ASSERT_EQ(skel->bss->test_uprobe_cookie_return[1], 3, "test_uprobe_cookie_return[1]");
+	ASSERT_EQ(skel->bss->test_uprobe_cookie_return[2], 1, "test_uprobe_cookie_return[2]");
+
+cleanup:
+	uprobe_multi_session_recursive__destroy(skel);
+}
+
 static void test_bench_attach_uprobe(void)
 {
 	long attach_start_ns = 0, attach_end_ns = 0;
@@ -776,4 +831,6 @@  void test_uprobe_multi_test(void)
 		test_session_skel_api();
 	if (test__start_subtest("session_cookie"))
 		test_session_cookie_skel_api();
+	if (test__start_subtest("session_cookie_recursive"))
+		test_session_recursive_skel_api();
 }
diff --git a/tools/testing/selftests/bpf/progs/uprobe_multi_session_recursive.c b/tools/testing/selftests/bpf/progs/uprobe_multi_session_recursive.c
new file mode 100644
index 000000000000..8fbcd69fae22
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/uprobe_multi_session_recursive.c
@@ -0,0 +1,44 @@ 
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <stdbool.h>
+#include "bpf_kfuncs.h"
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+
+int pid = 0;
+
+int idx_entry = 0;
+int idx_return = 0;
+
+__u64 test_uprobe_cookie_entry[6];
+__u64 test_uprobe_cookie_return[3];
+
+static int check_cookie(void)
+{
+	__u64 *cookie = bpf_session_cookie();
+
+	if (bpf_session_is_return()) {
+		if (idx_return >= ARRAY_SIZE(test_uprobe_cookie_return))
+			return 1;
+		test_uprobe_cookie_return[idx_return++] = *cookie;
+		return 0;
+	}
+
+	if (idx_entry >= ARRAY_SIZE(test_uprobe_cookie_entry))
+		return 1;
+	*cookie = test_uprobe_cookie_entry[idx_entry];
+	return idx_entry++ % 2;
+}
+
+
+SEC("uprobe.session//proc/self/exe:uprobe_session_recursive")
+int uprobe_recursive(struct pt_regs *ctx)
+{
+	if (bpf_get_current_pid_tgid() >> 32 != pid)
+		return 1;
+
+	return check_cookie();
+}