Skip to contentSkip to conversation

virtio-scsi: stop using aio_disable_external() during unplug

PATCH In review Stefan Hajnoczi 2 participants 4 messages Mar 23, 2023 – Apr 4, 2023

lore
Share & tools
lore
#0 lore raw

cover virtio-scsi: stop using aio_disable_external() during unplug

The aio_disable_external() API is a solution for stopping I/O during critical sections. The newer BlockDevOps->drained_begin/end/poll() callbacks offer a cleaner solution that supports the upcoming multi-queue block layer. This series removes aio_disable_external() from the virtio-scsi emulation code.

Patch 1 is a fix for something I noticed when reading the code.

Patch 2 replaces aio_disable_external() with functionality for safe hot unplug that's mostly already present in the SCSI emulation code.

Stefan Hajnoczi (2):
virtio-scsi: avoid race between unplug and transport event
virtio-scsi: stop using aio_disable_external() during unplug

 hw/scsi/scsi-bus.c    |  3 ++-
 hw/scsi/scsi-disk.c   |  1 +
 hw/scsi/virtio-scsi.c | 21 +++++++++------------
 3 files changed, 12 insertions(+), 13 deletions(-)
Signature · 1 line
2.39.2
#1 lore raw

PATCH 1/2 virtio-scsi: avoid race between unplug and transport event

Only report a transport reset event to the guest after the SCSIDevice has been unrealized by qdev_simple_device_unplug_cb().

qdev_simple_device_unplug_cb() sets the SCSIDevice's qdev.realized field to false so that scsi_device_find/get() no longer see it.

scsi_target_emulate_report_luns() also needs to be updated to filter out SCSIDevices that are unrealized.

These changes ensure that the guest driver does not see the SCSIDevice that's being unplugged if it responds very quickly to the transport reset event.

I noticed this issue when reading the code and have not reproduced it.

---
 hw/scsi/scsi-bus.c    |  3 ++-
 hw/scsi/virtio-scsi.c | 18 +++++++++---------
 2 files changed, 11 insertions(+), 10 deletions(-)
hw/scsi/scsi-bus.c +2 -1
index c97176110c..f9bd064833 100644
@@ -487,7 +487,8 @@ static bool scsi_target_emulate_report_luns(SCSITargetReq *r)
             DeviceState *qdev = kid->child;
             SCSIDevice *dev = SCSI_DEVICE(qdev);
 
-            if (dev->channel == channel && dev->id == id && dev->lun != 0) {
+            if (dev->channel == channel && dev->id == id && dev->lun != 0 &&
+                qatomic_load_acquire(&dev->qdev.realized)) {
                 store_lun(tmp, dev->lun);
                 g_byte_array_append(buf, tmp, 8);
                 len += 8;
hw/scsi/virtio-scsi.c +9 -9
index 612c525d9d..000961446c 100644
@@ -1063,15 +1063,6 @@ static void virtio_scsi_hotunplug(HotplugHandler *hotplug_dev, DeviceState *dev,
     SCSIDevice *sd = SCSI_DEVICE(dev);
     AioContext *ctx = s->ctx ?: qemu_get_aio_context();
 
-    if (virtio_vdev_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG)) {
-        virtio_scsi_acquire(s);
-        virtio_scsi_push_event(s, sd,
-                               VIRTIO_SCSI_T_TRANSPORT_RESET,
-                               VIRTIO_SCSI_EVT_RESET_REMOVED);
-        scsi_bus_set_ua(&s->bus, SENSE_CODE(REPORTED_LUNS_CHANGED));
-        virtio_scsi_release(s);
-    }
-
     aio_disable_external(ctx);
     qdev_simple_device_unplug_cb(hotplug_dev, dev, errp);
     aio_enable_external(ctx);
@@ -1082,6 +1073,15 @@ static void virtio_scsi_hotunplug(HotplugHandler *hotplug_dev, DeviceState *dev,
         blk_set_aio_context(sd->conf.blk, qemu_get_aio_context(), NULL);
         virtio_scsi_release(s);
     }
+
+    if (virtio_vdev_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG)) {
+        virtio_scsi_acquire(s);
+        virtio_scsi_push_event(s, sd,
+                               VIRTIO_SCSI_T_TRANSPORT_RESET,
+                               VIRTIO_SCSI_EVT_RESET_REMOVED);
+        scsi_bus_set_ua(&s->bus, SENSE_CODE(REPORTED_LUNS_CHANGED));
+        virtio_scsi_release(s);
+    }
 }
 
 static struct SCSIBusInfo virtio_scsi_scsi_info = {
Signature · 1 line
2.39.2
#2 lore raw

PATCH 2/2 virtio-scsi: stop using aio_disable_external() during unplug

In reply to Stefan Hajnoczi

This patch is part of an effort to remove the aio_disable_external() API because it does not fit in a multi-queue block layer world where many AioContexts may be submitting requests to the same disk.

The SCSI emulation code is already in good shape to stop using aio_disable_external(). The API is only used by commit 9c5aad84da1c ("virtio-scsi: fixed virtio_scsi_ctx_check failed when detaching scsi disk") to ensure that virtio_scsi_hotunplug() works while the guest driver is submitting I/O.

Ensure virtio_scsi_hotunplug() is safe as follows:

1. qdev_simple_device_unplug_cb() -> qdev_unrealize() -> device_set_realized() calls qatomic_set(&dev->realized, false) so that future scsi_device_get() calls return NULL because they exclude SCSIDevices with realized=false.

That means virtio-scsi will reject new I/O requests to this SCSIDevice with VIRTIO_SCSI_S_BAD_TARGET even while virtio_scsi_hotunplug() is still executing. We are protected against new requests!

2. Add a call to scsi_device_purge_requests() from scsi_unrealize() so that in-flight requests are cancelled synchronously. This ensures that no in-flight requests remain once qdev_simple_device_unplug_cb() returns.

Thanks to these two conditions we don't need aio_disable_external() anymore.

---
 hw/scsi/scsi-disk.c   | 1 +
 hw/scsi/virtio-scsi.c | 3 ---
 2 files changed, 1 insertion(+), 3 deletions(-)
hw/scsi/scsi-disk.c +1 -0
index 97c9b1c8cd..e01bd84541 100644
@@ -2522,6 +2522,7 @@ static void scsi_realize(SCSIDevice *dev, Error **errp)
 
 static void scsi_unrealize(SCSIDevice *dev)
 {
+    scsi_device_purge_requests(dev, SENSE_CODE(RESET));
     del_boot_device_lchs(&dev->qdev, NULL);
 }
 
hw/scsi/virtio-scsi.c +0 -3
index 000961446c..a02f9233ec 100644
@@ -1061,11 +1061,8 @@ static void virtio_scsi_hotunplug(HotplugHandler *hotplug_dev, DeviceState *dev,
     VirtIODevice *vdev = VIRTIO_DEVICE(hotplug_dev);
     VirtIOSCSI *s = VIRTIO_SCSI(vdev);
     SCSIDevice *sd = SCSI_DEVICE(dev);
-    AioContext *ctx = s->ctx ?: qemu_get_aio_context();
 
-    aio_disable_external(ctx);
     qdev_simple_device_unplug_cb(hotplug_dev, dev, errp);
-    aio_enable_external(ctx);
 
     if (s->ctx) {
         virtio_scsi_acquire(s);
Signature · 1 line
2.39.2
Reviewed-by on the v1 series
#3 lore raw
In reply to Stefan Hajnoczi
Quoted 10 lines · Stefan Hajnoczi

The aio_disable_external() API is a solution for stopping I/O during critical sections. The newer BlockDevOps->drained_begin/end/poll() callbacks offer a cleaner solution that supports the upcoming multi-queue block layer. This series removes aio_disable_external() from the virtio-scsi emulation code.

Patch 1 is a fix for something I noticed when reading the code.

Patch 2 replaces aio_disable_external() with functionality for safe hot unplug that's mostly already present in the SCSI emulation code.

Quoted 9 lines

Stefan Hajnoczi (2):
virtio-scsi: avoid race between unplug and transport event
virtio-scsi: stop using aio_disable_external() during unplug

  hw/scsi/scsi-bus.c    |  3 ++-
  hw/scsi/scsi-disk.c   |  1 +
  hw/scsi/virtio-scsi.c | 21 +++++++++------------
  3 files changed, 12 insertions(+), 13 deletions(-)

For both patches: