Skip to contentSkip to conversation

efi: efibc: Optionally report panic as a reboot reason

PATCH In review Guilherme G. Piccoli 3 participants 4 messages Nov 8 – Nov 12

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 efi: efibc: Optionally report panic as a reboot reason

The small efibc driver can be used to determine the cause of a reboot, in addition to controlling bootloader behavior. Currently, this driver reports "shutdown" or "reboot" as possible reasons for a system restart.

Add support for panic reporting, gated by a module parameter. If users set the parameter, a panic notifier is registered and tries to save "panic" as the reboot reason, if the kernel is configured to reboot on panic. Note that it is a best-effort approach, relying on the nonblocking version of the EFI variable setter.

---

Hi folks, this is a 2nd attempt to make the kernel notify UEFI about a panic. The first proposal was a new (small) driver, sent here:
https://lore.kernel.org/r/[email protected]/

The idea is to have a simple mechanism, independent of efi-pstore (since users could be using other pstore backends, or do not wish to collect logs for privacy), to let UEFI know a panic happened. Using efibc seems to be a reasonable and simple approach.

The option is gated by a parameter; this could be removed in case it's considered unnecessary. I think a parameter is less invasive than just changing the default.

Feedback / reviews are greatly appreciated.
Thanks in advance,

Guilherme

 drivers/firmware/efi/efibc.c | 46 ++++++++++++++++++++++++++++++++++--
 1 file changed, 44 insertions(+), 2 deletions(-)
drivers/firmware/efi/efibc.c +44 -2
index 345a10aa7086..b4014500513f 100644
@@ -11,9 +11,14 @@
 #include <linux/reboot.h>
 #include <linux/slab.h>
 #include <linux/ucs2_string.h>
+#include <linux/panic_notifier.h>
 
 #define MAX_DATA_LEN	512
 
+static bool report_panic;
+module_param(report_panic, bool, 0444);
+MODULE_PARM_DESC(report_panic, "Report kernel panic as a reboot reason too (default N)");
+
 static int efibc_set_variable(efi_char16_t *name, efi_char16_t *value,
 			      unsigned long len)
 {
@@ -65,6 +70,30 @@ static struct notifier_block efibc_reboot_notifier = {
 	.notifier_call = efibc_reboot_notifier_call,
 };
 
+static int efibc_panic_notifier_call(struct notifier_block *notifier,
+				     unsigned long event, void *data)
+{
+	efi_char16_t *reason = L"panic";
+
+	/* Skip if we aren't rebooting on panic. */
+	if (!panic_timeout)
+		return NOTIFY_DONE;
+
+	efi.set_variable_nonblocking(L"LoaderEntryRebootReason",
+				&LINUX_EFI_LOADER_ENTRY_GUID,
+				EFI_VARIABLE_NON_VOLATILE
+				| EFI_VARIABLE_BOOTSERVICE_ACCESS
+				| EFI_VARIABLE_RUNTIME_ACCESS,
+				ucs2_strlen(reason) * sizeof(efi_char16_t),
+				reason);
+	return NOTIFY_DONE;
+}
+
+static struct notifier_block efibc_panic_nb = {
+	.notifier_call	= efibc_panic_notifier_call,
+	.priority	= INT_MIN,
+};
+
 static int __init efibc_init(void)
 {
 	int ret;
@@ -73,16 +102,29 @@ static int __init efibc_init(void)
 		return -ENODEV;
 
 	ret = register_reboot_notifier(&efibc_reboot_notifier);
-	if (ret)
+	if (ret) {
 		pr_err("unable to register reboot notifier\n");
+		return ret;
+	}
 
-	return ret;
+	if (report_panic && efi.set_variable_nonblocking) {
+		ret = atomic_notifier_chain_register(&panic_notifier_list,
+							&efibc_panic_nb);
+		if (ret)
+			pr_err("unable to register panic notifier\n");
+	}
+
+	return 0;
 }
 module_init(efibc_init);
 
 static void __exit efibc_exit(void)
 {
 	unregister_reboot_notifier(&efibc_reboot_notifier);
+
+	if (report_panic && efi.set_variable_nonblocking)
+		atomic_notifier_chain_unregister(&panic_notifier_list,
+							&efibc_panic_nb);
 }
 module_exit(efibc_exit);
 
Signature · 1 line
2.50.1
#1 lore raw

Cc kexec list

Quoted 118 lines · Guilherme G. Piccoli

The small efibc driver can be used to determine the cause of a reboot, in addition to controlling bootloader behavior. Currently, this driver reports "shutdown" or "reboot" as possible reasons for a system restart.

Add support for panic reporting, gated by a module parameter. If users set the parameter, a panic notifier is registered and tries to save "panic" as the reboot reason, if the kernel is configured to reboot on panic. Note that it is a best-effort approach, relying on the nonblocking version of the EFI variable setter.

---

Hi folks, this is a 2nd attempt to make the kernel notify UEFI about a panic. The first proposal was a new (small) driver, sent here:
https://lore.kernel.org/r/[email protected]/

The idea is to have a simple mechanism, independent of efi-pstore (since users could be using other pstore backends, or do not wish to collect logs for privacy), to let UEFI know a panic happened. Using efibc seems to be a reasonable and simple approach.

The option is gated by a parameter; this could be removed in case it's considered unnecessary. I think a parameter is less invasive than just changing the default.

Feedback / reviews are greatly appreciated.
Thanks in advance,

Guilherme

 drivers/firmware/efi/efibc.c | 46 ++++++++++++++++++++++++++++++++++--
 1 file changed, 44 insertions(+), 2 deletions(-)
drivers/firmware/efi/efibc.c +44 -2
index 345a10aa7086..b4014500513f 100644
@@ -11,9 +11,14 @@
 #include <linux/reboot.h>
 #include <linux/slab.h>
 #include <linux/ucs2_string.h>
+#include <linux/panic_notifier.h>
 
 #define MAX_DATA_LEN	512
 
+static bool report_panic;
+module_param(report_panic, bool, 0444);
+MODULE_PARM_DESC(report_panic, "Report kernel panic as a reboot reason too (default N)");
+
 static int efibc_set_variable(efi_char16_t *name, efi_char16_t *value,
 			      unsigned long len)
 {
@@ -65,6 +70,30 @@ static struct notifier_block efibc_reboot_notifier = {
 	.notifier_call = efibc_reboot_notifier_call,
 };
 
+static int efibc_panic_notifier_call(struct notifier_block *notifier,
+				     unsigned long event, void *data)
+{
+	efi_char16_t *reason = L"panic";
+
+	/* Skip if we aren't rebooting on panic. */
+	if (!panic_timeout)
+		return NOTIFY_DONE;
+
+	efi.set_variable_nonblocking(L"LoaderEntryRebootReason",
+				&LINUX_EFI_LOADER_ENTRY_GUID,
+				EFI_VARIABLE_NON_VOLATILE
+				| EFI_VARIABLE_BOOTSERVICE_ACCESS
+				| EFI_VARIABLE_RUNTIME_ACCESS,
+				ucs2_strlen(reason) * sizeof(efi_char16_t),
+				reason);
+	return NOTIFY_DONE;
+}
+
+static struct notifier_block efibc_panic_nb = {
+	.notifier_call	= efibc_panic_notifier_call,
+	.priority	= INT_MIN,
+};
+
 static int __init efibc_init(void)
 {
 	int ret;
@@ -73,16 +102,29 @@ static int __init efibc_init(void)
 		return -ENODEV;
 
 	ret = register_reboot_notifier(&efibc_reboot_notifier);
-	if (ret)
+	if (ret) {
 		pr_err("unable to register reboot notifier\n");
+		return ret;
+	}
 
-	return ret;
+	if (report_panic && efi.set_variable_nonblocking) {
+		ret = atomic_notifier_chain_register(&panic_notifier_list,
+							&efibc_panic_nb);
+		if (ret)
+			pr_err("unable to register panic notifier\n");
+	}
+
+	return 0;
 }
 module_init(efibc_init);
 
 static void __exit efibc_exit(void)
 {
 	unregister_reboot_notifier(&efibc_reboot_notifier);
+
+	if (report_panic && efi.set_variable_nonblocking)
+		atomic_notifier_chain_unregister(&panic_notifier_list,
+							&efibc_panic_nb);
 }
 module_exit(efibc_exit);
 
#2 lore raw
Quoted 2 lines · Dave Young

Cc kexec list

Thanks a lot - a very good idea. I forgot doing it, my apologies. Cheers,

Guilherme

#3 lore raw
In reply to Guilherme G. Piccoli

It looks good to me.

Quoted 117 lines · Guilherme G. Piccoli

The small efibc driver can be used to determine the cause of a reboot, in addition to controlling bootloader behavior. Currently, this driver reports "shutdown" or "reboot" as possible reasons for a system restart.

Add support for panic reporting, gated by a module parameter. If users set the parameter, a panic notifier is registered and tries to save "panic" as the reboot reason, if the kernel is configured to reboot on panic. Note that it is a best-effort approach, relying on the nonblocking version of the EFI variable setter.

---

Hi folks, this is a 2nd attempt to make the kernel notify UEFI about a panic. The first proposal was a new (small) driver, sent here:
<https://lore.kernel.org/r/[email protected]/>

The idea is to have a simple mechanism, independent of efi-pstore (since users could be using other pstore backends, or do not wish to collect logs for privacy), to let UEFI know a panic happened. Using efibc seems to be a reasonable and simple approach.

The option is gated by a parameter; this could be removed in case it's considered unnecessary. I think a parameter is less invasive than just changing the default.

Feedback / reviews are greatly appreciated.
Thanks in advance,

Guilherme

 drivers/firmware/efi/efibc.c | 46 ++++++++++++++++++++++++++++++++++--
 1 file changed, 44 insertions(+), 2 deletions(-)
drivers/firmware/efi/efibc.c +44 -2
index 345a10aa7086..b4014500513f 100644
@@ -11,9 +11,14 @@
 #include <linux/reboot.h>
 #include <linux/slab.h>
 #include <linux/ucs2_string.h>
+#include <linux/panic_notifier.h>
 
 #define MAX_DATA_LEN 512
 
+static bool report_panic;
+module_param(report_panic, bool, 0444);
+MODULE_PARM_DESC(report_panic, "Report kernel panic as a reboot reason too (default N)");
+
 static int efibc_set_variable(efi_char16_t *name, efi_char16_t *value,
 			      unsigned long len)
 {
@@ -65,6 +70,30 @@ static struct notifier_block efibc_reboot_notifier = {
 	.notifier_call = efibc_reboot_notifier_call,
 };
 
+static int efibc_panic_notifier_call(struct notifier_block *notifier,
+				     unsigned long event, void *data)
+{
+	efi_char16_t *reason = L"panic";
+
+	/* Skip if we aren't rebooting on panic. */
+	if (!panic_timeout)
+		return NOTIFY_DONE;
+
+	efi.set_variable_nonblocking(L"LoaderEntryRebootReason",
+				&LINUX_EFI_LOADER_ENTRY_GUID,
+				EFI_VARIABLE_NON_VOLATILE
+				| EFI_VARIABLE_BOOTSERVICE_ACCESS
+				| EFI_VARIABLE_RUNTIME_ACCESS,
+				ucs2_strlen(reason) * sizeof(efi_char16_t),
+				reason);
+	return NOTIFY_DONE;
+}
+
+static struct notifier_block efibc_panic_nb = {
+	.notifier_call = efibc_panic_notifier_call,
+	.priority	= INT_MIN,
+};
+
 static int __init efibc_init(void)
 {
 	int ret;
@@ -73,16 +102,29 @@ static int __init efibc_init(void)
 		return -ENODEV;
 
 	ret = register_reboot_notifier(&efibc_reboot_notifier);
-	if (ret)
+	if (ret) {
 		pr_err("unable to register reboot notifier\n");
+		return ret;
+	}
 
-	return ret;
+	if (report_panic && efi.set_variable_nonblocking) {
+		ret = atomic_notifier_chain_register(&panic_notifier_list,
+							&efibc_panic_nb);
+		if (ret)
+			pr_err("unable to register panic notifier\n");
+	}
+
+	return 0;
 }
 module_init(efibc_init);
 
 static void __exit efibc_exit(void)
 {
 	unregister_reboot_notifier(&efibc_reboot_notifier);
+
+	if (report_panic && efi.set_variable_nonblocking)
+		atomic_notifier_chain_unregister(&panic_notifier_list,
+							&efibc_panic_nb);
 }
 module_exit(efibc_exit);