History

spi: add defer_optimize_message controller flag

Adding spi_optimize_message() broke the spi-mux driver because it calls spi_async() from it's transfer_one_message() callback. This resulted in passing an incorrectly optimized message to the controller. For example, if the underlying controller has an optimize_message() callback, this would have not been called and can cause a crash when the underlying controller driver tries to transfer the message. Also, since the spi-mux driver swaps out the controller pointer by replacing msg->spi, __spi_unoptimize_message() was being called with a different controller than the one used in __spi_optimize_message(). This could cause a crash when attempting to free the message resources when __spi_unoptimize_message() is called in spi_finalize_current_message() since it is being called with a controller that did not allocate the resources. This is fixed by adding a defer_optimize_message flag for controllers. This flag causes all of the spi_[maybe_][un]optimize_message() calls to be a no-op (other than attaching a pointer to the spi device to the message). This allows the spi-mux driver to pass an unmodified message to spi_async() in spi_mux_transfer_one_message() after the spi device has been swapped out. This causes __spi_optimize_message() and __spi_unoptimize_message() to be called only once per message and with the correct/same controller in each case.

David Lechner committed 2 years ago

Commit ca52aa41 parent c86a9183 files · +22 −1Browse files

Unified · Split

3 files
  1. drivers/spi/spi-mux.c +1 −0
  2. drivers/spi/spi.c +17 −1
  3. include/linux/spi/spi.h +4 −0
drivers/spi/spi-mux.c+1−0
@@ -164,6 +164,7 @@ static int spi_mux_probe(struct spi_device *spi) · 163 unmodified lines
164164 ctlr->bus_num = -1;
165165 ctlr->dev.of_node = spi->dev.of_node;
166166 ctlr->must_async = true;
167+ ctlr->defer_optimize_message = true;
167168
168169 ret = devm_spi_register_controller(&spi->dev, ctlr);
169170 if (ret)
drivers/spi/spi.c+17−1
@@ -2151,7 +2151,8 @@ static void __spi_unoptimize_message(struct spi_message *msg) · 2150 unmodified lines
21512151 */
21522152 static void spi_maybe_unoptimize_message(struct spi_message *msg)
21532153 {
2154− if (!msg->pre_optimized && msg->optimized)
2154+ if (!msg->pre_optimized && msg->optimized &&
2155+ !msg->spi->controller->defer_optimize_message)
21552156 __spi_unoptimize_message(msg);
21562157 }
21572158
@@ -4294,6 +4295,11 @@ static int __spi_optimize_message(struct spi_device *spi, · 2136 unmodified lines
42944295 static int spi_maybe_optimize_message(struct spi_device *spi,
42954296 struct spi_message *msg)
42964297 {
4298+ if (spi->controller->defer_optimize_message) {
4299+ msg->spi = spi;
4300+ return 0;
4301+ }
4302+
42974303 if (msg->pre_optimized)
42984304 return 0;
42994305
@@ -4324,6 +4330,13 @@ int spi_optimize_message(struct spi_device *spi, struct spi_message *msg) · 24 unmodified lines
43244330 {
43254331 int ret;
43264332
4333+ /*
4334+ * Pre-optimization is not supported and optimization is deferred e.g.
4335+ * when using spi-mux.
4336+ */
4337+ if (spi->controller->defer_optimize_message)
4338+ return 0;
4339+
43274340 ret = __spi_optimize_message(spi, msg);
43284341 if (ret)
43294342 return ret;
@@ -4350,6 +4363,9 @@ EXPORT_SYMBOL_GPL(spi_optimize_message); · 20 unmodified lines
43504363 */
43514364 void spi_unoptimize_message(struct spi_message *msg)
43524365 {
4366+ if (msg->spi->controller->defer_optimize_message)
4367+ return;
4368+
43534369 __spi_unoptimize_message(msg);
43544370 msg->pre_optimized = false;
43554371 }
include/linux/spi/spi.h+4−0
@@ -533,6 +533,9 @@ extern struct spi_device *spi_new_ancillary_device(struct spi_device *spi, u8 ch · 532 unmodified lines
533533 * @queue_empty: signal green light for opportunistically skipping the queue
534534 * for spi_sync transfers.
535535 * @must_async: disable all fast paths in the core
536+ * @defer_optimize_message: set to true if controller cannot pre-optimize messages
537+ * and needs to defer the optimization step until the message is actually
538+ * being transferred
536539 *
537540 * Each SPI controller can communicate with one or more @spi_device
538541 * children. These make a small bus, sharing MOSI, MISO and SCK signals
@@ -776,6 +779,7 @@ struct spi_controller { · 237 unmodified lines
776779 /* Flag for enabling opportunistic skipping of the queue in spi_sync */
777780 bool queue_empty;
778781 bool must_async;
782+ bool defer_optimize_message;
779783 };
780784
781785 static inline void *spi_controller_get_devdata(struct spi_controller *ctlr)