* [PATCH 0/2] spi: fix resource leaks on device setup failure
@ 2026-04-10 15:49 Johan Hovold
2026-04-10 15:49 ` [PATCH 1/2] " Johan Hovold
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Johan Hovold @ 2026-04-10 15:49 UTC (permalink / raw)
To: Mark Brown; +Cc: Saravana Kannan, linux-spi, linux-kernel, Johan Hovold
Make sure to call controller cleanup() if spi_setup() fails while
registering a device to avoid leaking any resources allocated by
setup().
Johan
Changes in v2:
- only call cleanup() if spi_setup() fails during registration
- use spi_cleanup() helper
- fix controller cleanup() documentation (new patch)
Johan Hovold (2):
spi: fix resource leaks on device setup failure
spi: fix controller cleanup() documentation
drivers/spi/spi.c | 61 +++++++++++++++++++++++++----------------
include/linux/spi/spi.h | 2 +-
2 files changed, 38 insertions(+), 25 deletions(-)
--
2.52.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 1/2] spi: fix resource leaks on device setup failure
2026-04-10 15:49 [PATCH 0/2] spi: fix resource leaks on device setup failure Johan Hovold
@ 2026-04-10 15:49 ` Johan Hovold
2026-04-10 15:49 ` [PATCH 2/2] spi: fix controller cleanup() documentation Johan Hovold
2026-04-10 15:51 ` [PATCH 0/2] spi: fix resource leaks on device setup failure Johan Hovold
2 siblings, 0 replies; 4+ messages in thread
From: Johan Hovold @ 2026-04-10 15:49 UTC (permalink / raw)
To: Mark Brown; +Cc: Saravana Kannan, linux-spi, linux-kernel, Johan Hovold, stable
Make sure to call controller cleanup() if spi_setup() fails while
registering a device to avoid leaking any resources allocated by
setup().
Fixes: c7299fea6769 ("spi: Fix spi device unregister flow")
Cc: stable@vger.kernel.org # 5.13
Cc: Saravana Kannan <saravanak@kernel.org>
Signed-off-by: Johan Hovold <johan@kernel.org>
---
drivers/spi/spi.c | 61 ++++++++++++++++++++++++++++-------------------
1 file changed, 37 insertions(+), 24 deletions(-)
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 272b45d6b5aa..c14f29457c29 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -43,6 +43,8 @@ EXPORT_TRACEPOINT_SYMBOL(spi_transfer_stop);
#include "internals.h"
+static int __spi_setup(struct spi_device *spi, bool initial_setup);
+
static DEFINE_IDR(spi_controller_idr);
static void spidev_release(struct device *dev)
@@ -726,7 +728,7 @@ static int __spi_add_device(struct spi_device *spi)
* normally rely on the device being setup. Devices
* using SPI_CS_HIGH can't coexist well otherwise...
*/
- status = spi_setup(spi);
+ status = __spi_setup(spi, true);
if (status < 0) {
dev_err(dev, "can't setup %s, status %d\n",
dev_name(&spi->dev), status);
@@ -3992,27 +3994,7 @@ static int spi_set_cs_timing(struct spi_device *spi)
return status;
}
-/**
- * spi_setup - setup SPI mode and clock rate
- * @spi: the device whose settings are being modified
- * Context: can sleep, and no requests are queued to the device
- *
- * SPI protocol drivers may need to update the transfer mode if the
- * device doesn't work with its default. They may likewise need
- * to update clock rates or word sizes from initial values. This function
- * changes those settings, and must be called from a context that can sleep.
- * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
- * effect the next time the device is selected and data is transferred to
- * or from it. When this function returns, the SPI device is deselected.
- *
- * Note that this call will fail if the protocol driver specifies an option
- * that the underlying controller or its driver does not support. For
- * example, not all hardware supports wire transfers using nine bit words,
- * LSB-first wire encoding, or active-high chipselects.
- *
- * Return: zero on success, else a negative error code.
- */
-int spi_setup(struct spi_device *spi)
+static int __spi_setup(struct spi_device *spi, bool initial_setup)
{
unsigned bad_bits, ugly_bits;
int status;
@@ -4097,7 +4079,7 @@ int spi_setup(struct spi_device *spi)
status = spi_set_cs_timing(spi);
if (status) {
mutex_unlock(&spi->controller->io_mutex);
- return status;
+ goto err_cleanup;
}
if (spi->controller->auto_runtime_pm && spi->controller->set_cs) {
@@ -4106,7 +4088,7 @@ int spi_setup(struct spi_device *spi)
mutex_unlock(&spi->controller->io_mutex);
dev_err(&spi->controller->dev, "Failed to power device: %d\n",
status);
- return status;
+ goto err_cleanup;
}
/*
@@ -4142,6 +4124,37 @@ int spi_setup(struct spi_device *spi)
status);
return status;
+
+err_cleanup:
+ if (initial_setup)
+ spi_cleanup(spi);
+
+ return status;
+}
+
+/**
+ * spi_setup - setup SPI mode and clock rate
+ * @spi: the device whose settings are being modified
+ * Context: can sleep, and no requests are queued to the device
+ *
+ * SPI protocol drivers may need to update the transfer mode if the
+ * device doesn't work with its default. They may likewise need
+ * to update clock rates or word sizes from initial values. This function
+ * changes those settings, and must be called from a context that can sleep.
+ * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
+ * effect the next time the device is selected and data is transferred to
+ * or from it. When this function returns, the SPI device is deselected.
+ *
+ * Note that this call will fail if the protocol driver specifies an option
+ * that the underlying controller or its driver does not support. For
+ * example, not all hardware supports wire transfers using nine bit words,
+ * LSB-first wire encoding, or active-high chipselects.
+ *
+ * Return: zero on success, else a negative error code.
+ */
+int spi_setup(struct spi_device *spi)
+{
+ return __spi_setup(spi, false);
}
EXPORT_SYMBOL_GPL(spi_setup);
--
2.52.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH 2/2] spi: fix controller cleanup() documentation
2026-04-10 15:49 [PATCH 0/2] spi: fix resource leaks on device setup failure Johan Hovold
2026-04-10 15:49 ` [PATCH 1/2] " Johan Hovold
@ 2026-04-10 15:49 ` Johan Hovold
2026-04-10 15:51 ` [PATCH 0/2] spi: fix resource leaks on device setup failure Johan Hovold
2 siblings, 0 replies; 4+ messages in thread
From: Johan Hovold @ 2026-04-10 15:49 UTC (permalink / raw)
To: Mark Brown; +Cc: Saravana Kannan, linux-spi, linux-kernel, Johan Hovold
The controller cleanup() callback is no longer called when releasing a
device, but rather when deregistering it (and on registration failures).
Fixes: c7299fea6769 ("spi: Fix spi device unregister flow")
Cc: Saravana Kannan <saravanak@kernel.org>
Signed-off-by: Johan Hovold <johan@kernel.org>
---
include/linux/spi/spi.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h
index 0dc671c07d3a..0de636484f3d 100644
--- a/include/linux/spi/spi.h
+++ b/include/linux/spi/spi.h
@@ -700,7 +700,7 @@ struct spi_controller {
int (*transfer)(struct spi_device *spi,
struct spi_message *mesg);
- /* Called on release() to free memory provided by spi_controller */
+ /* Called on deregistration to free memory provided by spi_controller */
void (*cleanup)(struct spi_device *spi);
/*
--
2.52.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH 0/2] spi: fix resource leaks on device setup failure
2026-04-10 15:49 [PATCH 0/2] spi: fix resource leaks on device setup failure Johan Hovold
2026-04-10 15:49 ` [PATCH 1/2] " Johan Hovold
2026-04-10 15:49 ` [PATCH 2/2] spi: fix controller cleanup() documentation Johan Hovold
@ 2026-04-10 15:51 ` Johan Hovold
2 siblings, 0 replies; 4+ messages in thread
From: Johan Hovold @ 2026-04-10 15:51 UTC (permalink / raw)
To: Mark Brown; +Cc: Saravana Kannan, linux-spi, linux-kernel
On Fri, Apr 10, 2026 at 05:49:05PM +0200, Johan Hovold wrote:
> Make sure to call controller cleanup() if spi_setup() fails while
> registering a device to avoid leaking any resources allocated by
> setup().
> Changes in v2:
> - only call cleanup() if spi_setup() fails during registration
> - use spi_cleanup() helper
> - fix controller cleanup() documentation (new patch)
Bah, I missed to add "v2" to Subject. Let me know if you prefer me to
send a v3...
Johan
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-04-10 15:51 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-10 15:49 [PATCH 0/2] spi: fix resource leaks on device setup failure Johan Hovold
2026-04-10 15:49 ` [PATCH 1/2] " Johan Hovold
2026-04-10 15:49 ` [PATCH 2/2] spi: fix controller cleanup() documentation Johan Hovold
2026-04-10 15:51 ` [PATCH 0/2] spi: fix resource leaks on device setup failure Johan Hovold
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox