nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0305-kern-efi-mm-Pass-up-errors-from-add_memory_regions.patch

b35c50
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
b35c50
From: Patrick Steinhardt <ps@pks.im>
b35c50
Date: Thu, 21 Apr 2022 15:24:21 +1000
b35c50
Subject: [PATCH] kern/efi/mm: Pass up errors from add_memory_regions()
b35c50
b35c50
The function add_memory_regions() is currently only called on system
b35c50
initialization to allocate a fixed amount of pages. As such, it didn't
b35c50
need to return any errors: in case it failed, we cannot proceed anyway.
b35c50
This will change with the upcoming support for requesting more memory
b35c50
from the firmware at runtime, where it doesn't make sense anymore to
b35c50
fail hard.
b35c50
b35c50
Refactor the function to return an error to prepare for this. Note that
b35c50
this does not change the behaviour when initializing the memory system
b35c50
because grub_efi_mm_init() knows to call grub_fatal() in case
b35c50
grub_efi_mm_add_regions() returns an error.
b35c50
b35c50
Signed-off-by: Patrick Steinhardt <ps@pks.im>
b35c50
Signed-off-by: Daniel Axtens <dja@axtens.net>
b35c50
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
b35c50
Tested-by: Patrick Steinhardt <ps@pks.im>
b35c50
(cherry picked from commit 15a015698921240adc1ac266a3b5bc5fcbd81521)
b35c50
---
b35c50
 grub-core/kern/efi/mm.c | 22 +++++++++++++++-------
b35c50
 1 file changed, 15 insertions(+), 7 deletions(-)
b35c50
b35c50
diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c
b35c50
index a1d3b51fe6..e0ebc65dba 100644
b35c50
--- a/grub-core/kern/efi/mm.c
b35c50
+++ b/grub-core/kern/efi/mm.c
b35c50
@@ -514,7 +514,7 @@ filter_memory_map (grub_efi_memory_descriptor_t *memory_map,
b35c50
 }
b35c50
 
b35c50
 /* Add memory regions.  */
b35c50
-static void
b35c50
+static grub_err_t
b35c50
 add_memory_regions (grub_efi_memory_descriptor_t *memory_map,
b35c50
 		    grub_efi_uintn_t desc_size,
b35c50
 		    grub_efi_memory_descriptor_t *memory_map_end,
b35c50
@@ -542,9 +542,9 @@ add_memory_regions (grub_efi_memory_descriptor_t *memory_map,
b35c50
 					   GRUB_EFI_ALLOCATE_ADDRESS,
b35c50
 					   GRUB_EFI_LOADER_CODE);      
b35c50
       if (! addr)
b35c50
-	grub_fatal ("cannot allocate conventional memory %p with %u pages",
b35c50
-		    (void *) ((grub_addr_t) start),
b35c50
-		    (unsigned) pages);
b35c50
+	return grub_error (GRUB_ERR_OUT_OF_MEMORY,
b35c50
+			    "Memory starting at %p (%u pages) marked as free, but EFI would not allocate",
b35c50
+			    (void *) ((grub_addr_t) start), (unsigned) pages);
b35c50
 
b35c50
       grub_mm_init_region (addr, PAGES_TO_BYTES (pages));
b35c50
 
b35c50
@@ -554,7 +554,11 @@ add_memory_regions (grub_efi_memory_descriptor_t *memory_map,
b35c50
     }
b35c50
 
b35c50
   if (required_pages > 0)
b35c50
-    grub_fatal ("too little memory");
b35c50
+    return grub_error (GRUB_ERR_OUT_OF_MEMORY,
b35c50
+                       "could not allocate all requested memory: %" PRIuGRUB_UINT64_T " pages still required after iterating EFI memory map",
b35c50
+                       required_pages);
b35c50
+
b35c50
+  return GRUB_ERR_NONE;
b35c50
 }
b35c50
 
b35c50
 void
b35c50
@@ -676,6 +680,7 @@ grub_efi_mm_add_regions (grub_size_t required_bytes)
b35c50
   grub_efi_memory_descriptor_t *filtered_memory_map_end;
b35c50
   grub_efi_uintn_t map_size;
b35c50
   grub_efi_uintn_t desc_size;
b35c50
+  grub_err_t err;
b35c50
   int mm_status;
b35c50
 
b35c50
   grub_nx_init ();
b35c50
@@ -722,8 +727,11 @@ grub_efi_mm_add_regions (grub_size_t required_bytes)
b35c50
   sort_memory_map (filtered_memory_map, desc_size, filtered_memory_map_end);
b35c50
 
b35c50
   /* Allocate memory regions for GRUB's memory management.  */
b35c50
-  add_memory_regions (filtered_memory_map, desc_size,
b35c50
-		      filtered_memory_map_end, BYTES_TO_PAGES (required_bytes));
b35c50
+  err = add_memory_regions (filtered_memory_map, desc_size,
b35c50
+			    filtered_memory_map_end,
b35c50
+			    BYTES_TO_PAGES (required_bytes));
b35c50
+  if (err != GRUB_ERR_NONE)
b35c50
+    return err;
b35c50
 
b35c50
 #if 0
b35c50
   /* For debug.  */