Skip to contentSkip to conversation

pci: Only unmap bus_master_enabled_region if was added previously

PATCH Applied Alexey Kardashevskiy 6 participants 7 messages Mar 31, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pci: Only unmap bus_master_enabled_region if was added previously

Normally pci_init_bus_master() would be called either via bus->machine_done.notify or directly from do_pci_register_device().

However if a device's realize() failed, pci_init_bus_master() is not called, and do_pci_unregister_device() fails on memory_region_del_subregion() as it was not mapped.

This adds a check that subregion was mapped before unmapping it.

Fixes c53598ed18e4 ("pci: Add missing drop of bus master AS reference")

---
This is to pass iotest 051 which does run QEMU like this:

ppc64-softmmu/qemu-system-ppc64 -nodefaults -machine accel=qtest -nographic -monitor stdio -serial none -drive if=virtio

which normally fails with:

qemu-system-ppc64: -drive if=virtio: Device needs media, but drive is empty

or asserts (without this patch):

qemu-system-ppc64: /home/aik/p/qemu-kvm/memory.c:2118: memory_region_del_subregion: Assertion `subregion->container == mr' failed.

---
 hw/pci/pci.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
hw/pci/pci.c +4 -2
index bd8043c460..259483b1c0 100644
@@ -869,8 +869,10 @@ static void do_pci_unregister_device(PCIDevice *pci_dev)
     pci_dev->bus->devices[pci_dev->devfn] = NULL;
     pci_config_free(pci_dev);
 
-    memory_region_del_subregion(&pci_dev->bus_master_container_region,
-                                &pci_dev->bus_master_enable_region);
+    if (memory_region_is_mapped(&pci_dev->bus_master_enable_region)) {
+        memory_region_del_subregion(&pci_dev->bus_master_container_region,
+                                    &pci_dev->bus_master_enable_region);
+    }
     address_space_destroy(&pci_dev->bus_master_as);
 }
 
Signature · 1 line
2.11.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 33 lines · Alexey Kardashevskiy

Normally pci_init_bus_master() would be called either via bus->machine_done.notify or directly from do_pci_register_device().

However if a device's realize() failed, pci_init_bus_master() is not called, and do_pci_unregister_device() fails on memory_region_del_subregion() as it was not mapped.

This adds a check that subregion was mapped before unmapping it.

Fixes c53598ed18e4 ("pci: Add missing drop of bus master AS reference")

---
This is to pass iotest 051 which does run QEMU like this:

ppc64-softmmu/qemu-system-ppc64 -nodefaults -machine accel=qtest -nographic -monitor stdio -serial none -drive if=virtio

which normally fails with:

qemu-system-ppc64: -drive if=virtio: Device needs media, but drive is empty

or asserts (without this patch):

qemu-system-ppc64: /home/aik/p/qemu-kvm/memory.c:2118: memory_region_del_subregion: Assertion `subregion->container == mr' failed.

---
 hw/pci/pci.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
hw/pci/pci.c +0 -0
index bd8043c460..259483b1c0 100644
@@ -869,8 +869,10 @@ static void do_pci_unregister_device(PCIDevice *pci_dev)
     pci_dev->bus->devices[pci_dev->devfn] = NULL;

hw/pci/pci.c static void do_pci_unregister_device(PCIDevice *pci_dev) · line 877

Quoted 10 lines
hw/pci/pci.c +4 -2
     pci_config_free(pci_dev);
 
-    memory_region_del_subregion(&pci_dev->bus_master_container_region,
-                                &pci_dev->bus_master_enable_region);
+    if (memory_region_is_mapped(&pci_dev->bus_master_enable_region)) {
+        memory_region_del_subregion(&pci_dev->bus_master_container_region,
+                                    &pci_dev->bus_master_enable_region);
+    }
     address_space_destroy(&pci_dev->bus_master_as);
 }

I'm not sure whether it's generally assumed to be safe to call memory_region_is_mapped() on "uninitialized" memory regions (bus_master_enable_region is just completely zeroed out before pci_init_bus_master(), as far as I can see and guess), but it certainly works in practice, so:

#2 lore raw
Quoted 51 lines · Max Reitz
Quoted 33 lines · Alexey Kardashevskiy

Normally pci_init_bus_master() would be called either via bus->machine_done.notify or directly from do_pci_register_device().

However if a device's realize() failed, pci_init_bus_master() is not called, and do_pci_unregister_device() fails on memory_region_del_subregion() as it was not mapped.

This adds a check that subregion was mapped before unmapping it.

Fixes c53598ed18e4 ("pci: Add missing drop of bus master AS reference")

---
This is to pass iotest 051 which does run QEMU like this:

ppc64-softmmu/qemu-system-ppc64 -nodefaults -machine accel=qtest -nographic -monitor stdio -serial none -drive if=virtio

which normally fails with:

qemu-system-ppc64: -drive if=virtio: Device needs media, but drive is empty

or asserts (without this patch):

qemu-system-ppc64: /home/aik/p/qemu-kvm/memory.c:2118: memory_region_del_subregion: Assertion `subregion->container == mr' failed.

---
 hw/pci/pci.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
hw/pci/pci.c +0 -0
index bd8043c460..259483b1c0 100644
@@ -869,8 +869,10 @@ static void do_pci_unregister_device(PCIDevice *pci_dev)
     pci_dev->bus->devices[pci_dev->devfn] = NULL;

hw/pci/pci.c static void do_pci_unregister_device(PCIDevice *pci_dev) · line 877

Quoted 10 lines
hw/pci/pci.c +4 -2
     pci_config_free(pci_dev);
 
-    memory_region_del_subregion(&pci_dev->bus_master_container_region,
-                                &pci_dev->bus_master_enable_region);
+    if (memory_region_is_mapped(&pci_dev->bus_master_enable_region)) {
+        memory_region_del_subregion(&pci_dev->bus_master_container_region,
+                                    &pci_dev->bus_master_enable_region);
+    }
     address_space_destroy(&pci_dev->bus_master_as);
 }

I'm not sure whether it's generally assumed to be safe to call memory_region_is_mapped() on "uninitialized" memory regions (bus_master_enable_region is just completely zeroed out before pci_init_bus_master(), as far as I can see and guess), but it certainly works in practice, so:

Quoted 1 line

PCI device is also initialized via object_initialize() which does memset(0) so I assume it is quite safe.

Signature · 1 line
Alexey
Reviewed-by on v1 1/1
#3 lore raw
In reply to Alexey Kardashevskiy
Quoted 35 lines · Alexey Kardashevskiy

Normally pci_init_bus_master() would be called either via bus->machine_done.notify or directly from do_pci_register_device().

However if a device's realize() failed, pci_init_bus_master() is not called, and do_pci_unregister_device() fails on memory_region_del_subregion() as it was not mapped.

This adds a check that subregion was mapped before unmapping it.

Fixes c53598ed18e4 ("pci: Add missing drop of bus master AS reference")

---
This is to pass iotest 051 which does run QEMU like this:

ppc64-softmmu/qemu-system-ppc64 -nodefaults -machine accel=qtest -nographic -monitor stdio -serial none -drive if=virtio

which normally fails with:

qemu-system-ppc64: -drive if=virtio: Device needs media, but drive is empty

or asserts (without this patch):

qemu-system-ppc64: /home/aik/p/qemu-kvm/memory.c:2118: memory_region_del_subregion: Assertion `subregion->container == mr' failed.

---
 hw/pci/pci.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
hw/pci/pci.c +0 -0
index bd8043c460..259483b1c0 100644
@@ -869,8 +869,10 @@ static void do_pci_unregister_device(PCIDevice *pci_dev)
     pci_dev->bus->devices[pci_dev->devfn] = NULL;
     pci_config_free(pci_dev);

hw/pci/pci.c static void do_pci_unregister_device(PCIDevice *pci_dev) · line 878

Quoted 10 lines
hw/pci/pci.c +4 -2
 
-    memory_region_del_subregion(&pci_dev->bus_master_container_region,
-                                &pci_dev->bus_master_enable_region);
+    if (memory_region_is_mapped(&pci_dev->bus_master_enable_region)) {
+        memory_region_del_subregion(&pci_dev->bus_master_container_region,
+                                    &pci_dev->bus_master_enable_region);
+    }
     address_space_destroy(&pci_dev->bus_master_as);
 }
 

Hi,

Thanks,
Marcel

Tested-by on v1 1/1
#4 lore raw
In reply to Alexey Kardashevskiy
Quoted 35 lines · Alexey Kardashevskiy

Normally pci_init_bus_master() would be called either via bus->machine_done.notify or directly from do_pci_register_device().

However if a device's realize() failed, pci_init_bus_master() is not called, and do_pci_unregister_device() fails on memory_region_del_subregion() as it was not mapped.

This adds a check that subregion was mapped before unmapping it.

Fixes c53598ed18e4 ("pci: Add missing drop of bus master AS reference")

---
This is to pass iotest 051 which does run QEMU like this:

ppc64-softmmu/qemu-system-ppc64 -nodefaults -machine accel=qtest -nographic -monitor stdio -serial none -drive if=virtio

which normally fails with:

qemu-system-ppc64: -drive if=virtio: Device needs media, but drive is empty

or asserts (without this patch):

qemu-system-ppc64: /home/aik/p/qemu-kvm/memory.c:2118: memory_region_del_subregion: Assertion `subregion->container == mr' failed.

---
 hw/pci/pci.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
hw/pci/pci.c +0 -0
index bd8043c460..259483b1c0 100644
@@ -869,8 +869,10 @@ static void do_pci_unregister_device(PCIDevice *pci_dev)
     pci_dev->bus->devices[pci_dev->devfn] = NULL;
     pci_config_free(pci_dev);

hw/pci/pci.c static void do_pci_unregister_device(PCIDevice *pci_dev) · line 878

Quoted 10 lines
hw/pci/pci.c +4 -2
 
-    memory_region_del_subregion(&pci_dev->bus_master_container_region,
-                                &pci_dev->bus_master_enable_region);
+    if (memory_region_is_mapped(&pci_dev->bus_master_enable_region)) {
+        memory_region_del_subregion(&pci_dev->bus_master_container_region,
+                                    &pci_dev->bus_master_enable_region);
+    }
     address_space_destroy(&pci_dev->bus_master_as);
 }
 

Thanks for the quick turnaround!

Acked-by
#5 lore raw
Quoted 53 lines · Alexey Kardashevskiy
Quoted 39 lines

Normally pci_init_bus_master() would be called either via bus->machine_done.notify or directly from do_pci_register_device().

However if a device's realize() failed, pci_init_bus_master() is not called, and do_pci_unregister_device() fails on memory_region_del_subregion() as it was not mapped.

This adds a check that subregion was mapped before unmapping it.

Fixes c53598ed18e4 ("pci: Add missing drop of bus master AS reference")

---
This is to pass iotest 051 which does run QEMU like this:

ppc64-softmmu/qemu-system-ppc64 -nodefaults -machine accel=qtest -nographic -monitor stdio -serial none -drive if=virtio

which normally fails with:

qemu-system-ppc64: -drive if=virtio: Device needs media, but drive is empty

or asserts (without this patch):

qemu-system-ppc64: /home/aik/p/qemu-kvm/memory.c:2118:
memory_region_del_subregion: Assertion `subregion->container == mr' failed.

---
 hw/pci/pci.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
hw/pci/pci.c +0 -0
index bd8043c460..259483b1c0 100644
@@ -869,8 +869,10 @@ static void do_pci_unregister_device(PCIDevice
 *pci_dev)
     pci_dev->bus->devices[pci_dev->devfn] = NULL;

hw/pci/pci.c static void do_pci_unregister_device(PCIDevice · line 878

Quoted 10 lines
hw/pci/pci.c +4 -2
     pci_config_free(pci_dev);
 
-    memory_region_del_subregion(&pci_dev->bus_master_container_region,
-                                &pci_dev->bus_master_enable_region);
+    if (memory_region_is_mapped(&pci_dev->bus_master_enable_region)) {
+        memory_region_del_subregion(&pci_dev->bus_master_container_region,
+                                    &pci_dev->bus_master_enable_region);
+    }
     address_space_destroy(&pci_dev->bus_master_as);
 }

Thanks for the quick turnaround!

Quoted 1 line
#6 lore raw
In reply to Alexey Kardashevskiy
Quoted 11 lines · Alexey Kardashevskiy

Normally pci_init_bus_master() would be called either via bus->machine_done.notify or directly from do_pci_register_device().

However if a device's realize() failed, pci_init_bus_master() is not called, and do_pci_unregister_device() fails on memory_region_del_subregion() as it was not mapped.

This adds a check that subregion was mapped before unmapping it.

Fixes c53598ed18e4 ("pci: Add missing drop of bus master AS reference")

Applied, thanks everyone.

Quoted 35 lines

---
This is to pass iotest 051 which does run QEMU like this:

ppc64-softmmu/qemu-system-ppc64 -nodefaults -machine accel=qtest -nographic -monitor stdio -serial none -drive if=virtio

which normally fails with:

qemu-system-ppc64: -drive if=virtio: Device needs media, but drive is empty

or asserts (without this patch):

qemu-system-ppc64: /home/aik/p/qemu-kvm/memory.c:2118: memory_region_del_subregion: Assertion `subregion->container == mr' failed.

---
 hw/pci/pci.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
hw/pci/pci.c +4 -2
index bd8043c460..259483b1c0 100644
@@ -869,8 +869,10 @@ static void do_pci_unregister_device(PCIDevice *pci_dev)
     pci_dev->bus->devices[pci_dev->devfn] = NULL;
     pci_config_free(pci_dev);
 
-    memory_region_del_subregion(&pci_dev->bus_master_container_region,
-                                &pci_dev->bus_master_enable_region);
+    if (memory_region_is_mapped(&pci_dev->bus_master_enable_region)) {
+        memory_region_del_subregion(&pci_dev->bus_master_container_region,
+                                    &pci_dev->bus_master_enable_region);
+    }
     address_space_destroy(&pci_dev->bus_master_as);
 }
 
Signature · 1 line
2.11.0