Skip to content

Commit 6c53b45

Browse files
mwallebroonie
authored andcommitted
spi: fix use-after-free of the add_lock mutex
Commit 6098475 ("spi: Fix deadlock when adding SPI controllers on SPI buses") introduced a per-controller mutex. But mutex_unlock() of said lock is called after the controller is already freed: spi_unregister_controller(ctlr) -> put_device(&ctlr->dev) -> spi_controller_release(dev) -> mutex_unlock(&ctrl->add_lock) Move the put_device() after the mutex_unlock(). Fixes: 6098475 ("spi: Fix deadlock when adding SPI controllers on SPI buses") Signed-off-by: Michael Walle <[email protected]> Reviewed-by: Uwe Kleine-König <[email protected]> Reviewed-by: Lukas Wunner <[email protected]> Cc: [email protected] # v5.15 Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Mark Brown <[email protected]>
1 parent 6532582 commit 6c53b45

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

drivers/spi/spi.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3058,12 +3058,6 @@ void spi_unregister_controller(struct spi_controller *ctlr)
30583058

30593059
device_del(&ctlr->dev);
30603060

3061-
/* Release the last reference on the controller if its driver
3062-
* has not yet been converted to devm_spi_alloc_master/slave().
3063-
*/
3064-
if (!ctlr->devm_allocated)
3065-
put_device(&ctlr->dev);
3066-
30673061
/* free bus id */
30683062
mutex_lock(&board_lock);
30693063
if (found == ctlr)
@@ -3072,6 +3066,12 @@ void spi_unregister_controller(struct spi_controller *ctlr)
30723066

30733067
if (IS_ENABLED(CONFIG_SPI_DYNAMIC))
30743068
mutex_unlock(&ctlr->add_lock);
3069+
3070+
/* Release the last reference on the controller if its driver
3071+
* has not yet been converted to devm_spi_alloc_master/slave().
3072+
*/
3073+
if (!ctlr->devm_allocated)
3074+
put_device(&ctlr->dev);
30753075
}
30763076
EXPORT_SYMBOL_GPL(spi_unregister_controller);
30773077

0 commit comments

Comments
 (0)