Skip to contentSkip to conversation

IOMMUFD: Deliver IO page faults to user space

PATCH Superseded Lu Baolu 4 participants 49 messages May 27, 2024 – Jun 17, 2024

Subsystems (6)

v6 of 13 Superseded Latest is v13 →

lore
Share & tools
lore

Lu Baolu

author
#0 lore raw

v6 cover IOMMUFD: Deliver IO page faults to user space

This series implements the functionality of delivering IO page faults to user space through the IOMMUFD framework. One feasible use case is the nested translation. Nested translation is a hardware feature that supports two-stage translation tables for IOMMU. The second-stage translation table is managed by the host VMM, while the first-stage translation table is owned by user space. This allows user space to control the IOMMU mappings for its devices.

When an IO page fault occurs on the first-stage translation table, the IOMMU hardware can deliver the page fault to user space through the IOMMUFD framework. User space can then handle the page fault and respond to the device top-down through the IOMMUFD. This allows user space to implement its own IO page fault handling policies.

User space application that is capable of handling IO page faults should allocate a fault object, and bind the fault object to any domain that it is willing to handle the fault generatd for them. On a successful return of fault object allocation, the user can retrieve and respond to page faults by reading or writing to the file descriptor (FD) returned.

The iommu selftest framework has been updated to test the IO page fault delivery and response functionality.

The series and related patches are available on GitHub:
https://github.com/LuBaolu/intel-iommu/commits/iommufd-io-pgfault-delivery-v6

Change log:
v6:
- Refine the attach handle code by shifting the handle allocation to the caller. The caller will then provide the allocated handle to the domain attachment interfaces.
- Add reference counter in iommufd_fault_iopf_enable/disable() helpers.
- Fix the return values of fault FD's read/write fops.
- Add IOMMU_CAP_USER_IOASID_TABLE capability and check it before roll back getting attach_handle to RID.
- Move the iopf respond queue from iommufd device to iommufd fault.
- Disallow PRI enablement on SR-IOV VF devices.
- Miscellaneous cleanup.

v5: https://lore.kernel.org/linux-iommu/[email protected]/
- Removed attach handle reference count from the core. Drivers will now synchronize their use of handles and domain attach/detach.
- Automatically responds to all outstanding faults in hwpt detach or replace paths.
- Supports getting a domain-type specific attach handle.
- Reorganized the series by changing the patch order.
- Miscellaneous cleanup.

v4: https://lore.kernel.org/linux-iommu/[email protected]/
- Add the iommu domain attachment handle to replace the iopf-specific domain attachment interfaces introduced in the previous v3.
- Replace the iommu_sva with iommu domain attachment handle.
- Refine some fields in the fault and response message encoding
according to feedback collected during v3 review period.
- Refine and fix some problems in the fault FD implementation.
- Miscellaneous cleanup.

v3: https://lore.kernel.org/linux-iommu/[email protected]/
- Add iopf domain attach/detach/replace interfaces to manage the reference counters of hwpt and device, ensuring that both can only be destroyed after all outstanding IOPFs have been responded to.
- Relocate the fault handling file descriptor from hwpt to a fault
object to enable a single fault handling object to be utilized
across multiple domains.
- Miscellaneous cleanup and performance improvements.

v2: https://lore.kernel.org/linux-iommu/[email protected]/
- Move all iommu refactoring patches into a sparated series and discuss
it in a different thread. The latest patch series [v6] is available at
https://lore.kernel.org/linux-iommu/[email protected]/
- We discussed the timeout of the pending page fault messages. We
agreed that we shouldn't apply any timeout policy for the page fault handling in user space.
https://lore.kernel.org/linux-iommu/20230616113232.GA84678@myrica/
- Jason suggested that we adopt a simple file descriptor interface for reading and responding to I/O page requests, so that user space applications can improve performance using io_uring.
https://lore.kernel.org/linux-iommu/[email protected]/

v1: https://lore.kernel.org/linux-iommu/[email protected]/

Lu Baolu (10):
iommu: Introduce domain attachment handle
iommu: Remove sva handle list
iommu: Add attach handle to struct iopf_group
iommu: Extend domain attach group with handle support
iommufd: Add fault and response message definitions
iommufd: Add iommufd fault object
iommufd: Fault-capable hwpt attach/detach/replace
iommufd: Associate fault object with iommufd_hw_pgtable iommufd/selftest: Add IOPF support for mock device iommufd/selftest: Add coverage for IOPF test

 include/linux/iommu.h                         |  42 +-
 drivers/iommu/iommu-priv.h                    |  11 +
 drivers/iommu/iommufd/iommufd_private.h       |  51 ++
 drivers/iommu/iommufd/iommufd_test.h          |   8 +
 include/uapi/linux/iommufd.h                  | 122 +++++
 tools/testing/selftests/iommu/iommufd_utils.h |  84 +++-
 drivers/dma/idxd/init.c                       |   2 +-
 drivers/iommu/io-pgfault.c                    |  61 +--
 drivers/iommu/iommu-sva.c                     |  37 +-
 drivers/iommu/iommu.c                         | 178 +++++--
 drivers/iommu/iommufd/device.c                |  16 +-
 drivers/iommu/iommufd/fault.c                 | 435 ++++++++++++++++++
 drivers/iommu/iommufd/hw_pagetable.c          |  41 +-
 drivers/iommu/iommufd/main.c                  |   6 +
 drivers/iommu/iommufd/selftest.c              |  64 +++
 tools/testing/selftests/iommu/iommufd.c       |  18 +
 .../selftests/iommu/iommufd_fail_nth.c        |   2 +-
 drivers/iommu/iommufd/Makefile                |   1 +
 18 files changed, 1060 insertions(+), 119 deletions(-)
 create mode 100644 drivers/iommu/iommufd/fault.c
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v6 1/10 iommu: Introduce domain attachment handle

Currently, when attaching a domain to a device or its PASID, domain is stored within the iommu group. It could be retrieved for use during the window between attachment and detachment.

With new features introduced, there's a need to store more information than just a domain pointer. This information essentially represents the association between a domain and a device. For example, the SVA code already has a custom struct iommu_sva which represents a bond between sva domain and a PASID of a device. Looking forward, the IOMMUFD needs a place to store the iommufd_device pointer in the core, so that the device object ID could be quickly retrieved in the critical fault handling path.

Introduce domain attachment handle that explicitly represents the attachment relationship between a domain and a device or its PASID.

---
 include/linux/iommu.h     | 18 +++++++++++++++---
 drivers/dma/idxd/init.c   |  2 +-
 drivers/iommu/iommu-sva.c | 13 ++++++++-----
 drivers/iommu/iommu.c     | 23 +++++++++++++----------
 4 files changed, 37 insertions(+), 19 deletions(-)
include/linux/iommu.h +15 -3
index 7bc8dff7cf6d..b89404acacd6 100644
@@ -989,12 +989,22 @@ struct iommu_fwspec {
 /* ATS is supported */
 #define IOMMU_FWSPEC_PCI_RC_ATS			(1 << 0)
 
+/*
+ * An iommu attach handle represents a relationship between an iommu domain
+ * and a PASID or RID of a device. It is allocated and managed by the component
+ * that manages the domain and is stored in the iommu group during the time the
+ * domain is attached.
+ */
+struct iommu_attach_handle {
+	struct iommu_domain		*domain;
+};
+
 /**
  * struct iommu_sva - handle to a device-mm bond
  */
 struct iommu_sva {
+	struct iommu_attach_handle	handle;
 	struct device			*dev;
-	struct iommu_domain		*domain;
 	struct list_head		handle_item;
 	refcount_t			users;
 };
@@ -1052,7 +1062,8 @@ int iommu_device_claim_dma_owner(struct device *dev, void *owner);
 void iommu_device_release_dma_owner(struct device *dev);
 
 int iommu_attach_device_pasid(struct iommu_domain *domain,
-			      struct device *dev, ioasid_t pasid);
+			      struct device *dev, ioasid_t pasid,
+			      struct iommu_attach_handle *handle);
 void iommu_detach_device_pasid(struct iommu_domain *domain,
 			       struct device *dev, ioasid_t pasid);
 struct iommu_domain *
@@ -1388,7 +1399,8 @@ static inline int iommu_device_claim_dma_owner(struct device *dev, void *owner)
 }
 
 static inline int iommu_attach_device_pasid(struct iommu_domain *domain,
-					    struct device *dev, ioasid_t pasid)
+					    struct device *dev, ioasid_t pasid,
+					    struct iommu_attach_handle *handle)
 {
 	return -ENODEV;
 }
drivers/dma/idxd/init.c +1 -1
index a7295943fa22..385c488c9cd1 100644
@@ -584,7 +584,7 @@ static int idxd_enable_system_pasid(struct idxd_device *idxd)
 	 * DMA domain is owned by the driver, it should support all valid
 	 * types such as DMA-FQ, identity, etc.
 	 */
-	ret = iommu_attach_device_pasid(domain, dev, pasid);
+	ret = iommu_attach_device_pasid(domain, dev, pasid, NULL);
 	if (ret) {
 		dev_err(dev, "failed to attach device pasid %d, domain type %d",
 			pasid, domain->type);
drivers/iommu/iommu-sva.c +8 -5
index 18a35e798b72..0fb923254062 100644
@@ -99,7 +99,9 @@ struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm
 
 	/* Search for an existing domain. */
 	list_for_each_entry(domain, &mm->iommu_mm->sva_domains, next) {
-		ret = iommu_attach_device_pasid(domain, dev, iommu_mm->pasid);
+		handle->handle.domain = domain;
+		ret = iommu_attach_device_pasid(domain, dev, iommu_mm->pasid,
+						&handle->handle);
 		if (!ret) {
 			domain->users++;
 			goto out;
@@ -113,7 +115,9 @@ struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm
 		goto out_free_handle;
 	}
 
-	ret = iommu_attach_device_pasid(domain, dev, iommu_mm->pasid);
+	handle->handle.domain = domain;
+	ret = iommu_attach_device_pasid(domain, dev, iommu_mm->pasid,
+					&handle->handle);
 	if (ret)
 		goto out_free_domain;
 	domain->users = 1;
@@ -124,7 +128,6 @@ struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm
 	list_add(&handle->handle_item, &mm->iommu_mm->sva_handles);
 	mutex_unlock(&iommu_sva_lock);
 	handle->dev = dev;
-	handle->domain = domain;
 	return handle;
 
 out_free_domain:
@@ -147,7 +150,7 @@ EXPORT_SYMBOL_GPL(iommu_sva_bind_device);
  */
 void iommu_sva_unbind_device(struct iommu_sva *handle)
 {
-	struct iommu_domain *domain = handle->domain;
+	struct iommu_domain *domain = handle->handle.domain;
 	struct iommu_mm_data *iommu_mm = domain->mm->iommu_mm;
 	struct device *dev = handle->dev;
 
@@ -170,7 +173,7 @@ EXPORT_SYMBOL_GPL(iommu_sva_unbind_device);
 
 u32 iommu_sva_get_pasid(struct iommu_sva *handle)
 {
-	struct iommu_domain *domain = handle->domain;
+	struct iommu_domain *domain = handle->handle.domain;
 
 	return mm_get_enqcmd_pasid(domain->mm);
 }
drivers/iommu/iommu.c +13 -10
index 9df7cc75c1bc..efd281ddfa63 100644
@@ -3352,16 +3352,17 @@ static void __iommu_remove_group_pasid(struct iommu_group *group,
  * @domain: the iommu domain.
  * @dev: the attached device.
  * @pasid: the pasid of the device.
+ * @handle: the attach handle.
  *
  * Return: 0 on success, or an error.
  */
 int iommu_attach_device_pasid(struct iommu_domain *domain,
-			      struct device *dev, ioasid_t pasid)
+			      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 group_device *device;
-	void *curr;
 	int ret;
 
 	if (!domain->ops->set_dev_pasid)
@@ -3382,11 +3383,9 @@ int iommu_attach_device_pasid(struct iommu_domain *domain,
 		}
 	}
 
-	curr = xa_cmpxchg(&group->pasid_array, pasid, NULL, domain, GFP_KERNEL);
-	if (curr) {
-		ret = xa_err(curr) ? : -EBUSY;
+	ret = xa_insert(&group->pasid_array, pasid, handle, GFP_KERNEL);
+	if (ret)
 		goto out_unlock;
-	}
 
 	ret = __iommu_set_group_pasid(domain, group, pasid);
 	if (ret)
@@ -3414,7 +3413,7 @@ void iommu_detach_device_pasid(struct iommu_domain *domain, struct device *dev,
 
 	mutex_lock(&group->mutex);
 	__iommu_remove_group_pasid(group, pasid, domain);
-	WARN_ON(xa_erase(&group->pasid_array, pasid) != domain);
+	xa_erase(&group->pasid_array, pasid);
 	mutex_unlock(&group->mutex);
 }
 EXPORT_SYMBOL_GPL(iommu_detach_device_pasid);
@@ -3439,15 +3438,19 @@ struct iommu_domain *iommu_get_domain_for_dev_pasid(struct device *dev,
 {
 	/* Caller must be a probed driver on dev */
 	struct iommu_group *group = dev->iommu_group;
-	struct iommu_domain *domain;
+	struct iommu_attach_handle *handle;
+	struct iommu_domain *domain = NULL;
 
 	if (!group)
 		return NULL;
 
 	xa_lock(&group->pasid_array);
-	domain = xa_load(&group->pasid_array, pasid);
+	handle = xa_load(&group->pasid_array, pasid);
+	if (handle)
+		domain = handle->domain;
+
 	if (type && domain && domain->type != type)
-		domain = ERR_PTR(-EBUSY);
+		domain = NULL;
 	xa_unlock(&group->pasid_array);
 
 	return domain;
Signature · 1 line
2.34.1
maintainer
#2 lore raw

RE: [PATCH v6 01/10] iommu: Introduce domain attachment handle

Quoted 4 lines

From: Lu Baolu <[email protected]>
Sent: Monday, May 27, 2024 12:05 PM

drivers/iommu/iommu-sva.c struct iommu_sva *iommu_sva_bind_device(struct device · line 107

Quoted 10 lines
drivers/iommu/iommu-sva.c +3 -1
@@ -99,7 +99,9 @@ struct iommu_sva *iommu_sva_bind_device(struct device
 *dev, struct mm_struct *mm
 
 	/* Search for an existing domain. */
 	list_for_each_entry(domain, &mm->iommu_mm->sva_domains, next) {
-		ret = iommu_attach_device_pasid(domain, dev, iommu_mm-
 >pasid);
+		handle->handle.domain = domain;
+		ret = iommu_attach_device_pasid(domain, dev, iommu_mm-
 >pasid,
+						&handle->handle);

move the setting of handle.domain into the helper?

drivers/iommu/iommu.c int iommu_attach_device_pasid(struct · line 3391

Quoted 13 lines
drivers/iommu/iommu.c +2 -4
@@ -3382,11 +3383,9 @@ int iommu_attach_device_pasid(struct
 iommu_domain *domain,
 		}
 	}
 
-	curr = xa_cmpxchg(&group->pasid_array, pasid, NULL, domain,
 GFP_KERNEL);
-	if (curr) {
-		ret = xa_err(curr) ? : -EBUSY;
+	ret = xa_insert(&group->pasid_array, pasid, handle, GFP_KERNEL);
+	if (ret)
 		goto out_unlock;
-	}

this leads to a slightly different implication comparing to the old code.

the domain pointer was always tracked in the old code but now the handle is optional.

if iommu core only needs to know whether a pasid has been attached (as in this helper), it still works as xa_insert() will mark the entry as reserved if handle==NULL and next xa_insert() at the same index will fail due to the entry being reserved.

But if certain path (other than iopf) in the iommu core needs to know the exact domain pointer then this change breaks it.

Anyway some explanation is welcomed here for why this change is safe.

drivers/iommu/iommu.c void iommu_detach_device_pasid(struct · line 3417

Quoted 7 lines
drivers/iommu/iommu.c +1 -1
@@ -3414,7 +3413,7 @@ void iommu_detach_device_pasid(struct
 iommu_domain *domain, struct device *dev,
 
 	mutex_lock(&group->mutex);
 	__iommu_remove_group_pasid(group, pasid, domain);
-	WARN_ON(xa_erase(&group->pasid_array, pasid) != domain);
+	xa_erase(&group->pasid_array, pasid);

if the entry is valid do we want to keep the WARN_ON() upon handle->domain?

author
#3 lore raw

Re: [PATCH v6 01/10] iommu: Introduce domain attachment handle

Quoted 9 lines · Kevin
Quoted 9 lines

From: Lu Baolu <[email protected]>
Sent: Monday, May 27, 2024 12:05 PM

drivers/iommu/iommu-sva.c +0 -1
@@ -99,7 +99,9 @@ struct iommu_sva *iommu_sva_bind_device(struct device
 *dev, struct mm_struct *mm
 
  	/* Search for an existing domain. */
  	list_for_each_entry(domain, &mm->iommu_mm->sva_domains, next) {
-		ret = iommu_attach_device_pasid(domain, dev, iommu_mm-
Quoted 7 lines

Review struct iommu_sva *iommu_sva_bind_device(struct device · line 107

Quoted 5 lines
(quoted diff) +3 -0
 > pasid);
+		handle->handle.domain = domain;
+		ret = iommu_attach_device_pasid(domain, dev, iommu_mm-
 > pasid,
+						&handle->handle);

move the setting of handle.domain into the helper?

Yes.

Quoted 20 lines
Quoted 4 lines
drivers/iommu/iommu.c +0 -0
@@ -3382,11 +3383,9 @@ int iommu_attach_device_pasid(struct
 iommu_domain *domain,
  		}

drivers/iommu/iommu.c int iommu_attach_device_pasid(struct · line 3391

Quoted 10 lines
drivers/iommu/iommu.c +2 -4
  	}
 
-	curr = xa_cmpxchg(&group->pasid_array, pasid, NULL, domain,
 GFP_KERNEL);
-	if (curr) {
-		ret = xa_err(curr) ? : -EBUSY;
+	ret = xa_insert(&group->pasid_array, pasid, handle, GFP_KERNEL);
+	if (ret)
  		goto out_unlock;
-	}

this leads to a slightly different implication comparing to the old code.

the domain pointer was always tracked in the old code but now the handle is optional.

Quoted 7 lines

if iommu core only needs to know whether a pasid has been attached (as in this helper), it still works as xa_insert() will mark the entry as reserved if handle==NULL and next xa_insert() at the same index will fail due to the entry being reserved.

But if certain path (other than iopf) in the iommu core needs to know the exact domain pointer then this change breaks it.

The iommu core should not fetch the domain pointer in paths other than attach/detach/replace. There is currently no reference counter for an iommu domain, hence fetching the domain for other purposes will potentially lead to a use-after-free issue.

Quoted 6 lines

Anyway some explanation is welcomed here for why this change is safe.

Quoted 3 lines
(quoted diff) +0 -0
@@ -3414,7 +3413,7 @@ void iommu_detach_device_pasid(struct
 iommu_domain *domain, struct device *dev,
 
Quoted 6 lines

drivers/iommu/iommu.c void iommu_detach_device_pasid(struct iommu_domain *domain, struct device *dev, · line 3416

Quoted 4 lines
drivers/iommu/iommu.c +1 -1
  	mutex_lock(&group->mutex);
  	__iommu_remove_group_pasid(group, pasid, domain);
-	WARN_ON(xa_erase(&group->pasid_array, pasid) != domain);
+	xa_erase(&group->pasid_array, pasid);

if the entry is valid do we want to keep the WARN_ON() upon handle->domain?

The domain pointer has already been passed to the iommu driver in __iommu_remove_group_pasid(). Therefore, the check for the pointer's validity should be performed before calling __iommu_remove_group_pasid(). Hence, in my view, using WARN_ON() around xa_erase() is not very useful.

Best regards,
baolu

#4 lore raw

Re: [PATCH v6 01/10] iommu: Introduce domain attachment handle

Quoted 7 lines · Baolu Lu
Quoted 2 lines

But if certain path (other than iopf) in the iommu core needs to know the exact domain pointer then this change breaks it.

The iommu core should not fetch the domain pointer in paths other than attach/detach/replace. There is currently no reference counter for an iommu domain, hence fetching the domain for other purposes will potentially lead to a use-after-free issue.

If you are doing this then we should get rid of iommu_get_domain_for_dev_pasid, and always return the handle there. Making it clear that all those flows require handles to work.

But is this really OK? What happened to the patch fixing the error unwind in attach_pasid? Doesn't it always need the domain to undo a failed attach?

Jason

author
#5 lore raw

Re: [PATCH v6 01/10] iommu: Introduce domain attachment handle

Quoted 5 lines · Jason Gunthorpe
Quoted 3 lines · Baolu Lu
Quoted 2 lines

But if certain path (other than iopf) in the iommu core needs to know the exact domain pointer then this change breaks it.

Quoted 8 lines
Quoted 4 lines

The iommu core should not fetch the domain pointer in paths other than attach/detach/replace. There is currently no reference counter for an iommu domain, hence fetching the domain for other purposes will potentially lead to a use-after-free issue.

If you are doing this then we should get rid of iommu_get_domain_for_dev_pasid, and always return the handle there. Making it clear that all those flows require handles to work.

I removed iommu_get_domain_for_dev_pasid() in this series. The iopf handling is the only path that requires fetching domain, where we requires attach handle to work.

Quoted 4 lines

But is this really OK? What happened to the patch fixing the error unwind in attach_pasid? Doesn't it always need the domain to undo a failed attach?

attach_pasid switches from being unassigned to being assigned with a real domain, so the unwind operation simply involves calling iommu_ops->remove_dev_pasid.

In the future, probably we will introduce a replace interface for pasid. In that scenario, the caller should explicitly pass both the old and new domains. If the replace operation fails, the interface will revert to the old domain.

In my mind, the iommu core should only manage the default domain for kernel DMA. All domains that are allocated by domain_alloc interfaces should be managed by the callers and passed through the attach/detach /replace interfaces.

Quoted 3 lines

Jason

Best regards,
baolu

author
#6 lore raw

PATCH v6 2/10 iommu: Remove sva handle list

In reply to Lu Baolu

The struct sva_iommu represents an association between an SVA domain and a PASID of a device. It's stored in the iommu group's pasid array and also tracked by a list in the per-mm data structure. Removes duplicate tracking of sva_iommu by eliminating the list.

---
 include/linux/iommu.h      |  2 --
 drivers/iommu/iommu-priv.h |  3 +++
 drivers/iommu/iommu-sva.c  | 21 ++++++++++++---------
 drivers/iommu/iommu.c      | 31 +++++++++++++++++++++++++++++++
 4 files changed, 46 insertions(+), 11 deletions(-)
include/linux/iommu.h +0 -2
index b89404acacd6..823fa3bcc2c6 100644
@@ -1005,14 +1005,12 @@ struct iommu_attach_handle {
 struct iommu_sva {
 	struct iommu_attach_handle	handle;
 	struct device			*dev;
-	struct list_head		handle_item;
 	refcount_t			users;
 };
 
 struct iommu_mm_data {
 	u32			pasid;
 	struct list_head	sva_domains;
-	struct list_head	sva_handles;
 };
 
 int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
drivers/iommu/iommu-priv.h +3 -0
index 5f731d994803..f1536a5ebb0d 100644
@@ -28,4 +28,7 @@ void iommu_device_unregister_bus(struct iommu_device *iommu,
 				 const struct bus_type *bus,
 				 struct notifier_block *nb);
 
+struct iommu_attach_handle *iommu_attach_handle_get(struct iommu_group *group,
+						    ioasid_t pasid,
+						    unsigned int type);
 #endif /* __LINUX_IOMMU_PRIV_H */
drivers/iommu/iommu-sva.c +12 -9
index 0fb923254062..e85f4ccc9dcc 100644
@@ -41,7 +41,6 @@ static struct iommu_mm_data *iommu_alloc_mm_data(struct mm_struct *mm, struct de
 	}
 	iommu_mm->pasid = pasid;
 	INIT_LIST_HEAD(&iommu_mm->sva_domains);
-	INIT_LIST_HEAD(&iommu_mm->sva_handles);
 	/*
 	 * Make sure the write to mm->iommu_mm is not reordered in front of
 	 * initialization to iommu_mm fields. If it does, readers may see a
@@ -69,11 +68,16 @@ static struct iommu_mm_data *iommu_alloc_mm_data(struct mm_struct *mm, struct de
  */
 struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm)
 {
+	struct iommu_group *group = dev->iommu_group;
+	struct iommu_attach_handle *attach_handle;
 	struct iommu_mm_data *iommu_mm;
 	struct iommu_domain *domain;
 	struct iommu_sva *handle;
 	int ret;
 
+	if (!group)
+		return ERR_PTR(-ENODEV);
+
 	mutex_lock(&iommu_sva_lock);
 
 	/* Allocate mm->pasid if necessary. */
@@ -83,12 +87,13 @@ struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm
 		goto out_unlock;
 	}
 
-	list_for_each_entry(handle, &mm->iommu_mm->sva_handles, handle_item) {
-		if (handle->dev == dev) {
-			refcount_inc(&handle->users);
-			mutex_unlock(&iommu_sva_lock);
-			return handle;
-		}
+	/* A bond already exists, just take a reference`. */
+	attach_handle = iommu_attach_handle_get(group, iommu_mm->pasid, IOMMU_DOMAIN_SVA);
+	if (!IS_ERR(attach_handle)) {
+		handle = container_of(attach_handle, struct iommu_sva, handle);
+		refcount_inc(&handle->users);
+		mutex_unlock(&iommu_sva_lock);
+		return handle;
 	}
 
 	handle = kzalloc(sizeof(*handle), GFP_KERNEL);
@@ -125,7 +130,6 @@ struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm
 
 out:
 	refcount_set(&handle->users, 1);
-	list_add(&handle->handle_item, &mm->iommu_mm->sva_handles);
 	mutex_unlock(&iommu_sva_lock);
 	handle->dev = dev;
 	return handle;
@@ -159,7 +163,6 @@ void iommu_sva_unbind_device(struct iommu_sva *handle)
 		mutex_unlock(&iommu_sva_lock);
 		return;
 	}
-	list_del(&handle->handle_item);
 
 	iommu_detach_device_pasid(domain, dev, iommu_mm->pasid);
 	if (--domain->users == 0) {
drivers/iommu/iommu.c +31 -0
index efd281ddfa63..0263814cba6b 100644
@@ -3483,3 +3483,34 @@ void iommu_free_global_pasid(ioasid_t pasid)
 	ida_free(&iommu_global_pasid_ida, pasid);
 }
 EXPORT_SYMBOL_GPL(iommu_free_global_pasid);
+
+/**
+ * iommu_attach_handle_get - Return the attach handle
+ * @group: the iommu group that domain was attached to
+ * @pasid: the pasid within the group
+ * @type: matched domain type, 0 for any match
+ *
+ * Return handle or ERR_PTR(-ENOENT) on none, ERR_PTR(-EBUSY) on mismatch.
+ *
+ * Return the attach handle to the caller. The life cycle of an iommu attach
+ * handle is from the time when the domain is attached to the time when the
+ * domain is detached. Callers are required to synchronize the call of
+ * iommu_attach_handle_get() with domain attachment and detachment. The attach
+ * handle can only be used during its life cycle.
+ */
+struct iommu_attach_handle *
+iommu_attach_handle_get(struct iommu_group *group, ioasid_t pasid, unsigned int type)
+{
+	struct iommu_attach_handle *handle;
+
+	xa_lock(&group->pasid_array);
+	handle = xa_load(&group->pasid_array, pasid);
+	if (!handle)
+		handle = ERR_PTR(-ENOENT);
+	else if (type && handle->domain->type != type)
+		handle = ERR_PTR(-EBUSY);
+	xa_unlock(&group->pasid_array);
+
+	return handle;
+}
+EXPORT_SYMBOL_NS_GPL(iommu_attach_handle_get, IOMMUFD_INTERNAL);
Signature · 1 line
2.34.1
maintainer
#7 lore raw

RE: [PATCH v6 02/10] iommu: Remove sva handle list

Quoted 4 lines

From: Lu Baolu <[email protected]>
Sent: Monday, May 27, 2024 12:05 PM

drivers/iommu/iommu-sva.c static struct iommu_mm_data · line 77

Quoted 10 lines
drivers/iommu/iommu-sva.c +2 -0
@@ -69,11 +68,16 @@ static struct iommu_mm_data
 *iommu_alloc_mm_data(struct mm_struct *mm, struct de
  */
 struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct
 mm_struct *mm)
 {
+	struct iommu_group *group = dev->iommu_group;
+	struct iommu_attach_handle *attach_handle;
 	struct iommu_mm_data *iommu_mm;
 	struct iommu_domain *domain;
 	struct iommu_sva *handle;

it's confusing to have both 'handle' and 'attach_handle' in one function.

Clearer to rename 'handle' as 'sva'.

Quoted 26 lines
drivers/iommu/iommu-sva.c +3 -0
 	int ret;
 
+	if (!group)
+		return ERR_PTR(-ENODEV);
+
 	mutex_lock(&iommu_sva_lock);
drivers/iommu/iommu-sva.c +0 -0
 	/* Allocate mm->pasid if necessary. */
@@ -83,12 +87,13 @@ struct iommu_sva *iommu_sva_bind_device(struct

device *dev, struct mm_struct *mm

drivers/iommu/iommu-sva.c +0 -1
 		goto out_unlock;
 	}
 
-	list_for_each_entry(handle, &mm->iommu_mm->sva_handles,

handle_item) {

drivers/iommu/iommu-sva.c +2 -5
-		if (handle->dev == dev) {
-			refcount_inc(&handle->users);
-			mutex_unlock(&iommu_sva_lock);
-			return handle;
-		}
+	/* A bond already exists, just take a reference`. */
+	attach_handle = iommu_attach_handle_get(group, iommu_mm-
Quoted 1 line

pasid, IOMMU_DOMAIN_SVA);

(quoted diff) +2 -0
+	if (!IS_ERR(attach_handle)) {
+		handle = container_of(attach_handle, struct iommu_sva,

handle);

drivers/iommu/iommu-sva.c struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm · line 97

Quoted 4 lines
drivers/iommu/iommu-sva.c +3 -0
+		refcount_inc(&handle->users);
+		mutex_unlock(&iommu_sva_lock);
+		return handle;
 	}

It's counter-intuitive to move forward when an error is returned.

e.g. if it's -EBUSY indicating the pasid already used for another type then following attempts shouldn't been tried.

probably we should have iommu_attach_handle_get() return NULL instead of -ENOENT when the entry is free? then:

	attach_handle = iommu_attach_handle_get();
	if (IS_ERR(attach_handle)) {
		ret = PTR_ERR(attach_handle);
		goto out_unlock;
	} else if (attach_handle) {
		/* matched and increase handle->users */
	}

	/* free entry falls through */

But then there is one potential issue with the design that 'handle' can be optional in iommu_attach_device_pasid(). In that case xa_load returns NULL then we cannot differentiate a real unused PASID vs. one which has been attached w/o an handle.

Does it suggest that having the caller to always provide a handle makes more sense?

author
#8 lore raw

Re: [PATCH v6 02/10] iommu: Remove sva handle list

Quoted 17 lines · Kevin
Quoted 4 lines

From: Lu Baolu <[email protected]>
Sent: Monday, May 27, 2024 12:05 PM

drivers/iommu/iommu-sva.c static struct iommu_mm_data · line 77

Quoted 10 lines
drivers/iommu/iommu-sva.c +2 -0
@@ -69,11 +68,16 @@ static struct iommu_mm_data
 *iommu_alloc_mm_data(struct mm_struct *mm, struct de
   */
  struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct
 mm_struct *mm)
  {
+	struct iommu_group *group = dev->iommu_group;
+	struct iommu_attach_handle *attach_handle;
  	struct iommu_mm_data *iommu_mm;
  	struct iommu_domain *domain;
  	struct iommu_sva *handle;

it's confusing to have both 'handle' and 'attach_handle' in one function.

Quoted 1 line

Clearer to rename 'handle' as 'sva'.

Yes. Could be cleaned up in a separated patch. All sva handle in iommu-sva.c should be converted if we decide to do that.

Quoted 49 lines
Quoted 27 lines
drivers/iommu/iommu-sva.c +3 -0
  	int ret;
 
+	if (!group)
+		return ERR_PTR(-ENODEV);
+
  	mutex_lock(&iommu_sva_lock);

/* Allocate mm->pasid if necessary. */

(quoted diff) +0 -0
@@ -83,12 +87,13 @@ struct iommu_sva *iommu_sva_bind_device(struct

device *dev, struct mm_struct *mm
goto out_unlock;
}

(quoted diff) +0 -1
-	list_for_each_entry(handle, &mm->iommu_mm->sva_handles,

handle_item) {

drivers/iommu/iommu-sva.c +2 -5
-		if (handle->dev == dev) {
-			refcount_inc(&handle->users);
-			mutex_unlock(&iommu_sva_lock);
-			return handle;
-		}
+	/* A bond already exists, just take a reference`. */
+	attach_handle = iommu_attach_handle_get(group, iommu_mm-
Quoted 1 line

pasid, IOMMU_DOMAIN_SVA);

(quoted diff) +2 -0
+	if (!IS_ERR(attach_handle)) {
+		handle = container_of(attach_handle, struct iommu_sva,

handle);

drivers/iommu/iommu-sva.c struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm · line 97

Quoted 4 lines
drivers/iommu/iommu-sva.c +3 -0
+		refcount_inc(&handle->users);
+		mutex_unlock(&iommu_sva_lock);
+		return handle;
  	}

It's counter-intuitive to move forward when an error is returned.

e.g. if it's -EBUSY indicating the pasid already used for another type then following attempts shouldn't been tried.

probably we should have iommu_attach_handle_get() return NULL instead of -ENOENT when the entry is free? then:

	attach_handle = iommu_attach_handle_get();
	if (IS_ERR(attach_handle)) {
		ret = PTR_ERR(attach_handle);
		goto out_unlock;
	} else if (attach_handle) {
		/* matched and increase handle->users */
	}

	/* free entry falls through */
Quoted 4 lines

But then there is one potential issue with the design that 'handle' can be optional in iommu_attach_device_pasid(). In that case xa_load returns NULL then we cannot differentiate a real unused PASID vs. one which has been attached w/o an handle.

The PASID should be allocated exclusively. This means that once a PASID is assigned to A, it shouldn't be assigned to B at the same time. If a single PASID is used for multiple purposes, it's likely a bug in the system.

So the logic of iommu_attach_handle_get() here is: has an SVA domain already been installed for this PASID? If so, just reuse it. Otherwise, try to install a new SVA domain.

Quoted 2 lines

Does it suggest that having the caller to always provide a handle makes more sense?

I'm neutral on this since only sva bind and iopf path delivery currently require an attach handle.

Best regards,
baolu

maintainer
#9 lore raw

RE: [PATCH v6 02/10] iommu: Remove sva handle list

Quoted 47 lines

From: Baolu Lu <[email protected]>
Sent: Thursday, June 6, 2024 2:07 PM

Quoted 38 lines · Kevin
Quoted 16 lines

From: Lu Baolu <[email protected]>
Sent: Monday, May 27, 2024 12:05 PM

(quoted diff) +0 -1
-	list_for_each_entry(handle, &mm->iommu_mm->sva_handles,

handle_item) {

drivers/iommu/iommu-sva.c +2 -5
-		if (handle->dev == dev) {
-			refcount_inc(&handle->users);
-			mutex_unlock(&iommu_sva_lock);
-			return handle;
-		}
+	/* A bond already exists, just take a reference`. */
+	attach_handle = iommu_attach_handle_get(group, iommu_mm-
Quoted 1 line

pasid, IOMMU_DOMAIN_SVA);

(quoted diff) +2 -0
+	if (!IS_ERR(attach_handle)) {
+		handle = container_of(attach_handle, struct iommu_sva,

handle);

drivers/iommu/iommu-sva.c struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm · line 97

Quoted 4 lines
drivers/iommu/iommu-sva.c +3 -0
+		refcount_inc(&handle->users);
+		mutex_unlock(&iommu_sva_lock);
+		return handle;
  	}

It's counter-intuitive to move forward when an error is returned.

e.g. if it's -EBUSY indicating the pasid already used for another type then following attempts shouldn't been tried.

probably we should have iommu_attach_handle_get() return NULL instead of -ENOENT when the entry is free? then:

	attach_handle = iommu_attach_handle_get();
	if (IS_ERR(attach_handle)) {
		ret = PTR_ERR(attach_handle);
		goto out_unlock;
	} else if (attach_handle) {
		/* matched and increase handle->users */
	}

	/* free entry falls through */
Quoted 4 lines

But then there is one potential issue with the design that 'handle' can be optional in iommu_attach_device_pasid(). In that case xa_load returns NULL then we cannot differentiate a real unused PASID vs. one which has been attached w/o an handle.

Quoted 4 lines

The PASID should be allocated exclusively. This means that once a PASID is assigned to A, it shouldn't be assigned to B at the same time. If a single PASID is used for multiple purposes, it's likely a bug in the system.

yes there is a bug but catching it here would make diagnostic easier.

Quoted 11 lines

So the logic of iommu_attach_handle_get() here is: has an SVA domain already been installed for this PASID? If so, just reuse it. Otherwise, try to install a new SVA domain.

Quoted 2 lines

Does it suggest that having the caller to always provide a handle makes more sense?

I'm neutral on this since only sva bind and iopf path delivery currently require an attach handle.

let's hear Jason's opinion.

#10 lore raw

Re: [PATCH v6 02/10] iommu: Remove sva handle list

Quoted 27 lines · Kevin
Quoted 27 lines

From: Baolu Lu <[email protected]>
Sent: Thursday, June 6, 2024 2:07 PM

Quoted 23 lines · Kevin
Quoted 16 lines

From: Lu Baolu <[email protected]>
Sent: Monday, May 27, 2024 12:05 PM

(quoted diff) +0 -1
-	list_for_each_entry(handle, &mm->iommu_mm->sva_handles,

handle_item) {

drivers/iommu/iommu-sva.c +2 -5
-		if (handle->dev == dev) {
-			refcount_inc(&handle->users);
-			mutex_unlock(&iommu_sva_lock);
-			return handle;
-		}
+	/* A bond already exists, just take a reference`. */
+	attach_handle = iommu_attach_handle_get(group, iommu_mm-
Quoted 1 line

pasid, IOMMU_DOMAIN_SVA);

(quoted diff) +2 -0
+	if (!IS_ERR(attach_handle)) {
+		handle = container_of(attach_handle, struct iommu_sva,

handle);

drivers/iommu/iommu-sva.c struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm · line 97

Quoted 4 lines
drivers/iommu/iommu-sva.c +3 -0
+		refcount_inc(&handle->users);
+		mutex_unlock(&iommu_sva_lock);
+		return handle;
  	}

It's counter-intuitive to move forward when an error is returned.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

e.g. if it's -EBUSY indicating the pasid already used for another type then following attempts shouldn't been tried.

Yes, it looks like iommu_sva_bind_device() should fail with EBUSY if the PASID is already in use and is not exactly the same SVA as being asked for here.

It will eventually do this naturally when iommu_attach_device_pasid() is called with an in-use PASID, but may as well do it here for clarity.

Also, is there a missing test for the same mm too?

I'd maybe change iommu_attach_handle() to return NULL if there is no handle and then write it like:

if (IS_ERR(attach_handle) && PTR_ERR(attach_handle) != -ENOENT) {
	ret = PTR_ERR(attach_handle);
	goto out_unlock;
}

if (!IS_ERR(attach_handle) && attach_handle->domain->mm == mm) {
/* Can re-use the existing SVA attachment */
}

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Does it suggest that having the caller to always provide a handle makes more sense?

I was thinking no just to avoid memory allocation.. But how does the caller not provide a handle? My original draft of this concept used an XA_MARK to indicate if the xarray pointed to a handle or a domain

This seems to require the handle:

- curr = xa_cmpxchg(&group->pasid_array, pasid, NULL, domain, GFP_KERNEL);
- if (curr) {
- ret = xa_err(curr) ? : -EBUSY;
+ ret = xa_insert(&group->pasid_array, pasid, handle, GFP_KERNEL);

Confused.

Quoted 4 lines
Quoted 2 lines

I'm neutral on this since only sva bind and iopf path delivery currently require an attach handle.

let's hear Jason's opinion.

At least iommu_attach_handle_get() should not return NULL if there is no handle, it should return EBUSY as though it couldn't match the type.

Jason

author
#11 lore raw

Re: [PATCH v6 02/10] iommu: Remove sva handle list

Quoted 44 lines · Jason Gunthorpe
Quoted 27 lines · Kevin
Quoted 27 lines

From: Baolu Lu <[email protected]>
Sent: Thursday, June 6, 2024 2:07 PM

Quoted 23 lines · Kevin
Quoted 16 lines

From: Lu Baolu <[email protected]>
Sent: Monday, May 27, 2024 12:05 PM

(quoted diff) +0 -1
-	list_for_each_entry(handle, &mm->iommu_mm->sva_handles,

handle_item) {

(quoted diff) +2 -5
-		if (handle->dev == dev) {
-			refcount_inc(&handle->users);
-			mutex_unlock(&iommu_sva_lock);
-			return handle;
-		}
+	/* A bond already exists, just take a reference`. */
+	attach_handle = iommu_attach_handle_get(group, iommu_mm-
Quoted 1 line

pasid, IOMMU_DOMAIN_SVA);

(quoted diff) +2 -0
+	if (!IS_ERR(attach_handle)) {
+		handle = container_of(attach_handle, struct iommu_sva,

handle);

Review

Quoted 4 lines
(quoted diff) +3 -0
+		refcount_inc(&handle->users);
+		mutex_unlock(&iommu_sva_lock);
+		return handle;
   	}

It's counter-intuitive to move forward when an error is returned.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

e.g. if it's -EBUSY indicating the pasid already used for another type then following attempts shouldn't been tried.

Yes, it looks like iommu_sva_bind_device() should fail with EBUSY if the PASID is already in use and is not exactly the same SVA as being asked for here.

It will eventually do this naturally when iommu_attach_device_pasid() is called with an in-use PASID, but may as well do it here for clarity.

Also, is there a missing test for the same mm too?

I'd maybe change iommu_attach_handle() to return NULL if there is no handle and then write it like:

Quoted 9 lines
if (IS_ERR(attach_handle) && PTR_ERR(attach_handle) != -ENOENT) {
	ret = PTR_ERR(attach_handle);
	goto out_unlock;
}
if (!IS_ERR(attach_handle) && attach_handle->domain->mm == mm) {
    /* Can re-use the existing SVA attachment */
}

Okay, I will change it like below:

drivers/iommu/iommu-sva.c +0 -0
@@ -91,11 +91,20 @@ struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm
        attach_handle = iommu_attach_handle_get(group, iommu_mm->pasid, 

IOMMU_DOMAIN_SVA);
if (!IS_ERR(attach_handle)) {
handle = container_of(attach_handle, struct iommu_sva,
handle);
+ if (attach_handle->domain->mm != mm) {
+ ret = -EBUSY;
+ goto out_unlock;
+ }

                refcount_inc(&handle->users);
                mutex_unlock(&iommu_sva_lock);
                return handle;
        }

+ if (PTR_ERR(attach_handle) != -ENOENT) {
+ ret = PTR_ERR(attach_handle);
+ goto out_unlock;
+ }
+

        handle = kzalloc(sizeof(*handle), GFP_KERNEL);
        if (!handle) {
                ret = -ENOMEM;
Quoted 14 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Does it suggest that having the caller to always provide a handle makes more sense?

I was thinking no just to avoid memory allocation.. But how does the caller not provide a handle? My original draft of this concept used an XA_MARK to indicate if the xarray pointed to a handle or a domain

This seems to require the handle:

(quoted diff) +1 -3
-	curr = xa_cmpxchg(&group->pasid_array, pasid, NULL, domain, GFP_KERNEL);
-	if (curr) {
-		ret = xa_err(curr) ? : -EBUSY;
+	ret = xa_insert(&group->pasid_array, pasid, handle, GFP_KERNEL);
Quoted 1 line

Confused.

XA_MARK was used to indicate whether the stored pointer was an iommu domain or an attach handle. Since this series removes iommu_get_domain_for_dev_pasid(), there is no longer any need to store the domain pointer at all.

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

I'm neutral on this since only sva bind and iopf path delivery currently require an attach handle.

let's hear Jason's opinion.

At least iommu_attach_handle_get() should not return NULL if there is no handle, it should return EBUSY as though it couldn't match the type.

Agreed.

Quoted 2 lines

Jason

Best regards,
baolu

author
#12 lore raw

PATCH v6 3/10 iommu: Add attach handle to struct iopf_group

In reply to Lu Baolu

Previously, the domain that a page fault targets is stored in an iopf_group, which represents a minimal set of page faults. With the introduction of attach handle, replace the domain with the handle so that the fault handler can obtain more information as needed when handling the faults.

iommu_report_device_fault() is currently used for SVA page faults, which handles the page fault in an internal cycle. The domain is retrieved with iommu_get_domain_for_dev_pasid() if the pasid in the fault message is valid. This doesn't work in IOMMUFD case, where if the pasid table of a device is wholly managed by user space, there is no domain attached to the PASID of the device, and all page faults are forwarded through a NESTING domain attaching to RID.

Add a new IOMMU capability flag, IOMMU_CAP_USER_IOASID_TABLE, which indicates if the IOMMU driver supports user-managed PASID tables. In the iopf deliver path, if no attach handle found for the iopf PASID, roll back to RID domain when the IOMMU driver supports this capability.

iommu_get_domain_for_dev_pasid() is no longer used and can be removed.

---
 include/linux/iommu.h      | 18 +++++-------
 drivers/iommu/io-pgfault.c | 59 +++++++++++++++++++++-----------------
 drivers/iommu/iommu-sva.c  |  3 +-
 drivers/iommu/iommu.c      | 39 -------------------------
 4 files changed, 41 insertions(+), 78 deletions(-)
include/linux/iommu.h +7 -11
index 823fa3bcc2c6..4067ebdd6232 100644
@@ -127,7 +127,7 @@ struct iopf_group {
 	/* list node for iommu_fault_param::faults */
 	struct list_head pending_node;
 	struct work_struct work;
-	struct iommu_domain *domain;
+	struct iommu_attach_handle *attach_handle;
 	/* The device's fault data parameter. */
 	struct iommu_fault_param *fault_param;
 };
@@ -249,6 +249,12 @@ enum iommu_cap {
 	 */
 	IOMMU_CAP_DEFERRED_FLUSH,
 	IOMMU_CAP_DIRTY_TRACKING,	/* IOMMU supports dirty tracking */
+	/*
+	 * IOMMU driver supports user-managed IOASID table. There is no
+	 * user domain for each PASID and the I/O page faults are forwarded
+	 * through the user domain attached to the device RID.
+	 */
+	IOMMU_CAP_USER_IOASID_TABLE,
 };
 
 /* These are the possible reserved region types */
@@ -1064,9 +1070,6 @@ int iommu_attach_device_pasid(struct iommu_domain *domain,
 			      struct iommu_attach_handle *handle);
 void iommu_detach_device_pasid(struct iommu_domain *domain,
 			       struct device *dev, ioasid_t pasid);
-struct iommu_domain *
-iommu_get_domain_for_dev_pasid(struct device *dev, ioasid_t pasid,
-			       unsigned int type);
 ioasid_t iommu_alloc_global_pasid(struct device *dev);
 void iommu_free_global_pasid(ioasid_t pasid);
 #else /* CONFIG_IOMMU_API */
@@ -1408,13 +1411,6 @@ static inline void iommu_detach_device_pasid(struct iommu_domain *domain,
 {
 }
 
-static inline struct iommu_domain *
-iommu_get_domain_for_dev_pasid(struct device *dev, ioasid_t pasid,
-			       unsigned int type)
-{
-	return NULL;
-}
-
 static inline ioasid_t iommu_alloc_global_pasid(struct device *dev)
 {
 	return IOMMU_PASID_INVALID;
drivers/iommu/io-pgfault.c +32 -27

82 lines not on this page · raw message

drivers/iommu/iommu-sva.c +2 -1
index e85f4ccc9dcc..36d2862941de 100644
@@ -265,7 +265,8 @@ static void iommu_sva_handle_iopf(struct work_struct *work)
 		if (status != IOMMU_PAGE_RESP_SUCCESS)
 			break;
 
-		status = iommu_sva_handle_mm(&iopf->fault, group->domain->mm);
+		status = iommu_sva_handle_mm(&iopf->fault,
+					     group->attach_handle->domain->mm);
 	}
 
 	iopf_group_response(group, status);
drivers/iommu/iommu.c +0 -39
index 0263814cba6b..c506185a2fad 100644
@@ -3418,45 +3418,6 @@ void iommu_detach_device_pasid(struct iommu_domain *domain, struct device *dev,
 }
 EXPORT_SYMBOL_GPL(iommu_detach_device_pasid);
 
-/*
- * iommu_get_domain_for_dev_pasid() - Retrieve domain for @pasid of @dev
- * @dev: the queried device
- * @pasid: the pasid of the device
- * @type: matched domain type, 0 for any match
- *
- * This is a variant of iommu_get_domain_for_dev(). It returns the existing
- * domain attached to pasid of a device. Callers must hold a lock around this
- * function, and both iommu_attach/detach_dev_pasid() whenever a domain of
- * type is being manipulated. This API does not internally resolve races with
- * attach/detach.
- *
- * Return: attached domain on success, NULL otherwise.
- */
-struct iommu_domain *iommu_get_domain_for_dev_pasid(struct device *dev,
-						    ioasid_t pasid,
-						    unsigned int type)
-{
-	/* Caller must be a probed driver on dev */
-	struct iommu_group *group = dev->iommu_group;
-	struct iommu_attach_handle *handle;
-	struct iommu_domain *domain = NULL;
-
-	if (!group)
-		return NULL;
-
-	xa_lock(&group->pasid_array);
-	handle = xa_load(&group->pasid_array, pasid);
-	if (handle)
-		domain = handle->domain;
-
-	if (type && domain && domain->type != type)
-		domain = NULL;
-	xa_unlock(&group->pasid_array);
-
-	return domain;
-}
-EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev_pasid);
-
 ioasid_t iommu_alloc_global_pasid(struct device *dev)
 {
 	int ret;
Signature · 1 line
2.34.1
maintainer
#13 lore raw

RE: [PATCH v6 03/10] iommu: Add attach handle to struct iopf_group

Quoted 8 lines

From: Lu Baolu <[email protected]>
Sent: Monday, May 27, 2024 12:05 PM

include/linux/iommu.h +0 -0
@@ -249,6 +249,12 @@ enum iommu_cap {
 	 */
 	IOMMU_CAP_DEFERRED_FLUSH,
 	IOMMU_CAP_DIRTY_TRACKING,	/* IOMMU supports dirty

tracking */

include/linux/iommu.h enum iommu_cap { · line 258

Quoted 7 lines
include/linux/iommu.h +6 -0
+	/*
+	 * IOMMU driver supports user-managed IOASID table. There is no
+	 * user domain for each PASID and the I/O page faults are forwarded
+	 * through the user domain attached to the device RID.
+	 */
+	IOMMU_CAP_USER_IOASID_TABLE,
 };

Given all other context are around PASID let's just call it as USER_PASID_TABLE.

btw this goes differently from your plan in [1] which tried to introduce different nesting types between Intel and other vendors.

I guess the reason might be that you want to avoid getting the handle for RID on Intel platform in case of failing to find the handle for the faulting PASID. and save a new domain type.

this looks fine to me but should be explained.

[1] https://lore.kernel.org/linux-iommu/[email protected]/

author
#14 lore raw

Re: [PATCH v6 03/10] iommu: Add attach handle to struct iopf_group

Quoted 18 lines · Kevin
Quoted 8 lines

From: Lu Baolu <[email protected]>
Sent: Monday, May 27, 2024 12:05 PM

include/linux/iommu.h +0 -0
@@ -249,6 +249,12 @@ enum iommu_cap {
  	 */
  	IOMMU_CAP_DEFERRED_FLUSH,
  	IOMMU_CAP_DIRTY_TRACKING,	/* IOMMU supports dirty

tracking */

include/linux/iommu.h enum iommu_cap { · line 258

Quoted 7 lines
include/linux/iommu.h +6 -0
+	/*
+	 * IOMMU driver supports user-managed IOASID table. There is no
+	 * user domain for each PASID and the I/O page faults are forwarded
+	 * through the user domain attached to the device RID.
+	 */
+	IOMMU_CAP_USER_IOASID_TABLE,
  };

Given all other context are around PASID let's just call it as USER_PASID_TABLE.

Quoted 8 lines

btw this goes differently from your plan in [1] which tried to introduce different nesting types between Intel and other vendors.

I guess the reason might be that you want to avoid getting the handle for RID on Intel platform in case of failing to find the handle for the faulting PASID. and save a new domain type.

this looks fine to me but should be explained.

Yeah! I was considering this in two aspects and chose this simple solution in the end.

- If we choose to add a new domain type, we need to change iommufd, iommu core and the individual driver. That seems too complicated to address a small issue here.

- Fundamentally, this is a hardware implementation difference, hence use the existing per-device iommu capability interface sounds more reasonable.

Quoted 3 lines

[1] https://lore.kernel.org/linux-iommu/[email protected]/

Best regards,
baolu

#15 lore raw

Re: [PATCH v6 03/10] iommu: Add attach handle to struct iopf_group

In reply to Lu Baolu

drivers/iommu/io-pgfault.c void iommu_report_device_fault(struct device *dev, struct iopf_fault *evt) · line 191

Quoted 13 lines · Lu Baolu
drivers/iommu/io-pgfault.c +7 -2
@@ -206,20 +182,49 @@ void iommu_report_device_fault(struct device *dev, struct iopf_fault *evt)
 	if (group == &abort_group)
 		goto err_abort;
 
-	group->domain = get_domain_for_iopf(dev, fault);
-	if (!group->domain)
+	if (fault->prm.flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID) {
+		group->attach_handle = iommu_attach_handle_get(dev->iommu_group,
+							       fault->prm.pasid,
+							       0);
+		if (IS_ERR(group->attach_handle)) {
+			if (!device_iommu_capable(dev, IOMMU_CAP_USER_IOASID_TABLE))
+				goto err_abort;

I'm not excited about calling a function pointer on every fault. Let's just add a constant flag to iommu_ops?

Jason

author
#16 lore raw

Re: [PATCH v6 03/10] iommu: Add attach handle to struct iopf_group

Quoted 5 lines · Jason Gunthorpe
Quoted 4 lines · Lu Baolu
(quoted diff) +0 -0
@@ -206,20 +182,49 @@ void iommu_report_device_fault(struct device *dev, struct iopf_fault *evt)
  	if (group == &abort_group)
  		goto err_abort;
 
Quoted 12 lines

Review void iommu_report_device_fault(struct device *dev, struct iopf_fault *evt) · line 191

Quoted 9 lines
(quoted diff) +7 -2
-	group->domain = get_domain_for_iopf(dev, fault);
-	if (!group->domain)
+	if (fault->prm.flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID) {
+		group->attach_handle = iommu_attach_handle_get(dev->iommu_group,
+							       fault->prm.pasid,
+							       0);
+		if (IS_ERR(group->attach_handle)) {
+			if (!device_iommu_capable(dev, IOMMU_CAP_USER_IOASID_TABLE))
+				goto err_abort;

I'm not excited about calling a function pointer on every fault. Let's just add a constant flag to iommu_ops?

Yes, it's reasonable given this is a critical path. How about below additional change?

include/linux/iommu.h +0 -6
index 16b3a2da91ef..69ea4d0374b9 100644
@@ -253,12 +253,6 @@ enum iommu_cap {
         */
        IOMMU_CAP_DEFERRED_FLUSH,
        IOMMU_CAP_DIRTY_TRACKING,       /* IOMMU supports dirty tracking */
-       /*
-        * IOMMU driver supports user-managed IOASID table. There is no
-        * user domain for each PASID and the I/O page faults are forwarded
-        * through the user domain attached to the device RID.
-        */
-       IOMMU_CAP_USER_IOASID_TABLE,
 };
 
 /* These are the possible reserved region types */
@@ -557,6 +551,10 @@ static inline int __iommu_copy_struct_from_user_array(
  * @default_domain: If not NULL this will always be set as the default 

domain.
* This should be an IDENTITY/BLOCKED/PLATFORM domain.
* Do not use in new drivers.
+ * @user_pasid_table: IOMMU driver supports user-managed PASID table. There is

+ *                    no user domain for each PASID and the I/O page faults are
+ *                    forwarded through the user domain attached to the device
+ *                    RID.
  */
 struct iommu_ops {
        bool (*capable)(struct device *dev, enum iommu_cap);
@@ -600,6 +598,7 @@ struct iommu_ops {
        struct iommu_domain *blocked_domain;
        struct iommu_domain *release_domain;
        struct iommu_domain *default_domain;

+ bool user_pasid_table;
};

/**

drivers/iommu/io-pgfault.c +0 -0
index a629d8a93614..cd679c13752e 100644
@@ -189,7 +189,9 @@ void iommu_report_device_fault(struct device *dev, struct iopf_fault *evt)
 

fault->prm.pasid,

                                                               0);
                if (IS_ERR(group->attach_handle)) {

- if (!device_iommu_capable(dev, IOMMU_CAP_USER_IOASID_TABLE))
+ const struct iommu_ops *ops = dev_iommu_ops(dev);
+
+ if (!ops->user_pasid_table)

                                goto err_abort;

Best regards,
baolu

#17 lore raw

Re: [PATCH v6 03/10] iommu: Add attach handle to struct iopf_group

Review struct iommu_ops { · line 602

Quoted 8 lines · Baolu Lu
 struct iommu_ops {
        bool (*capable)(struct device *dev, enum iommu_cap);
(quoted diff) +1 -0
@@ -600,6 +598,7 @@ struct iommu_ops {
        struct iommu_domain *blocked_domain;
        struct iommu_domain *release_domain;
        struct iommu_domain *default_domain;
+       bool user_pasid_table;
 };

Yeah, maybe spell it

u8 user_pasid_table : 1;

Jason

author
#18 lore raw

Re: [PATCH v6 03/10] iommu: Add attach handle to struct iopf_group

Quoted 12 lines · Jason Gunthorpe

Review struct iommu_ops { · line 602

Quoted 8 lines · Baolu Lu
  struct iommu_ops {
         bool (*capable)(struct device *dev, enum iommu_cap);
(quoted diff) +1 -0
@@ -600,6 +598,7 @@ struct iommu_ops {
         struct iommu_domain *blocked_domain;
         struct iommu_domain *release_domain;
         struct iommu_domain *default_domain;
+       bool user_pasid_table;
  };

Yeah, maybe spell it

Quoted 1 line
   u8 user_pasid_table : 1;

Done.

Best regards,
baolu

author
#19 lore raw

PATCH v6 4/10 iommu: Extend domain attach group with handle support

In reply to Lu Baolu

Unlike the SVA case where each PASID of a device has an SVA domain attached to it, the I/O page faults are handled by the fault handler of the SVA domain. The I/O page faults for a user page table might be handled by the domain attached to RID or the domain attached to the PASID, depending on whether the PASID table is managed by user space or kernel. As a result, there is a need for the domain attach group interfaces to have attach handle support. The attach handle will be forwarded to the fault handler of the user domain.

Add some variants of the domain attaching group interfaces so that they could support the attach handle and export them for use in IOMMUFD.

---
 drivers/iommu/iommu-priv.h |  8 +++
 drivers/iommu/iommu.c      | 99 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 107 insertions(+)
drivers/iommu/iommu-priv.h +8 -0
index f1536a5ebb0d..c37801c32f33 100644
@@ -31,4 +31,12 @@ void iommu_device_unregister_bus(struct iommu_device *iommu,
 struct iommu_attach_handle *iommu_attach_handle_get(struct iommu_group *group,
 						    ioasid_t pasid,
 						    unsigned int type);
+int iommu_attach_group_handle(struct iommu_domain *domain,
+			      struct iommu_group *group,
+			      struct iommu_attach_handle *handle);
+void iommu_detach_group_handle(struct iommu_domain *domain,
+			       struct iommu_group *group);
+int iommu_replace_group_handle(struct iommu_group *group,
+			       struct iommu_domain *new_domain,
+			       struct iommu_attach_handle *handle);
 #endif /* __LINUX_IOMMU_PRIV_H */
drivers/iommu/iommu.c +99 -0

102 lines not on this page · raw message

Signature · 1 line
2.34.1
#20 lore raw

Re: [PATCH v6 04/10] iommu: Extend domain attach group with handle support

Quoted 9 lines · Lu Baolu

Unlike the SVA case where each PASID of a device has an SVA domain attached to it, the I/O page faults are handled by the fault handler of the SVA domain. The I/O page faults for a user page table might be handled by the domain attached to RID or the domain attached to the PASID, depending on whether the PASID table is managed by user space or kernel. As a result, there is a need for the domain attach group interfaces to have attach handle support. The attach handle will be forwarded to the fault handler of the user domain.

Quoted 8 lines

Add some variants of the domain attaching group interfaces so that they could support the attach handle and export them for use in IOMMUFD.

---
 drivers/iommu/iommu-priv.h |  8 +++
 drivers/iommu/iommu.c      | 99 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 107 insertions(+)

I don't have an objection to it like this, but I wonder if we could be smaller to teach iommu_attach_device_pasid to use IOMMU_NO_PASID to attach the handle to the rid?

It would have an if there to call the __iommu_attach_group() instead of the pasid attach ?

Jason

author
#21 lore raw

Re: [PATCH v6 04/10] iommu: Extend domain attach group with handle support

Quoted 18 lines · Jason Gunthorpe
Quoted 9 lines · Lu Baolu

Unlike the SVA case where each PASID of a device has an SVA domain attached to it, the I/O page faults are handled by the fault handler of the SVA domain. The I/O page faults for a user page table might be handled by the domain attached to RID or the domain attached to the PASID, depending on whether the PASID table is managed by user space or kernel. As a result, there is a need for the domain attach group interfaces to have attach handle support. The attach handle will be forwarded to the fault handler of the user domain.

Quoted 8 lines

Add some variants of the domain attaching group interfaces so that they could support the attach handle and export them for use in IOMMUFD.

---
  drivers/iommu/iommu-priv.h |  8 +++
  drivers/iommu/iommu.c      | 99 ++++++++++++++++++++++++++++++++++++++
  2 files changed, 107 insertions(+)
Quoted 6 lines

I don't have an objection to it like this, but I wonder if we could be smaller to teach iommu_attach_device_pasid to use IOMMU_NO_PASID to attach the handle to the rid?

It would have an if there to call the __iommu_attach_group() instead of the pasid attach ?

This is a good idea. I guess that in the future, we will have iommu_attach_device_pasid(IOMMU_NO_PASID) to replace iommu_attach_group(). The group->domain and domain_ops::attach_dev will also be removed.

I'd suggest making such refactoring in a separate series with wider discussions. For now, let's make this special interface for iommufd.

Best regards,
baolu

author
#22 lore raw

PATCH v6 5/10 iommufd: Add fault and response message definitions

In reply to Lu Baolu

iommu_hwpt_pgfaults represent fault messages that the userspace can retrieve. Multiple iommu_hwpt_pgfaults might be put in an iopf group, with the IOMMU_PGFAULT_FLAGS_LAST_PAGE flag set only for the last iommu_hwpt_pgfault.

An iommu_hwpt_page_response is a response message that the userspace should send to the kernel after finishing handling a group of fault messages. The @dev_id, @pasid, and @grpid fields in the message identify an outstanding iopf group for a device. The @cookie field, which matches the cookie field of the last fault in the group, will be used by the kernel to look up the pending message.

---
 include/uapi/linux/iommufd.h | 96 ++++++++++++++++++++++++++++++++++++
 1 file changed, 96 insertions(+)
include/uapi/linux/iommufd.h +96 -0
index 1dfeaa2e649e..2f34d66436fb 100644
@@ -692,4 +692,100 @@ struct iommu_hwpt_invalidate {
 	__u32 __reserved;
 };
 #define IOMMU_HWPT_INVALIDATE _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_INVALIDATE)
+
+/**
+ * enum iommu_hwpt_pgfault_flags - flags for struct iommu_hwpt_pgfault
+ * @IOMMU_PGFAULT_FLAGS_PASID_VALID: The pasid field of the fault data is
+ *                                   valid.
+ * @IOMMU_PGFAULT_FLAGS_LAST_PAGE: It's the last fault of a fault group.
+ */
+enum iommu_hwpt_pgfault_flags {
+	IOMMU_PGFAULT_FLAGS_PASID_VALID		= (1 << 0),
+	IOMMU_PGFAULT_FLAGS_LAST_PAGE		= (1 << 1),
+};
+
+/**
+ * enum iommu_hwpt_pgfault_perm - perm bits for struct iommu_hwpt_pgfault
+ * @IOMMU_PGFAULT_PERM_READ: request for read permission
+ * @IOMMU_PGFAULT_PERM_WRITE: request for write permission
+ * @IOMMU_PGFAULT_PERM_EXEC: (PCIE 10.4.1) request with a PASID that has the
+ *                           Execute Requested bit set in PASID TLP Prefix.
+ * @IOMMU_PGFAULT_PERM_PRIV: (PCIE 10.4.1) request with a PASID that has the
+ *                           Privileged Mode Requested bit set in PASID TLP
+ *                           Prefix.
+ */
+enum iommu_hwpt_pgfault_perm {
+	IOMMU_PGFAULT_PERM_READ			= (1 << 0),
+	IOMMU_PGFAULT_PERM_WRITE		= (1 << 1),
+	IOMMU_PGFAULT_PERM_EXEC			= (1 << 2),
+	IOMMU_PGFAULT_PERM_PRIV			= (1 << 3),
+};
+
+/**
+ * struct iommu_hwpt_pgfault - iommu page fault data
+ * @size: sizeof(struct iommu_hwpt_pgfault)
+ * @flags: Combination of enum iommu_hwpt_pgfault_flags
+ * @dev_id: id of the originated device
+ * @pasid: Process Address Space ID
+ * @grpid: Page Request Group Index
+ * @perm: Combination of enum iommu_hwpt_pgfault_perm
+ * @addr: Fault address
+ * @length: a hint of how much data the requestor is expecting to fetch. For
+ *          example, if the PRI initiator knows it is going to do a 10MB
+ *          transfer, it could fill in 10MB and the OS could pre-fault in
+ *          10MB of IOVA. It's default to 0 if there's no such hint.
+ * @cookie: kernel-managed cookie identifying a group of fault messages. The
+ *          cookie number encoded in the last page fault of the group should
+ *          be echoed back in the response message.
+ */
+struct iommu_hwpt_pgfault {
+	__u32 size;
+	__u32 flags;
+	__u32 dev_id;
+	__u32 pasid;
+	__u32 grpid;
+	__u32 perm;
+	__u64 addr;
+	__u32 length;
+	__u32 cookie;
+};
+
+/**
+ * enum iommufd_page_response_code - Return status of fault handlers
+ * @IOMMUFD_PAGE_RESP_SUCCESS: Fault has been handled and the page tables
+ *                             populated, retry the access. This is the
+ *                             "Success" defined in PCI 10.4.2.1.
+ * @IOMMUFD_PAGE_RESP_INVALID: Could not handle this fault, don't retry the
+ *                             access. This is the "Invalid Request" in PCI
+ *                             10.4.2.1.
+ * @IOMMUFD_PAGE_RESP_FAILURE: General error. Drop all subsequent faults from
+ *                             this device if possible. This is the "Response
+ *                             Failure" in PCI 10.4.2.1.
+ */
+enum iommufd_page_response_code {
+	IOMMUFD_PAGE_RESP_SUCCESS = 0,
+	IOMMUFD_PAGE_RESP_INVALID,
+	IOMMUFD_PAGE_RESP_FAILURE,
+};
+
+/**
+ * struct iommu_hwpt_page_response - IOMMU page fault response
+ * @size: sizeof(struct iommu_hwpt_page_response)
+ * @flags: Must be set to 0
+ * @dev_id: device ID of target device for the response
+ * @pasid: Process Address Space ID
+ * @grpid: Page Request Group Index
+ * @code: One of response code in enum iommufd_page_response_code.
+ * @cookie: The kernel-managed cookie reported in the fault message.
+ */
+struct iommu_hwpt_page_response {
+	__u32 size;
+	__u32 flags;
+	__u32 dev_id;
+	__u32 pasid;
+	__u32 grpid;
+	__u32 code;
+	__u32 cookie;
+	__u32 reserved;
+};
 #endif
Signature · 1 line
2.34.1
maintainer
#23 lore raw

RE: [PATCH v6 05/10] iommufd: Add fault and response message definitions

Quoted 14 lines

From: Lu Baolu <[email protected]>
Sent: Monday, May 27, 2024 12:05 PM

include/uapi/linux/iommufd.h +11 -0
+
+/**
+ * struct iommu_hwpt_page_response - IOMMU page fault response
+ * @size: sizeof(struct iommu_hwpt_page_response)
+ * @flags: Must be set to 0
+ * @dev_id: device ID of target device for the response
+ * @pasid: Process Address Space ID
+ * @grpid: Page Request Group Index
+ * @code: One of response code in enum iommufd_page_response_code.
+ * @cookie: The kernel-managed cookie reported in the fault message.
+ */

include/uapi/linux/iommufd.h struct iommu_hwpt_invalidate { · line 790

Quoted 10 lines
include/uapi/linux/iommufd.h +10 -0
+struct iommu_hwpt_page_response {
+	__u32 size;
+	__u32 flags;
+	__u32 dev_id;
+	__u32 pasid;
+	__u32 grpid;
+	__u32 code;
+	__u32 cookie;
+	__u32 reserved;
+};

with the response queue per fault object we don't need all fields here, e.g. dev_id, pasid, etc. Cookie is sufficient.

author
#24 lore raw

Re: [PATCH v6 05/10] iommufd: Add fault and response message definitions

Quoted 19 lines · Kevin
Quoted 19 lines

From: Lu Baolu <[email protected]>
Sent: Monday, May 27, 2024 12:05 PM

include/uapi/linux/iommufd.h +16 -0
+
+/**
+ * struct iommu_hwpt_page_response - IOMMU page fault response
+ * @size: sizeof(struct iommu_hwpt_page_response)
+ * @flags: Must be set to 0
+ * @dev_id: device ID of target device for the response
+ * @pasid: Process Address Space ID
+ * @grpid: Page Request Group Index
+ * @code: One of response code in enum iommufd_page_response_code.
+ * @cookie: The kernel-managed cookie reported in the fault message.
+ */
+struct iommu_hwpt_page_response {
+	__u32 size;
+	__u32 flags;
+	__u32 dev_id;
+	__u32 pasid;
Quoted 8 lines

include/uapi/linux/iommufd.h struct iommu_hwpt_invalidate { · line 790

Quoted 5 lines
include/uapi/linux/iommufd.h +5 -0
+	__u32 grpid;
+	__u32 code;
+	__u32 cookie;
+	__u32 reserved;
+};

with the response queue per fault object we don't need all fields here, e.g. dev_id, pasid, etc. Cookie is sufficient.

I prefer not to mess the definition of user API data and the kernel driver implementation. The kernel driver may change in the future, but the user API will remain stable for a long time.

I am neutral about whether we could remove above fields, but I need all maintainers to agree on this, given that this has undergone five rounds of review. :-)

Best regards,
baolu

maintainer
#25 lore raw

RE: [PATCH v6 05/10] iommufd: Add fault and response message definitions

Quoted 33 lines

From: Baolu Lu <[email protected]>
Sent: Thursday, June 6, 2024 2:28 PM

Quoted 12 lines · Kevin

include/uapi/linux/iommufd.h struct iommu_hwpt_invalidate {

Quoted 12 lines

From: Lu Baolu <[email protected]>
Sent: Monday, May 27, 2024 12:05 PM

include/uapi/linux/iommufd.h +9 -0
+
+/**
+ * struct iommu_hwpt_page_response - IOMMU page fault response
+ * @size: sizeof(struct iommu_hwpt_page_response)
+ * @flags: Must be set to 0
+ * @dev_id: device ID of target device for the response
+ * @pasid: Process Address Space ID
+ * @grpid: Page Request Group Index
+ * @code: One of response code in enum

iommufd_page_response_code.

Quoted 7 lines
Quoted 7 lines

+ * @cookie: The kernel-managed cookie reported in the fault message.
+ */

include/uapi/linux/iommufd.h +5 -0
+struct iommu_hwpt_page_response {
+	__u32 size;
+	__u32 flags;
+	__u32 dev_id;
+	__u32 pasid;
Quoted 8 lines

include/uapi/linux/iommufd.h struct iommu_hwpt_invalidate { · line 790

Quoted 5 lines
include/uapi/linux/iommufd.h +5 -0
+	__u32 grpid;
+	__u32 code;
+	__u32 cookie;
+	__u32 reserved;
+};

with the response queue per fault object we don't need all fields here, e.g. dev_id, pasid, etc. Cookie is sufficient.

Quoted 3 lines

I prefer not to mess the definition of user API data and the kernel driver implementation. The kernel driver may change in the future, but the user API will remain stable for a long time.

sure it remains stable for reasonable reason. Here we defined some fields but they are even not used and checked in the kernel. IMHO it suggests redundant definition. If there is value to keep them, do we need to at least verify them same as the completion record?

Quoted 5 lines

I am neutral about whether we could remove above fields, but I need all maintainers to agree on this, given that this has undergone five rounds of review. :-)

sure let's hear their opinions.

#26 lore raw

Re: [PATCH v6 05/10] iommufd: Add fault and response message definitions

Quoted 17 lines · Kevin
Quoted 17 lines

From: Baolu Lu <[email protected]>
Sent: Thursday, June 6, 2024 2:28 PM

Quoted 12 lines · Kevin

include/uapi/linux/iommufd.h struct iommu_hwpt_invalidate {

Quoted 12 lines

From: Lu Baolu <[email protected]>
Sent: Monday, May 27, 2024 12:05 PM

include/uapi/linux/iommufd.h +9 -0
+
+/**
+ * struct iommu_hwpt_page_response - IOMMU page fault response
+ * @size: sizeof(struct iommu_hwpt_page_response)
+ * @flags: Must be set to 0
+ * @dev_id: device ID of target device for the response
+ * @pasid: Process Address Space ID
+ * @grpid: Page Request Group Index
+ * @code: One of response code in enum

iommufd_page_response_code.

Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

+ * @cookie: The kernel-managed cookie reported in the fault message.
+ */

include/uapi/linux/iommufd.h +5 -0
+struct iommu_hwpt_page_response {
+	__u32 size;
+	__u32 flags;
+	__u32 dev_id;
+	__u32 pasid;
Quoted 8 lines
Quoted 8 lines

include/uapi/linux/iommufd.h struct iommu_hwpt_invalidate { · line 790

Quoted 5 lines
include/uapi/linux/iommufd.h +5 -0
+	__u32 grpid;
+	__u32 code;
+	__u32 cookie;
+	__u32 reserved;
+};

with the response queue per fault object we don't need all fields here, e.g. dev_id, pasid, etc. Cookie is sufficient.

Wait, why did we make it per object? The fault FD is supposed to be sharable across HWPTs.

Quoted 8 lines
Quoted 3 lines

I prefer not to mess the definition of user API data and the kernel driver implementation. The kernel driver may change in the future, but the user API will remain stable for a long time.

sure it remains stable for reasonable reason. Here we defined some fields but they are even not used and checked in the kernel. IMHO it suggests redundant definition. If there is value to keep them, do we need to at least verify them same as the completion record?

They are not here for the kernel, they are here for userspace.

A single HWPT and a single fault queue can be attached to multiple devices we need to return the dev_id so that userspace can know which device initiated the PRI. Same with PASID.

The only way we could remove them is if we are sure that no vIOMMU requires RID or PASID in the virtual fault queue PRI fault message.. I don't think that is true?

Cookie is not a replacement, cookie is an opaque value for the kernel to use to match a response to a request.

Jason

#27 lore raw

Re: [PATCH v6 05/10] iommufd: Add fault and response message definitions

Quoted 15 lines · Jason Gunthorpe
Quoted 8 lines
Quoted 3 lines

I prefer not to mess the definition of user API data and the kernel driver implementation. The kernel driver may change in the future, but the user API will remain stable for a long time.

sure it remains stable for reasonable reason. Here we defined some fields but they are even not used and checked in the kernel. IMHO it suggests redundant definition. If there is value to keep them, do we need to at least verify them same as the completion record?

They are not here for the kernel, they are here for userspace.

A single HWPT and a single fault queue can be attached to multiple devices we need to return the dev_id so that userspace can know which device initiated the PRI. Same with PASID.

Quoted 6 lines

The only way we could remove them is if we are sure that no vIOMMU requires RID or PASID in the virtual fault queue PRI fault message.. I don't think that is true?

Cookie is not a replacement, cookie is an opaque value for the kernel to use to match a response to a request.

Oh I got this wrong, the above is the response, yeah we can ditch everything but the cookie, and code right?

struct iommu_hwpt_page_response {
       __u32 cookie;
       __u32 code;
};

What is the workflow here? We expect the VMM will take the vIOMMU response and match it to a request cookie for the kernel to complete?

Jason

author
#28 lore raw

Re: [PATCH v6 05/10] iommufd: Add fault and response message definitions

Quoted 23 lines · Jason Gunthorpe
Quoted 13 lines · Jason Gunthorpe
Quoted 7 lines
Quoted 3 lines

I prefer not to mess the definition of user API data and the kernel driver implementation. The kernel driver may change in the future, but the user API will remain stable for a long time.

sure it remains stable for reasonable reason. Here we defined some fields but they are even not used and checked in the kernel. IMHO it suggests redundant definition. If there is value to keep them, do we need to at least verify them same as the completion record?

They are not here for the kernel, they are here for userspace.

A single HWPT and a single fault queue can be attached to multiple devices we need to return the dev_id so that userspace can know which device initiated the PRI. Same with PASID.

Quoted 6 lines

The only way we could remove them is if we are sure that no vIOMMU requires RID or PASID in the virtual fault queue PRI fault message.. I don't think that is true?

Cookie is not a replacement, cookie is an opaque value for the kernel to use to match a response to a request.

Oh I got this wrong, the above is the response, yeah we can ditch everything but the cookie, and code right?

Quoted 7 lines
struct iommu_hwpt_page_response {
        __u32 cookie;
        __u32 code;
};

What is the workflow here? We expect the VMM will take the vIOMMU response and match it to a request cookie for the kernel to complete?

The workflow in the host kernel involves looking up the iopf_group using the cookie value in the response queue and then responding to the iopf_group with the code. Therefore, from the host kernel's perspective, it is acceptable if the user only passes the cookie and code in the response message.

Since you both agreed that the response message could be simplified, I will implement the above structure in the new version.

Best regards,
baolu

author
#29 lore raw

Re: [PATCH v6 05/10] iommufd: Add fault and response message definitions

In reply to Jason Gunthorpe
Quoted 32 lines · Jason Gunthorpe
Quoted 17 lines · Kevin
Quoted 17 lines

From: Baolu Lu<[email protected]>
Sent: Thursday, June 6, 2024 2:28 PM

Quoted 12 lines · Kevin

include/uapi/linux/iommufd.h struct iommu_hwpt_invalidate {

Quoted 12 lines

From: Lu Baolu<[email protected]>
Sent: Monday, May 27, 2024 12:05 PM

include/uapi/linux/iommufd.h +9 -0
+
+/**
+ * struct iommu_hwpt_page_response - IOMMU page fault response
+ * @size: sizeof(struct iommu_hwpt_page_response)
+ * @flags: Must be set to 0
+ * @dev_id: device ID of target device for the response
+ * @pasid: Process Address Space ID
+ * @grpid: Page Request Group Index
+ * @code: One of response code in enum

iommufd_page_response_code.

Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

+ * @cookie: The kernel-managed cookie reported in the fault message.
+ */

include/uapi/linux/iommufd.h +4 -0
+struct iommu_hwpt_page_response {
+	__u32 size;
+	__u32 flags;
+	__u32 dev_id;
Quoted 8 lines
Quoted 8 lines

include/uapi/linux/iommufd.h struct iommu_hwpt_invalidate { · line 790

Quoted 6 lines
include/uapi/linux/iommufd.h +6 -0
+	__u32 pasid;
+	__u32 grpid;
+	__u32 code;
+	__u32 cookie;
+	__u32 reserved;
+};

with the response queue per fault object we don't need all fields here, e.g. dev_id, pasid, etc. Cookie is sufficient.

Quoted 2 lines

Wait, why did we make it per object? The fault FD is supposed to be sharable across HWPTs.

The fault FD is shareable across HWPTs. Kevin was suggesting that the response queue (for all outstanding IOPFs awaiting responses) could be put in the fault structure.

Best regards,
baolu

#30 lore raw

Re: [PATCH v6 05/10] iommufd: Add fault and response message definitions

In reply to Lu Baolu
Quoted 9 lines · Lu Baolu
include/uapi/linux/iommufd.h +9 -0
+/**
+ * struct iommu_hwpt_pgfault - iommu page fault data
+ * @size: sizeof(struct iommu_hwpt_pgfault)
+ * @flags: Combination of enum iommu_hwpt_pgfault_flags
+ * @dev_id: id of the originated device
+ * @pasid: Process Address Space ID
+ * @grpid: Page Request Group Index
+ * @perm: Combination of enum iommu_hwpt_pgfault_perm
+ * @addr: Fault address

include/uapi/linux/iommufd.h struct iommu_hwpt_invalidate { · line 742

Quoted 10 lines
include/uapi/linux/iommufd.h +10 -0
+ * @length: a hint of how much data the requestor is expecting to fetch. For
+ *          example, if the PRI initiator knows it is going to do a 10MB
+ *          transfer, it could fill in 10MB and the OS could pre-fault in
+ *          10MB of IOVA. It's default to 0 if there's no such hint.
+ * @cookie: kernel-managed cookie identifying a group of fault messages. The
+ *          cookie number encoded in the last page fault of the group should
+ *          be echoed back in the response message.
+ */
+struct iommu_hwpt_pgfault {
+	__u32 size;

Given we fail the system call if size is not exactly the right value we should probably drop it here.

The ioctl to get the FD can someday specify the format of the fault messages if we need to upgrade. If we want to change it down the road then the old FD will be exactly as it is now, and the user will request a new format FD that only works in whatever the new way is.

Jason

author
#31 lore raw

Re: [PATCH v6 05/10] iommufd: Add fault and response message definitions

Quoted 23 lines · Jason Gunthorpe
Quoted 9 lines · Lu Baolu
include/uapi/linux/iommufd.h +9 -0
+/**
+ * struct iommu_hwpt_pgfault - iommu page fault data
+ * @size: sizeof(struct iommu_hwpt_pgfault)
+ * @flags: Combination of enum iommu_hwpt_pgfault_flags
+ * @dev_id: id of the originated device
+ * @pasid: Process Address Space ID
+ * @grpid: Page Request Group Index
+ * @perm: Combination of enum iommu_hwpt_pgfault_perm
+ * @addr: Fault address

include/uapi/linux/iommufd.h struct iommu_hwpt_invalidate { · line 742

Quoted 10 lines
include/uapi/linux/iommufd.h +10 -0
+ * @length: a hint of how much data the requestor is expecting to fetch. For
+ *          example, if the PRI initiator knows it is going to do a 10MB
+ *          transfer, it could fill in 10MB and the OS could pre-fault in
+ *          10MB of IOVA. It's default to 0 if there's no such hint.
+ * @cookie: kernel-managed cookie identifying a group of fault messages. The
+ *          cookie number encoded in the last page fault of the group should
+ *          be echoed back in the response message.
+ */
+struct iommu_hwpt_pgfault {
+	__u32 size;

Given we fail the system call if size is not exactly the right value we should probably drop it here.

Quoted 4 lines

The ioctl to get the FD can someday specify the format of the fault messages if we need to upgrade. If we want to change it down the road then the old FD will be exactly as it is now, and the user will request a new format FD that only works in whatever the new way is.

Okay, sure!

Best regards,
baolu

author
#32 lore raw

PATCH v6 6/10 iommufd: Add iommufd fault object

In reply to Lu Baolu

An iommufd fault object provides an interface for delivering I/O page faults to user space. These objects are created and destroyed by user space, and they can be associated with or dissociated from hardware page table objects during page table allocation or destruction.

User space interacts with the fault object through a file interface. This interface offers a straightforward and efficient way for user space to handle page faults. It allows user space to read fault messages sequentially and respond to them by writing to the same file. The file interface supports reading messages in poll mode, so it's recommended that user space applications use io_uring to enhance read and write efficiency.

A fault object can be associated with any iopf-capable iommufd_hw_pgtable during the pgtable's allocation. All I/O page faults triggered by devices when accessing the I/O addresses of an iommufd_hw_pgtable are routed through the fault object to user space. Similarly, user space's responses to these page faults are routed back to the iommu device driver through the same fault object.

---
 include/linux/iommu.h                   |   4 +
 drivers/iommu/iommufd/iommufd_private.h |  30 ++++
 include/uapi/linux/iommufd.h            |  18 ++
 drivers/iommu/io-pgfault.c              |   2 +
 drivers/iommu/iommufd/fault.c           | 227 ++++++++++++++++++++++++
 drivers/iommu/iommufd/main.c            |   6 +
 drivers/iommu/iommufd/Makefile          |   1 +
 7 files changed, 288 insertions(+)
 create mode 100644 drivers/iommu/iommufd/fault.c
include/linux/iommu.h +4 -0
index 4067ebdd6232..16b3a2da91ef 100644
@@ -124,12 +124,16 @@ struct iopf_fault {
 struct iopf_group {
 	struct iopf_fault last_fault;
 	struct list_head faults;
+	size_t fault_count;
 	/* list node for iommu_fault_param::faults */
 	struct list_head pending_node;
 	struct work_struct work;
 	struct iommu_attach_handle *attach_handle;
 	/* The device's fault data parameter. */
 	struct iommu_fault_param *fault_param;
+	/* Used by handler provider to hook the group on its own lists. */
+	struct list_head node;
+	u32 cookie;
 };
 
 /**
drivers/iommu/iommufd/iommufd_private.h +30 -0
index 991f864d1f9b..c8a4519f1405 100644
@@ -128,6 +128,7 @@ enum iommufd_object_type {
 	IOMMUFD_OBJ_HWPT_NESTED,
 	IOMMUFD_OBJ_IOAS,
 	IOMMUFD_OBJ_ACCESS,
+	IOMMUFD_OBJ_FAULT,
 #ifdef CONFIG_IOMMUFD_TEST
 	IOMMUFD_OBJ_SELFTEST,
 #endif
@@ -426,6 +427,35 @@ void iopt_remove_access(struct io_pagetable *iopt,
 			u32 iopt_access_list_id);
 void iommufd_access_destroy_object(struct iommufd_object *obj);
 
+/*
+ * An iommufd_fault object represents an interface to deliver I/O page faults
+ * to the user space. These objects are created/destroyed by the user space and
+ * associated with hardware page table objects during page-table allocation.
+ */
+struct iommufd_fault {
+	struct iommufd_object obj;
+	struct iommufd_ctx *ictx;
+	struct file *filep;
+
+	/* The lists of outstanding faults protected by below mutex. */
+	struct mutex mutex;
+	struct list_head deliver;
+	struct xarray response;
+
+	struct wait_queue_head wait_queue;
+};
+
+struct iommufd_attach_handle {
+	struct iommu_attach_handle handle;
+	struct iommufd_device *idev;
+};
+
+/* Convert an iommu attach handle to iommufd handle. */
+#define to_iommufd_handle(hdl)	container_of(hdl, struct iommufd_attach_handle, handle)
+
+int iommufd_fault_alloc(struct iommufd_ucmd *ucmd);
+void iommufd_fault_destroy(struct iommufd_object *obj);
+
 #ifdef CONFIG_IOMMUFD_TEST
 int iommufd_test(struct iommufd_ucmd *ucmd);
 void iommufd_selftest_destroy(struct iommufd_object *obj);
include/uapi/linux/iommufd.h +18 -0
index 2f34d66436fb..eba452d4344e 100644
@@ -50,6 +50,7 @@ enum {
 	IOMMUFD_CMD_HWPT_SET_DIRTY_TRACKING,
 	IOMMUFD_CMD_HWPT_GET_DIRTY_BITMAP,
 	IOMMUFD_CMD_HWPT_INVALIDATE,
+	IOMMUFD_CMD_FAULT_QUEUE_ALLOC,
 };
 
 /**
@@ -788,4 +789,21 @@ struct iommu_hwpt_page_response {
 	__u32 cookie;
 	__u32 reserved;
 };
+
+/**
+ * struct iommu_fault_alloc - ioctl(IOMMU_FAULT_QUEUE_ALLOC)
+ * @size: sizeof(struct iommu_fault_alloc)
+ * @flags: Must be 0
+ * @out_fault_id: The ID of the new FAULT
+ * @out_fault_fd: The fd of the new FAULT
+ *
+ * Explicitly allocate a fault handling object.
+ */
+struct iommu_fault_alloc {
+	__u32 size;
+	__u32 flags;
+	__u32 out_fault_id;
+	__u32 out_fault_fd;
+};
+#define IOMMU_FAULT_QUEUE_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_FAULT_QUEUE_ALLOC)
 #endif
drivers/iommu/io-pgfault.c +2 -0
index c62fcb67ef02..a629d8a93614 100644
@@ -110,6 +110,8 @@ static struct iopf_group *iopf_group_alloc(struct iommu_fault_param *iopf_param,
 	list_add(&group->pending_node, &iopf_param->faults);
 	mutex_unlock(&iopf_param->lock);
 
+	group->fault_count = list_count_nodes(&group->faults);
+
 	return group;
 }
 
drivers/iommu/iommufd/fault.c new file +227 -0

227 lines not on this page · raw message

drivers/iommu/iommufd/main.c +6 -0
index 39b32932c61e..961b2949c06f 100644
@@ -332,6 +332,7 @@ union ucmd_buffer {
 	struct iommu_ioas_unmap unmap;
 	struct iommu_option option;
 	struct iommu_vfio_ioas vfio_ioas;
+	struct iommu_fault_alloc fault;
 #ifdef CONFIG_IOMMUFD_TEST
 	struct iommu_test_cmd test;
 #endif
@@ -381,6 +382,8 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = {
 		 val64),
 	IOCTL_OP(IOMMU_VFIO_IOAS, iommufd_vfio_ioas, struct iommu_vfio_ioas,
 		 __reserved),
+	IOCTL_OP(IOMMU_FAULT_QUEUE_ALLOC, iommufd_fault_alloc, struct iommu_fault_alloc,
+		 out_fault_fd),
 #ifdef CONFIG_IOMMUFD_TEST
 	IOCTL_OP(IOMMU_TEST_CMD, iommufd_test, struct iommu_test_cmd, last),
 #endif
@@ -513,6 +516,9 @@ static const struct iommufd_object_ops iommufd_object_ops[] = {
 		.destroy = iommufd_hwpt_nested_destroy,
 		.abort = iommufd_hwpt_nested_abort,
 	},
+	[IOMMUFD_OBJ_FAULT] = {
+		.destroy = iommufd_fault_destroy,
+	},
 #ifdef CONFIG_IOMMUFD_TEST
 	[IOMMUFD_OBJ_SELFTEST] = {
 		.destroy = iommufd_selftest_destroy,
drivers/iommu/iommufd/Makefile +1 -0
index 34b446146961..b94a74366eed 100644
@@ -6,6 +6,7 @@ iommufd-y := \
 	ioas.o \
 	main.o \
 	pages.o \
+	fault.o \
 	vfio_compat.o
 
 iommufd-$(CONFIG_IOMMUFD_TEST) += selftest.o
Signature · 1 line
2.34.1
maintainer
#33 lore raw

RE: [PATCH v6 06/10] iommufd: Add iommufd fault object

Quoted 7 lines

From: Lu Baolu <[email protected]>
Sent: Monday, May 27, 2024 12:05 PM

drivers/iommu/iommufd/fault.c new file +4 -0
+static ssize_t iommufd_fault_fops_read(struct file *filep, char __user *buf,
+				       size_t count, loff_t *ppos)
+{
+	size_t fault_size = sizeof(struct iommu_hwpt_pgfault);

drivers/iommu/iommufd/fault.c line 65

Quoted 10 lines
drivers/iommu/iommufd/fault.c new file +10 -0
+	struct iommufd_fault *fault = filep->private_data;
+	struct iommu_hwpt_pgfault data;
+	struct iommufd_device *idev;
+	struct iopf_group *group;
+	struct iopf_fault *iopf;
+	size_t done = 0;
+	int rc = 0;
+
+	if (*ppos || count % fault_size)
+		return -ESPIPE;

the man page says:

"If count is zero, read() returns zero and has no other results."

Quoted 11 lines
drivers/iommu/iommufd/fault.c new file +11 -0
+
+	mutex_lock(&fault->mutex);
+	while (!list_empty(&fault->deliver) && count > done) {
+		group = list_first_entry(&fault->deliver,
+					 struct iopf_group, node);
+
+		if (group->fault_count * fault_size > count - done)
+			break;
+
+		rc = xa_alloc(&fault->response, &group->cookie, group,
+			      xa_limit_32b, GFP_KERNEL);

drivers/iommu/iommufd/fault.c line 86

Quoted 10 lines
drivers/iommu/iommufd/fault.c new file +10 -0
+		if (rc)
+			break;
+
+		idev = to_iommufd_handle(group->attach_handle)->idev;
+		list_for_each_entry(iopf, &group->faults, list) {
+			iommufd_compose_fault_message(&iopf->fault,
+						      &data, idev,
+						      group->cookie);
+			rc = copy_to_user(buf + done, &data, fault_size);
+			if (rc) {

'rc' should be converted to -EFAULT.

drivers/iommu/iommufd/fault.c line 97

Quoted 11 lines
drivers/iommu/iommufd/fault.c new file +11 -0
+				xa_erase(&fault->response, group->cookie);
+				break;
+			}
+			done += fault_size;
+		}
+
+		list_del(&group->node);
+	}
+	mutex_unlock(&fault->mutex);
+
+	return done == 0 ? rc : done;

again this doesn't match the manual:

"On error, -1 is returned, and errno is set appropriately. "

it doesn't matter whether 'done' is 0.

drivers/iommu/iommufd/fault.c line 156

Quoted 9 lines
drivers/iommu/iommufd/fault.c new file +9 -0
+
+static int iommufd_fault_fops_release(struct inode *inode, struct file *filep)
+{
+	struct iommufd_fault *fault = filep->private_data;
+
+	iommufd_ctx_put(fault->ictx);
+	refcount_dec(&fault->obj.users);
+	return 0;
+}

hmm this doesn't sound correct. the context and refcount are acquired in iommufd_fault_alloc() but here they are reverted when the fd is closed...

drivers/iommu/iommufd/fault.c line 198

Quoted 12 lines
(quoted diff) +2 -0
+
+	filep = anon_inode_getfile("[iommufd-pgfault]",

&iommufd_fault_fops,

drivers/iommu/iommufd/fault.c new file +9 -0
+				   fault, O_RDWR);
+	if (IS_ERR(filep)) {
+		rc = PTR_ERR(filep);
+		goto out_abort;
+	}
+
+	refcount_inc(&fault->obj.users);
+	iommufd_ctx_get(fault->ictx);
+	fault->filep = filep;

those 3 lines can be moved after below fdno get. It's reads slightly clearer to put file related work together before getting to the last piece of intiailzation.

drivers/iommu/iommufd/fault.c union ucmd_buffer { · line 335

Quoted 12 lines
drivers/iommu/iommufd/fault.c new file +8 -0
+
+	fdno = get_unused_fd_flags(O_CLOEXEC);
+	if (fdno < 0) {
+		rc = fdno;
+		goto out_fput;
+	}
+
@@ -332,6 +332,7 @@ union ucmd_buffer {
 	struct iommu_ioas_unmap unmap;
 	struct iommu_option option;
 	struct iommu_vfio_ioas vfio_ioas;
+	struct iommu_fault_alloc fault;

alphabetic

Review static const struct iommufd_ioctl_op · line 389

Quoted 9 lines
(quoted diff) +2 -0
@@ -381,6 +382,8 @@ static const struct iommufd_ioctl_op
 iommufd_ioctl_ops[] = {
 		 val64),
 	IOCTL_OP(IOMMU_VFIO_IOAS, iommufd_vfio_ioas, struct
 iommu_vfio_ioas,
 		 __reserved),
+	IOCTL_OP(IOMMU_FAULT_QUEUE_ALLOC, iommufd_fault_alloc,
 struct iommu_fault_alloc,
+		 out_fault_fd),

ditto

author
#34 lore raw

Re: [PATCH v6 06/10] iommufd: Add iommufd fault object

Quoted 20 lines · Kevin
Quoted 7 lines

From: Lu Baolu <[email protected]>
Sent: Monday, May 27, 2024 12:05 PM

drivers/iommu/iommufd/fault.c new file +4 -0
+static ssize_t iommufd_fault_fops_read(struct file *filep, char __user *buf,
+				       size_t count, loff_t *ppos)
+{
+	size_t fault_size = sizeof(struct iommu_hwpt_pgfault);

drivers/iommu/iommufd/fault.c line 65

Quoted 10 lines
drivers/iommu/iommufd/fault.c new file +10 -0
+	struct iommufd_fault *fault = filep->private_data;
+	struct iommu_hwpt_pgfault data;
+	struct iommufd_device *idev;
+	struct iopf_group *group;
+	struct iopf_fault *iopf;
+	size_t done = 0;
+	int rc = 0;
+
+	if (*ppos || count % fault_size)
+		return -ESPIPE;

the man page says:

Quoted 1 line

"If count is zero, read() returns zero and has no other results."

My understanding is that reading zero bytes is likely to check if a file descriptor is valid and ready for reading without actually taking any data from it.

In this code, it just returns 0 and it's compatible with the man page. Or, I overlooked anything?

Quoted 16 lines
Quoted 16 lines
drivers/iommu/iommufd/fault.c new file +15 -0
+
+	mutex_lock(&fault->mutex);
+	while (!list_empty(&fault->deliver) && count > done) {
+		group = list_first_entry(&fault->deliver,
+					 struct iopf_group, node);
+
+		if (group->fault_count * fault_size > count - done)
+			break;
+
+		rc = xa_alloc(&fault->response, &group->cookie, group,
+			      xa_limit_32b, GFP_KERNEL);
+		if (rc)
+			break;
+
+		idev = to_iommufd_handle(group->attach_handle)->idev;
Quoted 8 lines

drivers/iommu/iommufd/fault.c line 86

Quoted 6 lines
drivers/iommu/iommufd/fault.c new file +6 -0
+		list_for_each_entry(iopf, &group->faults, list) {
+			iommufd_compose_fault_message(&iopf->fault,
+						      &data, idev,
+						      group->cookie);
+			rc = copy_to_user(buf + done, &data, fault_size);
+			if (rc) {

'rc' should be converted to -EFAULT.

Yes. I will make it like this:

       if (copy_to_user(buf + done, &data, fault_size)) {
               xa_erase(&fault->response, group->cookie);
               rc = -EFAULT;
               break;
       }
Quoted 15 lines

drivers/iommu/iommufd/fault.c line 97

Quoted 12 lines
drivers/iommu/iommufd/fault.c new file +11 -0
+				xa_erase(&fault->response, group->cookie);
+				break;
+			}
+			done += fault_size;
+		}
+
+		list_del(&group->node);
+	}
+	mutex_unlock(&fault->mutex);
+
+	return done == 0 ? rc : done;

again this doesn't match the manual:

Quoted 3 lines

"On error, -1 is returned, and errno is set appropriately."

it doesn't matter whether 'done' is 0.

I don't quite follow here. The code is doing the following:

- If done == 0, it means nothing has been copied to user space. This could be due to two reasons:

1) the user read with a count set to 0, or
2) a failure case.

The code returns 0 for the first case and an error number for the second.

- If done != 0, some data has been copied to user space. In this case, the code returns the number of data copied regardless of the value of rc.

Quoted 14 lines

drivers/iommu/iommufd/fault.c line 156

Quoted 10 lines
drivers/iommu/iommufd/fault.c new file +9 -0
+
+static int iommufd_fault_fops_release(struct inode *inode, struct file *filep)
+{
+	struct iommufd_fault *fault = filep->private_data;
+
+	iommufd_ctx_put(fault->ictx);
+	refcount_dec(&fault->obj.users);
+	return 0;
+}

hmm this doesn't sound correct. the context and refcount are acquired in iommufd_fault_alloc() but here they are reverted when the fd is closed...

These two refcounts were requested when the fault object was installed to the fault FD.

       filep = anon_inode_getfile("[iommufd-pgfault]", &iommufd_fault_fops,
                                   fault, O_RDWR);
        if (IS_ERR(filep)) {
                rc = PTR_ERR(filep);
                goto out_abort;
        }

        refcount_inc(&fault->obj.users);
        iommufd_ctx_get(fault->ictx);
        fault->filep = filep;

These refcounts must then be released when the FD is released.

Quoted 8 lines
Quoted 8 lines
(quoted diff) +2 -0
+
+	filep = anon_inode_getfile("[iommufd-pgfault]",

&iommufd_fault_fops,

drivers/iommu/iommufd/fault.c new file +5 -0
+				   fault, O_RDWR);
+	if (IS_ERR(filep)) {
+		rc = PTR_ERR(filep);
+		goto out_abort;
+	}
Quoted 8 lines

drivers/iommu/iommufd/fault.c line 198

Quoted 4 lines
drivers/iommu/iommufd/fault.c new file +4 -0
+
+	refcount_inc(&fault->obj.users);
+	iommufd_ctx_get(fault->ictx);
+	fault->filep = filep;

those 3 lines can be moved after below fdno get. It's reads slightly clearer to put file related work together before getting to the last piece of intiailzation.

The filep is allocated and initialized together.

Quoted 15 lines

drivers/iommu/iommufd/fault.c union ucmd_buffer { · line 335

Quoted 12 lines
drivers/iommu/iommufd/fault.c new file +8 -0
+
+	fdno = get_unused_fd_flags(O_CLOEXEC);
+	if (fdno < 0) {
+		rc = fdno;
+		goto out_fput;
+	}
+
@@ -332,6 +332,7 @@ union ucmd_buffer {
  	struct iommu_ioas_unmap unmap;
  	struct iommu_option option;
  	struct iommu_vfio_ioas vfio_ioas;
+	struct iommu_fault_alloc fault;

alphabetic

Quoted 11 lines

Review static const struct iommufd_ioctl_op · line 389

Quoted 9 lines
(quoted diff) +2 -0
@@ -381,6 +382,8 @@ static const struct iommufd_ioctl_op
 iommufd_ioctl_ops[] = {
  		 val64),
  	IOCTL_OP(IOMMU_VFIO_IOAS, iommufd_vfio_ioas, struct
 iommu_vfio_ioas,
  		 __reserved),
+	IOCTL_OP(IOMMU_FAULT_QUEUE_ALLOC, iommufd_fault_alloc,
 struct iommu_fault_alloc,
+		 out_fault_fd),

ditto

Yes, sure. I wasn't aware of the order.

Best regards,
baolu

#35 lore raw

Re: [PATCH v6 06/10] iommufd: Add iommufd fault object

Quoted 7 lines · Baolu Lu
Quoted 7 lines

drivers/iommu/iommufd/fault.c line 155

Quoted 7 lines
drivers/iommu/iommufd/fault.c new file +7 -0
+static int iommufd_fault_fops_release(struct inode *inode, struct file *filep)
+{
+	struct iommufd_fault *fault = filep->private_data;
+
+	iommufd_ctx_put(fault->ictx);
+	refcount_dec(&fault->obj.users);
+	return 0;

This is in the wrong order, dec users before ctx_put.

Quoted 18 lines
Quoted 3 lines

hmm this doesn't sound correct. the context and refcount are acquired in iommufd_fault_alloc() but here they are reverted when the fd is closed...

These two refcounts were requested when the fault object was installed to the fault FD.

       filep = anon_inode_getfile("[iommufd-pgfault]", &iommufd_fault_fops,
                                   fault, O_RDWR);
        if (IS_ERR(filep)) {
                rc = PTR_ERR(filep);
                goto out_abort;
        }

        refcount_inc(&fault->obj.users);
        iommufd_ctx_get(fault->ictx);
        fault->filep = filep;
Quoted 1 line

These refcounts must then be released when the FD is released.

Yes

The ctx refcount is to avoid destroying the ctx FD, which can't work, while the fault FD has an object pinned. This is also why the above order is backwards.

Jason

author
#36 lore raw

Re: [PATCH v6 06/10] iommufd: Add iommufd fault object

Quoted 11 lines · Jason Gunthorpe
Quoted 7 lines · Baolu Lu
Quoted 7 lines

drivers/iommu/iommufd/fault.c line 155

Quoted 7 lines
drivers/iommu/iommufd/fault.c new file +7 -0
+static int iommufd_fault_fops_release(struct inode *inode, struct file *filep)
+{
+	struct iommufd_fault *fault = filep->private_data;
+
+	iommufd_ctx_put(fault->ictx);
+	refcount_dec(&fault->obj.users);
+	return 0;

This is in the wrong order, dec users before ctx_put.

Done.

Best regards,
baolu

#37 lore raw

Re: [PATCH v6 06/10] iommufd: Add iommufd fault object

In reply to Tian, Kevin
Quoted 20 lines · Kevin
Quoted 7 lines

From: Lu Baolu <[email protected]>
Sent: Monday, May 27, 2024 12:05 PM

drivers/iommu/iommufd/fault.c new file +4 -0
+static ssize_t iommufd_fault_fops_read(struct file *filep, char __user *buf,
+				       size_t count, loff_t *ppos)
+{
+	size_t fault_size = sizeof(struct iommu_hwpt_pgfault);

drivers/iommu/iommufd/fault.c line 65

Quoted 10 lines
drivers/iommu/iommufd/fault.c new file +10 -0
+	struct iommufd_fault *fault = filep->private_data;
+	struct iommu_hwpt_pgfault data;
+	struct iommufd_device *idev;
+	struct iopf_group *group;
+	struct iopf_fault *iopf;
+	size_t done = 0;
+	int rc = 0;
+
+	if (*ppos || count % fault_size)
+		return -ESPIPE;

the man page says:

Quoted 1 line

"If count is zero, read() returns zero and has no other results."

The above does that? 0 % X == 0

Quoted 15 lines
Quoted 15 lines
drivers/iommu/iommufd/fault.c new file +15 -0
+
+	mutex_lock(&fault->mutex);
+	while (!list_empty(&fault->deliver) && count > done) {
+		group = list_first_entry(&fault->deliver,
+					 struct iopf_group, node);
+
+		if (group->fault_count * fault_size > count - done)
+			break;
+
+		rc = xa_alloc(&fault->response, &group->cookie, group,
+			      xa_limit_32b, GFP_KERNEL);
+		if (rc)
+			break;
+
+		idev = to_iommufd_handle(group->attach_handle)->idev;
Quoted 8 lines

drivers/iommu/iommufd/fault.c line 86

Quoted 6 lines
drivers/iommu/iommufd/fault.c new file +6 -0
+		list_for_each_entry(iopf, &group->faults, list) {
+			iommufd_compose_fault_message(&iopf->fault,
+						      &data, idev,
+						      group->cookie);
+			rc = copy_to_user(buf + done, &data, fault_size);
+			if (rc) {

'rc' should be converted to -EFAULT.

Yes

Quoted 14 lines

drivers/iommu/iommufd/fault.c line 97

Quoted 11 lines
drivers/iommu/iommufd/fault.c new file +11 -0
+				xa_erase(&fault->response, group->cookie);
+				break;
+			}
+			done += fault_size;
+		}
+
+		list_del(&group->node);
+	}
+	mutex_unlock(&fault->mutex);
+
+	return done == 0 ? rc : done;

again this doesn't match the manual:

Quoted 3 lines

"On error, -1 is returned, and errno is set appropriately. "

it doesn't matter whether 'done' is 0.

It is setup so that once the list_del() below happens it is guarenteed that the system call will return a positive result so that the list_del'd items are always returned to userspace.

If we hit any fault here on the Nth item we should still return the prior items and ignore the fault.

If we hit a fault on the first item then we should return the fault.

Jason

maintainer
#38 lore raw

RE: [PATCH v6 06/10] iommufd: Add iommufd fault object

Quoted 25 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, June 12, 2024 9:24 PM

Quoted 4 lines · Kevin
Quoted 4 lines

From: Lu Baolu <[email protected]>
Sent: Monday, May 27, 2024 12:05 PM

+static ssize_t iommufd_fault_fops_read(struct file *filep, char __user

*buf,

Quoted 16 lines

drivers/iommu/iommufd/fault.c line 65

Quoted 13 lines
drivers/iommu/iommufd/fault.c new file +13 -0
+				       size_t count, loff_t *ppos)
+{
+	size_t fault_size = sizeof(struct iommu_hwpt_pgfault);
+	struct iommufd_fault *fault = filep->private_data;
+	struct iommu_hwpt_pgfault data;
+	struct iommufd_device *idev;
+	struct iopf_group *group;
+	struct iopf_fault *iopf;
+	size_t done = 0;
+	int rc = 0;
+
+	if (*ppos || count % fault_size)
+		return -ESPIPE;

the man page says:

Quoted 3 lines
Quoted 1 line

"If count is zero, read() returns zero and has no other results."

The above does that? 0 % X == 0

but if *ppos is non-zero then an error will be returned even if count==0.

but seems I looked at an old man page on an old system. A newer one says:

If count is zero, read() may detect the errors described below. In the absence of any errors, or if read() does not check for errors, a read() with a count of 0 returns zero and has no other effects.

so above should be fine. 😊

Quoted 18 lines
Quoted 14 lines

drivers/iommu/iommufd/fault.c line 97

Quoted 11 lines
drivers/iommu/iommufd/fault.c new file +11 -0
+				xa_erase(&fault->response, group->cookie);
+				break;
+			}
+			done += fault_size;
+		}
+
+		list_del(&group->node);
+	}
+	mutex_unlock(&fault->mutex);
+
+	return done == 0 ? rc : done;

again this doesn't match the manual:

Quoted 3 lines

"On error, -1 is returned, and errno is set appropriately. "

it doesn't matter whether 'done' is 0.

Quoted 9 lines

It is setup so that once the list_del() below happens it is guarenteed that the system call will return a positive result so that the list_del'd items are always returned to userspace.

If we hit any fault here on the Nth item we should still return the prior items and ignore the fault.

If we hit a fault on the first item then we should return the fault.

yes that does make sense. I just didn't find such statement in the man page which simply said to return -1 on error. The number of bytes is returned only on success.

RETURN VALUE
On success, the number of bytes read is returned (zero indicates end of file), and the file position is advanced by this number. It is not an error if this number is smaller than the number of bytes requested; this may happen for example because fewer bytes are actually available right now (maybe because we were close to end-of-file, or because we are reading from a pipe, or from a terminal), or because read() was interrupted by a signal. See also NOTES.

On error, -1 is returned, and errno is set to indicate the error. In this case, it is left unspecified whether the file position (if any) changes.

author
#39 lore raw

PATCH v6 7/10 iommufd: Fault-capable hwpt attach/detach/replace

In reply to Lu Baolu

Add iopf-capable hw page table attach/detach/replace helpers. The pointer to iommufd_device is stored in the domain attachment handle, so that it can be echo'ed back in the iopf_group.

The iopf-capable hw page tables can only be attached to devices that support the IOMMU_DEV_FEAT_IOPF feature. On the first attachment of an iopf-capable hw_pagetable to the device, the IOPF feature is enabled on the device. Similarly, after the last iopf-capable hwpt is detached from the device, the IOPF feature is disabled on the device.

The current implementation allows a replacement between iopf-capable and non-iopf-capable hw page tables. This matches the nested translation use case, where a parent domain is attached by default and can then be replaced with a nested user domain with iopf support.

---
 drivers/iommu/iommufd/iommufd_private.h |  12 ++
 drivers/iommu/iommufd/device.c          |  16 +-
 drivers/iommu/iommufd/fault.c           | 191 ++++++++++++++++++++++++
 3 files changed, 216 insertions(+), 3 deletions(-)
drivers/iommu/iommufd/iommufd_private.h +12 -0
index c8a4519f1405..ba89c86e1af7 100644
@@ -293,6 +293,7 @@ int iommufd_check_iova_range(struct io_pagetable *iopt,
 struct iommufd_hw_pagetable {
 	struct iommufd_object obj;
 	struct iommu_domain *domain;
+	struct iommufd_fault *fault;
 };
 
 struct iommufd_hwpt_paging {
@@ -396,6 +397,9 @@ struct iommufd_device {
 	/* always the physical device */
 	struct device *dev;
 	bool enforce_cache_coherency;
+	/* protect iopf_enabled counter */
+	struct mutex iopf_lock;
+	unsigned int iopf_enabled;
 };
 
 static inline struct iommufd_device *
@@ -456,6 +460,14 @@ struct iommufd_attach_handle {
 int iommufd_fault_alloc(struct iommufd_ucmd *ucmd);
 void iommufd_fault_destroy(struct iommufd_object *obj);
 
+int iommufd_fault_domain_attach_dev(struct iommufd_hw_pagetable *hwpt,
+				    struct iommufd_device *idev);
+void iommufd_fault_domain_detach_dev(struct iommufd_hw_pagetable *hwpt,
+				     struct iommufd_device *idev);
+int iommufd_fault_domain_replace_dev(struct iommufd_device *idev,
+				     struct iommufd_hw_pagetable *hwpt,
+				     struct iommufd_hw_pagetable *old);
+
 #ifdef CONFIG_IOMMUFD_TEST
 int iommufd_test(struct iommufd_ucmd *ucmd);
 void iommufd_selftest_destroy(struct iommufd_object *obj);
drivers/iommu/iommufd/device.c +13 -3
index 873630c111c1..63681d79b72d 100644
@@ -215,6 +215,7 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx,
 	refcount_inc(&idev->obj.users);
 	/* igroup refcount moves into iommufd_device */
 	idev->igroup = igroup;
+	mutex_init(&idev->iopf_lock);
 
 	/*
 	 * If the caller fails after this success it must call
@@ -376,7 +377,10 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt,
 	 * attachment.
 	 */
 	if (list_empty(&idev->igroup->device_list)) {
-		rc = iommu_attach_group(hwpt->domain, idev->igroup->group);
+		if (hwpt->fault)
+			rc = iommufd_fault_domain_attach_dev(hwpt, idev);
+		else
+			rc = iommu_attach_group(hwpt->domain, idev->igroup->group);
 		if (rc)
 			goto err_unresv;
 		idev->igroup->hwpt = hwpt;
@@ -402,7 +406,10 @@ iommufd_hw_pagetable_detach(struct iommufd_device *idev)
 	mutex_lock(&idev->igroup->lock);
 	list_del(&idev->group_item);
 	if (list_empty(&idev->igroup->device_list)) {
-		iommu_detach_group(hwpt->domain, idev->igroup->group);
+		if (hwpt->fault)
+			iommufd_fault_domain_detach_dev(hwpt, idev);
+		else
+			iommu_detach_group(hwpt->domain, idev->igroup->group);
 		idev->igroup->hwpt = NULL;
 	}
 	if (hwpt_is_paging(hwpt))
@@ -497,7 +504,10 @@ iommufd_device_do_replace(struct iommufd_device *idev,
 			goto err_unlock;
 	}
 
-	rc = iommu_group_replace_domain(igroup->group, hwpt->domain);
+	if (old_hwpt->fault || hwpt->fault)
+		rc = iommufd_fault_domain_replace_dev(idev, hwpt, old_hwpt);
+	else
+		rc = iommu_group_replace_domain(igroup->group, hwpt->domain);
 	if (rc)
 		goto err_unresv;
 
drivers/iommu/iommufd/fault.c +191 -0

203 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#40 lore raw

RE: [PATCH v6 07/10] iommufd: Fault-capable hwpt attach/detach/replace

Quoted 6 lines

From: Lu Baolu <[email protected]>
Sent: Monday, May 27, 2024 12:05 PM

Add iopf-capable hw page table attach/detach/replace helpers. The pointer to iommufd_device is stored in the domain attachment handle, so that it can be echo'ed back in the iopf_group.

this message needs an update. now the device pointer is not in the attach handle.

and there worths a explanation about VF in the commit msg.

drivers/iommu/iommufd/device.c int iommufd_hw_pagetable_attach(struct · line 385

Quoted 11 lines
drivers/iommu/iommufd/device.c +4 -1
@@ -376,7 +377,10 @@ int iommufd_hw_pagetable_attach(struct
 iommufd_hw_pagetable *hwpt,
 	 * attachment.
 	 */
 	if (list_empty(&idev->igroup->device_list)) {
-		rc = iommu_attach_group(hwpt->domain, idev->igroup->group);
+		if (hwpt->fault)
+			rc = iommufd_fault_domain_attach_dev(hwpt, idev);
+		else
+			rc = iommu_attach_group(hwpt->domain, idev-
 >igroup->group);

Does it read better to have a iommufd_attach_device() wrapper with above branches handled internally?

drivers/iommu/iommufd/fault.c line 26

Quoted 9 lines
drivers/iommu/iommufd/fault.c +8 -0
+static int iommufd_fault_iopf_enable(struct iommufd_device *idev)
+{
+	struct device *dev = idev->dev;
+	int ret;
+
+	/*
+	 * Once we turn on PCI/PRI support for VF, the response failure code
+	 * could not be forwarded to the hardware due to PRI being a shared

you could but just doing so is incorrect. 😊

s/could/should/

drivers/iommu/iommufd/fault.c line 29

Quoted 3 lines
drivers/iommu/iommufd/fault.c +3 -0
+	 * resource between PF and VFs. There is no coordination for this
+	 * shared capability. This waits for a vPRI reset to recover.
+	 */

this may go a bit further to talk about supporting it requires an emulation in iommufd (i.e. pause any further fault delivery until vPRI reset). It is a future work so disable it for VF at this point.

drivers/iommu/iommufd/fault.c line 147

Quoted 11 lines

+void iommufd_fault_domain_detach_dev(struct iommufd_hw_pagetable *hwpt,

drivers/iommu/iommufd/fault.c +9 -0
+				     struct iommufd_device *idev)
+{
+	struct iommufd_attach_handle *handle;
+
+	handle = iommufd_device_get_attach_handle(idev);
+	iommu_detach_group_handle(hwpt->domain, idev->igroup->group);
+	iommufd_auto_response_faults(hwpt, handle);
+	iommufd_fault_iopf_disable(idev);
+	kfree(handle);

this assumes that the detach callback of the iommu driver needs to drain in-fly page requests so no further reference to handle or queue new req to the deliver queue when it returns, otherwise there is a use-after-free race or stale requests in the fault queue which auto response doesn't cleanly handle.

iirc previous discussion reveals that only intel-iommu driver guarantees that behavior. In any case this should be documented to avoid this being used in a non-conforming iommu driver.

If I misunderstood, some comment why no race in this window is also appreciated. 😊

Quoted 22 lines
drivers/iommu/iommufd/fault.c +21 -0
+}
+
+static int __fault_domain_replace_dev(struct iommufd_device *idev,
+				      struct iommufd_hw_pagetable *hwpt,
+				      struct iommufd_hw_pagetable *old)
+{
+	struct iommufd_attach_handle *handle, *curr = NULL;
+	int ret;
+
+	if (old->fault)
+		curr = iommufd_device_get_attach_handle(idev);
+
+	if (hwpt->fault) {
+		handle = kzalloc(sizeof(*handle), GFP_KERNEL);
+		if (!handle)
+			return -ENOMEM;
+
+		handle->handle.domain = hwpt->domain;
+		handle->idev = idev;
+		ret = iommu_replace_group_handle(idev->igroup->group,
+						 hwpt->domain, &handle-
Quoted 1 line

handle);

drivers/iommu/iommufd/fault.c line 177

Quoted 9 lines
drivers/iommu/iommufd/fault.c +9 -0
+	} else {
+		ret = iommu_replace_group_handle(idev->igroup->group,
+						 hwpt->domain, NULL);
+	}
+
+	if (!ret && curr) {
+		iommufd_auto_response_faults(old, curr);
+		kfree(curr);
+	}

In last version you said auto response is required only when switching from fault-capable old to fault-incapable new. But above code doesn't reflect that description?

author
#41 lore raw

Re: [PATCH v6 07/10] iommufd: Fault-capable hwpt attach/detach/replace

Quoted 9 lines · Kevin
Quoted 6 lines

From: Lu Baolu <[email protected]>
Sent: Monday, May 27, 2024 12:05 PM

Add iopf-capable hw page table attach/detach/replace helpers. The pointer to iommufd_device is stored in the domain attachment handle, so that it can be echo'ed back in the iopf_group.

this message needs an update. now the device pointer is not in the attach handle.

The iommufd_device pointer is in the attach handle provided by iommufd in attach or replace path.

Quoted 12 lines

and there worths a explanation about VF in the commit msg.

Quoted 10 lines
drivers/iommu/iommufd/device.c +4 -1
@@ -376,7 +377,10 @@ int iommufd_hw_pagetable_attach(struct
 iommufd_hw_pagetable *hwpt,
  	 * attachment.
  	 */
  	if (list_empty(&idev->igroup->device_list)) {
-		rc = iommu_attach_group(hwpt->domain, idev->igroup->group);
+		if (hwpt->fault)
+			rc = iommufd_fault_domain_attach_dev(hwpt, idev);
+		else
+			rc = iommu_attach_group(hwpt->domain, idev-
Quoted 4 lines

Review int iommufd_hw_pagetable_attach(struct · line 385

Quoted 1 line
(quoted diff) +0 -0
 > igroup->group);

Does it read better to have a iommufd_attach_device() wrapper with above branches handled internally?

Yes. Will do this in the next version.

Quoted 13 lines

drivers/iommu/iommufd/fault.c line 26

Quoted 10 lines
drivers/iommu/iommufd/fault.c +8 -0
+static int iommufd_fault_iopf_enable(struct iommufd_device *idev)
+{
+	struct device *dev = idev->dev;
+	int ret;
+
+	/*
+	 * Once we turn on PCI/PRI support for VF, the response failure code
+	 * could not be forwarded to the hardware due to PRI being a shared

you could but just doing so is incorrect. 😊

Quoted 1 line

s/could/should/

Done.

Quoted 8 lines

drivers/iommu/iommufd/fault.c line 29

Quoted 4 lines
drivers/iommu/iommufd/fault.c +3 -0
+	 * resource between PF and VFs. There is no coordination for this
+	 * shared capability. This waits for a vPRI reset to recover.
+	 */

this may go a bit further to talk about supporting it requires an emulation in iommufd (i.e. pause any further fault delivery until vPRI reset). It is a future work so disable it for VF at this point.

Yes.

Quoted 19 lines

drivers/iommu/iommufd/fault.c line 147

Quoted 12 lines

+void iommufd_fault_domain_detach_dev(struct iommufd_hw_pagetable *hwpt,

drivers/iommu/iommufd/fault.c +9 -0
+				     struct iommufd_device *idev)
+{
+	struct iommufd_attach_handle *handle;
+
+	handle = iommufd_device_get_attach_handle(idev);
+	iommu_detach_group_handle(hwpt->domain, idev->igroup->group);
+	iommufd_auto_response_faults(hwpt, handle);
+	iommufd_fault_iopf_disable(idev);
+	kfree(handle);

this assumes that the detach callback of the iommu driver needs to drain in-fly page requests so no further reference to handle or queue new req to the deliver queue when it returns, otherwise there is a use-after-free race or stale requests in the fault queue which auto response doesn't cleanly handle.

Quoted 6 lines

iirc previous discussion reveals that only intel-iommu driver guarantees that behavior. In any case this should be documented to avoid this being used in a non-conforming iommu driver.

If I misunderstood, some comment why no race in this window is also appreciated. 😊

Yes. The iommu driver needs to guarantee that there will be no iopf request for a RID or PASID after the domain has been detached. This implies that:

- IOMMU hardware should not put further iopf in its hardware queue if the domain has been detached.
- Before the domain detachment is complete, the iommu driver should flush all iopf targeting the detached domain in the hardware page request queue.

Quoted 23 lines
Quoted 23 lines
drivers/iommu/iommufd/fault.c +21 -0
+}
+
+static int __fault_domain_replace_dev(struct iommufd_device *idev,
+				      struct iommufd_hw_pagetable *hwpt,
+				      struct iommufd_hw_pagetable *old)
+{
+	struct iommufd_attach_handle *handle, *curr = NULL;
+	int ret;
+
+	if (old->fault)
+		curr = iommufd_device_get_attach_handle(idev);
+
+	if (hwpt->fault) {
+		handle = kzalloc(sizeof(*handle), GFP_KERNEL);
+		if (!handle)
+			return -ENOMEM;
+
+		handle->handle.domain = hwpt->domain;
+		handle->idev = idev;
+		ret = iommu_replace_group_handle(idev->igroup->group,
+						 hwpt->domain, &handle-
Quoted 1 line

handle);

Quoted 13 lines

drivers/iommu/iommufd/fault.c line 177

Quoted 9 lines
drivers/iommu/iommufd/fault.c +9 -0
+	} else {
+		ret = iommu_replace_group_handle(idev->igroup->group,
+						 hwpt->domain, NULL);
+	}
+
+	if (!ret && curr) {
+		iommufd_auto_response_faults(old, curr);
+		kfree(curr);
+	}

In last version you said auto response is required only when switching from fault-capable old to fault-incapable new. But above code doesn't reflect that description?

What the current code does is auto-respond to all page faults targeting the old fault-capable hwpt. I'm also okay if we decide to limit this to flushing page faults only if the new hwpt is *not* fault-capable.

Best regards,
baolu

maintainer
#42 lore raw

RE: [PATCH v6 07/10] iommufd: Fault-capable hwpt attach/detach/replace

Quoted 9 lines

From: Baolu Lu <[email protected]>
Sent: Sunday, June 9, 2024 3:23 PM

Quoted 4 lines · Kevin
Quoted 4 lines

From: Lu Baolu <[email protected]>
Sent: Monday, May 27, 2024 12:05 PM

Add iopf-capable hw page table attach/detach/replace helpers. The

pointer

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

to iommufd_device is stored in the domain attachment handle, so that it can be echo'ed back in the iopf_group.

this message needs an update. now the device pointer is not in the attach handle.

The iommufd_device pointer is in the attach handle provided by iommufd in attach or replace path.

I thought it talked about iommu_attach_handle then it includes only the domain pointer.

but it's correct if iommufd_attach_handle is being talked here.

depends on what 'domain attachment handle' refers to. 😊

author
#43 lore raw

PATCH v6 8/10 iommufd: Associate fault object with iommufd_hw_pgtable

In reply to Lu Baolu

When allocating a user iommufd_hw_pagetable, the user space is allowed to associate a fault object with the hw_pagetable by specifying the fault object ID in the page table allocation data and setting the IOMMU_HWPT_FAULT_ID_VALID flag bit.

On a successful return of hwpt allocation, the user can retrieve and respond to page faults by reading and writing the file interface of the fault object.

Once a fault object has been associated with a hwpt, the hwpt is iopf-capable, indicated by hwpt->fault is non NULL. Attaching, detaching, or replacing an iopf-capable hwpt to an RID or PASID will differ from those that are not iopf-capable.

---
 drivers/iommu/iommufd/iommufd_private.h |  9 ++++++
 include/uapi/linux/iommufd.h            |  8 +++++
 drivers/iommu/iommufd/fault.c           | 17 ++++++++++
 drivers/iommu/iommufd/hw_pagetable.c    | 41 +++++++++++++++++++------
 4 files changed, 66 insertions(+), 9 deletions(-)
drivers/iommu/iommufd/iommufd_private.h +9 -0
index ba89c86e1af7..db50881e76f6 100644
@@ -457,8 +457,17 @@ struct iommufd_attach_handle {
 /* Convert an iommu attach handle to iommufd handle. */
 #define to_iommufd_handle(hdl)	container_of(hdl, struct iommufd_attach_handle, handle)
 
+static inline struct iommufd_fault *
+iommufd_get_fault(struct iommufd_ucmd *ucmd, u32 id)
+{
+	return container_of(iommufd_get_object(ucmd->ictx, id,
+					       IOMMUFD_OBJ_FAULT),
+			    struct iommufd_fault, obj);
+}
+
 int iommufd_fault_alloc(struct iommufd_ucmd *ucmd);
 void iommufd_fault_destroy(struct iommufd_object *obj);
+int iommufd_fault_iopf_handler(struct iopf_group *group);
 
 int iommufd_fault_domain_attach_dev(struct iommufd_hw_pagetable *hwpt,
 				    struct iommufd_device *idev);
include/uapi/linux/iommufd.h +8 -0
index eba452d4344e..5391db3a7180 100644
@@ -357,10 +357,13 @@ struct iommu_vfio_ioas {
  *                                the parent HWPT in a nesting configuration.
  * @IOMMU_HWPT_ALLOC_DIRTY_TRACKING: Dirty tracking support for device IOMMU is
  *                                   enforced on device attachment
+ * @IOMMU_HWPT_FAULT_ID_VALID: The fault_id field of hwpt allocation data is
+ *                             valid.
  */
 enum iommufd_hwpt_alloc_flags {
 	IOMMU_HWPT_ALLOC_NEST_PARENT = 1 << 0,
 	IOMMU_HWPT_ALLOC_DIRTY_TRACKING = 1 << 1,
+	IOMMU_HWPT_FAULT_ID_VALID = 1 << 2,
 };
 
 /**
@@ -412,6 +415,9 @@ enum iommu_hwpt_data_type {
  * @data_type: One of enum iommu_hwpt_data_type
  * @data_len: Length of the type specific data
  * @data_uptr: User pointer to the type specific data
+ * @fault_id: The ID of IOMMUFD_FAULT object. Valid only if flags field of
+ *            IOMMU_HWPT_FAULT_ID_VALID is set.
+ * @__reserved2: Padding to 64-bit alignment. Must be 0.
  *
  * Explicitly allocate a hardware page table object. This is the same object
  * type that is returned by iommufd_device_attach() and represents the
@@ -442,6 +448,8 @@ struct iommu_hwpt_alloc {
 	__u32 data_type;
 	__u32 data_len;
 	__aligned_u64 data_uptr;
+	__u32 fault_id;
+	__u32 __reserved2;
 };
 #define IOMMU_HWPT_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_ALLOC)
 
drivers/iommu/iommufd/fault.c +17 -0
index 94dde1f57cfc..dd07e3b1b4c1 100644
@@ -416,3 +416,20 @@ int iommufd_fault_alloc(struct iommufd_ucmd *ucmd)
 
 	return rc;
 }
+
+int iommufd_fault_iopf_handler(struct iopf_group *group)
+{
+	struct iommufd_hw_pagetable *hwpt;
+	struct iommufd_fault *fault;
+
+	hwpt = group->attach_handle->domain->fault_data;
+	fault = hwpt->fault;
+
+	mutex_lock(&fault->mutex);
+	list_add_tail(&group->node, &fault->deliver);
+	mutex_unlock(&fault->mutex);
+
+	wake_up_interruptible(&fault->wait_queue);
+
+	return 0;
+}
drivers/iommu/iommufd/hw_pagetable.c +32 -9

83 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer Reviewed-by on v6 8/10
#44 lore raw

RE: [PATCH v6 08/10] iommufd: Associate fault object with iommufd_hw_pgtable

Quoted 12 lines

From: Lu Baolu <[email protected]>
Sent: Monday, May 27, 2024 12:05 PM

When allocating a user iommufd_hw_pagetable, the user space is allowed to associate a fault object with the hw_pagetable by specifying the fault object ID in the page table allocation data and setting the IOMMU_HWPT_FAULT_ID_VALID flag bit.

On a successful return of hwpt allocation, the user can retrieve and respond to page faults by reading and writing the file interface of the fault object.

Quoted 6 lines

Once a fault object has been associated with a hwpt, the hwpt is iopf-capable, indicated by hwpt->fault is non NULL. Attaching, detaching, or replacing an iopf-capable hwpt to an RID or PASID will differ from those that are not iopf-capable.

author
#45 lore raw

PATCH v6 9/10 iommufd/selftest: Add IOPF support for mock device

In reply to Lu Baolu

Extend the selftest mock device to support generating and responding to an IOPF. Also add an ioctl interface to userspace applications to trigger the IOPF on the mock device. This would allow userspace applications to test the IOMMUFD's handling of IOPFs without having to rely on any real hardware.

---
 drivers/iommu/iommufd/iommufd_test.h |  8 ++++
 drivers/iommu/iommufd/selftest.c     | 64 ++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+)
drivers/iommu/iommufd/iommufd_test.h +8 -0
index e854d3f67205..acbbba1c6671 100644
@@ -22,6 +22,7 @@ enum {
 	IOMMU_TEST_OP_MOCK_DOMAIN_FLAGS,
 	IOMMU_TEST_OP_DIRTY,
 	IOMMU_TEST_OP_MD_CHECK_IOTLB,
+	IOMMU_TEST_OP_TRIGGER_IOPF,
 };
 
 enum {
@@ -127,6 +128,13 @@ struct iommu_test_cmd {
 			__u32 id;
 			__u32 iotlb;
 		} check_iotlb;
+		struct {
+			__u32 dev_id;
+			__u32 pasid;
+			__u32 grpid;
+			__u32 perm;
+			__u64 addr;
+		} trigger_iopf;
 	};
 	__u32 last;
 };
drivers/iommu/iommufd/selftest.c +64 -0

112 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#46 lore raw

PATCH v6 10/10 iommufd/selftest: Add coverage for IOPF test

In reply to Lu Baolu

Extend the selftest tool to add coverage of testing IOPF handling. This would include the following tests:

- Allocating and destroying an iommufd fault object.
- Allocating and destroying an IOPF-capable HWPT.
- Attaching/detaching/replacing an IOPF-capable HWPT on a device.
- Triggering an IOPF on the mock device.
- Retrieving and responding to the IOPF through the file interface.

---
 tools/testing/selftests/iommu/iommufd_utils.h | 84 +++++++++++++++++--
 tools/testing/selftests/iommu/iommufd.c       | 18 ++++
 .../selftests/iommu/iommufd_fail_nth.c        |  2 +-
 3 files changed, 98 insertions(+), 6 deletions(-)
tools/testing/selftests/iommu/iommufd_utils.h +79 -5
index 8d2b46b2114d..cb913ec89f96 100644
@@ -153,7 +153,7 @@ static int _test_cmd_mock_domain_replace(int fd, __u32 stdev_id, __u32 pt_id,
 	EXPECT_ERRNO(_errno, _test_cmd_mock_domain_replace(self->fd, stdev_id, \
 							   pt_id, NULL))
 
-static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id,
+static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id, __u32 ft_id,
 				__u32 flags, __u32 *hwpt_id, __u32 data_type,
 				void *data, size_t data_len)
 {
@@ -165,6 +165,7 @@ static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id,
 		.data_type = data_type,
 		.data_len = data_len,
 		.data_uptr = (uint64_t)data,
+		.fault_id = ft_id,
 	};
 	int ret;
 
@@ -177,24 +178,30 @@ static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id,
 }
 
 #define test_cmd_hwpt_alloc(device_id, pt_id, flags, hwpt_id)                  \
-	ASSERT_EQ(0, _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, flags,   \
+	ASSERT_EQ(0, _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, 0, flags,   \
 					  hwpt_id, IOMMU_HWPT_DATA_NONE, NULL, \
 					  0))
 #define test_err_hwpt_alloc(_errno, device_id, pt_id, flags, hwpt_id)   \
 	EXPECT_ERRNO(_errno, _test_cmd_hwpt_alloc(                      \
-				     self->fd, device_id, pt_id, flags, \
+				     self->fd, device_id, pt_id, 0, flags, \
 				     hwpt_id, IOMMU_HWPT_DATA_NONE, NULL, 0))
 
 #define test_cmd_hwpt_alloc_nested(device_id, pt_id, flags, hwpt_id,         \
 				   data_type, data, data_len)                \
-	ASSERT_EQ(0, _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, flags, \
+	ASSERT_EQ(0, _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, 0, flags, \
 					  hwpt_id, data_type, data, data_len))
 #define test_err_hwpt_alloc_nested(_errno, device_id, pt_id, flags, hwpt_id, \
 				   data_type, data, data_len)                \
 	EXPECT_ERRNO(_errno,                                                 \
-		     _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, flags, \
+		     _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, 0, flags, \
 					  hwpt_id, data_type, data, data_len))
 
+#define test_cmd_hwpt_alloc_iopf(device_id, pt_id, fault_id, flags, hwpt_id,    \
+				   data_type, data, data_len)                   \
+	ASSERT_EQ(0, _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, fault_id, \
+					  flags, hwpt_id, data_type, data,      \
+					  data_len))
+
 #define test_cmd_hwpt_check_iotlb(hwpt_id, iotlb_id, expected)                 \
 	({                                                                     \
 		struct iommu_test_cmd test_cmd = {                             \
@@ -684,3 +691,70 @@ static int _test_cmd_get_hw_info(int fd, __u32 device_id, void *data,
 
 #define test_cmd_get_hw_capabilities(device_id, caps, mask) \
 	ASSERT_EQ(0, _test_cmd_get_hw_info(self->fd, device_id, NULL, 0, &caps))
+
+static int _test_ioctl_fault_alloc(int fd, __u32 *fault_id, __u32 *fault_fd)
+{
+	struct iommu_fault_alloc cmd = {
+		.size = sizeof(cmd),
+	};
+	int ret;
+
+	ret = ioctl(fd, IOMMU_FAULT_QUEUE_ALLOC, &cmd);
+	if (ret)
+		return ret;
+	*fault_id = cmd.out_fault_id;
+	*fault_fd = cmd.out_fault_fd;
+	return 0;
+}
+
+#define test_ioctl_fault_alloc(fault_id, fault_fd)                       \
+	({                                                               \
+		ASSERT_EQ(0, _test_ioctl_fault_alloc(self->fd, fault_id, \
+						     fault_fd));         \
+		ASSERT_NE(0, *(fault_id));                               \
+		ASSERT_NE(0, *(fault_fd));                               \
+	})
+
+static int _test_cmd_trigger_iopf(int fd, __u32 device_id, __u32 fault_fd)
+{
+	struct iommu_test_cmd trigger_iopf_cmd = {
+		.size = sizeof(trigger_iopf_cmd),
+		.op = IOMMU_TEST_OP_TRIGGER_IOPF,
+		.trigger_iopf = {
+			.dev_id = device_id,
+			.pasid = 0x1,
+			.grpid = 0x2,
+			.perm = IOMMU_PGFAULT_PERM_READ | IOMMU_PGFAULT_PERM_WRITE,
+			.addr = 0xdeadbeaf,
+		},
+	};
+	struct iommu_hwpt_page_response response = {
+		.size = sizeof(struct iommu_hwpt_page_response),
+		.dev_id = device_id,
+		.pasid = 0x1,
+		.grpid = 0x2,
+		.code = 0,
+	};
+	struct iommu_hwpt_pgfault fault = {};
+	ssize_t bytes;
+	int ret;
+
+	ret = ioctl(fd, _IOMMU_TEST_CMD(IOMMU_TEST_OP_TRIGGER_IOPF), &trigger_iopf_cmd);
+	if (ret)
+		return ret;
+
+	bytes = read(fault_fd, &fault, sizeof(fault));
+	if (bytes <= 0)
+		return -EIO;
+
+	response.cookie = fault.cookie;
+
+	bytes = write(fault_fd, &response, sizeof(response));
+	if (bytes <= 0)
+		return -EIO;
+
+	return 0;
+}
+
+#define test_cmd_trigger_iopf(device_id, fault_fd) \
+	ASSERT_EQ(0, _test_cmd_trigger_iopf(self->fd, device_id, fault_fd))
tools/testing/selftests/iommu/iommufd.c +18 -0
index edf1c99c9936..5b0169875a4d 100644
@@ -279,6 +279,9 @@ TEST_F(iommufd_ioas, alloc_hwpt_nested)
 	uint32_t parent_hwpt_id = 0;
 	uint32_t parent_hwpt_id_not_work = 0;
 	uint32_t test_hwpt_id = 0;
+	uint32_t iopf_hwpt_id;
+	uint32_t fault_id;
+	uint32_t fault_fd;
 
 	if (self->device_id) {
 		/* Negative tests */
@@ -326,6 +329,7 @@ TEST_F(iommufd_ioas, alloc_hwpt_nested)
 					   sizeof(data));
 
 		/* Allocate two nested hwpts sharing one common parent hwpt */
+		test_ioctl_fault_alloc(&fault_id, &fault_fd);
 		test_cmd_hwpt_alloc_nested(self->device_id, parent_hwpt_id, 0,
 					   &nested_hwpt_id[0],
 					   IOMMU_HWPT_DATA_SELFTEST, &data,
@@ -334,6 +338,10 @@ TEST_F(iommufd_ioas, alloc_hwpt_nested)
 					   &nested_hwpt_id[1],
 					   IOMMU_HWPT_DATA_SELFTEST, &data,
 					   sizeof(data));
+		test_cmd_hwpt_alloc_iopf(self->device_id, parent_hwpt_id, fault_id,
+					 IOMMU_HWPT_FAULT_ID_VALID, &iopf_hwpt_id,
+					 IOMMU_HWPT_DATA_SELFTEST, &data,
+					 sizeof(data));
 		test_cmd_hwpt_check_iotlb_all(nested_hwpt_id[0],
 					      IOMMU_TEST_IOTLB_DEFAULT);
 		test_cmd_hwpt_check_iotlb_all(nested_hwpt_id[1],
@@ -504,14 +512,24 @@ TEST_F(iommufd_ioas, alloc_hwpt_nested)
 			     _test_ioctl_destroy(self->fd, nested_hwpt_id[1]));
 		test_ioctl_destroy(nested_hwpt_id[0]);
 
+		/* Switch from nested_hwpt_id[1] to iopf_hwpt_id */
+		test_cmd_mock_domain_replace(self->stdev_id, iopf_hwpt_id);
+		EXPECT_ERRNO(EBUSY,
+			     _test_ioctl_destroy(self->fd, iopf_hwpt_id));
+		/* Trigger an IOPF on the device */
+		test_cmd_trigger_iopf(self->device_id, fault_fd);
+
 		/* Detach from nested_hwpt_id[1] and destroy it */
 		test_cmd_mock_domain_replace(self->stdev_id, parent_hwpt_id);
 		test_ioctl_destroy(nested_hwpt_id[1]);
+		test_ioctl_destroy(iopf_hwpt_id);
 
 		/* Detach from the parent hw_pagetable and destroy it */
 		test_cmd_mock_domain_replace(self->stdev_id, self->ioas_id);
 		test_ioctl_destroy(parent_hwpt_id);
 		test_ioctl_destroy(parent_hwpt_id_not_work);
+		close(fault_fd);
+		test_ioctl_destroy(fault_id);
 	} else {
 		test_err_hwpt_alloc(ENOENT, self->device_id, self->ioas_id, 0,
 				    &parent_hwpt_id);
tools/testing/selftests/iommu/iommufd_fail_nth.c +1 -1
index f590417cd67a..c5d5e69452b0 100644
@@ -615,7 +615,7 @@ TEST_FAIL_NTH(basic_fail_nth, device)
 	if (_test_cmd_get_hw_info(self->fd, idev_id, &info, sizeof(info), NULL))
 		return -1;
 
-	if (_test_cmd_hwpt_alloc(self->fd, idev_id, ioas_id, 0, &hwpt_id,
+	if (_test_cmd_hwpt_alloc(self->fd, idev_id, ioas_id, 0, 0, &hwpt_id,
 				 IOMMU_HWPT_DATA_NONE, 0, 0))
 		return -1;
 
Signature · 1 line
2.34.1
maintainer
#47 lore raw
In reply to Lu Baolu
Quoted 7 lines · Lu Baolu

This series implements the functionality of delivering IO page faults to user space through the IOMMUFD framework. One feasible use case is the nested translation. Nested translation is a hardware feature that supports two-stage translation tables for IOMMU. The second-stage translation table is managed by the host VMM, while the first-stage translation table is owned by user space. This allows user space to control the IOMMU mappings for its devices.

This looks pretty close, will you post a v7 with the minor changes?

Thanks,
Jasno

author
#48 lore raw
Quoted 9 lines · Jason Gunthorpe
Quoted 7 lines · Lu Baolu

This series implements the functionality of delivering IO page faults to user space through the IOMMUFD framework. One feasible use case is the nested translation. Nested translation is a hardware feature that supports two-stage translation tables for IOMMU. The second-stage translation table is managed by the host VMM, while the first-stage translation table is owned by user space. This allows user space to control the IOMMU mappings for its devices.

This looks pretty close, will you post a v7 with the minor changes?

Yes. Sure.

Best regards,
baolu