Re: [PATCH bpf-next v1 06/14] resolve_btfids: Discover kfuncs from BTF ID sets

Ihor Solodrai <[email protected]>
Newsgroups gmane.linux.kbuild.devel,gmane.linux.kernel.bpf
Message-ID <[email protected]>
On 6/3/26 4:45 PM, Andrii Nakryiko wrote:
> On Mon, Jun 1, 2026 at 3:19 PM Ihor Solodrai <[email protected]> wrote:
>>
>> collect_kfuncs() currently uses bpf_kfunc decl tags to identify the
>> list of kfuncs. The decl tags are generated by pahole, which makes
>> current implementation implicitly rely on those tags being generated.
>>
>> The authoritative source, used by the the BPF verifier for kfunc
>> registration, of functions being BPF kfuncs are
>> BTF_KFUNCS_START()/END() declarations. These are BTF_ID_SET8 under the
>> hood. Currently resolve_btfids reads kfunc flags from these sets, and
>> populates them with BTF IDs.
>>
>> Implement kfunc discovery from BTF_ID_SET8 symbols in resolve_btfids,
>> removing the dependency on pahole's emmission of decl tags.
>>
>> Walk BTF_ID_KIND_SET8 sets, and use the address-to-symbol index to
>> look up set entry's BTF_ID symbol name (before .BTF_ids is patched),
>> recording the paired flags directly. This makes find_kfunc_flags()
>> helper unnecessary, so it's removed.
>>
>> Kernel functions can appear in more than one set, which is legitimate,
>> since kfunc sets are prog-type dependent in the kernel. So for btf2btf
>> processing deduplicate kfuncs by BTF ID, accumulate (OR) the flags,
>> and warn on flags mismatch to catch inconsistent declarations.
>>
>> Signed-off-by: Ihor Solodrai <[email protected]>
>> ---
>>  tools/bpf/resolve_btfids/main.c | 122 ++++++++++++++------------------
>>  1 file changed, 55 insertions(+), 67 deletions(-)
>>
>> diff --git a/tools/bpf/resolve_btfids/main.c b/tools/bpf/resolve_btfids/main.c
>> index 43512af13148..d35a7b2460e8 100644
>> --- a/tools/bpf/resolve_btfids/main.c
>> +++ b/tools/bpf/resolve_btfids/main.c
>> @@ -970,6 +970,23 @@ static int push_kfunc(struct btf2btf_context *ctx, struct kfunc *kfunc)
>>         struct kfunc *arr = ctx->kfuncs;
>>         u32 cap = ctx->max_kfuncs;
>>
>> +       /*
>> +        * A kfunc can be listed in multiple BTF ID sets.
>> +        * In this case, dedup by btf_id and accumulate kfunc flags.
>> +        */
>> +       for (u32 i = 0; i < ctx->nr_kfuncs; i++) {
>> +               if (ctx->kfuncs[i].btf_id != kfunc->btf_id)
>> +                       continue;
>> +
> 
> with hundreds of kfuncs, this O(N^2) approach is going to be a bit
> slow, should we use rb tree for lookups?

Yeah, it's O(n^2). I was thinking it's fine, because the number of
kfuncs is ~ a few hundreds. I'll use the rbtree in v2.

> 
>> +               if (ctx->kfuncs[i].flags != kfunc->flags) {
>> +                       pr_err("WARN: resolve_btfids: inconsistent flags for kfunc %s: 0x%x != 0x%x\n",
>> +                              kfunc->name, ctx->kfuncs[i].flags, kfunc->flags);
>> +                       warnings++;
>> +               }
>> +               ctx->kfuncs[i].flags |= kfunc->flags;
>> +               return 0;
>> +       }
>> +
>>         if (ctx->nr_kfuncs + 1 > cap) {
>>                 cap = max(cap + 256, cap * 2);
>>                 arr = realloc(arr, sizeof(struct kfunc) * cap);
> 
> [...]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.