]> xenbits.xensource.com Git - unikraft/unikraft.git/commitdiff
plat/kvm/x86: Fix `bootmemory_palloc` keeping empty memory region
authorSergiu Moga <sergiu.moga@protonmail.com>
Wed, 5 Apr 2023 09:42:21 +0000 (12:42 +0300)
committerUnikraft <monkey@unikraft.io>
Fri, 11 Aug 2023 10:18:44 +0000 (10:18 +0000)
Whenever a memory region allocation request done through
`bootmemory_palloc` would require a length equal to one of the
available memory regions, a new, equivalent, memory region would
be created and the original one would be left empty, thus breaking
the memory allocator.

Signed-off-by: Sergiu Moga <sergiu.moga@protonmail.com>
Reviewed-by: Michalis Pappas <michalis@unikraft.io>
Approved-by: Razvan Deaconescu <razvand@unikraft.io>
Tested-by: Unikraft CI <monkey@unikraft.io>
GitHub-Closes: #848

plat/kvm/x86/setup.c

index 044a513f7a4ad8cc48170f876d807778b4bcb232..d20b90459fcf02ce7900c51e8e2e0c4cfe65ffcb 100644 (file)
@@ -67,6 +67,21 @@ static void *bootmemory_palloc(__sz size, int type)
                ostart = mrd->pbase;
                olen   = mrd->len;
 
+               /* If fragmenting this memory region leaves it with length 0,
+                * then simply overwrite and return it instead
+                */
+               if (olen - (pstart - ostart) == size) {
+                       mrd->pbase = pstart;
+                       mrd->vbase = pstart;
+                       mrd->len = pend - pstart;
+                       mrd->type = type;
+                       mrd->flags = UKPLAT_MEMRF_READ |
+                                    UKPLAT_MEMRF_WRITE |
+                                    UKPLAT_MEMRF_MAP;
+
+                       return (void *)pstart;
+               }
+
                /* Adjust free region */
                mrd->len  -= pend - mrd->pbase;
                mrd->pbase = pend;