Skip to contentSkip to conversation

Fix hot-unplug: device removal while port in use

PATCH In review Amit Shah 2 participants 7 messages Mar 2, 2011 – Mar 16, 2011

Subsystems (3)
lore
Share & tools
lore

Amit Shah

author
#0 lore raw

cover Fix hot-unplug: device removal while port in use

A crash was observed when a device gets removed while a port is in use. When the port gets removed, we tried to free vq buffers. The vq no longer exists at this stage, just ensure we don't access it.

The second patch fixes a warning where the pci region is already freed. I'm not sure what or how the region gets freed, any clues there will be helpful.

Thanks,
Amit

Amit Shah (2):
virtio: console: Don't access vqs if device was unplugged
virtio: console: Don't call device_destroy() on port device

 drivers/char/virtio_console.c |   10 +++++++++-
 1 files changed, 9 insertions(+), 1 deletions(-)
Signature · 1 line
1.7.4
author
#1 lore raw

PATCH 1/2 virtio: console: Don't access vqs if device was unplugged

If a virtio-console device gets unplugged while a port is open, a subsequent close() call on the port accesses vqs to free up buffers. This can lead to a crash.

The buffers are already freed up as a result of the call to unplug_ports() from virtcons_remove(). The fix is to simply not access vq information if port->portdev is NULL.

---
 drivers/char/virtio_console.c |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)
drivers/char/virtio_console.c +8 -0
index 4903931..84b164d 100644
@@ -388,6 +388,10 @@ static void discard_port_data(struct port *port)
 	unsigned int len;
 	int ret;
 
+	if (!port->portdev) {
+		/* Device has been unplugged.  vqs are already gone. */
+		return;
+	}
 	vq = port->in_vq;
 	if (port->inbuf)
 		buf = port->inbuf;
@@ -470,6 +474,10 @@ static void reclaim_consumed_buffers(struct port *port)
 	void *buf;
 	unsigned int len;
 
+	if (!port->portdev) {
+		/* Device has been unplugged.  vqs are already gone. */
+		return;
+	}
 	while ((buf = virtqueue_get_buf(port->out_vq, &len))) {
 		kfree(buf);
 		port->outvq_full = false;
Signature · 1 line
1.7.4
author
#2 lore raw

PATCH 2/2 virtio: console: Don't call device_destroy() on port device

In reply to Amit Shah

This results in a warning mentioning the region being removed is already gone.

---
 drivers/char/virtio_console.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/char/virtio_console.c +1 -1
index 84b164d..5ac9fd9 100644
@@ -1244,7 +1244,7 @@ static void remove_port(struct kref *kref)
 	port = container_of(kref, struct port, kref);
 
 	sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
-	device_destroy(pdrvdata.class, port->dev->devt);
+
 	cdev_del(port->cdev);
 
 	kfree(port->name);
Signature · 1 line
1.7.4
#3 lore raw
In reply to Amit Shah
Quoted 7 lines · Amit Shah

A crash was observed when a device gets removed while a port is in use. When the port gets removed, we tried to free vq buffers. The vq no longer exists at this stage, just ensure we don't access it.

The second patch fixes a warning where the pci region is already freed. I'm not sure what or how the region gets freed, any clues there will be helpful.

Put a printk and WARN_ON() in the pci region freeing code, look through the backtraces?

Rusty.

author
#4 lore raw
Quoted 11 lines · Rusty Russell
Quoted 7 lines · Amit Shah

A crash was observed when a device gets removed while a port is in use. When the port gets removed, we tried to free vq buffers. The vq no longer exists at this stage, just ensure we don't access it.

The second patch fixes a warning where the pci region is already freed. I'm not sure what or how the region gets freed, any clues there will be helpful.

Put a printk and WARN_ON() in the pci region freeing code, look through the backtraces?

Well what seems to be happening is kref_put() in port_fops_release() calls remove_port(), which calls device_destroy(). Now this triggers another fput() on the same file, causing port_fops_release() to be called again, which leads to device_destroy() being called on the same region.

Slightly more clueful, but still clueless as to why this happens.

Amit

author
#5 lore raw
In reply to Amit Shah
Quoted 3 lines · Amit Shah

The second patch fixes a warning where the pci region is already freed. I'm not sure what or how the region gets freed, any clues there will be helpful.

OK, so in the case where a virtio-console port is in use (opened by a program) and a virtio-console device is removed, the port is kept around but all the virtio-related state is assumed to be gone.

When the port is finally released (close() called), we call device_destroy() on the port's device. This results in the parent device's structures to be freed as well. This includes the PCI regions for the virtio-console PCI device.

Once this is done, however, virtio_pci_release_dev() kicks in, as the last ref to the virtio device is now gone, and attempts to do

     pci_iounmap(pci_dev, vp_dev->ioaddr);
     pci_release_regions(pci_dev);
     pci_disable_device(pci_dev);

which results in the double-free warning.

Ideally virtio_pci_release_dev() shouldn't be needed at all; all that work can be moved to virtio_pci_remove(). virtio_pci_release_dev() was added in 29f9f12e to curb a warning:

virtio: add PCI device release() function

Add a release() function for virtio_pci devices so as to avoid:

Device 'virtio0' does not have a release() function, it is broken and must be fixed

So we could have an empty release() function that does nothing, and all of the current functionality be moved to virtio_pci_remove(), as it was earlier. This should keep everyone happy.

Is that fine?

Amit

#6 lore raw
Quoted 12 lines · Amit Shah

Ideally virtio_pci_release_dev() shouldn't be needed at all; all that work can be moved to virtio_pci_remove(). virtio_pci_release_dev() was added in 29f9f12e to curb a warning:

virtio: add PCI device release() function

Add a release() function for virtio_pci devices so as to avoid:

Device 'virtio0' does not have a release() function, it is broken and must be fixed

Quoted 5 lines

So we could have an empty release() function that does nothing, and all of the current functionality be moved to virtio_pci_remove(), as it was earlier. This should keep everyone happy.

Is that fine?

Greg K-H needs to be asked this question, I think.

Cheers,
Rusty.