Skip to contentSkip to conversation

panic: add panic_force_cpu= parameter to redirect panic to a specific CPU

PATCH Superseded Pnina Feder 3 participants 4 messages Jan 15 – Jan 22 Documentation

v7 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

PATCH v7 1/1 panic: add panic_force_cpu= parameter to redirect panic to a specific CPU

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 the panic_force_cpu= kernel command-line parameter to redirect panic execution to a designated CPU. When the parameter is provided, the CPU that initially triggers panic forwards the panic context to the target CPU via IPI, which then proceeds with the normal panic and kexec flow.

The IPI delivery is implemented as a weak function (panic_smp_redirect_cpu) so architectures with NMI support can override it for more reliable delivery.

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 v6 :
(addressing review comments from Petr Mladek)
- Renamed panic_force_target_cpu() to panic_try_force_cpu()
- Inverted return value logic: now returns true if redirected, false otherwise
- Added atomic panic_redirect_cpu variable to synchronize buffer access and prevent races when multiple CPUs call panic() in parallel
- Use panic_redirect_cpu to track original CPU and skip duplicate stack dump on target CPU
- Added set_cpu_online(false) before panic_smp_self_stop() to prevent panic_other_cpus_shutdown() from waiting for the redirecting CPU
- Moved panic_force_cpu declaration outside #ifdef block for use in vpanic()
- link to v6: https://lore.kernel.org/all/[email protected]/

Changes since v5:
- Restore (char *) cast in do_panic_on_target_cpu() to fix -Wformat warning
- link to v5: https://lore.kernel.org/all/[email protected]/

Changes sinse v4:
- Make IPI delivery an overridable weak function (panic_smp_redirect_cpu)
so architectures can use NMI where available
- Add declaration on include/linux/smp.h alongside other panic SMP functions
- Add warning to documentation about reduced reliability
- Address review comments from Andrew Morton (remove unnecessary cast,
add missing kernel-doc parameters)
- link to v4: https://lore.kernel.org/all/[email protected]/

Changes since v3:
- Dump original CPU's stack before redirecting to preserve debug info
- Add Documentation/admin-guide/kernel-parameters.txt entry
- Use smp_call_function_single_async() to avoid blocking in csd_lock()
- Add CONFIG_CRASH_DUMP dependency
- Reuse vpanic()'s static buffer instead of separate allocation
- Remove verbose warning messages
- link to v3: https://lore.kernel.org/all/[email protected]/

Changes since v2:
- Make panic redirection warnings generic and platform-agnostic
- link to v2: https://lore.kernel.org/all/[email protected]/

Changes since v1:
- Replace Kconfig option with a kernel command-line parameter
- Fix clang format warning reported by kernel test robot
- link to v1: https://lore.kernel.org/all/[email protected]/

---
 .../admin-guide/kernel-parameters.txt         |  15 ++
 include/linux/smp.h                           |   1 +
 kernel/panic.c                                | 139 +++++++++++++++++-
 3 files changed, 154 insertions(+), 1 deletion(-)
Documentation/admin-guide/kernel-parameters.txt +15 -0
index a8d0afde7f85..6d6f2880302f 100644
@@ -4753,6 +4753,21 @@ Kernel parameters
 	panic_on_warn=1	panic() instead of WARN().  Useful to cause kdump
 			on a WARN().
 
+	panic_force_cpu=
+			[KNL,SMP] Force panic handling to execute on a specific CPU.
+			Format: <cpu number>
+			Some platforms require panic handling to occur on a
+			specific CPU for the crash kernel to function correctly.
+			This can be due to firmware limitations, interrupt routing
+			constraints, or platform-specific requirements where only
+			a particular CPU can safely enter the crash kernel.
+			When set, panic() will redirect execution to the specified
+			CPU before proceeding with the normal panic and kexec flow.
+			If the target CPU is offline or unavailable, panic proceeds
+			on the current CPU.
+			This option should only be used for systems with the above
+			constraints as it might cause the panic operation to be less reliable.
+
 	panic_print=	Bitmask for printing system info when panic happens.
 			User can chose combination of the following bits:
 			bit 0: print all tasks info
include/linux/smp.h +1 -0
index 91d0ecf3b8d3..1ebd88026119 100644
@@ -62,6 +62,7 @@ int smp_call_function_single_async(int cpu, call_single_data_t *csd);
 void __noreturn panic_smp_self_stop(void);
 void __noreturn nmi_panic_self_stop(struct pt_regs *regs);
 void crash_smp_send_stop(void);
+int panic_smp_redirect_cpu(int target_cpu, void *msg);
 
 /*
  * Call a function on all processors
kernel/panic.c +138 -1

163 lines not on this page · raw message

Signature · 1 line
2.43.0
#1 lore raw
kernel test robot noticed the following build warnings:

Hi Pnina,

kernel test robot noticed the following build warnings:

[auto build test WARNING on akpm-mm/mm-everything]
[also build test WARNING on linus/master v6.19-rc5 next-20260115] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Pnina-Feder/panic-add-panic_force_cpu-parameter-to-redirect-panic-to-a-specific-CPU/20260115-210634
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20260115130552.3761679-1-pnina.feder%40mobileye.com
patch subject: [PATCH v7] panic: add panic_force_cpu= parameter to redirect panic to a specific CPU
config: x86_64-randconfig-121-20260116 (https://download.01.org/0day-ci/archive/20260116/[email protected]/config)
compiler: gcc-13 (Debian 13.3.0-16) 13.3.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260116/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

sparse warnings: (new ones prefixed by >>)

>> kernel/panic.c:305:10: sparse: sparse: symbol 'panic_redirect_cpu' was not declared. Should it be static?

vim +/panic_redirect_cpu +305 kernel/panic.c

303
304 atomic_t panic_cpu = ATOMIC_INIT(PANIC_CPU_INVALID);

 > 305	atomic_t panic_redirect_cpu = ATOMIC_INIT(PANIC_CPU_INVALID);

306

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#2 lore raw
In reply to Pnina Feder
Quoted 24 lines · Pnina Feder

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 the panic_force_cpu= kernel command-line parameter to redirect panic execution to a designated CPU. When the parameter is provided, the CPU that initially triggers panic forwards the panic context to the target CPU via IPI, which then proceeds with the normal panic and kexec flow.

The IPI delivery is implemented as a weak function (panic_smp_redirect_cpu) so architectures with NMI support can override it for more reliable delivery.

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.

kernel/panic.c +1 -0
@@ -299,6 +301,128 @@ void __weak crash_smp_send_stop(void)
 }
 
 atomic_t panic_cpu = ATOMIC_INIT(PANIC_CPU_INVALID);
+atomic_t panic_redirect_cpu = ATOMIC_INIT(PANIC_CPU_INVALID);

kernel/panic.c void __weak crash_smp_send_stop(void) · line 314

Quoted 10 lines
kernel/panic.c +10 -0
+
+#if defined(CONFIG_SMP) && defined(CONFIG_CRASH_DUMP)
+static int __init panic_force_cpu_setup(char *str)
+{
+	int cpu;
+
+	if (!str)
+		return -EINVAL;
+
+	if (kstrtoint(str, 0, &cpu) || cpu < 0) {

It should also fail when (cpu >= nr_cpu_ids).

IMHO, cpu_online(panic_force_cpu) might cause an invalid access otherwise.

Quoted 8 lines
kernel/panic.c +8 -0
+		pr_warn("panic_force_cpu: invalid value '%s'\n", str);
+		return -EINVAL;
+	}
+
+	panic_force_cpu = cpu;
+	return 0;
+}
+early_param("panic_force_cpu", panic_force_cpu_setup);

[...]

Quoted 26 lines
kernel/panic.c +26 -0
+/**
+ * panic_try_force_cpu - Redirect panic to a specific CPU for crash kernel
+ * @buf: buffer to format the panic message into
+ * @buf_size: size of the buffer
+ * @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_force_cpu= boot parameter.
+ *
+ * Returns false if panic should proceed on current CPU.
+ * Returns true if panic was redirected.
+ */
+__printf(3, 0)
+static bool panic_try_force_cpu(char *buf, int buf_size, const char *fmt, va_list args)
+{
+	int this_cpu = raw_smp_processor_id();
+	int old_cpu = PANIC_CPU_INVALID;
+
+	/* Feature not enabled via boot parameter */
+	if (panic_force_cpu < 0)
+		return false;
+
+	/* Already on target CPU - proceed normally */
+	if (this_cpu == panic_force_cpu)

kernel/panic.c void __weak crash_smp_send_stop(void) · line 384

Quoted 10 lines
kernel/panic.c +10 -0
+		return false;
+
+	/* Target CPU is offline, can't redirect */
+	if (!cpu_online(panic_force_cpu))
+		return false;
+
+	/* Another panic already in progress */
+	if (panic_in_progress()) {
+		return false;
+	}

Note that the preferred (panic_force_cpu) could enter panic() right now and use the buffer shared buffer in parallel.

kernel/panic.c void __weak crash_smp_send_stop(void) · line 393

Quoted 8 lines
kernel/panic.c +8 -0
+	/*
+	 * Only one CPU can do the redirect. Use atomic cmpxchg to ensure
+	 * we don't race with another CPU also trying to redirect.
+	 */
+	if (!atomic_try_cmpxchg(&panic_redirect_cpu, &old_cpu, this_cpu))
+		return false;
+
+	vsnprintf(buf, buf_size, fmt, args);

I am afraid that we can't share the buffer with the panic() function as safe way here.

Quoted 5 lines
kernel/panic.c +5 -0
+	console_verbose();
+	bust_spinlocks(1);
+
+	pr_emerg("panic: Redirecting from CPU %d to CPU %d for crash kernel.\n",
+		 this_cpu, panic_force_cpu);

kernel/panic.c void __weak crash_smp_send_stop(void) · line 409

Quoted 10 lines
kernel/panic.c +10 -0
+
+	/* Dump original CPU before redirecting */
+	if (!test_taint(TAINT_DIE) &&
+	    oops_in_progress <= 1 &&
+	    IS_ENABLED(CONFIG_DEBUG_BUGVERBOSE)) {
+		dump_stack();
+	}
+
+	printk_legacy_allow_panic_sync();
+	console_flush_on_panic(CONSOLE_FLUSH_PENDING);

Please, remove the two above functions. They are not safe. They increase the risk that __crash_kexec() won't be called. They should be called only by panic() when __crash_kexec() was not called...

kernel/panic.c void __weak crash_smp_send_stop(void) · line 413

Quoted 3 lines
kernel/panic.c +3 -0
+	if (panic_smp_redirect_cpu(panic_force_cpu, buf) != 0) {
+		atomic_set(&panic_redirect_cpu, PANIC_CPU_INVALID);
+		return false;

If we return "false" here then panic() will continue on this cpu. This CPU will likely acquire "panic_cpu" and crash_exec() will likely fail because this is not the preferred CPU.

We might want to warn about it. Or we should at least add a comment here.

And maybe we even should not call crash_exec() in panic() when

     panic_redirect_cpu != PANIC_CPU_INVALID &&
     panic_redirect_cpu != panic_cpu
Quoted 5 lines
kernel/panic.c +5 -0
+	}
+
+	/* IPI/NMI sent, this CPU should stop */
+	return true;
+}

kernel/panic.c void __weak crash_smp_send_stop(void) · line 428

Quoted 10 lines
kernel/panic.c +7 -0
+#else
+__printf(3, 0)
+static inline bool panic_try_force_cpu(char *buf, int buf_size, const char *fmt, va_list args)
+{
+	return false;
+}
+#endif /* CONFIG_SMP && CONFIG_CRASH_DUMP */
 
 bool panic_try_start(void)
 {

Best Regards,
Petr

#3 lore raw

Hi Petr,

Thank you for the review.

Quoted 36 lines · Pnina Feder
Quoted 22 lines

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 the panic_force_cpu= kernel command-line parameter to redirect panic execution to a designated CPU. When the parameter is provided, the CPU that initially triggers panic forwards the panic context to the target CPU via IPI, which then proceeds with the normal panic and kexec flow.

The IPI delivery is implemented as a weak function
(panic_smp_redirect_cpu) so architectures with NMI support can override it for more reliable delivery.

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.

kernel/panic.c +0 -0
@@ -299,6 +301,128 @@ void __weak crash_smp_send_stop(void)  }
 
 atomic_t panic_cpu = ATOMIC_INIT(PANIC_CPU_INVALID);

kernel/panic.c void __weak crash_smp_send_stop(void) } · line 312

Quoted 10 lines
kernel/panic.c +10 -0
+atomic_t panic_redirect_cpu = ATOMIC_INIT(PANIC_CPU_INVALID);
+
+#if defined(CONFIG_SMP) && defined(CONFIG_CRASH_DUMP) static int 
+__init panic_force_cpu_setup(char *str) {
+	int cpu;
+
+	if (!str)
+		return -EINVAL;
+
+	if (kstrtoint(str, 0, &cpu) || cpu < 0) {

It should also fail when (cpu >= nr_cpu_ids).

Quoted 1 line

IMHO, cpu_online(panic_force_cpu) might cause an invalid access otherwise.

Fixed.

Quoted 52 lines
Quoted 8 lines
kernel/panic.c +8 -0
+		pr_warn("panic_force_cpu: invalid value '%s'\n", str);
+		return -EINVAL;
+	}
+
+	panic_force_cpu = cpu;
+	return 0;
+}
+early_param("panic_force_cpu", panic_force_cpu_setup);

[...]

Quoted 28 lines
kernel/panic.c +28 -0
+/**
+ * panic_try_force_cpu - Redirect panic to a specific CPU for crash 
+kernel
+ * @buf: buffer to format the panic message into
+ * @buf_size: size of the buffer
+ * @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_force_cpu= boot parameter.
+ *
+ * Returns false if panic should proceed on current CPU.
+ * Returns true if panic was redirected.
+ */
+__printf(3, 0)
+static bool panic_try_force_cpu(char *buf, int buf_size, const char 
+*fmt, va_list args) {
+	int this_cpu = raw_smp_processor_id();
+	int old_cpu = PANIC_CPU_INVALID;
+
+	/* Feature not enabled via boot parameter */
+	if (panic_force_cpu < 0)
+		return false;
+
+	/* Already on target CPU - proceed normally */
+	if (this_cpu == panic_force_cpu)

kernel/panic.c void __weak crash_smp_send_stop(void) · line 384

Quoted 10 lines
kernel/panic.c +10 -0
+		return false;
+
+	/* Target CPU is offline, can't redirect */
+	if (!cpu_online(panic_force_cpu))
+		return false;
+
+	/* Another panic already in progress */
+	if (panic_in_progress()) {
+		return false;
+	}

Note that the preferred (panic_force_cpu) could enter panic() right now and use the buffer shared buffer in parallel.

Quoted 10 lines

kernel/panic.c void __weak crash_smp_send_stop(void) · line 393

Quoted 8 lines
kernel/panic.c +8 -0
+	/*
+	 * Only one CPU can do the redirect. Use atomic cmpxchg to ensure
+	 * we don't race with another CPU also trying to redirect.
+	 */
+	if (!atomic_try_cmpxchg(&panic_redirect_cpu, &old_cpu, this_cpu))
+		return false;
+
+	vsnprintf(buf, buf_size, fmt, args);

I am afraid that we can't share the buffer with the panic() function as safe way here.

Fixed via separate buffer using late_initcall + kmalloc. Falls back to static message for early boot panics or kmalloc fails.

Quoted 10 lines
Quoted 10 lines
kernel/panic.c +10 -0
+	console_verbose();
+	bust_spinlocks(1);
+
+	pr_emerg("panic: Redirecting from CPU %d to CPU %d for crash kernel.\n",
+		 this_cpu, panic_force_cpu);
+
+	/* Dump original CPU before redirecting */
+	if (!test_taint(TAINT_DIE) &&
+	    oops_in_progress <= 1 &&
+	    IS_ENABLED(CONFIG_DEBUG_BUGVERBOSE)) {
Quoted 8 lines

kernel/panic.c void __weak crash_smp_send_stop(void) · line 409

Quoted 5 lines
kernel/panic.c +5 -0
+		dump_stack();
+	}
+
+	printk_legacy_allow_panic_sync();
+	console_flush_on_panic(CONSOLE_FLUSH_PENDING);

Please, remove the two above functions. They are not safe. They increase the risk that __crash_kexec() won't be called. They should be called only by panic() when __crash_kexec() was not called...

Removed.

Quoted 7 lines

kernel/panic.c void __weak crash_smp_send_stop(void) · line 413

Quoted 3 lines
kernel/panic.c +3 -0
+	if (panic_smp_redirect_cpu(panic_force_cpu, buf) != 0) {
+		atomic_set(&panic_redirect_cpu, PANIC_CPU_INVALID);
+		return false;

If we return "false" here then panic() will continue on this cpu.
This CPU will likely acquire "panic_cpu" and crash_exec() will likely fail because this is not the preferred CPU.

Quoted 7 lines

We might want to warn about it. Or we should at least add a comment here.

And maybe we even should not call crash_exec() in panic() when

     panic_redirect_cpu != PANIC_CPU_INVALID &&
     panic_redirect_cpu != panic_cpu

Added pr_warn for offline CPU, redirect failure, and invalid parameter.

Thanks,
Pnina