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

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