From: Song Chen <chensong_2000@126.com>
To: Yonghong Song <yonghong.song@linux.dev>,
martin.lau@linux.dev, ast@kernel.org,
alexei.starovoitov@gmail.com, daniel@iogearbox.net,
andrii@kernel.org, eddyz87@gmail.com, song@kernel.org,
john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me,
haoluo@google.com, jolsa@kernel.org, kaitao.cheng@linux.dev
Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v4] btf: reject to register duplicated kfunc
Date: Wed, 3 Jun 2026 17:11:25 +0800 [thread overview]
Message-ID: <874ee728-51f2-46ac-93a5-7740abcb0a77@126.com> (raw)
In-Reply-To: <d4054d34-430c-4360-8392-a21f8ebacff4@linux.dev>
Hi,
在 2026/6/3 01:13, Yonghong Song 写道:
>
>
> On 6/2/26 4:07 AM, Song Chen wrote:
>> I had an ebpf program which calls a kfunc defined and
>> implemented in one of my kernel modules, it was working
>> fine in 6.16, but was rejected to run by libbpf in 6.19,
>> the error message was:
>>
>> libbpf: extern (func ksym) 'bpf_strstr': func_proto [5]
>> incompatible with vmlinux [94389]
>>
>> The reason is there is a new added kfunc in kernel 6.19
>> which happens to be the same name with my kfunc. However the
>> error message is not obvious enough to address problem
>> immediately.
>>
>> Therefore, this patches searches duplicated kfunc in
>> both btf_vmlinux and btf_modules list before a kernel module
>> attempts to register kfuncs through register_btf_kfunc_id_set,
>> it prints clear error message and returns error code if same name
>> kfunc has already implemented and registered, then developer
>> knows at the first place.
>>
>> Suggested-by: Alexei Starovoitov <alexei.starovoitov@gmail.com>
>> Suggested-by: Kaitao Cheng <kaitao.cheng@linux.dev>
>> Reviewed-by: Yonghong Song <yonghong.song@linux.dev>
>> Signed-off-by: Song Chen <chensong_2000@126.com>
>
> The subject can be [PATCH bpf v4] bpf: Reject to register duplicated kfunc
>
>>
>> ---
>> changelog:
>> v1 --- v2:
>> libbpf has already specified which module this kfunc belongs to as
>> ebpf code onwer's expectation, then verifier uses
>> find_kallsyms_symbol_value to search kfunc's addr.
>>
>> v2 --- v3:
>> After v2, I tried a new idea of introducing a namespace in libbpf
>> to specify kfunc owner in an ebpf program suggested by Kaitao Cheng,
>> please see [1]. Alex suggested to go back to report an error during
>> kmod load on conflicting kfunc name for now. What's more, v2 only
>> searched bpf_vmlinux, v3 also traverses btf_modules list.
>>
>> v3 --- v4
>> Fixed some coding style problems suggested from Kaitao Cheng.
>> ---
>> kernel/bpf/btf.c | 38 +++++++++++++++++++++++++++++++++++++-
>> 1 file changed, 37 insertions(+), 1 deletion(-)
>>
>> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
>> index 4872d2a6c42d..fe1612677a4a 100644
>> --- a/kernel/bpf/btf.c
>> +++ b/kernel/bpf/btf.c
>> @@ -8618,6 +8618,41 @@ static int btf_check_iter_kfuncs(struct btf
>> *btf, const char *func_name,
>> return 0;
>> }
>> +static int btf_check_kfunc_name(struct btf *btf, const char
>> *func_name, u32 kind)
>> +{
>> +#ifdef CONFIG_DEBUG_INFO_BTF_MODULES
>> + struct btf_module *btf_mod, *tmp;
>> +#endif
>> + s32 id;
>
> s32 err = 0, id;
>
>> +
>> + if (!btf_is_module(btf))
>> + return 0;
>> +
>> + id = btf_find_by_name_kind(bpf_get_btf_vmlinux(), func_name, kind);
>> + if (id >= 0) {
>> + pr_err("kfunc %s (id: %d) is already present in vmlinux.\n",
>> + func_name, id);
>> + return -EINVAL;
>> + }
>> +
>> +#ifdef CONFIG_DEBUG_INFO_BTF_MODULES
>> + mutex_lock(&btf_module_mutex);
>> + list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) {
>> + if (btf_mod->btf == btf)
>> + continue;
>> + id = btf_find_by_name_kind(btf_mod->btf, func_name, kind);
>> + if (id >= 0) {
>> + pr_err("kfunc %s (id: %d) is already present in module
>> %s.\n",
>> + func_name, id, btf_mod->module->name);
>> + mutex_unlock(&btf_module_mutex);
>> + return -EINVAL;
>
> Let us avoid the above mutex_unlock and 'return -EINVAL', just do
> err = -EINVAL;
> break;
>
>> + }
>> + }
>> + mutex_unlock(&btf_module_mutex);
>> +#endif
>> + return 0;
>
> return err;
>
I will go with guard(mutex), please review my next submit and let me
know if you're ok with it or not. Thanks.
>> +}
>> +
>> static int btf_check_kfunc_protos(struct btf *btf, u32 func_id, u32
>> func_flags)
>> {
>> const struct btf_type *func;
>> @@ -8631,7 +8666,8 @@ static int btf_check_kfunc_protos(struct btf
>> *btf, u32 func_id, u32 func_flags)
>> /* sanity check kfunc name */
>> func_name = btf_name_by_offset(btf, func->name_off);
>> - if (!func_name || !func_name[0])
>> + if (!func_name || !func_name[0] ||
>> + btf_check_kfunc_name(btf, func_name, BTF_INFO_KIND(func->info)))
>
> format issue:
> if (!func_name || !func_name[0] ||
> btf_check_kfunc_name(btf, func_name, BTF_INFO_KIND(func->info)))
> ...
>
I will fix it with indentation plus space.
>> return -EINVAL;
>> func = btf_type_by_id(btf, func->type);
>
many thanks.
Song
prev parent reply other threads:[~2026-06-03 9:12 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-02 11:07 Song Chen
2026-06-02 11:34 ` Kaitao Cheng
2026-06-02 17:13 ` Yonghong Song
2026-06-03 2:18 ` Leon Hwang
2026-06-03 9:15 ` Song Chen
2026-06-03 9:11 ` Song Chen [this message]
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=874ee728-51f2-46ac-93a5-7740abcb0a77@126.com \
--to=chensong_2000@126.com \
--cc=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kaitao.cheng@linux.dev \
--cc=kpsingh@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=sdf@fomichev.me \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
/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
all inboxes | Powered by JetHome®