spi: ch341: handle transfers without a TX or RX buffer
Subsystems (2)
PATCH 1/1 spi: ch341: handle transfers without a TX or RX buffer
ch341_transfer_one() unconditionally copies from trans->tx_buf into the TX packet and hands trans->rx_buf to usb_bulk_msg() for the readback.
The SPI core allows half-duplex transfers where either of the buffers is NULL, so a transfer without TX data crashes the kernel on the memcpy and a transfer without RX data makes usb_bulk_msg() store the received data at address 0.
Only copy TX data when the transfer carries a TX buffer and fall back to the TX packet buffer as a scratch area for the readback when the transfer has no RX buffer. The packet buffer is 32 bytes, which covers the maximum readback length of 31 bytes, and it is not used by anything else while the readback runs.
---
Reviewer notes:
- Both failure modes are reachable from unprivileged userspace through
spidev: SPI_IOC_MESSAGE accepts transfers with either of the buffers set to NULL, and the driver passes them on as-is.
- The readback fallback reuses the 32-byte TX packet buffer, which covers the maximum readback length of 31 bytes. It is only used by ch341_transfer_one() and ch341_set_cs(), both of which run synchronously from the SPI core's transfer handling, so nothing touches the buffer while the readback is in flight.
- Pre-fix reproducer: open /dev/spidev0.0 and issue a single SPI_IOC_MESSAGE transfer with tx_buf = NULL (memcpy from NULL in ch341_transfer_one()) or with rx_buf = NULL (usb_bulk_msg() stores the readback at address 0).
Functionally verified in QEMU on x86_64: a CH341a adapter was emulated with gadgetfs + dummy_hcd (the emulated device echoes every SPI stream packet's payload back on the bulk IN endpoint). With this patch applied the probe completes, and tx-only, rx-only and full-duplex transfers issued through spidev succeed, with the full-duplex readback matching the sent payload, and without a splat. The emulator and the test program are local test tooling and are not part of this submission.
drivers/spi/spi-ch341.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/spi/spi-ch341.c +9 -2
@@ -78,14 +78,21 @@ static int ch341_transfer_one(struct spi_controller *host, ch341->tx_buf[0 ] =CH341A_CMD_SPI_STREAM ; -memcpy (ch341->tx_buf +1 , trans->tx_buf, len -1 ); +if (trans->tx_buf) +memcpy (ch341->tx_buf +1 , trans->tx_buf, len -1 ); ret =usb_bulk_msg (ch341->udev, ch341->write_pipe, ch341->tx_buf, len,NULL ,CH341_DEFAULT_TIMEOUT );if (ret)return ret; -return usb_bulk_msg (ch341->udev, ch341->read_pipe, trans->rx_buf, +/* +* Half-duplex transfers can come without a RX buffer; the packet +* buffer is not used by anything else during the synchronous +* transfer, so reuse it as a scratch area for the readback. +*/ +return usb_bulk_msg (ch341->udev, ch341->read_pipe, + trans->rx_buf ? trans->rx_buf : ch341->tx_buf, len -1 ,NULL ,CH341_DEFAULT_TIMEOUT ); }
base-commit: 72d3fcf802c45d00b300f25b848a93c3a2bd7c7e
prerequisite-patch-id: e48582f6ffe124b3806593af6e22b74682c8a83f
prerequisite-patch-id: 369f74b9a7ecde56141b13ec671f9200345a3bab
Signature · 1 line
2.50.1
Mark Brown
maintainerQuoted 7 lines · Weibin Liu
ch341_transfer_one()unconditionally copies fromtrans->tx_bufinto the TX packet and handstrans->rx_buftousb_bulk_msg()for the readback.The SPI core allows half-duplex transfers where either of the buffers is NULL, so a transfer without TX data crashes the kernel on the memcpy and a transfer without RX data makes
usb_bulk_msg()store the received data at address 0.
...
Quoted 8 lines
Functionally verified in QEMU on
x86_64: a CH341a adapter was emulated with gadgetfs +dummy_hcd(the emulated device echoes every SPI stream packet's payload back on the bulk IN endpoint). With this patch applied the probe completes, and tx-only, rx-only and full-duplex transfers issued through spidev succeed, with the full-duplex readback matching the sent payload, and without a splat. The emulator and the test program are local test tooling and are not part of this submission.
Are you sure the actual hardware supports half duplex and the fix isn't to set _MUST_RX and _MUST_TX?
Hi Mark,
Thanks for the review.
Quoted 2 lines
Are you sure the actual hardware supports half duplex and the fix isn't to set
_MUST_RXand_MUST_TX?
It doesn't. The CH341A clocks data in both directions on every transfer and has no mode to disable a direction; the 4-wire mode has separate DIN/DOUT pins. So v2 sets SPI_CONTROLLER_MUST_TX | SPI_CONTROLLER_MUST_RX in probe and drops the driver-side NULL handling:
ctrl->flags = SPI_CONTROLLER_MUST_TX | SPI_CONTROLLER_MUST_RX;
Re-ran the gadgetfs + dummy_hcd emulation on top of v2: probe, tx-only, rx-only and full-duplex transfers all still pass, no splats.
No other changes.
Weibin
PATCH v2 1/1 spi: ch341: handle transfers without a TX or RX buffer
In reply to Weibin Liuch341_transfer_one() unconditionally copies from trans->tx_buf into the TX packet and hands trans->rx_buf to usb_bulk_msg() for the readback.
The SPI core allows half-duplex transfers where either of the buffers is NULL, so a transfer without TX data crashes the kernel on the memcpy and a transfer without RX data makes usb_bulk_msg() store the received data at address 0.
The CH341A clocks data in both directions on every transfer; there is no mode to disable a direction. Set SPI_CONTROLLER_MUST_TX and SPI_CONTROLLER_MUST_RX so the SPI core fills in dummy buffers for half-duplex transfers.
---
Changes in v2:
- Set SPI_CONTROLLER_MUST_TX | SPI_CONTROLLER_MUST_RX instead of handling NULL buffers in the driver (Mark Brown).
--- drivers/spi/spi-ch341.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/spi/spi-ch341.c +6 -0
@@ -189,6 +189,12 @@ static int ch341_probe(struct usb_interface *intf, ctrl->transfer_one = ch341_transfer_one; ctrl->set_cs = ch341_set_cs; ctrl->auto_runtime_pm = false; +/* +* The CH341A SPI_STREAM command clocks data in both directions on +* every transfer, so the driver always needs a TX and an RX buffer; +* let the SPI core supply dummy buffers for half-duplex transfers. +*/ + ctrl->flags =SPI_CONTROLLER_MUST_TX |SPI_CONTROLLER_MUST_RX ;usb_set_intfdata (intf, ch341);
base-commit: 72d3fcf802c45d00b300f25b848a93c3a2bd7c7e
Signature · 1 line
2.50.1
Mark Brown
maintainerQuoted 1 line · Weibin Liu
spi: ch341: handle transfers without a TX or RX buffer
Applied to
https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git for-7.4
Thanks!
[1/1] spi: ch341: handle transfers without a TX or RX buffer
https://git.kernel.org/broonie/spi/c/84182c48999f
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
Mark Brown
maintainerQuoted 2 lines · Weibin Liu
ch341_transfer_one()unconditionally copies fromtrans->tx_bufinto the TX packet and handstrans->rx_buftousb_bulk_msg()for the readback.
Please don't send new patches in reply to old patches or serieses, this makes it harder for both people and tools to understand what is going on - it can bury things in mailboxes and make it difficult to keep track of what current patches are, both for the new patches and the old ones.