Skip to content

Commit bb014db

Browse files
committed
Merge tag 'fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/rusty/linux
Pull virtio fixes from Rusty Russell: "More virtio console fixes than I'm happy with, but all real issues, and all CC:stable.." * tag 'fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/rusty/linux: virtio-scsi: Fix virtqueue affinity setup virtio: console: return -ENODEV on all read operations after unplug virtio: console: fix raising SIGIO after port unplug virtio: console: clean up port data immediately at time of unplug virtio: console: fix race in port_fops_open() and port unplug virtio: console: fix race with port unplug and open/close virtio/console: Add pipe_lock/unlock for splice_write virtio/console: Quit from splice_write if pipe->nrbufs is 0
2 parents 67ef626 + aa52aee commit bb014db

2 files changed

Lines changed: 49 additions & 23 deletions

File tree

‎drivers/char/virtio_console.c‎

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,12 @@ static struct port *find_port_by_devt_in_portdev(struct ports_device *portdev,
272272
unsigned long flags;
273273

274274
spin_lock_irqsave(&portdev->ports_lock, flags);
275-
list_for_each_entry(port, &portdev->ports, list)
276-
if (port->cdev->dev == dev)
275+
list_for_each_entry(port, &portdev->ports, list) {
276+
if (port->cdev->dev == dev) {
277+
kref_get(&port->kref);
277278
goto out;
279+
}
280+
}
278281
port = NULL;
279282
out:
280283
spin_unlock_irqrestore(&portdev->ports_lock, flags);
@@ -746,6 +749,10 @@ static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
746749

747750
port = filp->private_data;
748751

752+
/* Port is hot-unplugged. */
753+
if (!port->guest_connected)
754+
return -ENODEV;
755+
749756
if (!port_has_data(port)) {
750757
/*
751758
* If nothing's connected on the host just return 0 in
@@ -762,7 +769,7 @@ static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
762769
if (ret < 0)
763770
return ret;
764771
}
765-
/* Port got hot-unplugged. */
772+
/* Port got hot-unplugged while we were waiting above. */
766773
if (!port->guest_connected)
767774
return -ENODEV;
768775
/*
@@ -932,26 +939,43 @@ static ssize_t port_fops_splice_write(struct pipe_inode_info *pipe,
932939
if (is_rproc_serial(port->out_vq->vdev))
933940
return -EINVAL;
934941

942+
/*
943+
* pipe->nrbufs == 0 means there are no data to transfer,
944+
* so this returns just 0 for no data.
945+
*/
946+
pipe_lock(pipe);
947+
if (!pipe->nrbufs) {
948+
ret = 0;
949+
goto error_out;
950+
}
951+
935952
ret = wait_port_writable(port, filp->f_flags & O_NONBLOCK);
936953
if (ret < 0)
937-
return ret;
954+
goto error_out;
938955

939956
buf = alloc_buf(port->out_vq, 0, pipe->nrbufs);
940-
if (!buf)
941-
return -ENOMEM;
957+
if (!buf) {
958+
ret = -ENOMEM;
959+
goto error_out;
960+
}
942961

943962
sgl.n = 0;
944963
sgl.len = 0;
945964
sgl.size = pipe->nrbufs;
946965
sgl.sg = buf->sg;
947966
sg_init_table(sgl.sg, sgl.size);
948967
ret = __splice_from_pipe(pipe, &sd, pipe_to_sg);
968+
pipe_unlock(pipe);
949969
if (likely(ret > 0))
950970
ret = __send_to_port(port, buf->sg, sgl.n, sgl.len, buf, true);
951971

952972
if (unlikely(ret <= 0))
953973
free_buf(buf, true);
954974
return ret;
975+
976+
error_out:
977+
pipe_unlock(pipe);
978+
return ret;
955979
}
956980

957981
static unsigned int port_fops_poll(struct file *filp, poll_table *wait)
@@ -1019,14 +1043,14 @@ static int port_fops_open(struct inode *inode, struct file *filp)
10191043
struct port *port;
10201044
int ret;
10211045

1046+
/* We get the port with a kref here */
10221047
port = find_port_by_devt(cdev->dev);
1048+
if (!port) {
1049+
/* Port was unplugged before we could proceed */
1050+
return -ENXIO;
1051+
}
10231052
filp->private_data = port;
10241053

1025-
/* Prevent against a port getting hot-unplugged at the same time */
1026-
spin_lock_irq(&port->portdev->ports_lock);
1027-
kref_get(&port->kref);
1028-
spin_unlock_irq(&port->portdev->ports_lock);
1029-
10301054
/*
10311055
* Don't allow opening of console port devices -- that's done
10321056
* via /dev/hvc
@@ -1498,14 +1522,6 @@ static void remove_port(struct kref *kref)
14981522

14991523
port = container_of(kref, struct port, kref);
15001524

1501-
sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
1502-
device_destroy(pdrvdata.class, port->dev->devt);
1503-
cdev_del(port->cdev);
1504-
1505-
kfree(port->name);
1506-
1507-
debugfs_remove(port->debugfs_file);
1508-
15091525
kfree(port);
15101526
}
15111527

@@ -1539,12 +1555,14 @@ static void unplug_port(struct port *port)
15391555
spin_unlock_irq(&port->portdev->ports_lock);
15401556

15411557
if (port->guest_connected) {
1558+
/* Let the app know the port is going down. */
1559+
send_sigio_to_port(port);
1560+
1561+
/* Do this after sigio is actually sent */
15421562
port->guest_connected = false;
15431563
port->host_connected = false;
1544-
wake_up_interruptible(&port->waitqueue);
15451564

1546-
/* Let the app know the port is going down. */
1547-
send_sigio_to_port(port);
1565+
wake_up_interruptible(&port->waitqueue);
15481566
}
15491567

15501568
if (is_console_port(port)) {
@@ -1563,6 +1581,14 @@ static void unplug_port(struct port *port)
15631581
*/
15641582
port->portdev = NULL;
15651583

1584+
sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
1585+
device_destroy(pdrvdata.class, port->dev->devt);
1586+
cdev_del(port->cdev);
1587+
1588+
kfree(port->name);
1589+
1590+
debugfs_remove(port->debugfs_file);
1591+
15661592
/*
15671593
* Locks around here are not necessary - a port can't be
15681594
* opened after we removed the port struct from ports_list

‎drivers/scsi/virtio_scsi.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ static void __virtscsi_set_affinity(struct virtio_scsi *vscsi, bool affinity)
751751

752752
vscsi->affinity_hint_set = true;
753753
} else {
754-
for (i = 0; i < vscsi->num_queues - VIRTIO_SCSI_VQ_BASE; i++)
754+
for (i = 0; i < vscsi->num_queues; i++)
755755
virtqueue_set_affinity(vscsi->req_vqs[i].vq, -1);
756756

757757
vscsi->affinity_hint_set = false;

0 commit comments

Comments
 (0)