Skip to content

Commit 51fe9cd

Browse files
saschahauervinodkoul
authored andcommitted
dmaengine: virt-dma: Add missing locking
Originally freeing descriptors was split into a locked and an unlocked part. The locked part in vchan_get_all_descriptors() collected all descriptors on a separate list_head. This was done to allow iterating over that new list in vchan_dma_desc_free_list() without a lock held. This became broken in 13bb26a ("dmaengine: virt-dma: don't always free descriptor upon completion"). With this commit vchan_dma_desc_free_list() no longer exclusively operates on the separate list, but starts to put descriptors which can be reused back on &vc->desc_allocated. This list operation should have been locked, but wasn't. In the mean time drivers started to call vchan_dma_desc_free_list() with their lock held so that we now have the situation that vchan_dma_desc_free_list() is called locked from some drivers and unlocked from others. To clean this up we have to do two things: 1. Add missing locking in vchan_dma_desc_free_list() 2. Make sure drivers call vchan_dma_desc_free_list() unlocked This needs to be done atomically, so in this patch the locking is added and all drivers are fixed. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de> Reviewed-by: Peter Ujfalusi <peter.ujfalusi@ti.com> Tested-by: Peter Ujfalusi <peter.ujfalusi@ti.com> Reviewed-by: Green Wan <green.wan@sifive.com> Tested-by: Green Wan <green.wan@sifive.com> Link: https://lore.kernel.org/r/20191216105328.15198-3-s.hauer@pengutronix.de Signed-off-by: Vinod Koul <vkoul@kernel.org>
1 parent 5c8aacb commit 51fe9cd

7 files changed

Lines changed: 25 additions & 22 deletions

File tree

‎drivers/dma/dw-axi-dmac/dw-axi-dmac-platform.c‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -636,14 +636,10 @@ static int dma_chan_terminate_all(struct dma_chan *dchan)
636636

637637
vchan_get_all_descriptors(&chan->vc, &head);
638638

639-
/*
640-
* As vchan_dma_desc_free_list can access to desc_allocated list
641-
* we need to call it in vc.lock context.
642-
*/
643-
vchan_dma_desc_free_list(&chan->vc, &head);
644-
645639
spin_unlock_irqrestore(&chan->vc.lock, flags);
646640

641+
vchan_dma_desc_free_list(&chan->vc, &head);
642+
647643
dev_vdbg(dchan2dev(dchan), "terminated: %s\n", axi_chan_name(chan));
648644

649645
return 0;

‎drivers/dma/mediatek/mtk-uart-apdma.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,9 +430,10 @@ static int mtk_uart_apdma_terminate_all(struct dma_chan *chan)
430430

431431
spin_lock_irqsave(&c->vc.lock, flags);
432432
vchan_get_all_descriptors(&c->vc, &head);
433-
vchan_dma_desc_free_list(&c->vc, &head);
434433
spin_unlock_irqrestore(&c->vc.lock, flags);
435434

435+
vchan_dma_desc_free_list(&c->vc, &head);
436+
436437
return 0;
437438
}
438439

‎drivers/dma/owl-dma.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,10 +674,11 @@ static int owl_dma_terminate_all(struct dma_chan *chan)
674674
}
675675

676676
vchan_get_all_descriptors(&vchan->vc, &head);
677-
vchan_dma_desc_free_list(&vchan->vc, &head);
678677

679678
spin_unlock_irqrestore(&vchan->vc.lock, flags);
680679

680+
vchan_dma_desc_free_list(&vchan->vc, &head);
681+
681682
return 0;
682683
}
683684

‎drivers/dma/s3c24xx-dma.c‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -519,15 +519,6 @@ static void s3c24xx_dma_start_next_txd(struct s3c24xx_dma_chan *s3cchan)
519519
s3c24xx_dma_start_next_sg(s3cchan, txd);
520520
}
521521

522-
static void s3c24xx_dma_free_txd_list(struct s3c24xx_dma_engine *s3cdma,
523-
struct s3c24xx_dma_chan *s3cchan)
524-
{
525-
LIST_HEAD(head);
526-
527-
vchan_get_all_descriptors(&s3cchan->vc, &head);
528-
vchan_dma_desc_free_list(&s3cchan->vc, &head);
529-
}
530-
531522
/*
532523
* Try to allocate a physical channel. When successful, assign it to
533524
* this virtual channel, and initiate the next descriptor. The
@@ -709,8 +700,9 @@ static int s3c24xx_dma_terminate_all(struct dma_chan *chan)
709700
{
710701
struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(chan);
711702
struct s3c24xx_dma_engine *s3cdma = s3cchan->host;
703+
LIST_HEAD(head);
712704
unsigned long flags;
713-
int ret = 0;
705+
int ret;
714706

715707
spin_lock_irqsave(&s3cchan->vc.lock, flags);
716708

@@ -734,7 +726,15 @@ static int s3c24xx_dma_terminate_all(struct dma_chan *chan)
734726
}
735727

736728
/* Dequeue jobs not yet fired as well */
737-
s3c24xx_dma_free_txd_list(s3cdma, s3cchan);
729+
730+
vchan_get_all_descriptors(&s3cchan->vc, &head);
731+
732+
spin_unlock_irqrestore(&s3cchan->vc.lock, flags);
733+
734+
vchan_dma_desc_free_list(&s3cchan->vc, &head);
735+
736+
return 0;
737+
738738
unlock:
739739
spin_unlock_irqrestore(&s3cchan->vc.lock, flags);
740740

‎drivers/dma/sf-pdma/sf-pdma.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ static void sf_pdma_free_chan_resources(struct dma_chan *dchan)
155155
kfree(chan->desc);
156156
chan->desc = NULL;
157157
vchan_get_all_descriptors(&chan->vchan, &head);
158-
vchan_dma_desc_free_list(&chan->vchan, &head);
159158
sf_pdma_disclaim_chan(chan);
160159
spin_unlock_irqrestore(&chan->vchan.lock, flags);
160+
vchan_dma_desc_free_list(&chan->vchan, &head);
161161
}
162162

163163
static size_t sf_pdma_desc_residue(struct sf_pdma_chan *chan,
@@ -220,8 +220,8 @@ static int sf_pdma_terminate_all(struct dma_chan *dchan)
220220
chan->desc = NULL;
221221
chan->xfer_err = false;
222222
vchan_get_all_descriptors(&chan->vchan, &head);
223-
vchan_dma_desc_free_list(&chan->vchan, &head);
224223
spin_unlock_irqrestore(&chan->vchan.lock, flags);
224+
vchan_dma_desc_free_list(&chan->vchan, &head);
225225

226226
return 0;
227227
}

‎drivers/dma/sun4i-dma.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,12 +885,13 @@ static int sun4i_dma_terminate_all(struct dma_chan *chan)
885885
}
886886

887887
spin_lock_irqsave(&vchan->vc.lock, flags);
888-
vchan_dma_desc_free_list(&vchan->vc, &head);
889888
/* Clear these so the vchan is usable again */
890889
vchan->processing = NULL;
891890
vchan->pchan = NULL;
892891
spin_unlock_irqrestore(&vchan->vc.lock, flags);
893892

893+
vchan_dma_desc_free_list(&vchan->vc, &head);
894+
894895
return 0;
895896
}
896897

‎drivers/dma/virt-dma.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,11 @@ void vchan_dma_desc_free_list(struct virt_dma_chan *vc, struct list_head *head)
116116

117117
list_for_each_entry_safe(vd, _vd, head, node) {
118118
if (dmaengine_desc_test_reuse(&vd->tx)) {
119+
unsigned long flags;
120+
121+
spin_lock_irqsave(&vc->lock, flags);
119122
list_move_tail(&vd->node, &vc->desc_allocated);
123+
spin_unlock_irqrestore(&vc->lock, flags);
120124
} else {
121125
dev_dbg(vc->chan.device->dev, "txd %p: freeing\n", vd);
122126
list_del(&vd->node);

0 commit comments

Comments
 (0)