[PATCH bpf-next v10 11/13] libbpf: Add btf_is_sorted and btf_sorted_start_id helpers to refactor the code

Donglin Peng posted 13 patches 1 day ago
[PATCH bpf-next v10 11/13] libbpf: Add btf_is_sorted and btf_sorted_start_id helpers to refactor the code
Posted by Donglin Peng 1 day ago
From: pengdonglin <pengdonglin@xiaomi.com>

Introduce two new helper functions to clarify the code and no
functional changes are introduced.

Cc: Eduard Zingerman <eddyz87@gmail.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Alan Maguire <alan.maguire@oracle.com>
Cc: Ihor Solodrai <ihor.solodrai@linux.dev>
Cc: Xiaoqin Zhang <zhangxiaoqin@xiaomi.com>
Signed-off-by: pengdonglin <pengdonglin@xiaomi.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
Re: [PATCH bpf-next v10 11/13] libbpf: Add btf_is_sorted and btf_sorted_start_id helpers to refactor the code
Posted by Andrii Nakryiko 11 hours ago
On Thu, Dec 18, 2025 at 3:31 AM Donglin Peng <dolinux.peng@gmail.com> wrote:
>
> From: pengdonglin <pengdonglin@xiaomi.com>
>
> Introduce two new helper functions to clarify the code and no
> functional changes are introduced.
>
> Cc: Eduard Zingerman <eddyz87@gmail.com>
> Cc: Alexei Starovoitov <ast@kernel.org>
> Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com>
> Cc: Alan Maguire <alan.maguire@oracle.com>
> Cc: Ihor Solodrai <ihor.solodrai@linux.dev>
> Cc: Xiaoqin Zhang <zhangxiaoqin@xiaomi.com>
> Signed-off-by: pengdonglin <pengdonglin@xiaomi.com>
> ---
>  tools/lib/bpf/btf.c             | 14 ++++++++++++--
>  tools/lib/bpf/libbpf_internal.h |  2 ++
>  2 files changed, 14 insertions(+), 2 deletions(-)
>

It just adds more functions to jump to and check what it's doing. I
don't think this adds much value, just drop this patch


> 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
>
Re: [PATCH bpf-next v10 11/13] libbpf: Add btf_is_sorted and btf_sorted_start_id helpers to refactor the code
Posted by Donglin Peng 6 hours ago
On Fri, Dec 19, 2025 at 8:05 AM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Thu, Dec 18, 2025 at 3:31 AM Donglin Peng <dolinux.peng@gmail.com> wrote:
> >
> > From: pengdonglin <pengdonglin@xiaomi.com>
> >
> > Introduce two new helper functions to clarify the code and no
> > functional changes are introduced.
> >
> > Cc: Eduard Zingerman <eddyz87@gmail.com>
> > Cc: Alexei Starovoitov <ast@kernel.org>
> > Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com>
> > Cc: Alan Maguire <alan.maguire@oracle.com>
> > Cc: Ihor Solodrai <ihor.solodrai@linux.dev>
> > Cc: Xiaoqin Zhang <zhangxiaoqin@xiaomi.com>
> > Signed-off-by: pengdonglin <pengdonglin@xiaomi.com>
> > ---
> >  tools/lib/bpf/btf.c             | 14 ++++++++++++--
> >  tools/lib/bpf/libbpf_internal.h |  2 ++
> >  2 files changed, 14 insertions(+), 2 deletions(-)
> >
>
> It just adds more functions to jump to and check what it's doing. I
> don't think this adds much value, just drop this patch

Could adding the __always_inline be acceptable?

>
>
> > 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
> >
Re: [PATCH bpf-next v10 11/13] libbpf: Add btf_is_sorted and btf_sorted_start_id helpers to refactor the code
Posted by Eduard Zingerman 13 hours ago
On Thu, 2025-12-18 at 19:30 +0800, Donglin Peng wrote:
> From: pengdonglin <pengdonglin@xiaomi.com>
> 
> Introduce two new helper functions to clarify the code and no
> functional changes are introduced.
> 
> Cc: Eduard Zingerman <eddyz87@gmail.com>
> Cc: Alexei Starovoitov <ast@kernel.org>
> Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com>
> Cc: Alan Maguire <alan.maguire@oracle.com>
> Cc: Ihor Solodrai <ihor.solodrai@linux.dev>
> Cc: Xiaoqin Zhang <zhangxiaoqin@xiaomi.com>
> Signed-off-by: pengdonglin <pengdonglin@xiaomi.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;
> +}

Having this function declared differently in kernel and in libbpf is a
bit confusing. Is it needed in libbpf at all?

> +bool btf_is_sorted(const struct btf *btf)
> +{
> +	return btf->sorted_start_id > 0;
> +}
> +

Please squash this with the first btf_find_by_name_kind() change.

>  /* 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)
>  {
Re: [PATCH bpf-next v10 11/13] libbpf: Add btf_is_sorted and btf_sorted_start_id helpers to refactor the code
Posted by Donglin Peng 6 hours ago
On Fri, Dec 19, 2025 at 6:33 AM Eduard Zingerman <eddyz87@gmail.com> wrote:
>
> On Thu, 2025-12-18 at 19:30 +0800, Donglin Peng wrote:
> > From: pengdonglin <pengdonglin@xiaomi.com>
> >
> > Introduce two new helper functions to clarify the code and no
> > functional changes are introduced.
> >
> > Cc: Eduard Zingerman <eddyz87@gmail.com>
> > Cc: Alexei Starovoitov <ast@kernel.org>
> > Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com>
> > Cc: Alan Maguire <alan.maguire@oracle.com>
> > Cc: Ihor Solodrai <ihor.solodrai@linux.dev>
> > Cc: Xiaoqin Zhang <zhangxiaoqin@xiaomi.com>
> > Signed-off-by: pengdonglin <pengdonglin@xiaomi.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;
> > +}
>
> Having this function declared differently in kernel and in libbpf is a
> bit confusing. Is it needed in libbpf at all?

Thanks. Keeping it consistent between libbpf and kernel side.
Will remove in the next version.

>
> > +bool btf_is_sorted(const struct btf *btf)
> > +{
> > +     return btf->sorted_start_id > 0;
> > +}
> > +
>
> Please squash this with the first btf_find_by_name_kind() change.

Yes.

>
> >  /* 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)
> >  {