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