Skip to contentSkip to conversation

pci: Add missing drop of bus master AS reference

PATCH Applied Alexey Kardashevskiy 5 participants 6 messages Mar 27, 2017 – Mar 29, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pci: Add missing drop of bus master AS reference

The recent introduction of a bus master container added memory_region_add_subregion() into the PCI device registering path but missed memory_region_del_subregion() in the unregistering path leaving a reference to the root memory region of the new container.

This adds missing memory_region_del_subregion().

Fixes 3716d5902d743 ("pci: introduce a bus master container")
---
 hw/pci/pci.c | 2 ++
 1 file changed, 2 insertions(+)
hw/pci/pci.c +2 -0
index e6b08e1988..bd8043c460 100644
@@ -869,6 +869,8 @@ 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);
     address_space_destroy(&pci_dev->bus_master_as);
 }
 
Signature · 1 line
2.11.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 19 lines · Alexey Kardashevskiy

The recent introduction of a bus master container added memory_region_add_subregion() into the PCI device registering path but missed memory_region_del_subregion() in the unregistering path leaving a reference to the root memory region of the new container.

This adds missing memory_region_del_subregion().

Fixes 3716d5902d743 ("pci: introduce a bus master container")
---
 hw/pci/pci.c | 2 ++
 1 file changed, 2 insertions(+)
hw/pci/pci.c +0 -0
index e6b08e1988..bd8043c460 100644

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

Quoted 8 lines
hw/pci/pci.c +2 -0
@@ -869,6 +869,8 @@ 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);
     address_space_destroy(&pci_dev->bus_master_as);
 }
 

My own review fail. The enable subregion would be deleted when a memory region is finalized, but the enable subregions is keeping the owner alive. And until the owner is alive, the container region is not deleted either. So there is a reference count cycle, which we need to break.

It's probably good to revisit commit 2e2b8eb ("memory: allow destroying a non-empty MemoryRegion", 2015-10-01). For 2.9,

Paolo

Applied
#2 lore raw
Quoted 37 lines · Paolo Bonzini
Quoted 19 lines · Alexey Kardashevskiy

The recent introduction of a bus master container added memory_region_add_subregion() into the PCI device registering path but missed memory_region_del_subregion() in the unregistering path leaving a reference to the root memory region of the new container.

This adds missing memory_region_del_subregion().

Fixes 3716d5902d743 ("pci: introduce a bus master container")
---
 hw/pci/pci.c | 2 ++
 1 file changed, 2 insertions(+)
hw/pci/pci.c +0 -0
index e6b08e1988..bd8043c460 100644

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

Quoted 8 lines
hw/pci/pci.c +2 -0
@@ -869,6 +869,8 @@ 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);
     address_space_destroy(&pci_dev->bus_master_as);
 }
 

My own review fail. The enable subregion would be deleted when a memory region is finalized, but the enable subregions is keeping the owner alive. And until the owner is alive, the container region is not deleted either. So there is a reference count cycle, which we need to break.

Quoted 4 lines

It's probably good to revisit commit 2e2b8eb ("memory: allow destroying a non-empty MemoryRegion", 2015-10-01). For 2.9,

Merged to ppc-for-2.9.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#3 lore raw
Quoted 38 lines · David Gibson
Quoted 37 lines · Paolo Bonzini
Quoted 19 lines · Alexey Kardashevskiy

The recent introduction of a bus master container added memory_region_add_subregion() into the PCI device registering path but missed memory_region_del_subregion() in the unregistering path leaving a reference to the root memory region of the new container.

This adds missing memory_region_del_subregion().

Fixes 3716d5902d743 ("pci: introduce a bus master container")
---
 hw/pci/pci.c | 2 ++
 1 file changed, 2 insertions(+)
hw/pci/pci.c +0 -0
index e6b08e1988..bd8043c460 100644

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

Quoted 8 lines
hw/pci/pci.c +2 -0
@@ -869,6 +869,8 @@ 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);
     address_space_destroy(&pci_dev->bus_master_as);
 }
 

My own review fail. The enable subregion would be deleted when a memory region is finalized, but the enable subregions is keeping the owner alive. And until the owner is alive, the container region is not deleted either. So there is a reference count cycle, which we need to break.

Quoted 6 lines
Quoted 4 lines

It's probably good to revisit commit 2e2b8eb ("memory: allow destroying a non-empty MemoryRegion", 2015-10-01). For 2.9,

Merged to ppc-for-2.9.

I have it on pci branch, seems more appropriate.

Quoted 5 lines
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#4 lore raw
Quoted 39 lines · Michael S. Tsirkin
Quoted 38 lines · David Gibson
Quoted 37 lines · Paolo Bonzini
Quoted 19 lines · Alexey Kardashevskiy

The recent introduction of a bus master container added memory_region_add_subregion() into the PCI device registering path but missed memory_region_del_subregion() in the unregistering path leaving a reference to the root memory region of the new container.

This adds missing memory_region_del_subregion().

Fixes 3716d5902d743 ("pci: introduce a bus master container")
---
 hw/pci/pci.c | 2 ++
 1 file changed, 2 insertions(+)
hw/pci/pci.c +0 -0
index e6b08e1988..bd8043c460 100644

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

Quoted 8 lines
hw/pci/pci.c +2 -0
@@ -869,6 +869,8 @@ 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);
     address_space_destroy(&pci_dev->bus_master_as);
 }
 

My own review fail. The enable subregion would be deleted when a memory region is finalized, but the enable subregions is keeping the owner alive. And until the owner is alive, the container region is not deleted either. So there is a reference count cycle, which we need to break.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

It's probably good to revisit commit 2e2b8eb ("memory: allow destroying a non-empty MemoryRegion", 2015-10-01). For 2.9,

Merged to ppc-for-2.9.

I have it on pci branch, seems more appropriate.

Good point. I've dropped it from my tree.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
Acked-by on v1 1/1
#5 lore raw
In reply to Alexey Kardashevskiy
Quoted 18 lines · Alexey Kardashevskiy

The recent introduction of a bus master container added memory_region_add_subregion() into the PCI device registering path but missed memory_region_del_subregion() in the unregistering path leaving a reference to the root memory region of the new container.

This adds missing memory_region_del_subregion().

Fixes 3716d5902d743 ("pci: introduce a bus master container")
---
  hw/pci/pci.c | 2 ++
  1 file changed, 2 insertions(+)
hw/pci/pci.c +0 -0
index e6b08e1988..bd8043c460 100644

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

Quoted 8 lines
hw/pci/pci.c +2 -0
@@ -869,6 +869,8 @@ 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);
      address_space_destroy(&pci_dev->bus_master_as);
  }
 

Thanks!