Blame SOURCES/0321-mm-Preallocate-some-space-when-adding-new-regions.patch

fd0330
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
fd0330
From: Zhang Boyang <zhangboyang.id@gmail.com>
fd0330
Date: Sun, 29 Jan 2023 19:49:32 +0800
fd0330
Subject: [PATCH] mm: Preallocate some space when adding new regions
fd0330
fd0330
When grub_memalign() encounters out-of-memory, it will try
fd0330
grub_mm_add_region_fn() to request more memory from system firmware.
fd0330
However, it doesn't preallocate memory space for future allocation
fd0330
requests. In extreme cases, it requires one call to
fd0330
grub_mm_add_region_fn() for each memory allocation request. This can
fd0330
be very slow.
fd0330
fd0330
This patch introduces GRUB_MM_HEAP_GROW_EXTRA, the minimal heap growth
fd0330
granularity. The new region size is now set to the bigger one of its
fd0330
original value and GRUB_MM_HEAP_GROW_EXTRA. Thus, it will result in some
fd0330
memory space preallocated if current allocations request is small.
fd0330
fd0330
The value of GRUB_MM_HEAP_GROW_EXTRA is set to 1MB. If this value is
fd0330
smaller, the cost of small memory allocations will be higher. If this
fd0330
value is larger, more memory will be wasted and it might cause
fd0330
out-of-memory on machines with small amount of RAM.
fd0330
fd0330
Signed-off-by: Zhang Boyang <zhangboyang.id@gmail.com>
fd0330
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
fd0330
(cherry picked from commit 21869baec15239b6d99122b32b14a778af4c754f)
fd0330
---
fd0330
 grub-core/kern/mm.c | 6 ++++++
fd0330
 1 file changed, 6 insertions(+)
fd0330
fd0330
diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c
fd0330
index f29a3e5cbd..cc8a4703bc 100644
fd0330
--- a/grub-core/kern/mm.c
fd0330
+++ b/grub-core/kern/mm.c
fd0330
@@ -123,6 +123,9 @@
fd0330
 /* The size passed to grub_mm_add_region_fn() is aligned up by this value. */
fd0330
 #define GRUB_MM_HEAP_GROW_ALIGN	4096
fd0330
 
fd0330
+/* Minimal heap growth granularity when existing heap space is exhausted. */
fd0330
+#define GRUB_MM_HEAP_GROW_EXTRA	0x100000
fd0330
+
fd0330
 grub_mm_region_t grub_mm_base;
fd0330
 grub_mm_add_region_func_t grub_mm_add_region_fn;
fd0330
 
fd0330
@@ -471,6 +474,9 @@ grub_memalign (grub_size_t align, grub_size_t size)
fd0330
   if (grub_add (size + align, GRUB_MM_MGMT_OVERHEAD, &grow))
fd0330
     goto fail;
fd0330
 
fd0330
+  /* Preallocate some extra space if heap growth is small. */
fd0330
+  grow = grub_max (grow, GRUB_MM_HEAP_GROW_EXTRA);
fd0330
+
fd0330
   /* Align up heap growth to make it friendly to CPU/MMU. */
fd0330
   if (grow > ~(grub_size_t) (GRUB_MM_HEAP_GROW_ALIGN - 1))
fd0330
     goto fail;