Skip to content

Commit 5a52ae2

Browse files
committed
dw-dma: commit descriptor count only after successful alloc
The descriptor count was updated before the descriptor buffer was reallocated, so on allocation failure the count was left larger than the now-NULL buffer and a later config zeroed a NULL pointer using the stale count. Update the count only after a successful allocation and reset it on failure. Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
1 parent 3a03392 commit 5a52ae2

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

src/drivers/dw/dma.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -532,8 +532,6 @@ static int dw_dma_set_config(struct dma_chan_data *channel,
532532
/* do we need to realloc descriptors */
533533
if (config->elem_array.count != channel->desc_count) {
534534

535-
channel->desc_count = config->elem_array.count;
536-
537535
/*
538536
* Allocate descriptors for channel. They must be cache-line
539537
* size aligned to avoid corrupting adjacent memory when
@@ -542,18 +540,25 @@ static int dw_dma_set_config(struct dma_chan_data *channel,
542540
* allocations on Zephyr to always force cache-line size
543541
* alignment.
544542
*/
545-
if (dw_chan->lli)
546-
rfree(dw_chan->lli);
543+
rfree(dw_chan->lli);
547544

548545
dw_chan->lli = rmalloc(SOF_MEM_FLAG_KERNEL | SOF_MEM_FLAG_COHERENT | SOF_MEM_FLAG_DMA,
549-
sizeof(struct dw_lli) * channel->desc_count);
546+
sizeof(struct dw_lli) * config->elem_array.count);
550547
if (!dw_chan->lli) {
551548
tr_err(&dwdma_tr, "dma %d channel %d lli alloc failed",
552549
channel->dma->plat_data.id,
553550
channel->index);
551+
/* allocation failed, so dw_chan->lli is now NULL; reset
552+
* the count to match it so a later config does not
553+
* bzero() a NULL pointer using a stale count
554+
*/
555+
channel->desc_count = 0;
554556
ret = -ENOMEM;
555557
goto out;
556558
}
559+
560+
/* only commit the new count once the buffer is allocated */
561+
channel->desc_count = config->elem_array.count;
557562
}
558563

559564
/* initialise descriptors */

0 commit comments

Comments
 (0)