Fix bug in viommu_remove() by swapping out-of-order IOMMU unregistration
and sysfs removal steps.

Other IOMMU drivers all call iommu_device_unregister() before
iommu_device_sysfs_remove(). Before this change, viommu_remove() had
incorrectly reversed this ordering, leading to the following kernel
oops.

  BUG: kernel NULL pointer dereference, address: 0000000000000030
  ...snip...
  RIP: 0010:sysfs_remove_link_from_group+0x10/0x40
  ...snip..
  Call Trace:
   <TASK>
  ...snip...
   ? sysfs_remove_link_from_group+0x10/0x40
   iommu_deinit_device+0x2a/0xe0
   __iommu_group_remove_device+0xb0/0xe0
   ? __pfx_remove_iommu_group+0x10/0x10
   remove_iommu_group+0x35/0x60
   bus_for_each_dev+0x83/0xd0
   iommu_device_unregister+0x3d/0xb0
   viommu_remove+0x25/0x60
   virtio_dev_remove+0x3c/0x90
  ...snip...

iommu_device_sysfs_remove() will set iommu->dev = NULL after tearing
down the corresponding sysfs entry. Then iommu_device_unregister() will
iterate over every endpoint device attached to the IOMMU and reference
the null iommu->dev pointer, specifically via call
sysfs_remove_link_from_group(&iommu->dev->kobj, ...).

Fixes: edcd69ab9a32 ("iommu: Add virtio-iommu driver")
Signed-off-by: Cam Miller <[email protected]>
---
 drivers/iommu/virtio-iommu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/iommu/virtio-iommu.c b/drivers/iommu/virtio-iommu.c
index 587fc13197f1..a08474eeed58 100644
--- a/drivers/iommu/virtio-iommu.c
+++ b/drivers/iommu/virtio-iommu.c
@@ -1254,8 +1254,8 @@ static void viommu_remove(struct virtio_device *vdev)
 {
        struct viommu_dev *viommu = vdev->priv;
 
-       iommu_device_sysfs_remove(&viommu->iommu);
        iommu_device_unregister(&viommu->iommu);
+       iommu_device_sysfs_remove(&viommu->iommu);
 
        /* Stop all virtqueues */
        virtio_reset_device(vdev);
-- 
2.54.0


Reply via email to