[PATCH] spi: omap2-mcspi: lock the DMA request bit updates

Markus Klotzbuecher posted 1 patch an hour ago
drivers/spi/spi-omap2-mcspi.c | 7 +++++++
1 file changed, 7 insertions(+)
[PATCH] spi: omap2-mcspi: lock the DMA request bit updates
Posted by Markus Klotzbuecher an hour ago
omap2_mcspi_set_dma_req() does an unlocked read-modify-write of the
cached CHCONF value to set or clear DMAR/DMAW. On full-duplex DMA
transfers the RX and TX completion callbacks both call it to clear
their own bit. The callbacks are invoked from the per-channel dmaengine
tasklets, which can run concurrently on different CPUs. One of them
can then write back a stale copy that still has the other's bit set.

DMAR or DMAW then stays set in the cached value and is written back by
every later CHCONF update. With a stale DMAR, the next PIO transfer
times out waiting for RXS and the next RX DMA completes early.

Protect the read-modify-write in omap2_mcspi_set_dma_req() with a
spinlock. All other CHCONF updates run in the transfer thread while no
DMA callback is pending, so only this helper needs the lock.

Tested on a TI AM625 with linux-stable 6.18 PREEMPT_RT, a device on
McSPI2 CS1 at 50 MHz and four spidev transfers per cycle, the larger
ones via DMA. An instrumented kernel that flags stale DMAR/DMAW on PIO
entry reported more than 720 stale bit events and DMA transfer errors
in 15 minutes without this change, and none over an 11.5 hour soak
with it.

Fixes: 53741ed8f0a3 ("spi: omap2-mcspi: add DMA engine support")
Cc: stable@vger.kernel.org
Signed-off-by: Markus Klotzbuecher <mk@mkio.de>
---
Tested on 6.18.y with commit c126e07bde21 ("spi: omap2-mcspi: handle
DMA setup failures"); omap2_mcspi_set_dma_req() and the DMA callbacks
are the same on spi for-next. Compile-tested on spi for-next.

 drivers/spi/spi-omap2-mcspi.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/drivers/spi/spi-omap2-mcspi.c b/drivers/spi/spi-omap2-mcspi.c
index 061d50c65dfd..b1d3b8447d0b 100644
--- a/drivers/spi/spi-omap2-mcspi.c
+++ b/drivers/spi/spi-omap2-mcspi.c
@@ -118,6 +118,8 @@ struct omap2_mcspi_regs {
 
 struct omap2_mcspi {
 	struct completion	txdone;
+	/* serializes the DMAR/DMAW read-modify-write of the cached chconf */
+	spinlock_t		chconf_lock;
 	struct spi_controller	*ctlr;
 	/* Virtual base address of the controller */
 	void __iomem		*base;
@@ -205,8 +207,11 @@ static inline int mcspi_bytes_per_word(int word_len)
 static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
 		int is_read, int enable)
 {
+	struct omap2_mcspi *mcspi = spi_controller_get_devdata(spi->controller);
+	unsigned long flags;
 	u32 l, rw;
 
+	spin_lock_irqsave(&mcspi->chconf_lock, flags);
 	l = mcspi_cached_chconf0(spi);
 
 	if (is_read) /* 1 is read, 0 write */
@@ -220,6 +225,7 @@ static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
 		l &= ~rw;
 
 	mcspi_write_chconf0(spi, l);
+	spin_unlock_irqrestore(&mcspi->chconf_lock, flags);
 }
 
 static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
@@ -1630,6 +1636,7 @@ static int omap2_mcspi_probe(struct platform_device *pdev)
 	mcspi->base += regs_offset;
 
 	mcspi->dev = &pdev->dev;
+	spin_lock_init(&mcspi->chconf_lock);
 
 	INIT_LIST_HEAD(&mcspi->ctx.cs);
 

base-commit: 266ae6df1980a5a5c58735ae114ae62a2acad8db
-- 
2.53.0