virtio-balloon: assorted fixes
v2 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 (6):
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
virtio-balloon: drop ->stats_vq_offset
virtio-balloon: drop reset handler
---
v1 -> v2:
- added assert in patch 2
- new patches 5, 6
hw/virtio/virtio-balloon.c | 62 ++++++++++++++++++-------------------- hw/virtio/virtio.c | 3 +- include/hw/virtio/virtio-balloon.h | 2 +- 3 files changed, 32 insertions(+), 35 deletions(-)
Signature · 1 line
2.7.4
PATCH v2 1/6 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].
--- hw/virtio/virtio.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
hw/virtio/virtio.c +2 -1
@@ -92,7 +92,7 @@ struct VirtQueueuint16_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
Re: [Qemu-devel] [PATCH v2 1/6] virtio: assert on ->inuse underflow
Quoted 8 lines · Roman Kagan
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 wherevirtqueue_push()was called before the matchingvirtqueu_pop(). [That problem will be addressed in followup patches].
Quoted 7 lines
Signed-off-by Roman Kagan <[email protected]>Cc "Michael S. Tsirkin" <[email protected]>Cc Ladi Prosek <[email protected]>Cc Stefan Hajnoczi <[email protected]>--- hw/virtio/virtio.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
Re: [Qemu-devel] [PATCH v2 1/6] virtio: assert on ->inuse underflow
Quoted 17 lines · Stefan Hajnoczi
Quoted 8 lines · Roman Kagan
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 wherevirtqueue_push()was called before the matchingvirtqueu_pop(). [That problem will be addressed in followup patches].Quoted 7 lines
Signed-off-by Roman Kagan <[email protected]>Cc "Michael S. Tsirkin" <[email protected]>Cc Ladi Prosek <[email protected]>Cc Stefan Hajnoczi <[email protected]>--- hw/virtio/virtio.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 1 line
Reviewed-by Stefan Hajnoczi <[email protected]>
I'm not merging any asserts before 2.7. Please resubmit when 2.7 is out.
PATCH v2 2/6 virtio-balloon: make stats virtqueue length 1
In reply to Roman KaganThe 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.
---
v1 > v2:
- added assert in virtio_balloon_receive_stats
hw/virtio/virtio-balloon.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)
hw/virtio/virtio-balloon.c +3 -7
@@ -262,12 +262,8 @@ 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); - } +/* enforced by stats virtqueue depth being 1 */ +assert (!s->stats_vq_elem); s->stats_vq_elem = elem;
@@ -443,7 +439,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
Re: [Qemu-devel] [PATCH v2 2/6] virtio-balloon: make stats virtqueue length 1
Quoted 8 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.
Quoted 4 lines
Signed-off-by Roman Kagan <[email protected]>Cc "Michael S. Tsirkin" <[email protected]>Cc Ladi Prosek <[email protected]>Cc Stefan Hajnoczi <[email protected]>
This would need a bunch of compat handlers to
avoid changes for old machine types.
Quoted 37 lines
---
v1 > v2:
- added assert invirtio_balloon_receive_statshw/virtio/virtio-balloon.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)hw/virtio/virtio-balloon.c +3
-7@@ -262,12 +262,8 @@ 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); - } +/* enforced by stats virtqueue depth being 1 */ +assert (!s->stats_vq_elem); s->stats_vq_elem = elem;@@ -443,7 +439,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
Quoted 6 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.
Please disregard; there have been better fixes posted on the list.
Roman.
PATCH v2 3/6 virtio-balloon: don't restart stats timer in callback
In reply to Roman KaganThere'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.
--- hw/virtio/virtio-balloon.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-)
hw/virtio/virtio-balloon.c +4 -9
@@ -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
PATCH v2 4/6 virtio-balloon: keep collecting stats on save/restore
In reply to Roman KaganUpon 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.
--- 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
@@ -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,
@@ -414,6 +420,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);
@@ -436,6 +451,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); }
@@ -444,6 +460,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
@@ -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
Re: [Qemu-devel] [PATCH v2 4/6] virtio-balloon: keep collecting stats on save/restore
Quoted 104 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.
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.
Signed-off-by Roman Kagan <[email protected]>Cc "Michael S. Tsirkin" <[email protected]>Cc Ladi Prosek <[email protected]>Cc Stefan Hajnoczi <[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@@ -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,@@ -414,6 +420,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);@@ -436,6 +451,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); }@@ -444,6 +460,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@@ -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 --
Quoted 2 lines
2.7.4
Hi Roman,
I talked to Michael Tsirkin and he agrees with merging this patch for 2.7. Could you please resubmit and use the set_status callback instead of adding another VM state change handler?
static void virtio_balloon_set_status(VirtIODevice *vdev, uint8_t status)
{
VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
if (!vdev->vm_running && s->stats_vq_elem) {
balloon_stats_push_elem(s);
}
}and
vdc->set_status = virtio_balloon_set_status;
in virtio_balloon_class_init.
This is somewhat urgent because 2.7 will be out soon. If you're busy or if I don't hear from you I'll post it on your behalf.
Thanks!
Ladi
Re: [Qemu-devel] [PATCH v2 4/6] virtio-balloon: keep collecting stats on save/restore
Quoted 110 lines · Ladi Prosek
Quoted 104 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.
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.
Signed-off-by Roman Kagan <[email protected]>Cc "Michael S. Tsirkin" <[email protected]>Cc Ladi Prosek <[email protected]>Cc Stefan Hajnoczi <[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@@ -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,@@ -414,6 +420,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);@@ -436,6 +451,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); }@@ -444,6 +460,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@@ -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 --Quoted 2 lines
2.7.4
Hi Roman,
Quoted 2 lines
I talked to Michael Tsirkin and he agrees with merging this patch for 2.7.
I'm not happy with this patch: it tries to solve the problem on the "save" side and therefore doesn't fix the bug when migrating from an earlier QEMU version.
I wonder if we can do better and solve it on the "load" side. (At first I thought that your patch did that but on a closer look it turned out not the case).
In particular, with Stefan's patch to restore VirtQueue->inuse, we should be able to just rewind ->last_avail_idx by ->inuse during "load", which AFAICS would also fix the bug. What do you think?
Quoted 12 lines
Could you please resubmit and use the
set_statuscallback instead of adding another VM state change handler?static void
virtio_balloon_set_status(VirtIODevice *vdev, uint8_t status){ VirtIOBalloon *s = VIRTIO_BALLOON(vdev); if (!vdev->vm_running && s->stats_vq_elem) { balloon_stats_push_elem(s); } }
Quoted 4 lines
and
vdc->set_status = virtio_balloon_set_status;in
virtio_balloon_class_init.
If the scheme I described above works out this won't be needed at all.
Quoted 2 lines
This is somewhat urgent because 2.7 will be out soon. If you're busy or if I don't hear from you I'll post it on your behalf.
OK will focus on it now.
Thanks,
Roman.
Re: [Qemu-devel] [PATCH v2 4/6] virtio-balloon: keep collecting stats on save/restore
Quoted 118 lines · Roman Kagan
Quoted 110 lines · Ladi Prosek
Quoted 104 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.
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.
Signed-off-by Roman Kagan <[email protected]>Cc "Michael S. Tsirkin" <[email protected]>Cc Ladi Prosek <[email protected]>Cc Stefan Hajnoczi <[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@@ -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,@@ -414,6 +420,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);@@ -436,6 +451,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); }@@ -444,6 +460,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@@ -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 --Quoted 2 lines
2.7.4
Hi Roman,
Quoted 2 lines
I talked to Michael Tsirkin and he agrees with merging this patch for 2.7.
I'm not happy with this patch: it tries to solve the problem on the "save" side and therefore doesn't fix the bug when migrating from an earlier QEMU version.
Quoted 7 lines
I wonder if we can do better and solve it on the "load" side. (At first I thought that your patch did that but on a closer look it turned out not the case).
In particular, with Stefan's patch to restore
VirtQueue->inuse, we should be able to just rewind ->last_avail_idx by ->inuse during "load", which AFAICS would also fix the bug. What do you think?
Stefan was actually going to do exactly that in
https://lists.gnu.org/archive/html/qemu-devel/2016-08/msg02455.html
but then dropped the patch in favor of this "save" side approach. Going with Stefan's fix seems kind of unfortunate from a future-proof perspective. It wouldn't be easy to revert it if we ever decide that we want to leave something in the queue. But I understand that solving the problem on the "load" side would be more helpful for real-world deployments. Maybe we could do both but gate the load side hack with a source QEMU version check? I wonder what Stefan thinks.
Quoted 26 lines
Quoted 12 lines
Could you please resubmit and use the
set_statuscallback instead of adding another VM state change handler?static void
virtio_balloon_set_status(VirtIODevice *vdev, uint8_t status){ VirtIOBalloon *s = VIRTIO_BALLOON(vdev); if (!vdev->vm_running && s->stats_vq_elem) { balloon_stats_push_elem(s); } }Quoted 4 lines
and
vdc->set_status = virtio_balloon_set_status;in
virtio_balloon_class_init.If the scheme I described above works out this won't be needed at all.
Quoted 2 lines
This is somewhat urgent because 2.7 will be out soon. If you're busy or if I don't hear from you I'll post it on your behalf.
OK will focus on it now.
Thanks,
Roman.
Re: [Qemu-devel] [PATCH v2 4/6] virtio-balloon: keep collecting stats on save/restore
Quoted 131 lines · Ladi Prosek
Quoted 118 lines · Roman Kagan
Quoted 110 lines · Ladi Prosek
Quoted 104 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.
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.
Signed-off-by Roman Kagan <[email protected]>Cc "Michael S. Tsirkin" <[email protected]>Cc Ladi Prosek <[email protected]>Cc Stefan Hajnoczi <[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@@ -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,@@ -414,6 +420,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);@@ -436,6 +451,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); }@@ -444,6 +460,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@@ -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 --Quoted 2 lines
2.7.4
Hi Roman,
Quoted 2 lines
I talked to Michael Tsirkin and he agrees with merging this patch for 2.7.
I'm not happy with this patch: it tries to solve the problem on the "save" side and therefore doesn't fix the bug when migrating from an earlier QEMU version.
Quoted 7 lines
I wonder if we can do better and solve it on the "load" side. (At first I thought that your patch did that but on a closer look it turned out not the case).
In particular, with Stefan's patch to restore
VirtQueue->inuse, we should be able to just rewind ->last_avail_idx by ->inuse during "load", which AFAICS would also fix the bug. What do you think?Stefan was actually going to do exactly that in
https://lists.gnu.org/archive/html/qemu-devel/2016-08/msg02455.html
Quoted 7 lines
but then dropped the patch in favor of this "save" side approach. Going with Stefan's fix seems kind of unfortunate from a future-proof perspective. It wouldn't be easy to revert it if we ever decide that we want to leave something in the queue. But I understand that solving the problem on the "load" side would be more helpful for real-world deployments. Maybe we could do both but gate the load side hack with a source QEMU version check? I wonder what Stefan thinks.
It's a valid point. Try to resubmit Stefan's patches and we'll discuss?
Quoted 26 lines
Quoted 26 lines
Quoted 12 lines
Could you please resubmit and use the
set_statuscallback instead of adding another VM state change handler?static void
virtio_balloon_set_status(VirtIODevice *vdev, uint8_t status){ VirtIOBalloon *s = VIRTIO_BALLOON(vdev); if (!vdev->vm_running && s->stats_vq_elem) { balloon_stats_push_elem(s); } }Quoted 4 lines
and
vdc->set_status = virtio_balloon_set_status;in
virtio_balloon_class_init.If the scheme I described above works out this won't be needed at all.
Quoted 2 lines
This is somewhat urgent because 2.7 will be out soon. If you're busy or if I don't hear from you I'll post it on your behalf.
OK will focus on it now.
Thanks,
Roman.
Re: [Qemu-devel] [PATCH v2 4/6] virtio-balloon: keep collecting stats on save/restore
In reply to Ladi ProsekQuoted 9 lines · Ladi Prosek
Quoted 8 lines · Roman Kagan
I'm not happy with this patch: it tries to solve the problem on the "save" side and therefore doesn't fix the bug when migrating from an earlier QEMU version.
I wonder if we can do better and solve it on the "load" side. (At first I thought that your patch did that but on a closer look it turned out not the case).
Quoted 7 lines
Quoted 3 lines
In particular, with Stefan's patch to restore
VirtQueue->inuse, we should be able to just rewind ->last_avail_idx by ->inuse during "load", which AFAICS would also fix the bug. What do you think?Stefan was actually going to do exactly that in
https://lists.gnu.org/archive/html/qemu-devel/2016-08/msg02455.html
Hmm, obviously my mail search skills need to be improved :( I already coded a patch before I saw this.
Quoted 7 lines
but then dropped the patch in favor of this "save" side approach. Going with Stefan's fix seems kind of unfortunate from a future-proof perspective. It wouldn't be easy to revert it if we ever decide that we want to leave something in the queue. But I understand that solving the problem on the "load" side would be more helpful for real-world deployments. Maybe we could do both but gate the load side hack with a source QEMU version check? I wonder what Stefan thinks.
I woudn't say it looked like a hack so I wouldn't bother with a version check. Anyway let's look at the code and decide.
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Could you please resubmit and use the
set_statuscallback instead of adding another VM state change handler?static void
virtio_balloon_set_status(VirtIODevice *vdev, uint8_t status){ VirtIOBalloon *s = VIRTIO_BALLOON(vdev); if (!vdev->vm_running && s->stats_vq_elem) { balloon_stats_push_elem(s); } }
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
and
vdc->set_status = virtio_balloon_set_status;in
virtio_balloon_class_init.If the scheme I described above works out this won't be needed at all.
Sure I was wrong here, I used it to kick the stats collection on load.
I'll post the patch in a minute.
Roman.
PATCH v2 5/6 virtio-balloon: drop ->stats_vq_offset
In reply to Roman KaganAs we don't write anything to guest on stats virtqueue we should pass len=0 to virtqueue_push(). This makes ->stats_vq_offset unnecessary, so drop it.
--- hw/virtio/virtio-balloon.c | 3 +-- include/hw/virtio/virtio-balloon.h | 1 - 2 files changed, 1 insertion(+), 3 deletions(-)
hw/virtio/virtio-balloon.c +1 -2
@@ -92,7 +92,7 @@ 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); +virtqueue_push (s->svq, s->stats_vq_elem,0 );virtio_notify (vdev, s->svq);g_free (s->stats_vq_elem); s->stats_vq_elem =NULL ;
@@ -283,7 +283,6 @@ static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)if (tag <VIRTIO_BALLOON_S_NR ) s->stats[tag] = val; } - s->stats_vq_offset = offset;if (qemu_gettimeofday (&tv) <0 ) {fprintf (stderr,"warning: %s: failed to get time of day\n" , __func__);
include/hw/virtio/virtio-balloon.h +0 -1
@@ -38,7 +38,6 @@ typedef struct VirtIOBalloon {uint32_t actual;uint64_t stats[VIRTIO_BALLOON_S_NR ];VirtQueueElement *stats_vq_elem; -size_t stats_vq_offset;QEMUTimer *stats_timer;int64_t stats_last_update;int64_t stats_poll_interval;
Signature · 1 line
2.7.4
PATCH v2 6/6 virtio-ballon: drop reset handler
In reply to Roman KaganSince the release of ->stat_vq_elem is now ensured by the vm state change handler, there's no need in the reset handler doing it.
--- hw/virtio/virtio-balloon.c | 11 ----------- 1 file changed, 11 deletions(-)
hw/virtio/virtio-balloon.c +0 -11
@@ -465,16 +465,6 @@ static void virtio_balloon_device_unrealize(DeviceState *dev, Error **errp)virtio_cleanup (vdev); } -static void virtio_balloon_device_reset (VirtIODevice *vdev) -{ -VirtIOBalloon *s =VIRTIO_BALLOON (vdev); - -if (s->stats_vq_elem !=NULL ) { -g_free (s->stats_vq_elem); - s->stats_vq_elem =NULL ; - } -} -static void virtio_balloon_instance_init (Object *obj) {VirtIOBalloon *s =VIRTIO_BALLOON (obj);
@@ -506,7 +496,6 @@ static void virtio_balloon_class_init(ObjectClass *klass, void *data)set_bit (DEVICE_CATEGORY_MISC , dc->categories); vdc->realize = virtio_balloon_device_realize; vdc->unrealize = virtio_balloon_device_unrealize; - vdc->reset = virtio_balloon_device_reset; vdc->get_config = virtio_balloon_get_config; vdc->set_config = virtio_balloon_set_config; vdc->get_features = virtio_balloon_get_features;
Signature · 1 line
2.7.4
Re: [Qemu-devel] [PATCH v2 6/6] virtio-ballon: drop reset handler
Quoted 2 lines · Roman Kagan
Since the release of ->stat_vq_elem is now ensured by the vm state change handler, there's no need in the reset handler doing it.
Oops, sorry, this is a duplicate of the same message modulo typo in the subject, please ignore.
Roman.
PATCH v2 6/6 virtio-balloon: drop reset handler
In reply to Roman KaganSince the release of ->stat_vq_elem is now ensured by the vm state change handler, there's no need in the reset handler doing it.
--- hw/virtio/virtio-balloon.c | 11 ----------- 1 file changed, 11 deletions(-)
hw/virtio/virtio-balloon.c +0 -11
@@ -465,16 +465,6 @@ static void virtio_balloon_device_unrealize(DeviceState *dev, Error **errp)virtio_cleanup (vdev); } -static void virtio_balloon_device_reset (VirtIODevice *vdev) -{ -VirtIOBalloon *s =VIRTIO_BALLOON (vdev); - -if (s->stats_vq_elem !=NULL ) { -g_free (s->stats_vq_elem); - s->stats_vq_elem =NULL ; - } -} -static void virtio_balloon_instance_init (Object *obj) {VirtIOBalloon *s =VIRTIO_BALLOON (obj);
@@ -506,7 +496,6 @@ static void virtio_balloon_class_init(ObjectClass *klass, void *data)set_bit (DEVICE_CATEGORY_MISC , dc->categories); vdc->realize = virtio_balloon_device_realize; vdc->unrealize = virtio_balloon_device_unrealize; - vdc->reset = virtio_balloon_device_reset; vdc->get_config = virtio_balloon_get_config; vdc->set_config = virtio_balloon_set_config; vdc->get_features = virtio_balloon_get_features;
Signature · 1 line
2.7.4