Skip to contentSkip to conversation

uprobes/x86: Fix red zone issue for optimized uprobes

PATCH Superseded Jiri Olsa 5 participants 28 messages May 18 – May 19

Subsystems (11)
x86 Kselftest BPF perf events +7 more subsystems

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Jiri Olsa

author reviewer
#0 lore raw

v2 cover uprobes/x86: Fix red zone issue for optimized uprobes

hi,
Andrii reported an issue with optimized uprobes [1] that can clobber redzone area with call instruction storing return address on stack where user code may keep temporary data without adjusting rsp.

Fixing this by moving the optimized uprobes on top of 10-bytes nop instruction, so we can squeeze another instruction to escape the redzone area before doing the call.

Note we need upstream update first for patch 3 (github.com/libbpf/usdt), if we decide to take this change.

thanks,
jirka

v1: https://lore.kernel.org/bpf/[email protected]/

v2 changes:
- several selftest fixes [sashiko]
- consolidate is_lea_insn and is_call_insn insto single check [Jakub Sitnicki]
- use proper mm_struct object in __in_uprobe_trampoline check [sashiko]
- allow to copy uprobe trampolines vma objects on fork [sashiko]
- change uprobe syscall detection error from -ENXIO to -EPROTO [Andrii]
- added fork/clone tests
- I kept the selftest changes and nop5->nop10 changes in separate commits for easier review, we can squash them later if we want to keep bisect working properly

[1] https://lore.kernel.org/bpf/[email protected]/
---
Andrii Nakryiko (1):
selftests/bpf: Add tests for uprobe nop10 red zone clobbering

Jiri Olsa (10):
uprobes/x86: Use proper mm_struct in __in_uprobe_trampoline uprobes/x86: Allow to copy uprobe trampolines on fork uprobes/x86: Move optimized uprobe from nop5 to nop10
libbpf: Change has_nop_combo to work on top of nop10
libbpf: Detect uprobe syscall with new error selftests/bpf: Emit nop,nop10 instructions combo for x86_64 arch selftests/bpf: Change uprobe syscall tests to use nop10 selftests/bpf: Change uprobe/usdt trigger bench code to use nop10 selftests/bpf: Add reattach tests for uprobe syscall selftests/bpf: Add tests for forked/cloned optimized uprobes

 arch/x86/kernel/uprobes.c                               | 144 ++++++++++++++++++++++------------
 tools/lib/bpf/features.c                                |   4 +-
 tools/lib/bpf/usdt.c                                    |  16 ++--
 tools/testing/selftests/bpf/bench.c                     |  20 ++---
 tools/testing/selftests/bpf/benchs/bench_trigger.c      |  38 ++++-----
 tools/testing/selftests/bpf/benchs/run_bench_uprobes.sh |   2 +-
 tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c | 307 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
 tools/testing/selftests/bpf/prog_tests/usdt.c           |  74 +++++++++++++++---
 tools/testing/selftests/bpf/progs/test_usdt.c           |  25 ++++++
 tools/testing/selftests/bpf/usdt.h                      |   2 +-
 tools/testing/selftests/bpf/usdt_2.c                    |  15 +++-
 11 files changed, 524 insertions(+), 123 deletions(-)
author reviewer
#1 lore raw

PATCH v2 1/11 uprobes/x86: Use proper mm_struct in __in_uprobe_trampoline

In the unregister path we use __in_uprobe_trampoline check with current->mm for the VMA lookup, which is wrong, because we are in the tracer context, not the traced process.

Add mm_struct pointer argument to __in_uprobe_trampoline and changing related callers to pass proper mm_struct pointer.

Fixes ba2bfc97b462 ("uprobes/x86: Add support to optimize uprobes")
---
 arch/x86/kernel/uprobes.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
arch/x86/kernel/uprobes.c +7 -7
index ebb1baf1eb1d..2be6707e3320 100644
@@ -761,9 +761,9 @@ void arch_uprobe_clear_state(struct mm_struct *mm)
 		destroy_uprobe_trampoline(tramp);
 }
 
-static bool __in_uprobe_trampoline(unsigned long ip)
+static bool __in_uprobe_trampoline(struct mm_struct *mm, unsigned long ip)
 {
-	struct vm_area_struct *vma = vma_lookup(current->mm, ip);
+	struct vm_area_struct *vma = vma_lookup(mm, ip);
 
 	return vma && vma_is_special_mapping(vma, &tramp_mapping);
 }
@@ -776,14 +776,14 @@ static bool in_uprobe_trampoline(unsigned long ip)
 
 	rcu_read_lock();
 	if (mmap_lock_speculate_try_begin(mm, &seq)) {
-		found = __in_uprobe_trampoline(ip);
+		found = __in_uprobe_trampoline(mm, ip);
 		retry = mmap_lock_speculate_retry(mm, seq);
 	}
 	rcu_read_unlock();
 
 	if (retry) {
 		mmap_read_lock(mm);
-		found = __in_uprobe_trampoline(ip);
+		found = __in_uprobe_trampoline(mm, ip);
 		mmap_read_unlock(mm);
 	}
 	return found;
@@ -1044,7 +1044,7 @@ static int copy_from_vaddr(struct mm_struct *mm, unsigned long vaddr, void *dst,
 	return 0;
 }
 
-static bool __is_optimized(uprobe_opcode_t *insn, unsigned long vaddr)
+static bool __is_optimized(struct mm_struct *mm, uprobe_opcode_t *insn, unsigned long vaddr)
 {
 	struct __packed __arch_relative_insn {
 		u8 op;
@@ -1053,7 +1053,7 @@ static bool __is_optimized(uprobe_opcode_t *insn, unsigned long vaddr)
 
 	if (!is_call_insn(insn))
 		return false;
-	return __in_uprobe_trampoline(vaddr + 5 + call->raddr);
+	return __in_uprobe_trampoline(mm, vaddr + 5 + call->raddr);
 }
 
 static int is_optimized(struct mm_struct *mm, unsigned long vaddr)
@@ -1064,7 +1064,7 @@ static int is_optimized(struct mm_struct *mm, unsigned long vaddr)
 	err = copy_from_vaddr(mm, vaddr, &insn, 5);
 	if (err)
 		return err;
-	return __is_optimized((uprobe_opcode_t *)&insn, vaddr);
+	return __is_optimized(mm, (uprobe_opcode_t *)&insn, vaddr);
 }
 
 static bool should_optimize(struct arch_uprobe *auprobe)
Signature · 1 line
2.53.0
author reviewer
#2 lore raw

PATCH v2 2/11 uprobes/x86: Allow to copy uprobe trampolines on fork

In reply to Jiri Olsa

When we do fork or clone without CLONE_VM the new process won't have uprobe trampoline vma objects and at the same time it will have optimized code calling the trampolines.

Fixing this by allowing vma uprobe trampoline objects to be copied on fork to the new process.

Fixes ba2bfc97b462 ("uprobes/x86: Add support to optimize uprobes")
---
 arch/x86/kernel/uprobes.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/uprobes.c +1 -1
index 2be6707e3320..37faf038be33 100644
@@ -702,7 +702,7 @@ static struct uprobe_trampoline *create_uprobe_trampoline(unsigned long vaddr)
 
 	tramp->vaddr = vaddr;
 	vma = _install_special_mapping(mm, tramp->vaddr, PAGE_SIZE,
-				VM_READ|VM_EXEC|VM_MAYEXEC|VM_MAYREAD|VM_DONTCOPY|VM_IO,
+				VM_READ|VM_EXEC|VM_MAYEXEC|VM_MAYREAD|VM_IO,
 				&tramp_mapping);
 	if (IS_ERR(vma)) {
 		kfree(tramp);
Signature · 1 line
2.53.0
#3 lore raw

Re: [PATCHv2 02/11] uprobes/x86: Allow to copy uprobe trampolines on fork

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Critical] __in_uprobe_trampoline uses current->mm instead of the target mm_struct, causing kernel…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] __in_uprobe_trampoline uses current->mm instead of the target mm_struct, causing kernel crashes and instruction stream corruption during uprobe_unregister.
- [High] Removing VM_DONTCOPY without synchronizing the uprobes state causes a linear VMA leak across fork chains.
--

commit be0e6254bd9df4f56adce23d7a5e24a78c286291
Author: Jiri Olsa <[email protected]> uprobes/x86: Allow to copy uprobe trampolines on fork

This commit allows vma uprobe trampoline objects to be copied on fork to the new process by removing the VM_DONTCOPY flag.

arch/x86/kernel/uprobes.c static struct uprobe_trampoline *create_uprobe_trampoline(unsigned long vaddr) · line 705

Quoted 9 lines
arch/x86/kernel/uprobes.c +1 -1
@@ -702,7 +702,7 @@ static struct uprobe_trampoline *create_uprobe_trampoline(unsigned long vaddr)
 
 	tramp->vaddr = vaddr;
 	vma = _install_special_mapping(mm, tramp->vaddr, PAGE_SIZE,
-				VM_READ|VM_EXEC|VM_MAYEXEC|VM_MAYREAD|VM_DONTCOPY|VM_IO,
+				VM_READ|VM_EXEC|VM_MAYEXEC|VM_MAYREAD|VM_IO,

Does removing VM_DONTCOPY without synchronizing the uprobes state cause a linear VMA leak across fork chains?

When dup_mmap() copies the uprobe trampoline VMA to the child process during fork(), the child's tracking list (mm->uprobes_state.head_tramps) is initialized as completely empty via mm_init_uprobes_state(), meaning the parent's state is not duplicated.

When the child process executes a uprobe, arch_uprobe_optimize() calls get_uprobe_trampoline(). Because head_tramps is empty, it fails to find the VMA it already inherited. It then falls back to create_uprobe_trampoline(), which calls find_nearest_trampoline(). Since the inherited VMA is already mapped, find_nearest_trampoline() finds a new unmapped area and installs a second duplicate trampoline VMA.

In a deep fork chain, each generation inherits all previous VMAs, has an empty head_tramps list, and creates a new VMA. Can this cause a linear accumulation of redundant VMAs, eventually exhausting vm.max_map_count and virtual memory?

As a related question about the uprobe trampolines introduced in the preceding commit ba2bfc97b462, does __in_uprobe_trampoline() safely handle unregistered uprobes?

When a uprobe is unregistered (e.g., via bpf_uprobe_unregister in a workqueue or by a different process like perf), set_orig_insn() calls is_optimized() to check if the uprobe is optimized. If is_optimized() internally calls __in_uprobe_trampoline() which hardcodes current->mm for the VMA lookup, does it completely ignore the passed mm parameter?

If unregistered asynchronously from a kernel thread, current->mm is NULL. Can this cause a deterministic NULL pointer dereference in vma_lookup() and crash the kernel?

Additionally, if unregistered by a different process, would vma_lookup() search the wrong mm, return false, and cause set_orig_insn() to incorrectly assume the instruction is not optimized? Could this lead to replacing only the first byte of the 5-byte CALL instruction with the original opcode, leaving the 4-byte relative offset as garbage and crashing the traced process when it executes the corrupted instruction?

Quoted 3 lines
arch/x86/kernel/uprobes.c +0 -0
 				&tramp_mapping);
 	if (IS_ERR(vma)) {
 		kfree(tramp);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#4 lore raw

Re: [PATCHv2 02/11] uprobes/x86: Allow to copy uprobe trampolines on fork

Quoted 37 lines · On Mon, May 18, 2026 at 11:42:08AM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] __in_uprobe_trampoline uses current->mm instead of the target mm_struct, causing kernel crashes and instruction stream corruption during uprobe_unregister.
- [High] Removing VM_DONTCOPY without synchronizing the uprobes state causes a linear VMA leak across fork chains.
--

commit be0e6254bd9df4f56adce23d7a5e24a78c286291
Author: Jiri Olsa <[email protected]> uprobes/x86: Allow to copy uprobe trampolines on fork

This commit allows vma uprobe trampoline objects to be copied on fork to the new process by removing the VM_DONTCOPY flag.

arch/x86/kernel/uprobes.c static struct uprobe_trampoline *create_uprobe_trampoline(unsigned long vaddr) · line 705

Quoted 9 lines
arch/x86/kernel/uprobes.c +1 -1
@@ -702,7 +702,7 @@ static struct uprobe_trampoline *create_uprobe_trampoline(unsigned long vaddr)
 
 	tramp->vaddr = vaddr;
 	vma = _install_special_mapping(mm, tramp->vaddr, PAGE_SIZE,
-				VM_READ|VM_EXEC|VM_MAYEXEC|VM_MAYREAD|VM_DONTCOPY|VM_IO,
+				VM_READ|VM_EXEC|VM_MAYEXEC|VM_MAYREAD|VM_IO,

Does removing VM_DONTCOPY without synchronizing the uprobes state cause a linear VMA leak across fork chains?

When dup_mmap() copies the uprobe trampoline VMA to the child process during fork(), the child's tracking list (mm->uprobes_state.head_tramps) is initialized as completely empty via mm_init_uprobes_state(), meaning the parent's state is not duplicated.

When the child process executes a uprobe, arch_uprobe_optimize() calls get_uprobe_trampoline(). Because head_tramps is empty, it fails to find the VMA it already inherited. It then falls back to create_uprobe_trampoline(), which calls find_nearest_trampoline(). Since the inherited VMA is already mapped, find_nearest_trampoline() finds a new unmapped area and installs a second duplicate trampoline VMA.

Quoted 4 lines

In a deep fork chain, each generation inherits all previous VMAs, has an empty head_tramps list, and creates a new VMA. Can this cause a linear accumulation of redundant VMAs, eventually exhausting vm.max_map_count and virtual memory?

I don't think it's big problem, because it's unlikely you'll insert extra uprobes to child process.. but perhaps we could go away with struct uprobe_trampoline object and operate directly on top of vma objects, it could event make code easier, will try

Quoted 10 lines

As a related question about the uprobe trampolines introduced in the preceding commit ba2bfc97b462, does __in_uprobe_trampoline() safely handle unregistered uprobes?

When a uprobe is unregistered (e.g., via bpf_uprobe_unregister in a workqueue or by a different process like perf), set_orig_insn() calls is_optimized() to check if the uprobe is optimized. If is_optimized() internally calls __in_uprobe_trampoline() which hardcodes current->mm for the VMA lookup, does it completely ignore the passed mm parameter?

__in_uprobe_trampoline has no longer hardcoded current->mm, check previous patch:

uprobes/x86: Use proper mm_struct in __in_uprobe_trampoline

jirka

Quoted 18 lines

If unregistered asynchronously from a kernel thread, current->mm is NULL. Can this cause a deterministic NULL pointer dereference in vma_lookup() and crash the kernel?

Additionally, if unregistered by a different process, would vma_lookup() search the wrong mm, return false, and cause set_orig_insn() to incorrectly assume the instruction is not optimized? Could this lead to replacing only the first byte of the 5-byte CALL instruction with the original opcode, leaving the 4-byte relative offset as garbage and crashing the traced process when it executes the corrupted instruction?

Quoted 3 lines
arch/x86/kernel/uprobes.c +0 -0
 				&tramp_mapping);
 	if (IS_ERR(vma)) {
 		kfree(tramp);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#5 lore raw

Re: [PATCHv2 02/11] uprobes/x86: Allow to copy uprobe trampolines on fork

Quoted 43 lines · Jiri Olsa
Quoted 37 lines · On Mon, May 18, 2026 at 11:42:08AM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] __in_uprobe_trampoline uses current->mm instead of the target mm_struct, causing kernel crashes and instruction stream corruption during uprobe_unregister.
- [High] Removing VM_DONTCOPY without synchronizing the uprobes state causes a linear VMA leak across fork chains.
--

commit be0e6254bd9df4f56adce23d7a5e24a78c286291
Author: Jiri Olsa <[email protected]> uprobes/x86: Allow to copy uprobe trampolines on fork

This commit allows vma uprobe trampoline objects to be copied on fork to the new process by removing the VM_DONTCOPY flag.

arch/x86/kernel/uprobes.c static struct uprobe_trampoline *create_uprobe_trampoline(unsigned long vaddr) · line 705

Quoted 9 lines
arch/x86/kernel/uprobes.c +1 -1
@@ -702,7 +702,7 @@ static struct uprobe_trampoline *create_uprobe_trampoline(unsigned long vaddr)
 
 	tramp->vaddr = vaddr;
 	vma = _install_special_mapping(mm, tramp->vaddr, PAGE_SIZE,
-				VM_READ|VM_EXEC|VM_MAYEXEC|VM_MAYREAD|VM_DONTCOPY|VM_IO,
+				VM_READ|VM_EXEC|VM_MAYEXEC|VM_MAYREAD|VM_IO,

Does removing VM_DONTCOPY without synchronizing the uprobes state cause a linear VMA leak across fork chains?

When dup_mmap() copies the uprobe trampoline VMA to the child process during fork(), the child's tracking list (mm->uprobes_state.head_tramps) is initialized as completely empty via mm_init_uprobes_state(), meaning the parent's state is not duplicated.

When the child process executes a uprobe, arch_uprobe_optimize() calls get_uprobe_trampoline(). Because head_tramps is empty, it fails to find the VMA it already inherited. It then falls back to create_uprobe_trampoline(), which calls find_nearest_trampoline(). Since the inherited VMA is already mapped, find_nearest_trampoline() finds a new unmapped area and installs a second duplicate trampoline VMA.

Quoted 4 lines

In a deep fork chain, each generation inherits all previous VMAs, has an empty head_tramps list, and creates a new VMA. Can this cause a linear accumulation of redundant VMAs, eventually exhausting vm.max_map_count and virtual memory?

Quoted 4 lines

I don't think it's big problem, because it's unlikely you'll insert extra uprobes to child process.. but perhaps we could go away with struct uprobe_trampoline object and operate directly on top of vma objects, it could event make code easier, will try

ok looks like we could go without struct uprobe_trampoline layer (below), that should address the comment, will do that in next version

jirka

---

arch/x86/kernel/uprobes.c +16 -72
index 2be6707e3320..c87079e53946 100644
@@ -631,11 +631,6 @@ static struct vm_special_mapping tramp_mapping = {
 	.pages  = tramp_mapping_pages,
 };
 
-struct uprobe_trampoline {
-	struct hlist_node	node;
-	unsigned long		vaddr;
-};
-
 static bool is_reachable_by_call(unsigned long vtramp, unsigned long vaddr)
 {
 	long delta = (long)(vaddr + 5 - vtramp);
@@ -682,12 +677,10 @@ static unsigned long find_nearest_trampoline(unsigned long vaddr)
 	return high_tramp;
 }
 
-static struct uprobe_trampoline *create_uprobe_trampoline(unsigned long vaddr)
+static struct vm_area_struct *create_uprobe_trampoline(unsigned long vaddr)
 {
 	struct pt_regs *regs = task_pt_regs(current);
 	struct mm_struct *mm = current->mm;
-	struct uprobe_trampoline *tramp;
-	struct vm_area_struct *vma;
 
 	if (!user_64bit_mode(regs))
 		return NULL;
@@ -696,69 +689,26 @@ static struct uprobe_trampoline *create_uprobe_trampoline(unsigned long vaddr)
 	if (IS_ERR_VALUE(vaddr))
 		return NULL;
 
-	tramp = kzalloc_obj(*tramp);
-	if (unlikely(!tramp))
-		return NULL;
-
-	tramp->vaddr = vaddr;
-	vma = _install_special_mapping(mm, tramp->vaddr, PAGE_SIZE,
+	return _install_special_mapping(mm, vaddr, PAGE_SIZE,
 				VM_READ|VM_EXEC|VM_MAYEXEC|VM_MAYREAD|VM_DONTCOPY|VM_IO,
 				&tramp_mapping);
-	if (IS_ERR(vma)) {
-		kfree(tramp);
-		return NULL;
-	}
-	return tramp;
 }
 
-static struct uprobe_trampoline *get_uprobe_trampoline(unsigned long vaddr, bool *new)
+static struct vm_area_struct *get_uprobe_trampoline(unsigned long vaddr)
 {
-	struct uprobes_state *state = &current->mm->uprobes_state;
-	struct uprobe_trampoline *tramp = NULL;
+	VMA_ITERATOR(vmi, current->mm, 0);
+	struct vm_area_struct *vma;
 
 	if (vaddr > TASK_SIZE || vaddr < PAGE_SIZE)
 		return NULL;
 
-	hlist_for_each_entry(tramp, &state->head_tramps, node) {
-		if (is_reachable_by_call(tramp->vaddr, vaddr)) {
-			*new = false;
-			return tramp;
-		}
+	for_each_vma(vmi, vma) {
+		if (!vma_is_special_mapping(vma, &tramp_mapping))
+			continue;
+		if (is_reachable_by_call(vma->vm_start, vaddr))
+			return vma;
 	}
-
-	tramp = create_uprobe_trampoline(vaddr);
-	if (!tramp)
-		return NULL;
-
-	*new = true;
-	hlist_add_head(&tramp->node, &state->head_tramps);
-	return tramp;
-}
-
-static void destroy_uprobe_trampoline(struct uprobe_trampoline *tramp)
-{
-	/*
-	 * We do not unmap and release uprobe trampoline page itself,
-	 * because there's no easy way to make sure none of the threads
-	 * is still inside the trampoline.
-	 */
-	hlist_del(&tramp->node);
-	kfree(tramp);
-}
-
-void arch_uprobe_init_state(struct mm_struct *mm)
-{
-	INIT_HLIST_HEAD(&mm->uprobes_state.head_tramps);
-}
-
-void arch_uprobe_clear_state(struct mm_struct *mm)
-{
-	struct uprobes_state *state = &mm->uprobes_state;
-	struct uprobe_trampoline *tramp;
-	struct hlist_node *n;
-
-	hlist_for_each_entry_safe(tramp, n, &state->head_tramps, node)
-		destroy_uprobe_trampoline(tramp);
+	return create_uprobe_trampoline(vaddr);
 }
 
 static bool __in_uprobe_trampoline(struct mm_struct *mm, unsigned long ip)
@@ -1111,21 +1061,15 @@ int set_orig_insn(struct arch_uprobe *auprobe, struct vm_area_struct *vma,
 static int __arch_uprobe_optimize(struct arch_uprobe *auprobe, struct mm_struct *mm,
 				  unsigned long vaddr)
 {
-	struct uprobe_trampoline *tramp;
-	struct vm_area_struct *vma;
-	bool new = false;
-	int err = 0;
+	struct vm_area_struct *vma, *tramp;
 
 	vma = find_vma(mm, vaddr);
 	if (!vma)
 		return -EINVAL;
-	tramp = get_uprobe_trampoline(vaddr, &new);
-	if (!tramp)
-		return -EINVAL;
-	err = swbp_optimize(auprobe, vma, vaddr, tramp->vaddr);
-	if (WARN_ON_ONCE(err) && new)
-		destroy_uprobe_trampoline(tramp);
-	return err;
+	tramp = get_uprobe_trampoline(vaddr);
+	if (IS_ERR_OR_NULL(tramp))
+		return PTR_ERR(tramp);
+	return WARN_ON_ONCE(swbp_optimize(auprobe, vma, vaddr, tramp->vm_start));
 }
 
 void arch_uprobe_optimize(struct arch_uprobe *auprobe, unsigned long vaddr)
include/linux/uprobes.h +0 -2
index f548fea2adec..43d950f444cf 100644
@@ -238,8 +238,6 @@ extern void uprobe_handle_trampoline(struct pt_regs *regs);
 extern void *arch_uretprobe_trampoline(unsigned long *psize);
 extern unsigned long uprobe_get_trampoline_vaddr(void);
 extern void uprobe_copy_from_page(struct page *page, unsigned long vaddr, void *dst, int len);
-extern void arch_uprobe_clear_state(struct mm_struct *mm);
-extern void arch_uprobe_init_state(struct mm_struct *mm);
 extern void handle_syscall_uprobe(struct pt_regs *regs, unsigned long bp_vaddr);
 extern void arch_uprobe_optimize(struct arch_uprobe *auprobe, unsigned long vaddr);
 extern unsigned long arch_uprobe_get_xol_area(void);
kernel/events/uprobes.c +0 -10
index 4084e926e284..b5c516168f84 100644
@@ -1806,14 +1806,6 @@ static struct xol_area *get_xol_area(void)
 	return area;
 }
 
-void __weak arch_uprobe_clear_state(struct mm_struct *mm)
-{
-}
-
-void __weak arch_uprobe_init_state(struct mm_struct *mm)
-{
-}
-
 /*
  * uprobe_clear_state - Free the area allocated for slots.
  */
@@ -1825,8 +1817,6 @@ void uprobe_clear_state(struct mm_struct *mm)
 	delayed_uprobe_remove(NULL, mm);
 	mutex_unlock(&delayed_uprobe_lock);
 
-	arch_uprobe_clear_state(mm);
-
 	if (!area)
 		return;
 
kernel/fork.c +0 -1
index 5f3fdfdb14c7..9c6baabdc961 100644
@@ -1059,7 +1059,6 @@ static void mm_init_uprobes_state(struct mm_struct *mm)
 {
 #ifdef CONFIG_UPROBES
 	mm->uprobes_state.xol_area = NULL;
-	arch_uprobe_init_state(mm);
 #endif
 }
 
author reviewer
#6 lore raw

PATCH v2 3/11 uprobes/x86: Move optimized uprobe from nop5 to nop10

In reply to Jiri Olsa

Andrii reported an issue with optimized uprobes [1] that can clobber redzone area with call instruction storing return address on stack where user code may keep temporary data without adjusting rsp.

Fixing this by moving the optimized uprobes on top of 10-bytes nop instruction, so we can squeeze another instruction to escape the redzone area before doing the call, like:

lea -0x80(%rsp), %rsp
call tramp

Note the lea instruction is used to adjust the rsp register without changing the flags.

The unoptimize path is bit tricky, because we can't change back to nop10 instruction, because we could have some thread already inside lea instruction. Instead we change it to 'jmp rel8' jump instruction to end of the 10-byte slot. The `jmp rel8' is also added as another instruction that allows optimized uprobe in can_optimize function.

The optimized uprobe performance stays the same:

        uprobe-nop     :    3.129 ± 0.013M/s
        uprobe-push    :    3.045 ± 0.006M/s
        uprobe-ret     :    1.095 ± 0.004M/s
  -->   uprobe-nop10   :    7.170 ± 0.020M/s
        uretprobe-nop  :    2.143 ± 0.021M/s
        uretprobe-push :    2.090 ± 0.000M/s
        uretprobe-ret  :    0.942 ± 0.000M/s
  -->   uretprobe-nop10:    3.381 ± 0.003M/s
        usdt-nop       :    3.245 ± 0.004M/s
  -->   usdt-nop10     :    7.256 ± 0.023M/s

[1] https://lore.kernel.org/bpf/[email protected]/

Fixes ba2bfc97b462 ("uprobes/x86: Add support to optimize uprobes")
---
 arch/x86/kernel/uprobes.c | 130 ++++++++++++++++++++++++++------------
 1 file changed, 89 insertions(+), 41 deletions(-)
arch/x86/kernel/uprobes.c +89 -41
index 37faf038be33..e0067d1b6242 100644
@@ -636,9 +636,26 @@ struct uprobe_trampoline {
 	unsigned long		vaddr;
 };
 
+#define LEA_INSN_SIZE		5
+#define OPT_INSN_SIZE		(LEA_INSN_SIZE + CALL_INSN_SIZE)
+#define OPT_JMP8_OFFSET		(OPT_INSN_SIZE - JMP8_INSN_SIZE)
+#define REDZONE_SIZE		0x80
+
+static const u8 lea_rsp[] = { 0x48, 0x8d, 0x64, 0x24, 0x80 };
+
+static bool is_opt_insns(const uprobe_opcode_t *insn)
+{
+	static const u8 opt_insns[] = {
+		0x48, 0x8d, 0x64, 0x24, REDZONE_SIZE, /* lea -0x80(%rsp), %rsp */
+		CALL_INSN_OPCODE
+	};
+
+	return !memcmp(insn, opt_insns, ARRAY_SIZE(opt_insns));
+}
+
 static bool is_reachable_by_call(unsigned long vtramp, unsigned long vaddr)
 {
-	long delta = (long)(vaddr + 5 - vtramp);
+	long delta = (long)(vaddr + OPT_INSN_SIZE - vtramp);
 
 	return delta >= INT_MIN && delta <= INT_MAX;
 }
@@ -651,7 +668,7 @@ static unsigned long find_nearest_trampoline(unsigned long vaddr)
 	};
 	unsigned long low_limit, high_limit;
 	unsigned long low_tramp, high_tramp;
-	unsigned long call_end = vaddr + 5;
+	unsigned long call_end = vaddr + OPT_INSN_SIZE;
 
 	if (check_add_overflow(call_end, INT_MIN, &low_limit))
 		low_limit = PAGE_SIZE;
@@ -810,7 +827,7 @@ SYSCALL_DEFINE0(uprobe)
 
 	/* Allow execution only from uprobe trampolines. */
 	if (!in_uprobe_trampoline(regs->ip))
-		return -ENXIO;
+		return -EPROTO;
 
 	err = copy_from_user(&args, (void __user *)regs->sp, sizeof(args));
 	if (err)
@@ -826,8 +843,8 @@ SYSCALL_DEFINE0(uprobe)
 	regs->ax  = args.ax;
 	regs->r11 = args.r11;
 	regs->cx  = args.cx;
-	regs->ip  = args.retaddr - 5;
-	regs->sp += sizeof(args);
+	regs->ip  = args.retaddr - OPT_INSN_SIZE;
+	regs->sp += sizeof(args) + REDZONE_SIZE;
 	regs->orig_ax = -1;
 
 	sp = regs->sp;
@@ -844,12 +861,12 @@ SYSCALL_DEFINE0(uprobe)
 	 */
 	if (regs->sp != sp) {
 		/* skip the trampoline call */
-		if (args.retaddr - 5 == regs->ip)
-			regs->ip += 5;
+		if (args.retaddr - OPT_INSN_SIZE == regs->ip)
+			regs->ip += OPT_INSN_SIZE;
 		return regs->ax;
 	}
 
-	regs->sp -= sizeof(args);
+	regs->sp -= sizeof(args) + REDZONE_SIZE;
 
 	/* for the case uprobe_consumer has changed ax/r11/cx */
 	args.ax  = regs->ax;
@@ -857,7 +874,7 @@ SYSCALL_DEFINE0(uprobe)
 	args.cx  = regs->cx;
 
 	/* keep return address unless we are instructed otherwise */
-	if (args.retaddr - 5 != regs->ip)
+	if (args.retaddr - OPT_INSN_SIZE != regs->ip)
 		args.retaddr = regs->ip;
 
 	if (shstk_push(args.retaddr) == -EFAULT)
@@ -891,7 +908,7 @@ asm (
 	"pop %rax\n"
 	"pop %r11\n"
 	"pop %rcx\n"
-	"ret\n"
+	"ret $" __stringify(REDZONE_SIZE) "\n"
 	"int3\n"
 	".balign " __stringify(PAGE_SIZE) "\n"
 	".popsection\n"
@@ -909,7 +926,7 @@ late_initcall(arch_uprobes_init);
 
 enum {
 	EXPECT_SWBP,
-	EXPECT_CALL,
+	EXPECT_OPTIMIZED,
 };
 
 struct write_opcode_ctx {
@@ -917,11 +934,6 @@ struct write_opcode_ctx {
 	int expect;
 };
 
-static int is_call_insn(uprobe_opcode_t *insn)
-{
-	return *insn == CALL_INSN_OPCODE;
-}
-
 /*
  * Verification callback used by int3_update uprobe_write calls to make sure
  * the underlying instruction is as expected - either int3 or call.
@@ -930,17 +942,17 @@ static int verify_insn(struct page *page, unsigned long vaddr, uprobe_opcode_t *
 		       int nbytes, void *data)
 {
 	struct write_opcode_ctx *ctx = data;
-	uprobe_opcode_t old_opcode[5];
+	uprobe_opcode_t old_opcode[OPT_INSN_SIZE];
 
-	uprobe_copy_from_page(page, ctx->base, (uprobe_opcode_t *) &old_opcode, 5);
+	uprobe_copy_from_page(page, ctx->base, old_opcode, OPT_INSN_SIZE);
 
 	switch (ctx->expect) {
 	case EXPECT_SWBP:
 		if (is_swbp_insn(&old_opcode[0]))
 			return 1;
 		break;
-	case EXPECT_CALL:
-		if (is_call_insn(&old_opcode[0]))
+	case EXPECT_OPTIMIZED:
+		if (is_opt_insns(&old_opcode[0]))
 			return 1;
 		break;
 	}
@@ -963,7 +975,7 @@ static int verify_insn(struct page *page, unsigned long vaddr, uprobe_opcode_t *
  *   - SMP sync all CPUs
  */
 static int int3_update(struct arch_uprobe *auprobe, struct vm_area_struct *vma,
-		       unsigned long vaddr, char *insn, bool optimize)
+		       unsigned long vaddr, char *insn, int size, bool optimize)
 {
 	uprobe_opcode_t int3 = UPROBE_SWBP_INSN;
 	struct write_opcode_ctx ctx = {
@@ -978,7 +990,7 @@ static int int3_update(struct arch_uprobe *auprobe, struct vm_area_struct *vma,
 	 * so we can skip this step for optimize == true.
 	 */
 	if (!optimize) {
-		ctx.expect = EXPECT_CALL;
+		ctx.expect = EXPECT_OPTIMIZED;
 		err = uprobe_write(auprobe, vma, vaddr, &int3, 1, verify_insn,
 				   true /* is_register */, false /* do_update_ref_ctr */,
 				   &ctx);
@@ -990,7 +1002,7 @@ static int int3_update(struct arch_uprobe *auprobe, struct vm_area_struct *vma,
 
 	/* Write all but the first byte of the patched range. */
 	ctx.expect = EXPECT_SWBP;
-	err = uprobe_write(auprobe, vma, vaddr + 1, insn + 1, 4, verify_insn,
+	err = uprobe_write(auprobe, vma, vaddr + 1, insn + 1, size - 1, verify_insn,
 			   true /* is_register */, false /* do_update_ref_ctr */,
 			   &ctx);
 	if (err)
@@ -1017,17 +1029,32 @@ static int int3_update(struct arch_uprobe *auprobe, struct vm_area_struct *vma,
 static int swbp_optimize(struct arch_uprobe *auprobe, struct vm_area_struct *vma,
 			 unsigned long vaddr, unsigned long tramp)
 {
-	u8 call[5];
+	u8 insn[OPT_INSN_SIZE], *call = &insn[LEA_INSN_SIZE];
 
-	__text_gen_insn(call, CALL_INSN_OPCODE, (const void *) vaddr,
+	/*
+	 * We have nop10 instruction (with first byte overwritten to int3),
+	 * changing it to:
+	 *   lea -0x80(%rsp), %rsp
+	 *   call tramp
+	 */
+	memcpy(insn, lea_rsp, LEA_INSN_SIZE);
+	__text_gen_insn(call, CALL_INSN_OPCODE,
+			(const void *) (vaddr + LEA_INSN_SIZE),
 			(const void *) tramp, CALL_INSN_SIZE);
-	return int3_update(auprobe, vma, vaddr, call, true /* optimize */);
+	return int3_update(auprobe, vma, vaddr, insn, OPT_INSN_SIZE, true /* optimize */);
 }
 
 static int swbp_unoptimize(struct arch_uprobe *auprobe, struct vm_area_struct *vma,
 			   unsigned long vaddr)
 {
-	return int3_update(auprobe, vma, vaddr, auprobe->insn, false /* optimize */);
+	/*
+	 * We have optimized nop10 (lea, call), changing it to 'jmp rel8' to
+	 * end of the 10-byte slot instead of restoring the original nop10,
+	 * because we could have thread already inside lea instruction.
+	 */
+	u8 jmp[OPT_INSN_SIZE] = { JMP8_INSN_OPCODE, OPT_JMP8_OFFSET };
+
+	return int3_update(auprobe, vma, vaddr, jmp, JMP8_INSN_SIZE, false /* optimize */);
 }
 
 static int copy_from_vaddr(struct mm_struct *mm, unsigned long vaddr, void *dst, int len)
@@ -1049,19 +1076,19 @@ static bool __is_optimized(struct mm_struct *mm, uprobe_opcode_t *insn, unsigned
 	struct __packed __arch_relative_insn {
 		u8 op;
 		s32 raddr;
-	} *call = (struct __arch_relative_insn *) insn;
+	} *call = (struct __arch_relative_insn *)(insn + LEA_INSN_SIZE);
 
-	if (!is_call_insn(insn))
+	if (!is_opt_insns(insn))
 		return false;
-	return __in_uprobe_trampoline(mm, vaddr + 5 + call->raddr);
+	return __in_uprobe_trampoline(mm, vaddr + OPT_INSN_SIZE + call->raddr);
 }
 
 static int is_optimized(struct mm_struct *mm, unsigned long vaddr)
 {
-	uprobe_opcode_t insn[5];
+	uprobe_opcode_t insn[OPT_INSN_SIZE];
 	int err;
 
-	err = copy_from_vaddr(mm, vaddr, &insn, 5);
+	err = copy_from_vaddr(mm, vaddr, &insn, OPT_INSN_SIZE);
 	if (err)
 		return err;
 	return __is_optimized(mm, (uprobe_opcode_t *)&insn, vaddr);
@@ -1095,14 +1122,25 @@ int set_orig_insn(struct arch_uprobe *auprobe, struct vm_area_struct *vma,
 		  unsigned long vaddr)
 {
 	if (test_bit(ARCH_UPROBE_FLAG_CAN_OPTIMIZE, &auprobe->flags)) {
-		int ret = is_optimized(vma->vm_mm, vaddr);
-		if (ret < 0)
+		uprobe_opcode_t insn[OPT_INSN_SIZE];
+		int ret;
+
+		ret = copy_from_vaddr(vma->vm_mm, vaddr, &insn, OPT_INSN_SIZE);
+		if (ret)
 			return ret;
-		if (ret) {
+		if (__is_optimized(vma->vm_mm, (uprobe_opcode_t *)&insn, vaddr)) {
 			ret = swbp_unoptimize(auprobe, vma, vaddr);
 			WARN_ON_ONCE(ret);
 			return ret;
 		}
+		/*
+		 * We can have re-attached probe on top of jmp8 instruction,
+		 * which did not get optimized. We need to restore the jmp8
+		 * instruction, instead of the original instruction (nop10).
+		 */
+		if (is_swbp_insn(&insn[0]) && insn[1] == OPT_JMP8_OFFSET)
+			return uprobe_write_opcode(auprobe, vma, vaddr, JMP8_INSN_OPCODE,
+						   false /* is_register */);
 	}
 	return uprobe_write_opcode(auprobe, vma, vaddr, *(uprobe_opcode_t *)&auprobe->insn,
 				   false /* is_register */);
@@ -1131,7 +1169,7 @@ static int __arch_uprobe_optimize(struct arch_uprobe *auprobe, struct mm_struct
 void arch_uprobe_optimize(struct arch_uprobe *auprobe, unsigned long vaddr)
 {
 	struct mm_struct *mm = current->mm;
-	uprobe_opcode_t insn[5];
+	uprobe_opcode_t insn[OPT_INSN_SIZE];
 
 	if (!should_optimize(auprobe))
 		return;
@@ -1142,7 +1180,7 @@ void arch_uprobe_optimize(struct arch_uprobe *auprobe, unsigned long vaddr)
 	 * Check if some other thread already optimized the uprobe for us,
 	 * if it's the case just go away silently.
 	 */
-	if (copy_from_vaddr(mm, vaddr, &insn, 5))
+	if (copy_from_vaddr(mm, vaddr, &insn, OPT_INSN_SIZE))
 		goto unlock;
 	if (!is_swbp_insn((uprobe_opcode_t*) &insn))
 		goto unlock;
@@ -1160,14 +1198,24 @@ void arch_uprobe_optimize(struct arch_uprobe *auprobe, unsigned long vaddr)
 
 static bool can_optimize(struct insn *insn, unsigned long vaddr)
 {
-	if (!insn->x86_64 || insn->length != 5)
+	if (!insn->x86_64)
 		return false;
 
-	if (!insn_is_nop(insn))
+	/* We can't do cross page atomic writes yet. */
+	if (PAGE_SIZE - (vaddr & ~PAGE_MASK) < OPT_INSN_SIZE)
 		return false;
 
-	/* We can't do cross page atomic writes yet. */
-	return PAGE_SIZE - (vaddr & ~PAGE_MASK) >= 5;
+	/* We can optimize on top of nop10.. */
+	if (insn->length == OPT_INSN_SIZE && insn_is_nop(insn))
+		return true;
+
+	/* .. and JMP rel8 to end of slot — check swbp_unoptimize. */
+	if (insn->length == 2 &&
+	    insn->opcode.bytes[0] == JMP8_INSN_OPCODE &&
+	    insn->immediate.value == OPT_JMP8_OFFSET)
+		return true;
+
+	return false;
 }
 #else /* 32-bit: */
 /*
Signature · 1 line
2.53.0
#7 lore raw

Re: [PATCHv2 03/11] uprobes/x86: Move optimized uprobe from nop5 to nop10

When re-optimizing from JMP8 back to LEA+CALL, does the CALL instruction at offset 5 get rewritten without INT3 protection?
Quoted 3 lines
arch/x86/kernel/uprobes.c +0 -0

[ ... ]

Quoted 19 lines
arch/x86/kernel/uprobes.c +2 -2
@@ -963,7 +975,7 @@ static int verify_insn(struct page *page, unsigned long vaddr, uprobe_opcode_t *new_opcode,
  *   - SMP sync all CPUs
  */
 static int int3_update(struct arch_uprobe *auprobe, struct vm_area_struct *vma,
-		       unsigned long vaddr, char *insn, bool optimize)
+		       unsigned long vaddr, char *insn, int size, bool optimize)
 {
 	uprobe_opcode_t int3 = UPROBE_SWBP_INSN;
 	struct write_opcode_ctx ctx = {
@@ -978,7 +990,7 @@ struct write_opcode_ctx ctx = {
 	 * so we can skip this step for optimize == true.
 	 */
 	if (!optimize) {
-		ctx.expect = EXPECT_CALL;
+		ctx.expect = EXPECT_OPTIMIZED;
 		err = uprobe_write(auprobe, vma, vaddr, &int3, 1, verify_insn,
 				   true /* is_register */, false /* do_update_ref_ctr */,
 				   &ctx);

arch/x86/kernel/uprobes.c struct write_opcode_ctx ctx = { · line 1005

Quoted 5 lines
arch/x86/kernel/uprobes.c +1 -1
@@ -990,7 +1002,7 @@ struct write_opcode_ctx ctx = {
 
 	/* Write all but the first byte of the patched range. */
 	ctx.expect = EXPECT_SWBP;
-	err = uprobe_write(auprobe, vma, vaddr + 1, insn + 1, 4, verify_insn,
+	err = uprobe_write(auprobe, vma, vaddr + 1, insn + 1, size - 1, verify_insn,

When re-optimizing from JMP8 back to LEA+CALL, does the CALL instruction at offset 5 get rewritten without INT3 protection?

The int3_update() sequence writes INT3 at offset 0 during unoptimize (!optimize path), then later writes bytes 1..9 while verifying offset 0 stays INT3. During re-optimization (optimize == true), the code skips the initial INT3 write and directly writes bytes 1..9.

Consider this sequence when re-optimizing a previously unoptimized uprobe that is currently 'jmp rel8' back to 'lea; call':

Step 7: Current state is 0xCC 0x08 ... (INT3 followed by JMP8 offset) Step 8: Write bytes 1..9, transforming to 0xCC 0x8d 0x64 0x24 0x80 0xE8 ...
The CALL opcode (0xE8) at offset 5 and its 4-byte displacement get written without an INT3 protecting that location.

A concurrent thread executing at vaddr could observe a partially-written CALL instruction during the transition. Peter Zijlstra suggested placing INT3 at both offset 0 and offset 5 to protect both the LEA and CALL instructions during modification:

Reference: https://lore.kernel.org/bpf/[email protected]/

"Note that from step 7 to step 8, you re-write the CALL instruction
without going through INT3. This means it is entirely possible for a concurrent execution to observe a composite instruction. This is NOT sound! However, I think it can be salvaged, if instead of only writing INT3 at +0, you also write INT3 at +5."

[ ... ]

Quoted 15 lines
arch/x86/kernel/uprobes.c +6 -3
@@ -1095,14 +1122,25 @@ int set_orig_insn(struct arch_uprobe *auprobe, struct vm_area_struct *vma,
 		  unsigned long vaddr)
 {
 	if (test_bit(ARCH_UPROBE_FLAG_CAN_OPTIMIZE, &auprobe->flags)) {
-		int ret = is_optimized(vma->vm_mm, vaddr);
-		if (ret < 0)
+		uprobe_opcode_t insn[OPT_INSN_SIZE];
+		int ret;
+
+		ret = copy_from_vaddr(vma->vm_mm, vaddr, &insn, OPT_INSN_SIZE);
+		if (ret)
 			return ret;
-		if (ret) {
+		if (__is_optimized(vma->vm_mm, (uprobe_opcode_t *)&insn, vaddr)) {
 			ret = swbp_unoptimize(auprobe, vma, vaddr);

arch/x86/kernel/uprobes.c int set_orig_insn(struct arch_uprobe *auprobe, struct vm_area_struct *vma, · line 1142

Quoted 10 lines
arch/x86/kernel/uprobes.c +7 -0
 			WARN_ON_ONCE(ret);
 			return ret;
 		}
+		/*
+		 * We can have re-attached probe on top of jmp8 instruction,
+		 * which did not get optimized. We need to restore the jmp8
+		 * instruction, instead of the original instruction (nop10).
+		 */
+		if (is_swbp_insn(&insn[0]) && insn[1] == OPT_JMP8_OFFSET)
+			return uprobe_write_opcode(auprobe, vma, vaddr, JMP8_INSN_OPCODE,

This isn't a bug, but the multi-line if statement is missing braces:

		if (is_swbp_insn(&insn[0]) && insn[1] == OPT_JMP8_OFFSET)
			return uprobe_write_opcode(auprobe, vma, vaddr, JMP8_INSN_OPCODE,
						   false /* is_register */);

Kernel coding style requires braces on any multi-line statement.

Reference: https://lore.kernel.org/bpf/[email protected]/

[ ... ]

Quoted 17 lines
arch/x86/kernel/uprobes.c +5 -4
@@ -1160,14 +1198,24 @@ void arch_uprobe_optimize(struct arch_uprobe *auprobe, unsigned long vaddr)
 
 static bool can_optimize(struct insn *insn, unsigned long vaddr)
 {
-	if (!insn->x86_64 || insn->length != 5)
+	if (!insn->x86_64)
 		return false;
 
-	if (!insn_is_nop(insn))
+	/* We can't do cross page atomic writes yet. */
+	if (PAGE_SIZE - (vaddr & ~PAGE_MASK) < OPT_INSN_SIZE)
 		return false;
 
-	/* We can't do cross page atomic writes yet. */
-	return PAGE_SIZE - (vaddr & ~PAGE_MASK) >= 5;
+	/* We can optimize on top of nop10.. */
+	if (insn->length == OPT_INSN_SIZE && insn_is_nop(insn))

arch/x86/kernel/uprobes.c void arch_uprobe_optimize(struct arch_uprobe *auprobe, unsigned long vaddr) · line 1219

Quoted 10 lines
arch/x86/kernel/uprobes.c +9 -0
+		return true;
+
+	/* .. and JMP rel8 to end of slot — check swbp_unoptimize. */
+	if (insn->length == 2 &&
+	    insn->opcode.bytes[0] == JMP8_INSN_OPCODE &&
+	    insn->immediate.value == OPT_JMP8_OFFSET)
+		return true;
+
+	return false;
 }

Can can_optimize() safely accept naturally occurring 'jmp +8' instructions from user binaries?

The JMP8 branch returns true for any 2-byte 'jmp rel8' with displacement 8 (bytes 0xEB 0x08). The comment says this is for swbp_unoptimize()'s kernel-placed jmp, but can_optimize() is called during initial analysis via prepare_uprobe() -> arch_uprobe_analyze_insn() -> uprobe_init_insn(), before any kernel modifications occur. The instruction bytes come directly from the user binary through copy_insn().

If a user binary naturally contains 'EB 08' at the uprobe location (e.g., a forward branch over an 8-byte literal pool or alignment padding), this path would trigger:

1. uprobe_init_insn() decodes the user's 'jmp 0x08' (length=2, opcode=0xEB,

     immediate=8).
  2. can_optimize() matches the new JMP8 branch and returns true.
  3. ARCH_UPROBE_FLAG_CAN_OPTIMIZE is set.
  4. set_swbp() writes INT3 at vaddr, replacing the 0xEB.
  5. When the breakpoint fires, swbp_optimize() -> int3_update() writes 10
     bytes (lea -0x80(%rsp),%rsp; call tramp) over vaddr..vaddr+9.

6. verify_insn() only checks that byte 0 is INT3 (EXPECT_SWBP) before writing bytes 1..9. It does not verify those 8 bytes are from a prior optimization.
7. The 8 bytes at vaddr+2..vaddr+9 (original user code/data following the jmp) get overwritten with the tail of the LEA and the CALL encoding.
8. On unregister, swbp_unoptimize() only writes 2 bytes back. Bytes vaddr+2..vaddr+9 stay corrupted with call instruction remnants in the COW'd page.

The corruption persists even though the original jmp would skip those bytes, because other control flow (indirect branches, function pointers, alternative entry points) might reach that range.

Contrast with nop10: when can_optimize returns true for a 10-byte nop (length==OPT_INSN_SIZE && insn_is_nop), writing 10 bytes only overwrites the nop itself. The earlier nop5 code was safe because it required insn->length == 5 and wrote exactly 5 bytes (write size matched instruction size).

The JMP8 case breaks that invariant: write size (10 bytes) exceeds instruction size (2 bytes), so the 8 trailing bytes are arbitrary user content. Would a length check (requiring insn->length == OPT_INSN_SIZE for the JMP8 case) or tracking optimization state via a flag prevent optimizing user instructions that happen to match the JMP8 pattern?

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/26030080109

author reviewer
#8 lore raw

PATCH v2 4/11 libbpf: Change has_nop_combo to work on top of nop10

In reply to Jiri Olsa

We now expect nop combo with 10 bytes nop instead of 5 bytes nop, fixing has_nop_combo to reflect that.

Fixes 41a5c7df4466 ("libbpf: Add support to detect nop,nop5 instructions combo for usdt probe")
---
 tools/lib/bpf/usdt.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
tools/lib/bpf/usdt.c +8 -8
index e3710933fd52..7e62e4d5bedd 100644
@@ -305,7 +305,7 @@ struct usdt_manager *usdt_manager_new(struct bpf_object *obj)
 
 	/*
 	 * Detect kernel support for uprobe() syscall, it's presence means we can
-	 * take advantage of faster nop5 uprobe handling.
+	 * take advantage of faster nop10 uprobe handling.
 	 * Added in: 56101b69c919 ("uprobes/x86: Add uprobe syscall to speed up uprobe")
 	 */
 	man->has_uprobe_syscall = kernel_supports(obj, FEAT_UPROBE_SYSCALL);
@@ -596,14 +596,14 @@ static int parse_usdt_spec(struct usdt_spec *spec, const struct usdt_note *note,
 #if defined(__x86_64__)
 static bool has_nop_combo(int fd, long off)
 {
-	unsigned char nop_combo[6] = {
-		0x90, 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nop,nop5 */
+	unsigned char nop_combo[11] = {
+		0x90, 0x66, 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00,
 	};
-	unsigned char buf[6];
+	unsigned char buf[11];
 
-	if (pread(fd, buf, 6, off) != 6)
+	if (pread(fd, buf, 11, off) != 11)
 		return false;
-	return memcmp(buf, nop_combo, 6) == 0;
+	return memcmp(buf, nop_combo, 11) == 0;
 }
 #else
 static bool has_nop_combo(int fd, long off)
@@ -814,8 +814,8 @@ static int collect_usdt_targets(struct usdt_manager *man, struct elf_fd *elf_fd,
 		memset(target, 0, sizeof(*target));
 
 		/*
-		 * We have uprobe syscall and usdt with nop,nop5 instructions combo,
-		 * so we can place the uprobe directly on nop5 (+1) and get this probe
+		 * We have uprobe syscall and usdt with nop,nop10 instructions combo,
+		 * so we can place the uprobe directly on nop10 (+1) and get this probe
 		 * optimized.
 		 */
 		if (man->has_uprobe_syscall && has_nop_combo(elf_fd->fd, usdt_rel_ip)) {
Signature · 1 line
2.53.0
#9 lore raw

Re: [PATCHv2 04/11] libbpf: Change has_nop_combo to work on top of nop10

Should the Fixes tag reference commit 5c3c59c7952a ("uprobes/x86: Move optimized uprobe from nop5 to nop10") instead? Commit 41a5c7df4466 originally introduced has_nop_combo() with the correct nop5…
Quoted 6 lines

libbpf: Change has_nop_combo to work on top of nop10

We now expect nop combo with 10 bytes nop instead of 5 bytes nop, fixing has_nop_combo to reflect that.

Fixes 41a5c7df4466 ("libbpf: Add support to detect nop,nop5 instructions combo for usdt probe")

Should the Fixes tag reference commit 5c3c59c7952a ("uprobes/x86: Move optimized uprobe from nop5 to nop10") instead? Commit 41a5c7df4466 originally introduced has_nop_combo() with the correct nop5 check. Commit 5c3c59c7952a later changed the kernel to use nop10 instead of nop5 for uprobe optimization but failed to update the libbpf has_nop_combo() function accordingly, creating a mismatch between kernel and userspace.

Quoted 7 lines
tools/lib/bpf/usdt.c +0 -0
index e3710933fd52..7e62e4d5bedd 100644

[ ... ]

Quoted 8 lines
tools/lib/bpf/usdt.c +2 -2
@@ -596,14 +596,14 @@ static int parse_usdt_spec(struct usdt_spec *spec, const struct usdt_note *note,
 #if defined(__x86_64__)
 static bool has_nop_combo(int fd, long off)
 {
-	unsigned char nop_combo[6] = {
-		0x90, 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nop,nop5 */
+	unsigned char nop_combo[11] = {
+		0x90, 0x66, 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00,

tools/lib/bpf/usdt.c static int parse_usdt_spec(struct usdt_spec *spec, const struct usdt_note *note, · line 607

Quoted 10 lines
tools/lib/bpf/usdt.c +3 -3
 	};
-	unsigned char buf[6];
+	unsigned char buf[11];
 
-	if (pread(fd, buf, 6, off) != 6)
+	if (pread(fd, buf, 11, off) != 11)
 		return false;
-	return memcmp(buf, nop_combo, 6) == 0;
+	return memcmp(buf, nop_combo, 11) == 0;
 }

This isn't a bug, but Jakub Sitnicki suggested in v1 review to use ARRAY_SIZE(buf) instead of repeating the scalar value 11 in multiple places. The suggestion was included alongside the Reviewed-by tag but was not addressed.

Reference: https://lore.kernel.org/bpf/[email protected]/

[ ... ]

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/26030080109

#10 lore raw

Re: [PATCHv2 04/11] libbpf: Change has_nop_combo to work on top of nop10

Quoted 5 lines · On Mon, May 18, 2026 at 11:37:58AM +0000, [email protected] wrote:
Quoted 5 lines

libbpf: Change has_nop_combo to work on top of nop10

We now expect nop combo with 10 bytes nop instead of 5 bytes nop, fixing has_nop_combo to reflect that.

Quoted 8 lines
Quoted 1 line
Fixes 41a5c7df4466 ("libbpf: Add support to detect nop,nop5 instructions combo for usdt probe")

Should the Fixes tag reference commit 5c3c59c7952a ("uprobes/x86: Move optimized uprobe from nop5 to nop10") instead? Commit 41a5c7df4466 originally introduced has_nop_combo() with the correct nop5 check. Commit 5c3c59c7952a later changed the kernel to use nop10 instead of nop5 for uprobe optimization but failed to update the libbpf has_nop_combo() function accordingly, creating a mismatch between kernel and userspace.

keeping things separated now for easier review

Quoted 23 lines
Quoted 8 lines
tools/lib/bpf/usdt.c +0 -0
index e3710933fd52..7e62e4d5bedd 100644

[ ... ]

Quoted 12 lines
tools/lib/bpf/usdt.c +3 -3
@@ -596,14 +596,14 @@ static int parse_usdt_spec(struct usdt_spec *spec, const struct usdt_note *note,
 #if defined(__x86_64__)
 static bool has_nop_combo(int fd, long off)
 {
-	unsigned char nop_combo[6] = {
-		0x90, 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nop,nop5 */
+	unsigned char nop_combo[11] = {
+		0x90, 0x66, 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00,
 	};
-	unsigned char buf[6];
+	unsigned char buf[11];
 
Quoted 11 lines

tools/lib/bpf/usdt.c static int parse_usdt_spec(struct usdt_spec *spec, const struct usdt_note *note, · line 607

Quoted 6 lines
tools/lib/bpf/usdt.c +2 -2
-	if (pread(fd, buf, 6, off) != 6)
+	if (pread(fd, buf, 11, off) != 11)
 		return false;
-	return memcmp(buf, nop_combo, 6) == 0;
+	return memcmp(buf, nop_combo, 11) == 0;
 }

This isn't a bug, but Jakub Sitnicki suggested in v1 review to use ARRAY_SIZE(buf) instead of repeating the scalar value 11 in multiple places. The suggestion was included alongside the Reviewed-by tag but was not addressed.

I think it's easy enough to keep the number

jirka

author reviewer
#11 lore raw

PATCH v2 5/11 libbpf: Detect uprobe syscall with new error

In reply to Jiri Olsa

In the previous optimized uprobe fix we changed the syscall error used for its detection from ENXIO to EPROTO.

Changing related probe_uprobe_syscall detection check.

Fixes 05738da0efa1 ("libbpf: Add uprobe syscall feature detection")
---
 tools/lib/bpf/features.c                                | 4 ++--
 tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
tools/lib/bpf/features.c +2 -2
index b7e388f99d0b..e5641fa60163 100644
@@ -577,10 +577,10 @@ static int probe_ldimm64_full_range_off(int token_fd)
 static int probe_uprobe_syscall(int token_fd)
 {
 	/*
-	 * If kernel supports uprobe() syscall, it will return -ENXIO when called
+	 * If kernel supports uprobe() syscall, it will return -EPROTO when called
 	 * from the outside of a kernel-generated uprobe trampoline.
 	 */
-	return syscall(__NR_uprobe) < 0 && errno == ENXIO;
+	return syscall(__NR_uprobe) < 0 && errno == EPROTO;
 }
 #else
 static int probe_uprobe_syscall(int token_fd)
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +1 -1
index 955a37751b52..c944136252c6 100644
@@ -762,7 +762,7 @@ static void test_uprobe_error(void)
 	long err = syscall(__NR_uprobe);
 
 	ASSERT_EQ(err, -1, "error");
-	ASSERT_EQ(errno, ENXIO, "errno");
+	ASSERT_EQ(errno, EPROTO, "errno");
 }
 
 static void __test_uprobe_syscall(void)
Signature · 1 line
2.53.0
#12 lore raw

Re: [PATCHv2 05/11] libbpf: Detect uprobe syscall with new error

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Missing kernel dependency causes immediate selftest failure and libbpf feature detection breakage.…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Missing kernel dependency causes immediate selftest failure and libbpf feature detection breakage.
- [High] libbpf feature detection breaks backward compatibility by not accepting the old ENXIO error code. --

commit 674b83913f8f61b2f16ba8f8b86e6bf8e84da5a3
Author: Jiri Olsa <[email protected]>

libbpf: Detect uprobe syscall with new error

This commit updates libbpf's feature detection and the uprobe selftests to expect the uprobe syscall to return -EPROTO instead of -ENXIO. This is in response to an optimized uprobe fix that changed the syscall's error code.

Quoted 5 lines
tools/lib/bpf/features.c +0 -0
index 4f19a0d79b0cf..a37aaea5253e5 100644

tools/lib/bpf/features.c static int probe_ldimm64_full_range_off(int token_fd) · line 583

Quoted 9 lines
tools/lib/bpf/features.c +2 -2
@@ -577,10 +577,10 @@ static int probe_ldimm64_full_range_off(int token_fd)
 static int probe_uprobe_syscall(int token_fd)
 {
 	/*
-	 * If kernel supports uprobe() syscall, it will return -ENXIO when called
+	 * If kernel supports uprobe() syscall, it will return -EPROTO when called
 	 * from the outside of a kernel-generated uprobe trampoline.
 	 */
-	return syscall(__NR_uprobe) < 0 && errno == ENXIO;
+	return syscall(__NR_uprobe) < 0 && errno == EPROTO;

Is it possible this change breaks libbpf's backward compatibility with older kernels? Since probe_uprobe_syscall() detects uprobe syscall support on various kernel versions, replacing the check rather than allowing either (e.g., errno == ENXIO || errno == EPROTO) might cause libbpf to silently fail to detect uprobe support on kernels that predate the EPROTO change.

Also, looking at arch/x86/kernel/uprobes.c, the kernel-side change does not appear to be present in the tree yet:

SYSCALL_DEFINE0(uprobe)

{
	...
	/* Allow execution only from uprobe trampolines. */
	if (!in_uprobe_trampoline(regs->ip))
		return -ENXIO;

Will replacing this check cause immediate regressions for both feature detection and the selftests until the kernel dependency is merged?

[ ... ]

tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c static void test_uprobe_error(void) · line 765

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +1 -1
index 955a37751b52d..c944136252c6d 100644
@@ -762,7 +762,7 @@ static void test_uprobe_error(void)
 	long err = syscall(__NR_uprobe);
 
 	ASSERT_EQ(err, -1, "error");
-	ASSERT_EQ(errno, ENXIO, "errno");
+	ASSERT_EQ(errno, EPROTO, "errno");

Similarly, does this selftest change introduce an immediate regression if the running kernel still returns -ENXIO?

Quoted 1 line
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +0 -0
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#13 lore raw

Re: [PATCHv2 05/11] libbpf: Detect uprobe syscall with new error

Quoted 26 lines · On Mon, May 18, 2026 at 11:31:26AM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Missing kernel dependency causes immediate selftest failure and libbpf feature detection breakage.
- [High] libbpf feature detection breaks backward compatibility by not accepting the old ENXIO error code. --

commit 674b83913f8f61b2f16ba8f8b86e6bf8e84da5a3
Author: Jiri Olsa <[email protected]>

libbpf: Detect uprobe syscall with new error

This commit updates libbpf's feature detection and the uprobe selftests to expect the uprobe syscall to return -EPROTO instead of -ENXIO. This is in response to an optimized uprobe fix that changed the syscall's error code.

Quoted 12 lines
tools/lib/bpf/features.c +1 -1
index 4f19a0d79b0cf..a37aaea5253e5 100644
@@ -577,10 +577,10 @@ static int probe_ldimm64_full_range_off(int token_fd)
 static int probe_uprobe_syscall(int token_fd)
 {
 	/*
-	 * If kernel supports uprobe() syscall, it will return -ENXIO when called
+	 * If kernel supports uprobe() syscall, it will return -EPROTO when called
 	 * from the outside of a kernel-generated uprobe trampoline.
 	 */
Quoted 8 lines

tools/lib/bpf/features.c static int probe_ldimm64_full_range_off(int token_fd) · line 583

Quoted 2 lines
tools/lib/bpf/features.c +1 -1
-	return syscall(__NR_uprobe) < 0 && errno == ENXIO;
+	return syscall(__NR_uprobe) < 0 && errno == EPROTO;

Is it possible this change breaks libbpf's backward compatibility with older kernels? Since probe_uprobe_syscall() detects uprobe syscall support on various kernel versions, replacing the check rather than allowing either (e.g., errno == ENXIO || errno == EPROTO) might cause libbpf to silently fail to detect uprobe support on kernels that predate the EPROTO change.

that's the idea, we don't want the feature to be used for kernel that return -ENXIO

Quoted 10 lines

Also, looking at arch/x86/kernel/uprobes.c, the kernel-side change does not appear to be present in the tree yet:

SYSCALL_DEFINE0(uprobe)

{
	...
	/* Allow execution only from uprobe trampolines. */
	if (!in_uprobe_trampoline(regs->ip))
		return -ENXIO;

huh? it's changed already via previous commit:
uprobes/x86: Move optimized uprobe from nop5 to nop10

jirka

Quoted 24 lines

Will replacing this check cause immediate regressions for both feature detection and the selftests until the kernel dependency is merged?

[ ... ]

tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c static void test_uprobe_error(void) · line 765

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +1 -1
index 955a37751b52d..c944136252c6d 100644
@@ -762,7 +762,7 @@ static void test_uprobe_error(void)
 	long err = syscall(__NR_uprobe);
 
 	ASSERT_EQ(err, -1, "error");
-	ASSERT_EQ(errno, ENXIO, "errno");
+	ASSERT_EQ(errno, EPROTO, "errno");

Similarly, does this selftest change introduce an immediate regression if the running kernel still returns -ENXIO?

Quoted 1 line
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +0 -0
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#14 lore raw

Re: [PATCHv2 05/11] libbpf: Detect uprobe syscall with new error

In reply to Jiri Olsa
The Fixes: tag points to commit 05738da0efa1 ("libbpf: Add uprobe syscall feature detection"), but wasn't the bug actually introduced by commit 5c3c59c7952a ("uprobes/x86: Move optimized uprobe from…
Quoted 10 lines

libbpf: Detect uprobe syscall with new error

In the previous optimized uprobe fix we changed the syscall error used for its detection from ENXIO to EPROTO.

Changing related probe_uprobe_syscall detection check.

Fixes 05738da0efa1 ("libbpf: Add uprobe syscall feature detection")

The Fixes: tag points to commit 05738da0efa1 ("libbpf: Add uprobe syscall feature detection"), but wasn't the bug actually introduced by commit 5c3c59c7952a ("uprobes/x86: Move optimized uprobe from nop5 to nop10")? Commit 05738da0efa1 originally added the ENXIO check correctly, but commit 5c3c59c7952a changed the kernel to return EPROTO instead of ENXIO, breaking the libbpf detection. The commit message even says "In the previous optimized uprobe fix we changed the syscall error," which refers to 5c3c59c7952a.

Should this use:

Fixes 5c3c59c7952a ("uprobes/x86: Move optimized uprobe from nop5 to nop10")
Quoted 22 lines
tools/lib/bpf/features.c +2 -2
index b7e388f99d0b..e5641fa60163 100644
@@ -577,10 +577,10 @@ static int probe_ldimm64_full_range_off(int token_fd)
 static int probe_uprobe_syscall(int token_fd)
 {
 	/*
-	 * If kernel supports uprobe() syscall, it will return -ENXIO when called
+	 * If kernel supports uprobe() syscall, it will return -EPROTO when called
 	 * from the outside of a kernel-generated uprobe trampoline.
 	 */
-	return syscall(__NR_uprobe) < 0 && errno == ENXIO;
+	return syscall(__NR_uprobe) < 0 && errno == EPROTO;
 }
 #else
 static int probe_uprobe_syscall(int token_fd)
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +0 -0
index 955a37751b52..c944136252c6 100644

tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c static void test_uprobe_error(void) · line 768

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +1 -1
@@ -762,7 +762,7 @@ static void test_uprobe_error(void)
 	long err = syscall(__NR_uprobe);
 
 	ASSERT_EQ(err, -1, "error");
-	ASSERT_EQ(errno, ENXIO, "errno");
+	ASSERT_EQ(errno, EPROTO, "errno");
 }
 
 static void __test_uprobe_syscall(void)

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/26030080109

Acked-by on v2 5/11
#15 lore raw

Re: [PATCHv2 05/11] libbpf: Detect uprobe syscall with new error

In reply to Jiri Olsa
Quoted 6 lines · Jiri Olsa

In the previous optimized uprobe fix we changed the syscall error used for its detection from ENXIO to EPROTO.

Changing related probe_uprobe_syscall detection check.

Quoted 8 lines
Fixes 05738da0efa1 ("libbpf: Add uprobe syscall feature detection")
---
 tools/lib/bpf/features.c                                | 4 ++--
 tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

sashiko is wrong, this change is correct (we do not want to attempt sys_uprobe optimization if -ENXIO is returned from broken kernels)

Quoted 33 lines
tools/lib/bpf/features.c +2 -2
index b7e388f99d0b..e5641fa60163 100644
@@ -577,10 +577,10 @@ static int probe_ldimm64_full_range_off(int token_fd)
 static int probe_uprobe_syscall(int token_fd)
 {
        /*
-        * If kernel supports uprobe() syscall, it will return -ENXIO when called
+        * If kernel supports uprobe() syscall, it will return -EPROTO when called
         * from the outside of a kernel-generated uprobe trampoline.
         */
-       return syscall(__NR_uprobe) < 0 && errno == ENXIO;
+       return syscall(__NR_uprobe) < 0 && errno == EPROTO;
 }
 #else
 static int probe_uprobe_syscall(int token_fd)
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +1 -2
index 955a37751b52..c944136252c6 100644
@@ -762,7 +762,7 @@ static void test_uprobe_error(void)
        long err = syscall(__NR_uprobe);
 
        ASSERT_EQ(err, -1, "error");
-       ASSERT_EQ(errno, ENXIO, "errno");
+       ASSERT_EQ(errno, EPROTO, "errno");
 }
 
 static void __test_uprobe_syscall(void)
--

2.53.0

author reviewer
#16 lore raw

PATCH v2 6/11 selftests/bpf: Emit nop,nop10 instructions combo for x86_64 arch

In reply to Jiri Olsa

Syncing latest usdt.h change [1].

Now that we have nop10 optimization support in kernel, let's emit nop,nop10 for usdt probe. We leave it up to the library to use desirable nop instruction.

[1] TBD

---
 tools/testing/selftests/bpf/usdt.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/usdt.h +1 -1
index c71e21df38b3..d359663b9c32 100644
@@ -313,7 +313,7 @@ struct usdt_sema { volatile unsigned short active; };
 #if defined(__ia64__) || defined(__s390__) || defined(__s390x__)
 #define USDT_NOP			nop 0
 #elif defined(__x86_64__)
-#define USDT_NOP                       .byte 0x90, 0x0f, 0x1f, 0x44, 0x00, 0x0 /* nop, nop5 */
+#define USDT_NOP                       .byte 0x90, 0x66, 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 /* nop, nop10 */
 #else
 #define USDT_NOP			nop
 #endif
Signature · 1 line
2.53.0
#17 lore raw

Re: [PATCHv2 06/11] selftests/bpf: Emit nop,nop10 instructions combo for x86_64 arch

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] The patch modifies `USDT_NOP` to emit an 11-byte `nop, nop10` sequence, breaking existing BPF…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The patch modifies USDT_NOP to emit an 11-byte nop, nop10 sequence, breaking existing BPF selftests that explicitly expect nop, nop5.
- [Medium] Libbpf is not updated to recognize the new nop, nop10 sequence, causing the intended uprobe optimization to silently fail. --

commit 1ccfa6afa421e13f88c377be82ec249ff187bf51
Author: Jiri Olsa <[email protected]>

selftests/bpf: Emit nop,nop10 instructions combo for x86_64 arch

This commit updates the USDT_NOP macro in the BPF selftests to use a nop,nop10 instruction sequence instead of nop,nop5 for x86_64. This is intended to support nop10 optimization testing.

tools/testing/selftests/bpf/usdt.h struct usdt_sema { volatile unsigned short active; }; · line 316

Quoted 10 lines
tools/testing/selftests/bpf/usdt.h +1 -1
index c71e21df38b32..d359663b9c329 100644
@@ -313,7 +313,7 @@ struct usdt_sema { volatile unsigned short active; };
 #if defined(__ia64__) || defined(__s390__) || defined(__s390x__)
 #define USDT_NOP			nop 0
 #elif defined(__x86_64__)
-#define USDT_NOP                       .byte 0x90, 0x0f, 0x1f, 0x44, 0x00, 0x0 /* nop, nop5 */
+#define USDT_NOP                       .byte 0x90, 0x66, 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 /* nop, nop10 */

Will this change break the existing BPF selftests that explicitly expect the nop,nop5 sequence?

Looking at tools/testing/selftests/bpf/prog_tests/usdt.c, subtest_optimized_attach() uses find_instr() to search for a hardcoded 6-byte sequence:

    const char nop1_nop5_combo[] = { 0x90, 0x0f, 0x1f, 0x44, 0x00, 0x00 };
    ...
    addr_2 = find_instr(usdt_2, nop1_nop5_combo, sizeof(nop1_nop5_combo));

Because usdt_2.c is compiled with the new 11-byte nop,nop10 sequence, find_instr() will return NULL and cause the ASSERT_OK_PTR() check to fail.

Additionally, does libbpf need to be updated to recognize this new sequence?

In tools/lib/bpf/usdt.c, has_nop_combo() appears to be hardcoded to check only for the 6-byte nop,nop5 sequence.

If libbpf doesn't recognize the new 11-byte sequence, it won't increment the instruction pointer to skip the initial 1-byte nop. This would cause the uprobe to attach to the unoptimized 1-byte nop instead of utilizing the intended optimization.

Quoted 3 lines
tools/testing/selftests/bpf/usdt.h +0 -0
 #else
 #define USDT_NOP			nop
 #endif
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#18 lore raw

Re: [PATCHv2 06/11] selftests/bpf: Emit nop,nop10 instructions combo for x86_64 arch

Quoted 41 lines · On Mon, May 18, 2026 at 11:17:33AM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The patch modifies USDT_NOP to emit an 11-byte nop, nop10 sequence, breaking existing BPF selftests that explicitly expect nop, nop5.
- [Medium] Libbpf is not updated to recognize the new nop, nop10 sequence, causing the intended uprobe optimization to silently fail. --

commit 1ccfa6afa421e13f88c377be82ec249ff187bf51
Author: Jiri Olsa <[email protected]>

selftests/bpf: Emit nop,nop10 instructions combo for x86_64 arch

This commit updates the USDT_NOP macro in the BPF selftests to use a nop,nop10 instruction sequence instead of nop,nop5 for x86_64. This is intended to support nop10 optimization testing.

tools/testing/selftests/bpf/usdt.h struct usdt_sema { volatile unsigned short active; }; · line 316

Quoted 10 lines
tools/testing/selftests/bpf/usdt.h +1 -1
index c71e21df38b32..d359663b9c329 100644
@@ -313,7 +313,7 @@ struct usdt_sema { volatile unsigned short active; };
 #if defined(__ia64__) || defined(__s390__) || defined(__s390x__)
 #define USDT_NOP			nop 0
 #elif defined(__x86_64__)
-#define USDT_NOP                       .byte 0x90, 0x0f, 0x1f, 0x44, 0x00, 0x0 /* nop, nop5 */
+#define USDT_NOP                       .byte 0x90, 0x66, 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 /* nop, nop10 */

Will this change break the existing BPF selftests that explicitly expect the nop,nop5 sequence?

Looking at tools/testing/selftests/bpf/prog_tests/usdt.c, subtest_optimized_attach() uses find_instr() to search for a hardcoded 6-byte sequence:

    const char nop1_nop5_combo[] = { 0x90, 0x0f, 0x1f, 0x44, 0x00, 0x00 };
    ...
    addr_2 = find_instr(usdt_2, nop1_nop5_combo, sizeof(nop1_nop5_combo));

Because usdt_2.c is compiled with the new 11-byte nop,nop10 sequence, find_instr() will return NULL and cause the ASSERT_OK_PTR() check to fail.

Additionally, does libbpf need to be updated to recognize this new sequence?

Quoted 7 lines

In tools/lib/bpf/usdt.c, has_nop_combo() appears to be hardcoded to check only for the 6-byte nop,nop5 sequence.

If libbpf doesn't recognize the new 11-byte sequence, it won't increment the instruction pointer to skip the initial 1-byte nop. This would cause the uprobe to attach to the unoptimized 1-byte nop instead of utilizing the intended optimization.

as I wrote in cover letter I prefer to keep these separated to make the review easier.. we can squash it once we think it's good to go

jirka

author reviewer
#19 lore raw

PATCH v2 7/11 selftests/bpf: Change uprobe syscall tests to use nop10

In reply to Jiri Olsa

Optimized uprobes are now on top of 10-bytes nop instructions, reflect that in existing tests.

---
 .../selftests/bpf/benchs/bench_trigger.c      |  2 +-
 .../selftests/bpf/prog_tests/uprobe_syscall.c | 29 ++++++++++---------
 tools/testing/selftests/bpf/prog_tests/usdt.c | 25 +++++++++-------
 tools/testing/selftests/bpf/usdt_2.c          |  2 +-
 4 files changed, 33 insertions(+), 25 deletions(-)
tools/testing/selftests/bpf/benchs/bench_trigger.c +1 -1
index 2f22ec61667b..bcc4820c802e 100644
@@ -398,7 +398,7 @@ static void *uprobe_producer_ret(void *input)
 #ifdef __x86_64__
 __nocf_check __weak void uprobe_target_nop5(void)
 {
-	asm volatile (".byte 0x0f, 0x1f, 0x44, 0x00, 0x00");
+	asm volatile (".byte 0x66, 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00");
 }
 
 static void *uprobe_producer_nop5(void *input)
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +16 -13
index c944136252c6..e4a19dc9df69 100644
@@ -17,7 +17,7 @@
 #include "uprobe_syscall_executed.skel.h"
 #include "bpf/libbpf_internal.h"
 
-#define USDT_NOP .byte 0x0f, 0x1f, 0x44, 0x00, 0x00
+#define USDT_NOP .byte 0x66, 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00
 #include "usdt.h"
 
 #pragma GCC diagnostic ignored "-Wattributes"
@@ -26,7 +26,7 @@ __attribute__((aligned(16)))
 __nocf_check __weak __naked unsigned long uprobe_regs_trigger(void)
 {
 	asm volatile (
-		".byte 0x0f, 0x1f, 0x44, 0x00, 0x00\n" /* nop5 */
+		".byte 0x66, 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00\n" /* nop10 */
 		"movq $0xdeadbeef, %rax\n"
 		"ret\n"
 	);
@@ -345,9 +345,9 @@ static void test_uretprobe_syscall_call(void)
 __attribute__((aligned(16)))
 __nocf_check __weak __naked void uprobe_test(void)
 {
-	asm volatile ("					\n"
-		".byte 0x0f, 0x1f, 0x44, 0x00, 0x00	\n"
-		"ret					\n"
+	asm volatile (
+		".byte 0x66, 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00\n" /* nop10 */
+		"ret\n"
 	);
 }
 
@@ -388,14 +388,16 @@ static int find_uprobes_trampoline(void *tramp_addr)
 	return ret;
 }
 
-static unsigned char nop5[5] = { 0x0f, 0x1f, 0x44, 0x00, 0x00 };
+static unsigned char jmp2B[2]   = { 0xeb, 8 };
+static unsigned char nop10[10]  = { 0x66, 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 };
+static unsigned char lea_rsp[5] = { 0x48, 0x8d, 0x64, 0x24, 0x80 };
 
-static void *find_nop5(void *fn)
+static void *find_nop10(void *fn)
 {
 	int i;
 
-	for (i = 0; i < 10; i++) {
-		if (!memcmp(nop5, fn + i, 5))
+	for (i = 0; i < 128; i++) {
+		if (!memcmp(nop10, fn + i, 10))
 			return fn + i;
 	}
 	return NULL;
@@ -420,7 +422,8 @@ static void *check_attach(struct uprobe_syscall_executed *skel, trigger_t trigge
 	ASSERT_EQ(skel->bss->executed, executed, "executed");
 
 	/* .. and check the trampoline is as expected. */
-	call = (struct __arch_relative_insn *) addr;
+	ASSERT_OK(memcmp(addr, lea_rsp, 5), "lea_rsp");
+	call = (struct __arch_relative_insn *)(addr + 5);
 	tramp = (void *) (call + 1) + call->raddr;
 	ASSERT_EQ(call->op, 0xe8, "call");
 	ASSERT_OK(find_uprobes_trampoline(tramp), "uprobes_trampoline");
@@ -432,7 +435,7 @@ static void check_detach(void *addr, void *tramp)
 {
 	/* [uprobes_trampoline] stays after detach */
 	ASSERT_OK(find_uprobes_trampoline(tramp), "uprobes_trampoline");
-	ASSERT_OK(memcmp(addr, nop5, 5), "nop5");
+	ASSERT_OK(memcmp(addr, jmp2B, 2), "jmp2B");
 }
 
 static void check(struct uprobe_syscall_executed *skel, struct bpf_link *link,
@@ -568,8 +571,8 @@ static void test_uprobe_usdt(void)
 	void *addr;
 
 	errno = 0;
-	addr = find_nop5(usdt_test);
-	if (!ASSERT_OK_PTR(addr, "find_nop5"))
+	addr = find_nop10(usdt_test);
+	if (!ASSERT_OK_PTR(addr, "find_nop10"))
 		return;
 
 	skel = uprobe_syscall_executed__open_and_load();
tools/testing/selftests/bpf/prog_tests/usdt.c +15 -10
index 69759b27794d..a160d7c4fa0d 100644
@@ -252,7 +252,7 @@ extern void usdt_1(void);
 extern void usdt_2(void);
 
 static unsigned char nop1[1] = { 0x90 };
-static unsigned char nop1_nop5_combo[6] = { 0x90, 0x0f, 0x1f, 0x44, 0x00, 0x00 };
+static unsigned char nop1_nop10_combo[11] = { 0x90, 0x66, 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 };
 
 static void *find_instr(void *fn, unsigned char *instr, size_t cnt)
 {
@@ -271,17 +271,17 @@ static void subtest_optimized_attach(void)
 	__u8 *addr_1, *addr_2;
 
 	/* usdt_1 USDT probe has single nop instruction */
-	addr_1 = find_instr(usdt_1, nop1_nop5_combo, 6);
-	if (!ASSERT_NULL(addr_1, "usdt_1_find_nop1_nop5_combo"))
+	addr_1 = find_instr(usdt_1, nop1_nop10_combo, 11);
+	if (!ASSERT_NULL(addr_1, "usdt_1_find_nop1_nop10_combo"))
 		return;
 
 	addr_1 = find_instr(usdt_1, nop1, 1);
 	if (!ASSERT_OK_PTR(addr_1, "usdt_1_find_nop1"))
 		return;
 
-	/* usdt_2 USDT probe has nop,nop5 instructions combo */
-	addr_2 = find_instr(usdt_2, nop1_nop5_combo, 6);
-	if (!ASSERT_OK_PTR(addr_2, "usdt_2_find_nop1_nop5_combo"))
+	/* usdt_2 USDT probe has nop,nop10 instructions combo */
+	addr_2 = find_instr(usdt_2, nop1_nop10_combo, 11);
+	if (!ASSERT_OK_PTR(addr_2, "usdt_2_find_nop1_nop10_combo"))
 		return;
 
 	skel = test_usdt__open_and_load();
@@ -309,12 +309,12 @@ static void subtest_optimized_attach(void)
 
 	bpf_link__destroy(skel->links.usdt_executed);
 
-	/* we expect the nop5 ip */
+	/* we expect the nop10 ip */
 	skel->bss->expected_ip = (unsigned long) addr_2 + 1;
 
 	/*
 	 * Attach program on top of usdt_2 which is probe defined on top
-	 * of nop1,nop5 combo, so the probe gets optimized on top of nop5.
+	 * of nop1,nop10 combo, so the probe gets optimized on top of nop10.
 	 */
 	skel->links.usdt_executed = bpf_program__attach_usdt(skel->progs.usdt_executed,
 						     0 /*self*/, "/proc/self/exe",
@@ -328,8 +328,13 @@ static void subtest_optimized_attach(void)
 	/* nop stays on addr_2 address */
 	ASSERT_EQ(*addr_2, 0x90, "nop");
 
-	/* call is on addr_2 + 1 address */
-	ASSERT_EQ(*(addr_2 + 1), 0xe8, "call");
+	/*
+	 * lea -0x80(%rsp), %rsp
+	 * call ...
+	 */
+	static unsigned char expected[] = { 0x48, 0x8d, 0x64, 0x24, 0x80, 0xe8 };
+
+	ASSERT_MEMEQ(addr_2 + 1, expected, sizeof(expected), "lea_and_call");
 	ASSERT_EQ(skel->bss->executed, 4, "executed");
 
 cleanup:
tools/testing/selftests/bpf/usdt_2.c +1 -1
index 789883aaca4c..b359b389f6c0 100644
@@ -3,7 +3,7 @@
 #if defined(__x86_64__)
 
 /*
- * Include usdt.h with default nop,nop5 instructions combo.
+ * Include usdt.h with default nop,nop10 instructions combo.
  */
 #include "usdt.h"
 
Signature · 1 line
2.53.0
#20 lore raw

Re: [PATCHv2 07/11] selftests/bpf: Change uprobe syscall tests to use nop10

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Medium] Missing update to USDT_NOP macro causes `subtest_optimized_attach` test failure. - [Medium]…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] Missing update to USDT_NOP macro causes subtest_optimized_attach test failure.
- [Medium] Inadequate search bounds in find_instr() may cause false test failures.
- [Low] Misleading benchmark and function names referencing nop5.
--

commit 1996488ddbe422eb1d441da9b06aafe6bd47e6d1
Author: Jiri Olsa <[email protected]>

selftests/bpf: Change uprobe syscall tests to use nop10

Optimized uprobes are now on top of 10-bytes nop instructions, reflect that in existing tests.

tools/testing/selftests/bpf/benchs/bench_trigger.c static void *uprobe_producer_ret(void *input) · line 404

Quoted 13 lines
tools/testing/selftests/bpf/benchs/bench_trigger.c +1 -1
index 2f22ec61667b9..bcc4820c802e5 100644
@@ -398,7 +398,7 @@ static void *uprobe_producer_ret(void *input)
 #ifdef __x86_64__
 __nocf_check __weak void uprobe_target_nop5(void)
 {
-	asm volatile (".byte 0x0f, 0x1f, 0x44, 0x00, 0x00");
+	asm volatile (".byte 0x66, 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00");
 }
 
 static void *uprobe_producer_nop5(void *input)

This isn't a bug, but should the function and benchmark names also be updated?

The benchmark exposes names like uprobe-nop5 and uretprobe-nop5, but they are now testing a 10-byte nop.

[ ... ]

tools/testing/selftests/bpf/prog_tests/usdt.c extern void usdt_1(void); · line 255

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/usdt.c +1 -1
index 69759b27794d7..a160d7c4fa0d3 100644
@@ -252,7 +252,7 @@ extern void usdt_1(void);
 extern void usdt_2(void);
 
 static unsigned char nop1[1] = { 0x90 };
-static unsigned char nop1_nop5_combo[6] = { 0x90, 0x0f, 0x1f, 0x44, 0x00, 0x00 };
+static unsigned char nop1_nop10_combo[11] = { 0x90, 0x66, 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 };

Is the loop in find_instr() adequate to find this instruction sequence?

tools/testing/selftests/bpf/prog_tests/usdt.c:find_instr() {
	for (i = 0; i < 10; i++) {
		if (!memcmp(instr, fn + i, cnt))
			return fn + i;
	}
	return NULL;
}

In uprobe_syscall.c, find_nop10() was updated to search up to 128 bytes to account for compiler-generated prologues. If a modern compiler generates a prologue longer than 9 bytes for usdt_2(), this will prematurely terminate and cause the subtest_optimized_attach() test to fail.

[ ... ]

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/usdt.c +2 -2
@@ -271,17 +271,17 @@ static void subtest_optimized_attach(void)
 	__u8 *addr_1, *addr_2;
 
 	/* usdt_1 USDT probe has single nop instruction */
-	addr_1 = find_instr(usdt_1, nop1_nop5_combo, 6);
-	if (!ASSERT_NULL(addr_1, "usdt_1_find_nop1_nop5_combo"))
+	addr_1 = find_instr(usdt_1, nop1_nop10_combo, 11);
+	if (!ASSERT_NULL(addr_1, "usdt_1_find_nop1_nop10_combo"))
 		return;
 

tools/testing/selftests/bpf/prog_tests/usdt.c static void subtest_optimized_attach(void) · line 284

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/usdt.c +3 -3
 	addr_1 = find_instr(usdt_1, nop1, 1);
 	if (!ASSERT_OK_PTR(addr_1, "usdt_1_find_nop1"))
 		return;
 
-	/* usdt_2 USDT probe has nop,nop5 instructions combo */
-	addr_2 = find_instr(usdt_2, nop1_nop5_combo, 6);
-	if (!ASSERT_OK_PTR(addr_2, "usdt_2_find_nop1_nop5_combo"))
+	/* usdt_2 USDT probe has nop,nop10 instructions combo */
+	addr_2 = find_instr(usdt_2, nop1_nop10_combo, 11);
+	if (!ASSERT_OK_PTR(addr_2, "usdt_2_find_nop1_nop10_combo"))

Does this test fail because USDT_NOP was not updated in usdt.h?

The test expects to find the 11-byte nop1_nop10_combo, but usdt_2.c relies on the default definition of USDT_NOP from tools/testing/selftests/bpf/usdt.h.

Because usdt.h was not updated in this patch, usdt_2.c will compile with the 6-byte nop sequence, causing find_instr() to return NULL here and the ASSERT_OK_PTR to fail.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#21 lore raw

Re: [PATCHv2 07/11] selftests/bpf: Change uprobe syscall tests to use nop10

Quoted 26 lines · On Mon, May 18, 2026 at 11:16:04AM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] Missing update to USDT_NOP macro causes subtest_optimized_attach test failure.
- [Medium] Inadequate search bounds in find_instr() may cause false test failures.
- [Low] Misleading benchmark and function names referencing nop5.
--

commit 1996488ddbe422eb1d441da9b06aafe6bd47e6d1
Author: Jiri Olsa <[email protected]>

selftests/bpf: Change uprobe syscall tests to use nop10

Optimized uprobes are now on top of 10-bytes nop instructions, reflect that in existing tests.

Quoted 12 lines
tools/testing/selftests/bpf/benchs/bench_trigger.c +1 -1
index 2f22ec61667b9..bcc4820c802e5 100644
@@ -398,7 +398,7 @@ static void *uprobe_producer_ret(void *input)
 #ifdef __x86_64__
 __nocf_check __weak void uprobe_target_nop5(void)
 {
-	asm volatile (".byte 0x0f, 0x1f, 0x44, 0x00, 0x00");
+	asm volatile (".byte 0x66, 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00");
 }
 
Quoted 6 lines

tools/testing/selftests/bpf/benchs/bench_trigger.c static void *uprobe_producer_ret(void *input) · line 404

Quoted 1 line
tools/testing/selftests/bpf/benchs/bench_trigger.c +0 -0
 static void *uprobe_producer_nop5(void *input)

This isn't a bug, but should the function and benchmark names also be updated?

The benchmark exposes names like uprobe-nop5 and uretprobe-nop5, but they are now testing a 10-byte nop.

they are updated in following separate commit

Quoted 24 lines

[ ... ]

tools/testing/selftests/bpf/prog_tests/usdt.c extern void usdt_1(void); · line 255

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/usdt.c +1 -1
index 69759b27794d7..a160d7c4fa0d3 100644
@@ -252,7 +252,7 @@ extern void usdt_1(void);
 extern void usdt_2(void);
 
 static unsigned char nop1[1] = { 0x90 };
-static unsigned char nop1_nop5_combo[6] = { 0x90, 0x0f, 0x1f, 0x44, 0x00, 0x00 };
+static unsigned char nop1_nop10_combo[11] = { 0x90, 0x66, 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 };

Is the loop in find_instr() adequate to find this instruction sequence?

tools/testing/selftests/bpf/prog_tests/usdt.c:find_instr() {
	for (i = 0; i < 10; i++) {
		if (!memcmp(instr, fn + i, cnt))
			return fn + i;
	}
	return NULL;
}
Quoted 4 lines

In uprobe_syscall.c, find_nop10() was updated to search up to 128 bytes to account for compiler-generated prologues. If a modern compiler generates a prologue longer than 9 bytes for usdt_2(), this will prematurely terminate and cause the subtest_optimized_attach() test to fail.

yea find_instr needs same update, will fix

Quoted 26 lines

[ ... ]

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/usdt.c +2 -2
@@ -271,17 +271,17 @@ static void subtest_optimized_attach(void)
 	__u8 *addr_1, *addr_2;
 
 	/* usdt_1 USDT probe has single nop instruction */
-	addr_1 = find_instr(usdt_1, nop1_nop5_combo, 6);
-	if (!ASSERT_NULL(addr_1, "usdt_1_find_nop1_nop5_combo"))
+	addr_1 = find_instr(usdt_1, nop1_nop10_combo, 11);
+	if (!ASSERT_NULL(addr_1, "usdt_1_find_nop1_nop10_combo"))
 		return;
 

tools/testing/selftests/bpf/prog_tests/usdt.c static void subtest_optimized_attach(void) · line 284

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/usdt.c +3 -3
 	addr_1 = find_instr(usdt_1, nop1, 1);
 	if (!ASSERT_OK_PTR(addr_1, "usdt_1_find_nop1"))
 		return;
 
-	/* usdt_2 USDT probe has nop,nop5 instructions combo */
-	addr_2 = find_instr(usdt_2, nop1_nop5_combo, 6);
-	if (!ASSERT_OK_PTR(addr_2, "usdt_2_find_nop1_nop5_combo"))
+	/* usdt_2 USDT probe has nop,nop10 instructions combo */
+	addr_2 = find_instr(usdt_2, nop1_nop10_combo, 11);
+	if (!ASSERT_OK_PTR(addr_2, "usdt_2_find_nop1_nop10_combo"))

Does this test fail because USDT_NOP was not updated in usdt.h?

Quoted 6 lines

The test expects to find the 11-byte nop1_nop10_combo, but usdt_2.c relies on the default definition of USDT_NOP from tools/testing/selftests/bpf/usdt.h.

Because usdt.h was not updated in this patch, usdt_2.c will compile with the 6-byte nop sequence, causing find_instr() to return NULL here and the ASSERT_OK_PTR to fail.

it's updated separately in another commit

jirka

#22 lore raw

Re: [PATCHv2 07/11] selftests/bpf: Change uprobe syscall tests to use nop10

In reply to Jiri Olsa
This isn't a bug, but should the function and benchmark names also be updated? The function is still called uprobe_target_nop5() and the benchmark exposes names like uprobe-nop5, uretprobe-nop5…

tools/testing/selftests/bpf/benchs/bench_trigger.c static void *uprobe_producer_ret(void *input) · line 404

Quoted 13 lines
tools/testing/selftests/bpf/benchs/bench_trigger.c +1 -1
index 2f22ec61667b..bcc4820c802e 100644
@@ -398,7 +398,7 @@ static void *uprobe_producer_ret(void *input)
 #ifdef __x86_64__
 __nocf_check __weak void uprobe_target_nop5(void)
 {
-	asm volatile (".byte 0x0f, 0x1f, 0x44, 0x00, 0x00");
+	asm volatile (".byte 0x66, 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00");
 }
 
 static void *uprobe_producer_nop5(void *input)

This isn't a bug, but should the function and benchmark names also be updated? The function is still called uprobe_target_nop5() and the benchmark exposes names like uprobe-nop5, uretprobe-nop5, uprobe-multi-nop5, uretprobe-multi-nop5, and usdt-nop5, but they are now all testing a 10-byte nop instruction.

[ ... ]

Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/usdt.c +1 -1
index 69759b27794d..a160d7c4fa0d 100644
@@ -252,7 +252,7 @@ extern void usdt_1(void);
 extern void usdt_2(void);
 
 static unsigned char nop1[1] = { 0x90 };
-static unsigned char nop1_nop5_combo[6] = { 0x90, 0x0f, 0x1f, 0x44, 0x00, 0x00 };
+static unsigned char nop1_nop10_combo[11] = { 0x90, 0x66, 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 };
 
 static void *find_instr(void *fn, unsigned char *instr, size_t cnt)
 {

tools/testing/selftests/bpf/prog_tests/usdt.c

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/usdt.c +0 -0
 	int i;
 
 	for (i = 0; i < 10; i++) {
 		if (!memcmp(instr, fn + i, cnt))
 			return fn + i;
 	}
 	return NULL;
 }

Is the loop in find_instr() adequate to find this instruction sequence? In uprobe_syscall.c, find_nop10() was updated to search up to 128 bytes to account for compiler-generated prologues. If a modern compiler generates a prologue longer than 9 bytes for usdt_2(), this will prematurely terminate and cause the subtest_optimized_attach() test to fail.

[ ... ]

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/26030080109

author reviewer
#23 lore raw

PATCH v2 8/11 selftests/bpf: Change uprobe/usdt trigger bench code to use nop10

In reply to Jiri Olsa

Changing uprobe/usdt trigger bench code to use nop10 instead of nop5. Also changing un_bench_uprobes.sh to use nop10 triggers.

---
 tools/testing/selftests/bpf/bench.c           | 20 +++++------
 .../selftests/bpf/benchs/bench_trigger.c      | 36 +++++++++----------
 .../selftests/bpf/benchs/run_bench_uprobes.sh |  2 +-
 3 files changed, 29 insertions(+), 29 deletions(-)
tools/testing/selftests/bpf/bench.c +10 -10
index 6155ce455c27..1252a1af2e84 100644
@@ -539,12 +539,12 @@ extern const struct bench bench_trig_uretprobe_multi_push;
 extern const struct bench bench_trig_uprobe_multi_ret;
 extern const struct bench bench_trig_uretprobe_multi_ret;
 #ifdef __x86_64__
-extern const struct bench bench_trig_uprobe_nop5;
-extern const struct bench bench_trig_uretprobe_nop5;
-extern const struct bench bench_trig_uprobe_multi_nop5;
-extern const struct bench bench_trig_uretprobe_multi_nop5;
+extern const struct bench bench_trig_uprobe_nop10;
+extern const struct bench bench_trig_uretprobe_nop10;
+extern const struct bench bench_trig_uprobe_multi_nop10;
+extern const struct bench bench_trig_uretprobe_multi_nop10;
 extern const struct bench bench_trig_usdt_nop;
-extern const struct bench bench_trig_usdt_nop5;
+extern const struct bench bench_trig_usdt_nop10;
 #endif
 
 extern const struct bench bench_rb_libbpf;
@@ -619,12 +619,12 @@ static const struct bench *benchs[] = {
 	&bench_trig_uprobe_multi_ret,
 	&bench_trig_uretprobe_multi_ret,
 #ifdef __x86_64__
-	&bench_trig_uprobe_nop5,
-	&bench_trig_uretprobe_nop5,
-	&bench_trig_uprobe_multi_nop5,
-	&bench_trig_uretprobe_multi_nop5,
+	&bench_trig_uprobe_nop10,
+	&bench_trig_uretprobe_nop10,
+	&bench_trig_uprobe_multi_nop10,
+	&bench_trig_uretprobe_multi_nop10,
 	&bench_trig_usdt_nop,
-	&bench_trig_usdt_nop5,
+	&bench_trig_usdt_nop10,
 #endif
 	/* ringbuf/perfbuf benchmarks */
 	&bench_rb_libbpf,
tools/testing/selftests/bpf/benchs/bench_trigger.c +18 -18
index bcc4820c802e..3998ea8ff9aa 100644
@@ -396,15 +396,15 @@ static void *uprobe_producer_ret(void *input)
 }
 
 #ifdef __x86_64__
-__nocf_check __weak void uprobe_target_nop5(void)
+__nocf_check __weak void uprobe_target_nop10(void)
 {
 	asm volatile (".byte 0x66, 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00");
 }
 
-static void *uprobe_producer_nop5(void *input)
+static void *uprobe_producer_nop10(void *input)
 {
 	while (true)
-		uprobe_target_nop5();
+		uprobe_target_nop10();
 	return NULL;
 }
 
@@ -418,7 +418,7 @@ static void *uprobe_producer_usdt_nop(void *input)
 	return NULL;
 }
 
-static void *uprobe_producer_usdt_nop5(void *input)
+static void *uprobe_producer_usdt_nop10(void *input)
 {
 	while (true)
 		usdt_2();
@@ -542,24 +542,24 @@ static void uretprobe_multi_ret_setup(void)
 }
 
 #ifdef __x86_64__
-static void uprobe_nop5_setup(void)
+static void uprobe_nop10_setup(void)
 {
-	usetup(false, false /* !use_multi */, &uprobe_target_nop5);
+	usetup(false, false /* !use_multi */, &uprobe_target_nop10);
 }
 
-static void uretprobe_nop5_setup(void)
+static void uretprobe_nop10_setup(void)
 {
-	usetup(true, false /* !use_multi */, &uprobe_target_nop5);
+	usetup(true, false /* !use_multi */, &uprobe_target_nop10);
 }
 
-static void uprobe_multi_nop5_setup(void)
+static void uprobe_multi_nop10_setup(void)
 {
-	usetup(false, true /* use_multi */, &uprobe_target_nop5);
+	usetup(false, true /* use_multi */, &uprobe_target_nop10);
 }
 
-static void uretprobe_multi_nop5_setup(void)
+static void uretprobe_multi_nop10_setup(void)
 {
-	usetup(true, true /* use_multi */, &uprobe_target_nop5);
+	usetup(true, true /* use_multi */, &uprobe_target_nop10);
 }
 
 static void usdt_setup(const char *name)
@@ -598,7 +598,7 @@ static void usdt_nop_setup(void)
 	usdt_setup("usdt_1");
 }
 
-static void usdt_nop5_setup(void)
+static void usdt_nop10_setup(void)
 {
 	usdt_setup("usdt_2");
 }
@@ -665,10 +665,10 @@ BENCH_TRIG_USERMODE(uretprobe_multi_nop, nop, "uretprobe-multi-nop");
 BENCH_TRIG_USERMODE(uretprobe_multi_push, push, "uretprobe-multi-push");
 BENCH_TRIG_USERMODE(uretprobe_multi_ret, ret, "uretprobe-multi-ret");
 #ifdef __x86_64__
-BENCH_TRIG_USERMODE(uprobe_nop5, nop5, "uprobe-nop5");
-BENCH_TRIG_USERMODE(uretprobe_nop5, nop5, "uretprobe-nop5");
-BENCH_TRIG_USERMODE(uprobe_multi_nop5, nop5, "uprobe-multi-nop5");
-BENCH_TRIG_USERMODE(uretprobe_multi_nop5, nop5, "uretprobe-multi-nop5");
+BENCH_TRIG_USERMODE(uprobe_nop10, nop10, "uprobe-nop10");
+BENCH_TRIG_USERMODE(uretprobe_nop10, nop10, "uretprobe-nop10");
+BENCH_TRIG_USERMODE(uprobe_multi_nop10, nop10, "uprobe-multi-nop10");
+BENCH_TRIG_USERMODE(uretprobe_multi_nop10, nop10, "uretprobe-multi-nop10");
 BENCH_TRIG_USERMODE(usdt_nop, usdt_nop, "usdt-nop");
-BENCH_TRIG_USERMODE(usdt_nop5, usdt_nop5, "usdt-nop5");
+BENCH_TRIG_USERMODE(usdt_nop10, usdt_nop10, "usdt-nop10");
 #endif
tools/testing/selftests/bpf/benchs/run_bench_uprobes.sh +1 -1
index 9ec59423b949..e490b337e960 100755
@@ -2,7 +2,7 @@
 
 set -eufo pipefail
 
-for i in usermode-count syscall-count {uprobe,uretprobe}-{nop,push,ret,nop5} usdt-nop usdt-nop5
+for i in usermode-count syscall-count {uprobe,uretprobe}-{nop,push,ret,nop10} usdt-nop usdt-nop10
 do
 	summary=$(sudo ./bench -w2 -d5 -a trig-$i | tail -n1 | cut -d'(' -f1 | cut -d' ' -f3-)
 	printf "%-15s: %s\n" $i "$summary"
Signature · 1 line
2.53.0
#24 lore raw

Re: [PATCHv2 08/11] selftests/bpf: Change uprobe/usdt trigger bench code to use nop10

This isn't a bug, but there's a typo in the commit message: the filename is written as "un_bench_uprobes.sh" but should be "run_bench_uprobes.sh" (starting with 'r').
Quoted 6 lines

selftests/bpf: Change uprobe/usdt trigger bench code to use nop10

Changing uprobe/usdt trigger bench code to use nop10 instead of nop5. Also changing un_bench_uprobes.sh to use nop10 triggers.

This isn't a bug, but there's a typo in the commit message: the filename is written as "un_bench_uprobes.sh" but should be "run_bench_uprobes.sh" (starting with 'r').

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/26030080109

author reviewer
#25 lore raw

PATCH v2 9/11 selftests/bpf: Add reattach tests for uprobe syscall

In reply to Jiri Olsa

Adding reattach tests for uprobe syscall tests to make sure we can re-attach and optimize same uprobe multiple times.

The reason is that optimized uprobe does not restore original nop10 after detach, but instead it uses 'jmp 8' instruction.

Making sure we can still install and optimize uprobe on top of the 'jmp 8' instruction.

---
 .../selftests/bpf/prog_tests/uprobe_syscall.c | 115 ++++++++++++++++--
 1 file changed, 105 insertions(+), 10 deletions(-)
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +105 -10
index e4a19dc9df69..e88b316a3f2c 100644
@@ -431,21 +431,27 @@ static void *check_attach(struct uprobe_syscall_executed *skel, trigger_t trigge
 	return tramp;
 }
 
-static void check_detach(void *addr, void *tramp)
+static bool check_detach(void *addr, void *tramp)
 {
+	bool ok = true;
+
 	/* [uprobes_trampoline] stays after detach */
-	ASSERT_OK(find_uprobes_trampoline(tramp), "uprobes_trampoline");
-	ASSERT_OK(memcmp(addr, jmp2B, 2), "jmp2B");
+	if (!ASSERT_OK(find_uprobes_trampoline(tramp), "uprobes_trampoline"))
+		ok = false;
+	if (!ASSERT_OK(memcmp(addr, jmp2B, 2), "jmp2B"))
+		ok = false;
+	return ok;
 }
 
-static void check(struct uprobe_syscall_executed *skel, struct bpf_link *link,
-		  trigger_t trigger, void *addr, int executed)
+static void *check(struct uprobe_syscall_executed *skel, struct bpf_link *link,
+		   trigger_t trigger, void *addr, int executed)
 {
 	void *tramp;
 
 	tramp = check_attach(skel, trigger, addr, executed);
 	bpf_link__destroy(link);
 	check_detach(addr, tramp);
+	return tramp;
 }
 
 static void test_uprobe_legacy(void)
@@ -456,6 +462,7 @@ static void test_uprobe_legacy(void)
 	);
 	struct bpf_link *link;
 	unsigned long offset;
+	void *tramp;
 
 	offset = get_uprobe_offset(&uprobe_test);
 	if (!ASSERT_GE(offset, 0, "get_uprobe_offset"))
@@ -473,7 +480,28 @@ static void test_uprobe_legacy(void)
 	if (!ASSERT_OK_PTR(link, "bpf_program__attach_uprobe_opts"))
 		goto cleanup;
 
-	check(skel, link, uprobe_test, uprobe_test, 2);
+	tramp = check(skel, link, uprobe_test, uprobe_test, 2);
+
+	/* reattach and detach without triggering optimization */
+	link = bpf_program__attach_uprobe_opts(skel->progs.test_uprobe,
+					       0, "/proc/self/exe", offset, NULL);
+	if (!ASSERT_OK_PTR(link, "bpf_program__attach_uprobe_opts"))
+		goto cleanup;
+
+	bpf_link__destroy(link);
+	if (!check_detach(uprobe_test, tramp))
+		goto cleanup;
+
+	uprobe_test();
+	ASSERT_EQ(skel->bss->executed, 2, "executed_no_probe");
+
+	/* reattach with triggering optimization */
+	link = bpf_program__attach_uprobe_opts(skel->progs.test_uprobe,
+				0, "/proc/self/exe", offset, NULL);
+	if (!ASSERT_OK_PTR(link, "bpf_program__attach_uprobe_opts"))
+		goto cleanup;
+
+	check(skel, link, uprobe_test, uprobe_test, 4);
 
 	/* uretprobe */
 	skel->bss->executed = 0;
@@ -495,6 +523,7 @@ static void test_uprobe_multi(void)
 	LIBBPF_OPTS(bpf_uprobe_multi_opts, opts);
 	struct bpf_link *link;
 	unsigned long offset;
+	void *tramp;
 
 	offset = get_uprobe_offset(&uprobe_test);
 	if (!ASSERT_GE(offset, 0, "get_uprobe_offset"))
@@ -515,7 +544,28 @@ static void test_uprobe_multi(void)
 	if (!ASSERT_OK_PTR(link, "bpf_program__attach_uprobe_multi"))
 		goto cleanup;
 
-	check(skel, link, uprobe_test, uprobe_test, 2);
+	tramp = check(skel, link, uprobe_test, uprobe_test, 2);
+
+	/* reattach and detach without triggering optimization */
+	link = bpf_program__attach_uprobe_multi(skel->progs.test_uprobe_multi,
+				0, "/proc/self/exe", NULL, &opts);
+	if (!ASSERT_OK_PTR(link, "bpf_program__attach_uprobe_multi"))
+		goto cleanup;
+
+	bpf_link__destroy(link);
+	if (!check_detach(uprobe_test, tramp))
+		goto cleanup;
+
+	uprobe_test();
+	ASSERT_EQ(skel->bss->executed, 2, "executed_no_probe");
+
+	/* reattach with triggering optimization */
+	link = bpf_program__attach_uprobe_multi(skel->progs.test_uprobe_multi,
+				0, "/proc/self/exe", NULL, &opts);
+	if (!ASSERT_OK_PTR(link, "bpf_program__attach_uprobe_multi"))
+		goto cleanup;
+
+	check(skel, link, uprobe_test, uprobe_test, 4);
 
 	/* uretprobe.multi */
 	skel->bss->executed = 0;
@@ -539,6 +589,7 @@ static void test_uprobe_session(void)
 	);
 	struct bpf_link *link;
 	unsigned long offset;
+	void *tramp;
 
 	offset = get_uprobe_offset(&uprobe_test);
 	if (!ASSERT_GE(offset, 0, "get_uprobe_offset"))
@@ -558,7 +609,28 @@ static void test_uprobe_session(void)
 	if (!ASSERT_OK_PTR(link, "bpf_program__attach_uprobe_multi"))
 		goto cleanup;
 
-	check(skel, link, uprobe_test, uprobe_test, 4);
+	tramp = check(skel, link, uprobe_test, uprobe_test, 4);
+
+	/* reattach and detach without triggering optimization */
+	link = bpf_program__attach_uprobe_multi(skel->progs.test_uprobe_session,
+				0, "/proc/self/exe", NULL, &opts);
+	if (!ASSERT_OK_PTR(link, "bpf_program__attach_uprobe_multi"))
+		goto cleanup;
+
+	bpf_link__destroy(link);
+	if (!check_detach(uprobe_test, tramp))
+		goto cleanup;
+
+	uprobe_test();
+	ASSERT_EQ(skel->bss->executed, 4, "executed_no_probe");
+
+	/* reattach with triggering optimization */
+	link = bpf_program__attach_uprobe_multi(skel->progs.test_uprobe_session,
+				0, "/proc/self/exe", NULL, &opts);
+	if (!ASSERT_OK_PTR(link, "bpf_program__attach_uprobe_multi"))
+		goto cleanup;
+
+	check(skel, link, uprobe_test, uprobe_test, 8);
 
 cleanup:
 	uprobe_syscall_executed__destroy(skel);
@@ -568,7 +640,7 @@ static void test_uprobe_usdt(void)
 {
 	struct uprobe_syscall_executed *skel;
 	struct bpf_link *link;
-	void *addr;
+	void *addr, *tramp;
 
 	errno = 0;
 	addr = find_nop10(usdt_test);
@@ -587,7 +659,30 @@ static void test_uprobe_usdt(void)
 	if (!ASSERT_OK_PTR(link, "bpf_program__attach_usdt"))
 		goto cleanup;
 
-	check(skel, link, usdt_test, addr, 2);
+	tramp = check(skel, link, usdt_test, addr, 2);
+
+	/* reattach and detach without triggering optimization */
+	link = bpf_program__attach_usdt(skel->progs.test_usdt,
+				-1 /* all PIDs */, "/proc/self/exe",
+				"optimized_uprobe", "usdt", NULL);
+	if (!ASSERT_OK_PTR(link, "bpf_program__attach_usdt"))
+		goto cleanup;
+
+	bpf_link__destroy(link);
+	if (!check_detach(addr, tramp))
+		goto cleanup;
+
+	usdt_test();
+	ASSERT_EQ(skel->bss->executed, 2, "executed_no_probe");
+
+	/* reattach with triggering optimization */
+	link = bpf_program__attach_usdt(skel->progs.test_usdt,
+				-1 /* all PIDs */, "/proc/self/exe",
+				"optimized_uprobe", "usdt", NULL);
+	if (!ASSERT_OK_PTR(link, "bpf_program__attach_usdt"))
+		goto cleanup;
+
+	check(skel, link, usdt_test, addr, 4);
 
 cleanup:
 	uprobe_syscall_executed__destroy(skel);
Signature · 1 line
2.53.0
author reviewer
#26 lore raw

PATCH v2 10/11 selftests/bpf: Add tests for uprobe nop10 red zone clobbering

In reply to Jiri Olsa

From: Andrii Nakryiko <[email protected]>

The uprobe nop5 optimization used to replace a 5-byte NOP with a 5-byte CALL to a trampoline. The CALL pushes a return address onto the stack at [rsp-8], clobbering whatever was stored there.

On x86-64, the red zone is the 128 bytes below rsp that user code may use for temporary storage without adjusting rsp. Compilers can place USDT argument operands there, generating specs like "8@-8(%rbp)" when rbp == rsp. With the CALL-based optimization, the return address overwrites that argument before the BPF-side USDT argument fetch runs.

Add two tests for this case. The uprobe_syscall subtest stores known values at -8(%rsp), -16(%rsp), and -24(%rsp), executes an optimized nop10 uprobe, and verifies the red-zone data is still intact. The USDT subtest triggers a probe in a function where the compiler places three USDT operands in the red zone and verifies that all 10 optimized invocations deliver the expected argument values to BPF.

On an unfixed kernel, the first hit goes through the INT3 path and later hits use the optimized CALL path, so the red-zone checks fail after optimization.

[ updates to use nop10 ]
---
 .../selftests/bpf/prog_tests/uprobe_syscall.c | 75 +++++++++++++++++++
 tools/testing/selftests/bpf/prog_tests/usdt.c | 49 ++++++++++++
 tools/testing/selftests/bpf/progs/test_usdt.c | 25 +++++++
 tools/testing/selftests/bpf/usdt_2.c          | 13 ++++
 4 files changed, 162 insertions(+)
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +75 -0
index e88b316a3f2c..739b0be13aa3 100644
@@ -357,6 +357,48 @@ __nocf_check __weak void usdt_test(void)
 	USDT(optimized_uprobe, usdt);
 }
 
+/*
+ * Assembly-level red zone clobbering test. Stores known values in the
+ * red zone (below RSP), executes a nop10 (uprobe site), and checks that
+ * the values survived. Returns 0 if intact, 1 if clobbered.
+ *
+ * The nop5 optimization used CALL (which pushes a return address to
+ * [rsp-8]), the value at -8(%rsp) was overwritten. The nop10 optimization
+ * should escape that by moving stackpointer below the redzone before
+ * doing the CALL.
+ */
+__attribute__((aligned(16)))
+__nocf_check __weak __naked unsigned long uprobe_red_zone_test(void)
+{
+	asm volatile (
+		"movabs $0x1111111111111111, %%rax\n"
+		"movq   %%rax, -8(%%rsp)\n"
+		"movabs $0x2222222222222222, %%rax\n"
+		"movq   %%rax, -16(%%rsp)\n"
+		"movabs $0x3333333333333333, %%rax\n"
+		"movq   %%rax, -24(%%rsp)\n"
+
+		".byte 0x66, 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00\n" /* nop10: uprobe site */
+
+		"movabs $0x1111111111111111, %%rax\n"
+		"cmpq   %%rax, -8(%%rsp)\n"
+		"jne    1f\n"
+		"movabs $0x2222222222222222, %%rax\n"
+		"cmpq   %%rax, -16(%%rsp)\n"
+		"jne    1f\n"
+		"movabs $0x3333333333333333, %%rax\n"
+		"cmpq   %%rax, -24(%%rsp)\n"
+		"jne    1f\n"
+
+		"xorl   %%eax, %%eax\n"
+		"retq\n"
+		"1:\n"
+		"movl   $1, %%eax\n"
+		"retq\n"
+		::: "rax", "memory"
+	);
+}
+
 static int find_uprobes_trampoline(void *tramp_addr)
 {
 	void *start, *end;
@@ -855,6 +897,37 @@ static void test_uprobe_race(void)
 #define __NR_uprobe 336
 #endif
 
+static void test_uprobe_red_zone(void)
+{
+	struct uprobe_syscall_executed *skel;
+	struct bpf_link *link;
+	void *nop10_addr;
+	size_t offset;
+	int i;
+
+	nop10_addr = find_nop10(uprobe_red_zone_test);
+	if (!ASSERT_NEQ(nop10_addr, NULL, "find_nop10"))
+		return;
+
+	skel = uprobe_syscall_executed__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+
+	offset = get_uprobe_offset(nop10_addr);
+	link = bpf_program__attach_uprobe_opts(skel->progs.test_uprobe,
+			0, "/proc/self/exe", offset, NULL);
+	if (!ASSERT_OK_PTR(link, "attach_uprobe"))
+		goto cleanup;
+
+	for (i = 0; i < 10; i++)
+		ASSERT_EQ(uprobe_red_zone_test(), 0, "red_zone_intact");
+
+	bpf_link__destroy(link);
+
+cleanup:
+	uprobe_syscall_executed__destroy(skel);
+}
+
 static void test_uprobe_error(void)
 {
 	long err = syscall(__NR_uprobe);
@@ -881,6 +954,8 @@ static void __test_uprobe_syscall(void)
 		test_uprobe_usdt();
 	if (test__start_subtest("uprobe_race"))
 		test_uprobe_race();
+	if (test__start_subtest("uprobe_red_zone"))
+		test_uprobe_red_zone();
 	if (test__start_subtest("uprobe_error"))
 		test_uprobe_error();
 	if (test__start_subtest("uprobe_regs_equal"))
tools/testing/selftests/bpf/prog_tests/usdt.c +49 -0

67 lines not on this page · raw message

tools/testing/selftests/bpf/progs/test_usdt.c +25 -0
index f00cb52874e0..0ee78fb050a1 100644
@@ -149,5 +149,30 @@ int usdt_executed(struct pt_regs *ctx)
 		executed++;
 	return 0;
 }
+
+int arg_total;
+int arg_bad;
+long arg_last[3];
+long expected_arg[3];
+int expected_pid;
+
+SEC("usdt")
+int BPF_USDT(usdt_check_arg, long arg1, long arg2, long arg3)
+{
+	if (expected_pid != (bpf_get_current_pid_tgid() >> 32))
+		return 0;
+
+	__sync_fetch_and_add(&arg_total, 1);
+	arg_last[0] = arg1;
+	arg_last[1] = arg2;
+	arg_last[2] = arg3;
+
+	if (arg1 != expected_arg[0] ||
+	    arg2 != expected_arg[1] ||
+	    arg3 != expected_arg[2])
+		__sync_fetch_and_add(&arg_bad, 1);
+
+	return 0;
+}
 #endif
 char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/usdt_2.c +13 -0
index b359b389f6c0..5e38f8605b02 100644
@@ -13,4 +13,17 @@ void usdt_2(void)
 	USDT(optimized_attach, usdt_2);
 }
 
+static volatile unsigned long usdt_red_zone_arg1 = 0xDEADBEEF;
+static volatile unsigned long usdt_red_zone_arg2 = 0xCAFEBABE;
+static volatile unsigned long usdt_red_zone_arg3 = 0xFEEDFACE;
+
+void __attribute__((noinline)) usdt_red_zone_trigger(void)
+{
+	unsigned long a1 = usdt_red_zone_arg1;
+	unsigned long a2 = usdt_red_zone_arg2;
+	unsigned long a3 = usdt_red_zone_arg3;
+
+	USDT(optimized_attach, usdt_red_zone, a1, a2, a3);
+}
+
 #endif
Signature · 1 line
2.53.0
author reviewer
#27 lore raw

PATCH v2 11/11 selftests/bpf: Add tests for forked/cloned optimized uprobes

In reply to Jiri Olsa

Adding tests for forked/cloned optimized uprobes and make sure the child can properly execute optimized probe for both fork (dups mm) and clone with CLONE_VM.

---
 .../selftests/bpf/prog_tests/uprobe_syscall.c | 88 +++++++++++++++++++
 1 file changed, 88 insertions(+)
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +88 -0
index 739b0be13aa3..713b42d4f190 100644
@@ -4,6 +4,8 @@
 
 #ifdef __x86_64__
 
+#define _GNU_SOURCE
+#include <sched.h>
 #include <unistd.h>
 #include <asm/ptrace.h>
 #include <linux/compiler.h>
@@ -936,6 +938,88 @@ static void test_uprobe_error(void)
 	ASSERT_EQ(errno, EPROTO, "errno");
 }
 
+__attribute__((aligned(16)))
+__nocf_check __weak __naked void uprobe_fork_test(void)
+{
+	asm volatile (
+		".byte 0x66, 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00\n" /* nop10 */
+		"ret\n"
+	);
+}
+
+static int child_func(void *arg)
+{
+	struct uprobe_syscall_executed *skel = arg;
+
+	/* Make sure the child's probe is still there and optimized.. */
+	if (memcmp(uprobe_fork_test, lea_rsp, sizeof(lea_rsp)))
+		_exit(1);
+
+	skel->bss->pid = getpid();
+
+	/* .. and it executes properly. */
+	uprobe_fork_test();
+
+	if (skel->bss->executed != 3)
+		_exit(2);
+
+	_exit(0);
+}
+
+static void test_uprobe_fork_optimized(bool clone_vm)
+{
+	struct uprobe_syscall_executed *skel = NULL;
+	struct bpf_link *link = NULL;
+	unsigned long offset;
+	int pid, status, err;
+	char stack[65535];
+
+	offset = get_uprobe_offset(&uprobe_fork_test);
+	if (!ASSERT_GE(offset, 0, "get_uprobe_offset"))
+		return;
+
+	skel = uprobe_syscall_executed__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		goto cleanup;
+
+	link = bpf_program__attach_uprobe_opts(skel->progs.test_uprobe,
+				-1, "/proc/self/exe", offset, NULL);
+	if (!ASSERT_OK_PTR(link, "attach_uprobe"))
+		goto cleanup;
+
+	skel->bss->pid = getpid();
+
+	/* Trigger optimization of uprobe in uprobe_fork_test.  */
+	uprobe_fork_test();
+	uprobe_fork_test();
+
+	/* Make sure it got optimied. */
+	if (!ASSERT_OK(memcmp(uprobe_fork_test, lea_rsp, sizeof(lea_rsp)), "optimized"))
+		goto cleanup;
+
+	if (clone_vm) {
+		pid = clone(child_func, stack + sizeof(stack), CLONE_VM|SIGCHLD, skel);
+		if (!ASSERT_GT(pid, 0, "clone"))
+			goto cleanup;
+	} else {
+		pid = fork();
+		if (!ASSERT_GE(pid, 0, "fork"))
+			goto cleanup;
+		if (pid == 0)
+			child_func(skel);
+	}
+
+	/* Wait for the child and verify it exited properly with 0. */
+	err = waitpid(pid, &status, 0);
+	if (ASSERT_EQ(err, pid, "waitpid")) {
+		ASSERT_EQ(WIFEXITED(status), 1, "child_exited");
+		ASSERT_EQ(WEXITSTATUS(status), 0, "child_exit_code");
+	}
+
+cleanup:
+	uprobe_syscall_executed__destroy(skel);
+}
+
 static void __test_uprobe_syscall(void)
 {
 	if (test__start_subtest("uretprobe_regs_equal"))
@@ -956,6 +1040,10 @@ static void __test_uprobe_syscall(void)
 		test_uprobe_race();
 	if (test__start_subtest("uprobe_red_zone"))
 		test_uprobe_red_zone();
+	if (test__start_subtest("uprobe_optimized_fork"))
+		test_uprobe_fork_optimized(false);
+	if (test__start_subtest("uprobe_optimized_clone_vm"))
+		test_uprobe_fork_optimized(true);
 	if (test__start_subtest("uprobe_error"))
 		test_uprobe_error();
 	if (test__start_subtest("uprobe_regs_equal"))
Signature · 1 line
2.53.0