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>,
	llvm@lists.linux.dev, Nathan Chancellor <nathan@kernel.org>,
	Nick Desaulniers <ndesaulniers@google.com>,
	Miguel Ojeda <ojeda@kernel.org>,
	Robin Murphy <robin.murphy@arm.com>, Tom Rix <trix@redhat.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 v2 12/14] iommu: Consolidate the default_domain setup to one function
Date: Wed, 29 Mar 2023 20:40:36 -0300	[thread overview]
Message-ID: <12-v2-cd32667d2ba6+70bd1-iommu_err_unwind_jgg@nvidia.com> (raw)
In-Reply-To: <0-v2-cd32667d2ba6+70bd1-iommu_err_unwind_jgg@nvidia.com>

The default_domain setup flow requires
 - Determining the default type
 - Allocating a default_domain
 - Attaching it to devices
 - Doing iommu_create_device_direct_mappings()

This is sprinkled around three places written in two different ways. Make
it all the same in one function.

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

diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 79e3fa80d0f6cc..26129423fa7b95 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -91,8 +91,6 @@ static const char * const iommu_group_resv_type_string[] = {
 
 static int iommu_bus_notifier(struct notifier_block *nb,
 			      unsigned long action, void *data);
-static int iommu_alloc_default_domain(struct iommu_group *group,
-				      struct device *dev);
 static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
 						 unsigned type);
 static int __iommu_attach_device(struct iommu_domain *domain,
@@ -124,7 +122,9 @@ static void __iommu_group_set_domain_nofail(struct iommu_group *group,
 		group, new_domain, IOMMU_SET_DOMAIN_MUST_SUCCEED));
 }
 
-static int iommu_create_device_direct_mappings(struct iommu_group *group,
+static int iommu_setup_default_domain(struct iommu_group *group,
+				      int target_type);
+static int iommu_create_device_direct_mappings(struct iommu_domain *domain,
 					       struct device *dev);
 static struct iommu_group *iommu_group_get_for_dev(struct device *dev);
 static ssize_t iommu_group_store_type(struct iommu_group *group,
@@ -436,29 +436,14 @@ int iommu_probe_device(struct device *dev)
 
 	if (group->domain) {
 		ret = iommu_group_do_dma_first_attach(group, dev);
+		if (ret)
+			goto err_unlock;
+		iommu_create_device_direct_mappings(group->domain, dev);
 	} 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);
-		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.
-		 */
+		ret = iommu_setup_default_domain(group, 0);
+		if (ret)
+			goto err_unlock;
 	}
-	if (ret)
-		goto err_unlock;
-
-	iommu_create_device_direct_mappings(group, dev);
 
 	mutex_unlock(&group->mutex);
 	iommu_group_put(group);
@@ -1004,16 +989,15 @@ int iommu_group_set_name(struct iommu_group *group, const char *name)
 }
 EXPORT_SYMBOL_GPL(iommu_group_set_name);
 
-static int iommu_create_device_direct_mappings(struct iommu_group *group,
+static int iommu_create_device_direct_mappings(struct iommu_domain *domain,
 					       struct device *dev)
 {
-	struct iommu_domain *domain = group->default_domain;
 	struct iommu_resv_region *entry;
 	struct list_head mappings;
 	unsigned long pg_size;
 	int ret = 0;
 
-	if (!domain || !iommu_is_dma_domain(domain))
+	if (!iommu_is_dma_domain(domain))
 		return 0;
 
 	BUG_ON(!domain->pgsize_bitmap);
@@ -1696,37 +1680,6 @@ static int iommu_get_def_domain_type(struct device *dev)
 	return 0;
 }
 
-static int iommu_group_alloc_default_domain(struct bus_type *bus,
-					    struct iommu_group *group,
-					    unsigned int type)
-{
-	struct iommu_domain *dom;
-
-	dom = __iommu_domain_alloc(bus, type);
-	if (!dom && type != IOMMU_DOMAIN_DMA) {
-		dom = __iommu_domain_alloc(bus, IOMMU_DOMAIN_DMA);
-		if (dom)
-			pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA",
-				type, group->name);
-	}
-
-	if (!dom)
-		return -ENOMEM;
-
-	group->default_domain = dom;
-	return 0;
-}
-
-static int iommu_alloc_default_domain(struct iommu_group *group,
-				      struct device *dev)
-{
-	unsigned int type;
-
-	type = iommu_get_def_domain_type(dev) ? : iommu_def_domain_type;
-
-	return iommu_group_alloc_default_domain(dev->bus, group, type);
-}
-
 /**
  * iommu_group_get_for_dev - Find or create the IOMMU group for a device
  * @dev: target device
@@ -1848,6 +1801,84 @@ static int iommu_get_default_domain_type(struct iommu_group *group,
 	return best_type;
 }
 
+/**
+ * iommu_setup_default_domain - Set the default_domain for the group
+ * @group: Group to change
+ * @target_type: Domain type to set as the default_domain
+ *
+ * Allocate a default domain and set it as the current domain on the group. If
+ * the group already has a default domain it will be changed to the target_type.
+ * When target_type is 0 the default domain is selected based on driver and
+ * system preferences.
+ */
+static int iommu_setup_default_domain(struct iommu_group *group,
+				      int target_type)
+{
+	struct group_device *gdev;
+	struct iommu_domain *dom;
+	struct bus_type *bus =
+		list_first_entry(&group->devices, struct group_device, list)
+			->dev->bus;
+	int ret;
+
+	lockdep_assert_held(&group->mutex);
+
+	target_type = iommu_get_default_domain_type(group, target_type);
+	if (target_type < 0)
+		return -EINVAL;
+
+	if (group->default_domain && group->default_domain->type == target_type)
+		return 0;
+
+	dom = __iommu_domain_alloc(bus, target_type);
+	if (!dom && target_type != IOMMU_DOMAIN_DMA) {
+		dom = __iommu_domain_alloc(bus, IOMMU_DOMAIN_DMA);
+		if (dom)
+			pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA",
+				target_type, group->name);
+	}
+
+	/*
+	 * There are still some drivers which don't support default domains, so
+	 * we ignore the failure and leave group->default_domain NULL.
+	 *
+	 * We assume that the iommu driver starts up the device in
+	 * 'set_platform_dma_ops' mode if it does not support default domains.
+	 */
+	if (!dom) {
+		ret = 0;
+		goto out_set;
+	}
+
+	ret = __iommu_group_set_domain_internal(group, dom,
+						IOMMU_SET_DOMAIN_WITH_DEFERRED);
+	if (ret) {
+		/*
+		 * An attach_dev failure may result in some devices being left
+		 * attached to dom. This is not cleaned up until release_device
+		 * is called. Thus we can't always free dom on failure, we have
+		 * no choice but to stick the broken domain into
+		 * group->default_domain to defer the free and try to continue.
+		 */
+		if (list_count_nodes(&group->devices) > 1)
+			goto out_set;
+
+		iommu_domain_free(dom);
+		dom = NULL;
+		goto out_set;
+	}
+
+	/* The domain must be attached before we can establish any mappings */
+	for_each_group_device(group, gdev)
+		iommu_create_device_direct_mappings(dom, gdev->dev);
+
+out_set:
+	if (group->default_domain)
+		iommu_domain_free(group->default_domain);
+	group->default_domain = dom;
+	return ret;
+}
+
 static int iommu_group_do_probe_finalize(struct device *dev, void *data)
 {
 	const struct iommu_ops *ops = dev_iommu_ops(dev);
@@ -1864,21 +1895,6 @@ static void __iommu_group_dma_finalize(struct iommu_group *group)
 				   iommu_group_do_probe_finalize);
 }
 
-static int iommu_do_create_direct_mappings(struct device *dev, void *data)
-{
-	struct iommu_group *group = data;
-
-	iommu_create_device_direct_mappings(group, dev);
-
-	return 0;
-}
-
-static int iommu_group_create_direct_mappings(struct iommu_group *group)
-{
-	return __iommu_group_for_each_dev(group, group,
-					  iommu_do_create_direct_mappings);
-}
-
 int bus_iommu_probe(struct bus_type *bus)
 {
 	struct iommu_group *group, *next;
@@ -1900,30 +1916,18 @@ int bus_iommu_probe(struct bus_type *bus)
 		/* Remove item from the list */
 		list_del_init(&group->entry);
 
-		/* Try to allocate default domain */
-		iommu_group_alloc_default_domain(
-			bus, group, iommu_get_default_domain_type(group, 0));
-
 		if (!group->default_domain) {
-			mutex_unlock(&group->mutex);
-			continue;
+			ret = iommu_setup_default_domain(group, 0);
+			if (ret) {
+				mutex_unlock(&group->mutex);
+				return ret;
+			}
 		}
-
-		iommu_group_create_direct_mappings(group);
-
-		ret = __iommu_group_set_domain_internal(
-			group, group->default_domain,
-			IOMMU_SET_DOMAIN_WITH_DEFERRED);
-
 		mutex_unlock(&group->mutex);
-
-		if (ret)
-			break;
-
 		__iommu_group_dma_finalize(group);
 	}
 
-	return ret;
+	return 0;
 }
 
 bool iommu_present(struct bus_type *bus)
@@ -2905,69 +2909,6 @@ int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
 }
 EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);
 
-/*
- * Changes the default domain of an iommu group
- *
- * @group: The group for which the default domain should be changed
- * @dev: The first device in the group
- * @type: The type of the new default domain that gets associated with the group
- *
- * Returns 0 on success and error code on failure
- *
- * Note:
- * 1. Presently, this function is called only when user requests to change the
- *    group's default domain type through /sys/kernel/iommu_groups/<grp_id>/type
- *    Please take a closer look if intended to use for other purposes.
- */
-static int iommu_change_dev_def_domain(struct iommu_group *group,
-				       struct device *dev, int type)
-{
-	struct iommu_domain *prev_dom;
-	int ret;
-
-	lockdep_assert_held(&group->mutex);
-
-	prev_dom = group->default_domain;
-	type = iommu_get_default_domain_type(group, type);
-	if (type < 0)
-		return -EINVAL;
-
-	/*
-	 * Switch to a new domain only if the requested domain type is different
-	 * from the existing default domain type
-	 */
-	if (prev_dom->type == type)
-		return 0;
-
-	group->default_domain = NULL;
-	group->domain = NULL;
-
-	/* Sets group->default_domain to the newly allocated domain */
-	ret = iommu_group_alloc_default_domain(dev->bus, group, type);
-	if (ret)
-		goto restore_old_domain;
-
-	group->domain = prev_dom;
-	ret = iommu_create_device_direct_mappings(group, dev);
-	if (ret)
-		goto free_new_domain;
-
-	ret = __iommu_group_set_domain(group, group->default_domain);
-	if (ret)
-		goto free_new_domain;
-
-	iommu_domain_free(prev_dom);
-
-	return 0;
-
-free_new_domain:
-	iommu_domain_free(group->default_domain);
-restore_old_domain:
-	group->default_domain = prev_dom;
-
-	return ret;
-}
-
 /*
  * Changing the default domain through sysfs requires the users to unbind the
  * drivers from the devices in the iommu group, except for a DMA -> DMA-FQ
@@ -2980,8 +2921,6 @@ static int iommu_change_dev_def_domain(struct iommu_group *group,
 static ssize_t iommu_group_store_type(struct iommu_group *group,
 				      const char *buf, size_t count)
 {
-	struct group_device *grp_dev;
-	struct device *dev;
 	int ret, req_type;
 
 	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
@@ -3019,10 +2958,7 @@ static ssize_t iommu_group_store_type(struct iommu_group *group,
 		return -EPERM;
 	}
 
-	grp_dev = list_first_entry(&group->devices, struct group_device, list);
-	dev = grp_dev->dev;
-
-	ret = iommu_change_dev_def_domain(group, dev, req_type);
+	ret = iommu_setup_default_domain(group, req_type);
 
 	/*
 	 * Release the mutex here because ops->probe_finalize() call-back of
-- 
2.40.0


  parent reply	other threads:[~2023-03-29 23:40 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-29 23:40 [PATCH v2 00/14] Consolidate the error handling around device attachment Jason Gunthorpe
2023-03-29 23:40 ` [PATCH v2 01/14] iommu: Replace iommu_group_device_count() with list_count_nodes() Jason Gunthorpe
2023-03-30  6:22   ` Baolu Lu
2023-04-04  9:15   ` Tian, Kevin
2023-03-29 23:40 ` [PATCH v2 02/14] iommu: Add for_each_group_device() Jason Gunthorpe
2023-03-29 23:52   ` Miguel Ojeda
2023-03-30 14:28     ` Jason Gunthorpe
2023-05-09 13:12       ` Miguel Ojeda
2023-05-10  1:01         ` Jason Gunthorpe
2023-03-30  6:23   ` Baolu Lu
2023-04-04  9:16   ` Tian, Kevin
2023-03-29 23:40 ` [PATCH v2 03/14] iommu: Make __iommu_group_set_domain() handle error unwind Jason Gunthorpe
2023-03-30  6:23   ` Baolu Lu
2023-03-29 23:40 ` [PATCH v2 04/14] iommu: Use __iommu_group_set_domain() for __iommu_attach_group() Jason Gunthorpe
2023-03-30  6:23   ` Baolu Lu
2023-03-29 23:40 ` [PATCH v2 05/14] iommu: Use __iommu_group_set_domain() in iommu_change_dev_def_domain() Jason Gunthorpe
2023-03-30  6:24   ` Baolu Lu
2023-04-04  9:16   ` Tian, Kevin
2023-03-29 23:40 ` [PATCH v2 06/14] iommu: Replace __iommu_group_dma_first_attach() with set_domain Jason Gunthorpe
2023-03-30  6:24   ` Baolu Lu
2023-03-29 23:40 ` [PATCH v2 07/14] iommu: Make iommu_group_do_dma_first_attach() simpler Jason Gunthorpe
2023-03-30  6:42   ` Baolu Lu
2023-03-30 14:41     ` Jason Gunthorpe
2023-03-31  2:21       ` Baolu Lu
2023-04-04  9:17   ` Tian, Kevin
2023-03-29 23:40 ` [PATCH v2 08/14] iommu: Make iommu_group_do_dma_first_attach() work with owned groups Jason Gunthorpe
2023-03-30  6:45   ` Baolu Lu
2023-03-30 15:54   ` Robin Murphy
2023-03-30 16:49     ` Jason Gunthorpe
2023-04-04  9:21   ` Tian, Kevin
2023-03-29 23:40 ` [PATCH v2 09/14] iommu: Fix iommu_probe_device() to attach the right domain Jason Gunthorpe
2023-03-30  7:33   ` Baolu Lu
2023-04-04  9:25   ` Tian, Kevin
2023-03-29 23:40 ` [PATCH v2 10/14] iommu: Remove the assignment of group->domain during default domain alloc Jason Gunthorpe
2023-03-30  7:33   ` Baolu Lu
2023-03-29 23:40 ` [PATCH v2 11/14] iommu: Consolidate the code to calculate the target default domain type Jason Gunthorpe
2023-03-30 11:51   ` Baolu Lu
2023-04-04  9:39   ` Tian, Kevin
2023-04-04 18:51     ` Jason Gunthorpe
2023-03-29 23:40 ` Jason Gunthorpe [this message]
2023-03-30 12:37   ` [PATCH v2 12/14] iommu: Consolidate the default_domain setup to one function Baolu Lu
2023-03-30 14:29     ` Robin Murphy
2023-03-30 14:45       ` Jason Gunthorpe
2023-03-30 15:42         ` Jason Gunthorpe
2023-04-04 11:29           ` Robin Murphy
2023-03-30 15:36     ` Jason Gunthorpe
2023-03-30 18:23       ` Robin Murphy
2023-03-30 19:01         ` Jason Gunthorpe
2023-03-29 23:40 ` [PATCH v2 13/14] iommu: Remove __iommu_group_for_each_dev() Jason Gunthorpe
2023-03-30 12:40   ` Baolu Lu
2023-03-29 23:40 ` [PATCH v2 14/14] iommu: Tidy the control flow in iommu_group_store_type() Jason Gunthorpe
2023-03-30 12:45   ` Baolu Lu

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=12-v2-cd32667d2ba6+70bd1-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=llvm@lists.linux.dev \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=nicolinc@nvidia.com \
    --cc=ojeda@kernel.org \
    --cc=robin.murphy@arm.com \
    --cc=trix@redhat.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.