From patchwork Fri Nov 9 13:27:29 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amit Shah X-Patchwork-Id: 198058 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 8E5792C0097 for ; Sat, 10 Nov 2012 00:28:15 +1100 (EST) Received: from localhost ([::1]:45247 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TWod3-0007VF-Mf for incoming@patchwork.ozlabs.org; Fri, 09 Nov 2012 08:28:13 -0500 Received: from eggs.gnu.org ([208.118.235.92]:50837) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TWock-0007CJ-Rj for qemu-devel@nongnu.org; Fri, 09 Nov 2012 08:28:00 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TWocd-0002lW-Hu for qemu-devel@nongnu.org; Fri, 09 Nov 2012 08:27:54 -0500 Received: from mx1.redhat.com ([209.132.183.28]:52816) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TWocd-0002lP-9V for qemu-devel@nongnu.org; Fri, 09 Nov 2012 08:27:47 -0500 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id qA9DRjZX032585 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 9 Nov 2012 08:27:45 -0500 Received: from localhost (ovpn-113-126.phx2.redhat.com [10.3.113.126]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id qA9DRiAC016335; Fri, 9 Nov 2012 08:27:45 -0500 From: Amit Shah To: Anthony Liguori Date: Fri, 9 Nov 2012 14:27:29 +0100 Message-Id: <90f4f6755dcebc8d205fd2bde47ff18058f4e3f5.1352466944.git.amit.shah@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Amit Shah , pbonzini@redhat.com, hpa@zytor.com, qemu list , afaerber@suse.de Subject: [Qemu-devel] [PATCH 1/3] virtio-rng: use virtqueue_get_avail_bytes, fix migration X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Popping an elem from the vq just to find out its length causes problems with save/load later on. Use the new virtqueue_get_avail_bytes() function instead, saves us the complexity in the migration code. Signed-off-by: Amit Shah --- hw/virtio-rng.c | 69 +++++++----------------------------------------------- 1 files changed, 9 insertions(+), 60 deletions(-) diff --git a/hw/virtio-rng.c b/hw/virtio-rng.c index b7fb5e9..42ac30b 100644 --- a/hw/virtio-rng.c +++ b/hw/virtio-rng.c @@ -22,14 +22,10 @@ typedef struct VirtIORNG { /* Only one vq - guest puts buffer(s) on it when it needs entropy */ VirtQueue *vq; - VirtQueueElement elem; /* Config data for the device -- currently only chardev */ VirtIORNGConf *conf; - /* Whether we've popped a vq element into 'elem' above */ - bool popped; - RngBackend *rng; } VirtIORNG; @@ -42,23 +38,19 @@ static bool is_guest_ready(VirtIORNG *vrng) return false; } -static size_t pop_an_elem(VirtIORNG *vrng) +static size_t get_request_size(VirtQueue *vq) { - size_t size; + unsigned int in, out; - if (!vrng->popped && !virtqueue_pop(vrng->vq, &vrng->elem)) { - return 0; - } - vrng->popped = true; - - size = iov_size(vrng->elem.in_sg, vrng->elem.in_num); - return size; + virtqueue_get_avail_bytes(vq, &in, &out); + return in; } /* Send data from a char device over to the guest */ static void chr_read(void *opaque, const void *buf, size_t size) { VirtIORNG *vrng = opaque; + VirtQueueElement elem; size_t len; int offset; @@ -68,15 +60,14 @@ static void chr_read(void *opaque, const void *buf, size_t size) offset = 0; while (offset < size) { - if (!pop_an_elem(vrng)) { + if (!virtqueue_pop(vrng->vq, &elem)) { break; } - len = iov_from_buf(vrng->elem.in_sg, vrng->elem.in_num, + len = iov_from_buf(elem.in_sg, elem.in_num, 0, buf + offset, size - offset); offset += len; - virtqueue_push(vrng->vq, &vrng->elem, len); - vrng->popped = false; + virtqueue_push(vrng->vq, &elem, len); } virtio_notify(&vrng->vdev, vrng->vq); @@ -96,7 +87,7 @@ static void handle_input(VirtIODevice *vdev, VirtQueue *vq) VirtIORNG *vrng = DO_UPCAST(VirtIORNG, vdev, vdev); size_t size; - size = pop_an_elem(vrng); + size = get_request_size(vq); if (size) { rng_backend_request_entropy(vrng->rng, size, chr_read, vrng); } @@ -112,23 +103,6 @@ static void virtio_rng_save(QEMUFile *f, void *opaque) VirtIORNG *vrng = opaque; virtio_save(&vrng->vdev, f); - - qemu_put_byte(f, vrng->popped); - if (vrng->popped) { - int i; - - qemu_put_be32(f, vrng->elem.index); - - qemu_put_be32(f, vrng->elem.in_num); - for (i = 0; i < vrng->elem.in_num; i++) { - qemu_put_be64(f, vrng->elem.in_addr[i]); - } - - qemu_put_be32(f, vrng->elem.out_num); - for (i = 0; i < vrng->elem.out_num; i++) { - qemu_put_be64(f, vrng->elem.out_addr[i]); - } - } } static int virtio_rng_load(QEMUFile *f, void *opaque, int version_id) @@ -139,30 +113,6 @@ static int virtio_rng_load(QEMUFile *f, void *opaque, int version_id) return -EINVAL; } virtio_load(&vrng->vdev, f); - - vrng->popped = qemu_get_byte(f); - if (vrng->popped) { - int i; - - vrng->elem.index = qemu_get_be32(f); - - vrng->elem.in_num = qemu_get_be32(f); - g_assert(vrng->elem.in_num < VIRTQUEUE_MAX_SIZE); - for (i = 0; i < vrng->elem.in_num; i++) { - vrng->elem.in_addr[i] = qemu_get_be64(f); - } - - vrng->elem.out_num = qemu_get_be32(f); - g_assert(vrng->elem.out_num < VIRTQUEUE_MAX_SIZE); - for (i = 0; i < vrng->elem.out_num; i++) { - vrng->elem.out_addr[i] = qemu_get_be64(f); - } - - virtqueue_map_sg(vrng->elem.in_sg, vrng->elem.in_addr, - vrng->elem.in_num, 1); - virtqueue_map_sg(vrng->elem.out_sg, vrng->elem.out_addr, - vrng->elem.out_num, 0); - } return 0; } @@ -195,7 +145,6 @@ VirtIODevice *virtio_rng_init(DeviceState *dev, VirtIORNGConf *conf) vrng->qdev = dev; vrng->conf = conf; - vrng->popped = false; register_savevm(dev, "virtio-rng", -1, 1, virtio_rng_save, virtio_rng_load, vrng);