From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796214673; b=4kSQxJTLOSCuJhmNH44M+oHKdWhu3587ztKoUDZiMROkngp1W+6aJr5qAsIighIMw4G3 kfh9rjpT6v5M/5HO7bMru/zH/PC+o4l0hRdRDJlE1qJLIENcN8SiRL4MK+DevQjXOHQUF AXtrkpyrUj1F0Gb2NuaX/kxM2p0kj6230YQE1U4ZyXCZhk52dWaBY3Fg4vHGW3DMfAeDV Iw+OxWpylHG9wz8u4gj4bbT+ee+xeGENumvIShVBXfKqF9COSQg18sV6O/74eLcYFTKpq Sab4o4w87fOkTK4GtPiV5ue/kTad6X0+V4XHtNP0NHJ2ING9wEBJ/h6SYMUcHf0iPCA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796214673; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=CONch48mLgpFZjboghlTb8bfdKZ4BhheK1jvVeuiGAk=; b=fB8pqcRt4GpxGlrBgzWP/NCp5TJJDPjdB51PjKqKLoO9sEtG1Jworffb533a4ysOCovv aWiUiReVo4vSP/omsgPQedmoxBKH7VV6hVClIveLgnOK+UHaA7GjqH0HMfVGOxK042Vrf 94o7Hdbsl5tYhd2UESJPe7Cw4QFqCcRKJYZ+68B555zA9hWDMfRGNkaug1tlusQpfax00 qkmTFMxAKzc2RFZu3npV/r740D5BsYsajQY4XUAfBBQFVp7bKvJQldkDf7h9KO5uukCI4 LPY2qWzR9I5XyoM2k/kFvPhZj3CoR79+TroG4FAc5T60PFDJ9HLshLCqIPTQVGjtJ/A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=SA5XXQTT; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A6C2A606A31A; Wed, 2 Dec 2026 12:31:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=SA5XXQTT Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3FEB51F00893; Wed, 2 Dec 2026 12:31:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796214673; bh=CONch48mLgpFZjboghlTb8bfdKZ4BhheK1jvVeuiGAk=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=SA5XXQTTXWZ/ucQbRT6zmRiSKnffTAJKCYnzums5KykFiqPfiqkz92omFpSWIMDqe gESs+Qlmf+KY0Zlwn/Ebhnh+4Lh/5AdbWRGwo3xRphetkNTfD+jVEJUTaCRAGEWz+6 yJBRxAOs8wTUfxIMGIX01lEAOKsaftLFC8QmgBv3BU0VfqQl/tKiPpElI/23PKnxe4 BbFclTdpSsXvShdwDdnPxa4AyXRZsmUfjph1rsD/klqtQSxcfxuVrTEl0IH0hIjJXK N9xXQGPM7pQ4uWpY9dCCoWaWuqiXAgHkRW1pwnPwAARSChunWaDv4fHLcCsmej2cLs zS8H/y4qSaucQ== From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Masami Hiramatsu Cc: bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org, Song Liu , Yonghong Song , Quentin Monnet , Alan Maguire 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 Message-ID: <20261202123035.10335-4-jolsa@kernel.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261202123035.10335-1-jolsa@kernel.org> References: <20261202123035.10335-1-jolsa@kernel.org> X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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