Skip to content

Commit f7da778

Browse files
l1kvinodkoul
authored andcommitted
dmaengine: bcm2835: Fix interrupt race on RT
If IRQ handlers are threaded (either because CONFIG_PREEMPT_RT_BASE is enabled or "threadirqs" was passed on the command line) and if system load is sufficiently high that wakeup latency of IRQ threads degrades, SPI DMA transactions on the BCM2835 occasionally break like this: ks8851 spi0.0: SPI transfer timed out bcm2835-dma 3f007000.dma: DMA transfer could not be terminated ks8851 spi0.0 eth2: ks8851_rdfifo: spi_sync() failed The root cause is an assumption made by the DMA driver which is documented in a code comment in bcm2835_dma_terminate_all(): /* * Stop DMA activity: we assume the callback will not be called * after bcm_dma_abort() returns (even if it does, it will see * c->desc is NULL and exit.) */ That assumption falls apart if the IRQ handler bcm2835_dma_callback() is threaded: A client may terminate a descriptor and issue a new one before the IRQ handler had a chance to run. In fact the IRQ handler may miss an *arbitrary* number of descriptors. The result is the following race condition: 1. A descriptor finishes, its interrupt is deferred to the IRQ thread. 2. A client calls dma_terminate_async() which sets channel->desc = NULL. 3. The client issues a new descriptor. Because channel->desc is NULL, bcm2835_dma_issue_pending() immediately starts the descriptor. 4. Finally the IRQ thread runs and writes BCM2835_DMA_INT to the CS register to acknowledge the interrupt. This clears the ACTIVE flag, so the newly issued descriptor is paused in the middle of the transaction. Because channel->desc is not NULL, the IRQ thread finalizes the descriptor and tries to start the next one. I see two possible solutions: The first is to call synchronize_irq() in bcm2835_dma_issue_pending() to wait until the IRQ thread has finished before issuing a new descriptor. The downside of this approach is unnecessary latency if clients desire rapidly terminating and re-issuing descriptors and don't have any use for an IRQ callback. (The SPI TX DMA channel is a case in point.) A better alternative is to make the IRQ thread recognize that it has missed descriptors and avoid finalizing the newly issued descriptor. So first of all, set the ACTIVE flag when acknowledging the interrupt. This keeps a newly issued descriptor running. If the descriptor was finished, the channel remains idle despite the ACTIVE flag being set. However the ACTIVE flag can then no longer be used to check whether the channel is idle, so instead check whether the register containing the current control block address is zero and finalize the current descriptor only if so. That way, there is no impact on latency and throughput if the client doesn't care for the interrupt: Only minimal additional overhead is introduced for non-cyclic descriptors as one further MMIO read is necessary per interrupt to check for idleness of the channel. Cyclic descriptors are sped up slightly by removing one MMIO write per interrupt. Fixes: 96286b5 ("dmaengine: Add support for BCM2835") Signed-off-by: Lukas Wunner <lukas@wunner.de> Cc: stable@vger.kernel.org # v3.14+ Cc: Frank Pavlic <f.pavlic@kunbus.de> Cc: Martin Sperl <kernel@martin.sperl.org> Cc: Florian Meier <florian.meier@koalo.de> Cc: Clive Messer <clive.m.messer@gmail.com> Cc: Matthias Reichl <hias@horus.com> Tested-by: Stefan Wahren <stefan.wahren@i2se.com> Acked-by: Florian Kauer <florian.kauer@koalo.de> Signed-off-by: Vinod Koul <vkoul@kernel.org>
1 parent bfeffd1 commit f7da778

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

drivers/dma/bcm2835-dma.c

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,12 @@ static int bcm2835_dma_abort(void __iomem *chan_base)
412412
long int timeout = 10000;
413413

414414
cs = readl(chan_base + BCM2835_DMA_CS);
415-
if (!(cs & BCM2835_DMA_ACTIVE))
415+
416+
/*
417+
* A zero control block address means the channel is idle.
418+
* (The ACTIVE flag in the CS register is not a reliable indicator.)
419+
*/
420+
if (!readl(chan_base + BCM2835_DMA_ADDR))
416421
return 0;
417422

418423
/* Write 0 to the active bit - Pause the DMA */
@@ -476,20 +481,23 @@ static irqreturn_t bcm2835_dma_callback(int irq, void *data)
476481

477482
spin_lock_irqsave(&c->vc.lock, flags);
478483

479-
/* Acknowledge interrupt */
480-
writel(BCM2835_DMA_INT, c->chan_base + BCM2835_DMA_CS);
484+
/*
485+
* Clear the INT flag to receive further interrupts. Keep the channel
486+
* active in case the descriptor is cyclic or in case the client has
487+
* already terminated the descriptor and issued a new one. (May happen
488+
* if this IRQ handler is threaded.) If the channel is finished, it
489+
* will remain idle despite the ACTIVE flag being set.
490+
*/
491+
writel(BCM2835_DMA_INT | BCM2835_DMA_ACTIVE,
492+
c->chan_base + BCM2835_DMA_CS);
481493

482494
d = c->desc;
483495

484496
if (d) {
485497
if (d->cyclic) {
486498
/* call the cyclic callback */
487499
vchan_cyclic_callback(&d->vd);
488-
489-
/* Keep the DMA engine running */
490-
writel(BCM2835_DMA_ACTIVE,
491-
c->chan_base + BCM2835_DMA_CS);
492-
} else {
500+
} else if (!readl(c->chan_base + BCM2835_DMA_ADDR)) {
493501
vchan_cookie_complete(&c->desc->vd);
494502
bcm2835_dma_start_desc(c);
495503
}
@@ -789,20 +797,15 @@ static int bcm2835_dma_terminate_all(struct dma_chan *chan)
789797
list_del_init(&c->node);
790798
spin_unlock(&d->lock);
791799

792-
/*
793-
* Stop DMA activity: we assume the callback will not be called
794-
* after bcm_dma_abort() returns (even if it does, it will see
795-
* c->desc is NULL and exit.)
796-
*/
800+
/* stop DMA activity */
797801
if (c->desc) {
798802
vchan_terminate_vdesc(&c->desc->vd);
799803
c->desc = NULL;
800804
bcm2835_dma_abort(c->chan_base);
801805

802806
/* Wait for stopping */
803807
while (--timeout) {
804-
if (!(readl(c->chan_base + BCM2835_DMA_CS) &
805-
BCM2835_DMA_ACTIVE))
808+
if (!readl(c->chan_base + BCM2835_DMA_ADDR))
806809
break;
807810

808811
cpu_relax();

0 commit comments

Comments
 (0)