From mboxrd@z Thu Jan 1 00:00:00 1970 From: Sasha Levin Subject: [PATCH 2/2 V2] kvm tools: Fix virt_queue__set_used_elem Date: Tue, 3 May 2011 23:28:07 +0300 Message-ID: <1304454487-2539-2-git-send-email-levinsasha928@gmail.com> References: <1304454487-2539-1-git-send-email-levinsasha928@gmail.com> Cc: mingo@elte.hu, asias.hejun@gmail.com, gorcunov@gmail.com, prasadjoshi124@gmail.com, kvm@vger.kernel.org, Sasha Levin To: penberg@kernel.org Return-path: Received: from mail-ww0-f44.google.com ([74.125.82.44]:61228 "EHLO mail-ww0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754756Ab1ECU2g (ORCPT ); Tue, 3 May 2011 16:28:36 -0400 Received: by mail-ww0-f44.google.com with SMTP id 36so489017wwa.1 for ; Tue, 03 May 2011 13:28:35 -0700 (PDT) In-Reply-To: <1304454487-2539-1-git-send-email-levinsasha928@gmail.com> Sender: kvm-owner@vger.kernel.org List-ID: 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 --- 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 #include #include +#include #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