Linux IOMMU Development
 help / color / mirror / Atom feed
From: Yi Liu <yi.l.liu@intel.com>
To: kevin.tian@intel.com, jgg@nvidia.com
Cc: joro@8bytes.org, baolu.lu@linux.intel.com, yi.l.liu@intel.com,
	iommu@lists.linux.dev, nicolinc@nvidia.com
Subject: [PATCH v9 03/21] iommu: Introduce a replace API for device pasid
Date: Thu, 13 Mar 2025 05:35:14 -0700	[thread overview]
Message-ID: <20250313123532.103522-4-yi.l.liu@intel.com> (raw)
In-Reply-To: <20250313123532.103522-1-yi.l.liu@intel.com>

Provide a high-level API to allow replacements of one domain with another
for specific pasid of a device. This is similar to
iommu_replace_group_handle() and it is expected to be used only by IOMMUFD.

Co-developed-by: Lu Baolu <baolu.lu@linux.intel.com>
Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Signed-off-by: Yi Liu <yi.l.liu@intel.com>
--
v8 -> v9: Adapted this patch to use iommu_group_pasid_store() which checks
	  the current pasid array entry and change the handle->domain under
	  xa_lock().
--
 drivers/iommu/iommu-priv.h |   4 ++
 drivers/iommu/iommu.c      | 111 ++++++++++++++++++++++++++++++++++---
 2 files changed, 107 insertions(+), 8 deletions(-)

diff --git a/drivers/iommu/iommu-priv.h b/drivers/iommu/iommu-priv.h
index b4508423e13b..2985f05d699f 100644
--- a/drivers/iommu/iommu-priv.h
+++ b/drivers/iommu/iommu-priv.h
@@ -43,4 +43,8 @@ void iommu_detach_group_handle(struct iommu_domain *domain,
 int iommu_replace_group_handle(struct iommu_group *group,
 			       struct iommu_domain *new_domain,
 			       struct iommu_attach_handle *handle);
+
+int iommu_replace_device_pasid(struct iommu_domain *domain,
+			       struct device *dev, ioasid_t pasid,
+			       struct iommu_attach_handle *handle);
 #endif /* __LINUX_IOMMU_PRIV_H */
diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 6a314122a9da..a828c96ad6c5 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -513,6 +513,13 @@ static void iommu_deinit_device(struct device *dev)
 	dev_iommu_free(dev);
 }
 
+static inline struct iommu_domain *pasid_array_entry_to_domain(void *entry)
+{
+	if (xa_pointer_tag(entry) == IOMMU_PASID_ARRAY_DOMAIN)
+		return xa_untag_pointer(entry);
+	return ((struct iommu_attach_handle *)xa_untag_pointer(entry))->domain;
+}
+
 DEFINE_MUTEX(iommu_probe_device_lock);
 
 static int __iommu_probe_device(struct device *dev, struct list_head *group_list)
@@ -2170,7 +2177,7 @@ static void iommu_pasid_array_entry_clear_handle(void *entry)
 
 /* Caller should have reserved memory for __xa_store() */
 static void iommu_group_pasid_store(struct iommu_group *group, ioasid_t pasid,
-				    struct iommu_domain *domain,
+				    void *curr, struct iommu_domain *domain,
 				    struct iommu_attach_handle *handle)
 {
 	void *entry;
@@ -2179,8 +2186,13 @@ static void iommu_group_pasid_store(struct iommu_group *group, ioasid_t pasid,
 
 	xa_lock(&group->pasid_array);
 	entry = iommu_make_pasid_array_entry(domain, handle);
+	if (curr == entry)
+		goto out_unlock;
+
 	WARN_ON(xa_is_err(__xa_store(&group->pasid_array, pasid, entry,
 				     GFP_KERNEL)));
+
+out_unlock:
 	xa_unlock(&group->pasid_array);
 }
 
@@ -3334,14 +3346,15 @@ static void iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid,
 }
 
 static int __iommu_set_group_pasid(struct iommu_domain *domain,
-				   struct iommu_group *group, ioasid_t pasid)
+				   struct iommu_group *group, ioasid_t pasid,
+				   struct iommu_domain *old)
 {
 	struct group_device *device, *last_gdev;
 	int ret;
 
 	for_each_group_device(group, device) {
 		ret = domain->ops->set_dev_pasid(domain, device->dev,
-						 pasid, NULL);
+						 pasid, old);
 		if (ret)
 			goto err_revert;
 	}
@@ -3353,7 +3366,15 @@ static int __iommu_set_group_pasid(struct iommu_domain *domain,
 	for_each_group_device(group, device) {
 		if (device == last_gdev)
 			break;
-		iommu_remove_dev_pasid(device->dev, pasid, domain);
+		/*
+		 * If no old domain, undo the succeeded devices/pasid.
+		 * Otherwise, rollback the succeeded devices/pasid to the old
+		 * domain. And it is a driver bug to fail attaching with a
+		 * previously good domain.
+		 */
+		if (!old || WARN_ON(old->ops->set_dev_pasid(old, device->dev,
+							    pasid, domain)))
+			iommu_remove_dev_pasid(device->dev, pasid, domain);
 	}
 	return ret;
 }
@@ -3416,7 +3437,7 @@ int iommu_attach_device_pasid(struct iommu_domain *domain,
 	if (ret)
 		goto out_unlock;
 
-	ret = __iommu_set_group_pasid(domain, group, pasid);
+	ret = __iommu_set_group_pasid(domain, group, pasid, NULL);
 	if (ret) {
 		xa_release(&group->pasid_array, pasid);
 		goto out_unlock;
@@ -3428,7 +3449,7 @@ int iommu_attach_device_pasid(struct iommu_domain *domain,
 	 * operation succeeds as we cannot tolerate PRIs becoming concurrently
 	 * queued and then failing attach.
 	 */
-	iommu_group_pasid_store(group, pasid, domain, handle);
+	iommu_group_pasid_store(group, pasid, NULL, domain, handle);
 
 out_unlock:
 	mutex_unlock(&group->mutex);
@@ -3436,6 +3457,79 @@ int iommu_attach_device_pasid(struct iommu_domain *domain,
 }
 EXPORT_SYMBOL_GPL(iommu_attach_device_pasid);
 
+/**
+ * iommu_replace_device_pasid - Replace the domain that a pasid
+ *                              is attached to
+ * @domain: the new iommu domain
+ * @dev: the attached device.
+ * @pasid: the pasid of the device.
+ * @handle: the attach handle.
+ *
+ * This API allows the pasid to switch domains. The @pasid should have been
+ * attached. Otherwise, this fails.
+ * The pasid will keep the old configuration if replacement failed.
+ * Return 0 on success, or an error.
+ */
+int iommu_replace_device_pasid(struct iommu_domain *domain,
+			       struct device *dev, ioasid_t pasid,
+			       struct iommu_attach_handle *handle)
+{
+	/* Caller must be a probed driver on dev */
+	struct iommu_group *group = dev->iommu_group;
+	struct iommu_domain *curr_domain;
+	void *curr;
+	int ret;
+
+	if (!group)
+		return -ENODEV;
+
+	if (!domain->ops->set_dev_pasid)
+		return -EOPNOTSUPP;
+
+	if (dev_iommu_ops(dev) != domain->owner ||
+	    pasid == IOMMU_NO_PASID || !handle)
+		return -EINVAL;
+
+	mutex_lock(&group->mutex);
+	curr = xa_cmpxchg(&group->pasid_array, pasid, NULL,
+			  XA_ZERO_ENTRY, GFP_KERNEL);
+	if (xa_is_err(curr)) {
+		ret = xa_err(curr);
+		goto out_unlock;
+	}
+
+	/*
+	 * No domain (with or without handle) attached, hence not
+	 * a replace case.
+	 */
+	if (!curr) {
+		xa_release(&group->pasid_array, pasid);
+		ret = -EINVAL;
+		goto out_unlock;
+	}
+
+	curr_domain = pasid_array_entry_to_domain(curr);
+	ret = 0;
+
+	if (curr_domain != domain) {
+		ret = __iommu_set_group_pasid(domain, group,
+					      pasid, curr_domain);
+		if (ret)
+			goto out_unlock;
+	}
+
+	/*
+	 * The above xa_cmpxchg() reserved the memory, and the
+	 * group->mutex is held, this cannot fail.
+	 */
+	iommu_group_pasid_store(group, pasid, curr, domain, handle);
+
+out_unlock:
+	mutex_unlock(&group->mutex);
+	return ret;
+}
+EXPORT_SYMBOL_NS_GPL(iommu_replace_device_pasid, "IOMMUFD_INTERNAL");
+
 /*
  * iommu_detach_device_pasid() - Detach the domain from pasid of device
  * @domain: the iommu domain.
@@ -3562,7 +3656,7 @@ int iommu_attach_group_handle(struct iommu_domain *domain,
 	 * operation succeeds as we cannot tolerate PRIs becoming concurrently
 	 * queued and then failing attach.
 	 */
-	iommu_group_pasid_store(group, IOMMU_NO_PASID, domain, handle);
+	iommu_group_pasid_store(group, IOMMU_NO_PASID, NULL, domain, handle);
 
 out_unlock:
 	mutex_unlock(&group->mutex);
@@ -3623,7 +3717,8 @@ int iommu_replace_group_handle(struct iommu_group *group,
 	if (ret)
 		goto err_release;
 
-	iommu_group_pasid_store(group, IOMMU_NO_PASID, new_domain, handle);
+	iommu_group_pasid_store(group, IOMMU_NO_PASID, NULL,
+				new_domain, handle);
 
 	mutex_unlock(&group->mutex);
 
-- 
2.34.1


  parent reply	other threads:[~2025-03-13 12:35 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-13 12:35 [PATCH v9 00/21] iommufd support pasid attach/replace Yi Liu
2025-03-13 12:35 ` [PATCH v9 01/21] iommu: Clear handle->domain in detach Yi Liu
2025-03-18 11:46   ` Jason Gunthorpe
2025-03-18 13:30     ` Yi Liu
2025-03-13 12:35 ` [PATCH v9 02/21] iommu: Wrap pasid_array entry creation and setting Yi Liu
2025-03-18 12:04   ` Jason Gunthorpe
2025-03-18 14:40     ` Yi Liu
2025-03-13 12:35 ` Yi Liu [this message]
2025-03-18 12:19   ` [PATCH v9 03/21] iommu: Introduce a replace API for device pasid Jason Gunthorpe
2025-03-18 13:50     ` Baolu Lu
2025-03-18 13:57       ` Jason Gunthorpe
2025-03-13 12:35 ` [PATCH v9 04/21] iommufd: Pass @pasid through the device attach/replace path Yi Liu
2025-03-13 12:35 ` [PATCH v9 05/21] iommufd/device: Only add reserved_iova in non-pasid path Yi Liu
2025-03-18 19:22   ` Nicolin Chen
2025-03-13 12:35 ` [PATCH v9 06/21] iommufd/device: Replace idev->igroup with local variable Yi Liu
2025-03-18 12:25   ` Jason Gunthorpe
2025-03-18 19:24   ` Nicolin Chen
2025-03-13 12:35 ` [PATCH v9 07/21] iommufd/device: Check !igroup->hwpt in iommufd_device_attach_reserved_iova() Yi Liu
2025-03-18 12:27   ` Jason Gunthorpe
2025-03-13 12:35 ` [PATCH v9 08/21] iommufd/device: Lift iommufd_attach_handle handling to upper level helpers Yi Liu
2025-03-18 12:30   ` Jason Gunthorpe
2025-03-13 12:35 ` [PATCH v9 09/21] iommufd/device: Use iommufd_attach_handle track attachment Yi Liu
2025-03-13 12:35 ` [PATCH v9 10/21] iommufd/device: Replace device_list with device_array Yi Liu
2025-03-13 12:35 ` [PATCH v9 11/21] iommufd/device: Move attached device tracking to handle Yi Liu
2025-03-18 12:34   ` Jason Gunthorpe
2025-03-18 13:25     ` Yi Liu
2025-03-18 13:26       ` Jason Gunthorpe
2025-03-18 13:50         ` Yi Liu
2025-03-18 13:56           ` Jason Gunthorpe
2025-03-18 14:13             ` Yi Liu
2025-03-18 14:32               ` Jason Gunthorpe
2025-03-13 12:35 ` [PATCH v9 12/21] iommufd/device: Add pasid_attach array to track per-PASID attach Yi Liu
2025-03-17  7:07   ` Yi Liu
2025-03-13 12:35 ` [PATCH v9 13/21] iommufd: Enforce PASID-compatible domain in PASID path Yi Liu
2025-03-13 12:35 ` [PATCH v9 14/21] iommufd: Support pasid attach/replace Yi Liu
2025-03-18 12:35   ` Jason Gunthorpe
2025-03-13 12:35 ` [PATCH v9 15/21] iommufd: Enforce PASID-compatible domain for RID Yi Liu
2025-03-18 12:38   ` Jason Gunthorpe
2025-03-18 14:09     ` Yi Liu
2025-03-13 12:35 ` [PATCH v9 16/21] iommu/vt-d: Add IOMMU_HWPT_ALLOC_PASID support Yi Liu
2025-03-13 12:35 ` [PATCH v9 17/21] iommufd: Allow allocating PASID-compatible domain Yi Liu
2025-03-18 12:39   ` Jason Gunthorpe
2025-03-13 12:35 ` [PATCH v9 18/21] iommufd/selftest: Add set_dev_pasid in mock iommu Yi Liu
2025-03-13 12:35 ` [PATCH v9 19/21] iommufd/selftest: Add a helper to get test device Yi Liu
2025-03-13 12:35 ` [PATCH v9 20/21] iommufd/selftest: Add test ops to test pasid attach/detach Yi Liu
2025-03-13 12:35 ` [PATCH v9 21/21] iommufd/selftest: Add coverage for iommufd " Yi Liu
2025-03-18 12:41 ` [PATCH v9 00/21] iommufd support pasid attach/replace Jason Gunthorpe
2025-03-18 14:37   ` Yi Liu

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=20250313123532.103522-4-yi.l.liu@intel.com \
    --to=yi.l.liu@intel.com \
    --cc=baolu.lu@linux.intel.com \
    --cc=iommu@lists.linux.dev \
    --cc=jgg@nvidia.com \
    --cc=joro@8bytes.org \
    --cc=kevin.tian@intel.com \
    --cc=nicolinc@nvidia.com \
    /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