From 6a658cc2d605e5acf35e638c62f480460d5939cd Mon Sep 17 00:00:00 2001 From: "Masami Hiramatsu (Google)" Date: Wed, 4 Nov 2026 08:58:27 +0900 Subject: [PATCH 01/11] kprobes: Fix error return and slot leak in __register_kprobe() When arm_kprobe() fails in __register_kprobe(), the probe is removed from kprobe_table and synchronize_rcu() is executed, but the error code ret was ignored and try_to_optimize_kprobe(p) was executed, returning 0. Additionally, the instruction slot allocated by prepare_kprobe() was leaked. Fix this by releasing the instruction slot via arch_remove_kprobe(p) and returning ret when arm_kprobe() fails. Link: https://lore.kernel.org/all/179375030756.247640.10859474201266455798.stgit@devnote2/ Fixes: 587e8e6d640b ("kprobes: Adopt guard() and scoped_guard()") Cc: stable@vger.kernel.org Signed-off-by: Masami Hiramatsu (Google) --- kernel/kprobes.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kernel/kprobes.c b/kernel/kprobes.c index 2441b9e39343..10946658bb35 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -1946,6 +1946,8 @@ static int __register_kprobe(struct kprobe *p) if (ret) { hlist_del_rcu(&p->hlist); synchronize_rcu(); + arch_remove_kprobe(p); + return ret; } } From 2053bac1d6e47b33fa2e570c87518395e64f7da0 Mon Sep 17 00:00:00 2001 From: "Masami Hiramatsu (Google)" Date: Wed, 4 Nov 2026 08:58:36 +0900 Subject: [PATCH 02/11] kprobes: Fix double free and NULL deref in unregister_kretprobes() In unregister_kretprobes(), if __unregister_kprobe_top() returns an error (e.g., if the kretprobe was not registered or was already unregistered), rethook_free(rps[i]->rh) or rcu_assign_pointer(rps[i]->rph->rp, NULL) is still executed. For rethook-based kretprobes, calling rethook_free() on an already-freed rethook causes a double-free / list corruption in RCU. For legacy kretprobes, rps[i]->rph is NULL after previous cleanup, resulting in a NULL pointer dereference. Skip rethook_free() and rph cleanup if __unregister_kprobe_top() fails, and clear rps[i]->rh after synchronize_rcu() has waited for in-flight pre_handler_kretprobe() invocations to complete. Link: https://lore.kernel.org/all/179375031660.247640.6349602689169856649.stgit@devnote2/ Fixes: 73f9b911faa7 ("kprobes: Use rethook for kretprobe if possible") Cc: stable@vger.kernel.org Signed-off-by: Masami Hiramatsu (Google) --- kernel/kprobes.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/kernel/kprobes.c b/kernel/kprobes.c index 10946658bb35..7a3da809bac7 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -2605,8 +2605,10 @@ void unregister_kretprobes(struct kretprobe **rps, int num) for (i = 0; i < num; i++) { guard(mutex)(&kprobe_mutex); - if (__unregister_kprobe_top(&rps[i]->kp) < 0) + if (__unregister_kprobe_top(&rps[i]->kp) < 0) { rps[i]->kp.addr = NULL; + continue; + } #ifdef CONFIG_KRETPROBE_ON_RETHOOK rethook_free(rps[i]->rh); #else @@ -2618,7 +2620,9 @@ void unregister_kretprobes(struct kretprobe **rps, int num) for (i = 0; i < num; i++) { if (rps[i]->kp.addr) { __unregister_kprobe_bottom(&rps[i]->kp); -#ifndef CONFIG_KRETPROBE_ON_RETHOOK +#ifdef CONFIG_KRETPROBE_ON_RETHOOK + rps[i]->rh = NULL; +#else free_rp_inst(rps[i]); #endif } From a553b499f22bf054964d4fd16d136f3d4f836c23 Mon Sep 17 00:00:00 2001 From: "Masami Hiramatsu (Google)" Date: Wed, 4 Nov 2026 08:58:45 +0900 Subject: [PATCH 03/11] kprobes: Validate re-registration earlier in register_kretprobe() register_kretprobe() only checked for re-registration if rp->kp.addr was specified, skipping the check when rp was registered with symbol_name and rp->kp.addr was NULL. In that scenario, register_kretprobe() proceeded to allocate a new rethook (or kretprobe holder), and when register_kprobe(&rp->kp) subsequently failed due to re-registration, the error path freed and NULL-ed rp->rh (or rp->rph) on the still-active kretprobe, leading to NULL pointer dereferences when the active probe was hit. Resolve the target address via _kprobe_addr() up-front and check whether the probe is already registered at that address via check_kprobe_rereg_at() before allocating any resources, and drop the WARN_ON_ONCE() in the re-registration check now that re-registration is gracefully detected and rejected. Link: https://lore.kernel.org/all/179375032568.247640.17429793187163536192.stgit@devnote2/ Fixes: 0188b87899ff ("kretprobe: Avoid re-registration of the same kretprobe earlier") Cc: stable@vger.kernel.org Signed-off-by: Masami Hiramatsu (Google) --- kernel/kprobes.c | 72 +++++++++++++++++++++++++++--------------------- 1 file changed, 40 insertions(+), 32 deletions(-) diff --git a/kernel/kprobes.c b/kernel/kprobes.c index 7a3da809bac7..0b41d69540ae 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -1793,52 +1793,61 @@ _kprobe_addr(kprobe_opcode_t *addr, const char *symbol_name, return addr; } -static kprobe_opcode_t *kprobe_addr(struct kprobe *p) -{ - bool on_func_entry; - - return _kprobe_addr(p->addr, p->symbol_name, p->offset, &on_func_entry); -} - /* * Check the 'p' is valid and return the aggregator kprobe * at the same address. */ -static struct kprobe *__get_valid_kprobe(struct kprobe *p) +static struct kprobe *__get_valid_kprobe_at(struct kprobe *p, kprobe_opcode_t *addr) { + struct hlist_head *head; struct kprobe *ap, *list_p; lockdep_assert_held(&kprobe_mutex); - ap = get_kprobe(p->addr); - if (unlikely(!ap)) + if (unlikely(!addr)) return NULL; - if (p == ap) - return ap; - - list_for_each_entry(list_p, &ap->list, list) - if (list_p == p) - /* kprobe p is a valid probe */ + head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)]; + hlist_for_each_entry_rcu(ap, head, hlist, + lockdep_is_held(&kprobe_mutex)) { + if (ap == p) { + p->addr = addr; return ap; + } + if (ap->addr == addr && kprobe_aggrprobe(ap)) { + list_for_each_entry(list_p, &ap->list, list) { + if (list_p == p) { + p->addr = addr; + return ap; + } + } + } + } return NULL; } -/* - * Warn and return error if the kprobe is being re-registered since - * there must be a software bug. - */ -static inline int warn_kprobe_rereg(struct kprobe *p) +static inline struct kprobe *__get_valid_kprobe(struct kprobe *p) +{ + return __get_valid_kprobe_at(p, p->addr); +} + +/* Return error if the kprobe is being re-registered */ +static inline int check_kprobe_rereg_at(struct kprobe *p, kprobe_opcode_t *addr) { guard(mutex)(&kprobe_mutex); - if (WARN_ON_ONCE(__get_valid_kprobe(p))) + if (__get_valid_kprobe_at(p, addr)) return -EINVAL; return 0; } +static inline int check_kprobe_rereg(struct kprobe *p) +{ + return check_kprobe_rereg_at(p, p->addr); +} + static int check_ftrace_location(struct kprobe *p) { unsigned long addr = (unsigned long)p->addr; @@ -1969,7 +1978,7 @@ int register_kprobe(struct kprobe *p) return PTR_ERR(addr); p->addr = addr; - ret = warn_kprobe_rereg(p); + ret = check_kprobe_rereg(p); if (ret) return ret; @@ -2505,20 +2514,19 @@ int register_kretprobe(struct kretprobe *rp) int ret; int i; void *addr; + bool on_func_entry; - ret = kprobe_on_func_entry(rp->kp.addr, rp->kp.symbol_name, rp->kp.offset); - if (ret) - return ret; + addr = _kprobe_addr(rp->kp.addr, rp->kp.symbol_name, rp->kp.offset, &on_func_entry); + if (IS_ERR(addr)) + return PTR_ERR(addr); - /* If only 'rp->kp.addr' is specified, check reregistering kprobes */ - if (rp->kp.addr && warn_kprobe_rereg(&rp->kp)) + if (!on_func_entry) + return -EINVAL; + + if (check_kprobe_rereg_at(&rp->kp, addr)) return -EINVAL; if (kretprobe_blacklist_size) { - addr = kprobe_addr(&rp->kp); - if (IS_ERR(addr)) - return PTR_ERR(addr); - for (i = 0; kretprobe_blacklist[i].name != NULL; i++) { if (kretprobe_blacklist[i].addr == addr) return -EINVAL; From 07038c33e791e80c6422c26211fcb9c751439f34 Mon Sep 17 00:00:00 2001 From: "Masami Hiramatsu (Google)" Date: Mon, 2 Nov 2026 16:11:04 +0900 Subject: [PATCH 04/11] samples/kprobes: Use trace_printk() in probe handlers Probe handlers execute in atomic/non-preemptible contexts. Furthermore, unless hit via an unoptimized breakpoint on certain architectures, they do not run with in_nmi() set (e.g. jump-optimized kprobes, ftrace-based kprobes, or kretprobe return handlers). Calling printk() from probe handlers can directly attempt to acquire console and scheduler locks (via console_unlock()), risking a deadlock if the probed context or interrupted code already holds one of those locks. Replace printk() (pr_info) in handler_pre(), handler_post(), and ret_handler() with trace_printk(), and update the comments to direct users to /sys/kernel/tracing/trace. Also update the stack backtrace comment in kprobe_example to reference trace_dump_stack(0). Also fix a minor format string typo ("p->addr," -> "p->addr =") in the S390 pre and post handlers while touching those lines. Additionally, update CONFIG_SAMPLE_KPROBES in samples/Kconfig to depend on EVENT_TRACING so that the tracing subsystem is enabled when building the samples. Link: https://lore.kernel.org/all/179360346488.115681.12990812332058260390.stgit@devnote2/ Reported-by: Tiezhu Yang Closes: https://lore.kernel.org/all/d179086d-78d8-d0e3-e113-9072cffa55f4@loongson.cn/ Signed-off-by: Masami Hiramatsu (Google) --- samples/Kconfig | 2 +- samples/kprobes/kprobe_example.c | 69 +++++++++++++++-------------- samples/kprobes/kretprobe_example.c | 10 ++--- 3 files changed, 41 insertions(+), 40 deletions(-) diff --git a/samples/Kconfig b/samples/Kconfig index 00eed83c07df..54414e57ff75 100644 --- a/samples/Kconfig +++ b/samples/Kconfig @@ -71,7 +71,7 @@ config SAMPLE_KOBJECT config SAMPLE_KPROBES tristate "Build kprobes examples -- loadable modules only" - depends on KPROBES && m + depends on KPROBES && EVENT_TRACING && m help This build several kprobes example modules. diff --git a/samples/kprobes/kprobe_example.c b/samples/kprobes/kprobe_example.c index 53ec6c8b8c40..0f516a2b04be 100644 --- a/samples/kprobes/kprobe_example.c +++ b/samples/kprobes/kprobe_example.c @@ -6,7 +6,7 @@ * For more information on theory of operation of kprobes, see * Documentation/trace/kprobes.rst * - * You will see the trace data in /var/log/messages and on the console + * You will see the trace data in /sys/kernel/tracing/trace * whenever kernel_clone() is invoked to create a new process. */ @@ -15,6 +15,7 @@ #include #include #include +#include static char symbol[KSYM_NAME_LEN] = "kernel_clone"; module_param_string(symbol, symbol, KSYM_NAME_LEN, 0644); @@ -28,39 +29,39 @@ static struct kprobe kp = { static int __kprobes handler_pre(struct kprobe *p, struct pt_regs *regs) { #ifdef CONFIG_X86 - pr_info("<%s> p->addr = 0x%p, ip = %lx, flags = 0x%lx\n", - p->symbol_name, p->addr, regs->ip, regs->flags); + trace_printk("<%s> p->addr = 0x%p, ip = %lx, flags = 0x%lx\n", + p->symbol_name, p->addr, regs->ip, regs->flags); #endif #ifdef CONFIG_PPC - pr_info("<%s> p->addr = 0x%p, nip = 0x%lx, msr = 0x%lx\n", - p->symbol_name, p->addr, regs->nip, regs->msr); + trace_printk("<%s> p->addr = 0x%p, nip = 0x%lx, msr = 0x%lx\n", + p->symbol_name, p->addr, regs->nip, regs->msr); #endif #ifdef CONFIG_MIPS - pr_info("<%s> p->addr = 0x%p, epc = 0x%lx, status = 0x%lx\n", - p->symbol_name, p->addr, regs->cp0_epc, regs->cp0_status); + trace_printk("<%s> p->addr = 0x%p, epc = 0x%lx, status = 0x%lx\n", + p->symbol_name, p->addr, regs->cp0_epc, regs->cp0_status); #endif #ifdef CONFIG_ARM64 - pr_info("<%s> p->addr = 0x%p, pc = 0x%lx, pstate = 0x%lx\n", - p->symbol_name, p->addr, (long)regs->pc, (long)regs->pstate); + trace_printk("<%s> p->addr = 0x%p, pc = 0x%lx, pstate = 0x%lx\n", + p->symbol_name, p->addr, (long)regs->pc, (long)regs->pstate); #endif #ifdef CONFIG_ARM - pr_info("<%s> p->addr = 0x%p, pc = 0x%lx, cpsr = 0x%lx\n", - p->symbol_name, p->addr, (long)regs->ARM_pc, (long)regs->ARM_cpsr); + trace_printk("<%s> p->addr = 0x%p, pc = 0x%lx, cpsr = 0x%lx\n", + p->symbol_name, p->addr, (long)regs->ARM_pc, (long)regs->ARM_cpsr); #endif #ifdef CONFIG_RISCV - pr_info("<%s> p->addr = 0x%p, pc = 0x%lx, status = 0x%lx\n", - p->symbol_name, p->addr, regs->epc, regs->status); + trace_printk("<%s> p->addr = 0x%p, pc = 0x%lx, status = 0x%lx\n", + p->symbol_name, p->addr, regs->epc, regs->status); #endif #ifdef CONFIG_S390 - pr_info("<%s> p->addr, 0x%p, ip = 0x%lx, flags = 0x%lx\n", - p->symbol_name, p->addr, regs->psw.addr, regs->flags); + trace_printk("<%s> p->addr = 0x%p, ip = 0x%lx, flags = 0x%lx\n", + p->symbol_name, p->addr, regs->psw.addr, regs->flags); #endif #ifdef CONFIG_LOONGARCH - pr_info("<%s> p->addr = 0x%p, era = 0x%lx, estat = 0x%lx\n", - p->symbol_name, p->addr, regs->csr_era, regs->csr_estat); + trace_printk("<%s> p->addr = 0x%p, era = 0x%lx, estat = 0x%lx\n", + p->symbol_name, p->addr, regs->csr_era, regs->csr_estat); #endif - /* A dump_stack() here will give a stack backtrace */ + /* A trace_dump_stack(0) here will give a stack backtrace */ return 0; } @@ -69,36 +70,36 @@ static void __kprobes handler_post(struct kprobe *p, struct pt_regs *regs, unsigned long flags) { #ifdef CONFIG_X86 - pr_info("<%s> p->addr = 0x%p, flags = 0x%lx\n", - p->symbol_name, p->addr, regs->flags); + trace_printk("<%s> p->addr = 0x%p, flags = 0x%lx\n", + p->symbol_name, p->addr, regs->flags); #endif #ifdef CONFIG_PPC - pr_info("<%s> p->addr = 0x%p, msr = 0x%lx\n", - p->symbol_name, p->addr, regs->msr); + trace_printk("<%s> p->addr = 0x%p, msr = 0x%lx\n", + p->symbol_name, p->addr, regs->msr); #endif #ifdef CONFIG_MIPS - pr_info("<%s> p->addr = 0x%p, status = 0x%lx\n", - p->symbol_name, p->addr, regs->cp0_status); + trace_printk("<%s> p->addr = 0x%p, status = 0x%lx\n", + p->symbol_name, p->addr, regs->cp0_status); #endif #ifdef CONFIG_ARM64 - pr_info("<%s> p->addr = 0x%p, pstate = 0x%lx\n", - p->symbol_name, p->addr, (long)regs->pstate); + trace_printk("<%s> p->addr = 0x%p, pstate = 0x%lx\n", + p->symbol_name, p->addr, (long)regs->pstate); #endif #ifdef CONFIG_ARM - pr_info("<%s> p->addr = 0x%p, cpsr = 0x%lx\n", - p->symbol_name, p->addr, (long)regs->ARM_cpsr); + trace_printk("<%s> p->addr = 0x%p, cpsr = 0x%lx\n", + p->symbol_name, p->addr, (long)regs->ARM_cpsr); #endif #ifdef CONFIG_RISCV - pr_info("<%s> p->addr = 0x%p, status = 0x%lx\n", - p->symbol_name, p->addr, regs->status); + trace_printk("<%s> p->addr = 0x%p, status = 0x%lx\n", + p->symbol_name, p->addr, regs->status); #endif #ifdef CONFIG_S390 - pr_info("<%s> p->addr, 0x%p, flags = 0x%lx\n", - p->symbol_name, p->addr, regs->flags); + trace_printk("<%s> p->addr = 0x%p, flags = 0x%lx\n", + p->symbol_name, p->addr, regs->flags); #endif #ifdef CONFIG_LOONGARCH - pr_info("<%s> p->addr = 0x%p, estat = 0x%lx\n", - p->symbol_name, p->addr, regs->csr_estat); + trace_printk("<%s> p->addr = 0x%p, estat = 0x%lx\n", + p->symbol_name, p->addr, regs->csr_estat); #endif } diff --git a/samples/kprobes/kretprobe_example.c b/samples/kprobes/kretprobe_example.c index 65d6dcafd742..9dd5cf9a7432 100644 --- a/samples/kprobes/kretprobe_example.c +++ b/samples/kprobes/kretprobe_example.c @@ -14,9 +14,8 @@ * Documentation/trace/kprobes.rst * * Build and insert the kernel module as done in the kprobe example. - * You will see the trace data in /var/log/messages and on the console - * whenever the probed function returns. (Some messages may be suppressed - * if syslogd is configured to eliminate duplicate messages.) + * You will see the trace data in /sys/kernel/tracing/trace + * whenever the probed function returns. */ #include @@ -24,6 +23,7 @@ #include #include #include +#include static char func_name[KSYM_NAME_LEN] = "kernel_clone"; module_param_string(func, func_name, KSYM_NAME_LEN, 0644); @@ -63,8 +63,8 @@ static int ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs) now = ktime_get(); delta = ktime_to_ns(ktime_sub(now, data->entry_stamp)); - pr_info("%s returned %lu and took %lld ns to execute\n", - func_name, retval, (long long)delta); + trace_printk("%s returned %lu and took %lld ns to execute\n", + func_name, retval, (long long)delta); return 0; } NOKPROBE_SYMBOL(ret_handler); From df87101f816083ad5da559f8be8eac1cee94e38f Mon Sep 17 00:00:00 2001 From: "Masami Hiramatsu (Google)" Date: Mon, 2 Nov 2026 16:11:15 +0900 Subject: [PATCH 05/11] samples/fprobe: Use trace_printk() and trace_dump_stack() Fprobe entry and exit callbacks execute in atomic context with preemption disabled under ftrace recursion protection. Calling printk() directly inside handlers can lead to deadlocks if console or scheduler locks are already held by the probed or interrupted context. Drop the use_trace module parameter and always use trace_printk() for handler logging. In addition, replace stack_trace_print() (which uses printk()) with trace_dump_stack(0) to output backtraces safely to the ftrace buffer. Also update the module header comment to reference /sys/kernel/tracing/trace. Additionally, update CONFIG_SAMPLE_FPROBE in samples/Kconfig to depend on EVENT_TRACING so that the tracing subsystem is enabled when building the sample. Link: https://lore.kernel.org/all/179360347512.115681.7201999490892291449.stgit@devnote2/ Signed-off-by: Masami Hiramatsu (Google) --- samples/Kconfig | 2 +- samples/fprobe/fprobe_example.c | 49 +++++++-------------------------- 2 files changed, 11 insertions(+), 40 deletions(-) diff --git a/samples/Kconfig b/samples/Kconfig index 54414e57ff75..744d991ea620 100644 --- a/samples/Kconfig +++ b/samples/Kconfig @@ -88,7 +88,7 @@ config SAMPLE_HW_BREAKPOINT config SAMPLE_FPROBE tristate "Build fprobe examples -- loadable modules only" - depends on FPROBE && m + depends on FPROBE && EVENT_TRACING && m help This builds a fprobe example module. This module has an option 'symbol'. You can specify a probed symbol or symbols separated with ','. diff --git a/samples/fprobe/fprobe_example.c b/samples/fprobe/fprobe_example.c index bfe98ce826f3..3b94839b3199 100644 --- a/samples/fprobe/fprobe_example.c +++ b/samples/fprobe/fprobe_example.c @@ -1,12 +1,12 @@ // SPDX-License-Identifier: GPL-2.0-only /* * Here's a sample kernel module showing the use of fprobe to dump a - * stack trace and selected registers when kernel_clone() is called. + * stack trace and function entry/exit when kernel_clone() is called. * - * For more information on theory of operation of kprobes, see - * Documentation/trace/kprobes.rst + * For more information on theory of operation of fprobe, see + * Documentation/trace/fprobe.rst * - * You will see the trace data in /var/log/messages and on the console + * You will see the trace data in /sys/kernel/tracing/trace * whenever kernel_clone() is invoked to create a new process. */ @@ -15,10 +15,9 @@ #include #include #include -#include #include +#include -#define BACKTRACE_DEPTH 16 #define MAX_SYMBOL_LEN 4096 static struct fprobe sample_probe; static unsigned long nhit; @@ -35,34 +34,14 @@ static bool stackdump = true; module_param(stackdump, bool, 0644); MODULE_PARM_DESC(stackdump, "Enable stackdump."); -static bool use_trace = false; -module_param(use_trace, bool, 0644); -MODULE_PARM_DESC(use_trace, "Use trace_printk instead of printk. This is only for debugging."); - -static void show_backtrace(void) -{ - unsigned long stacks[BACKTRACE_DEPTH]; - unsigned int len; - - len = stack_trace_save(stacks, BACKTRACE_DEPTH, 2); - stack_trace_print(stacks, len, 24); -} - static int sample_entry_handler(struct fprobe *fp, unsigned long ip, unsigned long ret_ip, struct ftrace_regs *fregs, void *data) { - if (use_trace) - /* - * This is just an example, no kernel code should call - * trace_printk() except when actively debugging. - */ - trace_printk("Enter <%pS> ip = 0x%p\n", (void *)ip, (void *)ip); - else - pr_info("Enter <%pS> ip = 0x%p\n", (void *)ip, (void *)ip); + trace_printk("Enter <%pS> ip = 0x%p\n", (void *)ip, (void *)ip); nhit++; if (stackdump) - show_backtrace(); + trace_dump_stack(0); return 0; } @@ -72,19 +51,11 @@ static void sample_exit_handler(struct fprobe *fp, unsigned long ip, { unsigned long rip = ret_ip; - if (use_trace) - /* - * This is just an example, no kernel code should call - * trace_printk() except when actively debugging. - */ - trace_printk("Return from <%pS> ip = 0x%p to rip = 0x%p (%pS)\n", - (void *)ip, (void *)ip, (void *)rip, (void *)rip); - else - pr_info("Return from <%pS> ip = 0x%p to rip = 0x%p (%pS)\n", - (void *)ip, (void *)ip, (void *)rip, (void *)rip); + trace_printk("Return from <%pS> ip = 0x%p to rip = 0x%p (%pS)\n", + (void *)ip, (void *)ip, (void *)rip, (void *)rip); nhit++; if (stackdump) - show_backtrace(); + trace_dump_stack(0); } static int __init fprobe_init(void) From 76a224e04a723af1e2afc185fa0a4b82101f62dd Mon Sep 17 00:00:00 2001 From: "Masami Hiramatsu (Google)" Date: Mon, 2 Nov 2026 16:11:25 +0900 Subject: [PATCH 06/11] docs: trace: Warn against using printk() in probe handlers Probe handlers (kprobes, kretprobes, and fprobes) execute in atomic or non-preemptible contexts. Because handlers do not run in NMI context (especially when jump-optimized or ftrace-based), calling printk() directly can attempt to synchronously acquire console driver locks or scheduler locks (via console_unlock()), risking a deadlock if the probed or interrupted context already holds those locks. Document this restriction in both kprobes.rst and fprobe.rst, and recommend using trace_printk() instead to safely log messages to the ftrace buffer. Link: https://lore.kernel.org/all/179360348516.115681.16358304979974648339.stgit@devnote2/ Signed-off-by: Masami Hiramatsu (Google) --- Documentation/trace/fprobe.rst | 5 +++++ Documentation/trace/kprobes.rst | 11 +++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Documentation/trace/fprobe.rst b/Documentation/trace/fprobe.rst index 46dd46a97c1f..72e0fd9e1808 100644 --- a/Documentation/trace/fprobe.rst +++ b/Documentation/trace/fprobe.rst @@ -101,6 +101,11 @@ handler. If the entry callback function returns !0, the corresponding exit callback will be cancelled. +Since the callbacks run with preemption disabled under ftrace recursion +protection, calling printk() or pr_*() from these callbacks is not safe +and can cause deadlocks on console driver or scheduler locks. Use +trace_printk() instead to log debugging output. + @fp This is the address of `fprobe` data structure related to this handler. You can embed the `fprobe` to your data structure and get it by diff --git a/Documentation/trace/kprobes.rst b/Documentation/trace/kprobes.rst index 5e606730cec6..19c04a91a664 100644 --- a/Documentation/trace/kprobes.rst +++ b/Documentation/trace/kprobes.rst @@ -563,8 +563,8 @@ course, has no way to distinguish the deliberately injected faults from the accidental ones. Don't drink and probe. Kprobes makes no attempt to prevent probe handlers from stepping on -each other -- e.g., probing printk() and then calling printk() from a -probe handler. If a probe handler hits a probe, that second probe's +each other -- e.g., probing trace_printk() and then calling trace_printk() +from a probe handler. If a probe handler hits a probe, that second probe's handlers won't be run in that instance, and the kprobe.nmissed member of the second probe will be incremented. @@ -579,6 +579,13 @@ which depends on the architecture and optimization state. (e.g., kretprobe handlers and optimized kprobe handlers run without interrupt disabled on x86/x86-64). In any case, your handler should not yield the CPU (e.g., by attempting to acquire a semaphore, or waiting I/O). +Furthermore, probe handlers should avoid calling printk() or pr_*(). +Because probe handlers execute in atomic/non-preemptible contexts without +being in NMI context (especially when jump-optimized or ftrace-based), +calling printk() may attempt to synchronously acquire console driver locks +or scheduler locks via console_unlock(), risking a deadlock if the probed +or interrupted context already holds those locks. Use trace_printk() +instead to safely output messages to the trace buffer. Since a return probe is implemented by replacing the return address with the trampoline's address, stack backtraces and calls From 2fc0b3070d6d5dcecfed43cd0e62aa0fd4b4315c Mon Sep 17 00:00:00 2001 From: Yifan Zhang Date: Wed, 4 Nov 2026 17:56:53 +0800 Subject: [PATCH 07/11] tracing/probes: bound symstr stores to reserved length Traceprobe calculates dynamic string sizes before storing the values. A probed pointer can change between those passes, so fetch_store_symstrlen() can reserve space for a short symbol while fetch_store_symstring() observes a longer one. The store path then overwrites the trace record. Format the symbol into a temporary KSYM_SYMBOL_LEN buffer and copy only the bytes that fit in the reserved field, including a trailing NUL. Update __data_loc with the length actually stored so consumers do not read beyond the truncated value. Link: https://lore.kernel.org/all/179378621331.55520.2052332247156241151@stu.pku.edu.cn/ Fixes: b26a124cbfa8 ("tracing/probes: Add symstr type for dynamic events") Signed-off-by: Yifan Zhang Signed-off-by: Masami Hiramatsu (Google) --- kernel/trace/trace_probe_tmpl.h | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/kernel/trace/trace_probe_tmpl.h b/kernel/trace/trace_probe_tmpl.h index 8db12f758fda..8acc0f0ebd7a 100644 --- a/kernel/trace/trace_probe_tmpl.h +++ b/kernel/trace/trace_probe_tmpl.h @@ -87,15 +87,27 @@ fetch_store_symstrlen(unsigned long addr) static nokprobe_inline int fetch_store_symstring(unsigned long addr, void *dest, void *base) { + char namebuf[KSYM_SYMBOL_LEN]; int maxlen = get_loc_len(*(u32 *)dest); void *__dest; + int len; if (unlikely(!maxlen)) return -ENOMEM; __dest = get_loc_data(dest, base); + len = sprint_symbol(namebuf, addr); + if (len < 0) { + *(u32 *)dest = make_data_loc(0, __dest - base); + return 0; + } - return sprint_symbol(__dest, addr); + len = min(len, maxlen - 1); + memcpy(__dest, namebuf, len); + ((char *)__dest)[len] = '\0'; + *(u32 *)dest = make_data_loc(len + 1, __dest - base); + + return len + 1; } /* common part of process_fetch_insn*/ From be94710b1f6ba1bea33e73129c110af80e999b2d Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sat, 7 Nov 2026 10:12:32 +0000 Subject: [PATCH 08/11] tracing/eprobe: Install the trigger for every instance that enables the eprobe The eprobe trigger lives in the instance of the enabling file, but it is only added when the eprobe is first enabled and only removed when the last file goes away. A second instance then records nothing, and the first instance's trigger is left behind once the eprobe is disabled there, so the eprobe can be deleted while that trigger still uses it. Add and remove the trigger for each file. The file is never NULL here, so warn if it is and drop the dead perf paths. Link: https://lore.kernel.org/all/20261107101233.151434-1-devnexen@gmail.com/ Fixes: 7491e2c44278 ("tracing: Add a probe that attaches to trace events") Cc: stable@vger.kernel.org Signed-off-by: David Carlier Signed-off-by: Masami Hiramatsu (Google) --- kernel/trace/trace_eprobe.c | 56 ++++++++++++------------------------- 1 file changed, 18 insertions(+), 38 deletions(-) diff --git a/kernel/trace/trace_eprobe.c b/kernel/trace/trace_eprobe.c index 998e6390937a..474be2f5ccd9 100644 --- a/kernel/trace/trace_eprobe.c +++ b/kernel/trace/trace_eprobe.c @@ -644,37 +644,32 @@ static int enable_trace_eprobe(struct trace_event_call *call, { struct trace_probe *tp; struct trace_eprobe *ep; - bool enabled; int ret = 0; int cnt = 0; + if (WARN_ON_ONCE(!file)) + return -EINVAL; + tp = trace_probe_primary_from_call(call); if (WARN_ON_ONCE(!tp)) return -ENODEV; - enabled = trace_probe_is_enabled(tp); /* This also changes "enabled" state */ - if (file) { - ret = trace_probe_add_file(tp, file); - if (ret) - return ret; - } else - trace_probe_set_flag(tp, TP_FLAG_PROFILE); - - if (enabled) - return 0; + ret = trace_probe_add_file(tp, file); + if (ret) + return ret; + /* The trigger lives in the instance of the file, add it for each file */ for_each_trace_eprobe_tp(ep, tp) { ret = enable_eprobe(ep, file); if (ret) break; - enabled = true; cnt++; } if (ret) { /* Failed to enable one of them. Roll back all */ - if (enabled) { + if (cnt) { /* * It's a bug if one failed for something other than memory * not being available but another eprobe succeeded. @@ -687,10 +682,7 @@ static int enable_trace_eprobe(struct trace_event_call *call, break; } } - if (file) - trace_probe_remove_file(tp, file); - else - trace_probe_clear_flag(tp, TP_FLAG_PROFILE); + trace_probe_remove_file(tp, file); } return ret; @@ -702,33 +694,21 @@ static int disable_trace_eprobe(struct trace_event_call *call, struct trace_probe *tp; struct trace_eprobe *ep; + if (WARN_ON_ONCE(!file)) + return -EINVAL; + tp = trace_probe_primary_from_call(call); if (WARN_ON_ONCE(!tp)) return -ENODEV; - if (file) { - if (!trace_probe_get_file_link(tp, file)) - return -ENOENT; - if (!trace_probe_has_single_file(tp)) - goto out; - trace_probe_clear_flag(tp, TP_FLAG_TRACE); - } else - trace_probe_clear_flag(tp, TP_FLAG_PROFILE); + if (!trace_probe_get_file_link(tp, file)) + return -ENOENT; - if (!trace_probe_is_enabled(tp)) { - for_each_trace_eprobe_tp(ep, tp) - disable_eprobe(ep, file->tr); - } + for_each_trace_eprobe_tp(ep, tp) + disable_eprobe(ep, file->tr); - out: - if (file) - /* - * Synchronization is done in below function. For perf event, - * file == NULL and perf_trace_event_unreg() calls - * tracepoint_synchronize_unregister() to ensure synchronize - * event. We don't need to care about it. - */ - trace_probe_remove_file(tp, file); + /* Synchronization is done in below function. */ + trace_probe_remove_file(tp, file); return 0; } From 0be7a931c5e41dcfdaf297f5cd34def2696b0a7e Mon Sep 17 00:00:00 2001 From: Mani Tofigh Date: Thu, 5 Nov 2026 15:32:12 -0500 Subject: [PATCH 09/11] kprobes: Disarm disabled optprobe on forced unoptimization A disabled optimized probe can be freed with its int3 still in the text. It waits on the unoptimizing list for the optimizer to remove its jump. If another probe is armed on one of the bytes that jump replaced, unoptimize_kprobe() forces the unoptimization and moves the disabled probe straight to a cooling list without removing its int3: Task A (tracefs) Task B (optimizer) disable k1 /* k1 queued to unoptimize */ /* still on an earlier batch */ enable k2 on a byte k1's jump replaced unoptimize_kprobe(k1, true) /* int3 back, k1 on a cooling list */ remove k1 /* k1 freed, int3 left in the text */ The next call into the function then oopses: [ 9.924313] Oops: int3: 0000 [#1] SMP KASAN NOPTI [ 9.925627] CPU: 0 UID: 0 PID: 68 Comm: kprobeopt Tainted: G W 7.3.0-17087-gd865f2677c44 #1 PREEMPT [ 9.930933] RIP: 0010:__do_sys_sysinfo+0x8/0xe0 ... [ 9.949348] Call Trace: [ 9.949890] [ 9.950404] do_syscall_64+0xfb/0x590 [ 9.952027] entry_SYSCALL_64_after_hwframe+0x77/0x7f ... [ 9.966436] ... [ 9.968009] Kernel panic - not syncing: Fatal exception in interrupt Disarm a disabled probe on that path before it goes to the freeing or cooling list, like do_unoptimize_kprobes() does. With the fix, the int3 is gone by the time the probe is freed. This bug was identified and confirmed with the help of a deterministic hypervisor currently under development. The C reproducer below however hits it within about 10s. Link: https://lore.kernel.org/all/20261105-kprobes-optprobe-state-v2-1-7a2f5290baa0@columbia.edu/ Fixes: 06e0d3b89da1 ("kprobes: Make optprobe optimizer multi-generational and asynchronous") Signed-off-by: Mani Tofigh Signed-off-by: Masami Hiramatsu (Google) --- kernel/kprobes.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/kernel/kprobes.c b/kernel/kprobes.c index 0b41d69540ae..2df6f9894c3e 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -1002,9 +1002,10 @@ static void unoptimize_kprobe(struct kprobe *p, bool force) * for cooling down (or freeing if unused). */ force_unoptimize_kprobe(op); + /* Disarm probes if marked disabled and not gone */ + if (kprobe_disabled(&op->kp) && !kprobe_gone(&op->kp)) + arch_disarm_kprobe(&op->kp); if (kprobe_unused(&op->kp)) { - if (!kprobe_gone(&op->kp)) - arch_disarm_kprobe(&op->kp); hlist_del_init_rcu(&op->kp.hlist); list_move(&op->list, &cur->freeing_list); } else { From ce9bc14902d1c27b6c0eac25666e2516e13f2b19 Mon Sep 17 00:00:00 2001 From: Mani Tofigh Date: Thu, 5 Nov 2026 15:32:13 -0500 Subject: [PATCH 10/11] kprobes: Do not disarm unhashed optprobe on dispatch A new probe at the address of a force-unoptimized probe can stop firing. When unoptimize_kprobe() force-unoptimizes an unused probe it disarms and unhashes it, but the next dispatch disarms it again. If a new probe was registered at the same address in the meantime, the second disarm clears the new probe's int3. For a module address the same disarm would write to freed module text after rmmod. Skip the disarm for unhashed probes since they're always disarmed before they're unhashed. With the fix, the new probe fires. Link: https://lore.kernel.org/all/20261105-kprobes-optprobe-state-v2-2-7a2f5290baa0@columbia.edu/ Fixes: 06e0d3b89da1 ("kprobes: Make optprobe optimizer multi-generational and asynchronous") Signed-off-by: Mani Tofigh Signed-off-by: Masami Hiramatsu (Google) --- kernel/kprobes.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/kernel/kprobes.c b/kernel/kprobes.c index 2df6f9894c3e..4b79bc42c57f 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -633,8 +633,12 @@ static void do_unoptimize_kprobes(struct list_head *unopt_list, list_for_each_entry_safe(op, tmp, free_list, list) { /* Switching from detour code to origin */ op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED; - /* Disarm probes if marked disabled and not gone */ - if (kprobe_disabled(&op->kp) && !kprobe_gone(&op->kp)) + /* + * Disarm probes if marked disabled and not gone. Unhashed probes + * were disarmed before they were unhashed. + */ + if (kprobe_disabled(&op->kp) && !kprobe_gone(&op->kp) && + !hlist_unhashed(&op->kp.hlist)) arch_disarm_kprobe(&op->kp); if (kprobe_unused(&op->kp)) { /* From 1ffd6efb1b58e596ed70f660182803264a4c84d9 Mon Sep 17 00:00:00 2001 From: Mani Tofigh Date: Thu, 5 Nov 2026 15:32:14 -0500 Subject: [PATCH 11/11] kprobes: Arm reused optprobe on cooling list A new probe that reuses an unused probe still on a cooling list doesn't fire. reuse_unused_kprobe() clears DISABLED, so register_aggr_kprobe() doesn't arm it and the probe has no int3 until it's optimized again, or ever if it can't be (e.g., it has a post_handler). Keep a reused cooling probe disabled instead, so register_aggr_kprobe() arms it if the new probe is enabled, or enable_kprobe() does it later. The probe is then still unused when add_new_kprobe() force-unoptimizes it for a new probe with a post_handler, which would unhash it and queue it for freeing. Skip that since a cooling probe is already unoptimized. With the fix, the new probe fires. Link: https://lore.kernel.org/all/20261105-kprobes-optprobe-state-v2-3-7a2f5290baa0@columbia.edu/ Fixes: 06e0d3b89da1 ("kprobes: Make optprobe optimizer multi-generational and asynchronous") Suggested-by: Masami Hiramatsu (Google) Signed-off-by: Mani Tofigh Signed-off-by: Masami Hiramatsu (Google) --- kernel/kprobes.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/kernel/kprobes.c b/kernel/kprobes.c index 4b79bc42c57f..dddad1e01866 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -1061,6 +1061,10 @@ static int reuse_unused_kprobe(struct kprobe *ap) */ op = container_of(ap, struct optimized_kprobe, kp); WARN_ON_ONCE(list_empty(&op->list)); + /* Already disarmed, the caller arms it only for an enabled probe */ + if (optprobe_queued_cooling(op)) + return 0; + /* Enable the probe again */ ap->flags &= ~KPROBE_FLAG_DISABLED; /* Optimize it again. (remove from 'op->list') */ @@ -1573,7 +1577,8 @@ void kprobe_busy_end(void) /* Add the new probe to 'ap->list'. */ static int add_new_kprobe(struct kprobe *ap, struct kprobe *p) { - if (p->post_handler) + /* A reused cooling probe is already unoptimized */ + if (p->post_handler && !kprobe_unused(ap)) unoptimize_kprobe(ap, true); /* Fall back to normal kprobe */ list_add_rcu(&p->list, &ap->list);