[PATCH bpf-next v10 11/13] libbpf: Add btf_is_sorted and btf_sorted_start_id helpers to refactor the code
From: Donglin Peng
Date: Thu Dec 18 2025 - 06:32:43 EST
From: pengdonglin <pengdonglin@xxxxxxxxxx>
Introduce two new helper functions to clarify the code and no
functional changes are introduced.
Cc: Eduard Zingerman <eddyz87@xxxxxxxxx>
Cc: Alexei Starovoitov <ast@xxxxxxxxxx>
Cc: Andrii Nakryiko <andrii.nakryiko@xxxxxxxxx>
Cc: Alan Maguire <alan.maguire@xxxxxxxxxx>
Cc: Ihor Solodrai <ihor.solodrai@xxxxxxxxx>
Cc: Xiaoqin Zhang <zhangxiaoqin@xxxxxxxxxx>
Signed-off-by: pengdonglin <pengdonglin@xxxxxxxxxx>
---
tools/lib/bpf/btf.c | 14 ++++++++++++--
tools/lib/bpf/libbpf_internal.h | 2 ++
2 files changed, 14 insertions(+), 2 deletions(-)
diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index b5b0898d033d..571b72bd90b5 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -626,6 +626,16 @@ const struct btf *btf__base_btf(const struct btf *btf)
return btf->base_btf;
}
+int btf_sorted_start_id(const struct btf *btf)
+{
+ return btf->sorted_start_id;
+}
+
+bool btf_is_sorted(const struct btf *btf)
+{
+ return btf->sorted_start_id > 0;
+}
+
/* internal helper returning non-const pointer to a type */
struct btf_type *btf_type_by_id(const struct btf *btf, __u32 type_id)
{
@@ -976,11 +986,11 @@ static __s32 btf_find_by_name_kind(const struct btf *btf, int start_id,
if (kind == BTF_KIND_UNKN || strcmp(type_name, "void") == 0)
return 0;
- if (btf->sorted_start_id > 0 && type_name[0]) {
+ if (btf_is_sorted(btf) && type_name[0]) {
__s32 end_id = btf__type_cnt(btf) - 1;
/* skip anonymous types */
- start_id = max(start_id, btf->sorted_start_id);
+ start_id = max(start_id, btf_sorted_start_id(btf));
idx = btf_find_by_name_bsearch(btf, type_name, start_id, end_id);
if (unlikely(idx < 0))
return libbpf_err(-ENOENT);
diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h
index fc59b21b51b5..95e6848396b4 100644
--- a/tools/lib/bpf/libbpf_internal.h
+++ b/tools/lib/bpf/libbpf_internal.h
@@ -250,6 +250,8 @@ const struct btf_type *skip_mods_and_typedefs(const struct btf *btf, __u32 id, _
const struct btf_header *btf_header(const struct btf *btf);
void btf_set_base_btf(struct btf *btf, const struct btf *base_btf);
int btf_relocate(struct btf *btf, const struct btf *base_btf, __u32 **id_map);
+int btf_sorted_start_id(const struct btf *btf);
+bool btf_is_sorted(const struct btf *btf);
static inline enum btf_func_linkage btf_func_linkage(const struct btf_type *t)
{
--
2.34.1