[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20251218113051.455293-12-dolinux.peng@gmail.com>
Date: Thu, 18 Dec 2025 19:30:49 +0800
From: Donglin Peng <dolinux.peng@...il.com>
To: ast@...nel.org,
andrii.nakryiko@...il.com,
eddyz87@...il.com
Cc: zhangxiaoqin@...omi.com,
ihor.solodrai@...ux.dev,
linux-kernel@...r.kernel.org,
bpf@...r.kernel.org,
pengdonglin <pengdonglin@...omi.com>,
Alan Maguire <alan.maguire@...cle.com>
Subject: [PATCH bpf-next v10 11/13] libbpf: Add btf_is_sorted and btf_sorted_start_id helpers to refactor the code
From: pengdonglin <pengdonglin@...omi.com>
Introduce two new helper functions to clarify the code and no
functional changes are introduced.
Cc: Eduard Zingerman <eddyz87@...il.com>
Cc: Alexei Starovoitov <ast@...nel.org>
Cc: Andrii Nakryiko <andrii.nakryiko@...il.com>
Cc: Alan Maguire <alan.maguire@...cle.com>
Cc: Ihor Solodrai <ihor.solodrai@...ux.dev>
Cc: Xiaoqin Zhang <zhangxiaoqin@...omi.com>
Signed-off-by: pengdonglin <pengdonglin@...omi.com>
---
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
Powered by blists - more mailing lists