Skip to contentSkip to conversation

mm: Better handling of adding new regions

PATCH In review Zhang Boyang 2 participants 6 messages Dec 22, 2022 – Jan 14, 2023

lore
Share & tools
lore
#0 lore raw

v3 cover mm: Better handling of adding new regions

Hi,

This is the V3 patchset.

V2 is at:
https://lists.gnu.org/archive/html/grub-devel/2022-12/msg00164.html

V1 is at:
https://lists.gnu.org/archive/html/grub-devel/2022-11/msg00147.html

For changes in V2->V3, please see my reply at:
https://lists.gnu.org/archive/html/grub-devel/2022-12/msg00250.html

Best Regards,
Zhang Boyang

Zhang Boyang (2):
mm: Adjust new region size to take management overhead into account
mm: Preallocate some space when adding new regions

 grub-core/kern/mm.c | 58 +++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 56 insertions(+), 2 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH v3 1/2 mm: Adjust new region size to take management overhead into account

When grub_memalign() encounters out-of-memory, it will try grub_mm_add_region_fn() to request more memory from system firmware. However, the size passed to it doesn't take region management overhead into account. Adding a memory area of "size" bytes may result in a heap region of less than "size" bytes truely avaliable. Thus, the new region may not be adequate for current allocation request, confusing out-of-memory handling code.

This patch introduces GRUB_MM_MGMT_OVERHEAD to address the region management overhead (e.g. metadata, padding). The value of this new constant must be large enough to make sure grub_malloc(size) always success after a successful call to grub_mm_init_region(addr, size + GRUB_MM_MGMT_OVERHEAD), for any given addr and size (assuming no interger overflow).

The size passed to grub_mm_add_region_fn() is now set to "size + align + GRUB_MM_MGMT_OVERHEAD", thus if grub_mm_add_region_fn() succeeded, current allocation request can always success.

---
 grub-core/kern/mm.c | 54 +++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 52 insertions(+), 2 deletions(-)
grub-core/kern/mm.c +52 -2
index ae2279133..5c0e5bbbf 100644
@@ -83,6 +83,43 @@
 
 
 
+/*
+ * GRUB_MM_MGMT_OVERHEAD is an upper bound of management overhead of
+ * each region, with any possible padding taken into account.
+ *
+ * The value must be large enough to make sure grub_malloc(size) always
+ * success after a successful call to
+ * grub_mm_init_region(addr, size + GRUB_MM_MGMT_OVERHEAD), for any given
+ * addr and size (assuming no interger overflow).
+ *
+ * The worst case which has maximum overhead is shown in the figure below:
+ *
+ * +-- addr
+ * v                                           |<-   size   ->|
+ * +---------+----------------+----------------+--------------+---------+
+ * | padding | grub_mm_region | grub_mm_header | usable bytes | padding |
+ * +---------+----------------+----------------+--------------+---------+
+ * |<-  a  ->|<-     b      ->|<-     c      ->|<-    d     ->|<-  e  ->|
+ *                                             ^
+ *    addr % GRUB_MM_ALIGN == 1                +--/ This will be the pointer
+ *    a == GRUB_MM_ALIGN - 1                      | returned by next
+ *                                                | grub_malloc(size),
+ *    b == sizeof (struct grub_mm_region)         | if no other suitable free
+ *    c == sizeof (struct grub_mm_header)         \ block is available.
+ *
+ *    size % GRUB_MM_ALIGN == 1
+ *    d == size
+ *    e == GRUB_MM_ALIGN - 1
+ *
+ * Therefore, the maximum overhead is:
+ *    a + b + c + e == (GRUB_MM_ALIGN - 1) + sizeof (struct grub_mm_region)
+ *                     + sizeof (struct grub_mm_header) + (GRUB_MM_ALIGN - 1)
+ */
+#define GRUB_MM_MGMT_OVERHEAD ((GRUB_MM_ALIGN - 1) \
+			       + sizeof (struct grub_mm_region) \
+			       + sizeof (struct grub_mm_header) \
+			       + (GRUB_MM_ALIGN - 1))
+
 grub_mm_region_t grub_mm_base;
 grub_mm_add_region_func_t grub_mm_add_region_fn;
 
@@ -230,6 +267,11 @@ grub_mm_init_region (void *addr, grub_size_t size)
 
   grub_dprintf ("regions", "No: considering a new region at %p of size %" PRIxGRUB_SIZE "\n",
 		addr, size);
+  /*
+   * If you want to modify the code below, please also take a look at
+   * GRUB_MM_MGMT_OVERHEAD and make sure it is synchronized with the code.
+   */
+
   /* Allocate a region from the head.  */
   r = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) addr, GRUB_MM_ALIGN);
 
@@ -410,6 +452,7 @@ grub_memalign (grub_size_t align, grub_size_t size)
 {
   grub_mm_region_t r;
   grub_size_t n = ((size + GRUB_MM_ALIGN - 1) >> GRUB_MM_ALIGN_LOG2) + 1;
+  grub_size_t grow;
   int count = 0;
 
   if (!grub_mm_base)
@@ -424,6 +467,13 @@ grub_memalign (grub_size_t align, grub_size_t size)
   if ((size + align) > ~(grub_size_t) 0x100000)
     goto fail;
 
+  /*
+   * Pre-calculate the size of heap growth (if applicable),
+   * with region management overhead taken into account.
+   */
+  if (grub_add (size + align, GRUB_MM_MGMT_OVERHEAD, &grow))
+    goto fail;
+
   align = (align >> GRUB_MM_ALIGN_LOG2);
   if (align == 0)
     align = 1;
@@ -447,7 +497,7 @@ grub_memalign (grub_size_t align, grub_size_t size)
       count++;
 
       if (grub_mm_add_region_fn != NULL &&
-          grub_mm_add_region_fn (size, GRUB_MM_ADD_REGION_CONSECUTIVE) == GRUB_ERR_NONE)
+          grub_mm_add_region_fn (grow, GRUB_MM_ADD_REGION_CONSECUTIVE) == GRUB_ERR_NONE)
 	goto again;
 
       /* fallthrough  */
@@ -462,7 +512,7 @@ grub_memalign (grub_size_t align, grub_size_t size)
            * Try again even if this fails, in case it was able to partially
            * satisfy the request
            */
-          grub_mm_add_region_fn (size, GRUB_MM_ADD_REGION_NONE);
+          grub_mm_add_region_fn (grow, GRUB_MM_ADD_REGION_NONE);
           goto again;
         }
 
Signature · 1 line
2.30.2
#2 lore raw

Re: [PATCH v3 1/2] mm: Adjust new region size to take management overhead into account

Quoted 15 lines · Zhang Boyang

When grub_memalign() encounters out-of-memory, it will try grub_mm_add_region_fn() to request more memory from system firmware. However, the size passed to it doesn't take region management overhead into account. Adding a memory area of "size" bytes may result in a heap region of less than "size" bytes truely avaliable. Thus, the new region may not be adequate for current allocation request, confusing out-of-memory handling code.

This patch introduces GRUB_MM_MGMT_OVERHEAD to address the region management overhead (e.g. metadata, padding). The value of this new constant must be large enough to make sure grub_malloc(size) always success after a successful call to grub_mm_init_region(addr, size + GRUB_MM_MGMT_OVERHEAD), for any given addr and size (assuming no interger overflow).

Quoted 2 lines

The size passed to grub_mm_add_region_fn() is now set to "size + align + GRUB_MM_MGMT_OVERHEAD", thus if grub_mm_add_region_fn() succeeded,

This comment is not in line with the code...

Quoted 24 lines

current allocation request can always success.

---
 grub-core/kern/mm.c | 54 +++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 52 insertions(+), 2 deletions(-)
grub-core/kern/mm.c +9 -0
index ae2279133..5c0e5bbbf 100644
@@ -83,6 +83,43 @@
 
 
 
+/*
+ * GRUB_MM_MGMT_OVERHEAD is an upper bound of management overhead of
+ * each region, with any possible padding taken into account.
+ *
+ * The value must be large enough to make sure grub_malloc(size) always
+ * success after a successful call to
+ * grub_mm_init_region(addr, size + GRUB_MM_MGMT_OVERHEAD), for any given
+ * addr and size (assuming no interger overflow).
+ *

grub-core/kern/mm.c line 104

Quoted 10 lines
grub-core/kern/mm.c +10 -0
+ * The worst case which has maximum overhead is shown in the figure below:
+ *
+ * +-- addr
+ * v                                           |<-   size   ->|
+ * +---------+----------------+----------------+--------------+---------+
+ * | padding | grub_mm_region | grub_mm_header | usable bytes | padding |
+ * +---------+----------------+----------------+--------------+---------+
+ * |<-  a  ->|<-     b      ->|<-     c      ->|<-    d     ->|<-  e  ->|
+ *                                             ^
+ *    addr % GRUB_MM_ALIGN == 1                +--/ This will be the pointer

s/addr % GRUB_MM_ALIGN == 1/assuming addr % GRUB_MM_ALIGN == 1/

It took me some time to understand what do you mean by that.

grub-core/kern/mm.c line 110

Quoted 6 lines
grub-core/kern/mm.c +6 -0
+ *    a == GRUB_MM_ALIGN - 1                      | returned by next
+ *                                                | grub_malloc(size),
+ *    b == sizeof (struct grub_mm_region)         | if no other suitable free
+ *    c == sizeof (struct grub_mm_header)         \ block is available.
+ *
+ *    size % GRUB_MM_ALIGN == 1

Ditto...

grub-core/kern/mm.c line 117

Quoted 7 lines
grub-core/kern/mm.c +7 -0
+ *    d == size
+ *    e == GRUB_MM_ALIGN - 1
+ *
+ * Therefore, the maximum overhead is:
+ *    a + b + c + e == (GRUB_MM_ALIGN - 1) + sizeof (struct grub_mm_region)
+ *                     + sizeof (struct grub_mm_header) + (GRUB_MM_ALIGN - 1)
+ */

Thank you for this comment! It clarifies a lot...

grub-core/kern/mm.c line 121

Quoted 4 lines
grub-core/kern/mm.c +4 -0
+#define GRUB_MM_MGMT_OVERHEAD ((GRUB_MM_ALIGN - 1) \
+			       + sizeof (struct grub_mm_region) \
+			       + sizeof (struct grub_mm_header) \
+			       + (GRUB_MM_ALIGN - 1))

It is correct from math POV but the value may not be very friendly to the CPU/MMU. Please look below for more details...

Quoted 25 lines
grub-core/kern/mm.c +7 -0
+
 grub_mm_region_t grub_mm_base;
 grub_mm_add_region_func_t grub_mm_add_region_fn;
 
@@ -230,6 +267,11 @@ grub_mm_init_region (void *addr, grub_size_t size)
 
   grub_dprintf ("regions", "No: considering a new region at %p of size %" PRIxGRUB_SIZE "\n",
 		addr, size);
+  /*
+   * If you want to modify the code below, please also take a look at
+   * GRUB_MM_MGMT_OVERHEAD and make sure it is synchronized with the code.
+   */
+
   /* Allocate a region from the head.  */
   r = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) addr, GRUB_MM_ALIGN);
 
@@ -410,6 +452,7 @@ grub_memalign (grub_size_t align, grub_size_t size)
 {
   grub_mm_region_t r;
   grub_size_t n = ((size + GRUB_MM_ALIGN - 1) >> GRUB_MM_ALIGN_LOG2) + 1;
+  grub_size_t grow;
   int count = 0;
 
   if (!grub_mm_base)

grub-core/kern/mm.c grub_memalign (grub_size_t align, grub_size_t size) · line 475

Quoted 9 lines
grub-core/kern/mm.c +6 -0
@@ -424,6 +467,13 @@ grub_memalign (grub_size_t align, grub_size_t size)
   if ((size + align) > ~(grub_size_t) 0x100000)
     goto fail;
 
+  /*
+   * Pre-calculate the size of heap growth (if applicable),
+   * with region management overhead taken into account.
+   */
+  if (grub_add (size + align, GRUB_MM_MGMT_OVERHEAD, &grow))
+    goto fail;

This should go before "if ((size + align) > ~(grub_size_t) 0x100000)" and then you should replace "size" with "grow". Additionally, as I said above due to GRUB_MM_MGMT_OVERHEAD value final "grow" value may not be very CPU/MMU friendly. So, I suggest to do ALIGN_UP(grow, 4096) after grub_add() and before "if ((grow + align) > ...".

Quoted 14 lines
grub-core/kern/mm.c +2 -1
+
   align = (align >> GRUB_MM_ALIGN_LOG2);
   if (align == 0)
     align = 1;
@@ -447,7 +497,7 @@ grub_memalign (grub_size_t align, grub_size_t size)
       count++;
 
       if (grub_mm_add_region_fn != NULL &&
-          grub_mm_add_region_fn (size, GRUB_MM_ADD_REGION_CONSECUTIVE) == GRUB_ERR_NONE)
+          grub_mm_add_region_fn (grow, GRUB_MM_ADD_REGION_CONSECUTIVE) == GRUB_ERR_NONE)
 	goto again;
 
       /* fallthrough  */

grub-core/kern/mm.c grub_memalign (grub_size_t align, grub_size_t size) · line 517

Quoted 7 lines
grub-core/kern/mm.c +1 -1
@@ -462,7 +512,7 @@ grub_memalign (grub_size_t align, grub_size_t size)
            * Try again even if this fails, in case it was able to partially
            * satisfy the request
            */
-          grub_mm_add_region_fn (size, GRUB_MM_ADD_REGION_NONE);
+          grub_mm_add_region_fn (grow, GRUB_MM_ADD_REGION_NONE);
           goto again;
         }

Daniel

#3 lore raw

Re: [PATCH v3 1/2] mm: Adjust new region size to take management overhead into account

Hi,

Quoted 16 lines · Daniel Kiper
Quoted 15 lines · Zhang Boyang

When grub_memalign() encounters out-of-memory, it will try grub_mm_add_region_fn() to request more memory from system firmware. However, the size passed to it doesn't take region management overhead into account. Adding a memory area of "size" bytes may result in a heap region of less than "size" bytes truely avaliable. Thus, the new region may not be adequate for current allocation request, confusing out-of-memory handling code.

This patch introduces GRUB_MM_MGMT_OVERHEAD to address the region management overhead (e.g. metadata, padding). The value of this new constant must be large enough to make sure grub_malloc(size) always success after a successful call to grub_mm_init_region(addr, size + GRUB_MM_MGMT_OVERHEAD), for any given addr and size (assuming no interger overflow).

Quoted 5 lines
Quoted 2 lines

The size passed to grub_mm_add_region_fn() is now set to "size + align + GRUB_MM_MGMT_OVERHEAD", thus if grub_mm_add_region_fn() succeeded,

This comment is not in line with the code...

Fixed in v4.

Quoted 28 lines
Quoted 28 lines

current allocation request can always success.

---
  grub-core/kern/mm.c | 54 +++++++++++++++++++++++++++++++++++++++++++--
  1 file changed, 52 insertions(+), 2 deletions(-)
grub-core/kern/mm.c +13 -0
index ae2279133..5c0e5bbbf 100644
@@ -83,6 +83,43 @@
 
  
 
+/*
+ * GRUB_MM_MGMT_OVERHEAD is an upper bound of management overhead of
+ * each region, with any possible padding taken into account.
+ *
+ * The value must be large enough to make sure grub_malloc(size) always
+ * success after a successful call to
+ * grub_mm_init_region(addr, size + GRUB_MM_MGMT_OVERHEAD), for any given
+ * addr and size (assuming no interger overflow).
+ *
+ * The worst case which has maximum overhead is shown in the figure below:
+ *
+ * +-- addr
+ * v                                           |<-   size   ->|
Quoted 9 lines

grub-core/kern/mm.c line 104

Quoted 6 lines
grub-core/kern/mm.c +6 -0
+ * +---------+----------------+----------------+--------------+---------+
+ * | padding | grub_mm_region | grub_mm_header | usable bytes | padding |
+ * +---------+----------------+----------------+--------------+---------+
+ * |<-  a  ->|<-     b      ->|<-     c      ->|<-    d     ->|<-  e  ->|
+ *                                             ^
+ *    addr % GRUB_MM_ALIGN == 1                +--/ This will be the pointer

s/addr % GRUB_MM_ALIGN == 1/assuming addr % GRUB_MM_ALIGN == 1/

Fixed in v4.

Quoted 57 lines

It took me some time to understand what do you mean by that.

grub-core/kern/mm.c line 110

Quoted 6 lines
grub-core/kern/mm.c +6 -0
+ *    a == GRUB_MM_ALIGN - 1                      | returned by next
+ *                                                | grub_malloc(size),
+ *    b == sizeof (struct grub_mm_region)         | if no other suitable free
+ *    c == sizeof (struct grub_mm_header)         \ block is available.
+ *
+ *    size % GRUB_MM_ALIGN == 1

Ditto...

grub-core/kern/mm.c line 117

Quoted 7 lines
grub-core/kern/mm.c +7 -0
+ *    d == size
+ *    e == GRUB_MM_ALIGN - 1
+ *
+ * Therefore, the maximum overhead is:
+ *    a + b + c + e == (GRUB_MM_ALIGN - 1) + sizeof (struct grub_mm_region)
+ *                     + sizeof (struct grub_mm_header) + (GRUB_MM_ALIGN - 1)
+ */

Thank you for this comment! It clarifies a lot...

grub-core/kern/mm.c line 121

Quoted 4 lines
grub-core/kern/mm.c +4 -0
+#define GRUB_MM_MGMT_OVERHEAD ((GRUB_MM_ALIGN - 1) \
+			       + sizeof (struct grub_mm_region) \
+			       + sizeof (struct grub_mm_header) \
+			       + (GRUB_MM_ALIGN - 1))

It is correct from math POV but the value may not be very friendly to the CPU/MMU. Please look below for more details...

Quoted 28 lines
grub-core/kern/mm.c +7 -0
+
  grub_mm_region_t grub_mm_base;
  grub_mm_add_region_func_t grub_mm_add_region_fn;
 
@@ -230,6 +267,11 @@ grub_mm_init_region (void *addr, grub_size_t size)
 
    grub_dprintf ("regions", "No: considering a new region at %p of size %" PRIxGRUB_SIZE "\n",
  		addr, size);
+  /*
+   * If you want to modify the code below, please also take a look at
+   * GRUB_MM_MGMT_OVERHEAD and make sure it is synchronized with the code.
+   */
+
    /* Allocate a region from the head.  */
    r = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) addr, GRUB_MM_ALIGN);
 
@@ -410,6 +452,7 @@ grub_memalign (grub_size_t align, grub_size_t size)
  {
    grub_mm_region_t r;
    grub_size_t n = ((size + GRUB_MM_ALIGN - 1) >> GRUB_MM_ALIGN_LOG2) + 1;
+  grub_size_t grow;
    int count = 0;
 
    if (!grub_mm_base)
@@ -424,6 +467,13 @@ grub_memalign (grub_size_t align, grub_size_t size)
    if ((size + align) > ~(grub_size_t) 0x100000)
      goto fail;
 
Quoted 13 lines

grub-core/kern/mm.c grub_memalign (grub_size_t align, grub_size_t size) · line 475

Quoted 6 lines
grub-core/kern/mm.c +6 -0
+  /*
+   * Pre-calculate the size of heap growth (if applicable),
+   * with region management overhead taken into account.
+   */
+  if (grub_add (size + align, GRUB_MM_MGMT_OVERHEAD, &grow))
+    goto fail;

This should go before "if ((size + align) > ~(grub_size_t) 0x100000)" and then you should replace "size" with "grow". Additionally, as I said above due to GRUB_MM_MGMT_OVERHEAD value final "grow" value may not be very CPU/MMU friendly. So, I suggest to do ALIGN_UP(grow, 4096) after grub_add() and before "if ((grow + align) > ...".

Fixed in v4. Please check if my changes matches your intention.

Quoted 23 lines
Quoted 14 lines
grub-core/kern/mm.c +2 -1
+
    align = (align >> GRUB_MM_ALIGN_LOG2);
    if (align == 0)
      align = 1;
@@ -447,7 +497,7 @@ grub_memalign (grub_size_t align, grub_size_t size)
        count++;
 
        if (grub_mm_add_region_fn != NULL &&
-          grub_mm_add_region_fn (size, GRUB_MM_ADD_REGION_CONSECUTIVE) == GRUB_ERR_NONE)
+          grub_mm_add_region_fn (grow, GRUB_MM_ADD_REGION_CONSECUTIVE) == GRUB_ERR_NONE)
  	goto again;
 
        /* fallthrough  */

grub-core/kern/mm.c grub_memalign (grub_size_t align, grub_size_t size) · line 517

Quoted 7 lines
grub-core/kern/mm.c +1 -1
@@ -462,7 +512,7 @@ grub_memalign (grub_size_t align, grub_size_t size)
             * Try again even if this fails, in case it was able to partially
             * satisfy the request
             */
-          grub_mm_add_region_fn (size, GRUB_MM_ADD_REGION_NONE);
+          grub_mm_add_region_fn (grow, GRUB_MM_ADD_REGION_NONE);
            goto again;
          }

Daniel

Quoted 10 lines · Daniel Kiper

grub-core/kern/mm.c grub_memalign (grub_size_t align, grub_size_t size)

Quoted 8 lines · Zhang Boyang
grub-core/kern/mm.c +2 -1
    /*
-   * Pre-calculate the size of heap growth (if applicable),
+   * Pre-calculate the optimal size of heap growth (if applicable),
     * with region management overhead taken into account.
     */
    if (grub_add (size + align, GRUB_MM_MGMT_OVERHEAD, &grow))
      goto fail;
+  grow = grub_max (grow, GRUB_MM_HEAP_GROW_EXTRA);
Quoted 4 lines

If you update the #1 patch as I suggested then this should be

   grow = grub_max (size, GRUB_MM_HEAP_GROW_EXTRA);

I think this shouldn't be changed. If "size" is used here, then "grow" will be irrelevant to GRUB_MM_MGMT_OVERHEAD.

Quoted 1 line

Daniel

Best Regards,
Zhang Boyang

#4 lore raw

PATCH v3 2/2 mm: Preallocate some space when adding new regions

In reply to Zhang Boyang

When grub_memalign() encounters out-of-memory, it will try grub_mm_add_region_fn() to request more memory from system firmware. However, it doesn't preallocate memory space for future allocation requests. In extreme cases, it requires one call to grub_mm_add_region_fn() for each memory allocation request. This can be very slow.

This patch introduces GRUB_MM_HEAP_GROW_EXTRA, the minimal heap growth granularity. The new region size is now set to the bigger one of its original value and GRUB_MM_HEAP_GROW_EXTRA. Thus, it will result in some memory space preallocated if current allocations request is small.

The value of GRUB_MM_HEAP_GROW_EXTRA is set to 1MB. If this value is smaller, the cost of small memory allocations will be higher. If this value is larger, more memory will be wasted and it might cause out-of-memory on machines with small amount of RAM.

---
 grub-core/kern/mm.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
grub-core/kern/mm.c +5 -1
index 5c0e5bbbf..16c28cab8 100644
@@ -120,6 +120,9 @@
 			       + sizeof (struct grub_mm_header) \
 			       + (GRUB_MM_ALIGN - 1))
 
+/* Minimal heap growth granularity when existing heap space is exhausted. */
+#define GRUB_MM_HEAP_GROW_EXTRA 0x100000
+
 grub_mm_region_t grub_mm_base;
 grub_mm_add_region_func_t grub_mm_add_region_fn;
 
@@ -468,11 +471,12 @@ grub_memalign (grub_size_t align, grub_size_t size)
     goto fail;
 
   /*
-   * Pre-calculate the size of heap growth (if applicable),
+   * Pre-calculate the optimal size of heap growth (if applicable),
    * with region management overhead taken into account.
    */
   if (grub_add (size + align, GRUB_MM_MGMT_OVERHEAD, &grow))
     goto fail;
+  grow = grub_max (grow, GRUB_MM_HEAP_GROW_EXTRA);
 
   align = (align >> GRUB_MM_ALIGN_LOG2);
   if (align == 0)
Signature · 1 line
2.30.2
#5 lore raw

Re: [PATCH v3 2/2] mm: Preallocate some space when adding new regions

Quoted 37 lines · Zhang Boyang

When grub_memalign() encounters out-of-memory, it will try grub_mm_add_region_fn() to request more memory from system firmware. However, it doesn't preallocate memory space for future allocation requests. In extreme cases, it requires one call to grub_mm_add_region_fn() for each memory allocation request. This can be very slow.

This patch introduces GRUB_MM_HEAP_GROW_EXTRA, the minimal heap growth granularity. The new region size is now set to the bigger one of its original value and GRUB_MM_HEAP_GROW_EXTRA. Thus, it will result in some memory space preallocated if current allocations request is small.

The value of GRUB_MM_HEAP_GROW_EXTRA is set to 1MB. If this value is smaller, the cost of small memory allocations will be higher. If this value is larger, more memory will be wasted and it might cause out-of-memory on machines with small amount of RAM.

---
 grub-core/kern/mm.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
grub-core/kern/mm.c +3 -0
index 5c0e5bbbf..16c28cab8 100644
@@ -120,6 +120,9 @@
 			       + sizeof (struct grub_mm_header) \
 			       + (GRUB_MM_ALIGN - 1))
 
+/* Minimal heap growth granularity when existing heap space is exhausted. */
+#define GRUB_MM_HEAP_GROW_EXTRA 0x100000
+
 grub_mm_region_t grub_mm_base;
 grub_mm_add_region_func_t grub_mm_add_region_fn;
 

grub-core/kern/mm.c grub_memalign (grub_size_t align, grub_size_t size) · line 479

Quoted 10 lines
grub-core/kern/mm.c +2 -1
@@ -468,11 +471,12 @@ grub_memalign (grub_size_t align, grub_size_t size)
     goto fail;
 
   /*
-   * Pre-calculate the size of heap growth (if applicable),
+   * Pre-calculate the optimal size of heap growth (if applicable),
    * with region management overhead taken into account.
    */
   if (grub_add (size + align, GRUB_MM_MGMT_OVERHEAD, &grow))
     goto fail;
+  grow = grub_max (grow, GRUB_MM_HEAP_GROW_EXTRA);

If you update the #1 patch as I suggested then this should be

  grow = grub_max (size, GRUB_MM_HEAP_GROW_EXTRA);

Daniel