Skip to contentSkip to conversation

two optimizations to speed up the start time

PATCH In review Longpeng(Mike) via 3 participants 11 messages Dec 27, 2022 – Dec 29, 2022

lore
Share & tools
lore
#0 lore raw

v3 cover two optimizations to speed up the start time

From: Longpeng <[email protected]>

Changes v3->v2:
- cleanup the code [Philippe]

Changes v2->v1:
Patch-1:
- remove vq_init_count [Jason]
Patch-2:
- new added. [Jason]

v1: https://www.mail-archive.com/[email protected]/msg922499.html

Longpeng (Mike) (3):
vhost: simplify vhost_dev_enable_notifiers
vhost: configure all host notifiers in a single MR transaction
vdpa: commit all host notifier MRs in a single MR transaction

 hw/virtio/vhost-vdpa.c | 25 ++++++++++++++++++------
 hw/virtio/vhost.c      | 44 +++++++++++++++++++++++++++---------------
 2 files changed, 47 insertions(+), 22 deletions(-)
Signature · 1 line
2.23.0
#1 lore raw

PATCH v3 1/3 vhost: simplify vhost_dev_enable_notifiers

From: Longpeng <[email protected]>

Simplify the error path in vhost_dev_enable_notifiers by using vhost_dev_disable_notifiers directly.

---
 hw/virtio/vhost.c | 20 ++++----------------
 1 file changed, 4 insertions(+), 16 deletions(-)
hw/virtio/vhost.c +4 -16
index fdcd1a8fdf..5994559da8 100644
@@ -1551,7 +1551,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 i, r, e;
+    int i, r;
 
     /* We will pass the notifiers to the kernel, make sure that QEMU
      * doesn't interfere.
@@ -1559,7 +1559,7 @@ int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
     r = virtio_device_grab_ioeventfd(vdev);
     if (r < 0) {
         error_report("binding does not support host notifiers");
-        goto fail;
+        return r;
     }
 
     for (i = 0; i < hdev->nvqs; ++i) {
@@ -1567,24 +1567,12 @@ int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
                                          true);
         if (r < 0) {
             error_report("vhost VQ %d notifier binding failed: %d", i, -r);
-            goto fail_vq;
+            vhost_dev_disable_notifiers(hdev, vdev);
+            return r;
         }
     }
 
     return 0;
-fail_vq:
-    while (--i >= 0) {
-        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);
-        virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i);
-    }
-    virtio_device_release_ioeventfd(vdev);
-fail:
-    return r;
 }
 
 /* Stop processing guest IO notifications in vhost.
Signature · 1 line
2.23.0
#2 lore raw

PATCH v3 2/3 vhost: configure all host notifiers in a single MR transaction

In reply to 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 time spend on enabling notifiers reduce from 376ms to 9.1ms for a VM with 64 vCPUs and 3 vhost-vDPA generic devices (vdpa_sim_blk, 64vq per device)

---
 hw/virtio/vhost.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
hw/virtio/vhost.c +24 -0
index 5994559da8..064d4abe5c 100644
@@ -1562,16 +1562,25 @@ int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
         return 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,
                                          true);
         if (r < 0) {
             error_report("vhost VQ %d notifier binding failed: %d", i, -r);
+            memory_region_transaction_commit();
             vhost_dev_disable_notifiers(hdev, vdev);
             return r;
         }
     }
 
+    memory_region_transaction_commit();
+
     return 0;
 }
 
@@ -1585,6 +1594,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);
@@ -1592,6 +1607,15 @@ 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
#3 lore raw

Re: [PATCH v3 2/3] vhost: configure all host notifiers in a single MR transaction

Quoted 23 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 time spend on enabling notifiers reduce from 376ms to 9.1ms for a VM with 64 vCPUs and 3 vhost-vDPA generic devices (vdpa_sim_blk, 64vq per device)

---
  hw/virtio/vhost.c | 24 ++++++++++++++++++++++++
  1 file changed, 24 insertions(+)
hw/virtio/vhost.c +3 -0
index 5994559da8..064d4abe5c 100644
@@ -1562,16 +1562,25 @@ int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
          return r;
      }
 
+    /*
+     * Batch all the host notifiers in a single transaction to avoid
+     * quadratic time complexity in address_space_update_ioeventfds().

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

Quoted 10 lines
hw/virtio/vhost.c +4 -0
+     */
+    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);
          if (r < 0) {
              error_report("vhost VQ %d notifier binding failed: %d", i, -r);
+            memory_region_transaction_commit();
              vhost_dev_disable_notifiers(hdev, vdev);

Could we 'break' here, ...

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

Quoted 7 lines
hw/virtio/vhost.c +2 -0
              return r;
          }
      }
 
+    memory_region_transaction_commit();
+
      return 0;

... and return 'r' here?

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

Quoted 1 line
hw/virtio/vhost.c +0 -0
  }

Otherwise LGTM.

#4 lore raw

Re: [PATCH v3 2/3] vhost: configure all host notifiers in a single MR transaction

Quoted 36 lines · Philippe Mathieu-Daudé
Quoted 22 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 time spend on enabling notifiers reduce from 376ms to 9.1ms for a VM with 64 vCPUs and 3 vhost-vDPA generic devices (vdpa_sim_blk, 64vq per device)

---
  hw/virtio/vhost.c | 24 ++++++++++++++++++++++++
  1 file changed, 24 insertions(+)
hw/virtio/vhost.c +3 -0
index 5994559da8..064d4abe5c 100644
@@ -1562,16 +1562,25 @@ int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
          return r;
      }
+    /*
+     * Batch all the host notifiers in a single transaction to avoid
+     * quadratic time complexity in address_space_update_ioeventfds().

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

Quoted 10 lines
hw/virtio/vhost.c +4 -0
+     */
+    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);
          if (r < 0) {
              error_report("vhost VQ %d notifier binding failed: %d", i, -r);
+            memory_region_transaction_commit();
              vhost_dev_disable_notifiers(hdev, vdev);

Could we 'break' here, ...

Quoted 8 lines

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

Quoted 6 lines
hw/virtio/vhost.c +2 -0
              return r;
          }
      }
+    memory_region_transaction_commit();
+
      return 0;

... and return 'r' here?

won't this commit twice? seems ugly ...

Quoted 3 lines

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

Quoted 1 line
hw/virtio/vhost.c +0 -0
  }

Otherwise LGTM.

#5 lore raw

Re: [PATCH v3 2/3] vhost: configure all host notifiers in a single MR transaction

Quoted 44 lines · Michael S. Tsirkin
Quoted 43 lines · Philippe Mathieu-Daudé
Quoted 22 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 time spend on enabling notifiers reduce from 376ms to 9.1ms for a VM with 64 vCPUs and 3 vhost-vDPA generic devices (vdpa_sim_blk, 64vq per device)

---
   hw/virtio/vhost.c | 24 ++++++++++++++++++++++++
   1 file changed, 24 insertions(+)
hw/virtio/vhost.c +3 -0
index 5994559da8..064d4abe5c 100644
@@ -1562,16 +1562,25 @@ int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
           return r;
       }
+    /*
+     * Batch all the host notifiers in a single transaction to avoid
+     * quadratic time complexity in address_space_update_ioeventfds().

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

Quoted 10 lines
hw/virtio/vhost.c +4 -0
+     */
+    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);
           if (r < 0) {
               error_report("vhost VQ %d notifier binding failed: %d", i, -r);
+            memory_region_transaction_commit();
               vhost_dev_disable_notifiers(hdev, vdev);

Could we 'break' here, ...

Quoted 6 lines
hw/virtio/vhost.c +2 -0
               return r;
           }
       }
+    memory_region_transaction_commit();
+
       return 0;
Quoted 4 lines
Quoted 1 line

... and return 'r' here?

won't this commit twice? seems ugly ...

Twice? I meant keep the begin/commit() around the for() to have only *one* commit() call instead of 2:

-- >8 --
+ 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);
         if (r < 0) {
             error_report("vhost VQ %d notifier binding failed: %d", i, 
-r);
             vhost_dev_disable_notifiers(hdev, vdev);
-            return r;
+            break;
         }
     }

+ memory_region_transaction_commit();
+
- return 0;
+ return r;
}
---

Anyhow,

#6 lore raw

Re: [PATCH v3 2/3] vhost: configure all host notifiers in a single MR transaction

Quoted 46 lines · Philippe Mathieu-Daudé
Quoted 45 lines · Michael S. Tsirkin
Quoted 44 lines · Philippe Mathieu-Daudé
Quoted 23 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 time spend on enabling notifiers reduce from 376ms to 9.1ms for a VM with 64 vCPUs and 3 vhost-vDPA generic devices (vdpa_sim_blk, 64vq per device)

---
   hw/virtio/vhost.c | 24 ++++++++++++++++++++++++

   1 file changed, 24 insertions(+)
hw/virtio/vhost.c +3 -0
index 5994559da8..064d4abe5c 100644
@@ -1562,16 +1562,25 @@ int vhost_dev_enable_notifiers(struct 
 vhost_dev *hdev, VirtIODevice *vdev)
            return r;
        }
+    /*
+     * Batch all the host notifiers in a single transaction to avoid
+     * quadratic time complexity in address_space_update_ioeventfds().

hw/virtio/vhost.c int vhost_dev_enable_notifiers(struct · line 1577

Quoted 10 lines
hw/virtio/vhost.c +4 -0
+     */
+    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);
            if (r < 0) {
                error_report("vhost VQ %d notifier binding failed: %d", i, -r);
+            memory_region_transaction_commit();
                vhost_dev_disable_notifiers(hdev, vdev);

Could we 'break' here, ...

Quoted 6 lines
hw/virtio/vhost.c +2 -0
                return r;
            }
        }
+    memory_region_transaction_commit();
+
        return 0;
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

... and return 'r' here?

won't this commit twice? seems ugly ...

Twice? I meant keep the begin/commit() around the for() to have only *one* commit() call instead of 2:

There's a transaction in vhost_dev_disable_notifiers() too, We must commit the outter transaction before invoking it, you can see the comment in it.

Quoted 26 lines

-- >8 --
+    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);
         if (r < 0) {
             error_report("vhost VQ %d notifier binding failed: %d", i,

hw/virtio/vhost.c +4 -4
-r);
              vhost_dev_disable_notifiers(hdev, vdev);
-            return r;
+            break;
          }
      }
 
+    memory_region_transaction_commit();
+
-    return 0;
+    return r;
  }
---

Anyhow,

.

#7 lore raw

PATCH v3 3/3 vdpa: commit all host notifier MRs in a single MR transaction

In reply to Longpeng(Mike) via

From: Longpeng <[email protected]>

This allows the vhost-vdpa device to batch the setup of all its MRs of host notifiers.

This significantly reduces the device starting time, e.g. the time spend on setup the host notifier MRs reduce from 423ms to 32ms for a VM with 64 vCPUs and 3 vhost-vDPA generic devices (vdpa_sim_blk, 64vq per device).

---
 hw/virtio/vhost-vdpa.c | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)
hw/virtio/vhost-vdpa.c +19 -6
index fd0c33b0e1..870265188a 100644
@@ -512,9 +512,18 @@ static void vhost_vdpa_host_notifiers_uninit(struct vhost_dev *dev, int n)
 {
     int i;
 
+    /*
+     * Pack all the changes to the memory regions in a single
+     * transaction to avoid a few updating of the address space
+     * topology.
+     */
+    memory_region_transaction_begin();
+
     for (i = dev->vq_index; i < dev->vq_index + n; i++) {
         vhost_vdpa_host_notifier_uninit(dev, i);
     }
+
+    memory_region_transaction_commit();
 }
 
 static void vhost_vdpa_host_notifiers_init(struct vhost_dev *dev)
@@ -527,17 +536,21 @@ static void vhost_vdpa_host_notifiers_init(struct vhost_dev *dev)
         return;
     }
 
+    /*
+     * Pack all the changes to the memory regions in a single
+     * transaction to avoid a few updating of the address space
+     * topology.
+     */
+    memory_region_transaction_begin();
+
     for (i = dev->vq_index; i < dev->vq_index + dev->nvqs; i++) {
         if (vhost_vdpa_host_notifier_init(dev, i)) {
-            goto err;
+            vhost_vdpa_host_notifiers_uninit(dev, i - dev->vq_index);
+            break;
         }
     }
 
-    return;
-
-err:
-    vhost_vdpa_host_notifiers_uninit(dev, i - dev->vq_index);
-    return;
+    memory_region_transaction_commit();
 }
 
 static void vhost_vdpa_svq_cleanup(struct vhost_dev *dev)
Signature · 1 line
2.23.0
#8 lore raw

Re: [PATCH v3 3/3] vdpa: commit all host notifier MRs in a single MR transaction

Quoted 25 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

This allows the vhost-vdpa device to batch the setup of all its MRs of host notifiers.

This significantly reduces the device starting time, e.g. the time spend on setup the host notifier MRs reduce from 423ms to 32ms for a VM with 64 vCPUs and 3 vhost-vDPA generic devices (vdpa_sim_blk, 64vq per device).

---
  hw/virtio/vhost-vdpa.c | 25 +++++++++++++++++++------
  1 file changed, 19 insertions(+), 6 deletions(-)
hw/virtio/vhost-vdpa.c +3 -0
index fd0c33b0e1..870265188a 100644
@@ -512,9 +512,18 @@ static void vhost_vdpa_host_notifiers_uninit(struct vhost_dev *dev, int n)
  {
      int i;
 
+    /*
+     * Pack all the changes to the memory regions in a single
+     * transaction to avoid a few updating of the address space

hw/virtio/vhost-vdpa.c static void vhost_vdpa_host_notifiers_uninit(struct vhost_dev *dev, int n) · line 527

Quoted 10 lines
hw/virtio/vhost-vdpa.c +6 -0
+     * topology.
+     */
+    memory_region_transaction_begin();
+
      for (i = dev->vq_index; i < dev->vq_index + n; i++) {
          vhost_vdpa_host_notifier_uninit(dev, i);
      }
+
+    memory_region_transaction_commit();
  }

Instead of optimizing one frontend, I wonder if we shouldn't expose a 'bool memory_region_transaction_in_progress()' helper in memory.c, and in virtio_queue_set_host_notifier_mr() backend, assert we are within a transaction. That way we'd optimize all virtio frontends.

#9 lore raw

Re: [PATCH v3 3/3] vdpa: commit all host notifier MRs in a single MR transaction

Quoted 32 lines · Philippe Mathieu-Daudé
Quoted 31 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

This allows the vhost-vdpa device to batch the setup of all its MRs of host notifiers.

This significantly reduces the device starting time, e.g. the time spend on setup the host notifier MRs reduce from 423ms to 32ms for a VM with 64 vCPUs and 3 vhost-vDPA generic devices (vdpa_sim_blk, 64vq per device).

---
  hw/virtio/vhost-vdpa.c | 25 +++++++++++++++++++------
  1 file changed, 19 insertions(+), 6 deletions(-)
hw/virtio/vhost-vdpa.c +7 -0
index fd0c33b0e1..870265188a 100644
@@ -512,9 +512,18 @@ static void vhost_vdpa_host_notifiers_uninit(struct vhost_dev *dev, int n)
  {
      int i;
+    /*
+     * Pack all the changes to the memory regions in a single
+     * transaction to avoid a few updating of the address space
+     * topology.
+     */
+    memory_region_transaction_begin();
+
      for (i = dev->vq_index; i < dev->vq_index + n; i++) {
          vhost_vdpa_host_notifier_uninit(dev, i);
      }
Quoted 8 lines

hw/virtio/vhost-vdpa.c static void vhost_vdpa_host_notifiers_uninit(struct vhost_dev *dev, int n) · line 527

Quoted 3 lines
hw/virtio/vhost-vdpa.c +2 -0
+
+    memory_region_transaction_commit();
  }

Instead of optimizing one frontend, I wonder if we shouldn't expose a 'bool memory_region_transaction_in_progress()' helper in memory.c, and in virtio_queue_set_host_notifier_mr() backend, assert we are within a transaction. That way we'd optimize all virtio frontends.

If we are doing something like this, I'd rather pass around a "transaction" structure so this can be checked statically. Looks like something that can be done on top though.

Signature · 1 line
MST
#10 lore raw

Re: [PATCH v3 3/3] vdpa: commit all host notifier MRs in a single MR transaction

Quoted 43 lines · Michael S. Tsirkin
Quoted 32 lines · Philippe Mathieu-Daudé
Quoted 31 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

This allows the vhost-vdpa device to batch the setup of all its MRs of host notifiers.

This significantly reduces the device starting time, e.g. the time spend on setup the host notifier MRs reduce from 423ms to 32ms for a VM with 64 vCPUs and 3 vhost-vDPA generic devices (vdpa_sim_blk, 64vq per device).

---
   hw/virtio/vhost-vdpa.c | 25 +++++++++++++++++++------
   1 file changed, 19 insertions(+), 6 deletions(-)
hw/virtio/vhost-vdpa.c +7 -0
index fd0c33b0e1..870265188a 100644
@@ -512,9 +512,18 @@ static void vhost_vdpa_host_notifiers_uninit(struct vhost_dev *dev, int n)
   {
       int i;
+    /*
+     * Pack all the changes to the memory regions in a single
+     * transaction to avoid a few updating of the address space
+     * topology.
+     */
+    memory_region_transaction_begin();
+
       for (i = dev->vq_index; i < dev->vq_index + n; i++) {
           vhost_vdpa_host_notifier_uninit(dev, i);
       }
Quoted 8 lines

hw/virtio/vhost-vdpa.c static void vhost_vdpa_host_notifiers_uninit(struct vhost_dev *dev, int n) · line 527

Quoted 3 lines
hw/virtio/vhost-vdpa.c +2 -0
+
+    memory_region_transaction_commit();
   }

Instead of optimizing one frontend, I wonder if we shouldn't expose a 'bool memory_region_transaction_in_progress()' helper in memory.c, and in virtio_queue_set_host_notifier_mr() backend, assert we are within a transaction. That way we'd optimize all virtio frontends.

Quoted 2 lines

If we are doing something like this, I'd rather pass around a "transaction" structure so this can be checked statically.

Ah, clever.

Quoted 1 line

Looks like something that can be done on top though.

Sure.