On Thu, Dec 22, 2022 at 05:18:24PM +0800, Zhang Boyang wrote: > When grub_memalign() encounters out-of-memory, it will try > grub_mm_add_region_fn() to request more memory from system firmware. > However, it doesn't preallocate memory space for future allocation > requests. In extreme cases, it requires one call to > grub_mm_add_region_fn() for each memory allocation request. This can be > very slow. > > This patch introduces GRUB_MM_HEAP_GROW_EXTRA, the minimal heap growth > granularity. The new region size is now set to the bigger one of its > original value and GRUB_MM_HEAP_GROW_EXTRA. Thus, it will result in some > memory space preallocated if current allocations request is small. > > The value of GRUB_MM_HEAP_GROW_EXTRA is set to 1MB. If this value is > smaller, the cost of small memory allocations will be higher. If this > value is larger, more memory will be wasted and it might cause > out-of-memory on machines with small amount of RAM. > > Signed-off-by: Zhang Boyang <zhangboyang...@gmail.com> > --- > grub-core/kern/mm.c | 6 +++++- > 1 file changed, 5 insertions(+), 1 deletion(-) > > diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c > index 5c0e5bbbf..16c28cab8 100644 > --- a/grub-core/kern/mm.c > +++ b/grub-core/kern/mm.c > @@ -120,6 +120,9 @@ > + sizeof (struct grub_mm_header) \ > + (GRUB_MM_ALIGN - 1)) > > +/* Minimal heap growth granularity when existing heap space is exhausted. */ > +#define GRUB_MM_HEAP_GROW_EXTRA 0x100000 > + > grub_mm_region_t grub_mm_base; > grub_mm_add_region_func_t grub_mm_add_region_fn; > > @@ -468,11 +471,12 @@ grub_memalign (grub_size_t align, grub_size_t size) > goto fail; > > /* > - * Pre-calculate the size of heap growth (if applicable), > + * Pre-calculate the optimal 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; > + grow = grub_max (grow, GRUB_MM_HEAP_GROW_EXTRA);
If you update the #1 patch as I suggested then this should be grow = grub_max (size, GRUB_MM_HEAP_GROW_EXTRA); Daniel _______________________________________________ Grub-devel mailing list Grub-devel@gnu.org https://lists.gnu.org/mailman/listinfo/grub-devel