Skip to contentSkip to conversation

vhost-vdpa: free iommu domain after last use during cleanup

PATCH In review Gautam Dawar 3 participants 3 messages Mar 1, 2023 – Mar 2, 2023

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

PATCH 1/1 vhost-vdpa: free iommu domain after last use during cleanup

Currently vhost_vdpa_cleanup() unmaps the DMA mappings by calling iommu_unmap(v->domain, map->start, map->size);
from vhost_vdpa_general_unmap() when the parent vDPA driver doesn't provide DMA config operations.

However, the IOMMU domain referred to by v->domain is freed in vhost_vdpa_free_domain() before vhost_vdpa_cleanup() in vhost_vdpa_release() which results in NULL pointer de-reference. Accordingly, moving the call to vhost_vdpa_free_domain() in vhost_vdpa_cleanup() would makes sense. This will also help detaching the dma device in error handling of vhost_vdpa_alloc_domain().

This issue was observed on terminating QEMU with SIGQUIT.

Fixes 037d4305569a ("vhost-vdpa: call vhost_vdpa_cleanup during the release")
---
 drivers/vhost/vdpa.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/vhost/vdpa.c +2 -1
index ec32f785dfde..b7657984dd8d 100644
@@ -1134,6 +1134,7 @@ static int vhost_vdpa_alloc_domain(struct vhost_vdpa *v)
 
 err_attach:
 	iommu_domain_free(v->domain);
+	v->domain = NULL;
 	return ret;
 }
 
@@ -1178,6 +1179,7 @@ static void vhost_vdpa_cleanup(struct vhost_vdpa *v)
 			vhost_vdpa_remove_as(v, asid);
 	}
 
+	vhost_vdpa_free_domain(v);
 	vhost_dev_cleanup(&v->vdev);
 	kfree(v->vdev.vqs);
 }
@@ -1250,7 +1252,6 @@ static int vhost_vdpa_release(struct inode *inode, struct file *filep)
 	vhost_vdpa_clean_irq(v);
 	vhost_vdpa_reset(v);
 	vhost_dev_stop(&v->vdev);
-	vhost_vdpa_free_domain(v);
 	vhost_vdpa_config_put(v);
 	vhost_vdpa_cleanup(v);
 	mutex_unlock(&d->mutex);
Signature · 1 line
2.30.1
Acked-by on v1 1/1
#1 lore raw
Quoted 13 lines · Gautam Dawar

Currently vhost_vdpa_cleanup() unmaps the DMA mappings by calling iommu_unmap(v->domain, map->start, map->size);
from vhost_vdpa_general_unmap() when the parent vDPA driver doesn't provide DMA config operations.

However, the IOMMU domain referred to by v->domain is freed in vhost_vdpa_free_domain() before vhost_vdpa_cleanup() in vhost_vdpa_release() which results in NULL pointer de-reference. Accordingly, moving the call to vhost_vdpa_free_domain() in vhost_vdpa_cleanup() would makes sense. This will also help detaching the dma device in error handling of vhost_vdpa_alloc_domain().

Quoted 4 lines

This issue was observed on terminating QEMU with SIGQUIT.

Fixes 037d4305569a ("vhost-vdpa: call vhost_vdpa_cleanup during the release")

Thanks

Quoted 33 lines
---
 drivers/vhost/vdpa.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/vhost/vdpa.c +2 -2
index ec32f785dfde..b7657984dd8d 100644
@@ -1134,6 +1134,7 @@ static int vhost_vdpa_alloc_domain(struct vhost_vdpa *v)
 
 err_attach:
        iommu_domain_free(v->domain);
+       v->domain = NULL;
        return ret;
 }
 
@@ -1178,6 +1179,7 @@ static void vhost_vdpa_cleanup(struct vhost_vdpa *v)
                        vhost_vdpa_remove_as(v, asid);
        }
 
+       vhost_vdpa_free_domain(v);
        vhost_dev_cleanup(&v->vdev);
        kfree(v->vdev.vqs);
 }
@@ -1250,7 +1252,6 @@ static int vhost_vdpa_release(struct inode *inode, struct file *filep)
        vhost_vdpa_clean_irq(v);
        vhost_vdpa_reset(v);
        vhost_dev_stop(&v->vdev);
-       vhost_vdpa_free_domain(v);
        vhost_vdpa_config_put(v);
        vhost_vdpa_cleanup(v);
        mutex_unlock(&d->mutex);
--
Quoted 2 lines

2.30.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

Reviewed-by on v1 1/1
#2 lore raw
In reply to Gautam Dawar
Quoted 11 lines · Gautam Dawar

Currently vhost_vdpa_cleanup() unmaps the DMA mappings by calling iommu_unmap(v->domain, map->start, map->size);
from vhost_vdpa_general_unmap() when the parent vDPA driver doesn't provide DMA config operations.

However, the IOMMU domain referred to by v->domain is freed in vhost_vdpa_free_domain() before vhost_vdpa_cleanup() in vhost_vdpa_release() which results in NULL pointer de-reference. Accordingly, moving the call to vhost_vdpa_free_domain() in vhost_vdpa_cleanup() would makes sense. This will also help detaching the dma device in error handling of vhost_vdpa_alloc_domain().

Yep, good cleanup!

Quoted 8 lines

This issue was observed on terminating QEMU with SIGQUIT.

Fixes 037d4305569a ("vhost-vdpa: call vhost_vdpa_cleanup during the release")

---
drivers/vhost/vdpa.c | 3 ++-

1 file changed, 2 insertions(+), 1 deletion(-)

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization