bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jiri Olsa <jolsa@redhat.com>
To: Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>
Cc: netdev@vger.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>
Subject: [PATCH bpf-next 29/29] selftests/bpf: Add attach multi func test
Date: Thu, 18 Nov 2021 12:24:55 +0100	[thread overview]
Message-ID: <20211118112455.475349-30-jolsa@kernel.org> (raw)
In-Reply-To: <20211118112455.475349-1-jolsa@kernel.org>

Adding test code to check on trampolines spliting.

The tests attached various bpf_fetry_* functions in a way
so there's always non trivial IDs intersection, that leads
to trampoline splitting in kenrel code.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
 tools/testing/selftests/bpf/Makefile          |   4 +-
 .../bpf/prog_tests/multi_attach_test.c        | 176 ++++++++++++++++++
 .../selftests/bpf/progs/multi_attach.c        | 105 +++++++++++
 3 files changed, 284 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/multi_attach_test.c
 create mode 100644 tools/testing/selftests/bpf/progs/multi_attach.c

diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
index 48970e983250..095d966a747a 100644
--- a/tools/testing/selftests/bpf/Makefile
+++ b/tools/testing/selftests/bpf/Makefile
@@ -324,7 +324,8 @@ SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c
 LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h		\
 		linked_vars.skel.h linked_maps.skel.h			\
 		multi_fentry_test.skel.h multi_fexit_test.skel.h	\
-		multi_fentry_fexit_test.skel.h multi_mixed_test.skel.h
+		multi_fentry_fexit_test.skel.h multi_mixed_test.skel.h	\
+		multi_attach_test.skel.h
 
 LSKELS := kfunc_call_test.c fentry_test.c fexit_test.c fexit_sleep.c \
 	test_ringbuf.c atomics.c trace_printk.c trace_vprintk.c
@@ -340,6 +341,7 @@ multi_fentry_test.skel.h-deps := multi_fentry.o multi_check.o
 multi_fexit_test.skel.h-deps := multi_fexit.o multi_check.o
 multi_fentry_fexit_test.skel.h-deps := multi_fentry_fexit.o multi_check.o
 multi_mixed_test.skel.h-deps := multi_mixed.o multi_check.o
+multi_attach_test.skel.h-deps := multi_attach.o multi_check.o
 
 LINKED_BPF_SRCS := $(patsubst %.o,%.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps)))
 
diff --git a/tools/testing/selftests/bpf/prog_tests/multi_attach_test.c b/tools/testing/selftests/bpf/prog_tests/multi_attach_test.c
new file mode 100644
index 000000000000..c183941215a6
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/multi_attach_test.c
@@ -0,0 +1,176 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+#include <linux/btf_ids.h>
+#include "multi_attach_test.skel.h"
+#include <bpf/btf.h>
+
+static __u32 btf_ids[8];
+
+static int load_btf_ids(void)
+{
+	__u32 i, nr_types, cnt;
+	struct btf *btf;
+
+	btf = btf__load_vmlinux_btf();
+	if (!ASSERT_OK_PTR(btf, "btf__load_vmlinux_btf"))
+		return -1;
+
+	nr_types = btf__get_nr_types(btf);
+
+	for (i = 1, cnt = 0; i <= nr_types && cnt < 8; i++) {
+		const struct btf_type *t = btf__type_by_id(btf, i);
+		const char *name;
+
+		if (!btf_is_func(t))
+			continue;
+
+		name = btf__name_by_offset(btf, t->name_off);
+		if (!name)
+			continue;
+		if (strncmp(name, "bpf_fentry_test", sizeof("bpf_fentry_test") - 1))
+			continue;
+
+		btf_ids[cnt] = i;
+		cnt++;
+	}
+
+	btf__free(btf);
+	return ASSERT_EQ(cnt, 8, "bpf_fentry_test_cnt") ? 0 : -1;
+}
+
+static int link_prog_from_cnt(const struct bpf_program *prog, int from, int cnt)
+{
+	DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts);
+	enum bpf_attach_type attach_type;
+	int prog_fd, link_fd;
+
+	opts.multi.btf_ids = btf_ids + (from - 1);
+	opts.multi.btf_ids_cnt = cnt;
+
+	prog_fd = bpf_program__fd(prog);
+	if (!ASSERT_GE(prog_fd, 0, "link_from_to_prog_fd"))
+		return -1;
+	attach_type = bpf_program__get_expected_attach_type(prog);
+	link_fd = bpf_link_create(prog_fd, 0, attach_type, &opts);
+	if (!ASSERT_GE(link_fd, 0, "link_from_to_link_fd"))
+		return -1;
+	return link_fd;
+}
+
+static int prog_from_cnt(const struct bpf_program *prog, int *from, int *cnt)
+{
+	const char *sec;
+	int err, to;
+
+	sec = bpf_program__section_name(prog);
+	sec = strchr(sec, '/');
+	if (!sec)
+		return -1;
+	sec++;
+	err = sscanf(sec, "bpf_fentry_test%d-%d", from, &to);
+	if (err != 2)
+		return -1;
+	*cnt = to - *from + 1;
+	return 0;
+}
+
+static int link_test(const struct bpf_program *prog1,
+		     const struct bpf_program *prog2,
+		     __u64 *test_result1, __u64 *test_result2,
+		     bool do_close, int link_fd[2])
+{
+	int from1, cnt1, from2, cnt2, err;
+	__u32 duration = 0, retval;
+
+	if (!ASSERT_OK(prog_from_cnt(prog1, &from1, &cnt1), "prog_from_cnt__prog1"))
+		return -1;
+
+	if (!ASSERT_OK(prog_from_cnt(prog2, &from2, &cnt2), "prog_from_cnt__prog2"))
+		return -1;
+
+	link_fd[0] = link_prog_from_cnt(prog1, from1, cnt1);
+	if (link_fd[0] < 0)
+		return -1;
+
+	link_fd[1] = link_prog_from_cnt(prog2, from2, cnt2);
+	if (link_fd[1] < 0)
+		return -1;
+
+	*test_result1 = 0;
+	*test_result2 = 0;
+
+	err = bpf_prog_test_run(bpf_program__fd(prog1), 1, NULL, 0,
+				NULL, NULL, &retval, &duration);
+
+	ASSERT_OK(err, "test_run");
+	ASSERT_EQ(retval, 0, "test_run");
+	ASSERT_EQ(*test_result1, cnt1, "test_result");
+	ASSERT_EQ(*test_result2, cnt2, "test_result");
+
+	if (do_close) {
+		close(link_fd[0]);
+		close(link_fd[1]);
+	}
+	return err;
+}
+
+void test_multi_attach_test(void)
+{
+	struct bpf_link *link7 = NULL, *link8 = NULL;
+	int link_fd[6] = { -1 }, i, err;
+	struct multi_attach_test *skel;
+	__u32 duration = 0, retval;
+
+	for (i = 0; i < 6; i++)
+		link_fd[i] = -1;
+
+	skel = multi_attach_test__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "multi_attach__load"))
+		return;
+
+	if (!ASSERT_OK(load_btf_ids(), "load_btf_ids"))
+		goto cleanup;
+
+#define LINK_TEST(__prog1, __prog2, __close)			\
+	err = link_test(skel->progs.test ## __prog1,		\
+			skel->progs.test ## __prog2,		\
+			&skel->bss->test_result ## __prog1,	\
+			&skel->bss->test_result ## __prog2,	\
+			__close, link_fd + __prog1 - 1);	\
+	if (err)						\
+		goto cleanup;
+
+	LINK_TEST(1, 2, true);
+	LINK_TEST(3, 4, true);
+	LINK_TEST(1, 3, true);
+	LINK_TEST(2, 4, true);
+
+	LINK_TEST(1, 2, false);
+	LINK_TEST(3, 4, false);
+	LINK_TEST(5, 6, false);
+
+#undef LINK_TEST
+
+	link7 = bpf_program__attach(skel->progs.test7);
+	if (!ASSERT_OK_PTR(link7, "multi_attach_check__test1_attach"))
+		goto cleanup;
+
+	link8 = bpf_program__attach(skel->progs.test8);
+	if (!ASSERT_OK_PTR(link7, "multi_attach_check__test2_attach"))
+		goto cleanup;
+
+	err = bpf_prog_test_run(bpf_program__fd(skel->progs.test7), 1, NULL, 0,
+				NULL, NULL, &retval, &duration);
+
+	ASSERT_OK(err, "test_run");
+	ASSERT_EQ(retval, 0, "test_run");
+	ASSERT_EQ(skel->bss->test_result7, 1, "test_result7");
+	ASSERT_EQ(skel->bss->test_result8, 1, "test_result8");
+
+cleanup:
+	bpf_link__destroy(link8);
+	bpf_link__destroy(link7);
+	for (i = 0; i < 6; i++)
+		close(link_fd[i]);
+	multi_attach_test__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/multi_attach.c b/tools/testing/selftests/bpf/progs/multi_attach.c
new file mode 100644
index 000000000000..d403f5f1f27e
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/multi_attach.c
@@ -0,0 +1,105 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "GPL";
+
+__hidden extern void multi_arg_check(__u64 *ctx, __u64 *test_result);
+__hidden extern void multi_ret_check(void *ctx, __u64 *test_result);
+
+__u64 test_result1 = 0;
+
+SEC("fentry.multi/bpf_fentry_test1-5")
+int BPF_PROG(test1, __u64 a, __u64 b, __u64 c, __u64 d, __u64 e, __u64 f, int ret)
+{
+	multi_arg_check(ctx, &test_result1);
+	return 0;
+}
+
+__u64 test_result2 = 0;
+
+SEC("fentry.multi/bpf_fentry_test4-8")
+int BPF_PROG(test2, __u64 a, __u64 b, __u64 c, __u64 d, __u64 e, __u64 f, int ret)
+{
+	multi_arg_check(ctx, &test_result2);
+	return 0;
+}
+
+__u64 test_result3 = 0;
+
+SEC("fexit.multi/bpf_fentry_test1-5")
+int BPF_PROG(test3, __u64 a, __u64 b, __u64 c, __u64 d, __u64 e, __u64 f, int ret)
+{
+	__u64 arg_result = 0, ret_result = 0;
+
+	multi_arg_check(ctx, &arg_result);
+	multi_ret_check(ctx, &ret_result);
+
+	if (arg_result && ret_result)
+		test_result3 += 1;
+	return 0;
+}
+
+__u64 test_result4 = 0;
+
+SEC("fexit.multi/bpf_fentry_test4-8")
+int BPF_PROG(test4, __u64 a, __u64 b, __u64 c, __u64 d, __u64 e, __u64 f, int ret)
+{
+	__u64 arg_result = 0, ret_result = 0;
+
+	multi_arg_check(ctx, &arg_result);
+	multi_ret_check(ctx, &ret_result);
+
+	if (arg_result && ret_result)
+		test_result4 += 1;
+	return 0;
+}
+
+__u64 test_result5 = 0;
+
+SEC("fentry.multi/bpf_fentry_test1-8")
+int BPF_PROG(test5, __u64 a, __u64 b, __u64 c, __u64 d, __u64 e, __u64 f)
+{
+	multi_arg_check(ctx, &test_result5);
+	return 0;
+}
+
+__u64 test_result6 = 0;
+
+SEC("fexit.multi/bpf_fentry_test1-8")
+int BPF_PROG(test6, __u64 a, __u64 b, __u64 c, __u64 d, __u64 e, __u64 f)
+{
+	__u64 arg_result = 0, ret_result = 0;
+
+	multi_arg_check(ctx, &arg_result);
+	multi_ret_check(ctx, &ret_result);
+
+	if (arg_result && ret_result)
+		test_result6 += 1;
+	return 0;
+}
+
+__u64 test_result7 = 0;
+
+SEC("fentry/bpf_fentry_test1")
+int BPF_PROG(test7, int a)
+{
+	multi_arg_check(ctx, &test_result7);
+	return 0;
+}
+
+__u64 test_result8 = 0;
+
+SEC("fexit/bpf_fentry_test2")
+int BPF_PROG(test8, int a, __u64 b, int ret)
+{
+	__u64 arg_result = 0, ret_result = 0;
+
+	multi_arg_check(ctx, &arg_result);
+	multi_ret_check(ctx, &ret_result);
+
+	if (arg_result && ret_result)
+		test_result8 += 1;
+	return 0;
+}
-- 
2.31.1


      parent reply	other threads:[~2021-11-18 11:30 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-18 11:24 [RFC bpf-next v5 00/29] bpf: Add batch support for attaching trampolines Jiri Olsa
2021-11-18 11:24 ` [PATCH bpf-next 01/29] ftrace: Use direct_ops hash in unregister_ftrace_direct Jiri Olsa
2021-11-18 11:24 ` [PATCH bpf-next 02/29] ftrace: Add cleanup to unregister_ftrace_direct_multi Jiri Olsa
2021-11-18 11:24 ` [PATCH bpf-next 03/29] ftrace: Add ftrace_set_filter_ips function Jiri Olsa
2021-11-18 11:24 ` [PATCH bpf-next 04/29] bpf: Factor bpf_check_attach_target function Jiri Olsa
2021-11-18 11:24 ` [PATCH bpf-next 05/29] bpf: Add bpf_check_attach_model function Jiri Olsa
2021-11-18 11:24 ` [PATCH bpf-next 06/29] bpf: Add bpf_arg/bpf_ret_value helpers for tracing programs Jiri Olsa
2021-11-24 21:43   ` Andrii Nakryiko
2021-11-25 16:14     ` Alexei Starovoitov
2021-11-28 18:07       ` Jiri Olsa
2021-11-28 18:06     ` Jiri Olsa
2021-12-01  7:13       ` Andrii Nakryiko
2021-12-01 17:37         ` Alexei Starovoitov
2021-12-01 17:59           ` Andrii Nakryiko
2021-12-01 20:36             ` Alexei Starovoitov
2021-12-01 21:16             ` Jiri Olsa
2021-11-18 11:24 ` [PATCH bpf-next 07/29] bpf, x64: Allow to use caller address from stack Jiri Olsa
2021-11-19  4:14   ` Alexei Starovoitov
2021-11-19 21:46     ` Jiri Olsa
2021-11-18 11:24 ` [PATCH bpf-next 08/29] bpf: Keep active attached trampoline in bpf_prog Jiri Olsa
2021-11-24 21:48   ` Andrii Nakryiko
2021-11-28 17:24     ` Jiri Olsa
2021-11-18 11:24 ` [PATCH bpf-next 09/29] bpf: Add support to load multi func tracing program Jiri Olsa
2021-11-19  4:11   ` Alexei Starovoitov
2021-11-22 20:15     ` Jiri Olsa
2021-11-24 21:51       ` Andrii Nakryiko
2021-11-28 17:41         ` Jiri Olsa
2021-12-01  7:17           ` Andrii Nakryiko
2021-12-01 21:20             ` Jiri Olsa
2021-11-18 11:24 ` [PATCH bpf-next 10/29] bpf: Add bpf_trampoline_id object Jiri Olsa
2021-11-18 11:24 ` [PATCH bpf-next 11/29] bpf: Add addr to " Jiri Olsa
2021-11-18 11:24 ` [PATCH bpf-next 12/29] bpf: Add struct bpf_tramp_node layer Jiri Olsa
2021-11-18 11:24 ` [PATCH bpf-next 13/29] bpf: Add bpf_tramp_attach layer for trampoline attachment Jiri Olsa
2021-11-18 11:24 ` [PATCH bpf-next 14/29] bpf: Add support to store multiple ids in bpf_tramp_id object Jiri Olsa
2021-11-18 11:24 ` [PATCH bpf-next 15/29] bpf: Add support to store multiple addrs " Jiri Olsa
2021-11-18 11:24 ` [PATCH bpf-next 16/29] bpf: Add bpf_tramp_id_single function Jiri Olsa
2021-11-18 11:24 ` [PATCH bpf-next 17/29] bpf: Resolve id in bpf_tramp_id_single Jiri Olsa
2021-11-18 11:24 ` [PATCH bpf-next 18/29] bpf: Add refcount_t to struct bpf_tramp_id Jiri Olsa
2021-11-18 11:24 ` [PATCH bpf-next 19/29] bpf: Add support to attach trampolines with multiple IDs Jiri Olsa
2021-11-18 11:24 ` [PATCH bpf-next 20/29] bpf: Add support for tracing multi link Jiri Olsa
2021-11-18 11:24 ` [PATCH bpf-next 21/29] libbpf: Add btf__find_by_glob_kind function Jiri Olsa
2021-11-18 11:24 ` [PATCH bpf-next 22/29] libbpf: Add support to link multi func tracing program Jiri Olsa
2021-11-18 11:24 ` [PATCH bpf-next 23/29] selftests/bpf: Add bpf_arg/bpf_ret_value test Jiri Olsa
2021-11-18 11:24 ` [PATCH bpf-next 24/29] selftests/bpf: Add fentry multi func test Jiri Olsa
2021-11-18 11:24 ` [PATCH bpf-next 25/29] selftests/bpf: Add fexit " Jiri Olsa
2021-11-18 11:24 ` [PATCH bpf-next 26/29] selftests/bpf: Add fentry/fexit " Jiri Olsa
2021-11-18 11:24 ` [PATCH bpf-next 27/29] selftests/bpf: Add mixed " Jiri Olsa
2021-11-18 11:24 ` [PATCH bpf-next 28/29] selftests/bpf: Add ret_mod " Jiri Olsa
2021-11-18 11:24 ` Jiri Olsa [this message]

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=20211118112455.475349-30-jolsa@kernel.org \
    --to=jolsa@redhat.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=john.fastabend@gmail.com \
    --cc=kafai@fb.com \
    --cc=kpsingh@chromium.org \
    --cc=netdev@vger.kernel.org \
    --cc=songliubraving@fb.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).