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