[PATCHv2 2/2] x86/kprobes: Use ftrace_location_range in __recover_probed_insn

From: Jiri Olsa

Date: Sun Nov 22 2026 - 15:03:11 EST


The __recover_probed_insn function is called for every instruction
decoded on the way to the probed address and it uses ftrace_location
to find out whether the instruction is the ftrace location.

When the address is not ftrace location, ftrace_location does also
the symbol lookup to map the sym+0 address to its ftrace location.
But __recover_probed_insn only checks if the returned location is
the address itself, and that can't be the result of the mapping
after the lookup of the address failed, so the symbol lookup is
done for nothing for almost every decoded instruction.

Using ftrace_location_range for the address instead, which does
just the lookup of the address.

Adding the missing ftrace_location_range stub for the config
without dynamic ftrace.

Registering 20587 kprobes (as disabled) with the kprobe_many_example
sample module [1] in qemu with 8 CPUs goes from 4.02s to 3.70s.

The sample module was posted as the last patch of the kprobes
lookup cost series [1] only to demonstrate the testing, it is not
merged and is not going to be.

[1] https://lore.kernel.org/bpf/20261109195827.1091410-1-jolsa@xxxxxxxxxx/

Signed-off-by: Jiri Olsa <jolsa@xxxxxxxxxx>
---
arch/x86/kernel/kprobes/core.c | 6 +++++-
include/linux/ftrace.h | 4 ++++
2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c
index 133ff20caccd..34d71907e6d1 100644
--- a/arch/x86/kernel/kprobes/core.c
+++ b/arch/x86/kernel/kprobes/core.c
@@ -207,7 +207,11 @@ __recover_probed_insn(kprobe_opcode_t *buf, unsigned long addr)
bool faddr;

kp = get_kprobe((void *)addr);
- faddr = ftrace_location(addr) == addr;
+ /*
+ * Only the exact ftrace location matters, so skip the symbol
+ * lookup ftrace_location() does to map sym+0 to its location.
+ */
+ faddr = ftrace_location_range(addr, addr) == addr;
/*
* Use the current code if it is not modified by Kprobe
* and it cannot be modified by ftrace.
diff --git a/include/linux/ftrace.h b/include/linux/ftrace.h
index 1c2056a1a68b..255751899682 100644
--- a/include/linux/ftrace.h
+++ b/include/linux/ftrace.h
@@ -1075,6 +1075,10 @@ static inline unsigned long ftrace_location(unsigned long ip)
{
return 0;
}
+static inline unsigned long ftrace_location_range(unsigned long start, unsigned long end)
+{
+ return 0;
+}

/*
* Again users of functions that have ftrace_ops may not
--
2.56.1