Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: attach struct_ops maps before test prog runs

In test_loader based tests to bpf_map__attach_struct_ops()
before call to bpf_prog_test_run_opts() in order to trigger
bpf_struct_ops->reg() callbacks on kernel side.
This allows to use __retval macro for struct_ops tests.

Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
Link: https://lore.kernel.org/r/20240829210833.388152-6-martin.lau@linux.dev
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Eduard Zingerman and committed by
Alexei Starovoitov
a0dbf6d0 866d571e

+27
+27
tools/testing/selftests/bpf/test_loader.c
··· 890 890 { 891 891 struct test_subspec *subspec = unpriv ? &spec->unpriv : &spec->priv; 892 892 struct bpf_program *tprog = NULL, *tprog_iter; 893 + struct bpf_link *link, *links[32] = {}; 893 894 struct test_spec *spec_iter; 894 895 struct cap_state caps = {}; 895 896 struct bpf_object *tobj; 896 897 struct bpf_map *map; 897 898 int retval, err, i; 899 + int links_cnt = 0; 898 900 bool should_load; 899 901 900 902 if (!test__start_subtest(subspec->name)) ··· 1001 999 if (restore_capabilities(&caps)) 1002 1000 goto tobj_cleanup; 1003 1001 1002 + /* Do bpf_map__attach_struct_ops() for each struct_ops map. 1003 + * This should trigger bpf_struct_ops->reg callback on kernel side. 1004 + */ 1005 + bpf_object__for_each_map(map, tobj) { 1006 + if (!bpf_map__autocreate(map) || 1007 + bpf_map__type(map) != BPF_MAP_TYPE_STRUCT_OPS) 1008 + continue; 1009 + if (links_cnt >= ARRAY_SIZE(links)) { 1010 + PRINT_FAIL("too many struct_ops maps"); 1011 + goto tobj_cleanup; 1012 + } 1013 + link = bpf_map__attach_struct_ops(map); 1014 + if (!link) { 1015 + PRINT_FAIL("bpf_map__attach_struct_ops failed for map %s: err=%d\n", 1016 + bpf_map__name(map), err); 1017 + goto tobj_cleanup; 1018 + } 1019 + links[links_cnt++] = link; 1020 + } 1021 + 1004 1022 if (tester->pre_execution_cb) { 1005 1023 err = tester->pre_execution_cb(tobj); 1006 1024 if (err) { ··· 1035 1013 PRINT_FAIL("Unexpected retval: %d != %d\n", retval, subspec->retval); 1036 1014 goto tobj_cleanup; 1037 1015 } 1016 + /* redo bpf_map__attach_struct_ops for each test */ 1017 + while (links_cnt > 0) 1018 + bpf_link__destroy(links[--links_cnt]); 1038 1019 } 1039 1020 1040 1021 tobj_cleanup: 1022 + while (links_cnt > 0) 1023 + bpf_link__destroy(links[--links_cnt]); 1041 1024 bpf_object__close(tobj); 1042 1025 subtest_cleanup: 1043 1026 test__end_subtest();