Linux SPI subsystem development
 help / color / mirror / Atom feed
From: Lee Jones <lee@kernel.org>
To: lee@kernel.org, Mark Brown <broonie@kernel.org>,
	Florian Fainelli <florian.fainelli@broadcom.com>,
	Ray Jui <rjui@broadcom.com>,
	Scott Branden <sbranden@broadcom.com>,
	Broadcom internal kernel review list
	<bcm-kernel-feedback-list@broadcom.com>,
	Gabor Juhos <juhosg@openwrt.org>, Chris Boot <bootc@bootc.net>,
	Linus Walleij <linusw@kernel.org>,
	Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>,
	linux-spi@vger.kernel.org, linux-rpi-kernel@lists.infradead.org,
	linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org
Cc: stable@vger.kernel.org
Subject: [PATCH 02/10] spi: bcm2835: Store native chip-select GPIO descriptor per device
Date: Thu, 19 Nov 2026 09:17:14 +0000	[thread overview]
Message-ID: <20261119091810.3440619-3-lee@kernel.org> (raw)
In-Reply-To: <20261119091810.3440619-1-lee@kernel.org>

When a device uses a native chip-select, bcm2835_spi_setup() requests a
GPIO descriptor for it and stores the result in the controller-wide
bs->cs_gpio.  A second device using a native chip-select overwrites the
first device's descriptor, which is then leaked.

bcm2835_spi_cleanup() subsequently calls gpiod_put(bs->cs_gpio) for
every device, including devices whose chip-select was requested by the
SPI core from the cs-gpios property.  The same descriptor can therefore
be released several times, and the descriptor released is not
necessarily the one which belongs to the device being cleaned up.

Move the descriptor into the per-device struct bcm2835_spidev, which is
zero-initialised, so that each device releases only the descriptor it
requested itself.

Fixes: 21f252cd29f0 ("spi: bcm2835: reduce the abuse of the GPIO API")
Closes: https://sashiko.dev/#/bug/linux-677b4cdb-2c6c-4cad-8a5a-45a24dc5cbab
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---
 drivers/spi/spi-bcm2835.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/drivers/spi/spi-bcm2835.c b/drivers/spi/spi-bcm2835.c
index 9d589df90933..af118cccee1b 100644
--- a/drivers/spi/spi-bcm2835.c
+++ b/drivers/spi/spi-bcm2835.c
@@ -85,7 +85,6 @@ MODULE_PARM_DESC(polling_limit_us,
  * struct bcm2835_spi - BCM2835 SPI controller
  * @regs: base address of register map
  * @clk: core clock, divided to calculate serial clock
- * @cs_gpio: chip-select GPIO descriptor
  * @clk_hz: core clock cached speed
  * @irq: interrupt, signals TX FIFO empty or RX FIFO ¾ full
  * @tfr: SPI transfer currently processed
@@ -120,7 +119,6 @@ MODULE_PARM_DESC(polling_limit_us,
 struct bcm2835_spi {
 	void __iomem *regs;
 	struct clk *clk;
-	struct gpio_desc *cs_gpio;
 	unsigned long clk_hz;
 	int irq;
 	struct spi_transfer *tfr;
@@ -153,6 +151,7 @@ struct bcm2835_spi {
  * @clear_rx_desc: preallocated RX DMA descriptor used for TX-only transfers
  *	(cyclically clears RX FIFO by writing @clear_rx_cs to CS register)
  * @clear_rx_addr: bus address of @clear_rx_cs
+ * @cs_gpio: native chip-select GPIO descriptor requested by the driver
  * @clear_rx_cs: precalculated CS register value to clear RX FIFO
  *	(uses target-specific clock polarity and phase settings)
  */
@@ -160,6 +159,7 @@ struct bcm2835_spidev {
 	u32 prepare_cs;
 	struct dma_async_tx_descriptor *clear_rx_desc;
 	dma_addr_t clear_rx_addr;
+	struct gpio_desc *cs_gpio;
 	u32 clear_rx_cs ____cacheline_aligned;
 };
 
@@ -1157,7 +1157,6 @@ static void bcm2835_spi_cleanup(struct spi_device *spi)
 {
 	struct bcm2835_spidev *target = spi_get_ctldata(spi);
 	struct spi_controller *ctlr = spi->controller;
-	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
 
 	if (target->clear_rx_desc)
 		dmaengine_desc_free(target->clear_rx_desc);
@@ -1168,8 +1167,7 @@ static void bcm2835_spi_cleanup(struct spi_device *spi)
 				 sizeof(u32),
 				 DMA_TO_DEVICE);
 
-	if (!IS_ERR(bs->cs_gpio))
-		gpiod_put(bs->cs_gpio);
+	gpiod_put(target->cs_gpio);
 	spi_set_csgpiod(spi, 0, NULL);
 
 	kfree(target);
@@ -1232,6 +1230,7 @@ static int bcm2835_spi_setup(struct spi_device *spi)
 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
 	struct bcm2835_spidev *target = spi_get_ctldata(spi);
 	struct gpiod_lookup_table *lookup __free(kfree) = NULL;
+	struct gpio_desc *cs_gpio;
 	static const char * const pinctrl_compats[] = {
 		"brcm,bcm2835-gpio",
 		"brcm,bcm2711-gpio",
@@ -1334,14 +1333,15 @@ static int bcm2835_spi_setup(struct spi_device *spi)
 
 	gpiod_add_lookup_table(lookup);
 
-	bs->cs_gpio = gpiod_get(&spi->dev, "cs", GPIOD_OUT_LOW);
+	cs_gpio = gpiod_get(&spi->dev, "cs", GPIOD_OUT_LOW);
 	gpiod_remove_lookup_table(lookup);
-	if (IS_ERR(bs->cs_gpio)) {
-		ret = PTR_ERR(bs->cs_gpio);
+	if (IS_ERR(cs_gpio)) {
+		ret = PTR_ERR(cs_gpio);
 		goto err_cleanup;
 	}
 
-	spi_set_csgpiod(spi, 0, bs->cs_gpio);
+	target->cs_gpio = cs_gpio;
+	spi_set_csgpiod(spi, 0, cs_gpio);
 
 	/* and set up the "mode" and level */
 	dev_info(&spi->dev, "setting up native-CS%i to use GPIO\n",
-- 
2.56.1.708.g77570a6a1d-goog


  parent reply	other threads:[~2026-11-19  9:18 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19  9:17 [PATCH 00/10] spi: Fix data corruption, uninitialised data leaks and bounds checks Lee Jones
2026-11-19  9:17 ` [PATCH 01/10] spi: fsl-cpm: Map the zero page's address, not its struct page Lee Jones
2026-11-19  9:17 ` Lee Jones [this message]
2026-11-19  9:17 ` [PATCH 03/10] spi: ch341: Limit transfers to the hardware packet size Lee Jones
2026-11-19  9:17 ` [PATCH 04/10] spi: sprd-adi: Do not copy uninitialised data to rx_buf on read failure Lee Jones
2026-11-19  9:17 ` [PATCH 05/10] spi: topcliff-pch: Advance tx_buf between DMA chunks Lee Jones
2026-11-19  9:17 ` [PATCH 06/10] spi: cadence-quadspi: Bound chip select against f_pdata array size Lee Jones
2026-11-19 10:15   ` Théo Lebrun
2026-11-19  9:17 ` [PATCH 07/10] spi: cadence-xspi: Zero the tail word in m_iowriteq() Lee Jones
2026-11-19  9:17 ` [PATCH 08/10] spi: fsl-qspi: Propagate clock errors from fsl_qspi_select_mem() Lee Jones
2026-11-19  9:17 ` [PATCH 09/10] spi: cadence-xspi: Advance tx pointer between Marvell b0 chunks Lee Jones
2026-11-19  9:17 ` [PATCH 10/10] spi: cadence-xspi: Skip tail read for qword-aligned Marvell b0 receives Lee Jones

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=20261119091810.3440619-3-lee@kernel.org \
    --to=lee@kernel.org \
    --cc=bartosz.golaszewski@oss.qualcomm.com \
    --cc=bcm-kernel-feedback-list@broadcom.com \
    --cc=bootc@bootc.net \
    --cc=broonie@kernel.org \
    --cc=florian.fainelli@broadcom.com \
    --cc=juhosg@openwrt.org \
    --cc=linusw@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rpi-kernel@lists.infradead.org \
    --cc=linux-spi@vger.kernel.org \
    --cc=rjui@broadcom.com \
    --cc=sbranden@broadcom.com \
    --cc=stable@vger.kernel.org \
    /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