From patchwork Tue May 3 20:28:07 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sasha Levin X-Patchwork-Id: 751642 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id p43KSeS1010940 for ; Tue, 3 May 2011 20:28:40 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754794Ab1ECU2h (ORCPT ); Tue, 3 May 2011 16:28:37 -0400 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) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:from:to:cc:subject:date:message-id:x-mailer :in-reply-to:references; bh=AvtwrQsBK1RNknWvnipUm6UEX5GeuttDwvpJZxDWorg=; b=os4KcT20TPo2qWYrDaWRBpQ33I9hUPt4gxgKE8zYoSkz5m/fp2GlLQwpcnA3TjRog+ 6fqQX3MPaBsxtCIZV6uFO8yjFnJwfjskjR4XBAvxtMbY0LkfsGPkq9ziBXN/HibDu+Dv B7Hj0vYTCH/eE9lsV0Hwx4xcX2lBmce6eS3AQ= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; b=vOw1x5wE3itq+ulL5M2eHPw7e1oWff7hdn2saboPxT7MTBAu98PdthF1AmHnk/2neT NX9PDllyz9ihWDtR2MlaelEvXyX2fb/53ZDbMD9XMFVAbf4yPQMH9VQPxIxdFNAqT4wB sG7RaQlHept4ZumPUM71Zd0Ky0KUhzk9b+wZE= Received: by 10.216.65.8 with SMTP id e8mr4159177wed.35.1304454515512; Tue, 03 May 2011 13:28:35 -0700 (PDT) Received: from localhost.localdomain (bzq-79-179-202-103.red.bezeqint.net [79.179.202.103]) by mx.google.com with ESMTPS id f30sm239002wef.7.2011.05.03.13.28.33 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 03 May 2011 13:28:35 -0700 (PDT) From: Sasha Levin To: penberg@kernel.org Cc: mingo@elte.hu, asias.hejun@gmail.com, gorcunov@gmail.com, prasadjoshi124@gmail.com, kvm@vger.kernel.org, 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> X-Mailer: git-send-email 1.7.5.rc3 In-Reply-To: <1304454487-2539-1-git-send-email-levinsasha928@gmail.com> References: <1304454487-2539-1-git-send-email-levinsasha928@gmail.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter1.kernel.org [140.211.167.41]); Tue, 03 May 2011 20:28:40 +0000 (UTC) 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 Acked-by: Ingo Molnar --- 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; }