[PATCH bpf-next v10 12/13] btf: Add btf_is_sorted to refactor the code

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

Introduce a new helper function 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>
---
 include/linux/btf.h | 1 +
 kernel/bpf/btf.c    | 9 +++++++--
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/include/linux/btf.h b/include/linux/btf.h
index 2d28f2b22ae5..947ed2abf632 100644
--- a/include/linux/btf.h
+++ b/include/linux/btf.h
@@ -221,6 +221,7 @@ bool btf_is_vmlinux(const struct btf *btf);
 struct module *btf_try_get_module(const struct btf *btf);
 u32 btf_nr_types(const struct btf *btf);
 u32 btf_sorted_start_id(const struct btf *btf);
+bool btf_is_sorted(const struct btf *btf);
 struct btf *btf_base_btf(const struct btf *btf);
 bool btf_type_is_i32(const struct btf_type *t);
 bool btf_type_is_i64(const struct btf_type *t);
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 3aeb4f00cbfe..0f20887a6f02 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -555,6 +555,11 @@ u32 btf_sorted_start_id(const struct btf *btf)
 	return btf->sorted_start_id ?: (btf->start_id ?: 1);
 }
 
+bool btf_is_sorted(const struct btf *btf)
+{
+	return btf->sorted_start_id > 0;
+}
+
 /*
  * Assuming that types are sorted by name in ascending order.
  */
@@ -649,9 +654,9 @@ s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind)
 			return idx;
 	}
 
-	if (btf->sorted_start_id > 0 && name[0]) {
+	if (btf_is_sorted(btf) && name[0]) {
 		/* skip anonymous types */
-		s32 start_id = btf->sorted_start_id;
+		s32 start_id = btf_sorted_start_id(btf);
 		s32 end_id = btf_nr_types(btf) - 1;
 
 		idx = btf_find_by_name_bsearch(btf, name, start_id, end_id);
-- 
2.34.1
Re: [PATCH bpf-next v10 12/13] btf: Add btf_is_sorted 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 a new helper function 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>
> ---
>  include/linux/btf.h | 1 +
>  kernel/bpf/btf.c    | 9 +++++++--
>  2 files changed, 8 insertions(+), 2 deletions(-)
>

let's drop, this is not necessary


> diff --git a/include/linux/btf.h b/include/linux/btf.h
> index 2d28f2b22ae5..947ed2abf632 100644
> --- a/include/linux/btf.h
> +++ b/include/linux/btf.h
> @@ -221,6 +221,7 @@ bool btf_is_vmlinux(const struct btf *btf);
>  struct module *btf_try_get_module(const struct btf *btf);
>  u32 btf_nr_types(const struct btf *btf);
>  u32 btf_sorted_start_id(const struct btf *btf);
> +bool btf_is_sorted(const struct btf *btf);
>  struct btf *btf_base_btf(const struct btf *btf);
>  bool btf_type_is_i32(const struct btf_type *t);
>  bool btf_type_is_i64(const struct btf_type *t);
> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> index 3aeb4f00cbfe..0f20887a6f02 100644
> --- a/kernel/bpf/btf.c
> +++ b/kernel/bpf/btf.c
> @@ -555,6 +555,11 @@ u32 btf_sorted_start_id(const struct btf *btf)
>         return btf->sorted_start_id ?: (btf->start_id ?: 1);
>  }
>
> +bool btf_is_sorted(const struct btf *btf)
> +{
> +       return btf->sorted_start_id > 0;
> +}
> +
>  /*
>   * Assuming that types are sorted by name in ascending order.
>   */
> @@ -649,9 +654,9 @@ s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind)
>                         return idx;
>         }
>
> -       if (btf->sorted_start_id > 0 && name[0]) {
> +       if (btf_is_sorted(btf) && name[0]) {
>                 /* skip anonymous types */
> -               s32 start_id = btf->sorted_start_id;
> +               s32 start_id = btf_sorted_start_id(btf);
>                 s32 end_id = btf_nr_types(btf) - 1;
>
>                 idx = btf_find_by_name_bsearch(btf, name, start_id, end_id);
> --
> 2.34.1
>
Re: [PATCH bpf-next v10 12/13] btf: Add btf_is_sorted 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 a new helper function 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>
> ---

Please squash this patch, there is no need to keep this change separate.

[...]