Skip to contentSkip to conversation

mm: Better handling of adding new regions

PATCH Superseded Zhang Boyang 2 participants 12 messages Nov 29, 2022 – Dec 21, 2022

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover mm: Better handling of adding new regions

This patchset is splitted from the patch at
https://lists.gnu.org/archive/html/grub-devel/2022-10/msg00077.html

Sending as new patchset since some part of previous patchset have been merged.

Changes:
Renamed guarantee to bound.
New region size is now adjuested before comparing to GRUB_MM_HEAP_GROW. (i.e. GRUB_MM_HEAP_GROW now doesn't take management cost into account)

Best Regards,
Zhang Boyang

#1 lore raw

PATCH 1/2 mm: Adjust new region size to take management cost 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 cost into account. Adding a memory area of size bytes will result in a heap region of less than size bytes truely avaliable. Thus, the new region might not adequate for current allocation request, confusing out-of-memory handling code.

This patch introduces GRUB_MM_MAX_COST to address the region management cost (e.g. metadata, alignment). The value of this new constant should make grub_malloc (size) always success after a successful call to grub_mm_init_region (addr, size + GRUB_MM_MAX_COST).

The new region size is now set to size + GRUB_MM_MAX_COST, thus if grub_mm_add_region_fn() succeeded, current allocation request can always success.

---
 grub-core/kern/mm.c       | 15 ++++++++++++---
 include/grub/mm_private.h |  9 +++++++++
 2 files changed, 21 insertions(+), 3 deletions(-)
grub-core/kern/mm.c +12 -3
index ae2279133..973cb6b15 100644
@@ -410,7 +410,9 @@ 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 bound;
   int count = 0;
+  grub_size_t grow;
 
   if (!grub_mm_base)
     goto fail;
@@ -418,10 +420,13 @@ grub_memalign (grub_size_t align, grub_size_t size)
   if (size > ~(grub_size_t) align)
     goto fail;
 
+  /* If largest free chunk in region >= bound, allocation can always success */
+  bound = size + align;
+
   /* We currently assume at least a 32-bit grub_size_t,
      so limiting allocations to <adress space size> - 1MiB
      in name of sanity is beneficial. */
-  if ((size + align) > ~(grub_size_t) 0x100000)
+  if (bound > ~(grub_size_t) 0x100000)
     goto fail;
 
   align = (align >> GRUB_MM_ALIGN_LOG2);
@@ -443,11 +448,15 @@ grub_memalign (grub_size_t align, grub_size_t size)
   switch (count)
     {
     case 0:
+      /* Adjust heap growth to address the region management cost. */
+      if (grub_add (bound, GRUB_MM_MAX_COST, &grow))
+	goto fail;
+
       /* Request additional pages, contiguous */
       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 +471,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;
         }
 
include/grub/mm_private.h +9 -0
index 96c2d816b..f212110e4 100644
@@ -95,6 +95,15 @@ typedef struct grub_mm_region
 }
 *grub_mm_region_t;
 
+/*
+ * Set an upper bound of management cost of each region,
+ * with sizeof(struct grub_mm_region), sizeof(struct grub_mm_header) and
+ * any possible alignment or padding taken into account.
+ * The value should make `grub_malloc (size)` always success after a successful
+ * call to `grub_mm_init_region (addr, size + GRUB_MM_MAX_COST)`.
+ */
+#define GRUB_MM_MAX_COST 0x1000
+
 #ifndef GRUB_MACHINE_EMU
 extern grub_mm_region_t EXPORT_VAR (grub_mm_base);
 #endif
Signature · 1 line
2.30.2
#2 lore raw

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

Quoted 3 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 cost into

s/cost/overhead/? If yes then please fix everything below too...

Quoted 1 line

account. Adding a memory area of size bytes will result in a heap

s/`/"/ Same below if needed...

Quoted 2 lines

region of less than size bytes truely avaliable. Thus, the new region might not adequate for current allocation request, confusing

s/might not/may not be/

Quoted 3 lines

out-of-memory handling code.

This patch introduces GRUB_MM_MAX_COST to address the region management

s/GRUB_MM_MAX_COST/GRUB_MM_MGMT_OVERHEAD/?

Quoted 2 lines

cost (e.g. metadata, alignment). The value of this new constant should make grub_malloc (size) always success after a successful call to

s/grub_malloc (size)/grub_malloc(size)/

Quoted 1 line

grub_mm_init_region (addr, size + GRUB_MM_MAX_COST).

s/grub_mm_init_region (addr, size + GRUB_MM_MAX_COST)/grub_mm_init_region(addr, size + GRUB_MM_MAX_COST)/

Quoted 15 lines

The new region size is now set to size + GRUB_MM_MAX_COST, thus if grub_mm_add_region_fn() succeeded, current allocation request can always success.

---
 grub-core/kern/mm.c       | 15 ++++++++++++---
 include/grub/mm_private.h |  9 +++++++++
 2 files changed, 21 insertions(+), 3 deletions(-)
grub-core/kern/mm.c +0 -0
index ae2279133..973cb6b15 100644

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

Quoted 6 lines
grub-core/kern/mm.c +2 -0
@@ -410,7 +410,9 @@ 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 bound;
   int count = 0;
+  grub_size_t grow;

grub_size_t bound, grow;

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

Quoted 7 lines
grub-core/kern/mm.c +1 -0
   if (!grub_mm_base)
     goto fail;
@@ -418,10 +420,13 @@ grub_memalign (grub_size_t align, grub_size_t size)
   if (size > ~(grub_size_t) align)
     goto fail;
 
+  /* If largest free chunk in region >= bound, allocation can always success */

I am not sure I understand. Could you expand this comment a bit?

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

Quoted 1 line
grub-core/kern/mm.c +1 -0
+  bound = size + align;

I think you should use grub_add() here too...

And doing this you change the allocation behavior below. In original algorithm we allocate only size bytes below. I think this should not be changed.

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

Quoted 5 lines
grub-core/kern/mm.c +1 -1
   /* We currently assume at least a 32-bit grub_size_t,
      so limiting allocations to <adress space size> - 1MiB
      in name of sanity is beneficial. */
-  if ((size + align) > ~(grub_size_t) 0x100000)
+  if (bound > ~(grub_size_t) 0x100000)

s/bound/limit/?

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

Quoted 9 lines
grub-core/kern/mm.c +2 -0
     goto fail;
 
   align = (align >> GRUB_MM_ALIGN_LOG2);
@@ -443,11 +448,15 @@ grub_memalign (grub_size_t align, grub_size_t size)
   switch (count)
     {
     case 0:
+      /* Adjust heap growth to address the region management cost. */
+      if (grub_add (bound, GRUB_MM_MAX_COST, &grow))

I think this should happen before limit check above.

Quoted 27 lines
grub-core/kern/mm.c +4 -2
+	goto fail;
+
       /* Request additional pages, contiguous */
       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 +471,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;
         }
 
include/grub/mm_private.h +0 -0
index 96c2d816b..f212110e4 100644
@@ -95,6 +95,15 @@ typedef struct grub_mm_region
 }
 *grub_mm_region_t;

include/grub/mm_private.h typedef struct grub_mm_region · line 106

Quoted 10 lines
include/grub/mm_private.h +9 -0
 
+/*
+ * Set an upper bound of management cost of each region,
+ * with sizeof(struct grub_mm_region), sizeof(struct grub_mm_header) and
+ * any possible alignment or padding taken into account.
+ * The value should make `grub_malloc (size)` always success after a successful
+ * call to `grub_mm_init_region (addr, size + GRUB_MM_MAX_COST)`.
+ */
+#define GRUB_MM_MAX_COST 0x1000
+

I would define this variable in the following way even if the result is the same:

#define GRUB_MM_MGMT_OVERHEAD ALIGN_UP (ALIGN_UP (sizeof (*grub_mm_region_t), GRUB_MM_ALIGN) +
                                        10 * ALIGN_UP (sizeof (*grub_mm_header), GRUB_MM_ALIGN), 0x1000)

This way it will be more readable what is happening here, i.e. size of header of memory region plus size of 10, this is quite arbitrary figure, memory block headers aligned to page size (it would be nice to have a constant here but it would require more work to make it happen for all archs; so, let's use a number for this for time being). It would be nice to have relevant comment before GRUB_MM_MGMT_OVERHEAD definition too.

And probably it can be defined in grub-core/kern/mm.c instead of include/grub/mm_private.h.

Daniel

#3 lore raw

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

Hi,

Quoted 22 lines · Daniel Kiper
Quoted 3 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 cost into

s/cost/overhead/? If yes then please fix everything below too...

Quoted 1 line

account. Adding a memory area of size bytes will result in a heap

s/`/"/ Same below if needed...

Quoted 2 lines

region of less than size bytes truely avaliable. Thus, the new region might not adequate for current allocation request, confusing

s/might not/may not be/

Quoted 3 lines

out-of-memory handling code.

This patch introduces GRUB_MM_MAX_COST to address the region management

s/GRUB_MM_MAX_COST/GRUB_MM_MGMT_OVERHEAD/?

Quoted 9 lines
Quoted 2 lines

cost (e.g. metadata, alignment). The value of this new constant should make grub_malloc (size) always success after a successful call to

s/grub_malloc (size)/grub_malloc(size)/

Quoted 1 line

grub_mm_init_region (addr, size + GRUB_MM_MAX_COST).

s/grub_mm_init_region (addr, size + GRUB_MM_MAX_COST)/grub_mm_init_region(addr, size + GRUB_MM_MAX_COST)/

Fixed in v2.

Quoted 30 lines
Quoted 15 lines

The new region size is now set to size + GRUB_MM_MAX_COST, thus if grub_mm_add_region_fn() succeeded, current allocation request can always success.

---
  grub-core/kern/mm.c       | 15 ++++++++++++---
  include/grub/mm_private.h |  9 +++++++++
  2 files changed, 21 insertions(+), 3 deletions(-)
grub-core/kern/mm.c +0 -0
index ae2279133..973cb6b15 100644

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

Quoted 6 lines
grub-core/kern/mm.c +2 -0
@@ -410,7 +410,9 @@ 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 bound;
    int count = 0;
+  grub_size_t grow;

grub_size_t bound, grow;

Quoted 6 lines
grub-core/kern/mm.c +0 -0
    if (!grub_mm_base)
      goto fail;
@@ -418,10 +420,13 @@ grub_memalign (grub_size_t align, grub_size_t size)
    if (size > ~(grub_size_t) align)
      goto fail;
 
Quoted 4 lines

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

Quoted 1 line
grub-core/kern/mm.c +1 -0
+  /* If largest free chunk in region >= bound, allocation can always success */

I am not sure I understand. Could you expand this comment a bit?

Fixed. Please see v2 for new comment.

Quoted 3 lines

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

Quoted 1 line
grub-core/kern/mm.c +1 -0
+  bound = size + align;

I think you should use grub_add() here too...

The overflow is already checked in "if (size > ~(grub_size_t) align) goto fail;".

I can rewrote this check using grub_add() if necessary.

Quoted 5 lines

And doing this you change the allocation behavior below. In original algorithm we allocate only size bytes below. I think this should not be changed.

In fact, there is no change to algorithm. I just want to reuse the expression "size + align" (because it passes overflow check), so I give it the name "bound".

Quoted 8 lines

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

Quoted 5 lines
grub-core/kern/mm.c +1 -1
    /* We currently assume at least a 32-bit grub_size_t,
       so limiting allocations to <adress space size> - 1MiB
       in name of sanity is beneficial. */
-  if ((size + align) > ~(grub_size_t) 0x100000)
+  if (bound > ~(grub_size_t) 0x100000)

s/bound/limit/?

I think "bound" is more suitable. Because that value is a upper bound (an overestimate) of how much new memory we need.

Quoted 12 lines

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

Quoted 9 lines
grub-core/kern/mm.c +2 -0
      goto fail;
 
    align = (align >> GRUB_MM_ALIGN_LOG2);
@@ -443,11 +448,15 @@ grub_memalign (grub_size_t align, grub_size_t size)
    switch (count)
      {
      case 0:
+      /* Adjust heap growth to address the region management cost. */
+      if (grub_add (bound, GRUB_MM_MAX_COST, &grow))

I think this should happen before limit check above.

Fixed in v2.

Quoted 52 lines
Quoted 27 lines
grub-core/kern/mm.c +4 -2
+	goto fail;
+
        /* Request additional pages, contiguous */
        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 +471,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;
          }
 
include/grub/mm_private.h +0 -0
index 96c2d816b..f212110e4 100644
@@ -95,6 +95,15 @@ typedef struct grub_mm_region
  }
  *grub_mm_region_t;

include/grub/mm_private.h typedef struct grub_mm_region · line 106

Quoted 10 lines
include/grub/mm_private.h +9 -0
 
+/*
+ * Set an upper bound of management cost of each region,
+ * with sizeof(struct grub_mm_region), sizeof(struct grub_mm_header) and
+ * any possible alignment or padding taken into account.
+ * The value should make `grub_malloc (size)` always success after a successful
+ * call to `grub_mm_init_region (addr, size + GRUB_MM_MAX_COST)`.
+ */
+#define GRUB_MM_MAX_COST 0x1000
+

I would define this variable in the following way even if the result is the same:

#define GRUB_MM_MGMT_OVERHEAD ALIGN_UP (ALIGN_UP (sizeof (*grub_mm_region_t), GRUB_MM_ALIGN) +
                                         10 * ALIGN_UP (sizeof (*grub_mm_header), GRUB_MM_ALIGN), 0x1000)

This way it will be more readable what is happening here, i.e. size of header of memory region plus size of 10, this is quite arbitrary figure, memory block headers aligned to page size (it would be nice to have a constant here but it would require more work to make it happen for all archs; so, let's use a number for this for time being). It would be nice to have relevant comment before GRUB_MM_MGMT_OVERHEAD definition too.

Quoted 3 lines

And probably it can be defined in grub-core/kern/mm.c instead of include/grub/mm_private.h.

Please see v2 for new implementation.

Quoted 1 line

Daniel

Best Regards,
Zhang Boyang

#4 lore raw

v2 cover mm: Better handling of adding new regions

Hi,

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

For changes, please see my replys to Daniel.

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       | 38 +++++++++++++++++++++++++++++++++++---
 include/grub/mm_private.h |  3 +++
 2 files changed, 38 insertions(+), 3 deletions(-)
Signature · 1 line
2.30.2
#5 lore raw

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

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, the size passed to it doesn't take region management overhead into account. Adding a memory area of "size" bytes will 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 should make grub_malloc(size) always success after a successful call to grub_mm_init_region(addr, size + GRUB_MM_MGMT_OVERHEAD).

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

---
 grub-core/kern/mm.c | 37 ++++++++++++++++++++++++++++++++++---
 1 file changed, 34 insertions(+), 3 deletions(-)
grub-core/kern/mm.c +34 -3
index ae2279133..20bb54dde 100644
@@ -232,6 +232,7 @@ grub_mm_init_region (void *addr, grub_size_t size)
 		addr, size);
   /* Allocate a region from the head.  */
   r = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) addr, GRUB_MM_ALIGN);
+#define GRUB_MM_MGMT_OVERHEAD_HEAD_PADDING	(GRUB_MM_ALIGN - 1)
 
   /* If this region is too small, ignore it.  */
   if (size < GRUB_MM_ALIGN + (char *) r - (char *) addr + sizeof (*r))
@@ -239,15 +240,18 @@ grub_mm_init_region (void *addr, grub_size_t size)
 
   size -= (char *) r - (char *) addr + sizeof (*r);
 
+#define GRUB_MM_MGMT_OVERHEAD_MM_HEADER		sizeof (struct grub_mm_header)
   h = (grub_mm_header_t) (r + 1);
   h->next = h;
   h->magic = GRUB_MM_FREE_MAGIC;
   h->size = (size >> GRUB_MM_ALIGN_LOG2);
 
+#define GRUB_MM_MGMT_OVERHEAD_REGION_HEADER	sizeof (struct grub_mm_region)
   r->first = h;
   r->pre_size = (grub_addr_t) r - (grub_addr_t) addr;
   r->size = (h->size << GRUB_MM_ALIGN_LOG2);
   r->post_size = size - r->size;
+#define GRUB_MM_MGMT_OVERHEAD_TAIL_PADDING	(GRUB_MM_ALIGN - 1)
 
   /* Find where to insert this region. Put a smaller one before bigger ones,
      to prevent fragmentation.  */
@@ -259,6 +263,17 @@ grub_mm_init_region (void *addr, grub_size_t size)
   r->next = q;
 }
 
+/*
+ * GRUB_MM_MGMT_OVERHEAD is an upper bound of management overhead of
+ * each region, with any possible padding taken into account.
+ * The value should make grub_malloc(size) always success after a successful
+ * call to grub_mm_init_region(addr, size + GRUB_MM_MGMT_OVERHEAD).
+ */
+#define GRUB_MM_MGMT_OVERHEAD (GRUB_MM_MGMT_OVERHEAD_HEAD_PADDING + \
+			       GRUB_MM_MGMT_OVERHEAD_REGION_HEADER + \
+			       GRUB_MM_MGMT_OVERHEAD_MM_HEADER + \
+			       GRUB_MM_MGMT_OVERHEAD_TAIL_PADDING)
+
 /* Allocate the number of units N with the alignment ALIGN from the ring
  * buffer given in *FIRST.  ALIGN must be a power of two. Both N and
  * ALIGN are in units of GRUB_MM_ALIGN.  Return a non-NULL if successful,
@@ -410,6 +425,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 bound, grow;
   int count = 0;
 
   if (!grub_mm_base)
@@ -418,10 +434,25 @@ grub_memalign (grub_size_t align, grub_size_t size)
   if (size > ~(grub_size_t) align)
     goto fail;
 
+  /*
+   * If we have a free chunk whose real size is bigger than or equal to
+   * "size + align", the alignment requirement can always be satisfied.
+   * However, this is an overestimate. The allocation may still success even if
+   * all free chunks are smaller than "size + align".
+   */
+  bound = size + align;
+
   /* We currently assume at least a 32-bit grub_size_t,
      so limiting allocations to <adress space size> - 1MiB
      in name of sanity is beneficial. */
-  if ((size + align) > ~(grub_size_t) 0x100000)
+  if (bound > ~(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 (bound, GRUB_MM_MGMT_OVERHEAD, &grow))
     goto fail;
 
   align = (align >> GRUB_MM_ALIGN_LOG2);
@@ -447,7 +478,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 +493,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
#6 lore raw

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

Please do not send your new sets of patches as a reply to previous discussions. If you do that then it is more difficult to find them in threaded views.

Quoted 40 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 will 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 should make grub_malloc(size) always success after a successful call to grub_mm_init_region(addr, size + GRUB_MM_MGMT_OVERHEAD).

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

---
 grub-core/kern/mm.c | 37 ++++++++++++++++++++++++++++++++++---
 1 file changed, 34 insertions(+), 3 deletions(-)
grub-core/kern/mm.c +2 -0
index ae2279133..20bb54dde 100644
@@ -232,6 +232,7 @@ grub_mm_init_region (void *addr, grub_size_t size)
 		addr, size);
   /* Allocate a region from the head.  */
   r = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) addr, GRUB_MM_ALIGN);
+#define GRUB_MM_MGMT_OVERHEAD_HEAD_PADDING	(GRUB_MM_ALIGN - 1)
 
   /* If this region is too small, ignore it.  */
   if (size < GRUB_MM_ALIGN + (char *) r - (char *) addr + sizeof (*r))
@@ -239,15 +240,18 @@ grub_mm_init_region (void *addr, grub_size_t size)
 
   size -= (char *) r - (char *) addr + sizeof (*r);
 
+#define GRUB_MM_MGMT_OVERHEAD_MM_HEADER		sizeof (struct grub_mm_header)
   h = (grub_mm_header_t) (r + 1);

grub-core/kern/mm.c grub_mm_init_region (void *addr, grub_size_t size) · line 254

Quoted 10 lines
grub-core/kern/mm.c +2 -0
   h->next = h;
   h->magic = GRUB_MM_FREE_MAGIC;
   h->size = (size >> GRUB_MM_ALIGN_LOG2);
 
+#define GRUB_MM_MGMT_OVERHEAD_REGION_HEADER	sizeof (struct grub_mm_region)
   r->first = h;
   r->pre_size = (grub_addr_t) r - (grub_addr_t) addr;
   r->size = (h->size << GRUB_MM_ALIGN_LOG2);
   r->post_size = size - r->size;
+#define GRUB_MM_MGMT_OVERHEAD_TAIL_PADDING	(GRUB_MM_ALIGN - 1)

Please do not sprinkle constants definitions over the code. This does not help in reading. Additionally, I think these extra constants make code less readable.

Quoted 6 lines
grub-core/kern/mm.c +0 -0
   /* Find where to insert this region. Put a smaller one before bigger ones,
      to prevent fragmentation.  */
@@ -259,6 +263,17 @@ grub_mm_init_region (void *addr, grub_size_t size)
   r->next = q;
 }
 

grub-core/kern/mm.c grub_mm_init_region (void *addr, grub_size_t size) · line 275

Quoted 10 lines
grub-core/kern/mm.c +10 -0
+/*
+ * GRUB_MM_MGMT_OVERHEAD is an upper bound of management overhead of
+ * each region, with any possible padding taken into account.
+ * The value should make grub_malloc(size) always success after a successful
+ * call to grub_mm_init_region(addr, size + GRUB_MM_MGMT_OVERHEAD).
+ */
+#define GRUB_MM_MGMT_OVERHEAD (GRUB_MM_MGMT_OVERHEAD_HEAD_PADDING + \
+			       GRUB_MM_MGMT_OVERHEAD_REGION_HEADER + \
+			       GRUB_MM_MGMT_OVERHEAD_MM_HEADER + \
+			       GRUB_MM_MGMT_OVERHEAD_TAIL_PADDING)

I do not think this math is correct. Please use mine from previous reply. When it is wrong please say where exactly it is wrong. OK, I can agree using "sizeof (struct grub_mm_region)" is more readable than "sizeof (*grub_mm_region_t)". Anything else?

And I would define GRUB_MM_MGMT_OVERHEAD constant at the beginning of the grub-core/kern/mm.c file before grub_mm_base definition.

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

Quoted 8 lines
grub-core/kern/mm.c +1 -0
 /* Allocate the number of units N with the alignment ALIGN from the ring
  * buffer given in *FIRST.  ALIGN must be a power of two. Both N and
  * ALIGN are in units of GRUB_MM_ALIGN.  Return a non-NULL if successful,
@@ -410,6 +425,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 bound, grow;

I think you do not need bound at all. Just use grow...

Quoted 40 lines
grub-core/kern/mm.c +17 -2
   int count = 0;
 
   if (!grub_mm_base)
@@ -418,10 +434,25 @@ grub_memalign (grub_size_t align, grub_size_t size)
   if (size > ~(grub_size_t) align)
     goto fail;
 
+  /*
+   * If we have a free chunk whose real size is bigger than or equal to
+   * "size + align", the alignment requirement can always be satisfied.
+   * However, this is an overestimate. The allocation may still success even if
+   * all free chunks are smaller than "size + align".
+   */
+  bound = size + align;
+
   /* We currently assume at least a 32-bit grub_size_t,
      so limiting allocations to <adress space size> - 1MiB
      in name of sanity is beneficial. */
-  if ((size + align) > ~(grub_size_t) 0x100000)
+  if (bound > ~(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 (bound, GRUB_MM_MGMT_OVERHEAD, &grow))
     goto fail;
 
   align = (align >> GRUB_MM_ALIGN_LOG2);
@@ -447,7 +478,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 498

Quoted 7 lines
grub-core/kern/mm.c +1 -1
@@ -462,7 +493,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

#7 lore raw

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

Hi,

Quoted 4 lines · Daniel Kiper

Please do not send your new sets of patches as a reply to previous discussions. If you do that then it is more difficult to find them in threaded views.

OK. I will not do that in future.

Quoted 45 lines · Zhang Boyang
Quoted 44 lines

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 will 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 should make grub_malloc(size) always success after a successful call to grub_mm_init_region(addr, size + GRUB_MM_MGMT_OVERHEAD).

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

---
  grub-core/kern/mm.c | 37 ++++++++++++++++++++++++++++++++++---
  1 file changed, 34 insertions(+), 3 deletions(-)
grub-core/kern/mm.c +2 -0
index ae2279133..20bb54dde 100644
@@ -232,6 +232,7 @@ grub_mm_init_region (void *addr, grub_size_t size)
  		addr, size);
    /* Allocate a region from the head.  */
    r = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) addr, GRUB_MM_ALIGN);
+#define GRUB_MM_MGMT_OVERHEAD_HEAD_PADDING	(GRUB_MM_ALIGN - 1)
 
    /* If this region is too small, ignore it.  */
    if (size < GRUB_MM_ALIGN + (char *) r - (char *) addr + sizeof (*r))
@@ -239,15 +240,18 @@ grub_mm_init_region (void *addr, grub_size_t size)
 
    size -= (char *) r - (char *) addr + sizeof (*r);
 
+#define GRUB_MM_MGMT_OVERHEAD_MM_HEADER		sizeof (struct grub_mm_header)
    h = (grub_mm_header_t) (r + 1);
    h->next = h;
    h->magic = GRUB_MM_FREE_MAGIC;
    h->size = (size >> GRUB_MM_ALIGN_LOG2);
 
Quoted 11 lines

grub-core/kern/mm.c grub_mm_init_region (void *addr, grub_size_t size) · line 254

Quoted 6 lines
grub-core/kern/mm.c +2 -0
+#define GRUB_MM_MGMT_OVERHEAD_REGION_HEADER	sizeof (struct grub_mm_region)
    r->first = h;
    r->pre_size = (grub_addr_t) r - (grub_addr_t) addr;
    r->size = (h->size << GRUB_MM_ALIGN_LOG2);
    r->post_size = size - r->size;
+#define GRUB_MM_MGMT_OVERHEAD_TAIL_PADDING	(GRUB_MM_ALIGN - 1)

Please do not sprinkle constants definitions over the code. This does not help in reading. Additionally, I think these extra constants make code less readable.

My motivation is to force who want to modify this code notice there is a constant tightly connected to region layout. So he will not forget update the constant if he changes region layout.

I agree this will reduce readability. I will replace this with a comment in next version.

Quoted 6 lines
Quoted 6 lines
grub-core/kern/mm.c +0 -0
    /* Find where to insert this region. Put a smaller one before bigger ones,
       to prevent fragmentation.  */
@@ -259,6 +263,17 @@ grub_mm_init_region (void *addr, grub_size_t size)
    r->next = q;
  }
 
Quoted 16 lines

grub-core/kern/mm.c grub_mm_init_region (void *addr, grub_size_t size) · line 275

Quoted 10 lines
grub-core/kern/mm.c +10 -0
+/*
+ * GRUB_MM_MGMT_OVERHEAD is an upper bound of management overhead of
+ * each region, with any possible padding taken into account.
+ * The value should make grub_malloc(size) always success after a successful
+ * call to grub_mm_init_region(addr, size + GRUB_MM_MGMT_OVERHEAD).
+ */
+#define GRUB_MM_MGMT_OVERHEAD (GRUB_MM_MGMT_OVERHEAD_HEAD_PADDING + \
+			       GRUB_MM_MGMT_OVERHEAD_REGION_HEADER + \
+			       GRUB_MM_MGMT_OVERHEAD_MM_HEADER + \
+			       GRUB_MM_MGMT_OVERHEAD_TAIL_PADDING)

I do not think this math is correct. Please use mine from previous reply. When it is wrong please say where exactly it is wrong. OK, I can agree using "sizeof (struct grub_mm_region)" is more readable than "sizeof (*grub_mm_region_t)". Anything else?

I have to use "sizeof (struct grub_mm_region)", because "sizeof (*grub_mm_region_t)" won't compile.

After rethinking, I think the formula need to be refactored. The purpose of this constant, same as what's its comment says, is to make grub_malloc(size) always success after a successful call to grub_mm_init_region(addr, size + GRUB_MM_MGMT_OVERHEAD).

If new region can be merged into an existing region, all of its area can be used for forming a free block (grub_mm_header and user usable bytes). This is the most simple case. Even in this case, GRUB_MM_MGMT_OVERHEAD should be "sizeof(struct grub_mm_header) + (GRUB_MM_ALIGN-1)". This is because "size" may not be integral multiple of GRUB_MM_ALIGN. As GRUB_MM_MGMT_OVERHEAD is a constant designed to be added to "size", we can't use macros like ALIGN_UP. Therefore, we must make GRUB_MM_MGMT_OVERHEAD sufficient large, so "x + GRUB_MM_MGMT_OVERHEAD >= sizeof(struct grub_mm_header) + ALIGN_UP(x, GRUB_MM_ALIGN)" is always true for any "x". The smallest GRUB_MM_MGMT_OVERHEAD would be "sizeof(struct grub_mm_header) + (GRUB_MM_ALIGN-1)".

The complex case is, new region is independent and can't be merged into other regions. In this case, a grub_mm_header struct must be formed along with a free block. It looks like:

+---------+--------------------+--------------------+------------------+
| padding |   grub_mm_region   |   grub_mm_header   |   usable bytes   |
+---------+--------------------+--------------------+------------------+

If we ignore alignment/padding, using the method above, GRUB_MM_MGMT_OVERHEAD would be "sizeof (struct grub_mm_region) + sizeof(struct grub_mm_header) + (GRUB_MM_ALIGN-1)". Unfortunately, the grub_mm_region must be aligned, and the algorithm used by grub_mm_init_region(some_addr) is new_addr=ALIGN_UP(some_addr,GRUB_MM_ALIGN). So we must make GRUB_MM_MGMT_OVERHEAD sufficient large, so "x + GRUB_MM_MGMT_OVERHEAD >= (ALIGN_UP(y,GRUB_MM_ALIGN)-y) + sizeof (struct grub_mm_region) + sizeof(struct grub_mm_header) + (GRUB_MM_ALIGN-1)" is always true for any "x" and any "y". Thus, GRUB_MM_MGMT_OVERHEAD should be "(GRUB_MM_ALIGN-1) + sizeof (struct grub_mm_region) + sizeof(struct grub_mm_header) + (GRUB_MM_ALIGN-1)" (by the way, I miscalculated this component in V2, GRUB_MM_MGMT_OVERHEAD_TAIL_PADDING is not necessary)

Finally, we should take max of two GRUB_MM_MGMT_OVERHEAD from 2 cases. So the final value of GRUB_MM_MGMT_OVERHEAD shoule be:

(GRUB_MM_ALIGN-1) + sizeof (struct grub_mm_region) + sizeof(struct grub_mm_header) + (GRUB_MM_ALIGN-1)

Quoted 9 lines · Daniel Kiper

I would define this variable in the following way even if the result is the same:

#define GRUB_MM_MGMT_OVERHEAD ALIGN_UP (ALIGN_UP (sizeof (*grub_mm_region_t), GRUB_MM_ALIGN) +
                                         10 * ALIGN_UP (sizeof (*c), GRUB_MM_ALIGN), 0x1000)

This way it will be more readable what is happening here, i.e. size of header of memory region plus size of 10, this is quite arbitrary figure,

I didn't understand this formula and it is probably not what GRUB_MM_MGMT_OVERHEAD designed to be. By the way, grub_mm_header and grub_mm_region already aligned to GRUB_MM_ALIGN, so there is no need to ALIGN_UP them.

Quoted 5 lines

memory block headers aligned to page size (it would be nice to have a constant here but it would require more work to make it happen for all archs; so, let's use a number for this for time being). It would be nice to have relevant comment before GRUB_MM_MGMT_OVERHEAD definition too.

I don't think it is necessary to align some value to page size. If I understand correctly, grub_mm_init_region() make no assumptions on alignment of its arguments.

Quoted 14 lines

And I would define GRUB_MM_MGMT_OVERHEAD constant at the beginning of the grub-core/kern/mm.c file before grub_mm_base definition.

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

Quoted 8 lines
grub-core/kern/mm.c +1 -0
  /* Allocate the number of units N with the alignment ALIGN from the ring
   * buffer given in *FIRST.  ALIGN must be a power of two. Both N and
   * ALIGN are in units of GRUB_MM_ALIGN.  Return a non-NULL if successful,
@@ -410,6 +425,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 bound, grow;

I think you do not need bound at all. Just use grow...

OK, will fix in next version.

Best Regards,
Zhang Boyang

Quoted 49 lines
Quoted 40 lines
grub-core/kern/mm.c +17 -2
    int count = 0;
 
    if (!grub_mm_base)
@@ -418,10 +434,25 @@ grub_memalign (grub_size_t align, grub_size_t size)
    if (size > ~(grub_size_t) align)
      goto fail;
 
+  /*
+   * If we have a free chunk whose real size is bigger than or equal to
+   * "size + align", the alignment requirement can always be satisfied.
+   * However, this is an overestimate. The allocation may still success even if
+   * all free chunks are smaller than "size + align".
+   */
+  bound = size + align;
+
    /* We currently assume at least a 32-bit grub_size_t,
       so limiting allocations to <adress space size> - 1MiB
       in name of sanity is beneficial. */
-  if ((size + align) > ~(grub_size_t) 0x100000)
+  if (bound > ~(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 (bound, GRUB_MM_MGMT_OVERHEAD, &grow))
      goto fail;
 
    align = (align >> GRUB_MM_ALIGN_LOG2);
@@ -447,7 +478,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 498

Quoted 7 lines
grub-core/kern/mm.c +1 -1
@@ -462,7 +493,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

#8 lore raw

PATCH v2 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 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       | 3 ++-
 include/grub/mm_private.h | 3 +++
 2 files changed, 5 insertions(+), 1 deletion(-)
grub-core/kern/mm.c +2 -1
index 20bb54dde..f023247bb 100644
@@ -449,11 +449,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 (bound, 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)
include/grub/mm_private.h +3 -0
index 96c2d816b..6e4256555 100644
@@ -95,6 +95,9 @@ typedef struct grub_mm_region
 }
 *grub_mm_region_t;
 
+/* Minimal heap growth granularity when existing heap space is exhausted. */
+#define GRUB_MM_HEAP_GROW_EXTRA 0x100000
+
 #ifndef GRUB_MACHINE_EMU
 extern grub_mm_region_t EXPORT_VAR (grub_mm_base);
 #endif
Signature · 1 line
2.30.2
#9 lore raw

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

Quoted 46 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 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       | 3 ++-
 include/grub/mm_private.h | 3 +++
 2 files changed, 5 insertions(+), 1 deletion(-)
grub-core/kern/mm.c +2 -1
index 20bb54dde..f023247bb 100644
@@ -449,11 +449,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 (bound, 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)
include/grub/mm_private.h +0 -0
index 96c2d816b..6e4256555 100644

include/grub/mm_private.h typedef struct grub_mm_region · line 99

Quoted 5 lines
include/grub/mm_private.h +2 -0
@@ -95,6 +95,9 @@ typedef struct grub_mm_region
 }
 *grub_mm_region_t;
 
+/* Minimal heap growth granularity when existing heap space is exhausted. */
+#define GRUB_MM_HEAP_GROW_EXTRA 0x100000

Please move this constant to the beginning of the grub-core/kern/mm.c file.

Otherwise LGTM...

Daniel

#10 lore raw

PATCH 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, which may be too slow if those requests are small (e.g. about 4KB).

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

The value of GRUB_MM_HEAP_GROW 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       | 3 +++
 include/grub/mm_private.h | 3 +++
 2 files changed, 6 insertions(+)
grub-core/kern/mm.c +3 -0
index 973cb6b15..83c618c5b 100644
@@ -452,6 +452,9 @@ grub_memalign (grub_size_t align, grub_size_t size)
       if (grub_add (bound, GRUB_MM_MAX_COST, &grow))
 	goto fail;
 
+      /* Calculate optimal size of heap growth. */
+      grow = grub_max (grow, GRUB_MM_HEAP_GROW);
+
       /* Request additional pages, contiguous */
       count++;
 
include/grub/mm_private.h +3 -0
index f212110e4..268fbf094 100644
@@ -104,6 +104,9 @@ typedef struct grub_mm_region
  */
 #define GRUB_MM_MAX_COST 0x1000
 
+/* Minimal heap growth granularity when existing heap space is exhausted. */
+#define GRUB_MM_HEAP_GROW 0x100000
+
 #ifndef GRUB_MACHINE_EMU
 extern grub_mm_region_t EXPORT_VAR (grub_mm_base);
 #endif
Signature · 1 line
2.30.2
#11 lore raw

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

Quoted 6 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, which may be too slow if those requests are small (e.g. about 4KB).

s/, which may ... are small (e.g. about 4KB)./. This can be very slow./

Quoted 1 line

This patch introduces GRUB_MM_HEAP_GROW, the minimal heap growth

s/GRUB_MM_HEAP_GROW/GRUB_MM_HEAP_GROW_EXTRA/

Quoted 20 lines

granularity. The new region size is now set to the bigger one of original value and GRUB_MM_HEAP_GROW. Thus, it will result in some memory space preallocated if current allocations request is small.

The value of GRUB_MM_HEAP_GROW 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       | 3 +++
 include/grub/mm_private.h | 3 +++
 2 files changed, 6 insertions(+)
grub-core/kern/mm.c +0 -0
index 973cb6b15..83c618c5b 100644

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

Quoted 5 lines
grub-core/kern/mm.c +2 -0
@@ -452,6 +452,9 @@ grub_memalign (grub_size_t align, grub_size_t size)
       if (grub_add (bound, GRUB_MM_MAX_COST, &grow))
 	goto fail;
 
+      /* Calculate optimal size of heap growth. */
+      grow = grub_max (grow, GRUB_MM_HEAP_GROW);

This should be "if (grub_add (grow, GRUB_MM_HEAP_GROW, &grow))..."

And it should happen before first grub_mm_add_region_fn() call.

include/grub/mm_private.h typedef struct grub_mm_region · line 108

Quoted 13 lines
grub-core/kern/mm.c +0 -0
       /* Request additional pages, contiguous */
       count++;
 
include/grub/mm_private.h +2 -0
index f212110e4..268fbf094 100644
@@ -104,6 +104,9 @@ typedef struct grub_mm_region
  */
 #define GRUB_MM_MAX_COST 0x1000
 
+/* Minimal heap growth granularity when existing heap space is exhausted. */
+#define GRUB_MM_HEAP_GROW 0x100000

Is 1 MiB enough? I think yes but prefer to double check...

Daniel