Re: [RFC PATCH v3 3/3] btf: Reuse libbpf code for BTF type sorting verification and binary search

From: Donglin Peng

Date: Sun Nov 02 2025 - 20:21:05 EST


On Sat, Nov 1, 2025 at 12:51 AM Andrii Nakryiko
<andrii.nakryiko@xxxxxxxxx> wrote:
>
> On Tue, Oct 28, 2025 at 7:03 PM Donglin Peng <dolinux.peng@xxxxxxxxx> wrote:
> >
> > On Wed, Oct 29, 2025 at 2:40 AM Andrii Nakryiko
> > <andrii.nakryiko@xxxxxxxxx> wrote:
> > >
> > > On Mon, Oct 27, 2025 at 6:54 AM Donglin Peng <dolinux.peng@xxxxxxxxx> wrote:
> > > >
> > > > The previous commit implemented BTF sorting verification and binary
> > > > search algorithm in libbpf. This patch enables this functionality in
> > > > the kernel.
> > > >
> > > > Cc: Eduard Zingerman <eddyz87@xxxxxxxxx>
> > > > Cc: Alexei Starovoitov <ast@xxxxxxxxxx>
> > > > Cc: Andrii Nakryiko <andrii.nakryiko@xxxxxxxxx>
> > > > Cc: Alan Maguire <alan.maguire@xxxxxxxxxx>
> > > > Cc: Song Liu <song@xxxxxxxxxx>
> > > > Signed-off-by: pengdonglin <pengdonglin@xxxxxxxxxx>
> > > > Signed-off-by: Donglin Peng <dolinux.peng@xxxxxxxxx>
> > > > ---
> > > > v2->v3:
> > > > - Include btf_sort.c directly in btf.c to reduce function call overhead
> > > > ---
> > > > kernel/bpf/btf.c | 34 ++++++++++++++++++----------------
> > > > 1 file changed, 18 insertions(+), 16 deletions(-)
> > > >
> > > > diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> > > > index 0de8fc8a0e0b..df258815a6ca 100644
> > > > --- a/kernel/bpf/btf.c
> > > > +++ b/kernel/bpf/btf.c
> > > > @@ -33,6 +33,7 @@
> > > > #include <net/sock.h>
> > > > #include <net/xdp.h>
> > > > #include "../tools/lib/bpf/relo_core.h"
> > > > +#include "../tools/lib/bpf/btf_sort.h"
> > >
> > > I don't believe in code reuse for the sake of code reuse. This code
> > > sharing just makes everything more entangled and complicated.
> > > Reimplementing binary search is totally fine, IMO.
> >
> > Thanks. Would you be open to the approach from v2, where we place
> > the common code in btf_sort.c and compile it separately rather than
> > including it directly?
> >
>
> No, the code is too trivial to try to reuse it. Kernel and user space
> libbpf code are run with different constraints, sharing it is
> necessary evil (like for BPF CO-RE relocations), not something that
> should be encouraged.

Thanks, I will implement it separately as recommended.

>
> > >
> > > [...]