mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jiri Olsa <jolsa@kernel.org>
To: Oleg Nesterov <oleg@redhat.com>,
	Masami Hiramatsu <mhiramat@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Andrii Nakryiko <andrii@kernel.org>
Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-trace-kernel@vger.kernel.org, x86@kernel.org,
	Song Liu <songliubraving@fb.com>, Yonghong Song <yhs@fb.com>,
	John Fastabend <john.fastabend@gmail.com>,
	Hao Luo <haoluo@google.com>, Steven Rostedt <rostedt@goodmis.org>,
	Ingo Molnar <mingo@kernel.org>
Subject: [PATCH perf/core 08/11] selftests/bpf: Add uprobe multi context registers changes test
Date: Tue,  2 Sep 2025 16:35:01 +0200	[thread overview]
Message-ID: <20250902143504.1224726-9-jolsa@kernel.org> (raw)
In-Reply-To: <20250902143504.1224726-1-jolsa@kernel.org>

Adding test to check we can change common register values through
uprobe program.

It's x86_64 specific test.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
 .../selftests/bpf/prog_tests/bpf_cookie.c     |   1 +
 .../bpf/prog_tests/uprobe_multi_test.c        | 107 ++++++++++++++++++
 .../selftests/bpf/progs/uprobe_multi.c        |  24 ++++
 3 files changed, 132 insertions(+)

diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c b/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c
index 4a0670c056ba..2425fd26d1ea 100644
--- a/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c
+++ b/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c
@@ -6,6 +6,7 @@
 #include <sys/syscall.h>
 #include <sys/mman.h>
 #include <unistd.h>
+#include <asm/ptrace.h>
 #include <test_progs.h>
 #include <network_helpers.h>
 #include <bpf/btf.h>
diff --git a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
index 2ee17ef1dae2..012652b07399 100644
--- a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
+++ b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c
@@ -2,6 +2,7 @@
 
 #include <unistd.h>
 #include <pthread.h>
+#include <asm/ptrace.h>
 #include <test_progs.h>
 #include "uprobe_multi.skel.h"
 #include "uprobe_multi_bench.skel.h"
@@ -1340,6 +1341,111 @@ static void test_bench_attach_usdt(void)
 	printf("%s: detached in %7.3lfs\n", __func__, detach_delta);
 }
 
+#ifdef __x86_64__
+__naked __maybe_unused unsigned long uprobe_regs_change_trigger(void)
+{
+	asm volatile (
+		"ret\n"
+	);
+}
+
+static __naked void uprobe_regs_change(struct pt_regs *before, struct pt_regs *after)
+{
+	asm volatile (
+		"movq %r11,  48(%rdi)\n"
+		"movq %r10,  56(%rdi)\n"
+		"movq  %r9,  64(%rdi)\n"
+		"movq  %r8,  72(%rdi)\n"
+		"movq %rax,  80(%rdi)\n"
+		"movq %rcx,  88(%rdi)\n"
+		"movq %rdx,  96(%rdi)\n"
+		"movq %rsi, 104(%rdi)\n"
+		"movq %rdi, 112(%rdi)\n"
+
+		/* save 2nd argument */
+		"pushq %rsi\n"
+		"call uprobe_regs_change_trigger\n"
+
+		/* save  return value and load 2nd argument pointer to rax */
+		"pushq %rax\n"
+		"movq 8(%rsp), %rax\n"
+
+		"movq %r11,  48(%rax)\n"
+		"movq %r10,  56(%rax)\n"
+		"movq  %r9,  64(%rax)\n"
+		"movq  %r8,  72(%rax)\n"
+		"movq %rcx,  88(%rax)\n"
+		"movq %rdx,  96(%rax)\n"
+		"movq %rsi, 104(%rax)\n"
+		"movq %rdi, 112(%rax)\n"
+
+		/* restore return value and 2nd argument */
+		"pop %rax\n"
+		"pop %rsi\n"
+
+		"movq %rax,  80(%rsi)\n"
+		"ret\n"
+	);
+}
+
+static void unique_regs_common(void)
+{
+	struct pt_regs before = {}, after = {}, expected = {
+		.rax = 0xc0ffe,
+		.rcx = 0xbad,
+		.rdx = 0xdead,
+		.r8  = 0x8,
+		.r9  = 0x9,
+		.r10 = 0x10,
+		.r11 = 0x11,
+		.rdi = 0x12,
+		.rsi = 0x13,
+	};
+	LIBBPF_OPTS(bpf_uprobe_multi_opts, opts,
+		.unique = true,
+	);
+	struct uprobe_multi *skel;
+
+	skel = uprobe_multi__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "skel_open"))
+		return;
+
+	skel->bss->pid = getpid();
+	skel->bss->regs = expected;
+
+	skel->links.uprobe_change_regs = bpf_program__attach_uprobe_multi(
+						skel->progs.uprobe_change_regs,
+						-1, "/proc/self/exe",
+						"uprobe_regs_change_trigger",
+						&opts);
+	if (!ASSERT_OK_PTR(skel->links.uprobe_change_regs, "bpf_program__attach_uprobe_multi"))
+		goto cleanup;
+
+	uprobe_regs_change(&before, &after);
+
+	ASSERT_EQ(after.rax, expected.rax, "ax");
+	ASSERT_EQ(after.rcx, expected.rcx, "cx");
+	ASSERT_EQ(after.rdx, expected.rdx, "dx");
+	ASSERT_EQ(after.r8,  expected.r8,  "r8");
+	ASSERT_EQ(after.r9,  expected.r9,  "r9");
+	ASSERT_EQ(after.r10, expected.r10, "r10");
+	ASSERT_EQ(after.r11, expected.r11, "r11");
+	ASSERT_EQ(after.rdi, expected.rdi, "rdi");
+	ASSERT_EQ(after.rsi, expected.rsi, "rsi");
+
+cleanup:
+	uprobe_multi__destroy(skel);
+}
+
+static void test_unique(void)
+{
+	if (test__start_subtest("unique_regs_common"))
+		unique_regs_common();
+}
+#else
+static void test_unique(void) { }
+#endif
+
 void test_uprobe_multi_test(void)
 {
 	if (test__start_subtest("skel_api"))
@@ -1372,5 +1478,6 @@ void test_uprobe_multi_test(void)
 		test_session_cookie_skel_api();
 	if (test__start_subtest("session_cookie_recursive"))
 		test_session_recursive_skel_api();
+	test_unique();
 	RUN_TESTS(uprobe_multi_verifier);
 }
diff --git a/tools/testing/selftests/bpf/progs/uprobe_multi.c b/tools/testing/selftests/bpf/progs/uprobe_multi.c
index 44190efcdba2..f26f8b840985 100644
--- a/tools/testing/selftests/bpf/progs/uprobe_multi.c
+++ b/tools/testing/selftests/bpf/progs/uprobe_multi.c
@@ -141,3 +141,27 @@ int usdt_extra(struct pt_regs *ctx)
 	/* we need this one just to mix PID-filtered and global USDT probes */
 	return 0;
 }
+
+#if defined(__TARGET_ARCH_x86)
+struct pt_regs regs;
+
+SEC("uprobe.unique")
+int BPF_UPROBE(uprobe_change_regs)
+{
+	pid_t cur_pid = bpf_get_current_pid_tgid() >> 32;
+
+	if (cur_pid != pid)
+		return 0;
+
+	ctx->ax  = regs.ax;
+	ctx->cx  = regs.cx;
+	ctx->dx  = regs.dx;
+	ctx->r8  = regs.r8;
+	ctx->r9  = regs.r9;
+	ctx->r10 = regs.r10;
+	ctx->r11 = regs.r11;
+	ctx->di  = regs.di;
+	ctx->si  = regs.si;
+	return 0;
+}
+#endif
-- 
2.51.0


  parent reply	other threads:[~2025-09-02 14:36 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-02 14:34 [PATCH perf/core 00/11] uprobes: Add unique uprobe Jiri Olsa
2025-09-02 14:34 ` [PATCH perf/core 01/11] uprobes: Add unique flag to uprobe consumer Jiri Olsa
2025-09-02 15:11   ` Masami Hiramatsu
2025-09-03  6:35     ` Jiri Olsa
2025-09-03 10:49   ` Oleg Nesterov
2025-09-03 12:02     ` Jiri Olsa
2025-09-02 14:34 ` [PATCH perf/core 02/11] uprobes: Skip emulate/sstep on unique uprobe when ip is changed Jiri Olsa
2025-09-03 11:26   ` Oleg Nesterov
2025-09-03 18:20     ` Andrii Nakryiko
2025-09-03 19:57       ` Jiri Olsa
2025-09-03 19:50     ` Jiri Olsa
2025-09-04  8:49       ` Oleg Nesterov
2025-09-04 10:46         ` Jiri Olsa
2025-09-04 11:23           ` Oleg Nesterov
2025-09-04 15:01             ` Alexei Starovoitov
2025-09-04 18:06               ` Andrii Nakryiko
2025-09-04 18:55                 ` Oleg Nesterov
2025-09-04 19:42                   ` Andrii Nakryiko
2025-09-05  8:51                     ` Oleg Nesterov
2025-09-02 14:34 ` [PATCH perf/core 03/11] perf: Add support to attach standard unique uprobe Jiri Olsa
2025-09-02 16:13   ` Alexei Starovoitov
2025-09-03  3:32   ` kernel test robot
2025-09-03 11:59   ` Oleg Nesterov
2025-09-03 13:03     ` Jiri Olsa
2025-09-03 13:22       ` Oleg Nesterov
2025-09-02 14:34 ` [PATCH perf/core 04/11] bpf: Add support to attach uprobe_multi " Jiri Olsa
2025-09-02 16:11   ` Alexei Starovoitov
2025-09-03  6:35     ` Jiri Olsa
2025-09-03 15:32       ` Alexei Starovoitov
2025-09-03 19:55         ` Jiri Olsa
2025-09-02 14:34 ` [PATCH perf/core 05/11] bpf: Allow uprobe program to change context registers Jiri Olsa
2025-09-02 14:34 ` [PATCH perf/core 06/11] libbpf: Add support to attach unique uprobe_multi uprobe Jiri Olsa
2025-09-02 14:35 ` [PATCH perf/core 07/11] libbpf: Add support to attach generic unique uprobe Jiri Olsa
2025-09-03 18:26   ` Andrii Nakryiko
2025-09-02 14:35 ` Jiri Olsa [this message]
2025-09-02 14:35 ` [PATCH perf/core 09/11] selftests/bpf: Add uprobe multi context ip register change test Jiri Olsa
2025-09-02 14:35 ` [PATCH perf/core 10/11] selftests/bpf: Add uprobe multi unique attach test Jiri Olsa
2025-09-02 14:35 ` [PATCH perf/core 11/11] selftests/bpf: Add uprobe " Jiri Olsa

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=20250902143504.1224726-9-jolsa@kernel.org \
    --to=jolsa@kernel.org \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=mingo@kernel.org \
    --cc=oleg@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=songliubraving@fb.com \
    --cc=x86@kernel.org \
    --cc=yhs@fb.com \
    /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

all inboxes | Powered by JetHome®