The HID selftest skeleton contains several struct_ops maps, but each test
usually wants to load only the programs named by that test.

load_programs() disabled auto-attach for all maps, but left struct_ops
autocreate enabled. libbpf can enable autoload for programs referenced by
autocreated struct_ops maps, so an unrelated program can be loaded and fail
even when the current test does not use it.

Disable autocreate for all struct_ops maps by default, then re-enable it
only for the maps selected by the test before loading the skeleton.

Signed-off-by: Yiyang Chen <[email protected]>
---
 tools/testing/selftests/hid/hid_bpf.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

diff --git a/tools/testing/selftests/hid/hid_bpf.c 
b/tools/testing/selftests/hid/hid_bpf.c
index 1e979fb3542ba..269256e1decd8 100644
--- a/tools/testing/selftests/hid/hid_bpf.c
+++ b/tools/testing/selftests/hid/hid_bpf.c
@@ -86,6 +86,20 @@ static void load_programs(const struct test_program 
programs[],
        self->skel = hid__open();
        ASSERT_OK_PTR(self->skel) TEARDOWN_LOG("Error while calling hid__open");
 
+       /*
+        * Disable all struct_ops maps by default so libbpf does not autoload
+        * programs referenced by maps that are unrelated to the current test.
+        */
+       bpf_object__for_each_map(iter_map, *self->skel->skeleton->obj) {
+               if (bpf_map__type(iter_map) == BPF_MAP_TYPE_STRUCT_OPS) {
+                       err = bpf_map__set_autocreate(iter_map, false);
+                       ASSERT_OK(err) TH_LOG("can not disable struct_ops map 
'%s'",
+                                             bpf_map__name(iter_map));
+               }
+
+               bpf_map__set_autoattach(iter_map, false);
+       }
+
        for (int i = 0; i < progs_count; i++) {
                struct bpf_program *prog;
                struct bpf_map *map;
@@ -102,6 +116,10 @@ static void load_programs(const struct test_program 
programs[],
                ASSERT_OK_PTR(map) TH_LOG("can not find struct_ops by name 
'%s'",
                                          programs[i].name + 4);
 
+               err = bpf_map__set_autocreate(map, true);
+               ASSERT_OK(err) TH_LOG("can not enable struct_ops map '%s'",
+                                     programs[i].name + 4);
+
                /* hid_id is the first field of struct hid_bpf_ops */
                ops_hid_id = bpf_map__initial_value(map, NULL);
                ASSERT_OK_PTR(ops_hid_id) TH_LOG("unable to retrieve struct_ops 
data");
@@ -109,13 +127,6 @@ static void load_programs(const struct test_program 
programs[],
                *ops_hid_id = self->hid.hid_id;
        }
 
-       /* we disable the auto-attach feature of all maps because we
-        * only want the tested one to be manually attached in the next
-        * call to bpf_map__attach_struct_ops()
-        */
-       bpf_object__for_each_map(iter_map, *self->skel->skeleton->obj)
-               bpf_map__set_autoattach(iter_map, false);
-
        err = hid__load(self->skel);
        ASSERT_OK(err) TH_LOG("hid_skel_load failed: %d", err);
 
-- 
2.34.1


Reply via email to