[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250429150638.1446781-2-carlos.bilbao@kernel.org>
Date: Tue, 29 Apr 2025 10:06:37 -0500
From: carlos.bilbao@...nel.org
To: tglx@...utronix.de,
seanjc@...gle.com,
jan.glauber@...il.com
Cc: bilbao@...edu,
pmladek@...e.com,
akpm@...ux-foundation.org,
jani.nikula@...el.com,
linux-kernel@...r.kernel.org,
gregkh@...uxfoundation.org,
takakura@...inux.co.jp,
john.ogness@...utronix.de,
Carlos Bilbao <carlos.bilbao@...nel.org>
Subject: [PATCH v3 1/2] panic: Allow for dynamic custom behavior after panic
From: Carlos Bilbao <carlos.bilbao@...nel.org>
Introduce panic_set_handling() to allow overriding the default post-panic
behavior with a priority-based mechanism.
Signed-off-by: Carlos Bilbao (DigitalOcean) <carlos.bilbao@...nel.org>
Reviewed-by: Jan Glauber (DigitalOcean) <jan.glauber@...il.com>
---
include/linux/panic.h | 2 ++
kernel/panic.c | 22 ++++++++++++++++++++++
2 files changed, 24 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..559304546f2e 100644
--- a/kernel/panic.c
+++ b/kernel/panic.c
@@ -276,6 +276,25 @@ static void panic_other_cpus_shutdown(bool crash_kexec)
crash_smp_send_stop();
}
+/*
+ * If set, this function is called after a kernel panic is handled. It can
+ * be assigned using panic_set_handling(), which supports priority-based
+ * logic. For example, specific architectures may provide a default handler
+ * (priority 0) that halts the system to conserve CPU resources.
+ */
+static void (*panic_halt)(void);
+
+static int panic_halt_priority;
+
+void panic_set_handling(void (*fn)(void), int priority)
+{
+ if (panic_halt && priority <= panic_halt_priority)
+ return;
+
+ panic_halt_priority = priority;
+ panic_halt = fn;
+}
+
/**
* panic - halt the system
* @fmt: The text string to print
@@ -467,6 +486,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
Powered by blists - more mailing lists