Re: [PATCH -printk] printk, tracing: fix console tracepoint

From: Steven Rostedt
Date: Thu Jul 14 2022 - 11:17:58 EST


On Thu, 14 Jul 2022 16:53:24 +0200
Petr Mladek <pmladek@xxxxxxxx> wrote:

> --- a/kernel/printk/printk.c
> +++ b/kernel/printk/printk.c
> @@ -2108,7 +2108,15 @@ static u16 printk_sprint(char *text, u16 size, int facility,
> }
> }
>
> - trace_console_rcuidle(text, text_len);
> + /*
> + * trace_console_idle() is not working in NMI. printk()
> + * is used more often in NMI than in rcuidle context.
> + * Choose the less evil solution here.
> + *
> + * smp_processor_id() is reliable in rcuidle context.
> + */
> + if (!rcu_is_idle_cpu(smp_processor_id()))
> + trace_console(text, text_len);
>
> return text_len;
> }
> --

Although printk is not really a fast path, you could do this and avoid the
check when the trace event is not active:

(Not even compiled tested)

Tweaked the comment, and used raw_smp_processor_id() as I'm not sure we are
in a preempt disabled context, and we don't care if we are not.

-- Steve

diff --git a/include/trace/events/printk.h b/include/trace/events/printk.h
index 13d405b2fd8b..d0a5f63920bb 100644
--- a/include/trace/events/printk.h
+++ b/include/trace/events/printk.h
@@ -7,11 +7,20 @@

#include <linux/tracepoint.h>

-TRACE_EVENT(console,
+TRACE_EVENT_CONDITION(console,
TP_PROTO(const char *text, size_t len),

TP_ARGS(text, len),

+ /*
+ * trace_console_rcuidle() is not working in NMI. printk()
+ * is used more often in NMI than in rcuidle context.
+ * Choose the less evil solution here.
+ *
+ * raw_smp_processor_id() is reliable in rcuidle context.
+ */
+ TP_CONDITION(!rcu_is_idle_cpu(raw_smp_processor_id())),
+
TP_STRUCT__entry(
__dynamic_array(char, msg, len + 1)
),