From: Lu Baolu <baolu.lu@linux.intel.com>
To: iommu@lists.linux-foundation.org, iommu@lists.linux.dev
Cc: Kevin Tian <kevin.tian@intel.com>,
Ashok Raj <ashok.raj@intel.com>, Liu Yi L <yi.l.liu@intel.com>,
Jacob jun Pan <jacob.jun.pan@intel.com>,
linux-kernel@vger.kernel.org, Lu Baolu <baolu.lu@linux.intel.com>
Subject: [PATCH v4 11/11] iommu/vt-d: Convert global spinlock into per domain lock
Date: Wed, 6 Jul 2022 10:55:24 +0800 [thread overview]
Message-ID: <20220706025524.2904370-12-baolu.lu@linux.intel.com> (raw)
In-Reply-To: <20220706025524.2904370-1-baolu.lu@linux.intel.com>
Using a global device_domain_lock spinlock to protect per-domain device
tracking lists is an inefficient way, especially considering this lock
is also needed in the hot paths. This optimizes the locking mechanism
by converting the global lock to per domain lock.
On the other hand, as the device tracking lists are never accessed in
any interrupt context, there is no need to disable interrupts while
spinning. Replace irqsave variant with spinlock calls.
Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
---
drivers/iommu/intel/iommu.h | 1 +
drivers/iommu/intel/iommu.c | 42 ++++++++++++++++++-------------------
2 files changed, 22 insertions(+), 21 deletions(-)
diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h
index 198c6c822ef4..df64d3d9c49a 100644
--- a/drivers/iommu/intel/iommu.h
+++ b/drivers/iommu/intel/iommu.h
@@ -541,6 +541,7 @@ struct dmar_domain {
u8 force_snooping : 1; /* Create IOPTEs with snoop control */
u8 set_pte_snp:1;
+ spinlock_t lock; /* Protect device tracking lists */
struct list_head devices; /* all devices' list */
struct dma_pte *pgd; /* virtual address */
diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
index 46991e313bf3..e007049eb9ff 100644
--- a/drivers/iommu/intel/iommu.c
+++ b/drivers/iommu/intel/iommu.c
@@ -310,7 +310,6 @@ static int iommu_skip_te_disable;
#define IDENTMAP_GFX 2
#define IDENTMAP_AZALIA 4
-static DEFINE_SPINLOCK(device_domain_lock);
const struct iommu_ops intel_iommu_ops;
static bool translation_pre_enabled(struct intel_iommu *iommu)
@@ -535,7 +534,7 @@ static int domain_update_device_node(struct dmar_domain *domain)
struct device_domain_info *info;
int nid = NUMA_NO_NODE;
- spin_lock(&device_domain_lock);
+ spin_lock(&domain->lock);
list_for_each_entry(info, &domain->devices, link) {
/*
* There could possibly be multiple device numa nodes as devices
@@ -547,7 +546,7 @@ static int domain_update_device_node(struct dmar_domain *domain)
if (nid != NUMA_NO_NODE)
break;
}
- spin_unlock(&device_domain_lock);
+ spin_unlock(&domain->lock);
return nid;
}
@@ -1378,15 +1377,15 @@ iommu_support_dev_iotlb(struct dmar_domain *domain, struct intel_iommu *iommu,
if (!iommu->qi)
return NULL;
- spin_lock(&device_domain_lock);
+ spin_lock(&domain->lock);
list_for_each_entry(info, &domain->devices, link) {
if (info->iommu == iommu && info->bus == bus &&
info->devfn == devfn) {
- spin_unlock(&device_domain_lock);
+ spin_unlock(&domain->lock);
return info->ats_supported ? info : NULL;
}
}
- spin_unlock(&device_domain_lock);
+ spin_unlock(&domain->lock);
return NULL;
}
@@ -1396,7 +1395,7 @@ static void domain_update_iotlb(struct dmar_domain *domain)
struct device_domain_info *info;
bool has_iotlb_device = false;
- spin_lock(&device_domain_lock);
+ spin_lock(&domain->lock);
list_for_each_entry(info, &domain->devices, link) {
if (info->ats_enabled) {
has_iotlb_device = true;
@@ -1404,7 +1403,7 @@ static void domain_update_iotlb(struct dmar_domain *domain)
}
}
domain->has_iotlb_device = has_iotlb_device;
- spin_unlock(&device_domain_lock);
+ spin_unlock(&domain->lock);
}
static void iommu_enable_dev_iotlb(struct device_domain_info *info)
@@ -1500,10 +1499,10 @@ static void iommu_flush_dev_iotlb(struct dmar_domain *domain,
if (!domain->has_iotlb_device)
return;
- spin_lock(&device_domain_lock);
+ spin_lock(&domain->lock);
list_for_each_entry(info, &domain->devices, link)
__iommu_flush_dev_iotlb(info, addr, mask);
- spin_unlock(&device_domain_lock);
+ spin_unlock(&domain->lock);
}
static void iommu_flush_iotlb_psi(struct intel_iommu *iommu,
@@ -1763,6 +1762,7 @@ static struct dmar_domain *alloc_domain(unsigned int type)
domain->flags |= DOMAIN_FLAG_USE_FIRST_LEVEL;
domain->has_iotlb_device = false;
INIT_LIST_HEAD(&domain->devices);
+ spin_lock_init(&domain->lock);
return domain;
}
@@ -2446,9 +2446,9 @@ static int domain_add_dev_info(struct dmar_domain *domain, struct device *dev)
if (ret)
return ret;
info->domain = domain;
- spin_lock(&device_domain_lock);
+ spin_lock(&domain->lock);
list_add(&info->link, &domain->devices);
- spin_unlock(&device_domain_lock);
+ spin_unlock(&domain->lock);
/* PASID table is mandatory for a PCI device in scalable mode. */
if (sm_supported(iommu) && !dev_is_real_dma_subdevice(dev)) {
@@ -4123,6 +4123,7 @@ static void domain_context_clear(struct device_domain_info *info)
static void dmar_remove_one_dev_info(struct device *dev)
{
struct device_domain_info *info = dev_iommu_priv_get(dev);
+ struct dmar_domain *domain = info->domain;
struct intel_iommu *iommu = info->iommu;
if (!dev_is_real_dma_subdevice(info->dev)) {
@@ -4135,11 +4136,11 @@ static void dmar_remove_one_dev_info(struct device *dev)
intel_pasid_free_table(info->dev);
}
- spin_lock(&device_domain_lock);
+ spin_lock(&domain->lock);
list_del(&info->link);
- spin_unlock(&device_domain_lock);
+ spin_unlock(&domain->lock);
- domain_detach_iommu(info->domain, iommu);
+ domain_detach_iommu(domain, iommu);
}
static int md_domain_init(struct dmar_domain *domain, int guest_width)
@@ -4421,7 +4422,7 @@ static bool domain_support_force_snooping(struct dmar_domain *domain)
struct device_domain_info *info;
bool support = true;
- assert_spin_locked(&device_domain_lock);
+ assert_spin_locked(&domain->lock);
list_for_each_entry(info, &domain->devices, link) {
if (!ecap_sc_support(info->iommu->ecap)) {
support = false;
@@ -4436,8 +4437,7 @@ static void domain_set_force_snooping(struct dmar_domain *domain)
{
struct device_domain_info *info;
- assert_spin_locked(&device_domain_lock);
-
+ assert_spin_locked(&domain->lock);
/*
* Second level page table supports per-PTE snoop control. The
* iommu_map() interface will handle this by setting SNP bit.
@@ -4459,15 +4459,15 @@ static bool intel_iommu_enforce_cache_coherency(struct iommu_domain *domain)
if (dmar_domain->force_snooping)
return true;
- spin_lock(&device_domain_lock);
+ spin_lock(&dmar_domain->lock);
if (!domain_support_force_snooping(dmar_domain)) {
- spin_unlock(&device_domain_lock);
+ spin_unlock(&dmar_domain->lock);
return false;
}
domain_set_force_snooping(dmar_domain);
dmar_domain->force_snooping = true;
- spin_unlock(&device_domain_lock);
+ spin_unlock(&dmar_domain->lock);
return true;
}
--
2.25.1
prev parent reply other threads:[~2022-07-06 3:01 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-06 2:55 [PATCH v4 00/11] iommu/vt-d: Optimize the use of locks Lu Baolu
2022-07-06 2:55 ` [PATCH v4 01/11] iommu/vt-d: debugfs: Remove device_domain_lock usage Lu Baolu
[not found] ` <849557ab-9c1b-a2ff-6fc6-6564f38d1187@linux.intel.com>
2022-07-07 9:18 ` Baolu Lu
2022-07-06 2:55 ` [PATCH v4 02/11] iommu/vt-d: Remove clearing translation data in disable_dmar_iommu() Lu Baolu
2022-07-07 3:03 ` Tian, Kevin
2022-07-06 2:55 ` [PATCH v4 03/11] iommu/vt-d: Use pci_get_domain_bus_and_slot() in pgtable_walk() Lu Baolu
2022-07-06 2:55 ` [PATCH v4 04/11] iommu/vt-d: Unnecessary spinlock for root table alloc and free Lu Baolu
2022-07-06 2:55 ` [PATCH v4 05/11] iommu/vt-d: Replace spin_lock_irqsave() with spin_lock() Lu Baolu
2022-07-06 2:55 ` [PATCH v4 06/11] iommu/vt-d: Acquiring lock in domain ID allocation helpers Lu Baolu
2022-07-06 2:55 ` [PATCH v4 07/11] iommu/vt-d: Acquiring lock in pasid manipulation helpers Lu Baolu
2022-07-06 2:55 ` [PATCH v4 08/11] iommu/vt-d: Check device list of domain in domain free path Lu Baolu
2022-07-06 2:55 ` [PATCH v4 09/11] iommu/vt-d: Fold __dmar_remove_one_dev_info() into its caller Lu Baolu
2022-07-06 2:55 ` [PATCH v4 10/11] iommu/vt-d: Use device_domain_lock accurately Lu Baolu
2022-07-06 2:55 ` Lu Baolu [this message]
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=20220706025524.2904370-12-baolu.lu@linux.intel.com \
--to=baolu.lu@linux.intel.com \
--cc=ashok.raj@intel.com \
--cc=iommu@lists.linux-foundation.org \
--cc=iommu@lists.linux.dev \
--cc=jacob.jun.pan@intel.com \
--cc=kevin.tian@intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=yi.l.liu@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