You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
159 lines
4.9 KiB
159 lines
4.9 KiB
4 years ago
|
From 6bea96c79a0c25cfd4ca1e0ff203fa7600be5a58 Mon Sep 17 00:00:00 2001
|
||
|
From: Alexander Graf <agraf@suse.de>
|
||
|
Date: Thu, 31 Aug 2017 16:40:19 +0200
|
||
|
Subject: [PATCH 324/336] efi: Free malloc regions on exit
|
||
|
|
||
|
When we exit grub, we don't free all the memory that we allocated earlier
|
||
|
for our heap region. This can cause problems with setups where you try
|
||
|
to descend the boot order using "exit" entries, such as PXE -> HD boot
|
||
|
scenarios.
|
||
|
|
||
|
Signed-off-by: Alexander Graf <agraf@suse.de>
|
||
|
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
|
||
|
Upstream-commit-id: 92bfc33db98
|
||
|
---
|
||
|
grub-core/kern/efi/init.c | 1 +
|
||
|
grub-core/kern/efi/mm.c | 82 +++++++++++++++++++++++++++++++++++++++
|
||
|
include/grub/efi/efi.h | 1 +
|
||
|
3 files changed, 84 insertions(+)
|
||
|
|
||
|
diff --git a/grub-core/kern/efi/init.c b/grub-core/kern/efi/init.c
|
||
|
index c5d8a76144a..e8bf993f6d4 100644
|
||
|
--- a/grub-core/kern/efi/init.c
|
||
|
+++ b/grub-core/kern/efi/init.c
|
||
|
@@ -111,4 +111,5 @@ grub_efi_fini (void)
|
||
|
{
|
||
|
grub_efidisk_fini ();
|
||
|
grub_console_fini ();
|
||
|
+ grub_efi_memory_fini ();
|
||
|
}
|
||
|
diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c
|
||
|
index ddeca6073f6..abe9c70557f 100644
|
||
|
--- a/grub-core/kern/efi/mm.c
|
||
|
+++ b/grub-core/kern/efi/mm.c
|
||
|
@@ -49,6 +49,70 @@ static grub_efi_uintn_t finish_desc_size;
|
||
|
static grub_efi_uint32_t finish_desc_version;
|
||
|
int grub_efi_is_finished = 0;
|
||
|
|
||
|
+/*
|
||
|
+ * We need to roll back EFI allocations on exit. Remember allocations that
|
||
|
+ * we'll free on exit.
|
||
|
+ */
|
||
|
+struct efi_allocation;
|
||
|
+struct efi_allocation {
|
||
|
+ grub_efi_physical_address_t address;
|
||
|
+ grub_efi_uint64_t pages;
|
||
|
+ struct efi_allocation *next;
|
||
|
+};
|
||
|
+static struct efi_allocation *efi_allocated_memory;
|
||
|
+
|
||
|
+static void
|
||
|
+grub_efi_store_alloc (grub_efi_physical_address_t address,
|
||
|
+ grub_efi_uintn_t pages)
|
||
|
+{
|
||
|
+ grub_efi_boot_services_t *b;
|
||
|
+ struct efi_allocation *alloc;
|
||
|
+ grub_efi_status_t status;
|
||
|
+
|
||
|
+ b = grub_efi_system_table->boot_services;
|
||
|
+ status = efi_call_3 (b->allocate_pool, GRUB_EFI_LOADER_DATA,
|
||
|
+ sizeof(*alloc), (void**)&alloc);
|
||
|
+
|
||
|
+ if (status == GRUB_EFI_SUCCESS)
|
||
|
+ {
|
||
|
+ alloc->next = efi_allocated_memory;
|
||
|
+ alloc->address = address;
|
||
|
+ alloc->pages = pages;
|
||
|
+ efi_allocated_memory = alloc;
|
||
|
+ }
|
||
|
+ else
|
||
|
+ grub_printf ("Could not malloc memory to remember EFI allocation. "
|
||
|
+ "Exiting GRUB won't free all memory.\n");
|
||
|
+}
|
||
|
+
|
||
|
+static void
|
||
|
+grub_efi_drop_alloc (grub_efi_physical_address_t address,
|
||
|
+ grub_efi_uintn_t pages)
|
||
|
+{
|
||
|
+ struct efi_allocation *ea, *eap;
|
||
|
+ grub_efi_boot_services_t *b;
|
||
|
+
|
||
|
+ b = grub_efi_system_table->boot_services;
|
||
|
+
|
||
|
+ for (eap = NULL, ea = efi_allocated_memory; ea; eap = ea, ea = ea->next)
|
||
|
+ {
|
||
|
+ if (ea->address != address || ea->pages != pages)
|
||
|
+ continue;
|
||
|
+
|
||
|
+ /* Remove the current entry from the list. */
|
||
|
+ if (eap)
|
||
|
+ eap->next = ea->next;
|
||
|
+ else
|
||
|
+ efi_allocated_memory = ea->next;
|
||
|
+
|
||
|
+ /* Then free the memory backing it. */
|
||
|
+ efi_call_1 (b->free_pool, ea);
|
||
|
+
|
||
|
+ /* And leave, we're done. */
|
||
|
+ break;
|
||
|
+ }
|
||
|
+}
|
||
|
+
|
||
|
/* Allocate pages below a specified address */
|
||
|
void *
|
||
|
grub_efi_allocate_pages_max (grub_efi_physical_address_t max,
|
||
|
@@ -78,6 +142,8 @@ grub_efi_allocate_pages_max (grub_efi_physical_address_t max,
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
+ grub_efi_store_alloc (address, pages);
|
||
|
+
|
||
|
return (void *) ((grub_addr_t) address);
|
||
|
}
|
||
|
|
||
|
@@ -139,6 +205,8 @@ grub_efi_free_pages (grub_efi_physical_address_t address,
|
||
|
|
||
|
b = grub_efi_system_table->boot_services;
|
||
|
efi_call_2 (b->free_pages, address, pages);
|
||
|
+
|
||
|
+ grub_efi_drop_alloc (address, pages);
|
||
|
}
|
||
|
|
||
|
#if defined (__i386__) || defined (__x86_64__)
|
||
|
@@ -446,6 +514,20 @@ add_memory_regions (grub_efi_memory_descriptor_t *memory_map,
|
||
|
grub_fatal ("too little memory");
|
||
|
}
|
||
|
|
||
|
+void
|
||
|
+grub_efi_memory_fini (void)
|
||
|
+{
|
||
|
+ /*
|
||
|
+ * Free all stale allocations. grub_efi_free_pages() will remove
|
||
|
+ * the found entry from the list and it will always find the first
|
||
|
+ * list entry (efi_allocated_memory is the list start). Hence we
|
||
|
+ * remove all entries from the list until none is left altogether.
|
||
|
+ */
|
||
|
+ while (efi_allocated_memory)
|
||
|
+ grub_efi_free_pages (efi_allocated_memory->address,
|
||
|
+ efi_allocated_memory->pages);
|
||
|
+}
|
||
|
+
|
||
|
#if 0
|
||
|
/* Print the memory map. */
|
||
|
static void
|
||
|
diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h
|
||
|
index 9a2da0eb38d..f761bbb1389 100644
|
||
|
--- a/include/grub/efi/efi.h
|
||
|
+++ b/include/grub/efi/efi.h
|
||
|
@@ -51,6 +51,7 @@ EXPORT_FUNC(grub_efi_get_memory_map) (grub_efi_uintn_t *memory_map_size,
|
||
|
grub_efi_uintn_t *map_key,
|
||
|
grub_efi_uintn_t *descriptor_size,
|
||
|
grub_efi_uint32_t *descriptor_version);
|
||
|
+void grub_efi_memory_fini (void);
|
||
|
grub_efi_loaded_image_t *EXPORT_FUNC(grub_efi_get_loaded_image) (grub_efi_handle_t image_handle);
|
||
|
void EXPORT_FUNC(grub_efi_print_device_path) (grub_efi_device_path_t *dp);
|
||
|
char *EXPORT_FUNC(grub_efi_get_filename) (grub_efi_device_path_t *dp);
|
||
|
--
|
||
|
2.26.2
|
||
|
|