perf: stop using deprecated bpf APIs
Subsystems (2)
Andrii Nakryiko
maintainerv6 cover perf: stop using deprecated bpf APIs
libbpf's bpf_prog_load() and bpf__object_next() APIs are deprecated. remove perf's usage of these deprecated functions. After this patch set, the only remaining libbpf deprecated APIs in perf would be bpf_program__set_prep() and bpf_program__nth_fd().
v5 -> v6:
- rebase onto perf/core tree (Arnaldo);
v4 -> v5:
- add bpf_perf_object__add() and use it where appropriate (Jiri);
- use __maybe_unused in first patch;
v3 -> v4:
- Fixed commit title
- Added weak definition for deprecated function
v2 -> v3:
- Fixed commit message to use upstream perf
v1 -> v2:
- Added missing commit message
- Added more details to commit message and added steps to reproduce original test case.
Christy Lee (2):
perf: Stop using deprecated bpf_load_program() API
perf: Stop using deprecated bpf_object__next() API
tools/perf/tests/bpf.c | 14 ++---- tools/perf/util/bpf-event.c | 13 +++++ tools/perf/util/bpf-loader.c | 98 +++++++++++++++++++++++++++++------- 3 files changed, 96 insertions(+), 29 deletions(-)
Signature · 1 line
2.30.2
Andrii Nakryiko
maintainerPATCH v6 1/2 perf: Stop using deprecated bpf_load_program() API
From: Christy Lee <[email protected]>
bpf_load_program() API is deprecated, remove perf's usage of the deprecated function. Add a __weak function declaration for libbpf version compatibility.
--- tools/perf/tests/bpf.c | 14 ++++---------- tools/perf/util/bpf-event.c | 13 +++++++++++++ 2 files changed, 17 insertions(+), 10 deletions(-)
tools/perf/tests/bpf.c +4 -10
@@ -281,8 +281,8 @@ static int __test__bpf(int idx)static int check_env (void ) { +LIBBPF_OPTS (bpf_prog_load_opts, opts);int err; -unsigned int kver_int;char license[] ="GPL" ;struct bpf_insn insns[] = {
@@ -290,19 +290,13 @@ static int check_env(void) BPF_EXIT_INSN(), }; - err =fetch_kernel_version (&kver_int,NULL ,0 ); + err =fetch_kernel_version (&opts.kern_version,NULL ,0 );if (err) {pr_debug ("Unable to get kernel version\n" );return err; } - -/* temporarily disable libbpf deprecation warnings */ -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wdeprecated-declarations" - err =bpf_load_program (BPF_PROG_TYPE_KPROBE , insns, -ARRAY_SIZE (insns), - license, kver_int,NULL ,0 ); -#pragma GCC diagnostic pop + err =bpf_prog_load (BPF_PROG_TYPE_KPROBE ,NULL , license, insns, +ARRAY_SIZE (insns), &opts);if (err <0 ) {pr_err ("Missing basic BPF support, skip this test: %s\n" ,strerror (errno));
tools/perf/util/bpf-event.c +13 -0
@@ -33,6 +33,19 @@ struct btf * __weak btf__load_from_kernel_by_id(__u32 id)return err ?ERR_PTR (err) : btf; } +int __weak bpf_prog_load (enum bpf_prog_type prog_type, +const char *prog_name__maybe_unused , +const char *license, +const struct bpf_insn *insns,size_t insn_cnt, +const struct bpf_prog_load_opts *opts) +{ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" +return bpf_load_program (prog_type, insns, insn_cnt, license, + opts->kern_version, opts->log_buf, opts->log_size); +#pragma GCC diagnostic pop +} +struct bpf_program *__weak bpf_object__next_program (const struct bpf_object *obj,struct bpf_program *prev) {
Signature · 1 line
2.30.2
Andrii Nakryiko
maintainerPATCH v6 2/2 perf: Stop using deprecated bpf_object__next() API
In reply to Andrii NakryikoFrom: Christy Lee <[email protected]>
Libbpf has deprecated the ability to keep track of object list inside libbpf, it now requires applications to track usage multiple bpf objects directly. Remove usage of bpf_object__next() API and hoist the tracking logic to perf.
--- tools/perf/util/bpf-loader.c | 98 +++++++++++++++++++++++++++++------- 1 file changed, 79 insertions(+), 19 deletions(-)
tools/perf/util/bpf-loader.c +79 -19
@@ -49,8 +49,52 @@ struct bpf_prog_priv {int *type_mapping; }; +struct bpf_perf_object { +struct list_head list; +struct bpf_object *obj; +}; + +static LIST_HEAD (bpf_objects_list); + +static struct bpf_perf_object * +bpf_perf_object__next (struct bpf_perf_object *prev) +{ +struct bpf_perf_object *next; + +if (!prev) + next =list_first_entry (&bpf_objects_list, + struct bpf_perf_object, + list); +else + next =list_next_entry (prev, list); + +/* Empty list is noticed here so don't need checking on entry. */ +if (&next->list == &bpf_objects_list) +return NULL ; + +return next; +} + +#define bpf_perf_object__for_each (perf_obj, tmp) \ + for ((perf_obj) = bpf_perf_object__next(NULL), \ + (tmp) = bpf_perf_object__next(perf_obj); \ + (perf_obj) != NULL; \ + (perf_obj) = (tmp), (tmp) = bpf_perf_object__next(tmp)) +static bool libbpf_initialized; +static int bpf_perf_object__add (struct bpf_object *obj) +{ +struct bpf_perf_object *perf_obj =zalloc (sizeof (*perf_obj)); + +if (perf_obj) { +INIT_LIST_HEAD (&perf_obj->list); + perf_obj->obj = obj; +list_add_tail (&perf_obj->list, &bpf_objects_list); + } +return perf_obj ?0 : -ENOMEM ; +} +struct bpf_object *bpf__prepare_load_buffer (void *obj_buf,size_t obj_buf_sz,const char *name) {
@@ -67,9 +111,21 @@ bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz, const char *name)return ERR_PTR (-EINVAL ); } +if (bpf_perf_object__add (obj)) { +bpf_object__close (obj); +return ERR_PTR (-ENOMEM ); + } +return obj; } +static void bpf_perf_object__close (struct bpf_perf_object *perf_obj) +{ +list_del (&perf_obj->list); +bpf_object__close (perf_obj->obj); +free (perf_obj); +} +struct bpf_object *bpf__prepare_load (const char *filename,bool source) {struct bpf_object *obj;
@@ -100,24 +156,30 @@ struct bpf_object *bpf__prepare_load(const char *filename, bool source)llvm__dump_obj (filename, obj_buf, obj_buf_sz);free (obj_buf); - }else + }else { obj =bpf_object__open (filename); + }if (IS_ERR_OR_NULL (obj)) {pr_debug ("bpf: failed to load %s\n" , filename);return obj; } +if (bpf_perf_object__add (obj)) { +bpf_object__close (obj); +return ERR_PTR (-BPF_LOADER_ERRNO__COMPILE ); + } +return obj; }void bpf__clear (void ) { -struct bpf_object *obj, *tmp; +struct bpf_perf_object *perf_obj, *tmp; -bpf_object__for_each_safe (obj, tmp) { -bpf__unprobe (obj); -bpf_object__close (obj); +bpf_perf_object__for_each (perf_obj, tmp) { +bpf__unprobe (perf_obj->obj); +bpf_perf_object__close (perf_obj); } }
@@ -1501,11 +1563,11 @@ apply_obj_config_object(struct bpf_object *obj)int bpf__apply_obj_config (void ) { -struct bpf_object *obj, *tmp; +struct bpf_perf_object *perf_obj, *tmp;int err; -bpf_object__for_each_safe (obj, tmp) { - err =apply_obj_config_object (obj); +bpf_perf_object__for_each (perf_obj, tmp) { + err =apply_obj_config_object (perf_obj->obj);if (err)return err; }
@@ -1513,26 +1575,24 @@ int bpf__apply_obj_config(void)return 0 ; } -#define bpf__for_each_map (pos, obj, objtmp) \ - bpf_object__for_each_safe(obj, objtmp) \ - bpf_object__for_each_map(pos, obj) +#define bpf__perf_for_each_map (map, pobj, tmp) \ + bpf_perf_object__for_each(pobj, tmp) \ + bpf_object__for_each_map(map, pobj->obj) -#define bpf__for_each_map_named (pos, obj, objtmp, name) \ - bpf__for_each_map(pos, obj, objtmp) \ - if (bpf_map__name(pos) && \ - (strcmp(name, \ - bpf_map__name(pos)) == 0)) +#define bpf__perf_for_each_map_named (map, pobj, pobjtmp, name) \ + bpf__perf_for_each_map(map, pobj, pobjtmp) \ + if (bpf_map__name(map) && (strcmp(name, bpf_map__name(map)) == 0))struct evsel *bpf__setup_output_event (struct evlist *evlist,const char *name) {struct bpf_map_priv *tmpl_priv =NULL ; -struct bpf_object *obj, *tmp; +struct bpf_perf_object *perf_obj, *tmp;struct evsel *evsel =NULL ;struct bpf_map *map;int err;bool need_init = false; -bpf__for_each_map_named (map, obj, tmp, name) { +bpf__perf_for_each_map_named (map, perf_obj, tmp, name) {struct bpf_map_priv *priv =bpf_map__priv (map);if (IS_ERR (priv))
@@ -1568,7 +1628,7 @@ struct evsel *bpf__setup_output_event(struct evlist *evlist, const char *name) evsel =evlist__last (evlist); } -bpf__for_each_map_named (map, obj, tmp, name) { +bpf__perf_for_each_map_named (map, perf_obj, tmp, name) {struct bpf_map_priv *priv =bpf_map__priv (map);if (IS_ERR (priv))
Signature · 1 line
2.30.2
Quoted 7 lines · Andrii Nakryiko
libbpf's
bpf_prog_load()andbpf__object_next()APIs are deprecated. remove perf's usage of these deprecated functions. After this patch set, the only remaining libbpf deprecated APIs in perf would bebpf_program__set_prep()andbpf_program__nth_fd().v5 -> v6:
- rebase onto perf/core tree (Arnaldo);
looks good, tests are passing for me
jirka
Quoted 25 lines
v4 -> v5:
- addbpf_perf_object__add()and use it where appropriate (Jiri);
- use__maybe_unusedin first patch;
v3 -> v4:
- Fixed commit title
- Added weak definition for deprecated function
v2 -> v3:
- Fixed commit message to use upstream perf
v1 -> v2:
- Added missing commit message
- Added more details to commit message and added steps to reproduce original test case.Christy Lee (2):
perf: Stop using deprecatedbpf_load_program()API
perf: Stop using deprecatedbpf_object__next()APItools/perf/tests/bpf.c | 14 ++---- tools/perf/util/bpf-event.c | 13 +++++ tools/perf/util/bpf-loader.c | 98 +++++++++++++++++++++++++++++------- 3 files changed, 96 insertions(+), 29 deletions(-)Signature · 1 line
2.30.2
Quoted 11 lines · Jiri Olsa
Quoted 7 lines · Andrii Nakryiko
libbpf's
bpf_prog_load()andbpf__object_next()APIs are deprecated. remove perf's usage of these deprecated functions. After this patch set, the only remaining libbpf deprecated APIs in perf would bebpf_program__set_prep()andbpf_program__nth_fd().v5 -> v6:
- rebase onto perf/core tree (Arnaldo);looks good, tests are passing for me
great, thanks for checking!
Quoted 28 lines
jirka
Quoted 25 lines
v4 -> v5:
- addbpf_perf_object__add()and use it where appropriate (Jiri);
- use__maybe_unusedin first patch;
v3 -> v4:
- Fixed commit title
- Added weak definition for deprecated function
v2 -> v3:
- Fixed commit message to use upstream perf
v1 -> v2:
- Added missing commit message
- Added more details to commit message and added steps to reproduce original test case.Christy Lee (2):
perf: Stop using deprecatedbpf_load_program()API
perf: Stop using deprecatedbpf_object__next()APItools/perf/tests/bpf.c | 14 ++---- tools/perf/util/bpf-event.c | 13 +++++ tools/perf/util/bpf-loader.c | 98 +++++++++++++++++++++++++++++------- 3 files changed, 96 insertions(+), 29 deletions(-)--
2.30.2
Arnaldo Carvalho de Melo
maintainerEm Sat, Feb 12, 2022 at 05:47:25PM -0800, Andrii Nakryiko escreveu:
Quoted 8 lines · Jiri Olsa
Quoted 7 lines · Andrii Nakryiko
Quoted 5 lines
libbpf's
bpf_prog_load()andbpf__object_next()APIs are deprecated. remove perf's usage of these deprecated functions. After this patch set, the only remaining libbpf deprecated APIs in perf would bebpf_program__set_prep()andbpf_program__nth_fd().
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
v5 -> v6:
- rebase onto perf/core tree (Arnaldo);looks good, tests are passing for me
great, thanks for checking!
All tests passed, patches merged and pushed out to perf/core, thanks!
- Arnaldo
Quoted 28 lines
Quoted 28 lines
jirka
Quoted 25 lines
v4 -> v5:
- addbpf_perf_object__add()and use it where appropriate (Jiri);
- use__maybe_unusedin first patch;
v3 -> v4:
- Fixed commit title
- Added weak definition for deprecated function
v2 -> v3:
- Fixed commit message to use upstream perf
v1 -> v2:
- Added missing commit message
- Added more details to commit message and added steps to reproduce original test case.Christy Lee (2):
perf: Stop using deprecatedbpf_load_program()API
perf: Stop using deprecatedbpf_object__next()APItools/perf/tests/bpf.c | 14 ++---- tools/perf/util/bpf-event.c | 13 +++++ tools/perf/util/bpf-loader.c | 98 +++++++++++++++++++++++++++++------- 3 files changed, 96 insertions(+), 29 deletions(-)--
2.30.2
Signature · 1 line
- Arnaldo