BPF List
 help / color / mirror / Atom feed
From: Jiri Olsa <jolsa@kernel.org>
To: Masami Hiramatsu <mhiramat@kernel.org>,
	Naveen N Rao <naveen@kernel.org>,
	"David S. Miller" <davem@davemloft.net>
Cc: Peter Zijlstra <peterz@infradead.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	Ingo Molnar <mingo@redhat.com>,
	x86@kernel.org, linux-trace-kernel@vger.kernel.org,
	linux-kernel@vger.kernel.org, bpf@vger.kernel.org
Subject: [PATCHv2 2/2] x86/kprobes: Use ftrace_location_range in __recover_probed_insn
Date: Sun, 22 Nov 2026 21:02:39 +0100	[thread overview]
Message-ID: <20261122200239.1481088-3-jolsa@kernel.org> (raw)
In-Reply-To: <20261122200239.1481088-1-jolsa@kernel.org>

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@kernel.org/

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
 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


      parent reply	other threads:[~2026-11-22 20:03 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-22 20:02 [PATCHv2 0/2] kprobes: Reduce registration lookup costs Jiri Olsa
2026-11-22 20:02 ` [PATCHv2 1/2] kprobes: Increase the kprobe hash table size Jiri Olsa
2026-11-22 20:02 ` Jiri Olsa [this message]

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=20261122200239.1481088-3-jolsa@kernel.org \
    --to=jolsa@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=davem@davemloft.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=mingo@redhat.com \
    --cc=naveen@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.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