linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH AUTOSEL 4.14 01/18] spi: Fix spi device unregister flow
@ 2021-06-03 17:10 Sasha Levin
  2021-06-03 17:10 ` [PATCH AUTOSEL 4.14 02/18] net/nfc/rawsock.c: fix a permission check bug Sasha Levin
                   ` (16 more replies)
  0 siblings, 17 replies; 18+ messages in thread
From: Sasha Levin @ 2021-06-03 17:10 UTC (permalink / raw)
  To: linux-kernel, stable; +Cc: Saravana Kannan, Mark Brown, Sasha Levin, linux-spi

From: Saravana Kannan <saravanak@google.com>

[ Upstream commit c7299fea67696db5bd09d924d1f1080d894f92ef ]

When an SPI device is unregistered, the spi->controller->cleanup() is
called in the device's release callback. That's wrong for a couple of
reasons:

1. spi_dev_put() can be called before spi_add_device() is called. And
   it's spi_add_device() that calls spi_setup(). This will cause clean()
   to get called without the spi device ever being setup.

2. There's no guarantee that the controller's driver would be present by
   the time the spi device's release function gets called.

3. It also causes "sleeping in atomic context" stack dump[1] when device
   link deletion code does a put_device() on the spi device.

Fix these issues by simply moving the cleanup from the device release
callback to the actual spi_unregister_device() function.

[1] - https://lore.kernel.org/lkml/CAHp75Vc=FCGcUyS0v6fnxme2YJ+qD+Y-hQDQLa2JhWNON9VmsQ@mail.gmail.com/

Signed-off-by: Saravana Kannan <saravanak@google.com>
Link: https://lore.kernel.org/r/20210426235638.1285530-1-saravanak@google.com
Signed-off-by: Mark Brown <broonie@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 drivers/spi/spi.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index da71a53b0df7..49f39c54d679 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -52,10 +52,6 @@ static void spidev_release(struct device *dev)
 {
 	struct spi_device	*spi = to_spi_device(dev);
 
-	/* spi controllers may cleanup for released devices */
-	if (spi->controller->cleanup)
-		spi->controller->cleanup(spi);
-
 	spi_controller_put(spi->controller);
 	kfree(spi);
 }
@@ -501,6 +497,12 @@ static int spi_dev_check(struct device *dev, void *data)
 	return 0;
 }
 
+static void spi_cleanup(struct spi_device *spi)
+{
+	if (spi->controller->cleanup)
+		spi->controller->cleanup(spi);
+}
+
 /**
  * spi_add_device - Add spi_device allocated with spi_alloc_device
  * @spi: spi_device to register
@@ -562,11 +564,13 @@ int spi_add_device(struct spi_device *spi)
 
 	/* Device may be bound to an active driver when this returns */
 	status = device_add(&spi->dev);
-	if (status < 0)
+	if (status < 0) {
 		dev_err(dev, "can't add %s, status %d\n",
 				dev_name(&spi->dev), status);
-	else
+		spi_cleanup(spi);
+	} else {
 		dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
+	}
 
 done:
 	mutex_unlock(&spi_add_lock);
@@ -653,6 +657,8 @@ void spi_unregister_device(struct spi_device *spi)
 	if (!spi)
 		return;
 
+	spi_cleanup(spi);
+
 	if (spi->dev.of_node) {
 		of_node_clear_flag(spi->dev.of_node, OF_POPULATED);
 		of_node_put(spi->dev.of_node);
-- 
2.30.2


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

end of thread, other threads:[~2021-06-03 17:16 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-03 17:10 [PATCH AUTOSEL 4.14 01/18] spi: Fix spi device unregister flow Sasha Levin
2021-06-03 17:10 ` [PATCH AUTOSEL 4.14 02/18] net/nfc/rawsock.c: fix a permission check bug Sasha Levin
2021-06-03 17:10 ` [PATCH AUTOSEL 4.14 03/18] ASoC: sti-sas: add missing MODULE_DEVICE_TABLE Sasha Levin
2021-06-03 17:10 ` [PATCH AUTOSEL 4.14 04/18] isdn: mISDN: netjet: Fix crash in nj_probe: Sasha Levin
2021-06-03 17:10 ` [PATCH AUTOSEL 4.14 05/18] bonding: init notify_work earlier to avoid uninitialized use Sasha Levin
2021-06-03 17:10 ` [PATCH AUTOSEL 4.14 06/18] netlink: disable IRQs for netlink_lock_table() Sasha Levin
2021-06-03 17:10 ` [PATCH AUTOSEL 4.14 07/18] net: mdiobus: get rid of a BUG_ON() Sasha Levin
2021-06-03 17:10 ` [PATCH AUTOSEL 4.14 08/18] cgroup: disable controllers at parse time Sasha Levin
2021-06-03 17:10 ` [PATCH AUTOSEL 4.14 09/18] wq: handle VM suspension in stall detection Sasha Levin
2021-06-03 17:10 ` [PATCH AUTOSEL 4.14 10/18] net/qla3xxx: fix schedule while atomic in ql_sem_spinlock Sasha Levin
2021-06-03 17:10 ` [PATCH AUTOSEL 4.14 11/18] scsi: vmw_pvscsi: Set correct residual data length Sasha Levin
2021-06-03 17:10 ` [PATCH AUTOSEL 4.14 12/18] scsi: target: qla2xxx: Wait for stop_phase1 at WWN removal Sasha Levin
2021-06-03 17:10 ` [PATCH AUTOSEL 4.14 13/18] net: macb: ensure the device is available before accessing GEMGXL control registers Sasha Levin
2021-06-03 17:10 ` [PATCH AUTOSEL 4.14 14/18] net: appletalk: cops: Fix data race in cops_probe1 Sasha Levin
2021-06-03 17:10 ` [PATCH AUTOSEL 4.14 15/18] MIPS: Fix kernel hang under FUNCTION_GRAPH_TRACER and PREEMPT_TRACER Sasha Levin
2021-06-03 17:10 ` [PATCH AUTOSEL 4.14 16/18] bnx2x: Fix missing error code in bnx2x_iov_init_one() Sasha Levin
2021-06-03 17:10 ` [PATCH AUTOSEL 4.14 17/18] powerpc/fsl: set fsl,i2c-erratum-a004447 flag for P2041 i2c controllers Sasha Levin
2021-06-03 17:10 ` [PATCH AUTOSEL 4.14 18/18] powerpc/fsl: set fsl,i2c-erratum-a004447 flag for P1010 " Sasha Levin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).