diff mbox series

[bpf-next,29/29] selftests/bpf: Add attach multi func test

Message ID 20211118112455.475349-30-jolsa@kernel.org (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series bpf: Add batch support for attaching trampolines | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR fail PR summary
bpf/vmtest-bpf-next fail VM_Test
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Series has a cover letter
netdev/patch_count fail Series longer than 15 patches (and no cover letter)
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 3 maintainers not CCed: shuah@kernel.org kpsingh@kernel.org linux-kselftest@vger.kernel.org
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch fail CHECK: Macro argument '__prog1' may be better as '(__prog1)' to avoid precedence issues ERROR: Macros with multiple statements should be enclosed in a do - while loop WARNING: From:/Signed-off-by: email address mismatch: 'From: Jiri Olsa <jolsa@redhat.com>' != 'Signed-off-by: Jiri Olsa <jolsa@kernel.org>' WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? WARNING: line length of 82 exceeds 80 columns WARNING: line length of 84 exceeds 80 columns WARNING: macros should not use a trailing semicolon
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Jiri Olsa Nov. 18, 2021, 11:24 a.m. UTC
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 mbox series

Patch

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;
+}