spi: unoptimize message when queuing it fails
Subsystems (2)
v3 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 using --kunitconfig=drivers/spi on ARCH=um and on x86_64 under QEMU: the 7 new cases and the 4 existing DMA cases pass with patch 1, and the 3 queue error cases fail without it. Not tested on hardware.
Changes in v3:
- Rebased on spi/for-7.5.
- Patch 2: include drivers/spi/tests unconditionally from drivers/spi/Makefile instead of adding a second conditional line, as you suggested.
- Patch 2: add the test to the existing drivers/spi/.kunitconfig, which also runs the DMA tests, instead of adding a new one.
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.
v2: https://lore.kernel.org/linux-spi/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
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/.kunitconfig | 1 + drivers/spi/Kconfig | 13 ++ drivers/spi/Makefile | 2 +- drivers/spi/spi.c | 7 + drivers/spi/tests/Makefile | 1 + drivers/spi/tests/spi-core-kunit.c | 262 +++++++++++++++++++++++++++++ 6 files changed, 285 insertions(+), 1 deletion(-) create mode 100644 drivers/spi/tests/spi-core-kunit.c
base-commit: 281521f809f6a6ff406d082293faf1ed3bbfd949
Signature · 1 line
2.56.0.506.g506a7eabc5-goog
PATCH v3 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.
---
v3: No changes.
v2: Added Reviewed-by from David Lechner.
drivers/spi/spi.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/spi/spi.c +7 -0
@@ -4915,6 +4915,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);
@@ -5018,6 +5022,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.506.g506a7eabc5-goog
PATCH v3 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.
Include drivers/spi/tests from drivers/spi/Makefile unconditionally, since tests/Makefile already checks the config symbol of each test, and add the test to drivers/spi/.kunitconfig so that
./tools/testing/kunit/kunit.py run --kunitconfig=drivers/spi
runs it together with the DMA tests.
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.
---
v3: Include drivers/spi/tests unconditionally from drivers/spi/Makefile, and add the test to the existing drivers/spi/.kunitconfig instead of a new drivers/spi/tests/.kunitconfig, as Mark suggested.
v2: Inject errors through a transfer() callback on the fake controller instead of mem_ops, use designated initializers, and trim the comments.
drivers/spi/.kunitconfig | 1 + drivers/spi/Kconfig | 13 ++ drivers/spi/Makefile | 2 +- drivers/spi/tests/Makefile | 1 + drivers/spi/tests/spi-core-kunit.c | 262 +++++++++++++++++++++++++++++ 5 files changed, 278 insertions(+), 1 deletion(-) create mode 100644 drivers/spi/tests/spi-core-kunit.c
drivers/spi/.kunitconfig +1 -0
@@ -2,3 +2,4 @@ CONFIG_KUNIT=y CONFIG_SPI=y CONFIG_SPI_MASTER=y CONFIG_SPI_DMA_KUNIT_TEST=y +CONFIG_SPI_CORE_KUNIT_TEST=y
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 -1
@@ -13,7 +13,7 @@ obj-$(CONFIG_SPI_MUX) += spi-mux.o 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-y += tests/ # SPI master controller drivers (bus) obj-$(CONFIG_SPI_AIROHA_SNFI) += spi-airoha-snfi.o
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.506.g506a7eabc5-goog
Mark Brown
maintainerQuoted 14 lines · Joseph CT Chan
spi: unoptimize message when queuing it fails
From: Joseph CT Chan <[email protected]>
Hi Mark,
spi_async()andspi_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-EBUSYwhile another device holdsspi_bus_lock()(for examplemmc_spi), with a stopped queue, and with an error from a legacytransfer()callback. Patch 1 unoptimizes the message on those error paths, and patch 2 adds KUnit tests for them.
Quoted 1 line
[...]
Applied to
https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git for-7.5
Thanks!
[1/2] spi: unoptimize message when queuing it fails
https://git.kernel.org/broonie/spi/c/91db5febb159 [2/2] spi: add KUnit tests for message optimize state on queue errors https://git.kernel.org/broonie/spi/c/f6fa7376869c
All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.
You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.
If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.
Please add any relevant lists and maintainers to the CCs when replying to this mail.
Thanks,
Mark