The Lenovo ThinkPad T14s Gen 6 (Snapdragon X) suffers from intermittent boot failures, with ExitBootServices() returning EFI_INVALID_PARAMETER even after retrying. This appears to be caused by the firmware allocating and freeing small blocks of boot services data within ExitBootServices() before validating the memory map key. These allocations seem to fall within the 256-byte size class of the edk2 pool allocator. If no suitable free blocks are available, the pool allocates a new page, changing the memory map and invalidating the key. Work around this issue by allocating 16 blocks of 200 bytes of boot services data before obtaining the memory map. Free all but the last block, which prevents its pool page from being returned to the page allocator. This leaves suitable free blocks available for the firmware without further changes to the memory map. The retained allocation is reclaimed along with the rest of boot services memory. Creating an event before ExitBootServices() and leaving it allocated can also avoid the failure [1], but was not reliable across all boot methods. This workaround addresses the underlying pool allocation issue and was reliable across all tested boot methods. Restrict the workaround to the affected machine by matching the manufacturer and product family in SMBIOS. The failure was first reported in 2024 and is still present in BIOS 2.30, so the workaround is not restricted to specific firmware versions. The workaround was tested on the affected machine across three boot paths, with 75 boots in the failing state (60 forced, 15 natural), all of which booted. Without the workaround, all 15 boots in the failing state failed. Link: https://lore.kernel.org/linux-arm-msm/Z0gn1N3IsP8r3gTA@hovoldconsulting.com/ [1] Cc: stable@vger.kernel.org Signed-off-by: Christopher Obbard --- The Lenovo ThinkPad T14s Gen 6 sometimes fails to boot because ExitBootServices() returns EFI_INVALID_PARAMETER. The commit message explains the cause and how this patch works around it. Prior work ---------- Johan Hovold reported this failure on the T14s in November 2024 (see the Link tag in the patch). He found that creating an event in the BeforeExitBootServices group before calling ExitBootServices() can make the call succeed, as long as the event stays allocated. Signalling the event is often not needed. He also saw the failure return on some boot paths, such as starting the kernel from a UEFI shell startup.nsh. This appears to work as a side effect of the firmware's pool allocator. Creating the first event can cause the firmware to retain a small allocation, keeping a pool page with free blocks available for subsequent allocations. If this has already occurred during an earlier boot stage, creating another event may not provide the same benefit. The proposed workaround reserves suitable free blocks directly, without relying on the side effects of event creation. Testing ------- The failure appears to occur when the boot services pool has no free blocks of a suitable size when the kernel starts. This is the "failing state" below. A test-only stub patch recorded whether each boot was in that state. A boot services driver was used to force it by using up the free blocks. Both can be posted if useful. "Booted" means the kernel reached userspace; "failed" means it did not. Forced failure state Natural failure state Without workaround 5/5 failed 10/10 failed With workaround 60/60 booted 15/15 booted Freeing all 16 blocks 10/10 failed - The patch was tested using three boot paths: shim and GRUB; systemd-boot with a UKI; and the firmware boot menu with a UKI. Initrds ranged from 60 MB to ~300 MB. The workaround appears to leave about 11 free blocks in the failing state, where the firmware needs 2. This figure comes from a model of the edk2 pool allocator that was consistent with the results of all 75 logged boots. It was not counted directly. Scope ----- I have only reproduced the issue on the ThinkPad T14s Gen 6, so the workaround is deliberately restricted to this machine to avoid modifying the boot services pool on unaffected platforms. The issue has been reported to Lenovo. If a firmware update fixes it, the workaround can be limited to older firmware versions. Feedback on whether a machine-specific workaround is preferable to applying it unconditionally would be appreciated. AI assistance ------------- Claude Code (Opus 5.5) was used to develop the test tooling, execute tests on the target hardware and collate the results. The results were manually verified. --- drivers/firmware/efi/libstub/arm64.c | 28 ++++++++++++++++ drivers/firmware/efi/libstub/efi-stub-helper.c | 44 ++++++++++++++++++++++++++ drivers/firmware/efi/libstub/efistub.h | 1 + 3 files changed, 73 insertions(+) diff --git a/drivers/firmware/efi/libstub/arm64.c b/drivers/firmware/efi/libstub/arm64.c index c190a3ac97001..d9077381d03fa 100644 --- a/drivers/firmware/efi/libstub/arm64.c +++ b/drivers/firmware/efi/libstub/arm64.c @@ -55,6 +55,32 @@ static bool system_needs_vamap(void) return false; } +static bool system_needs_ebs_pool_reserve(void) +{ + const struct efi_smbios_type1_record *record; + const u8 *str; + + /* + * The Lenovo ThinkPad T14s Gen 6 requires the ExitBootServices() + * pool allocation workaround. Match the family string rather than + * the product name, which contains Lenovo's machine type code. + */ + record = (struct efi_smbios_type1_record *)efi_get_smbios_record(1); + if (!record || record->header.length < + offsetofend(struct efi_smbios_type1_record, family)) + return false; + + str = efi_get_smbios_string(record, manufacturer); + if (!str || strcmp(str, "LENOVO")) + return false; + + str = efi_get_smbios_string(record, family); + if (!str || strcmp(str, "ThinkPad T14s Gen 6")) + return false; + + return true; +} + efi_status_t check_platform_features(void) { u64 tg; @@ -67,6 +93,8 @@ efi_status_t check_platform_features(void) if (VA_BITS_MIN >= 48 && !system_needs_vamap()) efi_novamap = true; + efi_ebs_pool_reserve = system_needs_ebs_pool_reserve(); + /* UEFI mandates support for 4 KB granularity, no need to check */ if (IS_ENABLED(CONFIG_ARM64_4K_PAGES)) return EFI_SUCCESS; diff --git a/drivers/firmware/efi/libstub/efi-stub-helper.c b/drivers/firmware/efi/libstub/efi-stub-helper.c index e50deec996865..9eb60ee0c8391 100644 --- a/drivers/firmware/efi/libstub/efi-stub-helper.c +++ b/drivers/firmware/efi/libstub/efi-stub-helper.c @@ -21,6 +21,7 @@ bool efi_nochunk; bool efi_nokaslr = !IS_ENABLED(CONFIG_RANDOMIZE_BASE); bool efi_novamap; +bool efi_ebs_pool_reserve; static bool efi_noinitrd; static bool efi_nosoftreserve; @@ -387,6 +388,46 @@ char *efi_convert_cmdline(efi_loaded_image_t *image) return cmdline_addr; } +#define EFI_EBS_POOL_RESERVE_BLOCKS 16 +#define EFI_EBS_POOL_RESERVE_SIZE 200 + +/* + * The Lenovo ThinkPad T14s Gen 6 firmware appears to allocate and free + * boot services data within ExitBootServices() before validating the + * memory map key. If the pool has no suitable free blocks, these + * operations can change the memory map and invalidate the key. + * + * A 200 byte allocation falls in the 256 byte size class of the edk2 + * pool allocator once the pool header and tail are included. Allocate + * 16 blocks which provides sufficient headroom in the observed failing + * state, based on both modelling and testing. + * + * Free all but the last block. Retaining this allocation prevents its + * pool page from being returned to the page allocator, leaving free + * blocks available for the firmware. The retained allocation can be + * reclaimed along with the rest of boot services memory. + */ +static void efi_reserve_ebs_pool(void) +{ + void *blocks[EFI_EBS_POOL_RESERVE_BLOCKS]; + int i, n; + + efi_warn("Working around broken ExitBootServices()\n"); + + for (n = 0; n < EFI_EBS_POOL_RESERVE_BLOCKS; n++) { + if (efi_bs_call(allocate_pool, EFI_BOOT_SERVICES_DATA, + EFI_EBS_POOL_RESERVE_SIZE, + &blocks[n]) != EFI_SUCCESS) + break; + } + + if (n != EFI_EBS_POOL_RESERVE_BLOCKS) + efi_warn("Failed to fully populate ExitBootServices() reserve pool\n"); + + for (i = 0; i < n - 1; i++) + efi_bs_call(free_pool, blocks[i]); +} + /** * efi_exit_boot_services() - Exit boot services * @handle: handle of the exiting image @@ -411,6 +452,9 @@ efi_status_t efi_exit_boot_services(void *handle, void *priv, if (efi_disable_pci_dma) efi_pci_disable_bridge_busmaster(); + if (efi_ebs_pool_reserve) + efi_reserve_ebs_pool(); + status = efi_get_memory_map(&map, true); if (status != EFI_SUCCESS) return status; diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/libstub/efistub.h index df3f88f4786b8..4a1b86d995eae 100644 --- a/drivers/firmware/efi/libstub/efistub.h +++ b/drivers/firmware/efi/libstub/efistub.h @@ -44,6 +44,7 @@ extern bool efi_nokaslr; extern int efi_loglevel; extern int efi_mem_encrypt; extern bool efi_novamap; +extern bool efi_ebs_pool_reserve; extern const efi_system_table_t *efi_system_table; typedef union efi_dxe_services_table efi_dxe_services_table_t; --- base-commit: 697ced232513751c6924629ef8765911e613fd1c change-id: 20261205-wip-obbardc-qcom-t14s-gen6-ebs-workaround-95b50b13211e Best regards, -- Christopher Obbard