selftests/bpf: fix btfgen tests
Subsystems (3)
Andrii Nakryiko
maintainerPATCH v3 1/1 selftests/bpf: fix btfgen tests
There turned out to be a few problems with btfgen selftests.
First, core_btfgen tests are failing in BPF CI due to the use of full-featured bpftool, which has extra dependencies on libbfd, libcap, etc, which are present in BPF CI's build environment, but those shared libraries are missing in QEMU image in which test_progs is running.
To fix this problem, use minimal bootstrap version of bpftool instead. It only depend on libelf and libz, same as libbpf, so doesn't add any new requirements (and bootstrap bpftool still implementes entire bpftool gen functionality, which is quite convenient).
Second problem is even more interesting. Both core_btfgen and core_reloc reuse the same set of struct core_reloc_test_case array of test case definitions. That in itself is not a problem, but btfgen test replaces test_case->btf_src_file property with the path to temporary file into which minimized BTF is output by bpftool. This interferes with original core_reloc tests, depending on order of tests execution (core_btfgen is run first in sequential mode and skrews up subsequent core_reloc run by pointing to already deleted temporary file, instead of the original BTF files) and whether those two runs share the same process (in parallel mode the chances are high for them to run in two separate processes and so not interfere with each other).
To prevent this interference, create and use local copy of a test definition. Mark original array as constant to catch accidental modifcations. Note that setup_type_id_case_success() and setup_type_id_case_success() still modify common test_case->output memory area, but it is ok as each setup function has to re-initialize it completely anyways. In sequential mode it leads to deterministic and correct initialization. In parallel mode they will either each have their own process, or if core_reloc and core_btfgen happen to be run by the same worker process, they will still do that sequentially within the worker process. If they are sharded across multiple processes, they don't really share anything anyways.
Also, rename core_btfgen into core_reloc_btfgen, as it is indeed just a "flavor" of core_reloc test, not an independent set of tests. So make it more obvious.
Last problem that needed solving was that location of bpftool differs between test_progs and test_progs' flavors (e.g., test_progs-no_alu32). To keep it simple, create a symlink to bpftool both inside selftests/bpf/ directory and selftests/bpf/<flavor> subdirectory. That way, from inside core_reloc test, location to bpftool is just "./bpftool".
v2->v3:
- fix bpftool location relative the test_progs-no_alu32;v1->v2:
- fix corruption of core_reloc_test_case.
--- tools/testing/selftests/bpf/.gitignore | 1 + tools/testing/selftests/bpf/Makefile | 3 ++- .../selftests/bpf/prog_tests/core_reloc.c | 17 +++++++++++------ 3 files changed, 14 insertions(+), 7 deletions(-)
tools/testing/selftests/bpf/.gitignore +1 -0
@@ -1,4 +1,5 @@ # SPDX-License-Identifier: GPL-2.0-only +bpftool bpf-helpers* bpf-syscall* test_verifier
tools/testing/selftests/bpf/Makefile +2 -1
@@ -470,6 +470,7 @@ $(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS) \ $$(call msg,BINARY,,$$@) $(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@ $(Q)$(RESOLVE_BTFIDS) --btf $(TRUNNER_OUTPUT)/btf_data.o $$@ + $(Q)ln -sf $(if $2,..,.)/tools/build/bpftool/bootstrap/bpftool $(if $2,$2/)bpftool endef
@@ -555,7 +556,7 @@ $(OUTPUT)/bench: $(OUTPUT)/bench.o \ EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(SCRATCH_DIR) $(HOST_SCRATCH_DIR) \ prog_tests/tests.h map_tests/tests.h verifier/tests.h \ - feature \ + feature bpftool \ $(addprefix $(OUTPUT)/,*.o *.skel.h *.lskel.h no_alu32 bpf_gcc bpf_testmod.ko) .PHONY: docs docs-clean
tools/testing/selftests/bpf/prog_tests/core_reloc.c +11 -6
@@ -512,7 +512,7 @@ static int __trigger_module_test_read(const struct core_reloc_test_case *test) } -static struct core_reloc_test_case test_cases[] = { +static const struct core_reloc_test_case test_cases[] = {/* validate we can find kernel image and use its BTF for relocs */ { .case_name ="kernel" ,
@@ -843,7 +843,7 @@ static int run_btfgen(const char *src_btf, const char *dst_btf, const char *objpint n; n =snprintf (command,sizeof (command), -"./tools/build/bpftool/bpftool gen min_core_btf %s %s %s" , +"./bpftool gen min_core_btf %s %s %s" , src_btf, dst_btf, objpath);if (n <0 || n >=sizeof (command))return -1 ;
@@ -855,7 +855,7 @@ static void run_core_reloc_tests(bool use_btfgen) {const size_t mmap_sz =roundup_page (sizeof (struct data ));DECLARE_LIBBPF_OPTS (bpf_object_open_opts, open_opts); -struct core_reloc_test_case *test_case; +struct core_reloc_test_case *test_case, test_case_copy;const char *tp_name, *probe_name;int err, i, equal, fd;struct bpf_link *link =NULL ;
@@ -870,7 +870,10 @@ static void run_core_reloc_tests(bool use_btfgen)for (i =0 ; i <ARRAY_SIZE (test_cases); i++) {char btf_file[] ="/tmp/core_reloc.btf.XXXXXX" ; - test_case = &test_cases[i]; + + test_case_copy = test_cases[i]; + test_case = &test_case_copy; +if (!test__start_subtest (test_case->case_name))continue ;
@@ -881,6 +884,7 @@ static void run_core_reloc_tests(bool use_btfgen)/* generate a "minimal" BTF file and use it as source */ if (use_btfgen) { +if (!test_case->btf_src_file || test_case->fails) {test__skip ();continue ;
@@ -989,7 +993,8 @@ static void run_core_reloc_tests(bool use_btfgen)CHECK_FAIL (munmap (mmap_data, mmap_sz)); mmap_data =NULL ; } -remove (btf_file); +if (use_btfgen) +remove (test_case->btf_src_file);bpf_link__destroy (link); link =NULL ;bpf_object__close (obj);
@@ -1001,7 +1006,7 @@ void test_core_reloc(void)run_core_reloc_tests (false); } -void test_core_btfgen (void ) +void test_core_reloc_btfgen (void ) {run_core_reloc_tests (true); }
Signature · 1 line
2.30.2
Thanks and it looks great! CI is passing too
https://github.com/kernel-patches/bpf/runs/5262640148?check_suite_focus=true
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 8 lines · On Sat, 19 Feb 2022 20:27:20 -0800 you wrote:
There turned out to be a few problems with btfgen selftests.
First,
core_btfgentests are failing in BPF CI due to the use of full-featured bpftool, which has extra dependencies on libbfd, libcap, etc, which are present in BPF CI's build environment, but those shared libraries are missing in QEMU image in whichtest_progsis running.[...]
Here is the summary with links:
- [v3,bpf-next] selftests/bpf: fix btfgen tests
https://git.kernel.org/bpf/bpf-next/c/b03e19465b97
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html
Quoted 13 lines · Andrii Nakryiko
There turned out to be a few problems with btfgen selftests.
First,
core_btfgentests are failing in BPF CI due to the use of full-featured bpftool, which has extra dependencies on libbfd, libcap, etc, which are present in BPF CI's build environment, but those shared libraries are missing in QEMU image in whichtest_progsis running.To fix this problem, use minimal bootstrap version of bpftool instead. It only depend on libelf and libz, same as libbpf, so doesn't add any new requirements (and bootstrap bpftool still implementes entire
bpftool genfunctionality, which is quite convenient).
Quoted 6 lines
Second problem is even more interesting. Both
core_btfgenandcore_relocreuse the same set of structcore_reloc_test_casearray of test case definitions. That in itself is not a problem, but btfgen test replacestest_case->btf_src_fileproperty with the path to temporary file into which minimized BTF is output by bpftool. This interferes with originalcore_reloctests, depending on order of tests execution
ah, good catch! Thanks for the fix!