Skip to contentSkip to conversation

probes: Use trace_printk() in probe handlers

PATCH In review Masami Hiramatsu (Google) 4 participants 11 messages Nov 2 – Nov 5

Subsystems (3)
lore
Share & tools
lore

Masami Hiramatsu (Google)

author maintainer
#0 lore raw

cover probes: Use trace_printk() in probe handlers

Hi,

Here are patches to update the probe sample code to use trace_printk instead of normal printk. Since the fprobe and kprobe/kretprobe handlers can be called from somewhere where console is locked, using printk() is basically unsafe. Only the unoptimized kprobe on x86 is safe because it is ran with NMI context (in this case, the kernel uses printk_deferred() automatically), but optimized kprobe/kretprobe/fprobe are not.

Thus, this series replaces those printk() (pr_*()) with trace_printk() which should be safe to output the result into trace buffer, and use trace_dump_stack() instead of stack_trace_print().

The similar issue had been reported by Tiezhu Yang [1] in 2023, but it was pointed different reason, and not applicable to upstream. I investigated it again.

[1] https://lore.kernel.org/all/[email protected]/

Thank you,
---
base-commit: bc71431221f5ba8bb33918399552636e059c2c45

Masami Hiramatsu (Google) (3):
samples/kprobes: Use trace_printk() in probe handlers samples/fprobe: Use trace_printk() and trace_dump_stack()
docs: trace: Warn against using printk() in probe handlers

 Documentation/trace/fprobe.rst      |    5 +++
 Documentation/trace/kprobes.rst     |   11 +++++-
 samples/Kconfig                     |    4 +-
 samples/fprobe/fprobe_example.c     |   49 +++++--------------------
 samples/kprobes/kprobe_example.c    |   69 ++++++++++++++++++-----------------
 samples/kprobes/kretprobe_example.c |   10 +++--
 6 files changed, 66 insertions(+), 82 deletions(-)

--
Masami Hiramatsu (Google) <[email protected]>

author maintainer
#1 lore raw

PATCH 1/3 samples/kprobes: Use trace_printk() in probe handlers

From: Masami Hiramatsu (Google) <[email protected]>

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.

---
 samples/Kconfig                     |    2 +
 samples/kprobes/kprobe_example.c    |   69 ++++++++++++++++++-----------------
 samples/kprobes/kretprobe_example.c |   10 +++--
 3 files changed, 41 insertions(+), 40 deletions(-)
samples/Kconfig +1 -1
index 00eed83c07df..54414e57ff75 100644
@@ -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.
 
samples/kprobes/kprobe_example.c +35 -34

123 lines not on this page · raw message

samples/kprobes/kretprobe_example.c +5 -5
index 65d6dcafd742..9dd5cf9a7432 100644
@@ -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 <linux/kernel.h>
@@ -24,6 +23,7 @@
 #include <linux/kprobes.h>
 #include <linux/ktime.h>
 #include <linux/sched.h>
+#include <linux/trace_printk.h>
 
 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);
author maintainer
#2 lore raw

PATCH 2/3 samples/fprobe: Use trace_printk() and trace_dump_stack()

In reply to Masami Hiramatsu (Google)

From: Masami Hiramatsu (Google) <[email protected]>

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.

---
 samples/Kconfig                 |    2 +-
 samples/fprobe/fprobe_example.c |   49 ++++++++-------------------------------
 2 files changed, 11 insertions(+), 40 deletions(-)
samples/Kconfig +1 -1
index 54414e57ff75..744d991ea620 100644
@@ -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 ','.
samples/fprobe/fprobe_example.c +10 -39

85 lines not on this page · raw message

author maintainer
#3 lore raw

PATCH 3/3 docs: trace: Warn against using printk() in probe handlers

In reply to Masami Hiramatsu (Google)

From: Masami Hiramatsu (Google) <[email protected]>

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.

---
 Documentation/trace/fprobe.rst  |    5 +++++
 Documentation/trace/kprobes.rst |   11 +++++++++--
 2 files changed, 14 insertions(+), 2 deletions(-)
Documentation/trace/fprobe.rst +5 -0
index 46dd46a97c1f..72e0fd9e1808 100644
@@ -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
Documentation/trace/kprobes.rst +9 -2
index 5e606730cec6..19c04a91a664 100644
@@ -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
maintainer
#4 lore raw
In reply to Masami Hiramatsu (Google)

[ Cc'ing John Ogness ]

Quoted 8 lines · On Mon, 2 Nov 2026 16:10:54 +0900 "Masami Hiramatsu (Google)"…

Hi,

Here are patches to update the probe sample code to use trace_printk instead of normal printk. Since the fprobe and kprobe/kretprobe handlers can be called from somewhere where console is locked, using printk() is basically unsafe. Only the unoptimized kprobe on x86 is safe because it is ran with NMI context (in this case, the kernel uses printk_deferred() automatically), but optimized kprobe/kretprobe/fprobe are not.

I don't think printk() is unsafe in these situations anymore.

John?

-- Steve

Quoted 31 lines

Thus, this series replaces those printk() (pr_*()) with trace_printk() which should be safe to output the result into trace buffer, and use trace_dump_stack() instead of stack_trace_print().

The similar issue had been reported by Tiezhu Yang [1] in 2023, but it was pointed different reason, and not applicable to upstream. I investigated it again.

[1] https://lore.kernel.org/all/[email protected]/

Thank you,
---
base-commit: bc71431221f5ba8bb33918399552636e059c2c45

Masami Hiramatsu (Google) (3):
samples/kprobes: Use trace_printk() in probe handlers samples/fprobe: Use trace_printk() and trace_dump_stack()
docs: trace: Warn against using printk() in probe handlers

 Documentation/trace/fprobe.rst      |    5 +++
 Documentation/trace/kprobes.rst     |   11 +++++-
 samples/Kconfig                     |    4 +-
 samples/fprobe/fprobe_example.c     |   49 +++++--------------------
 samples/kprobes/kprobe_example.c    |   69 ++++++++++++++++++-----------------
 samples/kprobes/kretprobe_example.c |   10 +++--
 6 files changed, 66 insertions(+), 82 deletions(-)

--
Masami Hiramatsu (Google) <[email protected]>

#5 lore raw

[ Cc'ing Petr Mladek ]

Quoted 1 line · Steven Rostedt

[ Cc'ing John Ogness ]

Thanks.

Quoted 11 lines
Quoted 6 lines · On Mon, 2 Nov 2026 16:10:54 +0900 "Masami Hiramatsu (Google)"…

Here are patches to update the probe sample code to use trace_printk instead of normal printk. Since the fprobe and kprobe/kretprobe handlers can be called from somewhere where console is locked, using printk() is basically unsafe. Only the unoptimized kprobe on x86 is safe because it is ran with NMI context (in this case, the kernel uses printk_deferred() automatically), but optimized kprobe/kretprobe/fprobe are not.

I don't think printk() is unsafe in these situations anymore.

Since we still support legacy consoles, it is true that printk() is not safe in these contexts when a legacy console is registered.

However, the printk_deferred() variant _is_ safe from any context. Once we convert the legacy consoles (including boot consoles) to nbcon, printk_deferred() can become printk() and still be safe from any context.

It is misfortunate to add documentation saying printk() is not safe, since the problem is converting the remaining legacy consoles. But we can cleanup all such documentation later once the legacy consoles are gone.

John

Quoted 9 lines
Quoted 9 lines

Thus, this series replaces those printk() (pr_*()) with trace_printk() which should be safe to output the result into trace buffer, and use trace_dump_stack() instead of stack_trace_print().

The similar issue had been reported by Tiezhu Yang [1] in 2023, but it was pointed different reason, and not applicable to upstream. I investigated it again.

[1] https://lore.kernel.org/all/[email protected]/

author maintainer
#6 lore raw
Quoted 19 lines · John Ogness

[ Cc'ing Petr Mladek ]

Quoted 1 line · Steven Rostedt

[ Cc'ing John Ogness ]

Thanks.

Quoted 11 lines
Quoted 6 lines · On Mon, 2 Nov 2026 16:10:54 +0900 "Masami Hiramatsu (Google)"…

Here are patches to update the probe sample code to use trace_printk instead of normal printk. Since the fprobe and kprobe/kretprobe handlers can be called from somewhere where console is locked, using printk() is basically unsafe. Only the unoptimized kprobe on x86 is safe because it is ran with NMI context (in this case, the kernel uses printk_deferred() automatically), but optimized kprobe/kretprobe/fprobe are not.

I don't think printk() is unsafe in these situations anymore.

Quoted 7 lines

Since we still support legacy consoles, it is true that printk() is not safe in these contexts when a legacy console is registered.

However, the printk_deferred() variant _is_ safe from any context. Once we convert the legacy consoles (including boot consoles) to nbcon, printk_deferred() can become printk() and still be safe from any context.

Got it. But since printk_deferred() is not exposed to modules, these sample modules can not use it. (printk_deferred_enter/exit() are also not exposed.)

Quoted 5 lines

It is misfortunate to add documentation saying printk() is not safe, since the problem is converting the remaining legacy consoles. But we can cleanup all such documentation later once the legacy consoles are gone.

So, until it is done, this series is valid. Please let me know it. After that version, I can revert these.

Thanks,

Quoted 13 lines

John

Quoted 9 lines
Quoted 9 lines

Thus, this series replaces those printk() (pr_*()) with trace_printk() which should be safe to output the result into trace buffer, and use trace_dump_stack() instead of stack_trace_print().

The similar issue had been reported by Tiezhu Yang [1] in 2023, but it was pointed different reason, and not applicable to upstream. I investigated it again.

[1] https://lore.kernel.org/all/[email protected]/

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
#7 lore raw
Quoted 23 lines · Masami Hiramatsu (Google)
Quoted 22 lines · John Ogness

[ Cc'ing Petr Mladek ]

Quoted 1 line · Steven Rostedt

[ Cc'ing John Ogness ]

Thanks.

Quoted 11 lines
Quoted 6 lines · On Mon, 2 Nov 2026 16:10:54 +0900 "Masami Hiramatsu (Google)"…

Here are patches to update the probe sample code to use trace_printk instead of normal printk. Since the fprobe and kprobe/kretprobe handlers can be called from somewhere where console is locked, using printk() is basically unsafe. Only the unoptimized kprobe on x86 is safe because it is ran with NMI context (in this case, the kernel uses printk_deferred() automatically), but optimized kprobe/kretprobe/fprobe are not.

I don't think printk() is unsafe in these situations anymore.

Since we still support legacy consoles, it is true that printk() is not safe in these contexts when a legacy console is registered.

Quoted 8 lines
Quoted 4 lines

However, the printk_deferred() variant _is_ safe from any context. Once we convert the legacy consoles (including boot consoles) to nbcon, printk_deferred() can become printk() and still be safe from any context.

Got it. But since printk_deferred() is not exposed to modules, these sample modules can not use it. (printk_deferred_enter/exit() are also not exposed.)

We could export it if it would be useful for kprobes...

Quoted 7 lines
Quoted 4 lines

It is misfortunate to add documentation saying printk() is not safe, since the problem is converting the remaining legacy consoles. But we can cleanup all such documentation later once the legacy consoles are gone.

So, until it is done, this series is valid. Please let me know it. After that version, I can revert these.

I do not have strong opinion here.

It just looks to me that using trace_printk() everywhere is an overkill. It is true that printk() is not safe when probing some locations, e.g. where a console is locked. But these are locations where you could not use printk() even in the probed code.

I believe that trace_printk() has a similar problem. You could not use it inside the code used by trace_printk() because it might cause an unbound recursion.

I agree that the scope of the code used by trace_printk() is smaller than the code used by console drivers. Also it is better located. And maybe, the recursion can be even detected and handled.

But still, the point is that even trace_printk() is not a 100% safe cure. So, maybe, instead of proposing trace_printk() everywhere, it might be enough to propose it only for probing the printk/console related code. Well, I know that it involves also some less obvious areas, for example, irq_work, some timer and scheduler code.

That said, I do not want to block this patchset. It is solving some real problems...

Best Regards,
Petr

maintainer
#8 lore raw
Quoted 3 lines · On Tue, 3 Nov 2026 12:11:47 +0100 Petr Mladek <[email protected]> wrote:

I believe that trace_printk() has a similar problem. You could not use it inside the code used by trace_printk() because it might cause an unbound recursion.

That is slightly incorrect. I use trace_printk() to debug tracing! ;-)

Well, adding trace_printk() to the trace_printk() function itself will have issues, but the ring buffer logic allows trace_printk() to be injected into it. The ring buffer has recursion protection, so adding trace_printk() into the ftrace ring buffer will print the trace_printk(). You may need to add a way to not do the trace_printk() again, otherwise you may get multiple prints until the nested limit is reached.

But I just wanted to point out that I do use trace_printk() to debug the ring buffer even though trace_printk itself uses the ring buffer too.

-- Steve

author maintainer
#9 lore raw
Quoted 16 lines · Steven Rostedt
Quoted 3 lines · On Tue, 3 Nov 2026 12:11:47 +0100 Petr Mladek <[email protected]> wrote:

I believe that trace_printk() has a similar problem. You could not use it inside the code used by trace_printk() because it might cause an unbound recursion.

That is slightly incorrect. I use trace_printk() to debug tracing! ;-)

Well, adding trace_printk() to the trace_printk() function itself will have issues, but the ring buffer logic allows trace_printk() to be injected into it. The ring buffer has recursion protection, so adding trace_printk() into the ftrace ring buffer will print the trace_printk(). You may need to add a way to not do the trace_printk() again, otherwise you may get multiple prints until the nested limit is reached.

Quoted 2 lines

But I just wanted to point out that I do use trace_printk() to debug the ring buffer even though trace_printk itself uses the ring buffer too.

Yes, so for short term solution to fix the problem, I would like to use trace_printk() until we ensure to check the printk is safe. After removing legacy console driver (or introducing a Kconfig to ensure no legacy driver) I will revisit this.

Thank you,

Quoted 3 lines

-- Steve

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
author maintainer
#10 lore raw
In reply to Petr Mladek
Quoted 29 lines · Petr Mladek
Quoted 28 lines · Masami Hiramatsu (Google)
Quoted 26 lines · John Ogness

[ Cc'ing Petr Mladek ]

Quoted 1 line · Steven Rostedt

[ Cc'ing John Ogness ]

Thanks.

Quoted 11 lines
Quoted 6 lines · On Mon, 2 Nov 2026 16:10:54 +0900 "Masami Hiramatsu (Google)"…

Here are patches to update the probe sample code to use trace_printk instead of normal printk. Since the fprobe and kprobe/kretprobe handlers can be called from somewhere where console is locked, using printk() is basically unsafe. Only the unoptimized kprobe on x86 is safe because it is ran with NMI context (in this case, the kernel uses printk_deferred() automatically), but optimized kprobe/kretprobe/fprobe are not.

I don't think printk() is unsafe in these situations anymore.

Since we still support legacy consoles, it is true that printk() is not safe in these contexts when a legacy console is registered.

However, the printk_deferred() variant _is_ safe from any context. Once we convert the legacy consoles (including boot consoles) to nbcon, printk_deferred() can become printk() and still be safe from any context.

Quoted 5 lines
Quoted 3 lines

Got it. But since printk_deferred() is not exposed to modules, these sample modules can not use it. (printk_deferred_enter/exit() are also not exposed.)

We could export it if it would be useful for kprobes...

I also concern that exposing those means exposing internal implementation. If so, I would like to avoid it, because if printk() becomes safe, those simply become technical debt.

Quoted 9 lines
Quoted 8 lines
Quoted 5 lines

It is misfortunate to add documentation saying printk() is not safe, since the problem is converting the remaining legacy consoles. But we can cleanup all such documentation later once the legacy consoles are gone.

So, until it is done, this series is valid. Please let me know it. After that version, I can revert these.

Quoted 6 lines

I do not have strong opinion here.

It just looks to me that using trace_printk() everywhere is an overkill. It is true that printk() is not safe when probing some locations, e.g. where a console is locked. But these are locations where you could not use printk() even in the probed code.

Ah, sorry if I caused a misunderstanding. What I meant was that for callbacks — which might be invoked in critical contexts, such as those used by kprobe or fprobe — we should use trace_printk() that include recursion detection. This does not mean that trace_printk should be used everywhere.

Thank you,

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>