Skip to contentSkip to conversation

uprobes/x86: Fix red zone issue for optimized uprobes

PATCH Superseded Jiri Olsa 8 participants 67 messages May 26 – Jul 2

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

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Jiri Olsa

author reviewer
#0 lore raw

v4 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: https://lore.kernel.org/bpf/[email protected]/
v3: https://lore.kernel.org/bpf/[email protected]/

v4 changes:
- do not use 2nd int3 (ont +5 offset) because the call instruction is allways the same for the given nop10 address [Andrii/Peter]
- unmap unused trampoline vma after unsuccesfull optimization [sashiko]
- small change to patch#2 moved user_64bit_mode earlier in the path and pass/use mm_struct pointer directly from arch_uprobe_optimize instead of gettting current->mm
Andrii, keeping your ack, please shout otherwise

v3 changes:
- use nop10 update suggested by Peter in [2]
- remove struct uprobe_trampoline object, use vma objects directly instead
- selftests fixes [sashiko]
- ack from Andrii

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]/
[2] https://lore.kernel.org/bpf/[email protected]/#t
---
Andrii Nakryiko (1):
selftests/bpf: Add tests for uprobe nop10 red zone clobbering

Jiri Olsa (12):
uprobes/x86: Use proper mm_struct in __in_uprobe_trampoline uprobes/x86: Remove struct uprobe_trampoline object uprobes/x86: Allow to copy uprobe trampolines on fork uprobes/x86: Unmap trampoline vma object in case it's unused 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                               | 379 +++++++++++++++++++++++++++++++++++++++++++-----------------------------
 include/linux/uprobes.h                                 |   5 -
 kernel/events/uprobes.c                                 |  10 --
 kernel/fork.c                                           |   1 -
 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 ++-
 14 files changed, 653 insertions(+), 245 deletions(-)
author reviewer
#1 lore raw

PATCH v4 1/13 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.54.0
maintainer Reviewed-by on v4 1/13
#2 lore raw

Re: [PATCHv4 01/13] uprobes/x86: Use proper mm_struct in __in_uprobe_trampoline

Quoted 11 lines · Jiri Olsa

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")
author reviewer
#3 lore raw

PATCH v4 2/13 uprobes/x86: Remove struct uprobe_trampoline object

In reply to Jiri Olsa

Removing struct uprobe_trampoline object and it's tracking code, because it's not needed. We can do same thing directly on top of struct vm_area_struct objects.

This makes the code simpler and allows easy propagation of the trampoline vma object into child process in following change.

Note the original code called destroy_uprobe_trampoline if the optimiation failed, but it only freed the struct uprobe_trampoline object, not the vma. The new vma leak is fixed in following change.

---
 arch/x86/kernel/uprobes.c | 105 ++++++++------------------------------
 include/linux/uprobes.h   |   5 --
 kernel/events/uprobes.c   |  10 ----
 kernel/fork.c             |   1 -
 4 files changed, 21 insertions(+), 100 deletions(-)
arch/x86/kernel/uprobes.c +21 -84
index 2be6707e3320..733655bc610e 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,83 +677,28 @@ 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 *get_uprobe_trampoline(struct mm_struct *mm, unsigned long vaddr)
 {
-	struct pt_regs *regs = task_pt_regs(current);
-	struct mm_struct *mm = current->mm;
-	struct uprobe_trampoline *tramp;
+	VMA_ITERATOR(vmi, mm, 0);
 	struct vm_area_struct *vma;
 
-	if (!user_64bit_mode(regs))
-		return NULL;
+	if (vaddr > TASK_SIZE || vaddr < PAGE_SIZE)
+		return ERR_PTR(-EINVAL);
+
+	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;
+	}
 
 	vaddr = find_nearest_trampoline(vaddr);
 	if (IS_ERR_VALUE(vaddr))
-		return NULL;
+		return ERR_PTR(vaddr);
 
-	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)
-{
-	struct uprobes_state *state = &current->mm->uprobes_state;
-	struct uprobe_trampoline *tramp = NULL;
-
-	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;
-		}
-	}
-
-	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);
 }
 
 static bool __in_uprobe_trampoline(struct mm_struct *mm, unsigned long ip)
@@ -1111,21 +1051,18 @@ 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 pt_regs *regs = task_pt_regs(current);
+	struct vm_area_struct *vma, *tramp;
 
+	if (!user_64bit_mode(regs))
+		return -EINVAL;
 	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(mm, vaddr);
+	if (IS_ERR(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 -5
index f548fea2adec..18be159bbc34 100644
@@ -186,9 +186,6 @@ struct xol_area;
 
 struct uprobes_state {
 	struct xol_area		*xol_area;
-#ifdef CONFIG_X86_64
-	struct hlist_head	head_tramps;
-#endif
 };
 
 typedef int (*uprobe_write_verify_t)(struct page *page, unsigned long vaddr,
@@ -238,8 +235,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
 }
 
Signature · 1 line
2.54.0
#4 lore raw

Re: [PATCHv4 02/13] uprobes/x86: Remove struct uprobe_trampoline object

When swbp_optimize() fails in __arch_uprobe_optimize() and the preceding get_uprobe_trampoline() call created a new trampoline VMA via _install_special_mapping(), does the new VMA leak in the…
Quoted 4 lines
arch/x86/kernel/uprobes.c +0 -0
index 2be6707e3320..733655bc610e 100644

[ ... ]

Quoted 18 lines
arch/x86/kernel/uprobes.c +4 -6
@@ -1111,21 +1051,18 @@ 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 pt_regs *regs = task_pt_regs(current);
+	struct vm_area_struct *vma, *tramp;
 
+	if (!user_64bit_mode(regs))
+		return -EINVAL;
 	vma = find_vma(mm, vaddr);
 	if (!vma)
 		return -EINVAL;
-	tramp = get_uprobe_trampoline(vaddr, &new);
-	if (!tramp)

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

Quoted 10 lines
arch/x86/kernel/uprobes.c +4 -5
-		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(mm, vaddr);
+	if (IS_ERR(tramp))
+		return PTR_ERR(tramp);
+	return WARN_ON_ONCE(swbp_optimize(auprobe, vma, vaddr, tramp->vm_start));
 }

When swbp_optimize() fails in __arch_uprobe_optimize() and the preceding get_uprobe_trampoline() call created a new trampoline VMA via _install_special_mapping(), does the new VMA leak in the process address space?

The pre-patch code tracked whether the trampoline was newly created and called destroy_uprobe_trampoline() on the failure path:

  err = swbp_optimize(auprobe, vma, vaddr, tramp->vaddr);
  if (WARN_ON_ONCE(err) && new)
      destroy_uprobe_trampoline(tramp);
  return err;

The new code drops that cleanup entirely:

  return WARN_ON_ONCE(swbp_optimize(auprobe, vma, vaddr, tramp->vm_start));

Although the old destroy_uprobe_trampoline only freed the struct (not the underlying VMA), the new code appears to introduce a VMA leak: the freshly mapped PAGE_SIZE special mapping in the user's address space stays mapped even though optimization failed. arch_uprobe_optimize() then sets ARCH_UPROBE_FLAG_OPTIMIZE_FAIL so subsequent calls won't retry, leaving the orphan trampoline mapping in the address space until exit_mmap() reaps it at process teardown.

The commit message mentions: "Note the original code called destroy_uprobe_trampoline if the optimiation failed, but it only freed the struct uprobe_trampoline object, not the vma. The new vma leak is fixed in following change."

Is the VMA leak addressed in the subsequent commit in this series?

A secondary behaviour change is that 'return WARN_ON_ONCE(swbp_optimize(...))' now returns the boolean truth value of the error (0 or 1) instead of the original errno. While the current caller (arch_uprobe_optimize) only treats the value as boolean, could this surprise a future caller that propagates the return code?

Commit message:

Quoted 9 lines
arch/x86/kernel/uprobes.c +0 -0
    uprobes/x86: Remove struct uprobe_trampoline object
 

Removing struct uprobe_trampoline object and it's tracking code, because it's not needed. We can do same thing directly on top of struct vm_area_struct objects.

This makes the code simpler and allows easy propagation of the trampoline vma object into child process in following change.

Quoted 6 lines

Note the original code called destroy_uprobe_trampoline if the optimiation failed, but it only freed the struct uprobe_trampoline object, not the vma. The new vma leak is fixed in following change.

CI bot suggested adding a Fixes tag in v3: 'Fixes: ba2bfc97b462 ("uprobes/x86: Add support to optimize uprobes")' because the commit message notes a VMA resource leak in the original code.

Reference: https://lore.kernel.org/bpf/8861cc7af8e0dfff2674ca4ae6381ea2d82c242dec115774e8e11048bde358c4@mail.kernel.org/

The author responded that 'the logic stays' and a separate fix is needed, but did not explain why the Fixes tag should not be included. If this patch is documenting a pre-existing bug (even if the full fix comes later), should it include a Fixes tag for backporting purposes?

---
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/26475828601

#5 lore raw

Re: [PATCHv4 02/13] uprobes/x86: Remove struct uprobe_trampoline object

Quoted 61 lines · On Tue, May 26, 2026 at 09:46:44PM +0000, [email protected] wrote:
Quoted 4 lines
arch/x86/kernel/uprobes.c +0 -0
index 2be6707e3320..733655bc610e 100644

[ ... ]

Quoted 18 lines
arch/x86/kernel/uprobes.c +4 -6
@@ -1111,21 +1051,18 @@ 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 pt_regs *regs = task_pt_regs(current);
+	struct vm_area_struct *vma, *tramp;
 
+	if (!user_64bit_mode(regs))
+		return -EINVAL;
 	vma = find_vma(mm, vaddr);
 	if (!vma)
 		return -EINVAL;
-	tramp = get_uprobe_trampoline(vaddr, &new);
-	if (!tramp)

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

Quoted 10 lines
arch/x86/kernel/uprobes.c +4 -5
-		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(mm, vaddr);
+	if (IS_ERR(tramp))
+		return PTR_ERR(tramp);
+	return WARN_ON_ONCE(swbp_optimize(auprobe, vma, vaddr, tramp->vm_start));
 }

When swbp_optimize() fails in __arch_uprobe_optimize() and the preceding get_uprobe_trampoline() call created a new trampoline VMA via _install_special_mapping(), does the new VMA leak in the process address space?

The pre-patch code tracked whether the trampoline was newly created and called destroy_uprobe_trampoline() on the failure path:

  err = swbp_optimize(auprobe, vma, vaddr, tramp->vaddr);
  if (WARN_ON_ONCE(err) && new)
      destroy_uprobe_trampoline(tramp);
  return err;

The new code drops that cleanup entirely:

  return WARN_ON_ONCE(swbp_optimize(auprobe, vma, vaddr, tramp->vm_start));

Although the old destroy_uprobe_trampoline only freed the struct (not the underlying VMA), the new code appears to introduce a VMA leak: the freshly mapped PAGE_SIZE special mapping in the user's address space stays mapped even though optimization failed. arch_uprobe_optimize() then sets ARCH_UPROBE_FLAG_OPTIMIZE_FAIL so subsequent calls won't retry, leaving the orphan trampoline mapping in the address space until exit_mmap() reaps it at process teardown.

Quoted 6 lines

The commit message mentions: "Note the original code called destroy_uprobe_trampoline if the optimiation failed, but it only freed the struct uprobe_trampoline object, not the vma. The new vma leak is fixed in following change."

Is the VMA leak addressed in the subsequent commit in this series?

yes, in:

[1] uprobes/x86: Unmap trampoline vma object in case it's unused

Quoted 6 lines

A secondary behaviour change is that 'return WARN_ON_ONCE(swbp_optimize(...))' now returns the boolean truth value of the error (0 or 1) instead of the original errno. While the current caller (arch_uprobe_optimize) only treats the value as boolean, could this surprise a future caller that propagates the return code?

ah ok, this is actualy 'fixed' in [1] above, but yea we should fix that directly in this change, will do

Quoted 23 lines

Commit message:

Quoted 9 lines
arch/x86/kernel/uprobes.c +0 -0
    uprobes/x86: Remove struct uprobe_trampoline object
 

Removing struct uprobe_trampoline object and it's tracking code, because it's not needed. We can do same thing directly on top of struct vm_area_struct objects.

This makes the code simpler and allows easy propagation of the trampoline vma object into child process in following change.

Quoted 6 lines

Note the original code called destroy_uprobe_trampoline if the optimiation failed, but it only freed the struct uprobe_trampoline object, not the vma. The new vma leak is fixed in following change.

CI bot suggested adding a Fixes tag in v3: 'Fixes: ba2bfc97b462 ("uprobes/x86: Add support to optimize uprobes")' because the commit message notes a VMA resource leak in the original code.

Quoted 6 lines

Reference: https://lore.kernel.org/bpf/8861cc7af8e0dfff2674ca4ae6381ea2d82c242dec115774e8e11048bde358c4@mail.kernel.org/

The author responded that 'the logic stays' and a separate fix is needed, but did not explain why the Fixes tag should not be included. If this patch is documenting a pre-existing bug (even if the full fix comes later), should it include a Fixes tag for backporting purposes?

there's not functional change in this patch, the vma leak fix is in [1]

jirka

#6 lore raw

Re: [PATCHv4 02/13] uprobes/x86: Remove struct uprobe_trampoline object

On Wed, May 27, 2026 at 11:58:52AM +0200, Jiri Olsa wrote:

SNIP

Quoted 20 lines
Quoted 8 lines

Although the old destroy_uprobe_trampoline only freed the struct (not the underlying VMA), the new code appears to introduce a VMA leak: the freshly mapped PAGE_SIZE special mapping in the user's address space stays mapped even though optimization failed. arch_uprobe_optimize() then sets ARCH_UPROBE_FLAG_OPTIMIZE_FAIL so subsequent calls won't retry, leaving the orphan trampoline mapping in the address space until exit_mmap() reaps it at process teardown.

Quoted 6 lines

The commit message mentions: "Note the original code called destroy_uprobe_trampoline if the optimiation failed, but it only freed the struct uprobe_trampoline object, not the vma. The new vma leak is fixed in following change."

Is the VMA leak addressed in the subsequent commit in this series?

yes, in:

[1] uprobes/x86: Unmap trampoline vma object in case it's unused

Quoted 1 line
Quoted 8 lines
Quoted 5 lines

A secondary behaviour change is that 'return WARN_ON_ONCE(swbp_optimize(...))' now returns the boolean truth value of the error (0 or 1) instead of the original errno. While the current caller (arch_uprobe_optimize) only treats the value as boolean, could this surprise a future caller that propagates the return code?

ah ok, this is actualy 'fixed' in [1] above, but yea we should fix that directly in this change, will do

nah, it's ok, the caller does not care about the exact error value, just 0 or 1 is fine

jirka

maintainer Reviewed-by on v4 2/13
#7 lore raw

Re: [PATCHv4 02/13] uprobes/x86: Remove struct uprobe_trampoline object

In reply to Jiri Olsa
Quoted 8 lines · Jiri Olsa

Removing struct uprobe_trampoline object and it's tracking code, because it's not needed. We can do same thing directly on top of struct vm_area_struct objects.

This makes the code simpler and allows easy propagation of the trampoline vma object into child process in following change.

Quoted 6 lines

Note the original code called destroy_uprobe_trampoline if the optimiation failed, but it only freed the struct uprobe_trampoline object, not the vma. The new vma leak is fixed in following change.

---------------------------------------------------------------------
Although I can't convince myself I fully understand this code with or without this patch ;)

A couple of questions below...

arch/x86/kernel/uprobes.c static unsigned long find_nearest_trampoline(unsigned long vaddr) · line 686

Quoted 13 lines
arch/x86/kernel/uprobes.c +4 -6
-static struct uprobe_trampoline *create_uprobe_trampoline(unsigned long vaddr)
+static struct vm_area_struct *get_uprobe_trampoline(struct mm_struct *mm, unsigned long vaddr)
 {
-	struct pt_regs *regs = task_pt_regs(current);
-	struct mm_struct *mm = current->mm;
-	struct uprobe_trampoline *tramp;
+	VMA_ITERATOR(vmi, mm, 0);
 	struct vm_area_struct *vma;
 
-	if (!user_64bit_mode(regs))
-		return NULL;
+	if (vaddr > TASK_SIZE || vaddr < PAGE_SIZE)
+		return ERR_PTR(-EINVAL);

Do we really need this check? It looks a bit confusing to me... vaddr is bp_vaddr from handle_swbp(), it should be valid?

arch/x86/kernel/uprobes.c static unsigned long find_nearest_trampoline(unsigned long vaddr) · line 693

Quoted 7 lines
arch/x86/kernel/uprobes.c +7 -0
+
+	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;
+	}

Perhaps we can later optimize this code a bit? I mean something like

	start_reachable = ...;
	end_reachable = ...;

	VMA_ITERATOR(vmi, mm, start_reachable);

	for_each_vma(vmi, vma) {
		if (!vma_is_special_mapping(...))
			continue;
		if (vma->vm_start > end_reachable)
			break;
		return vma;
	}
Quoted 5 lines

static int __arch_uprobe_optimize(struct arch_uprobe *auprobe, struct mm_struct *mm,

arch/x86/kernel/uprobes.c +0 -2
 				  unsigned long vaddr)
 {
-	struct uprobe_trampoline *tramp;
-	struct vm_area_struct *vma;

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

Quoted 10 lines
arch/x86/kernel/uprobes.c +4 -2
-	bool new = false;
-	int err = 0;
+	struct pt_regs *regs = task_pt_regs(current);
+	struct vm_area_struct *vma, *tramp;
 
+	if (!user_64bit_mode(regs))
+		return -EINVAL;
 	vma = find_vma(mm, vaddr);
 	if (!vma)
 		return -EINVAL;

I guess find_vma() can't fail, the caller arch_uprobe_optimize() has called copy_from_vaddr() under mmap_write_lock()... Nevermind.

Oleg.

maintainer
#8 lore raw

Re: [PATCHv4 02/13] uprobes/x86: Remove struct uprobe_trampoline object

Quoted 15 lines · Oleg Nesterov

Perhaps we can later optimize this code a bit? I mean something like

	start_reachable = ...;
	end_reachable = ...;

	VMA_ITERATOR(vmi, mm, start_reachable);

	for_each_vma(vmi, vma) {
		if (!vma_is_special_mapping(...))
			continue;
		if (vma->vm_start > end_reachable)
			break;
		return vma;
	}

I have looked into include/linux/mm.h, we have for_each_vma_range(). So I guess

	for_each_vma_range(vmi, vma, end_reachable) {
		if (vma_is_special_mapping(...))
			return vma;
	}

should work.

Oleg.

#9 lore raw

Re: [PATCHv4 02/13] uprobes/x86: Remove struct uprobe_trampoline object

In reply to Oleg Nesterov
Quoted 33 lines · Oleg Nesterov
Quoted 8 lines · Jiri Olsa

Removing struct uprobe_trampoline object and it's tracking code, because it's not needed. We can do same thing directly on top of struct vm_area_struct objects.

This makes the code simpler and allows easy propagation of the trampoline vma object into child process in following change.

Quoted 6 lines

Note the original code called destroy_uprobe_trampoline if the optimiation failed, but it only freed the struct uprobe_trampoline object, not the vma. The new vma leak is fixed in following change.

---------------------------------------------------------------------
Although I can't convince myself I fully understand this code with or without this patch ;)

A couple of questions below...

Quoted 9 lines
arch/x86/kernel/uprobes.c +2 -4
-static struct uprobe_trampoline *create_uprobe_trampoline(unsigned long vaddr)
+static struct vm_area_struct *get_uprobe_trampoline(struct mm_struct *mm, unsigned long vaddr)
 {
-	struct pt_regs *regs = task_pt_regs(current);
-	struct mm_struct *mm = current->mm;
-	struct uprobe_trampoline *tramp;
+	VMA_ITERATOR(vmi, mm, 0);
 	struct vm_area_struct *vma;
Quoted 7 lines

arch/x86/kernel/uprobes.c static unsigned long find_nearest_trampoline(unsigned long vaddr) · line 686

Quoted 4 lines
arch/x86/kernel/uprobes.c +2 -2
-	if (!user_64bit_mode(regs))
-		return NULL;
+	if (vaddr > TASK_SIZE || vaddr < PAGE_SIZE)
+		return ERR_PTR(-EINVAL);

Do we really need this check? It looks a bit confusing to me... vaddr is bp_vaddr from handle_swbp(), it should be valid?

true, will remove

Quoted 11 lines

arch/x86/kernel/uprobes.c static unsigned long find_nearest_trampoline(unsigned long vaddr) · line 693

Quoted 8 lines
arch/x86/kernel/uprobes.c +7 -0
+
+	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;
+	}

Perhaps we can later optimize this code a bit? I mean something like

Quoted 12 lines
	start_reachable = ...;
	end_reachable = ...;

	VMA_ITERATOR(vmi, mm, start_reachable);

	for_each_vma(vmi, vma) {
		if (!vma_is_special_mapping(...))
			continue;
		if (vma->vm_start > end_reachable)
			break;
		return vma;
	}

looks good, will try to use that

Quoted 11 lines
Quoted 11 lines

static int __arch_uprobe_optimize(struct arch_uprobe *auprobe, struct mm_struct *mm,

arch/x86/kernel/uprobes.c +2 -4
 				  unsigned long vaddr)
 {
-	struct uprobe_trampoline *tramp;
-	struct vm_area_struct *vma;
-	bool new = false;
-	int err = 0;
+	struct pt_regs *regs = task_pt_regs(current);
+	struct vm_area_struct *vma, *tramp;
Quoted 8 lines

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

Quoted 5 lines
arch/x86/kernel/uprobes.c +2 -0
+	if (!user_64bit_mode(regs))
+		return -EINVAL;
 	vma = find_vma(mm, vaddr);
 	if (!vma)
 		return -EINVAL;

I guess find_vma() can't fail, the caller arch_uprobe_optimize() has called copy_from_vaddr() under mmap_write_lock()... Nevermind.

hum, how's that.. I'll check, but where's the magic? :)

thanks,
jirka

maintainer
#10 lore raw

Re: [PATCHv4 02/13] uprobes/x86: Remove struct uprobe_trampoline object

Quoted 18 lines · Jiri Olsa
Quoted 15 lines · Oleg Nesterov

Perhaps we can later optimize this code a bit? I mean something like

	start_reachable = ...;
	end_reachable = ...;

	VMA_ITERATOR(vmi, mm, start_reachable);

	for_each_vma(vmi, vma) {
		if (!vma_is_special_mapping(...))
			continue;
		if (vma->vm_start > end_reachable)
			break;
		return vma;
	}
Quoted 1 line

looks good, will try to use that

See my next email, we can use for_each_vma_range().

But let me repeat, we can add this mimor optimization later, I don't want to delay this series.

Quoted 19 lines
Quoted 10 lines
Quoted 10 lines

static int __arch_uprobe_optimize(struct arch_uprobe *auprobe, struct mm_struct *mm,

arch/x86/kernel/uprobes.c +2 -4
 				  unsigned long vaddr)
 {
-	struct uprobe_trampoline *tramp;
-	struct vm_area_struct *vma;
-	bool new = false;
-	int err = 0;
+	struct pt_regs *regs = task_pt_regs(current);
+	struct vm_area_struct *vma, *tramp;
Quoted 8 lines

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

Quoted 5 lines
arch/x86/kernel/uprobes.c +2 -0
+	if (!user_64bit_mode(regs))
+		return -EINVAL;
 	vma = find_vma(mm, vaddr);
 	if (!vma)
 		return -EINVAL;

I guess find_vma() can't fail, the caller arch_uprobe_optimize() has called copy_from_vaddr() under mmap_write_lock()... Nevermind.

Quoted 1 line

hum, how's that.. I'll check, but where's the magic? :)

arch_uprobe_optimize() -> copy_from_vaddr() reads this mm at the same vaddr, this means that vma at this vaddr must exist. Unless I am totally confused ;) But even if I am right please ignore. I just tried to understand if find_vma() can fail or not here.

Oleg.

#11 lore raw

Re: [PATCHv4 02/13] uprobes/x86: Remove struct uprobe_trampoline object

Quoted 45 lines · Oleg Nesterov
Quoted 18 lines · Jiri Olsa
Quoted 15 lines · Oleg Nesterov

Perhaps we can later optimize this code a bit? I mean something like

	start_reachable = ...;
	end_reachable = ...;

	VMA_ITERATOR(vmi, mm, start_reachable);

	for_each_vma(vmi, vma) {
		if (!vma_is_special_mapping(...))
			continue;
		if (vma->vm_start > end_reachable)
			break;
		return vma;
	}
Quoted 1 line

looks good, will try to use that

See my next email, we can use for_each_vma_range().

But let me repeat, we can add this mimor optimization later, I don't want to delay this series.

Quoted 19 lines
Quoted 18 lines
Quoted 5 lines

static int __arch_uprobe_optimize(struct arch_uprobe *auprobe, struct mm_struct *mm,

arch/x86/kernel/uprobes.c +0 -2
 				  unsigned long vaddr)
 {
-	struct uprobe_trampoline *tramp;
-	struct vm_area_struct *vma;

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

Quoted 10 lines
arch/x86/kernel/uprobes.c +4 -2
-	bool new = false;
-	int err = 0;
+	struct pt_regs *regs = task_pt_regs(current);
+	struct vm_area_struct *vma, *tramp;
 
+	if (!user_64bit_mode(regs))
+		return -EINVAL;
 	vma = find_vma(mm, vaddr);
 	if (!vma)
 		return -EINVAL;

I guess find_vma() can't fail, the caller arch_uprobe_optimize() has called copy_from_vaddr() under mmap_write_lock()... Nevermind.

Quoted 6 lines
Quoted 1 line

hum, how's that.. I'll check, but where's the magic? :)

arch_uprobe_optimize() -> copy_from_vaddr() reads this mm at the same vaddr, this means that vma at this vaddr must exist. Unless I am totally confused ;) But even if I am right please ignore. I just tried to understand if find_vma() can fail or not here.

ok, will leave these 2 changes for later

jirka

author reviewer
#12 lore raw

PATCH v4 3/13 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 that trampoline and crash.

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 733655bc610e..c8af41ed681a 100644
@@ -697,7 +697,7 @@ static struct vm_area_struct *get_uprobe_trampoline(struct mm_struct *mm, unsign
 		return ERR_PTR(vaddr);
 
 	return _install_special_mapping(mm, 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);
 }
 
Signature · 1 line
2.54.0
#13 lore raw

Re: [PATCHv4 03/13] uprobes/x86: Allow to copy uprobe trampolines on fork

When a process with optimized uprobes is forked, the child inherits the optimized code and trampoline. This means sys_uprobe invocations in the child will go into the uprobe trampoline, and the…

arch/x86/kernel/uprobes.c static struct vm_area_struct *get_uprobe_trampoline(struct mm_struct *mm, unsign · line 702

Quoted 12 lines
arch/x86/kernel/uprobes.c +1 -1
index 733655bc610e..c8af41ed681a 100644
@@ -697,7 +697,7 @@ static struct vm_area_struct *get_uprobe_trampoline(struct mm_struct *mm, unsign
 		return ERR_PTR(vaddr);
 
 	return _install_special_mapping(mm, 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);
 }

When a process with optimized uprobes is forked, the child inherits the optimized code and trampoline. This means sys_uprobe invocations in the child will go into the uprobe trampoline, and the syscall will keep returning -EPROTO since no handler is registered.

Is there a way to restore the original memory pages for clones?

This behavior seems suboptimal from a performance perspective. For standard int3-based uprobes, breakpoints are removed via UPROBE_HANDLER_REMOVE. Could something similar be implemented for optimized uprobes, perhaps with proper filtering in uprobe_multi?

Reference: https://lore.kernel.org/bpf/CAEf4BzYo-8PAXFJt9MHoUn9ux1O2YVxJADC0tGSsacVu_R8Stw@mail.gmail.com/

---
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/26475828601

#14 lore raw

Re: [PATCHv4 03/13] uprobes/x86: Allow to copy uprobe trampolines on fork

Quoted 18 lines · On Tue, May 26, 2026 at 09:46:40PM +0000, [email protected] wrote:

arch/x86/kernel/uprobes.c static struct vm_area_struct *get_uprobe_trampoline(struct mm_struct *mm, unsign · line 702

Quoted 12 lines
arch/x86/kernel/uprobes.c +1 -1
index 733655bc610e..c8af41ed681a 100644
@@ -697,7 +697,7 @@ static struct vm_area_struct *get_uprobe_trampoline(struct mm_struct *mm, unsign
 		return ERR_PTR(vaddr);
 
 	return _install_special_mapping(mm, 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);
 }

When a process with optimized uprobes is forked, the child inherits the optimized code and trampoline. This means sys_uprobe invocations in the child will go into the uprobe trampoline, and the syscall will keep returning -EPROTO since no handler is registered.

Quoted 8 lines

Is there a way to restore the original memory pages for clones?

This behavior seems suboptimal from a performance perspective. For standard int3-based uprobes, breakpoints are removed via UPROBE_HANDLER_REMOVE. Could something similar be implemented for optimized uprobes, perhaps with proper filtering in uprobe_multi?

Reference: https://lore.kernel.org/bpf/CAEf4BzYo-8PAXFJt9MHoUn9ux1O2YVxJADC0tGSsacVu_R8Stw@mail.gmail.com/

and I replied in that thread that we can address that later:
https://lore.kernel.org/bpf/ahNzlAfUnymDDGOR@krava/

jirka

maintainer Reviewed-by on v4 3/13
#15 lore raw

Re: [PATCHv4 03/13] uprobes/x86: Allow to copy uprobe trampolines on fork

In reply to Jiri Olsa
Quoted 5 lines · 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 that trampoline and crash.

Quoted 8 lines

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(-)
author reviewer
#16 lore raw

PATCH v4 4/13 uprobes/x86: Unmap trampoline vma object in case it's unused

In reply to Jiri Olsa

In case the optimization fails, we leak new-ly created trampoline vma mapping (in case we just created it), let's unmap it.

Fixes ba2bfc97b462 ("uprobes/x86: Add support to optimize uprobes")
---
 arch/x86/kernel/uprobes.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
arch/x86/kernel/uprobes.c +12 -3
index c8af41ed681a..af5af7d67999 100644
@@ -677,11 +677,14 @@ static unsigned long find_nearest_trampoline(unsigned long vaddr)
 	return high_tramp;
 }
 
-static struct vm_area_struct *get_uprobe_trampoline(struct mm_struct *mm, unsigned long vaddr)
+static struct vm_area_struct *get_uprobe_trampoline(struct mm_struct *mm, unsigned long vaddr,
+						    bool *new_mapping)
 {
 	VMA_ITERATOR(vmi, mm, 0);
 	struct vm_area_struct *vma;
 
+	*new_mapping = false;
+
 	if (vaddr > TASK_SIZE || vaddr < PAGE_SIZE)
 		return ERR_PTR(-EINVAL);
 
@@ -696,6 +699,7 @@ static struct vm_area_struct *get_uprobe_trampoline(struct mm_struct *mm, unsign
 	if (IS_ERR_VALUE(vaddr))
 		return ERR_PTR(vaddr);
 
+	*new_mapping = true;
 	return _install_special_mapping(mm, vaddr, PAGE_SIZE,
 				VM_READ|VM_EXEC|VM_MAYEXEC|VM_MAYREAD|VM_IO,
 				&tramp_mapping);
@@ -1053,16 +1057,21 @@ static int __arch_uprobe_optimize(struct arch_uprobe *auprobe, struct mm_struct
 {
 	struct pt_regs *regs = task_pt_regs(current);
 	struct vm_area_struct *vma, *tramp;
+	bool new_mapping;
+	int ret;
 
 	if (!user_64bit_mode(regs))
 		return -EINVAL;
 	vma = find_vma(mm, vaddr);
 	if (!vma)
 		return -EINVAL;
-	tramp = get_uprobe_trampoline(mm, vaddr);
+	tramp = get_uprobe_trampoline(mm, vaddr, &new_mapping);
 	if (IS_ERR(tramp))
 		return PTR_ERR(tramp);
-	return WARN_ON_ONCE(swbp_optimize(auprobe, vma, vaddr, tramp->vm_start));
+	ret = swbp_optimize(auprobe, vma, vaddr, tramp->vm_start);
+	if (WARN_ON_ONCE(ret) && new_mapping)
+		WARN_ON_ONCE(do_munmap(mm, tramp->vm_start, PAGE_SIZE, NULL));
+	return ret;
 }
 
 void arch_uprobe_optimize(struct arch_uprobe *auprobe, unsigned long vaddr)
Signature · 1 line
2.54.0
#17 lore raw

Re: [PATCHv4 04/13] uprobes/x86: Unmap trampoline vma object in case it's unused

Should this Fixes: tag point to a9fe6f54968c instead? The vma leak bug was actually introduced by commit a9fe6f54968c ("uprobes/x86: Remove struct uprobe_trampoline object"), not ba2bfc97b462.…
Quoted 11 lines

commit 68883203f0583fb1a8d79536f5c8945542974abd

Author: Jiri Olsa <[email protected]>
Date:   Tue May 26 22:58:31 2026 +0200

uprobes/x86: Unmap trampoline vma object in case it's unused

In case the optimization fails, we leak new-ly created trampoline vma mapping (in case we just created it), let's unmap it.

Fixes ba2bfc97b462 ("uprobes/x86: Add support to optimize uprobes")

Should this Fixes: tag point to a9fe6f54968c instead? The vma leak bug was actually introduced by commit a9fe6f54968c ("uprobes/x86: Remove struct uprobe_trampoline object"), not ba2bfc97b462. Commit a9fe6f54968c removed the destroy_uprobe_trampoline() cleanup call, and its commit message explicitly states: "The new vma leak is fixed in following change."

Suggested: Fixes: a9fe6f54968c ("uprobes/x86: Remove struct uprobe_trampoline object")

Quoted 10 lines
arch/x86/kernel/uprobes.c +1 -1
index c8af41ed681a..af5af7d67999 100644
@@ -677,11 +677,14 @@ static unsigned long find_nearest_trampoline(unsigned long vaddr)
 	return high_tramp;
 }
 
-static struct vm_area_struct *get_uprobe_trampoline(struct mm_struct *mm, unsigned long vaddr)
+static struct vm_area_struct *get_uprobe_trampoline(struct mm_struct *mm, unsigned long vaddr,

arch/x86/kernel/uprobes.c static unsigned long find_nearest_trampoline(unsigned long vaddr) · line 690

Quoted 10 lines
arch/x86/kernel/uprobes.c +3 -0
+						    bool *new_mapping)
 {
 	VMA_ITERATOR(vmi, mm, 0);
 	struct vm_area_struct *vma;
 
+	*new_mapping = false;
+
 	if (vaddr > TASK_SIZE || vaddr < PAGE_SIZE)
 		return ERR_PTR(-EINVAL);
 

Does this fix address the VMA leak scenario across fork chains? In v2, sashiko-bot raised a concern about inherited-but-not-tracked VMAs:

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

When VM_DONTCOPY is removed, child processes inherit trampoline VMAs but their tracking list (mm->uprobes_state.head_tramps) is empty. When the child executes a uprobe, get_uprobe_trampoline() fails to find the inherited VMA and creates a duplicate trampoline.

Child fork:
dup_mmap()
copies trampoline VMA to child
mm_init_uprobes_state()
initializes head_tramps as empty (parent's state not copied)

Child executes uprobe:
arch_uprobe_optimize()
get_uprobe_trampoline()
head_tramps is empty, fails to find inherited VMA create_uprobe_trampoline()
find_nearest_trampoline() finds new unmapped area installs 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 linear accumulation of redundant VMAs, eventually exhausting vm.max_map_count?

While this patch addresses unmapping failed optimization attempts, it does not prevent the inherited-but-not-tracked VMA leak scenario in fork chains.

arch/x86/kernel/uprobes.c static struct vm_area_struct *get_uprobe_trampoline(struct mm_struct *mm, unsign · line 705

Quoted 8 lines
arch/x86/kernel/uprobes.c +1 -0
@@ -696,6 +699,7 @@ static struct vm_area_struct *get_uprobe_trampoline(struct mm_struct *mm, unsign
 	if (IS_ERR_VALUE(vaddr))
 		return ERR_PTR(vaddr);
 
+	*new_mapping = true;
 	return _install_special_mapping(mm, vaddr, PAGE_SIZE,
 				VM_READ|VM_EXEC|VM_MAYEXEC|VM_MAYREAD|VM_IO,
 				&tramp_mapping);

[ ... ]

---
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/26475828601

#18 lore raw

Re: [PATCHv4 04/13] uprobes/x86: Unmap trampoline vma object in case it's unused

Quoted 12 lines · On Tue, May 26, 2026 at 09:46:49PM +0000, [email protected] wrote:
Quoted 11 lines

commit 68883203f0583fb1a8d79536f5c8945542974abd

Author: Jiri Olsa <[email protected]>
Date:   Tue May 26 22:58:31 2026 +0200

uprobes/x86: Unmap trampoline vma object in case it's unused

In case the optimization fails, we leak new-ly created trampoline vma mapping (in case we just created it), let's unmap it.

Fixes ba2bfc97b462 ("uprobes/x86: Add support to optimize uprobes")
Quoted 6 lines

Should this Fixes: tag point to a9fe6f54968c instead? The vma leak bug was actually introduced by commit a9fe6f54968c ("uprobes/x86: Remove struct uprobe_trampoline object"), not ba2bfc97b462. Commit a9fe6f54968c removed the destroy_uprobe_trampoline() cleanup call, and its commit message explicitly states: "The new vma leak is fixed in following change."

there's no a9fe6f54968c hash yet

Quoted 49 lines

Suggested: Fixes: a9fe6f54968c ("uprobes/x86: Remove struct uprobe_trampoline object")

Quoted 10 lines
arch/x86/kernel/uprobes.c +1 -1
index c8af41ed681a..af5af7d67999 100644
@@ -677,11 +677,14 @@ static unsigned long find_nearest_trampoline(unsigned long vaddr)
 	return high_tramp;
 }
 
-static struct vm_area_struct *get_uprobe_trampoline(struct mm_struct *mm, unsigned long vaddr)
+static struct vm_area_struct *get_uprobe_trampoline(struct mm_struct *mm, unsigned long vaddr,

arch/x86/kernel/uprobes.c static unsigned long find_nearest_trampoline(unsigned long vaddr) · line 690

Quoted 10 lines
arch/x86/kernel/uprobes.c +3 -0
+						    bool *new_mapping)
 {
 	VMA_ITERATOR(vmi, mm, 0);
 	struct vm_area_struct *vma;
 
+	*new_mapping = false;
+
 	if (vaddr > TASK_SIZE || vaddr < PAGE_SIZE)
 		return ERR_PTR(-EINVAL);
 

Does this fix address the VMA leak scenario across fork chains? In v2, sashiko-bot raised a concern about inherited-but-not-tracked VMAs:

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

When VM_DONTCOPY is removed, child processes inherit trampoline VMAs but their tracking list (mm->uprobes_state.head_tramps) is empty. When the child executes a uprobe, get_uprobe_trampoline() fails to find the inherited VMA and creates a duplicate trampoline.

Child fork:
dup_mmap()
copies trampoline VMA to child
mm_init_uprobes_state()
initializes head_tramps as empty (parent's state not copied)

Child executes uprobe:
arch_uprobe_optimize()
get_uprobe_trampoline()
head_tramps is empty, fails to find inherited VMA create_uprobe_trampoline()
find_nearest_trampoline() finds new unmapped area installs second duplicate trampoline VMA

Quoted 7 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 linear accumulation of redundant VMAs, eventually exhausting vm.max_map_count?

While this patch addresses unmapping failed optimization attempts, it does not prevent the inherited-but-not-tracked VMA leak scenario in fork chains.

there's not head_tramps list anymore, was removed by earlier:
uprobes/x86: Remove struct uprobe_trampoline object

jirka

maintainer Reviewed-by on v4 4/13
#19 lore raw

Re: [PATCHv4 04/13] uprobes/x86: Unmap trampoline vma object in case it's unused

In reply to Jiri Olsa
Quoted 6 lines · Jiri Olsa

In case the optimization fails, we leak new-ly created trampoline vma mapping (in case we just created it), let's unmap it.

Fixes ba2bfc97b462 ("uprobes/x86: Add support to optimize uprobes")

but I am a bit confused... It seems that this change doesn't depend on the previous 03/13 which removed VM_DONTCOPY ? So I think this patch could come as 3/13 after "Remove struct uprobe_trampoline object".

And the subject looks misleading to me. A tramp vma may become "unused" if (say) we remove some optimized breakpoint, afaics it will be never unmapped. Perhaps it should say something like "don't leak on failure".

But this all is really minor, please ignore.

Oleg.

#20 lore raw

Re: [PATCHv4 04/13] uprobes/x86: Unmap trampoline vma object in case it's unused

Quoted 8 lines · Oleg Nesterov
Quoted 6 lines · Jiri Olsa

In case the optimization fails, we leak new-ly created trampoline vma mapping (in case we just created it), let's unmap it.

Fixes ba2bfc97b462 ("uprobes/x86: Add support to optimize uprobes")
Quoted 6 lines

but I am a bit confused... It seems that this change doesn't depend on the previous 03/13 which removed VM_DONTCOPY ? So I think this patch could come as 3/13 after "Remove struct uprobe_trampoline object".

ok, will move it

Quoted 6 lines

And the subject looks misleading to me. A tramp vma may become "unused" if (say) we remove some optimized breakpoint, afaics it will be never unmapped. Perhaps it should say something like "don't leak on failure".

But this all is really minor, please ignore.

np, will change

thanks,
jirka

author reviewer
#21 lore raw

PATCH v4 5/13 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.

We use nop10 and following transformation to optimized instructions above and back as suggested by Peterz [2].

Optimize path (int3_update_optimize):

1) Initial state after set_swbp() installed the uprobe:
cc 2e 0f 1f 84 00 00 00 00 00

From offset 0 this is INT3 followed by the tail of the original 10-byte NOP.

After a previous unoptimization bytes 5..9 may still contain the old call instruction, which remains valid for threads already there.

2) Rewrite the LEA tail and call displacement:
cc [8d 64 24 80 e8 d0 d1 d2 d3]

From offset 0 this traps on the uprobe INT3. Bytes 1..9 are not executable entry points while byte 0 is trapped.

3) Publish the first LEA byte:

      [48] 8d 64 24 80 e8 d0 d1 d2 d3

     From offset 0 this is:
        lea -0x80(%rsp), %rsp
        call <uprobe-trampoline>

Unoptimize path (int3_update_unoptimize):

1) Initial optimized state:
48 8d 64 24 80 e8 d0 d1 d2 d3
Same as 3) above.

2) Trap new entries before restoring the NOP bytes:
[cc] 8d 64 24 80 e8 d0 d1 d2 d3

From offset 0 this traps. A thread that had already executed the LEA can still reach the intact CALL at offset 5.

3) Restore bytes 1..4 of the original NOP while keeping byte 0 trapped and byte 5 as CALL.
cc [2e 0f 1f 84] e8 d0 d1 d2 d3

From offset 0 this still traps. Offset 5 is still the CALL for any thread that was already past the first LEA byte.

4) Publish the first byte of the original NOP:
[66] 2e 0f 1f 84 e8 d0 d1 d2 d3

From offset 0 this is the restored 10-byte NOP; the CALL opcode and displacement are now only NOP operands. Offset 5 still decodes as CALL for a thread that was already there.

Tthere is only a single target uprobe-trampoline for the given nop10 instruction address, so the CALL instruction will not be changed across unoptimization/optimization cycles.
Therefore, any task that is preempted at the CALL instruction is guaranteed to observe that CALL and not anything else.

Note as explained in [2] we need to use following nop10:
PF1 PF2 ESC NOPL MOD SIB DISP32
NOP10: 0x66, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 -- cs nopw 0x00000000(%rax,%rax,1)

which means we need to allow 0x2e prefix which maps to INAT_PFX_CS attribute in is_prefix_bad function.

Also changing the uprobe syscall error when called out of uprobe trampoline to -EPROTO, so we are able to detect the fixed kernel.

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]/
[2] https://lore.kernel.org/bpf/[email protected]/#t

Fixes ba2bfc97b462 ("uprobes/x86: Add support to optimize uprobes")
Assisted-by Codex:GPT-5.5
---
 arch/x86/kernel/uprobes.c | 255 ++++++++++++++++++++++++++++----------
 1 file changed, 190 insertions(+), 65 deletions(-)
arch/x86/kernel/uprobes.c +190 -65
index af5af7d67999..de544516ea70 100644
@@ -266,7 +266,6 @@ static bool is_prefix_bad(struct insn *insn)
 		attr = inat_get_opcode_attribute(p);
 		switch (attr) {
 		case INAT_MAKE_PREFIX(INAT_PFX_ES):
-		case INAT_MAKE_PREFIX(INAT_PFX_CS):
 		case INAT_MAKE_PREFIX(INAT_PFX_DS):
 		case INAT_MAKE_PREFIX(INAT_PFX_SS):
 		case INAT_MAKE_PREFIX(INAT_PFX_LOCK):
@@ -631,9 +630,29 @@ static struct vm_special_mapping tramp_mapping = {
 	.pages  = tramp_mapping_pages,
 };
 
+
+#define LEA_INSN_SIZE		5
+#define OPT_INSN_SIZE		(LEA_INSN_SIZE + CALL_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)
+{
+	return !memcmp(insn, lea_rsp, LEA_INSN_SIZE) &&
+	       insn[LEA_INSN_SIZE] == CALL_INSN_OPCODE;
+}
+
+static bool is_swbp_opt_insns(uprobe_opcode_t *insn)
+{
+	return is_swbp_insn(&insn[0]) &&
+	       !memcmp(&insn[1], &lea_rsp[1], LEA_INSN_SIZE - 1) &&
+	       insn[LEA_INSN_SIZE] == CALL_INSN_OPCODE;
+}
+
 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;
 }
@@ -646,7 +665,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;
@@ -754,7 +773,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)
@@ -770,8 +789,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;
@@ -788,12 +807,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;
@@ -801,7 +820,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)
@@ -835,7 +854,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"
@@ -853,7 +872,8 @@ late_initcall(arch_uprobes_init);
 
 enum {
 	EXPECT_SWBP,
-	EXPECT_CALL,
+	EXPECT_OPTIMIZED,
+	EXPECT_SWBP_OPTIMIZED,
 };
 
 struct write_opcode_ctx {
@@ -861,30 +881,29 @@ 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.
+ * Verification callback used by uprobe_write calls to make sure the underlying
+ * instruction is in the expected stage of the INT3 update sequence.
  */
 static int verify_insn(struct page *page, unsigned long vaddr, uprobe_opcode_t *new_opcode,
 		       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;
+	case EXPECT_SWBP_OPTIMIZED:
+		if (is_swbp_opt_insns(&old_opcode[0]))
 			return 1;
 		break;
 	}
@@ -893,48 +912,112 @@ static int verify_insn(struct page *page, unsigned long vaddr, uprobe_opcode_t *
 }
 
 /*
- * Modify multi-byte instructions by using INT3 breakpoints on SMP.
+ * Modify the optimized instruction by using INT3 breakpoints on SMP.
  * We completely avoid using stop_machine() here, and achieve the
  * synchronization using INT3 breakpoints and SMP cross-calls.
  * (borrowed comment from smp_text_poke_batch_finish)
  *
- * The way it is done:
- *   - Add an INT3 trap to the address that will be patched
- *   - SMP sync all CPUs
- *   - Update all but the first byte of the patched range
- *   - SMP sync all CPUs
- *   - Replace the first byte (INT3) by the first byte of the replacing opcode
- *   - SMP sync all CPUs
+ * For optimization (int3_update_optimize):
+ *   1) Start with the uprobe INT3 trap already installed
+ *   2) Update everything but the first byte
+ *   3) Replace the first INT3 by the first byte of the LEA instruction
+ *
+ * For unoptimization (int3_update_unoptimize):
+ *   1) Start with the optimized uprobe lea/call instructions
+ *   2) Add an INT3 trap to the address that will be patched
+ *   3) Restore the NOP bytes before the call opcode
+ *   4) Replace the first INT3 by the first byte of the NOP instruction
+ *
+ * Note that unoptimization deliberately keeps the call opcode and displacement
+ * in bytes 5..9. Those bytes become operands of the restored 10-byte NOP.
+ *
+ * Since there is only a single target uprobe-trampoline for the given nop10
+ * instruction address, the CALL instruction will not be changed across
+ * unoptimization/optimization cycles.
+ * Therefore, any task that is preempted at the CALL instruction is guaranteed
+ * to observe that CALL and not anything else.
  */
-static int int3_update(struct arch_uprobe *auprobe, struct vm_area_struct *vma,
-		       unsigned long vaddr, char *insn, bool optimize)
+static int int3_update_optimize(struct arch_uprobe *auprobe, struct vm_area_struct *vma,
+				unsigned long vaddr, uprobe_opcode_t *insn)
 {
-	uprobe_opcode_t int3 = UPROBE_SWBP_INSN;
 	struct write_opcode_ctx ctx = {
 		.base = vaddr,
 	};
 	int err;
 
 	/*
-	 * Write int3 trap.
+	 * 1) Initial state after set_swbp() installed the uprobe:
+	 *    cc 2e 0f 1f 84 00 00 00 00 00
 	 *
-	 * The swbp_optimize path comes with breakpoint already installed,
-	 * so we can skip this step for optimize == true.
+	 *    After a previous unoptimization bytes 5..9 may still contain the
+	 *    old call instruction, which remains valid for threads already there.
 	 */
-	if (!optimize) {
-		ctx.expect = EXPECT_CALL;
-		err = uprobe_write(auprobe, vma, vaddr, &int3, 1, verify_insn,
-				   true /* is_register */, false /* do_update_ref_ctr */,
-				   &ctx);
-		if (err)
-			return err;
-	}
+	smp_text_poke_sync_each_cpu();
+
+	/*
+	 * 2) Rewrite the LEA tail and call displacement:
+	 *    cc [8d 64 24 80 e8 d0 d1 d2 d3]
+	 */
+	ctx.expect = EXPECT_SWBP;
+	err = uprobe_write(auprobe, vma, vaddr + 1, insn + 1,
+			   OPT_INSN_SIZE - 1, verify_insn,
+			   true /* is_register */, false /* do_update_ref_ctr */,
+			   &ctx);
+	if (err)
+		return err;
 
 	smp_text_poke_sync_each_cpu();
 
-	/* Write all but the first byte of the patched range. */
+	/*
+	 * 3) Publish the first LEA byte:
+	 *    [48] 8d 64 24 80 e8 d0 d1 d2 d3
+	 *
+	 *    From offset 0 this is:
+	 *      lea -0x80(%rsp), %rsp
+	 *      call <uprobe-trampoline>
+	 */
+	ctx.expect = EXPECT_SWBP_OPTIMIZED;
+	err = uprobe_write(auprobe, vma, vaddr, insn, 1, verify_insn,
+			   true /* is_register */, false /* do_update_ref_ctr */,
+			   &ctx);
+	if (err)
+		goto error;
+
+	smp_text_poke_sync_each_cpu();
+	return 0;
+
+error:
+	/*
+	 * In all intermediate states byte 0 is INT3, so EXPECT_SWBP covers every
+	 * case. Restore NOP bytes 1..4, but keep the valid CALL at bytes 5..9
+	 * for a thread that had already executed the LEA before a previous
+	 * unoptimization.
+	 */
 	ctx.expect = EXPECT_SWBP;
-	err = uprobe_write(auprobe, vma, vaddr + 1, insn + 1, 4, verify_insn,
+	uprobe_write(auprobe, vma, vaddr + 1, auprobe->insn + 1,
+		     LEA_INSN_SIZE - 1, verify_insn, true, false, &ctx);
+	smp_text_poke_sync_each_cpu();
+	return err;
+}
+
+static int int3_update_unoptimize(struct arch_uprobe *auprobe, struct vm_area_struct *vma,
+				  unsigned long vaddr, uprobe_opcode_t *insn)
+{
+	uprobe_opcode_t int3 = UPROBE_SWBP_INSN;
+	struct write_opcode_ctx ctx = {
+		.base = vaddr,
+		.expect = EXPECT_OPTIMIZED,
+	};
+	int err;
+
+	/*
+	 * 1) Initial optimized state:
+	 *    48 8d 64 24 80 e8 d0 d1 d2 d3
+	 *
+	 * 2) Trap new entries before restoring the NOP bytes:
+	 *    [cc] 8d 64 24 80 e8 d0 d1 d2 d3
+	 */
+	err = uprobe_write(auprobe, vma, vaddr, &int3, 1, verify_insn,
 			   true /* is_register */, false /* do_update_ref_ctr */,
 			   &ctx);
 	if (err)
@@ -943,13 +1026,31 @@ static int int3_update(struct arch_uprobe *auprobe, struct vm_area_struct *vma,
 	smp_text_poke_sync_each_cpu();
 
 	/*
-	 * Write first byte.
+	 * 3) Restore bytes 1..4 of the original NOP while keeping byte 0 trapped
+	 *    and byte 5 as CALL:
+	 *    cc [2e 0f 1f 84] e8 d0 d1 d2 d3
+	 */
+	ctx.expect = EXPECT_SWBP_OPTIMIZED;
+	err = uprobe_write(auprobe, vma, vaddr + 1, insn + 1,
+			   LEA_INSN_SIZE - 1, verify_insn,
+			   true /* is_register */, false /* do_update_ref_ctr */,
+			   &ctx);
+	if (err)
+		return err;
+
+	smp_text_poke_sync_each_cpu();
+
+	/*
+	 * 4) Publish the first byte of the original NOP:
+	 *    [66] 2e 0f 1f 84 e8 d0 d1 d2 d3
 	 *
-	 * The swbp_unoptimize needs to finish uprobe removal together
-	 * with ref_ctr update, using uprobe_write with proper flags.
+	 * From offset 0 this is the restored 10-byte NOP; the CALL opcode and
+	 * displacement are now only NOP operands.  Offset 5 still decodes as
+	 * CALL for a thread that was already there.
 	 */
+	ctx.expect = EXPECT_SWBP;
 	err = uprobe_write(auprobe, vma, vaddr, insn, 1, verify_insn,
-			   optimize /* is_register */, !optimize /* do_update_ref_ctr */,
+			   false /* is_register */, true /* do_update_ref_ctr */,
 			   &ctx);
 	if (err)
 		return err;
@@ -961,17 +1062,25 @@ 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_optimize(auprobe, vma, vaddr, insn);
 }
 
 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 */);
+	return int3_update_unoptimize(auprobe, vma, vaddr, auprobe->insn);
 }
 
 static int copy_from_vaddr(struct mm_struct *mm, unsigned long vaddr, void *dst, int len)
@@ -993,19 +1102,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);
@@ -1077,7 +1186,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;
@@ -1088,7 +1197,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;
@@ -1104,16 +1213,32 @@ void arch_uprobe_optimize(struct arch_uprobe *auprobe, unsigned long vaddr)
 	mmap_write_unlock(mm);
 }
 
+static bool is_optimizable_nop10(struct insn *insn)
+{
+	static const u8 nop10_prefix[] = {
+		0x66, 0x2e, 0x0f, 0x1f, 0x84
+	};
+
+	/*
+	 * Restrict this to the 10-byte NOP form whose last 5 bytes are
+	 * SIB/displacement operands. Unoptimization keeps the call opcode and
+	 * displacement in those bytes, so other NOP encodings are not safe.
+	 */
+	return insn->length == OPT_INSN_SIZE &&
+	       insn_is_nop(insn) &&
+	       !memcmp(insn->kaddr, nop10_prefix, ARRAY_SIZE(nop10_prefix));
+}
+
 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))
+	if (!is_optimizable_nop10(insn))
 		return false;
 
 	/* We can't do cross page atomic writes yet. */
-	return PAGE_SIZE - (vaddr & ~PAGE_MASK) >= 5;
+	return PAGE_SIZE - (vaddr & ~PAGE_MASK) >= OPT_INSN_SIZE;
 }
 #else /* 32-bit: */
 /*
Signature · 1 line
2.54.0
#22 lore raw

Re: [PATCHv4 05/13] uprobes/x86: Move optimized uprobe from nop5 to nop10

Quoted 108 lines · 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.

We use nop10 and following transformation to optimized instructions above and back as suggested by Peterz [2].

Optimize path (int3_update_optimize):

1) Initial state after set_swbp() installed the uprobe:
cc 2e 0f 1f 84 00 00 00 00 00

From offset 0 this is INT3 followed by the tail of the original 10-byte NOP.

After a previous unoptimization bytes 5..9 may still contain the old call instruction, which remains valid for threads already there.

2) Rewrite the LEA tail and call displacement:
cc [8d 64 24 80 e8 d0 d1 d2 d3]

From offset 0 this traps on the uprobe INT3. Bytes 1..9 are not executable entry points while byte 0 is trapped.

3) Publish the first LEA byte:

      [48] 8d 64 24 80 e8 d0 d1 d2 d3

     From offset 0 this is:
        lea -0x80(%rsp), %rsp
        call <uprobe-trampoline>

Unoptimize path (int3_update_unoptimize):

1) Initial optimized state:
48 8d 64 24 80 e8 d0 d1 d2 d3
Same as 3) above.

2) Trap new entries before restoring the NOP bytes:
[cc] 8d 64 24 80 e8 d0 d1 d2 d3

From offset 0 this traps. A thread that had already executed the LEA can still reach the intact CALL at offset 5.

3) Restore bytes 1..4 of the original NOP while keeping byte 0 trapped and byte 5 as CALL.
cc [2e 0f 1f 84] e8 d0 d1 d2 d3

From offset 0 this still traps. Offset 5 is still the CALL for any thread that was already past the first LEA byte.

4) Publish the first byte of the original NOP:
[66] 2e 0f 1f 84 e8 d0 d1 d2 d3

From offset 0 this is the restored 10-byte NOP; the CALL opcode and displacement are now only NOP operands. Offset 5 still decodes as CALL for a thread that was already there.

Tthere is only a single target uprobe-trampoline for the given nop10 instruction address, so the CALL instruction will not be changed across unoptimization/optimization cycles.
Therefore, any task that is preempted at the CALL instruction is guaranteed to observe that CALL and not anything else.

Note as explained in [2] we need to use following nop10:
PF1 PF2 ESC NOPL MOD SIB DISP32
NOP10: 0x66, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 -- cs nopw 0x00000000(%rax,%rax,1)

which means we need to allow 0x2e prefix which maps to INAT_PFX_CS attribute in is_prefix_bad function.

Also changing the uprobe syscall error when called out of uprobe trampoline to -EPROTO, so we are able to detect the fixed kernel.

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]/
[2] https://lore.kernel.org/bpf/[email protected]/#t

Fixes ba2bfc97b462 ("uprobes/x86: Add support to optimize uprobes")
Assisted-by Codex:GPT-5.5
---
 arch/x86/kernel/uprobes.c | 255 ++++++++++++++++++++++++++++----------
 1 file changed, 190 insertions(+), 65 deletions(-)

[...]

arch/x86/kernel/uprobes.c static int int3_update(struct arch_uprobe *auprobe, struct vm_area_struct *vma, · line 1036

Quoted 13 lines
arch/x86/kernel/uprobes.c +8 -1
@@ -943,13 +1026,31 @@ static int int3_update(struct arch_uprobe *auprobe, struct vm_area_struct *vma,
        smp_text_poke_sync_each_cpu();
 
        /*
-        * Write first byte.
+        * 3) Restore bytes 1..4 of the original NOP while keeping byte 0 trapped
+        *    and byte 5 as CALL:
+        *    cc [2e 0f 1f 84] e8 d0 d1 d2 d3
+        */
+       ctx.expect = EXPECT_SWBP_OPTIMIZED;
+       err = uprobe_write(auprobe, vma, vaddr + 1, insn + 1,
+                          LEA_INSN_SIZE - 1, verify_insn,
+                          true /* is_register */, false /* do_update_ref_ctr */,

tbh, it's quite subtle and non-obvious why is_register should be set to true first two times (and especially that is_register and do_update_ref_ctr are implicitly connected), not sure how to make it cleaner, but maybe leave a short comment explaining this twice register, once unregister sequence?

Quoted 5 lines
arch/x86/kernel/uprobes.c +5 -0
+                          &ctx);
+       if (err)
+               return err;
+
+       smp_text_poke_sync_each_cpu();

[...]

#23 lore raw

Re: [PATCHv4 05/13] uprobes/x86: Move optimized uprobe from nop5 to nop10

Quoted 115 lines · Andrii Nakryiko
Quoted 108 lines · 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.

We use nop10 and following transformation to optimized instructions above and back as suggested by Peterz [2].

Optimize path (int3_update_optimize):

1) Initial state after set_swbp() installed the uprobe:
cc 2e 0f 1f 84 00 00 00 00 00

From offset 0 this is INT3 followed by the tail of the original 10-byte NOP.

After a previous unoptimization bytes 5..9 may still contain the old call instruction, which remains valid for threads already there.

2) Rewrite the LEA tail and call displacement:
cc [8d 64 24 80 e8 d0 d1 d2 d3]

From offset 0 this traps on the uprobe INT3. Bytes 1..9 are not executable entry points while byte 0 is trapped.

3) Publish the first LEA byte:

      [48] 8d 64 24 80 e8 d0 d1 d2 d3

     From offset 0 this is:
        lea -0x80(%rsp), %rsp
        call <uprobe-trampoline>

Unoptimize path (int3_update_unoptimize):

1) Initial optimized state:
48 8d 64 24 80 e8 d0 d1 d2 d3
Same as 3) above.

2) Trap new entries before restoring the NOP bytes:
[cc] 8d 64 24 80 e8 d0 d1 d2 d3

From offset 0 this traps. A thread that had already executed the LEA can still reach the intact CALL at offset 5.

3) Restore bytes 1..4 of the original NOP while keeping byte 0 trapped and byte 5 as CALL.
cc [2e 0f 1f 84] e8 d0 d1 d2 d3

From offset 0 this still traps. Offset 5 is still the CALL for any thread that was already past the first LEA byte.

4) Publish the first byte of the original NOP:
[66] 2e 0f 1f 84 e8 d0 d1 d2 d3

From offset 0 this is the restored 10-byte NOP; the CALL opcode and displacement are now only NOP operands. Offset 5 still decodes as CALL for a thread that was already there.

Tthere is only a single target uprobe-trampoline for the given nop10 instruction address, so the CALL instruction will not be changed across unoptimization/optimization cycles.
Therefore, any task that is preempted at the CALL instruction is guaranteed to observe that CALL and not anything else.

Note as explained in [2] we need to use following nop10:
PF1 PF2 ESC NOPL MOD SIB DISP32
NOP10: 0x66, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 -- cs nopw 0x00000000(%rax,%rax,1)

which means we need to allow 0x2e prefix which maps to INAT_PFX_CS attribute in is_prefix_bad function.

Also changing the uprobe syscall error when called out of uprobe trampoline to -EPROTO, so we are able to detect the fixed kernel.

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]/
[2] https://lore.kernel.org/bpf/[email protected]/#t

Fixes ba2bfc97b462 ("uprobes/x86: Add support to optimize uprobes")
Assisted-by Codex:GPT-5.5
---
 arch/x86/kernel/uprobes.c | 255 ++++++++++++++++++++++++++++----------
 1 file changed, 190 insertions(+), 65 deletions(-)

[...]

Quoted 3 lines
(quoted diff) +0 -0
@@ -943,13 +1026,31 @@ static int int3_update(struct arch_uprobe *auprobe, struct vm_area_struct *vma,
        smp_text_poke_sync_each_cpu();
 
Quoted 16 lines

Review static int int3_update(struct arch_uprobe *auprobe, struct vm_area_struct *vma, · line 1036

Quoted 10 lines
(quoted diff) +8 -1
        /*
-        * Write first byte.
+        * 3) Restore bytes 1..4 of the original NOP while keeping byte 0 trapped
+        *    and byte 5 as CALL:
+        *    cc [2e 0f 1f 84] e8 d0 d1 d2 d3
+        */
+       ctx.expect = EXPECT_SWBP_OPTIMIZED;
+       err = uprobe_write(auprobe, vma, vaddr + 1, insn + 1,
+                          LEA_INSN_SIZE - 1, verify_insn,
+                          true /* is_register */, false /* do_update_ref_ctr */,

tbh, it's quite subtle and non-obvious why is_register should be set to true first two times (and especially that is_register and do_update_ref_ctr are implicitly connected), not sure how to make it cleaner, but maybe leave a short comment explaining this twice register, once unregister sequence?

ok, I came up with comment below

thanks,
jirka

---

arch/x86/kernel/uprobes.c +6 -0
index de544516ea70..92449f34c005 100644
@@ -1011,6 +1011,12 @@ static int int3_update_unoptimize(struct arch_uprobe *auprobe, struct vm_area_st
 	int err;
 
 	/*
+	 * Note the first two uprobe_write calls use is_register=true, because they
+	 * are intermediate patching states while the probe is still active.
+	 *
+	 * The last uprobe_write to nop10 instruction is called with is_register=false
+	 * and do_update_ref_ctr=true to trigger the refctr update.
+	 *
 	 * 1) Initial optimized state:
 	 *    48 8d 64 24 80 e8 d0 d1 d2 d3
 	 *
#24 lore raw

Re: [PATCHv4 05/13] uprobes/x86: Move optimized uprobe from nop5 to nop10

Quoted 146 lines · Jiri Olsa
Quoted 115 lines · Andrii Nakryiko
Quoted 108 lines · 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.

We use nop10 and following transformation to optimized instructions above and back as suggested by Peterz [2].

Optimize path (int3_update_optimize):

1) Initial state after set_swbp() installed the uprobe:
cc 2e 0f 1f 84 00 00 00 00 00

From offset 0 this is INT3 followed by the tail of the original 10-byte NOP.

After a previous unoptimization bytes 5..9 may still contain the old call instruction, which remains valid for threads already there.

2) Rewrite the LEA tail and call displacement:
cc [8d 64 24 80 e8 d0 d1 d2 d3]

From offset 0 this traps on the uprobe INT3. Bytes 1..9 are not executable entry points while byte 0 is trapped.

3) Publish the first LEA byte:

      [48] 8d 64 24 80 e8 d0 d1 d2 d3

     From offset 0 this is:
        lea -0x80(%rsp), %rsp
        call <uprobe-trampoline>

Unoptimize path (int3_update_unoptimize):

1) Initial optimized state:
48 8d 64 24 80 e8 d0 d1 d2 d3
Same as 3) above.

2) Trap new entries before restoring the NOP bytes:
[cc] 8d 64 24 80 e8 d0 d1 d2 d3

From offset 0 this traps. A thread that had already executed the LEA can still reach the intact CALL at offset 5.

3) Restore bytes 1..4 of the original NOP while keeping byte 0 trapped and byte 5 as CALL.
cc [2e 0f 1f 84] e8 d0 d1 d2 d3

From offset 0 this still traps. Offset 5 is still the CALL for any thread that was already past the first LEA byte.

4) Publish the first byte of the original NOP:
[66] 2e 0f 1f 84 e8 d0 d1 d2 d3

From offset 0 this is the restored 10-byte NOP; the CALL opcode and displacement are now only NOP operands. Offset 5 still decodes as CALL for a thread that was already there.

Tthere is only a single target uprobe-trampoline for the given nop10 instruction address, so the CALL instruction will not be changed across unoptimization/optimization cycles.
Therefore, any task that is preempted at the CALL instruction is guaranteed to observe that CALL and not anything else.

Note as explained in [2] we need to use following nop10:
PF1 PF2 ESC NOPL MOD SIB DISP32
NOP10: 0x66, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 -- cs nopw 0x00000000(%rax,%rax,1)

which means we need to allow 0x2e prefix which maps to INAT_PFX_CS attribute in is_prefix_bad function.

Also changing the uprobe syscall error when called out of uprobe trampoline to -EPROTO, so we are able to detect the fixed kernel.

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]/
[2] https://lore.kernel.org/bpf/[email protected]/#t

Fixes ba2bfc97b462 ("uprobes/x86: Add support to optimize uprobes")
Assisted-by Codex:GPT-5.5
---
 arch/x86/kernel/uprobes.c | 255 ++++++++++++++++++++++++++++----------
 1 file changed, 190 insertions(+), 65 deletions(-)

[...]

Quoted 3 lines
(quoted diff) +0 -0
@@ -943,13 +1026,31 @@ static int int3_update(struct arch_uprobe *auprobe, struct vm_area_struct *vma,
        smp_text_poke_sync_each_cpu();
 
Quoted 16 lines

Review static int int3_update(struct arch_uprobe *auprobe, struct vm_area_struct *vma, · line 1036

Quoted 10 lines
(quoted diff) +8 -1
        /*
-        * Write first byte.
+        * 3) Restore bytes 1..4 of the original NOP while keeping byte 0 trapped
+        *    and byte 5 as CALL:
+        *    cc [2e 0f 1f 84] e8 d0 d1 d2 d3
+        */
+       ctx.expect = EXPECT_SWBP_OPTIMIZED;
+       err = uprobe_write(auprobe, vma, vaddr + 1, insn + 1,
+                          LEA_INSN_SIZE - 1, verify_insn,
+                          true /* is_register */, false /* do_update_ref_ctr */,

tbh, it's quite subtle and non-obvious why is_register should be set to true first two times (and especially that is_register and do_update_ref_ctr are implicitly connected), not sure how to make it cleaner, but maybe leave a short comment explaining this twice register, once unregister sequence?

ok, I came up with comment below

thanks,
jirka

---

arch/x86/kernel/uprobes.c +0 -0
index de544516ea70..92449f34c005 100644

arch/x86/kernel/uprobes.c static int int3_update_unoptimize(struct arch_uprobe *auprobe, struct vm_area_st · line 1015

Quoted 5 lines
arch/x86/kernel/uprobes.c +2 -0
@@ -1011,6 +1011,12 @@ static int int3_update_unoptimize(struct arch_uprobe *auprobe, struct vm_area_st
        int err;
 
        /*
+        * Note the first two uprobe_write calls use is_register=true, because they
+        * are intermediate patching states while the probe is still active.

this doesn't really explain why is_register=true is the right one. It actually doesn't matter as long as do_update_ref_ctr=true, isn't that right? So maybe just to avoid a bit of confusion let's pass is_register=false and do_update_ref_ctr=false, and in the comment explain as you said that it's intermediate update and we don't want to update refctr just yet until the very last step?

Quoted 7 lines
arch/x86/kernel/uprobes.c +4 -0
+        *
+        * The last uprobe_write to nop10 instruction is called with is_register=false
+        * and do_update_ref_ctr=true to trigger the refctr update.
+        *
         * 1) Initial optimized state:
         *    48 8d 64 24 80 e8 d0 d1 d2 d3
         *
#25 lore raw

Re: [PATCHv4 05/13] uprobes/x86: Move optimized uprobe from nop5 to nop10

Quoted 149 lines · Andrii Nakryiko
Quoted 148 lines · Jiri Olsa
Quoted 115 lines · Andrii Nakryiko
Quoted 108 lines · 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.

We use nop10 and following transformation to optimized instructions above and back as suggested by Peterz [2].

Optimize path (int3_update_optimize):

1) Initial state after set_swbp() installed the uprobe:
cc 2e 0f 1f 84 00 00 00 00 00

From offset 0 this is INT3 followed by the tail of the original 10-byte NOP.

After a previous unoptimization bytes 5..9 may still contain the old call instruction, which remains valid for threads already there.

2) Rewrite the LEA tail and call displacement:
cc [8d 64 24 80 e8 d0 d1 d2 d3]

From offset 0 this traps on the uprobe INT3. Bytes 1..9 are not executable entry points while byte 0 is trapped.

3) Publish the first LEA byte:

      [48] 8d 64 24 80 e8 d0 d1 d2 d3

     From offset 0 this is:
        lea -0x80(%rsp), %rsp
        call <uprobe-trampoline>

Unoptimize path (int3_update_unoptimize):

1) Initial optimized state:
48 8d 64 24 80 e8 d0 d1 d2 d3
Same as 3) above.

2) Trap new entries before restoring the NOP bytes:
[cc] 8d 64 24 80 e8 d0 d1 d2 d3

From offset 0 this traps. A thread that had already executed the LEA can still reach the intact CALL at offset 5.

3) Restore bytes 1..4 of the original NOP while keeping byte 0 trapped and byte 5 as CALL.
cc [2e 0f 1f 84] e8 d0 d1 d2 d3

From offset 0 this still traps. Offset 5 is still the CALL for any thread that was already past the first LEA byte.

4) Publish the first byte of the original NOP:
[66] 2e 0f 1f 84 e8 d0 d1 d2 d3

From offset 0 this is the restored 10-byte NOP; the CALL opcode and displacement are now only NOP operands. Offset 5 still decodes as CALL for a thread that was already there.

Tthere is only a single target uprobe-trampoline for the given nop10 instruction address, so the CALL instruction will not be changed across unoptimization/optimization cycles.
Therefore, any task that is preempted at the CALL instruction is guaranteed to observe that CALL and not anything else.

Note as explained in [2] we need to use following nop10:
PF1 PF2 ESC NOPL MOD SIB DISP32
NOP10: 0x66, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 -- cs nopw 0x00000000(%rax,%rax,1)

which means we need to allow 0x2e prefix which maps to INAT_PFX_CS attribute in is_prefix_bad function.

Also changing the uprobe syscall error when called out of uprobe trampoline to -EPROTO, so we are able to detect the fixed kernel.

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]/
[2] https://lore.kernel.org/bpf/[email protected]/#t

Fixes ba2bfc97b462 ("uprobes/x86: Add support to optimize uprobes")
Assisted-by Codex:GPT-5.5
---
 arch/x86/kernel/uprobes.c | 255 ++++++++++++++++++++++++++++----------
 1 file changed, 190 insertions(+), 65 deletions(-)

[...]

Quoted 3 lines
(quoted diff) +0 -0
@@ -943,13 +1026,31 @@ static int int3_update(struct arch_uprobe *auprobe, struct vm_area_struct *vma,
        smp_text_poke_sync_each_cpu();
 
Quoted 16 lines

Review static int int3_update(struct arch_uprobe *auprobe, struct vm_area_struct *vma, · line 1036

Quoted 10 lines
(quoted diff) +8 -1
        /*
-        * Write first byte.
+        * 3) Restore bytes 1..4 of the original NOP while keeping byte 0 trapped
+        *    and byte 5 as CALL:
+        *    cc [2e 0f 1f 84] e8 d0 d1 d2 d3
+        */
+       ctx.expect = EXPECT_SWBP_OPTIMIZED;
+       err = uprobe_write(auprobe, vma, vaddr + 1, insn + 1,
+                          LEA_INSN_SIZE - 1, verify_insn,
+                          true /* is_register */, false /* do_update_ref_ctr */,

tbh, it's quite subtle and non-obvious why is_register should be set to true first two times (and especially that is_register and do_update_ref_ctr are implicitly connected), not sure how to make it cleaner, but maybe leave a short comment explaining this twice register, once unregister sequence?

ok, I came up with comment below

thanks,
jirka

---

arch/x86/kernel/uprobes.c +0 -0
index de544516ea70..92449f34c005 100644
@@ -1011,6 +1011,12 @@ static int int3_update_unoptimize(struct arch_uprobe *auprobe, struct vm_area_st
        int err;
 
Quoted 10 lines

arch/x86/kernel/uprobes.c static int int3_update_unoptimize(struct arch_uprobe *auprobe, struct vm_area_st · line 1015

Quoted 3 lines
arch/x86/kernel/uprobes.c +2 -0
        /*
+        * Note the first two uprobe_write calls use is_register=true, because they
+        * are intermediate patching states while the probe is still active.

this doesn't really explain why is_register=true is the right one. It actually doesn't matter as long as do_update_ref_ctr=true, isn't that right? So maybe just to avoid a bit of confusion let's pass is_register=false and do_update_ref_ctr=false, and in the comment explain as you said that it's intermediate update and we don't want to update refctr just yet until the very last step?

apart from refctr update there's also different way the concerned page is managed, IIUC:

with is_register=true we force to get exclusive anonymous page for the update (or pin the existing one)

with is_register=false we try to zap the private anonymous page and return the mapping to the original page

there are several comments on this in uprobe_write/__uprobe_write

how about the update below

jirka

---

arch/x86/kernel/uprobes.c +10 -0
index de544516ea70..09f5ff71227c 100644
@@ -1011,6 +1011,16 @@ static int int3_update_unoptimize(struct arch_uprobe *auprobe, struct vm_area_st
 	int err;
 
 	/*
+	 * Note the first two uprobe_write calls use is_register=true, because they
+	 * are intermediate patching states while the probe is still active, so
+	 * we force the exclusive anonymous page for the update.
+	 * Also we use do_update_ref_ctr=false because refctr was already updated by
+	 * the initial int3 install.
+	 *
+	 * The last uprobe_write to nop10 instruction is called with is_register=false
+	 * and do_update_ref_ctr=true to trigger the refctr update and to instruct
+	 * uprobe_write to zap the anonymous page if it now matches the file page.
+	 *
 	 * 1) Initial optimized state:
 	 *    48 8d 64 24 80 e8 d0 d1 d2 d3
 	 *
#26 lore raw

Re: [PATCHv4 05/13] uprobes/x86: Move optimized uprobe from nop5 to nop10

Quoted 187 lines · Jiri Olsa
Quoted 149 lines · Andrii Nakryiko
Quoted 148 lines · Jiri Olsa
Quoted 115 lines · Andrii Nakryiko
Quoted 108 lines · 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.

We use nop10 and following transformation to optimized instructions above and back as suggested by Peterz [2].

Optimize path (int3_update_optimize):

1) Initial state after set_swbp() installed the uprobe:
cc 2e 0f 1f 84 00 00 00 00 00

From offset 0 this is INT3 followed by the tail of the original 10-byte NOP.

After a previous unoptimization bytes 5..9 may still contain the old call instruction, which remains valid for threads already there.

2) Rewrite the LEA tail and call displacement:
cc [8d 64 24 80 e8 d0 d1 d2 d3]

From offset 0 this traps on the uprobe INT3. Bytes 1..9 are not executable entry points while byte 0 is trapped.

3) Publish the first LEA byte:

      [48] 8d 64 24 80 e8 d0 d1 d2 d3

     From offset 0 this is:
        lea -0x80(%rsp), %rsp
        call <uprobe-trampoline>

Unoptimize path (int3_update_unoptimize):

1) Initial optimized state:
48 8d 64 24 80 e8 d0 d1 d2 d3
Same as 3) above.

2) Trap new entries before restoring the NOP bytes:
[cc] 8d 64 24 80 e8 d0 d1 d2 d3

From offset 0 this traps. A thread that had already executed the LEA can still reach the intact CALL at offset 5.

3) Restore bytes 1..4 of the original NOP while keeping byte 0 trapped and byte 5 as CALL.
cc [2e 0f 1f 84] e8 d0 d1 d2 d3

From offset 0 this still traps. Offset 5 is still the CALL for any thread that was already past the first LEA byte.

4) Publish the first byte of the original NOP:
[66] 2e 0f 1f 84 e8 d0 d1 d2 d3

From offset 0 this is the restored 10-byte NOP; the CALL opcode and displacement are now only NOP operands. Offset 5 still decodes as CALL for a thread that was already there.

Tthere is only a single target uprobe-trampoline for the given nop10 instruction address, so the CALL instruction will not be changed across unoptimization/optimization cycles.
Therefore, any task that is preempted at the CALL instruction is guaranteed to observe that CALL and not anything else.

Note as explained in [2] we need to use following nop10:
PF1 PF2 ESC NOPL MOD SIB DISP32
NOP10: 0x66, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 -- cs nopw 0x00000000(%rax,%rax,1)

which means we need to allow 0x2e prefix which maps to INAT_PFX_CS attribute in is_prefix_bad function.

Also changing the uprobe syscall error when called out of uprobe trampoline to -EPROTO, so we are able to detect the fixed kernel.

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]/
[2] https://lore.kernel.org/bpf/[email protected]/#t

Fixes ba2bfc97b462 ("uprobes/x86: Add support to optimize uprobes")
Assisted-by Codex:GPT-5.5
---
 arch/x86/kernel/uprobes.c | 255 ++++++++++++++++++++++++++++----------
 1 file changed, 190 insertions(+), 65 deletions(-)

[...]

Quoted 3 lines
(quoted diff) +0 -0
@@ -943,13 +1026,31 @@ static int int3_update(struct arch_uprobe *auprobe, struct vm_area_struct *vma,
        smp_text_poke_sync_each_cpu();
 
Quoted 16 lines

Review static int int3_update(struct arch_uprobe *auprobe, struct vm_area_struct *vma, · line 1036

Quoted 10 lines
(quoted diff) +8 -1
        /*
-        * Write first byte.
+        * 3) Restore bytes 1..4 of the original NOP while keeping byte 0 trapped
+        *    and byte 5 as CALL:
+        *    cc [2e 0f 1f 84] e8 d0 d1 d2 d3
+        */
+       ctx.expect = EXPECT_SWBP_OPTIMIZED;
+       err = uprobe_write(auprobe, vma, vaddr + 1, insn + 1,
+                          LEA_INSN_SIZE - 1, verify_insn,
+                          true /* is_register */, false /* do_update_ref_ctr */,

tbh, it's quite subtle and non-obvious why is_register should be set to true first two times (and especially that is_register and do_update_ref_ctr are implicitly connected), not sure how to make it cleaner, but maybe leave a short comment explaining this twice register, once unregister sequence?

ok, I came up with comment below

thanks,
jirka

---

arch/x86/kernel/uprobes.c +0 -0
index de544516ea70..92449f34c005 100644
@@ -1011,6 +1011,12 @@ static int int3_update_unoptimize(struct arch_uprobe *auprobe, struct vm_area_st
        int err;
 
Quoted 10 lines

arch/x86/kernel/uprobes.c static int int3_update_unoptimize(struct arch_uprobe *auprobe, struct vm_area_st

Quoted 3 lines
arch/x86/kernel/uprobes.c +2 -0
        /*
+        * Note the first two uprobe_write calls use is_register=true, because they
+        * are intermediate patching states while the probe is still active.

this doesn't really explain why is_register=true is the right one. It actually doesn't matter as long as do_update_ref_ctr=true, isn't that right? So maybe just to avoid a bit of confusion let's pass is_register=false and do_update_ref_ctr=false, and in the comment explain as you said that it's intermediate update and we don't want to update refctr just yet until the very last step?

apart from refctr update there's also different way the concerned page is managed, IIUC:

with is_register=true we force to get exclusive anonymous page for the update (or pin the existing one)

with is_register=false we try to zap the private anonymous page and return the mapping to the original page

there are several comments on this in uprobe_write/__uprobe_write

how about the update below

jirka

---

arch/x86/kernel/uprobes.c +0 -0
index de544516ea70..09f5ff71227c 100644
@@ -1011,6 +1011,16 @@ static int int3_update_unoptimize(struct arch_uprobe *auprobe, struct vm_area_st
        int err;
 
        /*

arch/x86/kernel/uprobes.c static int int3_update_unoptimize(struct arch_uprobe *auprobe, struct vm_area_st · line 1023

Quoted 10 lines
arch/x86/kernel/uprobes.c +10 -0
+        * Note the first two uprobe_write calls use is_register=true, because they
+        * are intermediate patching states while the probe is still active, so
+        * we force the exclusive anonymous page for the update.
+        * Also we use do_update_ref_ctr=false because refctr was already updated by
+        * the initial int3 install.
+        *
+        * The last uprobe_write to nop10 instruction is called with is_register=false
+        * and do_update_ref_ctr=true to trigger the refctr update and to instruct
+        * uprobe_write to zap the anonymous page if it now matches the file page.
+        *

lgtm!

Quoted 3 lines
arch/x86/kernel/uprobes.c +0 -0
         * 1) Initial optimized state:
         *    48 8d 64 24 80 e8 d0 d1 d2 d3
         *
maintainer Reviewed-by on v4 5/13
#27 lore raw

Re: [PATCHv4 05/13] uprobes/x86: Move optimized uprobe from nop5 to nop10

In reply to Jiri Olsa
Quoted 9 lines · Jiri Olsa

+ * Note that unoptimization deliberately keeps the call opcode and displacement
+ * in bytes 5..9. Those bytes become operands of the restored 10-byte NOP.
+ *
+ * Since there is only a single target uprobe-trampoline for the given nop10
+ * instruction address, the CALL instruction will not be changed across
+ * unoptimization/optimization cycles.
+ * Therefore, any task that is preempted at the CALL instruction is guaranteed
+ * to observe that CALL and not anything else.

Understand... and I guess synchronize_rcu_tasks() is too heavy.

But this means that unregister/unapply will never discard the COW'ed anonymous page with optimized up; __uprobe_write() -> orig_page_is_identical() will never be true... Plus this means that we can never "gc" the unused tramp vma's, but this is minor.

OK. This is not critical, and other than that I don't see any problems in yout patch. (but I am sure this is only because I don't understand this code/patch enough ;)

So, FWIW

maintainer
#28 lore raw

Re: [PATCHv4 05/13] uprobes/x86: Move optimized uprobe from nop5 to nop10

In reply to Jiri Olsa
Quoted 3 lines · Jiri Olsa

which means we need to allow 0x2e prefix which maps to INAT_PFX_CS attribute in is_prefix_bad function.

...

arch/x86/kernel/uprobes.c static bool is_prefix_bad(struct insn *insn) · line 269

Quoted 7 lines
arch/x86/kernel/uprobes.c +0 -1
@@ -266,7 +266,6 @@ static bool is_prefix_bad(struct insn *insn)
 		attr = inat_get_opcode_attribute(p);
 		switch (attr) {
 		case INAT_MAKE_PREFIX(INAT_PFX_ES):
-		case INAT_MAKE_PREFIX(INAT_PFX_CS):

I know nothing about how x86 CPU works, so let me ask...

What if insn->x86_64 is false? Is it safe to allow the CS prefix in this case?

Oleg.

#29 lore raw

Re: [PATCHv4 05/13] uprobes/x86: Move optimized uprobe from nop5 to nop10

Quoted 17 lines · Oleg Nesterov
Quoted 3 lines · Jiri Olsa

which means we need to allow 0x2e prefix which maps to INAT_PFX_CS attribute in is_prefix_bad function.

...

arch/x86/kernel/uprobes.c static bool is_prefix_bad(struct insn *insn) · line 269

Quoted 7 lines
arch/x86/kernel/uprobes.c +0 -1
@@ -266,7 +266,6 @@ static bool is_prefix_bad(struct insn *insn)
 		attr = inat_get_opcode_attribute(p);
 		switch (attr) {
 		case INAT_MAKE_PREFIX(INAT_PFX_ES):
-		case INAT_MAKE_PREFIX(INAT_PFX_CS):

I know nothing about how x86 CPU works, so let me ask...

Quoted 5 lines

What if insn->x86_64 is false? Is it safe to allow the CS prefix in this case?

Oleg.

hum, right.. I think we could make it x86_64 specific

arch/x86/kernel/uprobes.c +4 -0
index 04cd2cdce8c8..de60ec1eeee7 100644
@@ -265,6 +265,10 @@ static bool is_prefix_bad(struct insn *insn)
 
 		attr = inat_get_opcode_attribute(p);
 		switch (attr) {
+		case INAT_MAKE_PREFIX(INAT_PFX_CS):
+			if (insn->x86_64)
+				break;
+			fallthrough;
 		case INAT_MAKE_PREFIX(INAT_PFX_ES):
 		case INAT_MAKE_PREFIX(INAT_PFX_DS):
 		case INAT_MAKE_PREFIX(INAT_PFX_SS):

or we could just skip it for nop10.. maybe that's better

jirka

arch/x86/kernel/uprobes.c +1 -1
index 04cd2cdce8c8..21f26e6fd452 100644
@@ -285,7 +285,7 @@ static int uprobe_init_insn(struct arch_uprobe *auprobe, struct insn *insn, bool
 	if (ret < 0)
 		return -ENOEXEC;
 
-	if (is_prefix_bad(insn))
+	if (!is_optimizable_nop10(insn) && is_prefix_bad(insn))
 		return -ENOTSUPP;
 
 	/* We should not singlestep on the exception masking instructions */
maintainer
#30 lore raw

Re: [PATCHv4 05/13] uprobes/x86: Move optimized uprobe from nop5 to nop10

Quoted 15 lines · Jiri Olsa
arch/x86/kernel/uprobes.c +4 -0
@@ -265,6 +265,10 @@ static bool is_prefix_bad(struct insn *insn)
 
 		attr = inat_get_opcode_attribute(p);
 		switch (attr) {
+		case INAT_MAKE_PREFIX(INAT_PFX_CS):
+			if (insn->x86_64)
+				break;
+			fallthrough;
 		case INAT_MAKE_PREFIX(INAT_PFX_ES):
 		case INAT_MAKE_PREFIX(INAT_PFX_DS):
 		case INAT_MAKE_PREFIX(INAT_PFX_SS):
Quoted 1 line

or we could just skip it for nop10.. maybe that's better

Well, if you ask me I'd agree with the "maybe that's better" plan ;) I mean... I don't think that INAT_PFX_CS should be "special" in is_prefix_bad.

But, whatever you do - I agree, feel free to keep my r-b.

Oleg.

#31 lore raw

Re: [PATCHv4 05/13] uprobes/x86: Move optimized uprobe from nop5 to nop10

Quoted 16 lines · Oleg Nesterov
Quoted 15 lines · Jiri Olsa
arch/x86/kernel/uprobes.c +4 -0
@@ -265,6 +265,10 @@ static bool is_prefix_bad(struct insn *insn)
 
 		attr = inat_get_opcode_attribute(p);
 		switch (attr) {
+		case INAT_MAKE_PREFIX(INAT_PFX_CS):
+			if (insn->x86_64)
+				break;
+			fallthrough;
 		case INAT_MAKE_PREFIX(INAT_PFX_ES):
 		case INAT_MAKE_PREFIX(INAT_PFX_DS):
 		case INAT_MAKE_PREFIX(INAT_PFX_SS):
Quoted 6 lines
Quoted 1 line

or we could just skip it for nop10.. maybe that's better

Well, if you ask me I'd agree with the "maybe that's better" plan ;) I mean... I don't think that INAT_PFX_CS should be "special" in is_prefix_bad.

But, whatever you do - I agree, feel free to keep my r-b.

I ended up with the bigger change below, wdyt?

jirka

---

arch/x86/kernel/uprobes.c +17 -12
index 92626fce06a9..521a120a0c78 100644
@@ -266,6 +266,7 @@ static bool is_prefix_bad(struct insn *insn)
 		attr = inat_get_opcode_attribute(p);
 		switch (attr) {
 		case INAT_MAKE_PREFIX(INAT_PFX_ES):
+		case INAT_MAKE_PREFIX(INAT_PFX_CS):
 		case INAT_MAKE_PREFIX(INAT_PFX_DS):
 		case INAT_MAKE_PREFIX(INAT_PFX_SS):
 		case INAT_MAKE_PREFIX(INAT_PFX_LOCK):
@@ -275,15 +276,9 @@ static bool is_prefix_bad(struct insn *insn)
 	return false;
 }
 
-static int uprobe_init_insn(struct arch_uprobe *auprobe, struct insn *insn, bool x86_64)
+static int uprobe_init_insn(struct arch_uprobe *auprobe, struct insn *insn)
 {
-	enum insn_mode m = x86_64 ? INSN_MODE_64 : INSN_MODE_32;
 	u32 volatile *good_insns;
-	int ret;
-
-	ret = insn_decode(insn, auprobe->insn, sizeof(auprobe->insn), m);
-	if (ret < 0)
-		return -ENOEXEC;
 
 	if (is_prefix_bad(insn))
 		return -ENOTSUPP;
@@ -292,7 +287,7 @@ static int uprobe_init_insn(struct arch_uprobe *auprobe, struct insn *insn, bool
 	if (insn_masking_exception(insn))
 		return -ENOTSUPP;
 
-	if (x86_64)
+	if (insn->x86_64)
 		good_insns = good_insns_64;
 	else
 		good_insns = good_insns_32;
@@ -1620,16 +1615,26 @@ static int push_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
  */
 int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long addr)
 {
+	enum insn_mode m = is_64bit_mm(mm) ? INSN_MODE_64 : INSN_MODE_32;
 	u8 fix_ip_or_call = UPROBE_FIX_IP;
 	struct insn insn;
 	int ret;
 
-	ret = uprobe_init_insn(auprobe, &insn, is_64bit_mm(mm));
-	if (ret)
-		return ret;
+	ret = insn_decode(&insn, auprobe->insn, sizeof(auprobe->insn), m);
+	if (ret < 0)
+		return -ENOEXEC;
 
-	if (can_optimize(&insn, addr))
+	/*
+	 * No need to check instruction in uprobe_init_insn in case we
+	 * are on top of optimizable nop10.
+	 */
+	if (can_optimize(&insn, addr)) {
 		set_bit(ARCH_UPROBE_FLAG_CAN_OPTIMIZE, &auprobe->flags);
+	} else {
+		ret = uprobe_init_insn(auprobe, &insn);
+		if (ret)
+			return ret;
+	}
 
 	ret = branch_setup_xol_ops(auprobe, &insn);
 	if (ret != -ENOSYS)
author reviewer
#32 lore raw

PATCH v4 6/13 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..484a4354e82b 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, 0x2e, 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.54.0
#33 lore raw

Re: [PATCHv4 06/13] libbpf: Change has_nop_combo to work on top of nop10

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The patch invalidly replaces `nop5` optimization support with `nop10`, which cannot be optimized…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The patch invalidly replaces nop5 optimization support with nop10, which cannot be optimized by the kernel, breaks backward compatibility for existing binaries, and causes selftest failures. --

commit df13b5dc7675e7ea56d8d1775d22295b93e92ab0
Author: Jiri Olsa <[email protected]>

libbpf: Change has_nop_combo to work on top of nop10

This commit updates the USDT nop combo detection to expect a 10-byte nop instead of a 5-byte nop. It updates has_nop_combo() to match the new byte sequence.

Quoted 4 lines
tools/lib/bpf/usdt.c +0 -0
index e3710933fd52a..484a4354e82ba 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, 0x2e, 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;
 }

[Severity: High]
Does replacing the 5-byte nop detection with a 10-byte nop detection break the kernel's uprobe optimization logic?

Looking at the kernel's can_optimize() in arch/x86/kernel/uprobes.c, it strictly requires the replaced instruction to be exactly 5 bytes. A 10-byte instruction fails this length check, causing the kernel to silently abort optimization and fall back to an unoptimized int3 probe.

Additionally, replacing this entirely rather than supplementing it means libbpf no longer recognizes the nop5 combo in existing binaries. This causes existing USDTs to fall back to unoptimized probes and breaks BPF selftests that expect the +1 offset applied.

Finally, if the kernel ever updated can_optimize() to allow 10-byte nops, sys_uprobe() currently hardcodes a return adjustment of regs->ip += 5. Returning +5 bytes into a 10-byte instruction would resume execution in the middle of the nop sequence, which could cause an application crash.

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

Re: [PATCHv4 06/13] libbpf: Change has_nop_combo to work on top of nop10

Quoted 41 lines · On Tue, May 26, 2026 at 09:28:39PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The patch invalidly replaces nop5 optimization support with nop10, which cannot be optimized by the kernel, breaks backward compatibility for existing binaries, and causes selftest failures. --

commit df13b5dc7675e7ea56d8d1775d22295b93e92ab0
Author: Jiri Olsa <[email protected]>

libbpf: Change has_nop_combo to work on top of nop10

This commit updates the USDT nop combo detection to expect a 10-byte nop instead of a 5-byte nop. It updates has_nop_combo() to match the new byte sequence.

Quoted 4 lines
tools/lib/bpf/usdt.c +0 -0
index e3710933fd52a..484a4354e82ba 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, 0x2e, 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;
 }

[Severity: High]
Does replacing the 5-byte nop detection with a 10-byte nop detection break the kernel's uprobe optimization logic?

Quoted 4 lines

Looking at the kernel's can_optimize() in arch/x86/kernel/uprobes.c, it strictly requires the replaced instruction to be exactly 5 bytes. A 10-byte instruction fails this length check, causing the kernel to silently abort optimization and fall back to an unoptimized int3 probe.

this seems wrong, the related kernel change is part of the patchset

jirka

Quoted 13 lines

Additionally, replacing this entirely rather than supplementing it means libbpf no longer recognizes the nop5 combo in existing binaries. This causes existing USDTs to fall back to unoptimized probes and breaks BPF selftests that expect the +1 offset applied.

Finally, if the kernel ever updated can_optimize() to allow 10-byte nops, sys_uprobe() currently hardcodes a return adjustment of regs->ip += 5. Returning +5 bytes into a 10-byte instruction would resume execution in the middle of the nop sequence, which could cause an application crash.

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

Re: [PATCHv4 06/13] libbpf: Change has_nop_combo to work on top of nop10

In reply to Jiri Olsa
The Fixes: tag references commit 41a5c7df4466 which originally added has_nop_combo() with correct nop5 support. However, the actual bug was introduced by commit af734a7ddb4e which changed the…
Quoted 6 lines

commit 5ad96df841a0877eeca7d2cbe4b3f726b7fd0b72

Author: Jiri Olsa <[email protected]>
Date:   Tue May 26 22:58:33 2026 +0200

libbpf: Change has_nop_combo to work on top of nop10

Quoted 7 lines

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")

The Fixes: tag references commit 41a5c7df4466 which originally added has_nop_combo() with correct nop5 support. However, the actual bug was introduced by commit af734a7ddb4e which changed the kernel's uprobe implementation from nop5 to nop10. Should the tag be:

Fixes af734a7ddb4e ("uprobes/x86: Move optimized uprobe from nop5 to nop10")

tools/lib/bpf/usdt.c struct usdt_manager *usdt_manager_new(struct bpf_object *obj) · line 310

Quoted 12 lines
tools/lib/bpf/usdt.c +1 -1
index e3710933fd52..484a4354e82b 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")
 	 */

The comment at the end of this block references commit 56101b69c919 which added the uprobe syscall. Andrii Nakryiko noted in v3 review that it would be good to also reference the commit that switches nop5 to nop10, though he acknowledged the hash isn't stable until it lands. Now that af734a7ddb4e is available, could this be updated to reference both commits?

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, 0x2e, 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 using ARRAY_SIZE(buf) instead of repeating the scalar value 11. The author preferred keeping the literal number for simplicity, though the value now appears in 5 locations (array declarations, pread size, pread check, memcmp size).

[ ... ]

---
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/26475828601

#36 lore raw

Re: [PATCHv4 06/13] libbpf: Change has_nop_combo to work on top of nop10

Quoted 14 lines · On Tue, May 26, 2026 at 09:46:37PM +0000, [email protected] wrote:
Quoted 6 lines

commit 5ad96df841a0877eeca7d2cbe4b3f726b7fd0b72

Author: Jiri Olsa <[email protected]>
Date:   Tue May 26 22:58:33 2026 +0200

libbpf: Change has_nop_combo to work on top of nop10

Quoted 7 lines

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")
Quoted 6 lines

The Fixes: tag references commit 41a5c7df4466 which originally added has_nop_combo() with correct nop5 support. However, the actual bug was introduced by commit af734a7ddb4e which changed the kernel's uprobe implementation from nop5 to nop10. Should the tag be:

Fixes af734a7ddb4e ("uprobes/x86: Move optimized uprobe from nop5 to nop10")

af734a7ddb4e hash is not available, the patch:
uprobes/x86: Move optimized uprobe from nop5 to nop10

is part of this patchset

Quoted 7 lines
Quoted 7 lines
tools/lib/bpf/usdt.c +0 -0
index e3710933fd52..484a4354e82b 100644
@@ -305,7 +305,7 @@ struct usdt_manager *usdt_manager_new(struct bpf_object *obj)
 
Quoted 12 lines

tools/lib/bpf/usdt.c struct usdt_manager *usdt_manager_new(struct bpf_object *obj) · line 310

Quoted 6 lines
tools/lib/bpf/usdt.c +1 -1
 	/*
 	 * 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")
 	 */

The comment at the end of this block references commit 56101b69c919 which added the uprobe syscall. Andrii Nakryiko noted in v3 review that it would be good to also reference the commit that switches nop5 to nop10, though he acknowledged the hash isn't stable until it lands. Now that af734a7ddb4e is available, could this be updated to reference both commits?

I proposed to change the comment after this gets merged

Quoted 13 lines
Quoted 13 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, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00,
 	};
-	unsigned char buf[6];
+	unsigned char buf[11];
 
Quoted 12 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 using ARRAY_SIZE(buf) instead of repeating the scalar value 11. The author preferred keeping the literal number for simplicity, though the value now appears in 5 locations (array declarations, pread size, pread check, memcmp size).

and I replied to that then

jirka

author reviewer
#37 lore raw

PATCH v4 7/13 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.54.0
#38 lore raw

Re: [PATCHv4 07/13] libbpf: Detect uprobe syscall with new error

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The uprobe syscall feature detection and selftest changes expect the kernel to return `-EPROTO`…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The uprobe syscall feature detection and selftest changes expect the kernel to return -EPROTO, but the corresponding kernel patch is missing from the tree, causing test failures and feature regression. --

commit 241886d344e64ee01c001ddedecbb173dc73fef3
Author: Jiri Olsa <[email protected]>

libbpf: Detect uprobe syscall with new error

This commit updates libbpf feature detection and selftests to expect -EPROTO instead of -ENXIO for the uprobe syscall. The change is intended to align with an optimized uprobe fix in the kernel.

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;

[Severity: Medium]
The commit message mentions a "previous optimized uprobe fix", but is that patch present in the current tree?

In arch/x86/kernel/uprobes.c, sys_uprobe() still returns -ENXIO. Does this cause probe_uprobe_syscall() to return false and silently disable the optimized uprobes feature?

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

Quoted 13 lines
tools/lib/bpf/features.c +0 -0
 }
 #else
 static int probe_uprobe_syscall(int token_fd)
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");

[Severity: Medium]
Will this test now deterministically fail? Since the kernel still returns -ENXIO, it seems errno will be ENXIO instead of EPROTO.

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

Re: [PATCHv4 07/13] libbpf: Detect uprobe syscall with new error

In reply to Jiri Olsa
The commit message states "In the previous optimized uprobe fix we changed the syscall error used for its detection from ENXIO to EPROTO."
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 commit message states "In the previous optimized uprobe fix we changed the syscall error used for its detection from ENXIO to EPROTO."

The Fixes tag points to commit 05738da0efa1 ("libbpf: Add uprobe syscall feature detection"), but that commit originally added the ENXIO check correctly. The bug was actually introduced by commit af734a7ddb4e ("uprobes/x86: Move optimized uprobe from nop5 to nop10"), which changed the kernel to return EPROTO instead of ENXIO but did not update the libbpf detection code to match.

Should the Fixes tag be:

Fixes af734a7ddb4e ("uprobes/x86: Move optimized uprobe from nop5 to nop10")

---
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/26475828601

author reviewer
#40 lore raw

PATCH v4 8/13 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..75687f50f4e2 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, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 /* nop, nop10 */
 #else
 #define USDT_NOP			nop
 #endif
Signature · 1 line
2.54.0
#41 lore raw

Re: [PATCHv4 08/13] 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] Changing the default `USDT_NOP` to an 11-byte `nop,nop10` sequence breaks `prog_tests/usdt.c`…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Changing the default USDT_NOP to an 11-byte nop,nop10 sequence breaks prog_tests/usdt.c and introduces an unsupported instruction sequence for libbpf and the kernel.
- [Low] The commit message contains an unfilled [1] TBD placeholder.
--

commit 7b65abd3a4116a7fb1cc328d784f515175461d44
Author: Jiri Olsa <[email protected]>

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

This commit updates the default USDT_NOP instruction sequence for the x86_64 architecture. It replaces the 6-byte nop,nop5 combination with an 11-byte nop,nop10 sequence to utilize nop10 optimization support.

Quoted 5 lines

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

[Severity: Low]
This isn't a bug, but there is an unfilled placeholder "[1] TBD" left in the commit message.

Quoted 4 lines
tools/testing/selftests/bpf/usdt.h +0 -0
index c71e21df38b32..75687f50f4e21 100644

[ ... ]

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

Quoted 6 lines
tools/testing/selftests/bpf/usdt.h +1 -1
@@ -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, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 /* nop, nop10 */

[Severity: Medium]
Will this 11-byte sequence break the USDT selftests?

It looks like subtest_optimized_attach() in prog_tests/usdt.c explicitly searches for the 6-byte nop1_nop5_combo and might fail the ASSERT_OK_PTR check when usdt_2.c emits this new 11-byte nop,nop10 sequence instead.

Additionally, does libbpf's has_nop_combo() or the kernel's can_optimize() in arch/x86/kernel/uprobes.c currently support optimizing this 10-byte NOP? If not, this might result in a loss of the intended optimization rather than an improvement.

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
#42 lore raw

Re: [PATCHv4 08/13] selftests/bpf: Emit nop,nop10 instructions combo for x86_64 arch

In reply to Jiri Olsa
Does this change break the USDT selftest? Multiple reviewers raised concerns across v1 through v4 that subtest_optimized_attach() in prog_tests/usdt.c searches for the old 6-byte nop1_nop5_combo…

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

Quoted 13 lines
tools/testing/selftests/bpf/usdt.h +1 -1
index c71e21df38b3..75687f50f4e2 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, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 /* nop, nop10 */
 #else
 #define USDT_NOP			nop
 #endif

Does this change break the USDT selftest? Multiple reviewers raised concerns across v1 through v4 that subtest_optimized_attach() in prog_tests/usdt.c searches for the old 6-byte nop1_nop5_combo sequence. When usdt_2.c emits the new 11-byte nop,nop10 instead, find_instr() will return NULL and fail the ASSERT_OK_PTR check.

You mentioned in v1/v2 that the test would be updated in a following commit, preferring to keep changes separated for easier review. However, this creates a git bisect regression where the test breaks at this commit. Should the test update be squashed in to avoid breaking bisectability?

The commit message contains a placeholder reference:

Quoted 7 lines

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

This isn't a bug, but the "[1] TBD" should either be filled in or removed.

---
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/26475828601

author reviewer
#43 lore raw

PATCH v4 9/13 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 | 30 +++++++++++--------
 tools/testing/selftests/bpf/prog_tests/usdt.c | 25 +++++++++-------
 tools/testing/selftests/bpf/usdt_2.c          |  2 +-
 4 files changed, 34 insertions(+), 25 deletions(-)
tools/testing/selftests/bpf/benchs/bench_trigger.c +1 -1
index 2f22ec61667b..a60b8173cdc4 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, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00");
 }
 
 static void *uprobe_producer_nop5(void *input)
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +17 -13
index c944136252c6..0868fb9793e0 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, 0x2e, 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, 0x2e, 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, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00\n" /* nop10 */
+		"ret\n"
 	);
 }
 
@@ -388,14 +388,15 @@ static int find_uprobes_trampoline(void *tramp_addr)
 	return ret;
 }
 
-static unsigned char nop5[5] = { 0x0f, 0x1f, 0x44, 0x00, 0x00 };
+static unsigned char nop10[10]  = { 0x66, 0x2e, 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 +421,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");
@@ -430,9 +432,11 @@ static void *check_attach(struct uprobe_syscall_executed *skel, trigger_t trigge
 
 static void check_detach(void *addr, void *tramp)
 {
+	static const char nop10_prefix[] = { 0x66, 0x2e, 0x0f, 0x1f, 0x84 };
+
 	/* [uprobes_trampoline] stays after detach */
 	ASSERT_OK(find_uprobes_trampoline(tramp), "uprobes_trampoline");
-	ASSERT_OK(memcmp(addr, nop5, 5), "nop5");
+	ASSERT_OK(memcmp(addr, nop10_prefix, 5), "nop10_prefix");
 }
 
 static void check(struct uprobe_syscall_executed *skel, struct bpf_link *link,
@@ -568,8 +572,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..fda3a298ccfc 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, 0x2e, 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.54.0
#44 lore raw

Re: [PATCHv4 09/13] 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] The usdt.h header was not updated to redefine the default USDT_NOP to 10 bytes, which will cause…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] The usdt.h header was not updated to redefine the default USDT_NOP to 10 bytes, which will cause the usdt test to reliably fail.
- [Medium] check_detach() only verifies the restoration of the first 5 bytes of the 10-byte NOP and uses a signed char array that can cause compiler warnings.
- [Medium] Benchmark functions and external reporting names were not updated to reflect the new 10-byte NOP behavior, producing misleading metrics. --

commit d8fb3aec4d3eab73b6231c402e78efd3cd47f8f0
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 402

Quoted 11 lines
tools/testing/selftests/bpf/benchs/bench_trigger.c +1 -1
index 2f22ec61667b9..a60b8173cdc46 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, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00");
 }

[Severity: Medium]
This isn't a bug, but the function name uprobe_target_nop5 and the associated reporting names in the benchmark registry (like uprobe-nop5) still use the nop5 suffix. Since they now use a 10-byte NOP, should these be updated to nop10 to avoid misleading benchmark metrics?

[ ... ]

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +0 -0
index 955a37751b52d..90851d1e4624f 100644

[ ... ]

tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c static void *check_attach(struct uprobe_syscall_executed *skel, trigger_t trigge · line 436

Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +2 -0
@@ -430,9 +432,11 @@ static void *check_attach(struct uprobe_syscall_executed *skel, trigger_t trigge
 
 static void check_detach(void *addr, void *tramp)
 {
+	static const char nop10_prefix[] = { 0x66, 0x2e, 0x0f, 0x1f, 0x84 };
+

[Severity: Medium]
Is char the correct type here? The value 0x84 exceeds the maximum positive value for a signed char, which can trigger compiler warnings on architectures where char is signed. Could this be changed to unsigned char instead?

Also, the optimized uprobe attachment replaces the full 10-byte NOP. Does verifying only the first 5 bytes adequately test that the detach correctly restored the original instruction stream?

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +1 -1
 	/* [uprobes_trampoline] stays after detach */
 	ASSERT_OK(find_uprobes_trampoline(tramp), "uprobes_trampoline");
-	ASSERT_OK(memcmp(addr, nop5, 5), "nop5");
+	ASSERT_OK(memcmp(addr, nop10_prefix, 5), "nop10_prefix");
 }

[ ... ]

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/usdt.c +1 -1
index 69759b27794d7..fda3a298ccfc7 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, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 };

[ ... ]

tools/testing/selftests/bpf/usdt_2.c line 8

Quoted 12 lines
tools/testing/selftests/bpf/usdt_2.c +1 -1
index 789883aaca4c3..b359b389f6c00 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"

[Severity: Medium]
It appears tools/testing/selftests/bpf/usdt.h was not updated in this patch to redefine USDT_NOP to 10 bytes for x86_64.

Since usdt_2.c includes usdt.h to build its probes, won't it still compile with the old 6-byte sequence? This would cause find_instr() in usdt.c to fail to locate nop1_nop10_combo and result in test failures.

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

Re: [PATCHv4 09/13] selftests/bpf: Change uprobe syscall tests to use nop10

Quoted 23 lines · On Tue, May 26, 2026 at 09:15:17PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] The usdt.h header was not updated to redefine the default USDT_NOP to 10 bytes, which will cause the usdt test to reliably fail.
- [Medium] check_detach() only verifies the restoration of the first 5 bytes of the 10-byte NOP and uses a signed char array that can cause compiler warnings.
- [Medium] Benchmark functions and external reporting names were not updated to reflect the new 10-byte NOP behavior, producing misleading metrics. --

commit d8fb3aec4d3eab73b6231c402e78efd3cd47f8f0
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 9 lines
tools/testing/selftests/bpf/benchs/bench_trigger.c +0 -1
index 2f22ec61667b9..a60b8173cdc46 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");
Quoted 8 lines

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

Quoted 2 lines
tools/testing/selftests/bpf/benchs/bench_trigger.c +1 -0
+	asm volatile (".byte 0x66, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00");
 }

[Severity: Medium]
This isn't a bug, but the function name uprobe_target_nop5 and the associated reporting names in the benchmark registry (like uprobe-nop5) still use the nop5 suffix. Since they now use a 10-byte NOP, should these be updated to nop10 to avoid misleading benchmark metrics?

it's changed in following patch that renames the related benchmarks
selftests/bpf: Change uprobe/usdt trigger bench code to use nop10

Quoted 10 lines

[ ... ]

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +0 -0
index 955a37751b52d..90851d1e4624f 100644

[ ... ]

Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +0 -0
@@ -430,9 +432,11 @@ static void *check_attach(struct uprobe_syscall_executed *skel, trigger_t trigge
 
Quoted 9 lines

tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c static void *check_attach(struct uprobe_syscall_executed *skel, trigger_t trigge · line 436

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +2 -0
 static void check_detach(void *addr, void *tramp)
 {
+	static const char nop10_prefix[] = { 0x66, 0x2e, 0x0f, 0x1f, 0x84 };
+

[Severity: Medium]
Is char the correct type here? The value 0x84 exceeds the maximum positive value for a signed char, which can trigger compiler warnings on architectures where char is signed. Could this be changed to unsigned char instead?

will change

Quoted 4 lines

Also, the optimized uprobe attachment replaces the full 10-byte NOP. Does verifying only the first 5 bytes adequately test that the detach correctly restored the original instruction stream?

the last 5 bytes is the call instruction that is always different

Quoted 39 lines
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +1 -1
 	/* [uprobes_trampoline] stays after detach */
 	ASSERT_OK(find_uprobes_trampoline(tramp), "uprobes_trampoline");
-	ASSERT_OK(memcmp(addr, nop5, 5), "nop5");
+	ASSERT_OK(memcmp(addr, nop10_prefix, 5), "nop10_prefix");
 }

[ ... ]

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/usdt.c +1 -1
index 69759b27794d7..fda3a298ccfc7 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, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 };

[ ... ]

tools/testing/selftests/bpf/usdt_2.c line 8

Quoted 12 lines
tools/testing/selftests/bpf/usdt_2.c +1 -1
index 789883aaca4c3..b359b389f6c00 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"

[Severity: Medium]
It appears tools/testing/selftests/bpf/usdt.h was not updated in this patch to redefine USDT_NOP to 10 bytes for x86_64.

Quoted 3 lines

Since usdt_2.c includes usdt.h to build its probes, won't it still compile with the old 6-byte sequence? This would cause find_instr() in usdt.c to fail to locate nop1_nop10_combo and result in test failures.

all those changes are in separate cpatches for easier review (as I thought..) I can squash them if that's what we want

jirka

#46 lore raw

Re: [PATCHv4 09/13] selftests/bpf: Change uprobe syscall tests to use nop10

In reply to Jiri Olsa
Is the loop in find_instr() adequate to find the updated instruction sequence? In uprobe_syscall.c, find_nop10() was updated to search up to 128 bytes to account for compiler-generated prologues:
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +0 -0
index c944136252c6..0868fb9793e0 100644

[ ... ]

Quoted 20 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +5 -4
@@ -388,14 +388,15 @@ static int find_uprobes_trampoline(void *tramp_addr)
 	return ret;
 }
 
-static unsigned char nop5[5] = { 0x0f, 0x1f, 0x44, 0x00, 0x00 };
+static unsigned char nop10[10]  = { 0x66, 0x2e, 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;

[ ... ]

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

Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/usdt.c +1 -1
index 69759b27794d..fda3a298ccfc 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, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 };
 
 static void *find_instr(void *fn, unsigned char *instr, size_t cnt)
 {

Is the loop in find_instr() adequate to find the updated instruction sequence? In uprobe_syscall.c, find_nop10() was updated to search up to 128 bytes to account for compiler-generated prologues:

tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c:find_nop10() {
	for (i = 0; i < 128; i++) {
		if (!memcmp(nop10, fn + i, 10))
			return fn + i;
	}
}

But find_instr() in usdt.c only searches the first 10 bytes:

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;
	}
}

If a modern compiler generates a prologue longer than 9 bytes for usdt_2(), find_instr() will prematurely terminate and cause subtest_optimized_attach() to fail. This concern was raised by reviewers in v2 and v3 of the patch series:

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

The author acknowledged this with "yea find_instr needs same update, will fix" but the loop bound remains 10 in the current v4 patch.

Quoted 11 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;
 
 	addr_1 = find_instr(usdt_1, nop1, 1);

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

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/usdt.c +3 -3
 	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;

[ ... ]

---
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/26475828601

#47 lore raw

Re: [PATCHv4 09/13] selftests/bpf: Change uprobe syscall tests to use nop10

On Tue, May 26, 2026 at 09:46:38PM +0000, [email protected] wrote:

SNIP

Quoted 34 lines

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

Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/usdt.c +1 -1
index 69759b27794d..fda3a298ccfc 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, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 };
 
 static void *find_instr(void *fn, unsigned char *instr, size_t cnt)
 {

Is the loop in find_instr() adequate to find the updated instruction sequence? In uprobe_syscall.c, find_nop10() was updated to search up to 128 bytes to account for compiler-generated prologues:

tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c:find_nop10() {
	for (i = 0; i < 128; i++) {
		if (!memcmp(nop10, fn + i, 10))
			return fn + i;
	}
}

But find_instr() in usdt.c only searches the first 10 bytes:

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;
	}
}
Quoted 4 lines

If a modern compiler generates a prologue longer than 9 bytes for usdt_2(), find_instr() will prematurely terminate and cause subtest_optimized_attach() to fail. This concern was raised by reviewers in v2 and v3 of the patch series:

find_instr is ok for its usage in subtest_optimized_attach, we first use it to verify the combo was not generated in usdt_1, and having 128 bytes search we'd find combo from usdt_2 function

modern compilers seems to be smart emough not to generate long prologue for usdt_2 function

jirka

Reviewed-by on v4 9/13
#48 lore raw

Re: [PATCHv4 09/13] selftests/bpf: Change uprobe syscall tests to use nop10

In reply to Jiri Olsa
Quoted 5 lines · Jiri Olsa

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

---

[...]

tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c static void *check_attach(struct uprobe_syscall_executed *skel, trigger_t trigge · line 442

Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +3 -1
@@ -430,9 +432,11 @@ static void *check_attach(struct uprobe_syscall_executed *skel, trigger_t trigge
 
 static void check_detach(void *addr, void *tramp)
 {
+	static const char nop10_prefix[] = { 0x66, 0x2e, 0x0f, 0x1f, 0x84 };
+
 	/* [uprobes_trampoline] stays after detach */
 	ASSERT_OK(find_uprobes_trampoline(tramp), "uprobes_trampoline");
-	ASSERT_OK(memcmp(addr, nop5, 5), "nop5");
+	ASSERT_OK(memcmp(addr, nop10_prefix, 5), "nop10_prefix");
 }
 
 static void check(struct uprobe_syscall_executed *skel, struct bpf_link *link,

Nit: You could just do memcmp(addr, nop10, 5) to match the prefix.

author reviewer
#49 lore raw

PATCH v4 10/13 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 run_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 a60b8173cdc4..61513efc167a 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, 0x2e, 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.54.0
author reviewer
#51 lore raw

PATCH v4 11/13 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.

---
 .../selftests/bpf/prog_tests/uprobe_syscall.c | 114 ++++++++++++++++--
 1 file changed, 104 insertions(+), 10 deletions(-)
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +104 -10
index 0868fb9793e0..969f4deba9fd 100644
@@ -430,23 +430,28 @@ 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)
 {
 	static const char nop10_prefix[] = { 0x66, 0x2e, 0x0f, 0x1f, 0x84 };
+	bool ok = true;
 
 	/* [uprobes_trampoline] stays after detach */
-	ASSERT_OK(find_uprobes_trampoline(tramp), "uprobes_trampoline");
-	ASSERT_OK(memcmp(addr, nop10_prefix, 5), "nop10_prefix");
+	if (!ASSERT_OK(find_uprobes_trampoline(tramp), "uprobes_trampoline"))
+		ok = false;
+	if (!ASSERT_OK(memcmp(addr, nop10_prefix, 5), "nop10_prefix"))
+		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)
@@ -457,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"))
@@ -474,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;
@@ -496,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"))
@@ -516,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;
@@ -540,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"))
@@ -559,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);
@@ -569,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);
@@ -588,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.54.0
#52 lore raw

Re: [PATCHv4 11/13] selftests/bpf: Add reattach tests for uprobe syscall

Quoted 21 lines · Jiri Olsa

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

---
 .../selftests/bpf/prog_tests/uprobe_syscall.c | 114 ++++++++++++++++--
 1 file changed, 104 insertions(+), 10 deletions(-)
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +2 -1
index 0868fb9793e0..969f4deba9fd 100644
@@ -430,23 +430,28 @@ 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)
 {
 	static const char nop10_prefix[] = { 0x66, 0x2e, 0x0f, 0x1f, 0x84 };
+	bool ok = true;

tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c static void *check_attach(struct uprobe_syscall_executed *skel, trigger_t trigge · line 444

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +5 -2
 
 	/* [uprobes_trampoline] stays after detach */
-	ASSERT_OK(find_uprobes_trampoline(tramp), "uprobes_trampoline");
-	ASSERT_OK(memcmp(addr, nop10_prefix, 5), "nop10_prefix");
+	if (!ASSERT_OK(find_uprobes_trampoline(tramp), "uprobes_trampoline"))
+		ok = false;
+	if (!ASSERT_OK(memcmp(addr, nop10_prefix, 5), "nop10_prefix"))
+		ok = false;
+	return ok;
 }

Nit: Maybe apply the same pattern you used in
progs/get_func_args_test.c?

ok &= ASSERT_OK(...)
ok &= ASSERT_OK(...)

tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c static void *check_attach(struct uprobe_syscall_executed *skel, trigger_t trigge · line 447

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +2 -2
-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)

Nit: Kinda wish that was called check_attach_detach().

Quoted 21 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +2 -0
 {
 	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)
@@ -457,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"))
@@ -474,7 +480,28 @@ static void test_uprobe_legacy(void)
 	if (!ASSERT_OK_PTR(link, "bpf_program__attach_uprobe_opts"))
 		goto cleanup;

tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c static void test_uprobe_legacy(void) · line 490

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +8 -1
 
-	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;
+

In theory we're missing a check here that an unoptimized uprobe was installed. If nothing happened at all between the last check() and check_destroy() below, the test would still pass.

Applies to the three similar changes after that one as well.

Quoted 17 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +14 -0
+	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;

[...]

#53 lore raw

Re: [PATCHv4 11/13] selftests/bpf: Add reattach tests for uprobe syscall

Quoted 36 lines · Jakub Sitnicki
Quoted 21 lines · Jiri Olsa

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

---
 .../selftests/bpf/prog_tests/uprobe_syscall.c | 114 ++++++++++++++++--
 1 file changed, 104 insertions(+), 10 deletions(-)
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +2 -1
index 0868fb9793e0..969f4deba9fd 100644
@@ -430,23 +430,28 @@ 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)
 {
 	static const char nop10_prefix[] = { 0x66, 0x2e, 0x0f, 0x1f, 0x84 };
+	bool ok = true;

tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c static void *check_attach(struct uprobe_syscall_executed *skel, trigger_t trigge · line 444

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +5 -2
 
 	/* [uprobes_trampoline] stays after detach */
-	ASSERT_OK(find_uprobes_trampoline(tramp), "uprobes_trampoline");
-	ASSERT_OK(memcmp(addr, nop10_prefix, 5), "nop10_prefix");
+	if (!ASSERT_OK(find_uprobes_trampoline(tramp), "uprobes_trampoline"))
+		ok = false;
+	if (!ASSERT_OK(memcmp(addr, nop10_prefix, 5), "nop10_prefix"))
+		ok = false;
+	return ok;
 }

Nit: Maybe apply the same pattern you used in
progs/get_func_args_test.c?

Quoted 2 lines

ok &= ASSERT_OK(...)
ok &= ASSERT_OK(...)

ok, safes space ;-)

Quoted 8 lines

tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c static void *check_attach(struct uprobe_syscall_executed *skel, trigger_t trigge · line 447

Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +2 -2
-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)

Nit: Kinda wish that was called check_attach_detach().

would need to check the existing code.. let's keep it

Quoted 37 lines
Quoted 22 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +2 -0
 {
 	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)
@@ -457,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"))
@@ -474,7 +480,28 @@ static void test_uprobe_legacy(void)
 	if (!ASSERT_OK_PTR(link, "bpf_program__attach_uprobe_opts"))
 		goto cleanup;

tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c static void test_uprobe_legacy(void) · line 490

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +8 -1
 
-	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;
+

In theory we're missing a check here that an unoptimized uprobe was installed. If nothing happened at all between the last check() and check_destroy() below, the test would still pass.

Quoted 1 line

Applies to the three similar changes after that one as well.

right, we can add extra check that there's int3 in here (and below)

thanks,
jirka

author reviewer
#54 lore raw

PATCH v4 12/13 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 969f4deba9fd..efff0c515184 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, 0x2e, 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.54.0
#55 lore raw

Re: [PATCHv4 12/13] selftests/bpf: Add tests for uprobe nop10 red zone clobbering

Does this loop execute too quickly to guarantee the uprobe optimization has completed?
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +0 -0
index 969f4deba9fd..efff0c515184 100644

[ ... ]

Quoted 18 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +14 -0
@@ -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"))

tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c static void test_uprobe_race(void) · line 923

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +10 -0
+		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");

Does this loop execute too quickly to guarantee the uprobe optimization has completed?

Uprobe optimization is performed asynchronously by a worker thread. Since this is a tight loop with no delays or blocking, the 10 iterations might finish before the background worker patches the code.

If all hits execute on the unoptimized INT3 path, the test could falsely pass on kernels where the CALL-based optimization bug is present.

(This concern was raised by [email protected] in v1 and remains unaddressed:
https://lore.kernel.org/bpf/[email protected]/)

Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +6 -0
+
+	bpf_link__destroy(link);
+
+cleanup:
+	uprobe_syscall_executed__destroy(skel);
+}

[ ... ]

Quoted 10 lines
tools/testing/selftests/bpf/usdt_2.c +2 -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;

tools/testing/selftests/bpf/usdt_2.c void usdt_2(void) · line 27

Quoted 10 lines
tools/testing/selftests/bpf/usdt_2.c +10 -0
+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);
+}

Is this test reliable across different compiler versions and optimizations?

The USDT macro uses the "nor" inline assembly constraint, which allows the compiler to allocate these operands to CPU registers.

Because there is no register pressure in usdt_red_zone_trigger(), modern compilers might allocate a1, a2, and a3 directly to registers rather than the stack's red zone.

If the operands are placed in registers, the uprobe CALL optimization clobbering [rsp-8] will not corrupt them. This could cause the test to pass even on buggy kernels, creating a false positive.

Would it be safer to explicitly force these operands into the red zone using inline assembly constraints, rather than depending on the compiler's register allocator?

(This concern was raised by [email protected] in v1 and v3 and remains unaddressed:
https://lore.kernel.org/bpf/e91d31e0c1378fe970db6385c7af419e97af0c2acd37b3b8775a2fac02ea2361@mail.kernel.org/)

---
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/26475828601

#56 lore raw

Re: [PATCHv4 12/13] selftests/bpf: Add tests for uprobe nop10 red zone clobbering

Quoted 39 lines · On Tue, May 26, 2026 at 09:46:42PM +0000, [email protected] wrote:
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +0 -0
index 969f4deba9fd..efff0c515184 100644

[ ... ]

Quoted 18 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +14 -0
@@ -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"))

tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c static void test_uprobe_race(void) · line 923

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +10 -0
+		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");

Does this loop execute too quickly to guarantee the uprobe optimization has completed?

Quoted 3 lines

Uprobe optimization is performed asynchronously by a worker thread. Since this is a tight loop with no delays or blocking, the 10 iterations might finish before the background worker patches the code.

uprobe is optimized synchronously whithin its first hit

Quoted 47 lines

If all hits execute on the unoptimized INT3 path, the test could falsely pass on kernels where the CALL-based optimization bug is present.

(This concern was raised by [email protected] in v1 and remains unaddressed:
https://lore.kernel.org/bpf/[email protected]/)

Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +6 -0
+
+	bpf_link__destroy(link);
+
+cleanup:
+	uprobe_syscall_executed__destroy(skel);
+}

[ ... ]

Quoted 10 lines
tools/testing/selftests/bpf/usdt_2.c +2 -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;

tools/testing/selftests/bpf/usdt_2.c void usdt_2(void) · line 27

Quoted 10 lines
tools/testing/selftests/bpf/usdt_2.c +10 -0
+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);
+}

Is this test reliable across different compiler versions and optimizations?

The USDT macro uses the "nor" inline assembly constraint, which allows the compiler to allocate these operands to CPU registers.

Because there is no register pressure in usdt_red_zone_trigger(), modern compilers might allocate a1, a2, and a3 directly to registers rather than the stack's red zone.

Quoted 7 lines

If the operands are placed in registers, the uprobe CALL optimization clobbering [rsp-8] will not corrupt them. This could cause the test to pass even on buggy kernels, creating a false positive.

Would it be safer to explicitly force these operands into the red zone using inline assembly constraints, rather than depending on the compiler's register allocator?

I think it's ok the way it is, usdt_red_zone_trigger uses redzone on x86_64 build:

0000000000830d02 <usdt_red_zone_trigger>:

  830d02:       55                      push   %rbp
  830d03:       48 89 e5                mov    %rsp,%rbp
  830d06:       48 8b 05 6b f3 44 03    mov    0x344f36b(%rip),%rax        # 3c80078 <usdt_red_zone_arg1>
  830d0d:       48 89 45 f8             mov    %rax,-0x8(%rbp)
  830d11:       48 8b 05 68 f3 44 03    mov    0x344f368(%rip),%rax        # 3c80080 <usdt_red_zone_arg2>
  830d18:       48 89 45 f0             mov    %rax,-0x10(%rbp)
  830d1c:       48 8b 05 65 f3 44 03    mov    0x344f365(%rip),%rax        # 3c80088 <usdt_red_zone_arg3>
  830d23:       48 89 45 e8             mov    %rax,-0x18(%rbp)

...

jirka

Reviewed-by on v4 12/13
#57 lore raw

Re: [PATCHv4 12/13] selftests/bpf: Add tests for uprobe nop10 red zone clobbering

In reply to Jiri Olsa
Quoted 37 lines · 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 +0 -0
index 969f4deba9fd..efff0c515184 100644

[...]

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +0 -0
@@ -855,6 +897,37 @@ static void test_uprobe_race(void)
 #define __NR_uprobe 336
 #endif
 

tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c static void test_uprobe_race(void) · line 909

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +10 -0
+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"))

Nit: ASSERT_OK_PTR would have worked as well. Dealer's choice.

Quoted 24 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +21 -0
+		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);

[...]

author reviewer
#58 lore raw

PATCH v4 13/13 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 efff0c515184..033d32b4cc27 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, 0x2e, 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.54.0
Reviewed-by on v4 13/13
#59 lore raw

Re: [PATCHv4 13/13] selftests/bpf: Add tests for forked/cloned optimized uprobes

Quoted 26 lines · 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 +2 -0
index efff0c515184..033d32b4cc27 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");
 }
 

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

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +10 -0
+__attribute__((aligned(16)))
+__nocf_check __weak __naked void uprobe_fork_test(void)
+{
+	asm volatile (
+		".byte 0x66, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00\n" /* nop10 */
+		"ret\n"
+	);
+}
+
+static int child_func(void *arg)

Nit: Could annotate with noreturn:

#include <stdnoreturn.h>

/* ... */

static noreturn int child_func(void *arg)

Quoted 17 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +17 -0
+{
+	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);
+}

[...]

#60 lore raw

Re: [PATCHv4 13/13] selftests/bpf: Add tests for forked/cloned optimized uprobes

Quoted 40 lines · Jakub Sitnicki
Quoted 26 lines · 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 +2 -0
index efff0c515184..033d32b4cc27 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");
 }
 

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

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +10 -0
+__attribute__((aligned(16)))
+__nocf_check __weak __naked void uprobe_fork_test(void)
+{
+	asm volatile (
+		".byte 0x66, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00\n" /* nop10 */
+		"ret\n"
+	);
+}
+
+static int child_func(void *arg)

Nit: Could annotate with noreturn:

Quoted 5 lines

#include <stdnoreturn.h>

/* ... */

static noreturn int child_func(void *arg)

yep, will change, thanks

jirka

Quoted 22 lines
Quoted 18 lines
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +17 -0
+{
+	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);
+}

[...]

#61 lore raw
In reply to Jiri Olsa
Quoted 20 lines · Jiri Olsa

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: https://lore.kernel.org/bpf/[email protected]/
v3: https://lore.kernel.org/bpf/[email protected]/

Quoted 8 lines

v4 changes:
- do not use 2nd int3 (ont +5 offset) because the call instruction is allways the same for the given nop10 address [Andrii/Peter]
- unmap unused trampoline vma after unsuccesfull optimization [sashiko]
- small change to patch#2 moved user_64bit_mode earlier in the path and pass/use mm_struct pointer directly from arch_uprobe_optimize instead of gettting current->mm
Andrii, keeping your ack, please shout otherwise

hi,
I think bots did not find anything substantial, I have just small selftests changes queued for v5

any other feedback/review would be great

thanks,
jirka

Quoted 54 lines

v3 changes:
- use nop10 update suggested by Peter in [2]
- remove struct uprobe_trampoline object, use vma objects directly instead
- selftests fixes [sashiko]
- ack from Andrii

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]/
[2] https://lore.kernel.org/bpf/[email protected]/#t
---
Andrii Nakryiko (1):
selftests/bpf: Add tests for uprobe nop10 red zone clobbering

Jiri Olsa (12):
uprobes/x86: Use proper mm_struct in __in_uprobe_trampoline uprobes/x86: Remove struct uprobe_trampoline object uprobes/x86: Allow to copy uprobe trampolines on fork uprobes/x86: Unmap trampoline vma object in case it's unused 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                               | 379 +++++++++++++++++++++++++++++++++++++++++++-----------------------------
 include/linux/uprobes.h                                 |   5 -
 kernel/events/uprobes.c                                 |  10 --
 kernel/fork.c                                           |   1 -
 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 ++-
 14 files changed, 653 insertions(+), 245 deletions(-)
#62 lore raw
Quoted 31 lines · Jiri Olsa
Quoted 20 lines · Jiri Olsa

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: https://lore.kernel.org/bpf/[email protected]/
v3: https://lore.kernel.org/bpf/[email protected]/

Quoted 8 lines

v4 changes:
- do not use 2nd int3 (ont +5 offset) because the call instruction is allways the same for the given nop10 address [Andrii/Peter]
- unmap unused trampoline vma after unsuccesfull optimization [sashiko]
- small change to patch#2 moved user_64bit_mode earlier in the path and pass/use mm_struct pointer directly from arch_uprobe_optimize instead of gettting current->mm
Andrii, keeping your ack, please shout otherwise

Quoted 6 lines

hi,
I think bots did not find anything substantial, I have just small selftests changes queued for v5

any other feedback/review would be great

one small nit on only, otherwise LGTM.

Peter, Masami, Ingo, should this go through tip tree or should we route this through bpf-next tree? I think we are fine either way, but might be more convenient to route through bpf-next given libbpf and BPF selftest changes.

If so, I'd appreciate another look at first 5 patches by Peter, if that's ok. Thanks!

Quoted 58 lines

thanks,
jirka

Quoted 54 lines

v3 changes:
- use nop10 update suggested by Peter in [2]
- remove struct uprobe_trampoline object, use vma objects directly instead
- selftests fixes [sashiko]
- ack from Andrii

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]/
[2] https://lore.kernel.org/bpf/[email protected]/#t
---
Andrii Nakryiko (1):
selftests/bpf: Add tests for uprobe nop10 red zone clobbering

Jiri Olsa (12):
uprobes/x86: Use proper mm_struct in __in_uprobe_trampoline uprobes/x86: Remove struct uprobe_trampoline object uprobes/x86: Allow to copy uprobe trampolines on fork uprobes/x86: Unmap trampoline vma object in case it's unused 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                               | 379 +++++++++++++++++++++++++++++++++++++++++++-----------------------------
 include/linux/uprobes.h                                 |   5 -
 kernel/events/uprobes.c                                 |  10 --
 kernel/fork.c                                           |   1 -
 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 ++-
 14 files changed, 653 insertions(+), 245 deletions(-)
#63 lore raw
Quoted 40 lines · Andrii Nakryiko
Quoted 31 lines · Jiri Olsa
Quoted 20 lines · Jiri Olsa

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: https://lore.kernel.org/bpf/[email protected]/
v3: https://lore.kernel.org/bpf/[email protected]/

Quoted 8 lines

v4 changes:
- do not use 2nd int3 (ont +5 offset) because the call instruction is allways the same for the given nop10 address [Andrii/Peter]
- unmap unused trampoline vma after unsuccesfull optimization [sashiko]
- small change to patch#2 moved user_64bit_mode earlier in the path and pass/use mm_struct pointer directly from arch_uprobe_optimize instead of gettting current->mm
Andrii, keeping your ack, please shout otherwise

Quoted 6 lines

hi,
I think bots did not find anything substantial, I have just small selftests changes queued for v5

any other feedback/review would be great

Quoted 7 lines

one small nit on only, otherwise LGTM.

Peter, Masami, Ingo, should this go through tip tree or should we route this through bpf-next tree? I think we are fine either way, but might be more convenient to route through bpf-next given libbpf and BPF selftest changes.

I'll assume that no one has any objections to route this through bpf-next. We got reviews from Oleg, so that's great. Jiri, seems like you will do small adjustments and send v5, please do, and then unless meanwhile no one raises any issues, this will go through bpf-next. Thanks!

Quoted 63 lines

If so, I'd appreciate another look at first 5 patches by Peter, if that's ok. Thanks!

Quoted 58 lines

thanks,
jirka

Quoted 54 lines

v3 changes:
- use nop10 update suggested by Peter in [2]
- remove struct uprobe_trampoline object, use vma objects directly instead
- selftests fixes [sashiko]
- ack from Andrii

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]/
[2] https://lore.kernel.org/bpf/[email protected]/#t
---
Andrii Nakryiko (1):
selftests/bpf: Add tests for uprobe nop10 red zone clobbering

Jiri Olsa (12):
uprobes/x86: Use proper mm_struct in __in_uprobe_trampoline uprobes/x86: Remove struct uprobe_trampoline object uprobes/x86: Allow to copy uprobe trampolines on fork uprobes/x86: Unmap trampoline vma object in case it's unused 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                               | 379 +++++++++++++++++++++++++++++++++++++++++++-----------------------------
 include/linux/uprobes.h                                 |   5 -
 kernel/events/uprobes.c                                 |  10 --
 kernel/fork.c                                           |   1 -
 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 ++-
 14 files changed, 653 insertions(+), 245 deletions(-)
maintainer
#64 lore raw
Quoted 50 lines · Andrii Nakryiko
Quoted 40 lines · Andrii Nakryiko
Quoted 31 lines · Jiri Olsa
Quoted 20 lines · Jiri Olsa

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: https://lore.kernel.org/bpf/[email protected]/
v3: https://lore.kernel.org/bpf/[email protected]/

Quoted 8 lines

v4 changes:
- do not use 2nd int3 (ont +5 offset) because the call instruction is allways the same for the given nop10 address [Andrii/Peter]
- unmap unused trampoline vma after unsuccesfull optimization [sashiko]
- small change to patch#2 moved user_64bit_mode earlier in the path and pass/use mm_struct pointer directly from arch_uprobe_optimize instead of gettting current->mm
Andrii, keeping your ack, please shout otherwise

Quoted 6 lines

hi,
I think bots did not find anything substantial, I have just small selftests changes queued for v5

any other feedback/review would be great

Quoted 7 lines

one small nit on only, otherwise LGTM.

Peter, Masami, Ingo, should this go through tip tree or should we route this through bpf-next tree? I think we are fine either way, but might be more convenient to route through bpf-next given libbpf and BPF selftest changes.

Quoted 5 lines

I'll assume that no one has any objections to route this through bpf-next. We got reviews from Oleg, so that's great. Jiri, seems like you will do small adjustments and send v5, please do, and then unless meanwhile no one raises any issues, this will go through bpf-next. Thanks!

Is it already applied? If not, I can stick it in tip/perf/core (and the first in /urgent).

maintainer
#65 lore raw
Quoted 8 lines · Peter Zijlstra
Quoted 5 lines

I'll assume that no one has any objections to route this through bpf-next. We got reviews from Oleg, so that's great. Jiri, seems like you will do small adjustments and send v5, please do, and then unless meanwhile no one raises any issues, this will go through bpf-next. Thanks!

Is it already applied? If not, I can stick it in tip/perf/core (and the first in /urgent).

Oh, I see v5. I'll go grab that then.

#66 lore raw
In reply to Jiri Olsa

hi, ping, thanks

jirka

Quoted 94 lines · Jiri Olsa
Quoted 20 lines · Jiri Olsa

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: https://lore.kernel.org/bpf/[email protected]/
v3: https://lore.kernel.org/bpf/[email protected]/

Quoted 8 lines

v4 changes:
- do not use 2nd int3 (ont +5 offset) because the call instruction is allways the same for the given nop10 address [Andrii/Peter]
- unmap unused trampoline vma after unsuccesfull optimization [sashiko]
- small change to patch#2 moved user_64bit_mode earlier in the path and pass/use mm_struct pointer directly from arch_uprobe_optimize instead of gettting current->mm
Andrii, keeping your ack, please shout otherwise

hi,
I think bots did not find anything substantial, I have just small selftests changes queued for v5

any other feedback/review would be great

thanks,
jirka

Quoted 54 lines

v3 changes:
- use nop10 update suggested by Peter in [2]
- remove struct uprobe_trampoline object, use vma objects directly instead
- selftests fixes [sashiko]
- ack from Andrii

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]/
[2] https://lore.kernel.org/bpf/[email protected]/#t
---
Andrii Nakryiko (1):
selftests/bpf: Add tests for uprobe nop10 red zone clobbering

Jiri Olsa (12):
uprobes/x86: Use proper mm_struct in __in_uprobe_trampoline uprobes/x86: Remove struct uprobe_trampoline object uprobes/x86: Allow to copy uprobe trampolines on fork uprobes/x86: Unmap trampoline vma object in case it's unused 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                               | 379 +++++++++++++++++++++++++++++++++++++++++++-----------------------------
 include/linux/uprobes.h                                 |   5 -
 kernel/events/uprobes.c                                 |  10 --
 kernel/fork.c                                           |   1 -
 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 ++-
 14 files changed, 653 insertions(+), 245 deletions(-)