Skip to content

Commit 93d2756

Browse files
marcinszkudlinskilgirdwood
authored andcommitted
mem: add mutex protection for alloc and free
virtual heap may be called from multiple threads, all operations should be protected my mutex Signed-off-by: Marcin Szkudlinski <marcin.szkudlinski@intel.com>
1 parent 38c4810 commit 93d2756

2 files changed

Lines changed: 34 additions & 7 deletions

File tree

zephyr/lib/alloc.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
struct vmh_heap;
2525
struct vmh_heap *virtual_buffers_heap;
26-
struct k_spinlock vmh_lock;
2726

2827
#undef HEAPMEM_SIZE
2928
/* Buffers are allocated from virtual space so we can safely reduce the heap size.
@@ -279,8 +278,6 @@ static const struct vmh_heap_config static_hp_buffers = {
279278

280279
static int virtual_heap_init(void)
281280
{
282-
k_spinlock_init(&vmh_lock);
283-
284281
virtual_buffers_heap = vmh_init_heap(&static_hp_buffers, false);
285282
if (!virtual_buffers_heap) {
286283
tr_err(&zephyr_tr, "Unable to init virtual heap");

zephyr/lib/regions_mm.c

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* will be contiguous or single block.
2929
*/
3030
struct vmh_heap {
31+
struct k_mutex lock;
3132
const struct sys_mm_drv_region *virtual_region;
3233
struct sys_mem_blocks *physical_blocks_allocators[MAX_MEMORY_ALLOCATORS_COUNT];
3334
struct sys_bitarray *allocation_sizes[MAX_MEMORY_ALLOCATORS_COUNT];
@@ -58,6 +59,7 @@ struct vmh_heap *vmh_init_heap(const struct vmh_heap_config *cfg, bool allocatin
5859
if (!new_heap)
5960
return NULL;
6061

62+
k_mutex_init(&new_heap->lock);
6163
struct vmh_heap_config new_config = {0};
6264

6365
/* Search for matching attribute so we place heap on shared virtual region */
@@ -378,7 +380,7 @@ static int vmh_unmap_region(struct sys_mem_blocks *region, void *ptr, size_t siz
378380
}
379381

380382
/**
381-
* @brief Alloc function
383+
* @brief Alloc function, not reentrant
382384
*
383385
* Allocates memory on heap from provided heap pointer.
384386
* Check if we need to map physical memory for given allocation
@@ -389,7 +391,7 @@ static int vmh_unmap_region(struct sys_mem_blocks *region, void *ptr, size_t siz
389391
* @retval ptr to allocated memory if successful
390392
* @retval NULL on allocation failure
391393
*/
392-
void *vmh_alloc(struct vmh_heap *heap, uint32_t alloc_size)
394+
static void *_vmh_alloc(struct vmh_heap *heap, uint32_t alloc_size)
393395
{
394396
if (!alloc_size)
395397
return NULL;
@@ -507,6 +509,20 @@ void *vmh_alloc(struct vmh_heap *heap, uint32_t alloc_size)
507509
return ptr;
508510
}
509511

512+
/**
513+
* @brief Alloc function, reentrant
514+
* see _vmh_alloc comment for details
515+
*/
516+
void *vmh_alloc(struct vmh_heap *heap, uint32_t alloc_size)
517+
{
518+
k_mutex_lock(&heap->lock, K_FOREVER);
519+
520+
void *ret = _vmh_alloc(heap, alloc_size);
521+
522+
k_mutex_unlock(&heap->lock);
523+
return ret;
524+
}
525+
510526
/**
511527
* @brief Free virtual memory heap
512528
*
@@ -542,7 +558,7 @@ int vmh_free_heap(struct vmh_heap *heap)
542558
}
543559

544560
/**
545-
* @brief Free ptr allocated on given heap
561+
* @brief Free ptr allocated on given heap, not reentrant
546562
*
547563
* Free the ptr allocation. After free action is complete
548564
* check if any physical memory block can be freed up as
@@ -553,7 +569,7 @@ int vmh_free_heap(struct vmh_heap *heap)
553569
* @retval 0 on success;
554570
* @retval -ENOTEMPTY on heap having active allocations.
555571
*/
556-
int vmh_free(struct vmh_heap *heap, void *ptr)
572+
static int _vmh_free(struct vmh_heap *heap, void *ptr)
557573
{
558574
int retval;
559575

@@ -670,6 +686,20 @@ int vmh_free(struct vmh_heap *heap, void *ptr)
670686
size_to_free);
671687
}
672688

689+
/**
690+
* @brief Free ptr function, reentrant
691+
* see _vmh_free comment for details
692+
*/
693+
int vmh_free(struct vmh_heap *heap, void *ptr)
694+
{
695+
k_mutex_lock(&heap->lock, K_FOREVER);
696+
697+
int ret = _vmh_free(heap, ptr);
698+
699+
k_mutex_unlock(&heap->lock);
700+
return ret;
701+
}
702+
673703
/**
674704
* @brief Get default configuration for heap
675705
*

0 commit comments

Comments
 (0)