nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0322-mm-Avoid-complex-heap-growth-math-in-hot-path.patch

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