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 v10 11/18] iommufd: Support pasid attach/replace
Date: Thu, 20 Mar 2025 06:47:37 -0700	[thread overview]
Message-ID: <20250320134744.5777-12-yi.l.liu@intel.com> (raw)
In-Reply-To: <20250320134744.5777-1-yi.l.liu@intel.com>

This extends the below APIs to support PASID. Device drivers to manage pasid
attach/replace/detach.

    int iommufd_device_attach(struct iommufd_device *idev,
			      ioasid_t pasid, u32 *pt_id);
    int iommufd_device_replace(struct iommufd_device *idev,
			       ioasid_t pasid, u32 *pt_id);
    void iommufd_device_detach(struct iommufd_device *idev,
			       ioasid_t pasid);

The pasid operations share underlying attach/replace/detach infrastructure
with the device operations, but still have some different implications:

 - no reserved region per pasid otherwise SVA architecture is already
   broken (CPU address space doesn't count device reserved regions);

 - accordingly no sw_msi trick;

Cache coherency enforcement is still applied to pasid operations since
it is about memory accesses post page table walking (no matter the walk
is per RID or per PASID).

Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Signed-off-by: Kevin Tian <kevin.tian@intel.com>
Signed-off-by: Yi Liu <yi.l.liu@intel.com>
---
v9 -> v10: minor tweaks per rebase
---
 drivers/iommu/iommufd/device.c   | 57 ++++++++++++++++++++------------
 drivers/iommu/iommufd/selftest.c |  8 ++---
 drivers/vfio/iommufd.c           | 10 +++---
 include/linux/iommufd.h          |  9 +++--
 4 files changed, 52 insertions(+), 32 deletions(-)

diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
index 6d003f9f0668..17c424d9a355 100644
--- a/drivers/iommu/iommufd/device.c
+++ b/drivers/iommu/iommufd/device.c
@@ -543,9 +543,12 @@ static int iommufd_hwpt_attach_device(struct iommufd_hw_pagetable *hwpt,
 	}
 
 	handle->idev = idev;
-	WARN_ON(pasid != IOMMU_NO_PASID);
-	rc = iommu_attach_group_handle(hwpt->domain, idev->igroup->group,
-				       &handle->handle);
+	if (pasid == IOMMU_NO_PASID)
+		rc = iommu_attach_group_handle(hwpt->domain, idev->igroup->group,
+					       &handle->handle);
+	else
+		rc = iommu_attach_device_pasid(hwpt->domain, idev->dev, pasid,
+					       &handle->handle);
 	if (rc)
 		goto out_disable_iopf;
 
@@ -579,10 +582,12 @@ static void iommufd_hwpt_detach_device(struct iommufd_hw_pagetable *hwpt,
 {
 	struct iommufd_attach_handle *handle;
 
-	WARN_ON(pasid != IOMMU_NO_PASID);
+	if (pasid == IOMMU_NO_PASID)
+		iommu_detach_group_handle(hwpt->domain, idev->igroup->group);
+	else
+		iommu_detach_device_pasid(hwpt->domain, idev->dev, pasid);
 
 	handle = iommufd_device_get_attach_handle(idev, pasid);
-	iommu_detach_group_handle(hwpt->domain, idev->igroup->group);
 	if (hwpt->fault) {
 		iommufd_auto_response_faults(hwpt, handle);
 		iommufd_fault_iopf_disable(idev);
@@ -598,8 +603,6 @@ static int iommufd_hwpt_replace_device(struct iommufd_device *idev,
 	struct iommufd_attach_handle *handle, *old_handle;
 	int rc;
 
-	WARN_ON(pasid != IOMMU_NO_PASID);
-
 	rc = iommufd_hwpt_pasid_compat(hwpt, idev, pasid);
 	if (rc)
 		return rc;
@@ -617,8 +620,12 @@ static int iommufd_hwpt_replace_device(struct iommufd_device *idev,
 	}
 
 	handle->idev = idev;
-	rc = iommu_replace_group_handle(idev->igroup->group, hwpt->domain,
-					&handle->handle);
+	if (pasid == IOMMU_NO_PASID)
+		rc = iommu_replace_group_handle(idev->igroup->group,
+						hwpt->domain, &handle->handle);
+	else
+		rc = iommu_replace_device_pasid(hwpt->domain, idev->dev,
+						pasid, &handle->handle);
 	if (rc)
 		goto out_disable_iopf;
 
@@ -1026,22 +1033,25 @@ static int iommufd_device_change_pt(struct iommufd_device *idev,
 }
 
 /**
- * iommufd_device_attach - Connect a device to an iommu_domain
+ * iommufd_device_attach - Connect a device/pasid to an iommu_domain
  * @idev: device to attach
+ * @pasid: pasid to attach
  * @pt_id: Input a IOMMUFD_OBJ_IOAS, or IOMMUFD_OBJ_HWPT_PAGING
  *         Output the IOMMUFD_OBJ_HWPT_PAGING ID
  *
- * This connects the device to an iommu_domain, either automatically or manually
- * selected. Once this completes the device could do DMA.
+ * This connects the device/pasid to an iommu_domain, either automatically
+ * or manually selected. Once this completes the device could do DMA with
+ * @pasid. @pasid is IOMMU_NO_PASID if this attach is for no pasid usage.
  *
  * The caller should return the resulting pt_id back to userspace.
  * This function is undone by calling iommufd_device_detach().
  */
-int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id)
+int iommufd_device_attach(struct iommufd_device *idev, ioasid_t pasid,
+			  u32 *pt_id)
 {
 	int rc;
 
-	rc = iommufd_device_change_pt(idev, IOMMU_NO_PASID, pt_id,
+	rc = iommufd_device_change_pt(idev, pasid, pt_id,
 				      &iommufd_device_do_attach);
 	if (rc)
 		return rc;
@@ -1056,8 +1066,9 @@ int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id)
 EXPORT_SYMBOL_NS_GPL(iommufd_device_attach, "IOMMUFD");
 
 /**
- * iommufd_device_replace - Change the device's iommu_domain
+ * iommufd_device_replace - Change the device/pasid's iommu_domain
  * @idev: device to change
+ * @pasid: pasid to change
  * @pt_id: Input a IOMMUFD_OBJ_IOAS, or IOMMUFD_OBJ_HWPT_PAGING
  *         Output the IOMMUFD_OBJ_HWPT_PAGING ID
  *
@@ -1068,27 +1079,31 @@ EXPORT_SYMBOL_NS_GPL(iommufd_device_attach, "IOMMUFD");
  *
  * If it fails then no change is made to the attachment. The iommu driver may
  * implement this so there is no disruption in translation. This can only be
- * called if iommufd_device_attach() has already succeeded.
+ * called if iommufd_device_attach() has already succeeded. @pasid is
+ * IOMMU_NO_PASID for no pasid usage.
  */
-int iommufd_device_replace(struct iommufd_device *idev, u32 *pt_id)
+int iommufd_device_replace(struct iommufd_device *idev, ioasid_t pasid,
+			   u32 *pt_id)
 {
-	return iommufd_device_change_pt(idev, IOMMU_NO_PASID, pt_id,
+	return iommufd_device_change_pt(idev, pasid, pt_id,
 					&iommufd_device_do_replace);
 }
 EXPORT_SYMBOL_NS_GPL(iommufd_device_replace, "IOMMUFD");
 
 /**
- * iommufd_device_detach - Disconnect a device to an iommu_domain
+ * iommufd_device_detach - Disconnect a device/device to an iommu_domain
  * @idev: device to detach
+ * @pasid: pasid to detach
  *
  * Undo iommufd_device_attach(). This disconnects the idev from the previously
  * attached pt_id. The device returns back to a blocked DMA translation.
+ * @pasid is IOMMU_NO_PASID for no pasid usage.
  */
-void iommufd_device_detach(struct iommufd_device *idev)
+void iommufd_device_detach(struct iommufd_device *idev, ioasid_t pasid)
 {
 	struct iommufd_hw_pagetable *hwpt;
 
-	hwpt = iommufd_hw_pagetable_detach(idev, IOMMU_NO_PASID);
+	hwpt = iommufd_hw_pagetable_detach(idev, pasid);
 	iommufd_hw_pagetable_put(idev->ictx, hwpt);
 	refcount_dec(&idev->obj.users);
 }
diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c
index d55dde28e9bc..0b3f5cbf242b 100644
--- a/drivers/iommu/iommufd/selftest.c
+++ b/drivers/iommu/iommufd/selftest.c
@@ -945,7 +945,7 @@ static int iommufd_test_mock_domain(struct iommufd_ucmd *ucmd,
 	}
 	sobj->idev.idev = idev;
 
-	rc = iommufd_device_attach(idev, &pt_id);
+	rc = iommufd_device_attach(idev, IOMMU_NO_PASID, &pt_id);
 	if (rc)
 		goto out_unbind;
 
@@ -960,7 +960,7 @@ static int iommufd_test_mock_domain(struct iommufd_ucmd *ucmd,
 	return 0;
 
 out_detach:
-	iommufd_device_detach(idev);
+	iommufd_device_detach(idev, IOMMU_NO_PASID);
 out_unbind:
 	iommufd_device_unbind(idev);
 out_mdev:
@@ -994,7 +994,7 @@ static int iommufd_test_mock_domain_replace(struct iommufd_ucmd *ucmd,
 		goto out_dev_obj;
 	}
 
-	rc = iommufd_device_replace(sobj->idev.idev, &pt_id);
+	rc = iommufd_device_replace(sobj->idev.idev, IOMMU_NO_PASID, &pt_id);
 	if (rc)
 		goto out_dev_obj;
 
@@ -1655,7 +1655,7 @@ void iommufd_selftest_destroy(struct iommufd_object *obj)
 
 	switch (sobj->type) {
 	case TYPE_IDEV:
-		iommufd_device_detach(sobj->idev.idev);
+		iommufd_device_detach(sobj->idev.idev, IOMMU_NO_PASID);
 		iommufd_device_unbind(sobj->idev.idev);
 		mock_dev_destroy(sobj->idev.mock_dev);
 		break;
diff --git a/drivers/vfio/iommufd.c b/drivers/vfio/iommufd.c
index 516294fd901b..37e1efa2c7bf 100644
--- a/drivers/vfio/iommufd.c
+++ b/drivers/vfio/iommufd.c
@@ -128,7 +128,7 @@ void vfio_iommufd_physical_unbind(struct vfio_device *vdev)
 	lockdep_assert_held(&vdev->dev_set->lock);
 
 	if (vdev->iommufd_attached) {
-		iommufd_device_detach(vdev->iommufd_device);
+		iommufd_device_detach(vdev->iommufd_device, IOMMU_NO_PASID);
 		vdev->iommufd_attached = false;
 	}
 	iommufd_device_unbind(vdev->iommufd_device);
@@ -146,9 +146,11 @@ int vfio_iommufd_physical_attach_ioas(struct vfio_device *vdev, u32 *pt_id)
 		return -EINVAL;
 
 	if (vdev->iommufd_attached)
-		rc = iommufd_device_replace(vdev->iommufd_device, pt_id);
+		rc = iommufd_device_replace(vdev->iommufd_device,
+					    IOMMU_NO_PASID, pt_id);
 	else
-		rc = iommufd_device_attach(vdev->iommufd_device, pt_id);
+		rc = iommufd_device_attach(vdev->iommufd_device,
+					   IOMMU_NO_PASID, pt_id);
 	if (rc)
 		return rc;
 	vdev->iommufd_attached = true;
@@ -163,7 +165,7 @@ void vfio_iommufd_physical_detach_ioas(struct vfio_device *vdev)
 	if (WARN_ON(!vdev->iommufd_device) || !vdev->iommufd_attached)
 		return;
 
-	iommufd_device_detach(vdev->iommufd_device);
+	iommufd_device_detach(vdev->iommufd_device, IOMMU_NO_PASID);
 	vdev->iommufd_attached = false;
 }
 EXPORT_SYMBOL_GPL(vfio_iommufd_physical_detach_ioas);
diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h
index 60eff9272551..34b6e6ca4bfa 100644
--- a/include/linux/iommufd.h
+++ b/include/linux/iommufd.h
@@ -8,6 +8,7 @@
 
 #include <linux/err.h>
 #include <linux/errno.h>
+#include <linux/iommu.h>
 #include <linux/refcount.h>
 #include <linux/types.h>
 #include <linux/xarray.h>
@@ -54,9 +55,11 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx,
 					   struct device *dev, u32 *id);
 void iommufd_device_unbind(struct iommufd_device *idev);
 
-int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id);
-int iommufd_device_replace(struct iommufd_device *idev, u32 *pt_id);
-void iommufd_device_detach(struct iommufd_device *idev);
+int iommufd_device_attach(struct iommufd_device *idev, ioasid_t pasid,
+			  u32 *pt_id);
+int iommufd_device_replace(struct iommufd_device *idev, ioasid_t pasid,
+			   u32 *pt_id);
+void iommufd_device_detach(struct iommufd_device *idev, ioasid_t pasid);
 
 struct iommufd_ctx *iommufd_device_to_ictx(struct iommufd_device *idev);
 u32 iommufd_device_to_id(struct iommufd_device *idev);
-- 
2.34.1


  parent reply	other threads:[~2025-03-20 13:47 UTC|newest]

Thread overview: 78+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-20 13:47 [PATCH v10 00/18] iommufd support pasid attach/replace Yi Liu
2025-03-20 13:47 ` [PATCH v10 01/18] iommu: Require passing new handles to APIs supporting handle Yi Liu
2025-03-20 15:23   ` Jason Gunthorpe
2025-03-20 23:51     ` Yi Liu
2025-03-21  2:35   ` Baolu Lu
2025-03-20 13:47 ` [PATCH v10 02/18] iommu: Introduce a replace API for device pasid Yi Liu
2025-03-20 17:24   ` Nicolin Chen
2025-03-20 23:58     ` Yi Liu
2025-03-21  0:14       ` Yi Liu
2025-03-21  3:21       ` Nicolin Chen
2025-03-21  4:06         ` Yi Liu
2025-03-21  3:08   ` Baolu Lu
2025-03-21  4:19     ` Yi Liu
2025-03-20 13:47 ` [PATCH v10 03/18] iommufd: Pass @pasid through the device attach/replace path Yi Liu
2025-03-21  3:13   ` Baolu Lu
2025-03-20 13:47 ` [PATCH v10 04/18] iommufd/device: Only add reserved_iova in non-pasid path Yi Liu
2025-03-21  3:14   ` Baolu Lu
2025-03-20 13:47 ` [PATCH v10 05/18] iommufd/device: Replace idev->igroup with local variable Yi Liu
2025-03-21  3:14   ` Baolu Lu
2025-03-20 13:47 ` [PATCH v10 06/18] iommufd/device: Add helper to detect the first attach of a group Yi Liu
2025-03-20 15:36   ` Jason Gunthorpe
2025-03-20 17:36   ` Nicolin Chen
2025-03-20 17:51     ` Nicolin Chen
2025-03-21  0:02       ` Yi Liu
2025-03-20 18:04     ` Jason Gunthorpe
2025-03-20 18:24       ` Nicolin Chen
2025-03-21  3:18   ` Baolu Lu
2025-03-20 13:47 ` [PATCH v10 07/18] iommufd/device: Wrap igroup->hwpt and igroup->device_list into attach struct Yi Liu
2025-03-20 15:48   ` Jason Gunthorpe
2025-03-20 18:03   ` Nicolin Chen
2025-03-21  3:22   ` Baolu Lu
2025-03-20 13:47 ` [PATCH v10 08/18] iommufd/device: Replace device_list with device_array Yi Liu
2025-03-20 17:20   ` Jason Gunthorpe
2025-03-21  0:25     ` Yi Liu
2025-03-20 18:38   ` Nicolin Chen
2025-03-21  0:30     ` Yi Liu
2025-03-21  3:25       ` Nicolin Chen
2025-03-20 13:47 ` [PATCH v10 09/18] iommufd/device: Add pasid_attach array to track per-PASID attach Yi Liu
2025-03-20 17:33   ` Jason Gunthorpe
2025-03-20 19:19   ` Nicolin Chen
2025-03-20 19:29     ` Jason Gunthorpe
2025-03-20 20:13       ` Nicolin Chen
2025-03-21  0:15     ` Yi Liu
2025-03-20 13:47 ` [PATCH v10 10/18] iommufd: Enforce PASID-compatible domain in PASID path Yi Liu
2025-03-20 13:47 ` Yi Liu [this message]
2025-03-20 20:42   ` [PATCH v10 11/18] iommufd: Support pasid attach/replace Nicolin Chen
2025-03-20 23:29     ` Jason Gunthorpe
2025-03-21  0:31     ` Yi Liu
2025-03-21  0:35       ` Nicolin Chen
2025-03-21  1:05         ` Yi Liu
2025-03-21 11:45           ` Jason Gunthorpe
2025-03-20 13:47 ` [PATCH v10 12/18] iommufd: Enforce PASID-compatible domain for RID Yi Liu
2025-03-20 17:35   ` Jason Gunthorpe
2025-03-20 22:23   ` Nicolin Chen
2025-03-20 23:31     ` Jason Gunthorpe
2025-03-21  0:45       ` Yi Liu
2025-03-21  0:41     ` Yi Liu
2025-03-20 13:47 ` [PATCH v10 13/18] iommu/vt-d: Add IOMMU_HWPT_ALLOC_PASID support Yi Liu
2025-03-20 13:47 ` [PATCH v10 14/18] iommufd: Allow allocating PASID-compatible domain Yi Liu
2025-03-20 17:51   ` Jason Gunthorpe
2025-03-21  0:52     ` Yi Liu
2025-03-20 22:36   ` Nicolin Chen
2025-03-20 13:47 ` [PATCH v10 15/18] iommufd/selftest: Add set_dev_pasid in mock iommu Yi Liu
2025-03-20 22:48   ` Nicolin Chen
2025-03-20 13:47 ` [PATCH v10 16/18] iommufd/selftest: Add a helper to get test device Yi Liu
2025-03-20 13:47 ` [PATCH v10 17/18] iommufd/selftest: Add test ops to test pasid attach/detach Yi Liu
2025-03-20 23:17   ` Nicolin Chen
2025-03-20 23:33     ` Jason Gunthorpe
2025-03-20 23:42     ` Nicolin Chen
2025-03-21  1:43     ` Yi Liu
2025-03-21 17:25       ` Nicolin Chen
2025-03-20 23:20   ` Nicolin Chen
2025-03-21  1:20     ` Yi Liu
2025-03-20 13:47 ` [PATCH v10 18/18] iommufd/selftest: Add coverage for iommufd " Yi Liu
2025-03-21  0:34   ` Nicolin Chen
2025-03-21 15:26     ` Yi Liu
2025-03-21 17:10       ` Nicolin Chen
2025-03-20 13:59 ` [PATCH v10 00/18] iommufd support pasid attach/replace 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=20250320134744.5777-12-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