spi: unoptimize message when queuing it fails
Subsystems (2)
v2 cover spi: unoptimize message when queuing it fails
From: Joseph CT Chan <[email protected]>
Hi Mark,
spi_async() and spi_sync() optimize a message before they queue it. If queuing fails, the message is never finalized, so the split transfers and the controller's optimize state leak and the message stays marked as optimized. This happens with -EBUSY while another device holds spi_bus_lock() (for example mmc_spi), with a stopped queue, and with an error from a legacy transfer() callback. Patch 1 unoptimizes the message on those error paths, and patch 2 adds KUnit tests for them.
Tested with KUnit on ARCH=um: all 7 cases pass with patch 1, and the 3 queue error cases fail without it. Not tested on hardware.
Changes in v2:
- Split out of the v1 14-patch series, as David suggested.
- Patch 1: added David's Reviewed-by.
- Patch 2: inject errors through the fake controller's transfer() callback instead of mem_ops, use designated initializers, and trim the comments.
- Moved the AI assistance note from an Assisted-by tag into the commit message.
v1: https://lore.kernel.org/all/[email protected]/
The spi-mux, spi-ti-qspi and lock context series I am sending separately are based on this one.
Walkthrough of the bug and the tests:
https://gist.github.com/joectchan/9a14b5e6facb75058ebc5a6ff1757131
Please consider this patch series for the next merge window.
Thanks,
Joseph
Joseph CT Chan (2):
spi: unoptimize message when queuing it fails
spi: add KUnit tests for message optimize state on queue errors
drivers/spi/Kconfig | 13 ++ drivers/spi/Makefile | 1 + drivers/spi/spi.c | 7 + drivers/spi/tests/.kunitconfig | 4 + drivers/spi/tests/Makefile | 1 + drivers/spi/tests/spi-core-kunit.c | 262 +++++++++++++++++++++++++++++ 6 files changed, 288 insertions(+) create mode 100644 drivers/spi/tests/.kunitconfig create mode 100644 drivers/spi/tests/spi-core-kunit.c
base-commit: c339f4010dea8b0d754f84ace70cd1b089878ef8
Signature · 1 line
2.56.0.385.gac671c819a-goog
PATCH v2 1/2 spi: unoptimize message when queuing it fails
From: Joseph CT Chan <[email protected]>
spi_async() and spi_sync() optimize a message before they queue it. When queuing then fails, the message is never finalized, but finalizing is the only place where the core undoes its own optimization (spi_finalize_current_message() calls spi_maybe_unoptimize_message()). So the error paths leak the split transfers and the controller's optimize state, and leave msg->optimized set. A caller that resends the message then optimizes an already optimized message.
This happens when spi_async() returns -EBUSY because another device holds spi_bus_lock() on the controller (for example mmc_spi), when the controller's message queue is stopped (-ESHUTDOWN), when the controller has no ->transfer() callback (-ENOTSUPP), and when __spi_sync() gets an error from a controller's legacy ->transfer() callback.
Call spi_maybe_unoptimize_message() on these two error paths. It only acts on messages the core optimized itself, so messages optimized by the peripheral driver and controllers that defer optimization (such as spi-mux) are not affected. A queued message is never unoptimized here.
Tested with KUnit (spi_core_optimize: spi_core_queue_error_test, added later in this series) on ARCH=um; builds with W=1 on x86_64. Not tested on hardware.
Developed with help from the Claude and Gemini AI assistants.
---
v2: Added Reviewed-by from David Lechner.
drivers/spi/spi.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/spi/spi.c +7 -0
@@ -4906,6 +4906,10 @@ int spi_async(struct spi_device *spi, struct spi_message *message)spin_unlock_irqrestore (&ctlr->bus_lock_spinlock, flags); +/* The message was not queued, so it will not be finalized. */ +if (ret) +spi_maybe_unoptimize_message (message); +return ret; }EXPORT_SYMBOL_GPL (spi_async);
@@ -5009,6 +5013,9 @@ static int __spi_sync(struct spi_device *spi, struct spi_message *message)if (status ==0 ) {wait_for_completion (&done); status = message->status; + }else { +/* The message was not queued, so it will not be finalized. */ +spi_maybe_unoptimize_message (message); } message->complete =NULL ; message->context =NULL ;
Signature · 1 line
2.56.0.385.gac671c819a-goog
PATCH v2 2/2 spi: add KUnit tests for message optimize state on queue errors
In reply to Joseph CT ChanFrom: Joseph CT Chan <[email protected]>
spi_async() and spi_sync() optimize a message before they queue it. If queuing fails, for example because another device holds spi_bus_lock() on the controller, because the controller's transfer() callback returns an error, or because the controller is suspended, the message is never finalized, and finalizing is where the core normally unoptimizes it.
Add KUnit tests with a fake controller that counts its optimize_message() and unoptimize_message() calls, splits transfers across a 2-byte boundary to exercise msg.resources, and tracks msg.opt_state. The tests cover both async and sync queue errors, verify split transfers are cleaned up and opt_state cleared, check that a message optimized by the peripheral driver is left alone on error, and check that a successful spi_sync() optimizes and unoptimizes the message exactly once on both queued and unqueued paths.
Without "spi: unoptimize message when queuing it fails" the queue error cases fail with unoptimize_count == 0, msg.resources leaked, and msg.optimized still true; the other cases pass.
Tested with KUnit on ARCH=um: all cases pass with the fixes in this series applied. Builds with W=1 on x86_64.
Developed with help from the Claude and Gemini AI assistants.
---
v2: Inject errors through a transfer() callback on the fake controller instead of mem_ops, use designated initializers, and trim the comments.
drivers/spi/Kconfig | 13 ++ drivers/spi/Makefile | 1 + drivers/spi/tests/.kunitconfig | 4 + drivers/spi/tests/Makefile | 1 + drivers/spi/tests/spi-core-kunit.c | 262 +++++++++++++++++++++++++++++ 5 files changed, 281 insertions(+) create mode 100644 drivers/spi/tests/.kunitconfig create mode 100644 drivers/spi/tests/spi-core-kunit.c
drivers/spi/Kconfig +13 -0
@@ -1398,6 +1398,19 @@ config SPI_DMA_KUNIT_TEST If unsure say N. +config SPI_CORE_KUNIT_TEST + tristate "KUnit tests for SPI core message optimize state" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + help + Build KUnit tests for the SPI core message optimize state. The tests + register a fake controller and check that spi_async() and spi_sync() + undo the optimization of a message they fail to queue, that a + message optimized by the peripheral driver is left alone, and that a + successful transfer optimizes and unoptimizes exactly once. + + If unsure say N. + config SPI_LOOPBACK_TEST tristate "spi loopback test framework support" depends on m
drivers/spi/Makefile +1 -0
@@ -14,6 +14,7 @@ obj-$(CONFIG_SPI_OFFLOAD) += spi-offload.o obj-$(CONFIG_SPI_SPIDEV) += spidev.o obj-$(CONFIG_SPI_LOOPBACK_TEST) += spi-loopback-test.o obj-$(CONFIG_SPI_DMA_KUNIT_TEST) += tests/ +obj-$(CONFIG_SPI_CORE_KUNIT_TEST) += tests/ # SPI master controller drivers (bus) obj-$(CONFIG_SPI_AIROHA_SNFI) += spi-airoha-snfi.o
drivers/spi/tests/.kunitconfig new file +4 -0
@@ -0,0 +1,4 @@ +CONFIG_KUNIT=y +CONFIG_SPI=y +CONFIG_SPI_MASTER=y +CONFIG_SPI_CORE_KUNIT_TEST=y
drivers/spi/tests/Makefile +1 -0
@@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only obj-$(CONFIG_SPI_DMA_KUNIT_TEST) += spi-dma-kunit.o +obj-$(CONFIG_SPI_CORE_KUNIT_TEST) += spi-core-kunit.o
drivers/spi/tests/spi-core-kunit.c new file +262 -0
262 lines not on this page · raw message
Signature · 1 line
2.56.0.385.gac671c819a-goog
Mark Brown
maintainerRe: [PATCH v2 2/2] spi: add KUnit tests for message optimize state on queue errors
drivers/spi/Makefile obj-$(CONFIG_SPI_OFFLOAD) += spi-offload.o · line 17
Quoted 12 lines
spi_async()andspi_sync()optimize a message before they queue it. If queuing fails, for example because another device holdsspi_bus_lock()on the controller, because the controller'stransfer()callback returns an error, or because the controller is suspended, the message is never finalized, and finalizing is where the core normally unoptimizes it.+++ b/drivers/spi/Makefile
drivers/spi/Makefile +1
-0@@ -14,6 +14,7 @@ obj-$(CONFIG_SPI_OFFLOAD) += spi-offload.o obj-$(CONFIG_SPI_SPIDEV) += spidev.o obj-$(CONFIG_SPI_LOOPBACK_TEST) += spi-loopback-test.o obj-$(CONFIG_SPI_DMA_KUNIT_TEST) += tests/ +obj-$(CONFIG_SPI_CORE_KUNIT_TEST) += tests/
This ends up working since kbuild will deduplicate when sorting but it's not how things normally work and might break in future - normally the tests directory would be unconditionally included and it's Makefile would have the config checks (which it already does).
drivers/spi/tests/.kunitconfig line 4
Quoted 7 lines
drivers/spi/tests/.kunitconfig new file +4
-0@@ -0,0 +1,4 @@ +CONFIG_KUNIT=y +CONFIG_SPI=y +CONFIG_SPI_MASTER=y +CONFIG_SPI_CORE_KUNIT_TEST=y
You should also add the DMA tests that are already there.
Signature · 1 line
Mark Brown <[email protected]>