* [PATCHv2] jump_label/x86: Avoid decoding instructions for jump entry size
@ 2026-11-17 19:55 Jiri Olsa
0 siblings, 0 replies; only message in thread
From: Jiri Olsa @ 2026-11-17 19:55 UTC (permalink / raw)
To: Peter Zijlstra, Josh Poimboeuf
Cc: Jason Baron, Alice Ryhl, Thomas Gleixner, Ingo Molnar, x86, linux-kernel
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.
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
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/20261109194916.1089703-4-jolsa@kernel.org/
arch/x86/kernel/jump_label.c | 18 +++++++++++++-----
1 file changed, 13 insertions(+), 5 deletions(-)
diff --git a/arch/x86/kernel/jump_label.c b/arch/x86/kernel/jump_label.c
index a7949a54a0ff..cf2df807e4c2 100644
--- a/arch/x86/kernel/jump_label.c
+++ b/arch/x86/kernel/jump_label.c
@@ -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 {
--
2.56.1
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-17 19:55 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-17 19:55 [PATCHv2] jump_label/x86: Avoid decoding instructions for jump entry size Jiri Olsa
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®