Skip to content

Commit 61616e1

Browse files
zhangboyangfrozencemetery
authored andcommitted
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 <[email protected]> Reviewed-by: Daniel Kiper <[email protected]> (cherry picked from commit 65bc459)
1 parent c9176ae commit 61616e1

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

grub-core/kern/mm.c

+20-14
Original file line numberDiff line numberDiff line change
@@ -467,20 +467,7 @@ grub_memalign (grub_size_t align, grub_size_t size)
467467
if (size > ~(grub_size_t) align)
468468
goto fail;
469469

470-
/*
471-
* Pre-calculate the necessary size of heap growth (if applicable),
472-
* with region management overhead taken into account.
473-
*/
474-
if (grub_add (size + align, GRUB_MM_MGMT_OVERHEAD, &grow))
475-
goto fail;
476-
477-
/* Preallocate some extra space if heap growth is small. */
478-
grow = grub_max (grow, GRUB_MM_HEAP_GROW_EXTRA);
479-
480-
/* Align up heap growth to make it friendly to CPU/MMU. */
481-
if (grow > ~(grub_size_t) (GRUB_MM_HEAP_GROW_ALIGN - 1))
482-
goto fail;
483-
grow = ALIGN_UP (grow, GRUB_MM_HEAP_GROW_ALIGN);
470+
grow = size + align;
484471

485472
/* We currently assume at least a 32-bit grub_size_t,
486473
so limiting allocations to <adress space size> - 1MiB
@@ -510,6 +497,25 @@ grub_memalign (grub_size_t align, grub_size_t size)
510497
/* Request additional pages, contiguous */
511498
count++;
512499

500+
/*
501+
* Calculate the necessary size of heap growth (if applicable),
502+
* with region management overhead taken into account.
503+
*/
504+
if (grub_add (grow, GRUB_MM_MGMT_OVERHEAD, &grow))
505+
goto fail;
506+
507+
/* Preallocate some extra space if heap growth is small. */
508+
grow = grub_max (grow, GRUB_MM_HEAP_GROW_EXTRA);
509+
510+
/* Align up heap growth to make it friendly to CPU/MMU. */
511+
if (grow > ~(grub_size_t) (GRUB_MM_HEAP_GROW_ALIGN - 1))
512+
goto fail;
513+
grow = ALIGN_UP (grow, GRUB_MM_HEAP_GROW_ALIGN);
514+
515+
/* Do the same sanity check again. */
516+
if (grow > ~(grub_size_t) 0x100000)
517+
goto fail;
518+
513519
if (grub_mm_add_region_fn != NULL &&
514520
grub_mm_add_region_fn (grow, GRUB_MM_ADD_REGION_CONSECUTIVE) == GRUB_ERR_NONE)
515521
goto again;

0 commit comments

Comments
 (0)