Skip to contentSkip to conversation

uprobe: uretprobe speed up

PATCH Superseded Jiri Olsa 5 participants 12 messages Mar 27, 2024 – Apr 1, 2024

Subsystems (10)
x86 Kselftest BPF perf events +6 more subsystems

v1 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Jiri Olsa

author reviewer
#0 lore raw

cover uprobe: uretprobe speed up

hi,
as part of the effort on speeding up the uprobes [0] coming with return uprobe optimization by using syscall instead of the trap on the uretprobe trampoline.

The speed up depends on instruction type that uprobe is installed and depends on specific HW type, please check patch 1 for details.

I added extra bpf selftest to check on registers values before and after uretprobe to make sure the syscall saves all the needed regs.

changes from RFC:
- using __NR_uretprobe instead hardcoded syscall value [Andrii]
- using __builtin_memcpy to copy registers [Andrii]
- adding more comments
- Oleg provided the code that enabled the syscall to take the sysret path, which made it lot faster [Oleg]
- nocf_check attribute change was sent separatelly
- added test for checking up changed registers in uprobe consumer

thanks,
jirka

[0] https://lore.kernel.org/bpf/ZeCXHKJ--iYYbmLj@krava/
---
Jiri Olsa (3):
uprobe: Add uretprobe syscall to speed up return probe selftests/bpf: Add uretprobe syscall test for regs integrity selftests/bpf: Add uretprobe syscall test for regs changes

 arch/x86/entry/syscalls/syscall_64.tbl                  |   1 +
 arch/x86/kernel/uprobes.c                               |  83 ++++++++++++++++++++++++++++++++++
 include/linux/syscalls.h                                |   2 +
 include/linux/uprobes.h                                 |   2 +
 include/uapi/asm-generic/unistd.h                       |   5 ++-
 kernel/events/uprobes.c                                 |  18 ++++++--
 kernel/sys_ni.c                                         |   2 +
 tools/include/linux/compiler.h                          |   4 ++
 tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c   | 119 ++++++++++++++++++++++++++++++++++++++++++++++++-
 tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c | 230 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 tools/testing/selftests/bpf/progs/uprobe_syscall.c      |  15 +++++++
 11 files changed, 475 insertions(+), 6 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c
 create mode 100644 tools/testing/selftests/bpf/progs/uprobe_syscall.c
author reviewer
#1 lore raw

PATCH 1/3 uprobe: Add uretprobe syscall to speed up return probe

Adding uretprobe syscall instead of trap to speed up return probe.

At the moment the uretprobe setup/path is:

- install entry uprobe

- when the uprobe is hit, it overwrites probed function's return address on stack with address of the trampoline that contains breakpoint instruction

- the breakpoint trap code handles the uretprobe consumers execution and jumps back to original return address

This patch replaces the above trampoline's breakpoint instruction with new ureprobe syscall call. This syscall does exactly the same job as the trap with some more extra work:

- syscall trampoline must save original value for rax/r11/rcx registers on stack - rax is set to syscall number and r11/rcx are changed and used by syscall instruction

- the syscall code reads the original values of those registers and restore those values in task's pt_regs area

Even with the extra registers handling code the having uretprobes handled by syscalls shows speed improvement.

On Intel (11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz)

current:

    base           :   15.888 ± 0.033M/s
    uprobe-nop     :    3.016 ± 0.000M/s
    uprobe-push    :    2.832 ± 0.005M/s
    uprobe-ret     :    1.104 ± 0.000M/s
    uretprobe-nop  :    1.487 ± 0.000M/s
    uretprobe-push :    1.456 ± 0.000M/s
    uretprobe-ret  :    0.816 ± 0.001M/s

with the fix:

    base           :   15.116 ± 0.045M/s
    uprobe-nop     :    3.001 ± 0.045M/s
    uprobe-push    :    2.831 ± 0.004M/s
    uprobe-ret     :    1.102 ± 0.001M/s
    uretprobe-nop  :    1.969 ± 0.001M/s  < 32% speedup
    uretprobe-push :    1.905 ± 0.004M/s  < 30% speedup
    uretprobe-ret  :    0.933 ± 0.002M/s  < 14% speedup

On Amd (AMD Ryzen 7 5700U)

current:

    base           :    5.105 ± 0.003M/s
    uprobe-nop     :    1.552 ± 0.002M/s
    uprobe-push    :    1.408 ± 0.003M/s
    uprobe-ret     :    0.827 ± 0.001M/s
    uretprobe-nop  :    0.779 ± 0.001M/s
    uretprobe-push :    0.750 ± 0.001M/s
    uretprobe-ret  :    0.539 ± 0.001M/s

with the fix:

    base           :    5.119 ± 0.002M/s
    uprobe-nop     :    1.523 ± 0.003M/s
    uprobe-push    :    1.384 ± 0.003M/s
    uprobe-ret     :    0.826 ± 0.002M/s
    uretprobe-nop  :    0.866 ± 0.002M/s  < 11% speedup
    uretprobe-push :    0.826 ± 0.002M/s  < 10% speedup
    uretprobe-ret  :    0.581 ± 0.001M/s  <  7% speedup
---
 arch/x86/entry/syscalls/syscall_64.tbl |  1 +
 arch/x86/kernel/uprobes.c              | 83 ++++++++++++++++++++++++++
 include/linux/syscalls.h               |  2 +
 include/linux/uprobes.h                |  2 +
 include/uapi/asm-generic/unistd.h      |  5 +-
 kernel/events/uprobes.c                | 18 ++++--
 kernel/sys_ni.c                        |  2 +
 7 files changed, 108 insertions(+), 5 deletions(-)
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
index 7e8d46f4147f..af0a33ab06ee 100644
@@ -383,6 +383,7 @@
 459	common	lsm_get_self_attr	sys_lsm_get_self_attr
 460	common	lsm_set_self_attr	sys_lsm_set_self_attr
 461	common	lsm_list_modules	sys_lsm_list_modules
+462	64	uretprobe		sys_uretprobe
 
 #
 # Due to a historical design error, certain syscalls are numbered differently
arch/x86/kernel/uprobes.c +83 -0

95 lines not on this page · raw message

include/linux/syscalls.h +2 -0
index 77eb9b0e7685..db150794f89d 100644
@@ -972,6 +972,8 @@ asmlinkage long sys_lsm_list_modules(u64 *ids, size_t *size, u32 flags);
 /* x86 */
 asmlinkage long sys_ioperm(unsigned long from, unsigned long num, int on);
 
+asmlinkage long sys_uretprobe(void);
+
 /* pciconfig: alpha, arm, arm64, ia64, sparc */
 asmlinkage long sys_pciconfig_read(unsigned long bus, unsigned long dfn,
 				unsigned long off, unsigned long len,
include/linux/uprobes.h +2 -0
index f46e0ca0169c..a490146ad89d 100644
@@ -138,6 +138,8 @@ extern bool arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check c
 extern bool arch_uprobe_ignore(struct arch_uprobe *aup, struct pt_regs *regs);
 extern void arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr,
 					 void *src, unsigned long len);
+extern void uprobe_handle_trampoline(struct pt_regs *regs);
+extern void *arch_uprobe_trampoline(unsigned long *psize);
 #else /* !CONFIG_UPROBES */
 struct uprobes_state {
 };
include/uapi/asm-generic/unistd.h +4 -1
index 75f00965ab15..8a747cd1d735 100644
@@ -842,8 +842,11 @@ __SYSCALL(__NR_lsm_set_self_attr, sys_lsm_set_self_attr)
 #define __NR_lsm_list_modules 461
 __SYSCALL(__NR_lsm_list_modules, sys_lsm_list_modules)
 
+#define __NR_uretprobe 462
+__SYSCALL(__NR_uretprobe, sys_uretprobe)
+
 #undef __NR_syscalls
-#define __NR_syscalls 462
+#define __NR_syscalls 463
 
 /*
  * 32 bit systems traditionally used different
kernel/events/uprobes.c +14 -4
index 929e98c62965..90395b16bde0 100644
@@ -1474,11 +1474,20 @@ static int xol_add_vma(struct mm_struct *mm, struct xol_area *area)
 	return ret;
 }
 
+void * __weak arch_uprobe_trampoline(unsigned long *psize)
+{
+	static uprobe_opcode_t insn = UPROBE_SWBP_INSN;
+
+	*psize = UPROBE_SWBP_INSN_SIZE;
+	return &insn;
+}
+
 static struct xol_area *__create_xol_area(unsigned long vaddr)
 {
 	struct mm_struct *mm = current->mm;
-	uprobe_opcode_t insn = UPROBE_SWBP_INSN;
+	unsigned long insns_size;
 	struct xol_area *area;
+	void *insns;
 
 	area = kmalloc(sizeof(*area), GFP_KERNEL);
 	if (unlikely(!area))
@@ -1502,7 +1511,8 @@ static struct xol_area *__create_xol_area(unsigned long vaddr)
 	/* Reserve the 1st slot for get_trampoline_vaddr() */
 	set_bit(0, area->bitmap);
 	atomic_set(&area->slot_count, 1);
-	arch_uprobe_copy_ixol(area->pages[0], 0, &insn, UPROBE_SWBP_INSN_SIZE);
+	insns = arch_uprobe_trampoline(&insns_size);
+	arch_uprobe_copy_ixol(area->pages[0], 0, insns, insns_size);
 
 	if (!xol_add_vma(mm, area))
 		return area;
@@ -2123,7 +2133,7 @@ static struct return_instance *find_next_ret_chain(struct return_instance *ri)
 	return ri;
 }
 
-static void handle_trampoline(struct pt_regs *regs)
+void uprobe_handle_trampoline(struct pt_regs *regs)
 {
 	struct uprobe_task *utask;
 	struct return_instance *ri, *next;
@@ -2188,7 +2198,7 @@ static void handle_swbp(struct pt_regs *regs)
 
 	bp_vaddr = uprobe_get_swbp_addr(regs);
 	if (bp_vaddr == get_trampoline_vaddr())
-		return handle_trampoline(regs);
+		return uprobe_handle_trampoline(regs);
 
 	uprobe = find_active_uprobe(bp_vaddr, &is_swbp);
 	if (!uprobe) {
kernel/sys_ni.c +2 -0
index faad00cce269..be6195e0d078 100644
@@ -391,3 +391,5 @@ COND_SYSCALL(setuid16);
 
 /* restartable sequence */
 COND_SYSCALL(rseq);
+
+COND_SYSCALL(uretprobe);
Signature · 1 line
2.44.0
maintainer Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH bpf-next 1/3] uprobe: Add uretprobe syscall to speed up return probe

Quoted 5 lines · Jiri Olsa
Quoted 9 lines
---
 arch/x86/entry/syscalls/syscall_64.tbl |  1 +
 arch/x86/kernel/uprobes.c              | 83 ++++++++++++++++++++++++++
 include/linux/syscalls.h               |  2 +
 include/linux/uprobes.h                |  2 +
 include/uapi/asm-generic/unistd.h      |  5 +-
 kernel/events/uprobes.c                | 18 ++++--
 kernel/sys_ni.c                        |  2 +
 7 files changed, 108 insertions(+), 5 deletions(-)

Just in case,

Acked-by on v1 1/3
#3 lore raw

Re: [PATCH bpf-next 1/3] uprobe: Add uretprobe syscall to speed up return probe

In reply to Jiri Olsa
Quoted 76 lines · Jiri Olsa

Adding uretprobe syscall instead of trap to speed up return probe.

At the moment the uretprobe setup/path is:

- install entry uprobe

- when the uprobe is hit, it overwrites probed function's return address on stack with address of the trampoline that contains breakpoint instruction

- the breakpoint trap code handles the uretprobe consumers execution and jumps back to original return address

This patch replaces the above trampoline's breakpoint instruction with new ureprobe syscall call. This syscall does exactly the same job as the trap with some more extra work:

- syscall trampoline must save original value for rax/r11/rcx registers on stack - rax is set to syscall number and r11/rcx are changed and used by syscall instruction

- the syscall code reads the original values of those registers and restore those values in task's pt_regs area

Even with the extra registers handling code the having uretprobes handled by syscalls shows speed improvement.

On Intel (11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz)

current:

    base           :   15.888 ± 0.033M/s
    uprobe-nop     :    3.016 ± 0.000M/s
    uprobe-push    :    2.832 ± 0.005M/s
    uprobe-ret     :    1.104 ± 0.000M/s
    uretprobe-nop  :    1.487 ± 0.000M/s
    uretprobe-push :    1.456 ± 0.000M/s
    uretprobe-ret  :    0.816 ± 0.001M/s

with the fix:

    base           :   15.116 ± 0.045M/s
    uprobe-nop     :    3.001 ± 0.045M/s
    uprobe-push    :    2.831 ± 0.004M/s
    uprobe-ret     :    1.102 ± 0.001M/s
    uretprobe-nop  :    1.969 ± 0.001M/s  < 32% speedup
    uretprobe-push :    1.905 ± 0.004M/s  < 30% speedup
    uretprobe-ret  :    0.933 ± 0.002M/s  < 14% speedup

On Amd (AMD Ryzen 7 5700U)

current:

    base           :    5.105 ± 0.003M/s
    uprobe-nop     :    1.552 ± 0.002M/s
    uprobe-push    :    1.408 ± 0.003M/s
    uprobe-ret     :    0.827 ± 0.001M/s
    uretprobe-nop  :    0.779 ± 0.001M/s
    uretprobe-push :    0.750 ± 0.001M/s
    uretprobe-ret  :    0.539 ± 0.001M/s

with the fix:

    base           :    5.119 ± 0.002M/s
    uprobe-nop     :    1.523 ± 0.003M/s
    uprobe-push    :    1.384 ± 0.003M/s
    uprobe-ret     :    0.826 ± 0.002M/s
    uretprobe-nop  :    0.866 ± 0.002M/s  < 11% speedup
    uretprobe-push :    0.826 ± 0.002M/s  < 10% speedup
    uretprobe-ret  :    0.581 ± 0.001M/s  <  7% speedup
Quoted 10 lines
---
 arch/x86/entry/syscalls/syscall_64.tbl |  1 +
 arch/x86/kernel/uprobes.c              | 83 ++++++++++++++++++++++++++
 include/linux/syscalls.h               |  2 +
 include/linux/uprobes.h                |  2 +
 include/uapi/asm-generic/unistd.h      |  5 +-
 kernel/events/uprobes.c                | 18 ++++--
 kernel/sys_ni.c                        |  2 +
 7 files changed, 108 insertions(+), 5 deletions(-)

Great work and results, thanks!

[...]

author reviewer
#4 lore raw

PATCH 2/3 selftests/bpf: Add uretprobe syscall test for regs integrity

In reply to Jiri Olsa

Add uretprobe syscall test that compares register values before and after the uretprobe is hit. It also compares the register values seen from attached bpf program.

---
 tools/include/linux/compiler.h                |   4 +
 .../selftests/bpf/prog_tests/uprobe_syscall.c | 163 ++++++++++++++++++
 .../selftests/bpf/progs/uprobe_syscall.c      |  15 ++
 3 files changed, 182 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c
 create mode 100644 tools/testing/selftests/bpf/progs/uprobe_syscall.c
tools/include/linux/compiler.h +4 -0
index 8a63a9913495..6f7f22ac9da5 100644
@@ -62,6 +62,10 @@
 #define __nocf_check __attribute__((nocf_check))
 #endif
 
+#ifndef __naked
+#define __naked __attribute__((__naked__))
+#endif
+
 /* Are two types/vars the same type (ignoring qualifiers)? */
 #ifndef __same_type
 # define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c new file +163 -0

163 lines not on this page · raw message

tools/testing/selftests/bpf/progs/uprobe_syscall.c new file +15 -0
new file mode 100644
index 000000000000..3b7e0a63f659
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <string.h>
+
+struct pt_regs regs;
+
+char _license[] SEC("license") = "GPL";
+
+SEC("uretprobe//proc/self/exe:uprobe_syscall_arch_test")
+int uretprobe(struct pt_regs *ctx)
+{
+	__builtin_memcpy(&regs, ctx, sizeof(regs));
+	return 0;
+}
Signature · 1 line
2.44.0
#5 lore raw

Re: [PATCH bpf-next 2/3] selftests/bpf: Add uretprobe syscall test for regs integrity

Quoted 13 lines · Jiri Olsa

Add uretprobe syscall test that compares register values before and after the uretprobe is hit. It also compares the register values seen from attached bpf program.

---
 tools/include/linux/compiler.h                |   4 +
 .../selftests/bpf/prog_tests/uprobe_syscall.c | 163 ++++++++++++++++++
 .../selftests/bpf/progs/uprobe_syscall.c      |  15 ++
 3 files changed, 182 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c
 create mode 100644 tools/testing/selftests/bpf/progs/uprobe_syscall.c

[...]

tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c line 20

Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c new file +9 -0
+__naked unsigned long uprobe_syscall_arch_test(void)
+{
+       asm volatile (
+               "movq $0xdeadbeef, %rax\n"
+               "ret\n"
+       );
+}
+
+__naked void uprobe_syscall_arch(struct pt_regs *before, struct pt_regs *after)

don't you get compiler warnings for using __naked with explicit function arguments?

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c new file +4 -0
+{
+       asm volatile (
+               "movq %r15,   0(%rdi)\n"
+               "movq %r14,   8(%rdi)\n"

[...]

tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c line 107

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c new file +5 -0
+       err = uprobe_syscall__attach(skel);
+       if (!ASSERT_OK(err, "uprobe_syscall__attach"))
+               goto cleanup;
+
+       uprobe_syscall_arch(&before, &after);

uprobe_syscall_arch() doesn't really do an explicit syscall uretprobe, it should work for int3-based uretprobes as well? Let's call it something a bit more generic then?

Also, I think patch #1 will go through Masami's trace tree, right? But we can land selftests into bpf-next even before that, given they should work for both syscall and interrupt based uretprobes.

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c new file +4 -0
+
+       pp = (unsigned long *) &skel->bss->regs;
+       cnt = sizeof(before)/sizeof(*pb);
+

[...]

#6 lore raw

Re: [PATCH bpf-next 2/3] selftests/bpf: Add uretprobe syscall test for regs integrity

Quoted 44 lines · Andrii Nakryiko
Quoted 13 lines · Jiri Olsa

Add uretprobe syscall test that compares register values before and after the uretprobe is hit. It also compares the register values seen from attached bpf program.

---
 tools/include/linux/compiler.h                |   4 +
 .../selftests/bpf/prog_tests/uprobe_syscall.c | 163 ++++++++++++++++++
 .../selftests/bpf/progs/uprobe_syscall.c      |  15 ++
 3 files changed, 182 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c
 create mode 100644 tools/testing/selftests/bpf/progs/uprobe_syscall.c

[...]

tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c line 20

Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c new file +9 -0
+__naked unsigned long uprobe_syscall_arch_test(void)
+{
+       asm volatile (
+               "movq $0xdeadbeef, %rax\n"
+               "ret\n"
+       );
+}
+
+__naked void uprobe_syscall_arch(struct pt_regs *before, struct pt_regs *after)

don't you get compiler warnings for using __naked with explicit function arguments?

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c new file +4 -0
+{
+       asm volatile (
+               "movq %r15,   0(%rdi)\n"
+               "movq %r14,   8(%rdi)\n"

[...]

tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c line 107

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c new file +5 -0
+       err = uprobe_syscall__attach(skel);
+       if (!ASSERT_OK(err, "uprobe_syscall__attach"))
+               goto cleanup;
+
+       uprobe_syscall_arch(&before, &after);
Quoted 7 lines

uprobe_syscall_arch() doesn't really do an explicit syscall uretprobe, it should work for int3-based uretprobes as well? Let's call it something a bit more generic then?

Also, I think patch #1 will go through Masami's trace tree, right? But we can land selftests into bpf-next even before that, given they should work for both syscall and interrupt based uretprobes.

Regardless the decision:

Quoted 7 lines
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c new file +4 -0
+
+       pp = (unsigned long *) &skel->bss->regs;
+       cnt = sizeof(before)/sizeof(*pb);
+

[...]

#7 lore raw

Re: [PATCH bpf-next 2/3] selftests/bpf: Add uretprobe syscall test for regs integrity

In reply to Andrii Nakryiko
Quoted 17 lines · Andrii Nakryiko
Quoted 13 lines · Jiri Olsa

Add uretprobe syscall test that compares register values before and after the uretprobe is hit. It also compares the register values seen from attached bpf program.

---
 tools/include/linux/compiler.h                |   4 +
 .../selftests/bpf/prog_tests/uprobe_syscall.c | 163 ++++++++++++++++++
 .../selftests/bpf/progs/uprobe_syscall.c      |  15 ++
 3 files changed, 182 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c
 create mode 100644 tools/testing/selftests/bpf/progs/uprobe_syscall.c

[...]

Quoted 12 lines

tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c line 20

Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c new file +9 -0
+__naked unsigned long uprobe_syscall_arch_test(void)
+{
+       asm volatile (
+               "movq $0xdeadbeef, %rax\n"
+               "ret\n"
+       );
+}
+
+__naked void uprobe_syscall_arch(struct pt_regs *before, struct pt_regs *after)

don't you get compiler warnings for using __naked with explicit function arguments?

nope, both gcc and clang are silent

Quoted 14 lines
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c new file +4 -0
+{
+       asm volatile (
+               "movq %r15,   0(%rdi)\n"
+               "movq %r14,   8(%rdi)\n"

[...]

tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c line 107

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c new file +5 -0
+       err = uprobe_syscall__attach(skel);
+       if (!ASSERT_OK(err, "uprobe_syscall__attach"))
+               goto cleanup;
+
+       uprobe_syscall_arch(&before, &after);
Quoted 3 lines

uprobe_syscall_arch() doesn't really do an explicit syscall uretprobe, it should work for int3-based uretprobes as well? Let's call it something a bit more generic then?

ok, how about

uprobe_syscall_arch -> uretprobe_regs uprobe_syscall_arch_test -> uretprobe_regs_trigger

Quoted 4 lines

Also, I think patch #1 will go through Masami's trace tree, right? But we can land selftests into bpf-next even before that, given they should work for both syscall and interrupt based uretprobes.

hm, not sure.. I did not originally cc Masami/Steven :-\ adding now

Masami, could patch 1 go through:

https://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace.git
probes/for-next

thanks,
jirka

#8 lore raw

Re: [PATCH bpf-next 2/3] selftests/bpf: Add uretprobe syscall test for regs integrity

Hi Jiri,

Quoted 57 lines · On Sun, 31 Mar 2024 21:37:03 +0200 Jiri Olsa <[email protected]> wrote:
Quoted 17 lines · Andrii Nakryiko
Quoted 13 lines · Jiri Olsa

Add uretprobe syscall test that compares register values before and after the uretprobe is hit. It also compares the register values seen from attached bpf program.

---
 tools/include/linux/compiler.h                |   4 +
 .../selftests/bpf/prog_tests/uprobe_syscall.c | 163 ++++++++++++++++++
 .../selftests/bpf/progs/uprobe_syscall.c      |  15 ++
 3 files changed, 182 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c
 create mode 100644 tools/testing/selftests/bpf/progs/uprobe_syscall.c

[...]

Quoted 12 lines

tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c line 20

Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c new file +9 -0
+__naked unsigned long uprobe_syscall_arch_test(void)
+{
+       asm volatile (
+               "movq $0xdeadbeef, %rax\n"
+               "ret\n"
+       );
+}
+
+__naked void uprobe_syscall_arch(struct pt_regs *before, struct pt_regs *after)

don't you get compiler warnings for using __naked with explicit function arguments?

nope, both gcc and clang are silent

Quoted 14 lines
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c new file +4 -0
+{
+       asm volatile (
+               "movq %r15,   0(%rdi)\n"
+               "movq %r14,   8(%rdi)\n"

[...]

tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c line 107

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c new file +5 -0
+       err = uprobe_syscall__attach(skel);
+       if (!ASSERT_OK(err, "uprobe_syscall__attach"))
+               goto cleanup;
+
+       uprobe_syscall_arch(&before, &after);
Quoted 3 lines

uprobe_syscall_arch() doesn't really do an explicit syscall uretprobe, it should work for int3-based uretprobes as well? Let's call it something a bit more generic then?

ok, how about

uprobe_syscall_arch -> uretprobe_regs uprobe_syscall_arch_test -> uretprobe_regs_trigger

Quoted 1 line
Quoted 5 lines
Quoted 3 lines

Also, I think patch #1 will go through Masami's trace tree, right? But we can land selftests into bpf-next even before that, given they should work for both syscall and interrupt based uretprobes.

hm, not sure.. I did not originally cc Masami/Steven :-\ adding now

Would you mean this patch?

https://lore.kernel.org/all/[email protected]/

It seems you don't Cc/To me nor linux-kernel-trace ML.

Quoted 5 lines

Masami, could patch 1 go through:

https://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace.git
probes/for-next

Could you resend it to linux-kernel-trace ML? (only the first one?)

Thank you,

Quoted 3 lines

thanks,
jirka

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
author reviewer
#9 lore raw

PATCH 3/3 selftests/bpf: Add uretprobe syscall test for regs changes

In reply to Jiri Olsa

Adding test that creates uprobe consumer on uretprobe which changes some of the registers. Making sure the changed registers are propagated to the user space when the ureptobe syscall trampoline is used on x86_64.

To be able to do this, adding support to bpf_testmod to create uprobe via new attribute file:
/sys/kernel/bpf_testmod_uprobe

This file is expecting file offset and creates related uprobe on current process exe file and removes existing uprobe if offset is 0. The can be only single uprobe at any time.

The uprobe has specific consumer that changes registers used in ureprobe syscall trampoline and which are later checked in the test.

---
 .../selftests/bpf/bpf_testmod/bpf_testmod.c   | 119 +++++++++++++++++-
 .../selftests/bpf/prog_tests/uprobe_syscall.c |  67 ++++++++++
 2 files changed, 185 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +118 -1
index 39ad96a18123..163b6cc64b4f 100644
@@ -10,6 +10,7 @@
 #include <linux/percpu-defs.h>
 #include <linux/sysfs.h>
 #include <linux/tracepoint.h>
+#include <linux/namei.h>
 #include "bpf_testmod.h"
 #include "bpf_testmod_kfunc.h"
 
@@ -343,6 +344,118 @@ static struct bin_attribute bin_attr_bpf_testmod_file __ro_after_init = {
 	.write = bpf_testmod_test_write,
 };
 
+/* bpf_testmod_uprobe sysfs attribute is so far enabled for x86_64 only,
+ * please see test_uretprobe_regs_change test  */
+#ifdef __x86_64__
+
+static int
+uprobe_ret_handler(struct uprobe_consumer *self, unsigned long func,
+		   struct pt_regs *regs)
+
+{
+	regs->ax  = 0x12345678deadbeef;
+	regs->cx  = 0x87654321feebdaed;
+	regs->r11 = (u64) -1;
+	return true;
+}
+
+struct testmod_uprobe {
+	struct path path;
+	loff_t offset;
+	struct uprobe_consumer consumer;
+};
+
+static DEFINE_MUTEX(testmod_uprobe_mutex);
+
+static struct testmod_uprobe uprobe = {
+	.consumer.ret_handler = uprobe_ret_handler,
+};
+
+static int testmod_register_uprobe(loff_t offset)
+{
+	int err = -EBUSY;
+
+	if (uprobe.offset)
+		return -EBUSY;
+
+	mutex_lock(&testmod_uprobe_mutex);
+
+	if (uprobe.offset)
+		goto out;
+
+	err = kern_path("/proc/self/exe", LOOKUP_FOLLOW, &uprobe.path);
+	if (err)
+		goto out;
+
+	err = uprobe_register_refctr(d_real_inode(uprobe.path.dentry),
+				     offset, 0, &uprobe.consumer);
+	if (err)
+		path_put(&uprobe.path);
+	else
+		uprobe.offset = offset;
+
+out:
+	mutex_unlock(&testmod_uprobe_mutex);
+	return err;
+}
+
+static void testmod_unregister_uprobe(void)
+{
+	mutex_lock(&testmod_uprobe_mutex);
+
+	if (uprobe.offset) {
+		uprobe_unregister(d_real_inode(uprobe.path.dentry),
+				  uprobe.offset, &uprobe.consumer);
+		uprobe.offset = 0;
+	}
+
+	mutex_unlock(&testmod_uprobe_mutex);
+}
+
+static ssize_t
+bpf_testmod_uprobe_write(struct file *file, struct kobject *kobj,
+			 struct bin_attribute *bin_attr,
+			 char *buf, loff_t off, size_t len)
+{
+	unsigned long offset;
+	int err;
+
+	if (kstrtoul(buf, 0, &offset))
+		return -EINVAL;
+
+	if (offset)
+		err = testmod_register_uprobe(offset);
+	else
+		testmod_unregister_uprobe();
+
+	return err ?: strlen(buf);
+}
+
+static struct bin_attribute bin_attr_bpf_testmod_uprobe_file __ro_after_init = {
+	.attr = { .name = "bpf_testmod_uprobe", .mode = 0666, },
+	.write = bpf_testmod_uprobe_write,
+};
+
+static int register_bpf_testmod_uprobe(void)
+{
+	return sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_uprobe_file);
+}
+
+static void unregister_bpf_testmod_uprobe(void)
+{
+	testmod_unregister_uprobe();
+	sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_uprobe_file);
+}
+
+#else
+static int register_bpf_testmod_uprobe(void)
+{
+	return 0;
+}
+
+static void unregister_bpf_testmod_uprobe(void) { }
+#endif
+
 BTF_KFUNCS_START(bpf_testmod_common_kfunc_ids)
 BTF_ID_FLAGS(func, bpf_iter_testmod_seq_new, KF_ITER_NEW)
 BTF_ID_FLAGS(func, bpf_iter_testmod_seq_next, KF_ITER_NEXT | KF_RET_NULL)
@@ -650,7 +763,10 @@ static int bpf_testmod_init(void)
 		return ret;
 	if (bpf_fentry_test1(0) < 0)
 		return -EINVAL;
-	return sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
+	ret = sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
+	if (ret < 0)
+		return ret;
+	return register_bpf_testmod_uprobe();
 }
 
 static void bpf_testmod_exit(void)
@@ -664,6 +780,7 @@ static void bpf_testmod_exit(void)
 		msleep(20);
 
 	sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
+	unregister_bpf_testmod_uprobe();
 }
 
 module_init(bpf_testmod_init);
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +67 -0

82 lines not on this page · raw message

Signature · 1 line
2.44.0
Acked-by on v1 3/3
#10 lore raw

Re: [PATCH bpf-next 3/3] selftests/bpf: Add uretprobe syscall test for regs changes

Quoted 13 lines · Jiri Olsa

Adding test that creates uprobe consumer on uretprobe which changes some of the registers. Making sure the changed registers are propagated to the user space when the ureptobe syscall trampoline is used on x86_64.

To be able to do this, adding support to bpf_testmod to create uprobe via new attribute file:
/sys/kernel/bpf_testmod_uprobe

This file is expecting file offset and creates related uprobe on current process exe file and removes existing uprobe if offset is 0. The can be only single uprobe at any time.

Quoted 9 lines

The uprobe has specific consumer that changes registers used in ureprobe syscall trampoline and which are later checked in the test.

---
 .../selftests/bpf/bpf_testmod/bpf_testmod.c   | 119 +++++++++++++++++-
 .../selftests/bpf/prog_tests/uprobe_syscall.c |  67 ++++++++++
 2 files changed, 185 insertions(+), 1 deletion(-)

LGTM:

[...]

tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c static int bpf_testmod_init(void) · line 769

Quoted 12 lines

BTF_KFUNCS_START(bpf_testmod_common_kfunc_ids) BTF_ID_FLAGS(func, bpf_iter_testmod_seq_new, KF_ITER_NEW) BTF_ID_FLAGS(func, bpf_iter_testmod_seq_next, KF_ITER_NEXT | KF_RET_NULL)

tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +4 -1
@@ -650,7 +763,10 @@ static int bpf_testmod_init(void)
                return ret;
        if (bpf_fentry_test1(0) < 0)
                return -EINVAL;
-       return sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
+       ret = sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
+       if (ret < 0)
+               return ret;
+       return register_bpf_testmod_uprobe();

nit: keep using the same pattern to make adding more actions easier in the future:

ret = register_bpf_testmod_uprobe();
if (ret < 0)

    return ret;

return 0;

Quoted 10 lines
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +1 -0
 }
 
 static void bpf_testmod_exit(void)
@@ -664,6 +780,7 @@ static void bpf_testmod_exit(void)
                msleep(20);
 
        sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
+       unregister_bpf_testmod_uprobe();
 }
 

[...]

#11 lore raw

Re: [PATCH bpf-next 3/3] selftests/bpf: Add uretprobe syscall test for regs changes

Quoted 46 lines · Andrii Nakryiko
Quoted 13 lines · Jiri Olsa

Adding test that creates uprobe consumer on uretprobe which changes some of the registers. Making sure the changed registers are propagated to the user space when the ureptobe syscall trampoline is used on x86_64.

To be able to do this, adding support to bpf_testmod to create uprobe via new attribute file:
/sys/kernel/bpf_testmod_uprobe

This file is expecting file offset and creates related uprobe on current process exe file and removes existing uprobe if offset is 0. The can be only single uprobe at any time.

Quoted 9 lines

The uprobe has specific consumer that changes registers used in ureprobe syscall trampoline and which are later checked in the test.

---
 .../selftests/bpf/bpf_testmod/bpf_testmod.c   | 119 +++++++++++++++++-
 .../selftests/bpf/prog_tests/uprobe_syscall.c |  67 ++++++++++
 2 files changed, 185 insertions(+), 1 deletion(-)

LGTM:

[...]

tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c static int bpf_testmod_init(void) · line 769

Quoted 12 lines

BTF_KFUNCS_START(bpf_testmod_common_kfunc_ids) BTF_ID_FLAGS(func, bpf_iter_testmod_seq_new, KF_ITER_NEW) BTF_ID_FLAGS(func, bpf_iter_testmod_seq_next, KF_ITER_NEXT | KF_RET_NULL)

tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +4 -1
@@ -650,7 +763,10 @@ static int bpf_testmod_init(void)
                return ret;
        if (bpf_fentry_test1(0) < 0)
                return -EINVAL;
-       return sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
+       ret = sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
+       if (ret < 0)
+               return ret;
+       return register_bpf_testmod_uprobe();

nit: keep using the same pattern to make adding more actions easier in the future:

Quoted 4 lines

ret = register_bpf_testmod_uprobe();
if (ret < 0)

    return ret;

return 0;

ok, will change

thanks,
jirka

Quoted 13 lines
Quoted 11 lines
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +1 -0
 }
 
 static void bpf_testmod_exit(void)
@@ -664,6 +780,7 @@ static void bpf_testmod_exit(void)
                msleep(20);
 
        sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
+       unregister_bpf_testmod_uprobe();
 }
 

[...]