Skip to contentSkip to conversation

intel_iommu: Take the bql before registering a new address space

PATCH Superseded CLEMENT MATHIEU--DRIF 2 participants 2 messages Apr 15, 2025 – Apr 16, 2025

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 intel_iommu: Take the bql before registering a new address space

Address space creation might end up being called without holding the bql as it is exposed through the IOMMU ops.

---
 hw/i386/intel_iommu.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
hw/i386/intel_iommu.c +13 -0
index dffd7ee885..3df7a7489b 100644
@@ -4216,6 +4216,7 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,
     vtd_dev_as = g_hash_table_lookup(s->vtd_address_spaces, &key);
     if (!vtd_dev_as) {
         struct vtd_as_key *new_key = g_malloc(sizeof(*new_key));
+        bool take_bql = !bql_locked();
 
         new_key->bus = bus;
         new_key->devfn = devfn;
@@ -4238,6 +4239,14 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,
         vtd_dev_as->context_cache_entry.context_cache_gen = 0;
         vtd_dev_as->iova_tree = iova_tree_new();
 
+        /*
+         * memory_region_add_subregion_overlap requires the bql,
+         * make sure we own it.
+         */
+        if (take_bql) {
+            bql_lock();
+        }
+
         memory_region_init(&vtd_dev_as->root, OBJECT(s), name, UINT64_MAX);
         address_space_init(&vtd_dev_as->as, &vtd_dev_as->root, "vtd-root");
 
@@ -4305,6 +4314,10 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,
 
         vtd_switch_address_space(vtd_dev_as);
 
+        if (take_bql) {
+            bql_unlock();
+        }
+
         g_hash_table_insert(s->vtd_address_spaces, new_key, vtd_dev_as);
     }
     return vtd_dev_as;
Signature · 1 line
2.49.0
#1 lore raw
Quoted 22 lines · CLEMENT MATHIEU--DRIF

Address space creation might end up being called without holding the bql as it is exposed through the IOMMU ops.

---
 hw/i386/intel_iommu.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
hw/i386/intel_iommu.c +1 -0
index dffd7ee885..3df7a7489b 100644
@@ -4216,6 +4216,7 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,
     vtd_dev_as = g_hash_table_lookup(s->vtd_address_spaces, &key);
     if (!vtd_dev_as) {
         struct vtd_as_key *new_key = g_malloc(sizeof(*new_key));
+        bool take_bql = !bql_locked();
 
         new_key->bus = bus;
         new_key->devfn = devfn;
@@ -4238,6 +4239,14 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,
         vtd_dev_as->context_cache_entry.context_cache_gen = 0;

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, · line 4249

Quoted 10 lines
hw/i386/intel_iommu.c +8 -0
         vtd_dev_as->iova_tree = iova_tree_new();
 
+        /*
+         * memory_region_add_subregion_overlap requires the bql,
+         * make sure we own it.
+         */
+        if (take_bql) {
+            bql_lock();
+        }
+

Using BQL_LOCK_GUARD(); here will auto clean-up as we exit and it handles the nested BQL state as well.

Quoted 14 lines
hw/i386/intel_iommu.c +4 -0
         memory_region_init(&vtd_dev_as->root, OBJECT(s), name, UINT64_MAX);
         address_space_init(&vtd_dev_as->as, &vtd_dev_as->root, "vtd-root");
 
@@ -4305,6 +4314,10 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,
 
         vtd_switch_address_space(vtd_dev_as);
 
+        if (take_bql) {
+            bql_unlock();
+        }
+
         g_hash_table_insert(s->vtd_address_spaces, new_key, vtd_dev_as);
     }
     return vtd_dev_as;
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro