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.
Unified · Split
| @@ -164,6 +164,7 @@ static int spi_mux_probe(struct spi_device *spi) · 163 unmodified lines | |||
| 164 | ctlr->bus_num = -1; | 164 | ctlr->bus_num = -1; |
| 165 | ctlr->dev.of_node = spi->dev.of_node; | 165 | ctlr->dev.of_node = spi->dev.of_node; |
| 166 | ctlr->must_async = true; | 166 | ctlr->must_async = true; |
| 167 | ctlr->defer_optimize_message = true; | ||
| 167 | 168 | ||
| 168 | ret = devm_spi_register_controller(&spi->dev, ctlr); | 169 | ret = devm_spi_register_controller(&spi->dev, ctlr); |
| 169 | if (ret) | 170 | if (ret) |
| @@ -2151,7 +2151,8 @@ static void __spi_unoptimize_message(struct spi_message *msg) · 2150 unmodified lines | |||
| 2151 | */ | 2151 | */ |
| 2152 | static void spi_maybe_unoptimize_message(struct spi_message *msg) | 2152 | static void spi_maybe_unoptimize_message(struct spi_message *msg) |
| 2153 | { | 2153 | { |
| 2154 | if (!msg->pre_optimized && msg->optimized) | 2154 | if (!msg->pre_optimized && msg->optimized && |
| 2155 | !msg->spi->controller->defer_optimize_message) | ||
| 2155 | __spi_unoptimize_message(msg); | 2156 | __spi_unoptimize_message(msg); |
| 2156 | } | 2157 | } |
| 2157 | 2158 | ||
| @@ -4294,6 +4295,11 @@ static int __spi_optimize_message(struct spi_device *spi, · 2136 unmodified lines | |||
| 4294 | static int spi_maybe_optimize_message(struct spi_device *spi, | 4295 | static int spi_maybe_optimize_message(struct spi_device *spi, |
| 4295 | struct spi_message *msg) | 4296 | struct spi_message *msg) |
| 4296 | { | 4297 | { |
| 4298 | if (spi->controller->defer_optimize_message) { | ||
| 4299 | msg->spi = spi; | ||
| 4300 | return 0; | ||
| 4301 | } | ||
| 4302 | |||
| 4297 | if (msg->pre_optimized) | 4303 | if (msg->pre_optimized) |
| 4298 | return 0; | 4304 | return 0; |
| 4299 | 4305 | ||
| @@ -4324,6 +4330,13 @@ int spi_optimize_message(struct spi_device *spi, struct spi_message *msg) · 24 unmodified lines | |||
| 4324 | { | 4330 | { |
| 4325 | int ret; | 4331 | int ret; |
| 4326 | 4332 | ||
| 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 | |||
| 4327 | ret = __spi_optimize_message(spi, msg); | 4340 | ret = __spi_optimize_message(spi, msg); |
| 4328 | if (ret) | 4341 | if (ret) |
| 4329 | return ret; | 4342 | return ret; |
| @@ -4350,6 +4363,9 @@ EXPORT_SYMBOL_GPL(spi_optimize_message); · 20 unmodified lines | |||
| 4350 | */ | 4363 | */ |
| 4351 | void spi_unoptimize_message(struct spi_message *msg) | 4364 | void spi_unoptimize_message(struct spi_message *msg) |
| 4352 | { | 4365 | { |
| 4366 | if (msg->spi->controller->defer_optimize_message) | ||
| 4367 | return; | ||
| 4368 | |||
| 4353 | __spi_unoptimize_message(msg); | 4369 | __spi_unoptimize_message(msg); |
| 4354 | msg->pre_optimized = false; | 4370 | msg->pre_optimized = false; |
| 4355 | } | 4371 | } |
| @@ -533,6 +533,9 @@ extern struct spi_device *spi_new_ancillary_device(struct spi_device *spi, u8 ch · 532 unmodified lines | |||
| 533 | * @queue_empty: signal green light for opportunistically skipping the queue | 533 | * @queue_empty: signal green light for opportunistically skipping the queue |
| 534 | * for spi_sync transfers. | 534 | * for spi_sync transfers. |
| 535 | * @must_async: disable all fast paths in the core | 535 | * @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 | ||
| 536 | * | 539 | * |
| 537 | * Each SPI controller can communicate with one or more @spi_device | 540 | * Each SPI controller can communicate with one or more @spi_device |
| 538 | * children. These make a small bus, sharing MOSI, MISO and SCK signals | 541 | * children. These make a small bus, sharing MOSI, MISO and SCK signals |
| @@ -776,6 +779,7 @@ struct spi_controller { · 237 unmodified lines | |||
| 776 | /* Flag for enabling opportunistic skipping of the queue in spi_sync */ | 779 | /* Flag for enabling opportunistic skipping of the queue in spi_sync */ |
| 777 | bool queue_empty; | 780 | bool queue_empty; |
| 778 | bool must_async; | 781 | bool must_async; |
| 782 | bool defer_optimize_message; | ||
| 779 | }; | 783 | }; |
| 780 | 784 | ||
| 781 | static inline void *spi_controller_get_devdata(struct spi_controller *ctlr) | 785 | static inline void *spi_controller_get_devdata(struct spi_controller *ctlr) |