[PATCH] RISC-V: Add fast call path of crash_kexec()

From: Xianting Tian
Date: Sun May 15 2022 - 09:14:27 EST


Currently, almost all archs (x86, arm64, mips...) support fast call
of crash_kexec() when "regs && kexec_should_crash()" is true. But
RISC-V not, it can only enter crash system via panic(). However panic()
doesn't pass the regs of the real accident scene to crash_kexec(),
it caused we can't get accurate backtrace via gdb,
$ riscv64-linux-gnu-gdb vmlinux vmcore
Reading symbols from vmlinux...
[New LWP 95]
#0 console_unlock () at kernel/printk/printk.c:2557
2557 if (do_cond_resched)
(gdb) bt
#0 console_unlock () at kernel/printk/printk.c:2557
#1 0x0000000000000000 in ?? ()

With the patch we can get the accurate backtrace,
$ riscv64-linux-gnu-gdb vmlinux vmcore
Reading symbols from vmlinux...
[New LWP 95]
#0 0xffffffe00063a4e0 in test_thread (data=<optimized out>) at drivers/virtio/virtio_mmio.c:806
806 *(int *)p = 0xdead;
(gdb)
(gdb) bt
#0 0xffffffe00063a4e0 in test_thread (data=<optimized out>) at drivers/virtio/virtio_mmio.c:806
#1 0x0000000000000000 in ?? ()

Test code to produce NULL address dereference,
+extern int panic_on_oops;
+static struct task_struct *k;
+static int test_thread(void *data) {
+
+ void *p = NULL;
+
+ while (!panic_on_oops)
+ msleep(2000);
+
+ *(int *)p = 0xdead;
+
+ return 0;
+}
+
static int __init virtio_mmio_init(void)
{
+ k = kthread_run(test_thread, NULL, "test_thread");
+ if (IS_ERR(k))
+ pr_err("Couldn't create test kthread\n");
+
return platform_driver_register(&virtio_mmio_driver);
}

Signed-off-by: Xianting Tian <xianting.tian@xxxxxxxxxxxxxxxxx>
---
arch/riscv/kernel/traps.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c
index fe92e119e6a3..e666ebfa2a64 100644
--- a/arch/riscv/kernel/traps.c
+++ b/arch/riscv/kernel/traps.c
@@ -16,6 +16,7 @@
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/irq.h>
+#include <linux/kexec.h>

#include <asm/asm-prototypes.h>
#include <asm/bug.h>
@@ -44,6 +45,9 @@ void die(struct pt_regs *regs, const char *str)

ret = notify_die(DIE_OOPS, str, regs, 0, regs->cause, SIGSEGV);

+ if (regs && kexec_should_crash(current))
+ crash_kexec(regs);
+
bust_spinlocks(0);
add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
spin_unlock_irq(&die_lock);
--
2.17.1