diff options
| author | Donglin Peng <pengdonglin@xiaomi.com> | 2026-01-09 20:59:57 +0800 |
|---|---|---|
| committer | Andrii Nakryiko <andrii@kernel.org> | 2026-01-13 16:16:59 -0800 |
| commit | 33ecca574f1c27cbf560aee9c1b3045dcb9f8de5 (patch) | |
| tree | fe9f4e9338d84135367cbbb982070faee466c2f0 /tools/lib | |
| parent | d836e5e64992363b5fa9b121f1ab4a1a1b89162d (diff) | |
libbpf: Verify BTF sorting
This patch checks whether the BTF is sorted by name in ascending
order. If sorted, binary search will be used when looking up types.
Signed-off-by: Donglin Peng <pengdonglin@xiaomi.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/bpf/20260109130003.3313716-6-dolinux.peng@gmail.com
Diffstat (limited to 'tools/lib')
| -rw-r--r-- | tools/lib/bpf/btf.c | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c index 5a6ac40439e4..808e53961ed6 100644 --- a/tools/lib/bpf/btf.c +++ b/tools/lib/bpf/btf.c @@ -899,6 +899,30 @@ int btf__resolve_type(const struct btf *btf, __u32 type_id) return type_id; } +static void btf_check_sorted(struct btf *btf) +{ + __u32 i, n, named_start_id = 0; + + n = btf__type_cnt(btf); + for (i = btf->start_id + 1; i < n; i++) { + struct btf_type *ta = btf_type_by_id(btf, i - 1); + struct btf_type *tb = btf_type_by_id(btf, i); + const char *na = btf__str_by_offset(btf, ta->name_off); + const char *nb = btf__str_by_offset(btf, tb->name_off); + + if (strcmp(na, nb) > 0) + return; + + if (named_start_id == 0 && na[0] != '\0') + named_start_id = i - 1; + if (named_start_id == 0 && nb[0] != '\0') + named_start_id = i; + } + + if (named_start_id) + btf->named_start_id = named_start_id; +} + static __s32 btf_find_type_by_name_bsearch(const struct btf *btf, const char *name, __s32 start_id) { @@ -1130,6 +1154,7 @@ static struct btf *btf_new(const void *data, __u32 size, struct btf *base_btf, b err = err ?: btf_sanity_check(btf); if (err) goto done; + btf_check_sorted(btf); done: if (err) { |
