]> git.ipfire.org Git - thirdparty/grub.git/commit
mm: Adjust new region size to take management overhead into account
authorZhang Boyang <zhangboyang.id@gmail.com>
Sun, 29 Jan 2023 11:49:31 +0000 (19:49 +0800)
committerDaniel Kiper <daniel.kiper@oracle.com>
Thu, 2 Feb 2023 18:44:56 +0000 (19:44 +0100)
commit2282cbfe5aa1ff6c1bbcbdcd2003089ad7c03ba3
tree99d6d12428593e65da34864302b77e8d61d69021
parent6d729ced70afe2d1990be9a88cc88c3c2a93b51f
mm: Adjust new region size to take management overhead into account

When grub_memalign() encounters out-of-memory, it will try
grub_mm_add_region_fn() to request more memory from system firmware.
However, the size passed to it doesn't take region management overhead
into account. Adding a memory area of "size" bytes may result in a heap
region of less than "size" bytes really available. Thus, the new region
may not be adequate for current allocation request, confusing
out-of-memory handling code.

This patch introduces GRUB_MM_MGMT_OVERHEAD to address the region
management overhead (e.g. metadata, padding). The value of this new
constant must be large enough to make sure grub_memalign(align, size)
always succeeds after a successful call to
  grub_mm_init_region(addr, size + align + GRUB_MM_MGMT_OVERHEAD),
for any given addr and size (assuming no integer overflow).

The size passed to grub_mm_add_region_fn() is now correctly adjusted,
thus if grub_mm_add_region_fn() succeeded, current allocation request
can always succeed.

Signed-off-by: Zhang Boyang <zhangboyang.id@gmail.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
grub-core/kern/mm.c