aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark Brown <broonie@kernel.org>2016-07-21 18:53:31 -0400
committerMark Brown <broonie@kernel.org>2016-07-25 06:47:52 -0400
commitef4d96ec4ad947360f48677b6007a4c77953b090 (patch)
tree3596ca87c41742a64b444d8c129389298fc30921
parentf4502dd1da9b060a49d539eb754ff86cb97b89f0 (diff)
spi: Split bus and I/O locking
The current SPI code attempts to use bus_lock_mutex for two purposes. One is to implement spi_bus_lock() which grants exclusive access to the bus. The other is to serialize access to the physical hardware. This duplicate purpose causes confusion which leads to cases where access is not locked when a caller holds the bus lock mutex. Fix this by splitting out the I/O functionality into a new io_mutex. This means taking both mutexes in the DMA path, replacing the existing mutex with the new I/O one in the message pump (the mutex now always being taken in the message pump) and taking the bus lock mutex in spi_sync(), allowing __spi_sync() to have no mutex handling. While we're at it hoist the mutex further up the message pump before we power up the device so that all power up/down of the block is covered by it and there are no races with in-line pumping of messages. Reported-by: Rich Felker <dalias@libc.org> Tested-by: Rich Felker <dalias@libc.org> Signed-off-by: Mark Brown <broonie@kernel.org>
-rw-r--r--drivers/spi/spi.c38
-rw-r--r--include/linux/spi/spi.h6
2 files changed, 23 insertions, 21 deletions
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index c9a8d544e467..d2e7f1350ef6 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -1069,7 +1069,6 @@ EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
1069 * __spi_pump_messages - function which processes spi message queue 1069 * __spi_pump_messages - function which processes spi message queue
1070 * @master: master to process queue for 1070 * @master: master to process queue for
1071 * @in_kthread: true if we are in the context of the message pump thread 1071 * @in_kthread: true if we are in the context of the message pump thread
1072 * @bus_locked: true if the bus mutex is held when calling this function
1073 * 1072 *
1074 * This function checks if there is any spi message in the queue that 1073 * This function checks if there is any spi message in the queue that
1075 * needs processing and if so call out to the driver to initialize hardware 1074 * needs processing and if so call out to the driver to initialize hardware
@@ -1079,8 +1078,7 @@ EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
1079 * inside spi_sync(); the queue extraction handling at the top of the 1078 * inside spi_sync(); the queue extraction handling at the top of the
1080 * function should deal with this safely. 1079 * function should deal with this safely.
1081 */ 1080 */
1082static void __spi_pump_messages(struct spi_master *master, bool in_kthread, 1081static void __spi_pump_messages(struct spi_master *master, bool in_kthread)
1083 bool bus_locked)
1084{ 1082{
1085 unsigned long flags; 1083 unsigned long flags;
1086 bool was_busy = false; 1084 bool was_busy = false;
@@ -1152,6 +1150,8 @@ static void __spi_pump_messages(struct spi_master *master, bool in_kthread,
1152 master->busy = true; 1150 master->busy = true;
1153 spin_unlock_irqrestore(&master->queue_lock, flags); 1151 spin_unlock_irqrestore(&master->queue_lock, flags);
1154 1152
1153 mutex_lock(&master->io_mutex);
1154
1155 if (!was_busy && master->auto_runtime_pm) { 1155 if (!was_busy && master->auto_runtime_pm) {
1156 ret = pm_runtime_get_sync(master->dev.parent); 1156 ret = pm_runtime_get_sync(master->dev.parent);
1157 if (ret < 0) { 1157 if (ret < 0) {
@@ -1176,9 +1176,6 @@ static void __spi_pump_messages(struct spi_master *master, bool in_kthread,
1176 } 1176 }
1177 } 1177 }
1178 1178
1179 if (!bus_locked)
1180 mutex_lock(&master->bus_lock_mutex);
1181
1182 trace_spi_message_start(master->cur_msg); 1179 trace_spi_message_start(master->cur_msg);
1183 1180
1184 if (master->prepare_message) { 1181 if (master->prepare_message) {
@@ -1208,8 +1205,7 @@ static void __spi_pump_messages(struct spi_master *master, bool in_kthread,
1208 } 1205 }
1209 1206
1210out: 1207out:
1211 if (!bus_locked) 1208 mutex_unlock(&master->io_mutex);
1212 mutex_unlock(&master->bus_lock_mutex);
1213 1209
1214 /* Prod the scheduler in case transfer_one() was busy waiting */ 1210 /* Prod the scheduler in case transfer_one() was busy waiting */
1215 if (!ret) 1211 if (!ret)
@@ -1225,7 +1221,7 @@ static void spi_pump_messages(struct kthread_work *work)
1225 struct spi_master *master = 1221 struct spi_master *master =
1226 container_of(work, struct spi_master, pump_messages); 1222 container_of(work, struct spi_master, pump_messages);
1227 1223
1228 __spi_pump_messages(master, true, master->bus_lock_flag); 1224 __spi_pump_messages(master, true);
1229} 1225}
1230 1226
1231static int spi_init_queue(struct spi_master *master) 1227static int spi_init_queue(struct spi_master *master)
@@ -1887,6 +1883,7 @@ int spi_register_master(struct spi_master *master)
1887 spin_lock_init(&master->queue_lock); 1883 spin_lock_init(&master->queue_lock);
1888 spin_lock_init(&master->bus_lock_spinlock); 1884 spin_lock_init(&master->bus_lock_spinlock);
1889 mutex_init(&master->bus_lock_mutex); 1885 mutex_init(&master->bus_lock_mutex);
1886 mutex_init(&master->io_mutex);
1890 master->bus_lock_flag = 0; 1887 master->bus_lock_flag = 0;
1891 init_completion(&master->xfer_completion); 1888 init_completion(&master->xfer_completion);
1892 if (!master->max_dma_len) 1889 if (!master->max_dma_len)
@@ -2767,6 +2764,7 @@ int spi_flash_read(struct spi_device *spi,
2767 } 2764 }
2768 2765
2769 mutex_lock(&master->bus_lock_mutex); 2766 mutex_lock(&master->bus_lock_mutex);
2767 mutex_lock(&master->io_mutex);
2770 if (master->dma_rx) { 2768 if (master->dma_rx) {
2771 rx_dev = master->dma_rx->device->dev; 2769 rx_dev = master->dma_rx->device->dev;
2772 ret = spi_map_buf(master, rx_dev, &msg->rx_sg, 2770 ret = spi_map_buf(master, rx_dev, &msg->rx_sg,
@@ -2779,6 +2777,7 @@ int spi_flash_read(struct spi_device *spi,
2779 if (msg->cur_msg_mapped) 2777 if (msg->cur_msg_mapped)
2780 spi_unmap_buf(master, rx_dev, &msg->rx_sg, 2778 spi_unmap_buf(master, rx_dev, &msg->rx_sg,
2781 DMA_FROM_DEVICE); 2779 DMA_FROM_DEVICE);
2780 mutex_unlock(&master->io_mutex);
2782 mutex_unlock(&master->bus_lock_mutex); 2781 mutex_unlock(&master->bus_lock_mutex);
2783 2782
2784 if (master->auto_runtime_pm) 2783 if (master->auto_runtime_pm)
@@ -2800,8 +2799,7 @@ static void spi_complete(void *arg)
2800 complete(arg); 2799 complete(arg);
2801} 2800}
2802 2801
2803static int __spi_sync(struct spi_device *spi, struct spi_message *message, 2802static int __spi_sync(struct spi_device *spi, struct spi_message *message)
2804 int bus_locked)
2805{ 2803{
2806 DECLARE_COMPLETION_ONSTACK(done); 2804 DECLARE_COMPLETION_ONSTACK(done);
2807 int status; 2805 int status;
@@ -2819,9 +2817,6 @@ static int __spi_sync(struct spi_device *spi, struct spi_message *message,
2819 SPI_STATISTICS_INCREMENT_FIELD(&master->statistics, spi_sync); 2817 SPI_STATISTICS_INCREMENT_FIELD(&master->statistics, spi_sync);
2820 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync); 2818 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync);
2821 2819
2822 if (!bus_locked)
2823 mutex_lock(&master->bus_lock_mutex);
2824
2825 /* If we're not using the legacy transfer method then we will 2820 /* If we're not using the legacy transfer method then we will
2826 * try to transfer in the calling context so special case. 2821 * try to transfer in the calling context so special case.
2827 * This code would be less tricky if we could remove the 2822 * This code would be less tricky if we could remove the
@@ -2839,9 +2834,6 @@ static int __spi_sync(struct spi_device *spi, struct spi_message *message,
2839 status = spi_async_locked(spi, message); 2834 status = spi_async_locked(spi, message);
2840 } 2835 }
2841 2836
2842 if (!bus_locked)
2843 mutex_unlock(&master->bus_lock_mutex);
2844
2845 if (status == 0) { 2837 if (status == 0) {
2846 /* Push out the messages in the calling context if we 2838 /* Push out the messages in the calling context if we
2847 * can. 2839 * can.
@@ -2851,7 +2843,7 @@ static int __spi_sync(struct spi_device *spi, struct spi_message *message,
2851 spi_sync_immediate); 2843 spi_sync_immediate);
2852 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, 2844 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics,
2853 spi_sync_immediate); 2845 spi_sync_immediate);
2854 __spi_pump_messages(master, false, bus_locked); 2846 __spi_pump_messages(master, false);
2855 } 2847 }
2856 2848
2857 wait_for_completion(&done); 2849 wait_for_completion(&done);
@@ -2884,7 +2876,13 @@ static int __spi_sync(struct spi_device *spi, struct spi_message *message,
2884 */ 2876 */
2885int spi_sync(struct spi_device *spi, struct spi_message *message) 2877int spi_sync(struct spi_device *spi, struct spi_message *message)
2886{ 2878{
2887 return __spi_sync(spi, message, spi->master->bus_lock_flag); 2879 int ret;
2880
2881 mutex_lock(&spi->master->bus_lock_mutex);
2882 ret = __spi_sync(spi, message);
2883 mutex_unlock(&spi->master->bus_lock_mutex);
2884
2885 return ret;
2888} 2886}
2889EXPORT_SYMBOL_GPL(spi_sync); 2887EXPORT_SYMBOL_GPL(spi_sync);
2890 2888
@@ -2906,7 +2904,7 @@ EXPORT_SYMBOL_GPL(spi_sync);
2906 */ 2904 */
2907int spi_sync_locked(struct spi_device *spi, struct spi_message *message) 2905int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
2908{ 2906{
2909 return __spi_sync(spi, message, 1); 2907 return __spi_sync(spi, message);
2910} 2908}
2911EXPORT_SYMBOL_GPL(spi_sync_locked); 2909EXPORT_SYMBOL_GPL(spi_sync_locked);
2912 2910
diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h
index 7b53af4ba5f8..072cb2aa2413 100644
--- a/include/linux/spi/spi.h
+++ b/include/linux/spi/spi.h
@@ -312,8 +312,9 @@ static inline void spi_unregister_driver(struct spi_driver *sdrv)
312 * @flags: other constraints relevant to this driver 312 * @flags: other constraints relevant to this driver
313 * @max_transfer_size: function that returns the max transfer size for 313 * @max_transfer_size: function that returns the max transfer size for
314 * a &spi_device; may be %NULL, so the default %SIZE_MAX will be used. 314 * a &spi_device; may be %NULL, so the default %SIZE_MAX will be used.
315 * @io_mutex: mutex for physical bus access
315 * @bus_lock_spinlock: spinlock for SPI bus locking 316 * @bus_lock_spinlock: spinlock for SPI bus locking
316 * @bus_lock_mutex: mutex for SPI bus locking 317 * @bus_lock_mutex: mutex for exclusion of multiple callers
317 * @bus_lock_flag: indicates that the SPI bus is locked for exclusive use 318 * @bus_lock_flag: indicates that the SPI bus is locked for exclusive use
318 * @setup: updates the device mode and clocking records used by a 319 * @setup: updates the device mode and clocking records used by a
319 * device's SPI controller; protocol code may call this. This 320 * device's SPI controller; protocol code may call this. This
@@ -446,6 +447,9 @@ struct spi_master {
446 */ 447 */
447 size_t (*max_transfer_size)(struct spi_device *spi); 448 size_t (*max_transfer_size)(struct spi_device *spi);
448 449
450 /* I/O mutex */
451 struct mutex io_mutex;
452
449 /* lock and mutex for SPI bus locking */ 453 /* lock and mutex for SPI bus locking */
450 spinlock_t bus_lock_spinlock; 454 spinlock_t bus_lock_spinlock;
451 struct mutex bus_lock_mutex; 455 struct mutex bus_lock_mutex;