Skip to contentSkip to conversation

uprobe/x86: Add support to optimize prologue

RFC PATCH In review Jiri Olsa 5 participants 16 messages Nov 17, 2025 – Dec 8, 2025

Subsystems (7)
x86 Kselftest BPF perf events +3 more subsystems
lore
Share & tools
lore

Jiri Olsa

author reviewer
#0 lore raw

cover uprobe/x86: Add support to optimize prologue

hi,
the subject is bit too optimistic, in nutshell the idea is to allow optimization on top of emulated instructions and then add support to emulate more instructions with high presence in function prologues.

This patchset adds support to optimize uprobe on top of instruction that could be emulated and also adds support to emulate particular versions of mov and sub instructions to cover some of the user space functions prologues, like:

pushq %rbp
movq %rsp,%rbp
subq $0xb0,%rsp

The idea is to store instructions on underlying 5 bytes and emulate them during the int3 and uprobe syscall execution:

- install 'call trampoline' through standard int3 update
- if int3 is hit before we finish optimizing we emulate all underlying instructions
- when call is installed the uprobe syscall will emulate all underlying instructions

There's an additional issue that single instruction replacement does not have and it's the possibility of the user space code to jump in the middle of those 5 bytes. I think it's unlikely to happen at the function prologue, but uprobe could be placed anywhere. I'm not sure how to mitigate this other than having some enable/disable switch or config option, which is unfortunate.

The patchset is based on bpf-next/master with [1] changes merged in.

thanks,
jirka

[1] https://lore.kernel.org/lkml/[email protected]/T/#m95a3208943ec24c5eee17ad6113002fdc6776cf8
---
Jiri Olsa (8):
uprobe/x86: Introduce struct arch_uprobe_xol object
uprobe/x86: Use struct arch_uprobe_xol in emulate callback
uprobe/x86: Add support to emulate mov reg,reg instructions
uprobe/x86: Add support to emulate sub imm,reg instructions
uprobe/x86: Add support to optimize on top of emulated instructions
selftests/bpf: Add test for mov and sub emulation
selftests/bpf: Add test for uprobe prologue optimization
selftests/bpf: Add race test for uprobe proglog optimization

 arch/x86/include/asm/uprobes.h                          |  35 +++++++---
 arch/x86/kernel/uprobes.c                               | 336 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------
 include/linux/uprobes.h                                 |   1 +
 kernel/events/uprobes.c                                 |   6 ++
 tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c | 129 ++++++++++++++++++++++++++++++++-----
 5 files changed, 434 insertions(+), 73 deletions(-)
author reviewer
#1 lore raw

RFC PATCH 1/8 uprobe/x86: Introduce struct arch_uprobe_xol object

Mov xol data into separate arch_uprobe_xol object so we can hold more of them in following changes.

---
 arch/x86/include/asm/uprobes.h | 16 ++++---
 arch/x86/kernel/uprobes.c      | 78 +++++++++++++++++-----------------
 2 files changed, 49 insertions(+), 45 deletions(-)
arch/x86/include/asm/uprobes.h +10 -6
index 1ee2e5115955..819e35aa61c4 100644
@@ -27,12 +27,7 @@ enum {
 
 struct uprobe_xol_ops;
 
-struct arch_uprobe {
-	union {
-		u8			insn[MAX_UINSN_BYTES];
-		u8			ixol[MAX_UINSN_BYTES];
-	};
-
+struct arch_uprobe_xol {
 	const struct uprobe_xol_ops	*ops;
 
 	union {
@@ -50,6 +45,15 @@ struct arch_uprobe {
 			u8	ilen;
 		}			push;
 	};
+};
+
+struct arch_uprobe {
+	union {
+		u8			insn[MAX_UINSN_BYTES];
+		u8			ixol[MAX_UINSN_BYTES];
+	};
+
+	struct arch_uprobe_xol		xol;
 
 	unsigned long flags;
 };
arch/x86/kernel/uprobes.c +39 -39

210 lines not on this page · raw message

Signature · 1 line
2.51.1
author reviewer
#2 lore raw

RFC PATCH 2/8 uprobe/x86: Use struct arch_uprobe_xol in emulate callback

In reply to Jiri Olsa

Using struct arch_uprobe_xol also in emulate callback which will help in following changes.

---
 arch/x86/kernel/uprobes.c | 32 +++++++++++++++++---------------
 1 file changed, 17 insertions(+), 15 deletions(-)
arch/x86/kernel/uprobes.c +17 -15
index fb9457b29dbc..7d7a5e677472 100644
@@ -1212,7 +1212,7 @@ static bool can_optimize(struct insn *insn, unsigned long vaddr)
 #endif /* CONFIG_X86_64 */
 
 struct uprobe_xol_ops {
-	bool	(*emulate)(struct arch_uprobe *, struct pt_regs *);
+	bool	(*emulate)(struct arch_uprobe*, struct arch_uprobe_xol *, struct pt_regs *);
 	int	(*pre_xol)(struct arch_uprobe *, struct pt_regs *);
 	int	(*post_xol)(struct arch_uprobe *, struct pt_regs *);
 	void	(*abort)(struct arch_uprobe *, struct pt_regs *);
@@ -1291,9 +1291,9 @@ static const struct uprobe_xol_ops default_xol_ops = {
 	.abort	  = default_abort_op,
 };
 
-static bool branch_is_call(struct arch_uprobe *auprobe)
+static bool branch_is_call(struct arch_uprobe_xol *xol)
 {
-	return auprobe->xol.branch.opc1 == 0xe8;
+	return xol->branch.opc1 == 0xe8;
 }
 
 #define CASE_COND					\
@@ -1325,11 +1325,11 @@ static bool is_cond_jmp_opcode(u8 opcode)
 	}
 }
 
-static bool check_jmp_cond(struct arch_uprobe *auprobe, struct pt_regs *regs)
+static bool check_jmp_cond(struct arch_uprobe_xol *xol, struct pt_regs *regs)
 {
 	unsigned long flags = regs->flags;
 
-	switch (auprobe->xol.branch.opc1) {
+	switch (xol->branch.opc1) {
 	#define DO(expr)	\
 		return expr;
 	CASE_COND
@@ -1344,12 +1344,13 @@ static bool check_jmp_cond(struct arch_uprobe *auprobe, struct pt_regs *regs)
 #undef	COND
 #undef	CASE_COND
 
-static bool branch_emulate_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
+static bool branch_emulate_op(struct arch_uprobe *auprobe, struct arch_uprobe_xol *xol,
+			      struct pt_regs *regs)
 {
-	unsigned long new_ip = regs->ip += auprobe->xol.branch.ilen;
-	unsigned long offs = (long)auprobe->xol.branch.offs;
+	unsigned long new_ip = regs->ip += xol->branch.ilen;
+	unsigned long offs = (long)xol->branch.offs;
 
-	if (branch_is_call(auprobe)) {
+	if (branch_is_call(xol)) {
 		/*
 		 * If it fails we execute this (mangled, see the comment in
 		 * branch_clear_offset) insn out-of-line. In the likely case
@@ -1361,7 +1362,7 @@ static bool branch_emulate_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
 		 */
 		if (emulate_push_stack(regs, new_ip))
 			return false;
-	} else if (!check_jmp_cond(auprobe, regs)) {
+	} else if (!check_jmp_cond(xol, regs)) {
 		offs = 0;
 	}
 
@@ -1369,19 +1370,20 @@ static bool branch_emulate_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
 	return true;
 }
 
-static bool push_emulate_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
+static bool push_emulate_op(struct arch_uprobe *auprobe, struct arch_uprobe_xol *xol,
+			    struct pt_regs *regs)
 {
-	unsigned long *src_ptr = (void *)regs + auprobe->xol.push.reg_offset;
+	unsigned long *src_ptr = (void *)regs + xol->push.reg_offset;
 
 	if (emulate_push_stack(regs, *src_ptr))
 		return false;
-	regs->ip += auprobe->xol.push.ilen;
+	regs->ip += xol->push.ilen;
 	return true;
 }
 
 static int branch_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
 {
-	BUG_ON(!branch_is_call(auprobe));
+	BUG_ON(!branch_is_call(&auprobe->xol));
 	/*
 	 * We can only get here if branch_emulate_op() failed to push the ret
 	 * address _and_ another thread expanded our stack before the (mangled)
@@ -1767,7 +1769,7 @@ void arch_uprobe_abort_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
 static bool __skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
 {
 	if (auprobe->xol.ops->emulate)
-		return auprobe->xol.ops->emulate(auprobe, regs);
+		return auprobe->xol.ops->emulate(auprobe, &auprobe->xol, regs);
 	return false;
 }
 
Signature · 1 line
2.51.1
author reviewer
#3 lore raw

RFC PATCH 3/8 uprobe/x86: Add support to emulate mov reg,reg instructions

In reply to Jiri Olsa

Adding support to emulate mov reg to reg instructions, because it's often part of the function prologue.

---
 arch/x86/include/asm/uprobes.h |  5 +++
 arch/x86/kernel/uprobes.c      | 61 ++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+)
arch/x86/include/asm/uprobes.h +5 -0
index 819e35aa61c4..e6fd87a1cbc3 100644
@@ -44,6 +44,11 @@ struct arch_uprobe_xol {
 			u8	reg_offset;	/* to the start of pt_regs */
 			u8	ilen;
 		}			push;
+		struct {
+			u16	src;	/* to the start of pt_regs */
+			u16	dst;    /* to the start of pt_regs */
+			u8	ilen;
+		}			mov;
 	};
 };
 
arch/x86/kernel/uprobes.c +61 -0

91 lines not on this page · raw message

Signature · 1 line
2.51.1
#4 lore raw

Re: [RFC PATCH 3/8] uprobe/x86: Add support to emulate mov reg,reg instructions

kernel test robot noticed the following build errors:

Hi Jiri,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:

[auto build test ERROR on tip/x86/core]
[also build test ERROR on bpf-next/net bpf-next/master bpf/master linus/master v6.18-rc6 next-20251119] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Jiri-Olsa/uprobe-x86-Introduce-struct-arch_uprobe_xol-object/20251117-205911
base:   tip/x86/core
patch link:    https://lore.kernel.org/r/20251117124057.687384-4-jolsa%40kernel.org
patch subject: [RFC PATCH 3/8] uprobe/x86: Add support to emulate mov reg,reg instructions
config: x86_64-buildonly-randconfig-6003-20251120 (https://download.01.org/0day-ci/archive/20251120/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251120/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> arch/x86/kernel/uprobes.c:1600:39: error: too few arguments to function call, expected 2, have 1
    1600 |         off_src = insn_get_modrm_reg_off(insn);
         |                   ~~~~~~~~~~~~~~~~~~~~~~     ^
   arch/x86/include/asm/insn-eval.h:23:5: note: 'insn_get_modrm_reg_off' declared here
      23 | int insn_get_modrm_reg_off(struct insn *insn, struct pt_regs *regs);
         |     ^                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   arch/x86/kernel/uprobes.c:1603:38: error: too few arguments to function call, expected 2, have 1
    1603 |         off_dst = insn_get_modrm_rm_off(insn);
         |                   ~~~~~~~~~~~~~~~~~~~~~     ^
   arch/x86/include/asm/insn-eval.h:22:5: note: 'insn_get_modrm_rm_off' declared here
      22 | int insn_get_modrm_rm_off(struct insn *insn, struct pt_regs *regs);
         |     ^                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

2 errors generated.

vim +1600 arch/x86/kernel/uprobes.c

1580
1581 #ifdef CONFIG_X86_64
1582 /* Returns -ENOSYS if mov_xol_ops doesn't handle this insn */
1583 static int mov_setup_xol_ops(struct arch_uprobe_xol *xol, struct insn *insn)
1584 {
1585 u8 opc1 = OPCODE1(insn);
1586 int off_src, off_dst;
1587
1588 /* validate opcode */
1589 if (opc1 != 0x89)
1590 return -ENOSYS;
1591 if (insn->rex_prefix.nbytes != 1 ||
1592 insn->rex_prefix.bytes[0] != 0x48)
1593 return -ENOSYS;
1594
1595 /* only register operands */
1596 if (X86_MODRM_MOD(insn->modrm.value) != 3)
1597 return -ENOSYS;
1598
1599 /* get registers offset */

> 1600		off_src = insn_get_modrm_reg_off(insn);

1601 if (off_src < 0)
1602 return off_src;
1603 off_dst = insn_get_modrm_rm_off(insn);
1604 if (off_dst < 0)
1605 return off_dst;
1606
1607 xol->mov.src = off_src;
1608 xol->mov.dst = off_dst;
1609 xol->mov.ilen = insn->length;
1610 xol->ops = &mov_xol_ops;
1611 return 0;
1612 }
1613 #else
1614 static int mov_setup_xol_ops(struct arch_uprobe_xol *xol, struct insn *insn)
1615 {
1616 return -ENOSYS;
1617 }
1618 #endif
1619

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author reviewer
#5 lore raw

RFC PATCH 4/8 uprobe/x86: Add support to emulate sub imm,reg instructions

In reply to Jiri Olsa

Adding support to emulate sub reg, imm instructions, because it's often part of the function prologue.

---
 arch/x86/include/asm/uprobes.h |  5 +++
 arch/x86/kernel/uprobes.c      | 73 ++++++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+)
arch/x86/include/asm/uprobes.h +5 -0
index e6fd87a1cbc3..e09aab82b8c1 100644
@@ -49,6 +49,11 @@ struct arch_uprobe_xol {
 			u16	dst;    /* to the start of pt_regs */
 			u8	ilen;
 		}			mov;
+		struct {
+			s32	val;
+			u16	reg;	/* to the start of pt_regs */
+			u8	ilen;
+		}			sub;
 	};
 };
 
arch/x86/kernel/uprobes.c +73 -0

102 lines not on this page · raw message

Signature · 1 line
2.51.1
author reviewer
#6 lore raw

RFC PATCH 5/8 uprobe/x86: Add support to optimize on top of emulated instructions

In reply to Jiri Olsa

Adding support to optimize uprobe on top of instructions that can be emulated.

The idea is to store instructions on underlying 5 bytes and emulate them during the int3 and uprobe syscall execution:

- install 'call trampoline' through standard int3 update
- if int3 is hit before we finish optimizing we emulate all underlying instructions
- when call is installed the uprobe syscall will emulate all underlying instructions

Adding opt_xol_ops that emulate instructions that are replaced by 5 bytes call instruction used to optimize the uprobe.

---
 arch/x86/include/asm/uprobes.h |  13 ++--
 arch/x86/kernel/uprobes.c      | 106 ++++++++++++++++++++++++++++++++-
 include/linux/uprobes.h        |   1 +
 kernel/events/uprobes.c        |   6 ++
 4 files changed, 120 insertions(+), 6 deletions(-)
arch/x86/include/asm/uprobes.h +9 -4
index e09aab82b8c1..eaa80dc1c836 100644
@@ -21,8 +21,9 @@ typedef u8 uprobe_opcode_t;
 #define UPROBE_SWBP_INSN_SIZE		   1
 
 enum {
-	ARCH_UPROBE_FLAG_CAN_OPTIMIZE   = 0,
-	ARCH_UPROBE_FLAG_OPTIMIZE_FAIL  = 1,
+	ARCH_UPROBE_FLAG_CAN_OPTIMIZE     = 0,
+	ARCH_UPROBE_FLAG_OPTIMIZE_FAIL    = 1,
+	ARCH_UPROBE_FLAG_OPTIMIZE_EMULATE = 2,
 };
 
 struct uprobe_xol_ops;
@@ -59,11 +60,15 @@ struct arch_uprobe_xol {
 
 struct arch_uprobe {
 	union {
-		u8			insn[MAX_UINSN_BYTES];
+		u8			insn[5*MAX_UINSN_BYTES];
 		u8			ixol[MAX_UINSN_BYTES];
 	};
 
-	struct arch_uprobe_xol		xol;
+	struct arch_uprobe_xol	xol;
+	struct {
+		struct arch_uprobe_xol	xol[5];
+		int			cnt;
+	} opt;
 
 	unsigned long flags;
 };
arch/x86/kernel/uprobes.c +104 -2

153 lines not on this page · raw message

include/linux/uprobes.h +1 -0
index ee3d36eda45d..4b9f81ad8316 100644
@@ -242,6 +242,7 @@ 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 void arch_uprobe_optimized_emulate(struct arch_uprobe *auprobe, struct pt_regs *regs);
 #else /* !CONFIG_UPROBES */
 struct uprobes_state {
 };
kernel/events/uprobes.c +6 -0
index f11ceb8be8c4..dd893030e32e 100644
@@ -2701,6 +2701,10 @@ void __weak arch_uprobe_optimize(struct arch_uprobe *auprobe, unsigned long vadd
 {
 }
 
+void __weak arch_uprobe_optimized_emulate(struct arch_uprobe *auprobe, struct pt_regs *regs)
+{
+}
+
 /*
  * Run handler and ask thread to singlestep.
  * Ensure all non-fatal signals cannot interrupt thread while it singlesteps.
@@ -2801,6 +2805,8 @@ void handle_syscall_uprobe(struct pt_regs *regs, unsigned long bp_vaddr)
 	if (arch_uprobe_ignore(&uprobe->arch, regs))
 		return;
 	handler_chain(uprobe, regs);
+
+	arch_uprobe_optimized_emulate(&uprobe->arch, regs);
 }
 
 /*
Signature · 1 line
2.51.1
maintainer
#7 lore raw

Re: [RFC PATCH 5/8] uprobe/x86: Add support to optimize on top of emulated instructions

Hi Jiri,

I am trying to understand this series, will try to read it more carefully later...

(damn why do you always send the patches when I am on PTO? ;)

arch/x86/include/asm/uprobes.h struct arch_uprobe_xol { · line 63

Quoted 5 lines · Jiri Olsa

struct arch_uprobe {

arch/x86/include/asm/uprobes.h +1 -1
 	union {
-		u8			insn[MAX_UINSN_BYTES];
+		u8			insn[5*MAX_UINSN_BYTES];

Hmm. OK, this matches the "for (i = 0; i < 5; i++)" loop in opt_setup_xol_ops(), but do we really need this change? Please see the question at the end.

arch/x86/kernel/uprobes.c static int sub_setup_xol_ops(struct arch_uprobe_xol *xol, struct insn *insn) · line 1735

Quoted 8 lines
arch/x86/kernel/uprobes.c +8 -0
+static int opt_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
+{
+	unsigned long offset = insn->length;
+	struct insn insnX;
+	int i, ret;
+
+	if (test_bit(ARCH_UPROBE_FLAG_CAN_OPTIMIZE, &auprobe->flags))
+		return -ENOSYS;

I think this logic needs some cleanups... If ARCH_UPROBE_FLAG_CAN_OPTIMIZE is set by the caller, the it doesn't make sense to call xxx_setup_xol_ops(), right? But lets forget it for now.

arch/x86/kernel/uprobes.c static int sub_setup_xol_ops(struct arch_uprobe_xol *xol, struct insn *insn) · line 1737

Quoted 1 line
arch/x86/kernel/uprobes.c +1 -0
+	ret = opt_setup_xol_insns(auprobe, &auprobe->opt.xol[0], insn);

I think this should go into the main loop, see below

Quoted 4 lines
arch/x86/kernel/uprobes.c +4 -0
+	for (i = 1; i < 5; i++) {
+		ret = uprobe_init_insn_offset(auprobe, offset, &insnX, true);
+		if (ret)
+			break;

arch/x86/kernel/uprobes.c static int sub_setup_xol_ops(struct arch_uprobe_xol *xol, struct insn *insn) · line 1758

Quoted 10 lines
arch/x86/kernel/uprobes.c +10 -0
+		ret = opt_setup_xol_insns(auprobe, &auprobe->opt.xol[i], &insnX);
+		if (ret)
+			break;
+		offset += insnX.length;
+		auprobe->opt.cnt++;
+		if (offset >= 5)
+			goto optimize;
+	}
+
+	return -ENOSYS;

I don't think -ENOSYS makes sense if opt_setup_xol_insns() succeeds at least once. IOW, how about

	static int opt_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
	{
		unsigned long offset = 0;
		struct insn insnX;
		int i, ret;

		if (test_bit(ARCH_UPROBE_FLAG_CAN_OPTIMIZE, &auprobe->flags))
			return -ENOSYS;

		for (i = 0; i < 5; i++) {
			ret = opt_setup_xol_insns(auprobe, &auprobe->opt.xol[i], insn);
			if (ret)
				break;
			offset += insn->length;
			if (offset >= 5)
				break;

			insn = &insnX;
			ret = uprobe_init_insn_offset(auprobe, offset, insn, true);
			if (ret)
				break;
		}

		if (!offset)
			return -ENOSYS;

		if (offset >= 5) {
			auprobe->opt.cnt = i + 1;
			auprobe->xol.ops = &opt_xol_ops;
			set_bit(ARCH_UPROBE_FLAG_CAN_OPTIMIZE, &auprobe->flags);
			set_bit(ARCH_UPROBE_FLAG_OPTIMIZE_EMULATE, &auprobe->flags);
		}

		return 0;
	}

?

This way the caller, arch_uprobe_analyze_insn(), doesn't need to call push/mov/sub/_setup_xol_ops(), and the code looks a bit simpler to me.

No?

arch/x86/kernel/uprobes.c static int sub_setup_xol_ops(struct arch_uprobe_xol *xol, struct insn *insn) · line 1768

Quoted 3 lines
arch/x86/kernel/uprobes.c +3 -0
+      * TODO perhaps we could 'emulate' nop, so there would be no need for
+      * ARCH_UPROBE_FLAG_OPTIMIZE_EMULATE flag, because we would emulate
+      * allways.

Agreed... and this connects to "this logic needs some cleanups" above. I guess we need nop_setup_xol_ops() extracted from branch_setup_xol_ops() but again, lets forget it for now.

-------------------------------------------------------------------------------
Now the main question. What if we avoid this change

	-             u8                      insn[MAX_UINSN_BYTES];
	+             u8                      insn[5*MAX_UINSN_BYTES];

mentioned above, and change opt_setup_xol_ops() to just do

	-	for (i = 0; i < 5; i++)
	+	for (i = 0;; i++)

?

The main loop stops when offset >= 5 anyway.

And. if auprobe->insn[offset:MAX_UINSN_BYTES] doesn't contain a full/valid insn at the start, then uprobe_init_insn_offset()->insn_decode() should fail?

Most probably I missed something, but I can't understand this part.

Oleg.

#8 lore raw

Re: [RFC PATCH 5/8] uprobe/x86: Add support to optimize on top of emulated instructions

Quoted 6 lines · Oleg Nesterov

Hi Jiri,

I am trying to understand this series, will try to read it more carefully later...

(damn why do you always send the patches when I am on PTO? ;)

it's more fun that way ;-) thanks for checking on it

Quoted 83 lines · Jiri Olsa

arch/x86/include/asm/uprobes.h struct arch_uprobe_xol { · line 63

Quoted 5 lines

struct arch_uprobe {

arch/x86/include/asm/uprobes.h +1 -1
 	union {
-		u8			insn[MAX_UINSN_BYTES];
+		u8			insn[5*MAX_UINSN_BYTES];

Hmm. OK, this matches the "for (i = 0; i < 5; i++)" loop in opt_setup_xol_ops(), but do we really need this change? Please see the question at the end.

arch/x86/kernel/uprobes.c static int sub_setup_xol_ops(struct arch_uprobe_xol *xol, struct insn *insn) · line 1735

Quoted 8 lines
arch/x86/kernel/uprobes.c +8 -0
+static int opt_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
+{
+	unsigned long offset = insn->length;
+	struct insn insnX;
+	int i, ret;
+
+	if (test_bit(ARCH_UPROBE_FLAG_CAN_OPTIMIZE, &auprobe->flags))
+		return -ENOSYS;

I think this logic needs some cleanups... If ARCH_UPROBE_FLAG_CAN_OPTIMIZE is set by the caller, the it doesn't make sense to call xxx_setup_xol_ops(), right? But lets forget it for now.

arch/x86/kernel/uprobes.c static int sub_setup_xol_ops(struct arch_uprobe_xol *xol, struct insn *insn) · line 1737

Quoted 1 line
arch/x86/kernel/uprobes.c +1 -0
+	ret = opt_setup_xol_insns(auprobe, &auprobe->opt.xol[0], insn);

I think this should go into the main loop, see below

Quoted 4 lines
arch/x86/kernel/uprobes.c +4 -0
+	for (i = 1; i < 5; i++) {
+		ret = uprobe_init_insn_offset(auprobe, offset, &insnX, true);
+		if (ret)
+			break;

arch/x86/kernel/uprobes.c static int sub_setup_xol_ops(struct arch_uprobe_xol *xol, struct insn *insn) · line 1758

Quoted 10 lines
arch/x86/kernel/uprobes.c +10 -0
+		ret = opt_setup_xol_insns(auprobe, &auprobe->opt.xol[i], &insnX);
+		if (ret)
+			break;
+		offset += insnX.length;
+		auprobe->opt.cnt++;
+		if (offset >= 5)
+			goto optimize;
+	}
+
+	return -ENOSYS;

I don't think -ENOSYS makes sense if opt_setup_xol_insns() succeeds at least once. IOW, how about

	static int opt_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
	{
		unsigned long offset = 0;
		struct insn insnX;
		int i, ret;

		if (test_bit(ARCH_UPROBE_FLAG_CAN_OPTIMIZE, &auprobe->flags))
			return -ENOSYS;

		for (i = 0; i < 5; i++) {
			ret = opt_setup_xol_insns(auprobe, &auprobe->opt.xol[i], insn);
			if (ret)
				break;
			offset += insn->length;
			if (offset >= 5)
				break;

			insn = &insnX;
			ret = uprobe_init_insn_offset(auprobe, offset, insn, true);
			if (ret)
				break;
		}

		if (!offset)
			return -ENOSYS;

		if (offset >= 5) {
			auprobe->opt.cnt = i + 1;
			auprobe->xol.ops = &opt_xol_ops;
			set_bit(ARCH_UPROBE_FLAG_CAN_OPTIMIZE, &auprobe->flags);
			set_bit(ARCH_UPROBE_FLAG_OPTIMIZE_EMULATE, &auprobe->flags);
		}

		return 0;
	}
Quoted 4 lines

?

This way the caller, arch_uprobe_analyze_insn(), doesn't need to call push/mov/sub/_setup_xol_ops(), and the code looks a bit simpler to me.

ah nice, will try that

Quoted 10 lines

No?

arch/x86/kernel/uprobes.c static int sub_setup_xol_ops(struct arch_uprobe_xol *xol, struct insn *insn) · line 1768

Quoted 3 lines
arch/x86/kernel/uprobes.c +3 -0
+      * TODO perhaps we could 'emulate' nop, so there would be no need for
+      * ARCH_UPROBE_FLAG_OPTIMIZE_EMULATE flag, because we would emulate
+      * allways.

Agreed... and this connects to "this logic needs some cleanups" above. I guess we need nop_setup_xol_ops() extracted from branch_setup_xol_ops() but again, lets forget it for now.

ok, it will hopefully make the code simpler, will check on that

Quoted 14 lines

-------------------------------------------------------------------------------
Now the main question. What if we avoid this change

	-             u8                      insn[MAX_UINSN_BYTES];
	+             u8                      insn[5*MAX_UINSN_BYTES];

mentioned above, and change opt_setup_xol_ops() to just do

	-	for (i = 0; i < 5; i++)
	+	for (i = 0;; i++)

?

Quoted 7 lines

The main loop stops when offset >= 5 anyway.

And. if auprobe->insn[offset:MAX_UINSN_BYTES] doesn't contain a full/valid insn at the start, then uprobe_init_insn_offset()->insn_decode() should fail?

Most probably I missed something, but I can't understand this part.

no, I think you're right, I did not realize we fit under MAX_UINSN_BYTES anyway, call instruction needs only 5 bytes

thanks,
jirka

author reviewer
#9 lore raw

RFC PATCH 6/8 selftests/bpf: Add test for mov and sub emulation

In reply to Jiri Olsa

Adding test code for mov and sub instructions emulation.

TODO add test for sub flags value emulation.

---
 .../selftests/bpf/prog_tests/uprobe_syscall.c | 50 +++++++++++++++++++
 1 file changed, 50 insertions(+)
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +50 -0
index 955a37751b52..27fa6f309188 100644
@@ -765,6 +765,54 @@ static void test_uprobe_error(void)
 	ASSERT_EQ(errno, ENXIO, "errno");
 }
 
+__attribute__((aligned(16)))
+__nocf_check __weak __naked unsigned long emulate_mov_trigger(void)
+{
+	asm volatile (
+		"pushq %rbp\n"
+		"movq  %rsp,%rbp\n"
+		"subq  $0xb0,%rsp\n"
+		"addq  $0xb0,%rsp\n"
+		"pop %rbp\n"
+		"ret\n"
+	);
+}
+
+static void test_emulate(void)
+{
+	struct uprobe_syscall *skel = NULL;
+	unsigned long offset;
+
+	offset = get_uprobe_offset(&emulate_mov_trigger);
+	if (!ASSERT_GE(offset, 0, "get_uprobe_offset"))
+		return;
+
+	skel = uprobe_syscall__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "uprobe_syscall__open_and_load"))
+		goto cleanup;
+
+	/* mov */
+	skel->links.probe = bpf_program__attach_uprobe_opts(skel->progs.probe,
+				0, "/proc/self/exe", offset + 1, NULL);
+	if (!ASSERT_OK_PTR(skel->links.probe, "bpf_program__attach_uprobe_opts"))
+		goto cleanup;
+
+	emulate_mov_trigger();
+
+	bpf_link__destroy(skel->links.probe);
+
+	/* sub */
+	skel->links.probe = bpf_program__attach_uprobe_opts(skel->progs.probe,
+				0, "/proc/self/exe", offset + 4, NULL);
+	if (!ASSERT_OK_PTR(skel->links.probe, "bpf_program__attach_uprobe_opts"))
+		goto cleanup;
+
+	emulate_mov_trigger();
+
+cleanup:
+	uprobe_syscall__destroy(skel);
+}
+
 static void __test_uprobe_syscall(void)
 {
 	if (test__start_subtest("uretprobe_regs_equal"))
@@ -789,6 +837,8 @@ static void __test_uprobe_syscall(void)
 		test_uprobe_regs_equal(false);
 	if (test__start_subtest("regs_change"))
 		test_regs_change();
+	if (test__start_subtest("emulate_mov"))
+		test_emulate();
 }
 #else
 static void __test_uprobe_syscall(void)
Signature · 1 line
2.51.1
author reviewer
#10 lore raw

RFC PATCH 7/8 selftests/bpf: Add test for uprobe prologue optimization

In reply to Jiri Olsa

Adding test that places uprobe on top of supported prologue and checks that the uprobe gets properly optimized.

---
 .../selftests/bpf/prog_tests/uprobe_syscall.c | 63 ++++++++++++++++---
 1 file changed, 53 insertions(+), 10 deletions(-)
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +53 -10
index 27fa6f309188..c6a58afc7ace 100644
@@ -428,21 +428,21 @@ static void *check_attach(struct uprobe_syscall_executed *skel, trigger_t trigge
 	return tramp;
 }
 
-static void check_detach(void *addr, void *tramp)
+static void check_detach(void *addr, void *tramp, unsigned char *orig)
 {
 	/* [uprobes_trampoline] stays after detach */
 	ASSERT_OK(find_uprobes_trampoline(tramp), "uprobes_trampoline");
-	ASSERT_OK(memcmp(addr, nop5, 5), "nop5");
+	ASSERT_OK(memcmp(addr, orig, 5), "orig");
 }
 
 static void check(struct uprobe_syscall_executed *skel, struct bpf_link *link,
-		  trigger_t trigger, void *addr, int executed)
+		  trigger_t trigger, void *addr, int executed, unsigned char *orig)
 {
 	void *tramp;
 
 	tramp = check_attach(skel, trigger, addr, executed);
 	bpf_link__destroy(link);
-	check_detach(addr, tramp);
+	check_detach(addr, tramp, orig);
 }
 
 static void test_uprobe_legacy(void)
@@ -470,7 +470,7 @@ 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);
+	check(skel, link, uprobe_test, uprobe_test, 2, nop5);
 
 	/* uretprobe */
 	skel->bss->executed = 0;
@@ -480,7 +480,7 @@ 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);
+	check(skel, link, uprobe_test, uprobe_test, 2, nop5);
 
 cleanup:
 	uprobe_syscall_executed__destroy(skel);
@@ -512,7 +512,7 @@ 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);
+	check(skel, link, uprobe_test, uprobe_test, 2, nop5);
 
 	/* uretprobe.multi */
 	skel->bss->executed = 0;
@@ -522,7 +522,7 @@ 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);
+	check(skel, link, uprobe_test, uprobe_test, 2, nop5);
 
 cleanup:
 	uprobe_syscall_executed__destroy(skel);
@@ -555,7 +555,7 @@ 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);
+	check(skel, link, uprobe_test, uprobe_test, 4, nop5);
 
 cleanup:
 	uprobe_syscall_executed__destroy(skel);
@@ -584,7 +584,7 @@ static void test_uprobe_usdt(void)
 	if (!ASSERT_OK_PTR(link, "bpf_program__attach_usdt"))
 		goto cleanup;
 
-	check(skel, link, usdt_test, addr, 2);
+	check(skel, link, usdt_test, addr, 2, nop5);
 
 cleanup:
 	uprobe_syscall_executed__destroy(skel);
@@ -813,6 +813,47 @@ static void test_emulate(void)
 	uprobe_syscall__destroy(skel);
 }
 
+__attribute__((aligned(16)))
+__nocf_check __weak __naked void prologue_trigger(void)
+{
+	asm volatile (
+		"pushq %rbp\n"
+		"movq  %rsp,%rbp\n"
+		"subq  $0xb0,%rsp\n"
+		"addq  $0xb0,%rsp\n"
+		"pop %rbp\n"
+		"ret\n"
+	);
+}
+
+static void test_optimize_prologue(void)
+{
+	struct uprobe_syscall_executed *skel = NULL;
+	struct bpf_link *link;
+	unsigned long offset;
+
+	offset = get_uprobe_offset(&prologue_trigger);
+	if (!ASSERT_GE(offset, 0, "get_uprobe_offset"))
+		goto cleanup;
+
+	/* uprobe */
+	skel = uprobe_syscall_executed__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "uprobe_syscall_executed__open_and_load"))
+		return;
+
+	skel->bss->pid = getpid();
+
+	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, prologue_trigger, prologue_trigger, 2, (unsigned char *) prologue_trigger);
+
+cleanup:
+	uprobe_syscall_executed__destroy(skel);
+}
+
 static void __test_uprobe_syscall(void)
 {
 	if (test__start_subtest("uretprobe_regs_equal"))
@@ -839,6 +880,8 @@ static void __test_uprobe_syscall(void)
 		test_regs_change();
 	if (test__start_subtest("emulate_mov"))
 		test_emulate();
+	if (test__start_subtest("optimize_prologue"))
+		test_optimize_prologue();
 }
 #else
 static void __test_uprobe_syscall(void)
Signature · 1 line
2.51.1
author reviewer
#11 lore raw

RFC PATCH 8/8 selftests/bpf: Add race test for uprobe proglog optimization

In reply to Jiri Olsa

Adding uprobe race test on top of prologue instructions.

---
 .../selftests/bpf/prog_tests/uprobe_syscall.c    | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +10 -6
index c6a58afc7ace..8793fbd61ffd 100644
@@ -654,10 +654,11 @@ static USDT_DEFINE_SEMA(race);
 
 static void *worker_trigger(void *arg)
 {
+	trigger_t trigger = (trigger_t) arg;
 	unsigned long rounds = 0;
 
 	while (!race_stop) {
-		uprobe_test();
+		trigger();
 		rounds++;
 	}
 
@@ -667,6 +668,7 @@ static void *worker_trigger(void *arg)
 
 static void *worker_attach(void *arg)
 {
+	trigger_t trigger = (trigger_t) arg;
 	LIBBPF_OPTS(bpf_uprobe_opts, opts);
 	struct uprobe_syscall_executed *skel;
 	unsigned long rounds = 0, offset;
@@ -677,7 +679,7 @@ static void *worker_attach(void *arg)
 	unsigned long *ref;
 	int err;
 
-	offset = get_uprobe_offset(&uprobe_test);
+	offset = get_uprobe_offset(trigger);
 	if (!ASSERT_GE(offset, 0, "get_uprobe_offset"))
 		return NULL;
 
@@ -722,7 +724,7 @@ static useconds_t race_msec(void)
 	return 500;
 }
 
-static void test_uprobe_race(void)
+static void test_uprobe_race(trigger_t trigger)
 {
 	int err, i, nr_threads;
 	pthread_t *threads;
@@ -738,7 +740,7 @@ static void test_uprobe_race(void)
 
 	for (i = 0; i < nr_threads; i++) {
 		err = pthread_create(&threads[i], NULL, i % 2 ? worker_trigger : worker_attach,
-				     NULL);
+				     trigger);
 		if (!ASSERT_OK(err, "pthread_create"))
 			goto cleanup;
 	}
@@ -870,8 +872,10 @@ static void __test_uprobe_syscall(void)
 		test_uprobe_session();
 	if (test__start_subtest("uprobe_usdt"))
 		test_uprobe_usdt();
-	if (test__start_subtest("uprobe_race"))
-		test_uprobe_race();
+	if (test__start_subtest("uprobe_race_nop5"))
+		test_uprobe_race(uprobe_test);
+	if (test__start_subtest("uprobe_race_prologue"))
+		test_uprobe_race(prologue_trigger);
 	if (test__start_subtest("uprobe_error"))
 		test_uprobe_error();
 	if (test__start_subtest("uprobe_regs_equal"))
Signature · 1 line
2.51.1
maintainer
#12 lore raw
In reply to Jiri Olsa
Quoted 9 lines · Jiri Olsa

This patchset adds support to optimize uprobe on top of instruction that could be emulated and also adds support to emulate particular versions of mov and sub instructions to cover some of the user space functions prologues, like:

pushq %rbp
movq %rsp,%rbp
subq $0xb0,%rsp

...

Quoted 6 lines

There's an additional issue that single instruction replacement does not have and it's the possibility of the user space code to jump in the middle of those 5 bytes. I think it's unlikely to happen at the function prologue, but uprobe could be placed anywhere. I'm not sure how to mitigate this other than having some enable/disable switch or config option, which is unfortunate.

plus this breaks single-stepping... Although perhaps we don't really care.

Oleg.

#13 lore raw
Quoted 13 lines · On Mon, 24 Nov 2025 19:12:42 +0100 Oleg Nesterov <[email protected]> wrote:
Quoted 9 lines · Jiri Olsa

This patchset adds support to optimize uprobe on top of instruction that could be emulated and also adds support to emulate particular versions of mov and sub instructions to cover some of the user space functions prologues, like:

pushq %rbp
movq %rsp,%rbp
subq $0xb0,%rsp

...

Quoted 8 lines
Quoted 6 lines

There's an additional issue that single instruction replacement does not have and it's the possibility of the user space code to jump in the middle of those 5 bytes. I think it's unlikely to happen at the function prologue, but uprobe could be placed anywhere. I'm not sure how to mitigate this other than having some enable/disable switch or config option, which is unfortunate.

plus this breaks single-stepping... Although perhaps we don't really care.

Yeah, and I think we can stop optimization if post_handler is set.

Thanks,

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
maintainer
#14 lore raw
Quoted 13 lines · Masami Hiramatsu
Quoted 9 lines · On Mon, 24 Nov 2025 19:12:42 +0100 Oleg Nesterov <[email protected]> wrote:
Quoted 7 lines · Jiri Olsa

There's an additional issue that single instruction replacement does not have and it's the possibility of the user space code to jump in the middle of those 5 bytes. I think it's unlikely to happen at the function prologue, but uprobe could be placed anywhere. I'm not sure how to mitigate this other than having some enable/disable switch or config option, which is unfortunate.

Quoted 3 lines
Quoted 1 line

plus this breaks single-stepping... Although perhaps we don't really care.

Yeah, and I think we can stop optimization if post_handler is set.

Hmm, why? This doesn't depend on whether ->ret_handler is set or not...

Oleg.

#15 lore raw
In reply to Jiri Olsa
Quoted 14 lines · Jiri Olsa

hi,
the subject is bit too optimistic, in nutshell the idea is to allow optimization on top of emulated instructions and then add support to emulate more instructions with high presence in function prologues.

This patchset adds support to optimize uprobe on top of instruction that could be emulated and also adds support to emulate particular versions of mov and sub instructions to cover some of the user space functions prologues, like:

pushq %rbp
movq %rsp,%rbp
subq $0xb0,%rsp

Quoted 8 lines

The idea is to store instructions on underlying 5 bytes and emulate them during the int3 and uprobe syscall execution:

- install 'call trampoline' through standard int3 update
- if int3 is hit before we finish optimizing we emulate all underlying instructions
- when call is installed the uprobe syscall will emulate all underlying instructions

David, sorry I used wrong email.. I think the update here might be a problem, any chance you could check?

thanks,
jirka

Quoted 32 lines

There's an additional issue that single instruction replacement does not have and it's the possibility of the user space code to jump in the middle of those 5 bytes. I think it's unlikely to happen at the function prologue, but uprobe could be placed anywhere. I'm not sure how to mitigate this other than having some enable/disable switch or config option, which is unfortunate.

The patchset is based on bpf-next/master with [1] changes merged in.

thanks,
jirka

[1] https://lore.kernel.org/lkml/[email protected]/T/#m95a3208943ec24c5eee17ad6113002fdc6776cf8
---
Jiri Olsa (8):
uprobe/x86: Introduce struct arch_uprobe_xol object
uprobe/x86: Use struct arch_uprobe_xol in emulate callback
uprobe/x86: Add support to emulate mov reg,reg instructions
uprobe/x86: Add support to emulate sub imm,reg instructions
uprobe/x86: Add support to optimize on top of emulated instructions
selftests/bpf: Add test for mov and sub emulation
selftests/bpf: Add test for uprobe prologue optimization
selftests/bpf: Add race test for uprobe proglog optimization

 arch/x86/include/asm/uprobes.h                          |  35 +++++++---
 arch/x86/kernel/uprobes.c                               | 336 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------
 include/linux/uprobes.h                                 |   1 +
 kernel/events/uprobes.c                                 |   6 ++
 tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c | 129 ++++++++++++++++++++++++++++++++-----
 5 files changed, 434 insertions(+), 73 deletions(-)