tools/btf: extends libbpf APIs to work with btf w/o kernel
Subsystems (4)
cover tools/btf: extends libbpf APIs to work with btf w/o kernel
This patchset changes existing btf__new() API call to only load and initialize struct btf, while exposing new btf__load() API to attempt to load and validate BTF in kernel. It also adds ability to copy raw BTF data out of struct btf for further processing by external applications.
This makes utilizing libbpf's APIs that don't require kernel facilities (e.g., btf_dedup) simpler and more natural from external application.
Andrii Nakryiko (2):
btf: separate btf creation and loading
btf: expose API to work with raw btf data
tools/lib/bpf/btf.c | 63 +++++++++++++++++++++++++--------------- tools/lib/bpf/btf.h | 3 ++ tools/lib/bpf/libbpf.c | 2 +- tools/lib/bpf/libbpf.map | 3 ++ 4 files changed, 46 insertions(+), 25 deletions(-)
Signature · 1 line
2.17.1
PATCH 2/2 btf: expose API to work with raw btf data
This patch exposes two new APIs btf__get_raw_data_size() and btf__get_raw_data() that allows to get a copy of raw BTF data out of struct btf. This is useful for external programs that need to manipulate raw data, e.g., pahole using btf__dedup() to deduplicate BTF type info and then writing it back to file.
--- tools/lib/bpf/btf.c | 10 ++++++++++ tools/lib/bpf/btf.h | 2 ++ tools/lib/bpf/libbpf.map | 2 ++ 3 files changed, 14 insertions(+)
tools/lib/bpf/btf.c +10 -0
@@ -437,6 +437,16 @@ int btf__fd(const struct btf *btf)return btf->fd; } +__u32 btf__get_raw_data_size (const struct btf *btf) +{ +return btf->data_size; +} + +void btf__get_raw_data (const struct btf *btf,char *data) +{ +memcpy (data, btf->data, btf->data_size); +} +void btf__get_strings (const struct btf *btf,const char **strings,__u32 *str_len) {
tools/lib/bpf/btf.h +2 -0
@@ -66,6 +66,8 @@ LIBBPF_API const struct btf_type *btf__type_by_id(const struct btf *btf, LIBBPF_API __s64 btf__resolve_size(const struct btf *btf, __u32 type_id); LIBBPF_API int btf__resolve_type(const struct btf *btf, __u32 type_id); LIBBPF_API int btf__fd(const struct btf *btf); +LIBBPF_API __u32 btf__get_raw_data_size(const struct btf *btf); +LIBBPF_API void btf__get_raw_data(const struct btf *btf, char *data); LIBBPF_API void btf__get_strings(const struct btf *btf, const char **strings,__u32 *str_len); LIBBPF_API const char *btf__name_by_offset(const struct btf *btf, __u32 offset);
tools/lib/bpf/libbpf.map +2 -0
@@ -137,6 +137,8 @@ LIBBPF_0.0.2 { btf__load; btf__get_map_kv_tids; btf__get_nr_types; + btf__get_raw_data; + btf__get_raw_data_size; btf__get_strings; btf_ext__free; btf_ext__func_info_rec_size;
Signature · 1 line
2.17.1
PATCH 1/2 btf: separate btf creation and loading
In reply to Andrii NakryikoThis change splits out previous btf__new functionality of constructing struct btf and loading it into kernel into two:
- btf__new() just creates and initializes struct btf
- btf__load() attempts to load existing struct btf into kernel
btf__free will still close BTF fd, if it was ever loaded successfully into kernel.
This change allows users of libbpf to manipulate BTF using its API, without the need to unnecessarily load it into kernel.
One of the intended use cases is pahole using libbpf to do DWARF to BTF conversion and deduplication using libbpf, while handling ELF sections overwrites and other concerns on its own.
--- tools/lib/bpf/btf.c | 53 ++++++++++++++++++++++------------------ tools/lib/bpf/btf.h | 1 + tools/lib/bpf/libbpf.c | 2 +- tools/lib/bpf/libbpf.map | 1 + 4 files changed, 32 insertions(+), 25 deletions(-)
tools/lib/bpf/btf.c +29 -24
@@ -366,8 +366,6 @@ void btf__free(struct btf *btf)struct btf *btf__new (__u8 *data,__u32 size) { -__u32 log_buf_size =0 ; -char *log_buf =NULL ;struct btf *btf;int err;
@@ -377,15 +375,6 @@ struct btf *btf__new(__u8 *data, __u32 size) btf->fd =-1 ; - log_buf =malloc (BPF_LOG_BUF_SIZE ); -if (!log_buf) { - err = -ENOMEM ; - goto done; - } - - *log_buf =0 ; - log_buf_size =BPF_LOG_BUF_SIZE ; - btf->data =malloc (size);if (!btf->data) { err = -ENOMEM ;
@@ -395,17 +384,6 @@ struct btf *btf__new(__u8 *data, __u32 size)memcpy (btf->data, data, size); btf->data_size = size; - btf->fd =bpf_load_btf (btf->data, btf->data_size, - log_buf, log_buf_size, false); - -if (btf->fd ==-1 ) { - err = -errno; -pr_warning ("Error loading BTF: %s(%d)\n" ,strerror (errno), errno); -if (log_buf && *log_buf) -pr_warning ("%s\n" , log_buf); - goto done; - } - err =btf_parse_hdr (btf);if (err) goto done;
@@ -417,8 +395,6 @@ struct btf *btf__new(__u8 *data, __u32 size) err =btf_parse_type_sec (btf); done: -free (log_buf); -if (err) {btf__free (btf);return ERR_PTR (err);
@@ -427,6 +403,35 @@ struct btf *btf__new(__u8 *data, __u32 size)return btf; } +int btf__load (struct btf * btf) { +__u32 log_buf_size =BPF_LOG_BUF_SIZE ; +char *log_buf =NULL ; + +if (btf->fd >=0 ) { +return -EEXIST ; + } + + log_buf =malloc (log_buf_size); +if (!log_buf) +return -ENOMEM ; + + *log_buf =0 ; + + btf->fd =bpf_load_btf (btf->data, btf->data_size, + log_buf, log_buf_size, false); +if (btf->fd <0 ) { + btf->fd = -errno; +pr_warning ("Error loading BTF: %s(%d)\n" ,strerror (errno), errno); +if (*log_buf) +pr_warning ("%s\n" , log_buf); + goto done; + } + +done: +free (log_buf); +return btf->fd; +} +int btf__fd (const struct btf *btf) {return btf->fd;
tools/lib/bpf/btf.h +1 -0
@@ -57,6 +57,7 @@ struct btf_ext_header { LIBBPF_API void btf__free(struct btf *btf); LIBBPF_API struct btf *btf__new(__u8 *data, __u32 size); +LIBBPF_API int btf__load(struct btf* btf); LIBBPF_API __s32 btf__find_by_name(const struct btf *btf,const char *type_name); LIBBPF_API __u32 btf__get_nr_types(const struct btf *btf);
tools/lib/bpf/libbpf.c +1 -1
@@ -835,7 +835,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj, int flags) obj->efile.maps_shndx = idx; else if (strcmp(name, BTF_ELF_SEC) == 0) { obj->btf =btf__new (data->d_buf, data->d_size); -if (IS_ERR (obj->btf)) { +if (IS_ERR (obj->btf) ||btf__load (obj->btf) <0 ) {pr_warning ("Error loading ELF section %s: %ld. Ignored and continue.\n" ,BTF_ELF_SEC ,PTR_ERR (obj->btf)); obj->btf =NULL ;
tools/lib/bpf/libbpf.map +1 -0
@@ -134,6 +134,7 @@ LIBBPF_0.0.2 { bpf_object__find_map_fd_by_name; bpf_get_link_xdp_id; btf__dedup; + btf__load; btf__get_map_kv_tids; btf__get_nr_types; btf__get_strings;
Signature · 1 line
2.17.1
Re: [PATCH bpf-next 1/2] btf: separate btf creation and loading
Quoted 92 lines · Andrii Nakryiko
This change splits out previous
btf__newfunctionality of constructing struct btf and loading it into kernel into two:
-btf__new()just creates and initializes struct btf
-btf__load()attempts to load existing struct btf into kernel
btf__freewill still close BTF fd, if it was ever loaded successfully into kernel.This change allows users of libbpf to manipulate BTF using its API, without the need to unnecessarily load it into kernel.
One of the intended use cases is pahole using libbpf to do DWARF to BTF conversion and deduplication using libbpf, while handling ELF sections overwrites and other concerns on its own.
Signed-off-by Andrii Nakryiko <[email protected]>Acked-by Song Liu <[email protected]>--- tools/lib/bpf/btf.c | 53 ++++++++++++++++++++++------------------ tools/lib/bpf/btf.h | 1 + tools/lib/bpf/libbpf.c | 2 +- tools/lib/bpf/libbpf.map | 1 + 4 files changed, 32 insertions(+), 25 deletions(-)tools/lib/bpf/btf.c +8
-24@@ -366,8 +366,6 @@ void btf__free(struct btf *btf)struct btf *btf__new (__u8 *data,__u32 size) { -__u32 log_buf_size =0 ; -char *log_buf =NULL ;struct btf *btf;int err;@@ -377,15 +375,6 @@ struct btf *btf__new(__u8 *data, __u32 size) btf->fd =-1 ; - log_buf =malloc (BPF_LOG_BUF_SIZE ); -if (!log_buf) { - err = -ENOMEM ; - goto done; - } - - *log_buf =0 ; - log_buf_size =BPF_LOG_BUF_SIZE ; - btf->data =malloc (size);if (!btf->data) { err = -ENOMEM ;@@ -395,17 +384,6 @@ struct btf *btf__new(__u8 *data, __u32 size)memcpy (btf->data, data, size); btf->data_size = size; - btf->fd =bpf_load_btf (btf->data, btf->data_size, - log_buf, log_buf_size, false); - -if (btf->fd ==-1 ) { - err = -errno; -pr_warning ("Error loading BTF: %s(%d)\n" ,strerror (errno), errno); -if (log_buf && *log_buf) -pr_warning ("%s\n" , log_buf); - goto done; - } - err =btf_parse_hdr (btf);if (err) goto done;@@ -417,8 +395,6 @@ struct btf *btf__new(__u8 *data, __u32 size) err =btf_parse_type_sec (btf); done: -free (log_buf); -if (err) {btf__free (btf);return ERR_PTR (err);@@ -427,6 +403,35 @@ struct btf *btf__new(__u8 *data, __u32 size)return btf; } +int btf__load (struct btf * btf) { +__u32 log_buf_size =BPF_LOG_BUF_SIZE ; +char *log_buf =NULL ; + +if (btf->fd >=0 ) { +return -EEXIST ; + } +
tools/lib/bpf/btf.c struct btf *btf__new(__u8 *data, __u32 size) · line 423
Quoted 10 lines
tools/lib/bpf/btf.c +10
-0+ log_buf =malloc (log_buf_size); +if (!log_buf) +return -ENOMEM ; + + *log_buf =0 ; + + btf->fd =bpf_load_btf (btf->data, btf->data_size, + log_buf, log_buf_size, false); +if (btf->fd <0 ) { + btf->fd = -errno;
Why you set btf->fd = -errno? Do you have any intended use for it later. If not, I would still prefer the existing value -1. This will be consistent with all other fd field convention in libbpf.
Quoted 44 lines
tools/lib/bpf/btf.c +11
-0+pr_warning ("Error loading BTF: %s(%d)\n" ,strerror (errno), errno); +if (*log_buf) +pr_warning ("%s\n" , log_buf); + goto done; + } + +done: +free (log_buf); +return btf->fd; +} +int btf__fd (const struct btf *btf) {return btf->fd;tools/lib/bpf/btf.h +1
-0@@ -57,6 +57,7 @@ struct btf_ext_header { LIBBPF_API void btf__free(struct btf *btf); LIBBPF_API struct btf *btf__new(__u8 *data, __u32 size); +LIBBPF_API int btf__load(struct btf* btf); LIBBPF_API __s32 btf__find_by_name(const struct btf *btf,const char *type_name); LIBBPF_API __u32 btf__get_nr_types(const struct btf *btf);tools/lib/bpf/libbpf.c +1
-1@@ -835,7 +835,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj, int flags) obj->efile.maps_shndx = idx; else if (strcmp(name, BTF_ELF_SEC) == 0) { obj->btf =btf__new (data->d_buf, data->d_size); -if (IS_ERR (obj->btf)) { +if (IS_ERR (obj->btf) ||btf__load (obj->btf) <0 ) {pr_warning ("Error loading ELF section %s: %ld. Ignored and continue.\n" ,BTF_ELF_SEC ,PTR_ERR (obj->btf)); obj->btf =NULL ;tools/lib/bpf/libbpf.map +0
-0
tools/lib/bpf/libbpf.map LIBBPF_0.0.2 { · line 137
Quoted 4 lines
tools/lib/bpf/libbpf.map +1
-0@@ -134,6 +134,7 @@ LIBBPF_0.0.2 { bpf_object__find_map_fd_by_name; bpf_get_link_xdp_id; btf__dedup; + btf__load;
Maybe put btf__load after btf__get_strings based on alphabetical order. With the above changes,
Quoted 4 lines
tools/lib/bpf/libbpf.map +0
-0btf__get_map_kv_tids; btf__get_nr_types; btf__get_strings;
Re: [PATCH bpf-next 2/2] btf: expose API to work with raw btf data
In reply to Andrii NakryikoQuoted 8 lines · Andrii Nakryiko
This patch exposes two new APIs
btf__get_raw_data_size()andbtf__get_raw_data()that allows to get a copy of raw BTF data out of struct btf. This is useful for external programs that need to manipulate raw data, e.g., pahole usingbtf__dedup()to deduplicate BTF type info and then writing it back to file.Signed-off-by Andrii Nakryiko <[email protected]>Acked-by Song Liu <[email protected]>
Quoted 54 lines
--- tools/lib/bpf/btf.c | 10 ++++++++++ tools/lib/bpf/btf.h | 2 ++ tools/lib/bpf/libbpf.map | 2 ++ 3 files changed, 14 insertions(+)tools/lib/bpf/btf.c +10
-0@@ -437,6 +437,16 @@ int btf__fd(const struct btf *btf)return btf->fd; } +__u32 btf__get_raw_data_size (const struct btf *btf) +{ +return btf->data_size; +} + +void btf__get_raw_data (const struct btf *btf,char *data) +{ +memcpy (data, btf->data, btf->data_size); +} +void btf__get_strings (const struct btf *btf,const char **strings,__u32 *str_len) {tools/lib/bpf/btf.h +2
-0@@ -66,6 +66,8 @@ LIBBPF_API const struct btf_type *btf__type_by_id(const struct btf *btf, LIBBPF_API __s64 btf__resolve_size(const struct btf *btf, __u32 type_id); LIBBPF_API int btf__resolve_type(const struct btf *btf, __u32 type_id); LIBBPF_API int btf__fd(const struct btf *btf); +LIBBPF_API __u32 btf__get_raw_data_size(const struct btf *btf); +LIBBPF_API void btf__get_raw_data(const struct btf *btf, char *data); LIBBPF_API void btf__get_strings(const struct btf *btf, const char **strings,__u32 *str_len); LIBBPF_API const char *btf__name_by_offset(const struct btf *btf, __u32 offset);tools/lib/bpf/libbpf.map +2
-0@@ -137,6 +137,8 @@ LIBBPF_0.0.2 { btf__load; btf__get_map_kv_tids; btf__get_nr_types; + btf__get_raw_data; + btf__get_raw_data_size; btf__get_strings; btf_ext__free; btf_ext__func_info_rec_size;
Re: [PATCH bpf-next 1/2] btf: separate btf creation and loading
In reply to Yonghong SongQuoted 102 lines · Yonghong Song
Quoted 98 lines · Andrii Nakryiko
This change splits out previous
btf__newfunctionality of constructing struct btf and loading it into kernel into two:
-btf__new()just creates and initializes struct btf
-btf__load()attempts to load existing struct btf into kernel
btf__freewill still close BTF fd, if it was ever loaded successfully into kernel.This change allows users of libbpf to manipulate BTF using its API, without the need to unnecessarily load it into kernel.
One of the intended use cases is pahole using libbpf to do DWARF to BTF conversion and deduplication using libbpf, while handling ELF sections overwrites and other concerns on its own.
Signed-off-by Andrii Nakryiko <[email protected]>Acked-by Song Liu <[email protected]>--- tools/lib/bpf/btf.c | 53 ++++++++++++++++++++++------------------ tools/lib/bpf/btf.h | 1 + tools/lib/bpf/libbpf.c | 2 +- tools/lib/bpf/libbpf.map | 1 + 4 files changed, 32 insertions(+), 25 deletions(-)tools/lib/bpf/btf.c +14
-24@@ -366,8 +366,6 @@ void btf__free(struct btf *btf)struct btf *btf__new (__u8 *data,__u32 size) { -__u32 log_buf_size =0 ; -char *log_buf =NULL ;struct btf *btf;int err;@@ -377,15 +375,6 @@ struct btf *btf__new(__u8 *data, __u32 size) btf->fd =-1 ; - log_buf =malloc (BPF_LOG_BUF_SIZE ); -if (!log_buf) { - err = -ENOMEM ; - goto done; - } - - *log_buf =0 ; - log_buf_size =BPF_LOG_BUF_SIZE ; - btf->data =malloc (size);if (!btf->data) { err = -ENOMEM ;@@ -395,17 +384,6 @@ struct btf *btf__new(__u8 *data, __u32 size)memcpy (btf->data, data, size); btf->data_size = size; - btf->fd =bpf_load_btf (btf->data, btf->data_size, - log_buf, log_buf_size, false); - -if (btf->fd ==-1 ) { - err = -errno; -pr_warning ("Error loading BTF: %s(%d)\n" ,strerror (errno), errno); -if (log_buf && *log_buf) -pr_warning ("%s\n" , log_buf); - goto done; - } - err =btf_parse_hdr (btf);if (err) goto done;@@ -417,8 +395,6 @@ struct btf *btf__new(__u8 *data, __u32 size) err =btf_parse_type_sec (btf); done: -free (log_buf); -if (err) {btf__free (btf);return ERR_PTR (err);@@ -427,6 +403,35 @@ struct btf *btf__new(__u8 *data, __u32 size)return btf; } +int btf__load (struct btf * btf) { +__u32 log_buf_size =BPF_LOG_BUF_SIZE ; +char *log_buf =NULL ; + +if (btf->fd >=0 ) { +return -EEXIST ; + } + + log_buf =malloc (log_buf_size); +if (!log_buf) +return -ENOMEM ; + + *log_buf =0 ; +
Quoted 8 lines
tools/lib/bpf/btf.c struct btf *btf__new(__u8 *data, __u32 size) · line 423
Quoted 4 lines
tools/lib/bpf/btf.c +4
-0+ btf->fd =bpf_load_btf (btf->data, btf->data_size, + log_buf, log_buf_size, false); +if (btf->fd <0 ) { + btf->fd = -errno;Why you set
btf->fd= -errno? Do you have any intended use for it later. If not, I would still prefer the existing value -1. This will be consistent with all other fd field convention in libbpf.
I though it would be convenient to get created fd back from btf_load(), but I don't have strong preferences. In any case, we have btf__get_fd() to get it back, so I will change btf__load() to return just error code.
Quoted 44 lines
Quoted 44 lines
tools/lib/bpf/btf.c +11
-0+pr_warning ("Error loading BTF: %s(%d)\n" ,strerror (errno), errno); +if (*log_buf) +pr_warning ("%s\n" , log_buf); + goto done; + } + +done: +free (log_buf); +return btf->fd; +} +int btf__fd (const struct btf *btf) {return btf->fd;tools/lib/bpf/btf.h +1
-0@@ -57,6 +57,7 @@ struct btf_ext_header { LIBBPF_API void btf__free(struct btf *btf); LIBBPF_API struct btf *btf__new(__u8 *data, __u32 size); +LIBBPF_API int btf__load(struct btf* btf); LIBBPF_API __s32 btf__find_by_name(const struct btf *btf,const char *type_name); LIBBPF_API __u32 btf__get_nr_types(const struct btf *btf);tools/lib/bpf/libbpf.c +1
-1@@ -835,7 +835,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj, int flags) obj->efile.maps_shndx = idx; else if (strcmp(name, BTF_ELF_SEC) == 0) { obj->btf =btf__new (data->d_buf, data->d_size); -if (IS_ERR (obj->btf)) { +if (IS_ERR (obj->btf) ||btf__load (obj->btf) <0 ) {pr_warning ("Error loading ELF section %s: %ld. Ignored and continue.\n" ,BTF_ELF_SEC ,PTR_ERR (obj->btf)); obj->btf =NULL ;tools/lib/bpf/libbpf.map +0
-0
Quoted 8 lines
tools/lib/bpf/libbpf.map LIBBPF_0.0.2 { · line 137
Quoted 5 lines
tools/lib/bpf/libbpf.map +1
-0@@ -134,6 +134,7 @@ LIBBPF_0.0.2 { bpf_object__find_map_fd_by_name; bpf_get_link_xdp_id; btf__dedup; + btf__load;Maybe put
btf__loadafterbtf__get_stringsbased on alphabetical order. With the above changes,
Yep, will fix to be alphabetically sorted.
Quoted 6 lines
Quoted 6 lines
tools/lib/bpf/libbpf.map +0
-0btf__get_map_kv_tids; btf__get_nr_types; btf__get_strings;