virtio_balloon: don't push uninitialized buffers to stats virtqueue
Subsystems (2)
PATCH 1/1 virtio_balloon: don't push uninitialized buffers to stats virtqueue
When init_vqs runs, virtio_balloon.stats is either uninitialized or contains stale values. The host updates its state with garbage data because it has no way of knowing that this is just a marker buffer used for signaling.
This patch initializes all tags with U16_MAX which is guaranteed to be ignored by the host.
The alternative fix would be to push an empty buffer in init_vqs but that's not easily done with the current virtio implementation and would still not eliminate the invariant that update_balloon_stats has to update all VIRTIO_BALLOON_S_NR fields.
--- drivers/virtio/virtio_balloon.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/virtio/virtio_balloon.c +4 -0
@@ -423,12 +423,16 @@ static int init_vqs(struct virtio_balloon *vb) vb->deflate_vq = vqs[1 ];if (virtio_has_feature (vb->vdev,VIRTIO_BALLOON_F_STATS_VQ )) {struct scatterlist sg; +int idx; vb->stats_vq = vqs[2 ];/* * Prime this virtqueue with one buffer so the hypervisor can * use it to signal us later (it can't be broken yet!). */ +for (idx =0 ; idx <VIRTIO_BALLOON_S_NR ; idx++) +update_stat (vb, idx,U16_MAX ,0 ); +sg_init_one (&sg, vb->stats,sizeof vb->stats);if (virtqueue_add_outbuf (vb->stats_vq, &sg,1 , vb,GFP_KERNEL ) <0 )
Signature · 1 line
2.7.4
Michael S. Tsirkin
maintainerQuoted 26 lines · Ladi Prosek
When
init_vqsruns, virtio_balloon.stats is either uninitialized or contains stale values. The host updates its state with garbage data because it has no way of knowing that this is just a marker buffer used for signaling.This patch initializes all tags with
U16_MAXwhich is guaranteed to be ignored by the host.The alternative fix would be to push an empty buffer in
init_vqsbut that's not easily done with the current virtio implementation and would still not eliminate the invariant thatupdate_balloon_statshas to update allVIRTIO_BALLOON_S_NRfields.Signed-off-by Ladi Prosek <[email protected]>--- drivers/virtio/virtio_balloon.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/virtio/virtio_balloon.c +0
-0@@ -423,12 +423,16 @@ static int init_vqs(struct virtio_balloon *vb) vb->deflate_vq = vqs[1 ];if (virtio_has_feature (vb->vdev,VIRTIO_BALLOON_F_STATS_VQ )) {struct scatterlist sg;
drivers/virtio/virtio_balloon.c static int init_vqs(struct virtio_balloon *vb) · line 435
Quoted 10 lines
drivers/virtio/virtio_balloon.c +4
-0+int idx; vb->stats_vq = vqs[2 ];/* * Prime this virtqueue with one buffer so the hypervisor can * use it to signal us later (it can't be broken yet!). */ +for (idx =0 ; idx <VIRTIO_BALLOON_S_NR ; idx++) +update_stat (vb, idx,U16_MAX ,0 ); +
Can't we fill in actual values?
Quoted 5 lines
drivers/virtio/virtio_balloon.c +0
-0sg_init_one (&sg, vb->stats,sizeof vb->stats);if (virtqueue_add_outbuf (vb->stats_vq, &sg,1 , vb,GFP_KERNEL ) <0 )Signature · 1 line
2.7.4
Quoted 30 lines · Michael S. Tsirkin
Quoted 29 lines · Ladi Prosek
When
init_vqsruns, virtio_balloon.stats is either uninitialized or contains stale values. The host updates its state with garbage data because it has no way of knowing that this is just a marker buffer used for signaling.This patch initializes all tags with
U16_MAXwhich is guaranteed to be ignored by the host.The alternative fix would be to push an empty buffer in
init_vqsbut that's not easily done with the current virtio implementation and would still not eliminate the invariant thatupdate_balloon_statshas to update allVIRTIO_BALLOON_S_NRfields.Signed-off-by Ladi Prosek <[email protected]>--- drivers/virtio/virtio_balloon.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/virtio/virtio_balloon.c +1
-0@@ -423,12 +423,16 @@ static int init_vqs(struct virtio_balloon *vb) vb->deflate_vq = vqs[1 ];if (virtio_has_feature (vb->vdev,VIRTIO_BALLOON_F_STATS_VQ )) {struct scatterlist sg; +int idx; vb->stats_vq = vqs[2 ];
Quoted 9 lines
drivers/virtio/virtio_balloon.c static int init_vqs(struct virtio_balloon *vb) · line 435
Quoted 7 lines
drivers/virtio/virtio_balloon.c +3
-0/* * Prime this virtqueue with one buffer so the hypervisor can * use it to signal us later (it can't be broken yet!). */ +for (idx =0 ; idx <VIRTIO_BALLOON_S_NR ; idx++) +update_stat (vb, idx,U16_MAX ,0 ); +Can't we fill in actual values?
We can if we're fine with the unsolicited update.
In QEMU, this would mean that you'll see valid stats in /machine/peripheral/balloon0/guest-stats without setting the guest-stats-polling-interval. And they would be updated on driver (re-)load, so kind of arbitrarily from host pov.
On the other hand, this patch does not really comply with (5.5.6.3.1 Driver Requirements: Memory Statistics):
"Driver MUST supply the same subset of statistics in all buffers submitted to the statsq."
So going by the letter of the law, we should fill in actual values. Going by the (the purpose of the initial buffer is to wait for the host to request stats), we should push an empty buffer or invalid values.
Quoted 5 lines
Quoted 5 lines
drivers/virtio/virtio_balloon.c +0
-1sg_init_one (&sg, vb->stats,sizeof vb->stats);if (virtqueue_add_outbuf (vb->stats_vq, &sg,1 , vb,GFP_KERNEL ) <0 ) --2.7.4
Michael S. Tsirkin
maintainerQuoted 41 lines · Ladi Prosek
Quoted 30 lines · Michael S. Tsirkin
Quoted 29 lines · Ladi Prosek
When
init_vqsruns, virtio_balloon.stats is either uninitialized or contains stale values. The host updates its state with garbage data because it has no way of knowing that this is just a marker buffer used for signaling.This patch initializes all tags with
U16_MAXwhich is guaranteed to be ignored by the host.The alternative fix would be to push an empty buffer in
init_vqsbut that's not easily done with the current virtio implementation and would still not eliminate the invariant thatupdate_balloon_statshas to update allVIRTIO_BALLOON_S_NRfields.Signed-off-by Ladi Prosek <[email protected]>--- drivers/virtio/virtio_balloon.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/virtio/virtio_balloon.c +1
-0@@ -423,12 +423,16 @@ static int init_vqs(struct virtio_balloon *vb) vb->deflate_vq = vqs[1 ];if (virtio_has_feature (vb->vdev,VIRTIO_BALLOON_F_STATS_VQ )) {struct scatterlist sg; +int idx; vb->stats_vq = vqs[2 ];Quoted 9 lines
drivers/virtio/virtio_balloon.c static int init_vqs(struct virtio_balloon *vb) · line 435
Quoted 7 lines
drivers/virtio/virtio_balloon.c +3
-0/* * Prime this virtqueue with one buffer so the hypervisor can * use it to signal us later (it can't be broken yet!). */ +for (idx =0 ; idx <VIRTIO_BALLOON_S_NR ; idx++) +update_stat (vb, idx,U16_MAX ,0 ); +Can't we fill in actual values?
Quoted 6 lines
We can if we're fine with the unsolicited update.
In QEMU, this would mean that you'll see valid stats in /machine/peripheral/balloon0/guest-stats without setting the guest-stats-polling-interval. And they would be updated on driver (re-)load, so kind of arbitrarily from host pov.
IUC the only issue would be that someone might come to depend on this. Doesn't look like a big deal to me.
Quoted 15 lines
On the other hand, this patch does not really comply with (5.5.6.3.1 Driver Requirements: Memory Statistics):
"Driver MUST supply the same subset of statistics in all buffers submitted to the statsq."
So going by the letter of the law, we should fill in actual values. Going by the (the purpose of the initial buffer is to wait for the host to request stats), we should push an empty buffer or invalid values.
Quoted 5 lines
Quoted 5 lines
drivers/virtio/virtio_balloon.c +0
-1sg_init_one (&sg, vb->stats,sizeof vb->stats);if (virtqueue_add_outbuf (vb->stats_vq, &sg,1 , vb,GFP_KERNEL ) <0 ) --2.7.4
Quoted 44 lines · Michael S. Tsirkin
Quoted 43 lines · Ladi Prosek
Quoted 30 lines · Michael S. Tsirkin
Quoted 29 lines · Ladi Prosek
When
init_vqsruns, virtio_balloon.stats is either uninitialized or contains stale values. The host updates its state with garbage data because it has no way of knowing that this is just a marker buffer used for signaling.This patch initializes all tags with
U16_MAXwhich is guaranteed to be ignored by the host.The alternative fix would be to push an empty buffer in
init_vqsbut that's not easily done with the current virtio implementation and would still not eliminate the invariant thatupdate_balloon_statshas to update allVIRTIO_BALLOON_S_NRfields.Signed-off-by Ladi Prosek <[email protected]>--- drivers/virtio/virtio_balloon.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/virtio/virtio_balloon.c +1
-0@@ -423,12 +423,16 @@ static int init_vqs(struct virtio_balloon *vb) vb->deflate_vq = vqs[1 ];if (virtio_has_feature (vb->vdev,VIRTIO_BALLOON_F_STATS_VQ )) {struct scatterlist sg; +int idx; vb->stats_vq = vqs[2 ];Quoted 9 lines
drivers/virtio/virtio_balloon.c static int init_vqs(struct virtio_balloon *vb) · line 435
Quoted 7 lines
drivers/virtio/virtio_balloon.c +3
-0/* * Prime this virtqueue with one buffer so the hypervisor can * use it to signal us later (it can't be broken yet!). */ +for (idx =0 ; idx <VIRTIO_BALLOON_S_NR ; idx++) +update_stat (vb, idx,U16_MAX ,0 ); +Can't we fill in actual values?
We can if we're fine with the unsolicited update.
Quoted 7 lines
Quoted 4 lines
In QEMU, this would mean that you'll see valid stats in /machine/peripheral/balloon0/guest-stats without setting the guest-stats-polling-interval. And they would be updated on driver (re-)load, so kind of arbitrarily from host pov.
IUC the only issue would be that someone might come to depend on this. Doesn't look like a big deal to me.
Ok, I'll post v2 of the patch to use actual values when sending the initial buffer.
Btw, I checked the Windows driver and it's been sending all FF's in the first stats buffer since about 2011. Just for completeness, probably won't change your mind :)
Thanks!
Ladi
Quoted 15 lines
Quoted 15 lines
On the other hand, this patch does not really comply with (5.5.6.3.1 Driver Requirements: Memory Statistics):
"Driver MUST supply the same subset of statistics in all buffers submitted to the statsq."
So going by the letter of the law, we should fill in actual values. Going by the (the purpose of the initial buffer is to wait for the host to request stats), we should push an empty buffer or invalid values.
Quoted 5 lines
Quoted 5 lines
drivers/virtio/virtio_balloon.c +0
-1sg_init_one (&sg, vb->stats,sizeof vb->stats);if (virtqueue_add_outbuf (vb->stats_vq, &sg,1 , vb,GFP_KERNEL ) <0 ) --2.7.4