The storage device hangs if two conditions are met:
- Data phase fits into a single bulk-IN transfer submitted to the
device while the disk read is still in flight
- CSW bulk-IN is submitted (queued at the USB endpoint) behind that
data transfer before the read completes
Under these conditions, the SCSI request completes synchronously from
inside usb_msd_copy_data() and usb_packet_complete() processes the next
queued packet on the endpoint synchronously, so usb_msd_handle_data()
runs again before usb_msd_command_complete() has cleared s->req. The
device sees s->req still set, takes the "still in flight" branch, and
parks the CSW packet again with USB_RET_ASYNC; since the SCSI request is
already complete, nothing will ever complete it, causing the deadlock.
s->req tracks the SCSI request, which already finished when
usb_msd_command_complete() is called; what follows only concerns the
USB transfer. Knowing this, the fix result is to move the s->req clear
from the bottom to the top of the function.
Fixes: db4be873d312 ("usb: maintain async packet list per endpoint")
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4658
Signed-off-by: Tommaso Califano <[email protected]>
---
hw/usb/dev-storage.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/hw/usb/dev-storage.c b/hw/usb/dev-storage.c
index 040cf15051..620d02a4a9 100644
--- a/hw/usb/dev-storage.c
+++ b/hw/usb/dev-storage.c
@@ -273,6 +273,14 @@ void usb_msd_command_complete(SCSIRequest *req, size_t
resid)
s->csw.residue = cpu_to_le32(s->data_len);
s->csw.status = req->status != 0;
+ /*
+ * Drop it before completing the packet.
+ * usb_packet_complete() may synchronously run a CSW packet queued
+ * behind it, which must not see the request as still in flight.
+ */
+ scsi_req_unref(req);
+ s->req = NULL;
+
if (s->packet) {
if (s->data_len == 0 && s->mode == USB_MSDM_DATAOUT) {
/* A deferred packet with no write data remaining must be
@@ -300,8 +308,6 @@ void usb_msd_command_complete(SCSIRequest *req, size_t
resid)
} else if (s->data_len == 0) {
s->mode = USB_MSDM_CSW;
}
- scsi_req_unref(req);
- s->req = NULL;
}
void usb_msd_request_cancelled(SCSIRequest *req)
--
2.56.0