Skip to contentSkip to conversation

RISC-V: Add fixup to support fast call of crash_kexec()

PATCH Superseded Xianting Tian 3 participants 5 messages Jun 6, 2022 – Jun 17, 2022 RISC-V

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 RISC-V: Add fixup to support fast call of crash_kexec()

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/test_crash.c:81
	81             *(int *)p = 0xdead;
	(gdb)
	(gdb) bt
	#0  0xffffffe00064d5c0 in test_thread (data=<optimized out>) at drivers/test_crash.c:81
	#1  0x0000000000000000 in ?? ()

Test code to produce NULL address dereference in test_crash.c,

	void *p = NULL;
	*(int *)p = 0xdead;
Fixes 76d2a0493a17 ("RISC-V: Init and Halt Code")

---
Changes from v1:
- simplify the commit message
Changes from v2:
- add fixup in title

---
 arch/riscv/kernel/traps.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/riscv/kernel/traps.c +4 -0
index b40426509244..39d0f8bba4b4 100644
@@ -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);
Signature · 1 line
2.17.1
Reviewed-by on v3 1/1
#1 lore raw
Quoted 54 lines · Xianting Tian

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/test_crash.c:81
	81             *(int *)p = 0xdead;
	(gdb)
	(gdb) bt
	#0  0xffffffe00064d5c0 in test_thread (data=<optimized out>) at drivers/test_crash.c:81
	#1  0x0000000000000000 in ?? ()

Test code to produce NULL address dereference in test_crash.c,

	void *p = NULL;
	*(int *)p = 0xdead;
Fixes 76d2a0493a17 ("RISC-V: Init and Halt Code")

---
Changes from v1:
- simplify the commit message
Changes from v2:
- add fixup in title

---
  arch/riscv/kernel/traps.c | 4 ++++
  1 file changed, 4 insertions(+)
arch/riscv/kernel/traps.c +1 -0
index b40426509244..39d0f8bba4b4 100644
@@ -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>

arch/riscv/kernel/traps.c void die(struct pt_regs *regs, const char *str) · line 50

Quoted 6 lines
arch/riscv/kernel/traps.c +3 -0
@@ -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);
+

It seems that the regs won't be null, right? except that,

Quoted 3 lines
arch/riscv/kernel/traps.c +0 -0
  	bust_spinlocks(0);
  	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  	spin_unlock_irq(&die_lock);
#2 lore raw
Quoted 53 lines · Kefeng Wang
Quoted 51 lines · Xianting Tian

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/test_crash.c:81
    81             *(int *)p = 0xdead;
    (gdb)
    (gdb) bt
    #0  0xffffffe00064d5c0 in test_thread (data=<optimized out>) at drivers/test_crash.c:81
    #1  0x0000000000000000 in ?? ()

Test code to produce NULL address dereference in test_crash.c, void *p = NULL;
    *(int *)p = 0xdead;

Fixes 76d2a0493a17 ("RISC-V: Init and Halt Code")

---
Changes from v1:
- simplify the commit message
Changes from v2:
- add fixup in title
---
  arch/riscv/kernel/traps.c | 4 ++++

  1 file changed, 4 insertions(+)
arch/riscv/kernel/traps.c +1 -0
index b40426509244..39d0f8bba4b4 100644
@@ -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>
Quoted 8 lines

arch/riscv/kernel/traps.c void die(struct pt_regs *regs, const char *str) · line 48

Quoted 6 lines
arch/riscv/kernel/traps.c +2 -0
   #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);
+

It seems that the regs won't be null, right? except that,

Autually both regs won't be null, But if it is triggered by panic() , the regs are got via riscv_crash_save_regs(), which are the regs of that moment, but not the real accident scene.

Quoted 6 lines
Quoted 3 lines
arch/riscv/kernel/traps.c +0 -0
       bust_spinlocks(0);
       add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
       spin_unlock_irq(&die_lock);
#3 lore raw

Hi Palmer

Will you apply this patch for 5.19?

thanks

Quoted 72 lines · Xianting Tian
Quoted 53 lines · Kefeng Wang
Quoted 51 lines · Xianting Tian

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/test_crash.c:81
    81             *(int *)p = 0xdead;
    (gdb)
    (gdb) bt
    #0  0xffffffe00064d5c0 in test_thread (data=<optimized out>) at drivers/test_crash.c:81
    #1  0x0000000000000000 in ?? ()

Test code to produce NULL address dereference in test_crash.c, void *p = NULL;
    *(int *)p = 0xdead;

Fixes 76d2a0493a17 ("RISC-V: Init and Halt Code")

---
Changes from v1:
- simplify the commit message
Changes from v2:
- add fixup in title
---
  arch/riscv/kernel/traps.c | 4 ++++

  1 file changed, 4 insertions(+)
arch/riscv/kernel/traps.c +1 -0
index b40426509244..39d0f8bba4b4 100644
@@ -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>
Quoted 8 lines

arch/riscv/kernel/traps.c void die(struct pt_regs *regs, const char *str) · line 48

Quoted 6 lines
arch/riscv/kernel/traps.c +2 -0
   #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);
+

It seems that the regs won't be null, right? except that,

Autually both regs won't be null, But if it is triggered by panic() , the regs are got via riscv_crash_save_regs(), which are the regs of that moment, but not the real accident scene.

Quoted 6 lines
Quoted 3 lines
arch/riscv/kernel/traps.c +0 -0
       bust_spinlocks(0);
       add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
       spin_unlock_irq(&die_lock);
#4 lore raw

Hi Xianting,

Quoted 4 lines · Xianting Tian

Hi Palmer

Will you apply this patch for 5.19?

Maybe we could update tile with [PATCH v3] RISC-V: Fixup fast call of crash_kexec() for a V4.

It's a fixup not feature, that would mislead maintainer. And add "Reviewed-by: Kefeng Wang".

Quoted 80 lines

thanks

Quoted 76 lines · Xianting Tian
Quoted 55 lines · Kefeng Wang
Quoted 53 lines · Xianting Tian

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/test_crash.c:81
    81             *(int *)p = 0xdead;
    (gdb)
    (gdb) bt
    #0  0xffffffe00064d5c0 in test_thread (data=<optimized out>) at
drivers/test_crash.c:81
    #1  0x0000000000000000 in ?? ()

Test code to produce NULL address dereference in test_crash.c,

    void *p = NULL;
    *(int *)p = 0xdead;
Fixes 76d2a0493a17 ("RISC-V: Init and Halt Code")

---
Changes from v1:
- simplify the commit message
Changes from v2:
- add fixup in title

---
  arch/riscv/kernel/traps.c | 4 ++++
  1 file changed, 4 insertions(+)
arch/riscv/kernel/traps.c +1 -0
index b40426509244..39d0f8bba4b4 100644
@@ -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>
Quoted 8 lines

arch/riscv/kernel/traps.c void die(struct pt_regs *regs, const char *str) · line 48

Quoted 6 lines
arch/riscv/kernel/traps.c +2 -0
  #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);
+

It seems that the regs won't be null, right? except that,

Autually both regs won't be null, But if it is triggered by panic() , the regs are got via riscv_crash_save_regs(), which are the regs of that moment, but not the real accident scene.

Quoted 6 lines
Quoted 3 lines
arch/riscv/kernel/traps.c +0 -0
      bust_spinlocks(0);
      add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
      spin_unlock_irq(&die_lock);
Signature · 4 lines
Best Regards
 Guo Ren

ML: https://lore.kernel.org/linux-csky/