libbpf: support weak typed ksyms.
Subsystems (6)
PATCH 1/1 libbpf: support weak typed ksyms.
Currently weak typeless ksyms have default value zero, when they don't exist in the kernel. However, weak typed ksyms are rejected by libbpf. This means that if a bpf object contains the declaration of a non-existing weak typed ksym, it will be rejected even if there is no program that references the symbol.
In fact, we could let them to pass the checks in libbpf and leave the object to be rejected by the bpf verifier. More specifically, upon seeing a weak typed symbol, libbpf can assign it a zero btf_id, which is associated to the type 'void'. The verifier expects the symbol to be BTF_VAR_KIND instead, therefore will reject loading.
In practice, we often add new kernel symbols and roll out the kernel changes to fleet. And we want to release a single bpf object that can be loaded on both the new and the old kernels. Passing weak typed ksyms in libbpf allows us to do so as long as the programs that reference the new symbols are disabled on the old kernel.
--- tools/lib/bpf/libbpf.c | 17 +++++- .../selftests/bpf/prog_tests/ksyms_btf.c | 42 +++++++++++++ .../selftests/bpf/progs/test_ksyms_weak.c | 60 +++++++++++++++++++ 3 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_weak.c
tools/lib/bpf/libbpf.c +14 -3
@@ -6584,7 +6584,7 @@ static int bpf_object__read_kallsyms_file(struct bpf_object *obj) }static int find_ksym_btf_id (struct bpf_object *obj,const char *ksym_name, -__u16 kind,struct btf **res_btf, +__u16 kind,bool is_weak,struct btf **res_btf,int *res_btf_fd) {int i, id, btf_fd, err;
@@ -6608,6 +6608,9 @@ static int find_ksym_btf_id(struct bpf_object *obj, const char *ksym_name,break ; } } +if (is_weak && id == -ENOENT ) +return 0 ; +if (id <=0 ) {pr_warn ("extern (%s ksym) '%s': failed to find BTF ID in kernel BTF(s).\n" ,__btf_kind_str (kind), ksym_name);
@@ -6627,11 +6630,19 @@ static int bpf_object__resolve_ksym_var_btf_id(struct bpf_object *obj,const char *targ_var_name;int id, btf_fd =0 , err;struct btf *btf =NULL ; +bool is_weak = ext->is_weak; - id =find_ksym_btf_id (obj, ext->name,BTF_KIND_VAR , &btf, &btf_fd); + id =find_ksym_btf_id (obj, ext->name,BTF_KIND_VAR , is_weak, &btf, &btf_fd);if (id <0 )return id; +if (is_weak && id ==0 ) { + ext->is_set = true; + ext->ksym.kernel_btf_obj_fd =0 ; + ext->ksym.kernel_btf_id =0 ; +return 0 ; + } +/* find local type_id */ local_type_id = ext->ksym.type_id;
@@ -6676,7 +6687,7 @@ static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj, local_func_proto_id = ext->ksym.type_id; - kfunc_id =find_ksym_btf_id (obj, ext->name,BTF_KIND_FUNC , + kfunc_id =find_ksym_btf_id (obj, ext->name,BTF_KIND_FUNC , false, &kern_btf, &kern_btf_fd);if (kfunc_id <0 ) { pr_warn("extern (func ksym) '%s': not found in kernel BTF\n",
tools/testing/selftests/bpf/prog_tests/ksyms_btf.c +42 -0
58 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_ksyms_weak.c new file +60 -0
60 lines not on this page · raw message
Signature · 1 line
2.32.0.554.ge1b32706d8-goog
Quoted 13 lines · Hao Luo
Currently weak typeless ksyms have default value zero, when they don't exist in the kernel. However, weak typed ksyms are rejected by libbpf. This means that if a bpf object contains the declaration of a non-existing weak typed ksym, it will be rejected even if there is no program that references the symbol.
In fact, we could let them to pass the checks in libbpf and leave the object to be rejected by the bpf verifier. More specifically, upon seeing a weak typed symbol, libbpf can assign it a zero
btf_id, which is associated to the type 'void'. The verifier expects the symbol to beBTF_VAR_KINDinstead, therefore will reject loading.
Quoted 5 lines
In practice, we often add new kernel symbols and roll out the kernel changes to fleet. And we want to release a single bpf object that can be loaded on both the new and the old kernels. Passing weak typed ksyms in libbpf allows us to do so as long as the programs that reference the new symbols are disabled on the old kernel.
How do you detect whether a given ksym is present or not? You check that from user-space and then use .rodata to turn off pieces of BPF logic? That's quite inconvenient. It would be great if these typed ksyms worked the same way as typeless ones:
extern const int bpf_link_fops3 __ksym __weak;
/* then in BPF program */
if (&bpf_link_fops3) {
/* use bpf_link_fops3 */
}
I haven't tried, but I suspect it could be made to work if libbpf replaces corresponding ldimm64 instruction (with BTF ID) into a plain ldimm64 instruction loading 0 directly. That would allow the above check (and it would be known false to the verifier) to succeed without the verifier rejecting the BPF program. If actual use of non-existing typed symbol is not guarded properly, verifier would see that register is not PTR_TO_BTF_ID and wouldn't allow to use it for direct memory reads or passing it to BPF helpers.
Have you considered such an approach?
Separately, please use ASSERT_XXX() macros for tests, not plain CHECK()s. Thanks.
Quoted 9 lines
Signed-off-by Hao Luo <[email protected]>--- tools/lib/bpf/libbpf.c | 17 +++++- .../selftests/bpf/prog_tests/ksyms_btf.c | 42 +++++++++++++ .../selftests/bpf/progs/test_ksyms_weak.c | 60 +++++++++++++++++++ 3 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_weak.c
[...]
Thanks for taking a look.
Quoted 21 lines · Andrii Nakryiko
Quoted 13 lines · Hao Luo
Currently weak typeless ksyms have default value zero, when they don't exist in the kernel. However, weak typed ksyms are rejected by libbpf. This means that if a bpf object contains the declaration of a non-existing weak typed ksym, it will be rejected even if there is no program that references the symbol.
In fact, we could let them to pass the checks in libbpf and leave the object to be rejected by the bpf verifier. More specifically, upon seeing a weak typed symbol, libbpf can assign it a zero
btf_id, which is associated to the type 'void'. The verifier expects the symbol to beBTF_VAR_KINDinstead, therefore will reject loading.Quoted 5 lines
In practice, we often add new kernel symbols and roll out the kernel changes to fleet. And we want to release a single bpf object that can be loaded on both the new and the old kernels. Passing weak typed ksyms in libbpf allows us to do so as long as the programs that reference the new symbols are disabled on the old kernel.
Quoted 5 lines
How do you detect whether a given ksym is present or not? You check that from user-space and then use .rodata to turn off pieces of BPF logic? That's quite inconvenient. It would be great if these typed ksyms worked the same way as typeless ones:
It's not by detect. In my use case, I can add a flag to the application to disable/enable loading a BPF program. Because we know at which kernel version a new symbol was introduced, we can coordinate the application flag with the kernel version to avoid the faulting code being loaded on an old kernel.
Quoted 18 lines
extern const int
bpf_link_fops3__ksym__weak;/* then in BPF program */
if (&bpf_link_fops3) { /* use bpf_link_fops3 */ }I haven't tried, but I suspect it could be made to work if libbpf replaces corresponding ldimm64 instruction (with BTF ID) into a plain ldimm64 instruction loading 0 directly. That would allow the above check (and it would be known false to the verifier) to succeed without the verifier rejecting the BPF program. If actual use of non-existing typed symbol is not guarded properly, verifier would see that register is not
PTR_TO_BTF_IDand wouldn't allow to use it for direct memory reads or passing it to BPF helpers.
Quoted 2 lines
Have you considered such an approach?
I haven't thought about this approach. I just grabbed the quickest solution I can think of. Will follow your suggestion and see if it works.
Quoted 4 lines
Separately, please use
ASSERT_XXX()macros for tests, not plain CHECK()s. Thanks.
ACK.
Quoted 11 lines
Quoted 9 lines
Signed-off-by Hao Luo <[email protected]>--- tools/lib/bpf/libbpf.c | 17 +++++- .../selftests/bpf/prog_tests/ksyms_btf.c | 42 +++++++++++++ .../selftests/bpf/progs/test_ksyms_weak.c | 60 +++++++++++++++++++ 3 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_weak.c[...]