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.
Showing the changes to include (1 of 3 files). Show all
Unified · Split
| @@ -533,6 +533,9 @@ extern struct spi_device *spi_new_ancillary_device(struct spi_device *spi, u8 ch · 532 unmodified lines | ||
| 533 | 533 | * @queue_empty: signal green light for opportunistically skipping the queue |
| 534 | 534 | * for spi_sync transfers. |
| 535 | 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 | 540 | * Each SPI controller can communicate with one or more @spi_device |
| 538 | 541 | * children. These make a small bus, sharing MOSI, MISO and SCK signals |
| @@ -776,6 +779,7 @@ struct spi_controller { · 237 unmodified lines | ||
| 776 | 779 | /* Flag for enabling opportunistic skipping of the queue in spi_sync */ |
| 777 | 780 | bool queue_empty; |
| 778 | 781 | bool must_async; |
| 782 | + bool defer_optimize_message; | |
| 779 | 783 | }; |
| 780 | 784 | |
| 781 | 785 | static inline void *spi_controller_get_devdata(struct spi_controller *ctlr) |