[PATCH v2 1/2] panic: Allow for dynamic custom behavior after panic

carlos.bilbao@kernel.org posted 2 patches 9 months, 2 weeks ago
[PATCH v2 1/2] panic: Allow for dynamic custom behavior after panic
Posted by carlos.bilbao@kernel.org 9 months, 2 weeks ago
From: Carlos Bilbao <carlos.bilbao@kernel.org>

Introduce panic_set_handling() to allow overriding the default post-panic
behavior.

Signed-off-by: Carlos Bilbao (DigitalOcean) <carlos.bilbao@kernel.org>
---
 include/linux/panic.h |  2 ++
 kernel/panic.c        | 27 +++++++++++++++++++++++++++
 2 files changed, 29 insertions(+)

diff --git a/include/linux/panic.h b/include/linux/panic.h
index 2494d51707ef..cf8d4a944407 100644
--- a/include/linux/panic.h
+++ b/include/linux/panic.h
@@ -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 */
diff --git a/kernel/panic.c b/kernel/panic.c
index a3889f38153d..2cdd83b4afb6 100644
--- a/kernel/panic.c
+++ b/kernel/panic.c
@@ -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();
-- 
2.47.1
Re: [PATCH v2 1/2] panic: Allow for dynamic custom behavior after panic
Posted by Sean Christopherson 9 months, 2 weeks ago
On Mon, Apr 28, 2025, carlos.bilbao@kernel.org wrote:
> diff --git a/kernel/panic.c b/kernel/panic.c
> index a3889f38153d..2cdd83b4afb6 100644
> --- a/kernel/panic.c
> +++ b/kernel/panic.c
> @@ -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;

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.

> +static int panic_hlt_priority;

Uber nit, pick one of halt or hlt.

> +
> +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)

> +		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.

> +
>  /**
>   *	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();
> -- 
> 2.47.1
>
Re: [PATCH v2 1/2] panic: Allow for dynamic custom behavior after panic
Posted by Carlos Bilbao 9 months, 2 weeks ago
Hello,


Sean, thanks for getting back to me so fast!


On 4/29/25 09:54, Sean Christopherson wrote:
> On Mon, Apr 28, 2025, carlos.bilbao@kernel.org wrote:
>> diff --git a/kernel/panic.c b/kernel/panic.c
>> index a3889f38153d..2cdd83b4afb6 100644
>> --- a/kernel/panic.c
>> +++ b/kernel/panic.c
>> @@ -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;
> 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.


Agreed.


>
>> +static int panic_hlt_priority;
> Uber nit, pick one of halt or hlt.


True, will use halt.


>> +
>> +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)
>
>> +		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.
>
>> +
>>  /**
>>   *	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();
>> -- 
>> 2.47.1
>>

Agree with all. Will fix in v3.


Thanks,

Carlos