SHA256
1
0
forked from pool/grub2

Accepting request 522250 from home:algraf:branches:Base:System

- Fix reboot in UEFI environments (bsc#1047331)
  * Add grub2-efi-Move-grub_reboot-into-kernel.patch
  * Refresh grub2-efi-Free-malloc-regions-on-exit.patch

OBS-URL: https://build.opensuse.org/request/show/522250
OBS-URL: https://build.opensuse.org/package/show/Base:System/grub2?expand=0&rev=277
This commit is contained in:
Dirk Mueller 2017-09-11 13:20:51 +00:00 committed by Git OBS Bridge
parent f7e71f54e0
commit f7b29ac20a
4 changed files with 274 additions and 45 deletions

View File

@ -1,7 +1,6 @@
From 0c5fbc745846a53cc04ac1052cfbd35c699394c5 Mon Sep 17 00:00:00 2001
From 33298c813c36a50ccc881c33c9b6eacfb830c21a Mon Sep 17 00:00:00 2001
From: Alexander Graf <agraf@suse.de>
Date: Thu, 19 May 2016 15:01:06 +0200
Subject: [PATCH] efi: Free malloc regions on exit
Subject: [PATCH v7 2/2] 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
@ -9,76 +8,172 @@ to descend the boot order using "exit" entries, such as PXE -> HD boot
scenarios.
Signed-off-by: Alexander Graf <agraf@suse.de>
---
v2 -> v3:
- 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
v3 -> v4:
- 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
v4 -> v5:
- free dynamic list entries on allocation removal
v5 -> v6:
- move next ptr to last field
- s/start_addr/address/
- s/grub_efi_unremember_pages()/grub_efi_drop_alloc()/
- s/grub_efi_remember_pages()/grub_efi_store_alloc()/
- move grub_efi_drop_alloc after grub_efi_store_alloc
- s/grub2/GRUB/
- Make grub_efi_memory_fini comment more verbose
- Drop useless efi_allocated_memory = NULL; line
v6 -> v7:
- rewrite grub_efi_drop_alloc() according to Daniel's comments
---
grub-core/kern/efi/init.c | 1 +
grub-core/kern/efi/mm.c | 24 ++++++++++++++++++++++++
grub-core/kern/efi/mm.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++
include/grub/efi/efi.h | 1 +
3 files changed, 26 insertions(+)
3 files changed, 81 insertions(+)
Index: grub-2.02~beta3/grub-core/kern/efi/init.c
===================================================================
--- grub-2.02~beta3.orig/grub-core/kern/efi/init.c
+++ grub-2.02~beta3/grub-core/kern/efi/init.c
@@ -167,4 +167,5 @@ grub_efi_fini (void)
diff --git a/grub-core/kern/efi/init.c b/grub-core/kern/efi/init.c
index 2c31847bf..3dfdf2d22 100644
--- a/grub-core/kern/efi/init.c
+++ b/grub-core/kern/efi/init.c
@@ -80,4 +80,5 @@ grub_efi_fini (void)
{
grub_efidisk_fini ();
grub_console_fini ();
+ grub_efi_memory_fini ();
}
Index: grub-2.02~beta3/grub-core/kern/efi/mm.c
===================================================================
--- grub-2.02~beta3.orig/grub-core/kern/efi/mm.c
+++ grub-2.02~beta3/grub-core/kern/efi/mm.c
@@ -49,6 +49,12 @@ static grub_efi_uintn_t finish_desc_size
diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c
index ac2a4c556..6da8c206a 100644
--- a/grub-core/kern/efi/mm.c
+++ b/grub-core/kern/efi/mm.c
@@ -49,6 +49,69 @@ 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_uint64_t start_addr;
+ grub_uint64_t pages;
+} efi_allocated_memory[16];
+unsigned int efi_allocated_memory_idx = 0;
+ 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,
@@ -440,6 +446,13 @@ add_memory_regions (grub_efi_memory_desc
(void *) ((grub_addr_t) start),
(unsigned) pages);
@@ -79,6 +142,7 @@ grub_efi_allocate_pages_real (grub_efi_physical_address_t address,
return 0;
}
+ /* Track up to 16 regions that we allocate from */
+ if (efi_allocated_memory_idx < ARRAY_SIZE(efi_allocated_memory)) {
+ efi_allocated_memory[efi_allocated_memory_idx].start_addr = start;
+ efi_allocated_memory[efi_allocated_memory_idx].pages = pages;
+ efi_allocated_memory_idx++;
+ }
+
grub_mm_init_region (addr, PAGES_TO_BYTES (pages));
+ grub_efi_store_alloc (address, pages);
return (void *) ((grub_addr_t) address);
}
required_pages -= pages;
@@ -451,6 +464,17 @@ add_memory_regions (grub_efi_memory_desc
@@ -108,6 +172,7 @@ 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__)
@@ -422,6 +487,20 @@ add_memory_regions (grub_efi_memory_descriptor_t *memory_map,
grub_fatal ("too little memory");
}
+void
+grub_efi_memory_fini (void)
+{
+ unsigned int i;
+
+ for (i = 0; i < efi_allocated_memory_idx; i++) {
+ grub_efi_free_pages (efi_allocated_memory[i].start_addr,
+ efi_allocated_memory[i].pages);
+ }
+ /*
+ * 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
Index: grub-2.02~beta3/include/grub/efi/efi.h
===================================================================
--- grub-2.02~beta3.orig/include/grub/efi/efi.h
+++ grub-2.02~beta3/include/grub/efi/efi.h
@@ -51,6 +51,7 @@ EXPORT_FUNC(grub_efi_get_memory_map) (gr
diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h
index 3fa082816..c996913e5 100644
--- a/include/grub/efi/efi.h
+++ b/include/grub/efi/efi.h
@@ -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);
@ -86,3 +181,6 @@ Index: grub-2.02~beta3/include/grub/efi/efi.h
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.12.3

View File

@ -0,0 +1,122 @@
From dee74e9bd88aa31e38b1e77dc7845930aa1cd60f Mon Sep 17 00:00:00 2001
From: Alexander Graf <agraf@suse.de>
Subject: [PATCH v7 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.
Signed-off-by: Alexander Graf <agraf@suse.de>
---
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
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
index a65c27f7f..1cc4ce374 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -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;
diff --git a/grub-core/kern/efi/efi.c b/grub-core/kern/efi/efi.c
index d467785fc..708581fcb 100644
--- a/grub-core/kern/efi/efi.c
+++ b/grub-core/kern/efi/efi.c
@@ -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);
diff --git a/grub-core/lib/efi/reboot.c b/grub-core/lib/efi/reboot.c
deleted file mode 100644
index 7de8bcb5d..000000000
--- a/grub-core/lib/efi/reboot.c
+++ /dev/null
@@ -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 (;;) ;
-}
diff --git a/include/grub/misc.h b/include/grub/misc.h
index 2a9f87cc2..372f009e8 100644
--- a/include/grub/misc.h
+++ b/include/grub/misc.h
@@ -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));
--
2.12.3

View File

@ -1,3 +1,10 @@
-------------------------------------------------------------------
Fri Sep 8 08:04:41 UTC 2017 - agraf@suse.com
- Fix reboot in UEFI environments (bsc#1047331)
* Add grub2-efi-Move-grub_reboot-into-kernel.patch
* Refresh grub2-efi-Free-malloc-regions-on-exit.patch
-------------------------------------------------------------------
Sun Sep 3 12:12:21 UTC 2017 - mchang@suse.com

View File

@ -231,7 +231,8 @@ Patch123: grub2-efi-xen-cfg-unquote.patch
Patch140: grub2-Add-hidden-menu-entries.patch
Patch141: grub2-SUSE-Add-the-t-hotkey.patch
# EFI free memory on exit fix (bsc#980739)
Patch150: grub2-efi-Free-malloc-regions-on-exit.patch
Patch150: grub2-efi-Move-grub_reboot-into-kernel.patch
Patch151: grub2-efi-Free-malloc-regions-on-exit.patch
# Linux root device related patches
Patch163: grub2-zipl-setup-fix-btrfs-multipledev.patch
Patch164: grub2-suse-remove-linux-root-param.patch
@ -359,9 +360,9 @@ provides support for %{platform} systems.
%package %{grubefiarch}
Summary: Bootloader with support for Linux, Multiboot and more
Group: System/Boot
# Require efibootmgr
# Without it grub-install is broken so break the package as well if unavailable
Group: System/Boot
Requires: efibootmgr
Requires(post): efibootmgr
Requires: %{name} = %{version}-%{release}
@ -498,6 +499,7 @@ swap partition while in resuming
%patch140 -p1
%patch141 -p1
%patch150 -p1
%patch151 -p1
%patch163 -p1
%patch164 -p1
%patch205 -p1