[PATCHv2 1/2] kprobes: Increase the kprobe hash table size

From: Jiri Olsa

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


The kprobe hash table has 64 buckets, so with many probes registered
each get_kprobe() call walks a long chain. With 20000 probes that is
more than 300 entries per bucket on average, and every entry is a
separate struct kprobe, so the walk is mostly cache misses.

get_kprobe() is not called just once per registered probe. On x86 the
registration path calls it for each address it has to check around
the new probe: get_optimized_kprobe(), __recover_probed_insn(),
__recover_optprobed_insn() and arch_check_optimized_kprobe() all do
one lookup per byte or per decoded instruction, and most of these
lookups miss, which means walking the whole chain. It is also called
from the breakpoint and ftrace handlers on every probe hit.

Increase KPROBE_HASH_BITS from 6 to 12, which gives 4096 buckets and
cuts the chains to about 5 entries for 20000 probes and about 12
entries for 50000 probes.

Time to register the probes with the kprobe_many_example sample
module [1] (insmod with nr_functions=20000 and nr_functions=50000)
in a KVM guest, mean of 5 runs, for different KPROBE_HASH_BITS
values. The table memory is the size of the static kprobe_table on
64-bit:

bits buckets table memory 20000 probes 50000 probes
6 64 512 B 4.71 s 29.99 s
8 256 2 KB 2.05 s 9.90 s
10 1024 8 KB 1.39 s 4.63 s
12 4096 32 KB 1.30 s 3.64 s
14 16384 128 KB 1.28 s 3.41 s
16 65536 512 KB 1.28 s 3.38 s

With 12 bits the registration of 20000 probes is 3.6 times faster
and the registration of 50000 probes is 8.2 times faster than with
the current 6 bits. 12 bits is where the curve flattens: compared
to 10 bits it is still 21% faster for 50000 probes (6% for 20000),
while going further to 16 bits gains only 1.5% for 20000 probes and
7% for 50000 probes, for a table 16 times bigger (512KB vs 32KB).

The cost is the size of the static table, which grows from 64 to
4096 hlist heads, that is from 512 bytes to 32KB on 64-bit. The
functions that walk the whole table (module notifier, arming or
optimizing all probes, the debugfs list) are slow paths that take
kprobe_mutex and now scan 4096 buckets.

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/

Suggested-by: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>
Signed-off-by: Jiri Olsa <jolsa@xxxxxxxxxx>
---
kernel/kprobes.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/kprobes.c b/kernel/kprobes.c
index f14f7ff45961..47ce0bfb7298 100644
--- a/kernel/kprobes.c
+++ b/kernel/kprobes.c
@@ -50,7 +50,7 @@
#include <asm/errno.h>
#include <linux/uaccess.h>

-#define KPROBE_HASH_BITS 6
+#define KPROBE_HASH_BITS 12
#define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)

#if !defined(CONFIG_OPTPROBES) || !defined(CONFIG_SYSCTL)
--
2.56.1