Skip to contentSkip to conversation

efi: Free memory on exit

PATCH Superseded Alexander Graf 1 participant 3 messages Aug 29, 2017

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v4 cover efi: Free memory on exit

When exiting grub, we currently leave the heap allocated. This means that if we exit and rerun grub, after a few iterations we can run into memory shortage.

This patch set frees the heap on exit. That way we can rerun grub many times without much problem.

v3 -> v4:

- Move grub_reboot to kern/efi/efi.c
- use dynamic list instead of static array at runtime
- use allocate_pool for list, so we are not bound by heap or random numbers
- remember all allocations, not just the heap

Alexander Graf (2):
efi: Move grub_reboot() into kernel
efi: Free malloc regions on exit

 grub-core/Makefile.core.def |  4 ---
 grub-core/kern/efi/efi.c    |  9 +++++++
 grub-core/kern/efi/init.c   |  1 +
 grub-core/kern/efi/mm.c     | 65 +++++++++++++++++++++++++++++++++++++++++++++
 grub-core/lib/efi/reboot.c  | 33 -----------------------
 include/grub/efi/efi.h      |  1 +
 include/grub/misc.h         |  3 ++-
 7 files changed, 78 insertions(+), 38 deletions(-)
 delete mode 100644 grub-core/lib/efi/reboot.c
Signature · 1 line
2.12.3
#1 lore raw

PATCH v4 1/2 efi: Move grub_reboot() into kernel

The reboot function calls machine_fini() and then reboots the system. Currently it lives in lib/ which means it gets compiled into the reboot module which lives on the heap.

In a following patch, I want to free the heap on machine_fini() though, so we would free the memory that the code is running in. That obviously breaks with smarter UEFI implementations.

So this patch moves it into the core. That way we ensure that all code running after machine_fini() in the UEFI case is running from memory that got allocated (and gets deallocated) by the UEFI core.

---

v3 -> v4:

- Move grub_reboot to kern/efi/efi.c

---
 grub-core/Makefile.core.def |  4 ----
 grub-core/kern/efi/efi.c    |  9 +++++++++
 grub-core/lib/efi/reboot.c  | 33 ---------------------------------
 include/grub/misc.h         |  3 ++-
 4 files changed, 11 insertions(+), 38 deletions(-)
 delete mode 100644 grub-core/lib/efi/reboot.c
grub-core/Makefile.core.def +0 -4
index a65c27f7f..1cc4ce374 100644
@@ -872,10 +872,6 @@ module = {
   name = reboot;
   i386 = lib/i386/reboot.c;
   i386 = lib/i386/reboot_trampoline.S;
-  ia64_efi = lib/efi/reboot.c;
-  x86_64_efi = lib/efi/reboot.c;
-  arm_efi = lib/efi/reboot.c;
-  arm64_efi = lib/efi/reboot.c;
   powerpc_ieee1275 = lib/ieee1275/reboot.c;
   sparc64_ieee1275 = lib/ieee1275/reboot.c;
   mips_arc = lib/mips/arc/reboot.c;
grub-core/kern/efi/efi.c +9 -0
index d467785fc..708581fcb 100644
@@ -155,6 +155,15 @@ grub_efi_get_loaded_image (grub_efi_handle_t image_handle)
 }
 
 void
+grub_reboot (void)
+{
+  grub_machine_fini (GRUB_LOADER_FLAG_NORETURN);
+  efi_call_4 (grub_efi_system_table->runtime_services->reset_system,
+              GRUB_EFI_RESET_COLD, GRUB_EFI_SUCCESS, 0, NULL);
+  for (;;) ;
+}
+
+void
 grub_exit (void)
 {
   grub_machine_fini (GRUB_LOADER_FLAG_NORETURN);
grub-core/lib/efi/reboot.c deleted +0 -33
deleted file mode 100644
index 7de8bcb5d..000000000
@@ -1,33 +0,0 @@
-/*
- *  GRUB  --  GRand Unified Bootloader
- *  Copyright (C) 2011  Free Software Foundation, Inc.
- *
- *  GRUB is free software: you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation, either version 3 of the License, or
- *  (at your option) any later version.
- *
- *  GRUB is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-#include <grub/efi/api.h>
-#include <grub/efi/efi.h>
-#include <grub/mm.h>
-#include <grub/misc.h>
-#include <grub/kernel.h>
-#include <grub/loader.h>
-
-void
-grub_reboot (void)
-{
-  grub_machine_fini (GRUB_LOADER_FLAG_NORETURN);
-  efi_call_4 (grub_efi_system_table->runtime_services->reset_system,
-              GRUB_EFI_RESET_COLD, GRUB_EFI_SUCCESS, 0, NULL);
-  for (;;) ;
-}
include/grub/misc.h +2 -1
index 2a9f87cc2..372f009e8 100644
@@ -396,7 +396,8 @@ grub_abs (int x)
 }
 
 /* Reboot the machine.  */
-#if defined (GRUB_MACHINE_EMU) || defined (GRUB_MACHINE_QEMU_MIPS)
+#if defined (GRUB_MACHINE_EMU) || defined (GRUB_MACHINE_QEMU_MIPS) || \
+    defined (GRUB_MACHINE_EFI)
 void EXPORT_FUNC(grub_reboot) (void) __attribute__ ((noreturn));
 #else
 void grub_reboot (void) __attribute__ ((noreturn));
Signature · 1 line
2.12.3
#2 lore raw

PATCH v4 2/2 efi: Free malloc regions on exit

In reply to Alexander Graf

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.

---

v1 -> v2:

- add comment explaining the number of regions
- move nr of regions into a define
- add warning if we exceed the number of freeable regions
- reset region counter to 0 on fini

v2 -> v3:

- use dynamic list instead of static array at runtime
- use allocate_pool for list, so we are not bound by heap or random numbers
- remember all allocations, not just the heap

---
 grub-core/kern/efi/init.c |  1 +
 grub-core/kern/efi/mm.c   | 65 +++++++++++++++++++++++++++++++++++++++++++++++
 include/grub/efi/efi.h    |  1 +
 3 files changed, 67 insertions(+)
grub-core/kern/efi/init.c +1 -0
index 2c31847bf..3dfdf2d22 100644
@@ -80,4 +80,5 @@ grub_efi_fini (void)
 {
   grub_efidisk_fini ();
   grub_console_fini ();
+  grub_efi_memory_fini ();
 }
grub-core/kern/efi/mm.c +65 -0

83 lines not on this page · raw message

include/grub/efi/efi.h +1 -0
index 3fa082816..c996913e5 100644
@@ -55,6 +55,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);
Signature · 1 line
2.12.3