[PATCH] arm64: traps: add tracepoints for unusal exception cases

From: Sangmoon Kim
Date: Fri Mar 05 2021 - 07:46:30 EST


When kernel panic occurs, a kernel module can use either the
panic_notifier or die_notifier to obtain the debugging information.

However, in case of these exceptions like do_undefinstr(), regs and
esr data are not passed on. Although a module might be able to find
those data in the console messages, parsing text messages is very
expensive behavior for a module especially on mobile devices.

These bare tracepoints allow a module to probe regs and esr information
for debugging purpose. _tp suffix comes from bare tracepoints of
sched/core.c

Signed-off-by: Sangmoon Kim <sangmoon.kim@xxxxxxxxxxx>
---
arch/arm64/kernel/traps.c | 15 +++++++++++++
include/trace/events/traps.h | 41 ++++++++++++++++++++++++++++++++++++
2 files changed, 56 insertions(+)
create mode 100644 include/trace/events/traps.h

diff --git a/arch/arm64/kernel/traps.c b/arch/arm64/kernel/traps.c
index a05d34f0e82a..e58797743442 100644
--- a/arch/arm64/kernel/traps.c
+++ b/arch/arm64/kernel/traps.c
@@ -45,6 +45,17 @@
#include <asm/system_misc.h>
#include <asm/sysreg.h>

+#define CREATE_TRACE_POINTS
+#include <trace/events/traps.h>
+
+/*
+ * Export tracepoints that act as a bare tracehook (ie: have no trace event
+ * associated with them) to allow external modules to probe them.
+ */
+EXPORT_TRACEPOINT_SYMBOL_GPL(traps_undefinstr_tp);
+EXPORT_TRACEPOINT_SYMBOL_GPL(traps_bad_mode_tp);
+EXPORT_TRACEPOINT_SYMBOL_GPL(traps_serror_panic_tp);
+
static const char *handler[] = {
"Synchronous Abort",
"IRQ",
@@ -403,6 +414,8 @@ void do_undefinstr(struct pt_regs *regs)
if (call_undef_hook(regs) == 0)
return;

+ if (!user_mode(regs))
+ trace_traps_undefinstr_tp(regs, 0);
BUG_ON(!user_mode(regs));
force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
}
@@ -766,6 +779,7 @@ asmlinkage void notrace bad_mode(struct pt_regs *regs, int reason, unsigned int

__show_regs(regs);
local_daif_mask();
+ trace_traps_bad_mode_tp(regs, esr);
panic("bad mode");
}

@@ -832,6 +846,7 @@ void __noreturn arm64_serror_panic(struct pt_regs *regs, u32 esr)
if (regs)
__show_regs(regs);

+ trace_traps_serror_panic_tp(regs, esr);
nmi_panic(regs, "Asynchronous SError Interrupt");

cpu_park_loop();
diff --git a/include/trace/events/traps.h b/include/trace/events/traps.h
new file mode 100644
index 000000000000..b4f53db90ce7
--- /dev/null
+++ b/include/trace/events/traps.h
@@ -0,0 +1,41 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM traps
+
+#if !defined(_TRACE_TRAPS_H) || defined(TRACE_HEADER_MULTI_READ)
+#define _TRACE_TRAPS_H
+
+#include <linux/tracepoint.h>
+
+/*
+ * Following tracepoints are not exported in tracefs and provide hooking
+ * mechanisms only for testing and debugging purposes.
+ *
+ * Postfixed with _tp to make them easily identifiable in the code.
+ */
+
+/*
+ * Tracepoint for unhandled undefined exception at EL1.
+ */
+DECLARE_TRACE(traps_undefinstr_tp,
+ TP_PROTO(struct pt_regs *regs, unsigned int esr),
+ TP_ARGS(regs, esr));
+
+/*
+ * Tracepoint for bad_mode for the impossible case in the exception vector.
+ */
+DECLARE_TRACE(traps_bad_mode_tp,
+ TP_PROTO(struct pt_regs *regs, unsigned int esr),
+ TP_ARGS(regs, esr));
+
+/*
+ * Tracepoint for unhandled SError exception.
+ */
+DECLARE_TRACE(traps_serror_panic_tp,
+ TP_PROTO(struct pt_regs *regs, unsigned int esr),
+ TP_ARGS(regs, esr));
+
+#endif /* _TRACE_TRAPS_H */
+
+/* This part must be outside protection */
+#include <trace/define_trace.h>
--
2.17.1