[PATCH 1/2] panic: Allow archs to reduce CPU consumption after panic

From: carlos . bilbao
Date: Wed Mar 26 2025 - 11:18:55 EST


From: Carlos Bilbao <carlos.bilbao@xxxxxxxxxx>

After handling a panic, the kernel enters a busy-wait loop, unnecessarily
consuming CPU and potentially impacting other workloads including other
guest VMs in the case of virtualized setups.

Introduce cpu_halt_after_panic(), a weak function that archs can override
for a more efficient halt of CPU work. By default, it preserves the
pre-existing behavior of delay.

Signed-off-by: Carlos Bilbao (DigitalOcean) <carlos.bilbao@xxxxxxxxxx>
Reviewed-by: Jan Glauber (DigitalOcean) <jan.glauber@xxxxxxxxx>
---
kernel/panic.c | 12 +++++++++++-
1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/kernel/panic.c b/kernel/panic.c
index fbc59b3b64d0..fafe3fa22533 100644
--- a/kernel/panic.c
+++ b/kernel/panic.c
@@ -276,6 +276,16 @@ static void panic_other_cpus_shutdown(bool crash_kexec)
crash_smp_send_stop();
}

+/*
+ * Called after a kernel panic has been handled, at which stage halting
+ * the CPU can help reduce unnecessary CPU consumption. In the absence of
+ * arch-specific implementations, just delay
+ */
+static void __weak cpu_halt_after_panic(void)
+{
+ mdelay(PANIC_TIMER_STEP);
+}
+
/**
* panic - halt the system
* @fmt: The text string to print
@@ -474,7 +484,7 @@ void panic(const char *fmt, ...)
i += panic_blink(state ^= 1);
i_next = i + 3600 / PANIC_BLINK_SPD;
}
- mdelay(PANIC_TIMER_STEP);
+ cpu_halt_after_panic();
}
}

--
2.47.1