5c83f50
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
5c83f50
From: Zhang Boyang <zhangboyang.id@gmail.com>
5c83f50
Date: Sun, 29 Jan 2023 19:49:33 +0800
5c83f50
Subject: [PATCH] mm: Avoid complex heap growth math in hot path
5c83f50
5c83f50
We do a lot of math about heap growth in hot path of grub_memalign().
5c83f50
However, the result is only used if out of memory is encountered, which
5c83f50
is seldom.
5c83f50
5c83f50
This patch moves these calculations away from hot path. These
5c83f50
calculations are now only done if out of memory is encountered. This
5c83f50
change can also help compiler to optimize integer overflow checks away.
5c83f50
5c83f50
Signed-off-by: Zhang Boyang <zhangboyang.id@gmail.com>
5c83f50
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
5c83f50
(cherry picked from commit 65bc45963014773e2062ccc63ff34a089d2e352e)
5c83f50
---
5c83f50
 grub-core/kern/mm.c | 34 ++++++++++++++++++++--------------
5c83f50
 1 file changed, 20 insertions(+), 14 deletions(-)
5c83f50
5c83f50
diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c
5c83f50
index cc8a4703bc..630d7be0e2 100644
5c83f50
--- a/grub-core/kern/mm.c
5c83f50
+++ b/grub-core/kern/mm.c
5c83f50
@@ -467,20 +467,7 @@ grub_memalign (grub_size_t align, grub_size_t size)
5c83f50
   if (size > ~(grub_size_t) align)
5c83f50
     goto fail;
5c83f50
 
5c83f50
-  /*
5c83f50
-   * Pre-calculate the necessary size of heap growth (if applicable),
5c83f50
-   * with region management overhead taken into account.
5c83f50
-   */
5c83f50
-  if (grub_add (size + align, GRUB_MM_MGMT_OVERHEAD, &grow))
5c83f50
-    goto fail;
5c83f50
-
5c83f50
-  /* Preallocate some extra space if heap growth is small. */
5c83f50
-  grow = grub_max (grow, GRUB_MM_HEAP_GROW_EXTRA);
5c83f50
-
5c83f50
-  /* Align up heap growth to make it friendly to CPU/MMU. */
5c83f50
-  if (grow > ~(grub_size_t) (GRUB_MM_HEAP_GROW_ALIGN - 1))
5c83f50
-    goto fail;
5c83f50
-  grow = ALIGN_UP (grow, GRUB_MM_HEAP_GROW_ALIGN);
5c83f50
+  grow = size + align;
5c83f50
 
5c83f50
   /* We currently assume at least a 32-bit grub_size_t,
5c83f50
      so limiting allocations to <adress space size> - 1MiB
5c83f50
@@ -510,6 +497,25 @@ grub_memalign (grub_size_t align, grub_size_t size)
5c83f50
       /* Request additional pages, contiguous */
5c83f50
       count++;
5c83f50
 
5c83f50
+      /*
5c83f50
+       * Calculate the necessary size of heap growth (if applicable),
5c83f50
+       * with region management overhead taken into account.
5c83f50
+       */
5c83f50
+      if (grub_add (grow, GRUB_MM_MGMT_OVERHEAD, &grow))
5c83f50
+	goto fail;
5c83f50
+
5c83f50
+      /* Preallocate some extra space if heap growth is small. */
5c83f50
+      grow = grub_max (grow, GRUB_MM_HEAP_GROW_EXTRA);
5c83f50
+
5c83f50
+      /* Align up heap growth to make it friendly to CPU/MMU. */
5c83f50
+      if (grow > ~(grub_size_t) (GRUB_MM_HEAP_GROW_ALIGN - 1))
5c83f50
+	goto fail;
5c83f50
+      grow = ALIGN_UP (grow, GRUB_MM_HEAP_GROW_ALIGN);
5c83f50
+
5c83f50
+      /* Do the same sanity check again. */
5c83f50
+      if (grow > ~(grub_size_t) 0x100000)
5c83f50
+	goto fail;
5c83f50
+
5c83f50
       if (grub_mm_add_region_fn != NULL &&
5c83f50
           grub_mm_add_region_fn (grow, GRUB_MM_ADD_REGION_CONSECUTIVE) == GRUB_ERR_NONE)
5c83f50
 	goto again;