Skip to contentSkip to conversation

virtio: detach VirtQueueElements freed by reset

PATCH In review Stefan Hajnoczi 4 participants 15 messages Sep 19, 2016 – Oct 5, 2016

lore
Share & tools
lore
#0 lore raw

cover virtio: detach VirtQueueElements freed by reset

virtio-blk and virtio-serial need to free VirtQueueElements during device reset. Simply calling g_free(elem) is not enough because the scatter-gather list should be unmapped and vq->inuse must be decremented.

These patches address the issue. I am not including a patch that changes vq->inuse = 0 to assert(!vq->inuse) in virtio_reset() yet because virtio-9p, virtio-gpu, and virtio-net have code paths that do not decrement vq->inuse.

Stefan Hajnoczi (3):
virtio: add virtio_detach_element()
virtio-blk: add missing virtio_detach_element() call
virtio-serial: add missing virtio_detach_element() call

 hw/block/virtio-blk.c       |  1 +
 hw/char/virtio-serial-bus.c | 14 ++++++++++++++
 hw/virtio/virtio.c          | 27 +++++++++++++++++++++++++--
 include/hw/virtio/virtio.h  |  2 ++
 4 files changed, 42 insertions(+), 2 deletions(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/3 virtio: add virtio_detach_element()

During device reset or similar situations a VirtQueueElement needs to be freed without pushing it onto the used ring or rewinding the virtqueue. Extract a new function to do this.

Later patches add virtio_detach_element() calls to existing device so that scatter-gather lists are unmapped and vq->inuse goes back to zero during device reset. Currently some devices don't bother and simply call g_free(elem) which is not a clean way to throw away a VirtQueueElement.

---
 hw/virtio/virtio.c         | 27 +++++++++++++++++++++++++--
 include/hw/virtio/virtio.h |  2 ++
 2 files changed, 27 insertions(+), 2 deletions(-)
hw/virtio/virtio.c +25 -2
index fcf3358..adcef45 100644
@@ -264,12 +264,35 @@ static void virtqueue_unmap_sg(VirtQueue *vq, const VirtQueueElement *elem,
                                   0, elem->out_sg[i].iov_len);
 }
 
+/* virtqueue_detach_element:
+ * @vq: The #VirtQueue
+ * @elem: The #VirtQueueElement
+ * @len: number of bytes written
+ *
+ * Detach the element from the virtqueue.  This function is suitable for device
+ * reset or other situations where a #VirtQueueElement is simply freed and will
+ * not be pushed or discarded.
+ */
+void virtqueue_detach_element(VirtQueue *vq, const VirtQueueElement *elem,
+                              unsigned int len)
+{
+    vq->inuse--;
+    virtqueue_unmap_sg(vq, elem, len);
+}
+
+/* virtqueue_discard:
+ * @vq: The #VirtQueue
+ * @elem: The #VirtQueueElement
+ * @len: number of bytes written
+ *
+ * Pretend the most recent element wasn't popped from the virtqueue.  The next
+ * call to virtqueue_pop() will refetch the element.
+ */
 void virtqueue_discard(VirtQueue *vq, const VirtQueueElement *elem,
                        unsigned int len)
 {
     vq->last_avail_idx--;
-    vq->inuse--;
-    virtqueue_unmap_sg(vq, elem, len);
+    virtqueue_detach_element(vq, elem, len);
 }
 
 /* virtqueue_rewind:
include/hw/virtio/virtio.h +2 -0
index f05559d..ad1e2d6 100644
@@ -152,6 +152,8 @@ void *virtqueue_alloc_element(size_t sz, unsigned out_num, unsigned in_num);
 void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
                     unsigned int len);
 void virtqueue_flush(VirtQueue *vq, unsigned int count);
+void virtqueue_detach_element(VirtQueue *vq, const VirtQueueElement *elem,
+                              unsigned int len);
 void virtqueue_discard(VirtQueue *vq, const VirtQueueElement *elem,
                        unsigned int len);
 bool virtqueue_rewind(VirtQueue *vq, unsigned int num);
Signature · 1 line
2.7.4
Acked-by on v1 1/3
#2 lore raw

Re: [Qemu-devel] [PATCH 1/3] virtio: add virtio_detach_element()

Quoted 4 lines · On Mon, 19 Sep 2016 14:28:03 +0100 Stefan Hajnoczi <[email protected]> wrote:

During device reset or similar situations a VirtQueueElement needs to be freed without pushing it onto the used ring or rewinding the virtqueue. Extract a new function to do this.

Quoted 8 lines

Later patches add virtio_detach_element() calls to existing device so that scatter-gather lists are unmapped and vq->inuse goes back to zero during device reset. Currently some devices don't bother and simply call g_free(elem) which is not a clean way to throw away a VirtQueueElement.

---

FWIW

Quoted 59 lines
 hw/virtio/virtio.c         | 27 +++++++++++++++++++++++++--
 include/hw/virtio/virtio.h |  2 ++
 2 files changed, 27 insertions(+), 2 deletions(-)
hw/virtio/virtio.c +25 -2
index fcf3358..adcef45 100644
@@ -264,12 +264,35 @@ static void virtqueue_unmap_sg(VirtQueue *vq, const VirtQueueElement *elem,
                                   0, elem->out_sg[i].iov_len);
 }
 
+/* virtqueue_detach_element:
+ * @vq: The #VirtQueue
+ * @elem: The #VirtQueueElement
+ * @len: number of bytes written
+ *
+ * Detach the element from the virtqueue.  This function is suitable for device
+ * reset or other situations where a #VirtQueueElement is simply freed and will
+ * not be pushed or discarded.
+ */
+void virtqueue_detach_element(VirtQueue *vq, const VirtQueueElement *elem,
+                              unsigned int len)
+{
+    vq->inuse--;
+    virtqueue_unmap_sg(vq, elem, len);
+}
+
+/* virtqueue_discard:
+ * @vq: The #VirtQueue
+ * @elem: The #VirtQueueElement
+ * @len: number of bytes written
+ *
+ * Pretend the most recent element wasn't popped from the virtqueue.  The next
+ * call to virtqueue_pop() will refetch the element.
+ */
 void virtqueue_discard(VirtQueue *vq, const VirtQueueElement *elem,
                        unsigned int len)
 {
     vq->last_avail_idx--;
-    vq->inuse--;
-    virtqueue_unmap_sg(vq, elem, len);
+    virtqueue_detach_element(vq, elem, len);
 }
 
 /* virtqueue_rewind:
include/hw/virtio/virtio.h +2 -0
index f05559d..ad1e2d6 100644
@@ -152,6 +152,8 @@ void *virtqueue_alloc_element(size_t sz, unsigned out_num, unsigned in_num);
 void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
                     unsigned int len);
 void virtqueue_flush(VirtQueue *vq, unsigned int count);
+void virtqueue_detach_element(VirtQueue *vq, const VirtQueueElement *elem,
+                              unsigned int len);
 void virtqueue_discard(VirtQueue *vq, const VirtQueueElement *elem,
                        unsigned int len);
 bool virtqueue_rewind(VirtQueue *vq, unsigned int num);
Reviewed-by on v1 1/3
#3 lore raw

Re: [Qemu-devel] [PATCH 1/3] virtio: add virtio_detach_element()

In reply to Stefan Hajnoczi
Quoted 45 lines · Stefan Hajnoczi

During device reset or similar situations a VirtQueueElement needs to be freed without pushing it onto the used ring or rewinding the virtqueue. Extract a new function to do this.

Later patches add virtio_detach_element() calls to existing device so that scatter-gather lists are unmapped and vq->inuse goes back to zero during device reset. Currently some devices don't bother and simply call g_free(elem) which is not a clean way to throw away a VirtQueueElement.

---
 hw/virtio/virtio.c         | 27 +++++++++++++++++++++++++--
 include/hw/virtio/virtio.h |  2 ++
 2 files changed, 27 insertions(+), 2 deletions(-)
hw/virtio/virtio.c +21 -0
index fcf3358..adcef45 100644
@@ -264,12 +264,35 @@ static void virtqueue_unmap_sg(VirtQueue *vq, const VirtQueueElement *elem,
                                   0, elem->out_sg[i].iov_len);
 }
 
+/* virtqueue_detach_element:
+ * @vq: The #VirtQueue
+ * @elem: The #VirtQueueElement
+ * @len: number of bytes written
+ *
+ * Detach the element from the virtqueue.  This function is suitable for device
+ * reset or other situations where a #VirtQueueElement is simply freed and will
+ * not be pushed or discarded.
+ */
+void virtqueue_detach_element(VirtQueue *vq, const VirtQueueElement *elem,
+                              unsigned int len)
+{
+    vq->inuse--;
+    virtqueue_unmap_sg(vq, elem, len);
+}
+
+/* virtqueue_discard:
+ * @vq: The #VirtQueue
+ * @elem: The #VirtQueueElement
+ * @len: number of bytes written
+ *

hw/virtio/virtio.c static void virtqueue_unmap_sg(VirtQueue *vq, const VirtQueueElement *elem, · line 295

Quoted 10 lines
hw/virtio/virtio.c +4 -2
+ * Pretend the most recent element wasn't popped from the virtqueue.  The next
+ * call to virtqueue_pop() will refetch the element.
+ */
 void virtqueue_discard(VirtQueue *vq, const VirtQueueElement *elem,
                        unsigned int len)
 {
     vq->last_avail_idx--;
-    vq->inuse--;
-    virtqueue_unmap_sg(vq, elem, len);
+    virtqueue_detach_element(vq, elem, len);

Random comment, not directly related to this change. Would it be worth adding an assert to this function that elem->index and vq->last_avail_idx match? In other words, enforce the "most recent" qualifier mentioned in the comment. As more virtqueue_* functions are added and the complexity goes up, it is easy to get confused. Also, I think that naming this function virtqueue_unpop instead of virtqueue_discard would help.

Quoted 17 lines
hw/virtio/virtio.c +0 -0
 }
 
 /* virtqueue_rewind:
include/hw/virtio/virtio.h +2 -1
index f05559d..ad1e2d6 100644
@@ -152,6 +152,8 @@ void *virtqueue_alloc_element(size_t sz, unsigned out_num, unsigned in_num);
 void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
                     unsigned int len);
 void virtqueue_flush(VirtQueue *vq, unsigned int count);
+void virtqueue_detach_element(VirtQueue *vq, const VirtQueueElement *elem,
+                              unsigned int len);
 void virtqueue_discard(VirtQueue *vq, const VirtQueueElement *elem,
                        unsigned int len);
 bool virtqueue_rewind(VirtQueue *vq, unsigned int num);
--
Quoted 2 lines

2.7.4

Thanks!
Ladi

#4 lore raw

Re: [Qemu-devel] [PATCH 1/3] virtio: add virtio_detach_element()

Quoted 17 lines · Ladi Prosek
Quoted 12 lines · Stefan Hajnoczi

During device res> > +/* virtqueue_discard:
+ * @vq: The #VirtQueue
+ * @elem: The #VirtQueueElement
+ * @len: number of bytes written
+ *
+ * Pretend the most recent element wasn't popped from the virtqueue. The next
+ * call to virtqueue_pop() will refetch the element.
+ */
void virtqueue_discard(VirtQueue *vq, const VirtQueueElement *elem,
unsigned int len)

 {
     vq->last_avail_idx--;

hw/virtio/virtio.c static void virtqueue_unmap_sg(VirtQueue *vq, const VirtQueueElement *elem, · line 295

Quoted 3 lines
hw/virtio/virtio.c +1 -2
-    vq->inuse--;
-    virtqueue_unmap_sg(vq, elem, len);
+    virtqueue_detach_element(vq, elem, len);
Quoted 7 lines

Random comment, not directly related to this change. Would it be worth adding an assert to this function that elem->index and vq->last_avail_idx match? In other words, enforce the "most recent" qualifier mentioned in the comment. As more virtqueue_* functions are added and the complexity goes up, it is easy to get confused. Also, I think that naming this function virtqueue_unpop instead of virtqueue_discard would help.

elem->index is a descriptor ring index. vq->last_avail_idx is an index into the available ring. They are different but your suggestion makes sense in general.

We shouldn't read from vring memory again for an assertion so deferencing the available ring isn't possible (because we cannot rely on vring memory contents after processing the request). One way to implement the assertion is to put VirtQueueElements on a linked list (vq->inuse_elems) but that probably needs live migration support.

I agree that renaming to unpop makes the semantics clearer.

Would you like to submit a patch for either or both?

#5 lore raw

Re: [Qemu-devel] [PATCH 1/3] virtio: add virtio_detach_element()

Quoted 26 lines · Stefan Hajnoczi
Quoted 17 lines · Ladi Prosek
Quoted 12 lines · Stefan Hajnoczi

During device res> > +/* virtqueue_discard:
+ * @vq: The #VirtQueue
+ * @elem: The #VirtQueueElement
+ * @len: number of bytes written
+ *
+ * Pretend the most recent element wasn't popped from the virtqueue. The next
+ * call to virtqueue_pop() will refetch the element.
+ */
void virtqueue_discard(VirtQueue *vq, const VirtQueueElement *elem,
unsigned int len)

 {
     vq->last_avail_idx--;

hw/virtio/virtio.c static void virtqueue_unmap_sg(VirtQueue *vq, const VirtQueueElement *elem, · line 295

Quoted 3 lines
hw/virtio/virtio.c +1 -2
-    vq->inuse--;
-    virtqueue_unmap_sg(vq, elem, len);
+    virtqueue_detach_element(vq, elem, len);
Quoted 7 lines

Random comment, not directly related to this change. Would it be worth adding an assert to this function that elem->index and vq->last_avail_idx match? In other words, enforce the "most recent" qualifier mentioned in the comment. As more virtqueue_* functions are added and the complexity goes up, it is easy to get confused. Also, I think that naming this function virtqueue_unpop instead of virtqueue_discard would help.

Quoted 3 lines

elem->index is a descriptor ring index. vq->last_avail_idx is an index into the available ring. They are different but your suggestion makes sense in general.

Oh, right, I didn't mean they would be identical but something like this:

  g_assert(elem->index == virtqueue_get_head(vq, vq->last_avail_idx));
Quoted 3 lines

We shouldn't read from vring memory again for an assertion so deferencing the available ring isn't possible (because we cannot rely on vring memory contents after processing the request).

Not sure I follow, shouldn't available ring memory at that index still be the same? Basically I'd like to assert that the next virtqueue_pop would return the same element.

Quoted 7 lines

One way to
implement the assertion is to put VirtQueueElements on a linked list (vq->inuse_elems) but that probably needs live migration support.

I agree that renaming to unpop makes the semantics clearer.

Would you like to submit a patch for either or both?

Yes, I'll do both.

#6 lore raw

Re: [Qemu-devel] [PATCH 1/3] virtio: add virtio_detach_element()

Quoted 35 lines · Ladi Prosek
Quoted 26 lines · Stefan Hajnoczi
Quoted 17 lines · Ladi Prosek
Quoted 12 lines · Stefan Hajnoczi

During device res> > +/* virtqueue_discard:
+ * @vq: The #VirtQueue
+ * @elem: The #VirtQueueElement
+ * @len: number of bytes written
+ *
+ * Pretend the most recent element wasn't popped from the virtqueue. The next
+ * call to virtqueue_pop() will refetch the element.
+ */
void virtqueue_discard(VirtQueue *vq, const VirtQueueElement *elem,
unsigned int len)

 {
     vq->last_avail_idx--;

hw/virtio/virtio.c static void virtqueue_unmap_sg(VirtQueue *vq, const VirtQueueElement *elem, · line 295

Quoted 3 lines
hw/virtio/virtio.c +1 -2
-    vq->inuse--;
-    virtqueue_unmap_sg(vq, elem, len);
+    virtqueue_detach_element(vq, elem, len);
Quoted 7 lines

Random comment, not directly related to this change. Would it be worth adding an assert to this function that elem->index and vq->last_avail_idx match? In other words, enforce the "most recent" qualifier mentioned in the comment. As more virtqueue_* functions are added and the complexity goes up, it is easy to get confused. Also, I think that naming this function virtqueue_unpop instead of virtqueue_discard would help.

Quoted 3 lines

elem->index is a descriptor ring index. vq->last_avail_idx is an index into the available ring. They are different but your suggestion makes sense in general.

Oh, right, I didn't mean they would be identical but something like this:

  g_assert(elem->index == virtqueue_get_head(vq, vq->last_avail_idx));
Quoted 7 lines
Quoted 3 lines

We shouldn't read from vring memory again for an assertion so deferencing the available ring isn't possible (because we cannot rely on vring memory contents after processing the request).

Not sure I follow, shouldn't available ring memory at that index still be the same? Basically I'd like to assert that the next virtqueue_pop would return the same element.

Assertions cannot be guest-triggerable. The guest can make the assertion fail by writing a new value to the available ring.

That might not sound like an issue but consider a scenario where the virtio PCI device is passed through to a nested guest. Now the nested guest can kill the parent hypervisor and all sibling VMs.

Stefan

#7 lore raw

Re: [Qemu-devel] [PATCH 1/3] virtio: add virtio_detach_element()

Quoted 44 lines · Stefan Hajnoczi
Quoted 35 lines · Ladi Prosek
Quoted 26 lines · Stefan Hajnoczi
Quoted 17 lines · Ladi Prosek
Quoted 12 lines · Stefan Hajnoczi

During device res> > +/* virtqueue_discard:
+ * @vq: The #VirtQueue
+ * @elem: The #VirtQueueElement
+ * @len: number of bytes written
+ *
+ * Pretend the most recent element wasn't popped from the virtqueue. The next
+ * call to virtqueue_pop() will refetch the element.
+ */
void virtqueue_discard(VirtQueue *vq, const VirtQueueElement *elem,
unsigned int len)

 {
     vq->last_avail_idx--;

hw/virtio/virtio.c static void virtqueue_unmap_sg(VirtQueue *vq, const VirtQueueElement *elem, · line 295

Quoted 3 lines
hw/virtio/virtio.c +1 -2
-    vq->inuse--;
-    virtqueue_unmap_sg(vq, elem, len);
+    virtqueue_detach_element(vq, elem, len);
Quoted 7 lines

Random comment, not directly related to this change. Would it be worth adding an assert to this function that elem->index and vq->last_avail_idx match? In other words, enforce the "most recent" qualifier mentioned in the comment. As more virtqueue_* functions are added and the complexity goes up, it is easy to get confused. Also, I think that naming this function virtqueue_unpop instead of virtqueue_discard would help.

Quoted 3 lines

elem->index is a descriptor ring index. vq->last_avail_idx is an index into the available ring. They are different but your suggestion makes sense in general.

Oh, right, I didn't mean they would be identical but something like this:

  g_assert(elem->index == virtqueue_get_head(vq, vq->last_avail_idx));
Quoted 7 lines
Quoted 3 lines

We shouldn't read from vring memory again for an assertion so deferencing the available ring isn't possible (because we cannot rely on vring memory contents after processing the request).

Not sure I follow, shouldn't available ring memory at that index still be the same? Basically I'd like to assert that the next virtqueue_pop would return the same element.

Quoted 6 lines

Assertions cannot be guest-triggerable. The guest can make the assertion fail by writing a new value to the available ring.

That might not sound like an issue but consider a scenario where the virtio PCI device is passed through to a nested guest. Now the nested guest can kill the parent hypervisor and all sibling VMs.

Got it, all clear now, thanks!

Quoted 1 line

Stefan

#8 lore raw

PATCH 2/3 virtio-blk: add missing virtio_detach_element() call

In reply to Stefan Hajnoczi

Make sure to unmap the scatter-gather list and decrement vq->inuse before freeing requests in virtio_blk_reset().

---
 hw/block/virtio-blk.c | 1 +
 1 file changed, 1 insertion(+)
hw/block/virtio-blk.c +1 -0
index 3a6112f..c7ca4d6 100644
@@ -665,6 +665,7 @@ static void virtio_blk_reset(VirtIODevice *vdev)
     while (s->rq) {
         req = s->rq;
         s->rq = req->next;
+        virtqueue_detach_element(req->vq, &req->elem, 0);
         virtio_blk_free_request(req);
     }
 
Signature · 1 line
2.7.4
Reviewed-by on v1 2/3
#9 lore raw

Re: [Qemu-devel] [PATCH 2/3] virtio-blk: add missing virtio_detach_element() call

Quoted 13 lines · Stefan Hajnoczi

Make sure to unmap the scatter-gather list and decrement vq->inuse before freeing requests in virtio_blk_reset().

---
 hw/block/virtio-blk.c | 1 +
 1 file changed, 1 insertion(+)
hw/block/virtio-blk.c +0 -0
index 3a6112f..c7ca4d6 100644

hw/block/virtio-blk.c static void virtio_blk_reset(VirtIODevice *vdev) · line 669

Quoted 5 lines
hw/block/virtio-blk.c +1 -0
@@ -665,6 +665,7 @@ static void virtio_blk_reset(VirtIODevice *vdev)
     while (s->rq) {
         req = s->rq;
         s->rq = req->next;
+        virtqueue_detach_element(req->vq, &req->elem, 0);
         virtio_blk_free_request(req);

Random observation. virtio_blk_free_request should be static and removed from the header file. Or maybe removed altogether because g_free takes NULL pointers just fine.

Quoted 5 lines
hw/block/virtio-blk.c +0 -1
     }
 
--

2.7.4

Thanks!

#10 lore raw

Re: [Qemu-devel] [PATCH 2/3] virtio-blk: add missing virtio_detach_element() call

Quoted 9 lines · On Tue, 27 Sep 2016 09:49:17 +0200 Ladi Prosek <[email protected]> wrote:
Quoted 8 lines · Stefan Hajnoczi

Make sure to unmap the scatter-gather list and decrement vq->inuse before freeing requests in virtio_blk_reset().

---
 hw/block/virtio-blk.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 13 lines

hw/block/virtio-blk.c static void virtio_blk_reset(VirtIODevice *vdev) · line 669

Quoted 10 lines
hw/block/virtio-blk.c +1 -0
index 3a6112f..c7ca4d6 100644
@@ -665,6 +665,7 @@ static void virtio_blk_reset(VirtIODevice *vdev)
     while (s->rq) {
         req = s->rq;
         s->rq = req->next;
+        virtqueue_detach_element(req->vq, &req->elem, 0);
         virtio_blk_free_request(req);  

Random observation. virtio_blk_free_request should be static and removed from the header file.

I've sent a followup patch for this:

<147487884587.6679.6170297932839464278.stgit@bahia>

Quoted 2 lines

Or maybe removed altogether because g_free takes NULL pointers just fine.

virtio_blk_free_request() does not seem useful indeed... :)

Cheers.

--
Greg

Quoted 10 lines
Quoted 5 lines
hw/block/virtio-blk.c +0 -1
     }
 
--

2.7.4

Thanks!

#11 lore raw

PATCH 3/3 virtio-serial: add missing virtio_detach_element() call

In reply to Stefan Hajnoczi

Ports enter a "throttled" state when writing to the chardev would block. The current output VirtQueueElement is kept around until the chardev becomes writable again.

There are several places in the virtio-serial lifecycle where the VirtQueueElement should be thrown away. For example, if the virtio device is reset then virtqueue elements are no longer valid.

This patch adds the discard_throttle_data() function to unmap the scatter-gather list and decrement vq->inuse. This ensures that the VirtQueueElement is freed properly.

---
 hw/char/virtio-serial-bus.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
hw/char/virtio-serial-bus.c +14 -0
index db57a38..35aa3c7 100644
@@ -132,6 +132,15 @@ static void discard_vq_data(VirtQueue *vq, VirtIODevice *vdev)
     virtio_notify(vdev, vq);
 }
 
+static void discard_throttle_data(VirtIOSerialPort *port)
+{
+    if (port->elem) {
+        virtqueue_detach_element(port->ovq, port->elem, 0);
+        g_free(port->elem);
+        port->elem = NULL;
+    }
+}
+
 static void do_flush_queued_data(VirtIOSerialPort *port, VirtQueue *vq,
                                  VirtIODevice *vdev)
 {
@@ -254,6 +263,7 @@ int virtio_serial_close(VirtIOSerialPort *port)
      * consume, reset the throttling flag and discard the data.
      */
     port->throttled = false;
+    discard_throttle_data(port);
     discard_vq_data(port->ovq, VIRTIO_DEVICE(port->vser));
 
     send_control_event(port->vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 0);
@@ -554,6 +564,9 @@ static void guest_reset(VirtIOSerial *vser)
 
     QTAILQ_FOREACH(port, &vser->ports, next) {
         vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
+
+        discard_throttle_data(port);
+
         if (port->guest_connected) {
             port->guest_connected = false;
             if (vsc->set_guest_connected) {
@@ -864,6 +877,7 @@ static void remove_port(VirtIOSerial *vser, uint32_t port_id)
     assert(port);
 
     /* Flush out any unconsumed buffers first */
+    discard_throttle_data(port);
     discard_vq_data(port->ovq, VIRTIO_DEVICE(port->vser));
 
     send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_REMOVE, 1);
Signature · 1 line
2.7.4
Tested-by Reviewed-by on v1 3/3
#12 lore raw

Re: [Qemu-devel] [PATCH 3/3] virtio-serial: add missing virtio_detach_element() call

Quoted 8 lines · Stefan Hajnoczi

Ports enter a "throttled" state when writing to the chardev would block. The current output VirtQueueElement is kept around until the chardev becomes writable again.

There are several places in the virtio-serial lifecycle where the VirtQueueElement should be thrown away. For example, if the virtio device is reset then virtqueue elements are no longer valid.

Quoted 6 lines

This patch adds the discard_throttle_data() function to unmap the scatter-gather list and decrement vq->inuse. This ensures that the VirtQueueElement is freed properly.

I have tested this patch with a simple serial stress test to exercise the code paths.

#13 lore raw
In reply to Stefan Hajnoczi
Quoted 13 lines · Stefan Hajnoczi

virtio-blk and virtio-serial need to free VirtQueueElements during device reset. Simply calling g_free(elem) is not enough because the scatter-gather list should be unmapped and vq->inuse must be decremented.

These patches address the issue. I am not including a patch that changes vq->inuse = 0 to assert(!vq->inuse) in virtio_reset() yet because virtio-9p, virtio-gpu, and virtio-net have code paths that do not decrement vq->inuse.

Stefan Hajnoczi (3):
virtio: add virtio_detach_element()
virtio-blk: add missing virtio_detach_element() call
virtio-serial: add missing virtio_detach_element() call

Quoted 5 lines
 hw/block/virtio-blk.c       |  1 +
 hw/char/virtio-serial-bus.c | 14 ++++++++++++++
 hw/virtio/virtio.c          | 27 +++++++++++++++++++++++++--
 include/hw/virtio/virtio.h  |  2 ++
 4 files changed, 42 insertions(+), 2 deletions(-)

Ping?

#14 lore raw
Quoted 14 lines · On Tue, 27 Sep 2016 11:08:53 +0100 Stefan Hajnoczi <[email protected]> wrote:
Quoted 13 lines · Stefan Hajnoczi

virtio-blk and virtio-serial need to free VirtQueueElements during device reset. Simply calling g_free(elem) is not enough because the scatter-gather list should be unmapped and vq->inuse must be decremented.

These patches address the issue. I am not including a patch that changes vq->inuse = 0 to assert(!vq->inuse) in virtio_reset() yet because virtio-9p, virtio-gpu, and virtio-net have code paths that do not decrement vq->inuse.

Stefan Hajnoczi (3):
virtio: add virtio_detach_element()
virtio-blk: add missing virtio_detach_element() call
virtio-serial: add missing virtio_detach_element() call

Quoted 7 lines
Quoted 5 lines
 hw/block/virtio-blk.c       |  1 +
 hw/char/virtio-serial-bus.c | 14 ++++++++++++++
 hw/virtio/virtio.c          | 27 +++++++++++++++++++++++++--
 include/hw/virtio/virtio.h  |  2 ++
 4 files changed, 42 insertions(+), 2 deletions(-)  

Ping?

+1 because patch 1/3 of this series is required by:

Subject: [PATCH v4 0/9] virtio: avoid inappropriate QEMU termination in device code
Date: Fri, 30 Sep 2016 17:12:32 +0200
Message-Id: <147524835229.953.16853431367911587573.stgit@bahia>

Cheers.

--
Greg