qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v5 0/2] intel_iommu: Fix locking issues
@ 2025-04-30 12:48 CLEMENT MATHIEU--DRIF
  2025-04-30 12:48 ` [PATCH v5 1/2] intel_iommu: Use BQL_LOCK_GUARD to manage cleanup automatically CLEMENT MATHIEU--DRIF
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: CLEMENT MATHIEU--DRIF @ 2025-04-30 12:48 UTC (permalink / raw)
  To: qemu-devel@nongnu.org
  Cc: jasowang@redhat.com, zhenzhong.duan@intel.com,
	kevin.tian@intel.com, yi.l.liu@intel.com, peterx@redhat.com,
	mst@redhat.com, CLEMENT MATHIEU--DRIF

This series introduces 2 fixes and improves locking style
consistency in the VT-d device.

Changes since v4:
	- Re-check if the address space is present once both bql and
	  iommu lock are held.


Clement Mathieu--Drif (2):
  intel_iommu: Use BQL_LOCK_GUARD to manage cleanup automatically
  intel_iommu: Take locks when looking for and creating address spaces

 hw/i386/intel_iommu.c | 35 +++++++++++++++++++++++++----------
 1 file changed, 25 insertions(+), 10 deletions(-)

-- 
2.49.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v5 1/2] intel_iommu: Use BQL_LOCK_GUARD to manage cleanup automatically
  2025-04-30 12:48 [PATCH v5 0/2] intel_iommu: Fix locking issues CLEMENT MATHIEU--DRIF
@ 2025-04-30 12:48 ` CLEMENT MATHIEU--DRIF
  2025-04-30 12:48 ` [PATCH v5 2/2] intel_iommu: Take locks when looking for and creating address spaces CLEMENT MATHIEU--DRIF
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: CLEMENT MATHIEU--DRIF @ 2025-04-30 12:48 UTC (permalink / raw)
  To: qemu-devel@nongnu.org
  Cc: jasowang@redhat.com, zhenzhong.duan@intel.com,
	kevin.tian@intel.com, yi.l.liu@intel.com, peterx@redhat.com,
	mst@redhat.com, CLEMENT MATHIEU--DRIF

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

Signed-off-by: Clement Mathieu--Drif <clement.mathieu--drif@eviden.com>
---
 hw/i386/intel_iommu.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)

diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c
index dffd7ee885..dad1d9f300 100644
--- a/hw/i386/intel_iommu.c
+++ b/hw/i386/intel_iommu.c
@@ -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;
 }
 
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v5 2/2] intel_iommu: Take locks when looking for and creating address spaces
  2025-04-30 12:48 [PATCH v5 0/2] intel_iommu: Fix locking issues CLEMENT MATHIEU--DRIF
  2025-04-30 12:48 ` [PATCH v5 1/2] intel_iommu: Use BQL_LOCK_GUARD to manage cleanup automatically CLEMENT MATHIEU--DRIF
@ 2025-04-30 12:48 ` CLEMENT MATHIEU--DRIF
  2025-05-14 11:48   ` Michael S. Tsirkin
  2025-05-06  5:07 ` [PATCH v5 0/2] intel_iommu: Fix locking issues CLEMENT MATHIEU--DRIF
  2025-05-06  9:54 ` Duan, Zhenzhong
  3 siblings, 1 reply; 7+ messages in thread
From: CLEMENT MATHIEU--DRIF @ 2025-04-30 12:48 UTC (permalink / raw)
  To: qemu-devel@nongnu.org
  Cc: jasowang@redhat.com, zhenzhong.duan@intel.com,
	kevin.tian@intel.com, yi.l.liu@intel.com, peterx@redhat.com,
	mst@redhat.com, CLEMENT MATHIEU--DRIF

vtd_find_add_as can be called by multiple threads which leads to a race
condition. Taking the IOMMU lock ensures we avoid such a race.
Moreover we also need to take the bql to avoid an assert to fail in
memory_region_add_subregion_overlap when actually allocating a new
address space.

Signed-off-by: Clement Mathieu--Drif <clement.mathieu--drif@eviden.com>
---
 hw/i386/intel_iommu.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c
index dad1d9f300..144e25622a 100644
--- a/hw/i386/intel_iommu.c
+++ b/hw/i386/intel_iommu.c
@@ -4205,9 +4205,30 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,
     VTDAddressSpace *vtd_dev_as;
     char name[128];
 
+    vtd_iommu_lock(s);
     vtd_dev_as = g_hash_table_lookup(s->vtd_address_spaces, &key);
+    vtd_iommu_unlock(s);
+
     if (!vtd_dev_as) {
-        struct vtd_as_key *new_key = g_malloc(sizeof(*new_key));
+        struct vtd_as_key *new_key;
+        /* Slow path */
+
+        /*
+        * memory_region_add_subregion_overlap requires the bql,
+        * make sure we own it.
+        */
+        BQL_LOCK_GUARD();
+        vtd_iommu_lock(s);
+
+        /* Check again as we released the lock for a moment */
+        vtd_dev_as = g_hash_table_lookup(s->vtd_address_spaces, &key);
+        if (vtd_dev_as) {
+            vtd_iommu_unlock(s);
+            return vtd_dev_as;
+        }
+
+        /* Still nothing, allocate a new address space */
+        new_key = g_malloc(sizeof(*new_key));
 
         new_key->bus = bus;
         new_key->devfn = devfn;
@@ -4298,6 +4319,8 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,
         vtd_switch_address_space(vtd_dev_as);
 
         g_hash_table_insert(s->vtd_address_spaces, new_key, vtd_dev_as);
+    
+        vtd_iommu_unlock(s);
     }
     return vtd_dev_as;
 }
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v5 0/2] intel_iommu: Fix locking issues
  2025-04-30 12:48 [PATCH v5 0/2] intel_iommu: Fix locking issues CLEMENT MATHIEU--DRIF
  2025-04-30 12:48 ` [PATCH v5 1/2] intel_iommu: Use BQL_LOCK_GUARD to manage cleanup automatically CLEMENT MATHIEU--DRIF
  2025-04-30 12:48 ` [PATCH v5 2/2] intel_iommu: Take locks when looking for and creating address spaces CLEMENT MATHIEU--DRIF
@ 2025-05-06  5:07 ` CLEMENT MATHIEU--DRIF
  2025-05-06  9:54 ` Duan, Zhenzhong
  3 siblings, 0 replies; 7+ messages in thread
From: CLEMENT MATHIEU--DRIF @ 2025-05-06  5:07 UTC (permalink / raw)
  To: qemu-devel@nongnu.org
  Cc: jasowang@redhat.com, zhenzhong.duan@intel.com,
	kevin.tian@intel.com, yi.l.liu@intel.com, peterx@redhat.com,
	mst@redhat.com, Paolo Bonzini

Cc'ing Paolo

On 30/04/2025 2:48 pm, CLEMENT MATHIEU--DRIF wrote:
> This series introduces 2 fixes and improves locking style
> consistency in the VT-d device.
> 
> Changes since v4:
> 	- Re-check if the address space is present once both bql and
> 	  iommu lock are held.
> 
> 
> Clement Mathieu--Drif (2):
>    intel_iommu: Use BQL_LOCK_GUARD to manage cleanup automatically
>    intel_iommu: Take locks when looking for and creating address spaces
> 
>   hw/i386/intel_iommu.c | 35 +++++++++++++++++++++++++----------
>   1 file changed, 25 insertions(+), 10 deletions(-)
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: [PATCH v5 0/2] intel_iommu: Fix locking issues
  2025-04-30 12:48 [PATCH v5 0/2] intel_iommu: Fix locking issues CLEMENT MATHIEU--DRIF
                   ` (2 preceding siblings ...)
  2025-05-06  5:07 ` [PATCH v5 0/2] intel_iommu: Fix locking issues CLEMENT MATHIEU--DRIF
@ 2025-05-06  9:54 ` Duan, Zhenzhong
  3 siblings, 0 replies; 7+ messages in thread
From: Duan, Zhenzhong @ 2025-05-06  9:54 UTC (permalink / raw)
  To: CLEMENT MATHIEU--DRIF, qemu-devel@nongnu.org
  Cc: jasowang@redhat.com, Tian, Kevin, Liu, Yi L, peterx@redhat.com,
	mst@redhat.com

Hi Clement,

>-----Original Message-----
>From: CLEMENT MATHIEU--DRIF <clement.mathieu--drif@eviden.com>
>Subject: [PATCH v5 0/2] intel_iommu: Fix locking issues
>
>This series introduces 2 fixes and improves locking style
>consistency in the VT-d device.
>
>Changes since v4:
>	- Re-check if the address space is present once both bql and
>	  iommu lock are held.

Yes, this is cleaner. For the whole series:

Reviewed-by: Zhenzhong Duan <zhenzhong.duan@intel.com>

Thanks
Zhenzhong



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v5 2/2] intel_iommu: Take locks when looking for and creating address spaces
  2025-04-30 12:48 ` [PATCH v5 2/2] intel_iommu: Take locks when looking for and creating address spaces CLEMENT MATHIEU--DRIF
@ 2025-05-14 11:48   ` Michael S. Tsirkin
  2025-05-14 16:29     ` CLEMENT MATHIEU--DRIF
  0 siblings, 1 reply; 7+ messages in thread
From: Michael S. Tsirkin @ 2025-05-14 11:48 UTC (permalink / raw)
  To: CLEMENT MATHIEU--DRIF
  Cc: qemu-devel@nongnu.org, jasowang@redhat.com,
	zhenzhong.duan@intel.com, kevin.tian@intel.com,
	yi.l.liu@intel.com, peterx@redhat.com

On Wed, Apr 30, 2025 at 12:48:06PM +0000, CLEMENT MATHIEU--DRIF wrote:
> vtd_find_add_as can be called by multiple threads which leads to a race
> condition. Taking the IOMMU lock ensures we avoid such a race.
> Moreover we also need to take the bql to avoid an assert to fail in
> memory_region_add_subregion_overlap when actually allocating a new
> address space.
> 
> Signed-off-by: Clement Mathieu--Drif <clement.mathieu--drif@eviden.com>
> ---
>  hw/i386/intel_iommu.c | 25 ++++++++++++++++++++++++-
>  1 file changed, 24 insertions(+), 1 deletion(-)
> 
> diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c
> index dad1d9f300..144e25622a 100644
> --- a/hw/i386/intel_iommu.c
> +++ b/hw/i386/intel_iommu.c
> @@ -4205,9 +4205,30 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,
>      VTDAddressSpace *vtd_dev_as;
>      char name[128];
>  
> +    vtd_iommu_lock(s);
>      vtd_dev_as = g_hash_table_lookup(s->vtd_address_spaces, &key);
> +    vtd_iommu_unlock(s);
> +
>      if (!vtd_dev_as) {
> -        struct vtd_as_key *new_key = g_malloc(sizeof(*new_key));
> +        struct vtd_as_key *new_key;
> +        /* Slow path */
> +
> +        /*
> +        * memory_region_add_subregion_overlap requires the bql,
> +        * make sure we own it.
> +        */


not how comments should look

> +        BQL_LOCK_GUARD();
> +        vtd_iommu_lock(s);
> +
> +        /* Check again as we released the lock for a moment */
> +        vtd_dev_as = g_hash_table_lookup(s->vtd_address_spaces, &key);
> +        if (vtd_dev_as) {
> +            vtd_iommu_unlock(s);
> +            return vtd_dev_as;
> +        }
> +
> +        /* Still nothing, allocate a new address space */
> +        new_key = g_malloc(sizeof(*new_key));
>  
>          new_key->bus = bus;
>          new_key->devfn = devfn;
> @@ -4298,6 +4319,8 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,
>          vtd_switch_address_space(vtd_dev_as);
>  
>          g_hash_table_insert(s->vtd_address_spaces, new_key, vtd_dev_as);
> +    

trailing whitespace here

> +        vtd_iommu_unlock(s);
>      }
>      return vtd_dev_as;
>  }



I fixed these up but pls take care, Clement.

> -- 
> 2.49.0



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v5 2/2] intel_iommu: Take locks when looking for and creating address spaces
  2025-05-14 11:48   ` Michael S. Tsirkin
@ 2025-05-14 16:29     ` CLEMENT MATHIEU--DRIF
  0 siblings, 0 replies; 7+ messages in thread
From: CLEMENT MATHIEU--DRIF @ 2025-05-14 16:29 UTC (permalink / raw)
  To: Michael S. Tsirkin
  Cc: qemu-devel@nongnu.org, jasowang@redhat.com,
	zhenzhong.duan@intel.com, kevin.tian@intel.com,
	yi.l.liu@intel.com, peterx@redhat.com



On 14/05/2025 1:48 pm, Michael S. Tsirkin wrote:
> 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.
> 
> 
> On Wed, Apr 30, 2025 at 12:48:06PM +0000, CLEMENT MATHIEU--DRIF wrote:
>> vtd_find_add_as can be called by multiple threads which leads to a race
>> condition. Taking the IOMMU lock ensures we avoid such a race.
>> Moreover we also need to take the bql to avoid an assert to fail in
>> memory_region_add_subregion_overlap when actually allocating a new
>> address space.
>>
>> Signed-off-by: Clement Mathieu--Drif <clement.mathieu--drif@eviden.com>
>> ---
>>   hw/i386/intel_iommu.c | 25 ++++++++++++++++++++++++-
>>   1 file changed, 24 insertions(+), 1 deletion(-)
>>
>> diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c
>> index dad1d9f300..144e25622a 100644
>> --- a/hw/i386/intel_iommu.c
>> +++ b/hw/i386/intel_iommu.c
>> @@ -4205,9 +4205,30 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,
>>       VTDAddressSpace *vtd_dev_as;
>>       char name[128];
>>
>> +    vtd_iommu_lock(s);
>>       vtd_dev_as = g_hash_table_lookup(s->vtd_address_spaces, &key);
>> +    vtd_iommu_unlock(s);
>> +
>>       if (!vtd_dev_as) {
>> -        struct vtd_as_key *new_key = g_malloc(sizeof(*new_key));
>> +        struct vtd_as_key *new_key;
>> +        /* Slow path */
>> +
>> +        /*
>> +        * memory_region_add_subregion_overlap requires the bql,
>> +        * make sure we own it.
>> +        */
> 
> 
> not how comments should look
> 
>> +        BQL_LOCK_GUARD();
>> +        vtd_iommu_lock(s);
>> +
>> +        /* Check again as we released the lock for a moment */
>> +        vtd_dev_as = g_hash_table_lookup(s->vtd_address_spaces, &key);
>> +        if (vtd_dev_as) {
>> +            vtd_iommu_unlock(s);
>> +            return vtd_dev_as;
>> +        }
>> +
>> +        /* Still nothing, allocate a new address space */
>> +        new_key = g_malloc(sizeof(*new_key));
>>
>>           new_key->bus = bus;
>>           new_key->devfn = devfn;
>> @@ -4298,6 +4319,8 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,
>>           vtd_switch_address_space(vtd_dev_as);
>>
>>           g_hash_table_insert(s->vtd_address_spaces, new_key, vtd_dev_as);
>> +
> 
> trailing whitespace here
> 
>> +        vtd_iommu_unlock(s);
>>       }
>>       return vtd_dev_as;
>>   }
> 
> 
> 
> I fixed these up but pls take care, Clement.

Ouch, sorry, thanks Michael

> 
>> --
>> 2.49.0
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2025-05-14 16:44 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-30 12:48 [PATCH v5 0/2] intel_iommu: Fix locking issues CLEMENT MATHIEU--DRIF
2025-04-30 12:48 ` [PATCH v5 1/2] intel_iommu: Use BQL_LOCK_GUARD to manage cleanup automatically CLEMENT MATHIEU--DRIF
2025-04-30 12:48 ` [PATCH v5 2/2] intel_iommu: Take locks when looking for and creating address spaces CLEMENT MATHIEU--DRIF
2025-05-14 11:48   ` Michael S. Tsirkin
2025-05-14 16:29     ` CLEMENT MATHIEU--DRIF
2025-05-06  5:07 ` [PATCH v5 0/2] intel_iommu: Fix locking issues CLEMENT MATHIEU--DRIF
2025-05-06  9:54 ` Duan, Zhenzhong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).