bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jiri Olsa <olsajiri@gmail.com>
To: Jiri Olsa <olsajiri@gmail.com>
Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	bpf@vger.kernel.org, Martin KaFai Lau <kafai@fb.com>,
	Song Liu <songliubraving@fb.com>, Yonghong Song <yhs@fb.com>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@chromium.org>,
	Stanislav Fomichev <sdf@google.com>, Hao Luo <haoluo@google.com>,
	Viktor Malik <vmalik@redhat.com>,
	"Masami Hiramatsu (Google)" <mhiramat@kernel.org>
Subject: Re: [PATCH bpf-next 6/7] selftests/bpf: Add kprobe multi session test
Date: Tue, 30 Apr 2024 10:10:22 +0200	[thread overview]
Message-ID: <ZjCnbvVsWFSbUEkZ@krava> (raw)
In-Reply-To: <ZijwsrKWCbo57vUE@krava>

On Wed, Apr 24, 2024 at 01:44:50PM +0200, Jiri Olsa wrote:

SNIP

> > see below, even if array of ksym ptrs idea doesn't work out, at least
> > results can be an array (which is cleaner to work with both on BPF and
> > user space sides)
> 
> I recall in past we used to do that and we switched to specific values
> to be more explicit I guess.. but it might make sense in here, will try it 
> 
> SNIP
> 
> > > +static int session_check(void *ctx, bool is_return)
> > > +{
> > > +       if (bpf_get_current_pid_tgid() >> 32 != pid)
> > > +               return 1;
> > > +
> > > +       __u64 addr = bpf_get_func_ip(ctx);
> > > +
> > > +#define SET(__var, __addr) ({                  \
> > > +       if ((const void *) addr == __addr)      \
> > > +               __var = 1;                      \
> > > +})
> > > +
> > > +       if (is_return) {
> > > +               SET(kretprobe_test1_result, &bpf_fentry_test1);
> > > +               SET(kretprobe_test2_result, &bpf_fentry_test2);
> > > +               SET(kretprobe_test3_result, &bpf_fentry_test3);
> > > +               SET(kretprobe_test4_result, &bpf_fentry_test4);
> > > +               SET(kretprobe_test5_result, &bpf_fentry_test5);
> > > +               SET(kretprobe_test6_result, &bpf_fentry_test6);
> > > +               SET(kretprobe_test7_result, &bpf_fentry_test7);
> > > +               SET(kretprobe_test8_result, &bpf_fentry_test8);
> > > +       } else {
> > > +               SET(kprobe_test1_result, &bpf_fentry_test1);
> > > +               SET(kprobe_test2_result, &bpf_fentry_test2);
> > > +               SET(kprobe_test3_result, &bpf_fentry_test3);
> > > +               SET(kprobe_test4_result, &bpf_fentry_test4);
> > > +               SET(kprobe_test5_result, &bpf_fentry_test5);
> > > +               SET(kprobe_test6_result, &bpf_fentry_test6);
> > > +               SET(kprobe_test7_result, &bpf_fentry_test7);
> > > +               SET(kprobe_test8_result, &bpf_fentry_test8);
> > > +       }
> > > +
> > > +#undef SET
> > 
> > curious, have you tried implementing this through a proper for loop? I
> > wonder if something like
> > 
> > void *kfuncs[] = { &bpf_fentry_test1, ..., &bpf_fentry_test8 };
> > 
> > and then generic loop over this array would work. Can you please try?
> 
> yep, will try, let's see if it gets nicer

ok it looks better, I'll send new version

thanks,
jirka


---
diff --git a/tools/testing/selftests/bpf/bpf_kfuncs.h b/tools/testing/selftests/bpf/bpf_kfuncs.h
index 14ebe7d9e1a3..180030b5d828 100644
--- a/tools/testing/selftests/bpf/bpf_kfuncs.h
+++ b/tools/testing/selftests/bpf/bpf_kfuncs.h
@@ -75,4 +75,6 @@ extern void bpf_key_put(struct bpf_key *key) __ksym;
 extern int bpf_verify_pkcs7_signature(struct bpf_dynptr *data_ptr,
 				      struct bpf_dynptr *sig_ptr,
 				      struct bpf_key *trusted_keyring) __ksym;
+
+extern bool bpf_session_is_return(void) __ksym;
 #endif
diff --git a/tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c b/tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c
index 51628455b6f5..f6eac16a9339 100644
--- a/tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c
+++ b/tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c
@@ -4,6 +4,7 @@
 #include "trace_helpers.h"
 #include "kprobe_multi_empty.skel.h"
 #include "kprobe_multi_override.skel.h"
+#include "kprobe_multi_session.skel.h"
 #include "bpf/libbpf_internal.h"
 #include "bpf/hashmap.h"
 
@@ -326,6 +327,46 @@ static void test_attach_api_fails(void)
 	kprobe_multi__destroy(skel);
 }
 
+static void test_session_skel_api(void)
+{
+	struct kprobe_multi_session *skel = NULL;
+	LIBBPF_OPTS(bpf_kprobe_multi_opts, opts);
+	LIBBPF_OPTS(bpf_test_run_opts, topts);
+	struct bpf_link *link = NULL;
+	int err, prog_fd;
+
+	skel = kprobe_multi_session__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "kprobe_multi_session__open_and_load"))
+		return;
+
+	skel->bss->pid = getpid();
+
+	err = kprobe_multi_session__attach(skel);
+	if (!ASSERT_OK(err, " kprobe_multi_session__attach"))
+		goto cleanup;
+
+	prog_fd = bpf_program__fd(skel->progs.trigger);
+	err = bpf_prog_test_run_opts(prog_fd, &topts);
+	ASSERT_OK(err, "test_run");
+	ASSERT_EQ(topts.retval, 0, "test_run");
+
+	/* bpf_fentry_test1-4 trigger return probe, result is 2 */
+	ASSERT_EQ(skel->bss->kprobe_session_result[0], 2, "kprobe_session_result[0]");
+	ASSERT_EQ(skel->bss->kprobe_session_result[1], 2, "kprobe_session_result[1]");
+	ASSERT_EQ(skel->bss->kprobe_session_result[2], 2, "kprobe_session_result[2]");
+	ASSERT_EQ(skel->bss->kprobe_session_result[3], 2, "kprobe_session_result[3]");
+
+	/* bpf_fentry_test5-8 trigger only entry probe, result is 1 */
+	ASSERT_EQ(skel->bss->kprobe_session_result[4], 1, "kprobe_session_result[4]");
+	ASSERT_EQ(skel->bss->kprobe_session_result[5], 1, "kprobe_session_result[5]");
+	ASSERT_EQ(skel->bss->kprobe_session_result[6], 1, "kprobe_session_result[6]");
+	ASSERT_EQ(skel->bss->kprobe_session_result[7], 1, "kprobe_session_result[7]");
+
+cleanup:
+	bpf_link__destroy(link);
+	kprobe_multi_session__destroy(skel);
+}
+
 static size_t symbol_hash(long key, void *ctx __maybe_unused)
 {
 	return str_hash((const char *) key);
@@ -690,4 +731,6 @@ void test_kprobe_multi_test(void)
 		test_attach_api_fails();
 	if (test__start_subtest("attach_override"))
 		test_attach_override();
+	if (test__start_subtest("session"))
+		test_session_skel_api();
 }
diff --git a/tools/testing/selftests/bpf/progs/kprobe_multi_session.c b/tools/testing/selftests/bpf/progs/kprobe_multi_session.c
new file mode 100644
index 000000000000..3f4137100482
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/kprobe_multi_session.c
@@ -0,0 +1,78 @@
+// 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"
+
+char _license[] SEC("license") = "GPL";
+
+extern const void bpf_fentry_test1 __ksym;
+extern const void bpf_fentry_test2 __ksym;
+extern const void bpf_fentry_test3 __ksym;
+extern const void bpf_fentry_test4 __ksym;
+extern const void bpf_fentry_test5 __ksym;
+extern const void bpf_fentry_test6 __ksym;
+extern const void bpf_fentry_test7 __ksym;
+extern const void bpf_fentry_test8 __ksym;
+
+int pid = 0;
+
+__u64 kprobe_session_result[8];
+
+static const void *kfuncs[8] = {
+	&bpf_fentry_test1,
+	&bpf_fentry_test2,
+	&bpf_fentry_test3,
+	&bpf_fentry_test4,
+	&bpf_fentry_test5,
+	&bpf_fentry_test6,
+	&bpf_fentry_test7,
+	&bpf_fentry_test8,
+};
+
+static int session_check(void *ctx, bool is_return)
+{
+	unsigned int i;
+	__u64 addr;
+
+	if (bpf_get_current_pid_tgid() >> 32 != pid)
+		return 1;
+
+	addr = bpf_get_func_ip(ctx);
+
+	for (i = 0; i < 8; i++) {
+		if (kfuncs[i] == (void *) addr) {
+			kprobe_session_result[i]++;
+			break;
+		}
+	}
+
+	/*
+	 * Force probes for function bpf_fentry_test[5-8] not to
+	 * install and execute the return probe
+	 */
+	if (((const void *) addr == &bpf_fentry_test5) ||
+	    ((const void *) addr == &bpf_fentry_test6) ||
+	    ((const void *) addr == &bpf_fentry_test7) ||
+	    ((const void *) addr == &bpf_fentry_test8))
+		return 1;
+
+	return 0;
+}
+
+/*
+ * No tests in here, just to trigger 'bpf_fentry_test*'
+ * through tracing test_run
+ */
+SEC("fentry/bpf_modify_return_test")
+int BPF_PROG(trigger)
+{
+	return 0;
+}
+
+SEC("kprobe.session/bpf_fentry_test*")
+int test_kprobe(struct pt_regs *ctx)
+{
+	return session_check(ctx, bpf_session_is_return());
+}

  reply	other threads:[~2024-04-30  8:10 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-22 12:12 [PATCH bpf-next 0/7] bpf: Introduce kprobe_multi session attach Jiri Olsa
2024-04-22 12:12 ` [PATCH bpf-next 1/7] bpf: Add support for kprobe multi " Jiri Olsa
2024-04-24  0:26   ` Andrii Nakryiko
2024-04-24 11:46     ` Jiri Olsa
2024-04-22 12:12 ` [PATCH bpf-next 2/7] bpf: Add support for kprobe multi session context Jiri Olsa
2024-04-24  0:26   ` Andrii Nakryiko
2024-04-24 11:45     ` Jiri Olsa
2024-04-22 12:12 ` [PATCH bpf-next 3/7] bpf: Add support for kprobe multi session cookie Jiri Olsa
2024-04-22 17:48   ` Alexei Starovoitov
2024-04-22 20:55     ` Jiri Olsa
2024-04-24  0:26   ` Andrii Nakryiko
2024-04-24 11:45     ` Jiri Olsa
2024-04-22 12:12 ` [PATCH bpf-next 4/7] libbpf: Add support for kprobe multi session attach Jiri Olsa
2024-04-24  0:26   ` Andrii Nakryiko
2024-04-24 11:45     ` Jiri Olsa
2024-04-22 12:12 ` [PATCH bpf-next 5/7] libbpf: Add kprobe session attach type name to attach_type_name Jiri Olsa
2024-04-24  0:27   ` Andrii Nakryiko
2024-04-24 11:44     ` Jiri Olsa
2024-04-22 12:12 ` [PATCH bpf-next 6/7] selftests/bpf: Add kprobe multi session test Jiri Olsa
2024-04-24  0:27   ` Andrii Nakryiko
2024-04-24 11:44     ` Jiri Olsa
2024-04-30  8:10       ` Jiri Olsa [this message]
2024-04-22 12:12 ` [PATCH bpf-next 7/7] selftests/bpf: Add kprobe multi wrapper cookie test Jiri Olsa
2024-04-24  0:27   ` Andrii Nakryiko
2024-04-24 11:44     ` Jiri Olsa
2024-04-24  0:27 ` [PATCH bpf-next 0/7] bpf: Introduce kprobe_multi session attach Andrii Nakryiko
2024-04-24  5:12   ` John Fastabend
2024-04-24 11:43     ` Jiri Olsa

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZjCnbvVsWFSbUEkZ@krava \
    --to=olsajiri@gmail.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=kafai@fb.com \
    --cc=kpsingh@chromium.org \
    --cc=mhiramat@kernel.org \
    --cc=sdf@google.com \
    --cc=songliubraving@fb.com \
    --cc=vmalik@redhat.com \
    --cc=yhs@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).