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
Makefile
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
cper.c Merge branch 'acpi-apei' 2026-02-05 15:17:54 +01:00
cper_cxl.c
dev-path-parser.c
earlycon.c sysfb: Replace screen_info with sysfb_primary_display 2025-12-16 14:12:44 +01:00
efi-bgrt.c
efi-init.c efi: export sysfb_primary_display for EDID 2026-02-17 12:25:09 +01:00
efi-pstore.c
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
memattr.c efi/memattr: Convert efi_memattr_init() return type to void 2025-11-11 19:11:12 +01:00
memmap.c
mokvar-table.c x86/efi: defer freeing of boot services memory 2026-02-25 12:02:48 +01:00
ovmf-debug-log.c
rci2-table.c
reboot.c
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
unaccepted_memory.c efi: Align unaccepted memory range to page boundary 2026-02-18 11:26:20 +01:00
vars.c