All of lore.kernel.org
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] virtio: Make memory barriers be memory barriers
@ 2011-09-01  6:09 David Gibson
  2011-09-01  6:47 ` Paolo Bonzini
                   ` (2 more replies)
  0 siblings, 3 replies; 32+ messages in thread
From: David Gibson @ 2011-09-01  6:09 UTC (permalink / raw)
  To: aliguori; +Cc: aik, pbonzini, rusty, qemu-devel, agraf

The virtio code already has memory barrier wmb() macros in the code.
However they are was defined as no-ops.  The comment claims that real
barriers are not necessary because the code does not run concurrent.
However, with kvm and io-thread enabled, this is not true and this qemu
code can indeed run concurrently with the guest kernel.  This does not
cause problems on x86 due to it's strongly ordered storage model, but it
causes a race leading to virtio errors on POWER which has a relaxed storage
ordering model.

Specifically, the QEMU puts new element into the "used" ring and then
updates the ring free-running counter.  Without a barrier between these
under the right circumstances, the guest linux driver can receive an
interrupt, read the counter change but find the ring element to be handled
still has an old value, leading to an "id %u is not a head!\n" error
message.

The problem is easy to reproduce on POWER using virtio-net with heavy
traffic.

The patch defines wmb() as __sync_synchronize(), a cross platform memory
barrier primitive available in sufficiently recent gcc versions (gcc 4.2
and after?).  If we care about older gccs then this patch will need to
be updated with some sort of fallback.

Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 hw/virtio.c |   10 ++--------
 1 files changed, 2 insertions(+), 8 deletions(-)

diff --git a/hw/virtio.c b/hw/virtio.c
index 13aa0fa..c9f0e75 100644
--- a/hw/virtio.c
+++ b/hw/virtio.c
@@ -21,14 +21,8 @@
  * x86 pagesize again. */
 #define VIRTIO_PCI_VRING_ALIGN         4096
 
-/* QEMU doesn't strictly need write barriers since everything runs in
- * lock-step.  We'll leave the calls to wmb() in though to make it obvious for
- * KVM or if kqemu gets SMP support.
- * In any case, we must prevent the compiler from reordering the code.
- * TODO: we likely need some rmb()/mb() as well.
- */
-
-#define wmb() __asm__ __volatile__("": : :"memory")
+ /* TODO: we may also need rmb()s.  It hasn't bitten us yet, but.. */
+ #define wmb() __sync_synchronize()
 
 typedef struct VRingDesc
 {
-- 
1.7.5.4

^ permalink raw reply related	[flat|nested] 32+ messages in thread

end of thread, other threads:[~2011-09-06  9:39 UTC | newest]

Thread overview: 32+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-09-01  6:09 [Qemu-devel] [PATCH] virtio: Make memory barriers be memory barriers David Gibson
2011-09-01  6:47 ` Paolo Bonzini
2011-09-02  0:08   ` David Gibson
2011-09-02  6:49     ` Paolo Bonzini
2011-09-03 11:53       ` Blue Swirl
2011-09-01  7:37 ` Sasha Levin
2011-09-01  7:38   ` Sasha Levin
2011-09-01  7:56   ` Paolo Bonzini
2011-09-02  0:09     ` David Gibson
2011-09-01 15:30 ` Michael S. Tsirkin
2011-09-01 16:14   ` Paolo Bonzini
2011-09-01 16:34     ` Michael S. Tsirkin
2011-09-01 20:31       ` Paolo Bonzini
2011-09-02 15:45         ` Michael S. Tsirkin
2011-09-03 14:46           ` David Gibson
2011-09-04  9:16             ` Michael S. Tsirkin
2011-09-05  4:43               ` David Gibson
2011-09-05  9:19                 ` Michael S. Tsirkin
2011-09-06  3:12                   ` David Gibson
2011-09-06  6:55                     ` Paolo Bonzini
2011-09-06  9:02                       ` David Gibson
2011-09-06  9:28                       ` Avi Kivity
2011-09-06  9:35                         ` Michael S. Tsirkin
2011-09-06  9:38                         ` Paolo Bonzini
2011-09-05  7:41               ` Paolo Bonzini
2011-09-05  8:06                 ` Michael S. Tsirkin
2011-09-05  9:42                   ` Paolo Bonzini
2011-09-03 16:19           ` Paolo Bonzini
2011-09-04  8:47             ` Michael S. Tsirkin
2011-09-02  0:11     ` David Gibson
2011-09-02  6:11       ` Paolo Bonzini
2011-09-02 15:57         ` Michael S. Tsirkin

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.