public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Sasha Levin <levinsasha928@gmail.com>
To: penberg@kernel.org
Cc: mingo@elte.hu, asias.hejun@gmail.com, gorcunov@gmail.com,
	prasadjoshi124@gmail.com, kvm@vger.kernel.org,
	Sasha Levin <levinsasha928@gmail.com>
Subject: [PATCH 2/2 V2] kvm tools: Fix virt_queue__set_used_elem
Date: Tue,  3 May 2011 23:28:07 +0300	[thread overview]
Message-ID: <1304454487-2539-2-git-send-email-levinsasha928@gmail.com> (raw)
In-Reply-To: <1304454487-2539-1-git-send-email-levinsasha928@gmail.com>

Increase idx only after updating the used element.
Not doing so may mark a buffer as used without having
it's head and length updated.

Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
---
 tools/kvm/virtio.c |   19 ++++++++++++++++++-
 1 files changed, 18 insertions(+), 1 deletions(-)

diff --git a/tools/kvm/virtio.c b/tools/kvm/virtio.c
index 6249521..266a1b6 100644
--- a/tools/kvm/virtio.c
+++ b/tools/kvm/virtio.c
@@ -1,15 +1,32 @@
 #include <linux/virtio_ring.h>
 #include <stdint.h>
 #include <sys/uio.h>
+#include <asm/system.h>
 #include "kvm/kvm.h"
 #include "kvm/virtio.h"
 
 struct vring_used_elem *virt_queue__set_used_elem(struct virt_queue *queue, uint32_t head, uint32_t len)
 {
 	struct vring_used_elem *used_elem;
-	used_elem	= &queue->vring.used->ring[queue->vring.used->idx++ % queue->vring.num];
+	used_elem	= &queue->vring.used->ring[queue->vring.used->idx % queue->vring.num];
 	used_elem->id	= head;
 	used_elem->len	= len;
+
+	/*
+	 * Use wmb to assure that used elem was updated with head and len.
+	 * We need a wmb here since we can't advance idx unless we're ready
+	 * to pass the used element to the guest.
+	 */
+	wmb();
+	queue->vring.used->idx++;
+
+	/*
+	 * Use wmb to assure used idx has been increased before we signal the guest.
+	 * Without a wmb here the guest may ignore the queue since it won't see
+	 * an updated idx.
+	 */
+	wmb();
+
 	return used_elem;
 }
 
-- 
1.7.5.rc3


  reply	other threads:[~2011-05-03 20:28 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-03 20:28 [PATCH 1/2 V2] kvm tools: Drop ALIGN from bios.h Sasha Levin
2011-05-03 20:28 ` Sasha Levin [this message]
2011-05-03 20:47   ` [PATCH 2/2 V2] kvm tools: Fix virt_queue__set_used_elem Ingo Molnar
2011-05-04  4:41     ` Sasha Levin
2011-05-04  6:33       ` Ingo Molnar
2011-05-04 23:47       ` Asias He
2011-05-05  4:38         ` Sasha Levin
2011-05-05  7:02         ` Ingo Molnar
2011-05-05  7:13           ` Pekka Enberg
2011-05-05  7:18             ` Sasha Levin
2011-05-05  7:32               ` Pekka Enberg
2011-05-05  7:47             ` Ingo Molnar
2011-05-05  7:52               ` Pekka Enberg
2011-05-05  8:01                 ` Sasha Levin
2011-05-05  8:07                 ` Ingo Molnar
2011-05-05  8:12                   ` Ingo Molnar
2011-05-05  8:22                     ` [PATCH] kvm: Fix 32-bit build of the asm/system.h include Ingo Molnar
2011-05-05 14:45                       ` Pekka Enberg
2011-05-05 15:33                         ` Asias He
2011-05-03 20:35 ` [PATCH 1/2 V2] kvm tools: Drop ALIGN from bios.h Cyrill Gorcunov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1304454487-2539-2-git-send-email-levinsasha928@gmail.com \
    --to=levinsasha928@gmail.com \
    --cc=asias.hejun@gmail.com \
    --cc=gorcunov@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=penberg@kernel.org \
    --cc=prasadjoshi124@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox