]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
selftests/hid: Load only requested struct_ops maps
authorYiyang Chen <chenyy23@mails.tsinghua.edu.cn>
Tue, 23 Jun 2026 06:23:14 +0000 (06:23 +0000)
committerBenjamin Tissoires <bentiss@kernel.org>
Wed, 1 Jul 2026 07:55:36 +0000 (09:55 +0200)
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 <chenyy23@mails.tsinghua.edu.cn>
Fixes: f64c1a459339 ("selftests/hid: disable struct_ops auto-attach")
Signed-off-by: Benjamin Tissoires <bentiss@kernel.org>
tools/testing/selftests/hid/hid_bpf.c

index 1e979fb3542bab79803ae2c772e048d93927df33..269256e1decd843d2f35c7524a413ac26548e2e0 100644 (file)
@@ -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);