Skip to content

Commit ca52aa4

Browse files
dlechbroonie
authored andcommitted
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. Reported-by: Oleksij Rempel <[email protected]> Closes: https://lore.kernel.org/linux-spi/[email protected]/ Reported-by: Marc Kleine-Budde <[email protected]> Closes: https://lore.kernel.org/linux-spi/[email protected]/ Fixes: 7b1d87a ("spi: add spi_optimize_message() APIs") Signed-off-by: David Lechner <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Mark Brown <[email protected]>
1 parent c86a918 commit ca52aa4

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

drivers/spi/spi-mux.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ static int spi_mux_probe(struct spi_device *spi)
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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2151,7 +2151,8 @@ static void __spi_unoptimize_message(struct spi_message *msg)
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,
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)
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);
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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,9 @@ extern struct spi_device *spi_new_ancillary_device(struct spi_device *spi, u8 ch
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 {
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)

0 commit comments

Comments
 (0)