From: Jiri Olsa <jolsa@kernel.org>
To: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Masami Hiramatsu <mhiramat@kernel.org>
Cc: bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org,
linux-kernel@vger.kernel.org, x86@kernel.org,
Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
Quentin Monnet <qmo@kernel.org>,
Alan Maguire <almagui@kernel.org>
Subject: [PATCH RFC bpf-next 03/31] kprobes: Add enable_kprobes to enable multiple kprobes at once
Date: Wed, 2 Dec 2026 13:30:07 +0100 [thread overview]
Message-ID: <20261202123035.10335-4-jolsa@kernel.org> (raw)
In-Reply-To: <20261202123035.10335-1-jolsa@kernel.org>
Enabling kprobe puts the breakpoint in place and waits for all the
other CPUs to see it. On x86 that is an IPI to every CPU for each
kprobe, which gets expensive with thousands of kprobes.
Adding enable_kprobes function that enables array of kprobes under
single kprobe_mutex lock, installs their breakpoints without waiting
for the other CPUs and syncs them just once at the end.
To support that adding two arch functions:
arch_arm_kprobe_nosync - installs the breakpoint without the sync
arch_arm_kprobes_sync - syncs all the breakpoints installed so far
The default arch_arm_kprobe_nosync falls back to arch_arm_kprobe and
the default arch_arm_kprobes_sync does nothing, so there's no change
for other architectures. Adding the x86 version.
The ftrace based kprobes are armed through ftrace as before, there's
no difference for them.
If any of the kprobes fails to get enabled, enable_kprobes returns
the error and leaves the kprobes enabled so far as they are, the
caller can find them with kprobe_disabled(). The sync is done also
in this case.
Time to enable disabled kprobes with the kprobe_many_example sample
module [1], enable_kprobe() in a loop against single enable_kprobes()
call, on bare metal AMD Ryzen 7 PRO 8840HS (16 CPUs), median of
6 runs, CONFIG_KPROBE_HASH_BITS=16:
probes enable_kprobe enable_kprobes speedup
1000 12.1 ms 0.36 ms 34x
5000 55.3 ms 1.70 ms 33x
10000 102 ms 3.50 ms 29x
20000 187 ms 7.30 ms 26x
50000 354 ms 21.4 ms 17x
The speedup depends on the kprobe hash table size, because arming
the kprobe does several get_kprobe() lookups. With
CONFIG_KPROBE_HASH_BITS=10 it is 33x for 1000 kprobes, but only
2.7x for 20000 and 1.3x for 50000, where the hash chain walks take
most of the time.
[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 | 13 +++++
include/linux/kprobes.h | 7 +++
kernel/kprobes.c | 90 +++++++++++++++++++++++++++++-----
3 files changed, 99 insertions(+), 11 deletions(-)
diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c
index 34d71907e6d1..02f27ca3db2b 100644
--- a/arch/x86/kernel/kprobes/core.c
+++ b/arch/x86/kernel/kprobes/core.c
@@ -793,6 +793,19 @@ void arch_arm_kprobe(struct kprobe *p)
perf_event_text_poke(p->addr, &p->opcode, 1, &int3, 1);
}
+void arch_arm_kprobe_nosync(struct kprobe *p)
+{
+ u8 int3 = INT3_INSN_OPCODE;
+
+ text_poke(p->addr, &int3, 1);
+ perf_event_text_poke(p->addr, &p->opcode, 1, &int3, 1);
+}
+
+void arch_arm_kprobes_sync(void)
+{
+ smp_text_poke_sync_each_cpu();
+}
+
void arch_disarm_kprobe(struct kprobe *p)
{
u8 int3 = INT3_INSN_OPCODE;
diff --git a/include/linux/kprobes.h b/include/linux/kprobes.h
index e6de7ae55bda..e1d30001da98 100644
--- a/include/linux/kprobes.h
+++ b/include/linux/kprobes.h
@@ -272,6 +272,8 @@ extern struct kretprobe_blackpoint kretprobe_blacklist[];
extern int arch_prepare_kprobe(struct kprobe *p);
extern void arch_arm_kprobe(struct kprobe *p);
+extern void arch_arm_kprobe_nosync(struct kprobe *p);
+extern void arch_arm_kprobes_sync(void);
extern void arch_disarm_kprobe(struct kprobe *p);
extern int arch_init_kprobes(void);
extern void kprobes_inc_nmissed_count(struct kprobe *p);
@@ -423,6 +425,7 @@ void kprobe_free_init_mem(void);
int disable_kprobe(struct kprobe *kp);
int enable_kprobe(struct kprobe *kp);
+int enable_kprobes(struct kprobe **kps, int num);
void dump_kprobe(struct kprobe *kp);
@@ -498,6 +501,10 @@ static inline int disable_kprobe(struct kprobe *kp)
{
return -EOPNOTSUPP;
}
+static inline int enable_kprobes(struct kprobe **kps, int num)
+{
+ return -EOPNOTSUPP;
+}
static inline int enable_kprobe(struct kprobe *kp)
{
return -EOPNOTSUPP;
diff --git a/kernel/kprobes.c b/kernel/kprobes.c
index eba446e8ae37..836924e99a76 100644
--- a/kernel/kprobes.c
+++ b/kernel/kprobes.c
@@ -417,6 +417,20 @@ static inline void copy_kprobe(struct kprobe *ap, struct kprobe *p)
memcpy(&p->ainsn, &ap->ainsn, sizeof(struct arch_specific_insn));
}
+/*
+ * Architectures can put the breakpoint without waiting for other CPUs
+ * in arch_arm_kprobe_nosync() and do the wait for all the breakpoints
+ * at once in arch_arm_kprobes_sync().
+ */
+void __weak arch_arm_kprobe_nosync(struct kprobe *p)
+{
+ arch_arm_kprobe(p);
+}
+
+void __weak arch_arm_kprobes_sync(void)
+{
+}
+
#ifdef CONFIG_OPTPROBES
/* NOTE: This is protected by 'kprobe_mutex'. */
static bool kprobes_allow_optimization;
@@ -1284,7 +1298,7 @@ static bool kprobe_queued_unopt(struct kprobe *p)
}
/* Put a breakpoint for a probe. */
-static void __arm_kprobe(struct kprobe *p)
+static void __arm_kprobe(struct kprobe *p, bool sync)
{
struct kprobe *_p;
@@ -1296,8 +1310,12 @@ static void __arm_kprobe(struct kprobe *p)
/* Fallback to unoptimized kprobe */
unoptimize_kprobe(_p, true);
- if (!kprobe_queued_unopt(p))
- arch_arm_kprobe(p);
+ if (!kprobe_queued_unopt(p)) {
+ if (sync)
+ arch_arm_kprobe(p);
+ else
+ arch_arm_kprobe_nosync(p);
+ }
optimize_kprobe(p); /* Try to optimize (add kprobe to a list) */
if (unlikely(kprobe_queued_unopt(p)))
unoptimize_kprobe(p, true);
@@ -1354,7 +1372,8 @@ static void __init init_optprobe(void)
#define kill_optimized_kprobe(p) do {} while (0)
#define prepare_optimized_kprobe(p) do {} while (0)
#define try_to_optimize_kprobe(p) do {} while (0)
-#define __arm_kprobe(p) arch_arm_kprobe(p)
+#define __arm_kprobe(p, sync) \
+ ((sync) ? arch_arm_kprobe(p) : arch_arm_kprobe_nosync(p))
#define __disarm_kprobe(p, o) arch_disarm_kprobe(p)
#define kprobe_disarmed(p) kprobe_disabled(p)
#define wait_for_kprobe_optimizer_locked() \
@@ -1499,17 +1518,27 @@ static int prepare_kprobe(struct kprobe *p)
return arch_prepare_kprobe(p);
}
-static int arm_kprobe(struct kprobe *kp)
+/*
+ * Without 'sync' the breakpoint is installed without waiting for other
+ * CPUs and the caller needs to call arch_arm_kprobes_sync() afterwards.
+ * It makes no difference for ftrace based kprobes.
+ */
+static int arm_kprobe_sync(struct kprobe *kp, bool sync)
{
if (unlikely(kprobe_ftrace(kp)))
return arm_kprobe_ftrace(kp);
guard(cpus_read_lock)();
guard(mutex)(&text_mutex);
- __arm_kprobe(kp);
+ __arm_kprobe(kp, sync);
return 0;
}
+static int arm_kprobe(struct kprobe *kp)
+{
+ return arm_kprobe_sync(kp, true);
+}
+
static int disarm_kprobe(struct kprobe *kp, bool reopt)
{
if (unlikely(kprobe_ftrace(kp)))
@@ -2772,14 +2801,17 @@ int disable_kprobe(struct kprobe *kp)
}
EXPORT_SYMBOL_GPL(disable_kprobe);
-/* Enable one kprobe */
-int enable_kprobe(struct kprobe *kp)
+/*
+ * Enable 'kp' and arm its aggregator kprobe 'p' if needed.
+ *
+ * Without 'sync' the caller is responsible for calling
+ * arch_arm_kprobes_sync() when it's done with all the kprobes.
+ */
+static int __enable_kprobe(struct kprobe *kp, bool sync)
{
int ret = 0;
struct kprobe *p;
- guard(mutex)(&kprobe_mutex);
-
/* Check whether specified probe is valid. */
p = __get_valid_kprobe(kp);
if (unlikely(p == NULL))
@@ -2794,7 +2826,7 @@ int enable_kprobe(struct kprobe *kp)
if (!kprobes_all_disarmed && kprobe_disabled(p)) {
p->flags &= ~KPROBE_FLAG_DISABLED;
- ret = arm_kprobe(p);
+ ret = arm_kprobe_sync(p, sync);
if (ret) {
p->flags |= KPROBE_FLAG_DISABLED;
if (p != kp)
@@ -2803,8 +2835,44 @@ int enable_kprobe(struct kprobe *kp)
}
return ret;
}
+
+/* Enable one kprobe */
+int enable_kprobe(struct kprobe *kp)
+{
+ guard(mutex)(&kprobe_mutex);
+
+ return __enable_kprobe(kp, true);
+}
EXPORT_SYMBOL_GPL(enable_kprobe);
+/*
+ * Enable @num kprobes in @kps. The breakpoints are installed for all
+ * of them first and other CPUs are synced just once at the end, instead
+ * of once per kprobe as enable_kprobe() does.
+ *
+ * On error some of the kprobes might be already enabled.
+ */
+int enable_kprobes(struct kprobe **kps, int num)
+{
+ int i, ret = 0;
+
+ if (num <= 0)
+ return -EINVAL;
+
+ guard(mutex)(&kprobe_mutex);
+
+ for (i = 0; i < num; i++) {
+ ret = __enable_kprobe(kps[i], false);
+ if (ret)
+ break;
+ }
+
+ /* Sync also on error, some breakpoints might be already installed. */
+ arch_arm_kprobes_sync();
+ return ret;
+}
+EXPORT_SYMBOL_GPL(enable_kprobes);
+
/* Caller must NOT call this in usual path. This is only for critical case */
void dump_kprobe(struct kprobe *kp)
{
--
2.56.1
next prev parent reply other threads:[~2026-12-02 12:31 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 12:30 [PATCH RFC bpf-next 00/31] bpf: Attach kprobe multi link to inlined functions Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 01/31] x86/ptrace: Add regs_get_dwarf_register function Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 02/31] kprobes: Make the kprobe hash table size configurable Jiri Olsa
2026-12-02 12:30 ` Jiri Olsa [this message]
2026-12-02 12:30 ` [PATCH RFC bpf-next 04/31] module: Add module_sect_addr function Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 05/31] bpf: Add bpf_get_btf_vmlinux_inline function Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 06/31] bpf: Use goto labels for error path in bpf_kprobe_multi_link_attach Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 07/31] bpf: Add support to attach inlined functions with kprobe multi link Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 08/31] bpf: Add cookie support for kprobe multi inlined functions Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 09/31] bpf: Add link info " Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 10/31] bpf: Add fdinfo " Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 11/31] bpf: Add btf_for_each_module_inline function Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 12/31] bpf: Change get_modules_for_addrs to use struct modules_array Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 13/31] tbpf: Add support to attach inlined functions in modules with kprobe multi link Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 14/31] bpf: Add BPF_F_KPROBE_MULTI_INLINE_STRICT flag for " Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 15/31] bpf: Add get_func_arg[_cnt] support for kprobe multi functions Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 16/31] bpf: Add get_func_arg[_cnt] support for kprobe multi inlined functions Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 17/31] bpf: Add log messages for kprobe multi inline attach Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 18/31] bpf: Add inlined functions for kprobe multi inline tests Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 19/31] libbpf: Add inline options to bpf_kprobe_multi_opts Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 20/31] libbpf: Print kernel log for failed kprobe multi attach Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 21/31] bpftool: Display inlined functions for kprobe multi link Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 22/31] selftests/bpf: Add inlined functions to bpf_testmod Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 23/31] selftests/bpf: Add kprobe multi inline attach test Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 24/31] selftests/bpf: Add kprobe multi inline mixed test Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 25/31] selftests/bpf: Add kprobe multi inline skip test Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 26/31] selftests/bpf: Add kprobe multi inline fails test Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 27/31] selftests/bpf: Add kprobe multi inline module test Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 28/31] selftests/bpf: Add kprobe multi inline strict test Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 29/31] selftests/bpf: Add kprobe multi inline args test Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 30/31] selftests/bpf: Add kprobe multi inline bench attach test Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 31/31] selftests/bpf: Add kprobe multi inline link info test Jiri Olsa
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=20261202123035.10335-4-jolsa@kernel.org \
--to=jolsa@kernel.org \
--cc=almagui@kernel.org \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=mhiramat@kernel.org \
--cc=qmo@kernel.org \
--cc=song@kernel.org \
--cc=x86@kernel.org \
--cc=yonghong.song@linux.dev \
/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