Skip to contentSkip to conversation

virtio-balloon: assorted fixes

PATCH Superseded Roman Kagan 3 participants 15 messages Aug 18, 2016 – Aug 19, 2016

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover virtio-balloon: assorted fixes

This patchset addresses a few problems discovered when analyzing aborts of (an older version of) QEMU with backported commit afd9096eb1882f23929f5b5c177898ed231bac66 "virtio: error out if guest exceeds virtqueue size". Those problems are present in master, too, except that they don't trigger an abort and are thus not as easy to notice.

Roman Kagan (4):
virtio: assert on ->inuse underflow
virtio-balloon: make stats virtqueue length 1
virtio-balloon: don't restart stats timer in callback
virtio-balloon: keep collecting stats on save/restore

Cc "Michael S. Tsirkin" <[email protected]>
 hw/virtio/virtio-balloon.c         | 49 +++++++++++++++++++++-----------------
 hw/virtio/virtio.c                 |  3 ++-
 include/hw/virtio/virtio-balloon.h |  1 +
 3 files changed, 30 insertions(+), 23 deletions(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/4 virtio: assert on ->inuse underflow

Make sure that ->inuse counter on virtqueue never goes negative.

This complements commit afd9096eb1882f23929f5b5c177898ed231bac66, "virtio: error out if guest exceeds virtqueue size", which, due to signed ->inuse comparison against unsigned ->vring.num, manifested a bug in virtio-balloon where virtqueue_push() was called before the matching virtqueu_pop(). [That problem will be addressed in followup patches].

Cc "Michael S. Tsirkin" <[email protected]>
Cc Stefan Hajnoczi <[email protected]>
---
 hw/virtio/virtio.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/virtio/virtio.c +2 -1
index 15ee3a7..7a57857 100644
@@ -92,7 +92,7 @@ struct VirtQueue
 
     uint16_t queue_index;
 
-    int inuse;
+    unsigned int inuse;
 
     uint16_t vector;
     VirtIOHandleOutput handle_output;
@@ -290,6 +290,7 @@ void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
 void virtqueue_flush(VirtQueue *vq, unsigned int count)
 {
     uint16_t old, new;
+    assert(vq->inuse >= count);
     /* Make sure buffer is written before we update index. */
     smp_wmb();
     trace_virtqueue_flush(vq, count);
Signature · 1 line
2.7.4
#2 lore raw

PATCH 2/4 virtio-balloon: make stats virtqueue length 1

In reply to Roman Kagan

The protocol for virtio-balloon stats virtqueue doesn't allow more than one element in the virtqueue.

So, instead of trying to compensate for guest misbehavior if it sends new data before the slot has been released by the host, just define the stats virtqueue length to 1 initially and rely on the generic virtio code to handle overflows.

Cc "Michael S. Tsirkin" <[email protected]>
Cc Ladi Prosek <[email protected]>
---
 hw/virtio/virtio-balloon.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
hw/virtio/virtio-balloon.c +1 -8
index 5af429a..0baf4b3 100644
@@ -262,13 +262,6 @@ static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
         goto out;
     }
 
-    if (s->stats_vq_elem != NULL) {
-        /* This should never happen if the driver follows the spec. */
-        virtqueue_push(vq, s->stats_vq_elem, 0);
-        virtio_notify(vdev, vq);
-        g_free(s->stats_vq_elem);
-    }
-
     s->stats_vq_elem = elem;
 
     /* Initialize the stats to get rid of any stale values.  This is only
@@ -443,7 +436,7 @@ static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
 
     s->ivq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
     s->dvq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
-    s->svq = virtio_add_queue(vdev, 128, virtio_balloon_receive_stats);
+    s->svq = virtio_add_queue(vdev, 1, virtio_balloon_receive_stats);
 
     reset_stats(s);
 }
Signature · 1 line
2.7.4
#3 lore raw

Re: [Qemu-devel] [PATCH 2/4] virtio-balloon: make stats virtqueue length 1

Quoted 7 lines · Roman Kagan

The protocol for virtio-balloon stats virtqueue doesn't allow more than one element in the virtqueue.

So, instead of trying to compensate for guest misbehavior if it sends new data before the slot has been released by the host, just define the stats virtqueue length to 1 initially and rely on the generic virtio code to handle overflows.

Looks good to me, one nit inline.

Quoted 12 lines
Cc "Michael S. Tsirkin" <[email protected]>
Cc Ladi Prosek <[email protected]>
---
 hw/virtio/virtio-balloon.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
hw/virtio/virtio-balloon.c +0 -0
index 5af429a..0baf4b3 100644

hw/virtio/virtio-balloon.c static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq) · line 271

Quoted 10 lines
hw/virtio/virtio-balloon.c +0 -7
@@ -262,13 +262,6 @@ static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
         goto out;
     }
 
-    if (s->stats_vq_elem != NULL) {
-        /* This should never happen if the driver follows the spec. */
-        virtqueue_push(vq, s->stats_vq_elem, 0);
-        virtio_notify(vdev, vq);
-        g_free(s->stats_vq_elem);
-    }
-

Worth adding an assert?

assert(s->stats_vq_elem == NULL); // enforced by one-element stats virtqueue

Quoted 15 lines
hw/virtio/virtio-balloon.c +1 -2
     s->stats_vq_elem = elem;
 
     /* Initialize the stats to get rid of any stale values.  This is only
@@ -443,7 +436,7 @@ static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
 
     s->ivq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
     s->dvq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
-    s->svq = virtio_add_queue(vdev, 128, virtio_balloon_receive_stats);
+    s->svq = virtio_add_queue(vdev, 1, virtio_balloon_receive_stats);
 
     reset_stats(s);
 }
--

2.7.4

#4 lore raw

Re: [Qemu-devel] [PATCH 2/4] virtio-balloon: make stats virtqueue length 1

Quoted 36 lines · Ladi Prosek
Quoted 7 lines · Roman Kagan

The protocol for virtio-balloon stats virtqueue doesn't allow more than one element in the virtqueue.

So, instead of trying to compensate for guest misbehavior if it sends new data before the slot has been released by the host, just define the stats virtqueue length to 1 initially and rely on the generic virtio code to handle overflows.

Looks good to me, one nit inline.

Quoted 12 lines
Cc "Michael S. Tsirkin" <[email protected]>
Cc Ladi Prosek <[email protected]>
---
 hw/virtio/virtio-balloon.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
hw/virtio/virtio-balloon.c +0 -0
index 5af429a..0baf4b3 100644

hw/virtio/virtio-balloon.c static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq) · line 271

Quoted 10 lines
hw/virtio/virtio-balloon.c +0 -7
@@ -262,13 +262,6 @@ static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
         goto out;
     }
 
-    if (s->stats_vq_elem != NULL) {
-        /* This should never happen if the driver follows the spec. */
-        virtqueue_push(vq, s->stats_vq_elem, 0);
-        virtio_notify(vdev, vq);
-        g_free(s->stats_vq_elem);
-    }
-

Worth adding an assert?

Quoted 1 line

assert(s->stats_vq_elem == NULL); // enforced by one-element stats virtqueue

Yep, makes perfect sense.

Thanks,
Roman.

#5 lore raw

Re: [Qemu-devel] [PATCH 2/4] virtio-balloon: make stats virtqueue length 1

In reply to Roman Kagan
Quoted 34 lines · Roman Kagan

The protocol for virtio-balloon stats virtqueue doesn't allow more than one element in the virtqueue.

So, instead of trying to compensate for guest misbehavior if it sends new data before the slot has been released by the host, just define the stats virtqueue length to 1 initially and rely on the generic virtio code to handle overflows.

Cc "Michael S. Tsirkin" <[email protected]>
Cc Ladi Prosek <[email protected]>
---
 hw/virtio/virtio-balloon.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
hw/virtio/virtio-balloon.c +0 -7
index 5af429a..0baf4b3 100644
@@ -262,13 +262,6 @@ static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
         goto out;
     }
 
-    if (s->stats_vq_elem != NULL) {
-        /* This should never happen if the driver follows the spec. */
-        virtqueue_push(vq, s->stats_vq_elem, 0);
-        virtio_notify(vdev, vq);
-        g_free(s->stats_vq_elem);
-    }
-
     s->stats_vq_elem = elem;
 
     /* Initialize the stats to get rid of any stale values.  This is only

hw/virtio/virtio-balloon.c static void virtio_balloon_device_realize(DeviceState *dev, Error **errp) · line 439

Quoted 5 lines
hw/virtio/virtio-balloon.c +1 -1
@@ -443,7 +436,7 @@ static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
 
     s->ivq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
     s->dvq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
-    s->svq = virtio_add_queue(vdev, 128, virtio_balloon_receive_stats);
+    s->svq = virtio_add_queue(vdev, 1, virtio_balloon_receive_stats);

This change breaks live migration compatibility. The device is not allowed to change in existing machine types.

I think Ladi's fix for this bug is the way to go. It preserves migration compatibility.

#6 lore raw

Re: [Qemu-devel] [PATCH 2/4] virtio-balloon: make stats virtqueue length 1

Quoted 36 lines · Stefan Hajnoczi
Quoted 35 lines · Roman Kagan

The protocol for virtio-balloon stats virtqueue doesn't allow more than one element in the virtqueue.

So, instead of trying to compensate for guest misbehavior if it sends new data before the slot has been released by the host, just define the stats virtqueue length to 1 initially and rely on the generic virtio code to handle overflows.

Cc "Michael S. Tsirkin" <[email protected]>
Cc Ladi Prosek <[email protected]>
---
 hw/virtio/virtio-balloon.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
hw/virtio/virtio-balloon.c +0 -7
index 5af429a..0baf4b3 100644
@@ -262,13 +262,6 @@ static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
         goto out;
     }
 
-    if (s->stats_vq_elem != NULL) {
-        /* This should never happen if the driver follows the spec. */
-        virtqueue_push(vq, s->stats_vq_elem, 0);
-        virtio_notify(vdev, vq);
-        g_free(s->stats_vq_elem);
-    }
-
     s->stats_vq_elem = elem;
 
     /* Initialize the stats to get rid of any stale values.  This is only
@@ -443,7 +436,7 @@ static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
 
Quoted 7 lines

hw/virtio/virtio-balloon.c static void virtio_balloon_device_realize(DeviceState *dev, Error **errp) · line 439

Quoted 4 lines
hw/virtio/virtio-balloon.c +1 -1
     s->ivq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
     s->dvq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
-    s->svq = virtio_add_queue(vdev, 128, virtio_balloon_receive_stats);
+    s->svq = virtio_add_queue(vdev, 1, virtio_balloon_receive_stats);

This change breaks live migration compatibility. The device is not allowed to change in existing machine types.

Ideed.

Quoted 2 lines

I think Ladi's fix for this bug is the way to go. It preserves migration compatibility.

Agreed.

Thanks,
Roman.

#7 lore raw

PATCH 3/4 virtio-balloon: don't restart stats timer in callback

In reply to Roman Kagan

There's no need to restart the stats timer in its callback. If the callback happens to run when there's nothing to do just do nothing and return.

The timer is armed either in receive handler or initially when periodic stats collection is enabled via QMP.

While at this, observe that the presence of ->stats_vq_elem is enough to indicate there's work to do here, and drop the check for the stats feature.

Cc "Michael S. Tsirkin" <[email protected]>
---
 hw/virtio/virtio-balloon.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
hw/virtio/virtio-balloon.c +4 -9
index 0baf4b3..b56fecd 100644
@@ -68,12 +68,6 @@ static inline void reset_stats(VirtIOBalloon *dev)
     for (i = 0; i < VIRTIO_BALLOON_S_NR; dev->stats[i++] = -1);
 }
 
-static bool balloon_stats_supported(const VirtIOBalloon *s)
-{
-    VirtIODevice *vdev = VIRTIO_DEVICE(s);
-    return virtio_vdev_has_feature(vdev, VIRTIO_BALLOON_F_STATS_VQ);
-}
-
 static bool balloon_stats_enabled(const VirtIOBalloon *s)
 {
     return s->stats_poll_interval > 0;
@@ -99,9 +93,10 @@ static void balloon_stats_poll_cb(void *opaque)
     VirtIOBalloon *s = opaque;
     VirtIODevice *vdev = VIRTIO_DEVICE(s);
 
-    if (s->stats_vq_elem == NULL || !balloon_stats_supported(s)) {
-        /* re-schedule */
-        balloon_stats_change_timer(s, s->stats_poll_interval);
+    if (!s->stats_vq_elem) {
+        /* The guest hasn't sent the stats yet (either not enabled or we came
+         * too early), nothing to do.  Once the guest starts sending stats the
+         * timer will get armed in receive handler */
         return;
     }
 
Signature · 1 line
2.7.4
#8 lore raw

PATCH 4/4 virtio-balloon: keep collecting stats on save/restore

In reply to Roman Kagan

Upon save/restore virtio-balloon stats acquisition stops. The reason is that the fact that the (only) virtqueue element is being used by QEMU is not recorded anywhere on save, so upon restore it's not released to the guest, making further progress impossible.

Saving the information about the used element would introduce unjustified vmstate incompatibility.

So instead just make sure the element is pushed before save, leaving the ball on the guest side. For that, add vm state change handler to virtio-ballon which would take care of pushing the element if there is one.

Cc "Michael S. Tsirkin" <[email protected]>
---
 hw/virtio/virtio-balloon.c         | 27 ++++++++++++++++++++++-----
 include/hw/virtio/virtio-balloon.h |  1 +
 2 files changed, 23 insertions(+), 5 deletions(-)
hw/virtio/virtio-balloon.c +22 -5
index b56fecd..66a926a 100644
@@ -88,10 +88,19 @@ static void balloon_stats_change_timer(VirtIOBalloon *s, int64_t secs)
     timer_mod(s->stats_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + secs * 1000);
 }
 
+static void balloon_stats_push_elem(VirtIOBalloon *s)
+{
+    VirtIODevice *vdev = VIRTIO_DEVICE(s);
+
+    virtqueue_push(s->svq, s->stats_vq_elem, s->stats_vq_offset);
+    virtio_notify(vdev, s->svq);
+    g_free(s->stats_vq_elem);
+    s->stats_vq_elem = NULL;
+}
+
 static void balloon_stats_poll_cb(void *opaque)
 {
     VirtIOBalloon *s = opaque;
-    VirtIODevice *vdev = VIRTIO_DEVICE(s);
 
     if (!s->stats_vq_elem) {
         /* The guest hasn't sent the stats yet (either not enabled or we came
@@ -100,10 +109,7 @@ static void balloon_stats_poll_cb(void *opaque)
         return;
     }
 
-    virtqueue_push(s->svq, s->stats_vq_elem, s->stats_vq_offset);
-    virtio_notify(vdev, s->svq);
-    g_free(s->stats_vq_elem);
-    s->stats_vq_elem = NULL;
+    balloon_stats_push_elem(s);
 }
 
 static void balloon_stats_get_all(Object *obj, Visitor *v, const char *name,
@@ -411,6 +417,15 @@ static int virtio_balloon_load_device(VirtIODevice *vdev, QEMUFile *f,
     return 0;
 }
 
+static void balloon_vm_state_change(void *opaque, int running, RunState state)
+{
+    VirtIOBalloon *s = opaque;
+
+    if (!running && s->stats_vq_elem) {
+        balloon_stats_push_elem(s);
+    }
+}
+
 static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
@@ -433,6 +448,7 @@ static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
     s->dvq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
     s->svq = virtio_add_queue(vdev, 1, virtio_balloon_receive_stats);
 
+    s->change = qemu_add_vm_change_state_handler(balloon_vm_state_change, s);
     reset_stats(s);
 }
 
@@ -441,6 +457,7 @@ static void virtio_balloon_device_unrealize(DeviceState *dev, Error **errp)
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtIOBalloon *s = VIRTIO_BALLOON(dev);
 
+    qemu_del_vm_change_state_handler(s->change);
     balloon_stats_destroy_timer(s);
     qemu_remove_balloon_handler(s);
     virtio_cleanup(vdev);
include/hw/virtio/virtio-balloon.h +1 -0
index 1ea13bd..d72ff7f 100644
@@ -43,6 +43,7 @@ typedef struct VirtIOBalloon {
     int64_t stats_last_update;
     int64_t stats_poll_interval;
     uint32_t host_features;
+    VMChangeStateEntry *change;
 } VirtIOBalloon;
 
 #endif
Signature · 1 line
2.7.4
#9 lore raw

Re: [Qemu-devel] [PATCH 4/4] virtio-balloon: keep collecting stats on save/restore

Quoted 5 lines · Roman Kagan

Upon save/restore virtio-balloon stats acquisition stops. The reason is that the fact that the (only) virtqueue element is being used by QEMU is not recorded anywhere on save, so upon restore it's not released to the guest, making further progress impossible.

Quoted 7 lines

Saving the information about the used element would introduce unjustified vmstate incompatibility.

So instead just make sure the element is pushed before save, leaving the ball on the guest side. For that, add vm state change handler to virtio-ballon which would take care of pushing the element if there is one.

Please take a look at:
https://lists.gnu.org/archive/html/qemu-devel/2016-08/msg01038.html

virtqueue_discard looks like a better choice than virtqueue_push. There's no need to cause churn on the guest side and receive an extra set of stats in between the regular timer callbacks. Also in that thread see the note about stats_vq_offset which is misused and can be killed.

Thanks,
Ladi

Quoted 92 lines
Cc "Michael S. Tsirkin" <[email protected]>
---
 hw/virtio/virtio-balloon.c         | 27 ++++++++++++++++++++++-----
 include/hw/virtio/virtio-balloon.h |  1 +
 2 files changed, 23 insertions(+), 5 deletions(-)
hw/virtio/virtio-balloon.c +22 -5
index b56fecd..66a926a 100644
@@ -88,10 +88,19 @@ static void balloon_stats_change_timer(VirtIOBalloon *s, int64_t secs)
     timer_mod(s->stats_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + secs * 1000);
 }
 
+static void balloon_stats_push_elem(VirtIOBalloon *s)
+{
+    VirtIODevice *vdev = VIRTIO_DEVICE(s);
+
+    virtqueue_push(s->svq, s->stats_vq_elem, s->stats_vq_offset);
+    virtio_notify(vdev, s->svq);
+    g_free(s->stats_vq_elem);
+    s->stats_vq_elem = NULL;
+}
+
 static void balloon_stats_poll_cb(void *opaque)
 {
     VirtIOBalloon *s = opaque;
-    VirtIODevice *vdev = VIRTIO_DEVICE(s);
 
     if (!s->stats_vq_elem) {
         /* The guest hasn't sent the stats yet (either not enabled or we came
@@ -100,10 +109,7 @@ static void balloon_stats_poll_cb(void *opaque)
         return;
     }
 
-    virtqueue_push(s->svq, s->stats_vq_elem, s->stats_vq_offset);
-    virtio_notify(vdev, s->svq);
-    g_free(s->stats_vq_elem);
-    s->stats_vq_elem = NULL;
+    balloon_stats_push_elem(s);
 }
 
 static void balloon_stats_get_all(Object *obj, Visitor *v, const char *name,
@@ -411,6 +417,15 @@ static int virtio_balloon_load_device(VirtIODevice *vdev, QEMUFile *f,
     return 0;
 }
 
+static void balloon_vm_state_change(void *opaque, int running, RunState state)
+{
+    VirtIOBalloon *s = opaque;
+
+    if (!running && s->stats_vq_elem) {
+        balloon_stats_push_elem(s);
+    }
+}
+
 static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
@@ -433,6 +448,7 @@ static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
     s->dvq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
     s->svq = virtio_add_queue(vdev, 1, virtio_balloon_receive_stats);
 
+    s->change = qemu_add_vm_change_state_handler(balloon_vm_state_change, s);
     reset_stats(s);
 }
 
@@ -441,6 +457,7 @@ static void virtio_balloon_device_unrealize(DeviceState *dev, Error **errp)
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtIOBalloon *s = VIRTIO_BALLOON(dev);
 
+    qemu_del_vm_change_state_handler(s->change);
     balloon_stats_destroy_timer(s);
     qemu_remove_balloon_handler(s);
     virtio_cleanup(vdev);
include/hw/virtio/virtio-balloon.h +1 -1
index 1ea13bd..d72ff7f 100644
@@ -43,6 +43,7 @@ typedef struct VirtIOBalloon {
     int64_t stats_last_update;
     int64_t stats_poll_interval;
     uint32_t host_features;
+    VMChangeStateEntry *change;
 } VirtIOBalloon;
 
 #endif
--

2.7.4

#10 lore raw

Re: [Qemu-devel] [PATCH 4/4] virtio-balloon: keep collecting stats on save/restore

Quoted 9 lines · Ladi Prosek
Quoted 8 lines · Roman Kagan

Upon save/restore virtio-balloon stats acquisition stops. The reason is that the fact that the (only) virtqueue element is being used by QEMU is not recorded anywhere on save, so upon restore it's not released to the guest, making further progress impossible.

Saving the information about the used element would introduce unjustified vmstate incompatibility.

Quoted 7 lines
Quoted 4 lines

So instead just make sure the element is pushed before save, leaving the ball on the guest side. For that, add vm state change handler to virtio-ballon which would take care of pushing the element if there is one.

Please take a look at:
https://lists.gnu.org/archive/html/qemu-devel/2016-08/msg01038.html

Thanks for the pointer, I wish I did a better maillist search before submitting.

Quoted 3 lines

virtqueue_discard looks like a better choice than virtqueue_push. There's no need to cause churn on the guest side and receive an extra set of stats in between the regular timer callbacks.

I forgot about virtqueue_discard; however, I still tend to slightly prefer the push version as it IMO makes things easier to follow: the pumping of stats is always initiated by the guest (both initially on guest driver load and upon restore), and ->stats_vq_elem being non-NULL is enough to know that it needs pushing (both in the timer callback and in vm state change handler).

Quoted 2 lines

Also in that thread see the note about stats_vq_offset which is misused and can be killed.

Indeed.

Thanks,
Roman.

Quoted 92 lines
Quoted 92 lines
Cc "Michael S. Tsirkin" <[email protected]>
---
 hw/virtio/virtio-balloon.c         | 27 ++++++++++++++++++++++-----
 include/hw/virtio/virtio-balloon.h |  1 +
 2 files changed, 23 insertions(+), 5 deletions(-)
hw/virtio/virtio-balloon.c +22 -5
index b56fecd..66a926a 100644
@@ -88,10 +88,19 @@ static void balloon_stats_change_timer(VirtIOBalloon *s, int64_t secs)
     timer_mod(s->stats_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + secs * 1000);
 }
 
+static void balloon_stats_push_elem(VirtIOBalloon *s)
+{
+    VirtIODevice *vdev = VIRTIO_DEVICE(s);
+
+    virtqueue_push(s->svq, s->stats_vq_elem, s->stats_vq_offset);
+    virtio_notify(vdev, s->svq);
+    g_free(s->stats_vq_elem);
+    s->stats_vq_elem = NULL;
+}
+
 static void balloon_stats_poll_cb(void *opaque)
 {
     VirtIOBalloon *s = opaque;
-    VirtIODevice *vdev = VIRTIO_DEVICE(s);
 
     if (!s->stats_vq_elem) {
         /* The guest hasn't sent the stats yet (either not enabled or we came
@@ -100,10 +109,7 @@ static void balloon_stats_poll_cb(void *opaque)
         return;
     }
 
-    virtqueue_push(s->svq, s->stats_vq_elem, s->stats_vq_offset);
-    virtio_notify(vdev, s->svq);
-    g_free(s->stats_vq_elem);
-    s->stats_vq_elem = NULL;
+    balloon_stats_push_elem(s);
 }
 
 static void balloon_stats_get_all(Object *obj, Visitor *v, const char *name,
@@ -411,6 +417,15 @@ static int virtio_balloon_load_device(VirtIODevice *vdev, QEMUFile *f,
     return 0;
 }
 
+static void balloon_vm_state_change(void *opaque, int running, RunState state)
+{
+    VirtIOBalloon *s = opaque;
+
+    if (!running && s->stats_vq_elem) {
+        balloon_stats_push_elem(s);
+    }
+}
+
 static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
@@ -433,6 +448,7 @@ static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
     s->dvq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
     s->svq = virtio_add_queue(vdev, 1, virtio_balloon_receive_stats);
 
+    s->change = qemu_add_vm_change_state_handler(balloon_vm_state_change, s);
     reset_stats(s);
 }
 
@@ -441,6 +457,7 @@ static void virtio_balloon_device_unrealize(DeviceState *dev, Error **errp)
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtIOBalloon *s = VIRTIO_BALLOON(dev);
 
+    qemu_del_vm_change_state_handler(s->change);
     balloon_stats_destroy_timer(s);
     qemu_remove_balloon_handler(s);
     virtio_cleanup(vdev);
include/hw/virtio/virtio-balloon.h +1 -1
index 1ea13bd..d72ff7f 100644
@@ -43,6 +43,7 @@ typedef struct VirtIOBalloon {
     int64_t stats_last_update;
     int64_t stats_poll_interval;
     uint32_t host_features;
+    VMChangeStateEntry *change;
 } VirtIOBalloon;
 
 #endif
--

2.7.4

#11 lore raw

Re: [Qemu-devel] [PATCH 4/4] virtio-balloon: keep collecting stats on save/restore

Quoted 25 lines · Roman Kagan
Quoted 9 lines · Ladi Prosek
Quoted 8 lines · Roman Kagan

Upon save/restore virtio-balloon stats acquisition stops. The reason is that the fact that the (only) virtqueue element is being used by QEMU is not recorded anywhere on save, so upon restore it's not released to the guest, making further progress impossible.

Saving the information about the used element would introduce unjustified vmstate incompatibility.

Quoted 7 lines
Quoted 4 lines

So instead just make sure the element is pushed before save, leaving the ball on the guest side. For that, add vm state change handler to virtio-ballon which would take care of pushing the element if there is one.

Please take a look at:
https://lists.gnu.org/archive/html/qemu-devel/2016-08/msg01038.html

Thanks for the pointer, I wish I did a better maillist search before submitting.

Quoted 3 lines

virtqueue_discard looks like a better choice than virtqueue_push. There's no need to cause churn on the guest side and receive an extra set of stats in between the regular timer callbacks.

Quoted 6 lines

I forgot about virtqueue_discard; however, I still tend to slightly prefer the push version as it IMO makes things easier to follow: the pumping of stats is always initiated by the guest (both initially on guest driver load and upon restore), and ->stats_vq_elem being non-NULL is enough to know that it needs pushing (both in the timer callback and in vm state change handler).

I think I agree, it is nicer this way. virtqueue_discard would add another state of the system (element needs to be popped without first getting notified by the guest) which is not necessary. Thanks!

Quoted 100 lines
Quoted 2 lines

Also in that thread see the note about stats_vq_offset which is misused and can be killed.

Indeed.

Thanks,
Roman.

Quoted 92 lines
Quoted 92 lines
Cc "Michael S. Tsirkin" <[email protected]>
---
 hw/virtio/virtio-balloon.c         | 27 ++++++++++++++++++++++-----
 include/hw/virtio/virtio-balloon.h |  1 +
 2 files changed, 23 insertions(+), 5 deletions(-)
hw/virtio/virtio-balloon.c +22 -5
index b56fecd..66a926a 100644
@@ -88,10 +88,19 @@ static void balloon_stats_change_timer(VirtIOBalloon *s, int64_t secs)
     timer_mod(s->stats_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + secs * 1000);
 }
 
+static void balloon_stats_push_elem(VirtIOBalloon *s)
+{
+    VirtIODevice *vdev = VIRTIO_DEVICE(s);
+
+    virtqueue_push(s->svq, s->stats_vq_elem, s->stats_vq_offset);
+    virtio_notify(vdev, s->svq);
+    g_free(s->stats_vq_elem);
+    s->stats_vq_elem = NULL;
+}
+
 static void balloon_stats_poll_cb(void *opaque)
 {
     VirtIOBalloon *s = opaque;
-    VirtIODevice *vdev = VIRTIO_DEVICE(s);
 
     if (!s->stats_vq_elem) {
         /* The guest hasn't sent the stats yet (either not enabled or we came
@@ -100,10 +109,7 @@ static void balloon_stats_poll_cb(void *opaque)
         return;
     }
 
-    virtqueue_push(s->svq, s->stats_vq_elem, s->stats_vq_offset);
-    virtio_notify(vdev, s->svq);
-    g_free(s->stats_vq_elem);
-    s->stats_vq_elem = NULL;
+    balloon_stats_push_elem(s);
 }
 
 static void balloon_stats_get_all(Object *obj, Visitor *v, const char *name,
@@ -411,6 +417,15 @@ static int virtio_balloon_load_device(VirtIODevice *vdev, QEMUFile *f,
     return 0;
 }
 
+static void balloon_vm_state_change(void *opaque, int running, RunState state)
+{
+    VirtIOBalloon *s = opaque;
+
+    if (!running && s->stats_vq_elem) {
+        balloon_stats_push_elem(s);
+    }
+}
+
 static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
@@ -433,6 +448,7 @@ static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
     s->dvq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
     s->svq = virtio_add_queue(vdev, 1, virtio_balloon_receive_stats);
 
+    s->change = qemu_add_vm_change_state_handler(balloon_vm_state_change, s);
     reset_stats(s);
 }
 
@@ -441,6 +457,7 @@ static void virtio_balloon_device_unrealize(DeviceState *dev, Error **errp)
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtIOBalloon *s = VIRTIO_BALLOON(dev);
 
+    qemu_del_vm_change_state_handler(s->change);
     balloon_stats_destroy_timer(s);
     qemu_remove_balloon_handler(s);
     virtio_cleanup(vdev);
include/hw/virtio/virtio-balloon.h +1 -1
index 1ea13bd..d72ff7f 100644
@@ -43,6 +43,7 @@ typedef struct VirtIOBalloon {
     int64_t stats_last_update;
     int64_t stats_poll_interval;
     uint32_t host_features;
+    VMChangeStateEntry *change;
 } VirtIOBalloon;
 
 #endif
--

2.7.4

#12 lore raw

Re: [Qemu-devel] [PATCH 4/4] virtio-balloon: keep collecting stats on save/restore

Quoted 33 lines · Ladi Prosek
Quoted 25 lines · Roman Kagan
Quoted 9 lines · Ladi Prosek
Quoted 8 lines · Roman Kagan

Upon save/restore virtio-balloon stats acquisition stops. The reason is that the fact that the (only) virtqueue element is being used by QEMU is not recorded anywhere on save, so upon restore it's not released to the guest, making further progress impossible.

Saving the information about the used element would introduce unjustified vmstate incompatibility.

Quoted 7 lines
Quoted 4 lines

So instead just make sure the element is pushed before save, leaving the ball on the guest side. For that, add vm state change handler to virtio-ballon which would take care of pushing the element if there is one.

Please take a look at:
https://lists.gnu.org/archive/html/qemu-devel/2016-08/msg01038.html

Thanks for the pointer, I wish I did a better maillist search before submitting.

Quoted 3 lines

virtqueue_discard looks like a better choice than virtqueue_push. There's no need to cause churn on the guest side and receive an extra set of stats in between the regular timer callbacks.

Quoted 6 lines

I forgot about virtqueue_discard; however, I still tend to slightly prefer the push version as it IMO makes things easier to follow: the pumping of stats is always initiated by the guest (both initially on guest driver load and upon restore), and ->stats_vq_elem being non-NULL is enough to know that it needs pushing (both in the timer callback and in vm state change handler).

Quoted 3 lines

I think I agree, it is nicer this way. virtqueue_discard would add another state of the system (element needs to be popped without first getting notified by the guest) which is not necessary. Thanks!

On a second thought (induced by Stefan's comment), my patch doesn't fix the problem for loading the state saved by unfixed QEMU, while yours does.

So please disregrard this series, and let's move on with yours.

Thanks,
Roman.

#13 lore raw

Re: [Qemu-devel] [PATCH 4/4] virtio-balloon: keep collecting stats on save/restore

Quoted 34 lines · Roman Kagan
Quoted 33 lines · Ladi Prosek
Quoted 31 lines · Roman Kagan
Quoted 9 lines · Ladi Prosek
Quoted 8 lines · Roman Kagan

Upon save/restore virtio-balloon stats acquisition stops. The reason is that the fact that the (only) virtqueue element is being used by QEMU is not recorded anywhere on save, so upon restore it's not released to the guest, making further progress impossible.

Saving the information about the used element would introduce unjustified vmstate incompatibility.

Quoted 7 lines
Quoted 4 lines

So instead just make sure the element is pushed before save, leaving the ball on the guest side. For that, add vm state change handler to virtio-ballon which would take care of pushing the element if there is one.

Please take a look at:
https://lists.gnu.org/archive/html/qemu-devel/2016-08/msg01038.html

Thanks for the pointer, I wish I did a better maillist search before submitting.

Quoted 3 lines

virtqueue_discard looks like a better choice than virtqueue_push. There's no need to cause churn on the guest side and receive an extra set of stats in between the regular timer callbacks.

I forgot about virtqueue_discard; however, I still tend to slightly prefer the push version as it IMO makes things easier to follow: the pumping of stats is always initiated by the guest (both initially on guest driver load and upon restore), and ->stats_vq_elem being non-NULL is enough to know that it needs pushing (both in the timer callback and in vm state change handler).

Quoted 7 lines
Quoted 3 lines

I think I agree, it is nicer this way. virtqueue_discard would add another state of the system (element needs to be popped without first getting notified by the guest) which is not necessary. Thanks!

On a second thought (induced by Stefan's comment), my patch doesn't fix the problem for loading the state saved by unfixed QEMU, while yours does.

Interesting - I don't think that either patch fixes the issue when only the loading side is fixed. But your patch handles the opposite case where only the saving side is fixed, while mine doesn't. With your patch the state that gets saved is a valid state today (the timing would have to be right but it is possible to get it with today's QEMU). With my patch I'm adding a new state that the loading QEMU must understand, kind of what I alluded to in the previous email.

I hope I'm not missing something. This is where a whiteboard would be handy :)

Quoted 4 lines

So please disregrard this series, and let's move on with yours.

Thanks,
Roman.

#14 lore raw
In reply to Roman Kagan
Quoted 14 lines · Roman Kagan

This patchset addresses a few problems discovered when analyzing aborts of (an older version of) QEMU with backported commit afd9096eb1882f23929f5b5c177898ed231bac66 "virtio: error out if guest exceeds virtqueue size". Those problems are present in master, too, except that they don't trigger an abort and are thus not as easy to notice.

Roman Kagan (4):
virtio: assert on ->inuse underflow
virtio-balloon: make stats virtqueue length 1
virtio-balloon: don't restart stats timer in callback
virtio-balloon: keep collecting stats on save/restore

Quoted 6 lines
Cc "Michael S. Tsirkin" <[email protected]>
 hw/virtio/virtio-balloon.c         | 49 +++++++++++++++++++++-----------------
 hw/virtio/virtio.c                 |  3 ++-
 include/hw/virtio/virtio-balloon.h |  1 +
 3 files changed, 30 insertions(+), 23 deletions(-)

Please see these related patches on the mailing list:

1. [PATCH v3] balloon: Fix failure of updating guest memory status

This addresses the virtio-balloon stats virtqueue hang after migration.

2. [PATCH 0/2] virtio: fix VirtQueue->inuse field

Fixes to update virtio.c in code paths where it currently becomes inconsistent.