Skip to contentSkip to conversation

uprobes/x86: Add support to emulate nop instructions

PATCH Superseded Jiri Olsa 4 participants 6 messages Apr 11, 2025 – Apr 13, 2025

Subsystems (6)
x86 Kselftest BPF Tracing +2 more subsystems

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jiri Olsa

author reviewer
#0 lore raw

PATCH v2 1/2 uprobes/x86: Add support to emulate nop instructions

Adding support to emulate all nop instructions as the original uprobe instruction.

This change speeds up uprobe on top of all nop instructions and is a preparation for usdt probe optimization, that will be done on top of nop5 instruction.

With this change the usdt probe on top of nop5 won't take the performance hit compared to usdt probe on top of standard nop instruction.

---
v2 changes:
- follow Adndrii/Oleg's suggestion and emulate all the nops

 arch/x86/kernel/uprobes.c | 6 ++++++
 1 file changed, 6 insertions(+)
arch/x86/kernel/uprobes.c +6 -0
index 9194695662b2..262960189a1c 100644
@@ -840,6 +840,12 @@ static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
 	insn_byte_t p;
 	int i;
 
+	/* x86_nops[i]; same as jmp with .offs = 0 */
+	for (i = 1; i <= ASM_NOP_MAX; ++i) {
+		if (!memcmp(insn->kaddr, x86_nops[i], i))
+			goto setup;
+	}
+
 	switch (opc1) {
 	case 0xeb:	/* jmp 8 */
 	case 0xe9:	/* jmp 32 */
Signature · 1 line
2.49.0
author reviewer
#1 lore raw

PATCH v2 2/2 selftests/bpf: Add 5-byte nop uprobe trigger bench

Add 5-byte nop uprobe trigger bench (x86_64 specific) to measure uprobes/uretprobes on top of nop5 instruction.

---
 tools/testing/selftests/bpf/bench.c           | 12 ++++++
 .../selftests/bpf/benchs/bench_trigger.c      | 42 +++++++++++++++++++
 .../selftests/bpf/benchs/run_bench_uprobes.sh |  2 +-
 3 files changed, 55 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/bench.c +12 -0
index 1bd403a5ef7b..0fd8c9b0d38f 100644
@@ -526,6 +526,12 @@ extern const struct bench bench_trig_uprobe_multi_push;
 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;
+#endif
 
 extern const struct bench bench_rb_libbpf;
 extern const struct bench bench_rb_custom;
@@ -586,6 +592,12 @@ static const struct bench *benchs[] = {
 	&bench_trig_uretprobe_multi_push,
 	&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,
+#endif
 	/* ringbuf/perfbuf benchmarks */
 	&bench_rb_libbpf,
 	&bench_rb_custom,
tools/testing/selftests/bpf/benchs/bench_trigger.c +42 -0

57 lines not on this page · raw message

tools/testing/selftests/bpf/benchs/run_bench_uprobes.sh +1 -1
index af169f831f2f..03f55405484b 100755
@@ -2,7 +2,7 @@
 
 set -eufo pipefail
 
-for i in usermode-count syscall-count {uprobe,uretprobe}-{nop,push,ret}
+for i in usermode-count syscall-count {uprobe,uretprobe}-{nop,push,ret,nop5}
 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.49.0
maintainer Acked-by on v2 1/2
#2 lore raw
In reply to Jiri Olsa

arch/x86/kernel/uprobes.c static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn) · line 847

Quoted 12 lines · Jiri Olsa
arch/x86/kernel/uprobes.c +5 -0
@@ -840,6 +840,12 @@ static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
 	insn_byte_t p;
 	int i;
 
+	/* x86_nops[i]; same as jmp with .offs = 0 */
+	for (i = 1; i <= ASM_NOP_MAX; ++i) {
+		if (!memcmp(insn->kaddr, x86_nops[i], i))
+			goto setup;
+	}
#3 lore raw
In reply to Jiri Olsa
Quoted 26 lines · Jiri Olsa

Adding support to emulate all nop instructions as the original uprobe instruction.

This change speeds up uprobe on top of all nop instructions and is a preparation for usdt probe optimization, that will be done on top of nop5 instruction.

With this change the usdt probe on top of nop5 won't take the performance hit compared to usdt probe on top of standard nop instruction.

---
v2 changes:
- follow Adndrii/Oleg's suggestion and emulate all the nops

 arch/x86/kernel/uprobes.c | 6 ++++++
 1 file changed, 6 insertions(+)
arch/x86/kernel/uprobes.c +0 -0
index 9194695662b2..262960189a1c 100644

arch/x86/kernel/uprobes.c static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn) · line 844

Quoted 5 lines
arch/x86/kernel/uprobes.c +2 -0
@@ -840,6 +840,12 @@ static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
        insn_byte_t p;
        int i;
 
+       /* x86_nops[i]; same as jmp with .offs = 0 */
+       for (i = 1; i <= ASM_NOP_MAX; ++i) {

i <= ASM_NOP_MAX && i <= insn->length

?

otherwise what prevents us from reading past the actual instruction bytes?

or, actually, shouldn't we just check memcmp(x86_nops[insn->length]) if insn->length < ASM_NOP_MAX ?

Quoted 10 lines
arch/x86/kernel/uprobes.c +4 -1
+               if (!memcmp(insn->kaddr, x86_nops[i], i))
+                       goto setup;
+       }
+
        switch (opc1) {
        case 0xeb:      /* jmp 8 */
        case 0xe9:      /* jmp 32 */
--

2.49.0

maintainer
#4 lore raw
Quoted 7 lines · Andrii Nakryiko
Quoted 7 lines
arch/x86/kernel/uprobes.c +0 -0
@@ -840,6 +840,12 @@ static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
        insn_byte_t p;
        int i;
 
Quoted 8 lines

arch/x86/kernel/uprobes.c static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn) · line 844

Quoted 2 lines
arch/x86/kernel/uprobes.c +2 -0
+       /* x86_nops[i]; same as jmp with .offs = 0 */
+       for (i = 1; i <= ASM_NOP_MAX; ++i) {

i <= ASM_NOP_MAX && i <= insn->length

?

otherwise what prevents us from reading past the actual instruction bytes?

Well, copy_insn() just copies MAX_UINSN_BYTES into arch_uprobe.insn[]. If, say, the 1st 11 bytes of arch_uprobe.insn (or insn->kaddr) match x86_nops[11] then insn->length must be 11, or insn_decode() is buggy?

Quoted 2 lines

or, actually, shouldn't we just check memcmp(x86_nops[insn->length]) if insn->length < ASM_NOP_MAX ?

Hmm... agreed.

Either way this check can't (doesn't even try to) detect, say, "rep; BYTES_NOP5", so we do not care if insn->length == 6 in this case.

Good point!

Oleg.

#5 lore raw
Quoted 13 lines · Oleg Nesterov
Quoted 12 lines · Andrii Nakryiko

arch/x86/kernel/uprobes.c static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn) · line 844

Quoted 9 lines
arch/x86/kernel/uprobes.c +2 -0
@@ -840,6 +840,12 @@ static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
        insn_byte_t p;
        int i;
 
+       /* x86_nops[i]; same as jmp with .offs = 0 */
+       for (i = 1; i <= ASM_NOP_MAX; ++i) {

i <= ASM_NOP_MAX && i <= insn->length

Quoted 10 lines
Quoted 3 lines

?

otherwise what prevents us from reading past the actual instruction bytes?

Well, copy_insn() just copies MAX_UINSN_BYTES into arch_uprobe.insn[]. If, say, the 1st 11 bytes of arch_uprobe.insn (or insn->kaddr) match x86_nops[11] then insn->length must be 11, or insn_decode() is buggy?

Quoted 2 lines

or, actually, shouldn't we just check memcmp(x86_nops[insn->length]) if insn->length < ASM_NOP_MAX ?

nice, did not think of that

Quoted 7 lines

Hmm... agreed.

Either way this check can't (doesn't even try to) detect, say, "rep; BYTES_NOP5", so we do not care if insn->length == 6 in this case.

Good point!

I'll run tests and send formal patch for change below

thanks,
jirka

---

arch/x86/kernel/uprobes.c +5 -0
index 9194695662b2..6d383839e839 100644
@@ -840,6 +840,11 @@ static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
 	insn_byte_t p;
 	int i;
 
+	/* x86_nops[insn->length]; same as jmp with .offs = 0 */
+	if (insn->length <= ASM_NOP_MAX &&
+	    !memcmp(insn->kaddr, x86_nops[insn->length], insn->length))
+		goto setup;
+
 	switch (opc1) {
 	case 0xeb:	/* jmp 8 */
 	case 0xe9:	/* jmp 32 */