bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andrii Nakryiko <andrii@kernel.org>
To: <bpf@vger.kernel.org>, <ast@kernel.org>, <daniel@iogearbox.net>
Cc: <andrii@kernel.org>, <kernel-team@fb.com>,
	Peter Zijlstra <peterz@infradead.org>
Subject: [PATCH v5 bpf-next 16/16] selftests/bpf: add ref_ctr_offset selftests
Date: Sun, 15 Aug 2021 00:06:09 -0700	[thread overview]
Message-ID: <20210815070609.987780-17-andrii@kernel.org> (raw)
In-Reply-To: <20210815070609.987780-1-andrii@kernel.org>

Extend attach_probe selftests to specify ref_ctr_offset for uprobe/uretprobe
and validate that its value is incremented from zero.

Turns out that once uprobe is attached with ref_ctr_offset, uretprobe for the
same location/function *has* to use ref_ctr_offset as well, otherwise
perf_event_open() fails with -EINVAL. So this test uses ref_ctr_offset for
both uprobe and uretprobe, even though for the purpose of test uprobe would be
enough.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
---
 .../selftests/bpf/prog_tests/attach_probe.c   | 39 +++++++++++++------
 tools/testing/selftests/bpf/trace_helpers.c   | 21 ++++++++++
 tools/testing/selftests/bpf/trace_helpers.h   |  1 +
 3 files changed, 50 insertions(+), 11 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/attach_probe.c b/tools/testing/selftests/bpf/prog_tests/attach_probe.c
index e40b41c44f8b..bf307bb9e446 100644
--- a/tools/testing/selftests/bpf/prog_tests/attach_probe.c
+++ b/tools/testing/selftests/bpf/prog_tests/attach_probe.c
@@ -2,14 +2,18 @@
 #include <test_progs.h>
 #include "test_attach_probe.skel.h"
 
+/* this is how USDT semaphore is actually defined, except volatile modifier */
+volatile unsigned short uprobe_ref_ctr __attribute__((unused)) __attribute((section(".probes")));
+
 void test_attach_probe(void)
 {
+	DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, uprobe_opts);
 	int duration = 0;
 	struct bpf_link *kprobe_link, *kretprobe_link;
 	struct bpf_link *uprobe_link, *uretprobe_link;
 	struct test_attach_probe* skel;
 	size_t uprobe_offset;
-	ssize_t base_addr;
+	ssize_t base_addr, ref_ctr_offset;
 
 	base_addr = get_base_addr();
 	if (CHECK(base_addr < 0, "get_base_addr",
@@ -17,6 +21,10 @@ void test_attach_probe(void)
 		return;
 	uprobe_offset = get_uprobe_offset(&get_base_addr, base_addr);
 
+	ref_ctr_offset = get_rel_offset((uintptr_t)&uprobe_ref_ctr);
+	if (!ASSERT_GE(ref_ctr_offset, 0, "ref_ctr_offset"))
+		return;
+
 	skel = test_attach_probe__open_and_load();
 	if (CHECK(!skel, "skel_open", "failed to open skeleton\n"))
 		return;
@@ -37,20 +45,28 @@ void test_attach_probe(void)
 		goto cleanup;
 	skel->links.handle_kretprobe = kretprobe_link;
 
-	uprobe_link = bpf_program__attach_uprobe(skel->progs.handle_uprobe,
-						 false /* retprobe */,
-						 0 /* self pid */,
-						 "/proc/self/exe",
-						 uprobe_offset);
+	ASSERT_EQ(uprobe_ref_ctr, 0, "uprobe_ref_ctr_before");
+
+	uprobe_opts.retprobe = false;
+	uprobe_opts.ref_ctr_offset = ref_ctr_offset;
+	uprobe_link = bpf_program__attach_uprobe_opts(skel->progs.handle_uprobe,
+						      0 /* self pid */,
+						      "/proc/self/exe",
+						      uprobe_offset,
+						      &uprobe_opts);
 	if (!ASSERT_OK_PTR(uprobe_link, "attach_uprobe"))
 		goto cleanup;
 	skel->links.handle_uprobe = uprobe_link;
 
-	uretprobe_link = bpf_program__attach_uprobe(skel->progs.handle_uretprobe,
-						    true /* retprobe */,
-						    -1 /* any pid */,
-						    "/proc/self/exe",
-						    uprobe_offset);
+	ASSERT_GT(uprobe_ref_ctr, 0, "uprobe_ref_ctr_after");
+
+	/* if uprobe uses ref_ctr, uretprobe has to use ref_ctr as well */
+	uprobe_opts.retprobe = true;
+	uprobe_opts.ref_ctr_offset = ref_ctr_offset;
+	uretprobe_link = bpf_program__attach_uprobe_opts(skel->progs.handle_uretprobe,
+							 -1 /* any pid */,
+							 "/proc/self/exe",
+							 uprobe_offset, &uprobe_opts);
 	if (!ASSERT_OK_PTR(uretprobe_link, "attach_uretprobe"))
 		goto cleanup;
 	skel->links.handle_uretprobe = uretprobe_link;
@@ -77,4 +93,5 @@ void test_attach_probe(void)
 
 cleanup:
 	test_attach_probe__destroy(skel);
+	ASSERT_EQ(uprobe_ref_ctr, 0, "uprobe_ref_ctr_cleanup");
 }
diff --git a/tools/testing/selftests/bpf/trace_helpers.c b/tools/testing/selftests/bpf/trace_helpers.c
index 381dafce1d8f..e7a19b04d4ea 100644
--- a/tools/testing/selftests/bpf/trace_helpers.c
+++ b/tools/testing/selftests/bpf/trace_helpers.c
@@ -202,3 +202,24 @@ ssize_t get_base_addr(void)
 	fclose(f);
 	return -EINVAL;
 }
+
+ssize_t get_rel_offset(uintptr_t addr)
+{
+	size_t start, end, offset;
+	char buf[256];
+	FILE *f;
+
+	f = fopen("/proc/self/maps", "r");
+	if (!f)
+		return -errno;
+
+	while (fscanf(f, "%zx-%zx %s %zx %*[^\n]\n", &start, &end, buf, &offset) == 4) {
+		if (addr >= start && addr < end) {
+			fclose(f);
+			return (size_t)addr - start + offset;
+		}
+	}
+
+	fclose(f);
+	return -EINVAL;
+}
diff --git a/tools/testing/selftests/bpf/trace_helpers.h b/tools/testing/selftests/bpf/trace_helpers.h
index 3d9435b3dd3b..d907b445524d 100644
--- a/tools/testing/selftests/bpf/trace_helpers.h
+++ b/tools/testing/selftests/bpf/trace_helpers.h
@@ -20,5 +20,6 @@ void read_trace_pipe(void);
 
 ssize_t get_uprobe_offset(const void *addr, ssize_t base);
 ssize_t get_base_addr(void);
+ssize_t get_rel_offset(uintptr_t addr);
 
 #endif
-- 
2.30.2


  parent reply	other threads:[~2021-08-15  7:07 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-15  7:05 [PATCH v5 bpf-next 00/16] BPF perf link and user-provided bpf_cookie Andrii Nakryiko
2021-08-15  7:05 ` [PATCH v5 bpf-next 01/16] bpf: refactor BPF_PROG_RUN into a function Andrii Nakryiko
2021-08-15  7:05 ` [PATCH v5 bpf-next 02/16] bpf: refactor BPF_PROG_RUN_ARRAY family of macros into functions Andrii Nakryiko
2021-08-15  7:05 ` [PATCH v5 bpf-next 03/16] bpf: refactor perf_event_set_bpf_prog() to use struct bpf_prog input Andrii Nakryiko
2021-08-15  7:05 ` [PATCH v5 bpf-next 04/16] bpf: implement minimal BPF perf link Andrii Nakryiko
2021-08-15  7:05 ` [PATCH v5 bpf-next 05/16] bpf: allow to specify user-provided bpf_cookie for BPF perf links Andrii Nakryiko
2021-08-16 11:46   ` Peter Zijlstra
2021-08-15  7:05 ` [PATCH v5 bpf-next 06/16] bpf: add bpf_get_attach_cookie() BPF helper to access bpf_cookie value Andrii Nakryiko
2021-08-15  7:06 ` [PATCH v5 bpf-next 07/16] libbpf: re-build libbpf.so when libbpf.map changes Andrii Nakryiko
2021-08-15  7:06 ` [PATCH v5 bpf-next 08/16] libbpf: remove unused bpf_link's destroy operation, but add dealloc Andrii Nakryiko
2021-08-15  7:06 ` [PATCH v5 bpf-next 09/16] libbpf: use BPF perf link when supported by kernel Andrii Nakryiko
2021-08-15  7:06 ` [PATCH v5 bpf-next 10/16] libbpf: add bpf_cookie support to bpf_link_create() API Andrii Nakryiko
2021-08-15  7:06 ` [PATCH v5 bpf-next 11/16] libbpf: add bpf_cookie to perf_event, kprobe, uprobe, and tp attach APIs Andrii Nakryiko
2021-08-15  7:06 ` [PATCH v5 bpf-next 12/16] selftests/bpf: test low-level perf BPF link API Andrii Nakryiko
2021-08-15  7:06 ` [PATCH v5 bpf-next 13/16] selftests/bpf: extract uprobe-related helpers into trace_helpers.{c,h} Andrii Nakryiko
2021-08-15  7:06 ` [PATCH v5 bpf-next 14/16] selftests/bpf: add bpf_cookie selftests for high-level APIs Andrii Nakryiko
2021-08-15  7:06 ` [PATCH v5 bpf-next 15/16] libbpf: add uprobe ref counter offset support for USDT semaphores Andrii Nakryiko
2021-08-15  7:06 ` Andrii Nakryiko [this message]
2021-08-16 23:10 ` [PATCH v5 bpf-next 00/16] BPF perf link and user-provided bpf_cookie patchwork-bot+netdevbpf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210815070609.987780-17-andrii@kernel.org \
    --to=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kernel-team@fb.com \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).