Skip to content

Commit 492ab02

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
libbpf: Make bpf_map order and indices stable
Currently, libbpf re-sorts bpf_map structs after all the maps are added and initialized, which might change their relative order and invalidate any bpf_map pointer or index taken before that. This is inconvenient and error-prone. For instance, it can cause .kconfig map index to point to a wrong map. Furthermore, libbpf itself doesn't rely on any specific ordering of bpf_maps, so it's just an unnecessary complication right now. This patch drops sorting of maps and makes their relative positions fixed. If efficient index is ever needed, it's better to have a separate array of pointers as a search index, instead of reordering bpf_map struct in-place. This will be less error-prone and will allow multiple independent orderings, if necessary (e.g., either by section index or by name). Fixes: 166750b ("libbpf: Support libbpf-provided extern variables") Reported-by: Martin KaFai Lau <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent f5bfcd9 commit 492ab02

File tree

1 file changed

+0
-14
lines changed

1 file changed

+0
-14
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,16 +1123,6 @@ bpf_object__init_kversion(struct bpf_object *obj, void *data, size_t size)
11231123
return 0;
11241124
}
11251125

1126-
static int compare_bpf_map(const void *_a, const void *_b)
1127-
{
1128-
const struct bpf_map *a = _a;
1129-
const struct bpf_map *b = _b;
1130-
1131-
if (a->sec_idx != b->sec_idx)
1132-
return a->sec_idx - b->sec_idx;
1133-
return a->sec_offset - b->sec_offset;
1134-
}
1135-
11361126
static bool bpf_map_type__is_map_in_map(enum bpf_map_type type)
11371127
{
11381128
if (type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
@@ -2196,10 +2186,6 @@ static int bpf_object__init_maps(struct bpf_object *obj,
21962186
if (err)
21972187
return err;
21982188

2199-
if (obj->nr_maps) {
2200-
qsort(obj->maps, obj->nr_maps, sizeof(obj->maps[0]),
2201-
compare_bpf_map);
2202-
}
22032189
return 0;
22042190
}
22052191

0 commit comments

Comments
 (0)