Skip to contentSkip to conversation

vhost: don't set vring call if no vector

PATCH In review Jason Wang 3 participants 7 messages Aug 1, 2016 – Aug 3, 2016

lore
Share & tools
lore

Jason Wang

author
#0 lore raw

PATCH 1/1 vhost: don't set vring call if no vector

We used to set vring call fd unconditionally even if guest driver does not use MSIX for this vritqueue at all. This will cause lots of unnecessary userspace access and other checks for drivers does not use interrupt at all (e.g virtio-net pmd). So check and clean vring call fd if guest does not use any vector for this virtqueue at all.

Perf diffs (on rx) shows lots of cpus wasted on vhost_signal() were saved:

#

    28.12%  -27.82%  [vhost]           [k] vhost_signal
    14.44%   -1.69%  [kernel.vmlinux]  [k] copy_user_generic_string
     7.05%   +1.53%  [kernel.vmlinux]  [k] __free_page_frag
     6.51%   +5.53%  [vhost]           [k] vhost_get_vq_desc

...

Pktgen tests shows 15.8% improvement on rx pps and 6.5% on tx pps.

Before: RX 2.08Mpps TX 1.35Mpps
After:  RX 2.41Mpps TX 1.44Mpps
---
 hw/virtio/vhost.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
hw/virtio/vhost.c +14 -0
index 3d0c807..bd051ab 100644
@@ -822,6 +822,9 @@ static int vhost_virtqueue_start(struct vhost_dev *dev,
                                 struct vhost_virtqueue *vq,
                                 unsigned idx)
 {
+    BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
+    VirtioBusState *vbus = VIRTIO_BUS(qbus);
+    VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
     hwaddr s, l, a;
     int r;
     int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, idx);
@@ -912,8 +915,19 @@ static int vhost_virtqueue_start(struct vhost_dev *dev,
         vhost_virtqueue_mask(dev, vdev, idx, false);
     }
 
+    if (k->query_guest_notifiers &&
+        k->query_guest_notifiers(qbus->parent) &&
+        virtio_queue_vector(vdev, idx) == VIRTIO_NO_VECTOR) {
+        file.fd = -1;
+        r = dev->vhost_ops->vhost_set_vring_call(dev, &file);
+        if (r) {
+            goto fail_vector;
+        }
+    }
+
     return 0;
 
+fail_vector:
 fail_kick:
 fail_alloc:
     cpu_physical_memory_unmap(vq->ring, virtio_queue_get_ring_size(vdev, idx),
Signature · 1 line
2.7.4
#1 lore raw
Quoted 44 lines · Jason Wang

We used to set vring call fd unconditionally even if guest driver does not use MSIX for this vritqueue at all. This will cause lots of unnecessary userspace access and other checks for drivers does not use interrupt at all (e.g virtio-net pmd). So check and clean vring call fd if guest does not use any vector for this virtqueue at all.

Perf diffs (on rx) shows lots of cpus wasted on vhost_signal() were saved:

#

    28.12%  -27.82%  [vhost]           [k] vhost_signal
    14.44%   -1.69%  [kernel.vmlinux]  [k] copy_user_generic_string
     7.05%   +1.53%  [kernel.vmlinux]  [k] __free_page_frag
     6.51%   +5.53%  [vhost]           [k] vhost_get_vq_desc

...

Pktgen tests shows 15.8% improvement on rx pps and 6.5% on tx pps.

Before: RX 2.08Mpps TX 1.35Mpps
After:  RX 2.41Mpps TX 1.44Mpps
---
 hw/virtio/vhost.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
hw/virtio/vhost.c +3 -0
index 3d0c807..bd051ab 100644
@@ -822,6 +822,9 @@ static int vhost_virtqueue_start(struct vhost_dev *dev,
                                 struct vhost_virtqueue *vq,
                                 unsigned idx)
 {
+    BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
+    VirtioBusState *vbus = VIRTIO_BUS(qbus);
+    VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
     hwaddr s, l, a;
     int r;
     int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, idx);
@@ -912,8 +915,19 @@ static int vhost_virtqueue_start(struct vhost_dev *dev,
         vhost_virtqueue_mask(dev, vdev, idx, false);
     }
 

hw/virtio/vhost.c static int vhost_virtqueue_start(struct vhost_dev *dev, · line 927

Quoted 10 lines
hw/virtio/vhost.c +10 -0
+    if (k->query_guest_notifiers &&
+        k->query_guest_notifiers(qbus->parent) &&
+        virtio_queue_vector(vdev, idx) == VIRTIO_NO_VECTOR) {
+        file.fd = -1;
+        r = dev->vhost_ops->vhost_set_vring_call(dev, &file);
+        if (r) {
+            goto fail_vector;
+        }
+    }
+

It's rather asymmetrical though. Wouldn't a better place be in vhost_virtqueue_mask?

Quoted 8 lines
hw/virtio/vhost.c +1 -0
     return 0;
 
+fail_vector:
 fail_kick:
 fail_alloc:
     cpu_physical_memory_unmap(vq->ring, virtio_queue_get_ring_size(vdev, idx),
Signature · 1 line
2.7.4
author
#2 lore raw
Quoted 45 lines · Michael S. Tsirkin
Quoted 44 lines · Jason Wang

We used to set vring call fd unconditionally even if guest driver does not use MSIX for this vritqueue at all. This will cause lots of unnecessary userspace access and other checks for drivers does not use interrupt at all (e.g virtio-net pmd). So check and clean vring call fd if guest does not use any vector for this virtqueue at all.

Perf diffs (on rx) shows lots of cpus wasted on vhost_signal() were saved:

#

     28.12%  -27.82%  [vhost]           [k] vhost_signal
     14.44%   -1.69%  [kernel.vmlinux]  [k] copy_user_generic_string
      7.05%   +1.53%  [kernel.vmlinux]  [k] __free_page_frag
      6.51%   +5.53%  [vhost]           [k] vhost_get_vq_desc

...

Pktgen tests shows 15.8% improvement on rx pps and 6.5% on tx pps.

Before: RX 2.08Mpps TX 1.35Mpps
After:  RX 2.41Mpps TX 1.44Mpps
---
  hw/virtio/vhost.c | 14 ++++++++++++++
  1 file changed, 14 insertions(+)
hw/virtio/vhost.c +3 -0
index 3d0c807..bd051ab 100644
@@ -822,6 +822,9 @@ static int vhost_virtqueue_start(struct vhost_dev *dev,
                                  struct vhost_virtqueue *vq,
                                  unsigned idx)
  {
+    BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
+    VirtioBusState *vbus = VIRTIO_BUS(qbus);
+    VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
      hwaddr s, l, a;
      int r;
      int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, idx);
@@ -912,8 +915,19 @@ static int vhost_virtqueue_start(struct vhost_dev *dev,
          vhost_virtqueue_mask(dev, vdev, idx, false);
      }
 
Quoted 12 lines

hw/virtio/vhost.c static int vhost_virtqueue_start(struct vhost_dev *dev, · line 927

Quoted 10 lines
hw/virtio/vhost.c +10 -0
+    if (k->query_guest_notifiers &&
+        k->query_guest_notifiers(qbus->parent) &&
+        virtio_queue_vector(vdev, idx) == VIRTIO_NO_VECTOR) {
+        file.fd = -1;
+        r = dev->vhost_ops->vhost_set_vring_call(dev, &file);
+        if (r) {
+            goto fail_vector;
+        }
+    }
+

It's rather asymmetrical though. Wouldn't a better place be in vhost_virtqueue_mask?

Looks not, since it will be only called when

- backend does not support interrupt masking (vhost-user), the patch should work for vhost-kernel too
- mask or unmask an vector but the patch only work for a virtqueue of VIRTIO_NO_VECTOR

Quoted 10 lines
Quoted 10 lines
hw/virtio/vhost.c +1 -0
      return 0;
 
+fail_vector:
  fail_kick:
  fail_alloc:
      cpu_physical_memory_unmap(vq->ring, virtio_queue_get_ring_size(vdev, idx),
Signature · 1 line
2.7.4
#3 lore raw
In reply to Jason Wang
Quoted 6 lines · On Mon, 1 Aug 2016 16:07:58 +0800 Jason Wang <[email protected]> wrote:

We used to set vring call fd unconditionally even if guest driver does not use MSIX for this vritqueue at all. This will cause lots of unnecessary userspace access and other checks for drivers does not use interrupt at all (e.g virtio-net pmd). So check and clean vring call fd if guest does not use any vector for this virtqueue at all.

So the basic idea is: don't setup signalling via eventfd if the guest did not enable interrupts for this queue, right?

Quoted 35 lines

Perf diffs (on rx) shows lots of cpus wasted on vhost_signal() were saved:

#

    28.12%  -27.82%  [vhost]           [k] vhost_signal
    14.44%   -1.69%  [kernel.vmlinux]  [k] copy_user_generic_string
     7.05%   +1.53%  [kernel.vmlinux]  [k] __free_page_frag
     6.51%   +5.53%  [vhost]           [k] vhost_get_vq_desc

...

Pktgen tests shows 15.8% improvement on rx pps and 6.5% on tx pps.

Before: RX 2.08Mpps TX 1.35Mpps
After:  RX 2.41Mpps TX 1.44Mpps
---
 hw/virtio/vhost.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
hw/virtio/vhost.c +3 -0
index 3d0c807..bd051ab 100644
@@ -822,6 +822,9 @@ static int vhost_virtqueue_start(struct vhost_dev *dev,
                                 struct vhost_virtqueue *vq,
                                 unsigned idx)
 {
+    BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
+    VirtioBusState *vbus = VIRTIO_BUS(qbus);
+    VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
     hwaddr s, l, a;
     int r;
     int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, idx);

hw/virtio/vhost.c static int vhost_virtqueue_start(struct vhost_dev *dev, · line 920

Quoted 6 lines
hw/virtio/vhost.c +3 -0
@@ -912,8 +915,19 @@ static int vhost_virtqueue_start(struct vhost_dev *dev,
         vhost_virtqueue_mask(dev, vdev, idx, false);
     }
 
+    if (k->query_guest_notifiers &&
+        k->query_guest_notifiers(qbus->parent) &&
+        virtio_queue_vector(vdev, idx) == VIRTIO_NO_VECTOR) {

I'm trying to imagine what this means for virtio-ccw. Keep in mind that we don't have the concept of setting a 'vector' by the OS (the vector is setup internally to the queue index and the OS does not see it.)

->query_guest_notifiers() is true if the OS has enabled the subchannel of the proxy device (i.e., if it is enabled for doing *anything* with the subchannel, regardless whether the OS wants to be notified or is planning to poll.) The second condition will never hold true for any valid queue once the OS has setup the queues.

So this won't break anything for virtio-ccw AFAICS, but I don't think we gain anything.

Quoted 13 lines
hw/virtio/vhost.c +8 -0
+        file.fd = -1;
+        r = dev->vhost_ops->vhost_set_vring_call(dev, &file);
+        if (r) {
+            goto fail_vector;
+        }
+    }
+
     return 0;
 
+fail_vector:
 fail_kick:
 fail_alloc:
     cpu_physical_memory_unmap(vq->ring, virtio_queue_get_ring_size(vdev, idx),
author
#4 lore raw
Quoted 11 lines · Cornelia Huck
Quoted 6 lines · On Mon, 1 Aug 2016 16:07:58 +0800 Jason Wang <[email protected]> wrote:

We used to set vring call fd unconditionally even if guest driver does not use MSIX for this vritqueue at all. This will cause lots of unnecessary userspace access and other checks for drivers does not use interrupt at all (e.g virtio-net pmd). So check and clean vring call fd if guest does not use any vector for this virtqueue at all.

So the basic idea is: don't setup signalling via eventfd if the guest did not enable interrupts for this queue, right?

Right.

Quoted 44 lines
Quoted 34 lines

Perf diffs (on rx) shows lots of cpus wasted on vhost_signal() were saved:

#

     28.12%  -27.82%  [vhost]           [k] vhost_signal
     14.44%   -1.69%  [kernel.vmlinux]  [k] copy_user_generic_string
      7.05%   +1.53%  [kernel.vmlinux]  [k] __free_page_frag
      6.51%   +5.53%  [vhost]           [k] vhost_get_vq_desc

...

Pktgen tests shows 15.8% improvement on rx pps and 6.5% on tx pps.

Before: RX 2.08Mpps TX 1.35Mpps
After:  RX 2.41Mpps TX 1.44Mpps
---
  hw/virtio/vhost.c | 14 ++++++++++++++
  1 file changed, 14 insertions(+)
hw/virtio/vhost.c +3 -0
index 3d0c807..bd051ab 100644
@@ -822,6 +822,9 @@ static int vhost_virtqueue_start(struct vhost_dev *dev,
                                  struct vhost_virtqueue *vq,
                                  unsigned idx)
  {
+    BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
+    VirtioBusState *vbus = VIRTIO_BUS(qbus);
+    VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
      hwaddr s, l, a;
      int r;
      int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, idx);

hw/virtio/vhost.c static int vhost_virtqueue_start(struct vhost_dev *dev, · line 920

Quoted 6 lines
hw/virtio/vhost.c +3 -0
@@ -912,8 +915,19 @@ static int vhost_virtqueue_start(struct vhost_dev *dev,
          vhost_virtqueue_mask(dev, vdev, idx, false);
      }
 
+    if (k->query_guest_notifiers &&
+        k->query_guest_notifiers(qbus->parent) &&
+        virtio_queue_vector(vdev, idx) == VIRTIO_NO_VECTOR) {

I'm trying to imagine what this means for virtio-ccw. Keep in mind that we don't have the concept of setting a 'vector' by the OS (the vector is setup internally to the queue index and the OS does not see it.)

Quoted 5 lines

->query_guest_notifiers() is true if the OS has enabled the subchannel of the proxy device (i.e., if it is enabled for doing *anything* with the subchannel, regardless whether the OS wants to be notified or is planning to poll.) The second condition will never hold true for any valid queue once the OS has setup the queues.

I see, so If I understand correctly, there's no way to detect whether or not guest will use a specific virtqueue interrupt?

Quoted 3 lines

So this won't break anything for virtio-ccw AFAICS, but I don't think we gain anything.

Yes, but if we could infer whether or not polling is used in the driver, this is probably all we can do for ccw.

Thanks

Quoted 14 lines
Quoted 14 lines
hw/virtio/vhost.c +8 -0
+        file.fd = -1;
+        r = dev->vhost_ops->vhost_set_vring_call(dev, &file);
+        if (r) {
+            goto fail_vector;
+        }
+    }
+
      return 0;
 
+fail_vector:
  fail_kick:
  fail_alloc:
      cpu_physical_memory_unmap(vq->ring, virtio_queue_get_ring_size(vdev, idx),
#5 lore raw
Quoted 11 lines · On Tue, 2 Aug 2016 10:39:22 +0800 Jason Wang <[email protected]> wrote:
Quoted 10 lines · Cornelia Huck

hw/virtio/vhost.c static int vhost_virtqueue_start(struct vhost_dev *dev, · line 920

Quoted 3 lines · On Mon, 1 Aug 2016 16:07:58 +0800 Jason Wang <[email protected]> wrote:
hw/virtio/vhost.c +3 -0
+    if (k->query_guest_notifiers &&
+        k->query_guest_notifiers(qbus->parent) &&
+        virtio_queue_vector(vdev, idx) == VIRTIO_NO_VECTOR) {

I'm trying to imagine what this means for virtio-ccw. Keep in mind that we don't have the concept of setting a 'vector' by the OS (the vector is setup internally to the queue index and the OS does not see it.)

Quoted 8 lines
Quoted 5 lines

->query_guest_notifiers() is true if the OS has enabled the subchannel of the proxy device (i.e., if it is enabled for doing *anything* with the subchannel, regardless whether the OS wants to be notified or is planning to poll.) The second condition will never hold true for any valid queue once the OS has setup the queues.

I see, so If I understand correctly, there's no way to detect whether or not guest will use a specific virtqueue interrupt?

Yes. The guest will either be notified for any virtqueue (if it registered indicators; this is always done for every vq of the device at once), or for none.

Quoted 7 lines
Quoted 4 lines

So this won't break anything for virtio-ccw AFAICS, but I don't think we gain anything.

Yes, but if we could infer whether or not polling is used in the driver, this is probably all we can do for ccw.

What we could do is check whether the driver has registed indicators and disable setting up notification for any vq of the device if not. But I'm not sure an always-polling driver is worth optimizing for.

author
#6 lore raw
Quoted 24 lines · Cornelia Huck
Quoted 10 lines · On Tue, 2 Aug 2016 10:39:22 +0800 Jason Wang <[email protected]> wrote:
Quoted 9 lines · Cornelia Huck

hw/virtio/vhost.c static int vhost_virtqueue_start(struct vhost_dev *dev, · line 920

Quoted 3 lines · On Mon, 1 Aug 2016 16:07:58 +0800 Jason Wang <[email protected]> wrote:
hw/virtio/vhost.c +3 -0
+    if (k->query_guest_notifiers &&
+        k->query_guest_notifiers(qbus->parent) &&
+        virtio_queue_vector(vdev, idx) == VIRTIO_NO_VECTOR) {

I'm trying to imagine what this means for virtio-ccw. Keep in mind that we don't have the concept of setting a 'vector' by the OS (the vector is setup internally to the queue index and the OS does not see it.)

Quoted 7 lines
Quoted 5 lines

->query_guest_notifiers() is true if the OS has enabled the subchannel of the proxy device (i.e., if it is enabled for doing *anything* with the subchannel, regardless whether the OS wants to be notified or is planning to poll.) The second condition will never hold true for any valid queue once the OS has setup the queues.

I see, so If I understand correctly, there's no way to detect whether or not guest will use a specific virtqueue interrupt?

Yes. The guest will either be notified for any virtqueue (if it registered indicators; this is always done for every vq of the device at once), or for none.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

So this won't break anything for virtio-ccw AFAICS, but I don't think we gain anything.

Yes, but if we could infer whether or not polling is used in the driver, this is probably all we can do for ccw.

What we could do is check whether the driver has registed indicators and disable setting up notification for any vq of the device if not. But I'm not sure an always-polling driver is worth optimizing for.

It's worth for at least pci transport. Consider virito-net pmd in guest, a NULL vring call can save unnecessary userspace memory access and memory barriers.