Skip to content

Commit 80b9040

Browse files
nordic-krchkartben
authored andcommitted
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>
1 parent 37b4407 commit 80b9040

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

soc/nordic/common/dmm.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ struct dmm_region {
4444
struct dmm_heap {
4545
struct sys_heap heap;
4646
const struct dmm_region *region;
47+
struct k_spinlock lock;
4748
};
4849

4950
static const struct dmm_region dmm_regions[] = {
@@ -54,6 +55,7 @@ struct {
5455
struct dmm_heap dmm_heaps[ARRAY_SIZE(dmm_regions)];
5556
} dmm_heaps_data;
5657

58+
5759
static struct dmm_heap *dmm_heap_find(void *region)
5860
{
5961
struct dmm_heap *dh;
@@ -113,13 +115,25 @@ static size_t dmm_heap_size_get(struct dmm_heap *dh)
113115

114116
static void *dmm_buffer_alloc(struct dmm_heap *dh, size_t length)
115117
{
118+
void *ret;
119+
k_spinlock_key_t key;
120+
116121
length = ROUND_UP(length, dh->region->dt_align);
117-
return sys_heap_aligned_alloc(&dh->heap, dh->region->dt_align, length);
122+
123+
key = k_spin_lock(&dh->lock);
124+
ret = sys_heap_aligned_alloc(&dh->heap, dh->region->dt_align, length);
125+
k_spin_unlock(&dh->lock, key);
126+
127+
return ret;
118128
}
119129

120130
static void dmm_buffer_free(struct dmm_heap *dh, void *buffer)
121131
{
132+
k_spinlock_key_t key;
133+
134+
key = k_spin_lock(&dh->lock);
122135
sys_heap_free(&dh->heap, buffer);
136+
k_spin_unlock(&dh->lock, key);
123137
}
124138

125139
int dmm_buffer_out_prepare(void *region, void const *user_buffer, size_t user_length,

0 commit comments

Comments
 (0)