Skip to content

Commit 75dde79

Browse files
committed
efi/x86: Free EFI memory map only when installing a new one.
The logic in __efi_memmap_init() is shared between two different execution flows: - mapping the EFI memory map early or late into the kernel VA space, so that its entries can be accessed; - the x86 specific cloning of the EFI memory map in order to insert new entries that are created as a result of making a memory reservation via a call to efi_mem_reserve(). In the former case, the underlying memory containing the kernel's view of the EFI memory map (which may be heavily modified by the kernel itself on x86) is not modified at all, and the only thing that changes is the virtual mapping of this memory, which is different between early and late boot. In the latter case, an entirely new allocation is created that carries a new, updated version of the kernel's view of the EFI memory map. When installing this new version, the old version will no longer be referenced, and if the memory was allocated by the kernel, it will leak unless it gets freed. The logic that implements this freeing currently lives on the code path that is shared between these two use cases, but it should only apply to the latter. So move it to the correct spot. While at it, drop the dummy definition for non-x86 architectures, as that is no longer needed. Cc: <[email protected]> Fixes: f0ef652 ("efi: Fix efi_memmap_alloc() leaks") Tested-by: Ashish Kalra <[email protected]> Link: https://lore.kernel.org/all/[email protected] Signed-off-by: Ard Biesheuvel <[email protected]>
1 parent a1439d8 commit 75dde79

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

arch/x86/include/asm/efi.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,6 @@ extern int __init efi_memmap_alloc(unsigned int num_entries,
401401
struct efi_memory_map_data *data);
402402
extern void __efi_memmap_free(u64 phys, unsigned long size,
403403
unsigned long flags);
404-
#define __efi_memmap_free __efi_memmap_free
405404

406405
extern int __init efi_memmap_install(struct efi_memory_map_data *data);
407406
extern int __init efi_memmap_split_count(efi_memory_desc_t *md,

arch/x86/platform/efi/memmap.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,22 @@ int __init efi_memmap_alloc(unsigned int num_entries,
9292
*/
9393
int __init efi_memmap_install(struct efi_memory_map_data *data)
9494
{
95+
unsigned long size = efi.memmap.desc_size * efi.memmap.nr_map;
96+
unsigned long flags = efi.memmap.flags;
97+
u64 phys = efi.memmap.phys_map;
98+
int ret;
99+
95100
efi_memmap_unmap();
96101

97102
if (efi_enabled(EFI_PARAVIRT))
98103
return 0;
99104

100-
return __efi_memmap_init(data);
105+
ret = __efi_memmap_init(data);
106+
if (ret)
107+
return ret;
108+
109+
__efi_memmap_free(phys, size, flags);
110+
return 0;
101111
}
102112

103113
/**

drivers/firmware/efi/memmap.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
#include <asm/early_ioremap.h>
1616
#include <asm/efi.h>
1717

18-
#ifndef __efi_memmap_free
19-
#define __efi_memmap_free(phys, size, flags) do { } while (0)
20-
#endif
21-
2218
/**
2319
* __efi_memmap_init - Common code for mapping the EFI memory map
2420
* @data: EFI memory map data
@@ -51,11 +47,6 @@ int __init __efi_memmap_init(struct efi_memory_map_data *data)
5147
return -ENOMEM;
5248
}
5349

54-
if (efi.memmap.flags & (EFI_MEMMAP_MEMBLOCK | EFI_MEMMAP_SLAB))
55-
__efi_memmap_free(efi.memmap.phys_map,
56-
efi.memmap.desc_size * efi.memmap.nr_map,
57-
efi.memmap.flags);
58-
5950
map.phys_map = data->phys_map;
6051
map.nr_map = data->size / data->desc_size;
6152
map.map_end = map.map + data->size;

0 commit comments

Comments
 (0)