Skip to contentSkip to conversation

virtio: Add corresponding memory_listener_unregister to unrealize

PATCH Superseded Eugenio Pérez 3 participants 6 messages Jan 22, 2021 – Jan 25, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 virtio: Add corresponding memory_listener_unregister to unrealize

Cannot destroy address spaces of IOMMU-aware virtio devices without it, since they can contain memory listeners.

Fixes c611c76417f ("virtio: add MemoryListener to cache ring translations")
---
 hw/virtio/virtio.c | 1 +
 1 file changed, 1 insertion(+)
hw/virtio/virtio.c +1 -0
index b308026596..67efd2c301 100644
@@ -3680,6 +3680,7 @@ static void virtio_device_unrealize(DeviceState *dev)
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
 
+    memory_listener_unregister(&vdev->listener);
     virtio_bus_device_unplugged(vdev);
 
     if (vdc->unrealize != NULL) {
Signature · 1 line
2.27.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 25 lines · Eugenio Pérez

Cannot destroy address spaces of IOMMU-aware virtio devices without it, since they can contain memory listeners.

Fixes c611c76417f ("virtio: add MemoryListener to cache ring translations")
---
 hw/virtio/virtio.c | 1 +
 1 file changed, 1 insertion(+)
hw/virtio/virtio.c +1 -0
index b308026596..67efd2c301 100644
@@ -3680,6 +3680,7 @@ static void virtio_device_unrealize(DeviceState *dev)
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
 
+    memory_listener_unregister(&vdev->listener);
     virtio_bus_device_unplugged(vdev);
 
     if (vdc->unrealize != NULL) {
Signature · 1 line
2.27.0

CC stable, assuming that's what we need too.

Thanks,

Signature · 1 line
Peter Xu
#2 lore raw
In reply to Eugenio Pérez
Quoted 2 lines · Eugenio Pérez

Cannot destroy address spaces of IOMMU-aware virtio devices without it, since they can contain memory listeners.

It's better to explain why the one in finalize doesn't work here.

Thanks

Quoted 20 lines
Fixes c611c76417f ("virtio: add MemoryListener to cache ring translations")
---
  hw/virtio/virtio.c | 1 +
  1 file changed, 1 insertion(+)
hw/virtio/virtio.c +1 -0
index b308026596..67efd2c301 100644
@@ -3680,6 +3680,7 @@ static void virtio_device_unrealize(DeviceState *dev)
      VirtIODevice *vdev = VIRTIO_DEVICE(dev);
      VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
 
+    memory_listener_unregister(&vdev->listener);
      virtio_bus_device_unplugged(vdev);
 
      if (vdc->unrealize != NULL) {
#3 lore raw
Quoted 9 lines · Jason Wang
Quoted 2 lines · Eugenio Pérez

Cannot destroy address spaces of IOMMU-aware virtio devices without it, since they can contain memory listeners.

It's better to explain why the one in finalize doesn't work here.

Hi Jason! Good point. The other call is at virtio_device_instance_finalize.

Function virtio_device_instance_finalize is called after address_space_destroy if we follow steps of buglink.

Address_space_destroy is called by pci_qdev_unrealize/do_pci_unregister_device, after call to virtio_device_unrealize. After that call, virtio_device_instance_finalize is called through object_deinit, freeing the bus.

Also, memory_listener_unregister can be called again because it checks for listener->address_space != NULL at start, and sets it to NULL at end.

In regular shutdown, nothing of this is called, so maybe we could safely delete the call to memory_listener_unregister at virtio_device_instance_finalize?

If not, should I send again the patch with a new commit message?

Thanks!

Quoted 24 lines

Thanks

Quoted 20 lines
Fixes c611c76417f ("virtio: add MemoryListener to cache ring translations")
---
  hw/virtio/virtio.c | 1 +
  1 file changed, 1 insertion(+)
hw/virtio/virtio.c +1 -0
index b308026596..67efd2c301 100644
@@ -3680,6 +3680,7 @@ static void virtio_device_unrealize(DeviceState *dev)
      VirtIODevice *vdev = VIRTIO_DEVICE(dev);
      VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
 
+    memory_listener_unregister(&vdev->listener);
      virtio_bus_device_unplugged(vdev);
 
      if (vdc->unrealize != NULL) {
#4 lore raw
Quoted 22 lines · Eugenio Perez Martin
Quoted 9 lines · Jason Wang
Quoted 2 lines · Eugenio Pérez

Cannot destroy address spaces of IOMMU-aware virtio devices without it, since they can contain memory listeners.

It's better to explain why the one in finalize doesn't work here.

Hi Jason! Good point. The other call is at virtio_device_instance_finalize.

Function virtio_device_instance_finalize is called after address_space_destroy if we follow steps of buglink.

Address_space_destroy is called by pci_qdev_unrealize/do_pci_unregister_device, after call to virtio_device_unrealize. After that call, virtio_device_instance_finalize is called through object_deinit, freeing the bus.

Quoted 7 lines

Also, memory_listener_unregister can be called again because it checks for listener->address_space != NULL at start, and sets it to NULL at end.

In regular shutdown, nothing of this is called, so maybe we could safely delete the call to memory_listener_unregister at virtio_device_instance_finalize?

I didn't notice this; if so we'd better remove that call if it's destined to be a noop after all.

Quoted 2 lines

If not, should I send again the patch with a new commit message?

Maybe attach the full backtrace too along with above? The assertion itself could be a very good explanation of what's happened.

Thanks,

Signature · 1 line
Peter Xu
#5 lore raw
Quoted 32 lines · Peter Xu
Quoted 22 lines · Eugenio Perez Martin
Quoted 9 lines · Jason Wang
Quoted 2 lines · Eugenio Pérez

Cannot destroy address spaces of IOMMU-aware virtio devices without it, since they can contain memory listeners.

It's better to explain why the one in finalize doesn't work here.

Hi Jason! Good point. The other call is at virtio_device_instance_finalize.

Function virtio_device_instance_finalize is called after address_space_destroy if we follow steps of buglink.

Address_space_destroy is called by pci_qdev_unrealize/do_pci_unregister_device, after call to virtio_device_unrealize. After that call, virtio_device_instance_finalize is called through object_deinit, freeing the bus.

Quoted 7 lines

Also, memory_listener_unregister can be called again because it checks for listener->address_space != NULL at start, and sets it to NULL at end.

In regular shutdown, nothing of this is called, so maybe we could safely delete the call to memory_listener_unregister at virtio_device_instance_finalize?

Quoted 9 lines

I didn't notice this; if so we'd better remove that call if it's destined to be a noop after all.

Quoted 2 lines

If not, should I send again the patch with a new commit message?

Maybe attach the full backtrace too along with above? The assertion itself could be a very good explanation of what's happened.

I agree, sending v2.

Thanks!

Quoted 5 lines

Thanks,

--
Peter Xu