efi/x86: Avoid the need to mangle the EFI memory map
Subsystems (4)
cover efi/x86: Avoid the need to mangle the EFI memory map
From: Ard Biesheuvel <[email protected]>
At boot, x86 uses E820 tables, memblock tables and the EFI memory map to reason about which parts of system RAM are available to the OS, and which are reserved.
While other EFI architectures treat the EFI memory map as immutable, the x86 boot code modifies it to keep track of memory reservations of boot services data regions, in order to distinguish which parts have been memblock_reserve()'d permanently, and which ones have been reserved only temporarily to work around buggy implementations of the EFI runtime service [SetVirtualAddressMap()] that reconfigures the VA space of the runtime services themselves.
This method is mostly fine for marking entire regions as reserved, but it gets complicated when the code decides to split EFI memory map entries in order to mark some of it permanently reserved, and the rest of it temporarily reserved.
Let's clean this up, by
- marking permanent reservations of EFI boot services data memory as MEMBLOCK_RSRV_KERN
- taking this marking into account when deciding whether or not a EFI boot services data region can be freed
- dropping all of the EFI memory map insertion/splitting logic and the allocation/freeing logic, all of which have become redundant.
Ard Biesheuvel (9):
memblock: Permit existing reserved regions to be marked RSRV_KERN
efi: Tag memblock reservations of boot services regions as RSRV_KERN x86/efi: Omit RSRV_KERN memblock reservations when freeing boot
regions
x86/efi: Defer sub-1M check from unmap to free stage x86/efi: Unmap kernel-reserved boot regions from EFI page tables x86/efi: Do not rely on EFI_MEMORY_RUNTIME bit and avoid entry
splitting
x86/efi: Reuse memory map instead of reallocating it x86/efi: Defer compaction of the EFI memory map
x86/efi: Free unused tail of the EFI memory map
arch/x86/include/asm/efi.h | 7 - arch/x86/platform/efi/memmap.c | 221 +------------------ arch/x86/platform/efi/quirks.c | 222 +++++++------------- drivers/firmware/efi/efi.c | 4 +- include/linux/efi.h | 2 - include/linux/memblock.h | 1 + mm/memblock.c | 15 ++ 7 files changed, 96 insertions(+), 376 deletions(-)
base-commit: a4b0bf6a40f3c107c67a24fbc614510ef5719980 # linux-efi/urgent
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
RFC PATCH 1/9 memblock: Permit existing reserved regions to be marked RSRV_KERN
From: Ard Biesheuvel <[email protected]>
Permit existing memblock reservations to be marked as RSRV_KERN. This will be used by the EFI code on x86 to distinguish between reservations of boot services data regions that have actual significance to the kernel and regions that are reserved temporarily to work around buggy firmware.
--- include/linux/memblock.h | 1 + mm/memblock.c | 15 +++++++++++++++ 2 files changed, 16 insertions(+)
include/linux/memblock.h +1 -0
@@ -155,6 +155,7 @@ int memblock_mark_mirror(phys_addr_t base, phys_addr_t size);int memblock_mark_nomap (phys_addr_t base,phys_addr_t size);int memblock_clear_nomap (phys_addr_t base,phys_addr_t size);int memblock_reserved_mark_noinit (phys_addr_t base,phys_addr_t size); +int memblock_reserved_mark_kern (phys_addr_t base,phys_addr_t size);int memblock_mark_kho_scratch (phys_addr_t base,phys_addr_t size);int memblock_clear_kho_scratch (phys_addr_t base,phys_addr_t size);
mm/memblock.c +15 -0
@@ -1115,6 +1115,21 @@ int __init_memblock memblock_reserved_mark_noinit(phys_addr_t base, phys_addr_t MEMBLOCK_RSRV_NOINIT); } +/** +* memblock_reserved_mark_kern - Mark a reserved memory region with flag +* MEMBLOCK_RSRV_KERN +* +* @base: the base phys addr of the region +* @size: the size of the region +* +* Return: 0 on success, -errno on failure. +*/ +int __init_memblock memblock_reserved_mark_kern(phys_addr_t base, phys_addr_t size) +{ +return memblock_setclr_flag (&memblock.reserved, base, size,1 , +MEMBLOCK_RSRV_KERN ); +} +/** * memblock_mark_kho_scratch - Mark a memory region as MEMBLOCK_KHO_SCRATCH. * @base: the base phys addr of the region
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
RFC PATCH 2/9 efi: Tag memblock reservations of boot services regions as RSRV_KERN
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
By definition, EFI memory regions of type boot services code or data have no special significance to the firmware at runtime, only to the OS. In some cases, the firmware will allocate tables and other assets that are passed in memory in regions of this type, and leave it up to the OS to decide whether or not to treat the allocation as special, or simply consume the contents at boot and recycle the RAM for ordinary use. The reason for this approach is that it avoids needless memory reservations for assets that the OS knows nothing about, and therefore doesn't know how to free either.
This means that any memblock reservations covering such regions can be marked as MEMBLOCK_RSRV_KERN - this is a better match semantically, and is useful on x86 to distinguish true reservations from temporary reservations that are only needed to work around firmware bugs.
--- drivers/firmware/efi/efi.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/firmware/efi/efi.c +3 -1
@@ -600,7 +600,9 @@ void __init efi_mem_reserve(phys_addr_t addr, u64 size)return ;if (!memblock_is_region_reserved (addr, size)) -memblock_reserve (addr, size); +memblock_reserve_kern (addr, size); +else +memblock_reserved_mark_kern (addr, size);/* * Some architectures (x86) reserve all boot services ranges
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
RFC PATCH 3/9 x86/efi: Omit RSRV_KERN memblock reservations when freeing boot regions
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Instead of freeing all EFI boot services code and data regions that were preliminarily reserved during early boot to work around buggy firmware, take care to only free those parts that are not marked as MEMBLOCK_RSRV_KERN. This marking is used by the generic implementation of efi_mem_reserve() to mark things like informational tables that are provided to the OS by the firmware, but where the contents of memory have no significance to the firmware itself. Such assets are often passed in a EFI boot service data region, leaving it to the OS to decide whether it needs to be reserved or not.
This removes the need to mark such regions as EFI_MEMORY_RUNTIME, which is a hack that results in a lot of complexity in updating and re-allocating the EFI memory map, which would otherwise not need to be modified at all. Note that x86 is the only EFI arch that does any of this, others just treat the EFI memory map as immutable.
--- arch/x86/platform/efi/quirks.c | 40 +++++++++++++++++--- 1 file changed, 35 insertions(+), 5 deletions(-)
arch/x86/platform/efi/quirks.c +35 -5
@@ -536,6 +536,40 @@ void __init efi_unmap_boot_services(void) } } +static unsigned long __init +efi_free_unreserved_subregions (u64 range_start,u64 range_end) +{ +struct memblock_region *region; +unsigned long freed =0 ; + +for_each_reserved_mem_region (region) { +u64 region_end = region->base + region->size; +u64 start, end; + +/* memblock tables are sorted so no need to carry on */ +if (region->base >= range_end) +break ; + +if (region_end < range_start) +continue ; + +if (region->flags &MEMBLOCK_RSRV_KERN ) +continue ; + + start =PAGE_ALIGN (max (range_start, region->base)); + end =PAGE_ALIGN_DOWN (min (range_end, region_end)); + +if (start >= end) +continue ; + +free_reserved_area (phys_to_virt (start), +phys_to_virt (end),-1 ,NULL ); + freed += (end - start); + } + +return freed; +} +static int __init efi_free_boot_services (void ) {struct efi_freeable_range *range = ranges_to_free;
@@ -545,11 +579,7 @@ static int __init efi_free_boot_services(void)return 0 ;while (range->start) { -void *start =phys_to_virt (range->start); -void *end =phys_to_virt (range->end); - -free_reserved_area (start, end,-1 ,NULL ); - freed += (end - start); + freed +=efi_free_unreserved_subregions (range->start, range->end); range++; }kfree (ranges_to_free);
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
RFC PATCH 4/9 x86/efi: Defer sub-1M check from unmap to free stage
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
As a first step towards moving the free logic to a later stage altogether, and only keeping the unmap and the realmode trampoline hack during the early stage of freeing the boot service code and data regions, move the logic that avoids freeing memory below 1M to the later stage.
--- arch/x86/platform/efi/quirks.c | 21 ++++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-)
arch/x86/platform/efi/quirks.c +8 -13
@@ -475,18 +475,6 @@ void __init efi_unmap_boot_services(void) size -= rm_size; } -/* -* Don't free memory under 1M for two reasons: -* - BIOS might clobber it -* - Crash kernel needs it to be reserved -*/ -if (start + size <SZ_1M ) -continue ; -if (start <SZ_1M ) { - size -= (SZ_1M - start); - start =SZ_1M ; - } -/* * With CONFIG_DEFERRED_STRUCT_PAGE_INIT parts of the memory * map are still not initialized and we can't reliably free
@@ -579,7 +567,14 @@ static int __init efi_free_boot_services(void)return 0 ;while (range->start) { - freed +=efi_free_unreserved_subregions (range->start, range->end); +/* +* Don't free memory under 1M for two reasons: +* - BIOS might clobber it +* - Crash kernel needs it to be reserved +*/ +u64 start =max (range->start,SZ_1M ); + + freed +=efi_free_unreserved_subregions (start, range->end); range++; }kfree (ranges_to_free);
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
PATCH 4/4 x86/efi: Omit kernel reservations of boot services memory from memmap
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Now that efi_mem_reserve() has been updated to rely on RSRV_KERN memblock reservations, it is no longer needed to mark memblock reserved regions as EFI_MEMORY_RUNTIME. This means that it is no longer needed to split existing entries in the EFI memory map, removing the need to re-allocate/copy/remap the entire EFI memory map on every call to efi_mem_reserve().
So drop this functionality - it is no longer needed.
--- arch/x86/include/asm/efi.h | 4 - arch/x86/platform/efi/memmap.c | 138 -------------------- arch/x86/platform/efi/quirks.c | 54 -------- 3 files changed, 196 deletions(-)
arch/x86/include/asm/efi.h +0 -4
@@ -396,10 +396,6 @@ extern int __init efi_memmap_alloc(unsigned int num_entries, struct efi_memory_map_data *data);extern int __init efi_memmap_install (struct efi_memory_map_data *data); -extern int __init efi_memmap_split_count (efi_memory_desc_t *md, -struct range *range); -extern void __init efi_memmap_insert (struct efi_memory_map *old_memmap, -void *buf,struct efi_mem_range *mem);extern enum efi_secureboot_mode __x86_ima_efi_boot_mode (void );
arch/x86/platform/efi/memmap.c +0 -138
141 lines not on this page · raw message
arch/x86/platform/efi/quirks.c +0 -54
63 lines not on this page · raw message
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
RFC PATCH 5/9 x86/efi: Unmap kernel-reserved boot regions from EFI page tables
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Currently, the logic that unmaps boot services code and data regions that were mapped temporarily to work around firmware bugs disregards regions that have been marked as EFI_MEMORY_RUNTIME. However, such regions only have significance to the OS, and there is no reason the retain the mapping in the EFI page tables, given that the runtime firmware must never touch those regions.
So pull the unmap forward.
--- arch/x86/platform/efi/quirks.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
arch/x86/platform/efi/quirks.c +6 -6
@@ -442,12 +442,6 @@ void __init efi_unmap_boot_services(void)continue ; } -/* Do not free, someone else owns it: */ -if (md->attribute &EFI_MEMORY_RUNTIME ) { - num_entries++; -continue ; - } -/* * Before calling set_virtual_address_map(), EFI boot services * code/data regions were mapped as a quirk for buggy firmware.
@@ -455,6 +449,12 @@ void __init efi_unmap_boot_services(void)*/ efi_unmap_pages (md); +/* Do not free, someone else owns it: */ +if (md->attribute &EFI_MEMORY_RUNTIME ) { + num_entries++; +continue ; + } +/* * Nasty quirk: if all sub-1MB memory is used for boot * services, we can get here without having allocated the
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
RFC PATCH 6/9 x86/efi: Do not rely on EFI_MEMORY_RUNTIME bit and avoid entry splitting
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Now that efi_mem_reserve() has been updated to rely on RSRV_KERN memblock reservations, it is no longer needed to mark memblock reserved regions as EFI_MEMORY_RUNTIME. This means that it is no longer needed to split existing entries in the EFI memory map, removing the need to re-allocate/copy/remap the entire EFI memory map on every call to efi_mem_reserve().
So drop this functionality - it is no longer needed.
Note that, for the time being, this requires the E820 map to be consulted when deciding whether or not an entry with the EFI_MEMORY_RUNTIME cleared needs to be preserved in the runtime map or not. However, this will be superseded and removed by a subsequent patch, which combines the map compaction with the actual freeing, in which case the freeing logic can answer this question directly.
--- arch/x86/include/asm/efi.h | 4 - arch/x86/platform/efi/memmap.c | 138 -------------------- arch/x86/platform/efi/quirks.c | 60 +-------- 3 files changed, 5 insertions(+), 197 deletions(-)
arch/x86/include/asm/efi.h +0 -4
@@ -396,10 +396,6 @@ extern int __init efi_memmap_alloc(unsigned int num_entries, struct efi_memory_map_data *data);extern int __init efi_memmap_install (struct efi_memory_map_data *data); -extern int __init efi_memmap_split_count (efi_memory_desc_t *md, -struct range *range); -extern void __init efi_memmap_insert (struct efi_memory_map *old_memmap, -void *buf,struct efi_mem_range *mem);extern enum efi_secureboot_mode __x86_ima_efi_boot_mode (void );
arch/x86/platform/efi/memmap.c +0 -138
141 lines not on this page · raw message
arch/x86/platform/efi/quirks.c +5 -55
76 lines not on this page · raw message
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
RFC PATCH 7/9 x86/efi: Reuse memory map instead of reallocating it
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
The EFI memory map consists of 10s to 100s of entries of around 40 bytes each. The initial version is allocated and populated by the EFI stub, but later on, after freeing the boot services data regions and pruning the associated entries, a new memory map is allocated with room for only the remaining entries, which are typically much fewer in number.
Given that the original allocation is never freed, this does not actually save any memory, and it is much simpler to just move the entries that need to be preserved to the beginning of the map, and to truncate it. That way, a lot of the complicated memory map allocation and freeing code can simply be dropped.
--- arch/x86/include/asm/efi.h | 3 - arch/x86/platform/efi/memmap.c | 83 +------------------- arch/x86/platform/efi/quirks.c | 30 +++---- include/linux/efi.h | 2 - 4 files changed, 10 insertions(+), 108 deletions(-)
arch/x86/include/asm/efi.h +0 -3
@@ -392,9 +392,6 @@ static inline void efi_reserve_boot_services(void) }#endif /* CONFIG_EFI */ -extern int __init efi_memmap_alloc (unsigned int num_entries, -struct efi_memory_map_data *data); -extern int __init efi_memmap_install (struct efi_memory_map_data *data);extern enum efi_secureboot_mode __x86_ima_efi_boot_mode (void );
arch/x86/platform/efi/memmap.c +1 -82
99 lines not on this page · raw message
arch/x86/platform/efi/quirks.c +9 -21
@@ -359,12 +359,15 @@ static struct efi_freeable_range *ranges_to_free;void __init efi_unmap_boot_services (void ) { -struct efi_memory_map_data data = {0 }; +struct efi_memory_map_data data = { + .phys_map = efi.memmap.phys_map, + .desc_version = efi.memmap.desc_version, + .desc_size = efi.memmap.desc_size, + };efi_memory_desc_t *md; -int num_entries =0 ; +void *new_md;int idx =0 ;size_t sz; -void *new, *new_md;/* Keep all regions for /sys/kernel/debug/efi */ if (efi_enabled (EFI_DBG ))
@@ -377,6 +380,7 @@ void __init efi_unmap_boot_services(void)return ; } + new_md = efi.memmap.map;for_each_efi_memory_desc (md) {unsigned long long start = md->phys_addr;unsigned long long size = md->num_pages <<EFI_PAGE_SHIFT ;
@@ -384,7 +388,6 @@ void __init efi_unmap_boot_services(void)if (md->type !=EFI_BOOT_SERVICES_CODE && md->type !=EFI_BOOT_SERVICES_DATA ) { - num_entries++;continue ; }
@@ -397,7 +400,6 @@ void __init efi_unmap_boot_services(void)/* Do not free, someone else owns it: */ if (md->attribute &EFI_MEMORY_RUNTIME ) { - num_entries++;continue ; }
@@ -432,26 +434,12 @@ void __init efi_unmap_boot_services(void) idx++; } -if (!num_entries) -return ; - -if (efi_memmap_alloc (num_entries, &data) !=0 ) { -pr_err ("Failed to allocate new EFI memmap\n" ); -return ; - } - - new =memremap (data.phys_map, data.size,MEMREMAP_WB ); -if (!new) { -pr_err ("Failed to map new EFI memmap\n" ); -return ; - } -/* * Build a new EFI memmap that excludes any boot services * regions that are not tagged EFI_MEMORY_RUNTIME, since those * regions have now been freed. */ - new_md = new; + new_md = efi.memmap.map;for_each_efi_memory_desc (md) { if (!(md->attribute & EFI_MEMORY_RUNTIME) && (md->type == EFI_BOOT_SERVICES_CODE ||
@@ -466,7 +454,7 @@ void __init efi_unmap_boot_services(void) new_md += efi.memmap.desc_size; } -memunmap (new); + data.size = new_md - efi.memmap.map;if (efi_memmap_install (&data) !=0 ) {pr_err ("Could not install new EFI memmap\n" );
include/linux/efi.h +0 -2
@@ -572,8 +572,6 @@ struct efi_memory_map {unsigned long desc_version;unsigned long desc_size;#define EFI_MEMMAP_LATE (1UL << 0) -#define EFI_MEMMAP_MEMBLOCK (1UL << 1) -#define EFI_MEMMAP_SLAB (1UL << 2)unsigned long flags; };
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
RFC PATCH 8/9 x86/efi: Defer compaction of the EFI memory map
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Currently, the EFI memory map is compacted early at boot, to leave only the entries that are significant to the current kernel or potentially a kexec'ed kernel that comes after, and to suppress all boot services code and data entries that have no correspondence with anything that either the firmware or the kernel treats as reserved for firmware use.
Given that actually freeing those regions to the page allocator is not possible yet at this point, those suppressed entries are converted into yet another type of temporary memory reservation map, and freed during an arch_initcall(), which is the earliest convenient time to actually perform this operation.
Given that compacting the memory map does not need to occur that early to begin with, move it to the arch_initcall(). This removes the need for the special memory reservation map, as the entries still exist at this point, and can be consulted directly to decide whether they need to be preserved in their entirety or only partially.
--- arch/x86/platform/efi/quirks.c | 130 +++++++------------- 1 file changed, 46 insertions(+), 84 deletions(-)
arch/x86/platform/efi/quirks.c +46 -84
@@ -350,37 +350,10 @@ static void __init efi_unmap_pages(efi_memory_desc_t *md)pr_err ("Failed to unmap VA mapping for 0x%llx\n" , va); } -struct efi_freeable_range { -u64 start; -u64 end; -}; - -static struct efi_freeable_range *ranges_to_free; -void __init efi_unmap_boot_services (void ) { -struct efi_memory_map_data data = { - .phys_map = efi.memmap.phys_map, - .desc_version = efi.memmap.desc_version, - .desc_size = efi.memmap.desc_size, - };efi_memory_desc_t *md; -void *new_md; -int idx =0 ; -size_t sz; -/* Keep all regions for /sys/kernel/debug/efi */ -if (efi_enabled (EFI_DBG )) -return ; - - sz =sizeof (*ranges_to_free) * efi.memmap.nr_map +1 ; - ranges_to_free =kzalloc (sz,GFP_KERNEL ); -if (!ranges_to_free) { -pr_err ("Failed to allocate storage for freeable EFI regions\n" ); -return ; - } - - new_md = efi.memmap.map;for_each_efi_memory_desc (md) {unsigned long long start = md->phys_addr;unsigned long long size = md->num_pages <<EFI_PAGE_SHIFT ;
@@ -394,15 +367,10 @@ void __init efi_unmap_boot_services(void)/* * Before calling set_virtual_address_map(), EFI boot services * code/data regions were mapped as a quirk for buggy firmware. -* Unmap them from efi_pgd before freeing them up. +* Unmap them from efi_pgd, they will be freed later. */ efi_unmap_pages (md); -/* Do not free, someone else owns it: */ -if (md->attribute &EFI_MEMORY_RUNTIME ) { -continue ; - } -/* * Nasty quirk: if all sub-1MB memory is used for boot * services, we can get here without having allocated the
@@ -416,49 +384,14 @@ void __init efi_unmap_boot_services(void)* this happened, but Linux should still try to boot rather * panicking early.) */ - rm_size =real_mode_size_needed (); + rm_size =PAGE_ALIGN (real_mode_size_needed ());if (rm_size && (start + rm_size) < (1 <<20 ) && size >= rm_size) {set_real_mode_mem (start); - start += rm_size; - size -= rm_size; - } - -/* -* With CONFIG_DEFERRED_STRUCT_PAGE_INIT parts of the memory -* map are still not initialized and we can't reliably free -* memory here. -* Queue the ranges to free at a later point. -*/ - ranges_to_free[idx].start = start; - ranges_to_free[idx].end = start + size; - idx++; - } -/* -* Build a new EFI memmap that excludes any boot services -* regions that are not tagged EFI_MEMORY_RUNTIME, since those -* regions have now been freed. -*/ - new_md = efi.memmap.map; -for_each_efi_memory_desc (md) { -if (!(md->attribute &EFI_MEMORY_RUNTIME ) && - (md->type ==EFI_BOOT_SERVICES_CODE || - md->type ==EFI_BOOT_SERVICES_DATA ) && - !e820__mapped_any (md->phys_addr, - md->phys_addr + md->num_pages *EFI_PAGE_SIZE , -E820_TYPE_RESERVED )) { -continue ; +/* Remove the allocated space from the descriptor */ + md->phys_addr += rm_size; + md->num_pages -= rm_size /EFI_PAGE_SIZE ; } - -memcpy (new_md, md, efi.memmap.desc_size); - new_md += efi.memmap.desc_size; - } - - data.size = new_md - efi.memmap.map; - -if (efi_memmap_install (&data) !=0 ) { -pr_err ("Could not install new EFI memmap\n" ); -return ; } }
@@ -498,24 +431,53 @@ efi_free_unreserved_subregions(u64 range_start, u64 range_end)static int __init efi_free_boot_services (void ) { -struct efi_freeable_range *range = ranges_to_free; +struct efi_memory_map_data data = { + .phys_map = efi.memmap.phys_map, + .desc_version = efi.memmap.desc_version, + .desc_size = efi.memmap.desc_size, + };unsigned long freed =0 ; +efi_memory_desc_t *md; +void *new_md; -if (!ranges_to_free) +/* Keep all regions for /sys/kernel/debug/efi */ +if (efi_enabled (EFI_DBG ))return 0 ; -while (range->start) { -/* -* Don't free memory under 1M for two reasons: -* - BIOS might clobber it -* - Crash kernel needs it to be reserved -*/ -u64 start =max (range->start,SZ_1M ); + new_md = efi.memmap.map; +for_each_efi_memory_desc (md) { +u64 md_start =max (md->phys_addr,SZ_1M ); +u64 md_end = md->phys_addr + md->num_pages *EFI_PAGE_SIZE ; +bool preserve_entry = true; + +if (!(md->attribute &EFI_MEMORY_RUNTIME ) && + (md->type ==EFI_BOOT_SERVICES_CODE || + md->type ==EFI_BOOT_SERVICES_DATA )) { +u64 f =efi_free_unreserved_subregions (md_start, md_end); + +/* +* Omit the memory map entry of this region only if it +* has been freed entirely. This ensures that boot data +* regions for things like ESRT and BGRT tables carry +* over correctly during kexec. +*/ +if (f == md_end - md_start) + preserve_entry = false; + + freed += f; + } - freed +=efi_free_unreserved_subregions (start, range->end); - range++; +if (preserve_entry) { +if (new_md != md) +memcpy (new_md, md, efi.memmap.desc_size); + new_md += efi.memmap.desc_size; + } } -kfree (ranges_to_free); + + data.size = new_md - efi.memmap.map; + +if (efi_memmap_install (&data) !=0 ) +pr_err ("Could not install new EFI memmap\n" );if (freed)pr_info ("Freeing EFI boot services memory: %ldK\n" , freed /SZ_1K );
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
RFC PATCH 9/9 x86/efi: Free unused tail of the EFI memory map
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
After moving the relevant entries to the start of the map, the remainder can be handed back to the page allocator.
--- arch/x86/platform/efi/quirks.c | 5 +++++ 1 file changed, 5 insertions(+)
arch/x86/platform/efi/quirks.c +5 -0
@@ -475,10 +475,15 @@ static int __init efi_free_boot_services(void) } data.size = new_md - efi.memmap.map; + md = efi.memmap.map_end;if (efi_memmap_install (&data) !=0 )pr_err ("Could not install new EFI memmap\n" ); +/* Free the part of the memory map allocation that has become unused */ +free_reserved_area (new_md, md,-1 ,NULL ); + freed += (void *)md - new_md; +if (freed)pr_info ("Freeing EFI boot services memory: %ldK\n" , freed /SZ_1K );
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
Ard Biesheuvel
maintainerRe: [PATCH 4/4] x86/efi: Omit kernel reservations of boot services memory from memmap
In reply to Ard BiesheuvelPlease disregard this 4/4 - it is a stale version from my working dir, apologies.
Quoted 18 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
Now that
efi_mem_reserve()has been updated to rely onRSRV_KERNmemblock reservations, it is no longer needed to mark memblock reserved regions asEFI_MEMORY_RUNTIME. This means that it is no longer needed to split existing entries in the EFI memory map, removing the need to re-allocate/copy/remap the entire EFI memory map on every call toefi_mem_reserve().So drop this functionality - it is no longer needed.
Signed-off-by Ard Biesheuvel <[email protected]>--- arch/x86/include/asm/efi.h | 4 - arch/x86/platform/efi/memmap.c | 138 -------------------- arch/x86/platform/efi/quirks.c | 54 -------- 3 files changed, 196 deletions(-)
Re: [RFC PATCH 1/9] memblock: Permit existing reserved regions to be marked RSRV_KERN
In reply to Ard BiesheuvelQuoted 9 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
Permit existing memblock reservations to be marked as
RSRV_KERN. This will be used by the EFI code on x86 to distinguish between reservations of boot services data regions that have actual significance to the kernel and regions that are reserved temporarily to work around buggy firmware.Signed-off-by Ard Biesheuvel <[email protected]>
Quoted 46 lines
--- include/linux/memblock.h | 1 + mm/memblock.c | 15 +++++++++++++++ 2 files changed, 16 insertions(+)include/linux/memblock.h +1
-0@@ -155,6 +155,7 @@ int memblock_mark_mirror(phys_addr_t base, phys_addr_t size);int memblock_mark_nomap (phys_addr_t base,phys_addr_t size);int memblock_clear_nomap (phys_addr_t base,phys_addr_t size);int memblock_reserved_mark_noinit (phys_addr_t base,phys_addr_t size); +int memblock_reserved_mark_kern (phys_addr_t base,phys_addr_t size);int memblock_mark_kho_scratch (phys_addr_t base,phys_addr_t size);int memblock_clear_kho_scratch (phys_addr_t base,phys_addr_t size);mm/memblock.c +15
-0@@ -1115,6 +1115,21 @@ int __init_memblock memblock_reserved_mark_noinit(phys_addr_t base, phys_addr_t MEMBLOCK_RSRV_NOINIT); } +/** +* memblock_reserved_mark_kern - Mark a reserved memory region with flag +* MEMBLOCK_RSRV_KERN +* +* @base: the base phys addr of the region +* @size: the size of the region +* +* Return: 0 on success, -errno on failure. +*/ +int __init_memblock memblock_reserved_mark_kern(phys_addr_t base, phys_addr_t size) +{ +return memblock_setclr_flag (&memblock.reserved, base, size,1 , +MEMBLOCK_RSRV_KERN ); +} +/** * memblock_mark_kho_scratch - Mark a memory region as MEMBLOCK_KHO_SCRATCH. * @base: the base phys addr of the region Signature · 1 line
2.53.0.473.g4a7958ca14-goog
Signature · 2 lines
Sincerely yours, Mike.
Re: [RFC PATCH 2/9] efi: Tag memblock reservations of boot services regions as RSRV_KERN
In reply to Ard BiesheuvelQuoted 12 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
By definition, EFI memory regions of type boot services code or data have no special significance to the firmware at runtime, only to the OS. In some cases, the firmware will allocate tables and other assets that are passed in memory in regions of this type, and leave it up to the OS to decide whether or not to treat the allocation as special, or simply consume the contents at boot and recycle the RAM for ordinary use. The reason for this approach is that it avoids needless memory reservations for assets that the OS knows nothing about, and therefore doesn't know how to free either.
Quoted 6 lines
This means that any memblock reservations covering such regions can be marked as
MEMBLOCK_RSRV_KERN- this is a better match semantically, and is useful on x86 to distinguish true reservations from temporary reservations that are only needed to work around firmware bugs.Signed-off-by Ard Biesheuvel <[email protected]>
Quoted 22 lines
--- drivers/firmware/efi/efi.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/firmware/efi/efi.c +3
-1@@ -600,7 +600,9 @@ void __init efi_mem_reserve(phys_addr_t addr, u64 size)return ;if (!memblock_is_region_reserved (addr, size)) -memblock_reserve (addr, size); +memblock_reserve_kern (addr, size); +else +memblock_reserved_mark_kern (addr, size);/* * Some architectures (x86) reserve all boot services ranges Signature · 1 line
2.53.0.473.g4a7958ca14-goog
Signature · 2 lines
Sincerely yours, Mike.