panic: introduce panic status function family
Subsystems (8)
v2 cover panic: introduce panic status function family
This series introduces a family of helper functions to manage panic state and updates existing code to use them.
Before this series, panic state helpers were scattered and inconsistent. For example, panic_in_progress() was defined in printk/printk.c, not in panic.c or panic.h. As a result, developers had to look in unexpected places to understand or re-use panic state logic. Other checks were open- coded, duplicating logic across panic, crash, and watchdog paths.
The new helpers centralize the functionality in panic.c/panic.h:
- panic_try_start()
- panic_reset()
- panic_in_progress()
- panic_on_this_cpu()
- panic_on_other_cpu()
Patches 1–8 add the helpers and convert panic/crash and printk/nbcon code to use them.
Patch 9 fixes a bug in the watchdog subsystem by skipping checks when a panic is in progress, avoiding interference with the panic CPU.
Together, this makes panic state handling simpler, more discoverable, and more robust.
Signature · 36 lines
Changes from v1:
- The commit messages have been re-formatted based on Baoquan He's suggestions.
- The panic_in_progress() check has been moved to be the first check in
watchdog_overflow_callback(), as suggested by Yury Norov.
Link to v1:
https://lore.kernel.org/all/[email protected]/
Jinchao Wang (9):
panic: Introduce helper functions for panic state
fbdev: Use panic_in_progress() helper
crash_core: use panic_try_start() in crash_kexec()
panic: use panic_try_start() in nmi_panic()
panic: use panic_try_start() in vpanic()
printk/nbcon: use panic_on_this_cpu() helper
panic/printk: replace this_cpu_in_panic() with panic_on_this_cpu()
panic/printk: replace other_cpu_in_panic() with panic_on_other_cpu()
watchdog: skip checks when panic is in progress
drivers/video/fbdev/core/fbcon.c | 9 +---
include/linux/panic.h | 6 +++
include/linux/printk.h | 2 -
kernel/crash_core.c | 15 ++-----
kernel/panic.c | 71 +++++++++++++++++++++++++------
kernel/printk/internal.h | 1 -
kernel/printk/nbcon.c | 14 +++---
kernel/printk/printk.c | 37 +++-------------
kernel/printk/printk_ringbuffer.c | 2 +-
kernel/watchdog.c | 6 +++
kernel/watchdog_perf.c | 4 ++
lib/dump_stack.c | 2 +-
12 files changed, 91 insertions(+), 78 deletions(-)
--
2.43.0PATCH v2 1/9 panic: Introduce helper functions for panic state
This patch introduces four new helper functions to abstract the management of the panic_cpu variable. These functions will be used in subsequent patches to refactor existing code.
The direct use of panic_cpu can be error-prone and ambiguous, as it requires manual checks to determine which CPU is handling the panic. The new helpers clarify intent:
panic_try_start():
Atomically sets the current CPU as the panicking CPU.
panic_reset():
Reset panic_cpu to PANIC_CPU_INVALID.
panic_in_progress():
Checks if a panic has been triggered.
panic_on_this_cpu():
Returns true if the current CPU is the panic originator.
panic_on_other_cpu():
Returns true if a panic is on another CPU.
This change lays the groundwork for improved code readability and robustness in the panic handling subsystem.
--- include/linux/panic.h | 6 +++++ kernel/panic.c | 53 ++++++++++++++++++++++++++++++++++++++++++ kernel/printk/printk.c | 5 ---- 3 files changed, 59 insertions(+), 5 deletions(-)
include/linux/panic.h +6 -0
@@ -43,6 +43,12 @@ void abort(void);extern atomic_t panic_cpu;#define PANIC_CPU_INVALID -1 +bool panic_try_start (void ); +void panic_reset (void ); +bool panic_in_progress (void ); +bool panic_on_this_cpu (void ); +bool panic_on_other_cpu (void ); +/* * Only to be used by arch init code. If the user over-wrote the default * CONFIG_PANIC_TIMEOUT, honor it.
kernel/panic.c +53 -0
59 lines not on this page · raw message
kernel/printk/printk.c +0 -5
@@ -345,11 +345,6 @@ static void __up_console_sem(unsigned long ip) }#define up_console_sem () __up_console_sem(_RET_IP_) -static bool panic_in_progress (void ) -{ -return unlikely (atomic_read (&panic_cpu) !=PANIC_CPU_INVALID ); -} -/* Return true if a panic is in progress on the current CPU. */ bool this_cpu_in_panic (void ) {
Signature · 1 line
2.43.0
Re: [PATCH v2 1/9] panic: Introduce helper functions for panic state
Quoted 120 lines · Jinchao Wang
This patch introduces four new helper functions to abstract the management of the
panic_cpuvariable. These functions will be used in subsequent patches to refactor existing code.The direct use of
panic_cpucan be error-prone and ambiguous, as it requires manual checks to determine which CPU is handling the panic. The new helpers clarify intent:
panic_try_start():
Atomically sets the current CPU as the panicking CPU.
panic_reset():
Resetpanic_cputoPANIC_CPU_INVALID.
panic_in_progress():
Checks if a panic has been triggered.
panic_on_this_cpu():
Returns true if the current CPU is the panic originator.
panic_on_other_cpu():
Returns true if a panic is on another CPU.This change lays the groundwork for improved code readability and robustness in the panic handling subsystem.
Signed-off-by Jinchao Wang <[email protected]>--- include/linux/panic.h | 6 +++++ kernel/panic.c | 53 ++++++++++++++++++++++++++++++++++++++++++ kernel/printk/printk.c | 5 ---- 3 files changed, 59 insertions(+), 5 deletions(-)include/linux/panic.h +6
-0@@ -43,6 +43,12 @@ void abort(void);extern atomic_t panic_cpu;#define PANIC_CPU_INVALID -1 +bool panic_try_start (void ); +void panic_reset (void ); +bool panic_in_progress (void ); +bool panic_on_this_cpu (void ); +bool panic_on_other_cpu (void ); +/* * Only to be used by arch init code. If the user over-wrote the default * CONFIG_PANIC_TIMEOUT, honor it. kernel/panic.c +53
-0@@ -294,6 +294,59 @@ void __weak crash_smp_send_stop(void)atomic_t panic_cpu =ATOMIC_INIT (PANIC_CPU_INVALID ); +bool panic_try_start (void ) +{ +int old_cpu, this_cpu; + +/* +* Only one CPU is allowed to execute the crash_kexec() code as with +* panic(). Otherwise parallel calls of panic() and crash_kexec() +* may stop each other. To exclude them, we use panic_cpu here too. +*/ + old_cpu =PANIC_CPU_INVALID ; + this_cpu =raw_smp_processor_id (); + +return atomic_try_cmpxchg (&panic_cpu, &old_cpu, this_cpu); +} +EXPORT_SYMBOL (panic_try_start); + +void panic_reset (void ) +{ +atomic_set (&panic_cpu,PANIC_CPU_INVALID ); +} +EXPORT_SYMBOL (panic_reset); + +bool panic_in_progress (void ) +{ +return unlikely (atomic_read (&panic_cpu) !=PANIC_CPU_INVALID ); +} +EXPORT_SYMBOL (panic_in_progress); + +/* Return true if a panic is in progress on the current CPU. */ +bool panic_on_this_cpu (void ) +{ +/* +* We can use raw_smp_processor_id() here because it is impossible for +* the task to be migrated to the panic_cpu, or away from it. If +* panic_cpu has already been set, and we're not currently executing on +* that CPU, then we never will be. +*/ +return unlikely (atomic_read (&panic_cpu) ==raw_smp_processor_id ()); +} +EXPORT_SYMBOL (panic_on_this_cpu); + +/* +* Return true if a panic is in progress on a remote CPU. +* +* On true, the local CPU should immediately release any printing resources +* that may be needed by the panic CPU. +*/ +bool panic_on_other_cpu (void ) +{ +return (panic_in_progress () && !this_cpu_in_panic ()); +} +EXPORT_SYMBOL (panic_on_other_cpu); +/* * A variant of panic() called from NMI context. We return if we've already * panicked on this CPU. If another CPU already panicked, loop in kernel/printk/printk.c +0
-0@@ -345,11 +345,6 @@ static void __up_console_sem(unsigned long ip) }
kernel/printk/printk.c static void __up_console_sem(unsigned long ip) · line 350
Quoted 10 lines
kernel/printk/printk.c +0
-5#define up_console_sem () __up_console_sem(_RET_IP_) -static bool panic_in_progress (void ) -{ -return unlikely (atomic_read (&panic_cpu) !=PANIC_CPU_INVALID ); -} -/* Return true if a panic is in progress on the current CPU. */ bool this_cpu_in_panic (void ) {
All the functions are trivial. It would make sense to define them in linux/panic.h. Then the callers would benefit from the (unlikely) prediction macro...
It can be done in a followup path.
Otherwise, the patch looks good. I think that it is too late but feel free to use:
Best Regards,
Petr
Re: [PATCH v2 1/9] panic: Introduce helper functions for panic state
Quoted 136 lines · Petr Mladek
Quoted 120 lines · Jinchao Wang
This patch introduces four new helper functions to abstract the management of the
panic_cpuvariable. These functions will be used in subsequent patches to refactor existing code.The direct use of
panic_cpucan be error-prone and ambiguous, as it requires manual checks to determine which CPU is handling the panic. The new helpers clarify intent:
panic_try_start():
Atomically sets the current CPU as the panicking CPU.
panic_reset():
Resetpanic_cputoPANIC_CPU_INVALID.
panic_in_progress():
Checks if a panic has been triggered.
panic_on_this_cpu():
Returns true if the current CPU is the panic originator.
panic_on_other_cpu():
Returns true if a panic is on another CPU.This change lays the groundwork for improved code readability and robustness in the panic handling subsystem.
Signed-off-by Jinchao Wang <[email protected]>--- include/linux/panic.h | 6 +++++ kernel/panic.c | 53 ++++++++++++++++++++++++++++++++++++++++++ kernel/printk/printk.c | 5 ---- 3 files changed, 59 insertions(+), 5 deletions(-)include/linux/panic.h +6
-0@@ -43,6 +43,12 @@ void abort(void);extern atomic_t panic_cpu;#define PANIC_CPU_INVALID -1 +bool panic_try_start (void ); +void panic_reset (void ); +bool panic_in_progress (void ); +bool panic_on_this_cpu (void ); +bool panic_on_other_cpu (void ); +/* * Only to be used by arch init code. If the user over-wrote the default * CONFIG_PANIC_TIMEOUT, honor it. kernel/panic.c +53
-0@@ -294,6 +294,59 @@ void __weak crash_smp_send_stop(void)atomic_t panic_cpu =ATOMIC_INIT (PANIC_CPU_INVALID ); +bool panic_try_start (void ) +{ +int old_cpu, this_cpu; + +/* +* Only one CPU is allowed to execute the crash_kexec() code as with +* panic(). Otherwise parallel calls of panic() and crash_kexec() +* may stop each other. To exclude them, we use panic_cpu here too. +*/ + old_cpu =PANIC_CPU_INVALID ; + this_cpu =raw_smp_processor_id (); + +return atomic_try_cmpxchg (&panic_cpu, &old_cpu, this_cpu); +} +EXPORT_SYMBOL (panic_try_start); + +void panic_reset (void ) +{ +atomic_set (&panic_cpu,PANIC_CPU_INVALID ); +} +EXPORT_SYMBOL (panic_reset); + +bool panic_in_progress (void ) +{ +return unlikely (atomic_read (&panic_cpu) !=PANIC_CPU_INVALID ); +} +EXPORT_SYMBOL (panic_in_progress); + +/* Return true if a panic is in progress on the current CPU. */ +bool panic_on_this_cpu (void ) +{ +/* +* We can use raw_smp_processor_id() here because it is impossible for +* the task to be migrated to the panic_cpu, or away from it. If +* panic_cpu has already been set, and we're not currently executing on +* that CPU, then we never will be. +*/ +return unlikely (atomic_read (&panic_cpu) ==raw_smp_processor_id ()); +} +EXPORT_SYMBOL (panic_on_this_cpu); + +/* +* Return true if a panic is in progress on a remote CPU. +* +* On true, the local CPU should immediately release any printing resources +* that may be needed by the panic CPU. +*/ +bool panic_on_other_cpu (void ) +{ +return (panic_in_progress () && !this_cpu_in_panic ()); +} +EXPORT_SYMBOL (panic_on_other_cpu); +/* * A variant of panic() called from NMI context. We return if we've already * panicked on this CPU. If another CPU already panicked, loop in kernel/printk/printk.c +0
-0@@ -345,11 +345,6 @@ static void __up_console_sem(unsigned long ip) }kernel/printk/printk.c static void __up_console_sem(unsigned long ip) · line 350
Quoted 10 lines
kernel/printk/printk.c +0
-5#define up_console_sem () __up_console_sem(_RET_IP_) -static bool panic_in_progress (void ) -{ -return unlikely (atomic_read (&panic_cpu) !=PANIC_CPU_INVALID ); -} -/* Return true if a panic is in progress on the current CPU. */ bool this_cpu_in_panic (void ) {All the functions are trivial. It would make sense to define them in
linux/panic.h. Then the callers would benefit from the (unlikely) prediction macro...
Quoted 1 line
It can be done in a followup path.
Thanks for feedback, I will do it later.
BTW, this series was merged to -mm branch already.
Quoted 8 lines
Otherwise, the patch looks good. I think that it is too late but feel free to use:
Reviewed-by Petr Mladek <[email protected]>Best Regards,
Petr
Signature · 1 line
Jinchao
PATCH v2 2/9 fbdev: Use panic_in_progress() helper
In reply to Jinchao WangThis patch updates the fbcon_skip_panic() function to use the panic_in_progress() helper.
The previous direct access to panic_cpu is less readable and is being replaced by a dedicated function that more clearly expresses the intent.
This change is part of a series to refactor the kernel's panic handling logic for better clarity and robustness.
Acked-by Qianqiang Liu <[email protected]>
--- drivers/video/fbdev/core/fbcon.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-)
drivers/video/fbdev/core/fbcon.c +1 -8
@@ -279,14 +279,7 @@ static int fbcon_get_rotate(struct fb_info *info)static bool fbcon_skip_panic (struct fb_info *info) { -/* panic_cpu is not exported, and can't be used if built as module. Use -* oops_in_progress instead, but non-fatal oops won't be printed. -*/ -#if defined(MODULE ) -return (info->skip_panic &&unlikely (oops_in_progress)); -#else -return (info->skip_panic &&unlikely (atomic_read (&panic_cpu) !=PANIC_CPU_INVALID )); -#endif +return (info->skip_panic &&unlikely (panic_in_progress ())); }static inline bool fbcon_is_active (struct vc_data *vc,struct fb_info *info)
Signature · 1 line
2.43.0
PATCH v2 3/9 crash_core: use panic_try_start() in crash_kexec()
In reply to Jinchao Wangcrash_kexec() had its own code to exclude parallel execution by setting panic_cpu. This is already handled by panic_try_start(). Switch to panic_try_start() to remove the duplication and keep the logic consistent.
--- kernel/crash_core.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-)
kernel/crash_core.c +3 -12
@@ -4,6 +4,7 @@* Copyright (C) 2002-2004 Eric Biederman <[email protected]> */ +#include "linux/panic.h" #define pr_fmt (fmt) KBUILD_MODNAME ": " fmt#include <linux/buildid.h>
@@ -143,17 +144,7 @@ STACK_FRAME_NON_STANDARD(__crash_kexec); __bpf_kfunc void crash_kexec(struct pt_regs *regs) { -int old_cpu, this_cpu; - -/* -* Only one CPU is allowed to execute the crash_kexec() code as with -* panic(). Otherwise parallel calls of panic() and crash_kexec() -* may stop each other. To exclude them, we use panic_cpu here too. -*/ - old_cpu =PANIC_CPU_INVALID ; - this_cpu =raw_smp_processor_id (); - -if (atomic_try_cmpxchg (&panic_cpu, &old_cpu, this_cpu)) { +if (panic_try_start ()) {/* This is the 1st CPU which comes here, so go ahead. */ __crash_kexec (regs);
@@ -161,7 +152,7 @@ __bpf_kfunc void crash_kexec(struct pt_regs *regs)* Reset panic_cpu to allow another panic()/crash_kexec() * call. */ -atomic_set (&panic_cpu,PANIC_CPU_INVALID ); +panic_reset (); } }
Signature · 1 line
2.43.0
Re: [PATCH v2 3/9] crash_core: use panic_try_start() in crash_kexec()
Quoted 14 lines · Jinchao Wang
crash_kexec()had its own code to exclude parallel execution by settingpanic_cpu. This is already handled bypanic_try_start(). Switch topanic_try_start()to remove the duplication and keep the logic consistent.Signed-off-by Jinchao Wang <[email protected]>--- kernel/crash_core.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-)kernel/crash_core.c +0
-0
kernel/crash_core.c line 7
Quoted 4 lines
kernel/crash_core.c +1
-0@@ -4,6 +4,7 @@* Copyright (C) 2002-2004 Eric Biederman <[email protected]> */ +#include "linux/panic.h"
Can you change this line of code to #include <linux/panic.h>?
Quoted 33 lines
kernel/crash_core.c +2
-12#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt#include <linux/buildid.h> @@ -143,17 +144,7 @@ STACK_FRAME_NON_STANDARD(__crash_kexec); __bpf_kfunc void crash_kexec(struct pt_regs *regs) { -int old_cpu, this_cpu; - -/* -* Only one CPU is allowed to execute the crash_kexec() code as with -* panic(). Otherwise parallel calls of panic() and crash_kexec() -* may stop each other. To exclude them, we use panic_cpu here too. -*/ - old_cpu =PANIC_CPU_INVALID ; - this_cpu =raw_smp_processor_id (); - -if (atomic_try_cmpxchg (&panic_cpu, &old_cpu, this_cpu)) { +if (panic_try_start ()) {/* This is the 1st CPU which comes here, so go ahead. */ __crash_kexec (regs);@@ -161,7 +152,7 @@ __bpf_kfunc void crash_kexec(struct pt_regs *regs)* Reset panic_cpu to allow another panic()/crash_kexec() * call. */ -atomic_set (&panic_cpu,PANIC_CPU_INVALID ); +panic_reset (); } }Signature · 1 line
2.43.0
Signature · 2 lines
Best, Qianqiang Liu
Re: [PATCH v2 3/9] crash_core: use panic_try_start() in crash_kexec()
Quoted 18 lines · Qianqiang Liu
Quoted 17 lines · Jinchao Wang
crash_kexec()had its own code to exclude parallel execution by settingpanic_cpu. This is already handled bypanic_try_start(). Switch topanic_try_start()to remove the duplication and keep the logic consistent.Signed-off-by Jinchao Wang <[email protected]>--- kernel/crash_core.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-)kernel/crash_core.c +0
-0@@ -4,6 +4,7 @@* Copyright (C) 2002-2004 Eric Biederman <[email protected]> */
Quoted 3 lines
kernel/crash_core.c line 7
Quoted 1 line
kernel/crash_core.c +1
-0+#include "linux/panic.h" Can you change this line of code to #include <
linux/panic.h>?
Yes, I will.
Thanks for the feedback.>
Quoted 34 lines
Quoted 33 lines
kernel/crash_core.c +2
-12#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt#include <linux/buildid.h> @@ -143,17 +144,7 @@ STACK_FRAME_NON_STANDARD(__crash_kexec); __bpf_kfunc void crash_kexec(struct pt_regs *regs) { -int old_cpu, this_cpu; - -/* -* Only one CPU is allowed to execute the crash_kexec() code as with -* panic(). Otherwise parallel calls of panic() and crash_kexec() -* may stop each other. To exclude them, we use panic_cpu here too. -*/ - old_cpu =PANIC_CPU_INVALID ; - this_cpu =raw_smp_processor_id (); - -if (atomic_try_cmpxchg (&panic_cpu, &old_cpu, this_cpu)) { +if (panic_try_start ()) {/* This is the 1st CPU which comes here, so go ahead. */ __crash_kexec (regs);@@ -161,7 +152,7 @@ __bpf_kfunc void crash_kexec(struct pt_regs *regs)* Reset panic_cpu to allow another panic()/crash_kexec() * call. */ -atomic_set (&panic_cpu,PANIC_CPU_INVALID ); +panic_reset (); } }Signature · 1 line
2.43.0
Signature · 2 lines
Best regards, Jinchao
PATCH v2 4/9 panic: use panic_try_start() in nmi_panic()
In reply to Jinchao Wangnmi_panic() duplicated the logic to claim panic_cpu with atomic_try_cmpxchg. This is already wrapped in panic_try_start().
Replace the open-coded logic with panic_try_start(), and use panic_on_other_cpu() for the fallback path.
This removes duplication and keeps panic handling code consistent.
--- kernel/panic.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)
kernel/panic.c +2 -8
@@ -355,15 +355,9 @@ EXPORT_SYMBOL(panic_on_other_cpu);*/ void nmi_panic (struct pt_regs *regs,const char *msg) { -int old_cpu, this_cpu; - - old_cpu =PANIC_CPU_INVALID ; - this_cpu =raw_smp_processor_id (); - -/* atomic_try_cmpxchg updates old_cpu on failure */ -if (atomic_try_cmpxchg (&panic_cpu, &old_cpu, this_cpu)) +if (panic_try_start ())panic ("%s" , msg); -else if (old_cpu != this_cpu) +else if (panic_on_other_cpu ())nmi_panic_self_stop (regs); }EXPORT_SYMBOL (nmi_panic);
Signature · 1 line
2.43.0
PATCH v2 5/9 panic: use panic_try_start() in vpanic()
In reply to Jinchao Wangvpanic() had open-coded logic to claim panic_cpu with atomic_try_cmpxchg. This is already handled by panic_try_start().
Switch to panic_try_start() and use panic_on_other_cpu() for the fallback path.
This removes duplicate code and makes panic handling consistent across functions.
--- kernel/panic.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
kernel/panic.c +2 -6
@@ -415,7 +415,6 @@ void vpanic(const char *fmt, va_list args)static char buf[1024 ];long i, i_next =0 , len;int state =0 ; -int old_cpu, this_cpu;bool _crash_kexec_post_notifiers = crash_kexec_post_notifiers;if (panic_on_warn) {
@@ -452,13 +451,10 @@ void vpanic(const char *fmt, va_list args)* `old_cpu == this_cpu' means we came from nmi_panic() which sets * panic_cpu to this CPU. In this case, this is also the 1st CPU. */ - old_cpu =PANIC_CPU_INVALID ; - this_cpu =raw_smp_processor_id (); -/* atomic_try_cmpxchg updates old_cpu on failure */ -if (atomic_try_cmpxchg (&panic_cpu, &old_cpu, this_cpu)) { +if (panic_try_start ()) {/* go ahead */ - }else if (old_cpu != this_cpu) + }else if (panic_on_other_cpu ())panic_smp_self_stop ();console_verbose ();
Signature · 1 line
2.43.0
Petr Mladek
maintainerRe: [PATCH v2 5/9] panic: use panic_try_start() in vpanic()
Quoted 20 lines · Jinchao Wang
vpanic()had open-coded logic to claimpanic_cpuwithatomic_try_cmpxchg. This is already handled bypanic_try_start().Switch to
panic_try_start()and usepanic_on_other_cpu()for the fallback path.This removes duplicate code and makes panic handling consistent across functions.
kernel/panic.c +0
-1@@ -415,7 +415,6 @@ void vpanic(const char *fmt, va_list args)static char buf[1024 ];long i, i_next =0 , len;int state =0 ; -int old_cpu, this_cpu;bool _crash_kexec_post_notifiers = crash_kexec_post_notifiers;if (panic_on_warn) {
kernel/panic.c void vpanic(const char *fmt, va_list args) · line 453
Quoted 3 lines
kernel/panic.c +0
-0@@ -452,13 +451,10 @@ void vpanic(const char *fmt, va_list args)* `old_cpu == this_cpu' means we came from nmi_panic() which sets * panic_cpu to this CPU. In this case, this is also the 1st CPU. */
The above comment does not fit any longer. I think that it can be removed, maybe except for the 1st paragraph.
kernel/panic.c void vpanic(const char *fmt, va_list args) · line 454
Quoted 4 lines
kernel/panic.c +0
-3- old_cpu =PANIC_CPU_INVALID ; - this_cpu =raw_smp_processor_id (); -/* atomic_try_cmpxchg updates old_cpu on failure */
Also this comment should be removed.
kernel/panic.c void vpanic(const char *fmt, va_list args) · line 460
Quoted 8 lines
kernel/panic.c +2
-2-if (atomic_try_cmpxchg (&panic_cpu, &old_cpu, this_cpu)) { +if (panic_try_start ()) {/* go ahead */ - }else if (old_cpu != this_cpu) + }else if (panic_on_other_cpu ())panic_smp_self_stop ();console_verbose ();
Otherwise, it looks good. And the comments might be removed by a followup patch.
Best Regards,
Petr
PATCH v2 6/9 printk/nbcon: use panic_on_this_cpu() helper
In reply to Jinchao Wangnbcon_context_try_acquire() compared panic_cpu directly with smp_processor_id(). This open-coded check is now provided by panic_on_this_cpu().
Switch to panic_on_this_cpu() to simplify the code and improve readability.
--- kernel/printk/nbcon.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/printk/nbcon.c +2 -2
@@ -2,6 +2,7 @@// Copyright (C) 2022 Linutronix GmbH, John Ogness // Copyright (C) 2022 Intel, Thomas Gleixner +#include "linux/panic.h" #include <linux/atomic.h> #include <linux/bug.h> #include <linux/console.h>
@@ -589,7 +590,6 @@ static struct printk_buffers panic_nbcon_pbufs;*/ static bool nbcon_context_try_acquire (struct nbcon_context *ctxt,bool is_reacquire) { -unsigned int cpu =smp_processor_id ();struct console *con = ctxt->console;struct nbcon_state cur;int err;
@@ -614,7 +614,7 @@ static bool nbcon_context_try_acquire(struct nbcon_context *ctxt, bool is_reacqu/* Acquire succeeded. */ /* Assign the appropriate buffer for this context. */ -if (atomic_read (&panic_cpu) == cpu) +if (panic_on_this_cpu ()) ctxt->pbufs = &panic_nbcon_pbufs;else ctxt->pbufs = con->pbufs;
Signature · 1 line
2.43.0
Re: [PATCH v2 6/9] printk/nbcon: use panic_on_this_cpu() helper
Quoted 8 lines · Jinchao Wang
nbcon_context_try_acquire()comparedpanic_cpudirectly withsmp_processor_id(). This open-coded check is now provided bypanic_on_this_cpu().Switch to
panic_on_this_cpu()to simplify the code and improve readability.Signed-off-by Jinchao Wang <[email protected]>
It looks good to me:
Best Regards,
Petr
PATCH v2 7/9 panic/printk: replace this_cpu_in_panic() with panic_on_this_cpu()
In reply to Jinchao WangThe helper this_cpu_in_panic() duplicated logic already provided by panic_on_this_cpu().
Remove this_cpu_in_panic() and switch all users to panic_on_this_cpu().
This simplifies the code and avoids having two helpers for the same check.
--- include/linux/printk.h | 2 -- kernel/panic.c | 2 +- kernel/printk/nbcon.c | 2 +- kernel/printk/printk.c | 15 ++------------- kernel/printk/printk_ringbuffer.c | 2 +- lib/dump_stack.c | 2 +- 6 files changed, 6 insertions(+), 19 deletions(-)
include/linux/printk.h +0 -2
@@ -330,8 +330,6 @@ static inline bool pr_flush(int timeout_ms, bool reset_on_progress)#endif -bool this_cpu_in_panic (void ); -#ifdef CONFIG_SMP extern int __printk_cpu_sync_try_get (void );extern void __printk_cpu_sync_wait (void );
kernel/panic.c +1 -1
@@ -343,7 +343,7 @@ EXPORT_SYMBOL(panic_on_this_cpu);*/ bool panic_on_other_cpu (void ) { -return (panic_in_progress () && !this_cpu_in_panic ()); +return (panic_in_progress () && !panic_on_this_cpu ()); }EXPORT_SYMBOL (panic_on_other_cpu);
kernel/printk/nbcon.c +1 -1
@@ -1394,7 +1394,7 @@ enum nbcon_prio nbcon_get_default_prio(void) {unsigned int *cpu_emergency_nesting; -if (this_cpu_in_panic ()) +if (panic_on_this_cpu ())return NBCON_PRIO_PANIC ; cpu_emergency_nesting =nbcon_get_cpu_emergency_nesting ();
kernel/printk/printk.c +2 -13
@@ -17,6 +17,7 @@* 01Mar01 Andrew Morton */ +#include "linux/panic.h" #define pr_fmt (fmt) KBUILD_MODNAME ": " fmt#include <linux/kernel.h>
@@ -345,18 +346,6 @@ static void __up_console_sem(unsigned long ip) }#define up_console_sem () __up_console_sem(_RET_IP_) -/* Return true if a panic is in progress on the current CPU. */ -bool this_cpu_in_panic (void ) -{ -/* -* We can use raw_smp_processor_id() here because it is impossible for -* the task to be migrated to the panic_cpu, or away from it. If -* panic_cpu has already been set, and we're not currently executing on -* that CPU, then we never will be. -*/ -return unlikely (atomic_read (&panic_cpu) ==raw_smp_processor_id ()); -} -/* * Return true if a panic is in progress on a remote CPU. *
@@ -365,7 +354,7 @@ bool this_cpu_in_panic(void)*/ bool other_cpu_in_panic (void ) { -return (panic_in_progress () && !this_cpu_in_panic ()); +return (panic_in_progress () && !panic_on_this_cpu ()); }/*
kernel/printk/printk_ringbuffer.c +1 -1
@@ -2143,7 +2143,7 @@ static bool _prb_read_valid(struct printk_ringbuffer *rb, u64 *seq,* But it would have the sequence number returned * by "prb_next_reserve_seq() - 1". */ -if (this_cpu_in_panic () && +if (panic_on_this_cpu () && (!debug_non_panic_cpus || legacy_allow_panic_sync) && ((*seq +1 ) <prb_next_reserve_seq (rb))) { (*seq)++;
lib/dump_stack.c +1 -1
@@ -102,7 +102,7 @@ static void __dump_stack(const char *log_lvl)*/ asmlinkage __visible void dump_stack_lvl(const char *log_lvl) { -bool in_panic =this_cpu_in_panic (); +bool in_panic =panic_on_this_cpu ();unsigned long flags;/*
Signature · 1 line
2.43.0
Re: [PATCH v2 7/9] panic/printk: replace this_cpu_in_panic() with panic_on_this_cpu()
Quoted 8 lines · Jinchao Wang
The helper
this_cpu_in_panic()duplicated logic already provided bypanic_on_this_cpu().Remove
this_cpu_in_panic()and switch all users topanic_on_this_cpu().This simplifies the code and avoids having two helpers for the same check.
Signed-off-by Jinchao Wang <[email protected]>
It is great that the helper functions were consolidated and moved from printk to panic code.
Best Regards,
Petr
PATCH v2 8/9 panic/printk: replace other_cpu_in_panic() with panic_on_other_cpu()
In reply to Jinchao WangThe helper other_cpu_in_panic() duplicated logic already provided by panic_on_other_cpu().
Remove other_cpu_in_panic() and update all users to call panic_on_other_cpu() instead.
This removes redundant code and makes panic handling consistent.
--- kernel/printk/internal.h | 1 - kernel/printk/nbcon.c | 8 ++++---- kernel/printk/printk.c | 19 ++++--------------- 3 files changed, 8 insertions(+), 20 deletions(-)
kernel/printk/internal.h +0 -1
@@ -332,7 +332,6 @@ struct printk_message {unsigned long dropped; }; -bool other_cpu_in_panic (void );bool printk_get_next_message (struct printk_message *pmsg,u64 seq,bool is_extended,bool may_supress);
kernel/printk/nbcon.c +4 -4
@@ -255,7 +255,7 @@ static int nbcon_context_try_acquire_direct(struct nbcon_context *ctxt,* opportunity to perform any necessary cleanup if they were * interrupted by the panic CPU while printing. */ -if (other_cpu_in_panic () && +if (panic_on_other_cpu () && (!is_reacquire || cur->unsafe_takeover)) {return -EPERM ; }
@@ -310,7 +310,7 @@ static bool nbcon_waiter_matches(struct nbcon_state *cur, int expected_prio)* Event #2 implies the new context is PANIC. * Event #3 occurs when panic() has flushed the console. * Event #4 occurs when a non-panic CPU reacquires. -* Event #5 is not possible due to the other_cpu_in_panic() check +* Event #5 is not possible due to the panic_on_other_cpu() check * in nbcon_context_try_acquire_handover(). */
@@ -349,7 +349,7 @@ static int nbcon_context_try_acquire_requested(struct nbcon_context *ctxt,struct nbcon_state new;/* Note that the caller must still remove the request! */ -if (other_cpu_in_panic ()) +if (panic_on_other_cpu ())return -EPERM ;/*
@@ -447,7 +447,7 @@ static int nbcon_context_try_acquire_handover(struct nbcon_context *ctxt,* nbcon_waiter_matches(). In particular, the assumption that * lower priorities are ignored during panic. */ -if (other_cpu_in_panic ()) +if (panic_on_other_cpu ())return -EPERM ;/* Handover is not possible on the same CPU. */
kernel/printk/printk.c +4 -15
@@ -346,17 +346,6 @@ static void __up_console_sem(unsigned long ip) }#define up_console_sem () __up_console_sem(_RET_IP_) -/* -* Return true if a panic is in progress on a remote CPU. -* -* On true, the local CPU should immediately release any printing resources -* that may be needed by the panic CPU. -*/ -bool other_cpu_in_panic (void ) -{ -return (panic_in_progress () && !panic_on_this_cpu ()); -} -/* * This is used for debugging the mess that is the VT code by * keeping track if we have the console semaphore held. It's
@@ -2391,7 +2380,7 @@ asmlinkage int vprintk_emit(int facility, int level,* non-panic CPUs are generating any messages, they will be * silently dropped. */ -if (other_cpu_in_panic () && +if (panic_on_other_cpu () && !debug_non_panic_cpus && !panic_triggering_all_cpu_backtrace)return 0 ;
@@ -2827,7 +2816,7 @@ void console_lock(void)might_sleep ();/* On panic, the console_lock must be left to the panic cpu. */ -while (other_cpu_in_panic ()) +while (panic_on_other_cpu ())msleep (1000 );down_console_sem ();
@@ -2847,7 +2836,7 @@ EXPORT_SYMBOL(console_lock);int console_trylock (void ) {/* On panic, the console_lock must be left to the panic cpu. */ -if (other_cpu_in_panic ()) +if (panic_on_other_cpu ())return 0 ;if (down_trylock_console_sem ())return 0 ;
@@ -3227,7 +3216,7 @@ static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handove any_progress = true;/* Allow panic_cpu to take over the consoles safely. */ -if (other_cpu_in_panic ()) +if (panic_on_other_cpu ()) goto abandon;if (do_cond_resched)
Signature · 1 line
2.43.0
Re: [PATCH v2 8/9] panic/printk: replace other_cpu_in_panic() with panic_on_other_cpu()
Quoted 9 lines · Jinchao Wang
The helper
other_cpu_in_panic()duplicated logic already provided bypanic_on_other_cpu().Remove
other_cpu_in_panic()and update all users to callpanic_on_other_cpu()instead.This removes redundant code and makes panic handling consistent.
Signed-off-by Jinchao Wang <[email protected]>
Looks good:
Best Regards,
Petr
PATCH v2 9/9 watchdog: skip checks when panic is in progress
In reply to Jinchao WangThis issue was found when an EFI pstore was configured for kdump logging with the NMI hard lockup detector enabled. The efi-pstore write operation was slow, and with a large number of logs, the pstore dump callback within kmsg_dump() took a long time.
This delay triggered the NMI watchdog, leading to a nested panic. The call flow demonstrates how the secondary panic caused an emergency_restart() to be triggered before the initial pstore operation could finish, leading to a failure to dump the logs:
real panic() {
kmsg_dump() {
...
pstore_dump() {
start_dump();
... // long time operation triggers NMI watchdog
nmi panic() {
...
emergency_restart(); // pstore unfinished
}
...
finish_dump(); // never reached
}
}}
Both watchdog_buddy_check_hardlockup() and watchdog_overflow_callback() may trigger during a panic. This can lead to recursive panic handling.
Add panic_in_progress() checks so watchdog activity is skipped once a panic has begun.
This prevents recursive panic and keeps the panic path more reliable.
--- kernel/watchdog.c | 6 ++++++ kernel/watchdog_perf.c | 4 ++++ 2 files changed, 10 insertions(+)
kernel/watchdog.c +6 -0
@@ -740,6 +740,12 @@ static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)if (!watchdog_enabled)return HRTIMER_NORESTART ; +/* +* pass the buddy check if a panic is in process +*/ +if (panic_in_progress ()) +return HRTIMER_NORESTART ; +watchdog_hardlockup_kick ();/* kick the softlockup detector */
kernel/watchdog_perf.c +4 -0
@@ -12,6 +12,7 @@#define pr_fmt (fmt) "NMI watchdog: " fmt +#include <linux/panic.h> #include <linux/nmi.h> #include <linux/atomic.h> #include <linux/module.h>
@@ -108,6 +109,9 @@ static void watchdog_overflow_callback(struct perf_event *event,/* Ensure the watchdog never gets throttled */ event->hw.interrupts =0 ; +if (panic_in_progress ()) +return ; +if (!watchdog_check_timestamp ())return ;
Signature · 1 line
2.43.0