Skip to contentSkip to conversation

intel_iommu: Take the bql before registering a new address space

PATCH Superseded CLEMENT MATHIEU--DRIF 2 participants 6 messages Apr 23, 2025

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 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 | 6 ++++++
 1 file changed, 6 insertions(+)
hw/i386/intel_iommu.c +6 -0
index dffd7ee885..cc8c9857e1 100644
@@ -4238,6 +4238,12 @@ 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.
+         */
+        BQL_LOCK_GUARD();
+
         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");
 
Signature · 1 line
2.49.0
#1 lore raw

PATCH v3 2/2 intel_iommu: Use BQL_LOCK_GUARD to manage cleanup automatically

vtd_switch_address_space needs to take the BQL if not already held. Use BQL_LOCK_GUARD to make the iommu implementation more consistent.

---
 hw/i386/intel_iommu.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)
hw/i386/intel_iommu.c +1 -9
index cc8c9857e1..b7855f4b87 100644
@@ -1728,8 +1728,6 @@ static bool vtd_as_pt_enabled(VTDAddressSpace *as)
 static bool vtd_switch_address_space(VTDAddressSpace *as)
 {
     bool use_iommu, pt;
-    /* Whether we need to take the BQL on our own */
-    bool take_bql = !bql_locked();
 
     assert(as);
 
@@ -1746,9 +1744,7 @@ static bool vtd_switch_address_space(VTDAddressSpace *as)
      * from vtd_pt_enable_fast_path(). However the memory APIs need
      * it. We'd better make sure we have had it already, or, take it.
      */
-    if (take_bql) {
-        bql_lock();
-    }
+    BQL_LOCK_GUARD();
 
     /* Turn off first then on the other */
     if (use_iommu) {
@@ -1801,10 +1797,6 @@ static bool vtd_switch_address_space(VTDAddressSpace *as)
         memory_region_set_enabled(&as->iommu_ir_fault, false);
     }
 
-    if (take_bql) {
-        bql_unlock();
-    }
-
     return use_iommu;
 }
 
Signature · 1 line
2.49.0
#2 lore raw
In reply to CLEMENT MATHIEU--DRIF
Quoted 14 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 | 6 ++++++
 1 file changed, 6 insertions(+)
hw/i386/intel_iommu.c +0 -0
index dffd7ee885..cc8c9857e1 100644
@@ -4238,6 +4238,12 @@ 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 4248

Quoted 10 lines
hw/i386/intel_iommu.c +6 -0
         vtd_dev_as->iova_tree = iova_tree_new();
 
+        /*
+         * memory_region_add_subregion_overlap requires the bql,
+         * make sure we own it.
+         */
+        BQL_LOCK_GUARD();
+
         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");

Does not look like this addresses all races here:
https://lore.kernel.org/all/[email protected]

while this can be a separate patch on top, I'd rather we just address everything in a single patchset.

Quoted 2 lines
Signature · 1 line
2.49.0
#3 lore raw
Quoted 33 lines · Michael S. Tsirkin

Caution: External email. Do not open attachments or click links, unless this email comes from a known sender and you know the content is safe.

Quoted 14 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 | 6 ++++++
  1 file changed, 6 insertions(+)
hw/i386/intel_iommu.c +0 -0
index dffd7ee885..cc8c9857e1 100644
@@ -4238,6 +4238,12 @@ 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 4248

Quoted 10 lines
hw/i386/intel_iommu.c +6 -0
          vtd_dev_as->iova_tree = iova_tree_new();
 
+        /*
+         * memory_region_add_subregion_overlap requires the bql,
+         * make sure we own it.
+         */
+        BQL_LOCK_GUARD();
+
          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");

Does not look like this addresses all races here:
https://lore.kernel.org/all/[email protected]

Quoted 2 lines

while this can be a separate patch on top, I'd rather we just address everything in a single patchset.

Hi Michael,

We only aim to fix the potential crash here.
I saw Paolo's response and I know the race exists. I will send a patch set to fix it soon but are you sure both fixes must be in the same series? I think the nature is different.

cmd

Quoted 5 lines
Quoted 4 lines
hw/i386/intel_iommu.c +0 -1
--

2.49.0

#4 lore raw
Quoted 39 lines · CLEMENT MATHIEU--DRIF
Quoted 33 lines · Michael S. Tsirkin

Caution: External email. Do not open attachments or click links, unless this email comes from a known sender and you know the content is safe.

Quoted 14 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 | 6 ++++++
  1 file changed, 6 insertions(+)
hw/i386/intel_iommu.c +0 -0
index dffd7ee885..cc8c9857e1 100644
@@ -4238,6 +4238,12 @@ 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 4248

Quoted 10 lines
hw/i386/intel_iommu.c +6 -0
          vtd_dev_as->iova_tree = iova_tree_new();
 
+        /*
+         * memory_region_add_subregion_overlap requires the bql,
+         * make sure we own it.
+         */
+        BQL_LOCK_GUARD();
+
          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");

Does not look like this addresses all races here:
https://lore.kernel.org/all/[email protected]

Quoted 2 lines

while this can be a separate patch on top, I'd rather we just address everything in a single patchset.

Quoted 8 lines

Hi Michael,

We only aim to fix the potential crash here.
I saw Paolo's response and I know the race exists. I will send a patch set to fix it soon but are you sure both fixes must be in the same series? I think the nature is different.

cmd

If you have two races in the same function, fixing one can easily make another one occur more. Let's just fix it all please, I don't see any rush to apply a partial fix.

Signature · 1 line
MST
#5 lore raw
Quoted 50 lines · Michael S. Tsirkin

Caution: External email. Do not open attachments or click links, unless this email comes from a known sender and you know the content is safe.

Quoted 46 lines · CLEMENT MATHIEU--DRIF
Quoted 33 lines · Michael S. Tsirkin

Caution: External email. Do not open attachments or click links, unless this email comes from a known sender and you know the content is safe.

Quoted 14 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 | 6 ++++++
   1 file changed, 6 insertions(+)
hw/i386/intel_iommu.c +0 -0
index dffd7ee885..cc8c9857e1 100644
@@ -4238,6 +4238,12 @@ 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 4248

Quoted 10 lines
hw/i386/intel_iommu.c +6 -0
           vtd_dev_as->iova_tree = iova_tree_new();
 
+        /*
+         * memory_region_add_subregion_overlap requires the bql,
+         * make sure we own it.
+         */
+        BQL_LOCK_GUARD();
+
           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");

Does not look like this addresses all races here:
https://lore.kernel.org/all/[email protected]

Quoted 2 lines

while this can be a separate patch on top, I'd rather we just address everything in a single patchset.

Hi Michael,

We only aim to fix the potential crash here.
I saw Paolo's response and I know the race exists. I will send a patch set to fix it soon but are you sure both fixes must be in the same series? I think the nature is different.

Quoted 6 lines
Quoted 1 line

cmd

If you have two races in the same function, fixing one can easily make another one occur more. Let's just fix it all please, I don't see any rush to apply a partial fix.

Fine, will do!

Quoted 3 lines

--
MST