From: CLEMENT MATHIEU--DRIF <clement.mathieu--drif@eviden.com>
To: "qemu-devel@nongnu.org" <qemu-devel@nongnu.org>
Cc: "jasowang@redhat.com" <jasowang@redhat.com>,
"zhenzhong.duan@intel.com" <zhenzhong.duan@intel.com>,
"kevin.tian@intel.com" <kevin.tian@intel.com>,
"yi.l.liu@intel.com" <yi.l.liu@intel.com>,
"peterx@redhat.com" <peterx@redhat.com>,
"mst@redhat.com" <mst@redhat.com>,
CLEMENT MATHIEU--DRIF <clement.mathieu--drif@eviden.com>
Subject: [PATCH v4 3/3] intel_iommu: Take the VTD lock when looking for and creating address spaces
Date: Mon, 28 Apr 2025 05:12:55 +0000 [thread overview]
Message-ID: <20250428051235.10767-4-clement.mathieu--drif@eviden.com> (raw)
In-Reply-To: <20250428051235.10767-1-clement.mathieu--drif@eviden.com>
vtd_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.
Signed-off-by: Clement Mathieu--Drif <clement.mathieu--drif@eviden.com>
---
hw/i386/intel_iommu.c | 28 ++++++++++++++++++++++++++--
1 file changed, 26 insertions(+), 2 deletions(-)
diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c
index b7855f4b87..931ac01ef0 100644
--- a/hw/i386/intel_iommu.c
+++ b/hw/i386/intel_iommu.c
@@ -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;
}
--
2.49.0
next prev parent reply other threads:[~2025-04-28 5:14 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-28 5:12 [PATCH v4 0/3] intel_iommu: Fix locking issues CLEMENT MATHIEU--DRIF
2025-04-28 5:12 ` [PATCH v4 1/3] intel_iommu: Take the bql before registering a new address space CLEMENT MATHIEU--DRIF
2025-04-28 5:12 ` [PATCH v4 2/3] intel_iommu: Use BQL_LOCK_GUARD to manage cleanup automatically CLEMENT MATHIEU--DRIF
2025-04-28 5:12 ` CLEMENT MATHIEU--DRIF [this message]
2025-04-28 8:55 ` [PATCH v4 3/3] intel_iommu: Take the VTD lock when looking for and creating address spaces Duan, Zhenzhong
2025-04-28 14:32 ` CLEMENT MATHIEU--DRIF
2025-04-29 6:01 ` Duan, Zhenzhong
2025-04-30 12:58 ` CLEMENT MATHIEU--DRIF
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250428051235.10767-4-clement.mathieu--drif@eviden.com \
--to=clement.mathieu--drif@eviden.com \
--cc=jasowang@redhat.com \
--cc=kevin.tian@intel.com \
--cc=mst@redhat.com \
--cc=peterx@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=yi.l.liu@intel.com \
--cc=zhenzhong.duan@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).