提交 3c7969cc 编写于 作者: A Amit Shah 提交者: Rusty Russell

virtio: console: Ensure only one process can have a port open at a time

Add a guest_connected field that ensures only one process
can have a port open at a time.

This also ensures we don't have a race when we later add support for
dropping buffers when closing the char dev and buffer caching is turned
off for the particular port.
Signed-off-by: NAmit Shah <amit.shah@redhat.com>
Signed-off-by: NRusty Russell <rusty@rustcorp.com.au>
上级 2030fa49
...@@ -175,6 +175,9 @@ struct port { ...@@ -175,6 +175,9 @@ struct port {
/* Is the host device open */ /* Is the host device open */
bool host_connected; bool host_connected;
/* We should allow only one process to open a port */
bool guest_connected;
}; };
/* This is the very early arch-specified put chars function. */ /* This is the very early arch-specified put chars function. */
...@@ -528,6 +531,8 @@ static int port_fops_release(struct inode *inode, struct file *filp) ...@@ -528,6 +531,8 @@ 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);
port->guest_connected = false;
return 0; return 0;
} }
...@@ -546,6 +551,16 @@ static int port_fops_open(struct inode *inode, struct file *filp) ...@@ -546,6 +551,16 @@ static int port_fops_open(struct inode *inode, struct file *filp)
if (is_console_port(port)) if (is_console_port(port))
return -ENXIO; return -ENXIO;
/* Allow only one process to open a particular port at a time */
spin_lock_irq(&port->inbuf_lock);
if (port->guest_connected) {
spin_unlock_irq(&port->inbuf_lock);
return -EMFILE;
}
port->guest_connected = true;
spin_unlock_irq(&port->inbuf_lock);
/* Notify host of port being opened */ /* Notify host of port being opened */
send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1); send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
...@@ -709,6 +724,7 @@ int init_port_console(struct port *port) ...@@ -709,6 +724,7 @@ int init_port_console(struct port *port)
pdrvdata.next_vtermno++; pdrvdata.next_vtermno++;
list_add_tail(&port->cons.list, &pdrvdata.consoles); list_add_tail(&port->cons.list, &pdrvdata.consoles);
spin_unlock_irq(&pdrvdata_lock); spin_unlock_irq(&pdrvdata_lock);
port->guest_connected = true;
/* Notify host of port being opened */ /* Notify host of port being opened */
send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1); send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
...@@ -856,7 +872,7 @@ static int add_port(struct ports_device *portdev, u32 id) ...@@ -856,7 +872,7 @@ static int add_port(struct ports_device *portdev, u32 id)
port->inbuf = NULL; port->inbuf = NULL;
port->cons.hvc = NULL; port->cons.hvc = NULL;
port->host_connected = false; port->host_connected = port->guest_connected = false;
port->in_vq = portdev->in_vqs[port->id]; port->in_vq = portdev->in_vqs[port->id];
port->out_vq = portdev->out_vqs[port->id]; port->out_vq = portdev->out_vqs[port->id];
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册