From f3260668bfa4db80245d3c55d834b627adc35ddd Mon Sep 17 00:00:00 2001 From: Jakub Dabek Date: Fri, 9 Feb 2024 16:32:09 +0100 Subject: [PATCH 1/4] vmh: fix allocation size check When using non contiguous allocation block size is calculated after the check. Signed-off-by: Jakub Dabek --- zephyr/lib/regions_mm.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/zephyr/lib/regions_mm.c b/zephyr/lib/regions_mm.c index dbb1aa087b26..2ee151ac9ed9 100644 --- a/zephyr/lib/regions_mm.c +++ b/zephyr/lib/regions_mm.c @@ -263,9 +263,6 @@ void *vmh_alloc(struct vmh_heap *heap, uint32_t alloc_size) if (!heap->physical_blocks_allocators[mem_block_iterator]) continue; - /* If we do not span alloc and block is smaller than alloc we try next mem_block */ - if (block_size < alloc_size && !heap->allocating_continuously) - continue; /* calculate block count needed to allocate for current * mem_block. */ @@ -273,6 +270,10 @@ void *vmh_alloc(struct vmh_heap *heap, uint32_t alloc_size) 1 << heap->physical_blocks_allocators[mem_block_iterator]->info.blk_sz_shift; block_count = SOF_DIV_ROUND_UP((uint64_t)alloc_size, (uint64_t)block_size); + /* If we do not span alloc and block is smaller than alloc we try next mem_block */ + if (block_size < alloc_size && !heap->allocating_continuously) + continue; + if (block_count > heap->physical_blocks_allocators[mem_block_iterator]->info.num_blocks) continue; From 3ff62f3e95e235257c617f5d3960f43c9c1c5537 Mon Sep 17 00:00:00 2001 From: Jakub Dabek Date: Fri, 9 Feb 2024 16:37:35 +0100 Subject: [PATCH 2/4] vmh: fix same size block allocation When allocating non contiguously if exact block size was allocated on given physical allocator it would fail. Fix logic to include that event. Signed-off-by: Jakub Dabek --- zephyr/lib/regions_mm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zephyr/lib/regions_mm.c b/zephyr/lib/regions_mm.c index 2ee151ac9ed9..e901d7231543 100644 --- a/zephyr/lib/regions_mm.c +++ b/zephyr/lib/regions_mm.c @@ -284,7 +284,7 @@ void *vmh_alloc(struct vmh_heap *heap, uint32_t alloc_size) allocation_error_code = sys_mem_blocks_alloc_contiguous( heap->physical_blocks_allocators[mem_block_iterator], block_count, &ptr); - } else if (block_size > alloc_size) { + } else if (block_size >= alloc_size) { allocation_error_code = sys_mem_blocks_alloc( heap->physical_blocks_allocators[mem_block_iterator], block_count, &ptr); From 430935f4c50abff03a73cc3b08f3fb2719f21d8c Mon Sep 17 00:00:00 2001 From: Jakub Dabek Date: Fri, 23 Feb 2024 15:56:01 +0100 Subject: [PATCH 3/4] vmh: fix array update for contiguous allocation Array holding sizes of allocations was not updated correctly this fixes array update issue. Signed-off-by: Jakub Dabek --- zephyr/lib/regions_mm.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/zephyr/lib/regions_mm.c b/zephyr/lib/regions_mm.c index e901d7231543..1a0d4f00b5be 100644 --- a/zephyr/lib/regions_mm.c +++ b/zephyr/lib/regions_mm.c @@ -495,7 +495,7 @@ int vmh_free(struct vmh_heap *heap, void *ptr) size_t mem_block_iter, i, size_to_free, block_size, ptr_bit_array_offset, ptr_bit_array_position, physical_block_count, - check_offset, check_position, check_size; + check_offset, check_position, check_size, blocks_to_free; uintptr_t phys_aligned_ptr, phys_aligned_alloc_end, phys_block_ptr; bool ptr_range_found; @@ -581,10 +581,13 @@ int vmh_free(struct vmh_heap *heap, void *ptr) */ size_to_free = block_size; } - + blocks_to_free = size_to_free / block_size; retval = sys_mem_blocks_free_contiguous( heap->physical_blocks_allocators[mem_block_iter], ptr, - size_to_free / block_size); + blocks_to_free); + if (!retval) + sys_bitarray_clear_region(heap->allocation_sizes[mem_block_iter], + blocks_to_free, ptr_bit_array_position); } else { retval = sys_mem_blocks_free(heap->physical_blocks_allocators[mem_block_iter], 1, &ptr); From ea65d215735661926b50e5442c0571d102f3b0b8 Mon Sep 17 00:00:00 2001 From: Jakub Dabek Date: Fri, 23 Feb 2024 15:59:08 +0100 Subject: [PATCH 4/4] vmh: fix allocation size value calculation Allocation sizes were calculated with faulty logic. Bits to check size calculation is not needed since array position was calculated and loop should go from this position to array end. Signed-off-by: Jakub Dabek --- zephyr/lib/regions_mm.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/zephyr/lib/regions_mm.c b/zephyr/lib/regions_mm.c index 1a0d4f00b5be..9a232cf03d09 100644 --- a/zephyr/lib/regions_mm.c +++ b/zephyr/lib/regions_mm.c @@ -552,20 +552,14 @@ int vmh_free(struct vmh_heap *heap, void *ptr) return -EINVAL; if (bit_value) { - /* We know we have more than one block was allocated so - * we need to find the size - */ - size_t bits_to_check = - heap->physical_blocks_allocators - [mem_block_iter]->info.num_blocks - ptr_bit_array_position; - /* Neeeeeeeds optimization - thinking how to do it properly * each set bit in order after another means one allocated block. * When we get to 0 in such range we know that is last allocated block. * Testing bundles looks promising - need to investigate. */ for (i = ptr_bit_array_position; - i < bits_to_check; + i < heap->physical_blocks_allocators + [mem_block_iter]->info.num_blocks; i++) { sys_bitarray_test_bit(heap->allocation_sizes[mem_block_iter], i,