From patchwork Fri May 20 23:40:30 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 624697 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3rBPrh4D6Hz9t49 for ; Sat, 21 May 2016 09:53:08 +1000 (AEST) Received: from localhost ([::1]:38144 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b3uE6-0005IL-Kh for incoming@patchwork.ozlabs.org; Fri, 20 May 2016 19:53:06 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42247) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b3u2w-0002BO-A1 for qemu-devel@nongnu.org; Fri, 20 May 2016 19:41:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b3u2u-0006lh-0H for qemu-devel@nongnu.org; Fri, 20 May 2016 19:41:33 -0400 Received: from mx1.redhat.com ([209.132.183.28]:57777) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b3u2t-0006lX-OD for qemu-devel@nongnu.org; Fri, 20 May 2016 19:41:31 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 51621C04B308; Fri, 20 May 2016 23:41:31 +0000 (UTC) Received: from localhost (ovpn-112-19.ams2.redhat.com [10.36.112.19]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u4KNfQil032483; Fri, 20 May 2016 19:41:28 -0400 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Date: Fri, 20 May 2016 16:40:30 -0700 Message-Id: <1463787632-7241-8-git-send-email-stefanha@redhat.com> In-Reply-To: <1463787632-7241-1-git-send-email-stefanha@redhat.com> References: <1463787632-7241-1-git-send-email-stefanha@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Fri, 20 May 2016 23:41:31 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 7/9] virtio-blk: live migrate s->rq with multiqueue X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Paolo Bonzini , Ming Lei , Fam Zheng , Stefan Hajnoczi Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" Each request in s->rq belongs to a virtqueue. When multiqueue is enabled we can no longer default to the first virtqueue. Explicitly migrate virtqueue indices when needed. The migration stream looks like this: [s->rq][mq_rq_indices, ~QEMU_VM_SUBSECTION][virtio subsections] This patch adds the mq_rq_indices subsection. A terminator byte (~QEMU_VM_SUBSECTION) must be emitted after the subsection since the generic virtio subsections follow and vmstate_load_state() would attempt to load them too. This change preserves migration compatibility as follows: 1. Old -> new: multiqueue is not be enabled so the mq_rq_indices subsection is not in the migration stream. virtio_blk_load_device() attempts to load subsections but fails since any subsection is a generic virtio subsection and we continue. The generic virtio code will then load the subsection that we failed to load. 2. New -> old: when multiqueue is disabled the migration stream is unchanged and therefore compatible. When multiqueue is enabled the generic virtio subsection loading safely fails when it hits virtio_blk/mq_rq_indices. In summary, the only failure case is when multiqueue is enabled in a new QEMU and we migrate to an old QEMU. Signed-off-by: Stefan Hajnoczi --- hw/block/virtio-blk.c | 123 +++++++++++++++++++++++++++++++++++++++-- include/hw/virtio/virtio-blk.h | 5 ++ 2 files changed, 123 insertions(+), 5 deletions(-) diff --git a/hw/block/virtio-blk.c b/hw/block/virtio-blk.c index ab0f589..64b4185 100644 --- a/hw/block/virtio-blk.c +++ b/hw/block/virtio-blk.c @@ -16,6 +16,7 @@ #include "qemu-common.h" #include "qemu/iov.h" #include "qemu/error-report.h" +#include "migration/migration.h" #include "trace.h" #include "hw/block/block.h" #include "sysemu/block-backend.h" @@ -823,6 +824,42 @@ static void virtio_blk_set_status(VirtIODevice *vdev, uint8_t status) } } +static bool virtio_blk_mq_rq_indices_needed(void *opaque) +{ + VirtIOBlock *s = opaque; + + return s->conf.num_queues && s->rq; +} + +/* Array of virtqueue indices for requests in s->rq */ +static const VMStateDescription vmstate_virtio_blk_mq_rq_indices = { + .name = "virtio_blk/mq_rq_indices", + .version_id = 1, + .minimum_version_id = 1, + .minimum_version_id_old = 1, + .needed = virtio_blk_mq_rq_indices_needed, + .fields = (VMStateField[]) { + VMSTATE_UINT32(num_rq, VirtIOBlock), + VMSTATE_VARRAY_UINT32_ALLOC(mq_rq_indices, VirtIOBlock, num_rq, 1, + vmstate_info_uint32, uint32_t), + VMSTATE_END_OF_LIST() + } +}; + +static const VMStateDescription virtio_blk_vmstate = { + .name = "virtio_blk", + .version_id = 1, + .minimum_version_id = 1, + .minimum_version_id_old = 1, + .fields = (VMStateField[]) { + VMSTATE_END_OF_LIST() + }, + .subsections = (const VMStateDescription * []) { + &vmstate_virtio_blk_mq_rq_indices, + NULL + } +}; + static void virtio_blk_save(QEMUFile *f, void *opaque) { VirtIODevice *vdev = VIRTIO_DEVICE(opaque); @@ -842,12 +879,36 @@ static void virtio_blk_save_device(VirtIODevice *vdev, QEMUFile *f) VirtIOBlock *s = VIRTIO_BLK(vdev); VirtIOBlockReq *req = s->rq; + s->num_rq = 0; while (req) { qemu_put_sbyte(f, 1); qemu_put_virtqueue_element(f, &req->elem); req = req->next; + s->num_rq++; } qemu_put_sbyte(f, 0); + + /* In order to distinguish virtio-blk subsections from the generic virtio + * device subsections that follow we emit a terminating byte. Old versions + * of QEMU don't expect the terminating byte so, for compatibility, only + * write it when virtio-blk subsections are needed. + */ + if (virtio_blk_mq_rq_indices_needed(s)) { + uint32_t i; + + s->mq_rq_indices = g_new(uint32_t, s->num_rq); + req = s->rq; + for (i = 0; i < s->num_rq; i++) { + s->mq_rq_indices[i] = virtio_get_queue_index(req->vq); + req = req->next; + } + + vmstate_save_state(f, &virtio_blk_vmstate, s, NULL); + qemu_put_ubyte(f, ~QEMU_VM_SUBSECTION); + + g_free(s->mq_rq_indices); + s->mq_rq_indices = NULL; + } } static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id) @@ -865,16 +926,68 @@ static int virtio_blk_load_device(VirtIODevice *vdev, QEMUFile *f, int version_id) { VirtIOBlock *s = VIRTIO_BLK(vdev); + VirtQueue *vq0 = virtio_get_queue(vdev, 0); + VirtIOBlockReq **tailp = (VirtIOBlockReq **)&s->rq; + VirtIOBlockReq *req; + uint32_t num_rq = 0; + int ret; while (qemu_get_sbyte(f)) { - VirtIOBlockReq *req; req = qemu_get_virtqueue_element(f, sizeof(VirtIOBlockReq)); - virtio_blk_init_request(s, s->vq, req); - req->next = s->rq; - s->rq = req; + + /* Virtqueue is adjusted by a subsection in the multiqueue case */ + virtio_blk_init_request(s, vq0, req); + + *tailp = req; + tailp = &req->next; + num_rq++; + } + + s->num_rq = 0; + s->mq_rq_indices = NULL; + ret = vmstate_load_state(f, &virtio_blk_vmstate, s, 1); + if (ret == 0) { + uint32_t i; + + if (qemu_peek_byte(f, 0) != (uint8_t)~QEMU_VM_SUBSECTION) { + if (s->num_rq != 0) { + ret = -EINVAL; /* unexpected terminator byte */ + } else { + ret = 0; /* no subsection for us or generic virtio */ + } + goto out; + } + qemu_file_skip(f, 1); + + if (num_rq != s->num_rq) { + ret = -EINVAL; + goto out; + } + + req = s->rq; + for (i = 0; i < num_rq; i++) { + uint32_t idx = s->mq_rq_indices[i]; + + if (idx >= s->conf.num_queues) { + ret = -EINVAL; + goto out; + } + + req->vq = virtio_get_queue(vdev, idx); + req = req->next; + } + } else if (ret == -ENOENT) { + /* This could be the generic virtio subsections, ignore and let the + * virtio code have a try. If that fails too then load will really + * fail. + */ + ret = 0; } - return 0; +out: + g_free(s->mq_rq_indices); + s->mq_rq_indices = NULL; + return ret; } static void virtio_blk_resize(void *opaque) diff --git a/include/hw/virtio/virtio-blk.h b/include/hw/virtio/virtio-blk.h index b6e7860..0bf9ebc 100644 --- a/include/hw/virtio/virtio-blk.h +++ b/include/hw/virtio/virtio-blk.h @@ -49,6 +49,11 @@ typedef struct VirtIOBlock { BlockBackend *blk; VirtQueue *vq; void *rq; + + /* The following two fields are used only during save/load */ + uint32_t num_rq; + uint32_t *mq_rq_indices; + QEMUBH *bh; QEMUBH *batch_notify_bh; unsigned long *batch_notify_vqs;