Skip to content

Commit 8d622d2

Browse files
committed
virtio: fix up virtio_disable_cb
virtio_disable_cb is currently a nop for split ring with event index. This is because it used to be always called from a callback when we know device won't trigger more events until we update the index. However, now that we run with interrupts enabled a lot we also poll without a callback so that is different: disabling callbacks will help reduce the number of spurious interrupts. Further, if using event index with a packed ring, and if being called from a callback, we actually do disable interrupts which is unnecessary. Fix both issues by tracking whenever we get a callback. If that is the case disabling interrupts with event index can be a nop. If not the case disable interrupts. Note: with a split ring there's no explicit "no interrupts" value. For now we write a fixed value so our chance of triggering an interupt is 1/ring size. It's probably better to write something related to the last used index there to reduce the chance even further. For now I'm keeping it simple. Signed-off-by: Michael S. Tsirkin <[email protected]>
1 parent 22bc63c commit 8d622d2

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

drivers/virtio/virtio_ring.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ struct vring_virtqueue {
113113
/* Last used index we've seen. */
114114
u16 last_used_idx;
115115

116+
/* Hint for event idx: already triggered no need to disable. */
117+
bool event_triggered;
118+
116119
union {
117120
/* Available for split ring */
118121
struct {
@@ -739,7 +742,10 @@ static void virtqueue_disable_cb_split(struct virtqueue *_vq)
739742

740743
if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) {
741744
vq->split.avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
742-
if (!vq->event)
745+
if (vq->event)
746+
/* TODO: this is a hack. Figure out a cleaner value to write. */
747+
vring_used_event(&vq->split.vring) = 0x0;
748+
else
743749
vq->split.vring.avail->flags =
744750
cpu_to_virtio16(_vq->vdev,
745751
vq->split.avail_flags_shadow);
@@ -1605,6 +1611,7 @@ static struct virtqueue *vring_create_virtqueue_packed(
16051611
vq->weak_barriers = weak_barriers;
16061612
vq->broken = false;
16071613
vq->last_used_idx = 0;
1614+
vq->event_triggered = false;
16081615
vq->num_added = 0;
16091616
vq->packed_ring = true;
16101617
vq->use_dma_api = vring_use_dma_api(vdev);
@@ -1919,6 +1926,12 @@ void virtqueue_disable_cb(struct virtqueue *_vq)
19191926
{
19201927
struct vring_virtqueue *vq = to_vvq(_vq);
19211928

1929+
/* If device triggered an event already it won't trigger one again:
1930+
* no need to disable.
1931+
*/
1932+
if (vq->event_triggered)
1933+
return;
1934+
19221935
if (vq->packed_ring)
19231936
virtqueue_disable_cb_packed(_vq);
19241937
else
@@ -1942,6 +1955,9 @@ unsigned virtqueue_enable_cb_prepare(struct virtqueue *_vq)
19421955
{
19431956
struct vring_virtqueue *vq = to_vvq(_vq);
19441957

1958+
if (vq->event_triggered)
1959+
vq->event_triggered = false;
1960+
19451961
return vq->packed_ring ? virtqueue_enable_cb_prepare_packed(_vq) :
19461962
virtqueue_enable_cb_prepare_split(_vq);
19471963
}
@@ -2005,6 +2021,9 @@ bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
20052021
{
20062022
struct vring_virtqueue *vq = to_vvq(_vq);
20072023

2024+
if (vq->event_triggered)
2025+
vq->event_triggered = false;
2026+
20082027
return vq->packed_ring ? virtqueue_enable_cb_delayed_packed(_vq) :
20092028
virtqueue_enable_cb_delayed_split(_vq);
20102029
}
@@ -2044,6 +2063,10 @@ irqreturn_t vring_interrupt(int irq, void *_vq)
20442063
if (unlikely(vq->broken))
20452064
return IRQ_HANDLED;
20462065

2066+
/* Just a hint for performance: so it's ok that this can be racy! */
2067+
if (vq->event)
2068+
vq->event_triggered = true;
2069+
20472070
pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
20482071
if (vq->vq.callback)
20492072
vq->vq.callback(&vq->vq);
@@ -2083,6 +2106,7 @@ struct virtqueue *__vring_new_virtqueue(unsigned int index,
20832106
vq->weak_barriers = weak_barriers;
20842107
vq->broken = false;
20852108
vq->last_used_idx = 0;
2109+
vq->event_triggered = false;
20862110
vq->num_added = 0;
20872111
vq->use_dma_api = vring_use_dma_api(vdev);
20882112
#ifdef DEBUG

0 commit comments

Comments
 (0)