Blame SOURCES/0303-kern-efi-mm-Always-request-a-fixed-number-of-pages-o.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:19 +1000
b35c50
Subject: [PATCH] kern/efi/mm: Always request a fixed number of pages on init
b35c50
b35c50
When initializing the EFI memory subsystem, we will by default request
b35c50
a quarter of the available memory, bounded by a minimum/maximum value.
b35c50
Given that we're about to extend the EFI memory system to dynamically
b35c50
request additional pages from the firmware as required, this scaling of
b35c50
requested memory based on available memory will not make a lot of sense
b35c50
anymore.
b35c50
b35c50
Remove this logic as a preparatory patch such that we'll instead defer
b35c50
to the runtime memory allocator. Note that ideally, we'd want to change
b35c50
this after dynamic requesting of pages has been implemented for the EFI
b35c50
platform. But because we'll need to split up initialization of the
b35c50
memory subsystem and the request of pages from the firmware, we'd have
b35c50
to duplicate quite some logic at first only to remove it afterwards
b35c50
again. This seems quite pointless, so we instead have patches slightly
b35c50
out of order.
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 938c3760b8c0fca759140be48307179b50107ff6)
b35c50
---
b35c50
 grub-core/kern/efi/mm.c | 35 +++--------------------------------
b35c50
 1 file changed, 3 insertions(+), 32 deletions(-)
b35c50
b35c50
diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c
b35c50
index e460b072e6..782a1365a1 100644
b35c50
--- a/grub-core/kern/efi/mm.c
b35c50
+++ b/grub-core/kern/efi/mm.c
b35c50
@@ -38,9 +38,8 @@
b35c50
    a multiplier of 4KB.  */
b35c50
 #define MEMORY_MAP_SIZE	0x3000
b35c50
 
b35c50
-/* The minimum and maximum heap size for GRUB itself.  */
b35c50
-#define MIN_HEAP_SIZE	0x100000
b35c50
-#define MAX_HEAP_SIZE	(1600 * 0x100000)
b35c50
+/* The default heap size for GRUB itself in bytes.  */
b35c50
+#define DEFAULT_HEAP_SIZE	0x100000
b35c50
 
b35c50
 static void *finish_mmap_buf = 0;
b35c50
 static grub_efi_uintn_t finish_mmap_size = 0;
b35c50
@@ -514,23 +513,6 @@ filter_memory_map (grub_efi_memory_descriptor_t *memory_map,
b35c50
   return filtered_desc;
b35c50
 }
b35c50
 
b35c50
-/* Return the total number of pages.  */
b35c50
-static grub_efi_uint64_t
b35c50
-get_total_pages (grub_efi_memory_descriptor_t *memory_map,
b35c50
-		 grub_efi_uintn_t desc_size,
b35c50
-		 grub_efi_memory_descriptor_t *memory_map_end)
b35c50
-{
b35c50
-  grub_efi_memory_descriptor_t *desc;
b35c50
-  grub_efi_uint64_t total = 0;
b35c50
-
b35c50
-  for (desc = memory_map;
b35c50
-       desc < memory_map_end;
b35c50
-       desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))
b35c50
-    total += desc->num_pages;
b35c50
-
b35c50
-  return total;
b35c50
-}
b35c50
-
b35c50
 /* Add memory regions.  */
b35c50
 static void
b35c50
 add_memory_regions (grub_efi_memory_descriptor_t *memory_map,
b35c50
@@ -694,8 +676,6 @@ grub_efi_mm_init (void)
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_efi_uint64_t total_pages;
b35c50
-  grub_efi_uint64_t required_pages;
b35c50
   int mm_status;
b35c50
 
b35c50
   grub_nx_init ();
b35c50
@@ -737,22 +717,13 @@ grub_efi_mm_init (void)
b35c50
   filtered_memory_map_end = filter_memory_map (memory_map, filtered_memory_map,
b35c50
 					       desc_size, memory_map_end);
b35c50
 
b35c50
-  /* By default, request a quarter of the available memory.  */
b35c50
-  total_pages = get_total_pages (filtered_memory_map, desc_size,
b35c50
-				 filtered_memory_map_end);
b35c50
-  required_pages = (total_pages >> 2);
b35c50
-  if (required_pages < BYTES_TO_PAGES (MIN_HEAP_SIZE))
b35c50
-    required_pages = BYTES_TO_PAGES (MIN_HEAP_SIZE);
b35c50
-  else if (required_pages > BYTES_TO_PAGES (MAX_HEAP_SIZE))
b35c50
-    required_pages = BYTES_TO_PAGES (MAX_HEAP_SIZE);
b35c50
-
b35c50
   /* Sort the filtered descriptors, so that GRUB can allocate pages
b35c50
      from smaller regions.  */
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, required_pages);
b35c50
+		      filtered_memory_map_end, BYTES_TO_PAGES (DEFAULT_HEAP_SIZE));
b35c50
 
b35c50
 #if 0
b35c50
   /* For debug.  */