Introduce arch_do_panic
Subsystems (2)
v3 cover Introduce arch_do_panic
Changes in v3:
- Use __weak functions instead of symbol override via ifdef
- Patch 2: Better explain what is fixed (Bradley Morgan)
- Add statements about print order changes (Bradley Morgan)
- Patch 2: Add a pr_warn() to notify user about on_panic and panic_timeout relationship if they try to assign a trigger to on_panic action while panic_timeout is set to a value other than 0 (Bradley Morgan)
Changes in v2 - Address Sashiko findings;
- Patch 2: Remove unused leftover code
- Patch 2: Mention panic_timeout and shutdown_actions relationship for s390 in commit message
- Patch 3: Use bug.h instead of setup.h to pass around arch_do_panic implementation of sparc
Replace architecture-specific ifdef sections in vpanic() with a clean arch_do_panic() hook. Currently s390 and sparc embed their panic handlers directly in vpanic() using preprocessor conditionals, making the common code path harder to maintain.
Introduce arch_do_panic() as an architecture extension point called at the end of vpanic(). Architectures can use this hook to implement their specific panic handling without polluting the generic panic code.
Move s390 panic handling from the panic_notifier chain to arch_do_panic(). This corrects the execution order so that the panic_timeout is properly evaluated before architecture-specific actions. Reordering also allows more information to be printed during panic a for s390. sys_info(), kmsg_dump_desc(), etc... are now reachable.
Move sparc panic handling from ifdef blocks to arch_do_panic(). Remove the preprocessor conditionals from vpanic() and place the Stop-A enablement code in architecture-specific files where it belongs. Stop-A enablement markers are now printed after "end Kernel panic" line.
The cleanup reduces vpanic() complexity and establishes a pattern for other architectures needing custom panic behavior.
---
Mete Durlu (3):
panic: Introduce arch_do_panic
s390: Implement arch_do_panic
sparc: Implement arch_do_panic
arch/s390/kernel/ipl.c | 19 +++++-------------- arch/sparc/kernel/setup.c | 8 ++++++++ include/linux/panic.h | 2 ++ kernel/panic.c | 15 +++------------ 4 files changed, 18 insertions(+), 26 deletions(-)
---
base-commit: 48a5a7ab8d6ab7090564339e039c421f315de912
change-id: 20260724-arch_do_panic-a97f699aa332
Best regards,
Signature · 1 line
Mete Durlu <[email protected]>
PATCH v3 1/3 panic: Introduce arch_do_panic
Introduce a hook for architectures to put their specific panic handlers. s390 and sparc already has ifdef preprocessor checks to execute architecture specific code. Pave the way for vpanic() cleanup.
--- include/linux/panic.h | 2 ++ kernel/panic.c | 3 +++ 2 files changed, 5 insertions(+)
include/linux/panic.h +2 -0
@@ -110,4 +110,6 @@ extern void add_taint(unsigned flag, enum lockdep_ok);extern int test_taint (unsigned flag);extern unsigned long get_taint (void ); +void arch_do_panic (void ); +#endif /* _LINUX_PANIC_H */
kernel/panic.c +3 -0
@@ -567,6 +567,8 @@ static void panic_other_cpus_shutdown(bool crash_kexec)crash_smp_send_stop (); } +void __weak arch_do_panic (void ) {} +/** * vpanic - halt the system * @fmt: The text string to print
@@ -756,6 +758,7 @@ void vpanic(const char *fmt, va_list args)#endif pr_emerg ("---[ end Kernel panic - not syncing: %s ]---\n" , buf); +arch_do_panic ();/* Do not scroll important messages printed above */ suppress_printk =1 ;
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 1/3] panic: Introduce arch_do_panic
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines
Introduce a hook for architectures to put their specific panic handlers. s390 and sparc already has ifdef preprocessor checks to execute architecture specific code. Pave the way for
vpanic()cleanup.Suggested-by Sven Schnelle <[email protected]>Signed-off-by Mete Durlu <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH v3 1/3] panic: Introduce arch_do_panic
In reply to Mete DurluQuoted 5 lines · Mete Durlu
Introduce a hook for architectures to put their specific panic handlers. s390 and sparc already has ifdef preprocessor checks to execute architecture specific code. Pave the way for
vpanic()cleanup.Suggested-by Sven Schnelle <[email protected]>
Nice.
Quoted 33 lines
Signed-off-by Mete Durlu <[email protected]>---
include/linux/panic.h| 2 ++kernel/panic.c| 3 +++2 files changed, 5 insertions(+)include/linux/panic.h +2
-0@@ -110,4 +110,6 @@ extern void add_taint(unsigned flag, enum lockdep_ok);extern int test_taint (unsigned flag);extern unsigned long get_taint (void ); +void arch_do_panic (void ); +#endif /* _LINUX_PANIC_H */ kernel/panic.c +2
-0@@ -567,6 +567,8 @@ static void panic_other_cpus_shutdown(bool crash_kexec)crash_smp_send_stop (); } +void __weak arch_do_panic (void ) {} +/** * vpanic - halt the system * @fmt: The text string to print
kernel/panic.c void vpanic(const char *fmt, va_list args) · line 764
Quoted 7 lines
kernel/panic.c +1
-0@@ -756,6 +758,7 @@ void vpanic(const char *fmt, va_list args)#endif pr_emerg ("---[ end Kernel panic - not syncing: %s ]---\n" , buf); +arch_do_panic ();/* Do not scroll important messages printed above */ suppress_printk =1 ;
Thanks!
PATCH v3 2/3 s390: Implement arch_do_panic
In reply to Mete Durlus390 has a custom panic handler which carries out user specified actions during a panic scenario. This handler is invoked via the panic_notifier call chain and executed before panic_timeout value is evaluated in common code.
Use arch_do_panic() hook to invoke arch specific panic handling instead of using panic_notifier call chain. By reordering s390's panic handler allow more information to be printed during a panic.
The execution order of panic handlers now allows for user specified panic_timeout value to be taken into account. This fixes the broken "panic" kernel parameter for s390, earlier it was just ignored inexplicibly.
This now means that the panic_timeout value takes precedence over user defined on_panic behavior defined via "chshut" or writing to /sys/firmware/shutdown_actions/on_panic.
--- arch/s390/kernel/ipl.c | 19 +++++-------------- kernel/panic.c | 3 --- 2 files changed, 5 insertions(+), 17 deletions(-)
arch/s390/kernel/ipl.c +5 -14
@@ -2111,11 +2111,15 @@ static ssize_t on_panic_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t len) { + if (panic_timeout) { + pr_warn("on_panic action will be ignored in favor of panic timeout (panic=%d)", + panic_timeout); + }return set_trigger (buf, &on_panic_trigger, len); }static struct kobj_attribute on_panic_attr =__ATTR_RW (on_panic); -static void do_panic (void ) +void arch_do_panic (void ) {lgr_info_log (); on_panic_trigger.action->fn (&on_panic_trigger);
@@ -2331,18 +2335,6 @@ static int __init vmcmd_on_poff_setup(char *str) }__setup ("vmpoff=" , vmcmd_on_poff_setup); -static int on_panic_notify (struct notifier_block *self, -unsigned long event,void *data) -{ -do_panic (); -return NOTIFY_OK ; -} - -static struct notifier_block on_panic_nb = { - .notifier_call = on_panic_notify, - .priority =INT_MIN , -}; -void __init setup_ipl (void ) {BUILD_BUG_ON (sizeof (struct ipl_parameter_block ) !=PAGE_SIZE );
@@ -2375,7 +2367,6 @@ void __init setup_ipl(void)/* We have no info to copy */ break ; } -atomic_notifier_chain_register (&panic_notifier_list, &on_panic_nb); } void __no_stack_protector s390_reset_system(void)
kernel/panic.c +0 -3
@@ -752,9 +752,6 @@ void vpanic(const char *fmt, va_list args)pr_emerg ("Press Stop-A (L1-A) from sun keyboard or send break\n" "twice on console to return to the boot prom\n"); } -#endif -#if defined(CONFIG_S390 ) -disabled_wait ();#endif pr_emerg ("---[ end Kernel panic - not syncing: %s ]---\n" , buf);
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 2/3] s390: Implement arch_do_panic
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] The `pr_warn()` call in `on_panic_store()` is missing a terminating newline (`\n`), which will…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The pr_warn() call in on_panic_store() is missing a terminating newline (\n), which will cause garbled console output.
- [Low] The file arch/s390/kernel/ipl.c does not include <linux/panic.h>, leading to a missing prototype compiler warning for arch_do_panic(). --
--- Patch [2]: [PATCH v3 2/3] s390: Implement arch_do_panic --- commit fa5fa116f1eec4d70650b8cf3a838d2dff72e007
Author: Mete Durlu <[email protected]>
s390: Implement arch_do_panic
This commit replaces the s390 custom panic notifier handler with the arch_do_panic() hook. This reordering allows the user-specified panic_timeout value to be evaluated correctly in common code. The timeout now takes precedence over user-defined on_panic behavior.
arch/s390/kernel/ipl.c static ssize_t on_panic_store(struct kobject *kobj, · line 2116
Quoted 11 lines
arch/s390/kernel/ipl.c +3
-0@@ -2111,11 +2111,15 @@ static ssize_t on_panic_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t len) { + if (panic_timeout) { + pr_warn("on_panic action will be ignored in favor of panic timeout (panic=%d)", + panic_timeout);
[Severity: Medium]
Will this result in garbled console output since there is no terminating newline in the format string? Because printk does not automatically append a newline, the next message printed to the kernel log might be appended to this same line.
arch/s390/kernel/ipl.c static ssize_t on_panic_store(struct kobject *kobj, · line 2122
Quoted 7 lines
arch/s390/kernel/ipl.c +2
-1+ }return set_trigger (buf, &on_panic_trigger, len); }static struct kobj_attribute on_panic_attr =__ATTR_RW (on_panic); -static void do_panic (void ) +void arch_do_panic (void )
[Severity: Low]
Does this trigger a missing prototype compiler warning? The global function arch_do_panic() is defined here to override the weak definition, but its declaration is in linux/panic.h which does not seem to be included in this file.
Quoted 3 lines
arch/s390/kernel/ipl.c +0
-0{lgr_info_log (); on_panic_trigger.action->fn (&on_panic_trigger);
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Heiko Carstens
maintainerRe: [PATCH v3 2/3] s390: Implement arch_do_panic
In reply to Mete DurluQuoted 17 lines · Mete Durlu
s390 has a custom panic handler which carries out user specified actions during a panic scenario. This handler is invoked via the
panic_notifiercall chain and executed beforepanic_timeoutvalue is evaluated in common code.Use
arch_do_panic()hook to invoke arch specific panic handling instead of usingpanic_notifiercall chain. By reordering s390's panic handler allow more information to be printed during a panic.
The execution order of panic handlers now allows for user specifiedpanic_timeoutvalue to be taken into account. This fixes the broken "panic" kernel parameter for s390, earlier it was just ignored inexplicibly.This now means that the
panic_timeoutvalue takes precedence over user definedon_panicbehavior defined via "chshut" or writing to/sys/firmware/shutdown_actions/on_panic.
Quoted 7 lines
Fixes ff6b8ea68f4b ("[S390] ipl/dump on panic.")Suggested-by Sven Schnelle <[email protected]>Signed-off-by Mete Durlu <[email protected]>--- arch/s390/kernel/ipl.c | 19 +++++-------------- kernel/panic.c | 3 --- 2 files changed, 5 insertions(+), 17 deletions(-)
So, finally I took a closer look :)
Question: why is it desirable that panic_timeout takes precedence? The result of this change is quite surprising: if anybody (e.g. a distribution) sets CONFIG_PANIC_TIMEOUT to a non-zero value this completely breaks "on_panic" behaviour on s390.
I could understand if this change would result in a larger timeout and additional information being printed, but not that it breaks existing and actually designed and desired behaviour.
This change also makes it more likely that the system deadlocks on console messages, before the actual arch_do_panic() is called, if I'm not mistaken. Which would also be a regression.
What I like about this patch set is that it removes architecture dependent ifdefs from common code. But the side effects are very questionable.
The "obvious" cleanup would be to move only the existing ifdef'ed code into arch_do_panic(), and only then provide semantical changes, which wouldn't need to be part of such a cleanup series.
Quoted 5 lines
arch/s390/kernel/ipl.c +0
-0
arch/s390/kernel/ipl.c static ssize_t on_panic_store(struct kobject *kobj, · line 2119
Quoted 9 lines
arch/s390/kernel/ipl.c +4
-0@@ -2111,11 +2111,15 @@ static ssize_t on_panic_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t len) { + if (panic_timeout) { + pr_warn("on_panic action will be ignored in favor of panic timeout (panic=%d)", + panic_timeout); + }return set_trigger (buf, &on_panic_trigger, len); }
I'm wondering why AI doesn't complain about this user trigger-able warning message. This is not good. *If* we go this way, writing to this attribute should simply fail, instead of giving the user the impression that something has been configured, which would actually do something.
Re: [PATCH v3 2/3] s390: Implement arch_do_panic
Quoted 26 lines · Heiko Carstens
Quoted 17 lines · Mete Durlu
s390 has a custom panic handler which carries out user specified actions during a panic scenario. This handler is invoked via the
panic_notifiercall chain and executed beforepanic_timeoutvalue is evaluated in common code.Use
arch_do_panic()hook to invoke arch specific panic handling instead of usingpanic_notifiercall chain. By reordering s390's panic handler allow more information to be printed during a panic.
The execution order of panic handlers now allows for user specifiedpanic_timeoutvalue to be taken into account. This fixes the broken "panic" kernel parameter for s390, earlier it was just ignored inexplicibly.This now means that the
panic_timeoutvalue takes precedence over user definedon_panicbehavior defined via "chshut" or writing to/sys/firmware/shutdown_actions/on_panic.Quoted 7 lines
Fixes ff6b8ea68f4b ("[S390] ipl/dump on panic.")Suggested-by Sven Schnelle <[email protected]>Signed-off-by Mete Durlu <[email protected]>--- arch/s390/kernel/ipl.c | 19 +++++-------------- kernel/panic.c | 3 --- 2 files changed, 5 insertions(+), 17 deletions(-)
Quoted 6 lines
So, finally I took a closer look :)
Question: why is it desirable that
panic_timeouttakes precedence? The result of this change is quite surprising: if anybody (e.g. a distribution) setsCONFIG_PANIC_TIMEOUTto a non-zero value this completely breaks "on_panic" behaviour on s390.
panic timeout can be set during boot or compile time as you said, so it can be used to determine what will happen to a system if it panics during boot along with after boot. Since panic timeout covers a larger area I thought it should get precedence.
Being able to choose what will happen on panic before boot is a super power IMO and would help immensely if one would like to boot an untested kernel via kexec for example.
Quoted 3 lines
I could understand if this change would result in a larger timeout and additional information being printed, but not that it breaks existing and actually designed and desired behaviour.
For that to happen users have to "misconfigure" the system and try to use both panic_timeout and a custom "on_panic" action. The same goes for kdump, when kdump is configured "on_panic" actions are ignored silently and system always dumps on panic.
Quoted 3 lines
This change also makes it more likely that the system deadlocks on console messages, before the actual
arch_do_panic()is called, if I'm not mistaken. Which would also be a regression.
This I wasn't aware of, I don't understand how it can deadlock on console messages. I will look this up.
Quoted 6 lines
What I like about this patch set is that it removes architecture dependent ifdefs from common code. But the side effects are very questionable.
The "obvious" cleanup would be to move only the existing ifdef'ed code into
arch_do_panic(), and only then provide semantical changes, which wouldn't need to be part of such a cleanup series.
ifdef'ed code for s390 is unreachable as the code called by panic notifiers aka "on_panic_trigger" do the same regardless of the action it is configured to and stops with disabled_wait().
If that should be the way, I can remove the ifdef s390 chunk from vpanic and propose the panic_timeout changes in a separate patch.
Quoted 11 lines
Quoted 11 lines
arch/s390/kernel/ipl.c +3
-0@@ -2111,11 +2111,15 @@ static ssize_t on_panic_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t len) { + if (panic_timeout) { + pr_warn("on_panic action will be ignored in favor of panic timeout (panic=%d)", + panic_timeout);
Quoted 8 lines
arch/s390/kernel/ipl.c static ssize_t on_panic_store(struct kobject *kobj, · line 2119
Quoted 3 lines
arch/s390/kernel/ipl.c +1
-0+ }return set_trigger (buf, &on_panic_trigger, len); }I'm wondering why AI doesn't complain about this user trigger-able warning message. This is not good. *If* we go this way, writing to this attribute should simply fail, instead of giving the user the impression that something has been configured, which would actually do something.
My thought process was to put some marker to dmesg for users to figure out what happened and why their system didn't respect to their "on_panic" action. I know this is subobtimal but simply failing the write to "on_panic" doesn't provide a good solution IMO as users can first write to "on_panic" and then set a value to panic_timeout from sysctl.
Regardless, I think I can remove semantic changes for s390 from this patchset and submit them from a different patch series like you suggested.
Thank you for your input Heiko!
Heiko Carstens
maintainerRe: [PATCH v3 2/3] s390: Implement arch_do_panic
Quoted 6 lines · Mete Durlu
Quoted 4 lines · Heiko Carstens
Question: why is it desirable that
panic_timeouttakes precedence? The result of this change is quite surprising: if anybody (e.g. a distribution) setsCONFIG_PANIC_TIMEOUTto a non-zero value this completely breaks "on_panic" behaviour on s390.
Quoted 7 lines
panic timeout can be set during boot or compile time as you said, so it can be used to determine what will happen to a system if it panics during boot along with after boot. Since panic timeout covers a larger area I thought it should get precedence.
Being able to choose what will happen on panic before boot is a super power IMO and would help immensely if one would like to boot an untested kernel via kexec for example.
Wouldn't it then not make more sense to add an s390 specific kernel command line parameter which _disables_ the notifier entirely, and then you get your panic_timeout working?
Then you have to explicitely state what you want and don't get suprised by subtle semantic changes.
Quoted 8 lines
Quoted 3 lines
I could understand if this change would result in a larger timeout and additional information being printed, but not that it breaks existing and actually designed and desired behaviour.
For that to happen users have to "misconfigure" the system and try to use both
panic_timeoutand a custom "on_panic" action. The same goes for kdump, when kdump is configured "on_panic" actions are ignored silently and system always dumps on panic.
This is really not the same. If setting panic_timeout would disable something else that would be surprising at least, especially since it never did before.
PATCH v3 3/3 sparc: Implement arch_do_panic
In reply to Mete DurluImplement sparc specific arch_do_panic() instead of using sparc specific ifdef sections in vpanic() code.
Reorder arch specific panic handling, sparc's Stop-A messages are now printed after "end Kernel panic" marker.
--- arch/sparc/kernel/setup.c | 8 ++++++++ kernel/panic.c | 9 --------- 2 files changed, 8 insertions(+), 9 deletions(-)
arch/sparc/kernel/setup.c +8 -0
@@ -36,6 +36,14 @@ static const struct ctl_table sparc_sysctl_table[] = {#endif }; +void arch_do_panic (void ) +{ +extern int stop_a_enabled; +/* Make sure the user can actually press Stop-A (L1-A) */ + stop_a_enabled =1 ; +pr_emerg ("Press Stop-A (L1-A) from sun keyboard or send break\n" +"twice on console to return to the boot prom\n" ); +}static int __init init_sparc_sysctls (void ) {
kernel/panic.c +0 -9
@@ -744,15 +744,6 @@ void vpanic(const char *fmt, va_list args) reboot_mode = panic_reboot_mode;emergency_restart (); } -#ifdef __sparc__ - { -extern int stop_a_enabled; -/* Make sure the user can actually press Stop-A (L1-A) */ - stop_a_enabled =1 ; -pr_emerg ("Press Stop-A (L1-A) from sun keyboard or send break\n" -"twice on console to return to the boot prom\n" ); - } -#endif pr_emerg ("---[ end Kernel panic - not syncing: %s ]---\n" , buf);arch_do_panic ();
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 3/3] sparc: Implement arch_do_panic
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Low] Missing explicit `#include <linux/panic.h>` and `#include <linux/printk.h>` in…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Low] Missing explicit #include <linux/panic.h> and #include <linux/printk.h> in arch/sparc/kernel/setup.c.
- [Low] Redundant local block-scoped extern declaration for stop_a_enabled.
--
--- Patch [3]: [PATCH v3 3/3] sparc: Implement arch_do_panic --- commit 96c609160e8ec354a006cf513eb7fe2d614de6bc
Author: Mete Durlu <[email protected]>
sparc: Implement arch_do_panic
This commit implements a sparc specific arch_do_panic() instead of using ifdef sections in vpanic() code. It also reorders arch specific panic handling.
Quoted 4 lines
arch/sparc/kernel/setup.c +0
-0
[ ... ]
arch/sparc/kernel/setup.c static const struct ctl_table sparc_sysctl_table[] = { · line 40
Quoted 6 lines
arch/sparc/kernel/setup.c +2
-0@@ -36,6 +36,14 @@ static const struct ctl_table sparc_sysctl_table[] = {#endif }; +void arch_do_panic (void ) +{
[Severity: Low]
Should this file explicitly include <linux/panic.h> and <linux/printk.h>?
Since arch_do_panic() is a globally exported function, missing the panic.h header can cause a missing prototypes warning during compilation. It might also be better to explicitly include printk.h for pr_emerg() rather than relying on implicit header inclusions.
arch/sparc/kernel/setup.c static const struct ctl_table sparc_sysctl_table[] = { · line 41
Quoted 1 line
arch/sparc/kernel/setup.c +1
-0+extern int stop_a_enabled;
[Severity: Low]
Is this block-scoped extern declaration still needed?
While this declaration was copied from its old location in kernel/panic.c, it is redundant here because arch/sparc/kernel/setup.c already includes <asm/setup.h>, which provides the exact same global extern declaration for stop_a_enabled.
Quoted 5 lines
arch/sparc/kernel/setup.c +5
-0+/* Make sure the user can actually press Stop-A (L1-A) */ + stop_a_enabled =1 ; +pr_emerg ("Press Stop-A (L1-A) from sun keyboard or send break\n" +"twice on console to return to the boot prom\n" ); +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3