soc: nordic: dmm: Add lock around sys_heap operations

sys_heap alloc and free are not thread safe so lock is needed to
prevent data corruption.

Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
This commit is contained in:
Krzysztof Chruściński 2025-05-21 14:53:08 +02:00 committed by Benjamin Cabé
commit 80b9040146

View file

@ -44,6 +44,7 @@ struct dmm_region {
struct dmm_heap {
struct sys_heap heap;
const struct dmm_region *region;
struct k_spinlock lock;
};
static const struct dmm_region dmm_regions[] = {
@ -54,6 +55,7 @@ struct {
struct dmm_heap dmm_heaps[ARRAY_SIZE(dmm_regions)];
} dmm_heaps_data;
static struct dmm_heap *dmm_heap_find(void *region)
{
struct dmm_heap *dh;
@ -113,13 +115,25 @@ static size_t dmm_heap_size_get(struct dmm_heap *dh)
static void *dmm_buffer_alloc(struct dmm_heap *dh, size_t length)
{
void *ret;
k_spinlock_key_t key;
length = ROUND_UP(length, dh->region->dt_align);
return sys_heap_aligned_alloc(&dh->heap, dh->region->dt_align, length);
key = k_spin_lock(&dh->lock);
ret = sys_heap_aligned_alloc(&dh->heap, dh->region->dt_align, length);
k_spin_unlock(&dh->lock, key);
return ret;
}
static void dmm_buffer_free(struct dmm_heap *dh, void *buffer)
{
k_spinlock_key_t key;
key = k_spin_lock(&dh->lock);
sys_heap_free(&dh->heap, buffer);
k_spin_unlock(&dh->lock, key);
}
int dmm_buffer_out_prepare(void *region, void const *user_buffer, size_t user_length,