Skip to contentSkip to conversation

virtio-pci: fix memory MR cleanup for modern

PATCH In review Michael S. Tsirkin 2 participants 3 messages Jul 27, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 virtio-pci: fix memory MR cleanup for modern

Each memory_region_add_subregion must be paired with memory_region_del_subregion.

---
 hw/virtio/virtio-pci.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
hw/virtio/virtio-pci.c +15 -0
index db8f27c..c024161 100644
@@ -1414,6 +1414,13 @@ static void virtio_pci_modern_region_map(VirtIOPCIProxy *proxy,
     virtio_pci_add_mem_cap(proxy, cap);
 }
 
+static void virtio_pci_modern_region_unmap(VirtIOPCIProxy *proxy,
+                                           VirtIOPCIRegion *region)
+{
+    memory_region_del_subregion(&proxy->modern_bar,
+                                &region->mr);
+}
+
 /* This is called by virtio-bus just after the device is plugged. */
 static void virtio_pci_device_plugged(DeviceState *d, Error **errp)
 {
@@ -1520,8 +1527,16 @@ static void virtio_pci_device_plugged(DeviceState *d, Error **errp)
 static void virtio_pci_device_unplugged(DeviceState *d)
 {
     VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
+    bool modern = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN);
 
     virtio_pci_stop_ioeventfd(proxy);
+
+    if (modern) {
+        virtio_pci_modern_region_unmap(proxy, &proxy->common);
+        virtio_pci_modern_region_unmap(proxy, &proxy->isr);
+        virtio_pci_modern_region_unmap(proxy, &proxy->device);
+        virtio_pci_modern_region_unmap(proxy, &proxy->notify);
+    }
 }
 
 static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp)
Signature · 1 line
MST
Reviewed-by on v1 1/1
#1 lore raw
Quoted 15 lines · Michael S. Tsirkin
hw/virtio/virtio-pci.c +8 -0
+static void virtio_pci_modern_region_unmap(VirtIOPCIProxy *proxy,
+                                           VirtIOPCIRegion *region)
+{
+    memory_region_del_subregion(&proxy->modern_bar,
+                                &region->mr);
+}
+
 /* This is called by virtio-bus just after the device is plugged. */
 static void virtio_pci_device_plugged(DeviceState *d, Error **errp)
 {
@@ -1520,8 +1527,16 @@ static void virtio_pci_device_plugged(DeviceState *d, Error **errp)
 static void virtio_pci_device_unplugged(DeviceState *d)
 {
     VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
+    bool modern = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN);

hw/virtio/virtio-pci.c static void virtio_pci_device_plugged(DeviceState *d, Error **errp) · line 1540

Quoted 10 lines
hw/virtio/virtio-pci.c +7 -0
 
     virtio_pci_stop_ioeventfd(proxy);
+
+    if (modern) {
+        virtio_pci_modern_region_unmap(proxy, &proxy->common);
+        virtio_pci_modern_region_unmap(proxy, &proxy->isr);
+        virtio_pci_modern_region_unmap(proxy, &proxy->device);
+        virtio_pci_modern_region_unmap(proxy, &proxy->notify);
+    }
 }

Actually this is not necessary. memory_region_del_subregion is only needed inasmuch as it prevents further guest access to the region, so it's enough that the toplevel region (the modern_bar itself) is unmapped. The PCI core does that automatically.

That said, it's polite to unmap everything, so if you want this patch:

Paolo

#2 lore raw
Quoted 34 lines · Paolo Bonzini
Quoted 15 lines · Michael S. Tsirkin
hw/virtio/virtio-pci.c +8 -0
+static void virtio_pci_modern_region_unmap(VirtIOPCIProxy *proxy,
+                                           VirtIOPCIRegion *region)
+{
+    memory_region_del_subregion(&proxy->modern_bar,
+                                &region->mr);
+}
+
 /* This is called by virtio-bus just after the device is plugged. */
 static void virtio_pci_device_plugged(DeviceState *d, Error **errp)
 {
@@ -1520,8 +1527,16 @@ static void virtio_pci_device_plugged(DeviceState *d, Error **errp)
 static void virtio_pci_device_unplugged(DeviceState *d)
 {
     VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
+    bool modern = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN);

hw/virtio/virtio-pci.c static void virtio_pci_device_plugged(DeviceState *d, Error **errp) · line 1540

Quoted 10 lines
hw/virtio/virtio-pci.c +7 -0
 
     virtio_pci_stop_ioeventfd(proxy);
+
+    if (modern) {
+        virtio_pci_modern_region_unmap(proxy, &proxy->common);
+        virtio_pci_modern_region_unmap(proxy, &proxy->isr);
+        virtio_pci_modern_region_unmap(proxy, &proxy->device);
+        virtio_pci_modern_region_unmap(proxy, &proxy->notify);
+    }
 }

Actually this is not necessary. memory_region_del_subregion is only needed inasmuch as it prevents further guest access to the region, so it's enough that the toplevel region (the modern_bar itself) is unmapped. The PCI core does that automatically.

Quoted 3 lines

That said, it's polite to unmap everything, so if you want this patch:

After discussing on IRC the problem (which is that memory_region_add_subregion add a reference to the VirtioPCIProxy object, and you want to remove it), this is indeed the correct fix.

Paolo