Commit 8db05261 authored by Eduard Zingerman's avatar Eduard Zingerman Committed by Andrii Nakryiko

libbpf: Honor autocreate flag for struct_ops maps

Skip load steps for struct_ops maps not marked for automatic creation.
This should allow to load bpf object in situations like below:

    SEC("struct_ops/foo") int BPF_PROG(foo) { ... }
    SEC("struct_ops/bar") int BPF_PROG(bar) { ... }

    struct test_ops___v1 {
    	int (*foo)(void);
    };

    struct test_ops___v2 {
    	int (*foo)(void);
    	int (*does_not_exist)(void);
    };

    SEC(".struct_ops.link")
    struct test_ops___v1 map_for_old = {
    	.test_1 = (void *)foo
    };

    SEC(".struct_ops.link")
    struct test_ops___v2 map_for_new = {
    	.test_1 = (void *)foo,
    	.does_not_exist = (void *)bar
    };

Suppose program is loaded on old kernel that does not have definition
for 'does_not_exist' struct_ops member. After this commit it would be
possible to load such object file after the following tweaks:

    bpf_program__set_autoload(skel->progs.bar, false);
    bpf_map__set_autocreate(skel->maps.map_for_new, false);
Signed-off-by: default avatarEduard Zingerman <eddyz87@gmail.com>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Acked-by: default avatarDavid Vernet <void@manifault.com>
Link: https://lore.kernel.org/bpf/20240306104529.6453-4-eddyz87@gmail.com
parent d9ab2f76
...@@ -1212,6 +1212,9 @@ static int bpf_object__init_kern_struct_ops_maps(struct bpf_object *obj) ...@@ -1212,6 +1212,9 @@ static int bpf_object__init_kern_struct_ops_maps(struct bpf_object *obj)
if (!bpf_map__is_struct_ops(map)) if (!bpf_map__is_struct_ops(map))
continue; continue;
if (!map->autocreate)
continue;
err = bpf_map__init_kern_struct_ops(map); err = bpf_map__init_kern_struct_ops(map);
if (err) if (err)
return err; return err;
...@@ -8133,11 +8136,20 @@ static void bpf_map_prepare_vdata(const struct bpf_map *map) ...@@ -8133,11 +8136,20 @@ static void bpf_map_prepare_vdata(const struct bpf_map *map)
static int bpf_object_prepare_struct_ops(struct bpf_object *obj) static int bpf_object_prepare_struct_ops(struct bpf_object *obj)
{ {
struct bpf_map *map;
int i; int i;
for (i = 0; i < obj->nr_maps; i++) for (i = 0; i < obj->nr_maps; i++) {
if (bpf_map__is_struct_ops(&obj->maps[i])) map = &obj->maps[i];
bpf_map_prepare_vdata(&obj->maps[i]);
if (!bpf_map__is_struct_ops(map))
continue;
if (!map->autocreate)
continue;
bpf_map_prepare_vdata(map);
}
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment