Commit 88f251ac authored by Amit Shah's avatar Amit Shah Committed by Rusty Russell

virtio: console: Remove cached data on port close

Remove any data that we might have in a port's inbuf when closing a port
or when any data is received when a port is closed.
Signed-off-by: default avatarAmit Shah <amit.shah@redhat.com>
Signed-off-by: default avatarRusty Russell <rusty@rustcorp.com.au>
parent 431edb8a
...@@ -316,6 +316,31 @@ static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf) ...@@ -316,6 +316,31 @@ static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
return ret; return ret;
} }
/* Discard any unread data this port has. Callers lockers. */
static void discard_port_data(struct port *port)
{
struct port_buffer *buf;
struct virtqueue *vq;
unsigned int len;
vq = port->in_vq;
if (port->inbuf)
buf = port->inbuf;
else
buf = vq->vq_ops->get_buf(vq, &len);
if (!buf)
return;
if (add_inbuf(vq, buf) < 0) {
buf->len = buf->offset = 0;
dev_warn(port->dev, "Error adding buffer back to vq\n");
return;
}
port->inbuf = NULL;
}
static bool port_has_data(struct port *port) static bool port_has_data(struct port *port)
{ {
unsigned long flags; unsigned long flags;
...@@ -534,8 +559,13 @@ static int port_fops_release(struct inode *inode, struct file *filp) ...@@ -534,8 +559,13 @@ static int port_fops_release(struct inode *inode, struct file *filp)
/* Notify host of port being closed */ /* Notify host of port being closed */
send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0); send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
spin_lock_irq(&port->inbuf_lock);
port->guest_connected = false; port->guest_connected = false;
discard_port_data(port);
spin_unlock_irq(&port->inbuf_lock);
return 0; return 0;
} }
...@@ -872,6 +902,16 @@ static void in_intr(struct virtqueue *vq) ...@@ -872,6 +902,16 @@ static void in_intr(struct virtqueue *vq)
spin_lock_irqsave(&port->inbuf_lock, flags); spin_lock_irqsave(&port->inbuf_lock, flags);
port->inbuf = get_inbuf(port); port->inbuf = get_inbuf(port);
/*
* Don't queue up data when port is closed. This condition
* can be reached when a console port is not yet connected (no
* tty is spawned) and the host sends out data to console
* ports. For generic serial ports, the host won't
* (shouldn't) send data till the guest is connected.
*/
if (!port->guest_connected)
discard_port_data(port);
spin_unlock_irqrestore(&port->inbuf_lock, flags); spin_unlock_irqrestore(&port->inbuf_lock, flags);
wake_up_interruptible(&port->waitqueue); wake_up_interruptible(&port->waitqueue);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment