efi/x86: Free EFI memory map only when installing a new one.
authorArd Biesheuvel <ardb@kernel.org>
Mon, 10 Jun 2024 14:02:13 +0000 (16:02 +0200)
committerArd Biesheuvel <ardb@kernel.org>
Sat, 15 Jun 2024 08:25:02 +0000 (10:25 +0200)
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: <stable@vger.kernel.org>
Fixes: f0ef6523475f ("efi: Fix efi_memmap_alloc() leaks")
Tested-by: Ashish Kalra <Ashish.Kalra@amd.com>
Link: https://lore.kernel.org/all/36ad5079-4326-45ed-85f6-928ff76483d3@amd.com
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
arch/x86/include/asm/efi.h
arch/x86/platform/efi/memmap.c
drivers/firmware/efi/memmap.c

index 1dc600fa3ba536e904027df726d4d33360f0fe77..481096177500e117d0c955726e779e6537ca063b 100644 (file)
@@ -401,7 +401,6 @@ extern int __init efi_memmap_alloc(unsigned int num_entries,
                                   struct efi_memory_map_data *data);
 extern void __efi_memmap_free(u64 phys, unsigned long size,
                              unsigned long flags);
-#define __efi_memmap_free __efi_memmap_free
 
 extern int __init efi_memmap_install(struct efi_memory_map_data *data);
 extern int __init efi_memmap_split_count(efi_memory_desc_t *md,
index 4ef20b49eb5e72e4893fc5eabc969d6941368d35..6ed1935504b96e8ac7d1acb1aa215a617125c768 100644 (file)
@@ -92,12 +92,22 @@ int __init efi_memmap_alloc(unsigned int num_entries,
  */
 int __init efi_memmap_install(struct efi_memory_map_data *data)
 {
+       unsigned long size = efi.memmap.desc_size * efi.memmap.nr_map;
+       unsigned long flags = efi.memmap.flags;
+       u64 phys = efi.memmap.phys_map;
+       int ret;
+
        efi_memmap_unmap();
 
        if (efi_enabled(EFI_PARAVIRT))
                return 0;
 
-       return __efi_memmap_init(data);
+       ret = __efi_memmap_init(data);
+       if (ret)
+               return ret;
+
+       __efi_memmap_free(phys, size, flags);
+       return 0;
 }
 
 /**
index 3365944f7965448dbc5e0ec57dcd547cdf1d4e34..34109fd86c55d2f1e63b949968f75b6f3473bc7d 100644 (file)
 #include <asm/early_ioremap.h>
 #include <asm/efi.h>
 
-#ifndef __efi_memmap_free
-#define __efi_memmap_free(phys, size, flags) do { } while (0)
-#endif
-
 /**
  * __efi_memmap_init - Common code for mapping the EFI memory map
  * @data: EFI memory map data
@@ -51,11 +47,6 @@ int __init __efi_memmap_init(struct efi_memory_map_data *data)
                return -ENOMEM;
        }
 
-       if (efi.memmap.flags & (EFI_MEMMAP_MEMBLOCK | EFI_MEMMAP_SLAB))
-               __efi_memmap_free(efi.memmap.phys_map,
-                                 efi.memmap.desc_size * efi.memmap.nr_map,
-                                 efi.memmap.flags);
-
        map.phys_map = data->phys_map;
        map.nr_map = data->size / data->desc_size;
        map.map_end = map.map + data->size;