Skip to content

virtio-scsi: fix SCSIDevice hot unplug with IOThread

Stefan Hajnoczi qemu-devel 2 versions – Applied

Status from this message: Thanks, applied to the block branch.

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v3 Latest3 patches 1 replyApplied
  2. v23 patches 8 repliesSuperseded

Changes in v3 from the cover letter

- Fix s/see/sees/ typo in Patch 2 commit description [Eric]
- Add call stack to Patch 3 commit description to make it clear how blk_drain() is invoked [Kevin]

Patches in v3 Applied

#Subject and filesSizeRepliesApplied
1/3scsi: protect req->aiocb with AioContext lockhw/scsi/scsi-disk.c hw/scsi/scsi-generic.c+22 -120 replies
2/3dma-helpers: prevent dma_blk_cb() vs dma_aio_cancel() racehw/scsi/scsi-disk.c softmmu/dma-helpers.c+8 -80 replies
3/3virtio-scsi: reset SCSI devices from main loop threadhw/scsi/virtio-scsi.c include/hw/virtio/virtio-scsi.h+143 -370 replies

Reviews trailers per patch

All 3 patches have a Reviewed-by or Acked-by.

PatchEric BlakeKevin Wolf
1/3R-b R-b
2/3R-b
3/3R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v3 thread

Cover letter [PATCH v3 0/3] virtio-scsi: fix SCSIDevice hot unplug with IOThread

v3:
- Fix s/see/sees/ typo in Patch 2 commit description [Eric]
- Add call stack to Patch 3 commit description to make it clear how blk_drain() is invoked [Kevin]

Unplugging SCSIDevices when virtio-scsi is using an IOThread suffers from race conditions:
- scsi_device_purge_requests() is called from the IOThread in TMF emulation. This is unsafe, it should only be called from the BQL.
- SCSIRequest->aiocb is not protected by a lock, so there are races between the main loop thread and the IOThread when scsi_device_purge_requests() runs in the main loop thread.
- DMAAIOCB->acb is not protected by a lock, so there are races in the DMA helpers code when cancelling a request from the main loop thread.

These fixes solve assertion failures during SCSIDevice hot unplug in virtio-scsi with IOThread. Expanding the use of the AioContext lock isn't great since we're in the midst of trying to remove it. However, I think this solution is appropriate so that stable trees or distros can backport the fix without depending on QEMU multi-queue block layer refactoring.

Special thanks to Qing Wang, who helped me iterate these patches because I couldn't reproduce the assertion failures myself.

Stefan Hajnoczi (3):
scsi: protect req->aiocb with AioContext lock
dma-helpers: prevent dma_blk_cb() vs dma_aio_cancel() race
virtio-scsi: reset SCSI devices from main loop thread

 include/hw/virtio/virtio-scsi.h |  11 ++-
 hw/scsi/scsi-disk.c             |  23 +++--
 hw/scsi/scsi-generic.c          |  11 ++-
 hw/scsi/virtio-scsi.c           | 169 +++++++++++++++++++++++++-------
 softmmu/dma-helpers.c           |  12 ++-
 5 files changed, 171 insertions(+), 55 deletions(-)
Signature · 1 line
2.39.1