Skip to contentSkip to conversation

vhost: configure all host notifiers in a single MR transaction

PATCH Superseded Longpeng(Mike) via 3 participants 5 messages Nov 18, 2022 – Nov 29, 2022

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vhost: configure all host notifiers in a single MR transaction

From: Longpeng <[email protected]>

This allows the vhost device to batch the setup of all its host notifiers. This significantly reduces the device starting time, e.g. the vhost-vDPA generic device [1] start time reduce from 376ms to 9.1ms for a VM with 64 vCPUs and 3 vDPA device(64vq per device).

[1] https://www.mail-archive.com/[email protected]/msg921541.html

---
 hw/virtio/vhost.c | 39 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)
hw/virtio/vhost.c +38 -1
index d1c4c20b8c..bf82d9b176 100644
@@ -1507,6 +1507,7 @@ void vhost_dev_cleanup(struct vhost_dev *hdev)
 int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
 {
     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
+    int vq_init_count = 0;
     int i, r, e;
 
     /* We will pass the notifiers to the kernel, make sure that QEMU
@@ -1518,6 +1519,12 @@ int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
         goto fail;
     }
 
+    /*
+     * Batch all the host notifiers in a single transaction to avoid
+     * quadratic time complexity in address_space_update_ioeventfds().
+     */
+    memory_region_transaction_begin();
+
     for (i = 0; i < hdev->nvqs; ++i) {
         r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i,
                                          true);
@@ -1525,19 +1532,33 @@ int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
             error_report("vhost VQ %d notifier binding failed: %d", i, -r);
             goto fail_vq;
         }
+
+        vq_init_count++;
     }
 
+    memory_region_transaction_commit();
+
     return 0;
 fail_vq:
-    while (--i >= 0) {
+    for (i = 0; i < vq_init_count; i++) {
         e = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i,
                                          false);
         if (e < 0) {
             error_report("vhost VQ %d notifier cleanup error: %d", i, -r);
         }
         assert (e >= 0);
+    }
+
+    /*
+     * The transaction expects the ioeventfds to be open when it
+     * commits. Do it now, before the cleanup loop.
+     */
+    memory_region_transaction_commit();
+
+    for (i = 0; i < vq_init_count; i++) {
         virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i);
     }
+
     virtio_device_release_ioeventfd(vdev);
 fail:
     return r;
@@ -1553,6 +1574,12 @@ void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
     int i, r;
 
+    /*
+     * Batch all the host notifiers in a single transaction to avoid
+     * quadratic time complexity in address_space_update_ioeventfds().
+     */
+    memory_region_transaction_begin();
+
     for (i = 0; i < hdev->nvqs; ++i) {
         r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i,
                                          false);
@@ -1560,8 +1587,18 @@ void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
             error_report("vhost VQ %d notifier cleanup failed: %d", i, -r);
         }
         assert (r >= 0);
+    }
+
+    /*
+     * The transaction expects the ioeventfds to be open when it
+     * commits. Do it now, before the cleanup loop.
+     */
+    memory_region_transaction_commit();
+
+    for (i = 0; i < hdev->nvqs; ++i) {
         virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i);
     }
+
     virtio_device_release_ioeventfd(vdev);
 }
 
Signature · 1 line
2.23.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Longpeng(Mike) via

From: Longpeng <[email protected]>

This allows the vhost device to batch the setup of all its host notifiers. This significantly reduces the device starting time, e.g. the vhost-vDPA generic device [1] start time reduce from 376ms to 9.1ms for a VM with 64 vCPUs and 3 vDPA device(64vq per device).

Quoted 6 lines

[1] https://www.mail-archive.com/[email protected]/msg921541.html

---
 hw/virtio/vhost.c | 39 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)
#2 lore raw
In reply to Longpeng(Mike) via
Quoted 7 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

This allows the vhost device to batch the setup of all its host notifiers. This significantly reduces the device starting time, e.g. the vhost-vDPA generic device [1] start time reduce from 376ms to 9.1ms for a VM with 64 vCPUs and 3 vDPA device(64vq per device).

Great, I think we need to do this for host_notifiers_mr as well. This helps for the case when the notification area could be mapped directly to guests.

Quoted 34 lines

[1] https://www.mail-archive.com/[email protected]/msg921541.html

---
 hw/virtio/vhost.c | 39 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)
hw/virtio/vhost.c +7 -0
index d1c4c20b8c..bf82d9b176 100644
@@ -1507,6 +1507,7 @@ void vhost_dev_cleanup(struct vhost_dev *hdev)
 int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
 {
     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
+    int vq_init_count = 0;
     int i, r, e;
 
     /* We will pass the notifiers to the kernel, make sure that QEMU
@@ -1518,6 +1519,12 @@ int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
         goto fail;
     }
 
+    /*
+     * Batch all the host notifiers in a single transaction to avoid
+     * quadratic time complexity in address_space_update_ioeventfds().
+     */
+    memory_region_transaction_begin();
+
     for (i = 0; i < hdev->nvqs; ++i) {
         r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i,
                                          true);

hw/virtio/vhost.c int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev) · line 1537

Quoted 6 lines
hw/virtio/vhost.c +2 -0
@@ -1525,19 +1532,33 @@ int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
             error_report("vhost VQ %d notifier binding failed: %d", i, -r);
             goto fail_vq;
         }
+
+        vq_init_count++;
     }

Nit, the name needs some tweak, it's actually the number of the host notifiers that is initialized. And we can count it out of the loop.

hw/virtio/vhost.c int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev) · line 1543

Quoted 7 lines
hw/virtio/vhost.c +3 -1
+    memory_region_transaction_commit();
+
     return 0;
 fail_vq:
-    while (--i >= 0) {
+    for (i = 0; i < vq_init_count; i++) {

It looks to me there's no need for this change.

Others look good.

Thanks

Quoted 56 lines
hw/virtio/vhost.c +26 -1
         e = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i,
                                          false);
         if (e < 0) {
             error_report("vhost VQ %d notifier cleanup error: %d", i, -r);
         }
         assert (e >= 0);
+    }
+
+    /*
+     * The transaction expects the ioeventfds to be open when it
+     * commits. Do it now, before the cleanup loop.
+     */
+    memory_region_transaction_commit();
+
+    for (i = 0; i < vq_init_count; i++) {
         virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i);
     }
+
     virtio_device_release_ioeventfd(vdev);
 fail:
     return r;
@@ -1553,6 +1574,12 @@ void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
     int i, r;
 
+    /*
+     * Batch all the host notifiers in a single transaction to avoid
+     * quadratic time complexity in address_space_update_ioeventfds().
+     */
+    memory_region_transaction_begin();
+
     for (i = 0; i < hdev->nvqs; ++i) {
         r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i,
                                          false);
@@ -1560,8 +1587,18 @@ void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
             error_report("vhost VQ %d notifier cleanup failed: %d", i, -r);
         }
         assert (r >= 0);
+    }
+
+    /*
+     * The transaction expects the ioeventfds to be open when it
+     * commits. Do it now, before the cleanup loop.
+     */
+    memory_region_transaction_commit();
+
+    for (i = 0; i < hdev->nvqs; ++i) {
         virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i);
     }
+
     virtio_device_release_ioeventfd(vdev);
 }
 
--

2.23.0

#3 lore raw
Quoted 4 lines · Jason Wang
Quoted 3 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

Quoted 9 lines
Quoted 4 lines

This allows the vhost device to batch the setup of all its host notifiers. This significantly reduces the device starting time, e.g. the vhost-vDPA generic device [1] start time reduce from 376ms to 9.1ms for a VM with 64 vCPUs and 3 vDPA device(64vq per device).

Great, I think we need to do this for host_notifiers_mr as well. This helps for the case when the notification area could be mapped directly to guests.

Batch and commit once for host_notifiers_mrs can reduce the cost from 423ms to 32ms, testing on vdpasim_blk (3 devices and 64 vqs per device) with doorbell passthrough support. I'll append a patch in the next version.

Quoted 35 lines
Quoted 35 lines

[1] https://www.mail-archive.com/[email protected]/msg921541.html

---
  hw/virtio/vhost.c | 39 ++++++++++++++++++++++++++++++++++++++-
  1 file changed, 38 insertions(+), 1 deletion(-)
hw/virtio/vhost.c +7 -0
index d1c4c20b8c..bf82d9b176 100644
@@ -1507,6 +1507,7 @@ void vhost_dev_cleanup(struct vhost_dev *hdev)
  int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
  {
      BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
+    int vq_init_count = 0;
      int i, r, e;
 
      /* We will pass the notifiers to the kernel, make sure that QEMU
@@ -1518,6 +1519,12 @@ int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
          goto fail;
      }
 
+    /*
+     * Batch all the host notifiers in a single transaction to avoid
+     * quadratic time complexity in address_space_update_ioeventfds().
+     */
+    memory_region_transaction_begin();
+
      for (i = 0; i < hdev->nvqs; ++i) {
          r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i,
                                           true);
@@ -1525,19 +1532,33 @@ int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
              error_report("vhost VQ %d notifier binding failed: %d", i, -r);
Quoted 9 lines

hw/virtio/vhost.c int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev) · line 1537

Quoted 5 lines
hw/virtio/vhost.c +2 -0
              goto fail_vq;
          }
+
+        vq_init_count++;
      }

Nit, the name needs some tweak, it's actually the number of the host notifiers that is initialized. And we can count it out of the loop.

Ok, I will refer to virtio_device_start_ioeventfd_impl().

Quoted 72 lines

hw/virtio/vhost.c int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev) · line 1543

Quoted 7 lines
hw/virtio/vhost.c +3 -1
+    memory_region_transaction_commit();
+
      return 0;
  fail_vq:
-    while (--i >= 0) {
+    for (i = 0; i < vq_init_count; i++) {

It looks to me there's no need for this change.

Others look good.

Thanks

Quoted 54 lines
hw/virtio/vhost.c +26 -1
          e = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i,
                                           false);
          if (e < 0) {
              error_report("vhost VQ %d notifier cleanup error: %d", i, -r);
          }
          assert (e >= 0);
+    }
+
+    /*
+     * The transaction expects the ioeventfds to be open when it
+     * commits. Do it now, before the cleanup loop.
+     */
+    memory_region_transaction_commit();
+
+    for (i = 0; i < vq_init_count; i++) {
          virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i);
      }
+
      virtio_device_release_ioeventfd(vdev);
  fail:
      return r;
@@ -1553,6 +1574,12 @@ void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
      BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
      int i, r;
 
+    /*
+     * Batch all the host notifiers in a single transaction to avoid
+     * quadratic time complexity in address_space_update_ioeventfds().
+     */
+    memory_region_transaction_begin();
+
      for (i = 0; i < hdev->nvqs; ++i) {
          r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i,
                                           false);
@@ -1560,8 +1587,18 @@ void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
              error_report("vhost VQ %d notifier cleanup failed: %d", i, -r);
          }
          assert (r >= 0);
+    }
+
+    /*
+     * The transaction expects the ioeventfds to be open when it
+     * commits. Do it now, before the cleanup loop.
+     */
+    memory_region_transaction_commit();
+
+    for (i = 0; i < hdev->nvqs; ++i) {
          virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i);
      }
+
      virtio_device_release_ioeventfd(vdev);
  }
 
--
Quoted 2 lines

2.23.0

.

#4 lore raw
Quoted 13 lines · Cloud Infrastructure Service Product Dept.)
Quoted 9 lines · Jason Wang
Quoted 7 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

This allows the vhost device to batch the setup of all its host notifiers. This significantly reduces the device starting time, e.g. the vhost-vDPA generic device [1] start time reduce from 376ms to 9.1ms for a VM with 64 vCPUs and 3 vDPA device(64vq per device).

Quoted 8 lines
Quoted 4 lines

Great, I think we need to do this for host_notifiers_mr as well. This helps for the case when the notification area could be mapped directly to guests.

Batch and commit once for host_notifiers_mrs can reduce the cost from 423ms to 32ms, testing on vdpasim_blk (3 devices and 64 vqs per device) with doorbell passthrough support.
I'll append a patch in the next version.

Great.

Thanks

Quoted 120 lines
Quoted 36 lines
Quoted 36 lines

[1] https://www.mail-archive.com/[email protected]/msg921541.html

---
  hw/virtio/vhost.c | 39 ++++++++++++++++++++++++++++++++++++++-
  1 file changed, 38 insertions(+), 1 deletion(-)
hw/virtio/vhost.c +7 -0
index d1c4c20b8c..bf82d9b176 100644
@@ -1507,6 +1507,7 @@ void vhost_dev_cleanup(struct vhost_dev *hdev)
  int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
  {
      BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
+    int vq_init_count = 0;
      int i, r, e;
 
      /* We will pass the notifiers to the kernel, make sure that QEMU
@@ -1518,6 +1519,12 @@ int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
          goto fail;
      }
 
+    /*
+     * Batch all the host notifiers in a single transaction to avoid
+     * quadratic time complexity in address_space_update_ioeventfds().
+     */
+    memory_region_transaction_begin();
+
      for (i = 0; i < hdev->nvqs; ++i) {
          r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i,
                                           true);
@@ -1525,19 +1532,33 @@ int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
              error_report("vhost VQ %d notifier binding failed: %d", i, -r);
Quoted 9 lines

hw/virtio/vhost.c int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev) · line 1537

Quoted 5 lines
hw/virtio/vhost.c +2 -0
              goto fail_vq;
          }
+
+        vq_init_count++;
      }

Nit, the name needs some tweak, it's actually the number of the host notifiers that is initialized. And we can count it out of the loop.

Ok, I will refer to virtio_device_start_ioeventfd_impl().

Quoted 72 lines

hw/virtio/vhost.c int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev) · line 1543

Quoted 7 lines
hw/virtio/vhost.c +3 -1
+    memory_region_transaction_commit();
+
      return 0;
  fail_vq:
-    while (--i >= 0) {
+    for (i = 0; i < vq_init_count; i++) {

It looks to me there's no need for this change.

Others look good.

Thanks

Quoted 54 lines
hw/virtio/vhost.c +26 -1
          e = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i,
                                           false);
          if (e < 0) {
              error_report("vhost VQ %d notifier cleanup error: %d", i, -r);
          }
          assert (e >= 0);
+    }
+
+    /*
+     * The transaction expects the ioeventfds to be open when it
+     * commits. Do it now, before the cleanup loop.
+     */
+    memory_region_transaction_commit();
+
+    for (i = 0; i < vq_init_count; i++) {
          virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i);
      }
+
      virtio_device_release_ioeventfd(vdev);
  fail:
      return r;
@@ -1553,6 +1574,12 @@ void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
      BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
      int i, r;
 
+    /*
+     * Batch all the host notifiers in a single transaction to avoid
+     * quadratic time complexity in address_space_update_ioeventfds().
+     */
+    memory_region_transaction_begin();
+
      for (i = 0; i < hdev->nvqs; ++i) {
          r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i,
                                           false);
@@ -1560,8 +1587,18 @@ void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
              error_report("vhost VQ %d notifier cleanup failed: %d", i, -r);
          }
          assert (r >= 0);
+    }
+
+    /*
+     * The transaction expects the ioeventfds to be open when it
+     * commits. Do it now, before the cleanup loop.
+     */
+    memory_region_transaction_commit();
+
+    for (i = 0; i < hdev->nvqs; ++i) {
          virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i);
      }
+
      virtio_device_release_ioeventfd(vdev);
  }
 
--
Quoted 2 lines

2.23.0

.