kernel/bpf/verifier: Support module kfunc resolution via instruction offset
PATCH v2 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.
---
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.
--- kernel/bpf/verifier.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) mode change 100644 => 100755 kernel/bpf/verifier.c
kernel/bpf/verifier.c +11 -1
@@ -3303,6 +3303,7 @@ static struct btf *__find_kfunc_desc_btf(struct bpf_verifier_env *env,*/ sort (tab->descs, tab->nr_descs,sizeof (tab->descs[0 ]), kfunc_btf_cmp_by_off,NULL ); + }else { btf = b->btf; }
@@ -3498,7 +3499,16 @@ 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) { +struct bpf_kfunc_btf kf_btf = { .offset = offset }; +struct module *mod; +struct bpf_kfunc_btf *b; + b =bsearch (&kf_btf, btf_tab->descs, btf_tab->nr_descs, +sizeof (btf_tab->descs[0 ]), kfunc_btf_cmp_by_off); + mod = b->module; + addr =find_kallsyms_symbol_value (mod, 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 reviewShould this file really be made executable? Kernel source files should not have the executable bit set. This appears to be an accidental file mode change.
kernel/bpf/verifier.c
Quoted 3 lines
kernel/bpf/verifier.c +0
-0
Should this file really be made executable? Kernel source files should not have the executable bit set. This appears to be an accidental file mode change.
Quoted 3 lines
index
11054ad89c14..7c7441cc3c2ekernel/bpf/verifier.c +0
-0
[ ... ]
kernel/bpf/verifier.c int bpf_add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, u16 offset) · line 2743
Quoted 11 lines
kernel/bpf/verifier.c +6
-1@@ -2734,7 +2735,16 @@ int bpf_add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, u16 offset)if (err)return err; - addr =kallsyms_lookup_name (kfunc.name); +if (offset) { +struct bpf_kfunc_btf kf_btf = { .offset = offset }; +struct module *mod; +struct bpf_kfunc_btf *b; + b =bsearch (&kf_btf, btf_tab->descs, btf_tab->nr_descs, +sizeof (btf_tab->descs[0 ]), kfunc_btf_cmp_by_off);
^^^^
The continuation line here uses spaces instead of tabs. Should this be indented with tabs to match the kernel coding style?
kernel/bpf/verifier.c static int add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, s16 offset) · line 3511
Quoted 4 lines
kernel/bpf/verifier.c +4
-0+ mod = b->module; + addr =find_kallsyms_symbol_value (mod, kfunc.name); + }else + addr =kallsyms_lookup_name (kfunc.name);
The if branch uses braces for multiple statements, but the else branch omits them. Does this match the expected style for the rest of the file? The coding style guide suggests using braces on both branches when one requires them.
kernel/bpf/verifier.c static int add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, s16 offset) · line 3514
Quoted 3 lines
kernel/bpf/verifier.c +0
-0if (!addr) {verbose (env,"cannot find address for kernel function %s\n" , kfunc.name);return -EINVAL ;
---
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/25618517155
Quoted 21 lines · Song Chen
Use the instruction's 'off' field to indicate the source of a kfunc. When libbpf resolves a kfunc, it sets
insn->offto 0 for vmlinux kfuncs or a non-zero module ID for module kfuncs.The verifier now checks
insn->offto 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.
Suggested-by Alexei Starovoitov <[email protected]>Signed-off-by Song Chen <[email protected]>---
changelog:
v1 --- v2:
libbpf has already specified which module this kfunc belongs to as ebpf code onwer's expectation, then verifier usesfind_kallsyms_symbol_valueto search kfunc's addr.
Quoted 4 lines
--- kernel/bpf/verifier.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) mode change 100644 => 100755 kernel/bpf/verifier.c
In general, the second and following parts of a multi-part patch should be sent as a reply to the first part so that they all thread together at the receiving end. Tools like git and quilt have commands to mail out a set of patches with the proper threading.
kernel/bpf/verifier.c static struct btf *__find_kfunc_desc_btf(struct bpf_verifier_env *env, · line 3306
Quoted 11 lines
kernel/bpf/verifier.c +1
-0@@ -3303,6 +3303,7 @@ static struct btf *__find_kfunc_desc_btf(struct bpf_verifier_env *env,*/ sort (tab->descs, tab->nr_descs,sizeof (tab->descs[0 ]), kfunc_btf_cmp_by_off,NULL ); +
Unnecessary change.
Quoted 4 lines
kernel/bpf/verifier.c +0
-0}else { btf = b->btf; }
kernel/bpf/verifier.c static int add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, s16 offset) · line 3507
Quoted 10 lines
kernel/bpf/verifier.c +6
-1@@ -3498,7 +3499,16 @@ 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) { +struct bpf_kfunc_btf kf_btf = { .offset = offset }; +struct module *mod; +struct bpf_kfunc_btf *b; + b =bsearch (&kf_btf, btf_tab->descs, btf_tab->nr_descs, +sizeof (btf_tab->descs[0 ]), kfunc_btf_cmp_by_off);
Tip: before submission, you can run scripts/checkpatch.pl to check for formatting issues.
kernel/bpf/verifier.c static int add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, s16 offset) · line 3509
Quoted 2 lines
kernel/bpf/verifier.c +2
-0+ mod = b->module; + addr =find_kallsyms_symbol_value (mod, kfunc.name);
struct bpf_kfunc_btf b, kf_btf = { .offset = offset };
......addr = find_kallsyms_symbol_value(b->module, kfunc.name);
Quoted 5 lines
kernel/bpf/verifier.c +2
-0+ }else + addr =kallsyms_lookup_name (kfunc.name);if (!addr) {verbose (env,"cannot find address for kernel function %s\n" , kfunc.name);return -EINVAL ;
Signature · 2 lines
Thanks Kaitao Cheng
Hi,
Quoted 27 lines · Kaitao Cheng
Quoted 21 lines · Song Chen
Use the instruction's 'off' field to indicate the source of a kfunc. When libbpf resolves a kfunc, it sets
insn->offto 0 for vmlinux kfuncs or a non-zero module ID for module kfuncs.The verifier now checks
insn->offto 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.
Suggested-by Alexei Starovoitov <[email protected]>Signed-off-by Song Chen <[email protected]>---
changelog:
v1 --- v2:
libbpf has already specified which module this kfunc belongs to as ebpf code onwer's expectation, then verifier usesfind_kallsyms_symbol_valueto search kfunc's addr.Quoted 4 lines
--- kernel/bpf/verifier.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)mode change 100644 => 100755
kernel/bpf/verifier.c
Quoted 5 lines
In general, the second and following parts of a multi-part patch should be sent as a reply to the first part so that they all thread together at the receiving end. Tools like git and quilt have commands to mail out a set of patches with the proper threading.
you mean "git send-email 0001-*.patch 0002-*.patch 0003-*.patch", i didn't know that before, thanks so much.
Quoted 21 lines
kernel/bpf/verifier.c static struct btf *__find_kfunc_desc_btf(struct bpf_verifier_env *env, · line 3306
Quoted 11 lines
kernel/bpf/verifier.c +1
-0@@ -3303,6 +3303,7 @@ static struct btf *__find_kfunc_desc_btf(struct bpf_verifier_env *env,*/ sort (tab->descs, tab->nr_descs,sizeof (tab->descs[0 ]), kfunc_btf_cmp_by_off,NULL ); +Unnecessary change.
Quoted 7 lines
kernel/bpf/verifier.c +0
-0}else { btf = b->btf; }@@ -3498,7 +3499,16 @@ static int add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, s16 offset)if (err)return err;
Quoted 9 lines
kernel/bpf/verifier.c static int add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, s16 offset) · line 3507
Quoted 7 lines
kernel/bpf/verifier.c +6
-1- addr =kallsyms_lookup_name (kfunc.name); +if (offset) { +struct bpf_kfunc_btf kf_btf = { .offset = offset }; +struct module *mod; +struct bpf_kfunc_btf *b; + b =bsearch (&kf_btf, btf_tab->descs, btf_tab->nr_descs, +sizeof (btf_tab->descs[0 ]), kfunc_btf_cmp_by_off);Tip: before submission, you can run
scripts/checkpatch.plto check for formatting issues.
Many thanks, i did checkpatch every time, but i probably forgot it this time, sorry about that. As the reviews' comments, looks like this patch has its shot, more attention will be paid on v3.
Quoted 8 lines
kernel/bpf/verifier.c static int add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, s16 offset) · line 3509
Quoted 3 lines
kernel/bpf/verifier.c +2
-0+ mod = b->module; + addr =find_kallsyms_symbol_value (mod, kfunc.name);struct bpf_kfunc_btf b, kf_btf = { .offset = offset }; ......addr =
find_kallsyms_symbol_value(b->module, kfunc.name);
will do.
Quoted 6 lines
kernel/bpf/verifier.c static int add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, s16 offset) · line 3514
Quoted 5 lines
kernel/bpf/verifier.c +2
-0+ }else + addr =kallsyms_lookup_name (kfunc.name);if (!addr) {verbose (env,"cannot find address for kernel function %s\n" , kfunc.name);return -EINVAL ;
best regards,
Song