From: Carlos Bilbao <carlos.bilbao@kernel.org>
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.
Signed-off-by: Carlos Bilbao (DigitalOcean) <carlos.bilbao@kernel.org>
---
arch/x86/kernel/setup.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
index 9d2a13b37833..3bfef55e9adb 100644
--- a/arch/x86/kernel/setup.c
+++ b/arch/x86/kernel/setup.c
@@ -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
--
2.47.1
On 2025-04-28, carlos.bilbao@kernel.org wrote:
> From: Carlos Bilbao <carlos.bilbao@kernel.org>
>
> 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.
>
> Signed-off-by: Carlos Bilbao (DigitalOcean) <carlos.bilbao@kernel.org>
> ---
> arch/x86/kernel/setup.c | 12 ++++++++++++
> 1 file changed, 12 insertions(+)
>
> diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
> index 9d2a13b37833..3bfef55e9adb 100644
> --- a/arch/x86/kernel/setup.c
> +++ b/arch/x86/kernel/setup.c
> @@ -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();
> + }
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
Hello John,
On 4/29/25 11:53, John Ogness wrote:
> On 2025-04-28, carlos.bilbao@kernel.org wrote:
>> From: Carlos Bilbao <carlos.bilbao@kernel.org>
>>
>> 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.
>>
>> Signed-off-by: Carlos Bilbao (DigitalOcean) <carlos.bilbao@kernel.org>
>> ---
>> arch/x86/kernel/setup.c | 12 ++++++++++++
>> 1 file changed, 12 insertions(+)
>>
>> diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
>> index 9d2a13b37833..3bfef55e9adb 100644
>> --- a/arch/x86/kernel/setup.c
>> +++ b/arch/x86/kernel/setup.c
>> @@ -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();
>> + }
> 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.
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.
>
> John Ogness
Thanks,
Carlos
On 2025-04-29, Carlos Bilbao <carlos.bilbao.osdev@gmail.com> wrote: >> 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
Hello, On 4/30/25 02:58, John Ogness wrote: > On 2025-04-29, Carlos Bilbao <carlos.bilbao.osdev@gmail.com> wrote: >>> 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 :) > > John Thanks, Carlos
On Mon, Apr 28, 2025, carlos.bilbao@kernel.org wrote:
> From: Carlos Bilbao <carlos.bilbao@kernel.org>
>
> 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.
>
> Signed-off-by: Carlos Bilbao (DigitalOcean) <carlos.bilbao@kernel.org>
> ---
> arch/x86/kernel/setup.c | 12 ++++++++++++
> 1 file changed, 12 insertions(+)
>
> diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
> index 9d2a13b37833..3bfef55e9adb 100644
> --- a/arch/x86/kernel/setup.c
> +++ b/arch/x86/kernel/setup.c
> @@ -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)
> }
> }
>
> +
Spurious newline.
> +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.
> + console_unlock();
> + safe_halt();
> + }
> +}
Hello,
On 4/29/25 09:57, Sean Christopherson wrote:
> On Mon, Apr 28, 2025, carlos.bilbao@kernel.org wrote:
>> From: Carlos Bilbao <carlos.bilbao@kernel.org>
>>
>> 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.
>>
>> Signed-off-by: Carlos Bilbao (DigitalOcean) <carlos.bilbao@kernel.org>
>> ---
>> arch/x86/kernel/setup.c | 12 ++++++++++++
>> 1 file changed, 12 insertions(+)
>>
>> diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
>> index 9d2a13b37833..3bfef55e9adb 100644
>> --- a/arch/x86/kernel/setup.c
>> +++ b/arch/x86/kernel/setup.c
>> @@ -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)
>> }
>> }
>>
>> +
> Spurious newline.
>
>> +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.
>
>> + console_unlock();
>> + safe_halt();
>> + }
>> +}
Thanks,
Carlos
© 2016 - 2026 Red Hat, Inc.