From: Alexei Starovoitov Load a signed light skeleton with an arena, struct_ops, and programs and maps that are not loaded. Signed-off-by: Alexei Starovoitov --- tools/testing/selftests/bpf/Makefile.skel | 3 +- .../bpf/prog_tests/struct_ops_lskel.c | 32 ++++++++++ .../selftests/bpf/progs/struct_ops_places.c | 63 +++++++++++++++++++ 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_places.c diff --git a/tools/testing/selftests/bpf/Makefile.skel b/tools/testing/selftests/bpf/Makefile.skel index 2b8ceecacb29..f1708da95e74 100644 --- a/tools/testing/selftests/bpf/Makefile.skel +++ b/tools/testing/selftests/bpf/Makefile.skel @@ -38,7 +38,8 @@ LSKELS := fexit_sleep.c trace_printk.c trace_vprintk.c map_ptr_kern.c \ struct_ops_vmlinux.c struct_ops_mod.c \ test_ksyms_strong_typeless.c -LSKELS_SIGNED := fentry_test.c fexit_test.c share_subprogs_signed.c atomics.c +LSKELS_SIGNED := fentry_test.c fexit_test.c share_subprogs_signed.c atomics.c \ + struct_ops_places.c # Generate both light skeleton and libbpf skeleton for these LSKELS_EXTRA := test_ksyms_module.c test_ksyms_weak.c kfunc_call_test.c \ diff --git a/tools/testing/selftests/bpf/prog_tests/struct_ops_lskel.c b/tools/testing/selftests/bpf/prog_tests/struct_ops_lskel.c index a1fe16af6934..f3eda59c1d1d 100644 --- a/tools/testing/selftests/bpf/prog_tests/struct_ops_lskel.c +++ b/tools/testing/selftests/bpf/prog_tests/struct_ops_lskel.c @@ -1,11 +1,13 @@ // SPDX-License-Identifier: GPL-2.0 #include +#include #include #include "network_helpers.h" #include "test_btf.h" #include "struct_ops_vmlinux.lskel.h" #include "struct_ops_mod.lskel.h" +#include "struct_ops_places.lskel.h" static int set_tcp_ca_cb(int fd, void *opts) { @@ -115,6 +117,34 @@ static void test_module(void) struct_ops_mod_lskel__destroy(skel); } +static void test_places(void) +{ + LIBBPF_OPTS(bpf_test_run_opts, opts); + struct struct_ops_places_lskel *skel; + + if (!env.has_testmod) { + test__skip(); + return; + } + + skel = struct_ops_places_lskel__open(); + if (!ASSERT_OK_PTR(skel, "open")) + return; + skel->keyring_id = KEY_SPEC_SESSION_KEYRING; + if (!ASSERT_OK(struct_ops_places_lskel__load(skel), "load")) + goto cleanup; + + ASSERT_EQ(skel->maps.not_created.map_fd, 0, "not_created"); + ASSERT_EQ(skel->progs.not_attached.prog_fd, 0, "not_attached"); + ASSERT_EQ(skel->progs.not_loaded.prog_fd, 0, "not_loaded"); + ASSERT_OK(struct_ops_places_lskel__attach(skel), "attach"); + ASSERT_EQ(skel->bss->mul_result, 4 * 6, "mul_result"); + ASSERT_OK(bpf_prog_test_run_opts(skel->progs.alloc_arena_page.prog_fd, &opts), "run"); + ASSERT_EQ(opts.retval, 7, "alloc_arena_page"); +cleanup: + struct_ops_places_lskel__destroy(skel); +} + static void check_crafted_btf(const char *field_name, __u32 int_off, __u32 int_bits, __u32 int_sz, __u32 member_bit_off) { @@ -195,6 +225,8 @@ void test_struct_ops_lskel(void) test_vmlinux(); if (test__start_subtest("module")) test_module(); + if (test__start_subtest("places")) + test_places(); if (test__start_subtest("invalid_local_btf")) test_invalid_local_btf(); } diff --git a/tools/testing/selftests/bpf/progs/struct_ops_places.c b/tools/testing/selftests/bpf/progs/struct_ops_places.c new file mode 100644 index 000000000000..7f0c9ab2ba88 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/struct_ops_places.c @@ -0,0 +1,63 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include "bpf_experimental.h" +#include + +struct { + __uint(type, BPF_MAP_TYPE_ARENA); + __uint(map_flags, BPF_F_MMAPABLE); + __uint(max_entries, 16); +#ifdef __TARGET_ARCH_arm64 + __ulong(map_extra, 0x1ull << 32); +#else + __ulong(map_extra, 0x1ull << 44); +#endif +} arena SEC(".maps"); + +int mul_result; + +SEC("?raw_tp/sys_enter") +int not_attached(void *ctx) +{ + return 0; +} + +SEC("struct_ops/none") +int BPF_PROG(not_loaded) +{ + return 0; +} + +/* The map turns it on */ +SEC("?struct_ops/test_2") +void BPF_PROG(mul_args, int a, int b) +{ + mul_result = a * b; +} + +SEC("syscall") +int alloc_arena_page(void *ctx) +{ + return bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0) ? 7 : 0; +} + +struct bpf_testmod_ops___places { + int data; + void (*test_2)(int a, int b); + int (*none)(void); +}; + +SEC("?.struct_ops.link") +struct bpf_testmod_ops___places not_created = { + .none = (void *)not_loaded, +}; + +SEC(".struct_ops.link") +struct bpf_testmod_ops___places mul_ops = { + .data = 6, + .test_2 = (void *)mul_args, +}; + +char _license[] SEC("license") = "GPL"; -- 2.55.0