Skip to contentSkip to conversation

The UEFI panic notification mechanism, 2nd round

PATCH In review Guilherme G. Piccoli 2 participants 7 messages Jul 29, 2022 – Sep 9, 2022

Subsystems (2)
lore
Share & tools
lore

Guilherme G. Piccoli

author reviewer
#0 lore raw

v2 cover The UEFI panic notification mechanism, 2nd round

Hey folks, this is the 2nd iteration of the patchset adding a simple mechanism to notify the UEFI firmware about a panic event in the kernel. V1 here:
https://lore.kernel.org/lkml/[email protected]/

First thing, the differences in this V2:

- Ardb response in V1 mentioned a refactor aimed for v5.20 that removes an obsolete/confusing way of setting EFI variables - this led to a weird condition, deleted variables stayed in sysfs after deletion. Well, I've refactored the code based on efi/next, so I'm using the recommended API now - thanks a bunch for the advice Ardb!

- I've changed NULL-terminating char in patch 1 to the format I've seen in Ardb's code, L'\0'.

- Patch 2 is new, it's somewhat a fix for a patch only in efi/next, part of the efivar refactor.

In the V1 review, it was mentioned we could maybe use efi-pstore as a way to signal the firmware about a panic event - in the end, the efi-pstore mechanism can collect a dmesg, so it's even richer in the information level. But I disagree that it is the way to go, for 3 main reasons:

a) efi-pstore could be impossible to use, if the users are already using another pstore backend (like ramoops), which is _exactly_ our case! Of course, we could rework pstore and allow 2 backends, quite a bit of work, but...see next points!

b) Even if (a) is a not an issue, we have another one, even more important:
signaling the firmware about a panic is *different* than collecting a bunch of data, a full dmesg even. This could be considered a security issue for some users; also, the dmesg collected consumes a bunch more memory in the (potentially scarce) UEFI available memory.
Although related, the goal of pstore is orthogonal to our mechanism here:
users rely on pstore to collect data, our proposal is a simple infrastructure to just let the firmware know about a panic. Our kernel module also shows a message and automatically clears the UEFI variable, so it tracks a single panic, whereas efi-pstore logs are kept by default, in order to provide data to users.

c) Finally, it's faster and less "invasive"/risky to just write a byte in a variable on a panic event than having a ksmg dumper collecting the full dmesg and writing it to the UEFI memory; again, some users wish to have the logs, but not all of them.

With all of that said, I think this module makes sense, it's a very simple solution that opens doors to firmware panic handling approaches, like in our proposed case (a different splash screen on panic).

Finally, the variable name (PanicWarn) and value (0xFF by default, can be changed by a module parameter) are just my personal choices but I'm open to suggestions, not strongly attached to them heh

Thanks again for the reviews/suggestions!
Cheers,

Guilherme

Guilherme G. Piccoli (3):
efi: Add a generic helper to convert strings to unicode
efi: efibc: Guard against allocation failure
efi-panic: Introduce the UEFI panic notification mechanism

 drivers/firmware/efi/Kconfig      | 10 ++++
 drivers/firmware/efi/Makefile     |  1 +
 drivers/firmware/efi/efi-panic.c  | 89 +++++++++++++++++++++++++++++++
 drivers/firmware/efi/efi-pstore.c |  4 +-
 drivers/firmware/efi/efibc.c      | 11 ++--
 include/linux/efi.h               | 18 +++++++
 6 files changed, 125 insertions(+), 8 deletions(-)
 create mode 100644 drivers/firmware/efi/efi-panic.c
Signature · 1 line
2.37.1
author reviewer
#1 lore raw

PATCH v2 1/3 efi: Add a generic helper to convert strings to unicode

Currently both efi-pstore and efibc rely in simple for-loops to convert from regular char pointers to u16/unicode strings; so let's export this functionality as a helper to common EFI code to prevent code duplication. This helper will also be used in a subsequent patch (adding a new module).

Notice that efi-pstore didn't write the end NULL char in the unicode string before this patch, but this should not change anything in a relevant way for this module - other than that, no functional change is expected.

---
 drivers/firmware/efi/efi-pstore.c |  4 +---
 drivers/firmware/efi/efibc.c      |  9 ++++-----
 include/linux/efi.h               | 17 +++++++++++++++++
 3 files changed, 22 insertions(+), 8 deletions(-)
drivers/firmware/efi/efi-pstore.c +1 -3
index 3bddc152fcd4..965a4c0d6e60 100644
@@ -167,7 +167,6 @@ static int efi_pstore_write(struct pstore_record *record)
 	char name[DUMP_NAME_LEN];
 	efi_char16_t efi_name[DUMP_NAME_LEN];
 	efi_status_t status;
-	int i;
 
 	record->id = generic_id(record->time.tv_sec, record->part,
 				record->count);
@@ -180,8 +179,7 @@ static int efi_pstore_write(struct pstore_record *record)
 		 (long long)record->time.tv_sec,
 		 record->compressed ? 'C' : 'D');
 
-	for (i = 0; i < DUMP_NAME_LEN; i++)
-		efi_name[i] = name[i];
+	efi_str8_to_str16(efi_name, name, DUMP_NAME_LEN - 1);
 
 	if (efivar_trylock())
 		return -EBUSY;
drivers/firmware/efi/efibc.c +4 -5
index 8ced7af8e56d..7e3bf60d24e0 100644
@@ -39,7 +39,7 @@ static int efibc_reboot_notifier_call(struct notifier_block *notifier,
 						    : L"shutdown";
 	const u8 *str = data;
 	efi_char16_t *wdata;
-	unsigned long l;
+	size_t len;
 	int ret;
 
 	ret = efibc_set_variable(L"LoaderEntryRebootReason", reason,
@@ -48,11 +48,10 @@ static int efibc_reboot_notifier_call(struct notifier_block *notifier,
 		return NOTIFY_DONE;
 
 	wdata = kmalloc(MAX_DATA_LEN * sizeof(efi_char16_t), GFP_KERNEL);
-	for (l = 0; l < MAX_DATA_LEN - 1 && str[l] != '\0'; l++)
-		wdata[l] = str[l];
-	wdata[l] = L'\0';
 
-	efibc_set_variable(L"LoaderEntryOneShot", wdata, l);
+	len = efi_str8_to_str16(wdata, str, MAX_DATA_LEN - 1);
+
+	efibc_set_variable(L"LoaderEntryOneShot", wdata, len);
 
 	kfree(wdata);
 	return NOTIFY_DONE;
include/linux/efi.h +17 -0
index d2b84c2fec39..c0ea01be3772 100644
@@ -1030,6 +1030,23 @@ struct efivars {
 
 #define EFI_VAR_NAME_LEN	1024
 
+/*
+ * Helper that converts regular char buffer to u16 unicode-like strings;
+ * notice that the unicode buffer requires to be at least len+1 in size.
+ */
+static inline size_t
+efi_str8_to_str16(efi_char16_t *str16, const char *str8, size_t len)
+{
+	size_t i;
+
+	for (i = 0; i < len && str8[i] != '\0'; i++)
+		str16[i] = str8[i];
+
+	str16[i] = L'\0';
+
+	return i;
+}
+
 int efivars_register(struct efivars *efivars,
 		     const struct efivar_operations *ops,
 		     struct kobject *kobject);
Signature · 1 line
2.37.1
author reviewer
#2 lore raw

PATCH v2 2/3 efi: efibc: Guard against allocation failure

In reply to Guilherme G. Piccoli

There is a single kmalloc in this driver, and it's not currently guarded against allocation failure. Do it here by just letting the reboot handler to proceed, in case this tentative allocation fails.

Fixes 416581e48679 ("efi: efibc: avoid efivar API for setting variables")

---

Notice the sha-1 hash of the efibc patch we're fixing is from efi/next, might change in upstream once such patch is merged.
Feel free to remove the fixes tag if it makes sense, no issues from me =)

 drivers/firmware/efi/efibc.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/firmware/efi/efibc.c +2 -0
index 7e3bf60d24e0..9a8d914f91a6 100644
@@ -48,6 +48,8 @@ static int efibc_reboot_notifier_call(struct notifier_block *notifier,
 		return NOTIFY_DONE;
 
 	wdata = kmalloc(MAX_DATA_LEN * sizeof(efi_char16_t), GFP_KERNEL);
+	if (!wdata)
+		return NOTIFY_DONE;
 
 	len = efi_str8_to_str16(wdata, str, MAX_DATA_LEN - 1);
 
Signature · 1 line
2.37.1
author reviewer
#3 lore raw

PATCH v2 3/3 efi-panic: Introduce the UEFI panic notification mechanism

In reply to Guilherme G. Piccoli

Despite we have pstore, kdump and other panic-time mechanisms, there's no generic way to let the firmware know a panic event happened. Some hypervisors might have special code for that, but that's not generic.

The UEFI panic notification module hereby proposed aims to fill this need: once we face a panic, through the panic notifier infrastructure we set a UEFI variable named PanicWarn with a value - default is 0xFF, but it's adjustable via module parameter. The firmware is then enabled to take a proper action.

The module also let the users know that last execution likely panicked if the UEFI variable is present on module load - then it clears that to avoid confusing users, only the last panic event is recorded.

---
 drivers/firmware/efi/Kconfig     | 10 ++++
 drivers/firmware/efi/Makefile    |  1 +
 drivers/firmware/efi/efi-panic.c | 89 ++++++++++++++++++++++++++++++++
 include/linux/efi.h              |  1 +
 4 files changed, 101 insertions(+)
 create mode 100644 drivers/firmware/efi/efi-panic.c
drivers/firmware/efi/Kconfig +10 -0
index 6cb7384ad2ac..f1ed6128d623 100644
@@ -147,6 +147,16 @@ config EFI_BOOTLOADER_CONTROL
 	  bootloader reads this reboot reason and takes particular
 	  action according to its policy.
 
+config EFI_PANIC_NOTIFIER
+	tristate "UEFI Panic Notifier"
+	depends on EFI
+	default n
+	help
+	  With this module, kernel creates the UEFI variable "PanicWarn" if
+	  the system faces a panic event. With that, the firmware is entitled
+	  to take an action if the last kernel panicked; it also shows a
+	  message during boot time if last run faced a panic event.
+
 config EFI_CAPSULE_LOADER
 	tristate "EFI capsule loader"
 	depends on EFI && !IA64
drivers/firmware/efi/Makefile +1 -0
index 8d151e332584..c7cf3dcb284e 100644
@@ -25,6 +25,7 @@ obj-$(CONFIG_EFI_RUNTIME_WRAPPERS)	+= runtime-wrappers.o
 subdir-$(CONFIG_EFI_STUB)		+= libstub
 obj-$(CONFIG_EFI_FAKE_MEMMAP)		+= fake_map.o
 obj-$(CONFIG_EFI_BOOTLOADER_CONTROL)	+= efibc.o
+obj-$(CONFIG_EFI_PANIC_NOTIFIER)	+= efi-panic.o
 obj-$(CONFIG_EFI_TEST)			+= test/
 obj-$(CONFIG_EFI_DEV_PATH_PARSER)	+= dev-path-parser.o
 obj-$(CONFIG_APPLE_PROPERTIES)		+= apple-properties.o
drivers/firmware/efi/efi-panic.c new file +89 -0

89 lines not on this page · raw message

include/linux/efi.h +1 -0
index c0ea01be3772..38a5056cc72a 100644
@@ -367,6 +367,7 @@ void efi_native_runtime_setup(void);
 #define EFI_GLOBAL_VARIABLE_GUID		EFI_GUID(0x8be4df61, 0x93ca, 0x11d2,  0xaa, 0x0d, 0x00, 0xe0, 0x98, 0x03, 0x2b, 0x8c)
 #define UV_SYSTEM_TABLE_GUID			EFI_GUID(0x3b13a7d4, 0x633e, 0x11dd,  0x93, 0xec, 0xda, 0x25, 0x56, 0xd8, 0x95, 0x93)
 #define LINUX_EFI_CRASH_GUID			EFI_GUID(0xcfc8fc79, 0xbe2e, 0x4ddc,  0x97, 0xf0, 0x9f, 0x98, 0xbf, 0xe2, 0x98, 0xa0)
+#define LINUX_EFI_PANIC_WARN_GUID		EFI_GUID(0x9e85b665, 0x08d4, 0x42c9,  0x83, 0x24, 0x47, 0xed, 0x5f, 0xe5, 0x0b, 0xf3)
 #define LOADED_IMAGE_PROTOCOL_GUID		EFI_GUID(0x5b1b31a1, 0x9562, 0x11d2,  0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b)
 #define EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID	EFI_GUID(0x9042a9de, 0x23dc, 0x4a38,  0x96, 0xfb, 0x7a, 0xde, 0xd0, 0x80, 0x51, 0x6a)
 #define EFI_UGA_PROTOCOL_GUID			EFI_GUID(0x982c298b, 0xf4fa, 0x41cb,  0xb8, 0x38, 0x77, 0xaa, 0x68, 0x8f, 0xb8, 0x39)
Signature · 1 line
2.37.1
author reviewer
#4 lore raw
In reply to Guilherme G. Piccoli
Quoted 57 lines · Guilherme G. Piccoli

Hey folks, this is the 2nd iteration of the patchset adding a simple mechanism to notify the UEFI firmware about a panic event in the kernel. V1 here:
https://lore.kernel.org/lkml/[email protected]/

First thing, the differences in this V2:

- Ardb response in V1 mentioned a refactor aimed for v5.20 that removes an obsolete/confusing way of setting EFI variables - this led to a weird condition, deleted variables stayed in sysfs after deletion. Well, I've refactored the code based on efi/next, so I'm using the recommended API now - thanks a bunch for the advice Ardb!

- I've changed NULL-terminating char in patch 1 to the format I've seen in Ardb's code, L'\0'.

- Patch 2 is new, it's somewhat a fix for a patch only in efi/next, part of the efivar refactor.

In the V1 review, it was mentioned we could maybe use efi-pstore as a way to signal the firmware about a panic event - in the end, the efi-pstore mechanism can collect a dmesg, so it's even richer in the information level. But I disagree that it is the way to go, for 3 main reasons:

a) efi-pstore could be impossible to use, if the users are already using another pstore backend (like ramoops), which is _exactly_ our case! Of course, we could rework pstore and allow 2 backends, quite a bit of work, but...see next points!

b) Even if (a) is a not an issue, we have another one, even more important:
signaling the firmware about a panic is *different* than collecting a bunch of data, a full dmesg even. This could be considered a security issue for some users; also, the dmesg collected consumes a bunch more memory in the (potentially scarce) UEFI available memory.
Although related, the goal of pstore is orthogonal to our mechanism here:
users rely on pstore to collect data, our proposal is a simple infrastructure to just let the firmware know about a panic. Our kernel module also shows a message and automatically clears the UEFI variable, so it tracks a single panic, whereas efi-pstore logs are kept by default, in order to provide data to users.

c) Finally, it's faster and less "invasive"/risky to just write a byte in a variable on a panic event than having a ksmg dumper collecting the full dmesg and writing it to the UEFI memory; again, some users wish to have the logs, but not all of them.

With all of that said, I think this module makes sense, it's a very simple solution that opens doors to firmware panic handling approaches, like in our proposed case (a different splash screen on panic).

Finally, the variable name (PanicWarn) and value (0xFF by default, can be changed by a module parameter) are just my personal choices but I'm open to suggestions, not strongly attached to them heh

Quoted 7 lines

Thanks again for the reviews/suggestions!
Cheers,

Guilherme

Hi Ard, sorry for the ping =]

Any opinions in this one? Patch 2 is a simple fix, BTW. Cheers,

Guilherme

maintainer
#5 lore raw
Quoted 63 lines · Guilherme G. Piccoli
Quoted 61 lines · Guilherme G. Piccoli

Hey folks, this is the 2nd iteration of the patchset adding a simple mechanism to notify the UEFI firmware about a panic event in the kernel. V1 here:
https://lore.kernel.org/lkml/[email protected]/

First thing, the differences in this V2:

- Ardb response in V1 mentioned a refactor aimed for v5.20 that removes an obsolete/confusing way of setting EFI variables - this led to a weird condition, deleted variables stayed in sysfs after deletion. Well, I've refactored the code based on efi/next, so I'm using the recommended API now - thanks a bunch for the advice Ardb!

- I've changed NULL-terminating char in patch 1 to the format I've seen in Ardb's code, L'\0'.

- Patch 2 is new, it's somewhat a fix for a patch only in efi/next, part of the efivar refactor.

In the V1 review, it was mentioned we could maybe use efi-pstore as a way to signal the firmware about a panic event - in the end, the efi-pstore mechanism can collect a dmesg, so it's even richer in the information level. But I disagree that it is the way to go, for 3 main reasons:

a) efi-pstore could be impossible to use, if the users are already using another pstore backend (like ramoops), which is _exactly_ our case! Of course, we could rework pstore and allow 2 backends, quite a bit of work, but...see next points!

b) Even if (a) is a not an issue, we have another one, even more important:
signaling the firmware about a panic is *different* than collecting a bunch of data, a full dmesg even. This could be considered a security issue for some users; also, the dmesg collected consumes a bunch more memory in the (potentially scarce) UEFI available memory.
Although related, the goal of pstore is orthogonal to our mechanism here:
users rely on pstore to collect data, our proposal is a simple infrastructure to just let the firmware know about a panic. Our kernel module also shows a message and automatically clears the UEFI variable, so it tracks a single panic, whereas efi-pstore logs are kept by default, in order to provide data to users.

c) Finally, it's faster and less "invasive"/risky to just write a byte in a variable on a panic event than having a ksmg dumper collecting the full dmesg and writing it to the UEFI memory; again, some users wish to have the logs, but not all of them.

With all of that said, I think this module makes sense, it's a very simple solution that opens doors to firmware panic handling approaches, like in our proposed case (a different splash screen on panic).

Finally, the variable name (PanicWarn) and value (0xFF by default, can be changed by a module parameter) are just my personal choices but I'm open to suggestions, not strongly attached to them heh

Thanks again for the reviews/suggestions!
Cheers,

Quoted 7 lines
Quoted 3 lines

Guilherme

Hi Ard, sorry for the ping =]

Any opinions in this one? Patch 2 is a simple fix, BTW.

Hey,

No worries about the ping - apologies for the late response, I was on vacation.

I still don't see the point of this series, but I will take the fix if you could please rebase it so it doesn't depend on the first patch.

Thanks,
Ard.

author reviewer
#6 lore raw
Quoted 8 lines · Ard Biesheuvel

[...]

Quoted 3 lines

Hi Ard, sorry for the ping =]

Any opinions in this one? Patch 2 is a simple fix, BTW.

Hey,

No worries about the ping - apologies for the late response, I was on vacation.

Hi Ard, no need for apologies at all - this is a known vacation period in the year =)

Quoted 4 lines

I still don't see the point of this series, but I will take the fix if you could please rebase it so it doesn't depend on the first patch.

That part is sad heheh
I mean, it's a pity I could convince you - I still don't see how can kernel let UEFI know about a panic without this patch (or pstore, which I still think is the wrong way and sometimes impossible to use, due to other pstore backend usage).

Anyway, nothing I can do about that it seems, unfortunately heheh

Submitted the V2 fix here:
https://lore.kernel.org/linux-efi/[email protected]/

Thanks,

Guilherme