Skip to contentSkip to conversation

virtio: fix VirtQueue->inuse field

PATCH In review Stefan Hajnoczi 5 participants 12 messages Aug 15, 2016 – Aug 31, 2016

lore
Share & tools
lore
#0 lore raw

cover virtio: fix VirtQueue->inuse field

The VirtQueue->inuse field is not always updated correctly. These patches fix it.

Originally this series was called "virtio-balloon: fix stats vq migration" but Ladi Prosek posted a nicer fix called "balloon: Fix failure of updating guest memory status". I dropped the virtio-balloon patches.

Changes from previous series:
* Missing comma in error formatting [Fam]
* virtio_descard() -> virtio_discard() [Michael]
* Multi-line comment style [Cornelia]

Stefan Hajnoczi (2):
virtio: recalculate vq->inuse after migration
virtio: decrement vq->inuse in virtqueue_discard()

 hw/virtio/virtio.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
Signature · 1 line
2.7.4
#1 lore raw

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.

---
 hw/virtio/virtio.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
hw/virtio/virtio.c +15 -0
index 15ee3a7..6105c6e 100644
@@ -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;
+            }
         }
     }
 
Signature · 1 line
2.7.4
#2 lore raw

PATCH 2/2 virtio: decrement vq->inuse in virtqueue_discard()

In reply to Stefan Hajnoczi

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.

---
 hw/virtio/virtio.c | 1 +
 1 file changed, 1 insertion(+)
hw/virtio/virtio.c +1 -0
index 6105c6e..74c085c 100644
@@ -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);
 }
 
Signature · 1 line
2.7.4
#3 lore raw
In reply to Stefan Hajnoczi
Quoted 12 lines · Stefan Hajnoczi

The VirtQueue->inuse field is not always updated correctly. These patches fix it.

Originally this series was called "virtio-balloon: fix stats vq migration" but Ladi Prosek posted a nicer fix called "balloon: Fix failure of updating guest memory status". I dropped the virtio-balloon patches.

Changes from previous series:
* Missing comma in error formatting [Fam]
* virtio_descard() -> virtio_discard() [Michael]
* Multi-line comment style [Cornelia]

Quoted 6 lines

Stefan Hajnoczi (2):
virtio: recalculate vq->inuse after migration
virtio: decrement vq->inuse in virtqueue_discard()

 hw/virtio/virtio.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

I should mention this is for QEMU 2.7. These fixes are needed if the CVE-2016-5403 patch has been applied.

Without these patches any device that holds VirtQueueElements across live migration will terminate with a "Virtqueue size exceeded" error message. virtio-balloon and virtio-scsi are affected. virtio-blk probably too but I haven't tested it.

Stefan

#4 lore raw

Am 17.08.2016 um 15:58 schrieb Stefan Hajnoczi:

Quoted 22 lines · Stefan Hajnoczi
Quoted 12 lines

The VirtQueue->inuse field is not always updated correctly. These patches fix it.

Originally this series was called "virtio-balloon: fix stats vq migration" but Ladi Prosek posted a nicer fix called "balloon: Fix failure of updating guest memory status". I dropped the virtio-balloon patches.

Changes from previous series:
* Missing comma in error formatting [Fam]
* virtio_descard() -> virtio_discard() [Michael]
* Multi-line comment style [Cornelia]

Quoted 6 lines

Stefan Hajnoczi (2):

   virtio: recalculate vq->inuse after migration
   virtio: decrement vq->inuse in virtqueue_discard()
  hw/virtio/virtio.c | 16 ++++++++++++++++
  1 file changed, 16 insertions(+)

I should mention this is for QEMU 2.7. These fixes are needed if the CVE-2016-5403 patch has been applied.

Quoted 6 lines

Without these patches any device that holds VirtQueueElements across live migration will terminate with a "Virtqueue size exceeded" error message. virtio-balloon and virtio-scsi are affected. virtio-blk probably too but I haven't tested it.

Stefan

I noticed that these patches are not in master yet and therefore not included in the 2.7.0-rc4 tagges yesterday. Is there any issue with them?

Peter

#5 lore raw
Quoted 28 lines · Peter Lieven

Am 17.08.2016 um 15:58 schrieb Stefan Hajnoczi:

Quoted 27 lines · Stefan Hajnoczi
Quoted 12 lines

The VirtQueue->inuse field is not always updated correctly. These patches fix it.

Originally this series was called "virtio-balloon: fix stats vq migration" but Ladi Prosek posted a nicer fix called "balloon: Fix failure of updating guest memory status". I dropped the virtio-balloon patches.

Changes from previous series:
* Missing comma in error formatting [Fam]
* virtio_descard() -> virtio_discard() [Michael]
* Multi-line comment style [Cornelia]

Quoted 6 lines

Stefan Hajnoczi (2):

   virtio: recalculate vq->inuse after migration
   virtio: decrement vq->inuse in virtqueue_discard()
  hw/virtio/virtio.c | 16 ++++++++++++++++
  1 file changed, 16 insertions(+)

I should mention this is for QEMU 2.7. These fixes are needed if the CVE-2016-5403 patch has been applied.

Without these patches any device that holds VirtQueueElements across live migration will terminate with a "Virtqueue size exceeded" error message. virtio-balloon and virtio-scsi are affected. virtio-blk probably too but I haven't tested it.

Quoted 4 lines
Quoted 1 line

Stefan

I noticed that these patches are not in master yet and therefore not included in the 2.7.0-rc4 tagges yesterday. Is there any issue with them?

I think Michael Tsirkin is offline. This series is waiting on him to merge.

Stefan

#6 lore raw
In reply to Stefan Hajnoczi
Quoted 12 lines · Stefan Hajnoczi

The VirtQueue->inuse field is not always updated correctly. These patches fix it.

Originally this series was called "virtio-balloon: fix stats vq migration" but Ladi Prosek posted a nicer fix called "balloon: Fix failure of updating guest memory status". I dropped the virtio-balloon patches.

Changes from previous series:
* Missing comma in error formatting [Fam]
* virtio_descard() -> virtio_discard() [Michael]
* Multi-line comment style [Cornelia]

Quoted 7 lines

Stefan Hajnoczi (2):

   virtio: recalculate vq->inuse after migration
   virtio: decrement vq->inuse in virtqueue_discard()
  hw/virtio/virtio.c | 16 ++++++++++++++++
  1 file changed, 16 insertions(+)

these patches break 'make check' with the following:

GTESTER check-qtest-x86_64
Warning: path not on HugeTLBFS: /tmp/vhost-test-hRYeTb
Warning: path not on HugeTLBFS: /tmp/vhost-test-hRYeTb
Warning: path not on HugeTLBFS: /tmp/vhost-test-hRYeTb
qemu-system-x86_64: VQ 1 size 0x100 < last_avail_idx 0x0 - used_idx 0x1
qemu-system-x86_64: error while loading state for instance 0x0 of device '0000:00:03.0/virtio-net'
qemu-system-x86_64: load of migration failed: Operation not permitted
Broken pipe
qemu-system-x86_64: Failed to read msg header. Read 0 instead of 12. Original request 11.
GTester: last random seed: R02S122f07a3fc35cfd5b0204e3eb45c61e6
qemu-system-x86_64: Failed to read msg header. Read 0 instead of 12. Original request 11.
Warning: path not on HugeTLBFS: /tmp/vhost-test-60WtDz
blkdebug: Suspended request 'A'
blkdebug: Resuming request 'A'
main-loop: WARNING: I/O thread spun for 1000 iterations
main-loop: WARNING: I/O thread spun for 1000 iterations /home/den/src/git/qemu/tests/Makefile:400: recipe for target 'check-qtest-x86_64' failed
make: *** [check-qtest-x86_64] Error 1
iris ~/src/git/qemu $

Sorry, if I have missed the fix in the list.

Den

#7 lore raw
Quoted 42 lines · Denis V. Lunev
Quoted 12 lines · Stefan Hajnoczi

The VirtQueue->inuse field is not always updated correctly. These patches fix it.

Originally this series was called "virtio-balloon: fix stats vq migration" but Ladi Prosek posted a nicer fix called "balloon: Fix failure of updating guest memory status". I dropped the virtio-balloon patches.

Changes from previous series:
* Missing comma in error formatting [Fam]
* virtio_descard() -> virtio_discard() [Michael]
* Multi-line comment style [Cornelia]

Quoted 7 lines

Stefan Hajnoczi (2):

   virtio: recalculate vq->inuse after migration
   virtio: decrement vq->inuse in virtqueue_discard()
  hw/virtio/virtio.c | 16 ++++++++++++++++
  1 file changed, 16 insertions(+)

these patches break 'make check' with the following:

GTESTER check-qtest-x86_64
Warning: path not on HugeTLBFS: /tmp/vhost-test-hRYeTb
Warning: path not on HugeTLBFS: /tmp/vhost-test-hRYeTb
Warning: path not on HugeTLBFS: /tmp/vhost-test-hRYeTb
qemu-system-x86_64: VQ 1 size 0x100 < last_avail_idx 0x0 - used_idx 0x1
qemu-system-x86_64: error while loading state for instance 0x0 of device '0000:00:03.0/virtio-net'
qemu-system-x86_64: load of migration failed: Operation not permitted
Broken pipe
qemu-system-x86_64: Failed to read msg header. Read 0 instead of 12. Original request 11.
GTester: last random seed: R02S122f07a3fc35cfd5b0204e3eb45c61e6
qemu-system-x86_64: Failed to read msg header. Read 0 instead of 12. Original request 11.
Warning: path not on HugeTLBFS: /tmp/vhost-test-60WtDz
blkdebug: Suspended request 'A'
blkdebug: Resuming request 'A'
main-loop: WARNING: I/O thread spun for 1000 iterations
main-loop: WARNING: I/O thread spun for 1000 iterations /home/den/src/git/qemu/tests/Makefile:400: recipe for target 'check-qtest-x86_64' failed
make: *** [check-qtest-x86_64] Error 1
iris ~/src/git/qemu $

Quoted 4 lines

Sorry, if I have missed the fix in the list.

Den

VERY sorry, pls disregard. This is my mistake :(

Den

#8 lore raw
In reply to Denis V. Lunev
Quoted 50 lines · Denis V. Lunev
Quoted 16 lines · Stefan Hajnoczi

The VirtQueue->inuse field is not always updated correctly. These patches fix
it.

Originally this series was called "virtio-balloon: fix stats vq migration" but
Ladi Prosek posted a nicer fix called "balloon: Fix failure of updating guest
memory status". I dropped the virtio-balloon patches.

Changes from previous series:
* Missing comma in error formatting [Fam]
* virtio_descard() -> virtio_discard() [Michael]
* Multi-line comment style [Cornelia]

Quoted 7 lines

Stefan Hajnoczi (2):

   virtio: recalculate vq->inuse after migration
   virtio: decrement vq->inuse in virtqueue_discard()
  hw/virtio/virtio.c | 16 ++++++++++++++++
  1 file changed, 16 insertions(+)

these patches break 'make check' with the following:

GTESTER check-qtest-x86_64
Warning: path not on HugeTLBFS: /tmp/vhost-test-hRYeTb
Warning: path not on HugeTLBFS: /tmp/vhost-test-hRYeTb
Warning: path not on HugeTLBFS: /tmp/vhost-test-hRYeTb
qemu-system-x86_64: VQ 1 size 0x100 < last_avail_idx 0x0 - used_idx 0x1
qemu-system-x86_64: error while loading state for instance 0x0 of device '0000:00:03.0/virtio-net'
qemu-system-x86_64: load of migration failed: Operation not permitted Broken pipe
qemu-system-x86_64: Failed to read msg header. Read 0 instead of 12. Original request 11.
GTester: last random seed: R02S122f07a3fc35cfd5b0204e3eb45c61e6
qemu-system-x86_64: Failed to read msg header. Read 0 instead of 12. Original request 11.
Warning: path not on HugeTLBFS: /tmp/vhost-test-60WtDz
blkdebug: Suspended request 'A'
blkdebug: Resuming request 'A'
main-loop: WARNING: I/O thread spun for 1000 iterations
main-loop: WARNING: I/O thread spun for 1000 iterations /home/den/src/git/qemu/tests/Makefile:400: recipe for target 'check-qtest-x86_64' failed
make: *** [check-qtest-x86_64] Error 1
iris ~/src/git/qemu $

Quoted 1 line

Sorry, if I have missed the fix in the list.

I hit this issue when backporting to a QEMU 2.3-based source tree. This doesn't happen in qemu.git/master.

To save anyone doing a backport a lot of time:

If you enable vhost-user-test in a QEMU 2.3-based source tree with the migration test case, make sure you also backport 8c56c1a592b5092d91da8d8943c17777d6462a6f ("memory: emulate ioeventfd").

Stefan

#9 lore raw
Quoted 55 lines · Stefan Hajnoczi
Quoted 49 lines · Denis V. Lunev
Quoted 15 lines · Stefan Hajnoczi

The VirtQueue->inuse field is not always updated correctly. These patches fix
it.

Originally this series was called "virtio-balloon: fix stats vq migration" but
Ladi Prosek posted a nicer fix called "balloon: Fix failure of updating guest
memory status". I dropped the virtio-balloon patches.

Changes from previous series:
* Missing comma in error formatting [Fam]
* virtio_descard() -> virtio_discard() [Michael]
* Multi-line comment style [Cornelia]

Quoted 7 lines

Stefan Hajnoczi (2):

   virtio: recalculate vq->inuse after migration
   virtio: decrement vq->inuse in virtqueue_discard()
  hw/virtio/virtio.c | 16 ++++++++++++++++
  1 file changed, 16 insertions(+)

these patches break 'make check' with the following:

GTESTER check-qtest-x86_64
Warning: path not on HugeTLBFS: /tmp/vhost-test-hRYeTb
Warning: path not on HugeTLBFS: /tmp/vhost-test-hRYeTb
Warning: path not on HugeTLBFS: /tmp/vhost-test-hRYeTb
qemu-system-x86_64: VQ 1 size 0x100 < last_avail_idx 0x0 - used_idx 0x1
qemu-system-x86_64: error while loading state for instance 0x0 of device '0000:00:03.0/virtio-net'
qemu-system-x86_64: load of migration failed: Operation not permitted Broken pipe
qemu-system-x86_64: Failed to read msg header. Read 0 instead of 12. Original request 11.
GTester: last random seed: R02S122f07a3fc35cfd5b0204e3eb45c61e6
qemu-system-x86_64: Failed to read msg header. Read 0 instead of 12. Original request 11.
Warning: path not on HugeTLBFS: /tmp/vhost-test-60WtDz
blkdebug: Suspended request 'A'
blkdebug: Resuming request 'A'
main-loop: WARNING: I/O thread spun for 1000 iterations
main-loop: WARNING: I/O thread spun for 1000 iterations /home/den/src/git/qemu/tests/Makefile:400: recipe for target 'check-qtest-x86_64' failed
make: *** [check-qtest-x86_64] Error 1
iris ~/src/git/qemu $

Quoted 1 line

Sorry, if I have missed the fix in the list.

I hit this issue when backporting to a QEMU 2.3-based source tree. This doesn't happen in qemu.git/master.

Quoted 8 lines

To save anyone doing a backport a lot of time:

If you enable vhost-user-test in a QEMU 2.3-based source tree with the migration test case, make sure you also backport 8c56c1a592b5092d91da8d8943c17777d6462a6f ("memory: emulate ioeventfd").

Stefan

thanks a lot! We will give this a try tomorrow.

Den

#10 lore raw
In reply to Stefan Hajnoczi
Quoted 57 lines · Stefan Hajnoczi
Quoted 50 lines · Denis V. Lunev
Quoted 16 lines · Stefan Hajnoczi

The VirtQueue->inuse field is not always updated correctly. These patches fix
it.

Originally this series was called "virtio-balloon: fix stats vq migration" but
Ladi Prosek posted a nicer fix called "balloon: Fix failure of updating guest
memory status". I dropped the virtio-balloon patches.

Changes from previous series:
* Missing comma in error formatting [Fam]
* virtio_descard() -> virtio_discard() [Michael]
* Multi-line comment style [Cornelia]

Quoted 7 lines

Stefan Hajnoczi (2):

   virtio: recalculate vq->inuse after migration
   virtio: decrement vq->inuse in virtqueue_discard()
  hw/virtio/virtio.c | 16 ++++++++++++++++
  1 file changed, 16 insertions(+)

these patches break 'make check' with the following:

GTESTER check-qtest-x86_64
Warning: path not on HugeTLBFS: /tmp/vhost-test-hRYeTb
Warning: path not on HugeTLBFS: /tmp/vhost-test-hRYeTb
Warning: path not on HugeTLBFS: /tmp/vhost-test-hRYeTb
qemu-system-x86_64: VQ 1 size 0x100 < last_avail_idx 0x0 - used_idx 0x1
qemu-system-x86_64: error while loading state for instance 0x0 of device '0000:00:03.0/virtio-net'
qemu-system-x86_64: load of migration failed: Operation not permitted Broken pipe
qemu-system-x86_64: Failed to read msg header. Read 0 instead of 12. Original request 11.
GTester: last random seed: R02S122f07a3fc35cfd5b0204e3eb45c61e6
qemu-system-x86_64: Failed to read msg header. Read 0 instead of 12. Original request 11.
Warning: path not on HugeTLBFS: /tmp/vhost-test-60WtDz
blkdebug: Suspended request 'A'
blkdebug: Resuming request 'A'
main-loop: WARNING: I/O thread spun for 1000 iterations
main-loop: WARNING: I/O thread spun for 1000 iterations /home/den/src/git/qemu/tests/Makefile:400: recipe for target 'check-qtest-x86_64' failed
make: *** [check-qtest-x86_64] Error 1
iris ~/src/git/qemu $

Quoted 1 line

Sorry, if I have missed the fix in the list.

I hit this issue when backporting to a QEMU 2.3-based source tree. This doesn't happen in qemu.git/master.

Quoted 6 lines

To save anyone doing a backport a lot of time:

If you enable vhost-user-test in a QEMU 2.3-based source tree with the migration test case, make sure you also backport 8c56c1a592b5092d91da8d8943c17777d6462a6f ("memory: emulate ioeventfd").

That did it, thanks!

Roman.

#11 lore raw
In reply to Stefan Hajnoczi
Quoted 55 lines · Stefan Hajnoczi
Quoted 49 lines · Denis V. Lunev
Quoted 15 lines · Stefan Hajnoczi

The VirtQueue->inuse field is not always updated correctly. These patches fix
it.

Originally this series was called "virtio-balloon: fix stats vq migration" but
Ladi Prosek posted a nicer fix called "balloon: Fix failure of updating guest
memory status". I dropped the virtio-balloon patches.

Changes from previous series:
* Missing comma in error formatting [Fam]
* virtio_descard() -> virtio_discard() [Michael]
* Multi-line comment style [Cornelia]

Quoted 7 lines

Stefan Hajnoczi (2):

   virtio: recalculate vq->inuse after migration
   virtio: decrement vq->inuse in virtqueue_discard()
  hw/virtio/virtio.c | 16 ++++++++++++++++
  1 file changed, 16 insertions(+)

these patches break 'make check' with the following:

GTESTER check-qtest-x86_64
Warning: path not on HugeTLBFS: /tmp/vhost-test-hRYeTb
Warning: path not on HugeTLBFS: /tmp/vhost-test-hRYeTb
Warning: path not on HugeTLBFS: /tmp/vhost-test-hRYeTb
qemu-system-x86_64: VQ 1 size 0x100 < last_avail_idx 0x0 - used_idx 0x1
qemu-system-x86_64: error while loading state for instance 0x0 of device '0000:00:03.0/virtio-net'
qemu-system-x86_64: load of migration failed: Operation not permitted Broken pipe
qemu-system-x86_64: Failed to read msg header. Read 0 instead of 12. Original request 11.
GTester: last random seed: R02S122f07a3fc35cfd5b0204e3eb45c61e6
qemu-system-x86_64: Failed to read msg header. Read 0 instead of 12. Original request 11.
Warning: path not on HugeTLBFS: /tmp/vhost-test-60WtDz
blkdebug: Suspended request 'A'
blkdebug: Resuming request 'A'
main-loop: WARNING: I/O thread spun for 1000 iterations
main-loop: WARNING: I/O thread spun for 1000 iterations /home/den/src/git/qemu/tests/Makefile:400: recipe for target 'check-qtest-x86_64' failed
make: *** [check-qtest-x86_64] Error 1
iris ~/src/git/qemu $

Quoted 1 line

Sorry, if I have missed the fix in the list.

I hit this issue when backporting to a QEMU 2.3-based source tree. This doesn't happen in qemu.git/master.

Quoted 8 lines

To save anyone doing a backport a lot of time:

If you enable vhost-user-test in a QEMU 2.3-based source tree with the migration test case, make sure you also backport 8c56c1a592b5092d91da8d8943c17777d6462a6f ("memory: emulate ioeventfd").

Stefan

and, in addition to this, 4eae2a657d1ff5ada56eb9b4966eae0eff333b0b is also necessary.
Without it scenario with suspend/resume after guest reboot fails.

Thank you Roma for this finding.

Den