memblock: fix returned memory region overrunning reserved memory region

This commit is contained in:
2023-03-24 14:17:28 +00:00
parent 2bfb6bcd78
commit dd6c190720

View File

@@ -235,8 +235,8 @@ static phys_addr_t do_alloc(size_t size, phys_addr_t align)
align = 0x8;
}
/* the base address of the memory region to reserved */
phys_addr_t allocated_base = ADDR_MAX;
/* the bounds of the memory region to reserve */
phys_addr_t allocated_base = ADDR_MAX, allocated_limit = 0;
/* the address to return to the caller. may be different from
allocated_base depending on alignment requirements. */
phys_addr_t returned_base = ADDR_MAX;
@@ -255,6 +255,7 @@ static phys_addr_t do_alloc(size_t size, phys_addr_t align)
size_t region_size = it.it_limit - base + 1;
if (region_size >= size) {
allocated_base = it.it_base;
allocated_limit = base + size;
returned_base = base;
break;
}
@@ -264,7 +265,7 @@ static phys_addr_t do_alloc(size_t size, phys_addr_t align)
return 0;
}
int status = memblock_add_range(&memblock.reserved, allocated_base, size, MEMBLOCK_ALLOC);
int status = memblock_add_range(&memblock.reserved, allocated_base, allocated_limit - allocated_base, MEMBLOCK_ALLOC);
if (status != 0) {
return 0;
}