Skip to content

Commit 6098475

Browse files
committed
spi: Fix deadlock when adding SPI controllers on SPI buses
Currently we have a global spi_add_lock which we take when adding new devices so that we can check that we're not trying to reuse a chip select that's already controlled. This means that if the SPI device is itself a SPI controller and triggers the instantiation of further SPI devices we trigger a deadlock as we try to register and instantiate those devices while in the process of doing so for the parent controller and hence already holding the global spi_add_lock. Since we only care about concurrency within a single SPI bus move the lock to be per controller, avoiding the deadlock. This can be easily triggered in the case of spi-mux. Reported-by: Uwe Kleine-König <[email protected]> Signed-off-by: Mark Brown <[email protected]>
1 parent 75b3cb9 commit 6098475

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

drivers/spi/spi.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -478,12 +478,6 @@ static LIST_HEAD(spi_controller_list);
478478
*/
479479
static DEFINE_MUTEX(board_lock);
480480

481-
/*
482-
* Prevents addition of devices with same chip select and
483-
* addition of devices below an unregistering controller.
484-
*/
485-
static DEFINE_MUTEX(spi_add_lock);
486-
487481
/**
488482
* spi_alloc_device - Allocate a new SPI device
489483
* @ctlr: Controller to which device is connected
@@ -636,9 +630,9 @@ int spi_add_device(struct spi_device *spi)
636630
* chipselect **BEFORE** we call setup(), else we'll trash
637631
* its configuration. Lock against concurrent add() calls.
638632
*/
639-
mutex_lock(&spi_add_lock);
633+
mutex_lock(&ctlr->add_lock);
640634
status = __spi_add_device(spi);
641-
mutex_unlock(&spi_add_lock);
635+
mutex_unlock(&ctlr->add_lock);
642636
return status;
643637
}
644638
EXPORT_SYMBOL_GPL(spi_add_device);
@@ -658,7 +652,7 @@ static int spi_add_device_locked(struct spi_device *spi)
658652
/* Set the bus ID string */
659653
spi_dev_set_name(spi);
660654

661-
WARN_ON(!mutex_is_locked(&spi_add_lock));
655+
WARN_ON(!mutex_is_locked(&ctlr->add_lock));
662656
return __spi_add_device(spi);
663657
}
664658

@@ -2830,6 +2824,7 @@ int spi_register_controller(struct spi_controller *ctlr)
28302824
spin_lock_init(&ctlr->bus_lock_spinlock);
28312825
mutex_init(&ctlr->bus_lock_mutex);
28322826
mutex_init(&ctlr->io_mutex);
2827+
mutex_init(&ctlr->add_lock);
28332828
ctlr->bus_lock_flag = 0;
28342829
init_completion(&ctlr->xfer_completion);
28352830
if (!ctlr->max_dma_len)
@@ -2966,7 +2961,7 @@ void spi_unregister_controller(struct spi_controller *ctlr)
29662961

29672962
/* Prevent addition of new devices, unregister existing ones */
29682963
if (IS_ENABLED(CONFIG_SPI_DYNAMIC))
2969-
mutex_lock(&spi_add_lock);
2964+
mutex_lock(&ctlr->add_lock);
29702965

29712966
device_for_each_child(&ctlr->dev, NULL, __unregister);
29722967

@@ -2997,7 +2992,7 @@ void spi_unregister_controller(struct spi_controller *ctlr)
29972992
mutex_unlock(&board_lock);
29982993

29992994
if (IS_ENABLED(CONFIG_SPI_DYNAMIC))
3000-
mutex_unlock(&spi_add_lock);
2995+
mutex_unlock(&ctlr->add_lock);
30012996
}
30022997
EXPORT_SYMBOL_GPL(spi_unregister_controller);
30032998

include/linux/spi/spi.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,9 @@ struct spi_controller {
531531
/* I/O mutex */
532532
struct mutex io_mutex;
533533

534+
/* Used to avoid adding the same CS twice */
535+
struct mutex add_lock;
536+
534537
/* lock and mutex for SPI bus locking */
535538
spinlock_t bus_lock_spinlock;
536539
struct mutex bus_lock_mutex;

0 commit comments

Comments
 (0)