kernel/bpf/verifier: Support module kfunc resolution via instruction offset
PATCH 2/2 kernel/bpf/verifier: Support module kfunc resolution via instruction offset
Use the instruction's 'off' field to indicate the source of a kfunc. When libbpf resolves a kfunc, it sets insn->off to 0 for vmlinux kfuncs or a non-zero module ID for module kfuncs.
The verifier now checks insn->off to determine where to look up the kfunc address: off == 0 searches in vmlinux, while off > 0 searches in the corresponding module BTF. This enables proper resolution of module kfuncs that may override vmlinux kfuncs with the same name.
This works in conjunction with the libbpf change that prioritizes module kfuncs during BTF resolution.
--- kernel/bpf/verifier.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +4 -1
@@ -3498,7 +3498,10 @@ static int add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, s16 offset)if (err)return err; - addr =kallsyms_lookup_name (kfunc.name); +if (offset) + addr =module_kallsyms_lookup_name (kfunc.name); +else + addr =kallsyms_lookup_name (kfunc.name);if (!addr) {verbose (env,"cannot find address for kernel function %s\n" , kfunc.name);return -EINVAL ;
Signature · 1 line
2.43.0
[email protected]
AI reviewWhen offset is non-zero, module_kallsyms_lookup_name(kfunc.name) searches all loaded modules and returns the first match. How does this handle the case where multiple modules register kfuncs with…
Quoted 6 lines
kernel/bpf/verifier.c +0
-0@@ -2734,7 +2734,10 @@ int bpf_add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, u16 offset)if (err)
kernel/bpf/verifier.c int bpf_add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, u16 offset) · line 2743
Quoted 10 lines
kernel/bpf/verifier.c +4
-1return err; - addr =kallsyms_lookup_name (kfunc.name); +if (offset) + addr =module_kallsyms_lookup_name (kfunc.name); +else + addr =kallsyms_lookup_name (kfunc.name);if (!addr) {verbose (env,"cannot find address for kernel function %s\n" , kfunc.name);return -EINVAL ;
When offset is non-zero, module_kallsyms_lookup_name(kfunc.name) searches all loaded modules and returns the first match. How does this handle the case where multiple modules register kfuncs with the same name?
In the original discussion thread from January 2026, Alexei Starovoitov
asked: "but what about kfunc in module A with the same name as kfunc in module B?" You acknowledged this concern and indicated it was in your plan to address. However, the current implementation appears to rely on module load order when multiple modules have identically-named kfuncs.
Reference: https://lore.kernel.org/all/CAADnVQLVQW-Mb_d+zT+aSpPOZ042erPVcSHVBx9PMGET+a754Q@mail.gmail.com/
Is there a mechanism to ensure the correct module's kfunc is selected when names collide across different modules?
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/25150033548
Hi,
Quoted 21 lines · On 4/30/26 14:27, [email protected] wrote:
Quoted 6 lines
kernel/bpf/verifier.c +0
-0@@ -2734,7 +2734,10 @@ int bpf_add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, u16 offset)if (err)kernel/bpf/verifier.c int bpf_add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, u16 offset) · line 2743
Quoted 10 lines
kernel/bpf/verifier.c +4
-1return err; - addr =kallsyms_lookup_name (kfunc.name); +if (offset) + addr =module_kallsyms_lookup_name (kfunc.name); +else + addr =kallsyms_lookup_name (kfunc.name);if (!addr) {verbose (env,"cannot find address for kernel function %s\n" , kfunc.name);return -EINVAL ;When offset is non-zero,
module_kallsyms_lookup_name(kfunc.name)searches all loaded modules and returns the first match. How does this handle the case where multiple modules register kfuncs with the same name?
Quoted 8 lines
In the original discussion thread from January 2026, Alexei Starovoitov
asked: "but what about kfunc in module A with the same name as kfunc in module B?" You acknowledged this concern and indicated it was in your plan to address. However, the current implementation appears to rely on module load order when multiple modules have identically-named kfuncs.Reference: https://lore.kernel.org/all/CAADnVQLVQW-Mb_d+zT+aSpPOZ042erPVcSHVBx9PMGET+a754Q@mail.gmail.com/
This problem is in my mind too, but as far as my understanding, the ebpf code of calling an kfunc is:
85 10 00 00 ff ff ff ff
when libbpf relocates this kfunc, it searches all the modules only by obj->externs[i]->name, no additional indication tells libbpf which module to search from, as a result, the first finding will be relocated in ebpf code, like:
code=0x85 dst=0 src=2 off=1 imm=153416
Quoted 3 lines
Is there a mechanism to ensure the correct module's kfunc is selected when names collide across different modules?
Is it acceptable to add extra information in ebpf source code when it's calling a kfun:
extern int module_a__foo(void *p) __ksym;
extern int module_b__foo(void *p) __ksym;
extern int foo(void *p) __ksym;
Then libbpf interprets obj->externs[i]->name and knows which module or vmlinux this kfunc belongs to, kernel will find the correct addr in verifier.
Best regards,
/Song
Quoted 6 lines
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.mdCI run summary: https://github.com/kernel-patches/bpf/actions/runs/25150033548