public inbox for linux-spi@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/8] spi: fix explicit controller deregistration
@ 2026-04-14 13:43 Johan Hovold
  2026-04-14 13:43 ` [PATCH 1/8] spi: cadence: fix " Johan Hovold
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: Johan Hovold @ 2026-04-14 13:43 UTC (permalink / raw)
  To: Mark Brown; +Cc: linux-spi, linux-kernel, Johan Hovold

Turns out we have a few drivers that get the tear down ordering wrong
also when not using device managed registration (cf. [1] and [2]).

Fix this to avoid issues like system errors due to unclocked accesses,
NULL-pointer dereferences, hangs or failed I/O during during
deregistration (e.g. when powering down devices).

Johan


[1] https://lore.kernel.org/lkml/20260409120419.388546-2-johan@kernel.org/
[2] https://lore.kernel.org/lkml/20260410081757.503099-1-johan@kernel.org/


Johan Hovold (8):
  spi: cadence: fix controller deregistration
  spi: cadence-quadspi: fix controller deregistration
  spi: mpc52xx: fix controller deregistration
  spi: mpc52xx: fix use-after-free on unbind
  spi: mxic: fix controller deregistration
  spi: orion: fix controller deregistration
  spi: topcliff-pch: fix controller deregistration
  spi: topcliff-pch: fix use-after-free on unbind

 drivers/spi/spi-cadence-quadspi.c |  4 ++--
 drivers/spi/spi-cadence.c         |  6 +++++-
 drivers/spi/spi-mpc52xx.c         |  6 ++++--
 drivers/spi/spi-mxic.c            |  3 ++-
 drivers/spi/spi-orion.c           |  7 ++++++-
 drivers/spi/spi-topcliff-pch.c    | 11 ++++++++---
 6 files changed, 27 insertions(+), 10 deletions(-)

-- 
2.52.0


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH 1/8] spi: cadence: fix controller deregistration
  2026-04-14 13:43 [PATCH 0/8] spi: fix explicit controller deregistration Johan Hovold
@ 2026-04-14 13:43 ` Johan Hovold
  2026-04-14 13:43 ` [PATCH 2/8] spi: cadence-quadspi: " Johan Hovold
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Johan Hovold @ 2026-04-14 13:43 UTC (permalink / raw)
  To: Mark Brown; +Cc: linux-spi, linux-kernel, Johan Hovold, stable, Harini Katakam

Make sure to deregister the controller before disabling underlying
resources like clocks during driver unbind.

Fixes: c474b3866546 ("spi: Add driver for Cadence SPI controller")
Cc: stable@vger.kernel.org	# 3.16
Cc: Harini Katakam <harinik@xilinx.com>
Signed-off-by: Johan Hovold <johan@kernel.org>
---
 drivers/spi/spi-cadence.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/spi/spi-cadence.c b/drivers/spi/spi-cadence.c
index caa7a57e6d27..08d7dabe818d 100644
--- a/drivers/spi/spi-cadence.c
+++ b/drivers/spi/spi-cadence.c
@@ -777,6 +777,10 @@ static void cdns_spi_remove(struct platform_device *pdev)
 	struct spi_controller *ctlr = platform_get_drvdata(pdev);
 	struct cdns_spi *xspi = spi_controller_get_devdata(ctlr);
 
+	spi_controller_get(ctlr);
+
+	spi_unregister_controller(ctlr);
+
 	cdns_spi_write(xspi, CDNS_SPI_ER, CDNS_SPI_ER_DISABLE);
 
 	if (!spi_controller_is_target(ctlr)) {
@@ -784,7 +788,7 @@ static void cdns_spi_remove(struct platform_device *pdev)
 		pm_runtime_set_suspended(&pdev->dev);
 	}
 
-	spi_unregister_controller(ctlr);
+	spi_controller_put(ctlr);
 }
 
 /**
-- 
2.52.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH 2/8] spi: cadence-quadspi: fix controller deregistration
  2026-04-14 13:43 [PATCH 0/8] spi: fix explicit controller deregistration Johan Hovold
  2026-04-14 13:43 ` [PATCH 1/8] spi: cadence: fix " Johan Hovold
@ 2026-04-14 13:43 ` Johan Hovold
  2026-04-14 13:43 ` [PATCH 3/8] spi: mpc52xx: " Johan Hovold
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Johan Hovold @ 2026-04-14 13:43 UTC (permalink / raw)
  To: Mark Brown
  Cc: linux-spi, linux-kernel, Johan Hovold, stable,
	Khairul Anuar Romli

Make sure to deregister the controller before dropping the reference
count that allows new operations to start to allow SPI drivers to do I/O
during deregistration.

Fixes: 7446284023e8 ("spi: cadence-quadspi: Implement refcount to handle unbind during busy")
Cc: stable@vger.kernel.org	# 6.17
Cc: Khairul Anuar Romli <khairul.anuar.romli@altera.com>
Signed-off-by: Johan Hovold <johan@kernel.org>
---
 drivers/spi/spi-cadence-quadspi.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/spi/spi-cadence-quadspi.c b/drivers/spi/spi-cadence-quadspi.c
index 2ead419e896e..50ef65fc5ded 100644
--- a/drivers/spi/spi-cadence-quadspi.c
+++ b/drivers/spi/spi-cadence-quadspi.c
@@ -2020,13 +2020,13 @@ static void cqspi_remove(struct platform_device *pdev)
 
 	ddata = of_device_get_match_data(dev);
 
+	spi_unregister_controller(cqspi->host);
+
 	refcount_set(&cqspi->refcount, 0);
 
 	if (!refcount_dec_and_test(&cqspi->inflight_ops))
 		cqspi_wait_idle(cqspi);
 
-	spi_unregister_controller(cqspi->host);
-
 	if (cqspi->rx_chan)
 		dma_release_channel(cqspi->rx_chan);
 
-- 
2.52.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH 3/8] spi: mpc52xx: fix controller deregistration
  2026-04-14 13:43 [PATCH 0/8] spi: fix explicit controller deregistration Johan Hovold
  2026-04-14 13:43 ` [PATCH 1/8] spi: cadence: fix " Johan Hovold
  2026-04-14 13:43 ` [PATCH 2/8] spi: cadence-quadspi: " Johan Hovold
@ 2026-04-14 13:43 ` Johan Hovold
  2026-04-14 13:43 ` [PATCH 4/8] spi: mpc52xx: fix use-after-free on unbind Johan Hovold
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Johan Hovold @ 2026-04-14 13:43 UTC (permalink / raw)
  To: Mark Brown
  Cc: linux-spi, linux-kernel, Johan Hovold, stable, Grant Likely,
	Luotao Fu

Make sure to deregister the controller before disabling and releasing
underlying resources like interrupts and gpios during driver unbind.

Fixes: 42bbb70980f3 ("powerpc/5200: Add mpc5200-spi (non-PSC) device driver")
Fixes: b8d4e2ce60b6 ("mpc52xx_spi: add gpio chipselect")
Cc: stable@vger.kernel.org	# 2.6.33
Cc: Grant Likely <grant.likely@secretlab.ca>
Cc: Luotao Fu <l.fu@pengutronix.de>
Signed-off-by: Johan Hovold <johan@kernel.org>
---
 drivers/spi/spi-mpc52xx.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/spi/spi-mpc52xx.c b/drivers/spi/spi-mpc52xx.c
index 05bbd3795e7d..823b49f8ece2 100644
--- a/drivers/spi/spi-mpc52xx.c
+++ b/drivers/spi/spi-mpc52xx.c
@@ -517,6 +517,8 @@ static void mpc52xx_spi_remove(struct platform_device *op)
 	struct mpc52xx_spi *ms = spi_controller_get_devdata(host);
 	int i;
 
+	spi_unregister_controller(host);
+
 	cancel_work_sync(&ms->work);
 	free_irq(ms->irq0, ms);
 	free_irq(ms->irq1, ms);
@@ -525,7 +527,6 @@ static void mpc52xx_spi_remove(struct platform_device *op)
 		gpiod_put(ms->gpio_cs[i]);
 
 	kfree(ms->gpio_cs);
-	spi_unregister_controller(host);
 	iounmap(ms->regs);
 	spi_controller_put(host);
 }
-- 
2.52.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH 4/8] spi: mpc52xx: fix use-after-free on unbind
  2026-04-14 13:43 [PATCH 0/8] spi: fix explicit controller deregistration Johan Hovold
                   ` (2 preceding siblings ...)
  2026-04-14 13:43 ` [PATCH 3/8] spi: mpc52xx: " Johan Hovold
@ 2026-04-14 13:43 ` Johan Hovold
  2026-04-14 13:43 ` [PATCH 5/8] spi: mxic: fix controller deregistration Johan Hovold
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Johan Hovold @ 2026-04-14 13:43 UTC (permalink / raw)
  To: Mark Brown; +Cc: linux-spi, linux-kernel, Johan Hovold, stable, Pei Xiao

The state machine work is scheduled by the interrupt handler and
therefore needs to be cancelled after disabling interrupts to avoid a
potential use-after-free.

Fixes: 984836621aad ("spi: mpc52xx: Add cancel_work_sync before module remove")
Cc: stable@vger.kernel.org
Cc: Pei Xiao <xiaopei01@kylinos.cn>
Signed-off-by: Johan Hovold <johan@kernel.org>
---
 drivers/spi/spi-mpc52xx.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/spi/spi-mpc52xx.c b/drivers/spi/spi-mpc52xx.c
index 823b49f8ece2..c8c8e6bdf421 100644
--- a/drivers/spi/spi-mpc52xx.c
+++ b/drivers/spi/spi-mpc52xx.c
@@ -519,10 +519,11 @@ static void mpc52xx_spi_remove(struct platform_device *op)
 
 	spi_unregister_controller(host);
 
-	cancel_work_sync(&ms->work);
 	free_irq(ms->irq0, ms);
 	free_irq(ms->irq1, ms);
 
+	cancel_work_sync(&ms->work);
+
 	for (i = 0; i < ms->gpio_cs_count; i++)
 		gpiod_put(ms->gpio_cs[i]);
 
-- 
2.52.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH 5/8] spi: mxic: fix controller deregistration
  2026-04-14 13:43 [PATCH 0/8] spi: fix explicit controller deregistration Johan Hovold
                   ` (3 preceding siblings ...)
  2026-04-14 13:43 ` [PATCH 4/8] spi: mpc52xx: fix use-after-free on unbind Johan Hovold
@ 2026-04-14 13:43 ` Johan Hovold
  2026-04-14 13:43 ` [PATCH 6/8] spi: orion: " Johan Hovold
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Johan Hovold @ 2026-04-14 13:43 UTC (permalink / raw)
  To: Mark Brown; +Cc: linux-spi, linux-kernel, Johan Hovold, stable, Mason Yang

Make sure to deregister the controller before disabling underlying
resources like clocks (via runtime pm) during driver unbind.

Fixes: b942d80b0a39 ("spi: Add MXIC controller driver")
Cc: stable@vger.kernel.org	# 5.0: cc53711b2191
Cc: stable@vger.kernel.org	# 5.0
Cc: Mason Yang <masonccyang@mxic.com.tw>
Signed-off-by: Johan Hovold <johan@kernel.org>
---
 drivers/spi/spi-mxic.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/spi/spi-mxic.c b/drivers/spi/spi-mxic.c
index f9369c69911c..b0e7fc828a50 100644
--- a/drivers/spi/spi-mxic.c
+++ b/drivers/spi/spi-mxic.c
@@ -832,9 +832,10 @@ static void mxic_spi_remove(struct platform_device *pdev)
 	struct spi_controller *host = platform_get_drvdata(pdev);
 	struct mxic_spi *mxic = spi_controller_get_devdata(host);
 
+	spi_unregister_controller(host);
+
 	pm_runtime_disable(&pdev->dev);
 	mxic_spi_mem_ecc_remove(mxic);
-	spi_unregister_controller(host);
 }
 
 static const struct of_device_id mxic_spi_of_ids[] = {
-- 
2.52.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH 6/8] spi: orion: fix controller deregistration
  2026-04-14 13:43 [PATCH 0/8] spi: fix explicit controller deregistration Johan Hovold
                   ` (4 preceding siblings ...)
  2026-04-14 13:43 ` [PATCH 5/8] spi: mxic: fix controller deregistration Johan Hovold
@ 2026-04-14 13:43 ` Johan Hovold
  2026-04-14 13:43 ` [PATCH 7/8] spi: topcliff-pch: " Johan Hovold
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Johan Hovold @ 2026-04-14 13:43 UTC (permalink / raw)
  To: Mark Brown; +Cc: linux-spi, linux-kernel, Johan Hovold, stable

Make sure to deregister the controller before disabling underlying
resources like clocks during driver unbind.

Fixes: 60cadec9da7b ("spi: new orion_spi driver")
Cc: stable@vger.kernel.org	# 2.6.27
Signed-off-by: Johan Hovold <johan@kernel.org>
---
 drivers/spi/spi-orion.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/drivers/spi/spi-orion.c b/drivers/spi/spi-orion.c
index 7a2186b51b4c..c54cd4ef09bd 100644
--- a/drivers/spi/spi-orion.c
+++ b/drivers/spi/spi-orion.c
@@ -801,10 +801,15 @@ static void orion_spi_remove(struct platform_device *pdev)
 	struct spi_controller *host = platform_get_drvdata(pdev);
 	struct orion_spi *spi = spi_controller_get_devdata(host);
 
+	spi_controller_get(host);
+
+	spi_unregister_controller(host);
+
 	pm_runtime_get_sync(&pdev->dev);
 	clk_disable_unprepare(spi->axi_clk);
 
-	spi_unregister_controller(host);
+	spi_controller_put(host);
+
 	pm_runtime_disable(&pdev->dev);
 }
 
-- 
2.52.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH 7/8] spi: topcliff-pch: fix controller deregistration
  2026-04-14 13:43 [PATCH 0/8] spi: fix explicit controller deregistration Johan Hovold
                   ` (5 preceding siblings ...)
  2026-04-14 13:43 ` [PATCH 6/8] spi: orion: " Johan Hovold
@ 2026-04-14 13:43 ` Johan Hovold
  2026-04-14 13:43 ` [PATCH 8/8] spi: topcliff-pch: fix use-after-free on unbind Johan Hovold
  2026-04-20 11:40 ` [PATCH 0/8] spi: fix explicit controller deregistration Mark Brown
  8 siblings, 0 replies; 10+ messages in thread
From: Johan Hovold @ 2026-04-14 13:43 UTC (permalink / raw)
  To: Mark Brown; +Cc: linux-spi, linux-kernel, Johan Hovold, stable, Masayuki Ohtake

Make sure to deregister the controller before disabling and releasing
underlying resources like interrupts and DMA during driver unbind.

Fixes: e8b17b5b3f30 ("spi/topcliff: Add topcliff platform controller hub (PCH) spi bus driver")
Cc: stable@vger.kernel.org	# 2.6.37
Cc: Masayuki Ohtake <masa-korg@dsn.okisemi.com>
Signed-off-by: Johan Hovold <johan@kernel.org>
---
 drivers/spi/spi-topcliff-pch.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/drivers/spi/spi-topcliff-pch.c b/drivers/spi/spi-topcliff-pch.c
index cae2dcefabea..c120436434d0 100644
--- a/drivers/spi/spi-topcliff-pch.c
+++ b/drivers/spi/spi-topcliff-pch.c
@@ -1406,6 +1406,10 @@ static void pch_spi_pd_remove(struct platform_device *plat_dev)
 	dev_dbg(&plat_dev->dev, "%s:[ch%d] irq=%d\n",
 		__func__, plat_dev->id, board_dat->pdev->irq);
 
+	spi_controller_get(data->host);
+
+	spi_unregister_controller(data->host);
+
 	if (use_dma)
 		pch_free_dma_buf(board_dat, data);
 
@@ -1433,7 +1437,8 @@ static void pch_spi_pd_remove(struct platform_device *plat_dev)
 	}
 
 	pci_iounmap(board_dat->pdev, data->io_remap_addr);
-	spi_unregister_controller(data->host);
+
+	spi_controller_put(data->host);
 }
 #ifdef CONFIG_PM
 static int pch_spi_pd_suspend(struct platform_device *pd_dev,
-- 
2.52.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH 8/8] spi: topcliff-pch: fix use-after-free on unbind
  2026-04-14 13:43 [PATCH 0/8] spi: fix explicit controller deregistration Johan Hovold
                   ` (6 preceding siblings ...)
  2026-04-14 13:43 ` [PATCH 7/8] spi: topcliff-pch: " Johan Hovold
@ 2026-04-14 13:43 ` Johan Hovold
  2026-04-20 11:40 ` [PATCH 0/8] spi: fix explicit controller deregistration Mark Brown
  8 siblings, 0 replies; 10+ messages in thread
From: Johan Hovold @ 2026-04-14 13:43 UTC (permalink / raw)
  To: Mark Brown; +Cc: linux-spi, linux-kernel, Johan Hovold, stable, Tomoya MORINAGA

Give the driver a chance to flush its queue before releasing the DMA
buffers on driver unbind

Fixes: c37f3c2749b5 ("spi/topcliff_pch: DMA support")
Cc: stable@vger.kernel.org	# 3.1
Cc: Tomoya MORINAGA <tomoya-linux@dsn.okisemi.com>
Signed-off-by: Johan Hovold <johan@kernel.org>
---
 drivers/spi/spi-topcliff-pch.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/spi/spi-topcliff-pch.c b/drivers/spi/spi-topcliff-pch.c
index c120436434d0..14d11450e86d 100644
--- a/drivers/spi/spi-topcliff-pch.c
+++ b/drivers/spi/spi-topcliff-pch.c
@@ -1410,9 +1410,6 @@ static void pch_spi_pd_remove(struct platform_device *plat_dev)
 
 	spi_unregister_controller(data->host);
 
-	if (use_dma)
-		pch_free_dma_buf(board_dat, data);
-
 	/* check for any pending messages; no action is taken if the queue
 	 * is still full; but at least we tried.  Unload anyway */
 	count = 500;
@@ -1436,6 +1433,9 @@ static void pch_spi_pd_remove(struct platform_device *plat_dev)
 		free_irq(board_dat->pdev->irq, data);
 	}
 
+	if (use_dma)
+		pch_free_dma_buf(board_dat, data);
+
 	pci_iounmap(board_dat->pdev, data->io_remap_addr);
 
 	spi_controller_put(data->host);
-- 
2.52.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH 0/8] spi: fix explicit controller deregistration
  2026-04-14 13:43 [PATCH 0/8] spi: fix explicit controller deregistration Johan Hovold
                   ` (7 preceding siblings ...)
  2026-04-14 13:43 ` [PATCH 8/8] spi: topcliff-pch: fix use-after-free on unbind Johan Hovold
@ 2026-04-20 11:40 ` Mark Brown
  8 siblings, 0 replies; 10+ messages in thread
From: Mark Brown @ 2026-04-20 11:40 UTC (permalink / raw)
  To: Johan Hovold; +Cc: linux-spi, linux-kernel

On Tue, 14 Apr 2026 15:43:11 +0200, Johan Hovold wrote:
> spi: fix explicit controller deregistration
> 
> Turns out we have a few drivers that get the tear down ordering wrong
> also when not using device managed registration (cf. [1] and [2]).
> 
> Fix this to avoid issues like system errors due to unclocked accesses,
> NULL-pointer dereferences, hangs or failed I/O during during
> deregistration (e.g. when powering down devices).
> 
> [...]

Applied to

   https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git for-7.1

Thanks!

[1/8] spi: cadence: fix controller deregistration
      https://git.kernel.org/broonie/spi/c/666fa7e9ca98
[2/8] spi: cadence-quadspi: fix controller deregistration
      https://git.kernel.org/broonie/spi/c/964ee9793760
[3/8] spi: mpc52xx: fix controller deregistration
      https://git.kernel.org/broonie/spi/c/0f997fdae819
[4/8] spi: mpc52xx: fix use-after-free on unbind
      https://git.kernel.org/broonie/spi/c/706b3dc2ac7a
[5/8] spi: mxic: fix controller deregistration
      https://git.kernel.org/broonie/spi/c/adbc595e2720
[6/8] spi: orion: fix controller deregistration
      https://git.kernel.org/broonie/spi/c/220f4f11104a
[7/8] spi: topcliff-pch: fix controller deregistration
      https://git.kernel.org/broonie/spi/c/5d6f477d6fc0
[8/8] spi: topcliff-pch: fix use-after-free on unbind
      https://git.kernel.org/broonie/spi/c/9d72732fe70c

All being well this means that it will be integrated into the linux-next
tree (usually sometime in the next 24 hours) and sent to Linus during
the next merge window (or sooner if it is a bug fix), however if
problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing
and review of the tree, please engage with people reporting problems and
send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they
should be sent as incremental updates against current git, existing
patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying
to this mail.

Thanks,
Mark


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2026-04-20 16:45 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-14 13:43 [PATCH 0/8] spi: fix explicit controller deregistration Johan Hovold
2026-04-14 13:43 ` [PATCH 1/8] spi: cadence: fix " Johan Hovold
2026-04-14 13:43 ` [PATCH 2/8] spi: cadence-quadspi: " Johan Hovold
2026-04-14 13:43 ` [PATCH 3/8] spi: mpc52xx: " Johan Hovold
2026-04-14 13:43 ` [PATCH 4/8] spi: mpc52xx: fix use-after-free on unbind Johan Hovold
2026-04-14 13:43 ` [PATCH 5/8] spi: mxic: fix controller deregistration Johan Hovold
2026-04-14 13:43 ` [PATCH 6/8] spi: orion: " Johan Hovold
2026-04-14 13:43 ` [PATCH 7/8] spi: topcliff-pch: " Johan Hovold
2026-04-14 13:43 ` [PATCH 8/8] spi: topcliff-pch: fix use-after-free on unbind Johan Hovold
2026-04-20 11:40 ` [PATCH 0/8] spi: fix explicit controller deregistration Mark Brown

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox