Skip to contentSkip to conversation

mm: Better handling of adding new regions

PATCH Superseded Zhang Boyang 2 participants 5 messages Oct 15, 2022 – Nov 7, 2022

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover mm: Better handling of adding new regions

Hi,

Changes in V3 -> V4:

* [PATCH v4 1/2] mm: Try invalidate disk caches last when out of memory Added "Reviewed-by" tags.

* [PATCH v4 2/2] mm: Better handling of adding new regions Made this patch standalone.
Moved (and renamed back) new constants to "mm_private.h" from "mm.h". Documented more details in comments.

* The allocation type patch will be posted later.

Best Regards,
Zhang Boyang

----

Changes in V2 -> V3:

1. Splited into two patches. One deal with the disk cache problem. The other is focused on improvement.
2. Add types to regions. So small and large allocations are effectively seperated into different heaps. This facility can also seperate module segments and plain data chunks.
3. Region management costs are now denoted by GRUB_MM_MAX_COST, seperated from GRUB_MM_GROW_SMALL ( =GRUB_MM_HEAP_GROW in previous patch).

Some notes for the size of heap growth:

In order to guarantee the success of allocation when heap exhausted, the new region size should be at least (size + align + GRUB_MM_MAX_COST). For small chunks, it's adjusted to (GRUB_MM_GROW_SMALL + GRUB_MM_MAX_COST) to improve performance.

#1 lore raw

PATCH v4 1/2 mm: Try invalidate disk caches last when out of memory

Previously, every heap grow will cause all disk caches invalidated, which decreases performance severely. This patch moves disk cache invalidation code to the last of memory squeezing measures, so disk caches are released only when no other ways to get free memory.

---
 grub-core/kern/mm.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
grub-core/kern/mm.c +7 -7
index 75f6eacbe..ae2279133 100644
@@ -443,12 +443,6 @@ grub_memalign (grub_size_t align, grub_size_t size)
   switch (count)
     {
     case 0:
-      /* Invalidate disk caches.  */
-      grub_disk_cache_invalidate_all ();
-      count++;
-      goto again;
-
-    case 1:
       /* Request additional pages, contiguous */
       count++;
 
@@ -458,7 +452,7 @@ grub_memalign (grub_size_t align, grub_size_t size)
 
       /* fallthrough  */
 
-    case 2:
+    case 1:
       /* Request additional pages, anything at all */
       count++;
 
@@ -474,6 +468,12 @@ grub_memalign (grub_size_t align, grub_size_t size)
 
       /* fallthrough */
 
+    case 2:
+      /* Invalidate disk caches.  */
+      grub_disk_cache_invalidate_all ();
+      count++;
+      goto again;
+
     default:
       break;
     }
Signature · 1 line
2.30.2
#2 lore raw

PATCH v4 2/2 mm: Better handling of adding new regions

In reply to Zhang Boyang

Previously, there are two problems of the handling of adding new regions. First, it doesn't take region management cost into account. Adding a memory area of x bytes will result in a heap region of less than x bytes avaliable. Thus, the new region might not adequate for current allocation request. Second, it doesn't pre-allocate a big region for small allocations, so it might be slow for many small allocations.

This patch introduces GRUB_MM_MAX_COST to address the cost of region management. The value of this constant should make grub_mm_init_region (addr, size + GRUB_MM_MAX_COST) initialize a region of at least size free bytes available. The size of heap growth is now adjusted according this value before calling grub_mm_add_region_fn ().

This patch also introduces GRUB_MM_HEAP_GROW to address the minimal heap growth granularity. When a small allocation encounters heap space exhaustion, the size of heap growth is now expended to this value, and a new region of bigger size will be added. Thus subsequent allocations can use the remaining space of that new region.

---
 grub-core/kern/mm.c       | 18 +++++++++++++++---
 include/grub/mm_private.h | 12 ++++++++++++
 2 files changed, 27 insertions(+), 3 deletions(-)
grub-core/kern/mm.c +15 -3
index ae2279133..fe630f061 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 guarantee;
   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;
 
+  /* Allocation can always success if max continuous free chunk >= guarantee */
+  guarantee = 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 (guarantee > ~(grub_size_t) 0x100000)
     goto fail;
 
   align = (align >> GRUB_MM_ALIGN_LOG2);
@@ -443,11 +448,18 @@ grub_memalign (grub_size_t align, grub_size_t size)
   switch (count)
     {
     case 0:
+      /* Calculate optimal size of heap growth. */
+      grow = grub_max(guarantee, GRUB_MM_HEAP_GROW);
+
+      /* Adjust heap growth to address the cost of region management. */
+      if (grub_add(grow, 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 +474,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 +12 -0
index 96c2d816b..a01ba2507 100644
@@ -95,6 +95,18 @@ 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_mm_init_region (addr, size + GRUB_MM_MAX_COST)`
+ * initialize a region of at least `size` free bytes available.
+ */
+#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
#3 lore raw
Quoted 1 line · Zhang Boyang

Previously, there are two problems of the handling of adding new

Please drop "Previously". It is not needed...

Quoted 6 lines

regions. First, it doesn't take region management cost into account. Adding a memory area of x bytes will result in a heap region of less than x bytes avaliable. Thus, the new region might not adequate for current allocation request. Second, it doesn't pre-allocate a big region for small allocations, so it might be slow for many small allocations.

Quoted 6 lines

This patch introduces GRUB_MM_MAX_COST to address the cost of region management. The value of this constant should make grub_mm_init_region (addr, size + GRUB_MM_MAX_COST) initialize a region of at least size free bytes available. The size of heap growth is now adjusted according this value before calling grub_mm_add_region_fn ().

This should be separate patch...

Quoted 5 lines

This patch also introduces GRUB_MM_HEAP_GROW to address the minimal heap growth granularity. When a small allocation encounters heap space exhaustion, the size of heap growth is now expended to this value, and a new region of bigger size will be added. Thus subsequent allocations can use the remaining space of that new region.

... and this should be the second one, to be precise third patch in the series...

Quoted 36 lines
---
 grub-core/kern/mm.c       | 18 +++++++++++++++---
 include/grub/mm_private.h | 12 ++++++++++++
 2 files changed, 27 insertions(+), 3 deletions(-)
grub-core/kern/mm.c +6 -1
index ae2279133..fe630f061 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 guarantee;
   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;
 
+  /* Allocation can always success if max continuous free chunk >= guarantee */
+  guarantee = 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 (guarantee > ~(grub_size_t) 0x100000)
     goto fail;
 
   align = (align >> GRUB_MM_ALIGN_LOG2);

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

Quoted 5 lines
grub-core/kern/mm.c +2 -0
@@ -443,11 +448,18 @@ grub_memalign (grub_size_t align, grub_size_t size)
   switch (count)
     {
     case 0:
+      /* Calculate optimal size of heap growth. */
+      grow = grub_max(guarantee, GRUB_MM_HEAP_GROW);

Missing space before "(".

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

Quoted 3 lines
grub-core/kern/mm.c +3 -0
+
+      /* Adjust heap growth to address the cost of region management. */
+      if (grub_add(grow, GRUB_MM_MAX_COST, &grow))

Ditto...

Quoted 25 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 +474,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..a01ba2507 100644

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

Quoted 7 lines
include/grub/mm_private.h +4 -0
@@ -95,6 +95,18 @@ 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.

OK...

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

Quoted 4 lines
include/grub/mm_private.h +4 -0
+ * The value should make `grub_mm_init_region (addr, size + GRUB_MM_MAX_COST)`
+ * initialize a region of at least `size` free bytes available.
+ */
+#define GRUB_MM_MAX_COST 0x1000

... but why do not you use sizeof() here then?

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

Quoted 3 lines
include/grub/mm_private.h +3 -0
+
+/* Minimal heap growth granularity, when existing heap space is exhausted. */
+#define GRUB_MM_HEAP_GROW 0x100000

It would be nice if you shortly explain in the comment before why you come up with this value.

Daniel

#4 lore raw

Hi,

Quoted 20 lines · Daniel Kiper
Quoted 1 line · Zhang Boyang

Previously, there are two problems of the handling of adding new

Please drop "Previously". It is not needed...

Quoted 6 lines

regions. First, it doesn't take region management cost into account. Adding a memory area of x bytes will result in a heap region of less than x bytes avaliable. Thus, the new region might not adequate for current allocation request. Second, it doesn't pre-allocate a big region for small allocations, so it might be slow for many small allocations.

Quoted 6 lines

This patch introduces GRUB_MM_MAX_COST to address the cost of region management. The value of this constant should make grub_mm_init_region (addr, size + GRUB_MM_MAX_COST) initialize a region of at least size free bytes available. The size of heap growth is now adjusted according this value before calling grub_mm_add_region_fn ().

This should be separate patch...

Quoted 8 lines
Quoted 5 lines

This patch also introduces GRUB_MM_HEAP_GROW to address the minimal heap growth granularity. When a small allocation encounters heap space exhaustion, the size of heap growth is now expended to this value, and a new region of bigger size will be added. Thus subsequent allocations can use the remaining space of that new region.

... and this should be the second one, to be precise third patch in the series...

Will be fixed in next version.

Quoted 85 lines
Quoted 36 lines
---
  grub-core/kern/mm.c       | 18 +++++++++++++++---
  include/grub/mm_private.h | 12 ++++++++++++
  2 files changed, 27 insertions(+), 3 deletions(-)
grub-core/kern/mm.c +6 -1
index ae2279133..fe630f061 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 guarantee;
    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;
 
+  /* Allocation can always success if max continuous free chunk >= guarantee */
+  guarantee = 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 (guarantee > ~(grub_size_t) 0x100000)
      goto fail;
 
    align = (align >> GRUB_MM_ALIGN_LOG2);

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

Quoted 5 lines
grub-core/kern/mm.c +2 -0
@@ -443,11 +448,18 @@ grub_memalign (grub_size_t align, grub_size_t size)
    switch (count)
      {
      case 0:
+      /* Calculate optimal size of heap growth. */
+      grow = grub_max(guarantee, GRUB_MM_HEAP_GROW);

Missing space before "(".

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

Quoted 3 lines
grub-core/kern/mm.c +3 -0
+
+      /* Adjust heap growth to address the cost of region management. */
+      if (grub_add(grow, GRUB_MM_MAX_COST, &grow))

Ditto...

Quoted 25 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 +474,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..a01ba2507 100644

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

Quoted 7 lines
include/grub/mm_private.h +4 -0
@@ -95,6 +95,18 @@ 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.

OK...

Quoted 6 lines

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

Quoted 4 lines
include/grub/mm_private.h +4 -0
+ * The value should make `grub_mm_init_region (addr, size + GRUB_MM_MAX_COST)`
+ * initialize a region of at least `size` free bytes available.
+ */
+#define GRUB_MM_MAX_COST 0x1000

... but why do not you use sizeof() here then?

There are complex alignments which are hard to predict (although it's indeed possible). These alignments are determined by not only "size" but also "addr". Calculating a tight upper bound value will be complex and error prone. So I think it's better to give a sufficiently large value, so complex calculations are avoided.

Quoted 8 lines

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

Quoted 4 lines
include/grub/mm_private.h +3 -0
+
+/* Minimal heap growth granularity, when existing heap space is exhausted. */
+#define GRUB_MM_HEAP_GROW 0x100000

It would be nice if you shortly explain in the comment before why you come up with this value.

It was chosen almost arbitrarily. However, if this value is too small, the cost of small memory allocations will be expensive (more calls to grub_mm_add_region_fn(), which might be slow). If this value is too large, a lot of memory will be wasted and it might cause out-of-memory on machines with small amount of RAM.

Quoted 1 line

Daniel

Best Regards,
Zhang Boyang