From bccdef6b1a204db0f41ffb6e24ce373e4d7890d4 Mon Sep 17 00:00:00 2001 From: Stefan Hajnoczi Date: Mon, 15 Aug 2016 13:54:15 +0100 Subject: [PATCH 1/2] virtio: recalculate vq->inuse after migration The vq->inuse field is not migrated. Many devices don't hold VirtQueueElements across migration so it doesn't matter that vq->inuse starts at 0 on the destination QEMU. At least virtio-serial, virtio-blk, and virtio-balloon migrate while holding VirtQueueElements. For these devices we need to recalculate vq->inuse upon load so the value is correct. Cc: qemu-stable@nongnu.org Signed-off-by: Stefan Hajnoczi Reviewed-by: Michael S. Tsirkin Reviewed-by: Cornelia Huck Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/virtio/virtio.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index 15ee3a71fa..6105c6eb62 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -1648,6 +1648,21 @@ int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id) } vdev->vq[i].used_idx = vring_used_idx(&vdev->vq[i]); vdev->vq[i].shadow_avail_idx = vring_avail_idx(&vdev->vq[i]); + + /* + * Some devices migrate VirtQueueElements that have been popped + * from the avail ring but not yet returned to the used ring. + */ + vdev->vq[i].inuse = vdev->vq[i].last_avail_idx - + vdev->vq[i].used_idx; + if (vdev->vq[i].inuse > vdev->vq[i].vring.num) { + error_report("VQ %d size 0x%x < last_avail_idx 0x%x - " + "used_idx 0x%x", + i, vdev->vq[i].vring.num, + vdev->vq[i].last_avail_idx, + vdev->vq[i].used_idx); + return -1; + } } } From 58a83c61496eeb0d31571a07a51bc1947e3379ac Mon Sep 17 00:00:00 2001 From: Stefan Hajnoczi Date: Mon, 15 Aug 2016 13:54:16 +0100 Subject: [PATCH 2/2] virtio: decrement vq->inuse in virtqueue_discard() virtqueue_discard() moves vq->last_avail_idx back so the element can be popped again. It's necessary to decrement vq->inuse to avoid "leaking" the element count. Cc: qemu-stable@nongnu.org Signed-off-by: Stefan Hajnoczi Reviewed-by: Michael S. Tsirkin Reviewed-by: Cornelia Huck Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/virtio/virtio.c | 1 + 1 file changed, 1 insertion(+) diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index 6105c6eb62..74c085c74d 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -268,6 +268,7 @@ void virtqueue_discard(VirtQueue *vq, const VirtQueueElement *elem, unsigned int len) { vq->last_avail_idx--; + vq->inuse--; virtqueue_unmap_sg(vq, elem, len); }