Skip to contentSkip to conversation

jump_label/x86: Avoid decoding instructions for jump entry size

PATCH In review Jiri Olsa 2 participants 2 messages Nov 17 – Nov 21

Subsystems (2)
lore
Share & tools
lore

Jiri Olsa

author
#0 lore raw

PATCH v2 1/1 jump_label/x86: Avoid decoding instructions for jump entry size

arch_jump_entry_size() runs the full instruction decoder just to find out whether a jump entry is 2 or 5 bytes long. A jump label site can only ever hold a JMP8, a JMP32 or the matching 2/5 byte NOP, so the size can be determined directly from the opcode byte or by comparing against the x86_nops[] encodings.

This matters for kprobe registration, where the address safety check calls jump_label_text_reserved(), which walks the jump entries and calls arch_jump_entry_size() for each of them. When registering many kprobes the instruction decoding dominates the registration time.

---
v2 changes:
- sending just this single change from v1 post [1], rest will come later
- check code[0] against both JMP8 and JMP32 opcodes first and only then fall back to the memcmp() nop comparisons, instead of relying on the compiler to reorder the combined conditions [Peter]
- I measured the change with a test module that registers kprobes with register_kprobes() on 20000 generated functions (24 bytes each, probe placed at offset 8 in each function). The module is for testing only and is part of v1 [1].

    # perf stat -e cycles:k,instructions:k \
      insmod ./samples/kprobes/kprobe_many_example.ko nr_functions=20000
    before:
      51,498,815,127      cycles:k
      53,955,643,792      instructions:k

        10.133113175 seconds time elapsed

    after:
      23,945,896,734      cycles:k
      18,710,375,445      instructions:k

         4.695635577 seconds time elapsed

That's ~54% less time and ~65% fewer instructions.

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

 arch/x86/kernel/jump_label.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
arch/x86/kernel/jump_label.c +13 -5
index a7949a54a0ff..cf2df807e4c2 100644
@@ -15,16 +15,24 @@
 #include <asm/kprobes.h>
 #include <asm/alternative.h>
 #include <asm/text-patching.h>
-#include <asm/insn.h>
 
 int arch_jump_entry_size(struct jump_entry *entry)
 {
-	struct insn insn = {};
+	const u8 *code = (void *)jump_entry_code(entry);
 
-	insn_decode_kernel(&insn, (void *)jump_entry_code(entry));
-	BUG_ON(insn.length != 2 && insn.length != 5);
+	if (code[0] == JMP8_INSN_OPCODE)
+		return JMP8_INSN_SIZE;
 
-	return insn.length;
+	if (code[0] == JMP32_INSN_OPCODE)
+		return JMP32_INSN_SIZE;
+
+	if (!memcmp(code, x86_nops[JMP8_INSN_SIZE], JMP8_INSN_SIZE))
+		return JMP8_INSN_SIZE;
+
+	if (!memcmp(code, x86_nops[JMP32_INSN_SIZE], JMP32_INSN_SIZE))
+		return JMP32_INSN_SIZE;
+
+	BUG();
 }
 
 struct jump_label_patch {
Signature · 1 line
2.56.1
maintainer Acked-by on v2 1/1
#1 lore raw
Quoted 6 lines · Jiri Olsa

arch_jump_entry_size() runs the full instruction decoder just to find out whether a jump entry is 2 or 5 bytes long. A jump label site can only ever hold a JMP8, a JMP32 or the matching 2/5 byte NOP, so the size can be determined directly from the opcode byte or by comparing against the x86_nops[] encodings.

Quoted 6 lines

This matters for kprobe registration, where the address safety check calls jump_label_text_reserved(), which walks the jump entries and calls arch_jump_entry_size() for each of them. When registering many kprobes the instruction decoding dominates the registration time.

Signature · 1 line
Josh