From: Vasant Hegde <vasant.hegde@amd.com>
To: <iommu@lists.linux.dev>, <joro@8bytes.org>
Cc: <will@kernel.org>, <robin.murphy@arm.com>,
<suravee.suthikulpanit@amd.com>,
Vasant Hegde <vasant.hegde@amd.com>
Subject: [PATCH v3 06/10] iommu/amd: Reduce domain lock scope in attach device path
Date: Wed, 16 Oct 2024 05:34:57 +0000 [thread overview]
Message-ID: <20241016053501.97497-7-vasant.hegde@amd.com> (raw)
In-Reply-To: <20241016053501.97497-1-vasant.hegde@amd.com>
Currently attach device path takes protection domain lock followed by
dev_data lock. Most of the operations in this function is specific to
device data except pdom_attach_iommu() where it updates protection
domain structure. Hence reduce the scope of protection domain lock.
Note that this changes the locking order. Now it takes device lock
before taking domain lock (group->mutex -> dev_data->lock ->
pdom->lock). dev_data->lock is used only in device attachment path.
So changing order is fine. It will not create any issue.
Finally move numa node assignment to pdom_attach_iommu().
Signed-off-by: Vasant Hegde <vasant.hegde@amd.com>
---
drivers/iommu/amd/iommu.c | 49 +++++++++++++++++++++------------------
1 file changed, 27 insertions(+), 22 deletions(-)
diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c
index d74d3b65c939..a738d2d7f0c4 100644
--- a/drivers/iommu/amd/iommu.c
+++ b/drivers/iommu/amd/iommu.c
@@ -2016,16 +2016,23 @@ static int pdom_attach_iommu(struct amd_iommu *iommu,
struct protection_domain *pdom)
{
struct pdom_iommu_info *pdom_iommu_info, *curr;
+ struct io_pgtable_cfg *cfg = &pdom->iop.pgtbl.cfg;
+ unsigned long flags;
+ int ret = 0;
+
+ spin_lock_irqsave(&pdom->lock, flags);
pdom_iommu_info = xa_load(&pdom->iommu_array, iommu->index);
if (pdom_iommu_info) {
pdom_iommu_info->refcnt++;
- return 0;
+ goto out_unlock;
}
pdom_iommu_info = kzalloc(sizeof(*pdom_iommu_info), GFP_ATOMIC);
- if (!pdom_iommu_info)
- return -ENOMEM;
+ if (!pdom_iommu_info) {
+ ret = -ENOMEM;
+ goto out_unlock;
+ }
pdom_iommu_info->iommu = iommu;
pdom_iommu_info->refcnt = 1;
@@ -2034,43 +2041,52 @@ static int pdom_attach_iommu(struct amd_iommu *iommu,
NULL, pdom_iommu_info, GFP_ATOMIC);
if (curr) {
kfree(pdom_iommu_info);
- return -ENOSPC;
+ ret = -ENOSPC;
+ goto out_unlock;
}
- return 0;
+ /* Update NUMA Node ID */
+ if (cfg->amd.nid == NUMA_NO_NODE)
+ cfg->amd.nid = dev_to_node(&iommu->dev->dev);
+
+out_unlock:
+ spin_unlock_irqrestore(&pdom->lock, flags);
+ return ret;
}
static void pdom_detach_iommu(struct amd_iommu *iommu,
struct protection_domain *pdom)
{
struct pdom_iommu_info *pdom_iommu_info;
+ unsigned long flags;
+
+ spin_lock_irqsave(&pdom->lock, flags);
pdom_iommu_info = xa_load(&pdom->iommu_array, iommu->index);
- if (!pdom_iommu_info)
+ if (!pdom_iommu_info) {
+ spin_unlock_irqrestore(&pdom->lock, flags);
return;
+ }
pdom_iommu_info->refcnt--;
if (pdom_iommu_info->refcnt == 0) {
xa_erase(&pdom->iommu_array, iommu->index);
kfree(pdom_iommu_info);
}
+
+ spin_unlock_irqrestore(&pdom->lock, flags);
}
static int do_attach(struct iommu_dev_data *dev_data,
struct protection_domain *domain)
{
struct amd_iommu *iommu = get_amd_iommu_from_dev_data(dev_data);
- struct io_pgtable_cfg *cfg = &domain->iop.pgtbl.cfg;
int ret = 0;
/* Update data structures */
dev_data->domain = domain;
list_add(&dev_data->list, &domain->dev_list);
- /* Update NUMA Node ID */
- if (cfg->amd.nid == NUMA_NO_NODE)
- cfg->amd.nid = dev_to_node(dev_data->dev);
-
/* Do reference counting */
ret = pdom_attach_iommu(iommu, domain);
if (ret)
@@ -2119,11 +2135,8 @@ static int attach_device(struct device *dev,
struct protection_domain *domain)
{
struct iommu_dev_data *dev_data;
- unsigned long flags;
int ret = 0;
- spin_lock_irqsave(&domain->lock, flags);
-
dev_data = dev_iommu_priv_get(dev);
spin_lock(&dev_data->lock);
@@ -2138,8 +2151,6 @@ static int attach_device(struct device *dev,
out:
spin_unlock(&dev_data->lock);
- spin_unlock_irqrestore(&domain->lock, flags);
-
return ret;
}
@@ -2149,13 +2160,9 @@ static int attach_device(struct device *dev,
static void detach_device(struct device *dev)
{
struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
- struct protection_domain *domain = dev_data->domain;
struct amd_iommu *iommu = get_amd_iommu_from_dev_data(dev_data);
- unsigned long flags;
bool ppr = dev_data->ppr;
- spin_lock_irqsave(&domain->lock, flags);
-
spin_lock(&dev_data->lock);
/*
@@ -2179,8 +2186,6 @@ static void detach_device(struct device *dev)
out:
spin_unlock(&dev_data->lock);
- spin_unlock_irqrestore(&domain->lock, flags);
-
/* Remove IOPF handler */
if (ppr)
amd_iommu_iopf_remove_device(iommu, dev_data);
--
2.31.1
next prev parent reply other threads:[~2024-10-16 5:37 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-16 5:34 [PATCH v3 00/10] iommu/amd: Improve domain allocator and device attach code path Vasant Hegde
2024-10-16 5:34 ` [PATCH v3 01/10] iommu/amd: Use ida interface to manage protection domain ID Vasant Hegde
2024-10-17 13:14 ` Jason Gunthorpe
2024-10-25 12:16 ` Vasant Hegde
2024-10-16 5:34 ` [PATCH v3 02/10] iommu/amd: Remove protection_domain.dev_cnt variable Vasant Hegde
2024-10-17 13:14 ` Jason Gunthorpe
2024-10-16 5:34 ` [PATCH v3 03/10] iommu/amd: xarray to track protection_domain->iommu list Vasant Hegde
2024-10-17 13:19 ` Jason Gunthorpe
2024-10-25 11:23 ` Vasant Hegde
2024-10-16 5:34 ` [PATCH v3 04/10] iommu/amd: Remove unused amd_iommus variable Vasant Hegde
2024-10-17 13:20 ` Jason Gunthorpe
2024-10-16 5:34 ` [PATCH v3 05/10] iommu/amd: Do not detach devices in domain free path Vasant Hegde
2024-10-17 13:22 ` Jason Gunthorpe
2024-10-16 5:34 ` Vasant Hegde [this message]
2024-10-17 13:32 ` [PATCH v3 06/10] iommu/amd: Reduce domain lock scope in attach device path Jason Gunthorpe
2024-10-18 2:38 ` Baolu Lu
2024-10-18 11:54 ` Jason Gunthorpe
2024-10-21 4:58 ` Baolu Lu
2024-10-24 14:41 ` Vasant Hegde
2024-10-16 5:34 ` [PATCH v3 07/10] iommu/amd: Rearrange attach device code Vasant Hegde
2024-10-17 13:33 ` Jason Gunthorpe
2024-10-16 5:34 ` [PATCH v3 08/10] iommu/amd: Convert dev_data lock from spinlock to mutex Vasant Hegde
2024-10-17 13:33 ` Jason Gunthorpe
2024-10-16 5:35 ` [PATCH v3 09/10] iommu/amd: Reorder attach device code Vasant Hegde
2024-10-17 13:35 ` Jason Gunthorpe
2024-10-16 5:35 ` [PATCH v3 10/10] iommu/amd: Improve amd_iommu_release_device() Vasant Hegde
2024-10-17 13:39 ` Jason Gunthorpe
2024-10-24 14:30 ` Vasant Hegde
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=20241016053501.97497-7-vasant.hegde@amd.com \
--to=vasant.hegde@amd.com \
--cc=iommu@lists.linux.dev \
--cc=joro@8bytes.org \
--cc=robin.murphy@arm.com \
--cc=suravee.suthikulpanit@amd.com \
--cc=will@kernel.org \
/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