Skip to content

Commit 262cfb7

Browse files
Delyan Kratunovanakryiko
authored andcommitted
libbpf: Init btf_{key,value}_type_id on internal map open
For internal and user maps, look up the key and value btf types on open() and not load(), so that `bpf_map_btf_value_type_id` is usable in `bpftool gen`. Signed-off-by: Delyan Kratunov <delyank@fb.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/78dbe4e457b4a05e098fc6c8f50014b680c86e4e.1647473511.git.delyank@fb.com
1 parent bc380eb commit 262cfb7

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1517,6 +1517,9 @@ static char *internal_map_name(struct bpf_object *obj, const char *real_name)
15171517
return strdup(map_name);
15181518
}
15191519

1520+
static int
1521+
bpf_map_find_btf_info(struct bpf_object *obj, struct bpf_map *map);
1522+
15201523
static int
15211524
bpf_object__init_internal_map(struct bpf_object *obj, enum libbpf_map_type type,
15221525
const char *real_name, int sec_idx, void *data, size_t data_sz)
@@ -1564,6 +1567,9 @@ bpf_object__init_internal_map(struct bpf_object *obj, enum libbpf_map_type type,
15641567
return err;
15651568
}
15661569

1570+
/* failures are fine because of maps like .rodata.str1.1 */
1571+
(void) bpf_map_find_btf_info(obj, map);
1572+
15671573
if (data)
15681574
memcpy(map->mmaped, data, data_sz);
15691575

@@ -2046,6 +2052,9 @@ static int bpf_object__init_user_maps(struct bpf_object *obj, bool strict)
20462052
}
20472053
memcpy(&map->def, def, sizeof(struct bpf_map_def));
20482054
}
2055+
2056+
/* btf info may not exist but fill it in if it does exist */
2057+
(void) bpf_map_find_btf_info(obj, map);
20492058
}
20502059
return 0;
20512060
}
@@ -2534,6 +2543,10 @@ static int bpf_object__init_user_btf_map(struct bpf_object *obj,
25342543
fill_map_from_def(map->inner_map, &inner_def);
25352544
}
25362545

2546+
err = bpf_map_find_btf_info(obj, map);
2547+
if (err)
2548+
return err;
2549+
25372550
return 0;
25382551
}
25392552

@@ -4873,7 +4886,7 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, b
48734886
if (bpf_map__is_struct_ops(map))
48744887
create_attr.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
48754888

4876-
if (obj->btf && btf__fd(obj->btf) >= 0 && !bpf_map_find_btf_info(obj, map)) {
4889+
if (obj->btf && btf__fd(obj->btf) >= 0) {
48774890
create_attr.btf_fd = btf__fd(obj->btf);
48784891
create_attr.btf_key_type_id = map->btf_key_type_id;
48794892
create_attr.btf_value_type_id = map->btf_value_type_id;

0 commit comments

Comments
 (0)