From dd6c19072089e4014d0c38a83a16434726868943 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Fri, 24 Mar 2023 14:17:28 +0000 Subject: [PATCH] memblock: fix returned memory region overrunning reserved memory region --- vm/memblock.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/vm/memblock.c b/vm/memblock.c index c4fb007..14f33ba 100644 --- a/vm/memblock.c +++ b/vm/memblock.c @@ -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; }