libbpf: deprecate bpf_program__get_prog_info_linear
Subsystems (4)
v2 cover libbpf: deprecate bpf_program__get_prog_info_linear
bpf_program__get_prog_info_linear is a helper which wraps the bpf_obj_get_info_by_fd BPF syscall with some niceties that put all dynamic-length bpf_prog_info in one buffer contiguous with struct bpf_prog_info, and simplify the selection of which dynamic data to grab.
The resultant combined struct, bpf_prog_info_linear, is persisted to file by 'perf' to enable later annotation of BPF prog data. libbpf includes some vaddr <-> offset conversion helpers for struct bpf_prog_info_linear to simplify this.
This functionality is heavily tailored to perf's usecase, so its use as a general prog info API should be deemphasized in favor of just calling bpf_obj_get_info_by_fd, which can be more easily fit to purpose. Some examples from caller migrations in this series:
* Some callers weren't requesting or using dynamic-sized prog info and are well served by a simple get_info_by_fd call (e.g. dump_prog_id_as_func_ptr in bpftool)
* Some callers were requesting all of a specific dynamic info type but only using the first record, so can avoid unnecessary malloc by only requesting 1 (e.g. profile_target_name in bpftool)
* bpftool's do_dump saves some malloc/free by growing and reusing its dynamic prog_info buf as it loops over progs to grab info and dump.
Perf does need the full functionality of bpf_program__get_prog_info_linear and its accompanying structs + helpers, so copy the code to its codebase, migrate all other uses in the tree, and deprecate the helper in libbpf.
Since the deprecated symbols continue to be included in perf some renaming was necessary in perf's copy, otherwise functionality is unchanged.
This work was previously discussed in libbpf's issue tracker [0].
[0]: https://github.com/libbpf/libbpf/issues/313
v1->v2: fix bpftool do_dump changes to clear bpf_prog_info after use and correctly pass realloc'd ptr back (patch 2)
Dave Marchevsky (4):
libbpf: migrate internal use of bpf_program__get_prog_info_linear
bpftool: use bpf_obj_get_info_by_fd directly
perf: pull in bpf_program__get_prog_info_linear
libbpf: deprecate bpf_program__get_prog_info_linear
tools/bpf/bpftool/btf_dumper.c | 40 +-- tools/bpf/bpftool/prog.c | 154 ++++++++--- tools/lib/bpf/libbpf.c | 15 +- tools/lib/bpf/libbpf.h | 3 + .../Documentation/perf.data-file-format.txt | 2 +- tools/perf/util/Build | 1 + tools/perf/util/annotate.c | 3 +- tools/perf/util/bpf-event.c | 41 ++- tools/perf/util/bpf-event.h | 2 +- tools/perf/util/bpf-utils.c | 261 ++++++++++++++++++ tools/perf/util/bpf-utils.h | 76 +++++ tools/perf/util/bpf_counter.c | 6 +- tools/perf/util/dso.c | 1 + tools/perf/util/env.c | 1 + tools/perf/util/header.c | 13 +- 15 files changed, 527 insertions(+), 92 deletions(-) create mode 100644 tools/perf/util/bpf-utils.c create mode 100644 tools/perf/util/bpf-utils.h
Signature · 1 line
2.30.2
PATCH v2 1/4 libbpf: migrate internal use of bpf_program__get_prog_info_linear
In preparation for bpf_program__get_prog_info_linear, move the single use in libbpf to call bpf_obj_get_info_by_fd directly.
--- tools/lib/bpf/libbpf.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-)
tools/lib/bpf/libbpf.c +6 -9
@@ -8459,26 +8459,24 @@ int libbpf_find_vmlinux_btf_id(const char *name,static int libbpf_find_prog_btf_id (const char *name,__u32 attach_prog_fd) { -struct bpf_prog_info_linear *info_linear; -struct bpf_prog_info *info; +struct bpf_prog_info info = {}; +__u32 info_len =sizeof (info);struct btf *btf;int err; - info_linear =bpf_program__get_prog_info_linear (attach_prog_fd,0 ); - err =libbpf_get_error (info_linear); + err =bpf_obj_get_info_by_fd (attach_prog_fd, &info, &info_len);if (err) { -pr_warn ("failed get_prog_info_linear for FD %d\n" , +pr_warn ("failed bpf_obj_get_info_by_fd for FD %d\n" , attach_prog_fd);return err; } err = -EINVAL ; - info = &info_linear->info; -if (!info->btf_id) { +if (!info.btf_id) {pr_warn ("The target program doesn't have BTF\n" ); goto out; } - btf =btf__load_from_kernel_by_id (info->btf_id); + btf =btf__load_from_kernel_by_id (info.btf_id);if (libbpf_get_error (btf)) {pr_warn ("Failed to get BTF of the program\n" ); goto out;
@@ -8490,7 +8488,6 @@ static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd) goto out; } out: -free (info_linear);return err; }
Signature · 1 line
2.30.2
Re: [PATCH v2 bpf-next 1/4] libbpf: migrate internal use of bpf_program__get_prog_info_linear
Quoted 2 lines · Dave Marchevsky
In preparation for
bpf_program__get_prog_info_linear, move the single
for bpf_program__get_prog_info_linear *deprecation* ? fixed it up while applying
Quoted 4 lines
use in libbpf to call
bpf_obj_get_info_by_fddirectly.Signed-off-by Dave Marchevsky <[email protected]>---
I've applied this patch for now, as it is pretty independent (and I've already fixed it up already and didn't want to lose that :) I hope you don't mind.
Quoted 14 lines
tools/lib/bpf/libbpf.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-)tools/lib/bpf/libbpf.c +1
-2@@ -8459,26 +8459,24 @@ int libbpf_find_vmlinux_btf_id(const char *name,static int libbpf_find_prog_btf_id (const char *name,__u32 attach_prog_fd) { -struct bpf_prog_info_linear *info_linear; -struct bpf_prog_info *info; +struct bpf_prog_info info = {};
tools/lib/bpf/libbpf.c int libbpf_find_vmlinux_btf_id(const char *name, · line 8469
Quoted 10 lines
tools/lib/bpf/libbpf.c +3
-3+__u32 info_len =sizeof (info);struct btf *btf;int err; - info_linear =bpf_program__get_prog_info_linear (attach_prog_fd,0 ); - err =libbpf_get_error (info_linear); + err =bpf_obj_get_info_by_fd (attach_prog_fd, &info, &info_len);if (err) { - pr_warn("failed get_prog_info_linear for FD %d\n", + pr_warn("failed bpf_obj_get_info_by_fd for FD %d\n",
I've also added err code into warn message here
Quoted 5 lines
tools/lib/bpf/libbpf.c +0
-0attach_prog_fd);return err; } err = -EINVAL ;
tools/lib/bpf/libbpf.c int libbpf_find_vmlinux_btf_id(const char *name, · line 8481
Quoted 10 lines
tools/lib/bpf/libbpf.c +2
-3- info = &info_linear->info; -if (!info->btf_id) { +if (!info.btf_id) {pr_warn ("The target program doesn't have BTF\n" ); goto out; } - btf =btf__load_from_kernel_by_id (info->btf_id); + btf =btf__load_from_kernel_by_id (info.btf_id);if (libbpf_get_error (btf)) {pr_warn ("Failed to get BTF of the program\n" );
and here
Quoted 12 lines
tools/lib/bpf/libbpf.c +0
-2goto out;@@ -8490,7 +8488,6 @@ static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd) goto out; } out: -free (info_linear);return err; } --2.30.2
PATCH v2 2/4 bpftool: use bpf_obj_get_info_by_fd directly
In reply to Dave MarchevskyTo prepare for impending deprecation of libbpf's bpf_program__get_prog_info_linear, migrate uses of this function to use bpf_obj_get_info_by_fd.
Since the profile_target_name and dump_prog_id_as_func_ptr helpers were only looking at the first func_info, avoid grabbing the rest to save a malloc. For do_dump, add a more full-featured helper, but avoid free/realloc of buffer when possible for multi-prog dumps.
--- tools/bpf/bpftool/btf_dumper.c | 40 +++++---- tools/bpf/bpftool/prog.c | 154 +++++++++++++++++++++++++-------- 2 files changed, 144 insertions(+), 50 deletions(-)
tools/bpf/bpftool/btf_dumper.c +24 -16
@@ -32,14 +32,16 @@ static int dump_prog_id_as_func_ptr(const struct btf_dumper *d, const struct btf_type *func_proto, __u32 prog_id) { -struct bpf_prog_info_linear *prog_info =NULL ;const struct btf_type *func_type; +int prog_fd =-1 , func_sig_len; +struct bpf_prog_info info = {}; +__u32 info_len =sizeof (info);const char *prog_name =NULL ; -struct bpf_func_info *finfo;struct btf *prog_btf =NULL ; -struct bpf_prog_info *info; -int prog_fd, func_sig_len; +struct bpf_func_info finfo; +__u32 finfo_rec_size;char prog_str[1024 ]; +int err;/* Get the ptr's func_proto */ func_sig_len = btf_dump_func(d->btf, prog_str, func_proto, NULL, 0,
@@ -55,22 +57,27 @@ static int dump_prog_id_as_func_ptr(const struct btf_dumper *d,if (prog_fd ==-1 ) goto print; - prog_info =bpf_program__get_prog_info_linear (prog_fd, -1UL <<BPF_PROG_INFO_FUNC_INFO ); -close (prog_fd); -if (IS_ERR (prog_info)) { - prog_info =NULL ; + err =bpf_obj_get_info_by_fd (prog_fd, &info, &info_len); +if (err) goto print; - } - info = &prog_info->info; -if (!info->btf_id || !info->nr_func_info) +if (!info.btf_id || !info.nr_func_info) + goto print; + + finfo_rec_size = info.func_info_rec_size; +memset (&info,0 ,sizeof (info)); + info.nr_func_info =1 ; + info.func_info_rec_size = finfo_rec_size; + info.func_info =ptr_to_u64 (&finfo); + + err =bpf_obj_get_info_by_fd (prog_fd, &info, &info_len); +if (err) goto print; - prog_btf =btf__load_from_kernel_by_id (info->btf_id); + + prog_btf =btf__load_from_kernel_by_id (info.btf_id);if (libbpf_get_error (prog_btf)) goto print; - finfo =u64_to_ptr (info->func_info); - func_type =btf__type_by_id (prog_btf, finfo->type_id); + func_type =btf__type_by_id (prog_btf, finfo.type_id);if (!func_type || !btf_is_func (func_type)) goto print;
@@ -92,7 +99,8 @@ static int dump_prog_id_as_func_ptr(const struct btf_dumper *d, prog_str[sizeof (prog_str) -1 ] ='\0' ;jsonw_string (d->jw, prog_str);btf__free (prog_btf); -free (prog_info); +if (prog_fd !=-1 ) +close (prog_fd);return 0 ; }
tools/bpf/bpftool/prog.c +120 -34
235 lines not on this page · raw message
Signature · 1 line
2.30.2
Re: [PATCH v2 bpf-next 2/4] bpftool: use bpf_obj_get_info_by_fd directly
Quoted 42 lines · Dave Marchevsky
To prepare for impending deprecation of libbpf's
bpf_program__get_prog_info_linear, migrate uses of this function to usebpf_obj_get_info_by_fd.Since the
profile_target_nameanddump_prog_id_as_func_ptrhelpers were only looking at the firstfunc_info, avoid grabbing the rest to save a malloc. Fordo_dump, add a more full-featured helper, but avoid free/realloc of buffer when possible for multi-prog dumps.Signed-off-by Dave Marchevsky <[email protected]>--- tools/bpf/bpftool/btf_dumper.c | 40 +++++---- tools/bpf/bpftool/prog.c | 154 +++++++++++++++++++++++++-------- 2 files changed, 144 insertions(+), 50 deletions(-)tools/bpf/bpftool/btf_dumper.c +6
-4@@ -32,14 +32,16 @@ static int dump_prog_id_as_func_ptr(const struct btf_dumper *d, const struct btf_type *func_proto, __u32 prog_id) { -struct bpf_prog_info_linear *prog_info =NULL ;const struct btf_type *func_type; +int prog_fd =-1 , func_sig_len; +struct bpf_prog_info info = {}; +__u32 info_len =sizeof (info);const char *prog_name =NULL ; -struct bpf_func_info *finfo;struct btf *prog_btf =NULL ; -struct bpf_prog_info *info; -int prog_fd, func_sig_len; +struct bpf_func_info finfo; +__u32 finfo_rec_size;char prog_str[1024 ]; +int err;/* Get the ptr's func_proto */ func_sig_len = btf_dump_func(d->btf, prog_str, func_proto, NULL, 0,
tools/bpf/bpftool/btf_dumper.c static int dump_prog_id_as_func_ptr(const struct btf_dumper *d, · line 57
Quoted 1 line
tools/bpf/bpftool/btf_dumper.c +0
-0@@ -55,22 +57,27 @@ static int dump_prog_id_as_func_ptr(const struct btf_dumper *d,if (prog_fd ==-1 )
please change this to (prog_fd < 0), see [0] for why
we should check all the other places in bpftool to see if there are any patterns like this that would break on libbpf 1.0 (cc Quentin as well)
Quoted 38 lines
tools/bpf/bpftool/btf_dumper.c +16
-11goto print; - prog_info =bpf_program__get_prog_info_linear (prog_fd, -1UL <<BPF_PROG_INFO_FUNC_INFO ); -close (prog_fd); -if (IS_ERR (prog_info)) { - prog_info =NULL ; + err =bpf_obj_get_info_by_fd (prog_fd, &info, &info_len); +if (err) goto print; - } - info = &prog_info->info; -if (!info->btf_id || !info->nr_func_info) +if (!info.btf_id || !info.nr_func_info) + goto print; + + finfo_rec_size = info.func_info_rec_size; +memset (&info,0 ,sizeof (info)); + info.nr_func_info =1 ; + info.func_info_rec_size = finfo_rec_size; + info.func_info =ptr_to_u64 (&finfo); + + err =bpf_obj_get_info_by_fd (prog_fd, &info, &info_len); +if (err) goto print; - prog_btf =btf__load_from_kernel_by_id (info->btf_id); + + prog_btf =btf__load_from_kernel_by_id (info.btf_id);if (libbpf_get_error (prog_btf)) goto print; - finfo =u64_to_ptr (info->func_info); - func_type =btf__type_by_id (prog_btf, finfo->type_id); + func_type =btf__type_by_id (prog_btf, finfo.type_id);if (!func_type || !btf_is_func (func_type)) goto print;
tools/bpf/bpftool/btf_dumper.c static int dump_prog_id_as_func_ptr(const struct btf_dumper *d, · line 102
Quoted 5 lines
tools/bpf/bpftool/btf_dumper.c +1
-1@@ -92,7 +99,8 @@ static int dump_prog_id_as_func_ptr(const struct btf_dumper *d, prog_str[sizeof (prog_str) -1 ] ='\0' ;jsonw_string (d->jw, prog_str);btf__free (prog_btf); -free (prog_info); +if (prog_fd !=-1 )
similarly, prog_fd >= 0 here; also, isn't this a fix? Can you add
Fixes: tag then?
Quoted 41 lines
tools/bpf/bpftool/btf_dumper.c +1
-0+close (prog_fd);return 0 ; }tools/bpf/bpftool/prog.c +29
-0@@ -98,6 +98,72 @@ static enum bpf_attach_type parse_attach_type(const char *str)return __MAX_BPF_ATTACH_TYPE; } +#define holder_prep_needed_rec_sz (nr, rec_size)\ +({ \ + holder.nr = info->nr; \ + needed += holder.nr * rec_size; \ +}) + +#define holder_prep_needed (nr, rec_size) \ +({ \ + holder.nr = info->nr; \ + holder.rec_size = info->rec_size; \ + needed += holder.nr * holder.rec_size; \ +}) + +#define holder_set_ptr (field, nr, rec_size) \ +({ \ + holder.field = ptr_to_u64(ptr); \ + ptr += nr * rec_size; \ +}) + +static int prep_prog_info (struct bpf_prog_info *const info,enum dump_mode mode, +void **info_data,size_t *const info_data_sz) +{ +struct bpf_prog_info holder = {}; +size_t needed =0 ; +void *ptr; + +if (mode ==DUMP_JITED ) +holder_prep_needed_rec_sz (jited_prog_len,1 ); +else
tools/bpf/bpftool/prog.c static enum bpf_attach_type parse_attach_type(const char *str) · line 139
Quoted 10 lines
tools/bpf/bpftool/prog.c +10
-0+holder_prep_needed_rec_sz (xlated_prog_len,1 ); + +holder_prep_needed_rec_sz (nr_jited_ksyms,sizeof (__u64)); +holder_prep_needed_rec_sz (nr_jited_func_lens,sizeof (__u32)); +holder_prep_needed (nr_func_info, func_info_rec_size); +holder_prep_needed (nr_line_info, line_info_rec_size); +holder_prep_needed (nr_jited_line_info, jited_line_info_rec_size); + +if (needed > *info_data_sz) { + *info_data =realloc (*info_data, needed);
never do x = realloc(x);, if realloc fails, original memory is leaked. Temporary variable is necessary to work with realloc, see a bunch of other places in libbpf where we use realloc for an example.
Quoted 8 lines
tools/bpf/bpftool/prog.c +8
-0+if (!*info_data) +return -1 ; + *info_data_sz = needed; + } + + ptr = *info_data; + +if (mode ==DUMP_JITED )
tools/bpf/bpftool/prog.c static enum bpf_attach_type parse_attach_type(const char *str) · line 157
Quoted 10 lines
tools/bpf/bpftool/prog.c +10
-0+holder_set_ptr (jited_prog_insns, holder.jited_prog_len,1 ); + else + holder_set_ptr(xlated_prog_insns, holder.xlated_prog_len, 1); + +holder_set_ptr (jited_ksyms, holder.nr_jited_ksyms,sizeof (__u64)); +holder_set_ptr (jited_func_lens, holder.nr_jited_func_lens,sizeof (__u32)); +holder_set_ptr (func_info, holder.nr_func_info, holder.func_info_rec_size); +holder_set_ptr (line_info, holder.nr_line_info, holder.line_info_rec_size); +holder_set_ptr (jited_line_info, holder.nr_jited_line_info, + holder.jited_line_info_rec_size);
tbh, I completely lost track of what these holder_xxx() macro actually do here... You saved few lines of code, but I think we lost a lot in readability
tools/bpf/bpftool/prog.c static enum bpf_attach_type parse_attach_type(const char *str) · line 159
Quoted 2 lines
tools/bpf/bpftool/prog.c +2
-0+ + *info = holder;
instead of copying at the end, why not fill out the passed in bpf_prog_info directly? Of course *info stuff is annoying, but that's solved with a temporary variable, no?
Quoted 7 lines
tools/bpf/bpftool/prog.c +7
-0+return 0 ; +} + +#undef holder_prep_needed +#undef holder_prep_needed_rec_sz +#undef holder_set_ptr +
[...]
Re: [PATCH v2 bpf-next 2/4] bpftool: use bpf_obj_get_info_by_fd directly
Quoted 48 lines · Andrii Nakryiko
Quoted 42 lines · Dave Marchevsky
To prepare for impending deprecation of libbpf's
bpf_program__get_prog_info_linear, migrate uses of this function to usebpf_obj_get_info_by_fd.Since the
profile_target_nameanddump_prog_id_as_func_ptrhelpers were only looking at the firstfunc_info, avoid grabbing the rest to save a malloc. Fordo_dump, add a more full-featured helper, but avoid free/realloc of buffer when possible for multi-prog dumps.Signed-off-by Dave Marchevsky <[email protected]>--- tools/bpf/bpftool/btf_dumper.c | 40 +++++---- tools/bpf/bpftool/prog.c | 154 +++++++++++++++++++++++++-------- 2 files changed, 144 insertions(+), 50 deletions(-)tools/bpf/bpftool/btf_dumper.c +6
-4@@ -32,14 +32,16 @@ static int dump_prog_id_as_func_ptr(const struct btf_dumper *d, const struct btf_type *func_proto, __u32 prog_id) { -struct bpf_prog_info_linear *prog_info =NULL ;const struct btf_type *func_type; +int prog_fd =-1 , func_sig_len; +struct bpf_prog_info info = {}; +__u32 info_len =sizeof (info);const char *prog_name =NULL ; -struct bpf_func_info *finfo;struct btf *prog_btf =NULL ; -struct bpf_prog_info *info; -int prog_fd, func_sig_len; +struct bpf_func_info finfo; +__u32 finfo_rec_size;char prog_str[1024 ]; +int err;/* Get the ptr's func_proto */ func_sig_len = btf_dump_func(d->btf, prog_str, func_proto, NULL, 0,tools/bpf/bpftool/btf_dumper.c static int dump_prog_id_as_func_ptr(const struct btf_dumper *d, · line 57
Quoted 1 line
tools/bpf/bpftool/btf_dumper.c +0
-0@@ -55,22 +57,27 @@ static int dump_prog_id_as_func_ptr(const struct btf_dumper *d,if (prog_fd ==-1 )please change this to (
prog_fd< 0), see [0] for why
Quoted 5 lines
we should check all the other places in bpftool to see if there are any patterns like this that would break on libbpf 1.0 (cc Quentin as well)
Hi! Looking at bpftool's code (looking for "-1"), I could find only two occurrences of that pattern, one in btf_dumper.c as noted above, and another one in struct_ops.c:
fd = bpf_map_get_fd_by_id(id);
if (fd == -1) {
[...]
}Dave, are you willing to address it as well? I can send a patch later this week otherwise.
Thanks,
Quentin
PATCH v2 3/4 perf: pull in bpf_program__get_prog_info_linear
In reply to Dave MarchevskyTo prepare for impending deprecation of libbpf's bpf_program__get_prog_info_linear, pull in the function and associated helpers into the perf codebase and migrate existing uses to the perf copy.
Since libbpf's deprecated definitions will still be visible to perf, it is necessary to rename perf's definitions.
--- .../Documentation/perf.data-file-format.txt | 2 +- tools/perf/util/Build | 1 + tools/perf/util/annotate.c | 3 +- tools/perf/util/bpf-event.c | 41 ++- tools/perf/util/bpf-event.h | 2 +- tools/perf/util/bpf-utils.c | 261 ++++++++++++++++++ tools/perf/util/bpf-utils.h | 76 +++++ tools/perf/util/bpf_counter.c | 6 +- tools/perf/util/dso.c | 1 + tools/perf/util/env.c | 1 + tools/perf/util/header.c | 13 +- 11 files changed, 374 insertions(+), 33 deletions(-) create mode 100644 tools/perf/util/bpf-utils.c create mode 100644 tools/perf/util/bpf-utils.h
tools/perf/Documentation/perf.data-file-format.txt +1 -1
@@ -346,7 +346,7 @@ to special needs. HEADER_BPF_PROG_INFO = 25, -struct bpf_prog_info_linear, which contains detailed information about +struct perf_bpil, which contains detailed information about a BPF program, including type, id, tag, jited/xlated instructions, etc. HEADER_BPF_BTF = 26,
tools/perf/util/Build +1 -0
@@ -201,6 +201,7 @@ endif perf-y += perf-hooks.o perf-$(CONFIG_LIBBPF) += bpf-event.o +perf-$(CONFIG_LIBBPF) += bpf-utils.o perf-$(CONFIG_CXX) += c++/
tools/perf/util/annotate.c +2 -1
@@ -28,6 +28,7 @@#include "evsel.h" #include "evlist.h" #include "bpf-event.h" +#include "bpf-utils.h" #include "block-range.h" #include "string2.h" #include "util/event.h"
@@ -1700,12 +1701,12 @@ static int symbol__disassemble_bpf(struct symbol *sym, {struct annotation *notes =symbol__annotation (sym);struct annotation_options *opts = args->options; -struct bpf_prog_info_linear *info_linear;struct bpf_prog_linfo *prog_linfo =NULL ;struct bpf_prog_info_node *info_node;int len = sym->end - sym->start;disassembler_ftype disassemble;struct map *map = args->ms.map; +struct perf_bpil *info_linear;struct disassemble_info info;struct dso *dso = map->dso;int pc =0 , count, sub_id;
tools/perf/util/bpf-event.c +20 -21
90 lines not on this page · raw message
tools/perf/util/bpf-event.h +1 -1
@@ -19,7 +19,7 @@ struct evlist;struct target ;struct bpf_prog_info_node { -struct bpf_prog_info_linear *info_linear; +struct perf_bpil *info_linear;struct rb_node rb_node; };
tools/perf/util/bpf-utils.c new file +261 -0
261 lines not on this page · raw message
tools/perf/util/bpf-utils.h new file +76 -0
76 lines not on this page · raw message
tools/perf/util/bpf_counter.c +3 -3
@@ -13,6 +13,7 @@#include <perf/bpf_perf.h> #include "bpf_counter.h" +#include "bpf-utils.h" #include "counts.h" #include "debug.h" #include "evsel.h"
@@ -61,14 +62,13 @@ static int bpf_program_profiler__destroy(struct evsel *evsel)static char *bpf_target_prog_name (int tgt_fd) { -struct bpf_prog_info_linear *info_linear;struct bpf_func_info *func_info; +struct perf_bpil *info_linear;const struct btf_type *t;struct btf *btf =NULL ;char *name =NULL ; - info_linear =bpf_program__get_prog_info_linear ( - tgt_fd,1UL <<BPF_PROG_INFO_FUNC_INFO ); + info_linear =get_bpf_prog_info_linear (tgt_fd,1UL <<PERF_BPIL_FUNC_INFO );if (IS_ERR_OR_NULL (info_linear)) {pr_debug ("failed to get info_linear for prog FD %d\n" , tgt_fd);return NULL ;
tools/perf/util/dso.c +1 -0
@@ -14,6 +14,7 @@#ifdef HAVE_LIBBPF_SUPPORT #include <bpf/libbpf.h> #include "bpf-event.h" +#include "bpf-utils.h" #endif #include "compress.h" #include "env.h"
tools/perf/util/env.c +1 -0
@@ -16,6 +16,7 @@ struct perf_env perf_env;#ifdef HAVE_LIBBPF_SUPPORT #include "bpf-event.h" +#include "bpf-utils.h" #include <bpf/libbpf.h> void perf_env__insert_bpf_prog_info(struct perf_env *env,
tools/perf/util/header.c +7 -6
@@ -48,6 +48,7 @@#include "util/util.h" // perf_exe() #include "cputopo.h" #include "bpf-event.h" +#include "bpf-utils.h" #include "clockid.h" #include "pmu-hybrid.h"
@@ -1006,17 +1007,17 @@ static int write_bpf_prog_info(struct feat_fd *ff, node = rb_entry(next, struct bpf_prog_info_node, rb_node); next =rb_next (&node->rb_node); - len =sizeof (struct bpf_prog_info_linear ) + + len =sizeof (struct perf_bpil ) + node->info_linear->data_len;/* before writing to file, translate address to offset */ -bpf_program__bpil_addr_to_offs (node->info_linear); +bpil_addr_to_offs (node->info_linear); ret =do_write (ff, node->info_linear, len);/* * translate back to address even when do_write() fails, * so that this function never changes the data. */ -bpf_program__bpil_offs_to_addr (node->info_linear); +bpil_offs_to_addr (node->info_linear);if (ret <0 ) goto out; }
@@ -3018,9 +3019,9 @@ static int process_dir_format(struct feat_fd *ff,#ifdef HAVE_LIBBPF_SUPPORT static int process_bpf_prog_info (struct feat_fd *ff,void *data__maybe_unused ) { -struct bpf_prog_info_linear *info_linear;struct bpf_prog_info_node *info_node;struct perf_env *env = &ff->ph->env; +struct perf_bpil *info_linear;u32 count, i;int err =-1 ;
@@ -3049,7 +3050,7 @@ static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused) goto out; } - info_linear =malloc (sizeof (struct bpf_prog_info_linear ) + + info_linear =malloc (sizeof (struct perf_bpil ) + data_len);if (!info_linear) goto out;
@@ -3071,7 +3072,7 @@ static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused) goto out;/* after reading from file, translate offset to address */ -bpf_program__bpil_offs_to_addr (info_linear); +bpil_offs_to_addr (info_linear); info_node->info_linear = info_linear;perf_env__insert_bpf_prog_info (env, info_node); }
Signature · 1 line
2.30.2
Re: [PATCH v2 bpf-next 3/4] perf: pull in bpf_program__get_prog_info_linear
Quoted 11 lines · Dave Marchevsky
To prepare for impending deprecation of libbpf's
bpf_program__get_prog_info_linear, pull in the function and associated helpers into the perf codebase and migrate existing uses to the perf copy.Since libbpf's deprecated definitions will still be visible to perf, it is necessary to rename perf's definitions.
Signed-off-by Dave Marchevsky <[email protected]>---
LGTM, but, Song, can you please take a look as well?
Quoted 647 lines
.../Documentation/perf.data-file-format.txt | 2 +- tools/perf/util/Build | 1 + tools/perf/util/annotate.c | 3 +- tools/perf/util/bpf-event.c | 41 ++- tools/perf/util/bpf-event.h | 2 +- tools/perf/util/bpf-utils.c | 261 ++++++++++++++++++ tools/perf/util/bpf-utils.h | 76 +++++ tools/perf/util/bpf_counter.c | 6 +- tools/perf/util/dso.c | 1 + tools/perf/util/env.c | 1 + tools/perf/util/header.c | 13 +- 11 files changed, 374 insertions(+), 33 deletions(-) create mode 100644 tools/perf/util/bpf-utils.c create mode 100644 tools/perf/util/bpf-utils.htools/perf/Documentation/perf.data-file-format.txt +1
-1@@ -346,7 +346,7 @@ to special needs. HEADER_BPF_PROG_INFO = 25, -struct bpf_prog_info_linear, which contains detailed information about +struct perf_bpil, which contains detailed information about a BPF program, including type, id, tag, jited/xlated instructions, etc. HEADER_BPF_BTF = 26,tools/perf/util/Build +1
-0@@ -201,6 +201,7 @@ endif perf-y += perf-hooks.o perf-$(CONFIG_LIBBPF) += bpf-event.o +perf-$(CONFIG_LIBBPF) += bpf-utils.o perf-$(CONFIG_CXX) += c++/tools/perf/util/annotate.c +2
-1@@ -28,6 +28,7 @@#include "evsel.h" #include "evlist.h" #include "bpf-event.h" +#include "bpf-utils.h" #include "block-range.h" #include "string2.h" #include "util/event.h" @@ -1700,12 +1701,12 @@ static int symbol__disassemble_bpf(struct symbol *sym, {struct annotation *notes =symbol__annotation (sym);struct annotation_options *opts = args->options; -struct bpf_prog_info_linear *info_linear;struct bpf_prog_linfo *prog_linfo =NULL ;struct bpf_prog_info_node *info_node;int len = sym->end - sym->start;disassembler_ftype disassemble;struct map *map = args->ms.map; +struct perf_bpil *info_linear;struct disassemble_info info;struct dso *dso = map->dso;int pc =0 , count, sub_id;tools/perf/util/bpf-event.c +20
-21@@ -10,6 +10,7 @@#include <internal/lib.h> #include <symbol/kallsyms.h> #include "bpf-event.h" +#include "bpf-utils.h" #include "debug.h" #include "dso.h" #include "symbol.h" @@ -32,8 +33,6 @@ struct btf * __weak btf__load_from_kernel_by_id(__u32 id)return err ?ERR_PTR (err) : btf; } -#define ptr_to_u64 (ptr) ((__u64)(unsigned long)(ptr)) -static int snprintf_hex (char *buf,size_t size,unsigned char *data,size_t len) {int ret =0 ;@@ -48,9 +47,9 @@ static int machine__process_bpf_event_load(struct machine *machine,union perf_event *event, struct perf_sample *sample __maybe_unused) { - struct bpf_prog_info_linear *info_linear; struct bpf_prog_info_node *info_node;struct perf_env *env = machine->env; +struct perf_bpil *info_linear;int id = event->bpf.id;unsigned int i;@@ -175,9 +174,9 @@ static int perf_event__synthesize_one_bpf_prog(struct perf_session *session, {struct perf_record_ksymbol *ksymbol_event = &event->ksymbol;struct perf_record_bpf_event *bpf_event = &event->bpf; -struct bpf_prog_info_linear *info_linear;struct perf_tool *tool = session->tool;struct bpf_prog_info_node *info_node; +struct perf_bpil *info_linear;struct bpf_prog_info *info;struct btf *btf =NULL ;struct perf_env *env;@@ -191,15 +190,15 @@ static int perf_event__synthesize_one_bpf_prog(struct perf_session *session,*/ env = session->data ? &session->header.env : &perf_env; - arrays =1UL <<BPF_PROG_INFO_JITED_KSYMS ; - arrays |=1UL <<BPF_PROG_INFO_JITED_FUNC_LENS ; - arrays |=1UL <<BPF_PROG_INFO_FUNC_INFO ; - arrays |=1UL <<BPF_PROG_INFO_PROG_TAGS ; - arrays |=1UL <<BPF_PROG_INFO_JITED_INSNS ; - arrays |=1UL <<BPF_PROG_INFO_LINE_INFO ; - arrays |=1UL <<BPF_PROG_INFO_JITED_LINE_INFO ; + arrays =1UL <<PERF_BPIL_JITED_KSYMS ; + arrays |=1UL <<PERF_BPIL_JITED_FUNC_LENS ; + arrays |=1UL <<PERF_BPIL_FUNC_INFO ; + arrays |=1UL <<PERF_BPIL_PROG_TAGS ; + arrays |=1UL <<PERF_BPIL_JITED_INSNS ; + arrays |=1UL <<PERF_BPIL_LINE_INFO ; + arrays |=1UL <<PERF_BPIL_JITED_LINE_INFO ; - info_linear =bpf_program__get_prog_info_linear (fd, arrays); + info_linear =get_bpf_prog_info_linear (fd, arrays);if (IS_ERR_OR_NULL (info_linear)) { info_linear =NULL ;pr_debug ("%s: failed to get BPF program info. aborting\n" , __func__);@@ -452,8 +451,8 @@ int perf_event__synthesize_bpf_events(struct perf_session *session,static void perf_env__add_bpf_info (struct perf_env *env,u32 id) { -struct bpf_prog_info_linear *info_linear;struct bpf_prog_info_node *info_node; +struct perf_bpil *info_linear;struct btf *btf =NULL ;u64 arrays;u32 btf_id;@@ -463,15 +462,15 @@ static void perf_env__add_bpf_info(struct perf_env *env, u32 id)if (fd <0 )return ; - arrays =1UL <<BPF_PROG_INFO_JITED_KSYMS ; - arrays |=1UL <<BPF_PROG_INFO_JITED_FUNC_LENS ; - arrays |=1UL <<BPF_PROG_INFO_FUNC_INFO ; - arrays |=1UL <<BPF_PROG_INFO_PROG_TAGS ; - arrays |=1UL <<BPF_PROG_INFO_JITED_INSNS ; - arrays |=1UL <<BPF_PROG_INFO_LINE_INFO ; - arrays |=1UL <<BPF_PROG_INFO_JITED_LINE_INFO ; + arrays =1UL <<PERF_BPIL_JITED_KSYMS ; + arrays |=1UL <<PERF_BPIL_JITED_FUNC_LENS ; + arrays |=1UL <<PERF_BPIL_FUNC_INFO ; + arrays |=1UL <<PERF_BPIL_PROG_TAGS ; + arrays |=1UL <<PERF_BPIL_JITED_INSNS ; + arrays |=1UL <<PERF_BPIL_LINE_INFO ; + arrays |=1UL <<PERF_BPIL_JITED_LINE_INFO ; - info_linear =bpf_program__get_prog_info_linear (fd, arrays); + info_linear =get_bpf_prog_info_linear (fd, arrays);if (IS_ERR_OR_NULL (info_linear)) {pr_debug ("%s: failed to get BPF program info. aborting\n" , __func__); goto out;tools/perf/util/bpf-event.h +1
-1@@ -19,7 +19,7 @@ struct evlist;struct target ;struct bpf_prog_info_node { -struct bpf_prog_info_linear *info_linear; +struct perf_bpil *info_linear;struct rb_node rb_node; };tools/perf/util/bpf-utils.c new file +261
-0@@ -0,0 +1,261 @@ +// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif + +#include <errno.h> +#include <stdlib.h> +#include <linux/err.h> +#include <linux/kernel.h> +#include <bpf/bpf.h> +#include "bpf-utils.h" +#include "debug.h" + +struct bpil_array_desc { +int array_offset;/* e.g. offset of jited_prog_insns */ +int count_offset;/* e.g. offset of jited_prog_len */ +int size_offset;/* > 0: offset of rec size, +* < 0: fix size of -size_offset +*/ +}; + +static struct bpil_array_desc bpil_array_desc[] = { + [PERF_BPIL_JITED_INSNS ] = { + offsetof(struct bpf_prog_info , jited_prog_insns), + offsetof(struct bpf_prog_info , jited_prog_len), +-1 , + }, + [PERF_BPIL_XLATED_INSNS ] = { + offsetof(struct bpf_prog_info , xlated_prog_insns), + offsetof(struct bpf_prog_info , xlated_prog_len), +-1 , + }, + [PERF_BPIL_MAP_IDS ] = { + offsetof(struct bpf_prog_info , map_ids), + offsetof(struct bpf_prog_info , nr_map_ids), + -(int )sizeof (__u32), + }, + [PERF_BPIL_JITED_KSYMS ] = { + offsetof(struct bpf_prog_info , jited_ksyms), + offsetof(struct bpf_prog_info , nr_jited_ksyms), + -(int )sizeof (__u64), + }, + [PERF_BPIL_JITED_FUNC_LENS ] = { + offsetof(struct bpf_prog_info , jited_func_lens), + offsetof(struct bpf_prog_info , nr_jited_func_lens), + -(int )sizeof (__u32), + }, + [PERF_BPIL_FUNC_INFO ] = { + offsetof(struct bpf_prog_info , func_info), + offsetof(struct bpf_prog_info , nr_func_info), + offsetof(struct bpf_prog_info , func_info_rec_size), + }, + [PERF_BPIL_LINE_INFO ] = { + offsetof(struct bpf_prog_info , line_info), + offsetof(struct bpf_prog_info , nr_line_info), + offsetof(struct bpf_prog_info , line_info_rec_size), + }, + [PERF_BPIL_JITED_LINE_INFO ] = { + offsetof(struct bpf_prog_info , jited_line_info), + offsetof(struct bpf_prog_info , nr_jited_line_info), + offsetof(struct bpf_prog_info , jited_line_info_rec_size), + }, + [PERF_BPIL_PROG_TAGS ] = { + offsetof(struct bpf_prog_info , prog_tags), + offsetof(struct bpf_prog_info , nr_prog_tags), + -(int )sizeof (__u8) *BPF_TAG_SIZE , + }, + +}; + +static __u32 bpf_prog_info_read_offset_u32 (struct bpf_prog_info *info, +int offset) +{ +__u32 *array = (__u32 *)info; + +if (offset >=0 ) +return array[offset /sizeof (__u32)]; +return -(int )offset; +} + +static __u64 bpf_prog_info_read_offset_u64 (struct bpf_prog_info *info, +int offset) +{ +__u64 *array = (__u64 *)info; + +if (offset >=0 ) +return array[offset /sizeof (__u64)]; +return -(int )offset; +} + +static void bpf_prog_info_set_offset_u32 (struct bpf_prog_info *info,int offset, +__u32 val) +{ +__u32 *array = (__u32 *)info; + +if (offset >=0 ) + array[offset /sizeof (__u32)] = val; +} + +static void bpf_prog_info_set_offset_u64 (struct bpf_prog_info *info,int offset, +__u64 val) +{ +__u64 *array = (__u64 *)info; + +if (offset >=0 ) + array[offset /sizeof (__u64)] = val; +} + +struct perf_bpil * +get_bpf_prog_info_linear (int fd,__u64 arrays) +{ +struct bpf_prog_info info = {}; +struct perf_bpil *info_linear; +__u32 info_len =sizeof (info); +__u32 data_len =0 ; +int i, err; +void *ptr; + +if (arrays >>PERF_BPIL_LAST_ARRAY ) +return ERR_PTR (-EINVAL ); + +/* step 1: get array dimensions */ + err =bpf_obj_get_info_by_fd (fd, &info, &info_len); +if (err) { +pr_debug ("can't get prog info: %s" ,strerror (errno)); +return ERR_PTR (-EFAULT ); + } + +/* step 2: calculate total size of all arrays */ +for (i =PERF_BPIL_FIRST_ARRAY ; i <PERF_BPIL_LAST_ARRAY ; ++i) { +bool include_array = (arrays & (1UL << i)) >0 ; +struct bpil_array_desc *desc; +__u32 count, size; + + desc = bpil_array_desc + i; + +/* kernel is too old to support this field */ +if (info_len < desc->array_offset +sizeof (__u32) || + info_len < desc->count_offset +sizeof (__u32) || + (desc->size_offset >0 && info_len < (__u32 )desc->size_offset)) + include_array = false; + +if (!include_array) { + arrays &= ~(1UL << i);/* clear the bit */ +continue ; + } + + count =bpf_prog_info_read_offset_u32 (&info, desc->count_offset); + size =bpf_prog_info_read_offset_u32 (&info, desc->size_offset); + + data_len += count * size; + } + +/* step 3: allocate continuous memory */ + data_len =roundup (data_len,sizeof (__u64)); + info_linear =malloc (sizeof (struct perf_bpil ) + data_len); +if (!info_linear) +return ERR_PTR (-ENOMEM ); + +/* step 4: fill data to info_linear->info */ + info_linear->arrays = arrays; +memset (&info_linear->info,0 ,sizeof (info)); + ptr = info_linear->data; + +for (i =PERF_BPIL_FIRST_ARRAY ; i <PERF_BPIL_LAST_ARRAY ; ++i) { +struct bpil_array_desc *desc; +__u32 count, size; + +if ((arrays & (1UL << i)) ==0 ) +continue ; + + desc = bpil_array_desc + i; + count =bpf_prog_info_read_offset_u32 (&info, desc->count_offset); + size =bpf_prog_info_read_offset_u32 (&info, desc->size_offset); +bpf_prog_info_set_offset_u32 (&info_linear->info, + desc->count_offset, count); +bpf_prog_info_set_offset_u32 (&info_linear->info, + desc->size_offset, size); +bpf_prog_info_set_offset_u64 (&info_linear->info, + desc->array_offset, +ptr_to_u64 (ptr)); + ptr += count * size; + } + +/* step 5: call syscall again to get required arrays */ + err =bpf_obj_get_info_by_fd (fd, &info_linear->info, &info_len); +if (err) { +pr_debug ("can't get prog info: %s" ,strerror (errno)); +free (info_linear); +return ERR_PTR (-EFAULT ); + } + +/* step 6: verify the data */ +for (i =PERF_BPIL_FIRST_ARRAY ; i <PERF_BPIL_LAST_ARRAY ; ++i) { +struct bpil_array_desc *desc; +__u32 v1, v2; + +if ((arrays & (1UL << i)) ==0 ) +continue ; + + desc = bpil_array_desc + i; + v1 =bpf_prog_info_read_offset_u32 (&info, desc->count_offset); + v2 =bpf_prog_info_read_offset_u32 (&info_linear->info, + desc->count_offset); +if (v1 != v2) +pr_warning ("%s: mismatch in element count\n" , __func__); + + v1 =bpf_prog_info_read_offset_u32 (&info, desc->size_offset); + v2 =bpf_prog_info_read_offset_u32 (&info_linear->info, + desc->size_offset); +if (v1 != v2) +pr_warning ("%s: mismatch in rec size\n" , __func__); + } + +/* step 7: update info_len and data_len */ + info_linear->info_len =sizeof (struct bpf_prog_info ); + info_linear->data_len = data_len; + +return info_linear; +} + +void bpil_addr_to_offs (struct perf_bpil *info_linear) +{ +int i; + +for (i =PERF_BPIL_FIRST_ARRAY ; i <PERF_BPIL_LAST_ARRAY ; ++i) { +struct bpil_array_desc *desc; +__u64 addr, offs; + +if ((info_linear->arrays & (1UL << i)) ==0 ) +continue ; + + desc = bpil_array_desc + i; + addr =bpf_prog_info_read_offset_u64 (&info_linear->info, + desc->array_offset); + offs = addr -ptr_to_u64 (info_linear->data); +bpf_prog_info_set_offset_u64 (&info_linear->info, + desc->array_offset, offs); + } +} + +void bpil_offs_to_addr (struct perf_bpil *info_linear) +{ +int i; + +for (i =PERF_BPIL_FIRST_ARRAY ; i <PERF_BPIL_LAST_ARRAY ; ++i) { +struct bpil_array_desc *desc; +__u64 addr, offs; + +if ((info_linear->arrays & (1UL << i)) ==0 ) +continue ; + + desc = bpil_array_desc + i; + offs =bpf_prog_info_read_offset_u64 (&info_linear->info, + desc->array_offset); + addr = offs +ptr_to_u64 (info_linear->data); +bpf_prog_info_set_offset_u64 (&info_linear->info, + desc->array_offset, addr); + } +}tools/perf/util/bpf-utils.h new file +76
-0@@ -0,0 +1,76 @@ +/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ + +#ifndef __PERF_BPF_UTILS_H +#define __PERF_BPF_UTILS_H + +#define ptr_to_u64 (ptr) ((__u64)(unsigned long)(ptr)) + +#ifdef HAVE_LIBBPF_SUPPORT + +#include <bpf/libbpf.h> + +/* +* Get bpf_prog_info in continuous memory +* +* struct bpf_prog_info has multiple arrays. The user has option to choose +* arrays to fetch from kernel. The following APIs provide an uniform way to +* fetch these data. All arrays in bpf_prog_info are stored in a single +* continuous memory region. This makes it easy to store the info in a +* file. +* +* Before writing perf_bpil to files, it is necessary to +* translate pointers in bpf_prog_info to offsets. Helper functions +* bpil_addr_to_offs() and bpil_offs_to_addr() +* are introduced to switch between pointers and offsets. +* +* Examples: +* # To fetch map_ids and prog_tags: +* __u64 arrays = (1UL << PERF_BPIL_MAP_IDS) | +* (1UL << PERF_BPIL_PROG_TAGS); +* struct perf_bpil *info_linear = +* get_bpf_prog_info_linear(fd, arrays); +* +* # To save data in file +* bpil_addr_to_offs(info_linear); +* write(f, info_linear, sizeof(*info_linear) + info_linear->data_len); +* +* # To read data from file +* read(f, info_linear, <proper_size>); +* bpil_offs_to_addr(info_linear); +*/ +enum perf_bpil_array_types { +PERF_BPIL_FIRST_ARRAY =0 , +PERF_BPIL_JITED_INSNS =0 , +PERF_BPIL_XLATED_INSNS , +PERF_BPIL_MAP_IDS , +PERF_BPIL_JITED_KSYMS , +PERF_BPIL_JITED_FUNC_LENS , +PERF_BPIL_FUNC_INFO , +PERF_BPIL_LINE_INFO , +PERF_BPIL_JITED_LINE_INFO , +PERF_BPIL_PROG_TAGS , +PERF_BPIL_LAST_ARRAY , +}; + +struct perf_bpil { +/* size of struct bpf_prog_info, when the tool is compiled */ +__u32 info_len; +/* total bytes allocated for data, round up to 8 bytes */ +__u32 data_len; +/* which arrays are included in data */ +__u64 arrays; +struct bpf_prog_info info; +__u8 data[]; +}; + +struct perf_bpil * +get_bpf_prog_info_linear (int fd,__u64 arrays); + +void +bpil_addr_to_offs (struct perf_bpil *info_linear); + +void +bpil_offs_to_addr (struct perf_bpil *info_linear); + +#endif /* HAVE_LIBBPF_SUPPORT */ +#endif /* __PERF_BPF_UTILS_H */ tools/perf/util/bpf_counter.c +3
-3@@ -13,6 +13,7 @@#include <perf/bpf_perf.h> #include "bpf_counter.h" +#include "bpf-utils.h" #include "counts.h" #include "debug.h" #include "evsel.h" @@ -61,14 +62,13 @@ static int bpf_program_profiler__destroy(struct evsel *evsel)static char *bpf_target_prog_name (int tgt_fd) { -struct bpf_prog_info_linear *info_linear;struct bpf_func_info *func_info; +struct perf_bpil *info_linear;const struct btf_type *t;struct btf *btf =NULL ;char *name =NULL ; - info_linear =bpf_program__get_prog_info_linear ( - tgt_fd,1UL <<BPF_PROG_INFO_FUNC_INFO ); + info_linear =get_bpf_prog_info_linear (tgt_fd,1UL <<PERF_BPIL_FUNC_INFO );if (IS_ERR_OR_NULL (info_linear)) {pr_debug ("failed to get info_linear for prog FD %d\n" , tgt_fd);return NULL ;tools/perf/util/dso.c +1
-0@@ -14,6 +14,7 @@#ifdef HAVE_LIBBPF_SUPPORT #include <bpf/libbpf.h> #include "bpf-event.h" +#include "bpf-utils.h" #endif #include "compress.h" #include "env.h" tools/perf/util/env.c +1
-0@@ -16,6 +16,7 @@ struct perf_env perf_env;#ifdef HAVE_LIBBPF_SUPPORT #include "bpf-event.h" +#include "bpf-utils.h" #include <bpf/libbpf.h> void perf_env__insert_bpf_prog_info(struct perf_env *env,tools/perf/util/header.c +7
-7@@ -48,6 +48,7 @@#include "util/util.h" // perf_exe() #include "cputopo.h" #include "bpf-event.h" +#include "bpf-utils.h" #include "clockid.h" #include "pmu-hybrid.h" @@ -1006,17 +1007,17 @@ static int write_bpf_prog_info(struct feat_fd *ff, node = rb_entry(next, struct bpf_prog_info_node, rb_node); next =rb_next (&node->rb_node); - len =sizeof (struct bpf_prog_info_linear ) + + len =sizeof (struct perf_bpil ) + node->info_linear->data_len;/* before writing to file, translate address to offset */ -bpf_program__bpil_addr_to_offs (node->info_linear); +bpil_addr_to_offs (node->info_linear); ret =do_write (ff, node->info_linear, len);/* * translate back to address even when do_write() fails, * so that this function never changes the data. */ -bpf_program__bpil_offs_to_addr (node->info_linear); +bpil_offs_to_addr (node->info_linear);if (ret <0 ) goto out; }@@ -3018,9 +3019,9 @@ static int process_dir_format(struct feat_fd *ff,#ifdef HAVE_LIBBPF_SUPPORT static int process_bpf_prog_info (struct feat_fd *ff,void *data__maybe_unused ) { -struct bpf_prog_info_linear *info_linear;struct bpf_prog_info_node *info_node;struct perf_env *env = &ff->ph->env; +struct perf_bpil *info_linear;u32 count, i;int err =-1 ;@@ -3049,7 +3050,7 @@ static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused) goto out; } - info_linear =malloc (sizeof (struct bpf_prog_info_linear ) + + info_linear =malloc (sizeof (struct perf_bpil ) + data_len);if (!info_linear) goto out;@@ -3071,7 +3072,7 @@ static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused) goto out;/* after reading from file, translate offset to address */ -bpf_program__bpil_offs_to_addr (info_linear); +bpil_offs_to_addr (info_linear); info_node->info_linear = info_linear;perf_env__insert_bpf_prog_info (env, info_node); } --2.30.2
A message that is not in the archive (sent off-list or not yet received): [email protected]
Arnaldo Carvalho de Melo
maintainerRe: [PATCH v2 bpf-next 3/4] perf: pull in bpf_program__get_prog_info_linear
Em Thu, Oct 28, 2021 at 08:44:51PM +0000, Song Liu escreveu:
Quoted 17 lines
Quoted 17 lines · Andrii Nakryiko
Quoted 11 lines · Dave Marchevsky
To prepare for impending deprecation of libbpf's
bpf_program__get_prog_info_linear, pull in the function and associated helpers into the perf codebase and migrate existing uses to the perf copy.Since libbpf's deprecated definitions will still be visible to perf, it is necessary to rename perf's definitions.
Signed-off-by Dave Marchevsky <[email protected]>---
Quoted 7 lines
Quoted 1 line
LGTM, but, Song, can you please take a look as well?
Sorry for the delay.
Acked-by Song Liu <[email protected]>Also cc'ing Arnaldo's other email.
Thanks, I'll get to this.
- Arnaldo
PATCH v2 4/4 libbpf: deprecate bpf_program__get_prog_info_linear
In reply to Dave MarchevskyAs part of the road to libbpf 1.0, and discussed in libbpf issue tracker [0], bpf_program__get_prog_info_linear and its associated structs and helper functions should be deprecated. The functionality is too specific to the needs of 'perf', and there's little/no out-of-tree usage to preclude introduction of a more general helper in the future.
[0] Closes: https://github.com/libbpf/libbpf/issues/313
--- tools/lib/bpf/libbpf.h | 3 +++ 1 file changed, 3 insertions(+)
tools/lib/bpf/libbpf.h +3 -0
@@ -877,12 +877,15 @@ struct bpf_prog_info_linear {__u8 data[]; }; +LIBBPF_DEPRECATED_SINCE(0, 7, "use a custom linear prog_info wrapper") LIBBPF_API struct bpf_prog_info_linear *bpf_program__get_prog_info_linear (int fd,__u64 arrays); +LIBBPF_DEPRECATED_SINCE(0, 7, "use a custom linear prog_info wrapper") LIBBPF_API voidbpf_program__bpil_addr_to_offs (struct bpf_prog_info_linear *info_linear); +LIBBPF_DEPRECATED_SINCE(0, 7, "use a custom linear prog_info wrapper") LIBBPF_API voidbpf_program__bpil_offs_to_addr (struct bpf_prog_info_linear *info_linear);
Signature · 1 line
2.30.2
Quoted 8 lines · Dave Marchevsky
As part of the road to libbpf 1.0, and discussed in libbpf issue tracker [0],
bpf_program__get_prog_info_linearand its associated structs and helper functions should be deprecated. The functionality is too specific to the needs of 'perf', and there's little/no out-of-tree usage to preclude introduction of a more general helper in the future.[0] Closes: https://github.com/libbpf/libbpf/issues/313
styling nit: don't know if it's described anywhere or not, but when people do references like this, they use 2 spaces of indentation. No idea how it came to be, but that's what I did for a while and see others doing the same.
Quoted 13 lines
Signed-off-by Dave Marchevsky <[email protected]>--- tools/lib/bpf/libbpf.h | 3 +++ 1 file changed, 3 insertions(+)tools/lib/bpf/libbpf.h +0
-0@@ -877,12 +877,15 @@ struct bpf_prog_info_linear {__u8 data[]; };
tools/lib/bpf/libbpf.h struct bpf_prog_info_linear { · line 888
Quoted 10 lines
tools/lib/bpf/libbpf.h +3
-0+LIBBPF_DEPRECATED_SINCE(0, 7, "use a custom linear prog_info wrapper") LIBBPF_API struct bpf_prog_info_linear *bpf_program__get_prog_info_linear (int fd,__u64 arrays); +LIBBPF_DEPRECATED_SINCE(0, 7, "use a custom linear prog_info wrapper") LIBBPF_API voidbpf_program__bpil_addr_to_offs (struct bpf_prog_info_linear *info_linear); +LIBBPF_DEPRECATED_SINCE (0 ,7 ,"use a custom linear prog_info wrapper" )
we can actually deprecate all this starting from v0.6, because perf is building libbpf statically, so no worries about releases (also there are no replacement APIs we have to wait full release for)
Quoted 6 lines
tools/lib/bpf/libbpf.h +0
-1LIBBPF_API voidbpf_program__bpil_offs_to_addr (struct bpf_prog_info_linear *info_linear); --2.30.2
Quoted 37 lines · Andrii Nakryiko
Quoted 8 lines · Dave Marchevsky
As part of the road to libbpf 1.0, and discussed in libbpf issue tracker [0],
bpf_program__get_prog_info_linearand its associated structs and helper functions should be deprecated. The functionality is too specific to the needs of 'perf', and there's little/no out-of-tree usage to preclude introduction of a more general helper in the future.[0] Closes: https://github.com/libbpf/libbpf/issues/313
styling nit: don't know if it's described anywhere or not, but when people do references like this, they use 2 spaces of indentation. No idea how it came to be, but that's what I did for a while and see others doing the same.
Quoted 22 lines
Signed-off-by Dave Marchevsky <[email protected]>--- tools/lib/bpf/libbpf.h | 3 +++ 1 file changed, 3 insertions(+)tools/lib/bpf/libbpf.h +2
-0@@ -877,12 +877,15 @@ struct bpf_prog_info_linear {__u8 data[]; }; +LIBBPF_DEPRECATED_SINCE(0, 7, "use a custom linear prog_info wrapper") LIBBPF_API struct bpf_prog_info_linear *bpf_program__get_prog_info_linear (int fd,__u64 arrays); +LIBBPF_DEPRECATED_SINCE(0, 7, "use a custom linear prog_info wrapper") LIBBPF_API voidbpf_program__bpil_addr_to_offs (struct bpf_prog_info_linear *info_linear);
Quoted 5 lines
tools/lib/bpf/libbpf.h struct bpf_prog_info_linear { · line 888
Quoted 1 line
tools/lib/bpf/libbpf.h +1
-0+LIBBPF_DEPRECATED_SINCE (0 ,7 ,"use a custom linear prog_info wrapper" )we can actually deprecate all this starting from v0.6, because perf is building libbpf statically, so no worries about releases (also there are no replacement APIs we have to wait full release for)
Just FYI, we're also using this in libxdp, and that does link dynamically to libbpf. It's not an issue to move away from it[0], but perf is not the only user :)
-Toke
[0] Track that here: https://github.com/xdp-project/xdp-tools/issues/127
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 35 lines · Dave Marchevsky
Quoted 20 lines · Toke Høiland-Jørgensen
Quoted 18 lines · Andrii Nakryiko
Quoted 8 lines · Dave Marchevsky
As part of the road to libbpf 1.0, and discussed in libbpf issue tracker [0],
bpf_program__get_prog_info_linearand its associated structs and helper functions should be deprecated. The functionality is too specific to the needs of 'perf', and there's little/no out-of-tree usage to preclude introduction of a more general helper in the future.[0] Closes: https://github.com/libbpf/libbpf/issues/313
styling nit: don't know if it's described anywhere or not, but when people do references like this, they use 2 spaces of indentation. No idea how it came to be, but that's what I did for a while and see others doing the same.
Quoted 3 lines
Signed-off-by Dave Marchevsky <[email protected]>---
[...]
Quoted 10 lines
Quoted 3 lines
we can actually deprecate all this starting from v0.6, because perf is building libbpf statically, so no worries about releases (also there are no replacement APIs we have to wait full release for)
Just FYI, we're also using this in libxdp, and that does link dynamically to libbpf. It's not an issue to move away from it[0], but perf is not the only user :)
-Toke
Quoted 7 lines
Quoted 2 lines
[0] Track that here: https://github.com/xdp-project/xdp-tools/issues/127
I submitted a PR to migrate the xdp-tools usage as well. Strange that this didn't show up in an "all github" search.
Andrii, should the
DEPRECATED_SINCEstay at 0.7 in light of this?
There is no replacement API that we need to wait to go through full libbpf release, so no, it can stay as is.