panic: Reduce CPU consumption after panic
v2 cover panic: Reduce CPU consumption after panic
From: Carlos Bilbao <[email protected]>
Provide a priority-based mechanism to set the behavior of the kernel at the post-panic stage -- the current default is a waste of CPU except for cases with console that generate insightful output.
In v1 [1], I illustrated the potential to reduce unnecessary CPU usage with an experiment with VMs. The main delta is that, instead of a weak function that archs can overwrite, we provide a flexible priority-based mechanism (following suggestions by Sean Christopherson), panic_set_handling().
[1] https://lore.kernel.org/all/[email protected]/
Carlos:
panic: Allow for dynamic custom behavior after panic x86/panic: Add x86_panic_handler as default post-panic behavior
--- arch/x86/kernel/setup.c | 12 ++++++++++++ include/linux/panic.h | 2 ++ kernel/panic.c | 27 +++++++++++++++++++++++++++ 3 files changed, 41 insertions(+)
PATCH v2 1/2 panic: Allow for dynamic custom behavior after panic
From: Carlos Bilbao <[email protected]>
Introduce panic_set_handling() to allow overriding the default post-panic behavior.
--- include/linux/panic.h | 2 ++ kernel/panic.c | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+)
include/linux/panic.h +2 -0
@@ -98,4 +98,6 @@ extern void add_taint(unsigned flag, enum lockdep_ok);extern int test_taint (unsigned flag);extern unsigned long get_taint (void ); +void panic_set_handling (void (*fn)(void ),int priority); +#endif /* _LINUX_PANIC_H */
kernel/panic.c +27 -0
@@ -276,6 +276,30 @@ static void panic_other_cpus_shutdown(bool crash_kexec)crash_smp_send_stop (); } +/* +* This is the default function called after a kernel panic has been +* handled. Higher priority alternatives can be set with function +* panic_set_handling() +* +*/ +static void after_panic_handling (void ) +{ +mdelay (PANIC_TIMER_STEP ); +} + +static void (*panic_halt)(void ) = after_panic_handling; +static int panic_hlt_priority; + +void panic_set_handling (void (*fn)(void ),int priority) +{ +if (priority <= panic_hlt_priority) +return ; + + panic_hlt_priority = priority; + panic_halt = fn; +} +EXPORT_SYMBOL_GPL (panic_set_handling); +/** * panic - halt the system * @fmt: The text string to print
@@ -467,6 +491,9 @@ void panic(const char *fmt, ...)console_flush_on_panic (CONSOLE_FLUSH_PENDING );nbcon_atomic_flush_unsafe (); +if (panic_halt) +panic_halt (); +local_irq_enable ();for (i =0 ; ; i +=PANIC_TIMER_STEP ) {touch_softlockup_watchdog ();
Signature · 1 line
2.47.1
Re: [PATCH v2 1/2] panic: Allow for dynamic custom behavior after panic
Quoted 10 lines · On Mon, Apr 28, 2025, [email protected] wrote:
kernel/panic.c +2
-0@@ -276,6 +276,30 @@ static void panic_other_cpus_shutdown(bool crash_kexec)crash_smp_send_stop (); } +/* +* This is the default function called after a kernel panic has been
kernel/panic.c static void panic_other_cpus_shutdown(bool crash_kexec) · line 290
Quoted 10 lines
kernel/panic.c +10
-0+* handled. Higher priority alternatives can be set with function +* panic_set_handling() +* +*/ +static void after_panic_handling (void ) +{ +mdelay (PANIC_TIMER_STEP ); +} + +static void (*panic_halt)(void ) = after_panic_handling;
The default implementation clearly doesn't halt, which makes this unnecessarily confusing. And if you're going to provide a default implementation, why bother checking for NULL in panic()? Just leave panic_halt NULL.
kernel/panic.c static void panic_other_cpus_shutdown(bool crash_kexec) · line 291
Quoted 1 line
kernel/panic.c +1
-0+static int panic_hlt_priority;
Uber nit, pick one of halt or hlt.
kernel/panic.c static void panic_other_cpus_shutdown(bool crash_kexec) · line 295
Quoted 4 lines
kernel/panic.c +4
-0+ +void panic_set_handling (void (*fn)(void ),int priority) +{ +if (priority <= panic_hlt_priority)
If panic_halt is NULL by default, maybe do?
if (panic_halt && priority <= panic_halt_priority)
kernel/panic.c static void panic_other_cpus_shutdown(bool crash_kexec) · line 301
Quoted 6 lines
kernel/panic.c +6
-0+return ; + + panic_hlt_priority = priority; + panic_halt = fn; +} +EXPORT_SYMBOL_GPL (panic_set_handling);
This doesn't seem like something that should be exported unless it's absolutely necessary, and it shouldn't be necessary as of this series.
Quoted 17 lines
kernel/panic.c +4
-0+/** * panic - halt the system * @fmt: The text string to print @@ -467,6 +491,9 @@ void panic(const char *fmt, ...)console_flush_on_panic (CONSOLE_FLUSH_PENDING );nbcon_atomic_flush_unsafe (); +if (panic_halt) +panic_halt (); +local_irq_enable ();for (i =0 ; ; i +=PANIC_TIMER_STEP ) {touch_softlockup_watchdog ();Signature · 1 line
2.47.1
Re: [PATCH v2 1/2] panic: Allow for dynamic custom behavior after panic
Hello,
Sean, thanks for getting back to me so fast!
Quoted 9 lines · Sean Christopherson
Quoted 8 lines · On Mon, Apr 28, 2025, [email protected] wrote:
kernel/panic.c +0
-0@@ -276,6 +276,30 @@ static void panic_other_cpus_shutdown(bool crash_kexec)crash_smp_send_stop (); }
Quoted 15 lines
kernel/panic.c static void panic_other_cpus_shutdown(bool crash_kexec) · line 290
Quoted 12 lines
kernel/panic.c +12
-0+/* +* This is the default function called after a kernel panic has been +* handled. Higher priority alternatives can be set with function +* panic_set_handling() +* +*/ +static void after_panic_handling (void ) +{ +mdelay (PANIC_TIMER_STEP ); +} + +static void (*panic_halt)(void ) = after_panic_handling;The default implementation clearly doesn't halt, which makes this unnecessarily confusing. And if you're going to provide a default implementation, why bother checking for NULL in
panic()? Just leavepanic_haltNULL.
Agreed.
Quoted 3 lines
kernel/panic.c static void panic_other_cpus_shutdown(bool crash_kexec) · line 291
Quoted 2 lines
kernel/panic.c +1
-0+static int panic_hlt_priority;Uber nit, pick one of halt or hlt.
True, will use halt.
Quoted 25 lines
kernel/panic.c static void panic_other_cpus_shutdown(bool crash_kexec) · line 295
Quoted 4 lines
kernel/panic.c +4
-0+ +void panic_set_handling (void (*fn)(void ),int priority) +{ +if (priority <= panic_hlt_priority)If
panic_haltis NULL by default, maybe do?if (panic_halt && priority <= panic_halt_priority)kernel/panic.c static void panic_other_cpus_shutdown(bool crash_kexec) · line 301
Quoted 6 lines
kernel/panic.c +6
-0+return ; + + panic_hlt_priority = priority; + panic_halt = fn; +} +EXPORT_SYMBOL_GPL (panic_set_handling);This doesn't seem like something that should be exported unless it's absolutely necessary, and it shouldn't be necessary as of this series.
Quoted 8 lines
kernel/panic.c +1
-0+/** * panic - halt the system * @fmt: The text string to print @@ -467,6 +491,9 @@ void panic(const char *fmt, ...)console_flush_on_panic (CONSOLE_FLUSH_PENDING );nbcon_atomic_flush_unsafe ();
Quoted 9 lines
Quoted 9 lines
kernel/panic.c +3
-0+if (panic_halt) +panic_halt (); +local_irq_enable ();for (i =0 ; ; i +=PANIC_TIMER_STEP ) {touch_softlockup_watchdog ();Signature · 1 line
2.47.1
Agree with all. Will fix in v3.
Thanks,
Carlos
PATCH v2 2/2 x86/panic: Add x86_panic_handler as default post-panic behavior
In reply to [email protected]From: Carlos Bilbao <[email protected]>
Add function x86_panic_handler() as the default behavior for x86 for post-panic stage via panic_set_handling(). Instead of busy-wait loop, it will halt if there's no console to save CPU cycles.
--- arch/x86/kernel/setup.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
arch/x86/kernel/setup.c +12 -0
@@ -16,6 +16,7 @@#include <linux/initrd.h> #include <linux/iscsi_ibft.h> #include <linux/memblock.h> +#include <linux/panic.h> #include <linux/panic_notifier.h> #include <linux/pci.h> #include <linux/root_dev.h>
@@ -837,6 +838,15 @@ static void __init x86_report_nx(void) } } + +static void x86_panic_handler (void ) +{ +if (console_trylock ()) { +console_unlock (); +safe_halt (); + } +} +/* * Determine if we were loaded by an EFI loader. If so, then we have also been * passed the efi memmap, systab, etc., so we should use these data structures
@@ -1252,6 +1262,8 @@ void __init setup_arch(char **cmdline_p)#endif unwind_init (); + +panic_set_handling (x86_panic_handler,1 ); }#ifdef CONFIG_X86_32
Signature · 1 line
2.47.1
Re: [PATCH v2 2/2] x86/panic: Add x86_panic_handler as default post-panic behavior
Quoted 24 lines · On Mon, Apr 28, 2025, [email protected] wrote:
From: Carlos Bilbao <[email protected]>
Add function
x86_panic_handler()as the default behavior for x86 for post-panic stage viapanic_set_handling(). Instead of busy-wait loop, it will halt if there's no console to save CPU cycles.Signed-off-by Carlos Bilbao (DigitalOcean) <[email protected]>--- arch/x86/kernel/setup.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)arch/x86/kernel/setup.c +1
-0@@ -16,6 +16,7 @@#include <linux/initrd.h> #include <linux/iscsi_ibft.h> #include <linux/memblock.h> +#include <linux/panic.h> #include <linux/panic_notifier.h> #include <linux/pci.h> #include <linux/root_dev.h>
arch/x86/kernel/setup.c static void __init x86_report_nx(void) · line 841
Quoted 4 lines
arch/x86/kernel/setup.c +1
-0@@ -837,6 +838,15 @@ static void __init x86_report_nx(void) } } +
Spurious newline.
arch/x86/kernel/setup.c static void __init x86_report_nx(void) · line 844
Quoted 3 lines
arch/x86/kernel/setup.c +3
-0+static void x86_panic_handler (void ) +{ +if (console_trylock ()) {
A comment here would be very helpful. Even with the changelog saying "if there's no console", as an unfamiliar reader of console code, I have zero idea why being able to lock the console is an effective test for no console.
Quoted 4 lines
arch/x86/kernel/setup.c +4
-0+console_unlock (); +safe_halt (); + } +}
Re: [PATCH v2 2/2] x86/panic: Add x86_panic_handler as default post-panic behavior
Hello,
Quoted 31 lines · Sean Christopherson
Quoted 24 lines · On Mon, Apr 28, 2025, [email protected] wrote:
From: Carlos Bilbao <[email protected]>
Add function
x86_panic_handler()as the default behavior for x86 for post-panic stage viapanic_set_handling(). Instead of busy-wait loop, it will halt if there's no console to save CPU cycles.Signed-off-by Carlos Bilbao (DigitalOcean) <[email protected]>--- arch/x86/kernel/setup.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)arch/x86/kernel/setup.c +1
-0@@ -16,6 +16,7 @@#include <linux/initrd.h> #include <linux/iscsi_ibft.h> #include <linux/memblock.h> +#include <linux/panic.h> #include <linux/panic_notifier.h> #include <linux/pci.h> #include <linux/root_dev.h> arch/x86/kernel/setup.c static void __init x86_report_nx(void) · line 841
Quoted 4 lines
arch/x86/kernel/setup.c +1
-0@@ -837,6 +838,15 @@ static void __init x86_report_nx(void) } } +Spurious newline.
Quoted 6 lines
arch/x86/kernel/setup.c static void __init x86_report_nx(void) · line 844
Quoted 3 lines
arch/x86/kernel/setup.c +3
-0+static void x86_panic_handler (void ) +{ +if (console_trylock ()) {A comment here would be very helpful. Even with the changelog saying "if there's no console", as an unfamiliar reader of console code, I have zero idea why being able to lock the console is an effective test for no console.
Agree, will fix in v3.
Quoted 5 lines
arch/x86/kernel/setup.c static void __init x86_report_nx(void) · line 848
Quoted 5 lines
arch/x86/kernel/setup.c +4
-0+console_unlock (); +safe_halt (); + } +}
Thanks,
Carlos
Re: [PATCH v2 2/2] x86/panic: Add x86_panic_handler as default post-panic behavior
In reply to [email protected]Quoted 24 lines · On 2025-04-28, [email protected] wrote:
From: Carlos Bilbao <[email protected]>
Add function
x86_panic_handler()as the default behavior for x86 for post-panic stage viapanic_set_handling(). Instead of busy-wait loop, it will halt if there's no console to save CPU cycles.Signed-off-by Carlos Bilbao (DigitalOcean) <[email protected]>--- arch/x86/kernel/setup.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)arch/x86/kernel/setup.c +1
-0@@ -16,6 +16,7 @@#include <linux/initrd.h> #include <linux/iscsi_ibft.h> #include <linux/memblock.h> +#include <linux/panic.h> #include <linux/panic_notifier.h> #include <linux/pci.h> #include <linux/root_dev.h>
arch/x86/kernel/setup.c static void __init x86_report_nx(void) · line 847
Quoted 10 lines
arch/x86/kernel/setup.c +7
-0@@ -837,6 +838,15 @@ static void __init x86_report_nx(void) } } + +static void x86_panic_handler (void ) +{ +if (console_trylock ()) { +console_unlock (); +safe_halt (); + }
I do not understand what you are trying to accomplish with the console_trylock(). At this point in the panic, all the messages are already output. The console lock is totally irrelevant.
Also, the console lock is only valid for legacy consoles.
I see no reason why you don't just use safe_halt() as your panic handler.
John Ogness
Re: [PATCH v2 2/2] x86/panic: Add x86_panic_handler as default post-panic behavior
Hello John,
Quoted 39 lines · John Ogness
Quoted 24 lines · On 2025-04-28, [email protected] wrote:
From: Carlos Bilbao <[email protected]>
Add function
x86_panic_handler()as the default behavior for x86 for post-panic stage viapanic_set_handling(). Instead of busy-wait loop, it will halt if there's no console to save CPU cycles.Signed-off-by Carlos Bilbao (DigitalOcean) <[email protected]>--- arch/x86/kernel/setup.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)arch/x86/kernel/setup.c +1
-0@@ -16,6 +16,7 @@#include <linux/initrd.h> #include <linux/iscsi_ibft.h> #include <linux/memblock.h> +#include <linux/panic.h> #include <linux/panic_notifier.h> #include <linux/pci.h> #include <linux/root_dev.h> arch/x86/kernel/setup.c static void __init x86_report_nx(void) · line 847
Quoted 10 lines
arch/x86/kernel/setup.c +7
-0@@ -837,6 +838,15 @@ static void __init x86_report_nx(void) } } + +static void x86_panic_handler (void ) +{ +if (console_trylock ()) { +console_unlock (); +safe_halt (); + }I do not understand what you are trying to accomplish with the
console_trylock(). At this point in the panic, all the messages are already output. The console lock is totally irrelevant.
Quoted 4 lines
Also, the console lock is only valid for legacy consoles.
I see no reason why you don't just use
safe_halt()as your panic handler.
Yes, in my original implementation I simply used halt, but I was trying to be cautious in case any remaining messages hadn't flushed. I wonder, can we be certain that, as you said, all output (e.g., a backtrace) has already been displayed on screen at this point? I'm not sure.
Quoted 2 lines
John Ogness
Thanks,
Carlos
Re: [PATCH v2 2/2] x86/panic: Add x86_panic_handler as default post-panic behavior
Quoted 8 lines · Carlos Bilbao
Quoted 2 lines
I see no reason why you don't just use
safe_halt()as your panic handler.Yes, in my original implementation I simply used halt, but I was trying to be cautious in case any remaining messages hadn't flushed. I wonder, can we be certain that, as you said, all output (e.g., a backtrace) has already been displayed on screen at this point? I'm not sure.
Well, without this series, the kernel goes into an infinite loop. So if the messages are not all out, they won't magically appear during the infinite loop either.
John
Re: [PATCH v2 2/2] x86/panic: Add x86_panic_handler as default post-panic behavior
Hello,
Quoted 11 lines · John Ogness
Quoted 7 lines · Carlos Bilbao
Quoted 2 lines
I see no reason why you don't just use
safe_halt()as your panic handler.Yes, in my original implementation I simply used halt, but I was trying to be cautious in case any remaining messages hadn't flushed. I wonder, can we be certain that, as you said, all output (e.g., a backtrace) has already been displayed on screen at this point? I'm not sure.
Well, without this series, the kernel goes into an infinite loop. So if the messages are not all out, they won't magically appear during the infinite loop either.
I thought there may be some async stuff going on :)
Quoted 2 lines
John
Thanks,
Carlos