grub2/SOURCES/0322-mm-Avoid-complex-heap-...

73 lines
2.7 KiB
Diff

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