All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jason Gunthorpe <jgg@nvidia.com>
To: iommu@lists.linux.dev, Joerg Roedel <joro@8bytes.org>,
	Robin Murphy <robin.murphy@arm.com>,
	Will Deacon <will@kernel.org>
Cc: Lu Baolu <baolu.lu@linux.intel.com>,
	Kevin Tian <kevin.tian@intel.com>,
	Nicolin Chen <nicolinc@nvidia.com>
Subject: [PATCH 6/9] iommu: Fix iommu_probe_device() to attach the right domain
Date: Tue, 21 Mar 2023 16:53:18 -0300	[thread overview]
Message-ID: <6-v1-20507a7e6b7e+2d6-iommu_err_unwind_jgg@nvidia.com> (raw)
In-Reply-To: <0-v1-20507a7e6b7e+2d6-iommu_err_unwind_jgg@nvidia.com>

The general invariant is that all devices in an iommu_group are attached
to group->domain. We missed some cases here where an owned group would not
get the device attached.

Rework this logic so it follows the default domain flow of the
bus_iommu_probe() - call iommu_alloc_default_domain(), then use
__iommu_group_set_domain_internal() to set up all the devices.

Finally always attach the device to the current domain if it is already
set.

Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 drivers/iommu/iommu.c | 45 ++++++++++++++++++++++++-------------------
 1 file changed, 25 insertions(+), 20 deletions(-)

diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index ea7700bbcf3399..d58570f361bb92 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -429,27 +429,32 @@ int iommu_probe_device(struct device *dev)
 		goto err_release;
 	}
 
-	/*
-	 * Try to allocate a default domain - needs support from the
-	 * IOMMU driver. There are still some drivers which don't
-	 * support default domains, so the return value is not yet
-	 * checked.
-	 */
 	mutex_lock(&group->mutex);
-	iommu_alloc_default_domain(group, dev);
 
-	/*
-	 * If device joined an existing group which has been claimed, don't
-	 * attach the default domain.
-	 */
-	if (group->default_domain && !group->owner) {
+	if (group->domain) {
 		ret = iommu_group_do_dma_first_attach(group, dev);
-		if (ret) {
-			mutex_unlock(&group->mutex);
-			iommu_group_put(group);
-			goto err_release;
-		}
+	} else if (!group->default_domain) {
+		/*
+		 * Try to allocate a default domain - needs support from the
+		 * IOMMU driver. There are still some drivers which don't
+		 * support default domains, so the return value is not yet
+		 * checked.
+		 */
+		iommu_alloc_default_domain(group, dev);
+		group->domain = NULL;
+		if (group->default_domain)
+			ret = __iommu_group_set_domain_internal(
+				group, group->default_domain,
+				IOMMU_SET_DOMAIN_WITH_DEFERRED);
+
+		/*
+		 * We assume that the iommu driver starts up the device in
+		 * 'set_platform_dma_ops' mode if it does not support default
+		 * domains.
+		 */
 	}
+	if (ret)
+		goto err_unlock;
 
 	iommu_create_device_direct_mappings(group, dev);
 
@@ -462,6 +467,9 @@ int iommu_probe_device(struct device *dev)
 
 	return 0;
 
+err_unlock:
+	mutex_unlock(&group->mutex);
+	iommu_group_put(group);
 err_release:
 	iommu_release_device(dev);
 
@@ -1676,9 +1684,6 @@ static int iommu_alloc_default_domain(struct iommu_group *group,
 {
 	unsigned int type;
 
-	if (group->default_domain)
-		return 0;
-
 	type = iommu_get_def_domain_type(dev) ? : iommu_def_domain_type;
 
 	return iommu_group_alloc_default_domain(dev->bus, group, type);
-- 
2.40.0


  parent reply	other threads:[~2023-03-21 19:53 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-21 19:53 [PATCH 0/9] Consolidate the error handling around device attachment Jason Gunthorpe
2023-03-21 19:53 ` [PATCH 1/9] iommu: Make __iommu_group_set_domain() handle error unwind Jason Gunthorpe
2023-03-24  7:17   ` Tian, Kevin
2023-03-24 15:17     ` Jason Gunthorpe
2023-03-21 19:53 ` [PATCH 2/9] iommu: Use __iommu_group_set_domain() for __iommu_attach_group() Jason Gunthorpe
2023-03-24  7:18   ` Tian, Kevin
2023-03-21 19:53 ` [PATCH 3/9] iommu: Use __iommu_group_set_domain() in iommu_change_dev_def_domain() Jason Gunthorpe
2023-03-24  7:19   ` Tian, Kevin
2023-03-24 13:17     ` Jason Gunthorpe
2023-03-21 19:53 ` [PATCH 4/9] iommu: Replace __iommu_group_dma_first_attach() with set_domain Jason Gunthorpe
2023-03-24  7:20   ` Tian, Kevin
2023-03-21 19:53 ` [PATCH 5/9] iommu: Make iommu_group_do_dma_first_attach() simpler Jason Gunthorpe
2023-03-24  7:23   ` Tian, Kevin
2023-03-24 15:23     ` Jason Gunthorpe
2023-03-21 19:53 ` Jason Gunthorpe [this message]
2023-03-24  7:29   ` [PATCH 6/9] iommu: Fix iommu_probe_device() to attach the right domain Tian, Kevin
2023-03-24 15:42     ` Jason Gunthorpe
2023-03-24 19:36     ` Jason Gunthorpe
2023-03-21 19:53 ` [PATCH 7/9] iommu: Remove the assignment of group->domain during default domain alloc Jason Gunthorpe
2023-03-24  7:30   ` Tian, Kevin
2023-03-21 19:53 ` [PATCH 8/9] iommu: Consolidate the default_domain setup to one function Jason Gunthorpe
2023-03-21 19:53 ` [PATCH 9/9] iommu: Remove __iommu_group_for_each_dev() Jason Gunthorpe
2023-03-22 13:08   ` Joerg Roedel
2023-03-22 13:41     ` Jason Gunthorpe

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=6-v1-20507a7e6b7e+2d6-iommu_err_unwind_jgg@nvidia.com \
    --to=jgg@nvidia.com \
    --cc=baolu.lu@linux.intel.com \
    --cc=iommu@lists.linux.dev \
    --cc=joro@8bytes.org \
    --cc=kevin.tian@intel.com \
    --cc=nicolinc@nvidia.com \
    --cc=robin.murphy@arm.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.