intel_iommu: Fix locking issues
v4 cover intel_iommu: Fix locking issues
This series introduces 2 fixes and improves locking style consistency in the VT-d device.
Clement Mathieu--Drif (3):
intel_iommu: Take the bql before registering a new address space
intel_iommu: Use BQL_LOCK_GUARD to manage cleanup automatically
intel_iommu: Take the VTD lock when looking for and creating address
spaces
hw/i386/intel_iommu.c | 44 ++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-)
Signature · 1 line
2.49.0
PATCH v4 1/3 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
@@ -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
PATCH v4 2/3 intel_iommu: Use BQL_LOCK_GUARD to manage cleanup automatically
In reply to CLEMENT MATHIEU--DRIFvtd_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
@@ -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
PATCH v4 3/3 intel_iommu: Take the VTD lock when looking for and creating address spaces
In reply to CLEMENT MATHIEU--DRIFvtd_find_add_as can be called by multiple threads which leads to a race condition on address space creation. The IOMMU lock must be taken to avoid such a race.
--- hw/i386/intel_iommu.c | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-)
hw/i386/intel_iommu.c +26 -2
@@ -4203,11 +4203,15 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, .pasid = pasid, };VTDAddressSpace *vtd_dev_as; +struct vtd_as_key *new_key =NULL ;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)); + new_key =g_malloc (sizeof (*new_key)); new_key->bus = bus; new_key->devfn = devfn;
@@ -4302,9 +4306,29 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, &vtd_dev_as->nodmar, 0);vtd_switch_address_space (vtd_dev_as); + } -g_hash_table_insert (s->vtd_address_spaces, new_key, vtd_dev_as); +if (new_key !=NULL ) { +VTDAddressSpace *second_vtd_dev_as; + +/* +* Take the lock again and recheck as the AS might have +* been created in the meantime. +*/ +vtd_iommu_lock (s); + + second_vtd_dev_as =g_hash_table_lookup (s->vtd_address_spaces, &key); +if (!second_vtd_dev_as) { +g_hash_table_insert (s->vtd_address_spaces, new_key, vtd_dev_as); + }else { + vtd_dev_as = second_vtd_dev_as; +g_free (vtd_dev_as); +g_free (new_key); + } + +vtd_iommu_unlock (s); } +return vtd_dev_as; }
Signature · 1 line
2.49.0
RE: [PATCH v4 3/3] intel_iommu: Take the VTD lock when looking for and creating address spaces
Hi Clement,
Quoted 50 lines
-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: [PATCH v4 3/3]intel_iommu: Take the VTD lock when looking for and creating address spaces
vtd_find_add_ascan be called by multiple threads which leads to a race condition on address space creation. The IOMMU lock must be taken to avoid such a race.Signed-off-by Clement Mathieu--Drif <[email protected]>---
hw/i386/intel_iommu.c| 28 ++++++++++++++++++++++++++--1 file changed, 26 insertions(+), 2 deletions(-)hw/i386/intel_iommu.c +5
-1@@ -4203,11 +4203,15 @@ VTDAddressSpace *vtd_find_add_as (IntelIOMMUState *s, PCIBus *bus, .pasid = pasid, }; VTDAddressSpace *vtd_dev_as; +struct vtd_as_key *new_key =NULL ;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)); + new_key =g_malloc (sizeof (*new_key)); new_key->bus = bus;new_key->devfn = devfn;hw/i386/intel_iommu.c +7
-1@@ -4302,9 +4306,29 @@ VTDAddressSpace *vtd_find_add_as (IntelIOMMUState *s, PCIBus *bus, &vtd_dev_as->nodmar,0 );vtd_switch_address_space (vtd_dev_as); + } -g_hash_table_insert (s->vtd_address_spaces, new_key, vtd_dev_as); +if (new_key !=NULL ) { +VTDAddressSpace *second_vtd_dev_as; + +/* +* Take the lock again and recheck as the AS might have +* been created in the meantime.
hw/i386/intel_iommu.c VTDAddressSpace · line 4327
Quoted 10 lines
hw/i386/intel_iommu.c +10
-0+*/ +vtd_iommu_lock (s); + + second_vtd_dev_as =g_hash_table_lookup (s->vtd_address_spaces, &key); +if (!second_vtd_dev_as) { +g_hash_table_insert (s->vtd_address_spaces, new_key, vtd_dev_as); + }else { + vtd_dev_as = second_vtd_dev_as; +g_free (vtd_dev_as); +g_free (new_key);
We need to release memory regions under this vtd_dev_as to avoid leak.
Thanks
Zhenzhong
Re: [PATCH v4 3/3] intel_iommu: Take the VTD lock when looking for and creating address spaces
Hi Zhenzhong,
Quoted 59 lines · Zhenzhong
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.
Hi Clement,
Quoted 54 lines
-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: [PATCH v4 3/3]intel_iommu: Take the VTD lock when looking for and creating address spaces
vtd_find_add_ascan be called by multiple threads which leads to a race condition on address space creation. The IOMMU lock must be taken to avoid such a race.Signed-off-by Clement Mathieu--Drif <[email protected]>---
hw/i386/intel_iommu.c| 28 ++++++++++++++++++++++++++--1 file changed, 26 insertions(+), 2 deletions(-)hw/i386/intel_iommu.c +5
-1@@ -4203,11 +4203,15 @@ VTDAddressSpace *vtd_find_add_as (IntelIOMMUState *s, PCIBus *bus, .pasid = pasid, }; VTDAddressSpace *vtd_dev_as; +struct vtd_as_key *new_key =NULL ;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)); + new_key =g_malloc (sizeof (*new_key)); new_key->bus = bus;new_key->devfn = devfn;hw/i386/intel_iommu.c +11
-1@@ -4302,9 +4306,29 @@ VTDAddressSpace *vtd_find_add_as (IntelIOMMUState *s, PCIBus *bus, &vtd_dev_as->nodmar,0 );vtd_switch_address_space (vtd_dev_as); + } -g_hash_table_insert (s->vtd_address_spaces, new_key, vtd_dev_as); +if (new_key !=NULL ) { +VTDAddressSpace *second_vtd_dev_as; + +/* +* Take the lock again and recheck as the AS might have +* been created in the meantime. +*/ +vtd_iommu_lock (s); + + second_vtd_dev_as =g_hash_table_lookup (s->vtd_address_spaces, &key);
Quoted 8 lines
hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, · line 4326
Quoted 6 lines
hw/i386/intel_iommu.c +6
-0+if (!second_vtd_dev_as) { +g_hash_table_insert (s->vtd_address_spaces, new_key, vtd_dev_as); + }else { + vtd_dev_as = second_vtd_dev_as; +g_free (vtd_dev_as); +g_free (new_key);We need to release memory regions under this
vtd_dev_asto avoid leak.
Indeed, I'll have to take the BQL again.
Is it ok for you if it look like this:
vtd_iommu_lock(s);
second_vtd_dev_as = g_hash_table_lookup(s->vtd_address_spaces, &key);
if (!second_vtd_dev_as) {
g_hash_table_insert(s->vtd_address_spaces, new_key, vtd_dev_as);
vtd_iommu_unlock(s);
} else {
vtd_iommu_unlock(s);
BQL_LOCK_GUARD();
memory_region_del_subregion(&vtd_dev_as->root, &vtd_dev_as->nodmar);
memory_region_del_subregion(&vtd_dev_as->root,
MEMORY_REGION(&vtd_dev_as->iommu));
memory_region_del_subregion(&vtd_dev_as->root,
&vtd_dev_as->iommu_ir_fault);
memory_region_del_subregion(&vtd_dev_as->root,
&vtd_dev_as->iommu_ir);
memory_region_unref(MEMORY_REGION(&vtd_dev_as->iommu));
memory_region_unref(&vtd_dev_as->iommu_ir_fault);
memory_region_unref(&vtd_dev_as->iommu_ir);
memory_region_unref(&vtd_dev_as->nodmar);
address_space_destroy(&vtd_dev_as->as);
g_free(vtd_dev_as);
g_free(new_key);
vtd_dev_as = second_vtd_dev_as;
}...
return vtd_dev_as;
Quoted 3 lines
Thanks
Zhenzhong
RE: [PATCH v4 3/3] intel_iommu: Take the VTD lock when looking for and creating address spaces
Quoted 99 lines
-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: Re: [PATCH v4 3/3]intel_iommu: Take the VTD lock when looking for and creating address spacesHi Zhenzhong,
Quoted 1 line · Zhenzhong
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 47 lines
Hi Clement,
Quoted 43 lines
-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: [PATCH v4 3/3]intel_iommu: Take the VTD lock when looking for and creating address spaces
vtd_find_add_ascan be called by multiple threads which leads to a race condition on address space creation. The IOMMU lock must be taken to avoid such a race.Signed-off-by Clement Mathieu--Drif <[email protected]>---
hw/i386/intel_iommu.c| 28 ++++++++++++++++++++++++++--1 file changed, 26 insertions(+), 2 deletions(-)hw/i386/intel_iommu.c +5
-1@@ -4203,11 +4203,15 @@ VTDAddressSpace *vtd_find_add_as (IntelIOMMUState *s, PCIBus *bus, .pasid = pasid, }; VTDAddressSpace *vtd_dev_as; +struct vtd_as_key *new_key =NULL ;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)); + new_key =g_malloc (sizeof (*new_key)); new_key->bus = bus;new_key->devfn = devfn;hw/i386/intel_iommu.c +1
-0@@ -4302,9 +4306,29 @@ VTDAddressSpace *vtd_find_add_as (IntelIOMMUState *s, PCIBus *bus, &vtd_dev_as->nodmar,0 );vtd_switch_address_space (vtd_dev_as); + }Quoted 11 lines
hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,
Quoted 11 lines
hw/i386/intel_iommu.c +10
-1-g_hash_table_insert (s->vtd_address_spaces, new_key, vtd_dev_as); +if (new_key !=NULL ) { +VTDAddressSpace *second_vtd_dev_as; + +/* +* Take the lock again and recheck as the AS might have +* been created in the meantime. +*/ +vtd_iommu_lock (s); + + second_vtd_dev_as = g_hash_table_lookup(s->vtd_address_spaces,&key);
Quoted 8 lines
hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, · line 4326
Quoted 6 lines
hw/i386/intel_iommu.c +6
-0+if (!second_vtd_dev_as) { +g_hash_table_insert (s->vtd_address_spaces, new_key, vtd_dev_as); + }else { + vtd_dev_as = second_vtd_dev_as; +g_free (vtd_dev_as); +g_free (new_key);We need to release memory regions under this
vtd_dev_asto avoid leak.Indeed, I'll have to take the BQL again.
Is it ok for you if it look like this:
vtd_iommu_lock(s);
second_vtd_dev_as=g_hash_table_lookup(s->vtd_address_spaces, &key);if (!second_vtd_dev_as) { g_hash_table_insert(s->vtd_address_spaces, new_key, vtd_dev_as); vtd_iommu_unlock(s); } else { vtd_iommu_unlock(s); BQL_LOCK_GUARD(); memory_region_del_subregion(&vtd_dev_as->root, &vtd_dev_as->nodmar); memory_region_del_subregion(&vtd_dev_as->root, MEMORY_REGION(&vtd_dev_as->iommu)); memory_region_del_subregion(&vtd_dev_as->root,
Quoted 8 lines
&vtd_dev_as->iommu_ir_fault);
memory_region_del_subregion(&vtd_dev_as->root,
&vtd_dev_as->iommu_ir);
memory_region_unref(MEMORY_REGION(&vtd_dev_as->iommu));
memory_region_unref(&vtd_dev_as->iommu_ir_fault);
memory_region_unref(&vtd_dev_as->iommu_ir);
memory_region_unref(&vtd_dev_as->nodmar);s/memory_region_unref/object_unparent?
Quoted 2 lines
address_space_destroy(&vtd_dev_as->as);object_unparent(vtd_dev_as->root);
Thanks
Zhenzhong
Re: [PATCH v4 3/3] intel_iommu: Take the VTD lock when looking for and creating address spaces
Quoted 101 lines · Zhenzhong
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 98 lines
-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: Re: [PATCH v4 3/3]intel_iommu: Take the VTD lock when looking for and creating address spacesHi Zhenzhong,
Quoted 1 line · Zhenzhong
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
Hi Clement,
Quoted 43 lines
-----Original Message-----
From: CLEMENT MATHIEU--DRIF <[email protected]>
Subject: [PATCH v4 3/3]intel_iommu: Take the VTD lock when looking for and creating address spaces
vtd_find_add_ascan be called by multiple threads which leads to a race condition on address space creation. The IOMMU lock must be taken to avoid such a race.Signed-off-by Clement Mathieu--Drif <[email protected]>---
hw/i386/intel_iommu.c| 28 ++++++++++++++++++++++++++--1 file changed, 26 insertions(+), 2 deletions(-)hw/i386/intel_iommu.c +5
-1@@ -4203,11 +4203,15 @@ VTDAddressSpace *vtd_find_add_as (IntelIOMMUState *s, PCIBus *bus, .pasid = pasid, }; VTDAddressSpace *vtd_dev_as; +struct vtd_as_key *new_key =NULL ;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)); + new_key =g_malloc (sizeof (*new_key)); new_key->bus = bus;new_key->devfn = devfn;hw/i386/intel_iommu.c +1
-0@@ -4302,9 +4306,29 @@ VTDAddressSpace *vtd_find_add_as (IntelIOMMUState *s, PCIBus *bus, &vtd_dev_as->nodmar,0 );vtd_switch_address_space (vtd_dev_as); + }Quoted 11 lines
hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,
Quoted 11 lines
hw/i386/intel_iommu.c +10
-1-g_hash_table_insert (s->vtd_address_spaces, new_key, vtd_dev_as); +if (new_key !=NULL ) { +VTDAddressSpace *second_vtd_dev_as; + +/* +* Take the lock again and recheck as the AS might have +* been created in the meantime. +*/ +vtd_iommu_lock (s); + + second_vtd_dev_as = g_hash_table_lookup(s->vtd_address_spaces,&key);
Quoted 7 lines
hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, · line 4326
Quoted 6 lines
hw/i386/intel_iommu.c +6
-0+if (!second_vtd_dev_as) { +g_hash_table_insert (s->vtd_address_spaces, new_key, vtd_dev_as); + }else { + vtd_dev_as = second_vtd_dev_as; +g_free (vtd_dev_as); +g_free (new_key);We need to release memory regions under this
vtd_dev_asto avoid leak.Indeed, I'll have to take the BQL again.
Is it ok for you if it look like this:
vtd_iommu_lock(s);
second_vtd_dev_as=g_hash_table_lookup(s->vtd_address_spaces, &key);if (!second_vtd_dev_as) { g_hash_table_insert(s->vtd_address_spaces, new_key, vtd_dev_as); vtd_iommu_unlock(s); } else { vtd_iommu_unlock(s); BQL_LOCK_GUARD(); memory_region_del_subregion(&vtd_dev_as->root, &vtd_dev_as->nodmar); memory_region_del_subregion(&vtd_dev_as->root, MEMORY_REGION(&vtd_dev_as->iommu)); memory_region_del_subregion(&vtd_dev_as->root, &vtd_dev_as->iommu_ir_fault); memory_region_del_subregion(&vtd_dev_as->root, &vtd_dev_as->iommu_ir);
Quoted 8 lines
Quoted 4 lines
memory_region_unref(MEMORY_REGION(&vtd_dev_as->iommu)); memory_region_unref(&vtd_dev_as->iommu_ir_fault); memory_region_unref(&vtd_dev_as->iommu_ir); memory_region_unref(&vtd_dev_as->nodmar);s/memory_region_unref/object_unparent?
Quoted 1 line
address_space_destroy(&vtd_dev_as->as);
object_unparent(vtd_dev_as->root);
Hi Zhenzhong,
I think the issue can be fixed differently in a cleaner way. I sent a v5 for that.
Thanks
Quoted 3 lines
Thanks
Zhenzhong