Skip to contentSkip to conversation

iommu/virtio: Fix probe error handling

PATCH In review weimin xiong 2 participants 4 messages Jul 14 – Jul 15

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover iommu/virtio: Fix probe error handling

From: Xiong Weimin <[email protected]>

Hi,

This small series fixes two probe-time error handling issues in the virtio-iommu driver.

The first patch avoids dereferencing a device after dropping the reference returned by bus_find_device(). The second patch propagates failures from iommu_device_register() and unwinds the resources that were set up earlier in probe.

Xiong Weimin (2):
iommu/virtio: Avoid use-after-put in viommu_get_by_fwnode iommu/virtio: Handle iommu_device_register() failures

 drivers/iommu/virtio-iommu.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 iommu/virtio: Avoid use-after-put in viommu_get_by_fwnode

From: Xiong Weimin <[email protected]>

bus_find_device() returns a device reference that must be released with put_device(). viommu_get_by_fwnode() currently drops that reference before dereferencing the device to fetch the virtio-IOMMU private data.

Fetch the private data while the reference is still held, then release the device reference before returning.

---
 drivers/iommu/virtio-iommu.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/iommu/virtio-iommu.c +6 -2
index 587fc1319..342785c76 100644
@@ -1009,12 +1009,16 @@ static int viommu_match_node(struct device *dev, const void *data)
 
 static struct viommu_dev *viommu_get_by_fwnode(struct fwnode_handle *fwnode)
 {
+	struct viommu_dev *viommu = NULL;
 	struct device *dev = bus_find_device(virtio_bus_type, NULL, fwnode,
 					     viommu_match_node);
 
-	put_device(dev);
+	if (dev) {
+		viommu = dev_to_virtio(dev)->priv;
+		put_device(dev);
+	}
 
-	return dev ? dev_to_virtio(dev)->priv : NULL;
+	return viommu;
 }
 
 static struct iommu_device *viommu_probe_device(struct device *dev)
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/2 iommu/virtio: Handle iommu_device_register() failures

In reply to weimin xiong

From: Xiong Weimin <[email protected]>

iommu_device_register() returns an error when the IOMMU core fails to register the hardware instance or probe the buses. viommu_probe() currently ignores that error and continues as if the device was registered successfully.

Propagate the failure and unwind the sysfs entry and virtqueues that were set up earlier. Clear the driver data on the error path as well, since it is set before registration so bus probing can find the virtio-IOMMU instance.

---
 drivers/iommu/virtio-iommu.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/iommu/virtio-iommu.c +6 -1
index 342785c76..9118377d7 100644
@@ -1240,7 +1240,9 @@ static int viommu_probe(struct virtio_device *vdev)
 
 	vdev->priv = viommu;
 
-	iommu_device_register(&viommu->iommu, &viommu_ops, parent_dev);
+	ret = iommu_device_register(&viommu->iommu, &viommu_ops, parent_dev);
+	if (ret)
+		goto err_remove_sysfs;
 
 	dev_info(dev, "input address: %u bits\n",
 		 order_base_2(viommu->geometry.aperture_end));
@@ -1248,8 +1250,11 @@ static int viommu_probe(struct virtio_device *vdev)
 
 	return 0;
 
+err_remove_sysfs:
+	iommu_device_sysfs_remove(&viommu->iommu);
 err_free_vqs:
 	vdev->config->del_vqs(vdev);
+	vdev->priv = NULL;
 
 	return ret;
 }
Signature · 1 line
2.43.0
maintainer
#3 lore raw

Re: [PATCH v1 2/2] iommu/virtio: Handle iommu_device_register() failures

Quoted 22 lines · weimin xiong

From: Xiong Weimin <[email protected]>

iommu_device_register() returns an error when the IOMMU core fails to register the hardware instance or probe the buses. viommu_probe() currently ignores that error and continues as if the device was registered successfully.

Propagate the failure and unwind the sysfs entry and virtqueues that were set up earlier. Clear the driver data on the error path as well, since it is set before registration so bus probing can find the virtio-IOMMU instance.

---
 drivers/iommu/virtio-iommu.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/iommu/virtio-iommu.c +0 -0
index 342785c76..9118377d7 100644

drivers/iommu/virtio-iommu.c static int viommu_probe(struct virtio_device *vdev) · line 1245

Quoted 7 lines
drivers/iommu/virtio-iommu.c +3 -1
@@ -1240,7 +1240,9 @@ static int viommu_probe(struct virtio_device *vdev)
 
 	vdev->priv = viommu;
 
-	iommu_device_register(&viommu->iommu, &viommu_ops, parent_dev);
+	ret = iommu_device_register(&viommu->iommu, &viommu_ops, parent_dev);
+	if (ret)
+		goto err_remove_sysfs;

Given that the first patch shouldn't be needed (per Robin's comments at [1]) and this is augmented an error path that we shouldn't be hitting in practice, I don't think we need to treat this as a fix. You can probably just lump it in with your virtio-iommu hardening series.

Will