libbpf: Introduce 'btf_custom_path' to 'bpf_obj_open_opts'.
Subsystems (5)
PATCH v2 1/1 libbpf: Introduce 'btf_custom_path' to 'bpf_obj_open_opts'.
In order to enable the older kernel to use the CO-RE feature, load the vmlinux btf of the specified path.
Learn from Andrii's comments in [0], add the btf_custom_path parameter to bpf_obj_open_opts, you can directly use the skeleton's <objname>_bpf__open_opts function to pass in the btf_custom_path parameter.
Prior to this, there was also a developer who provided a patch with similar functions. It is a pity that the follow-up did not continue to advance. See [1].
[0]https://lore.kernel.org/bpf/CAEf4BzbJZLjNoiK8_VfeVg_Vrg=9iYFv+po-38SMe=UzwDKJ=Q@mail.gmail.com/#t [1]https://yhbt.net/lore/all/CAEf4Bzbgw49w2PtowsrzKQNcxD4fZRE6AKByX-5-dMo-+oWHHA@mail.gmail.com/
---
v1: https://lore.kernel.org/bpf/CAEf4BzaGjEC4t1OefDo11pj2-HfNy0BLhs_G2UREjRNTmb2u=A@mail.gmail.com/t/#m4d9f7c6761fbd2b436b5dfe491cd864b70225804v1->v2:
-- Change custom_btf_path to btf_custom_path.
-- If the length of btf_custom_path of bpf_obj_open_opts is too long,
return ERR_PTR(-ENAMETOOLONG).
-- Add custom BTF is in addition to vmlinux BTF
with btf_custom_path field.
tools/lib/bpf/libbpf.c | 27 ++++++++++++++++++++++++--- tools/lib/bpf/libbpf.h | 6 +++++- 2 files changed, 29 insertions(+), 4 deletions(-)
tools/lib/bpf/libbpf.c +24 -3
@@ -494,6 +494,10 @@ struct bpf_object {struct btf *btf;struct btf_ext *btf_ext; +/* custom BTF is in addition to vmlinux BTF (i.e., Use the CO-RE +* feature in the old kernel). +*/ +char *btf_custom_path;/* Parse and load BTF vmlinux if any of the programs in the object need * it at load time. */
@@ -2679,8 +2683,15 @@ static int bpf_object__load_vmlinux_btf(struct bpf_object *obj, bool force)if (!force && !obj_needs_vmlinux_btf (obj))return 0 ; - obj->btf_vmlinux =libbpf_find_kernel_btf (); - err =libbpf_get_error (obj->btf_vmlinux); +if (obj->btf_custom_path) { + obj->btf_vmlinux =btf__parse (obj->btf_custom_path,NULL ); + err =libbpf_get_error (obj->btf_vmlinux); +pr_debug ("loading custom vmlinux BTF '%s': %d\n" , obj->btf_custom_path, err); + }else { + obj->btf_vmlinux =libbpf_find_kernel_btf (); + err =libbpf_get_error (obj->btf_vmlinux); + } +if (err) {pr_warn ("Error loading vmlinux BTF: %d\n" , err); obj->btf_vmlinux =NULL ;
@@ -7554,7 +7565,7 @@ int bpf_program__load(struct bpf_program *prog, char *license, __u32 kern_ver) __bpf_object__open(const char *path, const void *obj_buf, size_t obj_buf_sz, const struct bpf_object_open_opts *opts) { - const char *obj_name, *kconfig; + const char *obj_name, *kconfig, *btf_tmp_path;struct bpf_program *prog;struct bpf_object *obj;char tmp_name[64 ];
@@ -7584,6 +7595,15 @@ int bpf_program__load(struct bpf_program *prog, char *license, __u32 kern_ver) obj =bpf_object__new (path, obj_buf, obj_buf_sz, obj_name);if (IS_ERR (obj))return obj; + + btf_tmp_path =OPTS_GET (opts, btf_custom_path,NULL ); +if (btf_tmp_path) { +if (strlen (btf_tmp_path) >=PATH_MAX ) +return ERR_PTR (-ENAMETOOLONG ); + obj->btf_custom_path =strdup (btf_tmp_path); +if (!obj->btf_custom_path) +return ERR_PTR (-ENOMEM ); + } kconfig =OPTS_GET (opts, kconfig,NULL );if (kconfig) {
@@ -8702,6 +8722,7 @@ void bpf_object__close(struct bpf_object *obj)for (i =0 ; i < obj->nr_maps; i++)bpf_map__destroy (&obj->maps[i]); +zfree (&obj->btf_custom_path);zfree (&obj->kconfig);zfree (&obj->externs); obj->nr_extern =0 ;
tools/lib/bpf/libbpf.h +5 -1
@@ -94,8 +94,12 @@ struct bpf_object_open_opts {* system Kconfig for CONFIG_xxx externs. */ const char *kconfig; +/* custom BTF is in addition to vmlinux BTF (i.e., Use the CO-RE +* feature in the old kernel). +*/ +char *btf_custom_path; }; -#define bpf_object_open_opts__last_field kconfig +#define bpf_object_open_opts__last_field btf_custom_path LIBBPF_API struct bpf_object *bpf_object__open(const char *path); LIBBPF_API struct bpf_object *
Signature · 1 line
1.8.3.1
Quoted 48 lines · Shuyi Cheng
In order to enable the older kernel to use the CO-RE feature, load the vmlinux btf of the specified path.
Learn from Andrii's comments in [0], add the
btf_custom_pathparameter tobpf_obj_open_opts, you can directly use the skeleton's <objname>_bpf__open_opts function to pass in thebtf_custom_pathparameter.Prior to this, there was also a developer who provided a patch with similar functions. It is a pity that the follow-up did not continue to advance. See [1].
[0]https://lore.kernel.org/bpf/CAEf4BzbJZLjNoiK8_VfeVg_Vrg=9iYFv+po-38SMe=UzwDKJ=Q@mail.gmail.com/#t [1]https://yhbt.net/lore/all/CAEf4Bzbgw49w2PtowsrzKQNcxD4fZRE6AKByX-5-dMo-+oWHHA@mail.gmail.com/Cc Andrii Nakryiko <[email protected]>Cc Daniel Borkmann <[email protected]>Signed-off-by Shuyi Cheng <[email protected]>---
v1: https://lore.kernel.org/bpf/CAEf4BzaGjEC4t1OefDo11pj2-HfNy0BLhs_G2UREjRNTmb2u=A@mail.gmail.com/t/#m4d9f7c6761fbd2b436b5dfe491cd864b70225804v1->v2:
-- Changecustom_btf_pathtobtf_custom_path.
-- If the length ofbtf_custom_pathofbpf_obj_open_optsis too long,
returnERR_PTR(-ENAMETOOLONG).
-- Addcustom BTF is in addition to vmlinux BTF
withbtf_custom_pathfield.tools/lib/bpf/libbpf.c | 27 ++++++++++++++++++++++++--- tools/lib/bpf/libbpf.h | 6 +++++- 2 files changed, 29 insertions(+), 4 deletions(-)tools/lib/bpf/libbpf.c +4
-0@@ -494,6 +494,10 @@ struct bpf_object {struct btf *btf;struct btf_ext *btf_ext; +/* custom BTF is in addition to vmlinux BTF (i.e., Use the CO-RE +* feature in the old kernel). +*/ +char *btf_custom_path;/* Parse and load BTF vmlinux if any of the programs in the object need * it at load time. */
tools/lib/bpf/libbpf.c static int bpf_object__load_vmlinux_btf(struct bpf_object *obj, bool force) · line 2687
Quoted 7 lines
tools/lib/bpf/libbpf.c +2
-2@@ -2679,8 +2683,15 @@ static int bpf_object__load_vmlinux_btf(struct bpf_object *obj, bool force)if (!force && !obj_needs_vmlinux_btf (obj))return 0 ; - obj->btf_vmlinux =libbpf_find_kernel_btf (); - err =libbpf_get_error (obj->btf_vmlinux); +if (obj->btf_custom_path) { + obj->btf_vmlinux =btf__parse (obj->btf_custom_path,NULL );
I don't think this is right. Keep in mind the general case where vmlinux BTF might be available, but user wants to perform CO-RE using custom BTF (like we do in our selftests in core_reloc.c). In such cases all the other features that rely on vmlinux BTF would use real vmlinux BTF, but CO-RE relocations would use custom BTF. See the original patch you are referencing, it loaded btf_override separately from obj->btf_vmlinux.
Quoted 20 lines
tools/lib/bpf/libbpf.c +8
-1+ err =libbpf_get_error (obj->btf_vmlinux); +pr_debug ("loading custom vmlinux BTF '%s': %d\n" , obj->btf_custom_path, err); + }else { + obj->btf_vmlinux =libbpf_find_kernel_btf (); + err =libbpf_get_error (obj->btf_vmlinux); + } +if (err) {pr_warn ("Error loading vmlinux BTF: %d\n" , err); obj->btf_vmlinux =NULL ;@@ -7554,7 +7565,7 @@ int bpf_program__load(struct bpf_program *prog, char *license, __u32 kern_ver) __bpf_object__open(const char *path, const void *obj_buf, size_t obj_buf_sz, const struct bpf_object_open_opts *opts) { - const char *obj_name, *kconfig; + const char *obj_name, *kconfig, *btf_tmp_path;struct bpf_program *prog;struct bpf_object *obj;char tmp_name[64 ];
tools/lib/bpf/libbpf.c int bpf_program__load(struct bpf_program *prog, char *license, __u32 kern_ver) · line 7602
Quoted 8 lines
tools/lib/bpf/libbpf.c +5
-0@@ -7584,6 +7595,15 @@ int bpf_program__load(struct bpf_program *prog, char *license, __u32 kern_ver) obj =bpf_object__new (path, obj_buf, obj_buf_sz, obj_name);if (IS_ERR (obj))return obj; + + btf_tmp_path =OPTS_GET (opts, btf_custom_path,NULL ); +if (btf_tmp_path) { +if (strlen (btf_tmp_path) >=PATH_MAX ) +return ERR_PTR (-ENAMETOOLONG );
we are leaking obj here
tools/lib/bpf/libbpf.c int bpf_program__load(struct bpf_program *prog, char *license, __u32 kern_ver) · line 7605
Quoted 3 lines
tools/lib/bpf/libbpf.c +3
-0+ obj->btf_custom_path =strdup (btf_tmp_path); +if (!obj->btf_custom_path) +return ERR_PTR (-ENOMEM );
and here
tools/lib/bpf/libbpf.c int bpf_program__load(struct bpf_program *prog, char *license, __u32 kern_ver) · line 7609
Quoted 4 lines
tools/lib/bpf/libbpf.c +1
-0+ } kconfig =OPTS_GET (opts, kconfig,NULL );if (kconfig) {
And a few lines below. You didn't introduce this bug, I just spotted it while reviewing your patch, but it would be nice to fix it as well.
Quoted 29 lines
tools/lib/bpf/libbpf.c +1
-0@@ -8702,6 +8722,7 @@ void bpf_object__close(struct bpf_object *obj)for (i =0 ; i < obj->nr_maps; i++)bpf_map__destroy (&obj->maps[i]); +zfree (&obj->btf_custom_path);zfree (&obj->kconfig);zfree (&obj->externs); obj->nr_extern =0 ;tools/lib/bpf/libbpf.h +5
-2@@ -94,8 +94,12 @@ struct bpf_object_open_opts {* system Kconfig for CONFIG_xxx externs. */ const char *kconfig; +/* custom BTF is in addition to vmlinux BTF (i.e., Use the CO-RE +* feature in the old kernel). +*/ +char *btf_custom_path; }; -#define bpf_object_open_opts__last_field kconfig +#define bpf_object_open_opts__last_field btf_custom_path LIBBPF_API struct bpf_object *bpf_object__open(const char *path); LIBBPF_API struct bpf_object * --1.8.3.1