libbpf: add libbpf_load_vmlinux_btf/libbpf_load_module_btf APIs
PATCH 1/1 libbpf: add libbpf_load_vmlinux_btf/libbpf_load_module_btf APIs
Add libbpf_load_vmlinux_btf/libbpf_load_module_btf APIs. This is part of the libbpf v1.0. [1]
[1] https://github.com/libbpf/libbpf/issues/280
--- tools/lib/bpf/btf.c | 24 +++++++++++++++++++++++- tools/lib/bpf/btf.h | 2 ++ tools/lib/bpf/libbpf.c | 8 ++++---- tools/lib/bpf/libbpf.map | 2 ++ 4 files changed, 31 insertions(+), 5 deletions(-)
tools/lib/bpf/btf.c +23 -1
@@ -4021,7 +4021,7 @@ static void btf_dedup_merge_hypot_map(struct btf_dedup *d)*/ if (d->hypot_adjust_canon)continue ; - +if (t_kind ==BTF_KIND_FWD && c_kind !=BTF_KIND_FWD ) d->map[t_id] = c_id;
@@ -4395,6 +4395,11 @@ static int btf_dedup_remap_types(struct btf_dedup *d)* data out of it to use for target BTF. */ struct btf *libbpf_find_kernel_btf (void ) +{ +return libbpf_load_vmlinux_btf (); +} + +struct btf *libbpf_load_vmlinux_btf (void ) {struct {const char *path_fmt;
@@ -4440,6 +4445,23 @@ struct btf *libbpf_find_kernel_btf(void)return libbpf_err_ptr (-ESRCH ); } +struct btf *libbpf_load_module_btf (const char *mod) +{ +char path[80 ]; +struct btf *base; +int err; + + base =libbpf_load_vmlinux_btf (); + err =libbpf_get_error (base); +if (err) { +pr_warn ("Error loading vmlinux BTF: %d\n" , err); +return base; + } + +snprintf (path,sizeof (path),"/sys/kernel/btf/%s" , mod); +return btf__parse_split (path, base); +} +int btf_type_visit_type_ids (struct btf_type *t,type_id_visit_fn visit,void *ctx) {int i, n, err;
tools/lib/bpf/btf.h +2 -0
@@ -90,6 +90,8 @@ LIBBPF_API __u32 btf_ext__func_info_rec_size(const struct btf_ext *btf_ext); LIBBPF_API __u32 btf_ext__line_info_rec_size(const struct btf_ext *btf_ext); LIBBPF_API struct btf *libbpf_find_kernel_btf(void); +LIBBPF_API struct btf *libbpf_load_vmlinux_btf(void); +LIBBPF_API struct btf *libbpf_load_module_btf(const char *mod); LIBBPF_API int btf__find_str(struct btf *btf, const char *s); LIBBPF_API int btf__add_str(struct btf *btf, const char *s);
tools/lib/bpf/libbpf.c +4 -4
@@ -2685,7 +2685,7 @@ 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 (); + obj->btf_vmlinux =libbpf_load_vmlinux_btf (); err =libbpf_get_error (obj->btf_vmlinux);if (err) {pr_warn ("Error loading vmlinux BTF: %d\n" , err);
@@ -7236,7 +7236,7 @@ static int bpf_object__collect_relos(struct bpf_object *obj)for (i =0 ; i < obj->nr_programs; i++) {struct bpf_program *p = &obj->programs[i]; - +if (!p->nr_reloc)continue ;
@@ -9562,7 +9562,7 @@ int libbpf_find_vmlinux_btf_id(const char *name,struct btf *btf;int err; - btf =libbpf_find_kernel_btf (); + btf =libbpf_load_vmlinux_btf (); err =libbpf_get_error (btf);if (err) {pr_warn ("vmlinux BTF is not found\n" );
@@ -10080,7 +10080,7 @@ struct bpf_link {int bpf_link__update_program (struct bpf_link *link,struct bpf_program *prog) {int ret; - + ret =bpf_link_update (bpf_link__fd (link),bpf_program__fd (prog),NULL );return libbpf_err_errno (ret); }
tools/lib/bpf/libbpf.map +2 -0
@@ -377,4 +377,6 @@ LIBBPF_0.5.0 { bpf_object__gen_loader; btf_dump__dump_type_data; libbpf_set_strict_mode; + libbpf_load_vmlinux_btf; + libbpf_load_module_btf; } LIBBPF_0.4.0;
Signature · 1 line
2.25.1
Quoted 5 lines · Hengqi Chen
Add libbpf_load_vmlinux_btf/libbpf_load_module_btf APIs. This is part of the libbpf v1.0. [1]
Saying it's part of libbpf 1.0 effort and given a link to Github PR is not really a sufficient commit message. Please expand on what you are doing in the patch and why.
Quoted 35 lines
Signed-off-by Hengqi Chen <[email protected]>--- tools/lib/bpf/btf.c | 24 +++++++++++++++++++++++- tools/lib/bpf/btf.h | 2 ++ tools/lib/bpf/libbpf.c | 8 ++++---- tools/lib/bpf/libbpf.map | 2 ++ 4 files changed, 31 insertions(+), 5 deletions(-)tools/lib/bpf/btf.c +6
-1@@ -4021,7 +4021,7 @@ static void btf_dedup_merge_hypot_map(struct btf_dedup *d)*/ if (d->hypot_adjust_canon)continue ; - +if (t_kind ==BTF_KIND_FWD && c_kind !=BTF_KIND_FWD ) d->map[t_id] = c_id;@@ -4395,6 +4395,11 @@ static int btf_dedup_remap_types(struct btf_dedup *d)* data out of it to use for target BTF. */ struct btf *libbpf_find_kernel_btf (void ) +{ +return libbpf_load_vmlinux_btf (); +} + +struct btf *libbpf_load_vmlinux_btf (void ) {struct {const char *path_fmt;
tools/lib/bpf/btf.c struct btf *libbpf_find_kernel_btf(void) · line 4448
Quoted 4 lines
tools/lib/bpf/btf.c +1
-0@@ -4440,6 +4445,23 @@ struct btf *libbpf_find_kernel_btf(void)return libbpf_err_ptr (-ESRCH ); } +struct btf *libbpf_load_module_btf (const char *mod)
So we probably need to allow user to pre-load and re-use vmlinux BTF for efficiency, especially if they have some use-case to load a lot of BTFs.
tools/lib/bpf/btf.c struct btf *libbpf_find_kernel_btf(void) · line 4458
Quoted 10 lines
tools/lib/bpf/btf.c +10
-0+{ +char path[80 ]; +struct btf *base; +int err; + + base =libbpf_load_vmlinux_btf (); + err =libbpf_get_error (base); +if (err) { +pr_warn ("Error loading vmlinux BTF: %d\n" , err); +return base;
libbpf_err_ptr() needs to be used here, pr_warn() could have destroyed errno already
tools/lib/bpf/btf.c struct btf *libbpf_find_kernel_btf(void) · line 4462
Quoted 4 lines
tools/lib/bpf/btf.c +4
-0+ } + +snprintf (path,sizeof (path),"/sys/kernel/btf/%s" , mod); +return btf__parse_split (path, base);
so who's freeing base BTF in this case?
Quoted 5 lines
tools/lib/bpf/btf.c +2
-0+} +int btf_type_visit_type_ids (struct btf_type *t,type_id_visit_fn visit,void *ctx) {int i, n, err;
[...]
Quoted 11 lines · Andrii Nakryiko
Quoted 5 lines · Hengqi Chen
Add libbpf_load_vmlinux_btf/libbpf_load_module_btf APIs. This is part of the libbpf v1.0. [1]
Saying it's part of libbpf 1.0 effort and given a link to Github PR is not really a sufficient commit message. Please expand on what you are doing in the patch and why.
Will do.
Quoted 38 lines
Quoted 38 lines
Signed-off-by Hengqi Chen <[email protected]>--- tools/lib/bpf/btf.c | 24 +++++++++++++++++++++++- tools/lib/bpf/btf.h | 2 ++ tools/lib/bpf/libbpf.c | 8 ++++---- tools/lib/bpf/libbpf.map | 2 ++ 4 files changed, 31 insertions(+), 5 deletions(-)tools/lib/bpf/btf.c +6
-1@@ -4021,7 +4021,7 @@ static void btf_dedup_merge_hypot_map(struct btf_dedup *d)*/ if (d->hypot_adjust_canon)continue ; - +if (t_kind ==BTF_KIND_FWD && c_kind !=BTF_KIND_FWD ) d->map[t_id] = c_id;@@ -4395,6 +4395,11 @@ static int btf_dedup_remap_types(struct btf_dedup *d)* data out of it to use for target BTF. */ struct btf *libbpf_find_kernel_btf (void ) +{ +return libbpf_load_vmlinux_btf (); +} + +struct btf *libbpf_load_vmlinux_btf (void ) {struct {const char *path_fmt;@@ -4440,6 +4445,23 @@ struct btf *libbpf_find_kernel_btf(void)return libbpf_err_ptr (-ESRCH ); }
Quoted 6 lines
tools/lib/bpf/btf.c struct btf *libbpf_find_kernel_btf(void) · line 4448
Quoted 1 line
tools/lib/bpf/btf.c +1
-0+struct btf *libbpf_load_module_btf (const char *mod)So we probably need to allow user to pre-load and re-use vmlinux BTF for efficiency, especially if they have some use-case to load a lot of BTFs.
Should the API change to this ?
struct btf *libbpf_load_module_btf(struct btf *base, const char *mod)
It seems better for the use-case you mentioned.
Quoted 5 lines
Quoted 5 lines
tools/lib/bpf/btf.c +5
-0+{ +char path[80 ]; +struct btf *base; +int err; +
Quoted 9 lines
tools/lib/bpf/btf.c struct btf *libbpf_find_kernel_btf(void) · line 4458
Quoted 5 lines
tools/lib/bpf/btf.c +5
-0+ base =libbpf_load_vmlinux_btf (); + err =libbpf_get_error (base); +if (err) { +pr_warn ("Error loading vmlinux BTF: %d\n" , err); +return base;
libbpf_err_ptr()needs to be used here,pr_warn()could have destroyed errno already
OK.
Quoted 7 lines
tools/lib/bpf/btf.c struct btf *libbpf_find_kernel_btf(void) · line 4462
Quoted 4 lines
tools/lib/bpf/btf.c +4
-0+ } + +snprintf (path,sizeof (path),"/sys/kernel/btf/%s" , mod); +return btf__parse_split (path, base);so who's freeing base BTF in this case?
Sorry, missed that.
But if we change the signature, then leave this to user.
Quoted 8 lines
Quoted 5 lines
tools/lib/bpf/btf.c +2
-0+} +int btf_type_visit_type_ids (struct btf_type *t,type_id_visit_fn visit,void *ctx) {int i, n, err;[...]
Quoted 63 lines · Hengqi Chen
Quoted 11 lines · Andrii Nakryiko
Quoted 5 lines · Hengqi Chen
Add libbpf_load_vmlinux_btf/libbpf_load_module_btf APIs. This is part of the libbpf v1.0. [1]
Saying it's part of libbpf 1.0 effort and given a link to Github PR is not really a sufficient commit message. Please expand on what you are doing in the patch and why.
Will do.
Quoted 38 lines
Quoted 38 lines
Signed-off-by Hengqi Chen <[email protected]>--- tools/lib/bpf/btf.c | 24 +++++++++++++++++++++++- tools/lib/bpf/btf.h | 2 ++ tools/lib/bpf/libbpf.c | 8 ++++---- tools/lib/bpf/libbpf.map | 2 ++ 4 files changed, 31 insertions(+), 5 deletions(-)tools/lib/bpf/btf.c +6
-1@@ -4021,7 +4021,7 @@ static void btf_dedup_merge_hypot_map(struct btf_dedup *d)*/ if (d->hypot_adjust_canon)continue ; - +if (t_kind ==BTF_KIND_FWD && c_kind !=BTF_KIND_FWD ) d->map[t_id] = c_id;@@ -4395,6 +4395,11 @@ static int btf_dedup_remap_types(struct btf_dedup *d)* data out of it to use for target BTF. */ struct btf *libbpf_find_kernel_btf (void ) +{ +return libbpf_load_vmlinux_btf (); +} + +struct btf *libbpf_load_vmlinux_btf (void ) {struct {const char *path_fmt;@@ -4440,6 +4445,23 @@ struct btf *libbpf_find_kernel_btf(void)return libbpf_err_ptr (-ESRCH ); }Quoted 6 lines
tools/lib/bpf/btf.c struct btf *libbpf_find_kernel_btf(void) · line 4448
Quoted 1 line
tools/lib/bpf/btf.c +1
-0+struct btf *libbpf_load_module_btf (const char *mod)So we probably need to allow user to pre-load and re-use vmlinux BTF for efficiency, especially if they have some use-case to load a lot of BTFs.
Quoted 5 lines
Should the API change to this ?
struct btf *libbpf_load_module_btf(struct btf *base, const char *mod)
It seems better for the use-case you mentioned.
Something like this, yeah. Let's put struct btf * as the last argument and module name as a first one, to follow the btf__parse_split() convention of having base_btf the last. And maybe let's call base a "vmlinux_btf", as in this case it's pretty specific that it's supposed to be the vmlinux BTF, not just any random BTF.
Quoted 23 lines
Quoted 15 lines
tools/lib/bpf/btf.c struct btf *libbpf_find_kernel_btf(void) · line 4458
Quoted 11 lines
tools/lib/bpf/btf.c +10
-0+{ +char path[80 ]; +struct btf *base; +int err; + + base =libbpf_load_vmlinux_btf (); + err =libbpf_get_error (base); +if (err) { +pr_warn ("Error loading vmlinux BTF: %d\n" , err); +return base;
libbpf_err_ptr()needs to be used here,pr_warn()could have destroyed errno alreadyOK.
Quoted 5 lines
Quoted 4 lines
tools/lib/bpf/btf.c +4
-0+ } + +snprintf (path,sizeof (path),"/sys/kernel/btf/%s" , mod); +return btf__parse_split (path, base);
Quoted 5 lines
Quoted 2 lines
so who's freeing base BTF in this case?
Sorry, missed that.
But if we change the signature, then leave this to user.
Right, that's what I was getting at. If we make vmlinux BTF non-optional, then user will have to own freeing it, just like we do that for other APIs w/ base BTF.
Quoted 9 lines
Quoted 9 lines
Quoted 6 lines
tools/lib/bpf/btf.c +2
-0+} +int btf_type_visit_type_ids (struct btf_type *t,type_id_visit_fn visit,void *ctx) {int i, n, err;[...]