Skip to contentSkip to conversation

hw/virtio/vring/event_idx: fix the vring_avail_event error

PATCH In review Bin Wu 3 participants 3 messages Oct 31, 2014

lore
Share & tools
lore

Bin Wu

author
#0 lore raw

PATCH v2 1/1 hw/virtio/vring/event_idx: fix the vring_avail_event error

The event idx in virtio is an effective way to reduce the number of interrupts and exits of the guest. When the guest puts an request into the virtio ring, it doesn't exit immediately to inform the backend. Instead, the guest checks the "avail" event idx to determine the notification.

In virtqueue_pop, when a request is poped, the current avail event idx should be set to the number of vq->last_avail_idx.

---
V2 -> V1:
update the same code in hw/virtio/dataplane/vring.c (Stefan)

---
 hw/virtio/dataplane/vring.c | 8 ++++----
 hw/virtio/virtio.c          | 2 +-
 2 files changed, 5 insertions(+), 5 deletions(-)
hw/virtio/dataplane/vring.c +4 -4
index 372706a..61f6d83 100644
@@ -352,10 +352,6 @@ int vring_pop(VirtIODevice *vdev, Vring *vring,
         goto out;
     }
 
-    if (vdev->guest_features & (1 << VIRTIO_RING_F_EVENT_IDX)) {
-        vring_avail_event(&vring->vr) = vring->vr.avail->idx;
-    }
-
     i = head;
     do {
         if (unlikely(i >= num)) {
@@ -392,6 +388,10 @@ int vring_pop(VirtIODevice *vdev, Vring *vring,
 
     /* On success, increment avail index. */
     vring->last_avail_idx++;
+    if (vdev->guest_features & (1 << VIRTIO_RING_F_EVENT_IDX)) {
+        vring_avail_event(&vring->vr) = vring->last_avail_idx;
+    }
+
     return head;
 
 out:
hw/virtio/virtio.c +1 -1
index 2c236bf..013979a 100644
@@ -469,7 +469,7 @@ int virtqueue_pop(VirtQueue *vq, VirtQueueElement *elem)
 
     i = head = virtqueue_get_head(vq, vq->last_avail_idx++);
     if (vdev->guest_features & (1 << VIRTIO_RING_F_EVENT_IDX)) {
-        vring_avail_event(vq, vring_avail_idx(vq));
+        vring_avail_event(vq, vq->last_avail_idx);
     }
 
     if (vring_desc_flags(vdev, desc_pa, i) & VRING_DESC_F_INDIRECT) {
Signature · 1 line
1.7.12.4
Reviewed-by on v2 1/1
#1 lore raw
Quoted 9 lines · Bin Wu

The event idx in virtio is an effective way to reduce the number of interrupts and exits of the guest. When the guest puts an request into the virtio ring, it doesn't exit immediately to inform the backend. Instead, the guest checks the "avail" event idx to determine the notification.

In virtqueue_pop, when a request is poped, the current avail event idx should be set to the number of vq->last_avail_idx.

Quoted 8 lines

---
V2 -> V1:
update the same code in hw/virtio/dataplane/vring.c (Stefan)

---
 hw/virtio/dataplane/vring.c | 8 ++++----
 hw/virtio/virtio.c          | 2 +-
 2 files changed, 5 insertions(+), 5 deletions(-)

By the way, did you see that kernel drivers/vhost/vhost.c doesn't update the field for each pop? Instead it only updates when notify is re-enabled. I wonder if that approach is better than what QEMU does.

Stefan

#2 lore raw
Quoted 19 lines · Stefan Hajnoczi
Quoted 9 lines · Bin Wu

The event idx in virtio is an effective way to reduce the number of interrupts and exits of the guest. When the guest puts an request into the virtio ring, it doesn't exit immediately to inform the backend. Instead, the guest checks the "avail" event idx to determine the notification.

In virtqueue_pop, when a request is poped, the current avail event idx should be set to the number of vq->last_avail_idx.

Quoted 8 lines

---
V2 -> V1:
update the same code in hw/virtio/dataplane/vring.c (Stefan)

---
 hw/virtio/dataplane/vring.c | 8 ++++----
 hw/virtio/virtio.c          | 2 +-
 2 files changed, 5 insertions(+), 5 deletions(-)
Quoted 7 lines

By the way, did you see that kernel drivers/vhost/vhost.c doesn't update the field for each pop? Instead it only updates when notify is re-enabled. I wonder if that approach is better than what QEMU does.

Stefan

Yes you should not move event idx if you don't want events.