linux/drivers/firmware/efi
Mike Rapoport (Microsoft) a4b0bf6a40 x86/efi: defer freeing of boot services memory
efi_free_boot_services() frees memory occupied by EFI_BOOT_SERVICES_CODE
and EFI_BOOT_SERVICES_DATA using memblock_free_late().

There are two issue with that: memblock_free_late() should be used for
memory allocated with memblock_alloc() while the memory reserved with
memblock_reserve() should be freed with free_reserved_area().

More acutely, with CONFIG_DEFERRED_STRUCT_PAGE_INIT=y
efi_free_boot_services() is called before deferred initialization of the
memory map is complete.

Benjamin Herrenschmidt reports that this causes a leak of ~140MB of
RAM on EC2 t3a.nano instances which only have 512MB or RAM.

If the freed memory resides in the areas that memory map for them is
still uninitialized, they won't be actually freed because
memblock_free_late() calls memblock_free_pages() and the latter skips
uninitialized pages.

Using free_reserved_area() at this point is also problematic because
__free_page() accesses the buddy of the freed page and that again might
end up in uninitialized part of the memory map.

Delaying the entire efi_free_boot_services() could be problematic
because in addition to freeing boot services memory it updates
efi.memmap without any synchronization and that's undesirable late in
boot when there is concurrency.

More robust approach is to only defer freeing of the EFI boot services
memory.

Split efi_free_boot_services() in two. First efi_unmap_boot_services()
collects ranges that should be freed into an array then
efi_free_boot_services() later frees them after deferred init is complete.

Link: https://lore.kernel.org/all/ec2aaef14783869b3be6e3c253b2dcbf67dbc12a.camel@kernel.crashing.org
Fixes: 916f676f8d ("x86, efi: Retain boot service code until after switching to virtual mode")
Cc: <stable@vger.kernel.org>
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Reviewed-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
2026-02-25 12:02:48 +01:00
..
libstub EFI updates for v7.0 2026-02-09 20:49:19 -08:00
stmm efi: stmm: Constify struct efivar_operations 2026-02-18 11:26:20 +01:00
test Convert more 'alloc_obj' cases to default GFP_KERNEL arguments 2026-02-21 20:03:00 -08:00
Kconfig efi: Explain OVMF acronym in OVMF_DEBUG_LOG help text 2025-08-29 19:02:04 +02:00
Makefile efi: add ovmf debug log driver 2025-07-09 20:31:38 +10:00
apple-properties.c Convert more 'alloc_obj' cases to default GFP_KERNEL arguments 2026-02-21 20:03:00 -08:00
arm-runtime.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
capsule-loader.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
capsule.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
cper-arm.c EFI/CPER: don't go past the ARM processor CPER record buffer 2026-01-14 17:04:21 +01:00
cper-x86.c efi/cper: Fix cper_ia_proc_ctx alignment 2025-02-23 17:30:04 +01:00
cper.c Merge branch 'acpi-apei' 2026-02-05 15:17:54 +01:00
cper_cxl.c efi/cper, cxl: Remove cper_cxl.h 2025-02-06 11:27:16 -07:00
dev-path-parser.c driver core: Constify API device_find_child() and adapt for various usages 2025-01-03 11:19:35 +01:00
earlycon.c sysfb: Replace screen_info with sysfb_primary_display 2025-12-16 14:12:44 +01:00
efi-bgrt.c efi/bgrt: Accept BGRT tables with a version of 0 2020-02-22 23:37:37 +01:00
efi-init.c efi: export sysfb_primary_display for EDID 2026-02-17 12:25:09 +01:00
efi-pstore.c module: Convert symbol namespace to string literal 2024-12-02 11:34:44 -08:00
efi.c treewide: Replace kmalloc with kmalloc_obj for non-scalar types 2026-02-21 01:02:28 -08:00
efibc.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
embedded-firmware.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
esrt.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
fdtparams.c move asm/unaligned.h to linux/unaligned.h 2024-10-02 17:23:23 -04:00
memattr.c efi/memattr: Convert efi_memattr_init() return type to void 2025-11-11 19:11:12 +01:00
memmap.c efi: Improve logging around memmap init 2025-05-09 11:07:28 +02:00
mokvar-table.c x86/efi: defer freeing of boot services memory 2026-02-25 12:02:48 +01:00
ovmf-debug-log.c efi: add ovmf debug log driver 2025-07-09 20:31:38 +10:00
rci2-table.c efi: rci2: mark bin_attribute as __ro_after_init 2025-02-21 09:20:32 +01:00
reboot.c efi: Fix efi_power_off() not being run before acpi_power_off() when necessary 2022-07-10 17:41:06 +02:00
riscv-runtime.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
runtime-wrappers.c efi/runtime-wrappers: Keep track of the efi_runtime_lock owner 2025-11-11 18:59:22 +00:00
sysfb_efi.c sysfb: Replace screen_info with sysfb_primary_display 2025-12-16 14:12:44 +01:00
tpm.c tpm: fix unsigned/signed mismatch errors related to __calc_tpm2_event_size 2024-10-15 20:27:29 +02:00
unaccepted_memory.c efi: Align unaccepted memory range to page boundary 2026-02-18 11:26:20 +01:00
vars.c module: Convert symbol namespace to string literal 2024-12-02 11:34:44 -08:00