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