[RFC PATCH v2 5/5] btf: add CONFIG_BPF_SORT_BTF_BY_KIND_NAME

Donglin Peng posted 5 patches 3 months, 3 weeks ago
There is a newer version of this series
[RFC PATCH v2 5/5] btf: add CONFIG_BPF_SORT_BTF_BY_KIND_NAME
Posted by Donglin Peng 3 months, 3 weeks ago
Pahole v1.32 and later supports BTF sorting. Add a new configuration
option to control whether to enable this feature for vmlinux and
kernel modules.

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: Song Liu <song@kernel.org>
Signed-off-by: pengdonglin <pengdonglin@xiaomi.com>
Signed-off-by: Donglin Peng <dolinux.peng@gmail.com>
---
 kernel/bpf/Kconfig   | 8 ++++++++
 scripts/Makefile.btf | 5 +++++
 2 files changed, 13 insertions(+)

diff --git a/kernel/bpf/Kconfig b/kernel/bpf/Kconfig
index eb3de35734f0..08251a250f06 100644
--- a/kernel/bpf/Kconfig
+++ b/kernel/bpf/Kconfig
@@ -101,4 +101,12 @@ config BPF_LSM
 
 	  If you are unsure how to answer this question, answer N.
 
+config BPF_SORT_BTF_BY_KIND_NAME
+	bool "Sort BTF types by kind and name"
+	depends on BPF_SYSCALL
+	help
+	  This option sorts BTF types in vmlinux and kernel modules by their
+	  kind and name, enabling binary search for btf_find_by_name_kind()
+	  and significantly improving its lookup performance.
+
 endmenu # "BPF subsystem"
diff --git a/scripts/Makefile.btf b/scripts/Makefile.btf
index db76335dd917..3f1a0b3c3f3f 100644
--- a/scripts/Makefile.btf
+++ b/scripts/Makefile.btf
@@ -29,6 +29,11 @@ ifneq ($(KBUILD_EXTMOD),)
 module-pahole-flags-$(call test-ge, $(pahole-ver), 128) += --btf_features=distilled_base
 endif
 
+ifeq ($(call test-ge, $(pahole-ver), 132),y)
+pahole-flags-$(CONFIG_BPF_SORT_BTF_BY_KIND_NAME) 	+= --btf_features=sort
+module-pahole-flags-$(CONFIG_BPF_SORT_BTF_BY_KIND_NAME) += --btf_features=sort
+endif
+
 endif
 
 pahole-flags-$(CONFIG_PAHOLE_HAS_LANG_EXCLUDE)		+= --lang_exclude=rust
-- 
2.34.1
Re: [RFC PATCH v2 5/5] btf: add CONFIG_BPF_SORT_BTF_BY_KIND_NAME
Posted by Alan Maguire 3 months, 2 weeks ago
On 20/10/2025 10:39, Donglin Peng wrote:
> Pahole v1.32 and later supports BTF sorting. Add a new configuration
> option to control whether to enable this feature for vmlinux and
> kernel modules.
> 
> 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: Song Liu <song@kernel.org>
> Signed-off-by: pengdonglin <pengdonglin@xiaomi.com>
> Signed-off-by: Donglin Peng <dolinux.peng@gmail.com>
> ---
>  kernel/bpf/Kconfig   | 8 ++++++++
>  scripts/Makefile.btf | 5 +++++
>  2 files changed, 13 insertions(+)
> 
> diff --git a/kernel/bpf/Kconfig b/kernel/bpf/Kconfig
> index eb3de35734f0..08251a250f06 100644
> --- a/kernel/bpf/Kconfig
> +++ b/kernel/bpf/Kconfig
> @@ -101,4 +101,12 @@ config BPF_LSM
>  
>  	  If you are unsure how to answer this question, answer N.
>  
> +config BPF_SORT_BTF_BY_KIND_NAME
> +	bool "Sort BTF types by kind and name"
> +	depends on BPF_SYSCALL
> +	help
> +	  This option sorts BTF types in vmlinux and kernel modules by their
> +	  kind and name, enabling binary search for btf_find_by_name_kind()
> +	  and significantly improving its lookup performance.
> +
>  endmenu # "BPF subsystem"
> diff --git a/scripts/Makefile.btf b/scripts/Makefile.btf
> index db76335dd917..3f1a0b3c3f3f 100644
> --- a/scripts/Makefile.btf
> +++ b/scripts/Makefile.btf
> @@ -29,6 +29,11 @@ ifneq ($(KBUILD_EXTMOD),)
>  module-pahole-flags-$(call test-ge, $(pahole-ver), 128) += --btf_features=distilled_base
>  endif
>  
> +ifeq ($(call test-ge, $(pahole-ver), 132),y)
> +pahole-flags-$(CONFIG_BPF_SORT_BTF_BY_KIND_NAME) 	+= --btf_features=sort
> +module-pahole-flags-$(CONFIG_BPF_SORT_BTF_BY_KIND_NAME) += --btf_features=sort
> +endif
> +

perhaps it's useful informationally, but you don't need to wrap the
addition of the sort flag in a pahole version check; unsupported
btf_features are just ignored. Also we're at v1.30 in pahole now (we'll
be releasing 1.31 shortly hopefully), so any version check should be
v1.30/v1.31. I'd say just leave out the version check though.

Alan
Re: [RFC PATCH v2 5/5] btf: add CONFIG_BPF_SORT_BTF_BY_KIND_NAME
Posted by Donglin Peng 3 months, 2 weeks ago
On Wed, Oct 22, 2025 at 1:28 AM Alan Maguire <alan.maguire@oracle.com> wrote:
>
> On 20/10/2025 10:39, Donglin Peng wrote:
> > Pahole v1.32 and later supports BTF sorting. Add a new configuration
> > option to control whether to enable this feature for vmlinux and
> > kernel modules.
> >
> > 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: Song Liu <song@kernel.org>
> > Signed-off-by: pengdonglin <pengdonglin@xiaomi.com>
> > Signed-off-by: Donglin Peng <dolinux.peng@gmail.com>
> > ---
> >  kernel/bpf/Kconfig   | 8 ++++++++
> >  scripts/Makefile.btf | 5 +++++
> >  2 files changed, 13 insertions(+)
> >
> > diff --git a/kernel/bpf/Kconfig b/kernel/bpf/Kconfig
> > index eb3de35734f0..08251a250f06 100644
> > --- a/kernel/bpf/Kconfig
> > +++ b/kernel/bpf/Kconfig
> > @@ -101,4 +101,12 @@ config BPF_LSM
> >
> >         If you are unsure how to answer this question, answer N.
> >
> > +config BPF_SORT_BTF_BY_KIND_NAME
> > +     bool "Sort BTF types by kind and name"
> > +     depends on BPF_SYSCALL
> > +     help
> > +       This option sorts BTF types in vmlinux and kernel modules by their
> > +       kind and name, enabling binary search for btf_find_by_name_kind()
> > +       and significantly improving its lookup performance.
> > +
> >  endmenu # "BPF subsystem"
> > diff --git a/scripts/Makefile.btf b/scripts/Makefile.btf
> > index db76335dd917..3f1a0b3c3f3f 100644
> > --- a/scripts/Makefile.btf
> > +++ b/scripts/Makefile.btf
> > @@ -29,6 +29,11 @@ ifneq ($(KBUILD_EXTMOD),)
> >  module-pahole-flags-$(call test-ge, $(pahole-ver), 128) += --btf_features=distilled_base
> >  endif
> >
> > +ifeq ($(call test-ge, $(pahole-ver), 132),y)
> > +pahole-flags-$(CONFIG_BPF_SORT_BTF_BY_KIND_NAME)     += --btf_features=sort
> > +module-pahole-flags-$(CONFIG_BPF_SORT_BTF_BY_KIND_NAME) += --btf_features=sort
> > +endif
> > +
>
> perhaps it's useful informationally, but you don't need to wrap the
> addition of the sort flag in a pahole version check; unsupported
> btf_features are just ignored. Also we're at v1.30 in pahole now (we'll
> be releasing 1.31 shortly hopefully), so any version check should be
> v1.30/v1.31. I'd say just leave out the version check though.

Understood, thanks. Will do.

>
> Alan
Re: [RFC PATCH v2 5/5] btf: add CONFIG_BPF_SORT_BTF_BY_KIND_NAME
Posted by Eduard Zingerman 3 months, 2 weeks ago
On Mon, 2025-10-20 at 17:39 +0800, Donglin Peng wrote:

[...]

> diff --git a/kernel/bpf/Kconfig b/kernel/bpf/Kconfig
> index eb3de35734f0..08251a250f06 100644
> --- a/kernel/bpf/Kconfig
> +++ b/kernel/bpf/Kconfig
> @@ -101,4 +101,12 @@ config BPF_LSM
>  
>  	  If you are unsure how to answer this question, answer N.
>  
> +config BPF_SORT_BTF_BY_KIND_NAME
> +	bool "Sort BTF types by kind and name"
> +	depends on BPF_SYSCALL
> +	help
> +	  This option sorts BTF types in vmlinux and kernel modules by their
> +	  kind and name, enabling binary search for btf_find_by_name_kind()
> +	  and significantly improving its lookup performance.
> +

Why having this as an option?
There are no downsides to always enabling, right?
The cost of sorting btf at build time should be negligible.

[...]
Re: [RFC PATCH v2 5/5] btf: add CONFIG_BPF_SORT_BTF_BY_KIND_NAME
Posted by Donglin Peng 3 months, 2 weeks ago
On Tue, Oct 21, 2025 at 8:50 AM Eduard Zingerman <eddyz87@gmail.com> wrote:
>
> On Mon, 2025-10-20 at 17:39 +0800, Donglin Peng wrote:
>
> [...]
>
> > diff --git a/kernel/bpf/Kconfig b/kernel/bpf/Kconfig
> > index eb3de35734f0..08251a250f06 100644
> > --- a/kernel/bpf/Kconfig
> > +++ b/kernel/bpf/Kconfig
> > @@ -101,4 +101,12 @@ config BPF_LSM
> >
> >         If you are unsure how to answer this question, answer N.
> >
> > +config BPF_SORT_BTF_BY_KIND_NAME
> > +     bool "Sort BTF types by kind and name"
> > +     depends on BPF_SYSCALL
> > +     help
> > +       This option sorts BTF types in vmlinux and kernel modules by their
> > +       kind and name, enabling binary search for btf_find_by_name_kind()
> > +       and significantly improving its lookup performance.
> > +
>
> Why having this as an option?
> There are no downsides to always enabling, right?
> The cost of sorting btf at build time should be negligible.

Thanks, I'll remove this config option in the next version as suggested.

>
> [...]