Skip to content

Commit a5c262c

Browse files
mstsirkinrustyrussell
authored andcommitted
virtio_ring: support event idx feature
Support for the new event idx feature: 1. When enabling interrupts, publish the current avail index value to the host to get interrupts on the next update. 2. Use the new avail_event feature to reduce the number of exits from the guest. Simple test with the simulator: [virtio]# time ./virtio_test spurious wakeus: 0x7 real 0m0.169s user 0m0.140s sys 0m0.019s [virtio]# time ./virtio_test --no-event-idx spurious wakeus: 0x11 real 0m0.649s user 0m0.295s sys 0m0.335s Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent bf7035b commit a5c262c

1 file changed

Lines changed: 24 additions & 2 deletions

File tree

‎drivers/virtio/virtio_ring.c‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ struct vring_virtqueue
8282
/* Host supports indirect buffers */
8383
bool indirect;
8484

85+
/* Host publishes avail event idx */
86+
bool event;
87+
8588
/* Number of free buffers */
8689
unsigned int num_free;
8790
/* Head of free buffer list. */
@@ -237,18 +240,22 @@ EXPORT_SYMBOL_GPL(virtqueue_add_buf_gfp);
237240
void virtqueue_kick(struct virtqueue *_vq)
238241
{
239242
struct vring_virtqueue *vq = to_vvq(_vq);
243+
u16 new, old;
240244
START_USE(vq);
241245
/* Descriptors and available array need to be set before we expose the
242246
* new available array entries. */
243247
virtio_wmb();
244248

245-
vq->vring.avail->idx += vq->num_added;
249+
old = vq->vring.avail->idx;
250+
new = vq->vring.avail->idx = old + vq->num_added;
246251
vq->num_added = 0;
247252

248253
/* Need to update avail index before checking if we should notify */
249254
virtio_mb();
250255

251-
if (!(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY))
256+
if (vq->event ?
257+
vring_need_event(vring_avail_event(&vq->vring), new, old) :
258+
!(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY))
252259
/* Prod other side to tell it about changes. */
253260
vq->notify(&vq->vq);
254261

@@ -324,6 +331,14 @@ void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
324331
ret = vq->data[i];
325332
detach_buf(vq, i);
326333
vq->last_used_idx++;
334+
/* If we expect an interrupt for the next entry, tell host
335+
* by writing event index and flush out the write before
336+
* the read in the next get_buf call. */
337+
if (!(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
338+
vring_used_event(&vq->vring) = vq->last_used_idx;
339+
virtio_mb();
340+
}
341+
327342
END_USE(vq);
328343
return ret;
329344
}
@@ -345,7 +360,11 @@ bool virtqueue_enable_cb(struct virtqueue *_vq)
345360

346361
/* We optimistically turn back on interrupts, then check if there was
347362
* more to do. */
363+
/* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
364+
* either clear the flags bit or point the event index at the next
365+
* entry. Always do both to keep code simple. */
348366
vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
367+
vring_used_event(&vq->vring) = vq->last_used_idx;
349368
virtio_mb();
350369
if (unlikely(more_used(vq))) {
351370
END_USE(vq);
@@ -438,6 +457,7 @@ struct virtqueue *vring_new_virtqueue(unsigned int num,
438457
#endif
439458

440459
vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
460+
vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
441461

442462
/* No callback? Tell other side not to bother us. */
443463
if (!callback)
@@ -472,6 +492,8 @@ void vring_transport_features(struct virtio_device *vdev)
472492
switch (i) {
473493
case VIRTIO_RING_F_INDIRECT_DESC:
474494
break;
495+
case VIRTIO_RING_F_EVENT_IDX:
496+
break;
475497
default:
476498
/* We don't understand this bit. */
477499
clear_bit(i, vdev->features);

0 commit comments

Comments
 (0)