From 355aecceb40af9188d8be548ee9fc663a588c903 Mon Sep 17 00:00:00 2001 From: Ranjani Sridharan Date: Sat, 5 Nov 2022 20:04:13 -0700 Subject: [PATCH 1/3] lib: dma: free dma channels when ref count is 0 Free the memory allocated for DMA channels when the ref count is 0 when using native zephyr drivers. Signed-off-by: Ranjani Sridharan --- src/lib/dma.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/lib/dma.c b/src/lib/dma.c index d8df3855442b..6f677c9fcfd1 100644 --- a/src/lib/dma.c +++ b/src/lib/dma.c @@ -128,7 +128,10 @@ void dma_put(struct dma *dma) k_spinlock_key_t key; key = k_spin_lock(&dma->lock); - --dma->sref; + if (--dma->sref == 0) { + rfree(dma->chan); + dma->chan = NULL; + } tr_info(&dma_tr, "dma_put(), dma = %p, sref = %d", dma, dma->sref); From df8318f2ccc6be09e72ad8f33a976b40b26c7833 Mon Sep 17 00:00:00 2001 From: Ranjani Sridharan Date: Sun, 6 Nov 2022 14:24:14 -0800 Subject: [PATCH 2/3] lib: dma: return NULL when DMA init fails Return NULL when dma_init() fails. Signed-off-by: Ranjani Sridharan --- src/lib/dma.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/lib/dma.c b/src/lib/dma.c index 6f677c9fcfd1..58aa64dc841e 100644 --- a/src/lib/dma.c +++ b/src/lib/dma.c @@ -110,17 +110,18 @@ struct dma *dma_get(uint32_t dir, uint32_t cap, uint32_t dev, uint32_t flags) if (ret < 0) { tr_err(&dma_tr, "dma_get(): dma-probe failed id = %d, ret = %d", dmin->plat_data.id, ret); + goto out; } } - if (!ret) - dmin->sref++; + + dmin->sref++; tr_info(&dma_tr, "dma_get() ID %d sref = %d busy channels %ld", dmin->plat_data.id, dmin->sref, atomic_read(&dmin->num_channels_busy)); - +out: k_spin_unlock(&dmin->lock, key); - return dmin; + return !ret ? dmin : NULL; } void dma_put(struct dma *dma) From 2f225f8ad84ff465503b58a9299cda5cbc214f7b Mon Sep 17 00:00:00 2001 From: Ranjani Sridharan Date: Sun, 6 Nov 2022 08:14:58 -0800 Subject: [PATCH 3/3] dai-zephyr: Free DMA config block during reset This prevents memory leaks during repeated PCM start/stop tests. Signed-off-by: Ranjani Sridharan --- src/audio/dai-zephyr.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/audio/dai-zephyr.c b/src/audio/dai-zephyr.c index ebe8f25216f2..a05a81fdddda 100644 --- a/src/audio/dai-zephyr.c +++ b/src/audio/dai-zephyr.c @@ -934,6 +934,7 @@ static int dai_reset(struct comp_dev *dev) dai_dma_release(dev); dma_sg_free(&config->elem_array); + rfree(dd->z_config->head_block); rfree(dd->z_config); if (dd->dma_buffer) {