Skip to contentSkip to conversation

virtio: unbreak virtio-pci with IOMMU after caching ring translations

PATCH In review Jason Wang 3 participants 5 messages Mar 1, 2017 – Mar 3, 2017

lore
Share & tools
lore

Jason Wang

author
#0 lore raw

PATCH 1/1 virtio: unbreak virtio-pci with IOMMU after caching ring translations

Commit c611c76417f5 ("virtio: add MemoryListener to cache ring translations") registers a memory listener to dma_as. This may not work when IOMMU is enabled: dma_as(bus_master_as) were initialized in pcibus_machine_done() after virtio_realize(). This will cause a segfault. Fixing this by using pci_device_iommu_address_space() instead to make sure address space were initialized at this time.

With this fix, IOMMU device were required to be initialized before any virtio-pci devices.

Fixes c611c76417f5 ("virtio: add MemoryListener to cache ring translations")
Cc Paolo Bonzini <[email protected]>
---
 hw/virtio/virtio-pci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/virtio/virtio-pci.c +1 -1
index 5ce42af..b76f3f6 100644
@@ -1153,7 +1153,7 @@ static AddressSpace *virtio_pci_get_dma_as(DeviceState *d)
     VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
     PCIDevice *dev = &proxy->pci_dev;
 
-    return pci_get_address_space(dev);
+    return pci_device_iommu_address_space(dev);
 }
 
 static int virtio_pci_add_mem_cap(VirtIOPCIProxy *proxy,
Signature · 1 line
2.7.4
#1 lore raw
Quoted 7 lines · Jason Wang

Commit c611c76417f5 ("virtio: add MemoryListener to cache ring translations") registers a memory listener to dma_as. This may not work when IOMMU is enabled: dma_as(bus_master_as) were initialized in pcibus_machine_done() after virtio_realize(). This will cause a segfault. Fixing this by using pci_device_iommu_address_space() instead to make sure address space were initialized at this time.

Quoted 6 lines

With this fix, IOMMU device were required to be initialized before any virtio-pci devices.

Fixes c611c76417f5 ("virtio: add MemoryListener to cache ring translations")
Cc Paolo Bonzini <[email protected]>

This is very ugly. I guess it's better than broken IOMMU ... Paolo?

Quoted 19 lines
---
 hw/virtio/virtio-pci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/virtio/virtio-pci.c +1 -1
index 5ce42af..b76f3f6 100644
@@ -1153,7 +1153,7 @@ static AddressSpace *virtio_pci_get_dma_as(DeviceState *d)
     VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
     PCIDevice *dev = &proxy->pci_dev;
 
-    return pci_get_address_space(dev);
+    return pci_device_iommu_address_space(dev);
 }
 
 static int virtio_pci_add_mem_cap(VirtIOPCIProxy *proxy,
Signature · 1 line
2.7.4
author
#2 lore raw
Quoted 8 lines · Michael S. Tsirkin
Quoted 7 lines · Jason Wang

Commit c611c76417f5 ("virtio: add MemoryListener to cache ring translations") registers a memory listener to dma_as. This may not work when IOMMU is enabled: dma_as(bus_master_as) were initialized in pcibus_machine_done() after virtio_realize(). This will cause a segfault. Fixing this by using pci_device_iommu_address_space() instead to make sure address space were initialized at this time.

Quoted 8 lines
Quoted 6 lines

With this fix, IOMMU device were required to be initialized before any virtio-pci devices.

Fixes c611c76417f5 ("virtio: add MemoryListener to cache ring translations")
Cc Paolo Bonzini <[email protected]>

This is very ugly. I guess it's better than broken IOMMU ... Paolo?

Maybe we can delay the registering of memory listener on bus master enabling or status setting.

Thanks

Quoted 20 lines
Quoted 20 lines
---
  hw/virtio/virtio-pci.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
hw/virtio/virtio-pci.c +1 -1
index 5ce42af..b76f3f6 100644
@@ -1153,7 +1153,7 @@ static AddressSpace *virtio_pci_get_dma_as(DeviceState *d)
      VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
      PCIDevice *dev = &proxy->pci_dev;
 
-    return pci_get_address_space(dev);
+    return pci_device_iommu_address_space(dev);
  }
 
  static int virtio_pci_add_mem_cap(VirtIOPCIProxy *proxy,
Signature · 1 line
2.7.4
#3 lore raw
Quoted 10 lines · Jason Wang
Quoted 7 lines
Quoted 5 lines
Fixes c611c76417f5 ("virtio: add MemoryListener to cache ring

translations")

Cc Paolo Bonzini <[email protected]>

This is very ugly. I guess it's better than broken IOMMU ... Paolo?

Maybe we can delay the registering of memory listener on bus master enabling or status setting.

Can we add a callback to PCIDeviceClass, and invoke it from pci_init_bus_master?

Paolo

author
#4 lore raw
Quoted 6 lines · Paolo Bonzini
Quoted 4 lines · Jason Wang
Quoted 4 lines
Quoted 4 lines
Fixes c611c76417f5 ("virtio: add MemoryListener to cache ring

translations")

Cc Paolo Bonzini <[email protected]>
Quoted 9 lines
Quoted 4 lines
Quoted 2 lines

This is very ugly. I guess it's better than broken IOMMU ... Paolo?

Maybe we can delay the registering of memory listener on bus master enabling or status setting.

Can we add a callback to PCIDeviceClass, and invoke it from pci_init_bus_master?

Paolo

This looks pci specific, I post a fix that reset the dma_as during status set which can solve this issue too. Please have a look.

Thanks