Skip to contentSkip to conversation

virtio: keep ioeventfd assigned

PATCH In review Cornelia Huck 4 participants 6 messages Mar 29, 2016 – Mar 31, 2016

lore
Share & tools
lore
#0 lore raw

cover virtio: keep ioeventfd assigned

This is just the ioventfd assignment fix, factored out into the individual transports. This (+ the assertion patch) has been surviving some minutes of doing a reboot loop so far.

While this is a bit larger than the aio handler changes, I think it is easier to understand.

Cornelia Huck (1):
virtio: fix ioeventfd assignment race

 hw/s390x/virtio-ccw.c          | 22 +++++++++++++++++-----
 hw/virtio/virtio-mmio.c        | 27 +++++++++++++++++----------
 hw/virtio/virtio-pci.c         | 28 ++++++++++++++++++----------
 include/hw/virtio/virtio-bus.h |  4 ++++
 4 files changed, 56 insertions(+), 25 deletions(-)
Signature · 1 line
2.8.0
#1 lore raw

PATCH 1/1 virtio: fix ioeventfd assignment race

The ->set_host_notifier() callback is invoked whenever we want to switch from or to the generic ioeventfd handler. Currently, all transports deregister the ioeventfd backing and then re-register it. This opens a race window where we are without ioeventfd backing for a time period: In the virtio-blk dataplane case, we observed notifications coming in from both the vcpu thread and the iothread.

Let's change pci, mmio and ccw to keep the ioeventfd during ->set_host_notifier() and only switch the ioeventfd handler.

---
 hw/s390x/virtio-ccw.c          | 22 +++++++++++++++++-----
 hw/virtio/virtio-mmio.c        | 27 +++++++++++++++++----------
 hw/virtio/virtio-pci.c         | 28 ++++++++++++++++++----------
 include/hw/virtio/virtio-bus.h |  4 ++++
 4 files changed, 56 insertions(+), 25 deletions(-)
hw/s390x/virtio-ccw.c +17 -5
index cb887ba..7b1088e 100644
@@ -1196,14 +1196,26 @@ static bool virtio_ccw_query_guest_notifiers(DeviceState *d)
 static int virtio_ccw_set_host_notifier(DeviceState *d, int n, bool assign)
 {
     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
+    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
+    VirtQueue *vq = virtio_get_queue(vdev, n);
 
-    /* Stop using the generic ioeventfd, we are doing eventfd handling
-     * ourselves below */
-    dev->ioeventfd_disabled = assign;
     if (assign) {
-        virtio_ccw_stop_ioeventfd(dev);
+        /* Stop using the generic ioeventfd, we are doing eventfd handling
+         * ourselves below */
+        dev->ioeventfd_disabled = true;
+    };
+    /*
+     * Just switch the handler, don't deassign the ioeventfd.
+     * Otherwise, there's a window where we don't have an
+     * ioeventfd and we may end up with a notification where
+     * we don't expect one.
+     */
+    virtio_queue_set_host_notifier_fd_handler(vq, assign, !assign);
+    if (!assign) {
+        /* Use generic ioeventfd handler again. */
+        dev->ioeventfd_disabled = false;
     }
-    return virtio_ccw_set_guest2host_notifier(dev, n, assign, false);
+    return 0;
 }
 
 static int virtio_ccw_get_mappings(VirtioCcwDevice *dev)
hw/virtio/virtio-mmio.c +17 -10
index d4cd91f..aafebdf 100644
@@ -502,19 +502,26 @@ static int virtio_mmio_set_host_notifier(DeviceState *opaque, int n,
                                          bool assign)
 {
     VirtIOMMIOProxy *proxy = VIRTIO_MMIO(opaque);
+    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
+    VirtQueue *vq = virtio_get_queue(vdev, n);
 
-    /* Stop using ioeventfd for virtqueue kick if the device starts using host
-     * notifiers.  This makes it easy to avoid stepping on each others' toes.
-     */
-    proxy->ioeventfd_disabled = assign;
     if (assign) {
-        virtio_mmio_stop_ioeventfd(proxy);
+        /* Stop using the generic ioeventfd, we are doing eventfd handling
+         * ourselves below */
+        proxy->ioeventfd_disabled = true;
+    };
+    /*
+     * Just switch the handler, don't deassign the ioeventfd.
+     * Otherwise, there's a window where we don't have an
+     * ioeventfd and we may end up with a notification where
+     * we don't expect one.
+     */
+    virtio_queue_set_host_notifier_fd_handler(vq, assign, !assign);
+    if (!assign) {
+        /* Use generic ioeventfd handler again. */
+        proxy->ioeventfd_disabled = false;
     }
-    /* We don't need to start here: it's not needed because backend
-     * currently only stops on status change away from ok,
-     * reset, vmstop and such. If we do add code to start here,
-     * need to check vmstate, device state etc. */
-    return virtio_mmio_set_host_notifier_internal(proxy, n, assign, false);
+    return 0;
 }
 
 /* virtio-mmio device */
hw/virtio/virtio-pci.c +18 -10
index 0dadb66..a91c1e8 100644
@@ -1115,18 +1115,26 @@ static int virtio_pci_set_host_notifier(DeviceState *d, int n, bool assign)
 {
     VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
 
-    /* Stop using ioeventfd for virtqueue kick if the device starts using host
-     * notifiers.  This makes it easy to avoid stepping on each others' toes.
-     */
-    proxy->ioeventfd_disabled = assign;
+    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
+    VirtQueue *vq = virtio_get_queue(vdev, n);
+
     if (assign) {
-        virtio_pci_stop_ioeventfd(proxy);
+        /* Stop using the generic ioeventfd, we are doing eventfd handling
+         * ourselves below */
+        proxy->ioeventfd_disabled = true;
+    };
+    /*
+     * Just switch the handler, don't deassign the ioeventfd.
+     * Otherwise, there's a window where we don't have an
+     * ioeventfd and we may end up with a notification where
+     * we don't expect one.
+     */
+    virtio_queue_set_host_notifier_fd_handler(vq, assign, !assign);
+    if (!assign) {
+        /* Use generic ioeventfd handler again. */
+        proxy->ioeventfd_disabled = false;
     }
-    /* We don't need to start here: it's not needed because backend
-     * currently only stops on status change away from ok,
-     * reset, vmstop and such. If we do add code to start here,
-     * need to check vmstate, device state etc. */
-    return virtio_pci_set_host_notifier_internal(proxy, n, assign, false);
+    return 0;
 }
 
 static void virtio_pci_vmstate_change(DeviceState *d, bool running)
include/hw/virtio/virtio-bus.h +4 -0
index 3f2c136..98c660a 100644
@@ -52,6 +52,10 @@ typedef struct VirtioBusClass {
     bool (*has_extra_state)(DeviceState *d);
     bool (*query_guest_notifiers)(DeviceState *d);
     int (*set_guest_notifiers)(DeviceState *d, int nvqs, bool assign);
+    /*
+     * Switch from/to the generic ioeventfd handler.
+     * assigned==false means 'use generic ioeventfd handler'.
+     */
     int (*set_host_notifier)(DeviceState *d, int n, bool assigned);
     void (*vmstate_change)(DeviceState *d, bool running);
     /*
Signature · 1 line
2.8.0
Tested-by on v1 1/1
#2 lore raw

Re: [Qemu-devel] [PATCH 1/1] virtio: fix ioeventfd assignment race

Quoted 143 lines · Cornelia Huck

The ->set_host_notifier() callback is invoked whenever we want to switch from or to the generic ioeventfd handler. Currently, all transports deregister the ioeventfd backing and then re-register it. This opens a race window where we are without ioeventfd backing for a time period: In the virtio-blk dataplane case, we observed notifications coming in from both the vcpu thread and the iothread.

Let's change pci, mmio and ccw to keep the ioeventfd during ->set_host_notifier() and only switch the ioeventfd handler.

---
 hw/s390x/virtio-ccw.c          | 22 +++++++++++++++++-----
 hw/virtio/virtio-mmio.c        | 27 +++++++++++++++++----------
 hw/virtio/virtio-pci.c         | 28 ++++++++++++++++++----------
 include/hw/virtio/virtio-bus.h |  4 ++++
 4 files changed, 56 insertions(+), 25 deletions(-)
hw/s390x/virtio-ccw.c +17 -5
index cb887ba..7b1088e 100644
@@ -1196,14 +1196,26 @@ static bool virtio_ccw_query_guest_notifiers(DeviceState *d)
 static int virtio_ccw_set_host_notifier(DeviceState *d, int n, bool assign)
 {
     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
+    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
+    VirtQueue *vq = virtio_get_queue(vdev, n);
 
-    /* Stop using the generic ioeventfd, we are doing eventfd handling
-     * ourselves below */
-    dev->ioeventfd_disabled = assign;
     if (assign) {
-        virtio_ccw_stop_ioeventfd(dev);
+        /* Stop using the generic ioeventfd, we are doing eventfd handling
+         * ourselves below */
+        dev->ioeventfd_disabled = true;
+    };
+    /*
+     * Just switch the handler, don't deassign the ioeventfd.
+     * Otherwise, there's a window where we don't have an
+     * ioeventfd and we may end up with a notification where
+     * we don't expect one.
+     */
+    virtio_queue_set_host_notifier_fd_handler(vq, assign, !assign);
+    if (!assign) {
+        /* Use generic ioeventfd handler again. */
+        dev->ioeventfd_disabled = false;
     }
-    return virtio_ccw_set_guest2host_notifier(dev, n, assign, false);
+    return 0;
 }
 
 static int virtio_ccw_get_mappings(VirtioCcwDevice *dev)
hw/virtio/virtio-mmio.c +17 -10
index d4cd91f..aafebdf 100644
@@ -502,19 +502,26 @@ static int virtio_mmio_set_host_notifier(DeviceState *opaque, int n,
                                          bool assign)
 {
     VirtIOMMIOProxy *proxy = VIRTIO_MMIO(opaque);
+    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
+    VirtQueue *vq = virtio_get_queue(vdev, n);
 
-    /* Stop using ioeventfd for virtqueue kick if the device starts using host
-     * notifiers.  This makes it easy to avoid stepping on each others' toes.
-     */
-    proxy->ioeventfd_disabled = assign;
     if (assign) {
-        virtio_mmio_stop_ioeventfd(proxy);
+        /* Stop using the generic ioeventfd, we are doing eventfd handling
+         * ourselves below */
+        proxy->ioeventfd_disabled = true;
+    };
+    /*
+     * Just switch the handler, don't deassign the ioeventfd.
+     * Otherwise, there's a window where we don't have an
+     * ioeventfd and we may end up with a notification where
+     * we don't expect one.
+     */
+    virtio_queue_set_host_notifier_fd_handler(vq, assign, !assign);
+    if (!assign) {
+        /* Use generic ioeventfd handler again. */
+        proxy->ioeventfd_disabled = false;
     }
-    /* We don't need to start here: it's not needed because backend
-     * currently only stops on status change away from ok,
-     * reset, vmstop and such. If we do add code to start here,
-     * need to check vmstate, device state etc. */
-    return virtio_mmio_set_host_notifier_internal(proxy, n, assign, false);
+    return 0;
 }
 
 /* virtio-mmio device */
hw/virtio/virtio-pci.c +18 -10
index 0dadb66..a91c1e8 100644
@@ -1115,18 +1115,26 @@ static int virtio_pci_set_host_notifier(DeviceState *d, int n, bool assign)
 {
     VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
 
-    /* Stop using ioeventfd for virtqueue kick if the device starts using host
-     * notifiers.  This makes it easy to avoid stepping on each others' toes.
-     */
-    proxy->ioeventfd_disabled = assign;
+    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
+    VirtQueue *vq = virtio_get_queue(vdev, n);
+
     if (assign) {
-        virtio_pci_stop_ioeventfd(proxy);
+        /* Stop using the generic ioeventfd, we are doing eventfd handling
+         * ourselves below */
+        proxy->ioeventfd_disabled = true;
+    };
+    /*
+     * Just switch the handler, don't deassign the ioeventfd.
+     * Otherwise, there's a window where we don't have an
+     * ioeventfd and we may end up with a notification where
+     * we don't expect one.
+     */
+    virtio_queue_set_host_notifier_fd_handler(vq, assign, !assign);
+    if (!assign) {
+        /* Use generic ioeventfd handler again. */
+        proxy->ioeventfd_disabled = false;
     }
-    /* We don't need to start here: it's not needed because backend
-     * currently only stops on status change away from ok,
-     * reset, vmstop and such. If we do add code to start here,
-     * need to check vmstate, device state etc. */
-    return virtio_pci_set_host_notifier_internal(proxy, n, assign, false);
+    return 0;
 }
 
 static void virtio_pci_vmstate_change(DeviceState *d, bool running)
include/hw/virtio/virtio-bus.h +0 -0
index 3f2c136..98c660a 100644

include/hw/virtio/virtio-bus.h typedef struct VirtioBusClass { · line 61

Quoted 10 lines
include/hw/virtio/virtio-bus.h +4 -0
@@ -52,6 +52,10 @@ typedef struct VirtioBusClass {
     bool (*has_extra_state)(DeviceState *d);
     bool (*query_guest_notifiers)(DeviceState *d);
     int (*set_guest_notifiers)(DeviceState *d, int nvqs, bool assign);
+    /*
+     * Switch from/to the generic ioeventfd handler.
+     * assigned==false means 'use generic ioeventfd handler'.
+     */
     int (*set_host_notifier)(DeviceState *d, int n, bool assigned);
     void (*vmstate_change)(DeviceState *d, bool running);
     /*

(survives about 15 minutes of reboot loops)

Paolo

#3 lore raw

Re: [Qemu-devel] [PATCH 1/1] virtio: fix ioeventfd assignment race

In reply to Cornelia Huck
Quoted 8 lines · Cornelia Huck

The ->set_host_notifier() callback is invoked whenever we want to switch from or to the generic ioeventfd handler. Currently, all transports deregister the ioeventfd backing and then re-register it. This opens a race window where we are without ioeventfd backing for a time period: In the virtio-blk dataplane case, we observed notifications coming in from both the vcpu thread and the iothread.

Quoted 4 lines

Let's change pci, mmio and ccw to keep the ioeventfd during ->set_host_notifier() and only switch the ioeventfd handler.

Seems to work fine. Next I will test Michaels patches.

Quoted 141 lines
---
 hw/s390x/virtio-ccw.c          | 22 +++++++++++++++++-----
 hw/virtio/virtio-mmio.c        | 27 +++++++++++++++++----------
 hw/virtio/virtio-pci.c         | 28 ++++++++++++++++++----------
 include/hw/virtio/virtio-bus.h |  4 ++++
 4 files changed, 56 insertions(+), 25 deletions(-)
hw/s390x/virtio-ccw.c +17 -5
index cb887ba..7b1088e 100644
@@ -1196,14 +1196,26 @@ static bool virtio_ccw_query_guest_notifiers(DeviceState *d)
 static int virtio_ccw_set_host_notifier(DeviceState *d, int n, bool assign)
 {
     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
+    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
+    VirtQueue *vq = virtio_get_queue(vdev, n);
 
-    /* Stop using the generic ioeventfd, we are doing eventfd handling
-     * ourselves below */
-    dev->ioeventfd_disabled = assign;
     if (assign) {
-        virtio_ccw_stop_ioeventfd(dev);
+        /* Stop using the generic ioeventfd, we are doing eventfd handling
+         * ourselves below */
+        dev->ioeventfd_disabled = true;
+    };
+    /*
+     * Just switch the handler, don't deassign the ioeventfd.
+     * Otherwise, there's a window where we don't have an
+     * ioeventfd and we may end up with a notification where
+     * we don't expect one.
+     */
+    virtio_queue_set_host_notifier_fd_handler(vq, assign, !assign);
+    if (!assign) {
+        /* Use generic ioeventfd handler again. */
+        dev->ioeventfd_disabled = false;
     }
-    return virtio_ccw_set_guest2host_notifier(dev, n, assign, false);
+    return 0;
 }
 
 static int virtio_ccw_get_mappings(VirtioCcwDevice *dev)
hw/virtio/virtio-mmio.c +17 -10
index d4cd91f..aafebdf 100644
@@ -502,19 +502,26 @@ static int virtio_mmio_set_host_notifier(DeviceState *opaque, int n,
                                          bool assign)
 {
     VirtIOMMIOProxy *proxy = VIRTIO_MMIO(opaque);
+    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
+    VirtQueue *vq = virtio_get_queue(vdev, n);
 
-    /* Stop using ioeventfd for virtqueue kick if the device starts using host
-     * notifiers.  This makes it easy to avoid stepping on each others' toes.
-     */
-    proxy->ioeventfd_disabled = assign;
     if (assign) {
-        virtio_mmio_stop_ioeventfd(proxy);
+        /* Stop using the generic ioeventfd, we are doing eventfd handling
+         * ourselves below */
+        proxy->ioeventfd_disabled = true;
+    };
+    /*
+     * Just switch the handler, don't deassign the ioeventfd.
+     * Otherwise, there's a window where we don't have an
+     * ioeventfd and we may end up with a notification where
+     * we don't expect one.
+     */
+    virtio_queue_set_host_notifier_fd_handler(vq, assign, !assign);
+    if (!assign) {
+        /* Use generic ioeventfd handler again. */
+        proxy->ioeventfd_disabled = false;
     }
-    /* We don't need to start here: it's not needed because backend
-     * currently only stops on status change away from ok,
-     * reset, vmstop and such. If we do add code to start here,
-     * need to check vmstate, device state etc. */
-    return virtio_mmio_set_host_notifier_internal(proxy, n, assign, false);
+    return 0;
 }
 
 /* virtio-mmio device */
hw/virtio/virtio-pci.c +18 -10
index 0dadb66..a91c1e8 100644
@@ -1115,18 +1115,26 @@ static int virtio_pci_set_host_notifier(DeviceState *d, int n, bool assign)
 {
     VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
 
-    /* Stop using ioeventfd for virtqueue kick if the device starts using host
-     * notifiers.  This makes it easy to avoid stepping on each others' toes.
-     */
-    proxy->ioeventfd_disabled = assign;
+    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
+    VirtQueue *vq = virtio_get_queue(vdev, n);
+
     if (assign) {
-        virtio_pci_stop_ioeventfd(proxy);
+        /* Stop using the generic ioeventfd, we are doing eventfd handling
+         * ourselves below */
+        proxy->ioeventfd_disabled = true;
+    };
+    /*
+     * Just switch the handler, don't deassign the ioeventfd.
+     * Otherwise, there's a window where we don't have an
+     * ioeventfd and we may end up with a notification where
+     * we don't expect one.
+     */
+    virtio_queue_set_host_notifier_fd_handler(vq, assign, !assign);
+    if (!assign) {
+        /* Use generic ioeventfd handler again. */
+        proxy->ioeventfd_disabled = false;
     }
-    /* We don't need to start here: it's not needed because backend
-     * currently only stops on status change away from ok,
-     * reset, vmstop and such. If we do add code to start here,
-     * need to check vmstate, device state etc. */
-    return virtio_pci_set_host_notifier_internal(proxy, n, assign, false);
+    return 0;
 }
 
 static void virtio_pci_vmstate_change(DeviceState *d, bool running)
include/hw/virtio/virtio-bus.h +4 -0
index 3f2c136..98c660a 100644
@@ -52,6 +52,10 @@ typedef struct VirtioBusClass {
     bool (*has_extra_state)(DeviceState *d);
     bool (*query_guest_notifiers)(DeviceState *d);
     int (*set_guest_notifiers)(DeviceState *d, int nvqs, bool assign);
+    /*
+     * Switch from/to the generic ioeventfd handler.
+     * assigned==false means 'use generic ioeventfd handler'.
+     */
     int (*set_host_notifier)(DeviceState *d, int n, bool assigned);
     void (*vmstate_change)(DeviceState *d, bool running);
     /*
#4 lore raw

Re: [Qemu-devel] [PATCH 1/1] virtio: fix ioeventfd assignment race

In reply to Cornelia Huck

Hi Cornelia:

I saw qemu crash for this patch on qemu master yesterday.

However, scsi disks of my lpar is not available because of s38 firmware update. I'll double check the test when it's ready. thx

Quoted 153 lines · Cornelia Huck

The ->set_host_notifier() callback is invoked whenever we want to switch from or to the generic ioeventfd handler. Currently, all transports deregister the ioeventfd backing and then re-register it. This opens a race window where we are without ioeventfd backing for a time period: In the virtio-blk dataplane case, we observed notifications coming in from both the vcpu thread and the iothread.

Let's change pci, mmio and ccw to keep the ioeventfd during ->set_host_notifier() and only switch the ioeventfd handler.

---
  hw/s390x/virtio-ccw.c          | 22 +++++++++++++++++-----
  hw/virtio/virtio-mmio.c        | 27 +++++++++++++++++----------
  hw/virtio/virtio-pci.c         | 28 ++++++++++++++++++----------
  include/hw/virtio/virtio-bus.h |  4 ++++
  4 files changed, 56 insertions(+), 25 deletions(-)
hw/s390x/virtio-ccw.c +17 -5
index cb887ba..7b1088e 100644
@@ -1196,14 +1196,26 @@ static bool virtio_ccw_query_guest_notifiers(DeviceState *d)
  static int virtio_ccw_set_host_notifier(DeviceState *d, int n, bool assign)
  {
      VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
+    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
+    VirtQueue *vq = virtio_get_queue(vdev, n);
 
-    /* Stop using the generic ioeventfd, we are doing eventfd handling
-     * ourselves below */
-    dev->ioeventfd_disabled = assign;
      if (assign) {
-        virtio_ccw_stop_ioeventfd(dev);
+        /* Stop using the generic ioeventfd, we are doing eventfd handling
+         * ourselves below */
+        dev->ioeventfd_disabled = true;
+    };
+    /*
+     * Just switch the handler, don't deassign the ioeventfd.
+     * Otherwise, there's a window where we don't have an
+     * ioeventfd and we may end up with a notification where
+     * we don't expect one.
+     */
+    virtio_queue_set_host_notifier_fd_handler(vq, assign, !assign);
+    if (!assign) {
+        /* Use generic ioeventfd handler again. */
+        dev->ioeventfd_disabled = false;
      }
-    return virtio_ccw_set_guest2host_notifier(dev, n, assign, false);
+    return 0;
  }
 
  static int virtio_ccw_get_mappings(VirtioCcwDevice *dev)
hw/virtio/virtio-mmio.c +17 -10
index d4cd91f..aafebdf 100644
@@ -502,19 +502,26 @@ static int virtio_mmio_set_host_notifier(DeviceState *opaque, int n,
                                           bool assign)
  {
      VirtIOMMIOProxy *proxy = VIRTIO_MMIO(opaque);
+    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
+    VirtQueue *vq = virtio_get_queue(vdev, n);
 
-    /* Stop using ioeventfd for virtqueue kick if the device starts using host
-     * notifiers.  This makes it easy to avoid stepping on each others' toes.
-     */
-    proxy->ioeventfd_disabled = assign;
      if (assign) {
-        virtio_mmio_stop_ioeventfd(proxy);
+        /* Stop using the generic ioeventfd, we are doing eventfd handling
+         * ourselves below */
+        proxy->ioeventfd_disabled = true;
+    };
+    /*
+     * Just switch the handler, don't deassign the ioeventfd.
+     * Otherwise, there's a window where we don't have an
+     * ioeventfd and we may end up with a notification where
+     * we don't expect one.
+     */
+    virtio_queue_set_host_notifier_fd_handler(vq, assign, !assign);
+    if (!assign) {
+        /* Use generic ioeventfd handler again. */
+        proxy->ioeventfd_disabled = false;
      }
-    /* We don't need to start here: it's not needed because backend
-     * currently only stops on status change away from ok,
-     * reset, vmstop and such. If we do add code to start here,
-     * need to check vmstate, device state etc. */
-    return virtio_mmio_set_host_notifier_internal(proxy, n, assign, false);
+    return 0;
  }
 
  /* virtio-mmio device */
hw/virtio/virtio-pci.c +18 -10
index 0dadb66..a91c1e8 100644
@@ -1115,18 +1115,26 @@ static int virtio_pci_set_host_notifier(DeviceState *d, int n, bool assign)
  {
      VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
 
-    /* Stop using ioeventfd for virtqueue kick if the device starts using host
-     * notifiers.  This makes it easy to avoid stepping on each others' toes.
-     */
-    proxy->ioeventfd_disabled = assign;
+    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
+    VirtQueue *vq = virtio_get_queue(vdev, n);
+
      if (assign) {
-        virtio_pci_stop_ioeventfd(proxy);
+        /* Stop using the generic ioeventfd, we are doing eventfd handling
+         * ourselves below */
+        proxy->ioeventfd_disabled = true;
+    };
+    /*
+     * Just switch the handler, don't deassign the ioeventfd.
+     * Otherwise, there's a window where we don't have an
+     * ioeventfd and we may end up with a notification where
+     * we don't expect one.
+     */
+    virtio_queue_set_host_notifier_fd_handler(vq, assign, !assign);
+    if (!assign) {
+        /* Use generic ioeventfd handler again. */
+        proxy->ioeventfd_disabled = false;
      }
-    /* We don't need to start here: it's not needed because backend
-     * currently only stops on status change away from ok,
-     * reset, vmstop and such. If we do add code to start here,
-     * need to check vmstate, device state etc. */
-    return virtio_pci_set_host_notifier_internal(proxy, n, assign, false);
+    return 0;
  }
 
  static void virtio_pci_vmstate_change(DeviceState *d, bool running)
include/hw/virtio/virtio-bus.h +4 -0
index 3f2c136..98c660a 100644
@@ -52,6 +52,10 @@ typedef struct VirtioBusClass {
      bool (*has_extra_state)(DeviceState *d);
      bool (*query_guest_notifiers)(DeviceState *d);
      int (*set_guest_notifiers)(DeviceState *d, int nvqs, bool assign);
+    /*
+     * Switch from/to the generic ioeventfd handler.
+     * assigned==false means 'use generic ioeventfd handler'.
+     */
      int (*set_host_notifier)(DeviceState *d, int n, bool assigned);
      void (*vmstate_change)(DeviceState *d, bool running);
      /*
#5 lore raw

Re: [Qemu-devel] [PATCH 1/1] virtio: fix ioeventfd assignment race

Hi Cornelia:

Quoted 6 lines · tu bo

Hi Cornelia:

I saw qemu crash for this patch on qemu master yesterday.

However, scsi disks of my lpar is not available because of s38 firmware update. I'll double check the test when it's ready. thx

I still can see crash like below,

(gdb) bt

#0  blk_aio_read_entry (opaque=0x0) at block/block-backend.c:916
#1  0x000002aa0d6e873e in coroutine_trampoline (i0=<optimized out>, i1=1275094848) at util/coroutine-ucontext.c:78
#2  0x000003ffab4d150a in __makecontext_ret () from /lib64/libc.so.6

(gdb) list
911 static void blk_aio_read_entry(void *opaque)
912 {
913 BlkAioEmAIOCB *acb = opaque;
914 BlkRwCo *rwco = &acb->rwco;
915
916 rwco->ret = blk_co_preadv(rwco->blk, rwco->offset, rwco->qiov->size,
917 rwco->qiov, rwco->flags);
918 blk_aio_complete(acb);
919 }
920

Quoted 169 lines · Cornelia Huck
Quoted 167 lines

The ->set_host_notifier() callback is invoked whenever we want to switch from or to the generic ioeventfd handler. Currently, all transports deregister the ioeventfd backing and then re-register it. This opens a race window where we are without ioeventfd backing for a time period: In the virtio-blk dataplane case, we observed notifications coming in from both the vcpu thread and the iothread.

Let's change pci, mmio and ccw to keep the ioeventfd during ->set_host_notifier() and only switch the ioeventfd handler.

---
  hw/s390x/virtio-ccw.c          | 22 +++++++++++++++++-----
  hw/virtio/virtio-mmio.c        | 27 +++++++++++++++++----------
  hw/virtio/virtio-pci.c         | 28 ++++++++++++++++++----------
  include/hw/virtio/virtio-bus.h |  4 ++++
  4 files changed, 56 insertions(+), 25 deletions(-)
hw/s390x/virtio-ccw.c +3 -4
index cb887ba..7b1088e 100644
@@ -1196,14 +1196,26 @@ static bool
 virtio_ccw_query_guest_notifiers(DeviceState *d)
  static int virtio_ccw_set_host_notifier(DeviceState *d, int n, bool
 assign)
  {
      VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
+    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
+    VirtQueue *vq = virtio_get_queue(vdev, n);
 
-    /* Stop using the generic ioeventfd, we are doing eventfd handling
-     * ourselves below */
-    dev->ioeventfd_disabled = assign;
      if (assign) {
-        virtio_ccw_stop_ioeventfd(dev);
+        /* Stop using the generic ioeventfd, we are doing eventfd

handling

hw/s390x/virtio-ccw.c +14 -1
+         * ourselves below */
+        dev->ioeventfd_disabled = true;
+    };
+    /*
+     * Just switch the handler, don't deassign the ioeventfd.
+     * Otherwise, there's a window where we don't have an
+     * ioeventfd and we may end up with a notification where
+     * we don't expect one.
+     */
+    virtio_queue_set_host_notifier_fd_handler(vq, assign, !assign);
+    if (!assign) {
+        /* Use generic ioeventfd handler again. */
+        dev->ioeventfd_disabled = false;
      }
-    return virtio_ccw_set_guest2host_notifier(dev, n, assign, false);
+    return 0;
  }
 
  static int virtio_ccw_get_mappings(VirtioCcwDevice *dev)
hw/virtio/virtio-mmio.c +2 -1
index d4cd91f..aafebdf 100644
@@ -502,19 +502,26 @@ static int
 virtio_mmio_set_host_notifier(DeviceState *opaque, int n,
                                           bool assign)
  {
      VirtIOMMIOProxy *proxy = VIRTIO_MMIO(opaque);
+    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
+    VirtQueue *vq = virtio_get_queue(vdev, n);
 
-    /* Stop using ioeventfd for virtqueue kick if the device starts

using host
- * notifiers. This makes it easy to avoid stepping on each
others' toes.

hw/virtio/virtio-mmio.c +1 -3
-     */
-    proxy->ioeventfd_disabled = assign;
      if (assign) {
-        virtio_mmio_stop_ioeventfd(proxy);
+        /* Stop using the generic ioeventfd, we are doing eventfd

handling

hw/virtio/virtio-mmio.c +13 -5
+         * ourselves below */
+        proxy->ioeventfd_disabled = true;
+    };
+    /*
+     * Just switch the handler, don't deassign the ioeventfd.
+     * Otherwise, there's a window where we don't have an
+     * ioeventfd and we may end up with a notification where
+     * we don't expect one.
+     */
+    virtio_queue_set_host_notifier_fd_handler(vq, assign, !assign);
+    if (!assign) {
+        /* Use generic ioeventfd handler again. */
+        proxy->ioeventfd_disabled = false;
      }
-    /* We don't need to start here: it's not needed because backend
-     * currently only stops on status change away from ok,
-     * reset, vmstop and such. If we do add code to start here,
-     * need to check vmstate, device state etc. */
-    return virtio_mmio_set_host_notifier_internal(proxy, n, assign,

false);

hw/virtio/virtio-mmio.c +1 -0
+    return 0;
  }
 
  /* virtio-mmio device */
hw/virtio/virtio-pci.c +0 -1
index 0dadb66..a91c1e8 100644
@@ -1115,18 +1115,26 @@ static int
 virtio_pci_set_host_notifier(DeviceState *d, int n, bool assign)
  {
      VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
 
-    /* Stop using ioeventfd for virtqueue kick if the device starts

using host
- * notifiers. This makes it easy to avoid stepping on each
others' toes.

hw/virtio/virtio-pci.c +4 -3
-     */
-    proxy->ioeventfd_disabled = assign;
+    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
+    VirtQueue *vq = virtio_get_queue(vdev, n);
+
      if (assign) {
-        virtio_pci_stop_ioeventfd(proxy);
+        /* Stop using the generic ioeventfd, we are doing eventfd

handling

hw/virtio/virtio-pci.c +13 -5
+         * ourselves below */
+        proxy->ioeventfd_disabled = true;
+    };
+    /*
+     * Just switch the handler, don't deassign the ioeventfd.
+     * Otherwise, there's a window where we don't have an
+     * ioeventfd and we may end up with a notification where
+     * we don't expect one.
+     */
+    virtio_queue_set_host_notifier_fd_handler(vq, assign, !assign);
+    if (!assign) {
+        /* Use generic ioeventfd handler again. */
+        proxy->ioeventfd_disabled = false;
      }
-    /* We don't need to start here: it's not needed because backend
-     * currently only stops on status change away from ok,
-     * reset, vmstop and such. If we do add code to start here,
-     * need to check vmstate, device state etc. */
-    return virtio_pci_set_host_notifier_internal(proxy, n, assign,

false);

hw/virtio/virtio-pci.c +1 -0
+    return 0;
  }
 
  static void virtio_pci_vmstate_change(DeviceState *d, bool running)
include/hw/virtio/virtio-bus.h +4 -0
index 3f2c136..98c660a 100644
@@ -52,6 +52,10 @@ typedef struct VirtioBusClass {
      bool (*has_extra_state)(DeviceState *d);
      bool (*query_guest_notifiers)(DeviceState *d);
      int (*set_guest_notifiers)(DeviceState *d, int nvqs, bool assign);
+    /*
+     * Switch from/to the generic ioeventfd handler.
+     * assigned==false means 'use generic ioeventfd handler'.
+     */
      int (*set_host_notifier)(DeviceState *d, int n, bool assigned);
      void (*vmstate_change)(DeviceState *d, bool running);
      /*