BPF List
 help / color / mirror / Atom feed
From: Eduard Zingerman <eddyz87@gmail.com>
To: Donglin Peng <dolinux.peng@gmail.com>
Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com>,
	Alexei Starovoitov	 <alexei.starovoitov@gmail.com>,
	Alan Maguire <alan.maguire@oracle.com>,
	 Alexei Starovoitov	 <ast@kernel.org>,
	LKML <linux-kernel@vger.kernel.org>, bpf <bpf@vger.kernel.org>,
	 Song Liu <song@kernel.org>, pengdonglin <pengdonglin@xiaomi.com>
Subject: Re: [RFC PATCH v2 2/5] btf: sort BTF types by kind and name to enable binary search
Date: Thu, 23 Oct 2025 20:15:21 -0700	[thread overview]
Message-ID: <f5cb8c37dc7a23beb0d83fe2aa0a4dc29bc40fd5.camel@gmail.com> (raw)
In-Reply-To: <CAErzpmtJmj-ZX+uL_N9e5-r1iL+kD=0vwM9BeDL3t4C2re261A@mail.gmail.com>

On Fri, 2025-10-24 at 11:04 +0800, Donglin Peng wrote:
> On Fri, Oct 24, 2025 at 10:32 AM Eduard Zingerman <eddyz87@gmail.com> wrote:
> > 
> > On Fri, 2025-10-24 at 10:23 +0800, Donglin Peng wrote:
> > > On Fri, Oct 24, 2025 at 9:59 AM Donglin Peng <dolinux.peng@gmail.com> wrote:
> > > > 
> > > > On Fri, Oct 24, 2025 at 3:40 AM Andrii Nakryiko
> > > > <andrii.nakryiko@gmail.com> wrote:
> > > > > 
> > > > > On Thu, Oct 23, 2025 at 11:37 AM Alexei Starovoitov
> > > > > <alexei.starovoitov@gmail.com> wrote:
> > > > > > 
> > > > > > On Thu, Oct 23, 2025 at 9:28 AM Andrii Nakryiko
> > > > > > <andrii.nakryiko@gmail.com> wrote:
> > > > > > > 
> > > > > > > 
> > > > > > > Speaking of flags, though. I think adding BTF_F_SORTED flag to
> > > > > > > btf_header->flags seems useful, as that would allow libbpf (and user
> > > > > > > space apps working with BTF in general) to use more optimal
> > > > > > > find_by_name implementation. The only gotcha is that old kernels
> > > > > > > enforce this btf_header->flags to be zero, so pahole would need to
> > > > > > > know not to emit this when building BTF for old kernels (or, rather,
> > > > > > > we'll just teach pahole_flags in kernel build scripts to add this
> > > > > > > going forward). This is not very important for kernel, because kernel
> > > > > > > has to validate all this anyways, but would allow saving time for user
> > > > > > > space.
> > > > > > 
> > > > > > Thinking more about it... I don't think it's worth it.
> > > > > > It's an operational headache. I'd rather have newer pahole sort it
> > > > > > without on/off flags and detection, so that people can upgrade
> > > > > > pahole and build older kernels.
> > > > > > Also BTF_F_SORTED doesn't spell out the way it's sorted.
> > > > > > Things may change and we will need a new flag and so on.
> > > > > > I think it's easier to check in the kernel and libbpf whether
> > > > > > BTF is sorted the way they want it.
> > > > > > The check is simple, fast and done once. Then both (kernel and libbpf) can
> > > > > > set an internal flag and use different functions to search
> > > > > > within a given BTF.
> > > > > 
> > > > > I guess that's fine. libbpf can do this check lazily on the first
> > > > > btf__find_by_name() to avoid unnecessary overhead. Agreed.
> > > > 
> > > > Thank you for all the feedback. Based on the suggestions above, the sorting
> > > > implementation will be redesigned in the next version as follows:
> > > > 
> > > > 1. The sorting operation will be fully handled by pahole, with no dependency on
> > > > libbpf. This means users can benefit from sorting simply by upgrading their
> > > > pahole version.
> > > 
> > > I suggest that libbpf provides a sorting function, such as the
> > > btf__permute suggested
> > > by Andrii, for pahole to call. This approach allows pahole to leverage
> > > libbpf's existing
> > > helper functions and avoids code duplication.
> > 
> > Could you please enumerate the functions you'd have to reimplement in
> > pahole?
> 
> Yes. Once the BTF types are sorted, the type IDs in both the BTF and BTF ext
> sections must be remapped. Libbpf provides helper functions like
> btf_field_iter_init,
> btf_field_iter_next,
> btf_ext_visit_type_ids
> to iterate through the btf_field and btf_ext_info_sec entries that
> require updating.
> We will likely need to reimplement these three functions for this purpose.

I think Andrii's suggestion is to have btf__permute in libbpf,
as it needs all the functions you mention.
But actual sorting can happen in pahole, then:
- allocate array of length num-types, initialize it 0..num-types;
- reorder it as one sees fit;
- call btf__permute() from libbpf and get all the renamings handled by it.

[...]

  reply	other threads:[~2025-10-24  3:15 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-20  9:39 [RFC PATCH v2 0/5] Significantly Improve BTF Type Lookup Performance Donglin Peng
2025-10-20  9:39 ` [RFC PATCH v2 1/5] btf: search local BTF before base BTF Donglin Peng
2025-10-21  1:06   ` Eduard Zingerman
2025-10-21  8:31     ` Donglin Peng
2025-10-21 15:56       ` Eduard Zingerman
2025-10-22  3:08         ` Donglin Peng
2025-10-20  9:39 ` [RFC PATCH v2 2/5] btf: sort BTF types by kind and name to enable binary search Donglin Peng
2025-10-21 17:24   ` Alan Maguire
2025-10-22  4:47     ` Donglin Peng
2025-10-21 18:59   ` Eduard Zingerman
2025-10-22  3:02     ` Donglin Peng
2025-10-22 20:50       ` Eduard Zingerman
2025-10-23 10:35         ` Donglin Peng
2025-10-23 15:52           ` Alexei Starovoitov
2025-10-23 16:28             ` Andrii Nakryiko
2025-10-23 18:37               ` Alexei Starovoitov
2025-10-23 19:39                 ` Andrii Nakryiko
2025-10-24  1:59                   ` Donglin Peng
2025-10-24  2:23                     ` Donglin Peng
2025-10-24  2:32                       ` Eduard Zingerman
2025-10-24  3:04                         ` Donglin Peng
2025-10-24  3:15                           ` Eduard Zingerman [this message]
2025-10-24  3:19                             ` Donglin Peng
2025-10-20  9:39 ` [RFC PATCH v2 3/5] libbpf: check if BTF is sorted " Donglin Peng
2025-10-20  9:39 ` [RFC PATCH v2 4/5] selftests/bpf: add tests for BTF deduplication and sorting Donglin Peng
2025-10-21 19:07   ` Eduard Zingerman
2025-10-23 11:20     ` Donglin Peng
2025-10-20  9:39 ` [RFC PATCH v2 5/5] btf: add CONFIG_BPF_SORT_BTF_BY_KIND_NAME Donglin Peng
2025-10-21  0:50   ` Eduard Zingerman
2025-10-21  8:33     ` Donglin Peng
2025-10-21 17:27   ` Alan Maguire
2025-10-22  1:15     ` Donglin Peng

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f5cb8c37dc7a23beb0d83fe2aa0a4dc29bc40fd5.camel@gmail.com \
    --to=eddyz87@gmail.com \
    --cc=alan.maguire@oracle.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=dolinux.peng@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pengdonglin@xiaomi.com \
    --cc=song@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox