[PATCH v10 4/4] ftrace: Add an option for tracing console latencies

From: Viktor Rosendahl (BMW)
Date: Fri Oct 18 2019 - 11:09:14 EST


This new kernel parameter "trace_console_latency" will enable the latency
tracers to trace the console latencies. Previously this has always been
implicitely disabled. I guess this is because they are considered to be
well known and unavoidable.

However, for some organizations it may nevertheless be desirable to
trace them. Basically, we want to be able to tell that there are
latencies in the system under test because someone has incorrectly
enabled the serial console.

Signed-off-by: Viktor Rosendahl (BMW) <viktor.rosendahl@xxxxxxxxx>
---
.../admin-guide/kernel-parameters.txt | 4 +++
include/linux/irqflags.h | 22 +++++++++++++
kernel/printk/printk.c | 6 ++--
kernel/trace/trace_irqsoff.c | 32 +++++++++++++++++++
4 files changed, 62 insertions(+), 2 deletions(-)

diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index a84a83f8881e..3f96a380c528 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -4768,6 +4768,10 @@
trace_buf_size=nn[KMG]
[FTRACE] will set tracing buffer size on each cpu.

+ trace_console_latency=[0|1]
+ [FTRACE] Trace the console latencies if 1 is given. Do
+ not trace the latencies if 0 or no parameter is given.
+
trace_event=[event-list]
[FTRACE] Set and start specified trace events in order
to facilitate early boot debugging. The event-list is a
diff --git a/include/linux/irqflags.h b/include/linux/irqflags.h
index 21619c92c377..3de891723331 100644
--- a/include/linux/irqflags.h
+++ b/include/linux/irqflags.h
@@ -13,6 +13,7 @@
#define _LINUX_TRACE_IRQFLAGS_H

#include <linux/typecheck.h>
+#include <linux/types.h>
#include <asm/irqflags.h>

/* Currently trace_softirqs_on/off is used only by lockdep */
@@ -68,9 +69,30 @@ do { \
defined(CONFIG_PREEMPT_TRACER)
extern void stop_critical_timings(void);
extern void start_critical_timings(void);
+ extern bool console_tracing_disabled(void);
+
+# define console_stop_critical_timings(flag) \
+ do { \
+ typecheck(bool, flag); \
+ flag = console_tracing_disabled(); \
+ if (flag) \
+ stop_critical_timings(); \
+ } while (0)
+
+# define console_start_critical_timings(flag) \
+ do { \
+ typecheck(bool, flag); \
+ if (flag) \
+ start_critical_timings(); \
+ } while (0)
+
#else
# define stop_critical_timings() do { } while (0)
# define start_critical_timings() do { } while (0)
+# define console_stop_critical_timings(flag) \
+ do { typecheck(bool, flag); } while (0)
+# define console_start_critical_timings(flag) \
+ do { typecheck(bool, flag); } while (0)
#endif

/*
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index ca65327a6de8..f27e96273453 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -2369,6 +2369,7 @@ void console_unlock(void)
static char ext_text[CONSOLE_EXT_LOG_MAX];
static char text[LOG_LINE_MAX + PREFIX_MAX];
unsigned long flags;
+ bool cflag;
bool do_cond_resched, retry;

if (console_suspended) {
@@ -2469,9 +2470,10 @@ void console_unlock(void)
*/
console_lock_spinning_enable();

- stop_critical_timings(); /* don't trace print latency */
+ /* don't trace print latency if it's disabled */
+ console_stop_critical_timings(cflag);
call_console_drivers(ext_text, ext_len, text, len);
- start_critical_timings();
+ console_start_critical_timings(cflag);

if (console_lock_spinning_disable_and_check()) {
printk_safe_exit_irqrestore(flags);
diff --git a/kernel/trace/trace_irqsoff.c b/kernel/trace/trace_irqsoff.c
index a745b0cee5d3..84876192cc73 100644
--- a/kernel/trace/trace_irqsoff.c
+++ b/kernel/trace/trace_irqsoff.c
@@ -14,6 +14,7 @@
#include <linux/uaccess.h>
#include <linux/module.h>
#include <linux/ftrace.h>
+#include <linux/init.h>
#include <linux/kprobes.h>

#include "trace.h"
@@ -456,6 +457,37 @@ void stop_critical_timings(void)
EXPORT_SYMBOL_GPL(stop_critical_timings);
NOKPROBE_SYMBOL(stop_critical_timings);

+static bool no_console_latency __read_mostly = true;
+
+static int __init trace_console_latency(char *str)
+{
+ int param;
+
+ get_option(&str, &param);
+ no_console_latency = !(param == 1);
+
+ return 0;
+}
+
+early_param("trace_console_latency", trace_console_latency);
+
+bool console_tracing_disabled(void)
+{
+ int pc = preempt_count();
+
+ /*
+ * If tracing is disabled, then the question of whether to trace console
+ * latencies is moot. By always returning false here we save the caller
+ * the calls to start/stop_critical_timings(). These calls would not do
+ * anything anyway.
+ */
+ if (!preempt_trace(pc) && !irq_trace())
+ return false;
+
+ return no_console_latency;
+}
+EXPORT_SYMBOL_GPL(console_tracing_disabled);
+
#ifdef CONFIG_FUNCTION_TRACER
static bool function_enabled;

--
2.17.1