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 09/10] iommu/amd: Reorder attach device code
Date: Wed, 16 Oct 2024 05:35:00 +0000 [thread overview]
Message-ID: <20241016053501.97497-10-vasant.hegde@amd.com> (raw)
In-Reply-To: <20241016053501.97497-1-vasant.hegde@amd.com>
Ideally in attach device path, it should take dev_data lock before
making changes to device data including IOPF enablement. So far dev_data
was using spinlock and it was hitting lock order issue when it tries to
enable IOPF. Hence Commit 526606b0a199 ("iommu/amd: Fix Invalid wait
context issue") moved IOPF enablement outside dev_data->lock.
Previous patch converted dev_data lock to mutex. Now its safe to call
amd_iommu_iopf_add_device() with dev_data->mutex. Hence move back PCI
device capability enablement (ATS, PRI, PASID) and IOPF enablement code
inside the lock. Also in attach_device(), update 'dev_data->domain' at
the end so that error handling becomes simple.
Signed-off-by: Vasant Hegde <vasant.hegde@amd.com>
---
drivers/iommu/amd/iommu.c | 65 +++++++++++++++++----------------------
1 file changed, 29 insertions(+), 36 deletions(-)
diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c
index 2fefb7abb9ab..cd2371819635 100644
--- a/drivers/iommu/amd/iommu.c
+++ b/drivers/iommu/amd/iommu.c
@@ -2086,6 +2086,7 @@ static int attach_device(struct device *dev,
{
struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
struct amd_iommu *iommu = get_amd_iommu_from_dev_data(dev_data);
+ struct pci_dev *pdev;
int ret = 0;
mutex_lock(&dev_data->mutex);
@@ -2095,10 +2096,6 @@ static int attach_device(struct device *dev,
goto out;
}
- /* Update data structures */
- dev_data->domain = domain;
- list_add(&dev_data->list, &domain->dev_list);
-
/* Do reference counting */
ret = pdom_attach_iommu(iommu, domain);
if (ret)
@@ -2113,6 +2110,28 @@ static int attach_device(struct device *dev,
}
}
+ pdev = dev_is_pci(dev_data->dev) ? to_pci_dev(dev_data->dev) : NULL;
+ if (pdev && pdom_is_sva_capable(domain)) {
+ pdev_enable_caps(pdev);
+
+ /*
+ * Device can continue to function even if IOPF
+ * enablement failed. Hence in error path just
+ * disable device PRI support.
+ */
+ if (amd_iommu_iopf_add_device(iommu, dev_data))
+ pdev_disable_cap_pri(pdev);
+ } else if (pdev) {
+ pdev_enable_cap_ats(pdev);
+ }
+
+ /* Update data structures */
+ dev_data->domain = domain;
+ list_add(&dev_data->list, &domain->dev_list);
+
+ /* Update device table */
+ dev_update_dte(dev_data, true);
+
out:
mutex_unlock(&dev_data->mutex);
@@ -2127,7 +2146,6 @@ static void detach_device(struct device *dev)
struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
struct amd_iommu *iommu = get_amd_iommu_from_dev_data(dev_data);
struct protection_domain *domain = dev_data->domain;
- bool ppr = dev_data->ppr;
mutex_lock(&dev_data->mutex);
@@ -2140,13 +2158,15 @@ static void detach_device(struct device *dev)
if (WARN_ON(!dev_data->domain))
goto out;
- if (ppr) {
+ /* Remove IOPF handler */
+ if (dev_data->ppr) {
iopf_queue_flush_dev(dev);
-
- /* Updated here so that it gets reflected in DTE */
- dev_data->ppr = false;
+ amd_iommu_iopf_remove_device(iommu, dev_data);
}
+ if (dev_is_pci(dev))
+ pdev_disable_caps(to_pci_dev(dev));
+
/* Clear DTE and flush the entry */
dev_update_dte(dev_data, false);
@@ -2166,14 +2186,6 @@ static void detach_device(struct device *dev)
out:
mutex_unlock(&dev_data->mutex);
-
- /* Remove IOPF handler */
- if (ppr)
- amd_iommu_iopf_remove_device(iommu, dev_data);
-
- if (dev_is_pci(dev))
- pdev_disable_caps(to_pci_dev(dev));
-
}
static struct iommu_device *amd_iommu_probe_device(struct device *dev)
@@ -2450,7 +2462,6 @@ static int amd_iommu_attach_device(struct iommu_domain *dom,
struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
struct protection_domain *domain = to_pdomain(dom);
struct amd_iommu *iommu = get_amd_iommu_from_dev(dev);
- struct pci_dev *pdev;
int ret;
/*
@@ -2483,24 +2494,6 @@ static int amd_iommu_attach_device(struct iommu_domain *dom,
}
#endif
- pdev = dev_is_pci(dev_data->dev) ? to_pci_dev(dev_data->dev) : NULL;
- if (pdev && pdom_is_sva_capable(domain)) {
- pdev_enable_caps(pdev);
-
- /*
- * Device can continue to function even if IOPF
- * enablement failed. Hence in error path just
- * disable device PRI support.
- */
- if (amd_iommu_iopf_add_device(iommu, dev_data))
- pdev_disable_cap_pri(pdev);
- } else if (pdev) {
- pdev_enable_cap_ats(pdev);
- }
-
- /* Update device table */
- dev_update_dte(dev_data, true);
-
return ret;
}
--
2.31.1
next prev parent reply other threads:[~2024-10-16 5:38 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 ` [PATCH v3 06/10] iommu/amd: Reduce domain lock scope in attach device path Vasant Hegde
2024-10-17 13:32 ` 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 ` Vasant Hegde [this message]
2024-10-17 13:35 ` [PATCH v3 09/10] iommu/amd: Reorder attach device code 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-10-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