From: sashiko-bot@kernel.org
To: "Binbin Zhou" <zhoubinbin@loongson.cn>
Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH 4/5] spi: loongson-io: Add DMA support to SPI-IO driver
Date: Mon, 23 Nov 2026 10:17:04 +0000 [thread overview]
Message-ID: <sashiko-outbox-229031@kernel.org> (raw)
In-Reply-To: <c97601131ad439f6648e5c8d5a624a2588479728.1795425389.git.zhoubinbin@loongson.cn>
Thank you for your contribution! Sashiko AI review found 8 potential
issues to consider:
- [High] spi: loongson-io: device DMA sync in
loongson_spi_io_dma_drain_rx() loses tail
- [High] spi: loongson-io: ERR_PTR dereference in
loongson_spi_dma_config_and_submit()
- [Medium] spi: loongson-io: PIO fallback in
loongson_spi_io_transfer_one() corrupts RX
- [Medium] spi: loongson-io: loongson_spi_io_transfer_one() re-evaluates
can_dma()
- [Medium] spi: loongson-io: EOT IRQ left enabled after
loongson_spi_dma_transfer()
- [Medium] spi: loongson-io: leaked TX DMA channel in
loongson_spi_io_probe() fallback
- [Medium] spi: loongson-io: missing buffer alignment and presence
checks in loongson_spi_can_dma()
- [Low] spi: loongson-io: unmasked EOTIE on exit in
loongson_spi_dma_transfer()
--
commit b9f174f53160693a3668bfcf2827fd85a0dafb9d
Author: Binbin Zhou <zhoubinbin@loongson.cn>
spi: loongson-io: Add DMA support to SPI-IO driver
This commit adds optional TX and RX DMA channel support to the Loongson
SPI-IO controller driver, falling back to interrupt mode when DMA is
unavailable or below the transfer threshold.
> diff --git a/drivers/spi/spi-loongson-io.c b/drivers/spi/spi-loongson-io.c
> index e273c9f4a9430..c153e483ab41d 100644
> --- a/drivers/spi/spi-loongson-io.c
> +++ b/drivers/spi/spi-loongson-io.c
[ ... ]
> @@ -264,11 +292,59 @@ static unsigned long loongson_spi_io_timeout(struct loongson_spi_priv *priv,
> return msecs_to_jiffies(max_t(u64, msec * 2, 1000));
> }
[ ... ]
> +static void loongson_spi_io_dma_drain_rx(struct loongson_spi_priv *priv,
> + struct spi_transfer *xfer)
> +{
> + struct device *rx_dev = dmaengine_get_dma_device(priv->dma_rx);
> + u32 rxflv = FIELD_GET(LOONGSON_SPI_IO_SR2_RXFLV,
> + loongson_spi_readl(priv, LOONGSON_SPI_IO_SR2_REG));
> + u8 frame_bytes = priv->frame_bytes;
> + u8 *dst = NULL;
> + u32 data;
> +
> + if (!rxflv)
> + return;
> +
> + if (xfer->rx_buf && rxflv <= xfer->len)
> + dst = (u8 *)xfer->rx_buf + xfer->len - rxflv;
> +
> + /* SPI core skips cache sync while transfer_one() is running. */
> + dma_sync_sgtable_for_cpu(rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
> + loongson_spi_io_set_thresh(priv, false);
> +
> + while (rxflv) {
> + data = loongson_spi_io_read_dr(priv);
> + if (dst) {
> + memcpy(dst, &data, frame_bytes);
> + dst += frame_bytes;
> + }
> + rxflv -= frame_bytes;
> + }
> +
> + dma_sync_sgtable_for_device(rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
> +}
[Severity: High]
Does the final dma_sync_sgtable_for_device(..., DMA_FROM_DEVICE) cause
the drained tail bytes in xfer->rx_buf to be overwritten when
transfer_one() returns?
When loongson_spi_io_transfer_one() returns 0, spi_transfer_one_message()
calls spi_dma_sync_for_cpu(), which runs dma_sync_sgtable_for_cpu(...,
DMA_FROM_DEVICE) on xfer->rx_sg. For a DMA_FROM_DEVICE mapping,
syncing for the device does not copy CPU writes into an active SWIOTLB
bounce buffer (and invalidates cache lines on non-coherent DMA), so the
subsequent sync for the CPU copies the stale bounce buffer back over the
bytes memcpy() just wrote into xfer->rx_buf.
[ ... ]
> @@ -423,6 +499,42 @@ static int loongson_spi_io_setup(struct spi_device *spi)
> return 0;
> }
>
> +/**
> + * loongson_spi_can_dma() - Check whether a transfer can use DMA
> + * @ctrl: SPI controller
> + * @spi: SPI target device
> + * @xfer: SPI transfer to check
> + *
> + * DMA requires both channels, a four-byte-aligned length and a transfer at
> + * least as large as dma_threshold. Three-wire transfers use interrupt mode
> + * because the DMA path requires both TX and RX scatterlists.
> + *
> + * Return: true when DMA is supported for @xfer.
> + */
> +static bool loongson_spi_can_dma(struct spi_controller *ctrl, struct spi_device *spi,
> + struct spi_transfer *xfer)
> +{
> + struct loongson_spi_priv *priv = spi_controller_get_devdata(ctrl);
> +
> + if (!priv->use_dma)
> + return false;
> +
> + /* The DMA path requires both TX and RX scatterlists. */
> + if (spi->mode & SPI_3WIRE)
> + return false;
> +
> + if (xfer->len < dma_threshold)
> + return false;
> +
> + if (!xfer->tx_buf && !xfer->rx_buf)
> + return false;
> +
> + if (xfer->len % 4)
> + return false;
> +
> + return true;
> +}
[Severity: Medium]
Does loongson_spi_can_dma() also need to check that xfer->tx_buf and
xfer->rx_buf are both non-NULL and 4-byte aligned, and that xfer->len is
non-zero?
loongson_spi_dma_config_and_submit() configures DMA_SLAVE_BUSWIDTH_4_BYTES
on both channels. When an unaligned vmalloc buffer is split at page
boundaries by spi_map_buf_attrs(), each scatterlist segment length is
PAGE_SIZE - offset_in_page(buf) (not a multiple of 4), and
loongson2_cmc_dma_prep_slave_sg() rounds each segment up with
DIV_ROUND_UP(sg_dma_len(sg), 4), reading or writing extra bytes across
segment boundaries.
In addition, checking (!xfer->tx_buf && !xfer->rx_buf) allows a transfer
with only one buffer to pass if dummy buffer allocation is not active, and
if dma_threshold is set to 0 via sysfs, a transfer with xfer->len == 0
passes loongson_spi_can_dma() and fails in sg_alloc_table() with -EINVAL.
> @@ -464,14 +576,191 @@ static int loongson_spi_io_start_batch(struct loongson_spi_priv *priv)
> return batch_size;
> }
[ ... ]
> +static int loongson_spi_dma_config_and_submit(struct loongson_spi_priv *priv,
> + struct spi_transfer *xfer)
> +{
[ ... ]
> + rx_desc = dmaengine_prep_slave_sg(priv->dma_rx, xfer->rx_sg.sgl,
> + xfer->rx_sg.nents, DMA_DEV_TO_MEM,
> + DMA_CTRL_ACK);
> + if (!rx_desc) {
> + dev_err(dev, "RX DMA prep_slave_sg failed");
> + return -EINVAL;
> + }
> +
> + ret = dma_submit_error(dmaengine_submit(rx_desc));
> + if (ret) {
> + dev_err(dev, "RX DMA submit failed");
> + return ret;
> + }
> +
> + tx_desc = dmaengine_prep_slave_sg(priv->dma_tx, xfer->tx_sg.sgl,
> + xfer->tx_sg.nents, DMA_MEM_TO_DEV,
> + DMA_CTRL_ACK);
> + if (!tx_desc) {
> + dev_err(dev, "TX DMA prep_slave_sg failed");
> + return -EINVAL;
> + }
> +
> + ret = dma_submit_error(dmaengine_submit(tx_desc));
[Severity: High]
Can rx_desc or tx_desc be an ERR_PTR here, causing dmaengine_submit() to
dereference an error pointer?
The paired DMA engine driver returns ERR_PTR(-ENOMEM) or ERR_PTR(-EINVAL)
on failure rather than NULL:
drivers/dma/loongson/loongson2-apb-cmc-dma.c:loongson2_cmc_dma_prep_slave_sg() {
...
desc = kzalloc_flex(*desc, sg_req, sg_len, GFP_NOWAIT);
if (!desc)
return ERR_PTR(-ENOMEM);
...
}
Using IS_ERR_OR_NULL() on rx_desc and tx_desc avoids dereferencing the
error pointer in dmaengine_submit().
[ ... ]
> +static int loongson_spi_dma_transfer(struct loongson_spi_priv *priv,
> + struct spi_transfer *xfer,
> + bool *bus_started)
> +{
[ ... ]
> +out_stop:
> + loongson_spi_update_bits(priv, LOONGSON_SPI_IO_CR1_REG,
> + LOONGSON_SPI_IO_CR1_CSTART, 0);
> + loongson_spi_update_bits(priv, LOONGSON_SPI_IO_CR2_REG,
> + LOONGSON_SPI_IO_CR2_DMAEN_MASK, 0);
> + priv->xfer_mode = XFER_IDLE;
[Severity: Low]
Should out_stop also clear LOONGSON_SPI_IO_IER_EOTIE in
LOONGSON_SPI_IO_IER_REG?
loongson_spi_io_transfer_one() enables LOONGSON_SPI_IO_IER_EOTIE before
calling loongson_spi_dma_transfer(), and neither the XFER_DMA IRQ path nor
out_stop masks it again, leaving EOT interrupts enabled while
priv->xfer_mode is XFER_IDLE.
[ ... ]
> @@ -497,6 +786,12 @@ static irqreturn_t loongson_spi_io_irq_handler(int irq, void *dev_id)
> loongson_spi_writel(priv, LOONGSON_SPI_IO_SR1_REG, LOONGSON_SPI_IO_SR1_EOT);
>
> switch (priv->xfer_mode) {
> + case XFER_DMA:
> + loongson_spi_update_bits(priv, LOONGSON_SPI_IO_CR1_REG,
> + LOONGSON_SPI_IO_CR1_CSTART, 0);
> + priv->xfer_ret = 0;
> + complete(&priv->xfer_completion);
> + break;
[Severity: Medium]
If loongson_spi_dma_wait_eot() times out, can a late EOT interrupt race
with out_stop on LOONGSON_SPI_IO_CR1_REG and priv->xfer_mode?
CPU0 CPU1 (<hardirq>)
loongson_spi_dma_transfer()
loongson_spi_dma_wait_eot()
/* times out */
out_stop:
loongson_spi_update_bits(...,
LOONGSON_SPI_IO_CR1_REG, ...)
loongson_spi_io_irq_handler()
case XFER_DMA:
loongson_spi_update_bits(...,
LOONGSON_SPI_IO_CR1_REG, ...)
priv->xfer_mode = XFER_IDLE;
Because LOONGSON_SPI_IO_IER_EOTIE is still enabled when
loongson_spi_dma_wait_eot() times out, loongson_spi_io_irq_handler() can
run concurrently with out_stop and perform an unsynchronized
read-modify-write on LOONGSON_SPI_IO_CR1_REG, or fire after
priv->xfer_mode is set to XFER_IDLE.
[ ... ]
> @@ -563,7 +862,27 @@ static int loongson_spi_io_transfer_one(struct spi_controller *ctrl, struct spi_
> loongson_spi_writel(priv, LOONGSON_SPI_IO_SR1_REG,
> LOONGSON_SPI_IO_SR1_EOT | LOONGSON_SPI_IO_SR1_ERR_MASK);
>
> - loongson_spi_io_set_thresh(priv);
> + use_dma = ctrl->can_dma && ctrl->can_dma(ctrl, spi, xfer);
> + loongson_spi_io_set_thresh(priv, use_dma);
[Severity: Medium]
Can calling ctrl->can_dma() again here disagree with the mapping decision
__spi_map_msg() already made for this transfer?
Because dma_threshold is writable at runtime (mode 0644), changing it
between __spi_map_msg() and loongson_spi_io_transfer_one() can make
can_dma() transition from false to true, causing
loongson_spi_dma_config_and_submit() to submit unmapped scatterlists with
nents == 0 (which makes loongson2_cmc_dma_prep_slave_sg() allocate a
0-element sg_req[] array and loongson2_cmc_dma_start_transfer() read
sg_req[0] out of bounds). If dma_threshold is raised instead, the
driver takes the interrupt PIO path while xfer->rx_sg is mapped and
owned by the DMA device, so SPI core's post-transfer
spi_dma_sync_for_cpu() overwrites xfer->rx_buf. Checking
xfer->tx_sg_mapped && xfer->rx_sg_mapped here avoids both mismatches.
> +
> + if (use_dma) {
> + loongson_spi_update_bits(priv, LOONGSON_SPI_IO_SR1_REG,
> + LOONGSON_SPI_IO_SR1_EOT,
> + LOONGSON_SPI_IO_SR1_EOT);
> + loongson_spi_update_bits(priv, LOONGSON_SPI_IO_IER_REG,
> + LOONGSON_SPI_IO_IER_EOTIE,
> + LOONGSON_SPI_IO_IER_EOTIE);
> + ret = loongson_spi_dma_transfer(priv, xfer, &bus_started);
> + if (!ret)
> + return 0;
> + if (bus_started) {
> + dev_err(&spi->dev, "DMA transfer failed: %d", ret);
> + return ret;
> + }
> +
> + dev_dbg(&spi->dev, "DMA setup failed (%d), using interrupt mode", ret);
> + loongson_spi_io_set_thresh(priv, false);
> + }
[Severity: Medium]
When DMA setup fails before bus_started is set, does falling through to
interrupt mode corrupt xfer->rx_buf because xfer->tx_sg and xfer->rx_sg
are still mapped and synced for the DMA device?
spi_transfer_one_message() calls spi_dma_sync_for_device() before
transfer_one() and spi_dma_sync_for_cpu() after transfer_one() returns 0.
If the driver runs PIO in place while xfer->rx_sg_mapped is true, the
post-transfer spi_dma_sync_for_cpu() copies the untouched SWIOTLB bounce
buffer over the PIO-received data in xfer->rx_buf. Setting
xfer->error |= SPI_TRANS_FAIL_NO_START and returning ret lets
spi_transfer_one_message() unmap the scatterlists and retry via its
fallback_pio path.
[ ... ]
> @@ -702,6 +1059,26 @@ static int loongson_spi_io_probe(struct platform_device *pdev)
> host->min_speed_hz = (u32)min_t(u64, DIV_ROUND_UP_ULL(priv->clk_rate, 255),
> U32_MAX);
>
> + priv->dma_tx = loongson_spi_io_request_dma_chan(dev, "tx");
> + if (IS_ERR(priv->dma_tx))
> + return PTR_ERR(priv->dma_tx);
> +
> + priv->dma_rx = loongson_spi_io_request_dma_chan(dev, "rx");
> + if (IS_ERR(priv->dma_rx))
> + return PTR_ERR(priv->dma_rx);
> +
> + if (priv->dma_tx && priv->dma_rx) {
> + priv->use_dma = true;
> + /* Let SPI core provide and map dummy buffers for full-duplex DMA. */
> + host->flags |= SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
> + host->dma_tx = priv->dma_tx;
> + host->dma_rx = priv->dma_rx;
> + host->can_dma = loongson_spi_can_dma;
> + dev_info(dev, "DMA channels acquired");
> + } else {
> + dev_info(dev, "DMA unavailable, using interrupt mode only");
> + }
[Severity: Medium]
If one DMA channel is acquired and the other returns NULL, does the
else branch leave the acquired channel allocated while running in
interrupt-only mode?
Because devm_dma_request_chan() only releases the channel when the device
is unbound, the unused channel remains held for the lifetime of the SPI
controller unless the else branch releases a non-NULL priv->dma_tx or
priv->dma_rx.
--
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1795425389.git.zhoubinbin@loongson.cn?part=4
next prev parent reply other threads:[~2026-11-23 10:17 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 9:44 [PATCH 0/5] spi: loongson: SPI-IO driver for Loongson-2K0300 Binbin Zhou
2026-11-23 9:44 ` [PATCH 1/5] spi: dt-bindings: loongson,ls2k-spi: Document loongson,ls2k0300-spi compatible Binbin Zhou
2026-11-23 9:52 ` sashiko-bot
2026-11-23 9:44 ` [PATCH 2/5] spi: dt-bindings: loongson,ls2k-spi: Document loongson,ls2k0300-spi-io compatible Binbin Zhou
2026-11-23 17:05 ` Conor Dooley
2026-11-23 9:44 ` [PATCH 3/5] spi: loongson-io: Add SPI-IO interrupt-driven driver Binbin Zhou
2026-11-23 10:05 ` sashiko-bot
2026-11-23 9:45 ` [PATCH 4/5] spi: loongson-io: Add DMA support to SPI-IO driver Binbin Zhou
2026-11-23 10:17 ` sashiko-bot [this message]
2026-11-23 9:45 ` [PATCH 5/5] MAINTAINERS: Add myself as Loongson SPI maintainer Binbin Zhou
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-229031@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=robh@kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
--cc=zhoubinbin@loongson.cn \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.