All of lore.kernel.org
 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 v2 3/5] spi: loongson: Add spi-io interrupt-driven driver
Date: Mon, 30 Nov 2026 02:48:03 +0000	[thread overview]
Message-ID: <sashiko-outbox-240266@kernel.org> (raw)
In-Reply-To: <3d3e3f7ec2b784b72020fe7887046c5e5cbbd9f2.1796005812.git.zhoubinbin@loongson.cn>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] spi: loongson: CS glitch in loongson_spi_io_cfg_write() while
  CS is active
- [Medium] spi: loongson: unsynchronized IRQ and active engine on
  timeout in loongson_spi_io_transfer_one_irq()
--

commit 142d6eaee16a385552bbf9c942915b74af0d7e00
Author: Binbin Zhou <zhoubinbin@loongson.cn>

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

This commit adds an interrupt-driven platform driver for the
Loongson-2K0300 SPI-IO controller.

> diff --git a/drivers/spi/spi-loongson-io.c b/drivers/spi/spi-loongson-io.c
> new file mode 100644
> index 0000000000000..2ca9ee7e2d1da
> --- /dev/null
> +++ b/drivers/spi/spi-loongson-io.c

[ ... ]

> +/**
> + * loongson_spi_io_cfg_write() - Write a configuration register
> + * @priv: controller private data
> + * @reg: configuration register offset
> + * @val: value to write
> + *
> + * CFG1/CFG2/CFG3 and CRCx writes are rejected while CR1.SPE is set.
> + * Temporarily disable the peripheral and restore CR1 around the write.
> + * The caller must ensure that no transfer is active. In normal SS mode
> + * ss_o = !SPE, so callers must also ensure CS is deasserted; toggling SPE
> + * while CS is active glitches the CS line.
> + */
> +static void loongson_spi_io_cfg_write(struct loongson_spi_priv *priv, u32 reg, u32 val)
> +{
> +	u32 cr1 = loongson_spi_readl(priv, LOONGSON_SPI_IO_CR1_REG);
> +
> +	loongson_spi_writel(priv, LOONGSON_SPI_IO_CR1_REG,
> +			    cr1 & ~LOONGSON_SPI_IO_CR1_SPE);
> +	loongson_spi_writel(priv, reg, val);
> +	loongson_spi_writel(priv, LOONGSON_SPI_IO_CR1_REG, cr1);
> +}

[ ... ]

> +static void loongson_spi_io_update_state(struct spi_device *spi, struct spi_transfer *t)
> +{
> +	struct loongson_spi_priv *priv = spi_controller_get_devdata(spi->controller);
> +	u8 bits_per_word = spi->bits_per_word;
> +
> +	if (t && t->bits_per_word)
> +		bits_per_word = t->bits_per_word;
> +
> +	if (t && priv->hz != t->speed_hz)
> +		loongson_spi_io_set_clk(priv, t->speed_hz);
> +
> +	if (((spi->mode ^ priv->mode) & (SPI_MODE_X_MASK | SPI_LSB_FIRST)) ||
> +	    priv->bits_per_word != bits_per_word)
> +		loongson_spi_io_set_mode(priv, spi->mode, bits_per_word);
> +}

[Severity: High]
Does calling loongson_spi_io_set_clk() and loongson_spi_io_set_mode()
from loongson_spi_io_transfer_one() glitch the hardware chip-select line?

spi_transfer_one_message() asserts CS via loongson_spi_io_set_cs() and
waits cs_setup before calling transfer_one().  Inside transfer_one(),
loongson_spi_io_set_clk() and loongson_spi_io_set_mode() invoke
loongson_spi_io_cfg_write(), which clears and restores
LOONGSON_SPI_IO_CR1_SPE while CS is active (on the first transfer after
probe and whenever speed_hz, bits_per_word, or spi->mode changes).
As noted above loongson_spi_io_cfg_write(), toggling SPE while CS is
asserted pulses CS high mid-transaction, and also changes CPOL/CPHA in
LOONGSON_SPI_IO_CFG1_REG while the target is selected.

Similarly, loongson_spi_io_unprepare_message() calls
loongson_spi_io_reset_state() -> loongson_spi_io_reginit() ->
loongson_spi_io_cfg_write(), which toggles SPE at the end of a message
even when the final transfer sets cs_change to keep CS active.  In
addition, loongson_spi_io_reginit() sets LOONGSON_SPI_IO_CR1_SPE during
probe without setting LOONGSON_SPI_IO_CR1_SSREV, driving the active-low
CS line low at probe time before any SPI device is set up.

[ ... ]

> +static int loongson_spi_io_transfer_one_irq(struct spi_device *spi, struct spi_transfer *xfer)
> +{
> +	struct loongson_spi_priv *priv = spi_controller_get_devdata(spi->controller);
> +	int ret;
> +
> +	loongson_spi_io_set_thresh(priv);
> +
> +	priv->tx_buf = xfer->tx_buf;
> +	priv->rx_buf = xfer->rx_buf;
> +	priv->remaining = xfer->len;
> +	priv->xfer_ret = 0;
> +
> +	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_ERR_MASK,
> +				 LOONGSON_SPI_IO_IER_EOTIE | LOONGSON_SPI_IO_IER_ERR_MASK);
> +
> +	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]
When wait_for_completion_timeout() times out, can an in-flight
loongson_spi_io_irq_handler() race with the timeout path before
loongson_spi_io_handle_err() runs synchronize_irq()?

CPU0                              CPU1
loongson_spi_io_transfer_one_irq()
  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()
  spi_set_cs()
    loongson_spi_io_set_cs()
      loongson_spi_update_bits(..., CR1_REG, ...)
                                      memcpy(priv->rx_buf, ...)
                                      loongson_spi_update_bits(..., CR1_REG, ...)
                                        <-- data race on rx_buf and CR1_REG

The timeout path returns -ETIMEDOUT without clearing
LOONGSON_SPI_IO_CR1_START_MASK or calling synchronize_irq(priv->irq).
Before spi_transfer_one_message() reaches ctlr->handle_err(), an
in-flight ISR on CPU1 can still write to priv->rx_buf after
spi_dma_sync_for_cpu(), start another batch from priv->tx_buf, or perform
unlocked read-modify-writes on LOONGSON_SPI_IO_IER_REG and
LOONGSON_SPI_IO_CR1_REG concurrently with loongson_spi_io_set_cs().

Also, LOONGSON_SPI_IO_IER_REG is enabled before
loongson_spi_io_start_batch() initializes priv->batch_size, leaving a
window where priv->batch_size holds the previous transfer's final batch
size.

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

  reply	other threads:[~2026-11-30  2:48 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  2:32 [PATCH v2 0/5] spi: loongson: SPI-IO driver for Loongson-2K0300 Binbin Zhou
2026-11-30  2:33 ` [PATCH v2 1/5] spi: dt-bindings: loongson,ls2k-spi: Document loongson,ls2k0300-spi compatible Binbin Zhou
2026-11-30 17:53   ` Conor Dooley
2026-12-01  2:57     ` Binbin Zhou
2026-12-01 18:23       ` Conor Dooley
2026-11-30  2:33 ` [PATCH v2 2/5] spi: dt-bindings: loongson,ls2k-spi: Document loongson,ls2k0300-spi-io compatible Binbin Zhou
2026-11-30  2:33 ` [PATCH v2 3/5] spi: loongson: Add spi-io interrupt-driven driver Binbin Zhou
2026-11-30  2:48   ` sashiko-bot [this message]
2026-11-30  2:33 ` [PATCH v2 4/5] spi: loongson: Add DMA support to SPI-IO driver Binbin Zhou
2026-11-30  2:53   ` sashiko-bot
2026-11-30  2:33 ` [PATCH v2 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-240266@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.