Skip to content

Commit a2c817c

Browse files
jhovoldgregkh
authored andcommitted
spi: fix resource leaks on device setup failure
[ Upstream commit db35703 ] Make sure to call controller cleanup() if spi_setup() fails while registering a device to avoid leaking any resources allocated by setup(). Fixes: c7299fe ("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> Link: https://patch.msgid.link/20260410154907.129248-2-johan@kernel.org Signed-off-by: Mark Brown <broonie@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 4c46413 commit a2c817c

1 file changed

Lines changed: 37 additions & 24 deletions

File tree

drivers/spi/spi.c

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ EXPORT_TRACEPOINT_SYMBOL(spi_transfer_stop);
4242

4343
#include "internals.h"
4444

45+
static int __spi_setup(struct spi_device *spi, bool initial_setup);
46+
4547
static DEFINE_IDR(spi_master_idr);
4648

4749
static void spidev_release(struct device *dev)
@@ -677,7 +679,7 @@ static int __spi_add_device(struct spi_device *spi)
677679
* normally rely on the device being setup. Devices
678680
* using SPI_CS_HIGH can't coexist well otherwise...
679681
*/
680-
status = spi_setup(spi);
682+
status = __spi_setup(spi, true);
681683
if (status < 0) {
682684
dev_err(dev, "can't setup %s, status %d\n",
683685
dev_name(&spi->dev), status);
@@ -3734,27 +3736,7 @@ static int spi_set_cs_timing(struct spi_device *spi)
37343736
return status;
37353737
}
37363738

3737-
/**
3738-
* spi_setup - setup SPI mode and clock rate
3739-
* @spi: the device whose settings are being modified
3740-
* Context: can sleep, and no requests are queued to the device
3741-
*
3742-
* SPI protocol drivers may need to update the transfer mode if the
3743-
* device doesn't work with its default. They may likewise need
3744-
* to update clock rates or word sizes from initial values. This function
3745-
* changes those settings, and must be called from a context that can sleep.
3746-
* Except for SPI_CS_HIGH, which takes effect immediately, the changes take
3747-
* effect the next time the device is selected and data is transferred to
3748-
* or from it. When this function returns, the SPI device is deselected.
3749-
*
3750-
* Note that this call will fail if the protocol driver specifies an option
3751-
* that the underlying controller or its driver does not support. For
3752-
* example, not all hardware supports wire transfers using nine bit words,
3753-
* LSB-first wire encoding, or active-high chipselects.
3754-
*
3755-
* Return: zero on success, else a negative error code.
3756-
*/
3757-
int spi_setup(struct spi_device *spi)
3739+
static int __spi_setup(struct spi_device *spi, bool initial_setup)
37583740
{
37593741
unsigned bad_bits, ugly_bits;
37603742
int status = 0;
@@ -3833,7 +3815,7 @@ int spi_setup(struct spi_device *spi)
38333815
status = spi_set_cs_timing(spi);
38343816
if (status) {
38353817
mutex_unlock(&spi->controller->io_mutex);
3836-
return status;
3818+
goto err_cleanup;
38373819
}
38383820

38393821
if (spi->controller->auto_runtime_pm && spi->controller->set_cs) {
@@ -3842,7 +3824,7 @@ int spi_setup(struct spi_device *spi)
38423824
mutex_unlock(&spi->controller->io_mutex);
38433825
dev_err(&spi->controller->dev, "Failed to power device: %d\n",
38443826
status);
3845-
return status;
3827+
goto err_cleanup;
38463828
}
38473829

38483830
/*
@@ -3879,6 +3861,37 @@ int spi_setup(struct spi_device *spi)
38793861
status);
38803862

38813863
return status;
3864+
3865+
err_cleanup:
3866+
if (initial_setup)
3867+
spi_cleanup(spi);
3868+
3869+
return status;
3870+
}
3871+
3872+
/**
3873+
* spi_setup - setup SPI mode and clock rate
3874+
* @spi: the device whose settings are being modified
3875+
* Context: can sleep, and no requests are queued to the device
3876+
*
3877+
* SPI protocol drivers may need to update the transfer mode if the
3878+
* device doesn't work with its default. They may likewise need
3879+
* to update clock rates or word sizes from initial values. This function
3880+
* changes those settings, and must be called from a context that can sleep.
3881+
* Except for SPI_CS_HIGH, which takes effect immediately, the changes take
3882+
* effect the next time the device is selected and data is transferred to
3883+
* or from it. When this function returns, the SPI device is deselected.
3884+
*
3885+
* Note that this call will fail if the protocol driver specifies an option
3886+
* that the underlying controller or its driver does not support. For
3887+
* example, not all hardware supports wire transfers using nine bit words,
3888+
* LSB-first wire encoding, or active-high chipselects.
3889+
*
3890+
* Return: zero on success, else a negative error code.
3891+
*/
3892+
int spi_setup(struct spi_device *spi)
3893+
{
3894+
return __spi_setup(spi, false);
38823895
}
38833896
EXPORT_SYMBOL_GPL(spi_setup);
38843897

0 commit comments

Comments
 (0)