Devicetree
 help / color / mirror / Atom feed
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 3/5] spi: loongson-io: Add SPI-IO interrupt-driven driver
Date: Mon, 23 Nov 2026 10:05:28 +0000	[thread overview]
Message-ID: <sashiko-outbox-229025@kernel.org> (raw)
In-Reply-To: <010b8f2cd618854142124c6418f4e9ccb80fb852.1795425389.git.zhoubinbin@loongson.cn>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] spi: loongson-io: missing error IRQ enables and uncleared SR1
  in ISR
- [High] spi: loongson-io: 32-bit divisor truncation in
  loongson_spi_io_timeout()
- [Medium] spi: loongson-io: IRQ enabled before hardware and batch init
  in probe/xfer
- [Medium] spi: loongson-io: unsynchronized timeout in
  loongson_spi_io_transfer_one()
- [Low] spi: loongson-io: inverted @high CS contract in
  loongson_spi_io_set_cs()
--

commit 7a2b450ad49a89d82f040f8bc7f2f42d5aadeb60
Author: Binbin Zhou <zhoubinbin@loongson.cn>

spi: loongson-io: Add SPI-IO interrupt-driven driver

This commit adds an interrupt-driven platform driver for the Loongson-2K0300
SPI-IO controller (SPI2 and SPI3).

> diff --git a/drivers/spi/spi-loongson-io.c b/drivers/spi/spi-loongson-io.c
> new file mode 100644
> index 0000000000000..e273c9f4a9430
> --- /dev/null
> +++ b/drivers/spi/spi-loongson-io.c
> @@ -0,0 +1,737 @@

[ ... ]

> +static unsigned long loongson_spi_io_timeout(struct loongson_spi_priv *priv,
> +					     struct spi_transfer *xfer)
> +{
> +	u64 frames = xfer->len / priv->frame_bytes;
> +	u64 nsec = frames * priv->bits_per_word * NSEC_PER_SEC;
> +	u32 rate = priv->hz ? priv->hz : 1000;
> +	u64 msec = DIV_ROUND_UP_ULL(nsec, (u64)rate * NSEC_PER_MSEC);
> +
> +	return msecs_to_jiffies(max_t(u64, msec * 2, 1000));
> +}

[Severity: High]
Does DIV_ROUND_UP_ULL() truncate the 64-bit divisor (u64)rate * NSEC_PER_MSEC
to 32 bits?

DIV_ROUND_UP_ULL() expands to do_div(), which casts its divisor to uint32_t.
For any SPI clock rate >= 4,295 Hz, (u64)rate * NSEC_PER_MSEC exceeds
U32_MAX, corrupting the timeout calculation, and when rate is a multiple of
67,108,864 Hz (2^26 Hz), the truncated 32-bit divisor becomes zero and causes
a division-by-zero panic.  In addition, because transfers run in 4-byte IRQ
batches, computing the timeout from ideal wire speed alone leaves large
high-speed transfers clamped at the 1000 ms floor despite per-batch interrupt
and MMIO overhead; using DIV64_U64_ROUND_UP() avoids the divisor truncation.

[ ... ]

> +/**
> + * loongson_spi_io_set_cs() - Set the hardware chip-select output
> + * @spi: SPI target device
> + * @high: true to assert chip select
> + *
> + * The SSREV bit controls the hardware chip-select assertion state.
> + */
> +static void loongson_spi_io_set_cs(struct spi_device *spi, bool high)
> +{
> +	struct loongson_spi_priv *priv = spi_controller_get_devdata(spi->controller);
> +	u32 val = high ? LOONGSON_SPI_IO_CR1_SSREV : 0;
> +
> +	loongson_spi_update_bits(priv, LOONGSON_SPI_IO_CR1_REG,
> +				 LOONGSON_SPI_IO_CR1_SSREV, val);
> +}

[Severity: Low]
Is the @high polarity in loongson_spi_io_set_cs() inverted relative to the
SPI core's set_cs() contract?

Because SPI_CS_HIGH is not set in host->mode_bits, spi_set_cs() passes
high == false (logic low) to assert an active-low chip select and
high == true (logic high) to deassert it.  If LOONGSON_SPI_IO_CR1_SSREV
asserts chip select as the comment states, the ternary condition is inverted;
if it drives the CS line high, the kernel-doc comment is inverted.

[ ... ]

> +static irqreturn_t loongson_spi_io_irq_handler(int irq, void *dev_id)
> +{
> +	struct loongson_spi_priv *priv = dev_id;
> +	u32 rx_data;
> +	u32 sr1;
> +
> +	sr1 = loongson_spi_readl(priv, LOONGSON_SPI_IO_SR1_REG);
> +
> +	if (sr1 & LOONGSON_SPI_IO_SR1_ERR_MASK) {
> +		dev_err(&priv->controller->dev, "SPI error: SR1=0x%x", sr1);
> +		loongson_spi_writel(priv, LOONGSON_SPI_IO_IER_REG, 0);
> +		priv->xfer_ret = -EIO;
> +		complete(&priv->xfer_completion);
> +		return IRQ_HANDLED;
> +	}

[Severity: High]
Does the error path in loongson_spi_io_irq_handler() leave
LOONGSON_SPI_IO_SR1_ERR_MASK latched in LOONGSON_SPI_IO_SR1_REG while none of
the error interrupt bits are enabled in LOONGSON_SPI_IO_IER_REG?

loongson_spi_io_transfer_one() only enables LOONGSON_SPI_IO_IER_EOTIE, so an
overrun, underrun, mode fault, or CRC error that halts a transfer before EOT
never raises an interrupt and stalls until the transfer timeout.  Conversely,
because loongson_spi_io_irq_handler() does not mask sr1 against
LOONGSON_SPI_IO_IER_REG and does not write LOONGSON_SPI_IO_SR1_ERR_MASK back
to LOONGSON_SPI_IO_SR1_REG to clear the status bits, any latched error bit
causes every subsequent interrupt from another device on the shared IRQ line
to re-enter this error branch, log an error, and complete
priv->xfer_completion while idle.

[ ... ]

> +	reinit_completion(&priv->xfer_completion);
> +	priv->xfer_mode = XFER_INTERRUPT;
> +
> +	loongson_spi_update_bits(priv, LOONGSON_SPI_IO_CR1_REG,
> +				 LOONGSON_SPI_IO_CR1_AUTOSUS,
> +				 LOONGSON_SPI_IO_CR1_AUTOSUS);
> +	loongson_spi_update_bits(priv, LOONGSON_SPI_IO_IER_REG,
> +				 LOONGSON_SPI_IO_IER_EOTIE,
> +				 LOONGSON_SPI_IO_IER_EOTIE);
> +
> +	loongson_spi_io_start_batch(priv);
> +
> +	ret = wait_for_completion_timeout(&priv->xfer_completion,
> +					  loongson_spi_io_timeout(priv, xfer));
> +	priv->xfer_mode = XFER_IDLE;
> +
> +	if (ret == 0) {
> +		loongson_spi_writel(priv, LOONGSON_SPI_IO_IER_REG, 0);
> +		dev_err(&spi->dev, "Interrupt transfer timeout");
> +		return -ETIMEDOUT;
> +	}
> +
> +	return priv->xfer_ret;
> +}

[Severity: Medium]
Can a timeout in loongson_spi_io_transfer_one() race with an in-flight
loongson_spi_io_irq_handler() on another CPU?

CPU0                              CPU1
loongson_spi_io_transfer_one()
  wait_for_completion_timeout()
  /* times out */
                                  loongson_spi_io_irq_handler()
                                    switch (priv->xfer_mode)
                                    case XFER_INTERRUPT:
  priv->xfer_mode = XFER_IDLE;
  loongson_spi_writel(..., IER_REG, 0);
  return -ETIMEDOUT;
spi_transfer_one_message()
  spi_dma_sync_for_cpu()
                                      memcpy(priv->rx_buf, ...)
                                      loongson_spi_io_start_batch()
                                        loongson_spi_update_bits(CR1_REG)
  spi_set_cs()
    loongson_spi_io_set_cs()
      loongson_spi_update_bits(CR1_REG)  <-- unlocked RMW race
  loongson_spi_io_handle_err()
    synchronize_irq(priv->irq)

Because loongson_spi_io_transfer_one() returns -ETIMEDOUT without a spinlock
or synchronize_irq(priv->irq), spi_transfer_one_message() runs
spi_dma_sync_for_cpu() and loongson_spi_io_set_cs() before calling
loongson_spi_io_handle_err().  A concurrent ISR that already read
XFER_INTERRUPT can touch priv->rx_buf and priv->tx_buf, start another hardware
batch, and perform an unlocked read-modify-write on LOONGSON_SPI_IO_CR1_REG
concurrently with loongson_spi_io_set_cs(), clobbering
LOONGSON_SPI_IO_CR1_SSREV or LOONGSON_SPI_IO_CR1_CSTART.

[ ... ]

> +	ret = devm_request_irq(dev, priv->irq, loongson_spi_io_irq_handler,
> +			       IRQF_SHARED, dev_name(dev), priv);
> +	if (ret)
> +		return dev_err_probe(dev, ret, "failed to request IRQ");
> +
> +	loongson_spi_io_reginit(priv);
> +	loongson_spi_writel(priv, LOONGSON_SPI_IO_IER_REG, 0);

[Severity: Medium]
Can an interrupt on the shared IRQ line fire in loongson_spi_io_probe() before
the controller registers are initialized and LOONGSON_SPI_IO_IER_REG is
masked?

devm_request_irq() registers the IRQF_SHARED handler before
loongson_spi_io_reginit() runs and before LOONGSON_SPI_IO_IER_REG is cleared
(and without clearing stale status flags in LOONGSON_SPI_IO_SR1_REG).
Similarly, in loongson_spi_io_transfer_one(), priv->xfer_mode = XFER_INTERRUPT
and LOONGSON_SPI_IO_IER_EOTIE are set before loongson_spi_io_start_batch()
initializes priv->batch_size and clears LOONGSON_SPI_IO_SR1_EOT, leaving a
window where a shared or stale IRQ sees the previous transfer's
priv->batch_size.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1795425389.git.zhoubinbin@loongson.cn?part=3

  reply	other threads:[~2026-11-23 10:05 UTC|newest]

Thread overview: 9+ 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  9:44 ` [PATCH 3/5] spi: loongson-io: Add SPI-IO interrupt-driven driver Binbin Zhou
2026-11-23 10:05   ` sashiko-bot [this message]
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
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-229025@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox