From a9e323776f72ea5982334690bdbc7e5a648b589d Mon Sep 17 00:00:00 2001
Message-Id: <a9e323776f72ea5982334690bdbc7e5a648b589d.1335294335.git.minovotn@redhat.com>
From: "Michael S. Tsirkin" <mst@redhat.com>
Date: Mon, 23 Apr 2012 13:58:49 +0200
Subject: [PATCH] virtio: add missing mb() on enable notification

RH-Author: Michael S. Tsirkin <mst@redhat.com>
Message-id: <d597889c4f6d76ac76a07462918a6cdbcd9f28ab.1335188730.git.mst@redhat.com>
Patchwork-id: 39378
O-Subject: [PATCH RHEL6.3 1/2] virtio: add missing mb() on enable notification
Bugzilla: 804578
RH-Acked-by: Xiao Wang <jasowang@redhat.com>
RH-Acked-by: Amos Kong <akong@redhat.com>
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>

This fixes an issue dual to the one fixed by
patch 'virtio: add missing mb() on notification'
and applies on top.

In this case, to enable vq kick to exit to host,
qemu writes out used flag then reads the
avail index. if these are reordered we get a race:

    host avail index read: ring is empty
    		guest avail index write
    		guest flag read: exit disabled
    host used flag write: enable exit

which results in a lost exit: host will never be notified about the
avail index update.  Again, happens in the field but only seems to
trigger on some specific hardware.

Insert an smp_mb barrier operation to ensure the correct ordering.

Signed-off-by: Michael S. Tsirkin <mst@redhat.com>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=804578
Brew build: http://brewweb.devel.redhat.com/brew/taskinfo?taskID=4327871
Tested: on x86_64 with virtio-net
Upstream status: posted
---
 hw/virtio.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)

Signed-off-by: Michal Novotny <minovotn@redhat.com>
---
 hw/virtio.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)

diff --git a/hw/virtio.c b/hw/virtio.c
index 2db9b68..08e64d9 100644
--- a/hw/virtio.c
+++ b/hw/virtio.c
@@ -218,6 +218,10 @@ void virtio_queue_set_notification(VirtQueue *vq, int enable)
     } else {
         vring_used_flags_set_bit(vq, VRING_USED_F_NO_NOTIFY);
     }
+    if (enable) {
+        /* We need to expose avail event/flags before checking avail idx. */
+        mb();
+    }
 }
 
 int virtio_queue_ready(VirtQueue *vq)
-- 
1.7.7.6

