|
|
b35c50 |
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
|
b35c50 |
From: Daniel Axtens <dja@axtens.net>
|
|
|
b35c50 |
Date: Thu, 25 Nov 2021 02:22:49 +1100
|
|
|
b35c50 |
Subject: [PATCH] mm: Document grub_mm_init_region()
|
|
|
b35c50 |
|
|
|
b35c50 |
The grub_mm_init_region() does some things that seem magical, especially
|
|
|
b35c50 |
around region merging. Make it a bit clearer.
|
|
|
b35c50 |
|
|
|
b35c50 |
Signed-off-by: Daniel Axtens <dja@axtens.net>
|
|
|
b35c50 |
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
|
|
|
b35c50 |
(cherry picked from commit 246d69b7ea619fc1e77dcc5960e37aea45a9808c)
|
|
|
b35c50 |
---
|
|
|
b35c50 |
grub-core/kern/mm.c | 31 ++++++++++++++++++++++++++++++-
|
|
|
b35c50 |
1 file changed, 30 insertions(+), 1 deletion(-)
|
|
|
b35c50 |
|
|
|
b35c50 |
diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c
|
|
|
b35c50 |
index 0351171cf9..1cbf98c7ab 100644
|
|
|
b35c50 |
--- a/grub-core/kern/mm.c
|
|
|
b35c50 |
+++ b/grub-core/kern/mm.c
|
|
|
b35c50 |
@@ -128,23 +128,52 @@ grub_mm_init_region (void *addr, grub_size_t size)
|
|
|
b35c50 |
if (((grub_addr_t) addr + 0x1000) > ~(grub_addr_t) size)
|
|
|
b35c50 |
size = ((grub_addr_t) -0x1000) - (grub_addr_t) addr;
|
|
|
b35c50 |
|
|
|
b35c50 |
+ /* Attempt to merge this region with every existing region */
|
|
|
b35c50 |
for (p = &grub_mm_base, q = *p; q; p = &(q->next), q = *p)
|
|
|
b35c50 |
+ /*
|
|
|
b35c50 |
+ * Is the new region immediately below an existing region? That
|
|
|
b35c50 |
+ * is, is the address of the memory we're adding now (addr) + size
|
|
|
b35c50 |
+ * of the memory we're adding (size) + the bytes we couldn't use
|
|
|
b35c50 |
+ * at the start of the region we're considering (q->pre_size)
|
|
|
b35c50 |
+ * equal to the address of q? In other words, does the memory
|
|
|
b35c50 |
+ * looks like this?
|
|
|
b35c50 |
+ *
|
|
|
b35c50 |
+ * addr q
|
|
|
b35c50 |
+ * |----size-----|-q->pre_size-|<q region>|
|
|
|
b35c50 |
+ */
|
|
|
b35c50 |
if ((grub_uint8_t *) addr + size + q->pre_size == (grub_uint8_t *) q)
|
|
|
b35c50 |
{
|
|
|
b35c50 |
+ /*
|
|
|
b35c50 |
+ * Yes, we can merge the memory starting at addr into the
|
|
|
b35c50 |
+ * existing region from below. Align up addr to GRUB_MM_ALIGN
|
|
|
b35c50 |
+ * so that our new region has proper alignment.
|
|
|
b35c50 |
+ */
|
|
|
b35c50 |
r = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) addr, GRUB_MM_ALIGN);
|
|
|
b35c50 |
+ /* Copy the region data across */
|
|
|
b35c50 |
*r = *q;
|
|
|
b35c50 |
+ /* Consider all the new size as pre-size */
|
|
|
b35c50 |
r->pre_size += size;
|
|
|
b35c50 |
-
|
|
|
b35c50 |
+
|
|
|
b35c50 |
+ /*
|
|
|
b35c50 |
+ * If we have enough pre-size to create a block, create a
|
|
|
b35c50 |
+ * block with it. Mark it as allocated and pass it to
|
|
|
b35c50 |
+ * grub_free (), which will sort out getting it into the free
|
|
|
b35c50 |
+ * list.
|
|
|
b35c50 |
+ */
|
|
|
b35c50 |
if (r->pre_size >> GRUB_MM_ALIGN_LOG2)
|
|
|
b35c50 |
{
|
|
|
b35c50 |
h = (grub_mm_header_t) (r + 1);
|
|
|
b35c50 |
+ /* block size is pre-size converted to cells */
|
|
|
b35c50 |
h->size = (r->pre_size >> GRUB_MM_ALIGN_LOG2);
|
|
|
b35c50 |
h->magic = GRUB_MM_ALLOC_MAGIC;
|
|
|
b35c50 |
+ /* region size grows by block size converted back to bytes */
|
|
|
b35c50 |
r->size += h->size << GRUB_MM_ALIGN_LOG2;
|
|
|
b35c50 |
+ /* adjust pre_size to be accurate */
|
|
|
b35c50 |
r->pre_size &= (GRUB_MM_ALIGN - 1);
|
|
|
b35c50 |
*p = r;
|
|
|
b35c50 |
grub_free (h + 1);
|
|
|
b35c50 |
}
|
|
|
b35c50 |
+ /* Replace the old region with the new region */
|
|
|
b35c50 |
*p = r;
|
|
|
b35c50 |
return;
|
|
|
b35c50 |
}
|