All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v6 perf/core 0/2] perf: stop using deprecated bpf APIs
@ 2022-02-12 15:51 Andrii Nakryiko
  2022-02-12 15:51 ` [PATCH v6 perf/core 1/2] perf: Stop using deprecated bpf_load_program() API Andrii Nakryiko
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Andrii Nakryiko @ 2022-02-12 15:51 UTC (permalink / raw)
  To: acme, linux-perf-users; +Cc: bpf, christylee, jolsa, Andrii Nakryiko

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(-)

-- 
2.30.2


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v6 perf/core 1/2] perf: Stop using deprecated bpf_load_program() API
  2022-02-12 15:51 [PATCH v6 perf/core 0/2] perf: stop using deprecated bpf APIs Andrii Nakryiko
@ 2022-02-12 15:51 ` Andrii Nakryiko
  2022-02-12 15:51 ` [PATCH v6 perf/core 2/2] perf: Stop using deprecated bpf_object__next() API Andrii Nakryiko
  2022-02-12 18:18 ` [PATCH v6 perf/core 0/2] perf: stop using deprecated bpf APIs Jiri Olsa
  2 siblings, 0 replies; 6+ messages in thread
From: Andrii Nakryiko @ 2022-02-12 15:51 UTC (permalink / raw)
  To: acme, linux-perf-users; +Cc: bpf, christylee, jolsa, Andrii Nakryiko

From: Christy Lee <christylee@fb.com>

bpf_load_program() API is deprecated, remove perf's usage of the
deprecated function. Add a __weak function declaration for libbpf
version compatibility.

Signed-off-by: Christy Lee <christylee@fb.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
---
 tools/perf/tests/bpf.c      | 14 ++++----------
 tools/perf/util/bpf-event.c | 13 +++++++++++++
 2 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/tools/perf/tests/bpf.c b/tools/perf/tests/bpf.c
index 573490530194..57b9591f7cbb 100644
--- a/tools/perf/tests/bpf.c
+++ b/tools/perf/tests/bpf.c
@@ -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));
diff --git a/tools/perf/util/bpf-event.c b/tools/perf/util/bpf-event.c
index a517eaa51eb3..bd1bc3c86cac 100644
--- a/tools/perf/util/bpf-event.c
+++ b/tools/perf/util/bpf-event.c
@@ -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)
 {
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v6 perf/core 2/2] perf: Stop using deprecated bpf_object__next() API
  2022-02-12 15:51 [PATCH v6 perf/core 0/2] perf: stop using deprecated bpf APIs Andrii Nakryiko
  2022-02-12 15:51 ` [PATCH v6 perf/core 1/2] perf: Stop using deprecated bpf_load_program() API Andrii Nakryiko
@ 2022-02-12 15:51 ` Andrii Nakryiko
  2022-02-12 18:18 ` [PATCH v6 perf/core 0/2] perf: stop using deprecated bpf APIs Jiri Olsa
  2 siblings, 0 replies; 6+ messages in thread
From: Andrii Nakryiko @ 2022-02-12 15:51 UTC (permalink / raw)
  To: acme, linux-perf-users; +Cc: bpf, christylee, jolsa, Andrii Nakryiko, Song Liu

From: Christy Lee <christylee@fb.com>

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.

Acked-by: Song Liu <songliubraving@fb.com>
Signed-off-by: Christy Lee <christylee@fb.com>
Signed-off-by: Jiri Olsa <jolsa@redhat.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
---
 tools/perf/util/bpf-loader.c | 98 +++++++++++++++++++++++++++++-------
 1 file changed, 79 insertions(+), 19 deletions(-)

diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c
index 7ecfaac7536a..db61e09be585 100644
--- a/tools/perf/util/bpf-loader.c
+++ b/tools/perf/util/bpf-loader.c
@@ -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))
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v6 perf/core 0/2] perf: stop using deprecated bpf APIs
  2022-02-12 15:51 [PATCH v6 perf/core 0/2] perf: stop using deprecated bpf APIs Andrii Nakryiko
  2022-02-12 15:51 ` [PATCH v6 perf/core 1/2] perf: Stop using deprecated bpf_load_program() API Andrii Nakryiko
  2022-02-12 15:51 ` [PATCH v6 perf/core 2/2] perf: Stop using deprecated bpf_object__next() API Andrii Nakryiko
@ 2022-02-12 18:18 ` Jiri Olsa
  2022-02-13  1:47   ` Andrii Nakryiko
  2 siblings, 1 reply; 6+ messages in thread
From: Jiri Olsa @ 2022-02-12 18:18 UTC (permalink / raw)
  To: Andrii Nakryiko; +Cc: acme, linux-perf-users, bpf, christylee, jolsa

On Sat, Feb 12, 2022 at 07:51:23AM -0800, Andrii Nakryiko wrote:
> 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);

looks good, tests are passing for me

jirka

> 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(-)
> 
> -- 
> 2.30.2
> 

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v6 perf/core 0/2] perf: stop using deprecated bpf APIs
  2022-02-12 18:18 ` [PATCH v6 perf/core 0/2] perf: stop using deprecated bpf APIs Jiri Olsa
@ 2022-02-13  1:47   ` Andrii Nakryiko
  2022-02-14 18:58     ` Arnaldo Carvalho de Melo
  0 siblings, 1 reply; 6+ messages in thread
From: Andrii Nakryiko @ 2022-02-13  1:47 UTC (permalink / raw)
  To: Jiri Olsa
  Cc: Andrii Nakryiko, Arnaldo Carvalho de Melo, linux-perf-use.,
	bpf, Christy Lee, Jiri Olsa

On Sat, Feb 12, 2022 at 10:18 AM Jiri Olsa <olsajiri@gmail.com> wrote:
>
> On Sat, Feb 12, 2022 at 07:51:23AM -0800, Andrii Nakryiko wrote:
> > 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);
>
> looks good, tests are passing for me

great, thanks for checking!

>
> jirka
>
> > 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(-)
> >
> > --
> > 2.30.2
> >

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v6 perf/core 0/2] perf: stop using deprecated bpf APIs
  2022-02-13  1:47   ` Andrii Nakryiko
@ 2022-02-14 18:58     ` Arnaldo Carvalho de Melo
  0 siblings, 0 replies; 6+ messages in thread
From: Arnaldo Carvalho de Melo @ 2022-02-14 18:58 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: Jiri Olsa, Andrii Nakryiko, linux-perf-use., bpf, Christy Lee, Jiri Olsa

Em Sat, Feb 12, 2022 at 05:47:25PM -0800, Andrii Nakryiko escreveu:
> On Sat, Feb 12, 2022 at 10:18 AM Jiri Olsa <olsajiri@gmail.com> wrote:
> >
> > On Sat, Feb 12, 2022 at 07:51:23AM -0800, Andrii Nakryiko wrote:
> > > 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);
> >
> > looks good, tests are passing for me
> 
> great, thanks for checking!

All tests passed, patches merged and pushed out to perf/core, thanks!

- Arnaldo
 
> >
> > jirka
> >
> > > 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(-)
> > >
> > > --
> > > 2.30.2
> > >

-- 

- Arnaldo

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-02-14 19:02 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-12 15:51 [PATCH v6 perf/core 0/2] perf: stop using deprecated bpf APIs Andrii Nakryiko
2022-02-12 15:51 ` [PATCH v6 perf/core 1/2] perf: Stop using deprecated bpf_load_program() API Andrii Nakryiko
2022-02-12 15:51 ` [PATCH v6 perf/core 2/2] perf: Stop using deprecated bpf_object__next() API Andrii Nakryiko
2022-02-12 18:18 ` [PATCH v6 perf/core 0/2] perf: stop using deprecated bpf APIs Jiri Olsa
2022-02-13  1:47   ` Andrii Nakryiko
2022-02-14 18:58     ` Arnaldo Carvalho de Melo

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.