Skip to content

Commit d4c19cd

Browse files
stefanhaRHmstsirkin
authored andcommitted
virtio-serial: add missing virtio_detach_element() call
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. Cc: amit.shah@redhat.com Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Tested-by: Ladi Prosek <lprosek@redhat.com> Reviewed-by: Ladi Prosek <lprosek@redhat.com> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
1 parent 97b93c8 commit d4c19cd

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

hw/char/virtio-serial-bus.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,15 @@ static void discard_vq_data(VirtQueue *vq, VirtIODevice *vdev)
145145
virtio_notify(vdev, vq);
146146
}
147147

148+
static void discard_throttle_data(VirtIOSerialPort *port)
149+
{
150+
if (port->elem) {
151+
virtqueue_detach_element(port->ovq, port->elem, 0);
152+
g_free(port->elem);
153+
port->elem = NULL;
154+
}
155+
}
156+
148157
static void do_flush_queued_data(VirtIOSerialPort *port, VirtQueue *vq,
149158
VirtIODevice *vdev)
150159
{
@@ -267,6 +276,7 @@ int virtio_serial_close(VirtIOSerialPort *port)
267276
* consume, reset the throttling flag and discard the data.
268277
*/
269278
port->throttled = false;
279+
discard_throttle_data(port);
270280
discard_vq_data(port->ovq, VIRTIO_DEVICE(port->vser));
271281

272282
send_control_event(port->vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 0);
@@ -591,6 +601,9 @@ static void guest_reset(VirtIOSerial *vser)
591601

592602
QTAILQ_FOREACH(port, &vser->ports, next) {
593603
vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
604+
605+
discard_throttle_data(port);
606+
594607
if (port->guest_connected) {
595608
port->guest_connected = false;
596609
if (vsc->set_guest_connected) {
@@ -901,6 +914,7 @@ static void remove_port(VirtIOSerial *vser, uint32_t port_id)
901914
assert(port);
902915

903916
/* Flush out any unconsumed buffers first */
917+
discard_throttle_data(port);
904918
discard_vq_data(port->ovq, VIRTIO_DEVICE(port->vser));
905919

906920
send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_REMOVE, 1);

0 commit comments

Comments
 (0)