book3s guards its machine check path against re-entrancy with
paca->in_mce and MAX_MCE_DEPTH. The booke/e500 path has no such limit.

On booke the machine check handler runs with MSR[ME]=0, yet a further
machine check can still be delivered into it. On an e5500 (P50xx) board,
a machine check taken while one was already being handled re-entered
machine_check_exception(); instrumenting the entry showed repeated
entries with MSR[ME]=0. Whether checkstop happens is platform and
firmware dependent, and nothing in the booke path is prepared for the
check being delivered again.

Worse, the booke machine check prolog reloads r1 to the top of the
per-CPU machine check stack on every entry, so a nested check overwrites
the frame still in use instead of growing the stack. A machine check that
interrupts code already running on the machine check stack is therefore
nested.

Detect that from the interrupted stack pointer and go down through
nmi_panic(), which sets up panic state and flushes the per-CPU NMI printk
buffers, instead of re-entering the handler. Do not print anything
before nmi_panic(), as the console lock may be held. nmi_panic() is not
__noreturn, so stop the CPU via nmi_panic_self_stop() if it returns.

Signed-off-by: Jinjie Ruan <[email protected]>
---
v2:
- Fix sashiko issues.
- v1: 
https://sashiko.dev/#/patchset/20261203091314.3662523-1-ruanjinjie%40huawei.com
---
 arch/powerpc/kernel/traps.c | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/arch/powerpc/kernel/traps.c b/arch/powerpc/kernel/traps.c
index 3a8f5416e3f6..05a3a16b09e8 100644
--- a/arch/powerpc/kernel/traps.c
+++ b/arch/powerpc/kernel/traps.c
@@ -804,6 +804,34 @@ void die_mce(const char *str, struct pt_regs *regs, long 
err)
        die(str, regs, err);
 }
 
+/*
+ * A machine check that interrupted code already running on the machine
+ * check stack is nested: the prolog would overwrite the frame still in use.
+ * User mode never runs there.
+ */
+#ifdef CONFIG_BOOKE
+static bool machine_check_is_nested(struct pt_regs *regs)
+{
+       unsigned long sp = regs->gpr[1];
+       unsigned long stack;
+       int cpu;
+
+       if (user_mode(regs))
+               return false;
+
+#if defined(CONFIG_PPC_BOOK3E_64) || !defined(CONFIG_SMP)
+       cpu = raw_smp_processor_id();
+#else
+       cpu = hard_smp_processor_id();
+#endif
+       stack = (unsigned long)mcheckirq_ctx[cpu];
+
+       return stack && sp >= stack && sp < stack + THREAD_SIZE;
+}
+#else
+static bool machine_check_is_nested(struct pt_regs *regs) { return false; }
+#endif
+
 /*
  * BOOK3S_64 does not usually call this handler as a non-maskable interrupt
  * (it uses its own early real-mode handler to handle the MCE proper
@@ -818,6 +846,15 @@ static void __machine_check_exception(struct pt_regs *regs)
 
        inc_irq_stat_and_enable(IRQ_COUNT_MCE);
 
+       if (machine_check_is_nested(regs)) {
+#ifdef CONFIG_PPC_E500
+               nmi_panic(regs, "Nested machine check (MCSR=%lx)", 
mfspr(SPRN_MCSR));
+#else
+               nmi_panic(regs, "Nested machine check");
+#endif
+               nmi_panic_self_stop(regs);      /* nmi_panic() may return */
+       }
+
        add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
 
        /* See if any machine dependent calls. In theory, we would want
-- 
2.34.1


Reply via email to