Skip to contentSkip to conversation

perf tools: Fix prologue generation

PATCH Superseded Jiri Olsa 5 participants 18 messages Apr 22, 2022 – Apr 27, 2022

Subsystems (5)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jiri Olsa

author reviewer
#0 lore raw

cover perf tools: Fix prologue generation

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

Also available in:
git://git.kernel.org/pub/scm/linux/kernel/git/jolsa/perf.git
perf/depre

thanks,
jirka

[1] https://lore.kernel.org/bpf/CAEf4BzaiBO3_617kkXZdYJ8hS8YF--ZLgapNbgeeEJ-pY0H88g@mail.gmail.com/
---
Jiri Olsa (5):
libbpf: Add bpf_program__set_insns function
libbpf: Load prog's instructions after prog_prepare_load_fn callback
perf tools: Move libbpf init in libbpf_init function
perf tools: Register perfkprobe libbpf section handler
perf tools: Rework prologue generation code

 tools/lib/bpf/libbpf.c                      |  10 +++++
 tools/lib/bpf/libbpf.h                      |  12 ++++++
 tools/lib/bpf/libbpf.map                    |   3 +-
 tools/perf/include/bpf/bpf.h                |   2 +-
 tools/perf/tests/bpf-script-example.c       |   2 +-
 tools/perf/tests/bpf-script-test-prologue.c |   2 +-
 tools/perf/util/bpf-loader.c                | 213 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------
 7 files changed, 212 insertions(+), 32 deletions(-)
author reviewer
#1 lore raw

PATCH 1/5 libbpf: Add bpf_program__set_insns function

Adding bpf_program__set_insns that allows to set new instructions for program.

Also moving bpf_program__attach_kprobe_multi_opts on the proper name sorted place in map file.

---
 tools/lib/bpf/libbpf.c   |  8 ++++++++
 tools/lib/bpf/libbpf.h   | 12 ++++++++++++
 tools/lib/bpf/libbpf.map |  3 ++-
 3 files changed, 22 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +8 -0
index 809fe209cdcc..284790d81c1b 100644
@@ -8457,6 +8457,14 @@ size_t bpf_program__insn_cnt(const struct bpf_program *prog)
 	return prog->insns_cnt;
 }
 
+void bpf_program__set_insns(struct bpf_program *prog,
+			    struct bpf_insn *insns, size_t insns_cnt)
+{
+	free(prog->insns);
+	prog->insns = insns;
+	prog->insns_cnt = insns_cnt;
+}
+
 int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
 			  bpf_program_prep_t prep)
 {
tools/lib/bpf/libbpf.h +12 -0
index 05dde85e19a6..b31ad58d335f 100644
@@ -323,6 +323,18 @@ struct bpf_insn;
  * different.
  */
 LIBBPF_API const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog);
+
+/**
+ * @brief **bpf_program__set_insns()** can set BPF program's underlying
+ * BPF instructions.
+ * @param prog BPF program for which to return instructions
+ * @param insn a pointer to an array of BPF instructions
+ * @param insns_cnt number of `struct bpf_insn`'s that form
+ * specified BPF program
+ */
+LIBBPF_API void bpf_program__set_insns(struct bpf_program *prog,
+				       struct bpf_insn *insns, size_t insns_cnt);
+
 /**
  * @brief **bpf_program__insn_cnt()** returns number of `struct bpf_insn`'s
  * that form specified BPF program.
tools/lib/bpf/libbpf.map +2 -1
index dd35ee58bfaa..afa10d24ab41 100644
@@ -444,7 +444,8 @@ LIBBPF_0.8.0 {
 	global:
 		bpf_object__destroy_subskeleton;
 		bpf_object__open_subskeleton;
+		bpf_program__attach_kprobe_multi_opts;
+		bpf_program__set_insns;
 		libbpf_register_prog_handler;
 		libbpf_unregister_prog_handler;
-		bpf_program__attach_kprobe_multi_opts;
 } LIBBPF_0.7.0;
Signature · 1 line
2.35.1
maintainer
#2 lore raw

Re: [PATCH perf/core 1/5] libbpf: Add bpf_program__set_insns function

Quoted 42 lines · Jiri Olsa

Adding bpf_program__set_insns that allows to set new instructions for program.

Also moving bpf_program__attach_kprobe_multi_opts on the proper name sorted place in map file.

---
  tools/lib/bpf/libbpf.c   |  8 ++++++++
  tools/lib/bpf/libbpf.h   | 12 ++++++++++++
  tools/lib/bpf/libbpf.map |  3 ++-
  3 files changed, 22 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +8 -0
index 809fe209cdcc..284790d81c1b 100644
@@ -8457,6 +8457,14 @@ size_t bpf_program__insn_cnt(const struct bpf_program *prog)
  	return prog->insns_cnt;
  }
 
+void bpf_program__set_insns(struct bpf_program *prog,
+			    struct bpf_insn *insns, size_t insns_cnt)
+{
+	free(prog->insns);
+	prog->insns = insns;
+	prog->insns_cnt = insns_cnt;
+}
+
  int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
  			  bpf_program_prep_t prep)
  {
tools/lib/bpf/libbpf.h +2 -0
index 05dde85e19a6..b31ad58d335f 100644
@@ -323,6 +323,18 @@ struct bpf_insn;
   * different.
   */
  LIBBPF_API const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog);
+
+/**

tools/lib/bpf/libbpf.h struct bpf_insn; · line 337

Quoted 10 lines
tools/lib/bpf/libbpf.h +10 -0
+ * @brief **bpf_program__set_insns()** can set BPF program's underlying
+ * BPF instructions.
+ * @param prog BPF program for which to return instructions
+ * @param insn a pointer to an array of BPF instructions
+ * @param insns_cnt number of `struct bpf_insn`'s that form
+ * specified BPF program
+ */
+LIBBPF_API void bpf_program__set_insns(struct bpf_program *prog,
+				       struct bpf_insn *insns, size_t insns_cnt);
+

Iiuc, patch 2 should be squashed into this one given they logically belong to the same change?

Fwiw, I think the API description should be elaborated a bit more, in particular that the passed-in insns need to be from allocated dynamic memory which is later on passed to free(), and maybe also constraints at which point in time bpf_program__set_insns() may be called.. (as well as high-level description on potential use cases e.g. around patch 4).

Quoted 18 lines
tools/lib/bpf/libbpf.h +0 -0
  /**
   * @brief **bpf_program__insn_cnt()** returns number of `struct bpf_insn`'s
   * that form specified BPF program.
tools/lib/bpf/libbpf.map +2 -1
index dd35ee58bfaa..afa10d24ab41 100644
@@ -444,7 +444,8 @@ LIBBPF_0.8.0 {
  	global:
  		bpf_object__destroy_subskeleton;
  		bpf_object__open_subskeleton;
+		bpf_program__attach_kprobe_multi_opts;
+		bpf_program__set_insns;
  		libbpf_register_prog_handler;
  		libbpf_unregister_prog_handler;
-		bpf_program__attach_kprobe_multi_opts;
  } LIBBPF_0.7.0;
#3 lore raw

Re: [PATCH perf/core 1/5] libbpf: Add bpf_program__set_insns function

Quoted 47 lines · Daniel Borkmann
Quoted 46 lines · Jiri Olsa

Adding bpf_program__set_insns that allows to set new instructions for program.

Also moving bpf_program__attach_kprobe_multi_opts on the proper name sorted place in map file.

---
  tools/lib/bpf/libbpf.c   |  8 ++++++++
  tools/lib/bpf/libbpf.h   | 12 ++++++++++++
  tools/lib/bpf/libbpf.map |  3 ++-
  3 files changed, 22 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +8 -0
index 809fe209cdcc..284790d81c1b 100644
@@ -8457,6 +8457,14 @@ size_t bpf_program__insn_cnt(const struct bpf_program *prog)
  	return prog->insns_cnt;
  }
+void bpf_program__set_insns(struct bpf_program *prog,
+			    struct bpf_insn *insns, size_t insns_cnt)
+{
+	free(prog->insns);
+	prog->insns = insns;
+	prog->insns_cnt = insns_cnt;
+}
+
  int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
  			  bpf_program_prep_t prep)
  {
tools/lib/bpf/libbpf.h +7 -0
index 05dde85e19a6..b31ad58d335f 100644
@@ -323,6 +323,18 @@ struct bpf_insn;
   * different.
   */
  LIBBPF_API const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog);
+
+/**
+ * @brief **bpf_program__set_insns()** can set BPF program's underlying
+ * BPF instructions.
+ * @param prog BPF program for which to return instructions
+ * @param insn a pointer to an array of BPF instructions
+ * @param insns_cnt number of `struct bpf_insn`'s that form
Quoted 8 lines

tools/lib/bpf/libbpf.h struct bpf_insn; · line 337

Quoted 5 lines
tools/lib/bpf/libbpf.h +5 -0
+ * specified BPF program
+ */
+LIBBPF_API void bpf_program__set_insns(struct bpf_program *prog,
+				       struct bpf_insn *insns, size_t insns_cnt);
+

Iiuc, patch 2 should be squashed into this one given they logically belong to the same change?

right, that's probably better

Quoted 6 lines

Fwiw, I think the API description should be elaborated a bit more, in particular that the passed-in insns need to be from allocated dynamic memory which is later on passed to free(), and maybe also constraints at which point in time bpf_program__set_insns() may be called.. (as well as high-level description on potential use cases e.g. around patch 4).

ok, will add that

thanks,
jirka

Quoted 20 lines
Quoted 19 lines
tools/lib/bpf/libbpf.h +0 -0
  /**
   * @brief **bpf_program__insn_cnt()** returns number of `struct bpf_insn`'s
   * that form specified BPF program.
tools/lib/bpf/libbpf.map +2 -1
index dd35ee58bfaa..afa10d24ab41 100644
@@ -444,7 +444,8 @@ LIBBPF_0.8.0 {
  	global:
  		bpf_object__destroy_subskeleton;
  		bpf_object__open_subskeleton;
+		bpf_program__attach_kprobe_multi_opts;
+		bpf_program__set_insns;
  		libbpf_register_prog_handler;
  		libbpf_unregister_prog_handler;
-		bpf_program__attach_kprobe_multi_opts;
  } LIBBPF_0.7.0;
#4 lore raw

Re: [PATCH perf/core 1/5] libbpf: Add bpf_program__set_insns function

In reply to Daniel Borkmann
Quoted 7 lines · Daniel Borkmann
Quoted 5 lines · Jiri Olsa

Adding bpf_program__set_insns that allows to set new instructions for program.

Also moving bpf_program__attach_kprobe_multi_opts on the proper name sorted place in map file.

would make sense to fix it as a separate patch, it has nothing to do with bpf_program__set_insns() API itself

Quoted 16 lines
Quoted 16 lines
---
  tools/lib/bpf/libbpf.c   |  8 ++++++++
  tools/lib/bpf/libbpf.h   | 12 ++++++++++++
  tools/lib/bpf/libbpf.map |  3 ++-
  3 files changed, 22 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +0 -0
index 809fe209cdcc..284790d81c1b 100644
@@ -8457,6 +8457,14 @@ size_t bpf_program__insn_cnt(const struct bpf_program *prog)
      return prog->insns_cnt;
  }
 
Quoted 6 lines

tools/lib/bpf/libbpf.c size_t bpf_program__insn_cnt(const struct bpf_program *prog) · line 8465

Quoted 6 lines
tools/lib/bpf/libbpf.c +6 -0
+void bpf_program__set_insns(struct bpf_program *prog,
+                         struct bpf_insn *insns, size_t insns_cnt)
+{
+     free(prog->insns);
+     prog->insns = insns;
+     prog->insns_cnt = insns_cnt;

let's not store user-provided pointer here. Please realloc prog->insns as necessary and copy over insns into it.

Also let's at least add the check for prog->loaded and return -EBUSY in such a case. And of course this API should return int, not void.

Quoted 14 lines
Quoted 14 lines
tools/lib/bpf/libbpf.c +2 -0
+}
+
  int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
                        bpf_program_prep_t prep)
  {
tools/lib/bpf/libbpf.h +1 -0
index 05dde85e19a6..b31ad58d335f 100644
@@ -323,6 +323,18 @@ struct bpf_insn;
   * different.
   */
  LIBBPF_API const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog);
+
Quoted 8 lines

tools/lib/bpf/libbpf.h struct bpf_insn; · line 334

Quoted 8 lines
tools/lib/bpf/libbpf.h +8 -0
+/**
+ * @brief **bpf_program__set_insns()** can set BPF program's underlying
+ * BPF instructions.
+ * @param prog BPF program for which to return instructions
+ * @param insn a pointer to an array of BPF instructions
+ * @param insns_cnt number of `struct bpf_insn`'s that form
+ * specified BPF program
+ */

This API makes me want to cry... but I can't come up with anything better for perf's use case.

But it can only more or less safely and sanely be used from the prog_prepare_load_fn callback, so please add a big warning here saying that this is a very advanced libbpf API and the user needs to know what they are doing and this should be used from prog_prepare_load_fn callback only. If bpf_program__set_insns() is called before prog_prepare_load_fn any map/subprog/etc relocation will most probably fail or corrupt BPF program code.

Quoted 2 lines

tools/lib/bpf/libbpf.h struct bpf_insn; · line 336

Quoted 2 lines
tools/lib/bpf/libbpf.h +2 -0
+LIBBPF_API void bpf_program__set_insns(struct bpf_program *prog,
+                                    struct bpf_insn *insns, size_t insns_cnt);

s/insns_cnt/insn_cnt/

Quoted 10 lines

tools/lib/bpf/libbpf.h struct bpf_insn; · line 337

Quoted 1 line
tools/lib/bpf/libbpf.h +1 -0
+

Iiuc, patch 2 should be squashed into this one given they logically belong to the same change?

Fwiw, I think the API description should be elaborated a bit more, in particular that the passed-in insns need to be from allocated dynamic memory which is later on passed to free(), and maybe also constraints at which point in time bpf_program__set_insns() may be called.. (as well as high-level description on potential use cases e.g. around patch 4).

Yep, patch #1 is kind of broken without patch #2, so let's combine them.

Quoted 20 lines
Quoted 19 lines
tools/lib/bpf/libbpf.h +0 -0
  /**
   * @brief **bpf_program__insn_cnt()** returns number of `struct bpf_insn`'s
   * that form specified BPF program.
tools/lib/bpf/libbpf.map +2 -1
index dd35ee58bfaa..afa10d24ab41 100644
@@ -444,7 +444,8 @@ LIBBPF_0.8.0 {
      global:
              bpf_object__destroy_subskeleton;
              bpf_object__open_subskeleton;
+             bpf_program__attach_kprobe_multi_opts;
+             bpf_program__set_insns;
              libbpf_register_prog_handler;
              libbpf_unregister_prog_handler;
-             bpf_program__attach_kprobe_multi_opts;
  } LIBBPF_0.7.0;
#5 lore raw

Re: [PATCH perf/core 1/5] libbpf: Add bpf_program__set_insns function

Quoted 11 lines · Andrii Nakryiko
Quoted 7 lines · Daniel Borkmann
Quoted 5 lines · Jiri Olsa

Adding bpf_program__set_insns that allows to set new instructions for program.

Also moving bpf_program__attach_kprobe_multi_opts on the proper name sorted place in map file.

would make sense to fix it as a separate patch, it has nothing to do with bpf_program__set_insns() API itself

np

Quoted 27 lines
Quoted 17 lines
Quoted 17 lines
---
  tools/lib/bpf/libbpf.c   |  8 ++++++++
  tools/lib/bpf/libbpf.h   | 12 ++++++++++++
  tools/lib/bpf/libbpf.map |  3 ++-
  3 files changed, 22 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +0 -0
index 809fe209cdcc..284790d81c1b 100644
@@ -8457,6 +8457,14 @@ size_t bpf_program__insn_cnt(const struct bpf_program *prog)
      return prog->insns_cnt;
  }
 
Quoted 6 lines

tools/lib/bpf/libbpf.c size_t bpf_program__insn_cnt(const struct bpf_program *prog) · line 8465

Quoted 6 lines
tools/lib/bpf/libbpf.c +6 -0
+void bpf_program__set_insns(struct bpf_program *prog,
+                         struct bpf_insn *insns, size_t insns_cnt)
+{
+     free(prog->insns);
+     prog->insns = insns;
+     prog->insns_cnt = insns_cnt;

let's not store user-provided pointer here. Please realloc prog->insns as necessary and copy over insns into it.

Quoted 2 lines

Also let's at least add the check for prog->loaded and return -EBUSY in such a case. And of course this API should return int, not void.

ok, will change

Quoted 27 lines
Quoted 15 lines
Quoted 15 lines
tools/lib/bpf/libbpf.c +2 -0
+}
+
  int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
                        bpf_program_prep_t prep)
  {
tools/lib/bpf/libbpf.h +1 -0
index 05dde85e19a6..b31ad58d335f 100644
@@ -323,6 +323,18 @@ struct bpf_insn;
   * different.
   */
  LIBBPF_API const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog);
+
Quoted 8 lines

tools/lib/bpf/libbpf.h struct bpf_insn; · line 334

Quoted 8 lines
tools/lib/bpf/libbpf.h +8 -0
+/**
+ * @brief **bpf_program__set_insns()** can set BPF program's underlying
+ * BPF instructions.
+ * @param prog BPF program for which to return instructions
+ * @param insn a pointer to an array of BPF instructions
+ * @param insns_cnt number of `struct bpf_insn`'s that form
+ * specified BPF program
+ */

This API makes me want to cry... but I can't come up with anything better for perf's use case.

Quoted 7 lines

But it can only more or less safely and sanely be used from the prog_prepare_load_fn callback, so please add a big warning here saying that this is a very advanced libbpf API and the user needs to know what they are doing and this should be used from prog_prepare_load_fn callback only. If bpf_program__set_insns() is called before prog_prepare_load_fn any map/subprog/etc relocation will most probably fail or corrupt BPF program code.

will add the warnings

Quoted 11 lines
Quoted 3 lines

tools/lib/bpf/libbpf.h struct bpf_insn; · line 336

Quoted 3 lines
tools/lib/bpf/libbpf.h +2 -0
+LIBBPF_API void bpf_program__set_insns(struct bpf_program *prog,
+                                    struct bpf_insn *insns, size_t insns_cnt);

s/insns_cnt/insn_cnt/

Quoted 5 lines

tools/lib/bpf/libbpf.h struct bpf_insn; · line 337

Quoted 1 line
tools/lib/bpf/libbpf.h +1 -0
+

Iiuc, patch 2 should be squashed into this one given they logically belong to the same change?

Quoted 7 lines
Quoted 5 lines

Fwiw, I think the API description should be elaborated a bit more, in particular that the passed-in insns need to be from allocated dynamic memory which is later on passed to free(), and maybe also constraints at which point in time bpf_program__set_insns() may be called.. (as well as high-level description on potential use cases e.g. around patch 4).

Yep, patch #1 is kind of broken without patch #2, so let's combine them.

ok

thanks,
jirka

#6 lore raw

Re: [PATCH perf/core 1/5] libbpf: Add bpf_program__set_insns function

Quoted 43 lines · Jiri Olsa
Quoted 11 lines · Andrii Nakryiko
Quoted 7 lines · Daniel Borkmann
Quoted 5 lines · Jiri Olsa

Adding bpf_program__set_insns that allows to set new instructions for program.

Also moving bpf_program__attach_kprobe_multi_opts on the proper name sorted place in map file.

would make sense to fix it as a separate patch, it has nothing to do with bpf_program__set_insns() API itself

np

Quoted 27 lines
Quoted 17 lines
Quoted 17 lines
---
  tools/lib/bpf/libbpf.c   |  8 ++++++++
  tools/lib/bpf/libbpf.h   | 12 ++++++++++++
  tools/lib/bpf/libbpf.map |  3 ++-
  3 files changed, 22 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +0 -0
index 809fe209cdcc..284790d81c1b 100644
@@ -8457,6 +8457,14 @@ size_t bpf_program__insn_cnt(const struct bpf_program *prog)
      return prog->insns_cnt;
  }
 
Quoted 6 lines

tools/lib/bpf/libbpf.c size_t bpf_program__insn_cnt(const struct bpf_program *prog) · line 8465

Quoted 6 lines
tools/lib/bpf/libbpf.c +6 -0
+void bpf_program__set_insns(struct bpf_program *prog,
+                         struct bpf_insn *insns, size_t insns_cnt)
+{
+     free(prog->insns);
+     prog->insns = insns;
+     prog->insns_cnt = insns_cnt;

let's not store user-provided pointer here. Please realloc prog->insns as necessary and copy over insns into it.

Quoted 32 lines
Quoted 2 lines

Also let's at least add the check for prog->loaded and return -EBUSY in such a case. And of course this API should return int, not void.

ok, will change

Quoted 27 lines
Quoted 15 lines
Quoted 15 lines
tools/lib/bpf/libbpf.c +2 -0
+}
+
  int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
                        bpf_program_prep_t prep)
  {
tools/lib/bpf/libbpf.h +1 -0
index 05dde85e19a6..b31ad58d335f 100644
@@ -323,6 +323,18 @@ struct bpf_insn;
   * different.
   */
  LIBBPF_API const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog);
+
Quoted 8 lines

tools/lib/bpf/libbpf.h struct bpf_insn; · line 334

Quoted 8 lines
tools/lib/bpf/libbpf.h +8 -0
+/**
+ * @brief **bpf_program__set_insns()** can set BPF program's underlying
+ * BPF instructions.
+ * @param prog BPF program for which to return instructions
+ * @param insn a pointer to an array of BPF instructions
+ * @param insns_cnt number of `struct bpf_insn`'s that form
+ * specified BPF program
+ */

This API makes me want to cry... but I can't come up with anything better for perf's use case.

So thinking about this some more. If we make libbpf not to close maps and prog FDs on BPF program load failure automatically and instead doing it in bpf_object__close(), which would seem to be a totally fine semantics and won't break any reasonable application as they always have to call bpf_object__close() anyways to clean up all the resources; we wouldn't need this horror of bpf_program__set_insns(). Your BPF program would fail to load, but you'll get its fully prepared instructions with bpf_program__insns(), then you can just append correct preamble. Meanwhile, all the maps will be created (they are always created before the first program load), so all the FDs will be correct.

This is certainly advanced knowledge of libbpf behavior, but the use case you are trying to solve is also very unique and advanced (and I wouldn't recommend anyone trying to do this anyways). WDYT? Would that work?

Quoted 33 lines
Quoted 7 lines

But it can only more or less safely and sanely be used from the prog_prepare_load_fn callback, so please add a big warning here saying that this is a very advanced libbpf API and the user needs to know what they are doing and this should be used from prog_prepare_load_fn callback only. If bpf_program__set_insns() is called before prog_prepare_load_fn any map/subprog/etc relocation will most probably fail or corrupt BPF program code.

will add the warnings

Quoted 11 lines
Quoted 3 lines

tools/lib/bpf/libbpf.h struct bpf_insn; · line 336

Quoted 3 lines
tools/lib/bpf/libbpf.h +2 -0
+LIBBPF_API void bpf_program__set_insns(struct bpf_program *prog,
+                                    struct bpf_insn *insns, size_t insns_cnt);

s/insns_cnt/insn_cnt/

Quoted 5 lines

tools/lib/bpf/libbpf.h struct bpf_insn; · line 337

Quoted 1 line
tools/lib/bpf/libbpf.h +1 -0
+

Iiuc, patch 2 should be squashed into this one given they logically belong to the same change?

Quoted 7 lines
Quoted 5 lines

Fwiw, I think the API description should be elaborated a bit more, in particular that the passed-in insns need to be from allocated dynamic memory which is later on passed to free(), and maybe also constraints at which point in time bpf_program__set_insns() may be called.. (as well as high-level description on potential use cases e.g. around patch 4).

Yep, patch #1 is kind of broken without patch #2, so let's combine them.

ok

thanks,
jirka

#7 lore raw

Re: [PATCH perf/core 1/5] libbpf: Add bpf_program__set_insns function

Quoted 89 lines · Andrii Nakryiko
Quoted 43 lines · Jiri Olsa
Quoted 11 lines · Andrii Nakryiko
Quoted 7 lines · Daniel Borkmann
Quoted 5 lines · Jiri Olsa

Adding bpf_program__set_insns that allows to set new instructions for program.

Also moving bpf_program__attach_kprobe_multi_opts on the proper name sorted place in map file.

would make sense to fix it as a separate patch, it has nothing to do with bpf_program__set_insns() API itself

np

Quoted 27 lines
Quoted 17 lines
Quoted 17 lines
---
  tools/lib/bpf/libbpf.c   |  8 ++++++++
  tools/lib/bpf/libbpf.h   | 12 ++++++++++++
  tools/lib/bpf/libbpf.map |  3 ++-
  3 files changed, 22 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +0 -0
index 809fe209cdcc..284790d81c1b 100644
@@ -8457,6 +8457,14 @@ size_t bpf_program__insn_cnt(const struct bpf_program *prog)
      return prog->insns_cnt;
  }
 
Quoted 6 lines

tools/lib/bpf/libbpf.c size_t bpf_program__insn_cnt(const struct bpf_program *prog) · line 8465

Quoted 6 lines
tools/lib/bpf/libbpf.c +6 -0
+void bpf_program__set_insns(struct bpf_program *prog,
+                         struct bpf_insn *insns, size_t insns_cnt)
+{
+     free(prog->insns);
+     prog->insns = insns;
+     prog->insns_cnt = insns_cnt;

let's not store user-provided pointer here. Please realloc prog->insns as necessary and copy over insns into it.

Quoted 32 lines
Quoted 2 lines

Also let's at least add the check for prog->loaded and return -EBUSY in such a case. And of course this API should return int, not void.

ok, will change

Quoted 27 lines
Quoted 15 lines
Quoted 15 lines
tools/lib/bpf/libbpf.c +2 -0
+}
+
  int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
                        bpf_program_prep_t prep)
  {
tools/lib/bpf/libbpf.h +1 -0
index 05dde85e19a6..b31ad58d335f 100644
@@ -323,6 +323,18 @@ struct bpf_insn;
   * different.
   */
  LIBBPF_API const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog);
+
Quoted 8 lines

tools/lib/bpf/libbpf.h struct bpf_insn; · line 334

Quoted 8 lines
tools/lib/bpf/libbpf.h +8 -0
+/**
+ * @brief **bpf_program__set_insns()** can set BPF program's underlying
+ * BPF instructions.
+ * @param prog BPF program for which to return instructions
+ * @param insn a pointer to an array of BPF instructions
+ * @param insns_cnt number of `struct bpf_insn`'s that form
+ * specified BPF program
+ */

This API makes me want to cry... but I can't come up with anything better for perf's use case.

So thinking about this some more. If we make libbpf not to close maps and prog FDs on BPF program load failure automatically and instead doing it in bpf_object__close(), which would seem to be a totally fine semantics and won't break any reasonable application as they always have to call bpf_object__close() anyways to clean up all the resources; we wouldn't need this horror of bpf_program__set_insns(). Your BPF program would fail to load, but you'll get its fully prepared instructions with bpf_program__insns(), then you can just append correct preamble. Meanwhile, all the maps will be created (they are always created before the first program load), so all the FDs will be correct.

Quoted 4 lines

This is certainly advanced knowledge of libbpf behavior, but the use case you are trying to solve is also very unique and advanced (and I wouldn't recommend anyone trying to do this anyways). WDYT? Would that work?

hm, so verifier will fail after all maps are set up during the walk of the program instructions.. I guess that could work, I'll give it a try, should be easy change in libbpf (like below) and also in perf

but still the bpf_program__set_insns seems less horror to me

jirka

---

tools/lib/bpf/libbpf.c +0 -13
index c8df74e5f658..1eb75d4231ff 100644
@@ -7577,19 +7577,6 @@ static int bpf_object_load(struct bpf_object *obj, int extra_log_level, const ch
 	obj->btf_vmlinux = NULL;
 
 	obj->loaded = true; /* doesn't matter if successfully or not */
-
-	if (err)
-		goto out;
-
-	return 0;
-out:
-	/* unpin any maps that were auto-pinned during load */
-	for (i = 0; i < obj->nr_maps; i++)
-		if (obj->maps[i].pinned && !obj->maps[i].reused)
-			bpf_map__unpin(&obj->maps[i], NULL);
-
-	bpf_object_unload(obj);
-	pr_warn("failed to load object '%s'\n", obj->path);
 	return libbpf_err(err);
 }
 
#8 lore raw

Re: [PATCH perf/core 1/5] libbpf: Add bpf_program__set_insns function

Quoted 96 lines · Jiri Olsa
Quoted 89 lines · Andrii Nakryiko
Quoted 43 lines · Jiri Olsa
Quoted 11 lines · Andrii Nakryiko
Quoted 7 lines · Daniel Borkmann
Quoted 5 lines · Jiri Olsa

Adding bpf_program__set_insns that allows to set new instructions for program.

Also moving bpf_program__attach_kprobe_multi_opts on the proper name sorted place in map file.

would make sense to fix it as a separate patch, it has nothing to do with bpf_program__set_insns() API itself

np

Quoted 27 lines
Quoted 17 lines
Quoted 17 lines
---
  tools/lib/bpf/libbpf.c   |  8 ++++++++
  tools/lib/bpf/libbpf.h   | 12 ++++++++++++
  tools/lib/bpf/libbpf.map |  3 ++-
  3 files changed, 22 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +0 -0
index 809fe209cdcc..284790d81c1b 100644
@@ -8457,6 +8457,14 @@ size_t bpf_program__insn_cnt(const struct bpf_program *prog)
      return prog->insns_cnt;
  }
 
Quoted 6 lines

tools/lib/bpf/libbpf.c size_t bpf_program__insn_cnt(const struct bpf_program *prog) · line 8465

Quoted 6 lines
tools/lib/bpf/libbpf.c +6 -0
+void bpf_program__set_insns(struct bpf_program *prog,
+                         struct bpf_insn *insns, size_t insns_cnt)
+{
+     free(prog->insns);
+     prog->insns = insns;
+     prog->insns_cnt = insns_cnt;

let's not store user-provided pointer here. Please realloc prog->insns as necessary and copy over insns into it.

Quoted 32 lines
Quoted 2 lines

Also let's at least add the check for prog->loaded and return -EBUSY in such a case. And of course this API should return int, not void.

ok, will change

Quoted 27 lines
Quoted 15 lines
Quoted 15 lines
tools/lib/bpf/libbpf.c +2 -0
+}
+
  int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
                        bpf_program_prep_t prep)
  {
tools/lib/bpf/libbpf.h +1 -0
index 05dde85e19a6..b31ad58d335f 100644
@@ -323,6 +323,18 @@ struct bpf_insn;
   * different.
   */
  LIBBPF_API const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog);
+
Quoted 8 lines

tools/lib/bpf/libbpf.h struct bpf_insn; · line 334

Quoted 8 lines
tools/lib/bpf/libbpf.h +8 -0
+/**
+ * @brief **bpf_program__set_insns()** can set BPF program's underlying
+ * BPF instructions.
+ * @param prog BPF program for which to return instructions
+ * @param insn a pointer to an array of BPF instructions
+ * @param insns_cnt number of `struct bpf_insn`'s that form
+ * specified BPF program
+ */

This API makes me want to cry... but I can't come up with anything better for perf's use case.

So thinking about this some more. If we make libbpf not to close maps and prog FDs on BPF program load failure automatically and instead doing it in bpf_object__close(), which would seem to be a totally fine semantics and won't break any reasonable application as they always have to call bpf_object__close() anyways to clean up all the resources; we wouldn't need this horror of bpf_program__set_insns(). Your BPF program would fail to load, but you'll get its fully prepared instructions with bpf_program__insns(), then you can just append correct preamble. Meanwhile, all the maps will be created (they are always created before the first program load), so all the FDs will be correct.

Quoted 4 lines

This is certainly advanced knowledge of libbpf behavior, but the use case you are trying to solve is also very unique and advanced (and I wouldn't recommend anyone trying to do this anyways). WDYT? Would that work?

Quoted 5 lines

hm, so verifier will fail after all maps are set up during the walk of the program instructions.. I guess that could work, I'll give it a try, should be easy change in libbpf (like below) and also in perf

but still the bpf_program__set_insns seems less horror to me

let's keep set_insns API then, but please do add a lot of warnings into the description to make it very-very scary :)

Quoted 29 lines

jirka

---

tools/lib/bpf/libbpf.c +0 -13
index c8df74e5f658..1eb75d4231ff 100644
@@ -7577,19 +7577,6 @@ static int bpf_object_load(struct bpf_object *obj, int extra_log_level, const ch
        obj->btf_vmlinux = NULL;
 
        obj->loaded = true; /* doesn't matter if successfully or not */
-
-       if (err)
-               goto out;
-
-       return 0;
-out:
-       /* unpin any maps that were auto-pinned during load */
-       for (i = 0; i < obj->nr_maps; i++)
-               if (obj->maps[i].pinned && !obj->maps[i].reused)
-                       bpf_map__unpin(&obj->maps[i], NULL);
-
-       bpf_object_unload(obj);
-       pr_warn("failed to load object '%s'\n", obj->path);
        return libbpf_err(err);
 }
 
author reviewer
#9 lore raw

PATCH 2/5 libbpf: Load prog's instructions after prog_prepare_load_fn callback

In reply to Jiri Olsa

We can have changed instructions after calling
prog_prepare_load_fn callback, reload them.

---
 tools/lib/bpf/libbpf.c | 2 ++
 1 file changed, 2 insertions(+)
tools/lib/bpf/libbpf.c +2 -0
index 284790d81c1b..c8df74e5f658 100644
@@ -6695,6 +6695,8 @@ static int bpf_object_load_prog_instance(struct bpf_object *obj, struct bpf_prog
 				prog->name, err);
 			return err;
 		}
+		insns = prog->insns;
+		insns_cnt = prog->insns_cnt;
 	}
 
 	if (obj->gen_loader) {
Signature · 1 line
2.35.1
author reviewer
#10 lore raw

PATCH 3/5 perf tools: Move libbpf init in libbpf_init function

In reply to Jiri Olsa

Moving the libbpf init code into single function,
so we have single place doing that.

---
 tools/perf/util/bpf-loader.c | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)
tools/perf/util/bpf-loader.c +18 -9
index b72cef1ae959..f8ad581ea247 100644
@@ -99,16 +99,26 @@ static int bpf_perf_object__add(struct bpf_object *obj)
 	return perf_obj ? 0 : -ENOMEM;
 }
 
+static int libbpf_init(void)
+{
+	if (libbpf_initialized)
+		return 0;
+
+	libbpf_set_print(libbpf_perf_print);
+	libbpf_initialized = true;
+	return 0;
+}
+
 struct bpf_object *
 bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz, const char *name)
 {
 	LIBBPF_OPTS(bpf_object_open_opts, opts, .object_name = name);
 	struct bpf_object *obj;
+	int err;
 
-	if (!libbpf_initialized) {
-		libbpf_set_print(libbpf_perf_print);
-		libbpf_initialized = true;
-	}
+	err = libbpf_init();
+	if (err)
+		return ERR_PTR(err);
 
 	obj = bpf_object__open_mem(obj_buf, obj_buf_sz, &opts);
 	if (IS_ERR_OR_NULL(obj)) {
@@ -135,14 +145,13 @@ struct bpf_object *bpf__prepare_load(const char *filename, bool source)
 {
 	LIBBPF_OPTS(bpf_object_open_opts, opts, .object_name = filename);
 	struct bpf_object *obj;
+	int err;
 
-	if (!libbpf_initialized) {
-		libbpf_set_print(libbpf_perf_print);
-		libbpf_initialized = true;
-	}
+	err = libbpf_init();
+	if (err)
+		return ERR_PTR(err);
 
 	if (source) {
-		int err;
 		void *obj_buf;
 		size_t obj_buf_sz;
 
Signature · 1 line
2.35.1
#11 lore raw

Re: [PATCH perf/core 3/5] perf tools: Move libbpf init in libbpf_init function

Em Fri, Apr 22, 2022 at 12:00:23PM +0200, Jiri Olsa escreveu:

Quoted 2 lines

Moving the libbpf init code into single function,
so we have single place doing that.

Cherry picked this one, waiting for Andrii to chime in wrt the libbpf changes, if its acceptable, how to proceed, i.e. in what tree to carry these?

- Arnaldo

Quoted 61 lines
---
 tools/perf/util/bpf-loader.c | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)
tools/perf/util/bpf-loader.c +18 -9
index b72cef1ae959..f8ad581ea247 100644
@@ -99,16 +99,26 @@ static int bpf_perf_object__add(struct bpf_object *obj)
 	return perf_obj ? 0 : -ENOMEM;
 }
 
+static int libbpf_init(void)
+{
+	if (libbpf_initialized)
+		return 0;
+
+	libbpf_set_print(libbpf_perf_print);
+	libbpf_initialized = true;
+	return 0;
+}
+
 struct bpf_object *
 bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz, const char *name)
 {
 	LIBBPF_OPTS(bpf_object_open_opts, opts, .object_name = name);
 	struct bpf_object *obj;
+	int err;
 
-	if (!libbpf_initialized) {
-		libbpf_set_print(libbpf_perf_print);
-		libbpf_initialized = true;
-	}
+	err = libbpf_init();
+	if (err)
+		return ERR_PTR(err);
 
 	obj = bpf_object__open_mem(obj_buf, obj_buf_sz, &opts);
 	if (IS_ERR_OR_NULL(obj)) {
@@ -135,14 +145,13 @@ struct bpf_object *bpf__prepare_load(const char *filename, bool source)
 {
 	LIBBPF_OPTS(bpf_object_open_opts, opts, .object_name = filename);
 	struct bpf_object *obj;
+	int err;
 
-	if (!libbpf_initialized) {
-		libbpf_set_print(libbpf_perf_print);
-		libbpf_initialized = true;
-	}
+	err = libbpf_init();
+	if (err)
+		return ERR_PTR(err);
 
 	if (source) {
-		int err;
 		void *obj_buf;
 		size_t obj_buf_sz;
 
Signature · 1 line
2.35.1
Signature · 1 line
- Arnaldo
#12 lore raw

Re: [PATCH perf/core 3/5] perf tools: Move libbpf init in libbpf_init function

Quoted 7 lines · Arnaldo Carvalho de Melo

Em Fri, Apr 22, 2022 at 12:00:23PM +0200, Jiri Olsa escreveu:

Quoted 2 lines

Moving the libbpf init code into single function,
so we have single place doing that.

Cherry picked this one, waiting for Andrii to chime in wrt the libbpf changes, if its acceptable, how to proceed, i.e. in what tree to carry these?

I think at this point it's ok with either yours perf/core or bpf-next/master, I waited for them to be in sync for this ;-)

but as you pointed out there's issue with perf linked with dynamic libbpf, because the current version does not have the libbpf_register_prog_handler api that perf needs now.. also it needs the fix and api introduced in this patchset

I'll check and perhaps we can temporirly disable perf/bpf prologue generation for dynamic linking..? until the libbpf release has all the needed changes

jirka

Quoted 68 lines

- Arnaldo

Quoted 61 lines
---
 tools/perf/util/bpf-loader.c | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)
tools/perf/util/bpf-loader.c +18 -9
index b72cef1ae959..f8ad581ea247 100644
@@ -99,16 +99,26 @@ static int bpf_perf_object__add(struct bpf_object *obj)
 	return perf_obj ? 0 : -ENOMEM;
 }
 
+static int libbpf_init(void)
+{
+	if (libbpf_initialized)
+		return 0;
+
+	libbpf_set_print(libbpf_perf_print);
+	libbpf_initialized = true;
+	return 0;
+}
+
 struct bpf_object *
 bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz, const char *name)
 {
 	LIBBPF_OPTS(bpf_object_open_opts, opts, .object_name = name);
 	struct bpf_object *obj;
+	int err;
 
-	if (!libbpf_initialized) {
-		libbpf_set_print(libbpf_perf_print);
-		libbpf_initialized = true;
-	}
+	err = libbpf_init();
+	if (err)
+		return ERR_PTR(err);
 
 	obj = bpf_object__open_mem(obj_buf, obj_buf_sz, &opts);
 	if (IS_ERR_OR_NULL(obj)) {
@@ -135,14 +145,13 @@ struct bpf_object *bpf__prepare_load(const char *filename, bool source)
 {
 	LIBBPF_OPTS(bpf_object_open_opts, opts, .object_name = filename);
 	struct bpf_object *obj;
+	int err;
 
-	if (!libbpf_initialized) {
-		libbpf_set_print(libbpf_perf_print);
-		libbpf_initialized = true;
-	}
+	err = libbpf_init();
+	if (err)
+		return ERR_PTR(err);
 
 	if (source) {
-		int err;
 		void *obj_buf;
 		size_t obj_buf_sz;
 
Signature · 1 line
2.35.1
Signature · 1 line
- Arnaldo
#13 lore raw

Re: [PATCH perf/core 3/5] perf tools: Move libbpf init in libbpf_init function

Quoted 6 lines · Jiri Olsa
Quoted 4 lines · Arnaldo Carvalho de Melo

Em Fri, Apr 22, 2022 at 12:00:23PM +0200, Jiri Olsa escreveu:

Quoted 2 lines

Moving the libbpf init code into single function,
so we have single place doing that.

Quoted 6 lines
Quoted 3 lines

Cherry picked this one, waiting for Andrii to chime in wrt the libbpf changes, if its acceptable, how to proceed, i.e. in what tree to carry these?

I think at this point it's ok with either yours perf/core or bpf-next/master, I waited for them to be in sync for this ;-)

I'd rather have all the libbpf code stay in bpf-next, otherwise Github sync process becomes very hairy. BTW, I think libbpf v0.8 release is pretty close, I plan to add few small features before cutting it, but that should be done pretty soon

Quoted 19 lines

but as you pointed out there's issue with perf linked with dynamic libbpf, because the current version does not have the libbpf_register_prog_handler api that perf needs now.. also it needs the fix and api introduced in this patchset

I'll check and perhaps we can temporirly disable perf/bpf prologue generation for dynamic linking..? until the libbpf release has all the needed changes

jirka

Quoted 8 lines

- Arnaldo

Quoted 5 lines
---
 tools/perf/util/bpf-loader.c | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)

[...]

author reviewer
#14 lore raw

PATCH 4/5 perf tools: Register perfkprobe libbpf section handler

In reply to Jiri Olsa

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 support for 'perfkprobe/' section name handler to take care of perf kprobe programs.

The handler servers 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.

---
 tools/perf/util/bpf-loader.c | 50 ++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
tools/perf/util/bpf-loader.c +50 -0
index f8ad581ea247..92dd8cc18edb 100644
@@ -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,61 @@ 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_0, 0),
+	BPF_MOV64_IMM(BPF_REG_1, 0),
+	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),
+};
+
+#define LIBBPF_SEC_PREFIX "perfkprobe/"
+
+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(LIBBPF_SEC_PREFIX,
+							  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;
 }
Signature · 1 line
2.35.1
#15 lore raw

Re: [PATCH perf/core 4/5] perf tools: Register perfkprobe libbpf section handler

Quoted 38 lines · Jiri Olsa

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 support for 'perfkprobe/' section name handler to take care of perf kprobe programs.

The handler servers 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.

---
 tools/perf/util/bpf-loader.c | 50 ++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
tools/perf/util/bpf-loader.c +1 -0
index f8ad581ea247..92dd8cc18edb 100644
@@ -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,61 @@ static int bpf_perf_object__add(struct bpf_object *obj)
        return perf_obj ? 0 : -ENOMEM;
 }
 

tools/perf/util/bpf-loader.c static int bpf_perf_object__add(struct bpf_object *obj) · line 112

Quoted 10 lines
tools/perf/util/bpf-loader.c +10 -0
+static struct bpf_insn prologue_init_insn[] = {
+       BPF_MOV64_IMM(BPF_REG_0, 0),
+       BPF_MOV64_IMM(BPF_REG_1, 0),
+       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),
+};
+
+#define LIBBPF_SEC_PREFIX "perfkprobe/"

libbpf allows to register fallback handler that will handle any SEC() definition besides the ones that libbpf handles. Would that work in this case instead of adding a custom prefix handler here? See prog_tests/custom_sec_handlers.c for example:

fallback_id = libbpf_register_prog_handler(NULL,
BPF_PROG_TYPE_SYSCALL, 0, &opts);

Quoted 1 line
tools/perf/util/bpf-loader.c +1 -0
+

[...]

#16 lore raw

Re: [PATCH perf/core 4/5] perf tools: Register perfkprobe libbpf section handler

Quoted 55 lines · Andrii Nakryiko
Quoted 38 lines · Jiri Olsa

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 support for 'perfkprobe/' section name handler to take care of perf kprobe programs.

The handler servers 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.

---
 tools/perf/util/bpf-loader.c | 50 ++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
tools/perf/util/bpf-loader.c +1 -0
index f8ad581ea247..92dd8cc18edb 100644
@@ -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,61 @@ static int bpf_perf_object__add(struct bpf_object *obj)
        return perf_obj ? 0 : -ENOMEM;
 }
 

tools/perf/util/bpf-loader.c static int bpf_perf_object__add(struct bpf_object *obj) · line 112

Quoted 10 lines
tools/perf/util/bpf-loader.c +10 -0
+static struct bpf_insn prologue_init_insn[] = {
+       BPF_MOV64_IMM(BPF_REG_0, 0),
+       BPF_MOV64_IMM(BPF_REG_1, 0),
+       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),
+};
+
+#define LIBBPF_SEC_PREFIX "perfkprobe/"

libbpf allows to register fallback handler that will handle any SEC() definition besides the ones that libbpf handles. Would that work in this case instead of adding a custom prefix handler here? See prog_tests/custom_sec_handlers.c for example:

Quoted 2 lines

fallback_id = libbpf_register_prog_handler(NULL,
BPF_PROG_TYPE_SYSCALL, 0, &opts);

nice, I did not see that.. that should be better, no need for the prefix

thanks,
jirka

author reviewer
#17 lore raw

PATCH 5/5 perf tools: Rework prologue generation code

In reply to Jiri Olsa

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

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

We workaround this 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 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 'perfkprobe/' 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 ;-)

---
 tools/perf/include/bpf/bpf.h                |   2 +-
 tools/perf/tests/bpf-script-example.c       |   2 +-
 tools/perf/tests/bpf-script-test-prologue.c |   2 +-
 tools/perf/util/bpf-loader.c                | 136 +++++++++++++++++---
 4 files changed, 120 insertions(+), 22 deletions(-)
tools/perf/include/bpf/bpf.h +1 -1
index b422aeef5339..91869f6fb672 100644
@@ -50,7 +50,7 @@ static void (*bpf_tail_call)(void *ctx, void *map, int index) = (void *)BPF_FUNC
 #define SEC(NAME) __attribute__((section(NAME),  used))
 
 #define probe(function, vars) \
-	SEC(#function "=" #function " " #vars) function
+	SEC("perfkprobe/" #function "=" #function " " #vars) function
 
 #define syscall_enter(name) \
 	SEC("syscalls:sys_enter_" #name) syscall_enter_ ## name
tools/perf/tests/bpf-script-example.c +1 -1
index ab4b98b3165d..56673fa1f30d 100644
@@ -32,7 +32,7 @@ struct bpf_map_def SEC("maps") flip_table = {
 	.max_entries = 1,
 };
 
-SEC("func=do_epoll_wait")
+SEC("perfkprobe/func=do_epoll_wait")
 int bpf_func__SyS_epoll_pwait(void *ctx)
 {
 	int ind =0;
tools/perf/tests/bpf-script-test-prologue.c +1 -1
index bd83d364cf30..00dac5a23938 100644
@@ -26,7 +26,7 @@
 static void (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) =
 	(void *) 6;
 
-SEC("func=null_lseek file->f_mode offset orig")
+SEC("perfkprobe/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)
 {
tools/perf/util/bpf-loader.c +117 -19

225 lines not on this page · raw message

Signature · 1 line
2.35.1