From: Jiri Olsa <jolsa@kernel.org>
To: Peter Zijlstra <peterz@infradead.org>,
Josh Poimboeuf <jpoimboe@kernel.org>
Cc: Jason Baron <jbaron@akamai.com>,
Alice Ryhl <aliceryhl@google.com>,
Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
x86@kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCHv2] jump_label/x86: Avoid decoding instructions for jump entry size
Date: Tue, 17 Nov 2026 20:55:06 +0100 [thread overview]
Message-ID: <20261117195506.23759-1-jolsa@kernel.org> (raw)
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
reply other threads:[~2026-11-17 19:55 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261117195506.23759-1-jolsa@kernel.org \
--to=jolsa@kernel.org \
--cc=aliceryhl@google.com \
--cc=jbaron@akamai.com \
--cc=jpoimboe@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=tglx@kernel.org \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®