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>, <jgg@ziepe.ca>,
Vasant Hegde <vasant.hegde@amd.com>,
Joerg Roedel <jroedel@suse.de>, Jason Gunthorpe <jgg@nvidia.com>
Subject: [PATCH v5 08/12] iommu/amd: Rearrange attach device code
Date: Wed, 30 Oct 2024 06:35:52 +0000 [thread overview]
Message-ID: <20241030063556.6104-9-vasant.hegde@amd.com> (raw)
In-Reply-To: <20241030063556.6104-1-vasant.hegde@amd.com>
attach_device() is just holding lock and calling do_attach(). There is
not need to have another function. Just move do_attach() code to
attach_device(). Similary move do_detach() code to detach_device().
Signed-off-by: Vasant Hegde <vasant.hegde@amd.com>
Reviewed-by: Joerg Roedel <jroedel@suse.de>
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
---
drivers/iommu/amd/iommu.c | 91 ++++++++++++++++-----------------------
1 file changed, 36 insertions(+), 55 deletions(-)
diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c
index f5ba715c8efc..7cf6f44ac90c 100644
--- a/drivers/iommu/amd/iommu.c
+++ b/drivers/iommu/amd/iommu.c
@@ -2081,12 +2081,24 @@ static void pdom_detach_iommu(struct amd_iommu *iommu,
spin_unlock_irqrestore(&pdom->lock, flags);
}
-static int do_attach(struct iommu_dev_data *dev_data,
- struct protection_domain *domain)
+/*
+ * If a device is not yet associated with a domain, this function makes the
+ * device visible in the domain
+ */
+static int attach_device(struct device *dev,
+ struct protection_domain *domain)
{
+ struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
struct amd_iommu *iommu = get_amd_iommu_from_dev_data(dev_data);
int ret = 0;
+ spin_lock(&dev_data->lock);
+
+ if (dev_data->domain != NULL) {
+ ret = -EBUSY;
+ goto out;
+ }
+
/* Update data structures */
dev_data->domain = domain;
list_add(&dev_data->list, &domain->dev_list);
@@ -2094,67 +2106,17 @@ static int do_attach(struct iommu_dev_data *dev_data,
/* Do reference counting */
ret = pdom_attach_iommu(iommu, domain);
if (ret)
- return ret;
+ goto out;
/* Setup GCR3 table */
if (pdom_is_sva_capable(domain)) {
ret = init_gcr3_table(dev_data, domain);
if (ret) {
pdom_detach_iommu(iommu, domain);
- return ret;
+ goto out;
}
}
- return ret;
-}
-
-static void do_detach(struct iommu_dev_data *dev_data)
-{
- struct protection_domain *domain = dev_data->domain;
- struct amd_iommu *iommu = get_amd_iommu_from_dev_data(dev_data);
- unsigned long flags;
-
- /* Clear DTE and flush the entry */
- dev_update_dte(dev_data, false);
-
- /* Flush IOTLB and wait for the flushes to finish */
- spin_lock_irqsave(&domain->lock, flags);
- amd_iommu_domain_flush_all(domain);
- spin_unlock_irqrestore(&domain->lock, flags);
-
- /* Clear GCR3 table */
- if (pdom_is_sva_capable(domain))
- destroy_gcr3_table(dev_data, domain);
-
- /* Update data structures */
- dev_data->domain = NULL;
- list_del(&dev_data->list);
-
- /* decrease reference counters - needs to happen after the flushes */
- pdom_detach_iommu(iommu, domain);
-}
-
-/*
- * If a device is not yet associated with a domain, this function makes the
- * device visible in the domain
- */
-static int attach_device(struct device *dev,
- struct protection_domain *domain)
-{
- struct iommu_dev_data *dev_data;
- int ret = 0;
-
- dev_data = dev_iommu_priv_get(dev);
-
- spin_lock(&dev_data->lock);
-
- if (dev_data->domain != NULL) {
- ret = -EBUSY;
- goto out;
- }
-
- ret = do_attach(dev_data, domain);
-
out:
spin_unlock(&dev_data->lock);
@@ -2168,7 +2130,9 @@ 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;
+ unsigned long flags;
spin_lock(&dev_data->lock);
@@ -2188,7 +2152,24 @@ static void detach_device(struct device *dev)
dev_data->ppr = false;
}
- do_detach(dev_data);
+ /* Clear DTE and flush the entry */
+ dev_update_dte(dev_data, false);
+
+ /* Flush IOTLB and wait for the flushes to finish */
+ spin_lock_irqsave(&domain->lock, flags);
+ amd_iommu_domain_flush_all(domain);
+ spin_unlock_irqrestore(&domain->lock, flags);
+
+ /* Clear GCR3 table */
+ if (pdom_is_sva_capable(domain))
+ destroy_gcr3_table(dev_data, domain);
+
+ /* Update data structures */
+ dev_data->domain = NULL;
+ list_del(&dev_data->list);
+
+ /* decrease reference counters - needs to happen after the flushes */
+ pdom_detach_iommu(iommu, domain);
out:
spin_unlock(&dev_data->lock);
--
2.31.1
next prev parent reply other threads:[~2024-10-30 6:48 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-30 6:35 [PATCH v5 00/12] iommu/amd: Improve domain allocator and device attach code path Vasant Hegde
2024-10-30 6:35 ` [PATCH v5 01/12] iommu/amd/pgtbl_v2: Take protection domain lock before invalidating TLB Vasant Hegde
2024-10-30 6:35 ` [PATCH v5 02/12] iommu/amd: Use ida interface to manage protection domain ID Vasant Hegde
2024-10-30 6:35 ` [PATCH v5 03/12] iommu/amd: Remove protection_domain.dev_cnt variable Vasant Hegde
2024-10-30 6:35 ` [PATCH v5 04/12] iommu/amd: xarray to track protection_domain->iommu list Vasant Hegde
2024-10-30 6:35 ` [PATCH v5 05/12] iommu/amd: Remove unused amd_iommus variable Vasant Hegde
2024-10-30 6:35 ` [PATCH v5 06/12] iommu/amd: Do not detach devices in domain free path Vasant Hegde
2024-10-30 6:35 ` [PATCH v5 07/12] iommu/amd: Reduce domain lock scope in attach device path Vasant Hegde
2024-10-30 6:35 ` Vasant Hegde [this message]
2024-10-30 6:35 ` [PATCH v5 09/12] iommu/amd: Convert dev_data lock from spinlock to mutex Vasant Hegde
2024-10-30 6:35 ` [PATCH v5 10/12] iommu/amd: Reorder attach device code Vasant Hegde
2024-10-30 6:35 ` [PATCH v5 11/12] iommu/amd: Add ops->release_domain Vasant Hegde
2024-10-30 6:35 ` [PATCH v5 12/12] iommu/amd: Improve amd_iommu_release_device() Vasant Hegde
2024-10-30 10:24 ` [PATCH v5 00/12] iommu/amd: Improve domain allocator and device attach code path Joerg Roedel
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=20241030063556.6104-9-vasant.hegde@amd.com \
--to=vasant.hegde@amd.com \
--cc=iommu@lists.linux.dev \
--cc=jgg@nvidia.com \
--cc=jgg@ziepe.ca \
--cc=joro@8bytes.org \
--cc=jroedel@suse.de \
--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