From 80b90401468f054258aea3b0b79c2f353915f08e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Chru=C5=9Bci=C5=84ski?= Date: Wed, 21 May 2025 14:53:08 +0200 Subject: [PATCH] soc: nordic: dmm: Add lock around sys_heap operations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit sys_heap alloc and free are not thread safe so lock is needed to prevent data corruption. Signed-off-by: Krzysztof Chruściński --- soc/nordic/common/dmm.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/soc/nordic/common/dmm.c b/soc/nordic/common/dmm.c index cfb3f3ad25f..0b4e42f8c6d 100644 --- a/soc/nordic/common/dmm.c +++ b/soc/nordic/common/dmm.c @@ -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,