Skip to contentSkip to conversation

pci: batch memory transactions around mapping updates

PATCH New Denis V. Lunev 1 participant 3 messages Sep 3

lore
Share & tools
lore
#0 lore raw

cover pci: batch memory transactions around mapping updates

Restoring a PCI device's config space remaps its BARs, updates the bridge windows when it is a bridge, and toggles the bus master region. Each of those ends a memory transaction of its own, and a transaction commit re-renders the flatviews it touches, at a cost which grows with the number of memory regions in the machine.

So a machine with a rich PCIe topology pays that render many times over while an incoming migration stream is read, once or more per device, even though nothing between the steps looks at an address space.

Both patches wrap an existing sequence in a single transaction. There is no new API and no behaviour a guest can observe: each transaction opens and closes inside the function that starts it, so nothing sees an intermediate state which was not already reachable. pci_bridge_update_mappings() has worked this way for the bridge windows for a long time; the first patch just makes pci_update_mappings() consistent with it.

Measured on a Windows Server 2022 guest with 64 vCPUs and 65 pcie-root-ports, the non-iterable part of an incoming migration drops by about a third. The saving scales with the number of PCI devices, so it is small for a plain machine and large for the topologies which hurt today.

Note that pci_update_mappings() is also reached from the runtime config write path, not only from migration. Several BAR moves in one config write now share a rebuild instead of taking one each, which is the same win in miniature and equally invisible to the guest.

CC Michael S. Tsirkin <[email protected]>

Denis V. Lunev (2):
pci: batch BAR remapping into one memory transaction
pci: load a device's config inside one memory transaction

 hw/pci/pci.c | 8 ++++++++
 1 file changed, 8 insertions(+)
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/2 pci: batch BAR remapping into one memory transaction

From: Denis V. Lunev <[email protected]>

pci_update_mappings() deletes and re-adds a subregion for each BAR whose address changed, and both of those commit their own transaction, so one BAR move costs two full flatview rebuilds.

Nothing between the delete and the add touches an address space, so the walk can run inside a single transaction. pci_bridge_update_mappings() already does this for the bridge windows.

CC Michael S. Tsirkin <[email protected]>
---
 hw/pci/pci.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/pci/pci.c +4 -0
index c15f2b9f08..14c78e3c62 100644
@@ -1742,6 +1742,8 @@ static void pci_update_mappings(PCIDevice *d)
     int i;
     pcibus_t new_addr;
 
+    memory_region_transaction_begin();
+
     for(i = 0; i < PCI_NUM_REGIONS; i++) {
         r = &d->io_regions[i];
 
@@ -1778,6 +1780,8 @@ static void pci_update_mappings(PCIDevice *d)
     }
 
     pci_update_vga(d);
+
+    memory_region_transaction_commit();
 }
 
 int pci_irq_disabled(PCIDevice *d)
Signature · 1 line
2.53.0
#2 lore raw

PATCH 2/2 pci: load a device's config inside one memory transaction

In reply to Denis V. Lunev

From: Denis V. Lunev <[email protected]>

Restoring config space remaps the BARs, updates the bridge windows when the device is a bridge, and toggles the bus master region. Each of those commits a transaction of its own, so one device costs several flatview rebuilds where one would do, and the cost of a rebuild grows with the number of regions in the machine.

Nothing between them touches an address space, so the three can share a transaction. The saving scales with the number of PCI devices.

CC Michael S. Tsirkin <[email protected]>
---
 hw/pci/pci.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/pci/pci.c +4 -0
index 14c78e3c62..0efb4eb4bb 100644
@@ -840,6 +840,8 @@ static int get_pci_config_device(QEMUFile *f, void *pv, size_t size,
     }
     memcpy(s->config, config, size);
 
+    memory_region_transaction_begin();
+
     pci_update_mappings(s);
     if (IS_PCI_BRIDGE(s)) {
         pci_bridge_update_mappings(PCI_BRIDGE(s));
@@ -848,6 +850,8 @@ static int get_pci_config_device(QEMUFile *f, void *pv, size_t size,
     pci_set_master(s, pci_get_word(s->config + PCI_COMMAND)
                       & PCI_COMMAND_MASTER);
 
+    memory_region_transaction_commit();
+
     g_free(config);
     return 0;
 }
Signature · 1 line
2.53.0