All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCHv4 bpf-next 0/2] perf tools: Fix prologue generation
@ 2022-06-03 20:45 Jiri Olsa
  2022-06-03 20:45 ` [PATCHv4 bpf-next 1/2] perf tools: Register fallback libbpf section handler Jiri Olsa
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: Jiri Olsa @ 2022-06-03 20:45 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko
  Cc: linux-perf-users, netdev, bpf, Ingo Molnar, Namhyung Kim,
	Alexander Shishkin, Peter Zijlstra, Martin KaFai Lau, Song Liu,
	Yonghong Song, John Fastabend, Ian Rogers

hi,
sending change we discussed some time ago [1] to get rid of
some deprecated functions we use in perf prologue code.

Despite the gloomy discussion I think the final code does
not look that bad ;-)

This patchset removes following libbpf functions from perf:
  bpf_program__set_prep
  bpf_program__nth_fd
  struct bpf_prog_prep_result

v4 changes:
  - fix typo [Andrii]

v3 changes:
  - removed R0/R1 zero init in libbpf_prog_prepare_load_fn,
    because it's not needed [Andrii]
  - rebased/post on top of bpf-next/master which now has
    all the needed perf/core changes

v2 changes:
  - use fallback section prog handler, so we don't need to
    use section prefix [Andrii]
  - realloc prog->insns array in bpf_program__set_insns [Andrii]
  - squash patch 1 from previous version with
    bpf_program__set_insns change [Daniel]
  - patch 3 already merged [Arnaldo]
  - added more comments

thanks,
jirka


[1] https://lore.kernel.org/bpf/CAEf4BzaiBO3_617kkXZdYJ8hS8YF--ZLgapNbgeeEJ-pY0H88g@mail.gmail.com/
---
Jiri Olsa (2):
      perf tools: Register fallback libbpf section handler
      perf tools: Rework prologue generation code

 tools/perf/util/bpf-loader.c | 173 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------
 1 file changed, 155 insertions(+), 18 deletions(-)

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

* [PATCHv4 bpf-next 1/2] perf tools: Register fallback libbpf section handler
  2022-06-03 20:45 [PATCHv4 bpf-next 0/2] perf tools: Fix prologue generation Jiri Olsa
@ 2022-06-03 20:45 ` Jiri Olsa
  2022-06-03 20:45 ` [PATCHv4 bpf-next 2/2] perf tools: Rework prologue generation code Jiri Olsa
  2022-06-09 20:31 ` [PATCHv4 bpf-next 0/2] perf tools: Fix prologue generation Andrii Nakryiko
  2 siblings, 0 replies; 13+ messages in thread
From: Jiri Olsa @ 2022-06-03 20:45 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko
  Cc: linux-perf-users, netdev, bpf, Ingo Molnar, Namhyung Kim,
	Alexander Shishkin, Peter Zijlstra, Martin KaFai Lau, Song Liu,
	Yonghong Song, John Fastabend, Ian Rogers

Perf is using section name to declare special kprobe arguments,
which no longer works with current libbpf, that either requires
certain form of the section name or allows to register custom
handler.

Adding perf support to register 'fallback' section handler to take
care of perf kprobe programs. The fallback means that it handles
any section definition besides the ones that libbpf handles.

The handler serves two purposes:
  - allows perf programs to have special arguments in section name
  - allows perf to use pre-load callback where we can attach init
    code (zeroing all argument registers) to each perf program

The second is essential part of new prologue generation code,
that's coming in following patch.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
 tools/perf/util/bpf-loader.c | 45 ++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c
index f8ad581ea247..e7992a0eb477 100644
--- a/tools/perf/util/bpf-loader.c
+++ b/tools/perf/util/bpf-loader.c
@@ -86,6 +86,7 @@ bpf_perf_object__next(struct bpf_perf_object *prev)
 	     (perf_obj) = (tmp), (tmp) = bpf_perf_object__next(tmp))
 
 static bool libbpf_initialized;
+static int libbpf_sec_handler;
 
 static int bpf_perf_object__add(struct bpf_object *obj)
 {
@@ -99,12 +100,56 @@ static int bpf_perf_object__add(struct bpf_object *obj)
 	return perf_obj ? 0 : -ENOMEM;
 }
 
+static struct bpf_insn prologue_init_insn[] = {
+	BPF_MOV64_IMM(BPF_REG_2, 0),
+	BPF_MOV64_IMM(BPF_REG_3, 0),
+	BPF_MOV64_IMM(BPF_REG_4, 0),
+	BPF_MOV64_IMM(BPF_REG_5, 0),
+};
+
+static int libbpf_prog_prepare_load_fn(struct bpf_program *prog,
+				       struct bpf_prog_load_opts *opts __maybe_unused,
+				       long cookie __maybe_unused)
+{
+	size_t init_size_cnt = ARRAY_SIZE(prologue_init_insn);
+	size_t orig_insn_cnt, insn_cnt, init_size, orig_size;
+	const struct bpf_insn *orig_insn;
+	struct bpf_insn *insn;
+
+	/* prepend initialization code to program instructions */
+	orig_insn = bpf_program__insns(prog);
+	orig_insn_cnt = bpf_program__insn_cnt(prog);
+	init_size = init_size_cnt * sizeof(*insn);
+	orig_size = orig_insn_cnt * sizeof(*insn);
+
+	insn_cnt = orig_insn_cnt + init_size_cnt;
+	insn = malloc(insn_cnt * sizeof(*insn));
+	if (!insn)
+		return -ENOMEM;
+
+	memcpy(insn, prologue_init_insn, init_size);
+	memcpy((char *) insn + init_size, orig_insn, orig_size);
+	bpf_program__set_insns(prog, insn, insn_cnt);
+	return 0;
+}
+
 static int libbpf_init(void)
 {
+	LIBBPF_OPTS(libbpf_prog_handler_opts, handler_opts,
+		.prog_prepare_load_fn = libbpf_prog_prepare_load_fn,
+	);
+
 	if (libbpf_initialized)
 		return 0;
 
 	libbpf_set_print(libbpf_perf_print);
+	libbpf_sec_handler = libbpf_register_prog_handler(NULL, BPF_PROG_TYPE_KPROBE,
+							  0, &handler_opts);
+	if (libbpf_sec_handler < 0) {
+		pr_debug("bpf: failed to register libbpf section handler: %d\n",
+			 libbpf_sec_handler);
+		return -BPF_LOADER_ERRNO__INTERNAL;
+	}
 	libbpf_initialized = true;
 	return 0;
 }
-- 
2.35.3


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

* [PATCHv4 bpf-next 2/2] perf tools: Rework prologue generation code
  2022-06-03 20:45 [PATCHv4 bpf-next 0/2] perf tools: Fix prologue generation Jiri Olsa
  2022-06-03 20:45 ` [PATCHv4 bpf-next 1/2] perf tools: Register fallback libbpf section handler Jiri Olsa
@ 2022-06-03 20:45 ` Jiri Olsa
  2022-06-09 20:31 ` [PATCHv4 bpf-next 0/2] perf tools: Fix prologue generation Andrii Nakryiko
  2 siblings, 0 replies; 13+ messages in thread
From: Jiri Olsa @ 2022-06-03 20:45 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko
  Cc: linux-perf-users, netdev, bpf, Ingo Molnar, Namhyung Kim,
	Alexander Shishkin, Peter Zijlstra, Martin KaFai Lau, Song Liu,
	Yonghong Song, John Fastabend, Ian Rogers

Some functions we use for bpf prologue generation are going to be
deprecated. This change reworks current code not to use them.

We need to replace following functions/struct:
   bpf_program__set_prep
   bpf_program__nth_fd
   struct bpf_prog_prep_result

Currently we use bpf_program__set_prep to hook perf callback before
program is loaded and provide new instructions with the prologue.

We replace this function/ality by taking instructions for specific
program, attaching prologue to them and load such new ebpf programs
with prologue using separate bpf_prog_load calls (outside libbpf
load machinery).

Before we can take and use program instructions, we need libbpf to
actually load it. This way we get the final shape of its instructions
with all relocations and verifier adjustments).

There's one glitch though.. perf kprobe program already assumes
generated prologue code with proper values in argument registers,
so loading such program directly will fail in the verifier.

That's where the fallback pre-load handler fits in and prepends
the initialization code to the program. Once such program is loaded
we take its instructions, cut off the initialization code and prepend
the prologue.

I know.. sorry ;-)

Suggested-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
 tools/perf/util/bpf-loader.c | 128 ++++++++++++++++++++++++++++++-----
 1 file changed, 110 insertions(+), 18 deletions(-)

diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c
index e7992a0eb477..30d0e688beec 100644
--- a/tools/perf/util/bpf-loader.c
+++ b/tools/perf/util/bpf-loader.c
@@ -9,6 +9,7 @@
 #include <linux/bpf.h>
 #include <bpf/libbpf.h>
 #include <bpf/bpf.h>
+#include <linux/filter.h>
 #include <linux/err.h>
 #include <linux/kernel.h>
 #include <linux/string.h>
@@ -49,6 +50,7 @@ struct bpf_prog_priv {
 	struct bpf_insn *insns_buf;
 	int nr_types;
 	int *type_mapping;
+	int *prologue_fds;
 };
 
 struct bpf_perf_object {
@@ -56,6 +58,11 @@ struct bpf_perf_object {
 	struct bpf_object *obj;
 };
 
+struct bpf_preproc_result {
+	struct bpf_insn *new_insn_ptr;
+	int new_insn_cnt;
+};
+
 static LIST_HEAD(bpf_objects_list);
 static struct hashmap *bpf_program_hash;
 static struct hashmap *bpf_map_hash;
@@ -233,14 +240,31 @@ struct bpf_object *bpf__prepare_load(const char *filename, bool source)
 	return obj;
 }
 
+static void close_prologue_programs(struct bpf_prog_priv *priv)
+{
+	struct perf_probe_event *pev;
+	int i, fd;
+
+	if (!priv->need_prologue)
+		return;
+	pev = &priv->pev;
+	for (i = 0; i < pev->ntevs; i++) {
+		fd = priv->prologue_fds[i];
+		if (fd != -1)
+			close(fd);
+	}
+}
+
 static void
 clear_prog_priv(const struct bpf_program *prog __maybe_unused,
 		void *_priv)
 {
 	struct bpf_prog_priv *priv = _priv;
 
+	close_prologue_programs(priv);
 	cleanup_perf_probe_events(&priv->pev, 1);
 	zfree(&priv->insns_buf);
+	zfree(&priv->prologue_fds);
 	zfree(&priv->type_mapping);
 	zfree(&priv->sys_name);
 	zfree(&priv->evt_name);
@@ -603,8 +627,8 @@ static int bpf__prepare_probe(void)
 
 static int
 preproc_gen_prologue(struct bpf_program *prog, int n,
-		     struct bpf_insn *orig_insns, int orig_insns_cnt,
-		     struct bpf_prog_prep_result *res)
+		     const struct bpf_insn *orig_insns, int orig_insns_cnt,
+		     struct bpf_preproc_result *res)
 {
 	struct bpf_prog_priv *priv = program_priv(prog);
 	struct probe_trace_event *tev;
@@ -652,7 +676,6 @@ preproc_gen_prologue(struct bpf_program *prog, int n,
 
 	res->new_insn_ptr = buf;
 	res->new_insn_cnt = prologue_cnt + orig_insns_cnt;
-	res->pfd = NULL;
 	return 0;
 
 errout:
@@ -760,7 +783,7 @@ static int hook_load_preprocessor(struct bpf_program *prog)
 	struct bpf_prog_priv *priv = program_priv(prog);
 	struct perf_probe_event *pev;
 	bool need_prologue = false;
-	int err, i;
+	int i;
 
 	if (IS_ERR_OR_NULL(priv)) {
 		pr_debug("Internal error when hook preprocessor\n");
@@ -798,6 +821,13 @@ static int hook_load_preprocessor(struct bpf_program *prog)
 		return -ENOMEM;
 	}
 
+	priv->prologue_fds = malloc(sizeof(int) * pev->ntevs);
+	if (!priv->prologue_fds) {
+		pr_debug("Not enough memory: alloc prologue fds failed\n");
+		return -ENOMEM;
+	}
+	memset(priv->prologue_fds, -1, sizeof(int) * pev->ntevs);
+
 	priv->type_mapping = malloc(sizeof(int) * pev->ntevs);
 	if (!priv->type_mapping) {
 		pr_debug("Not enough memory: alloc type_mapping failed\n");
@@ -806,13 +836,7 @@ static int hook_load_preprocessor(struct bpf_program *prog)
 	memset(priv->type_mapping, -1,
 	       sizeof(int) * pev->ntevs);
 
-	err = map_prologue(pev, priv->type_mapping, &priv->nr_types);
-	if (err)
-		return err;
-
-	err = bpf_program__set_prep(prog, priv->nr_types,
-				    preproc_gen_prologue);
-	return err;
+	return map_prologue(pev, priv->type_mapping, &priv->nr_types);
 }
 
 int bpf__probe(struct bpf_object *obj)
@@ -919,6 +943,77 @@ int bpf__unprobe(struct bpf_object *obj)
 	return ret;
 }
 
+static int bpf_object__load_prologue(struct bpf_object *obj)
+{
+	int init_cnt = ARRAY_SIZE(prologue_init_insn);
+	const struct bpf_insn *orig_insns;
+	struct bpf_preproc_result res;
+	struct perf_probe_event *pev;
+	struct bpf_program *prog;
+	int orig_insns_cnt;
+
+	bpf_object__for_each_program(prog, obj) {
+		struct bpf_prog_priv *priv = program_priv(prog);
+		int err, i, fd;
+
+		if (IS_ERR_OR_NULL(priv)) {
+			pr_debug("bpf: failed to get private field\n");
+			return -BPF_LOADER_ERRNO__INTERNAL;
+		}
+
+		if (!priv->need_prologue)
+			continue;
+
+		/*
+		 * For each program that needs prologue we do following:
+		 *
+		 * - take its current instructions and use them
+		 *   to generate the new code with prologue
+		 * - load new instructions with bpf_prog_load
+		 *   and keep the fd in prologue_fds
+		 * - new fd will be used in bpf__foreach_event
+		 *   to connect this program with perf evsel
+		 */
+		orig_insns = bpf_program__insns(prog);
+		orig_insns_cnt = bpf_program__insn_cnt(prog);
+
+		pev = &priv->pev;
+		for (i = 0; i < pev->ntevs; i++) {
+			/*
+			 * Skipping artificall prologue_init_insn instructions
+			 * (init_cnt), so the prologue can be generated instead
+			 * of them.
+			 */
+			err = preproc_gen_prologue(prog, i,
+						   orig_insns + init_cnt,
+						   orig_insns_cnt - init_cnt,
+						   &res);
+			if (err)
+				return err;
+
+			fd = bpf_prog_load(bpf_program__get_type(prog),
+					   bpf_program__name(prog), "GPL",
+					   res.new_insn_ptr,
+					   res.new_insn_cnt, NULL);
+			if (fd < 0) {
+				char bf[128];
+
+				libbpf_strerror(-errno, bf, sizeof(bf));
+				pr_debug("bpf: load objects with prologue failed: err=%d: (%s)\n",
+					 -errno, bf);
+				return -errno;
+			}
+			priv->prologue_fds[i] = fd;
+		}
+		/*
+		 * We no longer need the original program,
+		 * we can unload it.
+		 */
+		bpf_program__unload(prog);
+	}
+	return 0;
+}
+
 int bpf__load(struct bpf_object *obj)
 {
 	int err;
@@ -930,7 +1025,7 @@ int bpf__load(struct bpf_object *obj)
 		pr_debug("bpf: load objects failed: err=%d: (%s)\n", err, bf);
 		return err;
 	}
-	return 0;
+	return bpf_object__load_prologue(obj);
 }
 
 int bpf__foreach_event(struct bpf_object *obj,
@@ -965,13 +1060,10 @@ int bpf__foreach_event(struct bpf_object *obj,
 		for (i = 0; i < pev->ntevs; i++) {
 			tev = &pev->tevs[i];
 
-			if (priv->need_prologue) {
-				int type = priv->type_mapping[i];
-
-				fd = bpf_program__nth_fd(prog, type);
-			} else {
+			if (priv->need_prologue)
+				fd = priv->prologue_fds[i];
+			else
 				fd = bpf_program__fd(prog);
-			}
 
 			if (fd < 0) {
 				pr_debug("bpf: failed to get file descriptor\n");
-- 
2.35.3


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

* Re: [PATCHv4 bpf-next 0/2] perf tools: Fix prologue generation
  2022-06-03 20:45 [PATCHv4 bpf-next 0/2] perf tools: Fix prologue generation Jiri Olsa
  2022-06-03 20:45 ` [PATCHv4 bpf-next 1/2] perf tools: Register fallback libbpf section handler Jiri Olsa
  2022-06-03 20:45 ` [PATCHv4 bpf-next 2/2] perf tools: Rework prologue generation code Jiri Olsa
@ 2022-06-09 20:31 ` Andrii Nakryiko
  2022-06-10 18:34   ` Arnaldo Carvalho de Melo
  2 siblings, 1 reply; 13+ messages in thread
From: Andrii Nakryiko @ 2022-06-09 20:31 UTC (permalink / raw)
  To: Jiri Olsa, Arnaldo Carvalho de Melo
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	linux-perf-use.,
	Networking, bpf, Ingo Molnar, Namhyung Kim, Alexander Shishkin,
	Peter Zijlstra, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, Ian Rogers

On Fri, Jun 3, 2022 at 1:45 PM Jiri Olsa <jolsa@kernel.org> wrote:
>
> hi,
> sending change we discussed some time ago [1] to get rid of
> some deprecated functions we use in perf prologue code.
>
> Despite the gloomy discussion I think the final code does
> not look that bad ;-)
>
> This patchset removes following libbpf functions from perf:
>   bpf_program__set_prep
>   bpf_program__nth_fd
>   struct bpf_prog_prep_result
>
> v4 changes:
>   - fix typo [Andrii]
>
> v3 changes:
>   - removed R0/R1 zero init in libbpf_prog_prepare_load_fn,
>     because it's not needed [Andrii]
>   - rebased/post on top of bpf-next/master which now has
>     all the needed perf/core changes
>
> v2 changes:
>   - use fallback section prog handler, so we don't need to
>     use section prefix [Andrii]
>   - realloc prog->insns array in bpf_program__set_insns [Andrii]
>   - squash patch 1 from previous version with
>     bpf_program__set_insns change [Daniel]
>   - patch 3 already merged [Arnaldo]
>   - added more comments
>
> thanks,
> jirka
>

Arnaldo, can I get an ack from you for this patch set? Thank you!

>
> [1] https://lore.kernel.org/bpf/CAEf4BzaiBO3_617kkXZdYJ8hS8YF--ZLgapNbgeeEJ-pY0H88g@mail.gmail.com/
> ---
> Jiri Olsa (2):
>       perf tools: Register fallback libbpf section handler
>       perf tools: Rework prologue generation code
>
>  tools/perf/util/bpf-loader.c | 173 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------
>  1 file changed, 155 insertions(+), 18 deletions(-)

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

* Re: [PATCHv4 bpf-next 0/2] perf tools: Fix prologue generation
  2022-06-09 20:31 ` [PATCHv4 bpf-next 0/2] perf tools: Fix prologue generation Andrii Nakryiko
@ 2022-06-10 18:34   ` Arnaldo Carvalho de Melo
  2022-06-10 18:38     ` Arnaldo Carvalho de Melo
  2022-06-10 18:45     ` Andrii Nakryiko
  0 siblings, 2 replies; 13+ messages in thread
From: Arnaldo Carvalho de Melo @ 2022-06-10 18:34 UTC (permalink / raw)
  To: Jiri Olsa, Andrii Nakryiko
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	linux-perf-use.,
	Networking, bpf, Ingo Molnar, Namhyung Kim, Alexander Shishkin,
	Peter Zijlstra, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, Ian Rogers

Em Thu, Jun 09, 2022 at 01:31:52PM -0700, Andrii Nakryiko escreveu:
> On Fri, Jun 3, 2022 at 1:45 PM Jiri Olsa <jolsa@kernel.org> wrote:
> >
> > hi,
> > sending change we discussed some time ago [1] to get rid of
> > some deprecated functions we use in perf prologue code.
> >
> > Despite the gloomy discussion I think the final code does
> > not look that bad ;-)
> >
> > This patchset removes following libbpf functions from perf:
> >   bpf_program__set_prep
> >   bpf_program__nth_fd
> >   struct bpf_prog_prep_result
> >
> > v4 changes:
> >   - fix typo [Andrii]
> >
> > v3 changes:
> >   - removed R0/R1 zero init in libbpf_prog_prepare_load_fn,
> >     because it's not needed [Andrii]
> >   - rebased/post on top of bpf-next/master which now has
> >     all the needed perf/core changes
> >
> > v2 changes:
> >   - use fallback section prog handler, so we don't need to
> >     use section prefix [Andrii]
> >   - realloc prog->insns array in bpf_program__set_insns [Andrii]
> >   - squash patch 1 from previous version with
> >     bpf_program__set_insns change [Daniel]
> >   - patch 3 already merged [Arnaldo]
> >   - added more comments
> >
> > thanks,
> > jirka
> >
> 
> Arnaldo, can I get an ack from you for this patch set? Thank you!

So, before these patches:

[acme@quaco perf-urgent]$ git log --oneline -5
22905f78d181f446 (HEAD) libperf evsel: Open shouldn't leak fd on failure
a3c6da3dbd4bdf9c perf test: Fix "perf stat CSV output linter" test on s390
785cb9e85e8ba66f perf unwind: Fix uninitialized variable
874c8ca1e60b2c56 netfs: Fix gcc-12 warning by embedding vfs inode in netfs_i_context
3d9f55c57bc3659f Merge tag 'fs_for_v5.19-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs
[acme@quaco perf-urgent]$ sudo su -
[root@quaco ~]# perf -v
perf version 5.19.rc1.g22905f78d181
[root@quaco ~]# perf test 42
 42: BPF filter                                                      :
 42.1: Basic BPF filtering                                           : Ok
 42.2: BPF pinning                                                   : Ok
 42.3: BPF prologue generation                                       : Ok
[root@quaco ~]#

And after:

[acme@quaco perf-urgent]$ git log --oneline -5
f8ec656242acf2de (HEAD -> perf/urgent) perf tools: Rework prologue generation code
a750a8dd7e5d2d4b perf tools: Register fallback libbpf section handler
d28f2a8ad42af160 libperf evsel: Open shouldn't leak fd on failure
a3c6da3dbd4bdf9c perf test: Fix "perf stat CSV output linter" test on s390
785cb9e85e8ba66f perf unwind: Fix uninitialized variable
[acme@quaco perf-urgent]$ sudo su -
[root@quaco ~]# perf -v
perf version 5.19.rc1.gf8ec656242ac
[root@quaco ~]# perf test 42
 42: BPF filter                                                      :
 42.1: Basic BPF filtering                                           : FAILED!
 42.2: BPF pinning                                                   : FAILED!
 42.3: BPF prologue generation                                       : Ok
[root@quaco ~]# 

Jiri, can you try reproducing these? Do this require some other work
that is in bpf-next/master? Lemme try...

Further details:

[acme@quaco perf-urgent]$ clang -v
clang version 13.0.0 (five:git/llvm-project d667b96c98438edcc00ec85a3b151ac2dae862f3)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/12
Selected GCC installation: /usr/lib/gcc/x86_64-redhat-linux/12
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Selected multilib: .;@m64
[acme@quaco perf-urgent]$ cat /etc/fedora-release 
Fedora release 36 (Thirty Six)
[acme@quaco perf-urgent]$ gcc -v
Using built-in specs.
COLLECT_GCC=/usr/bin/gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/12/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --enable-libstdcxx-backtrace --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-12.1.1-20220507/obj-x86_64-redhat-linux/isl-install --enable-offload-targets=nvptx-none --without-cuda-driver --enable-offload-defaulted --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux --with-build-config=bootstrap-lto --enable-link-serialization=1
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.1.1 20220507 (Red Hat 12.1.1-1) (GCC) 
[acme@quaco perf-urgent]$

[root@quaco ~]# perf test -v 42
 42: BPF filter                                                      :
 42.1: Basic BPF filtering                                           :
--- start ---
test child forked, pid 638881
Kernel build dir is set to /lib/modules/5.17.11-300.fc36.x86_64/build
set env: KBUILD_DIR=/lib/modules/5.17.11-300.fc36.x86_64/build
unset env: KBUILD_OPTS
include option is set to -nostdinc -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h
set env: NR_CPUS=8
set env: LINUX_VERSION_CODE=0x5110b
set env: CLANG_EXEC=/usr/lib64/ccache/clang
set env: CLANG_OPTIONS=-xc -g
set env: KERNEL_INC_OPTIONS=-nostdinc -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h
set env: PERF_BPF_INC_OPTIONS=-I/home/acme/lib/perf/include/bpf
set env: WORKING_DIR=/lib/modules/5.17.11-300.fc36.x86_64/build
set env: CLANG_SOURCE=-
llvm compiling command template: echo '// SPDX-License-Identifier: GPL-2.0
/*
 * bpf-script-example.c
 * Test basic LLVM building
 */
#ifndef LINUX_VERSION_CODE
# error Need LINUX_VERSION_CODE
# error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig'
#endif
#define BPF_ANY 0
#define BPF_MAP_TYPE_ARRAY 2
#define BPF_FUNC_map_lookup_elem 1
#define BPF_FUNC_map_update_elem 2

static void *(*bpf_map_lookup_elem)(void *map, void *key) =
	(void *) BPF_FUNC_map_lookup_elem;
static void *(*bpf_map_update_elem)(void *map, void *key, void *value, int flags) =
	(void *) BPF_FUNC_map_update_elem;

struct bpf_map_def {
	unsigned int type;
	unsigned int key_size;
	unsigned int value_size;
	unsigned int max_entries;
};

#define SEC(NAME) __attribute__((section(NAME), used))
struct bpf_map_def SEC("maps") flip_table = {
	.type = BPF_MAP_TYPE_ARRAY,
	.key_size = sizeof(int),
	.value_size = sizeof(int),
	.max_entries = 1,
};

SEC("func=do_epoll_wait")
int bpf_func__SyS_epoll_pwait(void *ctx)
{
	int ind =0;
	int *flag = bpf_map_lookup_elem(&flip_table, &ind);
	int new_flag;
	if (!flag)
		return 0;
	/* flip flag and store back */
	new_flag = !*flag;
	bpf_map_update_elem(&flip_table, &ind, &new_flag, BPF_ANY);
	return new_flag;
}
char _license[] SEC("license") = "GPL";
int _version SEC("version") = LINUX_VERSION_CODE;
' | $CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS -DLINUX_VERSION_CODE=$LINUX_VERSION_CODE $CLANG_OPTIONS $PERF_BPF_INC_OPTIONS $KERNEL_INC_OPTIONS -Wno-unused-value -Wno-pointer-sign -working-directory $WORKING_DIR -c "$CLANG_SOURCE" -target bpf $CLANG_EMIT_LLVM -O2 -o - $LLVM_OPTIONS_PIPE
llvm compiling command : echo '// SPDX-License-Identifier: GPL-2.0
/*
 * bpf-script-example.c
 * Test basic LLVM building
 */
#ifndef LINUX_VERSION_CODE
# error Need LINUX_VERSION_CODE
# error Example: for 4.2 kernel, put 'clang-opt=-DLINUX_VERSION_CODE=0x40200 into llvm section of ~/.perfconfig'
#endif
#define BPF_ANY 0
#define BPF_MAP_TYPE_ARRAY 2
#define BPF_FUNC_map_lookup_elem 1
#define BPF_FUNC_map_update_elem 2

static void *(*bpf_map_lookup_elem)(void *map, void *key) =
	(void *) BPF_FUNC_map_lookup_elem;
static void *(*bpf_map_update_elem)(void *map, void *key, void *value, int flags) =
	(void *) BPF_FUNC_map_update_elem;

struct bpf_map_def {
	unsigned int type;
	unsigned int key_size;
	unsigned int value_size;
	unsigned int max_entries;
};

#define SEC(NAME) __attribute__((section(NAME), used))
struct bpf_map_def SEC(maps) flip_table = {
	.type = BPF_MAP_TYPE_ARRAY,
	.key_size = sizeof(int),
	.value_size = sizeof(int),
	.max_entries = 1,
};

SEC(func=do_epoll_wait)
int bpf_func__SyS_epoll_pwait(void *ctx)
{
	int ind =0;
	int *flag = bpf_map_lookup_elem(&flip_table, &ind);
	int new_flag;
	if (!flag)
		return 0;
	/* flip flag and store back */
	new_flag = !*flag;
	bpf_map_update_elem(&flip_table, &ind, &new_flag, BPF_ANY);
	return new_flag;
}
char _license[] SEC(license) = GPL;
int _version SEC(version) = LINUX_VERSION_CODE;
' | /usr/lib64/ccache/clang -D__KERNEL__ -D__NR_CPUS__=8 -DLINUX_VERSION_CODE=0x5110b -xc -g -I/home/acme/lib/perf/include/bpf -nostdinc -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h  -Wno-unused-value -Wno-pointer-sign -working-directory /lib/modules/5.17.11-300.fc36.x86_64/build -c - -target bpf  -O2 -o -
libbpf: loading object '[basic_bpf_test]' from buffer
libbpf: elf: section(3) func=do_epoll_wait, size 192, link 0, flags 6, type=1
libbpf: sec 'func=do_epoll_wait': found program 'bpf_func__SyS_epoll_pwait' at insn offset 0 (0 bytes), code size 24 insns (192 bytes)
libbpf: elf: section(4) .relfunc=do_epoll_wait, size 32, link 22, flags 0, type=9
libbpf: elf: section(5) maps, size 16, link 0, flags 3, type=1
libbpf: elf: section(6) license, size 4, link 0, flags 3, type=1
libbpf: license of [basic_bpf_test] is GPL
libbpf: elf: section(7) version, size 4, link 0, flags 3, type=1
libbpf: kernel version of [basic_bpf_test] is 5110b
libbpf: elf: section(13) .BTF, size 576, link 0, flags 0, type=1
libbpf: elf: section(15) .BTF.ext, size 256, link 0, flags 0, type=1
libbpf: elf: section(22) .symtab, size 336, link 1, flags 0, type=2
libbpf: looking for externs among 14 symbols...
libbpf: collected 0 externs total
libbpf: elf: found 1 legacy map definitions (16 bytes) in [basic_bpf_test]
libbpf: map 'flip_table' (legacy): legacy map definitions are deprecated, use BTF-defined maps instead
libbpf: map 'flip_table' (legacy): at sec_idx 5, offset 0.
libbpf: map 11 is "flip_table"
libbpf: Use of BPF_ANNOTATE_KV_PAIR is deprecated, use BTF-defined maps in .maps section instead
libbpf: map:flip_table container_name:____btf_map_flip_table cannot be found in BTF. Missing BPF_ANNOTATE_KV_PAIR?
libbpf: sec '.relfunc=do_epoll_wait': collecting relocation for section(3) 'func=do_epoll_wait'
libbpf: sec '.relfunc=do_epoll_wait': relo #0: insn #4 against 'flip_table'
libbpf: prog 'bpf_func__SyS_epoll_pwait': found map 0 (flip_table, sec 5, off 0) for insn #4
libbpf: sec '.relfunc=do_epoll_wait': relo #1: insn #17 against 'flip_table'
libbpf: prog 'bpf_func__SyS_epoll_pwait': found map 0 (flip_table, sec 5, off 0) for insn #17
bpf: config program 'func=do_epoll_wait'
symbol:do_epoll_wait file:(null) line:0 offset:0 return:0 lazy:(null)
bpf: config 'func=do_epoll_wait' is ok
Looking at the vmlinux_path (8 entries long)
symsrc__init: build id mismatch for vmlinux.
Using /usr/lib/debug/lib/modules/5.17.11-300.fc36.x86_64/vmlinux for symbols
Open Debuginfo file: /usr/lib/debug/.build-id/f2/26f5d75e6842add57095a0615a1e5c16783dd7.debug
Try to find probe point from debuginfo.
Matched function: do_epoll_wait [38063fb]
Probe point found: do_epoll_wait+0
Found 1 probe_trace_events.
Opening /sys/kernel/tracing//kprobe_events write=1
Opening /sys/kernel/tracing//README write=0
Writing event: p:perf_bpf_probe/func _text+3943040
libbpf: map 'flip_table': created successfully, fd=4
libbpf: prog 'bpf_func__SyS_epoll_pwait': BPF program load failed: Invalid argument
libbpf: prog 'bpf_func__SyS_epoll_pwait': -- BEGIN PROG LOAD LOG --
Invalid insn code at line_info[11].insn_off
processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
-- END PROG LOAD LOG --
libbpf: failed to load program 'bpf_func__SyS_epoll_pwait'
libbpf: failed to load object '[basic_bpf_test]'
bpf: load objects failed: err=-22: (Invalid argument)
Failed to add events selected by BPF
Opening /sys/kernel/tracing//kprobe_events write=1
Opening /sys/kernel/tracing//uprobe_events write=1
Parsing probe_events: p:perf_bpf_probe/func _text+3943040
Group:perf_bpf_probe Event:func probe:p
Writing event: -:perf_bpf_probe/func
test child finished with -1
---- end ----
BPF filter subtest 1: FAILED!
 42.2: BPF pinning                                                   :
--- start ---
test child forked, pid 639077
Kernel build dir is set to /lib/modules/5.17.11-300.fc36.x86_64/build
set env: KBUILD_DIR=/lib/modules/5.17.11-300.fc36.x86_64/build
unset env: KBUILD_OPTS
include option is set to -nostdinc -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h
set env: NR_CPUS=8
set env: LINUX_VERSION_CODE=0x5110b
set env: CLANG_EXEC=/usr/lib64/ccache/clang
set env: CLANG_OPTIONS=-xc -g
set env: KERNEL_INC_OPTIONS=-nostdinc -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h
set env: PERF_BPF_INC_OPTIONS=-I/home/acme/lib/perf/include/bpf
set env: WORKING_DIR=/lib/modules/5.17.11-300.fc36.x86_64/build
set env: CLANG_SOURCE=-
llvm compiling command template: echo '// SPDX-License-Identifier: GPL-2.0
/*
 * bpf-script-example.c
 * Test basic LLVM building
 */
#ifndef LINUX_VERSION_CODE
# error Need LINUX_VERSION_CODE
# error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig'
#endif
#define BPF_ANY 0
#define BPF_MAP_TYPE_ARRAY 2
#define BPF_FUNC_map_lookup_elem 1
#define BPF_FUNC_map_update_elem 2

static void *(*bpf_map_lookup_elem)(void *map, void *key) =
	(void *) BPF_FUNC_map_lookup_elem;
static void *(*bpf_map_update_elem)(void *map, void *key, void *value, int flags) =
	(void *) BPF_FUNC_map_update_elem;

struct bpf_map_def {
	unsigned int type;
	unsigned int key_size;
	unsigned int value_size;
	unsigned int max_entries;
};

#define SEC(NAME) __attribute__((section(NAME), used))
struct bpf_map_def SEC("maps") flip_table = {
	.type = BPF_MAP_TYPE_ARRAY,
	.key_size = sizeof(int),
	.value_size = sizeof(int),
	.max_entries = 1,
};

SEC("func=do_epoll_wait")
int bpf_func__SyS_epoll_pwait(void *ctx)
{
	int ind =0;
	int *flag = bpf_map_lookup_elem(&flip_table, &ind);
	int new_flag;
	if (!flag)
		return 0;
	/* flip flag and store back */
	new_flag = !*flag;
	bpf_map_update_elem(&flip_table, &ind, &new_flag, BPF_ANY);
	return new_flag;
}
char _license[] SEC("license") = "GPL";
int _version SEC("version") = LINUX_VERSION_CODE;
' | $CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS -DLINUX_VERSION_CODE=$LINUX_VERSION_CODE $CLANG_OPTIONS $PERF_BPF_INC_OPTIONS $KERNEL_INC_OPTIONS -Wno-unused-value -Wno-pointer-sign -working-directory $WORKING_DIR -c "$CLANG_SOURCE" -target bpf $CLANG_EMIT_LLVM -O2 -o - $LLVM_OPTIONS_PIPE
llvm compiling command : echo '// SPDX-License-Identifier: GPL-2.0
/*
 * bpf-script-example.c
 * Test basic LLVM building
 */
#ifndef LINUX_VERSION_CODE
# error Need LINUX_VERSION_CODE
# error Example: for 4.2 kernel, put 'clang-opt=-DLINUX_VERSION_CODE=0x40200 into llvm section of ~/.perfconfig'
#endif
#define BPF_ANY 0
#define BPF_MAP_TYPE_ARRAY 2
#define BPF_FUNC_map_lookup_elem 1
#define BPF_FUNC_map_update_elem 2

static void *(*bpf_map_lookup_elem)(void *map, void *key) =
	(void *) BPF_FUNC_map_lookup_elem;
static void *(*bpf_map_update_elem)(void *map, void *key, void *value, int flags) =
	(void *) BPF_FUNC_map_update_elem;

struct bpf_map_def {
	unsigned int type;
	unsigned int key_size;
	unsigned int value_size;
	unsigned int max_entries;
};

#define SEC(NAME) __attribute__((section(NAME), used))
struct bpf_map_def SEC(maps) flip_table = {
	.type = BPF_MAP_TYPE_ARRAY,
	.key_size = sizeof(int),
	.value_size = sizeof(int),
	.max_entries = 1,
};

SEC(func=do_epoll_wait)
int bpf_func__SyS_epoll_pwait(void *ctx)
{
	int ind =0;
	int *flag = bpf_map_lookup_elem(&flip_table, &ind);
	int new_flag;
	if (!flag)
		return 0;
	/* flip flag and store back */
	new_flag = !*flag;
	bpf_map_update_elem(&flip_table, &ind, &new_flag, BPF_ANY);
	return new_flag;
}
char _license[] SEC(license) = GPL;
int _version SEC(version) = LINUX_VERSION_CODE;
' | /usr/lib64/ccache/clang -D__KERNEL__ -D__NR_CPUS__=8 -DLINUX_VERSION_CODE=0x5110b -xc -g -I/home/acme/lib/perf/include/bpf -nostdinc -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h  -Wno-unused-value -Wno-pointer-sign -working-directory /lib/modules/5.17.11-300.fc36.x86_64/build -c - -target bpf  -O2 -o -
libbpf: loading object '[bpf_pinning]' from buffer
libbpf: elf: section(3) func=do_epoll_wait, size 192, link 0, flags 6, type=1
libbpf: sec 'func=do_epoll_wait': found program 'bpf_func__SyS_epoll_pwait' at insn offset 0 (0 bytes), code size 24 insns (192 bytes)
libbpf: elf: section(4) .relfunc=do_epoll_wait, size 32, link 22, flags 0, type=9
libbpf: elf: section(5) maps, size 16, link 0, flags 3, type=1
libbpf: elf: section(6) license, size 4, link 0, flags 3, type=1
libbpf: license of [bpf_pinning] is GPL
libbpf: elf: section(7) version, size 4, link 0, flags 3, type=1
libbpf: kernel version of [bpf_pinning] is 5110b
libbpf: elf: section(13) .BTF, size 576, link 0, flags 0, type=1
libbpf: elf: section(15) .BTF.ext, size 256, link 0, flags 0, type=1
libbpf: elf: section(22) .symtab, size 336, link 1, flags 0, type=2
libbpf: looking for externs among 14 symbols...
libbpf: collected 0 externs total
libbpf: elf: found 1 legacy map definitions (16 bytes) in [bpf_pinning]
libbpf: map 'flip_table' (legacy): legacy map definitions are deprecated, use BTF-defined maps instead
libbpf: map 'flip_table' (legacy): at sec_idx 5, offset 0.
libbpf: map 11 is "flip_table"
libbpf: Use of BPF_ANNOTATE_KV_PAIR is deprecated, use BTF-defined maps in .maps section instead
libbpf: map:flip_table container_name:____btf_map_flip_table cannot be found in BTF. Missing BPF_ANNOTATE_KV_PAIR?
libbpf: sec '.relfunc=do_epoll_wait': collecting relocation for section(3) 'func=do_epoll_wait'
libbpf: sec '.relfunc=do_epoll_wait': relo #0: insn #4 against 'flip_table'
libbpf: prog 'bpf_func__SyS_epoll_pwait': found map 0 (flip_table, sec 5, off 0) for insn #4
libbpf: sec '.relfunc=do_epoll_wait': relo #1: insn #17 against 'flip_table'
libbpf: prog 'bpf_func__SyS_epoll_pwait': found map 0 (flip_table, sec 5, off 0) for insn #17
bpf: config program 'func=do_epoll_wait'
symbol:do_epoll_wait file:(null) line:0 offset:0 return:0 lazy:(null)
bpf: config 'func=do_epoll_wait' is ok
Looking at the vmlinux_path (8 entries long)
symsrc__init: build id mismatch for vmlinux.
Using /usr/lib/debug/lib/modules/5.17.11-300.fc36.x86_64/vmlinux for symbols
Open Debuginfo file: /usr/lib/debug/.build-id/f2/26f5d75e6842add57095a0615a1e5c16783dd7.debug
Try to find probe point from debuginfo.
Matched function: do_epoll_wait [38063fb]
Probe point found: do_epoll_wait+0
Found 1 probe_trace_events.
Opening /sys/kernel/tracing//kprobe_events write=1
Opening /sys/kernel/tracing//README write=0
Writing event: p:perf_bpf_probe/func _text+3943040
libbpf: map 'flip_table': created successfully, fd=4
libbpf: prog 'bpf_func__SyS_epoll_pwait': BPF program load failed: Invalid argument
libbpf: prog 'bpf_func__SyS_epoll_pwait': -- BEGIN PROG LOAD LOG --
Invalid insn code at line_info[11].insn_off
processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
-- END PROG LOAD LOG --
libbpf: failed to load program 'bpf_func__SyS_epoll_pwait'
libbpf: failed to load object '[bpf_pinning]'
bpf: load objects failed: err=-22: (Invalid argument)
Failed to add events selected by BPF
Opening /sys/kernel/tracing//kprobe_events write=1
Opening /sys/kernel/tracing//uprobe_events write=1
Parsing probe_events: p:perf_bpf_probe/func _text+3943040
Group:perf_bpf_probe Event:func probe:p
Writing event: -:perf_bpf_probe/func
test child finished with -1
---- end ----
BPF filter subtest 2: FAILED!
 42.3: BPF prologue generation                                       :
--- start ---
test child forked, pid 639274
Kernel build dir is set to /lib/modules/5.17.11-300.fc36.x86_64/build
set env: KBUILD_DIR=/lib/modules/5.17.11-300.fc36.x86_64/build
unset env: KBUILD_OPTS
include option is set to -nostdinc -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h
set env: NR_CPUS=8
set env: LINUX_VERSION_CODE=0x5110b
set env: CLANG_EXEC=/usr/lib64/ccache/clang
set env: CLANG_OPTIONS=-xc -g
set env: KERNEL_INC_OPTIONS=-nostdinc -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h
set env: PERF_BPF_INC_OPTIONS=-I/home/acme/lib/perf/include/bpf
set env: WORKING_DIR=/lib/modules/5.17.11-300.fc36.x86_64/build
set env: CLANG_SOURCE=-
llvm compiling command template: echo '// SPDX-License-Identifier: GPL-2.0
/*
 * bpf-script-test-prologue.c
 * Test BPF prologue
 */
#ifndef LINUX_VERSION_CODE
# error Need LINUX_VERSION_CODE
# error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig'
#endif
#define SEC(NAME) __attribute__((section(NAME), used))

#include <uapi/linux/fs.h>

/*
 * If CONFIG_PROFILE_ALL_BRANCHES is selected,
 * 'if' is redefined after include kernel header.
 * Recover 'if' for BPF object code.
 */
#ifdef if
# undef if
#endif

#define FMODE_READ		0x1
#define FMODE_WRITE		0x2

static void (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) =
	(void *) 6;

SEC("func=null_lseek file->f_mode offset orig")
int bpf_func__null_lseek(void *ctx, int err, unsigned long _f_mode,
			 unsigned long offset, unsigned long orig)
{
	fmode_t f_mode = (fmode_t)_f_mode;

	if (err)
		return 0;
	if (f_mode & FMODE_WRITE)
		return 0;
	if (offset & 1)
		return 0;
	if (orig == SEEK_CUR)
		return 0;
	return 1;
}

char _license[] SEC("license") = "GPL";
int _version SEC("version") = LINUX_VERSION_CODE;
' | $CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS -DLINUX_VERSION_CODE=$LINUX_VERSION_CODE $CLANG_OPTIONS $PERF_BPF_INC_OPTIONS $KERNEL_INC_OPTIONS -Wno-unused-value -Wno-pointer-sign -working-directory $WORKING_DIR -c "$CLANG_SOURCE" -target bpf $CLANG_EMIT_LLVM -O2 -o - $LLVM_OPTIONS_PIPE
llvm compiling command :
libbpf: loading object '[bpf_prologue_test]' from buffer
libbpf: elf: section(3) func=null_lseek file->f_mode offset orig, size 112, link 0, flags 6, type=1
libbpf: sec 'func=null_lseek file->f_mode offset orig': found program 'bpf_func__null_lseek' at insn offset 0 (0 bytes), code size 14 insns (112 bytes)
libbpf: elf: section(4) license, size 4, link 0, flags 3, type=1
libbpf: license of [bpf_prologue_test] is GPL
libbpf: elf: section(5) version, size 4, link 0, flags 3, type=1
libbpf: kernel version of [bpf_prologue_test] is 5110b
libbpf: elf: section(11) .BTF, size 489, link 0, flags 0, type=1
libbpf: elf: section(13) .BTF.ext, size 144, link 0, flags 0, type=1
libbpf: elf: section(20) .symtab, size 312, link 1, flags 0, type=2
libbpf: looking for externs among 13 symbols...
libbpf: collected 0 externs total
bpf: config program 'func=null_lseek file->f_mode offset orig'
symbol:null_lseek file:(null) line:0 offset:0 return:0 lazy:(null)
parsing arg: file->f_mode into file, f_mode(1)
parsing arg: offset into offset
parsing arg: orig into orig
bpf: config 'func=null_lseek file->f_mode offset orig' is ok
Looking at the vmlinux_path (8 entries long)
symsrc__init: build id mismatch for vmlinux.
Using /usr/lib/debug/lib/modules/5.17.11-300.fc36.x86_64/vmlinux for symbols
Open Debuginfo file: /usr/lib/debug/.build-id/f2/26f5d75e6842add57095a0615a1e5c16783dd7.debug
Try to find probe point from debuginfo.
Opening /sys/kernel/tracing//README write=0
Matched function: null_lseek [73f7810]
Probe point found: null_lseek+0
Searching 'file' variable in context.
Converting variable file into trace event.
converting f_mode in file
f_mode type is unsigned int.
Searching 'offset' variable in context.
Converting variable offset into trace event.
offset type is long long int.
Searching 'orig' variable in context.
Converting variable orig into trace event.
orig type is int.
Found 1 probe_trace_events.
Opening /sys/kernel/tracing//kprobe_events write=1
Writing event: p:perf_bpf_probe/func _text+8502800 f_mode=+68(%di):x32 offset=%si:s64 orig=%dx:s32
In map_prologue, ntevs=1
mapping[0]=0
prologue: pass validation
prologue: slow path
prologue: fetch arg 0, base reg is %di
prologue: arg 0: offset 68
prologue: fetch arg 1, base reg is %si
prologue: fetch arg 2, base reg is %dx
prologue: load arg 0, insn_sz is BPF_W
prologue: load arg 1, insn_sz is BPF_DW
prologue: load arg 2, insn_sz is BPF_DW
add bpf event perf_bpf_probe:func and attach bpf program 5
adding perf_bpf_probe:func
adding perf_bpf_probe:func to 0x3ffa5d0
Using CPUID GenuineIntel-6-8E-A
mmap size 1052672B
Opening /sys/kernel/tracing//kprobe_events write=1
Opening /sys/kernel/tracing//uprobe_events write=1
Parsing probe_events: p:perf_bpf_probe/func _text+8502800 f_mode=+68(%di):x32 offset=%si:s64 orig=%dx:s32
Group:perf_bpf_probe Event:func probe:p
Writing event: -:perf_bpf_probe/func
test child finished with 0
---- end ----
BPF filter subtest 3: Ok
[root@quaco ~]#
[root@quaco ~]#

> > [1] https://lore.kernel.org/bpf/CAEf4BzaiBO3_617kkXZdYJ8hS8YF--ZLgapNbgeeEJ-pY0H88g@mail.gmail.com/
> > ---
> > Jiri Olsa (2):
> >       perf tools: Register fallback libbpf section handler
> >       perf tools: Rework prologue generation code
> >
> >  tools/perf/util/bpf-loader.c | 173 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------
> >  1 file changed, 155 insertions(+), 18 deletions(-)

-- 

- Arnaldo

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

* Re: [PATCHv4 bpf-next 0/2] perf tools: Fix prologue generation
  2022-06-10 18:34   ` Arnaldo Carvalho de Melo
@ 2022-06-10 18:38     ` Arnaldo Carvalho de Melo
  2022-06-10 18:45     ` Andrii Nakryiko
  1 sibling, 0 replies; 13+ messages in thread
From: Arnaldo Carvalho de Melo @ 2022-06-10 18:38 UTC (permalink / raw)
  To: Jiri Olsa, Andrii Nakryiko
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	linux-perf-use.,
	Networking, bpf, Ingo Molnar, Namhyung Kim, Alexander Shishkin,
	Peter Zijlstra, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, Ian Rogers

Em Fri, Jun 10, 2022 at 03:34:24PM -0300, Arnaldo Carvalho de Melo escreveu:
> Em Thu, Jun 09, 2022 at 01:31:52PM -0700, Andrii Nakryiko escreveu:
> > On Fri, Jun 3, 2022 at 1:45 PM Jiri Olsa <jolsa@kernel.org> wrote:
> > >
> > > hi,
> > > sending change we discussed some time ago [1] to get rid of
> > > some deprecated functions we use in perf prologue code.
> > >
> > > Despite the gloomy discussion I think the final code does
> > > not look that bad ;-)
> > >
> > > This patchset removes following libbpf functions from perf:
> > >   bpf_program__set_prep
> > >   bpf_program__nth_fd
> > >   struct bpf_prog_prep_result
> > >
> > > v4 changes:
> > >   - fix typo [Andrii]
> > >
> > > v3 changes:
> > >   - removed R0/R1 zero init in libbpf_prog_prepare_load_fn,
> > >     because it's not needed [Andrii]
> > >   - rebased/post on top of bpf-next/master which now has
> > >     all the needed perf/core changes
> > >
> > > v2 changes:
> > >   - use fallback section prog handler, so we don't need to
> > >     use section prefix [Andrii]
> > >   - realloc prog->insns array in bpf_program__set_insns [Andrii]
> > >   - squash patch 1 from previous version with
> > >     bpf_program__set_insns change [Daniel]
> > >   - patch 3 already merged [Arnaldo]
> > >   - added more comments
> > >
> > > thanks,
> > > jirka
> > >
> > 
> > Arnaldo, can I get an ack from you for this patch set? Thank you!
> 
> So, before these patches:
> 
> [acme@quaco perf-urgent]$ git log --oneline -5
> 22905f78d181f446 (HEAD) libperf evsel: Open shouldn't leak fd on failure
> a3c6da3dbd4bdf9c perf test: Fix "perf stat CSV output linter" test on s390
> 785cb9e85e8ba66f perf unwind: Fix uninitialized variable
> 874c8ca1e60b2c56 netfs: Fix gcc-12 warning by embedding vfs inode in netfs_i_context
> 3d9f55c57bc3659f Merge tag 'fs_for_v5.19-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs
> [acme@quaco perf-urgent]$ sudo su -
> [root@quaco ~]# perf -v
> perf version 5.19.rc1.g22905f78d181
> [root@quaco ~]# perf test 42
>  42: BPF filter                                                      :
>  42.1: Basic BPF filtering                                           : Ok
>  42.2: BPF pinning                                                   : Ok
>  42.3: BPF prologue generation                                       : Ok
> [root@quaco ~]#
> 
> And after:
> 
> [acme@quaco perf-urgent]$ git log --oneline -5
> f8ec656242acf2de (HEAD -> perf/urgent) perf tools: Rework prologue generation code
> a750a8dd7e5d2d4b perf tools: Register fallback libbpf section handler
> d28f2a8ad42af160 libperf evsel: Open shouldn't leak fd on failure
> a3c6da3dbd4bdf9c perf test: Fix "perf stat CSV output linter" test on s390
> 785cb9e85e8ba66f perf unwind: Fix uninitialized variable
> [acme@quaco perf-urgent]$ sudo su -
> [root@quaco ~]# perf -v
> perf version 5.19.rc1.gf8ec656242ac
> [root@quaco ~]# perf test 42
>  42: BPF filter                                                      :
>  42.1: Basic BPF filtering                                           : FAILED!
>  42.2: BPF pinning                                                   : FAILED!
>  42.3: BPF prologue generation                                       : Ok
> [root@quaco ~]# 
> 
> Jiri, can you try reproducing these? Do this require some other work
> that is in bpf-next/master? Lemme try...

No:

[acme@quaco perf-urgent]$ git log --oneline -5
d324948b907b292e (HEAD -> bpf-next-master) perf tools: Rework prologue generation code
911fc51320e09283 perf tools: Register fallback libbpf section handler
fe92833524e368e5 (bpf-next/master, bpf-next/for-next) libbpf: Fix uprobe symbol file offset calculation logic
492f99e4190a4574 bpf, docs: Fix typo "BFP_ALU" to "BPF_ALU"
0b817059a8830b8b bpftool: Fix bootstrapping during a cross compilation
[acme@quaco perf-urgent]$ sudo su -
[root@quaco ~]# perf -v
perf version 5.18.gd324948b907b
[root@quaco ~]# perf test 42
 42: BPF filter                                                      :
 42.1: Basic BPF filtering                                           : FAILED!
 42.2: BPF pinning                                                   : FAILED!
 42.3: BPF prologue generation                                       : Ok
[root@quaco ~]#

Removing these two patches:

[acme@quaco perf-urgent]$ git log --oneline -5
fe92833524e368e5 (HEAD -> bpf-next-master, bpf-next/master, bpf-next/for-next) libbpf: Fix uprobe symbol file offset calculation logic
492f99e4190a4574 bpf, docs: Fix typo "BFP_ALU" to "BPF_ALU"
0b817059a8830b8b bpftool: Fix bootstrapping during a cross compilation
d352bd889b6a9c97 Merge branch 'bpf: Add 64bit enum value support'
61dbd5982964074f docs/bpf: Update documentation for BTF_KIND_ENUM64 support
[acme@quaco perf-urgent]$ sudo su -
[root@quaco ~]# perf -v
perf version 5.18.gfe92833524e3
[root@quaco ~]# perf test 42
 42: BPF filter                                                      :
 42.1: Basic BPF filtering                                           : Ok
 42.2: BPF pinning                                                   : Ok
 42.3: BPF prologue generation                                       : Ok
[root@quaco ~]# 

- Arnaldo

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

* Re: [PATCHv4 bpf-next 0/2] perf tools: Fix prologue generation
  2022-06-10 18:34   ` Arnaldo Carvalho de Melo
  2022-06-10 18:38     ` Arnaldo Carvalho de Melo
@ 2022-06-10 18:45     ` Andrii Nakryiko
  2022-06-10 20:53       ` Jiri Olsa
  1 sibling, 1 reply; 13+ messages in thread
From: Andrii Nakryiko @ 2022-06-10 18:45 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Jiri Olsa, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	linux-perf-use.,
	Networking, bpf, Ingo Molnar, Namhyung Kim, Alexander Shishkin,
	Peter Zijlstra, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, Ian Rogers

On Fri, Jun 10, 2022 at 11:34 AM Arnaldo Carvalho de Melo
<acme@kernel.org> wrote:
>
> Em Thu, Jun 09, 2022 at 01:31:52PM -0700, Andrii Nakryiko escreveu:
> > On Fri, Jun 3, 2022 at 1:45 PM Jiri Olsa <jolsa@kernel.org> wrote:
> > >
> > > hi,
> > > sending change we discussed some time ago [1] to get rid of
> > > some deprecated functions we use in perf prologue code.
> > >
> > > Despite the gloomy discussion I think the final code does
> > > not look that bad ;-)
> > >
> > > This patchset removes following libbpf functions from perf:
> > >   bpf_program__set_prep
> > >   bpf_program__nth_fd
> > >   struct bpf_prog_prep_result
> > >
> > > v4 changes:
> > >   - fix typo [Andrii]
> > >
> > > v3 changes:
> > >   - removed R0/R1 zero init in libbpf_prog_prepare_load_fn,
> > >     because it's not needed [Andrii]
> > >   - rebased/post on top of bpf-next/master which now has
> > >     all the needed perf/core changes
> > >
> > > v2 changes:
> > >   - use fallback section prog handler, so we don't need to
> > >     use section prefix [Andrii]
> > >   - realloc prog->insns array in bpf_program__set_insns [Andrii]
> > >   - squash patch 1 from previous version with
> > >     bpf_program__set_insns change [Daniel]
> > >   - patch 3 already merged [Arnaldo]
> > >   - added more comments
> > >
> > > thanks,
> > > jirka
> > >
> >
> > Arnaldo, can I get an ack from you for this patch set? Thank you!
>
> So, before these patches:
>
> [acme@quaco perf-urgent]$ git log --oneline -5
> 22905f78d181f446 (HEAD) libperf evsel: Open shouldn't leak fd on failure
> a3c6da3dbd4bdf9c perf test: Fix "perf stat CSV output linter" test on s390
> 785cb9e85e8ba66f perf unwind: Fix uninitialized variable
> 874c8ca1e60b2c56 netfs: Fix gcc-12 warning by embedding vfs inode in netfs_i_context
> 3d9f55c57bc3659f Merge tag 'fs_for_v5.19-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs
> [acme@quaco perf-urgent]$ sudo su -
> [root@quaco ~]# perf -v
> perf version 5.19.rc1.g22905f78d181
> [root@quaco ~]# perf test 42
>  42: BPF filter                                                      :
>  42.1: Basic BPF filtering                                           : Ok
>  42.2: BPF pinning                                                   : Ok
>  42.3: BPF prologue generation                                       : Ok
> [root@quaco ~]#
>
> And after:
>
> [acme@quaco perf-urgent]$ git log --oneline -5
> f8ec656242acf2de (HEAD -> perf/urgent) perf tools: Rework prologue generation code
> a750a8dd7e5d2d4b perf tools: Register fallback libbpf section handler
> d28f2a8ad42af160 libperf evsel: Open shouldn't leak fd on failure
> a3c6da3dbd4bdf9c perf test: Fix "perf stat CSV output linter" test on s390
> 785cb9e85e8ba66f perf unwind: Fix uninitialized variable
> [acme@quaco perf-urgent]$ sudo su -
> [root@quaco ~]# perf -v
> perf version 5.19.rc1.gf8ec656242ac
> [root@quaco ~]# perf test 42
>  42: BPF filter                                                      :
>  42.1: Basic BPF filtering                                           : FAILED!
>  42.2: BPF pinning                                                   : FAILED!
>  42.3: BPF prologue generation                                       : Ok
> [root@quaco ~]#
>
> Jiri, can you try reproducing these? Do this require some other work
> that is in bpf-next/master? Lemme try...
>
> Further details:
>
> [acme@quaco perf-urgent]$ clang -v
> clang version 13.0.0 (five:git/llvm-project d667b96c98438edcc00ec85a3b151ac2dae862f3)
> Target: x86_64-unknown-linux-gnu
> Thread model: posix
> InstalledDir: /usr/local/bin
> Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/12
> Selected GCC installation: /usr/lib/gcc/x86_64-redhat-linux/12
> Candidate multilib: .;@m64
> Candidate multilib: 32;@m32
> Selected multilib: .;@m64
> [acme@quaco perf-urgent]$ cat /etc/fedora-release
> Fedora release 36 (Thirty Six)
> [acme@quaco perf-urgent]$ gcc -v
> Using built-in specs.
> COLLECT_GCC=/usr/bin/gcc
> COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/12/lto-wrapper
> OFFLOAD_TARGET_NAMES=nvptx-none
> OFFLOAD_TARGET_DEFAULT=1
> Target: x86_64-redhat-linux
> Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --enable-libstdcxx-backtrace --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-12.1.1-20220507/obj-x86_64-redhat-linux/isl-install --enable-offload-targets=nvptx-none --without-cuda-driver --enable-offload-defaulted --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux --with-build-config=bootstrap-lto --enable-link-serialization=1
> Thread model: posix
> Supported LTO compression algorithms: zlib zstd
> gcc version 12.1.1 20220507 (Red Hat 12.1.1-1) (GCC)
> [acme@quaco perf-urgent]$
>
> [root@quaco ~]# perf test -v 42
>  42: BPF filter                                                      :
>  42.1: Basic BPF filtering                                           :
> --- start ---
> test child forked, pid 638881
> Kernel build dir is set to /lib/modules/5.17.11-300.fc36.x86_64/build
> set env: KBUILD_DIR=/lib/modules/5.17.11-300.fc36.x86_64/build
> unset env: KBUILD_OPTS
> include option is set to -nostdinc -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h
> set env: NR_CPUS=8
> set env: LINUX_VERSION_CODE=0x5110b
> set env: CLANG_EXEC=/usr/lib64/ccache/clang
> set env: CLANG_OPTIONS=-xc -g
> set env: KERNEL_INC_OPTIONS=-nostdinc -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h
> set env: PERF_BPF_INC_OPTIONS=-I/home/acme/lib/perf/include/bpf
> set env: WORKING_DIR=/lib/modules/5.17.11-300.fc36.x86_64/build
> set env: CLANG_SOURCE=-
> llvm compiling command template: echo '// SPDX-License-Identifier: GPL-2.0
> /*
>  * bpf-script-example.c
>  * Test basic LLVM building
>  */
> #ifndef LINUX_VERSION_CODE
> # error Need LINUX_VERSION_CODE
> # error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig'
> #endif
> #define BPF_ANY 0
> #define BPF_MAP_TYPE_ARRAY 2
> #define BPF_FUNC_map_lookup_elem 1
> #define BPF_FUNC_map_update_elem 2
>
> static void *(*bpf_map_lookup_elem)(void *map, void *key) =
>         (void *) BPF_FUNC_map_lookup_elem;
> static void *(*bpf_map_update_elem)(void *map, void *key, void *value, int flags) =
>         (void *) BPF_FUNC_map_update_elem;
>
> struct bpf_map_def {
>         unsigned int type;
>         unsigned int key_size;
>         unsigned int value_size;
>         unsigned int max_entries;
> };
>
> #define SEC(NAME) __attribute__((section(NAME), used))
> struct bpf_map_def SEC("maps") flip_table = {
>         .type = BPF_MAP_TYPE_ARRAY,
>         .key_size = sizeof(int),
>         .value_size = sizeof(int),
>         .max_entries = 1,
> };
>
> SEC("func=do_epoll_wait")
> int bpf_func__SyS_epoll_pwait(void *ctx)
> {
>         int ind =0;
>         int *flag = bpf_map_lookup_elem(&flip_table, &ind);
>         int new_flag;
>         if (!flag)
>                 return 0;
>         /* flip flag and store back */
>         new_flag = !*flag;
>         bpf_map_update_elem(&flip_table, &ind, &new_flag, BPF_ANY);
>         return new_flag;
> }
> char _license[] SEC("license") = "GPL";
> int _version SEC("version") = LINUX_VERSION_CODE;
> ' | $CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS -DLINUX_VERSION_CODE=$LINUX_VERSION_CODE $CLANG_OPTIONS $PERF_BPF_INC_OPTIONS $KERNEL_INC_OPTIONS -Wno-unused-value -Wno-pointer-sign -working-directory $WORKING_DIR -c "$CLANG_SOURCE" -target bpf $CLANG_EMIT_LLVM -O2 -o - $LLVM_OPTIONS_PIPE
> llvm compiling command : echo '// SPDX-License-Identifier: GPL-2.0
> /*
>  * bpf-script-example.c
>  * Test basic LLVM building
>  */
> #ifndef LINUX_VERSION_CODE
> # error Need LINUX_VERSION_CODE
> # error Example: for 4.2 kernel, put 'clang-opt=-DLINUX_VERSION_CODE=0x40200 into llvm section of ~/.perfconfig'
> #endif
> #define BPF_ANY 0
> #define BPF_MAP_TYPE_ARRAY 2
> #define BPF_FUNC_map_lookup_elem 1
> #define BPF_FUNC_map_update_elem 2
>
> static void *(*bpf_map_lookup_elem)(void *map, void *key) =
>         (void *) BPF_FUNC_map_lookup_elem;
> static void *(*bpf_map_update_elem)(void *map, void *key, void *value, int flags) =
>         (void *) BPF_FUNC_map_update_elem;
>
> struct bpf_map_def {
>         unsigned int type;
>         unsigned int key_size;
>         unsigned int value_size;
>         unsigned int max_entries;
> };
>
> #define SEC(NAME) __attribute__((section(NAME), used))
> struct bpf_map_def SEC(maps) flip_table = {
>         .type = BPF_MAP_TYPE_ARRAY,
>         .key_size = sizeof(int),
>         .value_size = sizeof(int),
>         .max_entries = 1,
> };
>
> SEC(func=do_epoll_wait)
> int bpf_func__SyS_epoll_pwait(void *ctx)
> {
>         int ind =0;
>         int *flag = bpf_map_lookup_elem(&flip_table, &ind);
>         int new_flag;
>         if (!flag)
>                 return 0;
>         /* flip flag and store back */
>         new_flag = !*flag;
>         bpf_map_update_elem(&flip_table, &ind, &new_flag, BPF_ANY);
>         return new_flag;
> }
> char _license[] SEC(license) = GPL;
> int _version SEC(version) = LINUX_VERSION_CODE;
> ' | /usr/lib64/ccache/clang -D__KERNEL__ -D__NR_CPUS__=8 -DLINUX_VERSION_CODE=0x5110b -xc -g -I/home/acme/lib/perf/include/bpf -nostdinc -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h  -Wno-unused-value -Wno-pointer-sign -working-directory /lib/modules/5.17.11-300.fc36.x86_64/build -c - -target bpf  -O2 -o -
> libbpf: loading object '[basic_bpf_test]' from buffer
> libbpf: elf: section(3) func=do_epoll_wait, size 192, link 0, flags 6, type=1
> libbpf: sec 'func=do_epoll_wait': found program 'bpf_func__SyS_epoll_pwait' at insn offset 0 (0 bytes), code size 24 insns (192 bytes)
> libbpf: elf: section(4) .relfunc=do_epoll_wait, size 32, link 22, flags 0, type=9
> libbpf: elf: section(5) maps, size 16, link 0, flags 3, type=1
> libbpf: elf: section(6) license, size 4, link 0, flags 3, type=1
> libbpf: license of [basic_bpf_test] is GPL
> libbpf: elf: section(7) version, size 4, link 0, flags 3, type=1
> libbpf: kernel version of [basic_bpf_test] is 5110b
> libbpf: elf: section(13) .BTF, size 576, link 0, flags 0, type=1
> libbpf: elf: section(15) .BTF.ext, size 256, link 0, flags 0, type=1
> libbpf: elf: section(22) .symtab, size 336, link 1, flags 0, type=2
> libbpf: looking for externs among 14 symbols...
> libbpf: collected 0 externs total
> libbpf: elf: found 1 legacy map definitions (16 bytes) in [basic_bpf_test]
> libbpf: map 'flip_table' (legacy): legacy map definitions are deprecated, use BTF-defined maps instead
> libbpf: map 'flip_table' (legacy): at sec_idx 5, offset 0.
> libbpf: map 11 is "flip_table"
> libbpf: Use of BPF_ANNOTATE_KV_PAIR is deprecated, use BTF-defined maps in .maps section instead
> libbpf: map:flip_table container_name:____btf_map_flip_table cannot be found in BTF. Missing BPF_ANNOTATE_KV_PAIR?
> libbpf: sec '.relfunc=do_epoll_wait': collecting relocation for section(3) 'func=do_epoll_wait'
> libbpf: sec '.relfunc=do_epoll_wait': relo #0: insn #4 against 'flip_table'
> libbpf: prog 'bpf_func__SyS_epoll_pwait': found map 0 (flip_table, sec 5, off 0) for insn #4
> libbpf: sec '.relfunc=do_epoll_wait': relo #1: insn #17 against 'flip_table'
> libbpf: prog 'bpf_func__SyS_epoll_pwait': found map 0 (flip_table, sec 5, off 0) for insn #17
> bpf: config program 'func=do_epoll_wait'
> symbol:do_epoll_wait file:(null) line:0 offset:0 return:0 lazy:(null)
> bpf: config 'func=do_epoll_wait' is ok
> Looking at the vmlinux_path (8 entries long)
> symsrc__init: build id mismatch for vmlinux.
> Using /usr/lib/debug/lib/modules/5.17.11-300.fc36.x86_64/vmlinux for symbols
> Open Debuginfo file: /usr/lib/debug/.build-id/f2/26f5d75e6842add57095a0615a1e5c16783dd7.debug
> Try to find probe point from debuginfo.
> Matched function: do_epoll_wait [38063fb]
> Probe point found: do_epoll_wait+0
> Found 1 probe_trace_events.
> Opening /sys/kernel/tracing//kprobe_events write=1
> Opening /sys/kernel/tracing//README write=0
> Writing event: p:perf_bpf_probe/func _text+3943040
> libbpf: map 'flip_table': created successfully, fd=4
> libbpf: prog 'bpf_func__SyS_epoll_pwait': BPF program load failed: Invalid argument
> libbpf: prog 'bpf_func__SyS_epoll_pwait': -- BEGIN PROG LOAD LOG --
> Invalid insn code at line_info[11].insn_off

Mismatched line_info.

Jiri, I think we need to clear func_info and line_info in
bpf_program__set_insns() because at that point func/line info can be
mismatched and won't correspond to the actual set of instructions.

Arnaldo, thanks for testing and providing details!

> processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
> -- END PROG LOAD LOG --
> libbpf: failed to load program 'bpf_func__SyS_epoll_pwait'
> libbpf: failed to load object '[basic_bpf_test]'
> bpf: load objects failed: err=-22: (Invalid argument)
> Failed to add events selected by BPF
> Opening /sys/kernel/tracing//kprobe_events write=1
> Opening /sys/kernel/tracing//uprobe_events write=1
> Parsing probe_events: p:perf_bpf_probe/func _text+3943040
> Group:perf_bpf_probe Event:func probe:p
> Writing event: -:perf_bpf_probe/func
> test child finished with -1

[...]

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

* Re: [PATCHv4 bpf-next 0/2] perf tools: Fix prologue generation
  2022-06-10 18:45     ` Andrii Nakryiko
@ 2022-06-10 20:53       ` Jiri Olsa
  2022-06-12 16:25         ` Jiri Olsa
  0 siblings, 1 reply; 13+ messages in thread
From: Jiri Olsa @ 2022-06-10 20:53 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: Arnaldo Carvalho de Melo, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, linux-perf-use.,
	Networking, bpf, Ingo Molnar, Namhyung Kim, Alexander Shishkin,
	Peter Zijlstra, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, Ian Rogers

On Fri, Jun 10, 2022 at 11:45:48AM -0700, Andrii Nakryiko wrote:
> On Fri, Jun 10, 2022 at 11:34 AM Arnaldo Carvalho de Melo
> <acme@kernel.org> wrote:
> >
> > Em Thu, Jun 09, 2022 at 01:31:52PM -0700, Andrii Nakryiko escreveu:
> > > On Fri, Jun 3, 2022 at 1:45 PM Jiri Olsa <jolsa@kernel.org> wrote:
> > > >
> > > > hi,
> > > > sending change we discussed some time ago [1] to get rid of
> > > > some deprecated functions we use in perf prologue code.
> > > >
> > > > Despite the gloomy discussion I think the final code does
> > > > not look that bad ;-)
> > > >
> > > > This patchset removes following libbpf functions from perf:
> > > >   bpf_program__set_prep
> > > >   bpf_program__nth_fd
> > > >   struct bpf_prog_prep_result
> > > >
> > > > v4 changes:
> > > >   - fix typo [Andrii]
> > > >
> > > > v3 changes:
> > > >   - removed R0/R1 zero init in libbpf_prog_prepare_load_fn,
> > > >     because it's not needed [Andrii]
> > > >   - rebased/post on top of bpf-next/master which now has
> > > >     all the needed perf/core changes
> > > >
> > > > v2 changes:
> > > >   - use fallback section prog handler, so we don't need to
> > > >     use section prefix [Andrii]
> > > >   - realloc prog->insns array in bpf_program__set_insns [Andrii]
> > > >   - squash patch 1 from previous version with
> > > >     bpf_program__set_insns change [Daniel]
> > > >   - patch 3 already merged [Arnaldo]
> > > >   - added more comments
> > > >
> > > > thanks,
> > > > jirka
> > > >
> > >
> > > Arnaldo, can I get an ack from you for this patch set? Thank you!
> >
> > So, before these patches:
> >
> > [acme@quaco perf-urgent]$ git log --oneline -5
> > 22905f78d181f446 (HEAD) libperf evsel: Open shouldn't leak fd on failure
> > a3c6da3dbd4bdf9c perf test: Fix "perf stat CSV output linter" test on s390
> > 785cb9e85e8ba66f perf unwind: Fix uninitialized variable
> > 874c8ca1e60b2c56 netfs: Fix gcc-12 warning by embedding vfs inode in netfs_i_context
> > 3d9f55c57bc3659f Merge tag 'fs_for_v5.19-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs
> > [acme@quaco perf-urgent]$ sudo su -
> > [root@quaco ~]# perf -v
> > perf version 5.19.rc1.g22905f78d181
> > [root@quaco ~]# perf test 42
> >  42: BPF filter                                                      :
> >  42.1: Basic BPF filtering                                           : Ok
> >  42.2: BPF pinning                                                   : Ok
> >  42.3: BPF prologue generation                                       : Ok
> > [root@quaco ~]#
> >
> > And after:
> >
> > [acme@quaco perf-urgent]$ git log --oneline -5
> > f8ec656242acf2de (HEAD -> perf/urgent) perf tools: Rework prologue generation code
> > a750a8dd7e5d2d4b perf tools: Register fallback libbpf section handler
> > d28f2a8ad42af160 libperf evsel: Open shouldn't leak fd on failure
> > a3c6da3dbd4bdf9c perf test: Fix "perf stat CSV output linter" test on s390
> > 785cb9e85e8ba66f perf unwind: Fix uninitialized variable
> > [acme@quaco perf-urgent]$ sudo su -
> > [root@quaco ~]# perf -v
> > perf version 5.19.rc1.gf8ec656242ac
> > [root@quaco ~]# perf test 42
> >  42: BPF filter                                                      :
> >  42.1: Basic BPF filtering                                           : FAILED!
> >  42.2: BPF pinning                                                   : FAILED!
> >  42.3: BPF prologue generation                                       : Ok
> > [root@quaco ~]#
> >
> > Jiri, can you try reproducing these? Do this require some other work
> > that is in bpf-next/master? Lemme try...
> >
> > Further details:
> >
> > [acme@quaco perf-urgent]$ clang -v
> > clang version 13.0.0 (five:git/llvm-project d667b96c98438edcc00ec85a3b151ac2dae862f3)
> > Target: x86_64-unknown-linux-gnu
> > Thread model: posix
> > InstalledDir: /usr/local/bin
> > Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/12
> > Selected GCC installation: /usr/lib/gcc/x86_64-redhat-linux/12
> > Candidate multilib: .;@m64
> > Candidate multilib: 32;@m32
> > Selected multilib: .;@m64
> > [acme@quaco perf-urgent]$ cat /etc/fedora-release
> > Fedora release 36 (Thirty Six)
> > [acme@quaco perf-urgent]$ gcc -v
> > Using built-in specs.
> > COLLECT_GCC=/usr/bin/gcc
> > COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/12/lto-wrapper
> > OFFLOAD_TARGET_NAMES=nvptx-none
> > OFFLOAD_TARGET_DEFAULT=1
> > Target: x86_64-redhat-linux
> > Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --enable-libstdcxx-backtrace --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-12.1.1-20220507/obj-x86_64-redhat-linux/isl-install --enable-offload-targets=nvptx-none --without-cuda-driver --enable-offload-defaulted --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux --with-build-config=bootstrap-lto --enable-link-serialization=1
> > Thread model: posix
> > Supported LTO compression algorithms: zlib zstd
> > gcc version 12.1.1 20220507 (Red Hat 12.1.1-1) (GCC)
> > [acme@quaco perf-urgent]$
> >
> > [root@quaco ~]# perf test -v 42
> >  42: BPF filter                                                      :
> >  42.1: Basic BPF filtering                                           :
> > --- start ---
> > test child forked, pid 638881
> > Kernel build dir is set to /lib/modules/5.17.11-300.fc36.x86_64/build
> > set env: KBUILD_DIR=/lib/modules/5.17.11-300.fc36.x86_64/build
> > unset env: KBUILD_OPTS
> > include option is set to -nostdinc -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h
> > set env: NR_CPUS=8
> > set env: LINUX_VERSION_CODE=0x5110b
> > set env: CLANG_EXEC=/usr/lib64/ccache/clang
> > set env: CLANG_OPTIONS=-xc -g

ok, it's the BTF debug info as Andrii mentioned below,
I assume you have 'clang-opt=-g' in .perfconfig, right?

when I add it to mine I can reproduce, perfect

SNIP

> > bpf: config 'func=do_epoll_wait' is ok
> > Looking at the vmlinux_path (8 entries long)
> > symsrc__init: build id mismatch for vmlinux.
> > Using /usr/lib/debug/lib/modules/5.17.11-300.fc36.x86_64/vmlinux for symbols
> > Open Debuginfo file: /usr/lib/debug/.build-id/f2/26f5d75e6842add57095a0615a1e5c16783dd7.debug
> > Try to find probe point from debuginfo.
> > Matched function: do_epoll_wait [38063fb]
> > Probe point found: do_epoll_wait+0
> > Found 1 probe_trace_events.
> > Opening /sys/kernel/tracing//kprobe_events write=1
> > Opening /sys/kernel/tracing//README write=0
> > Writing event: p:perf_bpf_probe/func _text+3943040
> > libbpf: map 'flip_table': created successfully, fd=4
> > libbpf: prog 'bpf_func__SyS_epoll_pwait': BPF program load failed: Invalid argument
> > libbpf: prog 'bpf_func__SyS_epoll_pwait': -- BEGIN PROG LOAD LOG --
> > Invalid insn code at line_info[11].insn_off
> 
> Mismatched line_info.
> 
> Jiri, I think we need to clear func_info and line_info in
> bpf_program__set_insns() because at that point func/line info can be
> mismatched and won't correspond to the actual set of instructions.
> 
> Arnaldo, thanks for testing and providing details!

sounds good, I'll check on that.. now I can reproduce

thanks,
jirka

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

* Re: [PATCHv4 bpf-next 0/2] perf tools: Fix prologue generation
  2022-06-10 20:53       ` Jiri Olsa
@ 2022-06-12 16:25         ` Jiri Olsa
  2022-06-13 20:15           ` Arnaldo Carvalho de Melo
  2022-06-15 15:27           ` Arnaldo Carvalho de Melo
  0 siblings, 2 replies; 13+ messages in thread
From: Jiri Olsa @ 2022-06-12 16:25 UTC (permalink / raw)
  To: Andrii Nakryiko, Arnaldo Carvalho de Melo
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	linux-perf-use.,
	Networking, bpf, Ingo Molnar, Namhyung Kim, Alexander Shishkin,
	Peter Zijlstra, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, Ian Rogers

On Fri, Jun 10, 2022 at 10:53:57PM +0200, Jiri Olsa wrote:
> On Fri, Jun 10, 2022 at 11:45:48AM -0700, Andrii Nakryiko wrote:
> > On Fri, Jun 10, 2022 at 11:34 AM Arnaldo Carvalho de Melo
> > <acme@kernel.org> wrote:
> > >
> > > Em Thu, Jun 09, 2022 at 01:31:52PM -0700, Andrii Nakryiko escreveu:
> > > > On Fri, Jun 3, 2022 at 1:45 PM Jiri Olsa <jolsa@kernel.org> wrote:
> > > > >
> > > > > hi,
> > > > > sending change we discussed some time ago [1] to get rid of
> > > > > some deprecated functions we use in perf prologue code.
> > > > >
> > > > > Despite the gloomy discussion I think the final code does
> > > > > not look that bad ;-)
> > > > >
> > > > > This patchset removes following libbpf functions from perf:
> > > > >   bpf_program__set_prep
> > > > >   bpf_program__nth_fd
> > > > >   struct bpf_prog_prep_result
> > > > >
> > > > > v4 changes:
> > > > >   - fix typo [Andrii]
> > > > >
> > > > > v3 changes:
> > > > >   - removed R0/R1 zero init in libbpf_prog_prepare_load_fn,
> > > > >     because it's not needed [Andrii]
> > > > >   - rebased/post on top of bpf-next/master which now has
> > > > >     all the needed perf/core changes
> > > > >
> > > > > v2 changes:
> > > > >   - use fallback section prog handler, so we don't need to
> > > > >     use section prefix [Andrii]
> > > > >   - realloc prog->insns array in bpf_program__set_insns [Andrii]
> > > > >   - squash patch 1 from previous version with
> > > > >     bpf_program__set_insns change [Daniel]
> > > > >   - patch 3 already merged [Arnaldo]
> > > > >   - added more comments
> > > > >
> > > > > thanks,
> > > > > jirka
> > > > >
> > > >
> > > > Arnaldo, can I get an ack from you for this patch set? Thank you!
> > >
> > > So, before these patches:
> > >
> > > [acme@quaco perf-urgent]$ git log --oneline -5
> > > 22905f78d181f446 (HEAD) libperf evsel: Open shouldn't leak fd on failure
> > > a3c6da3dbd4bdf9c perf test: Fix "perf stat CSV output linter" test on s390
> > > 785cb9e85e8ba66f perf unwind: Fix uninitialized variable
> > > 874c8ca1e60b2c56 netfs: Fix gcc-12 warning by embedding vfs inode in netfs_i_context
> > > 3d9f55c57bc3659f Merge tag 'fs_for_v5.19-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs
> > > [acme@quaco perf-urgent]$ sudo su -
> > > [root@quaco ~]# perf -v
> > > perf version 5.19.rc1.g22905f78d181
> > > [root@quaco ~]# perf test 42
> > >  42: BPF filter                                                      :
> > >  42.1: Basic BPF filtering                                           : Ok
> > >  42.2: BPF pinning                                                   : Ok
> > >  42.3: BPF prologue generation                                       : Ok
> > > [root@quaco ~]#
> > >
> > > And after:
> > >
> > > [acme@quaco perf-urgent]$ git log --oneline -5
> > > f8ec656242acf2de (HEAD -> perf/urgent) perf tools: Rework prologue generation code
> > > a750a8dd7e5d2d4b perf tools: Register fallback libbpf section handler
> > > d28f2a8ad42af160 libperf evsel: Open shouldn't leak fd on failure
> > > a3c6da3dbd4bdf9c perf test: Fix "perf stat CSV output linter" test on s390
> > > 785cb9e85e8ba66f perf unwind: Fix uninitialized variable
> > > [acme@quaco perf-urgent]$ sudo su -
> > > [root@quaco ~]# perf -v
> > > perf version 5.19.rc1.gf8ec656242ac
> > > [root@quaco ~]# perf test 42
> > >  42: BPF filter                                                      :
> > >  42.1: Basic BPF filtering                                           : FAILED!
> > >  42.2: BPF pinning                                                   : FAILED!
> > >  42.3: BPF prologue generation                                       : Ok
> > > [root@quaco ~]#
> > >
> > > Jiri, can you try reproducing these? Do this require some other work
> > > that is in bpf-next/master? Lemme try...
> > >
> > > Further details:
> > >
> > > [acme@quaco perf-urgent]$ clang -v
> > > clang version 13.0.0 (five:git/llvm-project d667b96c98438edcc00ec85a3b151ac2dae862f3)
> > > Target: x86_64-unknown-linux-gnu
> > > Thread model: posix
> > > InstalledDir: /usr/local/bin
> > > Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/12
> > > Selected GCC installation: /usr/lib/gcc/x86_64-redhat-linux/12
> > > Candidate multilib: .;@m64
> > > Candidate multilib: 32;@m32
> > > Selected multilib: .;@m64
> > > [acme@quaco perf-urgent]$ cat /etc/fedora-release
> > > Fedora release 36 (Thirty Six)
> > > [acme@quaco perf-urgent]$ gcc -v
> > > Using built-in specs.
> > > COLLECT_GCC=/usr/bin/gcc
> > > COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/12/lto-wrapper
> > > OFFLOAD_TARGET_NAMES=nvptx-none
> > > OFFLOAD_TARGET_DEFAULT=1
> > > Target: x86_64-redhat-linux
> > > Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --enable-libstdcxx-backtrace --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-12.1.1-20220507/obj-x86_64-redhat-linux/isl-install --enable-offload-targets=nvptx-none --without-cuda-driver --enable-offload-defaulted --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux --with-build-config=bootstrap-lto --enable-link-serialization=1
> > > Thread model: posix
> > > Supported LTO compression algorithms: zlib zstd
> > > gcc version 12.1.1 20220507 (Red Hat 12.1.1-1) (GCC)
> > > [acme@quaco perf-urgent]$
> > >
> > > [root@quaco ~]# perf test -v 42
> > >  42: BPF filter                                                      :
> > >  42.1: Basic BPF filtering                                           :
> > > --- start ---
> > > test child forked, pid 638881
> > > Kernel build dir is set to /lib/modules/5.17.11-300.fc36.x86_64/build
> > > set env: KBUILD_DIR=/lib/modules/5.17.11-300.fc36.x86_64/build
> > > unset env: KBUILD_OPTS
> > > include option is set to -nostdinc -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h
> > > set env: NR_CPUS=8
> > > set env: LINUX_VERSION_CODE=0x5110b
> > > set env: CLANG_EXEC=/usr/lib64/ccache/clang
> > > set env: CLANG_OPTIONS=-xc -g
> 
> ok, it's the BTF debug info as Andrii mentioned below,
> I assume you have 'clang-opt=-g' in .perfconfig, right?
> 
> when I add it to mine I can reproduce, perfect
> 
> SNIP
> 
> > > bpf: config 'func=do_epoll_wait' is ok
> > > Looking at the vmlinux_path (8 entries long)
> > > symsrc__init: build id mismatch for vmlinux.
> > > Using /usr/lib/debug/lib/modules/5.17.11-300.fc36.x86_64/vmlinux for symbols
> > > Open Debuginfo file: /usr/lib/debug/.build-id/f2/26f5d75e6842add57095a0615a1e5c16783dd7.debug
> > > Try to find probe point from debuginfo.
> > > Matched function: do_epoll_wait [38063fb]
> > > Probe point found: do_epoll_wait+0
> > > Found 1 probe_trace_events.
> > > Opening /sys/kernel/tracing//kprobe_events write=1
> > > Opening /sys/kernel/tracing//README write=0
> > > Writing event: p:perf_bpf_probe/func _text+3943040
> > > libbpf: map 'flip_table': created successfully, fd=4
> > > libbpf: prog 'bpf_func__SyS_epoll_pwait': BPF program load failed: Invalid argument
> > > libbpf: prog 'bpf_func__SyS_epoll_pwait': -- BEGIN PROG LOAD LOG --
> > > Invalid insn code at line_info[11].insn_off
> > 
> > Mismatched line_info.
> > 
> > Jiri, I think we need to clear func_info and line_info in
> > bpf_program__set_insns() because at that point func/line info can be
> > mismatched and won't correspond to the actual set of instructions.

so the problem is that we prepend init proglogue instructions
for each program not just for the one that needs it, so it will
mismatch later on.. the fix below makes it work for me

Arnaldo,
I squashed and pushed the change below changes to my bpf/depre
branch, could you please retest?

thanks,
jirka


---
diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c
index 30d0e688beec..6bd7c288e820 100644
--- a/tools/perf/util/bpf-loader.c
+++ b/tools/perf/util/bpf-loader.c
@@ -107,6 +107,17 @@ static int bpf_perf_object__add(struct bpf_object *obj)
 	return perf_obj ? 0 : -ENOMEM;
 }
 
+static void *program_priv(const struct bpf_program *prog)
+{
+	void *priv;
+
+	if (IS_ERR_OR_NULL(bpf_program_hash))
+		return NULL;
+	if (!hashmap__find(bpf_program_hash, prog, &priv))
+		return NULL;
+	return priv;
+}
+
 static struct bpf_insn prologue_init_insn[] = {
 	BPF_MOV64_IMM(BPF_REG_2, 0),
 	BPF_MOV64_IMM(BPF_REG_3, 0),
@@ -120,9 +131,18 @@ static int libbpf_prog_prepare_load_fn(struct bpf_program *prog,
 {
 	size_t init_size_cnt = ARRAY_SIZE(prologue_init_insn);
 	size_t orig_insn_cnt, insn_cnt, init_size, orig_size;
+	struct bpf_prog_priv *priv = program_priv(prog);
 	const struct bpf_insn *orig_insn;
 	struct bpf_insn *insn;
 
+	if (IS_ERR_OR_NULL(priv)) {
+		pr_debug("bpf: failed to get private field\n");
+		return -BPF_LOADER_ERRNO__INTERNAL;
+	}
+
+	if (!priv->need_prologue)
+		return 0;
+
 	/* prepend initialization code to program instructions */
 	orig_insn = bpf_program__insns(prog);
 	orig_insn_cnt = bpf_program__insn_cnt(prog);
@@ -312,17 +332,6 @@ static bool ptr_equal(const void *key1, const void *key2,
 	return key1 == key2;
 }
 
-static void *program_priv(const struct bpf_program *prog)
-{
-	void *priv;
-
-	if (IS_ERR_OR_NULL(bpf_program_hash))
-		return NULL;
-	if (!hashmap__find(bpf_program_hash, prog, &priv))
-		return NULL;
-	return priv;
-}
-
 static int program_set_priv(struct bpf_program *prog, void *priv)
 {
 	void *old_priv;

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

* Re: [PATCHv4 bpf-next 0/2] perf tools: Fix prologue generation
  2022-06-12 16:25         ` Jiri Olsa
@ 2022-06-13 20:15           ` Arnaldo Carvalho de Melo
  2022-06-15 15:27           ` Arnaldo Carvalho de Melo
  1 sibling, 0 replies; 13+ messages in thread
From: Arnaldo Carvalho de Melo @ 2022-06-13 20:15 UTC (permalink / raw)
  To: Jiri Olsa
  Cc: Andrii Nakryiko, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, linux-perf-use.,
	Networking, bpf, Ingo Molnar, Namhyung Kim, Alexander Shishkin,
	Peter Zijlstra, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, Ian Rogers

Em Sun, Jun 12, 2022 at 06:25:09PM +0200, Jiri Olsa escreveu:
> On Fri, Jun 10, 2022 at 10:53:57PM +0200, Jiri Olsa wrote:
> > On Fri, Jun 10, 2022 at 11:45:48AM -0700, Andrii Nakryiko wrote:
> > > On Fri, Jun 10, 2022 at 11:34 AM Arnaldo Carvalho de Melo
> > > > libbpf: map 'flip_table': created successfully, fd=4
> > > > libbpf: prog 'bpf_func__SyS_epoll_pwait': BPF program load failed: Invalid argument
> > > > libbpf: prog 'bpf_func__SyS_epoll_pwait': -- BEGIN PROG LOAD LOG --
> > > > Invalid insn code at line_info[11].insn_off

> > > Mismatched line_info.

> > > Jiri, I think we need to clear func_info and line_info in
> > > bpf_program__set_insns() because at that point func/line info can be
> > > mismatched and won't correspond to the actual set of instructions.

> so the problem is that we prepend init proglogue instructions
> for each program not just for the one that needs it, so it will
> mismatch later on.. the fix below makes it work for me

> Arnaldo,
> I squashed and pushed the change below changes to my bpf/depre
> branch, could you please retest?

I'll check.

- Arnaldo

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

* Re: [PATCHv4 bpf-next 0/2] perf tools: Fix prologue generation
  2022-06-12 16:25         ` Jiri Olsa
  2022-06-13 20:15           ` Arnaldo Carvalho de Melo
@ 2022-06-15 15:27           ` Arnaldo Carvalho de Melo
  2022-06-15 19:36             ` Jiri Olsa
  1 sibling, 1 reply; 13+ messages in thread
From: Arnaldo Carvalho de Melo @ 2022-06-15 15:27 UTC (permalink / raw)
  To: Jiri Olsa
  Cc: Andrii Nakryiko, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, linux-perf-use.,
	Networking, bpf, Ingo Molnar, Namhyung Kim, Alexander Shishkin,
	Peter Zijlstra, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, Ian Rogers

Em Sun, Jun 12, 2022 at 06:25:09PM +0200, Jiri Olsa escreveu:
> so the problem is that we prepend init proglogue instructions
> for each program not just for the one that needs it, so it will
> mismatch later on.. the fix below makes it work for me

> Arnaldo,
> I squashed and pushed the change below changes to my bpf/depre
> branch, could you please retest?

Before:

[acme@quaco perf-urgent]$ git log --oneline -5
e2cf9d315f90670f (HEAD -> perf/urgent, five/perf/urgent) perf test topology: Use !strncmp(right platform) to fix guest PPC comparision check
42e4fb08ff987b50 perf test: Record only user callchains on the "Check Arm64 callgraphs are complete in fp mode" test
819d5c3cf75d0f95 perf beauty: Update copy of linux/socket.h with the kernel sources
ebdc02b3ece8238b perf test: Fix variable length array undefined behavior in bp_account
8ff58c35adb7f118 libperf evsel: Open shouldn't leak fd on failure
[acme@quaco perf-urgent]$ sudo su -
[root@quaco ~]# perf -v
perf version 5.19.rc2.ge2cf9d315f90
[root@quaco ~]# perf test bpf
 40: LLVM search and compile                                         :
 40.1: Basic BPF llvm compile                                        : Ok
 40.3: Compile source for BPF prologue generation                    : Ok
 40.4: Compile source for BPF relocation                             : Ok
 42: BPF filter                                                      :
 42.1: Basic BPF filtering                                           : Ok
 42.2: BPF pinning                                                   : Ok
 42.3: BPF prologue generation                                       : Ok
 63: Test libpfm4 support                                            :
 96: perf stat --bpf-counters test                                   : Ok
[root@quaco ~]#

After your first patch:

[acme@quaco perf-urgent]$ git log --oneline -5 jolsa/bpf/depre
9317b879db422632 (jolsa/bpf/depre) perf tools: Rework prologue generation code
4d40831f29f2c9ad perf tools: Register fallback libbpf section handler
f913ad6559e337b4 libbpf: Fix is_pow_of_2
f175ece2e3436748 selftests/bpf: Fix tc_redirect_dtime
7b711e721234f475 bpf, test_run: Remove unnecessary prog type checks
[acme@quaco perf-urgent]$ git cherry-pick 4d40831f29f2c9ad
[perf/urgent ab39fb6880b57507] perf tools: Register fallback libbpf section handler
 Author: Jiri Olsa <jolsa@kernel.org>
 Date: Thu Apr 21 15:22:25 2022 +0200
 1 file changed, 65 insertions(+), 11 deletions(-)
[acme@quaco perf-urgent]$
[acme@quaco perf-urgent]$ alias m='rm -rf ~/libexec/perf-core/ ; perf stat -e cycles:u,instructions:u make -k BUILD_BPF_SKEL=1 PYTHON=python3 O=/tmp/build/perf-urgent -C tools/perf install-bin && perf test python'
[acme@quaco perf-urgent]$ rm -rf /tmp/build/perf-urgent ; mkdir -p /tmp/build/perf-urgent ; m
<SNIP>
 19: 'import perf' in python                                         : Ok
[acme@quaco perf-urgent]$
[acme@quaco perf-urgent]$ sudo su -
[sudo] password for acme:
[root@quaco ~]# perf test bpf
 40: LLVM search and compile                                         :
 40.1: Basic BPF llvm compile                                        : Ok
 40.3: Compile source for BPF prologue generation                    : Ok
 40.4: Compile source for BPF relocation                             : Ok
 42: BPF filter                                                      :
 42.1: Basic BPF filtering                                           : Ok
 42.2: BPF pinning                                                   : Ok
 42.3: BPF prologue generation                                       : FAILED!
 63: Test libpfm4 support                                            :
 96: perf stat --bpf-counters test                                   : Ok
[root@quaco ~]#

perf test -v bpf later, lets see if landing the second patch fixes
things and this bisection problem is justified:

[acme@quaco perf-urgent]$ git log --oneline -5 jolsa/bpf/depre
9317b879db422632 (jolsa/bpf/depre) perf tools: Rework prologue generation code
4d40831f29f2c9ad perf tools: Register fallback libbpf section handler
f913ad6559e337b4 libbpf: Fix is_pow_of_2
f175ece2e3436748 selftests/bpf: Fix tc_redirect_dtime
7b711e721234f475 bpf, test_run: Remove unnecessary prog type checks
[acme@quaco perf-urgent]$ git remote update jolsa
Fetching jolsa
[acme@quaco perf-urgent]$ git cherry-pick 9317b879db422632
[perf/urgent 9a36c7c94e1f596d] perf tools: Rework prologue generation code
 Author: Jiri Olsa <jolsa@kernel.org>
 Date: Mon May 9 22:46:20 2022 +0200
 1 file changed, 110 insertions(+), 18 deletions(-)
[acme@quaco perf-urgent]$
[acme@quaco perf-urgent]$ rm -rf /tmp/build/perf-urgent ; mkdir -p /tmp/build/perf-urgent ; m
<SNIP>
 19: 'import perf' in python                                         : Ok
[acme@quaco perf-urgent]$ sudo su -
[root@quaco ~]# perf test bpf
 40: LLVM search and compile                                         :
 40.1: Basic BPF llvm compile                                        : Ok
 40.3: Compile source for BPF prologue generation                    : Ok
 40.4: Compile source for BPF relocation                             : Ok
 42: BPF filter                                                      :
 42.1: Basic BPF filtering                                           : Ok
 42.2: BPF pinning                                                   : Ok
 42.3: BPF prologue generation                                       : Ok
 63: Test libpfm4 support                                            :
 96: perf stat --bpf-counters test                                   : Ok
[root@quaco ~]#

So it works in the end, can it be made to work after the first step? I
didn't check that.

-v without the last patch:

[acme@quaco perf-urgent]$ git reset --hard HEAD~
HEAD is now at ab39fb6880b57507 perf tools: Register fallback libbpf section handler
[acme@quaco perf-urgent]$ rm -rf /tmp/build/perf-urgent ; mkdir -p /tmp/build/perf-urgent ; m
<SNIP>
 19: 'import perf' in python                                         : Ok
[acme@quaco perf-urgent]$ sudo su -
[root@quaco ~]# perf test -v bpf
 40: LLVM search and compile                                         :
 40.1: Basic BPF llvm compile                                        :
--- start ---
test child forked, pid 731590
Kernel build dir is set to /lib/modules/5.17.11-300.fc36.x86_64/build
set env: KBUILD_DIR=/lib/modules/5.17.11-300.fc36.x86_64/build
unset env: KBUILD_OPTS
include option is set to -nostdinc -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h 
set env: NR_CPUS=8
set env: LINUX_VERSION_CODE=0x5110b
set env: CLANG_EXEC=/usr/lib64/ccache/clang
set env: CLANG_OPTIONS=-xc -g
set env: KERNEL_INC_OPTIONS=-nostdinc -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h 
set env: PERF_BPF_INC_OPTIONS=-I/home/acme/lib/perf/include/bpf
set env: WORKING_DIR=/lib/modules/5.17.11-300.fc36.x86_64/build
set env: CLANG_SOURCE=-
llvm compiling command template: echo '// SPDX-License-Identifier: GPL-2.0
/*
 * bpf-script-example.c
 * Test basic LLVM building
 */
#ifndef LINUX_VERSION_CODE
# error Need LINUX_VERSION_CODE
# error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig'
#endif
#define BPF_ANY 0
#define BPF_MAP_TYPE_ARRAY 2
#define BPF_FUNC_map_lookup_elem 1
#define BPF_FUNC_map_update_elem 2

static void *(*bpf_map_lookup_elem)(void *map, void *key) =
	(void *) BPF_FUNC_map_lookup_elem;
static void *(*bpf_map_update_elem)(void *map, void *key, void *value, int flags) =
	(void *) BPF_FUNC_map_update_elem;

struct bpf_map_def {
	unsigned int type;
	unsigned int key_size;
	unsigned int value_size;
	unsigned int max_entries;
};

#define SEC(NAME) __attribute__((section(NAME), used))
struct bpf_map_def SEC("maps") flip_table = {
	.type = BPF_MAP_TYPE_ARRAY,
	.key_size = sizeof(int),
	.value_size = sizeof(int),
	.max_entries = 1,
};

SEC("func=do_epoll_wait")
int bpf_func__SyS_epoll_pwait(void *ctx)
{
	int ind =0;
	int *flag = bpf_map_lookup_elem(&flip_table, &ind);
	int new_flag;
	if (!flag)
		return 0;
	/* flip flag and store back */
	new_flag = !*flag;
	bpf_map_update_elem(&flip_table, &ind, &new_flag, BPF_ANY);
	return new_flag;
}
char _license[] SEC("license") = "GPL";
int _version SEC("version") = LINUX_VERSION_CODE;
' | $CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS -DLINUX_VERSION_CODE=$LINUX_VERSION_CODE $CLANG_OPTIONS $PERF_BPF_INC_OPTIONS $KERNEL_INC_OPTIONS -Wno-unused-value -Wno-pointer-sign -working-directory $WORKING_DIR -c "$CLANG_SOURCE" -target bpf $CLANG_EMIT_LLVM -O2 -o - $LLVM_OPTIONS_PIPE
llvm compiling command : echo '// SPDX-License-Identifier: GPL-2.0
/*
 * bpf-script-example.c
 * Test basic LLVM building
 */
#ifndef LINUX_VERSION_CODE
# error Need LINUX_VERSION_CODE
# error Example: for 4.2 kernel, put 'clang-opt=-DLINUX_VERSION_CODE=0x40200 into llvm section of ~/.perfconfig'
#endif
#define BPF_ANY 0
#define BPF_MAP_TYPE_ARRAY 2
#define BPF_FUNC_map_lookup_elem 1
#define BPF_FUNC_map_update_elem 2

static void *(*bpf_map_lookup_elem)(void *map, void *key) =
	(void *) BPF_FUNC_map_lookup_elem;
static void *(*bpf_map_update_elem)(void *map, void *key, void *value, int flags) =
	(void *) BPF_FUNC_map_update_elem;

struct bpf_map_def {
	unsigned int type;
	unsigned int key_size;
	unsigned int value_size;
	unsigned int max_entries;
};

#define SEC(NAME) __attribute__((section(NAME), used))
struct bpf_map_def SEC(maps) flip_table = {
	.type = BPF_MAP_TYPE_ARRAY,
	.key_size = sizeof(int),
	.value_size = sizeof(int),
	.max_entries = 1,
};

SEC(func=do_epoll_wait)
int bpf_func__SyS_epoll_pwait(void *ctx)
{
	int ind =0;
	int *flag = bpf_map_lookup_elem(&flip_table, &ind);
	int new_flag;
	if (!flag)
		return 0;
	/* flip flag and store back */
	new_flag = !*flag;
	bpf_map_update_elem(&flip_table, &ind, &new_flag, BPF_ANY);
	return new_flag;
}
char _license[] SEC(license) = GPL;
int _version SEC(version) = LINUX_VERSION_CODE;
' | /usr/lib64/ccache/clang -D__KERNEL__ -D__NR_CPUS__=8 -DLINUX_VERSION_CODE=0x5110b -xc -g -I/home/acme/lib/perf/include/bpf -nostdinc -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h  -Wno-unused-value -Wno-pointer-sign -working-directory /lib/modules/5.17.11-300.fc36.x86_64/build -c - -target bpf  -O2 -o - 
libbpf: map 'flip_table' (legacy): legacy map definitions are deprecated, use BTF-defined maps instead
libbpf: Use of BPF_ANNOTATE_KV_PAIR is deprecated, use BTF-defined maps in .maps section instead
test child finished with 0
---- end ----
LLVM search and compile subtest 1: Ok
 40.3: Compile source for BPF prologue generation                    :
--- start ---
test child forked, pid 731783
Kernel build dir is set to /lib/modules/5.17.11-300.fc36.x86_64/build
set env: KBUILD_DIR=/lib/modules/5.17.11-300.fc36.x86_64/build
unset env: KBUILD_OPTS
include option is set to -nostdinc -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h 
set env: NR_CPUS=8
set env: LINUX_VERSION_CODE=0x5110b
set env: CLANG_EXEC=/usr/lib64/ccache/clang
set env: CLANG_OPTIONS=-xc -g
set env: KERNEL_INC_OPTIONS=-nostdinc -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h 
set env: PERF_BPF_INC_OPTIONS=-I/home/acme/lib/perf/include/bpf
set env: WORKING_DIR=/lib/modules/5.17.11-300.fc36.x86_64/build
set env: CLANG_SOURCE=-
llvm compiling command template: echo '// SPDX-License-Identifier: GPL-2.0
/*
 * bpf-script-test-prologue.c
 * Test BPF prologue
 */
#ifndef LINUX_VERSION_CODE
# error Need LINUX_VERSION_CODE
# error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig'
#endif
#define SEC(NAME) __attribute__((section(NAME), used))

#include <uapi/linux/fs.h>

/*
 * If CONFIG_PROFILE_ALL_BRANCHES is selected,
 * 'if' is redefined after include kernel header.
 * Recover 'if' for BPF object code.
 */
#ifdef if
# undef if
#endif

#define FMODE_READ		0x1
#define FMODE_WRITE		0x2

static void (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) =
	(void *) 6;

SEC("func=null_lseek file->f_mode offset orig")
int bpf_func__null_lseek(void *ctx, int err, unsigned long _f_mode,
			 unsigned long offset, unsigned long orig)
{
	fmode_t f_mode = (fmode_t)_f_mode;

	if (err)
		return 0;
	if (f_mode & FMODE_WRITE)
		return 0;
	if (offset & 1)
		return 0;
	if (orig == SEEK_CUR)
		return 0;
	return 1;
}

char _license[] SEC("license") = "GPL";
int _version SEC("version") = LINUX_VERSION_CODE;
' | $CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS -DLINUX_VERSION_CODE=$LINUX_VERSION_CODE $CLANG_OPTIONS $PERF_BPF_INC_OPTIONS $KERNEL_INC_OPTIONS -Wno-unused-value -Wno-pointer-sign -working-directory $WORKING_DIR -c "$CLANG_SOURCE" -target bpf $CLANG_EMIT_LLVM -O2 -o - $LLVM_OPTIONS_PIPE
llvm compiling command : 
test child finished with 0
---- end ----
LLVM search and compile subtest 3: Ok
 40.4: Compile source for BPF relocation                             :
--- start ---
test child forked, pid 731976
Kernel build dir is set to /lib/modules/5.17.11-300.fc36.x86_64/build
set env: KBUILD_DIR=/lib/modules/5.17.11-300.fc36.x86_64/build
unset env: KBUILD_OPTS
include option is set to -nostdinc -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h 
set env: NR_CPUS=8
set env: LINUX_VERSION_CODE=0x5110b
set env: CLANG_EXEC=/usr/lib64/ccache/clang
set env: CLANG_OPTIONS=-xc -g
set env: KERNEL_INC_OPTIONS=-nostdinc -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h 
set env: PERF_BPF_INC_OPTIONS=-I/home/acme/lib/perf/include/bpf
set env: WORKING_DIR=/lib/modules/5.17.11-300.fc36.x86_64/build
set env: CLANG_SOURCE=-
llvm compiling command template: echo '// SPDX-License-Identifier: GPL-2.0
/*
 * bpf-script-test-relocation.c
 * Test BPF loader checking relocation
 */
#ifndef LINUX_VERSION_CODE
# error Need LINUX_VERSION_CODE
# error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig'
#endif
#define BPF_ANY 0
#define BPF_MAP_TYPE_ARRAY 2
#define BPF_FUNC_map_lookup_elem 1
#define BPF_FUNC_map_update_elem 2

static void *(*bpf_map_lookup_elem)(void *map, void *key) =
	(void *) BPF_FUNC_map_lookup_elem;
static void *(*bpf_map_update_elem)(void *map, void *key, void *value, int flags) =
	(void *) BPF_FUNC_map_update_elem;

struct bpf_map_def {
	unsigned int type;
	unsigned int key_size;
	unsigned int value_size;
	unsigned int max_entries;
};

#define SEC(NAME) __attribute__((section(NAME), used))
struct bpf_map_def SEC("maps") my_table = {
	.type = BPF_MAP_TYPE_ARRAY,
	.key_size = sizeof(int),
	.value_size = sizeof(int),
	.max_entries = 1,
};

int this_is_a_global_val;

SEC("func=sys_write")
int bpf_func__sys_write(void *ctx)
{
	int key = 0;
	int value = 0;

	/*
	 * Incorrect relocation. Should not allow this program be
	 * loaded into kernel.
	 */
	bpf_map_update_elem(&this_is_a_global_val, &key, &value, 0);
	return 0;
}
char _license[] SEC("license") = "GPL";
int _version SEC("version") = LINUX_VERSION_CODE;
' | $CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS -DLINUX_VERSION_CODE=$LINUX_VERSION_CODE $CLANG_OPTIONS $PERF_BPF_INC_OPTIONS $KERNEL_INC_OPTIONS -Wno-unused-value -Wno-pointer-sign -working-directory $WORKING_DIR -c "$CLANG_SOURCE" -target bpf $CLANG_EMIT_LLVM -O2 -o - $LLVM_OPTIONS_PIPE
llvm compiling command : echo '// SPDX-License-Identifier: GPL-2.0
/*
 * bpf-script-test-relocation.c
 * Test BPF loader checking relocation
 */
#ifndef LINUX_VERSION_CODE
# error Need LINUX_VERSION_CODE
# error Example: for 4.2 kernel, put 'clang-opt=-DLINUX_VERSION_CODE=0x40200 into llvm section of ~/.perfconfig'
#endif
#define BPF_ANY 0
#define BPF_MAP_TYPE_ARRAY 2
#define BPF_FUNC_map_lookup_elem 1
#define BPF_FUNC_map_update_elem 2

static void *(*bpf_map_lookup_elem)(void *map, void *key) =
	(void *) BPF_FUNC_map_lookup_elem;
static void *(*bpf_map_update_elem)(void *map, void *key, void *value, int flags) =
	(void *) BPF_FUNC_map_update_elem;

struct bpf_map_def {
	unsigned int type;
	unsigned int key_size;
	unsigned int value_size;
	unsigned int max_entries;
};

#define SEC(NAME) __attribute__((section(NAME), used))
struct bpf_map_def SEC(maps) my_table = {
	.type = BPF_MAP_TYPE_ARRAY,
	.key_size = sizeof(int),
	.value_size = sizeof(int),
	.max_entries = 1,
};

int this_is_a_global_val;

SEC(func=sys_write)
int bpf_func__sys_write(void *ctx)
{
	int key = 0;
	int value = 0;

	/*
	 * Incorrect relocation. Should not allow this program be
	 * loaded into kernel.
	 */
	bpf_map_update_elem(&this_is_a_global_val, &key, &value, 0);
	return 0;
}
char _license[] SEC(license) = GPL;
int _version SEC(version) = LINUX_VERSION_CODE;
' | /usr/lib64/ccache/clang -D__KERNEL__ -D__NR_CPUS__=8 -DLINUX_VERSION_CODE=0x5110b -xc -g -I/home/acme/lib/perf/include/bpf -nostdinc -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h  -Wno-unused-value -Wno-pointer-sign -working-directory /lib/modules/5.17.11-300.fc36.x86_64/build -c - -target bpf  -O2 -o - 
test child finished with 0
---- end ----
LLVM search and compile subtest 4: Ok
 42: BPF filter                                                      :
 42.1: Basic BPF filtering                                           :
--- start ---
test child forked, pid 732169
Kernel build dir is set to /lib/modules/5.17.11-300.fc36.x86_64/build
set env: KBUILD_DIR=/lib/modules/5.17.11-300.fc36.x86_64/build
unset env: KBUILD_OPTS
include option is set to -nostdinc -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h 
set env: NR_CPUS=8
set env: LINUX_VERSION_CODE=0x5110b
set env: CLANG_EXEC=/usr/lib64/ccache/clang
set env: CLANG_OPTIONS=-xc -g
set env: KERNEL_INC_OPTIONS=-nostdinc -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h 
set env: PERF_BPF_INC_OPTIONS=-I/home/acme/lib/perf/include/bpf
set env: WORKING_DIR=/lib/modules/5.17.11-300.fc36.x86_64/build
set env: CLANG_SOURCE=-
llvm compiling command template: echo '// SPDX-License-Identifier: GPL-2.0
/*
 * bpf-script-example.c
 * Test basic LLVM building
 */
#ifndef LINUX_VERSION_CODE
# error Need LINUX_VERSION_CODE
# error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig'
#endif
#define BPF_ANY 0
#define BPF_MAP_TYPE_ARRAY 2
#define BPF_FUNC_map_lookup_elem 1
#define BPF_FUNC_map_update_elem 2

static void *(*bpf_map_lookup_elem)(void *map, void *key) =
	(void *) BPF_FUNC_map_lookup_elem;
static void *(*bpf_map_update_elem)(void *map, void *key, void *value, int flags) =
	(void *) BPF_FUNC_map_update_elem;

struct bpf_map_def {
	unsigned int type;
	unsigned int key_size;
	unsigned int value_size;
	unsigned int max_entries;
};

#define SEC(NAME) __attribute__((section(NAME), used))
struct bpf_map_def SEC("maps") flip_table = {
	.type = BPF_MAP_TYPE_ARRAY,
	.key_size = sizeof(int),
	.value_size = sizeof(int),
	.max_entries = 1,
};

SEC("func=do_epoll_wait")
int bpf_func__SyS_epoll_pwait(void *ctx)
{
	int ind =0;
	int *flag = bpf_map_lookup_elem(&flip_table, &ind);
	int new_flag;
	if (!flag)
		return 0;
	/* flip flag and store back */
	new_flag = !*flag;
	bpf_map_update_elem(&flip_table, &ind, &new_flag, BPF_ANY);
	return new_flag;
}
char _license[] SEC("license") = "GPL";
int _version SEC("version") = LINUX_VERSION_CODE;
' | $CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS -DLINUX_VERSION_CODE=$LINUX_VERSION_CODE $CLANG_OPTIONS $PERF_BPF_INC_OPTIONS $KERNEL_INC_OPTIONS -Wno-unused-value -Wno-pointer-sign -working-directory $WORKING_DIR -c "$CLANG_SOURCE" -target bpf $CLANG_EMIT_LLVM -O2 -o - $LLVM_OPTIONS_PIPE
llvm compiling command : echo '// SPDX-License-Identifier: GPL-2.0
/*
 * bpf-script-example.c
 * Test basic LLVM building
 */
#ifndef LINUX_VERSION_CODE
# error Need LINUX_VERSION_CODE
# error Example: for 4.2 kernel, put 'clang-opt=-DLINUX_VERSION_CODE=0x40200 into llvm section of ~/.perfconfig'
#endif
#define BPF_ANY 0
#define BPF_MAP_TYPE_ARRAY 2
#define BPF_FUNC_map_lookup_elem 1
#define BPF_FUNC_map_update_elem 2

static void *(*bpf_map_lookup_elem)(void *map, void *key) =
	(void *) BPF_FUNC_map_lookup_elem;
static void *(*bpf_map_update_elem)(void *map, void *key, void *value, int flags) =
	(void *) BPF_FUNC_map_update_elem;

struct bpf_map_def {
	unsigned int type;
	unsigned int key_size;
	unsigned int value_size;
	unsigned int max_entries;
};

#define SEC(NAME) __attribute__((section(NAME), used))
struct bpf_map_def SEC(maps) flip_table = {
	.type = BPF_MAP_TYPE_ARRAY,
	.key_size = sizeof(int),
	.value_size = sizeof(int),
	.max_entries = 1,
};

SEC(func=do_epoll_wait)
int bpf_func__SyS_epoll_pwait(void *ctx)
{
	int ind =0;
	int *flag = bpf_map_lookup_elem(&flip_table, &ind);
	int new_flag;
	if (!flag)
		return 0;
	/* flip flag and store back */
	new_flag = !*flag;
	bpf_map_update_elem(&flip_table, &ind, &new_flag, BPF_ANY);
	return new_flag;
}
char _license[] SEC(license) = GPL;
int _version SEC(version) = LINUX_VERSION_CODE;
' | /usr/lib64/ccache/clang -D__KERNEL__ -D__NR_CPUS__=8 -DLINUX_VERSION_CODE=0x5110b -xc -g -I/home/acme/lib/perf/include/bpf -nostdinc -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h  -Wno-unused-value -Wno-pointer-sign -working-directory /lib/modules/5.17.11-300.fc36.x86_64/build -c - -target bpf  -O2 -o - 
libbpf: loading object '[basic_bpf_test]' from buffer
libbpf: elf: section(3) func=do_epoll_wait, size 192, link 0, flags 6, type=1
libbpf: sec 'func=do_epoll_wait': found program 'bpf_func__SyS_epoll_pwait' at insn offset 0 (0 bytes), code size 24 insns (192 bytes)
libbpf: elf: section(4) .relfunc=do_epoll_wait, size 32, link 22, flags 0, type=9
libbpf: elf: section(5) maps, size 16, link 0, flags 3, type=1
libbpf: elf: section(6) license, size 4, link 0, flags 3, type=1
libbpf: license of [basic_bpf_test] is GPL
libbpf: elf: section(7) version, size 4, link 0, flags 3, type=1
libbpf: kernel version of [basic_bpf_test] is 5110b
libbpf: elf: section(13) .BTF, size 576, link 0, flags 0, type=1
libbpf: elf: section(15) .BTF.ext, size 256, link 0, flags 0, type=1
libbpf: elf: section(22) .symtab, size 336, link 1, flags 0, type=2
libbpf: looking for externs among 14 symbols...
libbpf: collected 0 externs total
libbpf: elf: found 1 legacy map definitions (16 bytes) in [basic_bpf_test]
libbpf: map 'flip_table' (legacy): legacy map definitions are deprecated, use BTF-defined maps instead
libbpf: map 'flip_table' (legacy): at sec_idx 5, offset 0.
libbpf: map 11 is "flip_table"
libbpf: Use of BPF_ANNOTATE_KV_PAIR is deprecated, use BTF-defined maps in .maps section instead
libbpf: map:flip_table container_name:____btf_map_flip_table cannot be found in BTF. Missing BPF_ANNOTATE_KV_PAIR?
libbpf: sec '.relfunc=do_epoll_wait': collecting relocation for section(3) 'func=do_epoll_wait'
libbpf: sec '.relfunc=do_epoll_wait': relo #0: insn #4 against 'flip_table'
libbpf: prog 'bpf_func__SyS_epoll_pwait': found map 0 (flip_table, sec 5, off 0) for insn #4
libbpf: sec '.relfunc=do_epoll_wait': relo #1: insn #17 against 'flip_table'
libbpf: prog 'bpf_func__SyS_epoll_pwait': found map 0 (flip_table, sec 5, off 0) for insn #17
bpf: config program 'func=do_epoll_wait'
symbol:do_epoll_wait file:(null) line:0 offset:0 return:0 lazy:(null)
bpf: config 'func=do_epoll_wait' is ok
Looking at the vmlinux_path (8 entries long)
symsrc__init: build id mismatch for vmlinux.
Using /usr/lib/debug/lib/modules/5.17.11-300.fc36.x86_64/vmlinux for symbols
Open Debuginfo file: /usr/lib/debug/.build-id/f2/26f5d75e6842add57095a0615a1e5c16783dd7.debug
Try to find probe point from debuginfo.
Matched function: do_epoll_wait [38063fb]
Probe point found: do_epoll_wait+0

Found 1 probe_trace_events.
Opening /sys/kernel/tracing//kprobe_events write=1
Opening /sys/kernel/tracing//README write=0
Writing event: p:perf_bpf_probe/func _text+3943040
libbpf: map 'flip_table': created successfully, fd=4
add bpf event perf_bpf_probe:func and attach bpf program 5
adding perf_bpf_probe:func
adding perf_bpf_probe:func to 0x3017000
Using CPUID GenuineIntel-6-8E-A
mmap size 1052672B
Opening /sys/kernel/tracing//kprobe_events write=1
Opening /sys/kernel/tracing//uprobe_events write=1
Parsing probe_events: p:perf_bpf_probe/func _text+3943040
Group:perf_bpf_probe Event:func probe:p
Writing event: -:perf_bpf_probe/func
test child finished with 0
---- end ----
BPF filter subtest 1: Ok
 42.2: BPF pinning                                                   :
--- start ---
test child forked, pid 732362
Kernel build dir is set to /lib/modules/5.17.11-300.fc36.x86_64/build
set env: KBUILD_DIR=/lib/modules/5.17.11-300.fc36.x86_64/build
unset env: KBUILD_OPTS
include option is set to -nostdinc -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h 
set env: NR_CPUS=8
set env: LINUX_VERSION_CODE=0x5110b
set env: CLANG_EXEC=/usr/lib64/ccache/clang
set env: CLANG_OPTIONS=-xc -g
set env: KERNEL_INC_OPTIONS=-nostdinc -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h 
set env: PERF_BPF_INC_OPTIONS=-I/home/acme/lib/perf/include/bpf
set env: WORKING_DIR=/lib/modules/5.17.11-300.fc36.x86_64/build
set env: CLANG_SOURCE=-
llvm compiling command template: echo '// SPDX-License-Identifier: GPL-2.0
/*
 * bpf-script-example.c
 * Test basic LLVM building
 */
#ifndef LINUX_VERSION_CODE
# error Need LINUX_VERSION_CODE
# error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig'
#endif
#define BPF_ANY 0
#define BPF_MAP_TYPE_ARRAY 2
#define BPF_FUNC_map_lookup_elem 1
#define BPF_FUNC_map_update_elem 2

static void *(*bpf_map_lookup_elem)(void *map, void *key) =
	(void *) BPF_FUNC_map_lookup_elem;
static void *(*bpf_map_update_elem)(void *map, void *key, void *value, int flags) =
	(void *) BPF_FUNC_map_update_elem;

struct bpf_map_def {
	unsigned int type;
	unsigned int key_size;
	unsigned int value_size;
	unsigned int max_entries;
};

#define SEC(NAME) __attribute__((section(NAME), used))
struct bpf_map_def SEC("maps") flip_table = {
	.type = BPF_MAP_TYPE_ARRAY,
	.key_size = sizeof(int),
	.value_size = sizeof(int),
	.max_entries = 1,
};

SEC("func=do_epoll_wait")
int bpf_func__SyS_epoll_pwait(void *ctx)
{
	int ind =0;
	int *flag = bpf_map_lookup_elem(&flip_table, &ind);
	int new_flag;
	if (!flag)
		return 0;
	/* flip flag and store back */
	new_flag = !*flag;
	bpf_map_update_elem(&flip_table, &ind, &new_flag, BPF_ANY);
	return new_flag;
}
char _license[] SEC("license") = "GPL";
int _version SEC("version") = LINUX_VERSION_CODE;
' | $CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS -DLINUX_VERSION_CODE=$LINUX_VERSION_CODE $CLANG_OPTIONS $PERF_BPF_INC_OPTIONS $KERNEL_INC_OPTIONS -Wno-unused-value -Wno-pointer-sign -working-directory $WORKING_DIR -c "$CLANG_SOURCE" -target bpf $CLANG_EMIT_LLVM -O2 -o - $LLVM_OPTIONS_PIPE
llvm compiling command : echo '// SPDX-License-Identifier: GPL-2.0
/*
 * bpf-script-example.c
 * Test basic LLVM building
 */
#ifndef LINUX_VERSION_CODE
# error Need LINUX_VERSION_CODE
# error Example: for 4.2 kernel, put 'clang-opt=-DLINUX_VERSION_CODE=0x40200 into llvm section of ~/.perfconfig'
#endif
#define BPF_ANY 0
#define BPF_MAP_TYPE_ARRAY 2
#define BPF_FUNC_map_lookup_elem 1
#define BPF_FUNC_map_update_elem 2

static void *(*bpf_map_lookup_elem)(void *map, void *key) =
	(void *) BPF_FUNC_map_lookup_elem;
static void *(*bpf_map_update_elem)(void *map, void *key, void *value, int flags) =
	(void *) BPF_FUNC_map_update_elem;

struct bpf_map_def {
	unsigned int type;
	unsigned int key_size;
	unsigned int value_size;
	unsigned int max_entries;
};

#define SEC(NAME) __attribute__((section(NAME), used))
struct bpf_map_def SEC(maps) flip_table = {
	.type = BPF_MAP_TYPE_ARRAY,
	.key_size = sizeof(int),
	.value_size = sizeof(int),
	.max_entries = 1,
};

SEC(func=do_epoll_wait)
int bpf_func__SyS_epoll_pwait(void *ctx)
{
	int ind =0;
	int *flag = bpf_map_lookup_elem(&flip_table, &ind);
	int new_flag;
	if (!flag)
		return 0;
	/* flip flag and store back */
	new_flag = !*flag;
	bpf_map_update_elem(&flip_table, &ind, &new_flag, BPF_ANY);
	return new_flag;
}
char _license[] SEC(license) = GPL;
int _version SEC(version) = LINUX_VERSION_CODE;
' | /usr/lib64/ccache/clang -D__KERNEL__ -D__NR_CPUS__=8 -DLINUX_VERSION_CODE=0x5110b -xc -g -I/home/acme/lib/perf/include/bpf -nostdinc -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h  -Wno-unused-value -Wno-pointer-sign -working-directory /lib/modules/5.17.11-300.fc36.x86_64/build -c - -target bpf  -O2 -o - 
libbpf: loading object '[bpf_pinning]' from buffer
libbpf: elf: section(3) func=do_epoll_wait, size 192, link 0, flags 6, type=1
libbpf: sec 'func=do_epoll_wait': found program 'bpf_func__SyS_epoll_pwait' at insn offset 0 (0 bytes), code size 24 insns (192 bytes)
libbpf: elf: section(4) .relfunc=do_epoll_wait, size 32, link 22, flags 0, type=9
libbpf: elf: section(5) maps, size 16, link 0, flags 3, type=1
libbpf: elf: section(6) license, size 4, link 0, flags 3, type=1
libbpf: license of [bpf_pinning] is GPL
libbpf: elf: section(7) version, size 4, link 0, flags 3, type=1
libbpf: kernel version of [bpf_pinning] is 5110b
libbpf: elf: section(13) .BTF, size 576, link 0, flags 0, type=1
libbpf: elf: section(15) .BTF.ext, size 256, link 0, flags 0, type=1
libbpf: elf: section(22) .symtab, size 336, link 1, flags 0, type=2
libbpf: looking for externs among 14 symbols...
libbpf: collected 0 externs total
libbpf: elf: found 1 legacy map definitions (16 bytes) in [bpf_pinning]
libbpf: map 'flip_table' (legacy): legacy map definitions are deprecated, use BTF-defined maps instead
libbpf: map 'flip_table' (legacy): at sec_idx 5, offset 0.
libbpf: map 11 is "flip_table"
libbpf: Use of BPF_ANNOTATE_KV_PAIR is deprecated, use BTF-defined maps in .maps section instead
libbpf: map:flip_table container_name:____btf_map_flip_table cannot be found in BTF. Missing BPF_ANNOTATE_KV_PAIR?
libbpf: sec '.relfunc=do_epoll_wait': collecting relocation for section(3) 'func=do_epoll_wait'
libbpf: sec '.relfunc=do_epoll_wait': relo #0: insn #4 against 'flip_table'
libbpf: prog 'bpf_func__SyS_epoll_pwait': found map 0 (flip_table, sec 5, off 0) for insn #4
libbpf: sec '.relfunc=do_epoll_wait': relo #1: insn #17 against 'flip_table'
libbpf: prog 'bpf_func__SyS_epoll_pwait': found map 0 (flip_table, sec 5, off 0) for insn #17
bpf: config program 'func=do_epoll_wait'
symbol:do_epoll_wait file:(null) line:0 offset:0 return:0 lazy:(null)
bpf: config 'func=do_epoll_wait' is ok
Looking at the vmlinux_path (8 entries long)
symsrc__init: build id mismatch for vmlinux.
Using /usr/lib/debug/lib/modules/5.17.11-300.fc36.x86_64/vmlinux for symbols
Open Debuginfo file: /usr/lib/debug/.build-id/f2/26f5d75e6842add57095a0615a1e5c16783dd7.debug
Try to find probe point from debuginfo.
Matched function: do_epoll_wait [38063fb]
Probe point found: do_epoll_wait+0
Found 1 probe_trace_events.
Opening /sys/kernel/tracing//kprobe_events write=1
Opening /sys/kernel/tracing//README write=0
Writing event: p:perf_bpf_probe/func _text+3943040
libbpf: map 'flip_table': created successfully, fd=4
add bpf event perf_bpf_probe:func and attach bpf program 5
adding perf_bpf_probe:func
adding perf_bpf_probe:func to 0x3017000
Using CPUID GenuineIntel-6-8E-A
mmap size 1052672B
libbpf: pinned map '/sys/fs/bpf/perf_test/flip_table'
libbpf: pinned program '/sys/fs/bpf/perf_test/func=do_epoll_wait'
Opening /sys/kernel/tracing//kprobe_events write=1
Opening /sys/kernel/tracing//uprobe_events write=1
Parsing probe_events: p:perf_bpf_probe/func _text+3943040
Group:perf_bpf_probe Event:func probe:p
Writing event: -:perf_bpf_probe/func
test child finished with 0
---- end ----
BPF filter subtest 2: Ok
 42.3: BPF prologue generation                                       :
--- start ---
test child forked, pid 732556
Kernel build dir is set to /lib/modules/5.17.11-300.fc36.x86_64/build
set env: KBUILD_DIR=/lib/modules/5.17.11-300.fc36.x86_64/build
unset env: KBUILD_OPTS
include option is set to -nostdinc -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h 
set env: NR_CPUS=8
set env: LINUX_VERSION_CODE=0x5110b
set env: CLANG_EXEC=/usr/lib64/ccache/clang
set env: CLANG_OPTIONS=-xc -g
set env: KERNEL_INC_OPTIONS=-nostdinc -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h 
set env: PERF_BPF_INC_OPTIONS=-I/home/acme/lib/perf/include/bpf
set env: WORKING_DIR=/lib/modules/5.17.11-300.fc36.x86_64/build
set env: CLANG_SOURCE=-
llvm compiling command template: echo '// SPDX-License-Identifier: GPL-2.0
/*
 * bpf-script-test-prologue.c
 * Test BPF prologue
 */
#ifndef LINUX_VERSION_CODE
# error Need LINUX_VERSION_CODE
# error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig'
#endif
#define SEC(NAME) __attribute__((section(NAME), used))

#include <uapi/linux/fs.h>

/*
 * If CONFIG_PROFILE_ALL_BRANCHES is selected,
 * 'if' is redefined after include kernel header.
 * Recover 'if' for BPF object code.
 */
#ifdef if
# undef if
#endif

#define FMODE_READ		0x1
#define FMODE_WRITE		0x2

static void (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) =
	(void *) 6;

SEC("func=null_lseek file->f_mode offset orig")
int bpf_func__null_lseek(void *ctx, int err, unsigned long _f_mode,
			 unsigned long offset, unsigned long orig)
{
	fmode_t f_mode = (fmode_t)_f_mode;

	if (err)
		return 0;
	if (f_mode & FMODE_WRITE)
		return 0;
	if (offset & 1)
		return 0;
	if (orig == SEEK_CUR)
		return 0;
	return 1;
}

char _license[] SEC("license") = "GPL";
int _version SEC("version") = LINUX_VERSION_CODE;
' | $CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS -DLINUX_VERSION_CODE=$LINUX_VERSION_CODE $CLANG_OPTIONS $PERF_BPF_INC_OPTIONS $KERNEL_INC_OPTIONS -Wno-unused-value -Wno-pointer-sign -working-directory $WORKING_DIR -c "$CLANG_SOURCE" -target bpf $CLANG_EMIT_LLVM -O2 -o - $LLVM_OPTIONS_PIPE
llvm compiling command : 
libbpf: loading object '[bpf_prologue_test]' from buffer
libbpf: elf: section(3) func=null_lseek file->f_mode offset orig, size 112, link 0, flags 6, type=1
libbpf: sec 'func=null_lseek file->f_mode offset orig': found program 'bpf_func__null_lseek' at insn offset 0 (0 bytes), code size 14 insns (112 bytes)
libbpf: elf: section(4) license, size 4, link 0, flags 3, type=1
libbpf: license of [bpf_prologue_test] is GPL
libbpf: elf: section(5) version, size 4, link 0, flags 3, type=1
libbpf: kernel version of [bpf_prologue_test] is 5110b
libbpf: elf: section(11) .BTF, size 489, link 0, flags 0, type=1
libbpf: elf: section(13) .BTF.ext, size 144, link 0, flags 0, type=1
libbpf: elf: section(20) .symtab, size 312, link 1, flags 0, type=2
libbpf: looking for externs among 13 symbols...
libbpf: collected 0 externs total
bpf: config program 'func=null_lseek file->f_mode offset orig'
symbol:null_lseek file:(null) line:0 offset:0 return:0 lazy:(null)
parsing arg: file->f_mode into file, f_mode(1)
parsing arg: offset into offset
parsing arg: orig into orig
bpf: config 'func=null_lseek file->f_mode offset orig' is ok
Looking at the vmlinux_path (8 entries long)
symsrc__init: build id mismatch for vmlinux.
Using /usr/lib/debug/lib/modules/5.17.11-300.fc36.x86_64/vmlinux for symbols
Open Debuginfo file: /usr/lib/debug/.build-id/f2/26f5d75e6842add57095a0615a1e5c16783dd7.debug
Try to find probe point from debuginfo.
Opening /sys/kernel/tracing//README write=0
Matched function: null_lseek [73f7810]
Probe point found: null_lseek+0
Searching 'file' variable in context.
Converting variable file into trace event.
converting f_mode in file
f_mode type is unsigned int.
Searching 'offset' variable in context.
Converting variable offset into trace event.
offset type is long long int.
Searching 'orig' variable in context.
Converting variable orig into trace event.
orig type is int.
Found 1 probe_trace_events.
Opening /sys/kernel/tracing//kprobe_events write=1
Writing event: p:perf_bpf_probe/func _text+8502800 f_mode=+68(%di):x32 offset=%si:s64 orig=%dx:s32
In map_prologue, ntevs=1
mapping[0]=0
prologue: pass validation
prologue: slow path
prologue: fetch arg 0, base reg is %di
prologue: arg 0: offset 68
prologue: fetch arg 1, base reg is %si
prologue: fetch arg 2, base reg is %dx
prologue: load arg 0, insn_sz is BPF_W
prologue: load arg 1, insn_sz is BPF_DW
prologue: load arg 2, insn_sz is BPF_DW
add bpf event perf_bpf_probe:func and attach bpf program 4
adding perf_bpf_probe:func
adding perf_bpf_probe:func to 0x3739570
Using CPUID GenuineIntel-6-8E-A
mmap size 1052672B
BPF filter result incorrect, expected 28, got 222 samples
Opening /sys/kernel/tracing//kprobe_events write=1
Opening /sys/kernel/tracing//uprobe_events write=1
Parsing probe_events: p:perf_bpf_probe/func _text+8502800 f_mode=+68(%di):x32 offset=%si:s64 orig=%dx:s32
Group:perf_bpf_probe Event:func probe:p
Writing event: -:perf_bpf_probe/func
test child finished with -1
---- end ----
BPF filter subtest 3: FAILED!
 63: Test libpfm4 support                                            :
 96: perf stat --bpf-counters test                                   :
--- start ---
test child forked, pid 732749
test child finished with 0
---- end ----
perf stat --bpf-counters test: Ok
[root@quaco ~]# 
[root@quaco ~]# 
[root@quaco ~]# 



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

* Re: [PATCHv4 bpf-next 0/2] perf tools: Fix prologue generation
  2022-06-15 15:27           ` Arnaldo Carvalho de Melo
@ 2022-06-15 19:36             ` Jiri Olsa
  2022-06-15 22:12               ` Jiri Olsa
  0 siblings, 1 reply; 13+ messages in thread
From: Jiri Olsa @ 2022-06-15 19:36 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Jiri Olsa, Andrii Nakryiko, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, linux-perf-use.,
	Networking, bpf, Ingo Molnar, Namhyung Kim, Alexander Shishkin,
	Peter Zijlstra, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, Ian Rogers

On Wed, Jun 15, 2022 at 12:27:03PM -0300, Arnaldo Carvalho de Melo wrote:
> Em Sun, Jun 12, 2022 at 06:25:09PM +0200, Jiri Olsa escreveu:
> > so the problem is that we prepend init proglogue instructions
> > for each program not just for the one that needs it, so it will
> > mismatch later on.. the fix below makes it work for me
> 
> > Arnaldo,
> > I squashed and pushed the change below changes to my bpf/depre
> > branch, could you please retest?
> 
> Before:
> 
> [acme@quaco perf-urgent]$ git log --oneline -5
> e2cf9d315f90670f (HEAD -> perf/urgent, five/perf/urgent) perf test topology: Use !strncmp(right platform) to fix guest PPC comparision check
> 42e4fb08ff987b50 perf test: Record only user callchains on the "Check Arm64 callgraphs are complete in fp mode" test
> 819d5c3cf75d0f95 perf beauty: Update copy of linux/socket.h with the kernel sources
> ebdc02b3ece8238b perf test: Fix variable length array undefined behavior in bp_account
> 8ff58c35adb7f118 libperf evsel: Open shouldn't leak fd on failure
> [acme@quaco perf-urgent]$ sudo su -
> [root@quaco ~]# perf -v
> perf version 5.19.rc2.ge2cf9d315f90
> [root@quaco ~]# perf test bpf
>  40: LLVM search and compile                                         :
>  40.1: Basic BPF llvm compile                                        : Ok
>  40.3: Compile source for BPF prologue generation                    : Ok
>  40.4: Compile source for BPF relocation                             : Ok
>  42: BPF filter                                                      :
>  42.1: Basic BPF filtering                                           : Ok
>  42.2: BPF pinning                                                   : Ok
>  42.3: BPF prologue generation                                       : Ok
>  63: Test libpfm4 support                                            :
>  96: perf stat --bpf-counters test                                   : Ok
> [root@quaco ~]#
> 
> After your first patch:
> 
> [acme@quaco perf-urgent]$ git log --oneline -5 jolsa/bpf/depre
> 9317b879db422632 (jolsa/bpf/depre) perf tools: Rework prologue generation code
> 4d40831f29f2c9ad perf tools: Register fallback libbpf section handler
> f913ad6559e337b4 libbpf: Fix is_pow_of_2
> f175ece2e3436748 selftests/bpf: Fix tc_redirect_dtime
> 7b711e721234f475 bpf, test_run: Remove unnecessary prog type checks
> [acme@quaco perf-urgent]$ git cherry-pick 4d40831f29f2c9ad
> [perf/urgent ab39fb6880b57507] perf tools: Register fallback libbpf section handler
>  Author: Jiri Olsa <jolsa@kernel.org>
>  Date: Thu Apr 21 15:22:25 2022 +0200
>  1 file changed, 65 insertions(+), 11 deletions(-)
> [acme@quaco perf-urgent]$
> [acme@quaco perf-urgent]$ alias m='rm -rf ~/libexec/perf-core/ ; perf stat -e cycles:u,instructions:u make -k BUILD_BPF_SKEL=1 PYTHON=python3 O=/tmp/build/perf-urgent -C tools/perf install-bin && perf test python'
> [acme@quaco perf-urgent]$ rm -rf /tmp/build/perf-urgent ; mkdir -p /tmp/build/perf-urgent ; m
> <SNIP>
>  19: 'import perf' in python                                         : Ok
> [acme@quaco perf-urgent]$
> [acme@quaco perf-urgent]$ sudo su -
> [sudo] password for acme:
> [root@quaco ~]# perf test bpf
>  40: LLVM search and compile                                         :
>  40.1: Basic BPF llvm compile                                        : Ok
>  40.3: Compile source for BPF prologue generation                    : Ok
>  40.4: Compile source for BPF relocation                             : Ok
>  42: BPF filter                                                      :
>  42.1: Basic BPF filtering                                           : Ok
>  42.2: BPF pinning                                                   : Ok
>  42.3: BPF prologue generation                                       : FAILED!
>  63: Test libpfm4 support                                            :
>  96: perf stat --bpf-counters test                                   : Ok
> [root@quaco ~]#
> 
> perf test -v bpf later, lets see if landing the second patch fixes
> things and this bisection problem is justified:
> 
> [acme@quaco perf-urgent]$ git log --oneline -5 jolsa/bpf/depre
> 9317b879db422632 (jolsa/bpf/depre) perf tools: Rework prologue generation code
> 4d40831f29f2c9ad perf tools: Register fallback libbpf section handler
> f913ad6559e337b4 libbpf: Fix is_pow_of_2
> f175ece2e3436748 selftests/bpf: Fix tc_redirect_dtime
> 7b711e721234f475 bpf, test_run: Remove unnecessary prog type checks
> [acme@quaco perf-urgent]$ git remote update jolsa
> Fetching jolsa
> [acme@quaco perf-urgent]$ git cherry-pick 9317b879db422632
> [perf/urgent 9a36c7c94e1f596d] perf tools: Rework prologue generation code
>  Author: Jiri Olsa <jolsa@kernel.org>
>  Date: Mon May 9 22:46:20 2022 +0200
>  1 file changed, 110 insertions(+), 18 deletions(-)
> [acme@quaco perf-urgent]$
> [acme@quaco perf-urgent]$ rm -rf /tmp/build/perf-urgent ; mkdir -p /tmp/build/perf-urgent ; m
> <SNIP>
>  19: 'import perf' in python                                         : Ok
> [acme@quaco perf-urgent]$ sudo su -
> [root@quaco ~]# perf test bpf
>  40: LLVM search and compile                                         :
>  40.1: Basic BPF llvm compile                                        : Ok
>  40.3: Compile source for BPF prologue generation                    : Ok
>  40.4: Compile source for BPF relocation                             : Ok
>  42: BPF filter                                                      :
>  42.1: Basic BPF filtering                                           : Ok
>  42.2: BPF pinning                                                   : Ok
>  42.3: BPF prologue generation                                       : Ok
>  63: Test libpfm4 support                                            :
>  96: perf stat --bpf-counters test                                   : Ok
> [root@quaco ~]#
> 
> So it works in the end, can it be made to work after the first step? I
> didn't check that.

heya,
so the first patch is preparation and the last one is the real fix

at the moment it does not work without this change, so I don't
think it's a problem for the bisect, is it?

jirka

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

* Re: [PATCHv4 bpf-next 0/2] perf tools: Fix prologue generation
  2022-06-15 19:36             ` Jiri Olsa
@ 2022-06-15 22:12               ` Jiri Olsa
  0 siblings, 0 replies; 13+ messages in thread
From: Jiri Olsa @ 2022-06-15 22:12 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Jiri Olsa, Andrii Nakryiko, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, linux-perf-use.,
	Networking, bpf, Ingo Molnar, Namhyung Kim, Alexander Shishkin,
	Peter Zijlstra, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, Ian Rogers

On Wed, Jun 15, 2022 at 09:36:34PM +0200, Jiri Olsa wrote:
> On Wed, Jun 15, 2022 at 12:27:03PM -0300, Arnaldo Carvalho de Melo wrote:
> > Em Sun, Jun 12, 2022 at 06:25:09PM +0200, Jiri Olsa escreveu:
> > > so the problem is that we prepend init proglogue instructions
> > > for each program not just for the one that needs it, so it will
> > > mismatch later on.. the fix below makes it work for me
> > 
> > > Arnaldo,
> > > I squashed and pushed the change below changes to my bpf/depre
> > > branch, could you please retest?
> > 
> > Before:
> > 
> > [acme@quaco perf-urgent]$ git log --oneline -5
> > e2cf9d315f90670f (HEAD -> perf/urgent, five/perf/urgent) perf test topology: Use !strncmp(right platform) to fix guest PPC comparision check
> > 42e4fb08ff987b50 perf test: Record only user callchains on the "Check Arm64 callgraphs are complete in fp mode" test
> > 819d5c3cf75d0f95 perf beauty: Update copy of linux/socket.h with the kernel sources
> > ebdc02b3ece8238b perf test: Fix variable length array undefined behavior in bp_account
> > 8ff58c35adb7f118 libperf evsel: Open shouldn't leak fd on failure
> > [acme@quaco perf-urgent]$ sudo su -
> > [root@quaco ~]# perf -v
> > perf version 5.19.rc2.ge2cf9d315f90
> > [root@quaco ~]# perf test bpf
> >  40: LLVM search and compile                                         :
> >  40.1: Basic BPF llvm compile                                        : Ok
> >  40.3: Compile source for BPF prologue generation                    : Ok
> >  40.4: Compile source for BPF relocation                             : Ok
> >  42: BPF filter                                                      :
> >  42.1: Basic BPF filtering                                           : Ok
> >  42.2: BPF pinning                                                   : Ok
> >  42.3: BPF prologue generation                                       : Ok
> >  63: Test libpfm4 support                                            :
> >  96: perf stat --bpf-counters test                                   : Ok
> > [root@quaco ~]#
> > 
> > After your first patch:
> > 
> > [acme@quaco perf-urgent]$ git log --oneline -5 jolsa/bpf/depre
> > 9317b879db422632 (jolsa/bpf/depre) perf tools: Rework prologue generation code
> > 4d40831f29f2c9ad perf tools: Register fallback libbpf section handler
> > f913ad6559e337b4 libbpf: Fix is_pow_of_2
> > f175ece2e3436748 selftests/bpf: Fix tc_redirect_dtime
> > 7b711e721234f475 bpf, test_run: Remove unnecessary prog type checks
> > [acme@quaco perf-urgent]$ git cherry-pick 4d40831f29f2c9ad
> > [perf/urgent ab39fb6880b57507] perf tools: Register fallback libbpf section handler
> >  Author: Jiri Olsa <jolsa@kernel.org>
> >  Date: Thu Apr 21 15:22:25 2022 +0200
> >  1 file changed, 65 insertions(+), 11 deletions(-)
> > [acme@quaco perf-urgent]$
> > [acme@quaco perf-urgent]$ alias m='rm -rf ~/libexec/perf-core/ ; perf stat -e cycles:u,instructions:u make -k BUILD_BPF_SKEL=1 PYTHON=python3 O=/tmp/build/perf-urgent -C tools/perf install-bin && perf test python'
> > [acme@quaco perf-urgent]$ rm -rf /tmp/build/perf-urgent ; mkdir -p /tmp/build/perf-urgent ; m
> > <SNIP>
> >  19: 'import perf' in python                                         : Ok
> > [acme@quaco perf-urgent]$
> > [acme@quaco perf-urgent]$ sudo su -
> > [sudo] password for acme:
> > [root@quaco ~]# perf test bpf
> >  40: LLVM search and compile                                         :
> >  40.1: Basic BPF llvm compile                                        : Ok
> >  40.3: Compile source for BPF prologue generation                    : Ok
> >  40.4: Compile source for BPF relocation                             : Ok
> >  42: BPF filter                                                      :
> >  42.1: Basic BPF filtering                                           : Ok
> >  42.2: BPF pinning                                                   : Ok
> >  42.3: BPF prologue generation                                       : FAILED!
> >  63: Test libpfm4 support                                            :
> >  96: perf stat --bpf-counters test                                   : Ok
> > [root@quaco ~]#
> > 
> > perf test -v bpf later, lets see if landing the second patch fixes
> > things and this bisection problem is justified:
> > 
> > [acme@quaco perf-urgent]$ git log --oneline -5 jolsa/bpf/depre
> > 9317b879db422632 (jolsa/bpf/depre) perf tools: Rework prologue generation code
> > 4d40831f29f2c9ad perf tools: Register fallback libbpf section handler
> > f913ad6559e337b4 libbpf: Fix is_pow_of_2
> > f175ece2e3436748 selftests/bpf: Fix tc_redirect_dtime
> > 7b711e721234f475 bpf, test_run: Remove unnecessary prog type checks
> > [acme@quaco perf-urgent]$ git remote update jolsa
> > Fetching jolsa
> > [acme@quaco perf-urgent]$ git cherry-pick 9317b879db422632
> > [perf/urgent 9a36c7c94e1f596d] perf tools: Rework prologue generation code
> >  Author: Jiri Olsa <jolsa@kernel.org>
> >  Date: Mon May 9 22:46:20 2022 +0200
> >  1 file changed, 110 insertions(+), 18 deletions(-)
> > [acme@quaco perf-urgent]$
> > [acme@quaco perf-urgent]$ rm -rf /tmp/build/perf-urgent ; mkdir -p /tmp/build/perf-urgent ; m
> > <SNIP>
> >  19: 'import perf' in python                                         : Ok
> > [acme@quaco perf-urgent]$ sudo su -
> > [root@quaco ~]# perf test bpf
> >  40: LLVM search and compile                                         :
> >  40.1: Basic BPF llvm compile                                        : Ok
> >  40.3: Compile source for BPF prologue generation                    : Ok
> >  40.4: Compile source for BPF relocation                             : Ok
> >  42: BPF filter                                                      :
> >  42.1: Basic BPF filtering                                           : Ok
> >  42.2: BPF pinning                                                   : Ok
> >  42.3: BPF prologue generation                                       : Ok
> >  63: Test libpfm4 support                                            :
> >  96: perf stat --bpf-counters test                                   : Ok
> > [root@quaco ~]#
> > 
> > So it works in the end, can it be made to work after the first step? I
> > didn't check that.
> 
> heya,
> so the first patch is preparation and the last one is the real fix
> 
> at the moment it does not work without this change, so I don't
> think it's a problem for the bisect, is it?

ah I just checked that thing worked before :)) ok.. I think the only
way here is to squash them together.. doesn't look that bad

jirka

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

end of thread, other threads:[~2022-06-15 22:12 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-03 20:45 [PATCHv4 bpf-next 0/2] perf tools: Fix prologue generation Jiri Olsa
2022-06-03 20:45 ` [PATCHv4 bpf-next 1/2] perf tools: Register fallback libbpf section handler Jiri Olsa
2022-06-03 20:45 ` [PATCHv4 bpf-next 2/2] perf tools: Rework prologue generation code Jiri Olsa
2022-06-09 20:31 ` [PATCHv4 bpf-next 0/2] perf tools: Fix prologue generation Andrii Nakryiko
2022-06-10 18:34   ` Arnaldo Carvalho de Melo
2022-06-10 18:38     ` Arnaldo Carvalho de Melo
2022-06-10 18:45     ` Andrii Nakryiko
2022-06-10 20:53       ` Jiri Olsa
2022-06-12 16:25         ` Jiri Olsa
2022-06-13 20:15           ` Arnaldo Carvalho de Melo
2022-06-15 15:27           ` Arnaldo Carvalho de Melo
2022-06-15 19:36             ` Jiri Olsa
2022-06-15 22:12               ` Jiri Olsa

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.