Skip to contentSkip to conversation

virito-balloon: process all in sgs for free_page_vq

PATCH Superseded Jason Wang 5 participants 17 messages Nov 25, 2021 – Nov 29, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jason Wang

author
#0 lore raw

PATCH 1/2 virito-balloon: process all in sgs for free_page_vq

We only process the first in sg which may lead to the bitmap of the pages belongs to following sgs were not cleared. This may result more pages to be migrated. Fixing this by process all in sgs for free_page_vq.

---
 hw/virtio/virtio-balloon.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
hw/virtio/virtio-balloon.c +5 -2
index c6962fcbfe..17de2558cb 100644
@@ -510,6 +510,7 @@ static bool get_free_page_hints(VirtIOBalloon *dev)
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtQueue *vq = dev->free_page_vq;
     bool ret = true;
+    int i;
 
     while (dev->block_iothread) {
         qemu_cond_wait(&dev->free_page_cond, &dev->free_page_lock);
@@ -544,8 +545,10 @@ static bool get_free_page_hints(VirtIOBalloon *dev)
     }
 
     if (elem->in_num && dev->free_page_hint_status == FREE_PAGE_HINT_S_START) {
-        qemu_guest_free_page_hint(elem->in_sg[0].iov_base,
-                                  elem->in_sg[0].iov_len);
+        for (i = 0; i < elem->in_num; i++) {
+            qemu_guest_free_page_hint(elem->in_sg[i].iov_base,
+                                      elem->in_sg[i].iov_len);
+        }
     }
 
 out:
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH 2/2 virtio-balloon: correct used length

Spec said:

"and len the total of bytes written into the buffer."

For inflateq, deflateq and statsq, we don't process in_sg so the used length should be zero. For free_page_vq, since the pages could be changed in the destination, we should make all pages used for safety.

---
 hw/virtio/virtio-balloon.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
hw/virtio/virtio-balloon.c +5 -3
index 17de2558cb..fb4426ac0c 100644
@@ -231,7 +231,7 @@ static void balloon_stats_poll_cb(void *opaque)
         return;
     }
 
-    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;
@@ -438,7 +438,7 @@ static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
             memory_region_unref(section.mr);
         }
 
-        virtqueue_push(vq, elem, offset);
+        virtqueue_push(vq, elem, 0);
         virtio_notify(vdev, vq);
         g_free(elem);
         virtio_balloon_pbp_free(&pbp);
@@ -510,6 +510,7 @@ static bool get_free_page_hints(VirtIOBalloon *dev)
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtQueue *vq = dev->free_page_vq;
     bool ret = true;
+    size_t used = 0;
     int i;
 
     while (dev->block_iothread) {
@@ -548,11 +549,12 @@ static bool get_free_page_hints(VirtIOBalloon *dev)
         for (i = 0; i < elem->in_num; i++) {
             qemu_guest_free_page_hint(elem->in_sg[i].iov_base,
                                       elem->in_sg[i].iov_len);
+            used += elem->in_sg[i].iov_len;
         }
     }
 
 out:
-    virtqueue_push(vq, elem, 1);
+    virtqueue_push(vq, elem, used);
     g_free(elem);
     return ret;
 }
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH 2/2] virtio-balloon: correct used length

Quoted 7 lines · Jason Wang

Spec said:

"and len the total of bytes written into the buffer."

For inflateq, deflateq and statsq, we don't process in_sg so the used length should be zero. For free_page_vq, since the pages could be changed in the destination, we should make all pages used for safety.

Yea, about that, I know I said it, but I was wrong, sorry.

Spec says this:

\field{len} is particularly useful
for drivers using untrusted buffers: if a driver does not know exactly how much has been written by the device, the driver would have to zero the buffer in advance to ensure no data leakage occurs.

For example, a network driver may hand a received buffer directly to an unprivileged userspace application. If the network device has not overwritten the bytes which were in that buffer, this could leak the contents of freed memory from other processes to the application.

In other words, device must guarantee that used length was written into. Since we don't know that, we really should write 0 there, and the fact we don't is a spec violation.

Quoted 51 lines
---
 hw/virtio/virtio-balloon.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
hw/virtio/virtio-balloon.c +5 -3
index 17de2558cb..fb4426ac0c 100644
@@ -231,7 +231,7 @@ static void balloon_stats_poll_cb(void *opaque)
         return;
     }
 
-    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;
@@ -438,7 +438,7 @@ static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
             memory_region_unref(section.mr);
         }
 
-        virtqueue_push(vq, elem, offset);
+        virtqueue_push(vq, elem, 0);
         virtio_notify(vdev, vq);
         g_free(elem);
         virtio_balloon_pbp_free(&pbp);
@@ -510,6 +510,7 @@ static bool get_free_page_hints(VirtIOBalloon *dev)
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtQueue *vq = dev->free_page_vq;
     bool ret = true;
+    size_t used = 0;
     int i;
 
     while (dev->block_iothread) {
@@ -548,11 +549,12 @@ static bool get_free_page_hints(VirtIOBalloon *dev)
         for (i = 0; i < elem->in_num; i++) {
             qemu_guest_free_page_hint(elem->in_sg[i].iov_base,
                                       elem->in_sg[i].iov_len);
+            used += elem->in_sg[i].iov_len;
         }
     }
 
 out:
-    virtqueue_push(vq, elem, 1);
+    virtqueue_push(vq, elem, used);
     g_free(elem);
     return ret;
 }
Signature · 1 line
2.25.1
author
#3 lore raw

Re: [PATCH 2/2] virtio-balloon: correct used length

Quoted 25 lines · Michael S. Tsirkin
Quoted 7 lines · Jason Wang

Spec said:

"and len the total of bytes written into the buffer."

For inflateq, deflateq and statsq, we don't process in_sg so the used length should be zero. For free_page_vq, since the pages could be changed in the destination, we should make all pages used for safety.

Yea, about that, I know I said it, but I was wrong, sorry.

Spec says this:

\field{len} is particularly useful
for drivers using untrusted buffers: if a driver does not know exactly how much has been written by the device, the driver would have to zero the buffer in advance to ensure no data leakage occurs.

For example, a network driver may hand a received buffer directly to an unprivileged userspace application. If the network device has not overwritten the bytes which were in that buffer, this could leak the contents of freed memory from other processes to the application.

Quoted 3 lines

In other words, device must guarantee that used length was written into. Since we don't know that, we really should write 0 there, and the fact we don't is a spec violation.

The problem is, if we write 0, the driver may assume there's no change on those pages?

Thanks

Quoted 54 lines
Quoted 53 lines
---
 hw/virtio/virtio-balloon.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
hw/virtio/virtio-balloon.c +5 -4
index 17de2558cb..fb4426ac0c 100644
@@ -231,7 +231,7 @@ static void balloon_stats_poll_cb(void *opaque)
         return;
     }
 
-    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;
@@ -438,7 +438,7 @@ static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
             memory_region_unref(section.mr);
         }
 
-        virtqueue_push(vq, elem, offset);
+        virtqueue_push(vq, elem, 0);
         virtio_notify(vdev, vq);
         g_free(elem);
         virtio_balloon_pbp_free(&pbp);
@@ -510,6 +510,7 @@ static bool get_free_page_hints(VirtIOBalloon *dev)
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtQueue *vq = dev->free_page_vq;
     bool ret = true;
+    size_t used = 0;
     int i;
 
     while (dev->block_iothread) {
@@ -548,11 +549,12 @@ static bool get_free_page_hints(VirtIOBalloon *dev)
         for (i = 0; i < elem->in_num; i++) {
             qemu_guest_free_page_hint(elem->in_sg[i].iov_base,
                                       elem->in_sg[i].iov_len);
+            used += elem->in_sg[i].iov_len;
         }
     }
 
 out:
-    virtqueue_push(vq, elem, 1);
+    virtqueue_push(vq, elem, used);
     g_free(elem);
     return ret;
 }
--

2.25.1

#4 lore raw

Re: [PATCH 2/2] virtio-balloon: correct used length

Quoted 26 lines · Jason Wang
Quoted 25 lines · Michael S. Tsirkin
Quoted 7 lines · Jason Wang

Spec said:

"and len the total of bytes written into the buffer."

For inflateq, deflateq and statsq, we don't process in_sg so the used length should be zero. For free_page_vq, since the pages could be changed in the destination, we should make all pages used for safety.

Yea, about that, I know I said it, but I was wrong, sorry.

Spec says this:

\field{len} is particularly useful
for drivers using untrusted buffers: if a driver does not know exactly how much has been written by the device, the driver would have to zero the buffer in advance to ensure no data leakage occurs.

For example, a network driver may hand a received buffer directly to an unprivileged userspace application. If the network device has not overwritten the bytes which were in that buffer, this could leak the contents of freed memory from other processes to the application.

Quoted 8 lines
Quoted 3 lines

In other words, device must guarantee that used length was written into. Since we don't know that, we really should write 0 there, and the fact we don't is a spec violation.

The problem is, if we write 0, the driver may assume there's no change on those pages?

Thanks

No:

The driver MUST NOT make assumptions about data in device-writable buffers beyond the first \field{len} bytes, and SHOULD ignore this data.

Quoted 54 lines
Quoted 54 lines
Quoted 53 lines
---
 hw/virtio/virtio-balloon.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
hw/virtio/virtio-balloon.c +5 -4
index 17de2558cb..fb4426ac0c 100644
@@ -231,7 +231,7 @@ static void balloon_stats_poll_cb(void *opaque)
         return;
     }
 
-    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;
@@ -438,7 +438,7 @@ static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
             memory_region_unref(section.mr);
         }
 
-        virtqueue_push(vq, elem, offset);
+        virtqueue_push(vq, elem, 0);
         virtio_notify(vdev, vq);
         g_free(elem);
         virtio_balloon_pbp_free(&pbp);
@@ -510,6 +510,7 @@ static bool get_free_page_hints(VirtIOBalloon *dev)
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtQueue *vq = dev->free_page_vq;
     bool ret = true;
+    size_t used = 0;
     int i;
 
     while (dev->block_iothread) {
@@ -548,11 +549,12 @@ static bool get_free_page_hints(VirtIOBalloon *dev)
         for (i = 0; i < elem->in_num; i++) {
             qemu_guest_free_page_hint(elem->in_sg[i].iov_base,
                                       elem->in_sg[i].iov_len);
+            used += elem->in_sg[i].iov_len;
         }
     }
 
 out:
-    virtqueue_push(vq, elem, 1);
+    virtqueue_push(vq, elem, used);
     g_free(elem);
     return ret;
 }
--

2.25.1

author
#5 lore raw

Re: [PATCH 2/2] virtio-balloon: correct used length

Quoted 35 lines · Michael S. Tsirkin
Quoted 33 lines · Jason Wang
Quoted 25 lines · Michael S. Tsirkin
Quoted 7 lines · Jason Wang

Spec said:

"and len the total of bytes written into the buffer."

For inflateq, deflateq and statsq, we don't process in_sg so the used length should be zero. For free_page_vq, since the pages could be changed in the destination, we should make all pages used for safety.

Yea, about that, I know I said it, but I was wrong, sorry.

Spec says this:

\field{len} is particularly useful
for drivers using untrusted buffers: if a driver does not know exactly how much has been written by the device, the driver would have to zero the buffer in advance to ensure no data leakage occurs.

For example, a network driver may hand a received buffer directly to an unprivileged userspace application. If the network device has not overwritten the bytes which were in that buffer, this could leak the contents of freed memory from other processes to the application.

Quoted 3 lines

In other words, device must guarantee that used length was written into. Since we don't know that, we really should write 0 there, and the fact we don't is a spec violation.

The problem is, if we write 0, the driver may assume there's no change on those pages?

Quoted 7 lines
Quoted 1 line

Thanks

No:

The driver MUST NOT make assumptions about data in device-writable buffers beyond the first \field{len} bytes, and SHOULD ignore this data.

Good to know this. Will fix it in V2.

Thanks

Quoted 58 lines
Quoted 57 lines
Quoted 57 lines
Quoted 56 lines
---
 hw/virtio/virtio-balloon.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
hw/virtio/virtio-balloon.c +5 -4
index 17de2558cb..fb4426ac0c 100644
@@ -231,7 +231,7 @@ static void balloon_stats_poll_cb(void *opaque)
         return;
     }
 
-    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;
@@ -438,7 +438,7 @@ static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
             memory_region_unref(section.mr);
         }
 
-        virtqueue_push(vq, elem, offset);
+        virtqueue_push(vq, elem, 0);
         virtio_notify(vdev, vq);
         g_free(elem);
         virtio_balloon_pbp_free(&pbp);
@@ -510,6 +510,7 @@ static bool get_free_page_hints(VirtIOBalloon *dev)
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtQueue *vq = dev->free_page_vq;
     bool ret = true;
+    size_t used = 0;
     int i;
 
     while (dev->block_iothread) {
@@ -548,11 +549,12 @@ static bool get_free_page_hints(VirtIOBalloon *dev)
         for (i = 0; i < elem->in_num; i++) {
             qemu_guest_free_page_hint(elem->in_sg[i].iov_base,
                                       elem->in_sg[i].iov_len);
+            used += elem->in_sg[i].iov_len;
         }
     }
 
 out:
-    virtqueue_push(vq, elem, 1);
+    virtqueue_push(vq, elem, used);
     g_free(elem);
     return ret;
 }
--

2.25.1

#6 lore raw
In reply to Jason Wang
Quoted 26 lines · Jason Wang

We only process the first in sg which may lead to the bitmap of the pages belongs to following sgs were not cleared. This may result more pages to be migrated. Fixing this by process all in sgs for free_page_vq.

---
 hw/virtio/virtio-balloon.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
hw/virtio/virtio-balloon.c +1 -0
index c6962fcbfe..17de2558cb 100644
@@ -510,6 +510,7 @@ static bool get_free_page_hints(VirtIOBalloon *dev)
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtQueue *vq = dev->free_page_vq;
     bool ret = true;
+    int i;
 
     while (dev->block_iothread) {
         qemu_cond_wait(&dev->free_page_cond, &dev->free_page_lock);
@@ -544,8 +545,10 @@ static bool get_free_page_hints(VirtIOBalloon *dev)
     }
 

hw/virtio/virtio-balloon.c static bool get_free_page_hints(VirtIOBalloon *dev) · line 554

Quoted 10 lines
hw/virtio/virtio-balloon.c +4 -2
     if (elem->in_num && dev->free_page_hint_status == FREE_PAGE_HINT_S_START) {
-        qemu_guest_free_page_hint(elem->in_sg[0].iov_base,
-                                  elem->in_sg[0].iov_len);
+        for (i = 0; i < elem->in_num; i++) {
+            qemu_guest_free_page_hint(elem->in_sg[i].iov_base,
+                                      elem->in_sg[i].iov_len);
+        }
     }
 
 out:

Yes, but:

1. Linux never used more than one
2. QEMU never consumed more than one

The spec states:

"(b) The driver maps a series of pages and adds them to the free_page_vq as individual scatter-gather input buffer entries."

However, the spec was written by someone else (Alex) as the code was (Wei). The code was there first.

I don't particularly care what to adjust (code or spec). However, to me it feels more like the spec is slightly wrong and it was intended like the code is by the original code author.

But then again, I don't particularly care :)

Signature · 3 lines
Thanks,

David / dhildenb
#7 lore raw
Quoted 48 lines · David Hildenbrand
Quoted 26 lines · Jason Wang

We only process the first in sg which may lead to the bitmap of the pages belongs to following sgs were not cleared. This may result more pages to be migrated. Fixing this by process all in sgs for free_page_vq.

---
 hw/virtio/virtio-balloon.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
hw/virtio/virtio-balloon.c +1 -0
index c6962fcbfe..17de2558cb 100644
@@ -510,6 +510,7 @@ static bool get_free_page_hints(VirtIOBalloon *dev)
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtQueue *vq = dev->free_page_vq;
     bool ret = true;
+    int i;
 
     while (dev->block_iothread) {
         qemu_cond_wait(&dev->free_page_cond, &dev->free_page_lock);
@@ -544,8 +545,10 @@ static bool get_free_page_hints(VirtIOBalloon *dev)
     }
 

hw/virtio/virtio-balloon.c static bool get_free_page_hints(VirtIOBalloon *dev) · line 554

Quoted 10 lines
hw/virtio/virtio-balloon.c +4 -2
     if (elem->in_num && dev->free_page_hint_status == FREE_PAGE_HINT_S_START) {
-        qemu_guest_free_page_hint(elem->in_sg[0].iov_base,
-                                  elem->in_sg[0].iov_len);
+        for (i = 0; i < elem->in_num; i++) {
+            qemu_guest_free_page_hint(elem->in_sg[i].iov_base,
+                                      elem->in_sg[i].iov_len);
+        }
     }
 
 out:

Yes, but:

1. Linux never used more than one
2. QEMU never consumed more than one

The spec states:

"(b) The driver maps a series of pages and adds them to the free_page_vq as individual scatter-gather input buffer entries."

Quoted 8 lines

However, the spec was written by someone else (Alex) as the code was (Wei). The code was there first.

I don't particularly care what to adjust (code or spec). However, to me it feels more like the spec is slightly wrong and it was intended like the code is by the original code author.

But then again, I don't particularly care :)

Original QEMU side code had several bugs so, that's another one. Given nothing too bad happens if guest submits too many S/Gs, and given the spec also has a general chapter suggesting devices are flexible in accepting a single buffer split to multiple S/Gs, I'm inclined to accept the patch.

Quoted 4 lines
Signature · 3 lines
Thanks,

David / dhildenb
#8 lore raw
Quoted 56 lines · Michael S. Tsirkin
Quoted 55 lines · David Hildenbrand
Quoted 26 lines · Jason Wang

We only process the first in sg which may lead to the bitmap of the pages belongs to following sgs were not cleared. This may result more pages to be migrated. Fixing this by process all in sgs for free_page_vq.

---
 hw/virtio/virtio-balloon.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
hw/virtio/virtio-balloon.c +1 -0
index c6962fcbfe..17de2558cb 100644
@@ -510,6 +510,7 @@ static bool get_free_page_hints(VirtIOBalloon *dev)
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtQueue *vq = dev->free_page_vq;
     bool ret = true;
+    int i;
 
     while (dev->block_iothread) {
         qemu_cond_wait(&dev->free_page_cond, &dev->free_page_lock);
@@ -544,8 +545,10 @@ static bool get_free_page_hints(VirtIOBalloon *dev)
     }
 

hw/virtio/virtio-balloon.c static bool get_free_page_hints(VirtIOBalloon *dev) · line 554

Quoted 10 lines
hw/virtio/virtio-balloon.c +4 -2
     if (elem->in_num && dev->free_page_hint_status == FREE_PAGE_HINT_S_START) {
-        qemu_guest_free_page_hint(elem->in_sg[0].iov_base,
-                                  elem->in_sg[0].iov_len);
+        for (i = 0; i < elem->in_num; i++) {
+            qemu_guest_free_page_hint(elem->in_sg[i].iov_base,
+                                      elem->in_sg[i].iov_len);
+        }
     }
 
 out:

Yes, but:

1. Linux never used more than one
2. QEMU never consumed more than one

The spec states:

"(b) The driver maps a series of pages and adds them to the free_page_vq as individual scatter-gather input buffer entries."

However, the spec was written by someone else (Alex) as the code was (Wei). The code was there first.

I don't particularly care what to adjust (code or spec). However, to me it feels more like the spec is slightly wrong and it was intended like the code is by the original code author.

Quoted 7 lines
Quoted 1 line

But then again, I don't particularly care :)

Original QEMU side code had several bugs so, that's another one. Given nothing too bad happens if guest submits too many S/Gs, and given the spec also has a general chapter suggesting devices are flexible in accepting a single buffer split to multiple S/Gs, I'm inclined to accept the patch.

Yeah, as I said, I don't particularly care. It's certainly an "easy change".

Signature · 3 lines
Thanks,

David / dhildenb
#9 lore raw
Quoted 26 lines · David Hildenbrand
Quoted 12 lines · Michael S. Tsirkin
Quoted 11 lines · David Hildenbrand
Quoted 10 lines · Jason Wang

We only process the first in sg which may lead to the bitmap of the pages belongs to following sgs were not cleared. This may result more pages to be migrated. Fixing this by process all in sgs for free_page_vq.

---
 hw/virtio/virtio-balloon.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
Quoted 5 lines
Quoted 5 lines

hw/virtio/virtio-balloon.c static bool get_free_page_hints(VirtIOBalloon

Quoted 5 lines
hw/virtio/virtio-balloon.c +0 -0
index c6962fcbfe..17de2558cb 100644
@@ -510,6 +510,7 @@ static bool get_free_page_hints(VirtIOBalloon

*dev)

Quoted 7 lines
Quoted 7 lines

hw/virtio/virtio-balloon.c static bool get_free_page_hints(VirtIOBalloon *dev)

Quoted 7 lines
hw/virtio/virtio-balloon.c +1 -0
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtQueue *vq = dev->free_page_vq;
     bool ret = true;
+    int i;
 
     while (dev->block_iothread) {
         qemu_cond_wait(&dev->free_page_cond,
Quoted 22 lines

&dev->free_page_lock);

Quoted 1 line
Quoted 1 line

hw/virtio/virtio-balloon.c static bool get_free_page_hints(VirtIOBalloon

Quoted 1 line
hw/virtio/virtio-balloon.c +0 -0
@@ -544,8 +545,10 @@ static bool get_free_page_hints(VirtIOBalloon

*dev)

Quoted 3 lines
Quoted 3 lines

hw/virtio/virtio-balloon.c static bool get_free_page_hints(VirtIOBalloon · line 547

Quoted 3 lines
hw/virtio/virtio-balloon.c +0 -0
     }
 
     if (elem->in_num && dev->free_page_hint_status ==

FREE_PAGE_HINT_S_START) {

Quoted 15 lines
Quoted 15 lines

hw/virtio/virtio-balloon.c static bool get_free_page_hints(VirtIOBalloon *dev) · line 554

Quoted 10 lines
hw/virtio/virtio-balloon.c +4 -2
-        qemu_guest_free_page_hint(elem->in_sg[0].iov_base,
-                                  elem->in_sg[0].iov_len);
+        for (i = 0; i < elem->in_num; i++) {
+            qemu_guest_free_page_hint(elem->in_sg[i].iov_base,
+                                      elem->in_sg[i].iov_len);
+        }
     }
 
 out:

Yes, but:

1. Linux never used more than one
2. QEMU never consumed more than one

Yes, it works based on the fact that Linux only sends one hint each time.

Quoted 21 lines
Quoted 13 lines
Quoted 13 lines

The spec states:

"(b) The driver maps a series of pages and adds them to the free_page_vq as individual scatter-gather input buffer entries."

However, the spec was written by someone else (Alex) as the code was (Wei). The code was there first.

I don't particularly care what to adjust (code or spec). However, to me it feels more like the spec is slightly wrong and it was intended like the code is by the original code author.

Quoted 7 lines
Quoted 1 line

But then again, I don't particularly care :)

Original QEMU side code had several bugs so, that's another one. Given nothing too bad happens if guest submits too many S/Gs, and given the spec also has a general chapter suggesting devices are flexible in accepting a single buffer split to multiple S/Gs, I'm inclined to accept the patch.

Quoted 4 lines

Yeah, as I said, I don't particularly care. It's certainly an "easy change".

Don’t object the change.
Just in case something unexpected, it would be better if someone could help do a test.

Thanks,
Wei

#10 lore raw
Quoted 75 lines · Wei W
Quoted 26 lines · David Hildenbrand
Quoted 12 lines · Michael S. Tsirkin
Quoted 11 lines · David Hildenbrand
Quoted 10 lines · Jason Wang

We only process the first in sg which may lead to the bitmap of the pages belongs to following sgs were not cleared. This may result more pages to be migrated. Fixing this by process all in sgs for free_page_vq.

---
 hw/virtio/virtio-balloon.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
Quoted 5 lines
Quoted 5 lines

hw/virtio/virtio-balloon.c static bool get_free_page_hints(VirtIOBalloon

Quoted 5 lines
hw/virtio/virtio-balloon.c +0 -0
index c6962fcbfe..17de2558cb 100644
@@ -510,6 +510,7 @@ static bool get_free_page_hints(VirtIOBalloon

*dev)

Quoted 7 lines
Quoted 7 lines

hw/virtio/virtio-balloon.c static bool get_free_page_hints(VirtIOBalloon *dev)

Quoted 7 lines
hw/virtio/virtio-balloon.c +1 -0
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtQueue *vq = dev->free_page_vq;
     bool ret = true;
+    int i;
 
     while (dev->block_iothread) {
         qemu_cond_wait(&dev->free_page_cond,
Quoted 22 lines

&dev->free_page_lock);

Quoted 1 line
Quoted 1 line

hw/virtio/virtio-balloon.c static bool get_free_page_hints(VirtIOBalloon

Quoted 1 line
hw/virtio/virtio-balloon.c +0 -0
@@ -544,8 +545,10 @@ static bool get_free_page_hints(VirtIOBalloon

*dev)

Quoted 3 lines
Quoted 3 lines

hw/virtio/virtio-balloon.c static bool get_free_page_hints(VirtIOBalloon · line 547

Quoted 3 lines
hw/virtio/virtio-balloon.c +0 -0
     }
 
     if (elem->in_num && dev->free_page_hint_status ==

FREE_PAGE_HINT_S_START) {

Quoted 15 lines
Quoted 15 lines

hw/virtio/virtio-balloon.c static bool get_free_page_hints(VirtIOBalloon *dev) · line 554

Quoted 10 lines
hw/virtio/virtio-balloon.c +4 -2
-        qemu_guest_free_page_hint(elem->in_sg[0].iov_base,
-                                  elem->in_sg[0].iov_len);
+        for (i = 0; i < elem->in_num; i++) {
+            qemu_guest_free_page_hint(elem->in_sg[i].iov_base,
+                                      elem->in_sg[i].iov_len);
+        }
     }
 
 out:

Yes, but:

1. Linux never used more than one
2. QEMU never consumed more than one

Yes, it works based on the fact that Linux only sends one hint each time.

Quoted 23 lines
Quoted 13 lines
Quoted 13 lines

The spec states:

"(b) The driver maps a series of pages and adds them to the free_page_vq as individual scatter-gather input buffer entries."

However, the spec was written by someone else (Alex) as the code was (Wei). The code was there first.

I don't particularly care what to adjust (code or spec). However, to me it feels more like the spec is slightly wrong and it was intended like the code is by the original code author.

Quoted 7 lines
Quoted 1 line

But then again, I don't particularly care :)

Original QEMU side code had several bugs so, that's another one. Given nothing too bad happens if guest submits too many S/Gs, and given the spec also has a general chapter suggesting devices are flexible in accepting a single buffer split to multiple S/Gs, I'm inclined to accept the patch.

Yeah, as I said, I don't particularly care. It's certainly an "easy change".

Quoted 8 lines
Quoted 2 lines

Don’t object the change.
Just in case something unexpected, it would be better if someone could help do a test.

Thanks,
Wei

Yes, the setup you used to test the original patches will do fine ...

Signature · 1 line
MST
author
#11 lore raw
In reply to Wang, Wei W
Quoted 46 lines · Wei W
Quoted 44 lines · David Hildenbrand
Quoted 12 lines · Michael S. Tsirkin
Quoted 11 lines · David Hildenbrand
Quoted 10 lines · Jason Wang

We only process the first in sg which may lead to the bitmap of the pages belongs to following sgs were not cleared. This may result more pages to be migrated. Fixing this by process all in sgs for free_page_vq.

---
 hw/virtio/virtio-balloon.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
Quoted 5 lines
Quoted 5 lines

hw/virtio/virtio-balloon.c static bool get_free_page_hints(VirtIOBalloon

Quoted 5 lines
hw/virtio/virtio-balloon.c +0 -0
index c6962fcbfe..17de2558cb 100644
@@ -510,6 +510,7 @@ static bool get_free_page_hints(VirtIOBalloon

*dev)

Quoted 7 lines
Quoted 7 lines

hw/virtio/virtio-balloon.c static bool get_free_page_hints(VirtIOBalloon *dev)

Quoted 7 lines
hw/virtio/virtio-balloon.c +1 -0
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtQueue *vq = dev->free_page_vq;
     bool ret = true;
+    int i;
 
     while (dev->block_iothread) {
         qemu_cond_wait(&dev->free_page_cond,

&dev->free_page_lock);

Quoted 1 line
Quoted 1 line

hw/virtio/virtio-balloon.c static bool get_free_page_hints(VirtIOBalloon

Quoted 1 line
hw/virtio/virtio-balloon.c +0 -0
@@ -544,8 +545,10 @@ static bool get_free_page_hints(VirtIOBalloon

*dev)

Quoted 3 lines
Quoted 3 lines

hw/virtio/virtio-balloon.c static bool get_free_page_hints(VirtIOBalloon · line 547

Quoted 3 lines
hw/virtio/virtio-balloon.c +0 -0
     }
 
     if (elem->in_num && dev->free_page_hint_status ==

FREE_PAGE_HINT_S_START) {

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
hw/virtio/virtio-balloon.c +4 -2
-        qemu_guest_free_page_hint(elem->in_sg[0].iov_base,
-                                  elem->in_sg[0].iov_len);
+        for (i = 0; i < elem->in_num; i++) {
+            qemu_guest_free_page_hint(elem->in_sg[i].iov_base,
+                                      elem->in_sg[i].iov_len);
+        }
     }
 
 out:
Quoted 27 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Yes, but:

1. Linux never used more than one
2. QEMU never consumed more than one

Yes, it works based on the fact that Linux only sends one hint each time.

Quoted 20 lines
Quoted 20 lines
Quoted 6 lines

The spec states:

"(b) The driver maps a series of pages and adds them to the free_page_vq as individual scatter-gather input buffer entries."

Quoted 8 lines

However, the spec was written by someone else (Alex) as the code was (Wei). The code was there first.

I don't particularly care what to adjust (code or spec). However, to me it feels more like the spec is slightly wrong and it was intended like the code is by the original code author.

But then again, I don't particularly care :)

Original QEMU side code had several bugs so, that's another one. Given nothing too bad happens if guest submits too many S/Gs, and given the spec also has a general chapter suggesting devices are flexible in accepting a single buffer split to multiple S/Gs, I'm inclined to accept the patch.

Yes, and it's probably too late to change the spec even if we want to change.

Quoted 5 lines
Quoted 5 lines

Yeah, as I said, I don't particularly care. It's certainly an "easy change".

Thanks

Quoted 3 lines

Don’t object the change.
Just in case something unexpected, it would be better if someone could help do a test.

I've tested the code with migration before sending the patches, I see the hint works fine.

Thanks

Quoted 3 lines

Thanks,
Wei

Reviewed-by
#12 lore raw
Quoted 4 lines · Jason Wang

I've tested the code with migration before sending the patches, I see the hint works fine.

That's great (assume you saw great reduction in the migration time as well).

Thanks,
Wei

author
#13 lore raw
Quoted 9 lines · Wei W
Quoted 4 lines · Jason Wang

I've tested the code with migration before sending the patches, I see the hint works fine.

That's great (assume you saw great reduction in the migration time as well).

I don't measure that. But it should be sufficient to see the hint considering we don't modify any logic at dirty bitmap layer.

Thanks

Quoted 3 lines

Thanks,
Wei

#14 lore raw
In reply to Jason Wang
Quoted 9 lines · Jason Wang

We only process the first in sg which may lead to the bitmap of the pages belongs to following sgs were not cleared. This may result more pages to be migrated. Fixing this by process all in sgs for free_page_vq.

---
 hw/virtio/virtio-balloon.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

Typo "virtio" in subject.

#15 lore raw
Quoted 6 lines · Philippe Mathieu-Daudé
Quoted 5 lines · Jason Wang

We only process the first in sg which may lead to the bitmap of the pages belongs to following sgs were not cleared. This may result more pages to be migrated. Fixing this by process all in sgs for free_page_vq.

Quoted 6 lines
Quoted 4 lines
---
 hw/virtio/virtio-balloon.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

Typo "virtio" in subject.

Yes, it's an annoyingly common typo. If using vim, I suggest:

ab virito virtio

in your vimrc.

Signature · 1 line
MST
author
#16 lore raw
Quoted 13 lines · Michael S. Tsirkin
Quoted 11 lines · Philippe Mathieu-Daudé
Quoted 9 lines · Jason Wang

We only process the first in sg which may lead to the bitmap of the pages belongs to following sgs were not cleared. This may result more pages to be migrated. Fixing this by process all in sgs for free_page_vq.

---
 hw/virtio/virtio-balloon.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
Quoted 7 lines
Quoted 1 line

Typo "virtio" in subject.

Yes, it's an annoyingly common typo. If using vim, I suggest:

ab virito virtio

in your vimrc.

Right, actually I'm using flyspell with emacs. I will add a dedicated detection like this if it's possible.

Will fix it.

Thanks

Quoted 4 lines

--
MST