[PATCH v2] panic: add panic_force_cpu= parameter to redirect panic to a specific CPU

Pnina Feder posted 1 patch 2 days ago
There is a newer version of this series
kernel/panic.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 93 insertions(+)
[PATCH v2] panic: add panic_force_cpu= parameter to redirect panic to a specific CPU
Posted by Pnina Feder 2 days ago
Some platforms require panic handling to execute on a specific CPU for
crash dump to work reliably. This can be due to firmware limitations,
interrupt routing constraints, or platform-specific requirements where
only a single CPU is able to safely enter the crash kernel.

Add support for redirecting panic execution to a designated CPU via a
kernel command-line parameter. When the parameter is provided, the CPU
that initially triggers panic forwards the panic context to the target
CPU, which then proceeds with the normal panic and kexec flow.

If the specified CPU is invalid, offline, or a panic is already in
progress on another CPU, the redirection is skipped and panic continues
on the current CPU.

Changes since v1:
 - Replace Kconfig option with a kernel command-line parameter
 - Fix clang format warning reported by kernel test robot

Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/oe-kbuild-all/202601041820.6M8cIq2e-lkp@intel.com/
Signed-off-by: Pnina Feder <pnina.feder@mobileye.com>
---
 kernel/panic.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 93 insertions(+)

diff --git a/kernel/panic.c b/kernel/panic.c
index 0d52210a9e2b..fb48d14d2b44 100644
--- a/kernel/panic.c
+++ b/kernel/panic.c
@@ -300,6 +300,92 @@ void __weak crash_smp_send_stop(void)
 
 atomic_t panic_cpu = ATOMIC_INIT(PANIC_CPU_INVALID);
 
+#ifdef CONFIG_SMP
+/* CPU to redirect panic to, -1 means feature is disabled */
+static int panic_force_cpu = -1;
+
+static int __init panic_force_cpu_setup(char *str)
+{
+	int cpu;
+
+	if (!str)
+		return -EINVAL;
+
+	if (kstrtoint(str, 0, &cpu) || cpu < 0) {
+		pr_warn("panic_force_cpu: invalid value '%s'\n", str);
+		return -EINVAL;
+	}
+
+	panic_force_cpu = cpu;
+	pr_info("panic_force_cpu: panic will execute on CPU %d\n", cpu);
+	return 0;
+}
+early_param("panic_force_cpu", panic_force_cpu_setup);
+
+static void do_panic_on_target_cpu(void *info)
+{
+	panic("%s", (char *)info);
+}
+
+/**
+ * panic_force_target_cpu - Redirect panic to a specific CPU for crash kernel
+ * @fmt: panic message format string
+ * @args: arguments for format string
+ *
+ * Some platforms require panic handling to occur on a specific CPU
+ * for the crash kernel to function correctly. This function redirects
+ * panic handling to the CPU specified via the panic_redirect_cpu= boot parameter.
+ *
+ * Returns true if panic should proceed on current CPU.
+ * Returns false (never returns) if panic was redirected.
+ */
+__printf(1, 0)
+static bool panic_force_target_cpu(const char *fmt, va_list args)
+{
+	static char panic_redirect_msg[1024];
+	int cpu = raw_smp_processor_id();
+	int target_cpu = panic_force_cpu;
+
+	/* Feature not enabled via boot parameter */
+	if (target_cpu < 0)
+		return true;
+
+	/* Already on target CPU - proceed normally */
+	if (cpu == target_cpu)
+		return true;
+
+	/* Target CPU is offline, can't redirect */
+	if (!cpu_online(target_cpu)) {
+		pr_warn("panic: target CPU %d is offline, proceeding on CPU %d.\n"
+			"Crash kernel console output may be unavailable.\n", target_cpu, cpu);
+		return true;
+	}
+
+	/* Another panic already in progress */
+	if (panic_in_progress()) {
+		pr_warn("panic: Another panic in progress on CPU %d, cannot redirect to CPU %d.\n"
+			"Crash kernel console output may be unavailable.\n",
+			atomic_read(&panic_cpu), target_cpu);
+		return true;
+	}
+
+	pr_info("panic: Redirecting from CPU %d to CPU %d for crash kernel\n",
+		cpu, target_cpu);
+
+	vsnprintf(panic_redirect_msg, sizeof(panic_redirect_msg), fmt, args);
+
+	smp_call_function_single(target_cpu, do_panic_on_target_cpu, panic_redirect_msg, false);
+
+	return false;
+}
+#else
+__printf(1, 0)
+static inline bool panic_force_target_cpu(const char *fmt, va_list args)
+{
+	return true;
+}
+#endif /* CONFIG_SMP */
+
 bool panic_try_start(void)
 {
 	int old_cpu, this_cpu;
@@ -451,6 +537,13 @@ void vpanic(const char *fmt, va_list args)
 	local_irq_disable();
 	preempt_disable_notrace();
 
+	/*
+	 * Redirect panic to target CPU if configured via panic_force_cpu=.
+	 * Returns false and never returns if panic was redirected.
+	 */
+	if (!panic_force_target_cpu(fmt, args))
+		panic_smp_self_stop();
+
 	/*
 	 * It's possible to come here directly from a panic-assertion and
 	 * not have preempt disabled. Some functions called from here want
-- 
2.43.0
Re: [PATCH v2] panic: add panic_force_cpu= parameter to redirect panic to a specific CPU
Posted by Andrew Morton 21 hours ago
On Sun,  4 Jan 2026 22:42:10 +0200 Pnina Feder <pnina.feder@mobileye.com> wrote:

> Some platforms require panic handling to execute on a specific CPU for
> crash dump to work reliably. This can be due to firmware limitations,
> interrupt routing constraints, or platform-specific requirements where
> only a single CPU is able to safely enter the crash kernel.
> 
> Add support for redirecting panic execution to a designated CPU via a
> kernel command-line parameter.

Let's tell changelog readers what that command line parameter is called.

> When the parameter is provided, the CPU
> that initially triggers panic forwards the panic context to the target
> CPU, which then proceeds with the normal panic and kexec flow.
> 
> If the specified CPU is invalid, offline, or a panic is already in
> progress on another CPU, the redirection is skipped and panic continues
> on the current CPU.

Well I like it.  Others may not ;)

> Changes since v1:
>  - Replace Kconfig option with a kernel command-line parameter
>  - Fix clang format warning reported by kernel test robot
> 
> Reported-by: kernel test robot <lkp@intel.com>
> Closes: https://lore.kernel.org/oe-kbuild-all/202601041820.6M8cIq2e-lkp@intel.com/
> Signed-off-by: Pnina Feder <pnina.feder@mobileye.com>
> ---
>  kernel/panic.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++

Please update Documentation/admin-guide/kernel-parameters.txt?

> +__printf(1, 0)
> +static bool panic_force_target_cpu(const char *fmt, va_list args)
> +{
> +	static char panic_redirect_msg[1024];

It's sad to chew 1k of everyone's RAM for this. 
smp_call_function_single() is synchronous, yes?  Can we reduce that
message a lot and use automatic storage?

Or perhaps kmalloc the storage if the user provided the panic_force_cpu
kernel parameter?

> +	int cpu = raw_smp_processor_id();
> +	int target_cpu = panic_force_cpu;
> +
> +	/* Feature not enabled via boot parameter */
> +	if (target_cpu < 0)
> +		return true;
> +
> +	/* Already on target CPU - proceed normally */
> +	if (cpu == target_cpu)
> +		return true;
> +
> +	/* Target CPU is offline, can't redirect */
> +	if (!cpu_online(target_cpu)) {
> +		pr_warn("panic: target CPU %d is offline, proceeding on CPU %d.\n"

"panic: <lowercase>..."

> +			"Crash kernel console output may be unavailable.\n", target_cpu, cpu);
> +		return true;
> +	}
> +
> +	/* Another panic already in progress */
> +	if (panic_in_progress()) {
> +		pr_warn("panic: Another panic in progress on CPU %d, cannot redirect to CPU %d.\n"

"panic: <uppercase>..."

boy, was that a nit!

> +			"Crash kernel console output may be unavailable.\n",
> +			atomic_read(&panic_cpu), target_cpu);
> +		return true;
> +	}
> +
> +	pr_info("panic: Redirecting from CPU %d to CPU %d for crash kernel\n",
> +		cpu, target_cpu);
> +
> +	vsnprintf(panic_redirect_msg, sizeof(panic_redirect_msg), fmt, args);
> +
> +	smp_call_function_single(target_cpu, do_panic_on_target_cpu, panic_redirect_msg, false);
> +
> +	return false;
> +}
> +#else
> +__printf(1, 0)
> +static inline bool panic_force_target_cpu(const char *fmt, va_list args)
> +{
> +	return true;
> +}
> +#endif /* CONFIG_SMP */
> +
>  bool panic_try_start(void)
>  {
>  	int old_cpu, this_cpu;