Skip to contentSkip to conversation

perf/bpf: Remove prologue generation

PATCH In review Jiri Olsa 4 participants 15 messages Jan 23, 2022 – Feb 14, 2022

Subsystems (2)
lore
Share & tools
lore

Jiri Olsa

author
#0 lore raw

PATCH 1/3 perf/bpf: Remove prologue generation

Removing code for ebpf program prologue generation.

The prologue code was used to get data for extra arguments specified in program section name, like:

  SEC("lock_page=__lock_page page->flags")
  int lock_page(struct pt_regs *ctx, int err, unsigned long flags)
  {
         return 1;
  }

This code is using deprecated libbpf API and blocks its removal.

This feature was not documented and broken for some time without anyone complaining, also original authors are not responding, so I'm removing it.

---
 tools/perf/Makefile.config     |  11 -
 tools/perf/builtin-record.c    |  14 -
 tools/perf/util/bpf-loader.c   | 242 +---------------
 tools/perf/util/bpf-prologue.c | 508 ---------------------------------
 tools/perf/util/bpf-prologue.h |  37 ---
 5 files changed, 1 insertion(+), 811 deletions(-)
 delete mode 100644 tools/perf/util/bpf-prologue.c
 delete mode 100644 tools/perf/util/bpf-prologue.h
tools/perf/Makefile.config +0 -11
index 96ad944ca6a8..d9ff537d999e 100644
@@ -556,17 +556,6 @@ ifndef NO_LIBELF
       endif
     endif
 
-    ifndef NO_DWARF
-      ifdef PERF_HAVE_ARCH_REGS_QUERY_REGISTER_OFFSET
-        CFLAGS += -DHAVE_BPF_PROLOGUE
-        $(call detected,CONFIG_BPF_PROLOGUE)
-      else
-        msg := $(warning BPF prologue is not supported by architecture $(SRCARCH), missing regs_query_register_offset());
-      endif
-    else
-      msg := $(warning DWARF support is off, BPF prologue is disabled);
-    endif
-
   endif # NO_LIBBPF
 endif # NO_LIBELF
 
tools/perf/builtin-record.c +0 -14
index bb716c953d02..1a8111fdff2e 100644
@@ -2696,20 +2696,6 @@ int cmd_record(int argc, const char **argv)
 	set_nobuild('\0', "clang-path", true);
 	set_nobuild('\0', "clang-opt", true);
 # undef set_nobuild
-#endif
-
-#ifndef HAVE_BPF_PROLOGUE
-# if !defined (HAVE_DWARF_SUPPORT)
-#  define REASON  "NO_DWARF=1"
-# elif !defined (HAVE_LIBBPF_SUPPORT)
-#  define REASON  "NO_LIBBPF=1"
-# else
-#  define REASON  "this architecture doesn't support BPF prologue"
-# endif
-# define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, REASON, c)
-	set_nobuild('\0', "vmlinux", true);
-# undef set_nobuild
-# undef REASON
 #endif
 
 	rec->opts.affinity = PERF_AFFINITY_SYS;
tools/perf/util/bpf-loader.c +1 -241

278 lines not on this page · raw message

tools/perf/util/bpf-prologue.c deleted +0 -508

508 lines not on this page · raw message

tools/perf/util/bpf-prologue.h deleted +0 -37
deleted file mode 100644
index c50c7358009f..000000000000
@@ -1,37 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0 */
-/*
- * Copyright (C) 2015, He Kuang <[email protected]>
- * Copyright (C) 2015, Huawei Inc.
- */
-#ifndef __BPF_PROLOGUE_H
-#define __BPF_PROLOGUE_H
-
-#include <linux/compiler.h>
-#include <linux/filter.h>
-#include "probe-event.h"
-
-#define BPF_PROLOGUE_MAX_ARGS 3
-#define BPF_PROLOGUE_START_ARG_REG BPF_REG_3
-#define BPF_PROLOGUE_FETCH_RESULT_REG BPF_REG_2
-
-#ifdef HAVE_BPF_PROLOGUE
-int bpf__gen_prologue(struct probe_trace_arg *args, int nargs,
-		      struct bpf_insn *new_prog, size_t *new_cnt,
-		      size_t cnt_space);
-#else
-#include <errno.h>
-
-static inline int
-bpf__gen_prologue(struct probe_trace_arg *args __maybe_unused,
-		  int nargs __maybe_unused,
-		  struct bpf_insn *new_prog __maybe_unused,
-		  size_t *new_cnt,
-		  size_t cnt_space __maybe_unused)
-{
-	if (!new_cnt)
-		return -EINVAL;
-	*new_cnt = 0;
-	return -ENOTSUP;
-}
-#endif
-#endif /* __BPF_PROLOGUE_H */
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 2/3 perf/bpf: Remove special bpf config support

With bpf program prologue code removed, we can also remove the program section parsing, because it is no longer used.

---
 tools/perf/util/bpf-loader.c | 177 ++---------------------------------
 1 file changed, 8 insertions(+), 169 deletions(-)
tools/perf/util/bpf-loader.c +8 -169
index f9f329a48892..f7682eb7009e 100644
@@ -129,179 +129,18 @@ clear_prog_priv(struct bpf_program *prog __maybe_unused,
 }
 
 static int
-prog_config__exec(const char *value, struct perf_probe_event *pev)
-{
-	pev->uprobes = true;
-	pev->target = strdup(value);
-	if (!pev->target)
-		return -ENOMEM;
-	return 0;
-}
-
-static int
-prog_config__module(const char *value, struct perf_probe_event *pev)
-{
-	pev->uprobes = false;
-	pev->target = strdup(value);
-	if (!pev->target)
-		return -ENOMEM;
-	return 0;
-}
-
-static int
-prog_config__bool(const char *value, bool *pbool, bool invert)
-{
-	int err;
-	bool bool_value;
-
-	if (!pbool)
-		return -EINVAL;
-
-	err = strtobool(value, &bool_value);
-	if (err)
-		return err;
-
-	*pbool = invert ? !bool_value : bool_value;
-	return 0;
-}
-
-static int
-prog_config__inlines(const char *value,
-		     struct perf_probe_event *pev __maybe_unused)
-{
-	return prog_config__bool(value, &probe_conf.no_inlines, true);
-}
-
-static int
-prog_config__force(const char *value,
-		   struct perf_probe_event *pev __maybe_unused)
-{
-	return prog_config__bool(value, &probe_conf.force_add, false);
-}
-
-static struct {
-	const char *key;
-	const char *usage;
-	const char *desc;
-	int (*func)(const char *, struct perf_probe_event *);
-} bpf_prog_config_terms[] = {
-	{
-		.key	= "exec",
-		.usage	= "exec=<full path of file>",
-		.desc	= "Set uprobe target",
-		.func	= prog_config__exec,
-	},
-	{
-		.key	= "module",
-		.usage	= "module=<module name>    ",
-		.desc	= "Set kprobe module",
-		.func	= prog_config__module,
-	},
-	{
-		.key	= "inlines",
-		.usage	= "inlines=[yes|no]        ",
-		.desc	= "Probe at inline symbol",
-		.func	= prog_config__inlines,
-	},
-	{
-		.key	= "force",
-		.usage	= "force=[yes|no]          ",
-		.desc	= "Forcibly add events with existing name",
-		.func	= prog_config__force,
-	},
-};
-
-static int
-do_prog_config(const char *key, const char *value,
-	       struct perf_probe_event *pev)
-{
-	unsigned int i;
-
-	pr_debug("config bpf program: %s=%s\n", key, value);
-	for (i = 0; i < ARRAY_SIZE(bpf_prog_config_terms); i++)
-		if (strcmp(key, bpf_prog_config_terms[i].key) == 0)
-			return bpf_prog_config_terms[i].func(value, pev);
-
-	pr_debug("BPF: ERROR: invalid program config option: %s=%s\n",
-		 key, value);
-
-	pr_debug("\nHint: Valid options are:\n");
-	for (i = 0; i < ARRAY_SIZE(bpf_prog_config_terms); i++)
-		pr_debug("\t%s:\t%s\n", bpf_prog_config_terms[i].usage,
-			 bpf_prog_config_terms[i].desc);
-	pr_debug("\n");
-
-	return -BPF_LOADER_ERRNO__PROGCONF_TERM;
-}
-
-static const char *
-parse_prog_config_kvpair(const char *config_str, struct perf_probe_event *pev)
-{
-	char *text = strdup(config_str);
-	char *sep, *line;
-	const char *main_str = NULL;
-	int err = 0;
-
-	if (!text) {
-		pr_debug("Not enough memory: dup config_str failed\n");
-		return ERR_PTR(-ENOMEM);
-	}
-
-	line = text;
-	while ((sep = strchr(line, ';'))) {
-		char *equ;
-
-		*sep = '\0';
-		equ = strchr(line, '=');
-		if (!equ) {
-			pr_warning("WARNING: invalid config in BPF object: %s\n",
-				   line);
-			pr_warning("\tShould be 'key=value'.\n");
-			goto nextline;
-		}
-		*equ = '\0';
-
-		err = do_prog_config(line, equ + 1, pev);
-		if (err)
-			break;
-nextline:
-		line = sep + 1;
-	}
-
-	if (!err)
-		main_str = config_str + (line - text);
-	free(text);
-
-	return err ? ERR_PTR(err) : main_str;
-}
-
-static int
-parse_prog_config(const char *config_str, const char **p_main_str,
-		  bool *is_tp, struct perf_probe_event *pev)
+parse_prog_config(const char *config_str, bool *is_tp,
+		  struct perf_probe_event *pev)
 {
 	int err;
-	const char *main_str = parse_prog_config_kvpair(config_str, pev);
-
-	if (IS_ERR(main_str))
-		return PTR_ERR(main_str);
-
-	*p_main_str = main_str;
-	if (!strchr(main_str, '=')) {
-		/* Is a tracepoint event? */
-		const char *s = strchr(main_str, ':');
-
-		if (!s) {
-			pr_debug("bpf: '%s' is not a valid tracepoint\n",
-				 config_str);
-			return -BPF_LOADER_ERRNO__CONFIG;
-		}
 
+	if (strchr(config_str, ':')) {
 		*is_tp = true;
 		return 0;
 	}
 
 	*is_tp = false;
-	err = parse_perf_probe_command(main_str, pev);
+	err = parse_perf_probe_command(config_str, pev);
 	if (err < 0) {
 		pr_debug("bpf: '%s' is not a valid config string\n",
 			 config_str);
@@ -316,7 +155,7 @@ config_bpf_program(struct bpf_program *prog)
 {
 	struct perf_probe_event *pev = NULL;
 	struct bpf_prog_priv *priv = NULL;
-	const char *config_str, *main_str;
+	const char *config_str;
 	bool is_tp = false;
 	int err;
 
@@ -333,15 +172,15 @@ config_bpf_program(struct bpf_program *prog)
 
 	config_str = bpf_program__section_name(prog);
 	pr_debug("bpf: config program '%s'\n", config_str);
-	err = parse_prog_config(config_str, &main_str, &is_tp, pev);
+	err = parse_prog_config(config_str, &is_tp, pev);
 	if (err)
 		goto errout;
 
 	if (is_tp) {
-		char *s = strchr(main_str, ':');
+		char *s = strchr(config_str, ':');
 
 		priv->is_tp = true;
-		priv->sys_name = strndup(main_str, s - main_str);
+		priv->sys_name = strndup(config_str, s - config_str);
 		priv->evt_name = strdup(s + 1);
 		goto set_priv;
 	}
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH 3/3 perf tests: Remove bpf prologue generation test

In reply to Jiri Olsa

Removing bpf prologue generation test, because its support was removed in previous patches.

---
 tools/perf/tests/Build  |  9 +-------
 tools/perf/tests/bpf.c  | 51 -----------------------------------------
 tools/perf/tests/llvm.c | 19 ---------------
 tools/perf/tests/llvm.h |  2 --
 4 files changed, 1 insertion(+), 80 deletions(-)
tools/perf/tests/Build +1 -8
index af2b37ef7c70..10a5287dbc94 100644
@@ -36,7 +36,7 @@ perf-y += sample-parsing.o
 perf-y += parse-no-sample-id-all.o
 perf-y += kmod-path.o
 perf-y += thread-map.o
-perf-y += llvm.o llvm-src-base.o llvm-src-kbuild.o llvm-src-prologue.o llvm-src-relocation.o
+perf-y += llvm.o llvm-src-base.o llvm-src-kbuild.o llvm-src-relocation.o
 perf-y += bpf.o
 perf-y += topology.o
 perf-y += mem.o
@@ -81,13 +81,6 @@ $(OUTPUT)tests/llvm-src-kbuild.c: tests/bpf-script-test-kbuild.c tests/Build
 	$(Q)sed -e 's/"/\\"/g' -e 's/\(.*\)/"\1\\n"/g' $< >> $@
 	$(Q)echo ';' >> $@
 
-$(OUTPUT)tests/llvm-src-prologue.c: tests/bpf-script-test-prologue.c tests/Build
-	$(call rule_mkdir)
-	$(Q)echo '#include <tests/llvm.h>' > $@
-	$(Q)echo 'const char test_llvm__bpf_test_prologue_prog[] =' >> $@
-	$(Q)sed -e 's/"/\\"/g' -e 's/\(.*\)/"\1\\n"/g' $< >> $@
-	$(Q)echo ';' >> $@
-
 $(OUTPUT)tests/llvm-src-relocation.c: tests/bpf-script-test-relocation.c tests/Build
 	$(call rule_mkdir)
 	$(Q)echo '#include <tests/llvm.h>' > $@
tools/perf/tests/bpf.c +0 -51

76 lines not on this page · raw message

tools/perf/tests/llvm.c +0 -19
index 8ac0a3a457ef..b57f24185dda 100644
@@ -33,10 +33,6 @@ static struct {
 		.source = test_llvm__bpf_test_kbuild_prog,
 		.desc = "kbuild searching",
 	},
-	[LLVM_TESTCASE_BPF_PROLOGUE] = {
-		.source = test_llvm__bpf_test_prologue_prog,
-		.desc = "Compile source for BPF prologue generation",
-	},
 	[LLVM_TESTCASE_BPF_RELOCATION] = {
 		.source = test_llvm__bpf_test_relocation,
 		.desc = "Compile source for BPF relocation",
@@ -172,17 +168,6 @@ static int test__llvm__bpf_test_kbuild_prog(struct test_suite *test __maybe_unus
 #endif
 }
 
-static int test__llvm__bpf_test_prologue_prog(struct test_suite *test __maybe_unused,
-					      int subtest __maybe_unused)
-{
-#ifdef HAVE_LIBBPF_SUPPORT
-	return test__llvm(LLVM_TESTCASE_BPF_PROLOGUE);
-#else
-	pr_debug("Skip LLVM test because BPF support is not compiled\n");
-	return TEST_SKIP;
-#endif
-}
-
 static int test__llvm__bpf_test_relocation(struct test_suite *test __maybe_unused,
 					   int subtest __maybe_unused)
 {
@@ -199,14 +184,10 @@ static struct test_case llvm_tests[] = {
 #ifdef HAVE_LIBBPF_SUPPORT
 	TEST_CASE("Basic BPF llvm compile", llvm__bpf_base_prog),
 	TEST_CASE("kbuild searching", llvm__bpf_test_kbuild_prog),
-	TEST_CASE("Compile source for BPF prologue generation",
-		  llvm__bpf_test_prologue_prog),
 	TEST_CASE("Compile source for BPF relocation", llvm__bpf_test_relocation),
 #else
 	TEST_CASE_REASON("Basic BPF llvm compile", llvm__bpf_base_prog, "not compiled in"),
 	TEST_CASE_REASON("kbuild searching", llvm__bpf_test_kbuild_prog, "not compiled in"),
-	TEST_CASE_REASON("Compile source for BPF prologue generation",
-			llvm__bpf_test_prologue_prog, "not compiled in"),
 	TEST_CASE_REASON("Compile source for BPF relocation",
 			llvm__bpf_test_relocation, "not compiled in"),
 #endif
tools/perf/tests/llvm.h +0 -2
index f68b0d9b8ae2..8e1c4352b1cc 100644
@@ -11,13 +11,11 @@ extern "C" {
 
 extern const char test_llvm__bpf_base_prog[];
 extern const char test_llvm__bpf_test_kbuild_prog[];
-extern const char test_llvm__bpf_test_prologue_prog[];
 extern const char test_llvm__bpf_test_relocation[];
 
 enum test_llvm__testcase {
 	LLVM_TESTCASE_BASE,
 	LLVM_TESTCASE_KBUILD,
-	LLVM_TESTCASE_BPF_PROLOGUE,
 	LLVM_TESTCASE_BPF_RELOCATION,
 	__LLVM_TESTCASE_MAX,
 };
Signature · 1 line
2.34.1
#3 lore raw
In reply to Jiri Olsa
Quoted 18 lines · Jiri Olsa

Removing code for ebpf program prologue generation.

The prologue code was used to get data for extra arguments specified in program section name, like:

  SEC("lock_page=__lock_page page->flags")
  int lock_page(struct pt_regs *ctx, int err, unsigned long flags)
  {
         return 1;
  }

This code is using deprecated libbpf API and blocks its removal.

This feature was not documented and broken for some time without anyone complaining, also original authors are not responding, so I'm removing it.

Quoted 8 lines
---
 tools/perf/Makefile.config     |  11 -
 tools/perf/builtin-record.c    |  14 -
 tools/perf/util/bpf-loader.c   | 242 +---------------
 tools/perf/util/bpf-prologue.c | 508 ---------------------------------
 tools/perf/util/bpf-prologue.h |  37 ---
 5 files changed, 1 insertion(+), 811 deletions(-)

Love the stats! Thanks for taking this on!

Quoted 3 lines
 delete mode 100644 tools/perf/util/bpf-prologue.c
 delete mode 100644 tools/perf/util/bpf-prologue.h

[...]

#4 lore raw
Quoted 29 lines · Andrii Nakryiko
Quoted 18 lines · Jiri Olsa

Removing code for ebpf program prologue generation.

The prologue code was used to get data for extra arguments specified in program section name, like:

  SEC("lock_page=__lock_page page->flags")
  int lock_page(struct pt_regs *ctx, int err, unsigned long flags)
  {
         return 1;
  }

This code is using deprecated libbpf API and blocks its removal.

This feature was not documented and broken for some time without anyone complaining, also original authors are not responding, so I'm removing it.

Quoted 8 lines
---
 tools/perf/Makefile.config     |  11 -
 tools/perf/builtin-record.c    |  14 -
 tools/perf/util/bpf-loader.c   | 242 +---------------
 tools/perf/util/bpf-prologue.c | 508 ---------------------------------
 tools/perf/util/bpf-prologue.h |  37 ---
 5 files changed, 1 insertion(+), 811 deletions(-)
Quoted 2 lines

Love the stats! Thanks for taking this on!

Hi,

Was this ever applied? If not, are there any blockers? I assume this will go through the perf tree, right?

Quoted 5 lines
Quoted 3 lines
 delete mode 100644 tools/perf/util/bpf-prologue.c
 delete mode 100644 tools/perf/util/bpf-prologue.h

[...]

#5 lore raw

Em Tue, Feb 01, 2022 at 05:01:38PM -0800, Andrii Nakryiko escreveu:

Quoted 31 lines
Quoted 29 lines · Andrii Nakryiko
Quoted 26 lines · Jiri Olsa

Removing code for ebpf program prologue generation.

The prologue code was used to get data for extra arguments specified in program section name, like:

  SEC("lock_page=__lock_page page->flags")
  int lock_page(struct pt_regs *ctx, int err, unsigned long flags)
  {
         return 1;
  }

This code is using deprecated libbpf API and blocks its removal.

This feature was not documented and broken for some time without anyone complaining, also original authors are not responding, so I'm removing it.

---
 tools/perf/Makefile.config     |  11 -
 tools/perf/builtin-record.c    |  14 -
 tools/perf/util/bpf-loader.c   | 242 +---------------
 tools/perf/util/bpf-prologue.c | 508 ---------------------------------
 tools/perf/util/bpf-prologue.h |  37 ---
 5 files changed, 1 insertion(+), 811 deletions(-)
Quoted 7 lines
Quoted 2 lines

Love the stats! Thanks for taking this on!

Hi,

Was this ever applied? If not, are there any blockers? I assume this will go through the perf tree, right?

I'll go thru it today.

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
 delete mode 100644 tools/perf/util/bpf-prologue.c
 delete mode 100644 tools/perf/util/bpf-prologue.h

[...]

Signature · 1 line
- Arnaldo
#6 lore raw
Quoted 36 lines · Arnaldo Carvalho de Melo

Em Tue, Feb 01, 2022 at 05:01:38PM -0800, Andrii Nakryiko escreveu:

Quoted 34 lines
Quoted 31 lines · Andrii Nakryiko
Quoted 18 lines · Jiri Olsa

Removing code for ebpf program prologue generation.

The prologue code was used to get data for extra arguments specified in program section name, like:

  SEC("lock_page=__lock_page page->flags")
  int lock_page(struct pt_regs *ctx, int err, unsigned long flags)
  {
         return 1;
  }

This code is using deprecated libbpf API and blocks its removal.

This feature was not documented and broken for some time without anyone complaining, also original authors are not responding, so I'm removing it.

Quoted 8 lines
---
 tools/perf/Makefile.config     |  11 -
 tools/perf/builtin-record.c    |  14 -
 tools/perf/util/bpf-loader.c   | 242 +---------------
 tools/perf/util/bpf-prologue.c | 508 ---------------------------------
 tools/perf/util/bpf-prologue.h |  37 ---
 5 files changed, 1 insertion(+), 811 deletions(-)

Love the stats! Thanks for taking this on!

Quoted 6 lines
Quoted 4 lines

Hi,

Was this ever applied? If not, are there any blockers? I assume this will go through the perf tree, right?

I'll go thru it today.

Great, thank you!

Quoted 10 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
 delete mode 100644 tools/perf/util/bpf-prologue.c
 delete mode 100644 tools/perf/util/bpf-prologue.h

[...]

--

- Arnaldo

#7 lore raw
In reply to Jiri Olsa

Em Sun, Jan 23, 2022 at 11:19:30PM +0100, Jiri Olsa escreveu:

Quoted 11 lines

Removing code for ebpf program prologue generation.

The prologue code was used to get data for extra arguments specified in program section name, like:

  SEC("lock_page=__lock_page page->flags")
  int lock_page(struct pt_regs *ctx, int err, unsigned long flags)
  {
         return 1;
  }
Quoted 5 lines

This code is using deprecated libbpf API and blocks its removal.

This feature was not documented and broken for some time without anyone complaining, also original authors are not responding, so I'm removing it.

So, the example below breaks, how hard would be to move the deprecated APIs to perf like was done in some other cases?

- Arnaldo

Before:

[root@quaco perf]# cat tools/perf/examples/bpf/5sec.c // SPDX-License-Identifier: GPL-2.0
/*
Description:

. Disable strace like syscall tracing (--no-syscalls), or try tracing
just some (-e *sleep).

. Attach a filter function to a kernel function, returning when it should
be considered, i.e. appear on the output.

. Run it system wide, so that any sleep of >= 5 seconds and < than 6
seconds gets caught.

. Ask for callgraphs using DWARF info, so that userspace can be unwound

. While this is running, run something like "sleep 5s".

. If we decide to add tv_nsec as well, then it becomes:

int probe(hrtimer_nanosleep, rqtp->tv_sec rqtp->tv_nsec)(void *ctx, int err, long sec, long nsec)

I.e. add where it comes from (rqtp->tv_nsec) and where it will be accessible in the function body (nsec)

# perf trace --no-syscalls -e tools/perf/examples/bpf/5sec.c/call-graph=dwarf/
0.000 perf_bpf_probe:func:(ffffffff9811b5f0) tv_sec=5
hrtimer_nanosleep ([kernel.kallsyms]) __x64_sys_nanosleep ([kernel.kallsyms]) do_syscall_64 ([kernel.kallsyms]) entry_SYSCALL_64 ([kernel.kallsyms]) __GI___nanosleep (/usr/lib64/libc-2.26.so) rpl_nanosleep (/usr/bin/sleep) xnanosleep (/usr/bin/sleep) main (/usr/bin/sleep) __libc_start_main (/usr/lib64/libc-2.26.so) _start (/usr/bin/sleep)
^C#

Copyright (C) 2018 Red Hat, Inc., Arnaldo Carvalho de Melo <[email protected]>
*/

#include <bpf.h>

#define NSEC_PER_SEC 1000000000L

int probe(hrtimer_nanosleep, rqtp)(void *ctx, int err, long long sec)

{
	return sec / NSEC_PER_SEC == 5ULL;
}

license(GPL);
[root@quaco perf]# perf trace -e tools/perf/examples/bpf/5sec.c sleep 5s

     0.000 perf_bpf_probe:hrtimer_nanosleep(__probe_ip: -1994947936, rqtp: 5000000000)

[root@quaco perf]#

After:

[root@quaco perf]# perf trace -e tools/perf/examples/bpf/5sec.c sleep 5s event syntax error: 'tools/perf/examples/bpf/5sec.c'
\___ Permission denied

(add -v to see detail)
Run 'perf list' for a list of valid events

Usage: perf trace [<options>] [<command>]
or: perf trace [<options>] -- <command> [<options>]
or: perf trace record [<options>] [<command>]
or: perf trace record [<options>] -- <command> [<options>]

-e, --event <event> event/syscall selector. use 'perf list' to list available events
[root@quaco perf]# perf trace -v -e tools/perf/examples/bpf/5sec.c sleep 5s
bpf: builtin compilation failed: -95, try external compiler
Kernel build dir is set to /lib/modules/5.15.18-200.fc35.x86_64/build
set env: KBUILD_DIR=/lib/modules/5.15.18-200.fc35.x86_64/build
unset env: KBUILD_OPTS
include option is set to -nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/11/include -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=0x50f12
set env: CLANG_EXEC=/usr/lib64/ccache/clang
set env: CLANG_OPTIONS=-g
set env: KERNEL_INC_OPTIONS=-nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/11/include -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.15.18-200.fc35.x86_64/build
set env: CLANG_SOURCE=/home/acme/git/perf/tools/perf/examples/bpf/5sec.c
llvm compiling command template: $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 : /usr/lib64/ccache/clang -D__KERNEL__ -D__NR_CPUS__=8 -DLINUX_VERSION_CODE=0x50f12 -g -I/home/acme/lib/perf/include/bpf -nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/11/include -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.15.18-200.fc35.x86_64/build -c /home/acme/git/perf/tools/perf/examples/bpf/5sec.c -target bpf -O2 -o -
libbpf: loading object 'tools/perf/examples/bpf/5sec.c' from buffer
libbpf: elf: section(3) hrtimer_nanosleep=hrtimer_nanosleep rqtp, size 64, link 0, flags 6, type=1
libbpf: sec 'hrtimer_nanosleep=hrtimer_nanosleep rqtp': found program 'hrtimer_nanosleep' at insn offset 0 (0 bytes), code size 8 insns (64 bytes)
libbpf: elf: section(4) license, size 4, link 0, flags 3, type=1
libbpf: license of tools/perf/examples/bpf/5sec.c is GPL
libbpf: elf: section(5) version, size 4, link 0, flags 3, type=1
libbpf: kernel version of tools/perf/examples/bpf/5sec.c is 50f12
libbpf: elf: section(11) .BTF, size 558, link 0, flags 0, type=1
libbpf: elf: section(13) .BTF.ext, size 112, link 0, flags 0, type=1
libbpf: elf: section(20) .symtab, size 288, link 1, flags 0, type=2
libbpf: looking for externs among 12 symbols...
libbpf: collected 0 externs total
libbpf: prog 'hrtimer_nanosleep': unrecognized ELF section name 'hrtimer_nanosleep=hrtimer_nanosleep rqtp'
LLVM: dumping tools/perf/examples/bpf/5sec.o
bpf: config program 'hrtimer_nanosleep=hrtimer_nanosleep rqtp'
symbol:hrtimer_nanosleep file:(null) line:0 offset:0 return:0 lazy:(null)
parsing arg: rqtp into rqtp
bpf: config 'hrtimer_nanosleep=hrtimer_nanosleep rqtp' is ok
Looking at the vmlinux_path (8 entries long)
Using /usr/lib/debug/lib/modules/5.15.18-200.fc35.x86_64/vmlinux for symbols
Open Debuginfo file: /usr/lib/debug/.build-id/a5/6896963dc51b426302a1f1147842fb8f288ef2.debug
Try to find probe point from debuginfo.
Opening /sys/kernel/tracing//README write=0
Matched function: hrtimer_nanosleep [1af0959]
Probe point found: hrtimer_nanosleep+0
Searching 'rqtp' variable in context.
Converting variable rqtp into trace event.
rqtp type is long long int.
Found 1 probe_trace_events.
Opening /sys/kernel/tracing//kprobe_events write=1
Writing event: p:perf_bpf_probe/hrtimer_nanosleep _text+1540768 rqtp=%di:s64
libbpf: prog 'hrtimer_nanosleep': BPF program load failed: Permission denied
libbpf: prog 'hrtimer_nanosleep': -- BEGIN PROG LOAD LOG --
arg#0 reference type('UNKNOWN ') size cannot be determined: -22
; int probe(hrtimer_nanosleep, rqtp)(void *ctx, int err, long long sec)
0: (18) r1 = 0xfffffffed5fa0e00
; return sec / NSEC_PER_SEC == 5ULL;
2: (0f) r3 += r1
R3 !read_ok
processed 2 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 'hrtimer_nanosleep'
libbpf: failed to load object 'tools/perf/examples/bpf/5sec.c'
bpf: load objects failed: err=-13: (Permission denied)
event syntax error: 'tools/perf/examples/bpf/5sec.c'
\___ Permission denied

(add -v to see detail)
Run 'perf list' for a list of valid events

Usage: perf trace [<options>] [<command>]
or: perf trace [<options>] -- <command> [<options>]
or: perf trace record [<options>] [<command>]
or: perf trace record [<options>] -- <command> [<options>]

-e, --event <event> event/syscall selector. use 'perf list' to list available events
Opening /sys/kernel/tracing//kprobe_events write=1
Opening /sys/kernel/tracing//uprobe_events write=1
Parsing probe_events: p:perf_bpf_probe/hrtimer_nanosleep _text+1540768 rqtp=%di:s64 Group:perf_bpf_probe Event:hrtimer_nanosleep probe:p
Writing event: -:perf_bpf_probe/hrtimer_nanosleep
[root@quaco perf]#

#8 lore raw
Quoted 67 lines · Arnaldo Carvalho de Melo

Em Sun, Jan 23, 2022 at 11:19:30PM +0100, Jiri Olsa escreveu:

Quoted 11 lines

Removing code for ebpf program prologue generation.

The prologue code was used to get data for extra arguments specified in program section name, like:

  SEC("lock_page=__lock_page page->flags")
  int lock_page(struct pt_regs *ctx, int err, unsigned long flags)
  {
         return 1;
  }
Quoted 5 lines

This code is using deprecated libbpf API and blocks its removal.

This feature was not documented and broken for some time without anyone complaining, also original authors are not responding, so I'm removing it.

So, the example below breaks, how hard would be to move the deprecated APIs to perf like was done in some other cases?

- Arnaldo

Before:

[root@quaco perf]# cat tools/perf/examples/bpf/5sec.c // SPDX-License-Identifier: GPL-2.0
/*
Description:

. Disable strace like syscall tracing (--no-syscalls), or try tracing
just some (-e *sleep).

. Attach a filter function to a kernel function, returning when it should
be considered, i.e. appear on the output.

. Run it system wide, so that any sleep of >= 5 seconds and < than 6
seconds gets caught.

. Ask for callgraphs using DWARF info, so that userspace can be unwound

. While this is running, run something like "sleep 5s".

. If we decide to add tv_nsec as well, then it becomes:

int probe(hrtimer_nanosleep, rqtp->tv_sec rqtp->tv_nsec)(void *ctx, int err, long sec, long nsec)

I.e. add where it comes from (rqtp->tv_nsec) and where it will be accessible in the function body (nsec)

# perf trace --no-syscalls -e tools/perf/examples/bpf/5sec.c/call-graph=dwarf/
0.000 perf_bpf_probe:func:(ffffffff9811b5f0) tv_sec=5
hrtimer_nanosleep ([kernel.kallsyms]) __x64_sys_nanosleep ([kernel.kallsyms]) do_syscall_64 ([kernel.kallsyms]) entry_SYSCALL_64 ([kernel.kallsyms]) __GI___nanosleep (/usr/lib64/libc-2.26.so) rpl_nanosleep (/usr/bin/sleep) xnanosleep (/usr/bin/sleep) main (/usr/bin/sleep) __libc_start_main (/usr/lib64/libc-2.26.so) _start (/usr/bin/sleep)
^C#

Copyright (C) 2018 Red Hat, Inc., Arnaldo Carvalho de Melo <[email protected]>
*/

Quoted 8 lines

#include <bpf.h>

#define NSEC_PER_SEC 1000000000L

int probe(hrtimer_nanosleep, rqtp)(void *ctx, int err, long long sec)

{
	return sec / NSEC_PER_SEC == 5ULL;
}

that sucks ;-) I'll check if we can re-implement as we discussed earlier, however below is workaround how to do it without the prologue support

jirka

---

tools/perf/examples/bpf/5sec.c +10 -2
index e6b6181c6dc6..734d39debdb8 100644
@@ -43,9 +43,17 @@
 
 #define NSEC_PER_SEC	1000000000L
 
-int probe(hrtimer_nanosleep, rqtp)(void *ctx, int err, long long sec)
+struct pt_regs {
+        long di;
+};
+
+SEC("function=hrtimer_nanosleep")
+int krava(struct pt_regs *ctx)
 {
-	return sec / NSEC_PER_SEC == 5ULL;
+	unsigned long arg;
+
+	probe_read_kernel(&arg, sizeof(arg), __builtin_preserve_access_index(&ctx->di));
+	return arg / NSEC_PER_SEC == 5ULL;
 }
 
 license(GPL);
tools/perf/include/bpf/bpf.h +1 -0
index b422aeef5339..b7d6d2fc8342 100644
@@ -64,6 +64,7 @@ int _version SEC("version") = LINUX_VERSION_CODE;
 
 static int (*probe_read)(void *dst, int size, const void *unsafe_addr) = (void *)BPF_FUNC_probe_read;
 static int (*probe_read_str)(void *dst, int size, const void *unsafe_addr) = (void *)BPF_FUNC_probe_read_str;
+static long (*probe_read_kernel)(void *dst, __u32 size, const void *unsafe_ptr) = (void *) BPF_FUNC_probe_read_kernel;
 
 static int (*perf_event_output)(void *, struct bpf_map *, int, void *, unsigned long) = (void *)BPF_FUNC_perf_event_output;
 
tools/perf/util/llvm-utils.c +1 -1
index 96c8ef60f4f8..9274a3373847 100644
@@ -25,7 +25,7 @@
 		"$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"
+		"-g -c \"$CLANG_SOURCE\" -target bpf $CLANG_EMIT_LLVM -O2 -o - $LLVM_OPTIONS_PIPE"
 
 struct llvm_param llvm_param = {
 	.clang_path = "clang",
#9 lore raw
Quoted 16 lines · Jiri Olsa
Quoted 14 lines · Arnaldo Carvalho de Melo

Em Sun, Jan 23, 2022 at 11:19:30PM +0100, Jiri Olsa escreveu:

Quoted 13 lines

Removing code for ebpf program prologue generation.

The prologue code was used to get data for extra arguments specified in program section name, like:

  SEC("lock_page=__lock_page page->flags")
  int lock_page(struct pt_regs *ctx, int err, unsigned long flags)
  {
         return 1;
  }

This code is using deprecated libbpf API and blocks its removal.

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

This feature was not documented and broken for some time without anyone complaining, also original authors are not responding, so I'm removing it.

So, the example below breaks, how hard would be to move the deprecated APIs to perf like was done in some other cases?

Just copy/pasting libbpf code won't work. But there are three parts:

1. bpf_(program|map|object)__set_priv(). There is no equivalent API, but perf can maintain this private data by building hashmap where the key is bpf_object/bpf_map/bpf_program pointer itself. Annoying but very straightforward to replace.

2. For prologue generation, bpf_program__set_prep() doesn't have a direct equivalent. But program cloning and adjustment of the code can be achieved through bpf_program__insns()/bpf_program__insn_cnt() API to load one "prototype" program, gets its underlying insns and clone programs as necessary. After that, bpf_prog_load() would be used to load those cloned programs into kernel.

3. Those *very* custom SEC() definitions will be possible for perf to handle once [0] lands (I'll send new revision tomorrow, probably). You'll be able to register your own "fallback" handler with libbpf_register_prog_handler(NULL, ...).

[0] https://patchwork.kernel.org/project/netdevbpf/list/?series=611491&state=*

Sorry, it's not a straightforward copy/paste, but I hope this helps a bit.

Quoted 111 lines
Quoted 46 lines

- Arnaldo

Before:

[root@quaco perf]# cat tools/perf/examples/bpf/5sec.c // SPDX-License-Identifier: GPL-2.0
/*
Description:

. Disable strace like syscall tracing (--no-syscalls), or try tracing
just some (-e *sleep).

. Attach a filter function to a kernel function, returning when it should
be considered, i.e. appear on the output.

. Run it system wide, so that any sleep of >= 5 seconds and < than 6
seconds gets caught.

. Ask for callgraphs using DWARF info, so that userspace can be unwound

. While this is running, run something like "sleep 5s".

. If we decide to add tv_nsec as well, then it becomes:

int probe(hrtimer_nanosleep, rqtp->tv_sec rqtp->tv_nsec)(void *ctx, int err, long sec, long nsec)

I.e. add where it comes from (rqtp->tv_nsec) and where it will be accessible in the function body (nsec)

# perf trace --no-syscalls -e tools/perf/examples/bpf/5sec.c/call-graph=dwarf/
0.000 perf_bpf_probe:func:(ffffffff9811b5f0) tv_sec=5
hrtimer_nanosleep ([kernel.kallsyms]) __x64_sys_nanosleep ([kernel.kallsyms]) do_syscall_64 ([kernel.kallsyms]) entry_SYSCALL_64 ([kernel.kallsyms]) __GI___nanosleep (/usr/lib64/libc-2.26.so) rpl_nanosleep (/usr/bin/sleep) xnanosleep (/usr/bin/sleep) main (/usr/bin/sleep) __libc_start_main (/usr/lib64/libc-2.26.so) _start (/usr/bin/sleep)
^C#

Copyright (C) 2018 Red Hat, Inc., Arnaldo Carvalho de Melo <[email protected]>
*/

Quoted 8 lines

#include <bpf.h>

#define NSEC_PER_SEC 1000000000L

int probe(hrtimer_nanosleep, rqtp)(void *ctx, int err, long long sec)

{
      return sec / NSEC_PER_SEC == 5ULL;
}

that sucks ;-) I'll check if we can re-implement as we discussed earlier, however below is workaround how to do it without the prologue support

jirka

---

tools/perf/examples/bpf/5sec.c +10 -2
index e6b6181c6dc6..734d39debdb8 100644
@@ -43,9 +43,17 @@
 
 #define NSEC_PER_SEC   1000000000L
 
-int probe(hrtimer_nanosleep, rqtp)(void *ctx, int err, long long sec)
+struct pt_regs {
+        long di;
+};
+
+SEC("function=hrtimer_nanosleep")
+int krava(struct pt_regs *ctx)
 {
-       return sec / NSEC_PER_SEC == 5ULL;
+       unsigned long arg;
+
+       probe_read_kernel(&arg, sizeof(arg), __builtin_preserve_access_index(&ctx->di));
+       return arg / NSEC_PER_SEC == 5ULL;
 }
 
 license(GPL);
tools/perf/include/bpf/bpf.h +1 -0
index b422aeef5339..b7d6d2fc8342 100644
@@ -64,6 +64,7 @@ int _version SEC("version") = LINUX_VERSION_CODE;
 
 static int (*probe_read)(void *dst, int size, const void *unsafe_addr) = (void *)BPF_FUNC_probe_read;
 static int (*probe_read_str)(void *dst, int size, const void *unsafe_addr) = (void *)BPF_FUNC_probe_read_str;
+static long (*probe_read_kernel)(void *dst, __u32 size, const void *unsafe_ptr) = (void *) BPF_FUNC_probe_read_kernel;
 
 static int (*perf_event_output)(void *, struct bpf_map *, int, void *, unsigned long) = (void *)BPF_FUNC_perf_event_output;
 
tools/perf/util/llvm-utils.c +1 -1
index 96c8ef60f4f8..9274a3373847 100644
@@ -25,7 +25,7 @@
                "$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"
+               "-g -c \"$CLANG_SOURCE\" -target bpf $CLANG_EMIT_LLVM -O2 -o - $LLVM_OPTIONS_PIPE"
 
 struct llvm_param llvm_param = {
        .clang_path = "clang",
#10 lore raw
Quoted 32 lines · Andrii Nakryiko
Quoted 16 lines · Jiri Olsa
Quoted 14 lines · Arnaldo Carvalho de Melo

Em Sun, Jan 23, 2022 at 11:19:30PM +0100, Jiri Olsa escreveu:

Quoted 13 lines

Removing code for ebpf program prologue generation.

The prologue code was used to get data for extra arguments specified in program section name, like:

  SEC("lock_page=__lock_page page->flags")
  int lock_page(struct pt_regs *ctx, int err, unsigned long flags)
  {
         return 1;
  }

This code is using deprecated libbpf API and blocks its removal.

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

This feature was not documented and broken for some time without anyone complaining, also original authors are not responding, so I'm removing it.

So, the example below breaks, how hard would be to move the deprecated APIs to perf like was done in some other cases?

Just copy/pasting libbpf code won't work. But there are three parts:

1. bpf_(program|map|object)__set_priv(). There is no equivalent API, but perf can maintain this private data by building hashmap where the key is bpf_object/bpf_map/bpf_program pointer itself. Annoying but very straightforward to replace.

Quoted 6 lines

2. For prologue generation, bpf_program__set_prep() doesn't have a direct equivalent. But program cloning and adjustment of the code can be achieved through bpf_program__insns()/bpf_program__insn_cnt() API to load one "prototype" program, gets its underlying insns and clone programs as necessary. After that, bpf_prog_load() would be used to load those cloned programs into kernel.

hm, I can't see how to clone a program.. so we need to end up with several copies of the single program defined in the object.. I can get its intructions with bpf_program__insns, but how do I add more programs with these instructions customized/prefixed?

thanks,
jirka

Quoted 121 lines

3. Those *very* custom SEC() definitions will be possible for perf to handle once [0] lands (I'll send new revision tomorrow, probably). You'll be able to register your own "fallback" handler with libbpf_register_prog_handler(NULL, ...).

[0] https://patchwork.kernel.org/project/netdevbpf/list/?series=611491&state=*

Sorry, it's not a straightforward copy/paste, but I hope this helps a bit.

Quoted 111 lines
Quoted 46 lines

- Arnaldo

Before:

[root@quaco perf]# cat tools/perf/examples/bpf/5sec.c // SPDX-License-Identifier: GPL-2.0
/*
Description:

. Disable strace like syscall tracing (--no-syscalls), or try tracing
just some (-e *sleep).

. Attach a filter function to a kernel function, returning when it should
be considered, i.e. appear on the output.

. Run it system wide, so that any sleep of >= 5 seconds and < than 6
seconds gets caught.

. Ask for callgraphs using DWARF info, so that userspace can be unwound

. While this is running, run something like "sleep 5s".

. If we decide to add tv_nsec as well, then it becomes:

int probe(hrtimer_nanosleep, rqtp->tv_sec rqtp->tv_nsec)(void *ctx, int err, long sec, long nsec)

I.e. add where it comes from (rqtp->tv_nsec) and where it will be accessible in the function body (nsec)

# perf trace --no-syscalls -e tools/perf/examples/bpf/5sec.c/call-graph=dwarf/
0.000 perf_bpf_probe:func:(ffffffff9811b5f0) tv_sec=5
hrtimer_nanosleep ([kernel.kallsyms]) __x64_sys_nanosleep ([kernel.kallsyms]) do_syscall_64 ([kernel.kallsyms]) entry_SYSCALL_64 ([kernel.kallsyms]) __GI___nanosleep (/usr/lib64/libc-2.26.so) rpl_nanosleep (/usr/bin/sleep) xnanosleep (/usr/bin/sleep) main (/usr/bin/sleep) __libc_start_main (/usr/lib64/libc-2.26.so) _start (/usr/bin/sleep)
^C#

Copyright (C) 2018 Red Hat, Inc., Arnaldo Carvalho de Melo <[email protected]>
*/

Quoted 8 lines

#include <bpf.h>

#define NSEC_PER_SEC 1000000000L

int probe(hrtimer_nanosleep, rqtp)(void *ctx, int err, long long sec)

{
      return sec / NSEC_PER_SEC == 5ULL;
}

that sucks ;-) I'll check if we can re-implement as we discussed earlier, however below is workaround how to do it without the prologue support

jirka

---

tools/perf/examples/bpf/5sec.c +10 -2
index e6b6181c6dc6..734d39debdb8 100644
@@ -43,9 +43,17 @@
 
 #define NSEC_PER_SEC   1000000000L
 
-int probe(hrtimer_nanosleep, rqtp)(void *ctx, int err, long long sec)
+struct pt_regs {
+        long di;
+};
+
+SEC("function=hrtimer_nanosleep")
+int krava(struct pt_regs *ctx)
 {
-       return sec / NSEC_PER_SEC == 5ULL;
+       unsigned long arg;
+
+       probe_read_kernel(&arg, sizeof(arg), __builtin_preserve_access_index(&ctx->di));
+       return arg / NSEC_PER_SEC == 5ULL;
 }
 
 license(GPL);
tools/perf/include/bpf/bpf.h +1 -0
index b422aeef5339..b7d6d2fc8342 100644
@@ -64,6 +64,7 @@ int _version SEC("version") = LINUX_VERSION_CODE;
 
 static int (*probe_read)(void *dst, int size, const void *unsafe_addr) = (void *)BPF_FUNC_probe_read;
 static int (*probe_read_str)(void *dst, int size, const void *unsafe_addr) = (void *)BPF_FUNC_probe_read_str;
+static long (*probe_read_kernel)(void *dst, __u32 size, const void *unsafe_ptr) = (void *) BPF_FUNC_probe_read_kernel;
 
 static int (*perf_event_output)(void *, struct bpf_map *, int, void *, unsigned long) = (void *)BPF_FUNC_perf_event_output;
 
tools/perf/util/llvm-utils.c +1 -1
index 96c8ef60f4f8..9274a3373847 100644
@@ -25,7 +25,7 @@
                "$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"
+               "-g -c \"$CLANG_SOURCE\" -target bpf $CLANG_EMIT_LLVM -O2 -o - $LLVM_OPTIONS_PIPE"
 
 struct llvm_param llvm_param = {
        .clang_path = "clang",
#11 lore raw
Quoted 41 lines · Jiri Olsa
Quoted 32 lines · Andrii Nakryiko
Quoted 16 lines · Jiri Olsa
Quoted 14 lines · Arnaldo Carvalho de Melo

Em Sun, Jan 23, 2022 at 11:19:30PM +0100, Jiri Olsa escreveu:

Quoted 13 lines

Removing code for ebpf program prologue generation.

The prologue code was used to get data for extra arguments specified in program section name, like:

  SEC("lock_page=__lock_page page->flags")
  int lock_page(struct pt_regs *ctx, int err, unsigned long flags)
  {
         return 1;
  }

This code is using deprecated libbpf API and blocks its removal.

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

This feature was not documented and broken for some time without anyone complaining, also original authors are not responding, so I'm removing it.

So, the example below breaks, how hard would be to move the deprecated APIs to perf like was done in some other cases?

Just copy/pasting libbpf code won't work. But there are three parts:

1. bpf_(program|map|object)__set_priv(). There is no equivalent API, but perf can maintain this private data by building hashmap where the key is bpf_object/bpf_map/bpf_program pointer itself. Annoying but very straightforward to replace.

Quoted 6 lines

2. For prologue generation, bpf_program__set_prep() doesn't have a direct equivalent. But program cloning and adjustment of the code can be achieved through bpf_program__insns()/bpf_program__insn_cnt() API to load one "prototype" program, gets its underlying insns and clone programs as necessary. After that, bpf_prog_load() would be used to load those cloned programs into kernel.

Quoted 4 lines

hm, I can't see how to clone a program.. so we need to end up with several copies of the single program defined in the object.. I can get its intructions with bpf_program__insns, but how do I add more programs with these instructions customized/prefixed?

You can't add those clones back to bpf_object, of course. But after grabbing (and modifying) instructions, you can use bpf_prog_load() low-level API to create BPF programs and get their FDs back. You'll have to keep track of those prog FDs separately from libbpf' struct bpf_object.

Quoted 125 lines

thanks,
jirka

Quoted 121 lines

3. Those *very* custom SEC() definitions will be possible for perf to handle once [0] lands (I'll send new revision tomorrow, probably). You'll be able to register your own "fallback" handler with libbpf_register_prog_handler(NULL, ...).

[0] https://patchwork.kernel.org/project/netdevbpf/list/?series=611491&state=*

Sorry, it's not a straightforward copy/paste, but I hope this helps a bit.

Quoted 111 lines
Quoted 46 lines

- Arnaldo

Before:

[root@quaco perf]# cat tools/perf/examples/bpf/5sec.c // SPDX-License-Identifier: GPL-2.0
/*
Description:

. Disable strace like syscall tracing (--no-syscalls), or try tracing
just some (-e *sleep).

. Attach a filter function to a kernel function, returning when it should
be considered, i.e. appear on the output.

. Run it system wide, so that any sleep of >= 5 seconds and < than 6
seconds gets caught.

. Ask for callgraphs using DWARF info, so that userspace can be unwound

. While this is running, run something like "sleep 5s".

. If we decide to add tv_nsec as well, then it becomes:

int probe(hrtimer_nanosleep, rqtp->tv_sec rqtp->tv_nsec)(void *ctx, int err, long sec, long nsec)

I.e. add where it comes from (rqtp->tv_nsec) and where it will be accessible in the function body (nsec)

# perf trace --no-syscalls -e tools/perf/examples/bpf/5sec.c/call-graph=dwarf/
0.000 perf_bpf_probe:func:(ffffffff9811b5f0) tv_sec=5
hrtimer_nanosleep ([kernel.kallsyms]) __x64_sys_nanosleep ([kernel.kallsyms]) do_syscall_64 ([kernel.kallsyms]) entry_SYSCALL_64 ([kernel.kallsyms]) __GI___nanosleep (/usr/lib64/libc-2.26.so) rpl_nanosleep (/usr/bin/sleep) xnanosleep (/usr/bin/sleep) main (/usr/bin/sleep) __libc_start_main (/usr/lib64/libc-2.26.so) _start (/usr/bin/sleep)
^C#

Copyright (C) 2018 Red Hat, Inc., Arnaldo Carvalho de Melo <[email protected]>
*/

Quoted 8 lines

#include <bpf.h>

#define NSEC_PER_SEC 1000000000L

int probe(hrtimer_nanosleep, rqtp)(void *ctx, int err, long long sec)

{
      return sec / NSEC_PER_SEC == 5ULL;
}

that sucks ;-) I'll check if we can re-implement as we discussed earlier, however below is workaround how to do it without the prologue support

jirka

---

tools/perf/examples/bpf/5sec.c +10 -2
index e6b6181c6dc6..734d39debdb8 100644
@@ -43,9 +43,17 @@
 
 #define NSEC_PER_SEC   1000000000L
 
-int probe(hrtimer_nanosleep, rqtp)(void *ctx, int err, long long sec)
+struct pt_regs {
+        long di;
+};
+
+SEC("function=hrtimer_nanosleep")
+int krava(struct pt_regs *ctx)
 {
-       return sec / NSEC_PER_SEC == 5ULL;
+       unsigned long arg;
+
+       probe_read_kernel(&arg, sizeof(arg), __builtin_preserve_access_index(&ctx->di));
+       return arg / NSEC_PER_SEC == 5ULL;
 }
 
 license(GPL);
tools/perf/include/bpf/bpf.h +1 -0
index b422aeef5339..b7d6d2fc8342 100644
@@ -64,6 +64,7 @@ int _version SEC("version") = LINUX_VERSION_CODE;
 
 static int (*probe_read)(void *dst, int size, const void *unsafe_addr) = (void *)BPF_FUNC_probe_read;
 static int (*probe_read_str)(void *dst, int size, const void *unsafe_addr) = (void *)BPF_FUNC_probe_read_str;
+static long (*probe_read_kernel)(void *dst, __u32 size, const void *unsafe_ptr) = (void *) BPF_FUNC_probe_read_kernel;
 
 static int (*perf_event_output)(void *, struct bpf_map *, int, void *, unsigned long) = (void *)BPF_FUNC_perf_event_output;
 
tools/perf/util/llvm-utils.c +1 -1
index 96c8ef60f4f8..9274a3373847 100644
@@ -25,7 +25,7 @@
                "$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"
+               "-g -c \"$CLANG_SOURCE\" -target bpf $CLANG_EMIT_LLVM -O2 -o - $LLVM_OPTIONS_PIPE"
 
 struct llvm_param llvm_param = {
        .clang_path = "clang",
#13 lore raw
In reply to Jiri Olsa
Quoted 73 lines · Jiri Olsa
Quoted 71 lines · Arnaldo Carvalho de Melo

Em Sun, Jan 23, 2022 at 11:19:30PM +0100, Jiri Olsa escreveu:

Quoted 11 lines

Removing code for ebpf program prologue generation.

The prologue code was used to get data for extra arguments specified in program section name, like:

  SEC("lock_page=__lock_page page->flags")
  int lock_page(struct pt_regs *ctx, int err, unsigned long flags)
  {
         return 1;
  }
Quoted 5 lines

This code is using deprecated libbpf API and blocks its removal.

This feature was not documented and broken for some time without anyone complaining, also original authors are not responding, so I'm removing it.

So, the example below breaks, how hard would be to move the deprecated APIs to perf like was done in some other cases?

- Arnaldo

Before:

[root@quaco perf]# cat tools/perf/examples/bpf/5sec.c // SPDX-License-Identifier: GPL-2.0
/*
Description:

. Disable strace like syscall tracing (--no-syscalls), or try tracing
just some (-e *sleep).

. Attach a filter function to a kernel function, returning when it should
be considered, i.e. appear on the output.

. Run it system wide, so that any sleep of >= 5 seconds and < than 6
seconds gets caught.

. Ask for callgraphs using DWARF info, so that userspace can be unwound

. While this is running, run something like "sleep 5s".

. If we decide to add tv_nsec as well, then it becomes:

int probe(hrtimer_nanosleep, rqtp->tv_sec rqtp->tv_nsec)(void *ctx, int err, long sec, long nsec)

I.e. add where it comes from (rqtp->tv_nsec) and where it will be accessible in the function body (nsec)

# perf trace --no-syscalls -e tools/perf/examples/bpf/5sec.c/call-graph=dwarf/
0.000 perf_bpf_probe:func:(ffffffff9811b5f0) tv_sec=5
hrtimer_nanosleep ([kernel.kallsyms]) __x64_sys_nanosleep ([kernel.kallsyms]) do_syscall_64 ([kernel.kallsyms]) entry_SYSCALL_64 ([kernel.kallsyms]) __GI___nanosleep (/usr/lib64/libc-2.26.so) rpl_nanosleep (/usr/bin/sleep) xnanosleep (/usr/bin/sleep) main (/usr/bin/sleep) __libc_start_main (/usr/lib64/libc-2.26.so) _start (/usr/bin/sleep)
^C#

Copyright (C) 2018 Red Hat, Inc., Arnaldo Carvalho de Melo <[email protected]>
*/

#include <bpf.h>

#define NSEC_PER_SEC 1000000000L

Quoted 8 lines
Quoted 4 lines

int probe(hrtimer_nanosleep, rqtp)(void *ctx, int err, long long sec)

{
      return sec / NSEC_PER_SEC == 5ULL;
}

that sucks ;-) I'll check if we can re-implement as we discussed earlier, however below is workaround how to do it without the prologue support

I'm a bit confused. I thought that we discussed dropping custom SEC() definitions from perf as no one knew if anyone is even using this feature and when Jiri tried it didn't even work ([0]). Is this broken sample some other important use case that we are sure is used by people and thus must be preserved?

Or dropping this custom SEC() business and prologue generation still on the table?

Quoted 53 lines

jirka

---

tools/perf/examples/bpf/5sec.c +10 -2
index e6b6181c6dc6..734d39debdb8 100644
@@ -43,9 +43,17 @@
 
 #define NSEC_PER_SEC   1000000000L
 
-int probe(hrtimer_nanosleep, rqtp)(void *ctx, int err, long long sec)
+struct pt_regs {
+        long di;
+};
+
+SEC("function=hrtimer_nanosleep")
+int krava(struct pt_regs *ctx)
 {
-       return sec / NSEC_PER_SEC == 5ULL;
+       unsigned long arg;
+
+       probe_read_kernel(&arg, sizeof(arg), __builtin_preserve_access_index(&ctx->di));
+       return arg / NSEC_PER_SEC == 5ULL;
 }
 
 license(GPL);
tools/perf/include/bpf/bpf.h +1 -0
index b422aeef5339..b7d6d2fc8342 100644
@@ -64,6 +64,7 @@ int _version SEC("version") = LINUX_VERSION_CODE;
 
 static int (*probe_read)(void *dst, int size, const void *unsafe_addr) = (void *)BPF_FUNC_probe_read;
 static int (*probe_read_str)(void *dst, int size, const void *unsafe_addr) = (void *)BPF_FUNC_probe_read_str;
+static long (*probe_read_kernel)(void *dst, __u32 size, const void *unsafe_ptr) = (void *) BPF_FUNC_probe_read_kernel;
 
 static int (*perf_event_output)(void *, struct bpf_map *, int, void *, unsigned long) = (void *)BPF_FUNC_perf_event_output;
 
tools/perf/util/llvm-utils.c +1 -1
index 96c8ef60f4f8..9274a3373847 100644
@@ -25,7 +25,7 @@
                "$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"
+               "-g -c \"$CLANG_SOURCE\" -target bpf $CLANG_EMIT_LLVM -O2 -o - $LLVM_OPTIONS_PIPE"
 
 struct llvm_param llvm_param = {
        .clang_path = "clang",
#14 lore raw
Quoted 83 lines · Andrii Nakryiko
Quoted 73 lines · Jiri Olsa
Quoted 71 lines · Arnaldo Carvalho de Melo

Em Sun, Jan 23, 2022 at 11:19:30PM +0100, Jiri Olsa escreveu:

Quoted 11 lines

Removing code for ebpf program prologue generation.

The prologue code was used to get data for extra arguments specified in program section name, like:

  SEC("lock_page=__lock_page page->flags")
  int lock_page(struct pt_regs *ctx, int err, unsigned long flags)
  {
         return 1;
  }
Quoted 5 lines

This code is using deprecated libbpf API and blocks its removal.

This feature was not documented and broken for some time without anyone complaining, also original authors are not responding, so I'm removing it.

So, the example below breaks, how hard would be to move the deprecated APIs to perf like was done in some other cases?

- Arnaldo

Before:

[root@quaco perf]# cat tools/perf/examples/bpf/5sec.c // SPDX-License-Identifier: GPL-2.0
/*
Description:

. Disable strace like syscall tracing (--no-syscalls), or try tracing
just some (-e *sleep).

. Attach a filter function to a kernel function, returning when it should
be considered, i.e. appear on the output.

. Run it system wide, so that any sleep of >= 5 seconds and < than 6
seconds gets caught.

. Ask for callgraphs using DWARF info, so that userspace can be unwound

. While this is running, run something like "sleep 5s".

. If we decide to add tv_nsec as well, then it becomes:

int probe(hrtimer_nanosleep, rqtp->tv_sec rqtp->tv_nsec)(void *ctx, int err, long sec, long nsec)

I.e. add where it comes from (rqtp->tv_nsec) and where it will be accessible in the function body (nsec)

# perf trace --no-syscalls -e tools/perf/examples/bpf/5sec.c/call-graph=dwarf/
0.000 perf_bpf_probe:func:(ffffffff9811b5f0) tv_sec=5
hrtimer_nanosleep ([kernel.kallsyms]) __x64_sys_nanosleep ([kernel.kallsyms]) do_syscall_64 ([kernel.kallsyms]) entry_SYSCALL_64 ([kernel.kallsyms]) __GI___nanosleep (/usr/lib64/libc-2.26.so) rpl_nanosleep (/usr/bin/sleep) xnanosleep (/usr/bin/sleep) main (/usr/bin/sleep) __libc_start_main (/usr/lib64/libc-2.26.so) _start (/usr/bin/sleep)
^C#

Copyright (C) 2018 Red Hat, Inc., Arnaldo Carvalho de Melo <[email protected]>
*/

#include <bpf.h>

#define NSEC_PER_SEC 1000000000L

Quoted 8 lines
Quoted 4 lines

int probe(hrtimer_nanosleep, rqtp)(void *ctx, int err, long long sec)

{
      return sec / NSEC_PER_SEC == 5ULL;
}

that sucks ;-) I'll check if we can re-implement as we discussed earlier, however below is workaround how to do it without the prologue support

Quoted 5 lines

I'm a bit confused. I thought that we discussed dropping custom SEC() definitions from perf as no one knew if anyone is even using this feature and when Jiri tried it didn't even work ([0]). Is this broken sample some other important use case that we are sure is used by people and thus must be preserved?

I overlooked that script Arnaldo found in perf, so it's not as private feature as we thought before and there could be people using it

we discussed and I'll give it another try to add the support

Quoted 3 lines

Or dropping this custom SEC() business and prologue generation still on the table?

I was thinking another way could be to 'mark it' somehow on our side as deprecated with workaround below and remove it after some time

jirka

Quoted 54 lines
Quoted 54 lines

jirka

---

tools/perf/examples/bpf/5sec.c +10 -2
index e6b6181c6dc6..734d39debdb8 100644
@@ -43,9 +43,17 @@
 
 #define NSEC_PER_SEC   1000000000L
 
-int probe(hrtimer_nanosleep, rqtp)(void *ctx, int err, long long sec)
+struct pt_regs {
+        long di;
+};
+
+SEC("function=hrtimer_nanosleep")
+int krava(struct pt_regs *ctx)
 {
-       return sec / NSEC_PER_SEC == 5ULL;
+       unsigned long arg;
+
+       probe_read_kernel(&arg, sizeof(arg), __builtin_preserve_access_index(&ctx->di));
+       return arg / NSEC_PER_SEC == 5ULL;
 }
 
 license(GPL);
tools/perf/include/bpf/bpf.h +1 -0
index b422aeef5339..b7d6d2fc8342 100644
@@ -64,6 +64,7 @@ int _version SEC("version") = LINUX_VERSION_CODE;
 
 static int (*probe_read)(void *dst, int size, const void *unsafe_addr) = (void *)BPF_FUNC_probe_read;
 static int (*probe_read_str)(void *dst, int size, const void *unsafe_addr) = (void *)BPF_FUNC_probe_read_str;
+static long (*probe_read_kernel)(void *dst, __u32 size, const void *unsafe_ptr) = (void *) BPF_FUNC_probe_read_kernel;
 
 static int (*perf_event_output)(void *, struct bpf_map *, int, void *, unsigned long) = (void *)BPF_FUNC_perf_event_output;
 
tools/perf/util/llvm-utils.c +1 -1
index 96c8ef60f4f8..9274a3373847 100644
@@ -25,7 +25,7 @@
                "$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"
+               "-g -c \"$CLANG_SOURCE\" -target bpf $CLANG_EMIT_LLVM -O2 -o - $LLVM_OPTIONS_PIPE"
 
 struct llvm_param llvm_param = {
        .clang_path = "clang",
#12 lore raw
In reply to Andrii Nakryiko
Quoted 47 lines · Andrii Nakryiko
Quoted 41 lines · Jiri Olsa
Quoted 32 lines · Andrii Nakryiko
Quoted 16 lines · Jiri Olsa
Quoted 14 lines · Arnaldo Carvalho de Melo

Em Sun, Jan 23, 2022 at 11:19:30PM +0100, Jiri Olsa escreveu:

Quoted 13 lines

Removing code for ebpf program prologue generation.

The prologue code was used to get data for extra arguments specified in program section name, like:

  SEC("lock_page=__lock_page page->flags")
  int lock_page(struct pt_regs *ctx, int err, unsigned long flags)
  {
         return 1;
  }

This code is using deprecated libbpf API and blocks its removal.

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

This feature was not documented and broken for some time without anyone complaining, also original authors are not responding, so I'm removing it.

So, the example below breaks, how hard would be to move the deprecated APIs to perf like was done in some other cases?

Just copy/pasting libbpf code won't work. But there are three parts:

1. bpf_(program|map|object)__set_priv(). There is no equivalent API, but perf can maintain this private data by building hashmap where the key is bpf_object/bpf_map/bpf_program pointer itself. Annoying but very straightforward to replace.

Quoted 6 lines

2. For prologue generation, bpf_program__set_prep() doesn't have a direct equivalent. But program cloning and adjustment of the code can be achieved through bpf_program__insns()/bpf_program__insn_cnt() API to load one "prototype" program, gets its underlying insns and clone programs as necessary. After that, bpf_prog_load() would be used to load those cloned programs into kernel.

Quoted 4 lines

hm, I can't see how to clone a program.. so we need to end up with several copies of the single program defined in the object.. I can get its intructions with bpf_program__insns, but how do I add more programs with these instructions customized/prefixed?

Quoted 5 lines

You can't add those clones back to bpf_object, of course. But after grabbing (and modifying) instructions, you can use bpf_prog_load() low-level API to create BPF programs and get their FDs back. You'll have to keep track of those prog FDs separately from libbpf' struct bpf_object.

ok so loaded on the side with bpf_prog_load

thanks,
jirka