Skip to contentSkip to conversation

iommufd support pasid attach/replace

PATCH Superseded Yi Liu 6 participants 71 messages Dec 19, 2024 – Feb 20, 2025

Subsystems (4)

v6 of 11 Superseded Latest is v11 →

lore
Share & tools
lore

Yi Liu

author
#0 lore raw

v6 cover iommufd support pasid attach/replace

PASID (Process Address Space ID) is a PCIe extension to tag the DMA transactions out of a physical device, and most modern IOMMU hardware have supported PASID granular address translation. So a PASID-capable device can be attached to multiple hwpts (a.k.a. domains), and each attachment is tagged with a pasid.

This series is based on the preparation series [1] [2], it first adds a missing iommu API to replace the domain for a pasid. Based on the iommu pasid attach/ replace/detach APIs, this series adds iommufd APIs for device drivers to attach/replace/detach pasid to/from hwpt per userspace's request, add PASID compat domain enforcement, add PASID compat hwpt allocation in iommufd, and adds selftest to validate the iommufd APIs.

The completed code can be found in the below link [3]. Heads up! The existing iommufd selftest was broken, there was a temp fix patch in the top of the branch [3]. If want to run the iommufd selftest, please apply that fix. Sorry for the inconvenience.

[1] https://lore.kernel.org/linux-iommu/[email protected]/ # done
[2] https://lore.kernel.org/linux-iommu/[email protected]/ # done
[3] https://github.com/yiliu1765/iommufd/tree/iommufd_pasid

Change log:

v6:
- Add kdoc to iommufd_device_get_attach_handle() to note the returned handle should be used with care. (Baolu)
- Reworked the patch 07 and 08 of v5 to avoid domain allocation failure on VT-d after applying patch 07 of v5.
1) Split out the intel iommu driver IOMMU_HWPT_ALLOC_PASID support out of patch 08
2) Rework the PASID-compatible domain enforcement by checking the RID domain and idev->pasid_hwpts under the idev->igroup->lock.
- iommufd_device_pasid_do_attach() returns -EINVAL if there is old hwpt and it's not the same with new hwpt. This aligns with how the iommufd_device_do_attach() deals it. Otherwise, attaching the same pasid to the same ioas is going to fail before the auto_domain loop goes to the correct hwpt. Thsi is not reasonable. So make this change.
- Enhanced the pasid selftest to have non-pasid-capable device and pasid-capable device.
- The order of the series is tweaked to be prepare the iommufd for pasid attach, add pasid attach, add PASID-compat domain enforcement and then add the PASID-compat hwpt allocation.
- Rebased on top of 6.13-rc3 and some already applied patches.

v5: https://lore.kernel.org/linux-iommu/[email protected]/
- Fix a mistake in patch 02 of v4 (Kevin)
- Move the iommufd_handle helpers to device.c
- Add IOMMU_HWPT_ALLOC_PASID check to enforce pasid-compatible domain for pasid capable device in iommufd
- Update the iommufd selftest to use IOMMU_HWPT_ALLOC_PASID

v4: https://lore.kernel.org/linux-iommu/[email protected]/
- Replace remove_dev_pasid() by supporting set_dev_pasid() for blocking domain (Kevin)
- This is done by the preparation series "Support attaching PASID to the blocked_domain"
- Misc tweaks to foil the merging of the iommufd iopf series. Three new patches are added:
- iommufd: Always pass iommu_attach_handle to iommu core
- iommufd: Move the iommufd_handle helpers to iommufd_private.h
- iommufd: Refactor __fault_domain_replace_dev() to be a wrapper of iommu_replace_group_handle()
- Renmae patch 03 of v3 to be "iommufd: Support pasid attach/replace"
- Add test case for attaching/replacing iopf-capable hwpt to pasid

v3: https://lore.kernel.org/kvm/[email protected]/
- Split the set_dev_pasid op enhancements for domain replacement to be a separate series "Make set_dev_pasid op supportting domain replacement" [1]. The below changes are made in the separate series.
*) set_dev_pasid() callback should keep the old config if failed to attach to
a domain. This simplifies the caller a lot as caller does not need to attach it back to old domain explicitly. This also avoids some corner cases in which the core may do duplicated domain attachment as described in below link (Jason)
https://lore.kernel.org/linux-iommu/BN9PR11MB52768C98314A95AFCD2FA6478C0F2@BN9PR11MB5276.namprd11.prod.outlook.com/
*) Drop patch 10 of v2 as it's a bug fix and can be submitted separately (Kevin) *) Rebase on top of Baolu's domain_alloc_paging refactor series (Jason)
- Drop the attach_data which includes attach_fn and pasid, insteadly passing the pasid through the device attach path. (Jason)
- Add a pasid-num-bits property to mock dev to make pasid selftest work (Kevin)

v2: https://lore.kernel.org/linux-iommu/[email protected]/
- Domain replace for pasid should be handled in set_dev_pasid() callbacks instead of remove_dev_pasid and call set_dev_pasid afteward in iommu layer (Jason)
- Make xarray operations more self-contained in iommufd pasid attach/replace/detach (Jason)
- Tweak the dev_iommu_get_max_pasids() to allow iommu driver to populate the max_pasids. This makes the iommufd selftest simpler to meet the max_pasids check in iommu_attach_device_pasid() (Jason)

v1: https://lore.kernel.org/kvm/[email protected]/#r
- Implemnet iommu_replace_device_pasid() to fall back to the original domain if this replacement failed (Kevin)
- Add check in do_attach() to check corressponding attach_fn per the pasid value.

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

Regards,
Yi Liu

Yi Liu (14):
iommu: Introduce a replace API for device pasid
iommufd: Refactor __fault_domain_replace_dev() to be a wrapper of
iommu_replace_group_handle()
iommufd: Move the iommufd_handle helpers to device.c
iommufd: Always pass iommu_attach_handle to iommu core
iommufd: Pass pasid through the device attach/replace path
iommufd: Mark PASID-compatible domain
iommufd: Support pasid attach/replace
iommufd: Enforce PASID-compatible domain for RID iommu/vt-d: Add IOMMU_HWPT_ALLOC_PASID support
iommufd: Allow allocating PASID-compatible domain iommufd/selftest: Add set_dev_pasid in mock iommu iommufd/selftest: Add a helper to get test device iommufd/selftest: Add test ops to test pasid attach/detach iommufd/selftest: Add coverage for iommufd pasid attach/detach

 drivers/iommu/intel/iommu.c                   |   3 +-
 drivers/iommu/intel/nested.c                  |   2 +-
 drivers/iommu/iommu-priv.h                    |   4 +
 drivers/iommu/iommu.c                         |  90 ++++-
 drivers/iommu/iommufd/Makefile                |   1 +
 drivers/iommu/iommufd/device.c                | 123 +++++--
 drivers/iommu/iommufd/fault.c                 |  88 +----
 drivers/iommu/iommufd/hw_pagetable.c          |  15 +-
 drivers/iommu/iommufd/iommufd_private.h       |  95 ++++-
 drivers/iommu/iommufd/iommufd_test.h          |  31 ++
 drivers/iommu/iommufd/pasid.c                 | 173 +++++++++
 drivers/iommu/iommufd/selftest.c              | 231 +++++++++++-
 include/linux/iommufd.h                       |   7 +
 tools/testing/selftests/iommu/iommufd.c       | 348 ++++++++++++++++++
 .../selftests/iommu/iommufd_fail_nth.c        |  39 +-
 tools/testing/selftests/iommu/iommufd_utils.h | 102 +++++
 16 files changed, 1216 insertions(+), 136 deletions(-)
 create mode 100644 drivers/iommu/iommufd/pasid.c
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v6 1/14 iommu: Introduce a replace API for device pasid

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

---
 drivers/iommu/iommu-priv.h |  4 ++
 drivers/iommu/iommu.c      | 90 ++++++++++++++++++++++++++++++++++++--
 2 files changed, 90 insertions(+), 4 deletions(-)
drivers/iommu/iommu-priv.h +4 -0
index de5b54eaa8bf..90b367de267e 100644
@@ -27,6 +27,10 @@ static inline const struct iommu_ops *iommu_fwspec_ops(struct iommu_fwspec *fwsp
 int iommu_group_replace_domain(struct iommu_group *group,
 			       struct iommu_domain *new_domain);
 
+int iommu_replace_device_pasid(struct iommu_domain *domain,
+			       struct device *dev, ioasid_t pasid,
+			       struct iommu_attach_handle *handle);
+
 int iommu_device_register_bus(struct iommu_device *iommu,
 			      const struct iommu_ops *ops,
 			      const struct bus_type *bus,
drivers/iommu/iommu.c +86 -4

120 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#2 lore raw

Re: [PATCH v6 01/14] iommu: Introduce a replace API for device pasid

Quoted 35 lines · Yi Liu
drivers/iommu/iommu.c +35 -0
+/**
+ * iommu_replace_device_pasid - Replace the domain that a pasid is attached to
+ * @domain: the new iommu domain
+ * @dev: the attached device.
+ * @pasid: the pasid of the device.
+ * @handle: the attach handle.
+ *
+ * This API allows the pasid to switch domains. Return 0 on success, or an
+ * error. The pasid will keep the old configuration if replacement failed.
+ * This is supposed to be used by iommufd, and iommufd can guarantee that
+ * both iommu_attach_device_pasid() and iommu_replace_device_pasid() would
+ * pass in a valid @handle.
+ */
+int iommu_replace_device_pasid(struct iommu_domain *domain,
+			       struct device *dev, ioasid_t pasid,
+			       struct iommu_attach_handle *handle)
+{
+	/* Caller must be a probed driver on dev */
+	struct iommu_group *group = dev->iommu_group;
+	struct iommu_attach_handle *curr;
+	int ret;
+
+	if (!group)
+		return -ENODEV;
+
+	if (!domain->ops->set_dev_pasid)
+		return -EOPNOTSUPP;
+
+	if (dev_iommu_ops(dev) != domain->owner ||
+	    pasid == IOMMU_NO_PASID || !handle)
+		return -EINVAL;
+
+	handle->domain = domain;
+
+	mutex_lock(&group->mutex);

drivers/iommu/iommu.c int iommu_attach_device_pasid(struct iommu_domain *domain, · line 3474

Quoted 10 lines
drivers/iommu/iommu.c +10 -0
+	/*
+	 * The iommu_attach_handle of the pasid becomes inconsistent with the
+	 * actual handle per the below operation. The concurrent PRI path will
+	 * deliver the PRQs per the new handle, this does not have a functional
+	 * impact. The PRI path would eventually become consistent when the
+	 * replacement is done.
+	 */
+	curr = (struct iommu_attach_handle *)xa_store(&group->pasid_array,
+						      pasid, handle,
+						      GFP_KERNEL);

The pointer type cast seems unnecessary.

drivers/iommu/iommu.c int iommu_attach_device_pasid(struct iommu_domain *domain, · line 3483

Quoted 9 lines
drivers/iommu/iommu.c +9 -0
+	if (!curr) {
+		xa_erase(&group->pasid_array, pasid);
+		ret = -EINVAL;
+		goto out_unlock;
+	}
+
+	ret = xa_err(curr);
+	if (ret)
+		goto out_unlock;

xa_store() returns either the old pointer or an error code. It's better to handle error code first, as shown below:

	/* Failed to store the new pointer: */
	if (xa_is_err(curr)) {
		ret = xa_err(curr);
		goto out_unlock;
	}

	/* Not a replace case: */
	if (!curr) {
		xa_erase(&group->pasid_array, pasid);
		ret = -EINVAL;
		goto out_unlock;
	}

Your code is already functional, just suggest making it more readable.

Thanks,
baolu

maintainer
#3 lore raw

RE: [PATCH v6 01/14] iommu: Introduce a replace API for device pasid

In reply to Yi Liu
Quoted 19 lines

From: Liu, Yi L <[email protected]>
Sent: Thursday, December 19, 2024 9:28 PM

+/**
+ * iommu_replace_device_pasid - Replace the domain that a pasid is attached to
+ * @domain: the new iommu domain
+ * @dev: the attached device.
+ * @pasid: the pasid of the device.
+ * @handle: the attach handle.
+ *
+ * This API allows the pasid to switch domains. Return 0 on success, or an
+ * error. The pasid will keep the old configuration if replacement failed.
+ * This is supposed to be used by iommufd, and iommufd can guarantee that
+ * both iommu_attach_device_pasid() and iommu_replace_device_pasid() would
+ * pass in a valid @handle.
+ */

Better explain why a valid handle is required here.

Also iommu_attach_device_pasid() allows NULL handle:

	if (handle)
		handle->domain = domain;
maintainer
#4 lore raw

RE: [PATCH v6 01/14] iommu: Introduce a replace API for device pasid

In reply to Yi Liu
Quoted 23 lines

From: Tian, Kevin
Sent: Thursday, January 9, 2025 3:08 PM

Quoted 19 lines

From: Liu, Yi L <[email protected]>
Sent: Thursday, December 19, 2024 9:28 PM

+/**
+ * iommu_replace_device_pasid - Replace the domain that a pasid is attached to
+ * @domain: the new iommu domain
+ * @dev: the attached device.
+ * @pasid: the pasid of the device.
+ * @handle: the attach handle.
+ *
+ * This API allows the pasid to switch domains. Return 0 on success, or an
+ * error. The pasid will keep the old configuration if replacement failed.
+ * This is supposed to be used by iommufd, and iommufd can guarantee that
+ * both iommu_attach_device_pasid() and iommu_replace_device_pasid() would
+ * pass in a valid @handle.
+ */

Quoted 1 line

Better explain why a valid handle is required here.

Okay, it's because __iommu_set_group_pasid() requires the old domain now and the only way to retrieve it at this point is via a handle. It's probably also ok to directly store a domain pointer to the xarray when the handle is missing but that sounds more confusing.

Quoted 5 lines

Also iommu_attach_device_pasid() allows NULL handle:

	if (handle)
		handle->domain = domain;

With that it sounds more consistent to make attach requiring a valid handle too.

maintainer
#5 lore raw

Re: [PATCH v6 01/14] iommu: Introduce a replace API for device pasid

Quoted 23 lines · Kevin
Quoted 23 lines

From: Tian, Kevin
Sent: Thursday, January 9, 2025 3:08 PM

Quoted 19 lines

From: Liu, Yi L <[email protected]>
Sent: Thursday, December 19, 2024 9:28 PM

+/**
+ * iommu_replace_device_pasid - Replace the domain that a pasid is attached to
+ * @domain: the new iommu domain
+ * @dev: the attached device.
+ * @pasid: the pasid of the device.
+ * @handle: the attach handle.
+ *
+ * This API allows the pasid to switch domains. Return 0 on success, or an
+ * error. The pasid will keep the old configuration if replacement failed.
+ * This is supposed to be used by iommufd, and iommufd can guarantee that
+ * both iommu_attach_device_pasid() and iommu_replace_device_pasid() would
+ * pass in a valid @handle.
+ */

Quoted 6 lines
Quoted 1 line

Better explain why a valid handle is required here.

Okay, it's because __iommu_set_group_pasid() requires the old domain now and the only way to retrieve it at this point is via a handle. It's probably also ok to directly store a domain pointer to the xarray when the handle is missing but that sounds more confusing.

I had shared a xarray approach to do that at one point, it apparently was scary enough nobody picked it up :)

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

Jason

maintainer
#6 lore raw

Re: [PATCH v6 01/14] iommu: Introduce a replace API for device pasid

Quoted 24 lines · Jason Gunthorpe
Quoted 23 lines · Kevin
Quoted 23 lines

From: Tian, Kevin
Sent: Thursday, January 9, 2025 3:08 PM

Quoted 19 lines

From: Liu, Yi L<[email protected]>
Sent: Thursday, December 19, 2024 9:28 PM

+/**
+ * iommu_replace_device_pasid - Replace the domain that a pasid is attached to
+ * @domain: the new iommu domain
+ * @dev: the attached device.
+ * @pasid: the pasid of the device.
+ * @handle: the attach handle.
+ *
+ * This API allows the pasid to switch domains. Return 0 on success, or an
+ * error. The pasid will keep the old configuration if replacement failed.
+ * This is supposed to be used by iommufd, and iommufd can guarantee that
+ * both iommu_attach_device_pasid() and iommu_replace_device_pasid() would
+ * pass in a valid @handle.
+ */

Better explain why a valid handle is required here.

Quoted 8 lines
Quoted 4 lines

Okay, it's because __iommu_set_group_pasid() requires the old domain now and the only way to retrieve it at this point is via a handle. It's probably also ok to directly store a domain pointer to the xarray when the handle is missing but that sounds more confusing.

I had shared a xarray approach to do that at one point, it apparently was scary enough nobody picked it up 🙂

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

I evaluated it at that time and ultimately decided not using it. At that time, there was no requirement for pasid replacement, so the 'old domain' was not yet an issue. This is why I chose to allocate on demand at that stage. :-)

---
baolu

maintainer
#7 lore raw

RE: [PATCH v6 01/14] iommu: Introduce a replace API for device pasid

Quoted 41 lines

From: Baolu Lu <[email protected]>
Sent: Friday, January 10, 2025 10:32 AM

Quoted 7 lines · Jason Gunthorpe
Quoted 6 lines · Kevin
Quoted 6 lines

From: Tian, Kevin
Sent: Thursday, January 9, 2025 3:08 PM

Quoted 3 lines

From: Liu, Yi L<[email protected]>
Sent: Thursday, December 19, 2024 9:28 PM

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

+/**
+ * iommu_replace_device_pasid - Replace the domain that a pasid is attached to
+ * @domain: the new iommu domain
+ * @dev: the attached device.
+ * @pasid: the pasid of the device.
+ * @handle: the attach handle.
+ *
+ * This API allows the pasid to switch domains. Return 0 on success, or

an

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

+ * error. The pasid will keep the old configuration if replacement failed.
+ * This is supposed to be used by iommufd, and iommufd can

guarantee

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

that
+ * both iommu_attach_device_pasid() and

iommu_replace_device_pasid()

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
Quoted 3 lines

would
+ * pass in a valid @handle.
+ */

Better explain why a valid handle is required here.

Okay, it's because __iommu_set_group_pasid() requires the old domain

now

Quoted 1 line
Quoted 1 line

and the only way to retrieve it at this point is via a handle. It's probably

also

Quoted 1 line
Quoted 1 line

ok to directly store a domain pointer to the xarray when the handle is

missing

Quoted 4 lines
Quoted 1 line

but that sounds more confusing.

I had shared a xarray approach to do that at one point, it apparently was scary enough nobody picked it up 🙂

Quoted 7 lines
Quoted 1 line

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

I evaluated it at that time and ultimately decided not using it. At that time, there was no requirement for pasid replacement, so the 'old domain' was not yet an issue. This is why I chose to allocate on demand at that stage. :-)

Not that scary when reading it again. 😊

Somehow less restrictions on the kAPI is worthy of a bit more work Inside the helpers. I won't be the last one to think about the difference why the pasid variants impose such restriction while it's not applied to RID.

Yi, could you take a look and think about any oversight of adopting it?

author
#8 lore raw

Re: [PATCH v6 01/14] iommu: Introduce a replace API for device pasid

Quoted 49 lines · Kevin
Quoted 41 lines

From: Baolu Lu <[email protected]>
Sent: Friday, January 10, 2025 10:32 AM

Quoted 7 lines · Jason Gunthorpe
Quoted 6 lines · Kevin
Quoted 6 lines

From: Tian, Kevin
Sent: Thursday, January 9, 2025 3:08 PM

Quoted 3 lines

From: Liu, Yi L<[email protected]>
Sent: Thursday, December 19, 2024 9:28 PM

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

+/**
+ * iommu_replace_device_pasid - Replace the domain that a pasid is attached to
+ * @domain: the new iommu domain
+ * @dev: the attached device.
+ * @pasid: the pasid of the device.
+ * @handle: the attach handle.
+ *
+ * This API allows the pasid to switch domains. Return 0 on success, or

an

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

+ * error. The pasid will keep the old configuration if replacement failed.
+ * This is supposed to be used by iommufd, and iommufd can

guarantee

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

that
+ * both iommu_attach_device_pasid() and

iommu_replace_device_pasid()

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
Quoted 3 lines

would
+ * pass in a valid @handle.
+ */

Better explain why a valid handle is required here.

Okay, it's because __iommu_set_group_pasid() requires the old domain

now

Quoted 1 line
Quoted 1 line

and the only way to retrieve it at this point is via a handle. It's probably

also

Quoted 1 line
Quoted 1 line

ok to directly store a domain pointer to the xarray when the handle is

missing

Quoted 4 lines
Quoted 1 line

but that sounds more confusing.

I had shared a xarray approach to do that at one point, it apparently was scary enough nobody picked it up 🙂

Quoted 7 lines
Quoted 1 line

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

I evaluated it at that time and ultimately decided not using it. At that time, there was no requirement for pasid replacement, so the 'old domain' was not yet an issue. This is why I chose to allocate on demand at that stage. :-)

Quoted 8 lines

Not that scary when reading it again. 😊

Somehow less restrictions on the kAPI is worthy of a bit more work Inside the helpers. I won't be the last one to think about the difference why the pasid variants impose such restriction while it's not applied to RID.

Yi, could you take a look and think about any oversight of adopting it?

It sounds like the encoding scheme Jason once mentioned when I first asked this. Then we would have the iommu_attach_device_pasid() and iommu_attach_device_pasid_handle() pair just like the RID path.

I think it should be ok. iommufd pasid code will use the _handle() version for the RID and PASID path. While other drivers in kernel will only use the normal version. And I don't think we would add iommu_replace_device_pasid() since no known drivers need it.

Regards,
Yi Liu

maintainer
#9 lore raw

Re: [PATCH v6 01/14] iommu: Introduce a replace API for device pasid

In reply to Yi Liu

drivers/iommu/iommu.c int iommu_attach_device_pasid(struct iommu_domain *domain, · line 3474

Quoted 11 lines · Yi Liu
drivers/iommu/iommu.c +11 -0
+	mutex_lock(&group->mutex);
+	/*
+	 * The iommu_attach_handle of the pasid becomes inconsistent with the
+	 * actual handle per the below operation. The concurrent PRI path will
+	 * deliver the PRQs per the new handle, this does not have a functional
+	 * impact. The PRI path would eventually become consistent when the
+	 * replacement is done.
+	 */
+	curr = (struct iommu_attach_handle *)xa_store(&group->pasid_array,
+						      pasid, handle,
+						      GFP_KERNEL);

The cast is not necessary..

drivers/iommu/iommu.c int iommu_attach_device_pasid(struct iommu_domain *domain, · line 3491

Quoted 5 lines
drivers/iommu/iommu.c +5 -0
+
+	ret = __iommu_set_group_pasid(domain, group, pasid, curr->domain);
+	if (ret)
+		WARN_ON(handle != xa_store(&group->pasid_array, pasid,
+					   curr, GFP_KERNEL));

I wonder about the ordering here, is it OK to have PRIs being delivered to a domain that failed to attach? What cleans up that race condition with domain free?

Should we replace the domain then set the xarray? (and same ordering question for normal attach)

Jason

maintainer
#10 lore raw

RE: [PATCH v6 01/14] iommu: Introduce a replace API for device pasid

Quoted 33 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, January 14, 2025 4:22 AM

drivers/iommu/iommu.c int iommu_attach_device_pasid(struct iommu_domain *domain,

Quoted 3 lines · Yi Liu
drivers/iommu/iommu.c +3 -0
+	mutex_lock(&group->mutex);
+	/*
+	 * The iommu_attach_handle of the pasid becomes inconsistent with

the

Review

Quoted 1 line
(quoted diff) +1 -0
+	 * actual handle per the below operation. The concurrent PRI path

will

Review

Quoted 1 line
(quoted diff) +1 -0
+	 * deliver the PRQs per the new handle, this does not have a

functional

Review

Quoted 1 line
(quoted diff) +1 -0
+	 * impact. The PRI path would eventually become consistent when

the

drivers/iommu/iommu.c int iommu_attach_device_pasid(struct iommu_domain *domain, · line 3474

Quoted 5 lines
drivers/iommu/iommu.c +5 -0
+	 * replacement is done.
+	 */
+	curr = (struct iommu_attach_handle *)xa_store(&group->pasid_array,
+						      pasid, handle,
+						      GFP_KERNEL);

The cast is not necessary..

drivers/iommu/iommu.c int iommu_attach_device_pasid(struct iommu_domain *domain, · line 3491

Quoted 6 lines
(quoted diff) +2 -0
+
+	ret = __iommu_set_group_pasid(domain, group, pasid, curr-

domain);

drivers/iommu/iommu.c +3 -0
+	if (ret)
+		WARN_ON(handle != xa_store(&group->pasid_array, pasid,
+					   curr, GFP_KERNEL));

I wonder about the ordering here, is it OK to have PRIs being delivered to a domain that failed to attach? What cleans up that race condition with domain free?

Quoted 3 lines

Should we replace the domain then set the xarray? (and same ordering question for normal attach)

That makes sense to me.

But I don't think there is a problem with attach. xa_insert() will return error if an entry already exists. So there won't be any PRI being delivered at __iommu_set_group_pasid(), no matter it succeeds or not.

maintainer
#11 lore raw

Re: [PATCH v6 01/14] iommu: Introduce a replace API for device pasid

Quoted 13 lines · Kevin
Quoted 10 lines

drivers/iommu/iommu.c int iommu_attach_device_pasid(struct iommu_domain *domain, · line 3491

Quoted 5 lines
(quoted diff) +1 -0
+	ret = __iommu_set_group_pasid(domain, group, pasid, curr-

domain);

drivers/iommu/iommu.c +3 -0
+	if (ret)
+		WARN_ON(handle != xa_store(&group->pasid_array, pasid,
+					   curr, GFP_KERNEL));

I wonder about the ordering here, is it OK to have PRIs being delivered to a domain that failed to attach? What cleans up that race condition with domain free?

Quoted 2 lines

Should we replace the domain then set the xarray? (and same ordering question for normal attach)

Quoted 6 lines

That makes sense to me.

But I don't think there is a problem with attach. xa_insert() will return error if an entry already exists. So there won't be any PRI being delivered at __iommu_set_group_pasid(), no matter it succeeds or not.

It has the same issue:

	ret = xa_insert(&group->pasid_array, pasid, handle, GFP_KERNEL);
	if (ret)
		goto out_unlock;

	ret = __iommu_set_group_pasid(domain, group, pasid);
        .. Concurrently a PRI event is pushed to the domain ..
	if (ret)
		xa_erase(&group->pasid_array, pasid);

        .. Now what? Who fences the PRI event thread before the caller
	   frees the domain ..?

We arranged things so that detatch would fence the PRI, if detach is not called then there is no fence..

Jason

maintainer
#12 lore raw

RE: [PATCH v6 01/14] iommu: Introduce a replace API for device pasid

Quoted 39 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, January 14, 2025 9:46 PM

Quoted 13 lines · Kevin
Quoted 10 lines

drivers/iommu/iommu.c int iommu_attach_device_pasid(struct iommu_domain *domain, · line 3491

Quoted 5 lines
(quoted diff) +1 -0
+	ret = __iommu_set_group_pasid(domain, group, pasid, curr-

domain);

drivers/iommu/iommu.c +3 -0
+	if (ret)
+		WARN_ON(handle != xa_store(&group->pasid_array, pasid,
+					   curr, GFP_KERNEL));

I wonder about the ordering here, is it OK to have PRIs being delivered to a domain that failed to attach? What cleans up that race condition with domain free?

Quoted 2 lines

Should we replace the domain then set the xarray? (and same ordering question for normal attach)

Quoted 6 lines

That makes sense to me.

But I don't think there is a problem with attach. xa_insert() will return error if an entry already exists. So there won't be any PRI being delivered at __iommu_set_group_pasid(), no matter it succeeds or not.

It has the same issue:

	ret = xa_insert(&group->pasid_array, pasid, handle, GFP_KERNEL);
	if (ret)
		goto out_unlock;

	ret = __iommu_set_group_pasid(domain, group, pasid);
        .. Concurrently a PRI event is pushed to the domain ..
	if (ret)
		xa_erase(&group->pasid_array, pasid);

        .. Now what? Who fences the PRI event thread before the caller
	   frees the domain ..?
Quoted 3 lines

We arranged things so that detatch would fence the PRI, if detach is not called then there is no fence..

Though I'm fine to change the order in attach too as it looks more reasonable logically, I'm trying to understand the actual impact of the original order (e.g. is the change worth of a Fix tag?)

If there is no detach happened before then it's the 1st attach to a faultable domain and PRI will be enabled right before this function hence no fence required.

Would a sane device trigger PRI in this window?

If it's a detach-then-attach flow, detach will do the fence anyway before the attach.

maintainer
#13 lore raw

Re: [PATCH v6 01/14] iommu: Introduce a replace API for device pasid

Quoted 47 lines · Kevin
Quoted 39 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, January 14, 2025 9:46 PM

Quoted 13 lines · Kevin
Quoted 10 lines

drivers/iommu/iommu.c int iommu_attach_device_pasid(struct iommu_domain *domain, · line 3491

Quoted 5 lines
(quoted diff) +1 -0
+	ret = __iommu_set_group_pasid(domain, group, pasid, curr-

domain);

drivers/iommu/iommu.c +3 -0
+	if (ret)
+		WARN_ON(handle != xa_store(&group->pasid_array, pasid,
+					   curr, GFP_KERNEL));

I wonder about the ordering here, is it OK to have PRIs being delivered to a domain that failed to attach? What cleans up that race condition with domain free?

Quoted 2 lines

Should we replace the domain then set the xarray? (and same ordering question for normal attach)

Quoted 6 lines

That makes sense to me.

But I don't think there is a problem with attach. xa_insert() will return error if an entry already exists. So there won't be any PRI being delivered at __iommu_set_group_pasid(), no matter it succeeds or not.

It has the same issue:

	ret = xa_insert(&group->pasid_array, pasid, handle, GFP_KERNEL);
	if (ret)
		goto out_unlock;

	ret = __iommu_set_group_pasid(domain, group, pasid);
        .. Concurrently a PRI event is pushed to the domain ..
	if (ret)
		xa_erase(&group->pasid_array, pasid);

        .. Now what? Who fences the PRI event thread before the caller
	   frees the domain ..?
Quoted 3 lines

We arranged things so that detatch would fence the PRI, if detach is not called then there is no fence..

Though I'm fine to change the order in attach too as it looks more reasonable logically, I'm trying to understand the actual impact of the original order (e.g. is the change worth of a Fix tag?)

Quoted 5 lines

If there is no detach happened before then it's the 1st attach to a faultable domain and PRI will be enabled right before this function hence no fence required.

Would a sane device trigger PRI in this window?

I would say this is not a "sane" scenario, this is a theoretical race triggerable by the device. Perhaps a VFIO user can force the device to trigger this race and exploit the kernel.

Quoted 2 lines

If it's a detach-then-attach flow, detach will do the fence anyway before the attach.

The issue is the error, once we do the xa_insert() then any faults will get routed to our domain and the fault path threads will hold pointers to the domain. Once the xa_insert() is done we must flush the fault path threads before allowing the domain to be freed.

If __iommu_set_group_pasid() fails then we do an xa_erase() but nothing will flush the fault threads.

Jason

maintainer
#14 lore raw

RE: [PATCH v6 01/14] iommu: Introduce a replace API for device pasid

Quoted 66 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, January 15, 2025 10:44 PM

Quoted 9 lines · Kevin
Quoted 9 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, January 14, 2025 9:46 PM

Quoted 4 lines · Kevin
Quoted 4 lines

Review

Quoted 4 lines
(quoted diff) +1 -0
+	ret = __iommu_set_group_pasid(domain, group, pasid, curr-

domain);

(quoted diff) +2 -0
+	if (ret)
+		WARN_ON(handle != xa_store(&group->pasid_array,

pasid,

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

drivers/iommu/iommu.c int iommu_attach_device_pasid(struct iommu_domain *domain, · line 3491

Quoted 1 line
drivers/iommu/iommu.c +1 -0
+					   curr, GFP_KERNEL));

I wonder about the ordering here, is it OK to have PRIs being delivered to a domain that failed to attach? What cleans up that race condition with domain free?

Should we replace the domain then set the xarray? (and same

ordering

Quoted 31 lines
Quoted 23 lines
Quoted 8 lines
Quoted 1 line

question for normal attach)

That makes sense to me.

But I don't think there is a problem with attach. xa_insert() will return error if an entry already exists. So there won't be any PRI being delivered at __iommu_set_group_pasid(), no matter it succeeds or not.

It has the same issue:

	ret = xa_insert(&group->pasid_array, pasid, handle, GFP_KERNEL);
	if (ret)
		goto out_unlock;

	ret = __iommu_set_group_pasid(domain, group, pasid);
        .. Concurrently a PRI event is pushed to the domain ..
	if (ret)
		xa_erase(&group->pasid_array, pasid);

        .. Now what? Who fences the PRI event thread before the caller
	   frees the domain ..?
Quoted 3 lines

We arranged things so that detatch would fence the PRI, if detach is not called then there is no fence..

Though I'm fine to change the order in attach too as it looks more reasonable logically, I'm trying to understand the actual impact of the original order (e.g. is the change worth of a Fix tag?)

Quoted 5 lines

If there is no detach happened before then it's the 1st attach to a faultable domain and PRI will be enabled right before this function hence no fence required.

Would a sane device trigger PRI in this window?

I would say this is not a "sane" scenario, this is a theoretical race triggerable by the device. Perhaps a VFIO user can force the device to trigger this race and exploit the kernel.

Quoted 2 lines

If it's a detach-then-attach flow, detach will do the fence anyway before the attach.

Quoted 8 lines

The issue is the error, once we do the xa_insert() then any faults will get routed to our domain and the fault path threads will hold pointers to the domain. Once the xa_insert() is done we must flush the fault path threads before allowing the domain to be freed.

If __iommu_set_group_pasid() fails then we do an xa_erase() but nothing will flush the fault threads.

If __iommu_set_group_pasid() fails iommufd will attempt to disable PRI which reaches iopf_queue_remove_device(). The latter auto responds to the list of pending faults.

But this is not a reliable assumption e.g. when multiple functions (VFs and PF) share a single PRI entity.

So I agree it's conceptually clearer to swap the order of updating xarray and doing attach.

btw in reality this won't trigger any issue on VT-d. The spec says that PRI upon a non-present PASID entry (the state before attach succeeds) is auto-responded by HW as 'Invalid Request'. So the entire software faulting path won't triggered at all but this might be a vendor specific behavior...

author
#15 lore raw

Re: [PATCH v6 01/14] iommu: Introduce a replace API for device pasid

Quoted 75 lines · Kevin
Quoted 66 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, January 15, 2025 10:44 PM

Quoted 9 lines · Kevin
Quoted 9 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, January 14, 2025 9:46 PM

Quoted 4 lines · Kevin
Quoted 4 lines

Review

Quoted 4 lines
(quoted diff) +1 -0
+	ret = __iommu_set_group_pasid(domain, group, pasid, curr-

domain);

(quoted diff) +2 -0
+	if (ret)
+		WARN_ON(handle != xa_store(&group->pasid_array,

pasid,

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+					   curr, GFP_KERNEL));

I wonder about the ordering here, is it OK to have PRIs being delivered to a domain that failed to attach? What cleans up that race condition with domain free?

Should we replace the domain then set the xarray? (and same

ordering

Quoted 31 lines
Quoted 23 lines
Quoted 8 lines
Quoted 1 line

question for normal attach)

That makes sense to me.

But I don't think there is a problem with attach. xa_insert() will return error if an entry already exists. So there won't be any PRI being delivered at __iommu_set_group_pasid(), no matter it succeeds or not.

It has the same issue:

	ret = xa_insert(&group->pasid_array, pasid, handle, GFP_KERNEL);
	if (ret)
		goto out_unlock;

	ret = __iommu_set_group_pasid(domain, group, pasid);
         .. Concurrently a PRI event is pushed to the domain ..
	if (ret)
		xa_erase(&group->pasid_array, pasid);

         .. Now what? Who fences the PRI event thread before the caller
	   frees the domain ..?
Quoted 3 lines

We arranged things so that detatch would fence the PRI, if detach is not called then there is no fence..

Though I'm fine to change the order in attach too as it looks more reasonable logically, I'm trying to understand the actual impact of the original order (e.g. is the change worth of a Fix tag?)

Quoted 5 lines

If there is no detach happened before then it's the 1st attach to a faultable domain and PRI will be enabled right before this function hence no fence required.

Would a sane device trigger PRI in this window?

I would say this is not a "sane" scenario, this is a theoretical race triggerable by the device. Perhaps a VFIO user can force the device to trigger this race and exploit the kernel.

Quoted 2 lines

If it's a detach-then-attach flow, detach will do the fence anyway before the attach.

Quoted 8 lines

The issue is the error, once we do the xa_insert() then any faults will get routed to our domain and the fault path threads will hold pointers to the domain. Once the xa_insert() is done we must flush the fault path threads before allowing the domain to be freed.

If __iommu_set_group_pasid() fails then we do an xa_erase() but nothing will flush the fault threads.

Quoted 6 lines

If __iommu_set_group_pasid() fails iommufd will attempt to disable PRI which reaches iopf_queue_remove_device(). The latter auto responds to the list of pending faults.

But this is not a reliable assumption e.g. when multiple functions (VFs and PF) share a single PRI entity.

I think iommufd fault only supports PF so far. VF is not supported. :) But it's better not make decisions based on this limitation when considering the PRI flushing issue.

Quoted 8 lines

So I agree it's conceptually clearer to swap the order of updating xarray and doing attach.

btw in reality this won't trigger any issue on VT-d. The spec says that PRI upon a non-present PASID entry (the state before attach succeeds) is auto-responded by HW as 'Invalid Request'. So the entire software faulting path won't triggered at all but this might be a vendor specific behavior...

I'll swap the order between the group->pasid_array and __iommu_set_group_pasid() in both the PASID attach and replace path.

I think the RID attach/replace path have the same problem since the PRI forwarding path also retrieves the iommu_attach_handle from the group->pasid_array. Even worse, I didn't see the RID path set the handle to group->pasid_array. @Baolu, perhaps you can help to fix the RID path? :)

Signature · 2 lines
Regards,
Yi Liu
author
#16 lore raw

PATCH v6 2/14 iommufd: Refactor __fault_domain_replace_dev() to be a wrapper of iommu_replace_group_handle()

In reply to Yi Liu

There is a wrapper of iommu_attach_group_handle(), so making a wrapper for iommu_replace_group_handle() for further code refactor. No functional change intended.

---
 drivers/iommu/iommufd/fault.c | 50 ++++++++++++++++++-----------------
 1 file changed, 26 insertions(+), 24 deletions(-)
drivers/iommu/iommufd/fault.c +26 -24
index 1fe804e28a86..d09f4594c67a 100644
@@ -151,33 +151,23 @@ void iommufd_fault_domain_detach_dev(struct iommufd_hw_pagetable *hwpt,
 	kfree(handle);
 }
 
-static int __fault_domain_replace_dev(struct iommufd_device *idev,
-				      struct iommufd_hw_pagetable *hwpt,
-				      struct iommufd_hw_pagetable *old)
+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;
+	struct iommufd_attach_handle *handle;
 	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->idev = idev;
-		ret = iommu_replace_group_handle(idev->igroup->group,
-						 hwpt->domain, &handle->handle);
-	} else {
-		ret = iommu_replace_group_handle(idev->igroup->group,
-						 hwpt->domain, NULL);
-	}
+	handle = kzalloc(sizeof(*handle), GFP_KERNEL);
+	if (!handle)
+		return -ENOMEM;
 
-	if (!ret && curr) {
-		iommufd_auto_response_faults(old, curr);
-		kfree(curr);
-	}
+	handle->idev = idev;
+	ret = iommu_replace_group_handle(idev->igroup->group,
+					 hwpt->domain, &handle->handle);
+	if (ret)
+		kfree(handle);
 
 	return ret;
 }
@@ -188,6 +178,7 @@ int iommufd_fault_domain_replace_dev(struct iommufd_device *idev,
 {
 	bool iopf_off = !hwpt->fault && old->fault;
 	bool iopf_on = hwpt->fault && !old->fault;
+	struct iommufd_attach_handle *curr;
 	int ret;
 
 	if (iopf_on) {
@@ -196,13 +187,24 @@ int iommufd_fault_domain_replace_dev(struct iommufd_device *idev,
 			return ret;
 	}
 
-	ret = __fault_domain_replace_dev(idev, hwpt, old);
+	curr = iommufd_device_get_attach_handle(idev);
+
+	if (hwpt->fault)
+		ret = __fault_domain_replace_dev(idev, hwpt, old);
+	else
+		ret = iommu_replace_group_handle(idev->igroup->group,
+						 hwpt->domain, NULL);
 	if (ret) {
 		if (iopf_on)
 			iommufd_fault_iopf_disable(idev);
 		return ret;
 	}
 
+	if (curr) {
+		iommufd_auto_response_faults(old, curr);
+		kfree(curr);
+	}
+
 	if (iopf_off)
 		iommufd_fault_iopf_disable(idev);
 
Signature · 1 line
2.34.1
author
#17 lore raw

PATCH v6 3/14 iommufd: Move the iommufd_handle helpers to device.c

In reply to Yi Liu

The iommu_attach_handle is now only passed when attaching iopf-capable domain, while it is not convenient for the iommu core to track the attached domain of pasids. To address it, the iommu_attach_handle will be passed to iommu core for non-fault-able domain as well. Hence the iommufd_handle related helpers are no longer fault specific, it makes more sense to move it out of fault.c.

---
 drivers/iommu/iommufd/device.c          | 62 +++++++++++++++++++++++++
 drivers/iommu/iommufd/fault.c           | 56 +---------------------
 drivers/iommu/iommufd/iommufd_private.h |  8 ++++
 3 files changed, 72 insertions(+), 54 deletions(-)
drivers/iommu/iommufd/device.c +62 -0
index dfd0898fb6c1..0e1baf84e887 100644
@@ -293,6 +293,68 @@ u32 iommufd_device_to_id(struct iommufd_device *idev)
 }
 EXPORT_SYMBOL_NS_GPL(iommufd_device_to_id, "IOMMUFD");
 
+/**
+ * iommufd_device_get_attach_handle - Return the attach handle for the RID
+ *
+ * @idev: The device to get attach_handle
+ *
+ * Currently there is no locking to synchronize threads that access the
+ * returned handle with those attaching or replacing the domain which might
+ * change the handle. It's caller's duty to guarantee no use-after-free.
+ *
+ * Return valid attach_handle if there is, otherwise NULL.
+ */
+struct iommufd_attach_handle *
+iommufd_device_get_attach_handle(struct iommufd_device *idev)
+{
+	struct iommu_attach_handle *handle;
+
+	handle = iommu_attach_handle_get(idev->igroup->group, IOMMU_NO_PASID, 0);
+	if (IS_ERR(handle))
+		return NULL;
+
+	return to_iommufd_handle(handle);
+}
+
+int iommufd_dev_attach_handle(struct iommufd_hw_pagetable *hwpt,
+			      struct iommufd_device *idev)
+{
+	struct iommufd_attach_handle *handle;
+	int ret;
+
+	handle = kzalloc(sizeof(*handle), GFP_KERNEL);
+	if (!handle)
+		return -ENOMEM;
+
+	handle->idev = idev;
+	ret = iommu_attach_group_handle(hwpt->domain, idev->igroup->group,
+					&handle->handle);
+	if (ret)
+		kfree(handle);
+
+	return ret;
+}
+
+int iommufd_dev_replace_handle(struct iommufd_device *idev,
+			       struct iommufd_hw_pagetable *hwpt,
+			       struct iommufd_hw_pagetable *old)
+{
+	struct iommufd_attach_handle *handle;
+	int ret;
+
+	handle = kzalloc(sizeof(*handle), GFP_KERNEL);
+	if (!handle)
+		return -ENOMEM;
+
+	handle->idev = idev;
+	ret = iommu_replace_group_handle(idev->igroup->group,
+					 hwpt->domain, &handle->handle);
+	if (ret)
+		kfree(handle);
+
+	return ret;
+}
+
 static int iommufd_group_setup_msi(struct iommufd_group *igroup,
 				   struct iommufd_hwpt_paging *hwpt_paging)
 {
drivers/iommu/iommufd/fault.c +2 -54

86 lines not on this page · raw message

drivers/iommu/iommufd/iommufd_private.h +8 -0
index b6d706cf2c66..d5c83f10d83e 100644
@@ -459,6 +459,14 @@ 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)
 
+struct iommufd_attach_handle *
+iommufd_device_get_attach_handle(struct iommufd_device *idev);
+int iommufd_dev_attach_handle(struct iommufd_hw_pagetable *hwpt,
+			      struct iommufd_device *idev);
+int iommufd_dev_replace_handle(struct iommufd_device *idev,
+			       struct iommufd_hw_pagetable *hwpt,
+			       struct iommufd_hw_pagetable *old);
+
 static inline struct iommufd_fault *
 iommufd_get_fault(struct iommufd_ucmd *ucmd, u32 id)
 {
Signature · 1 line
2.34.1
maintainer
#18 lore raw

Re: [PATCH v6 03/14] iommufd: Move the iommufd_handle helpers to device.c

Quoted 20 lines · Yi Liu

The iommu_attach_handle is now only passed when attaching iopf-capable domain, while it is not convenient for the iommu core to track the attached domain of pasids. To address it, the iommu_attach_handle will be passed to iommu core for non-fault-able domain as well. Hence the iommufd_handle related helpers are no longer fault specific, it makes more sense to move it out of fault.c.

---
  drivers/iommu/iommufd/device.c          | 62 +++++++++++++++++++++++++
  drivers/iommu/iommufd/fault.c           | 56 +---------------------
  drivers/iommu/iommufd/iommufd_private.h |  8 ++++
  3 files changed, 72 insertions(+), 54 deletions(-)
drivers/iommu/iommufd/device.c +0 -0
index dfd0898fb6c1..0e1baf84e887 100644
@@ -293,6 +293,68 @@ u32 iommufd_device_to_id(struct iommufd_device *idev)
  }

drivers/iommu/iommufd/device.c u32 iommufd_device_to_id(struct iommufd_device *idev) · line 303

Quoted 10 lines
drivers/iommu/iommufd/device.c +8 -0
  EXPORT_SYMBOL_NS_GPL(iommufd_device_to_id, "IOMMUFD");
 
+/**
+ * iommufd_device_get_attach_handle - Return the attach handle for the RID
+ *
+ * @idev: The device to get attach_handle
+ *
+ * Currently there is no locking to synchronize threads that access the
+ * returned handle with those attaching or replacing the domain which might
+ * change the handle. It's caller's duty to guarantee no use-after-free.

It's better to make "It's caller's duty to guarantee no use-after-free" more specific. Something like, the caller is responsible for ensuring that the returned pointer is not used after the domain is removed from the device's RID.

Quoted 43 lines
drivers/iommu/iommufd/device.c +43 -0
+ *
+ * Return valid attach_handle if there is, otherwise NULL.
+ */
+struct iommufd_attach_handle *
+iommufd_device_get_attach_handle(struct iommufd_device *idev)
+{
+	struct iommu_attach_handle *handle;
+
+	handle = iommu_attach_handle_get(idev->igroup->group, IOMMU_NO_PASID, 0);
+	if (IS_ERR(handle))
+		return NULL;
+
+	return to_iommufd_handle(handle);
+}
+
+int iommufd_dev_attach_handle(struct iommufd_hw_pagetable *hwpt,
+			      struct iommufd_device *idev)
+{
+	struct iommufd_attach_handle *handle;
+	int ret;
+
+	handle = kzalloc(sizeof(*handle), GFP_KERNEL);
+	if (!handle)
+		return -ENOMEM;
+
+	handle->idev = idev;
+	ret = iommu_attach_group_handle(hwpt->domain, idev->igroup->group,
+					&handle->handle);
+	if (ret)
+		kfree(handle);
+
+	return ret;
+}
+
+int iommufd_dev_replace_handle(struct iommufd_device *idev,
+			       struct iommufd_hw_pagetable *hwpt,
+			       struct iommufd_hw_pagetable *old)
+{
+	struct iommufd_attach_handle *handle;
+	int ret;
+
+	handle = kzalloc(sizeof(*handle), GFP_KERNEL);
+	if (!handle)

drivers/iommu/iommufd/device.c u32 iommufd_device_to_id(struct iommufd_device *idev) · line 356

Quoted 10 lines
drivers/iommu/iommufd/device.c +10 -0
+		return -ENOMEM;
+
+	handle->idev = idev;
+	ret = iommu_replace_group_handle(idev->igroup->group,
+					 hwpt->domain, &handle->handle);
+	if (ret)
+		kfree(handle);
+
+	return ret;
+}

Where will the old handle be freed? It seems unreasonable to allocate the handle in these helper functions, only to have it freed by callers in other files.

Thanks,
baolu

author
#19 lore raw

Re: [PATCH v6 03/14] iommufd: Move the iommufd_handle helpers to device.c

Quoted 27 lines · Baolu Lu
Quoted 26 lines · Yi Liu

The iommu_attach_handle is now only passed when attaching iopf-capable domain, while it is not convenient for the iommu core to track the attached domain of pasids. To address it, the iommu_attach_handle will be passed to iommu core for non-fault-able domain as well. Hence the iommufd_handle related helpers are no longer fault specific, it makes more sense to move it out of fault.c.

---
  drivers/iommu/iommufd/device.c          | 62 +++++++++++++++++++++++++ drivers/iommu/iommufd/fault.c           | 56 +--------------------- drivers/iommu/iommufd/iommufd_private.h |  8 ++++

  3 files changed, 72 insertions(+), 54 deletions(-)
drivers/iommu/iommufd/device.c +5 -0
index dfd0898fb6c1..0e1baf84e887 100644
@@ -293,6 +293,68 @@ u32 iommufd_device_to_id(struct iommufd_device *idev)
   }
   EXPORT_SYMBOL_NS_GPL(iommufd_device_to_id, "IOMMUFD");
+/**
+ * iommufd_device_get_attach_handle - Return the attach handle for the RID
+ *
+ * @idev: The device to get attach_handle
+ *
Quoted 8 lines

drivers/iommu/iommufd/device.c u32 iommufd_device_to_id(struct iommufd_device *idev) · line 303

Quoted 3 lines
drivers/iommu/iommufd/device.c +3 -0
+ * Currently there is no locking to synchronize threads that access the
+ * returned handle with those attaching or replacing the domain which might
+ * change the handle. It's caller's duty to guarantee no use-after-free.

It's better to make "It's caller's duty to guarantee no use-after-free" more specific. Something like, the caller is responsible for ensuring that the returned pointer is not used after the domain is removed from the device's RID.

ok.

Quoted 50 lines
Quoted 50 lines
drivers/iommu/iommufd/device.c +49 -0
+ *
+ * Return valid attach_handle if there is, otherwise NULL.
+ */
+struct iommufd_attach_handle *
+iommufd_device_get_attach_handle(struct iommufd_device *idev)
+{
+    struct iommu_attach_handle *handle;
+
+    handle = iommu_attach_handle_get(idev->igroup->group, 
 IOMMU_NO_PASID, 0);
+    if (IS_ERR(handle))
+        return NULL;
+
+    return to_iommufd_handle(handle);
+}
+
+int iommufd_dev_attach_handle(struct iommufd_hw_pagetable *hwpt,
+                  struct iommufd_device *idev)
+{
+    struct iommufd_attach_handle *handle;
+    int ret;
+
+    handle = kzalloc(sizeof(*handle), GFP_KERNEL);
+    if (!handle)
+        return -ENOMEM;
+
+    handle->idev = idev;
+    ret = iommu_attach_group_handle(hwpt->domain, idev->igroup->group,
+                    &handle->handle);
+    if (ret)
+        kfree(handle);
+
+    return ret;
+}
+
+int iommufd_dev_replace_handle(struct iommufd_device *idev,
+                   struct iommufd_hw_pagetable *hwpt,
+                   struct iommufd_hw_pagetable *old)
+{
+    struct iommufd_attach_handle *handle;
+    int ret;
+
+    handle = kzalloc(sizeof(*handle), GFP_KERNEL);
+    if (!handle)
+        return -ENOMEM;
+
+    handle->idev = idev;
+    ret = iommu_replace_group_handle(idev->igroup->group,
+                     hwpt->domain, &handle->handle);
+    if (ret)
Quoted 8 lines

drivers/iommu/iommufd/device.c u32 iommufd_device_to_id(struct iommufd_device *idev) · line 336

Quoted 4 lines
drivers/iommu/iommufd/device.c +4 -0
+        kfree(handle);
+
+    return ret;
+}

Where will the old handle be freed? It seems unreasonable to allocate the handle in these helper functions, only to have it freed by callers in other files.

yes, it's in the caller side. See the below snippet in patch 02. Maybe it's an over work to split this patch with patch 04. Merging them may be helpful. Nic has a proposed patch as well for such purpose.

@@ -196,13 +187,24 @@ int iommufd_fault_domain_replace_dev(struct iommufd_device *idev,
return ret;
}

- ret = __fault_domain_replace_dev(idev, hwpt, old);
+ curr = iommufd_device_get_attach_handle(idev);
+
+ if (hwpt->fault)
+ ret = __fault_domain_replace_dev(idev, hwpt, old);
+ else
+ ret = iommu_replace_group_handle(idev->igroup->group,
+ hwpt->domain, NULL);
if (ret) {
if (iopf_on)
iommufd_fault_iopf_disable(idev);
return ret;
}

+ if (curr) {
+ iommufd_auto_response_faults(old, curr);
+ kfree(curr);
+ }
+

Signature · 2 lines
Regards,
Yi Liu
author
#20 lore raw

PATCH v6 4/14 iommufd: Always pass iommu_attach_handle to iommu core

In reply to Yi Liu

The iommu_attach_handle is optional in the RID attach/replace API and the PASID attach APIs. But it is a mandatory argument for the PASID replace API. Without it, the PASID replace path cannot get the old domain. Hence, the PASID path (attach/replace) requires the attach handle. As iommufd is the major user of the RID attach/replace with iommu_attach_handle, this also makes the iommufd always pass the attach handle for the RID path as well. This keeps the RID and PASID path much aligned.

---
 drivers/iommu/iommufd/fault.c           | 12 ++++--------
 drivers/iommu/iommufd/iommufd_private.h | 20 +++++++++++++++++---
 2 files changed, 21 insertions(+), 11 deletions(-)
drivers/iommu/iommufd/fault.c +4 -8
index d06893226070..19f6e2b84274 100644
@@ -137,21 +137,17 @@ int iommufd_fault_domain_replace_dev(struct iommufd_device *idev,
 
 	curr = iommufd_device_get_attach_handle(idev);
 
-	if (hwpt->fault)
-		ret = iommufd_dev_replace_handle(idev, hwpt, old);
-	else
-		ret = iommu_replace_group_handle(idev->igroup->group,
-						 hwpt->domain, NULL);
+	ret = iommufd_dev_replace_handle(idev, hwpt, old);
 	if (ret) {
 		if (iopf_on)
 			iommufd_fault_iopf_disable(idev);
 		return ret;
 	}
 
-	if (curr) {
+	if (old->fault)
 		iommufd_auto_response_faults(old, curr);
-		kfree(curr);
-	}
+
+	kfree(curr);
 
 	if (iopf_off)
 		iommufd_fault_iopf_disable(idev);
drivers/iommu/iommufd/iommufd_private.h +17 -3
index d5c83f10d83e..6cf9c1f10e85 100644
@@ -493,28 +493,42 @@ static inline int iommufd_hwpt_attach_device(struct iommufd_hw_pagetable *hwpt,
 	if (hwpt->fault)
 		return iommufd_fault_domain_attach_dev(hwpt, idev);
 
-	return iommu_attach_group(hwpt->domain, idev->igroup->group);
+	return iommufd_dev_attach_handle(hwpt, idev);
 }
 
 static inline void iommufd_hwpt_detach_device(struct iommufd_hw_pagetable *hwpt,
 					      struct iommufd_device *idev)
 {
+	struct iommufd_attach_handle *handle;
+
 	if (hwpt->fault) {
 		iommufd_fault_domain_detach_dev(hwpt, idev);
 		return;
 	}
 
-	iommu_detach_group(hwpt->domain, idev->igroup->group);
+	handle = iommufd_device_get_attach_handle(idev);
+	iommu_detach_group_handle(hwpt->domain, idev->igroup->group);
+	kfree(handle);
 }
 
 static inline int iommufd_hwpt_replace_device(struct iommufd_device *idev,
 					      struct iommufd_hw_pagetable *hwpt,
 					      struct iommufd_hw_pagetable *old)
 {
+	struct iommufd_attach_handle *curr;
+	int ret;
+
 	if (old->fault || hwpt->fault)
 		return iommufd_fault_domain_replace_dev(idev, hwpt, old);
 
-	return iommu_group_replace_domain(idev->igroup->group, hwpt->domain);
+	curr = iommufd_device_get_attach_handle(idev);
+
+	ret = iommufd_dev_replace_handle(idev, hwpt, old);
+	if (ret)
+		return ret;
+
+	kfree(curr);
+	return 0;
 }
 
 static inline struct iommufd_viommu *
Signature · 1 line
2.34.1
#21 lore raw

Re: [PATCH v6 04/14] iommufd: Always pass iommu_attach_handle to iommu core

Hi Yi,

Quoted 42 lines · Yi Liu

The iommu_attach_handle is optional in the RID attach/replace API and the PASID attach APIs. But it is a mandatory argument for the PASID replace API. Without it, the PASID replace path cannot get the old domain. Hence, the PASID path (attach/replace) requires the attach handle. As iommufd is the major user of the RID attach/replace with iommu_attach_handle, this also makes the iommufd always pass the attach handle for the RID path as well. This keeps the RID and PASID path much aligned.

drivers/iommu/iommufd/iommufd_private.h +8 -2
index d5c83f10d83e..6cf9c1f10e85 100644
@@ -493,28 +493,42 @@ static inline int iommufd_hwpt_attach_device(struct iommufd_hw_pagetable *hwpt,
 	if (hwpt->fault)
 		return iommufd_fault_domain_attach_dev(hwpt, idev);
 
-	return iommu_attach_group(hwpt->domain, idev->igroup->group);
+	return iommufd_dev_attach_handle(hwpt, idev);
 }
 
 static inline void iommufd_hwpt_detach_device(struct iommufd_hw_pagetable *hwpt,
 					      struct iommufd_device *idev)
 {
+	struct iommufd_attach_handle *handle;
+
 	if (hwpt->fault) {
 		iommufd_fault_domain_detach_dev(hwpt, idev);
 		return;
 	}
 
-	iommu_detach_group(hwpt->domain, idev->igroup->group);
+	handle = iommufd_device_get_attach_handle(idev);
+	iommu_detach_group_handle(hwpt->domain, idev->igroup->group);
+	kfree(handle);
 }
 
 static inline int iommufd_hwpt_replace_device(struct iommufd_device *idev,
 					      struct iommufd_hw_pagetable *hwpt,
 					      struct iommufd_hw_pagetable *old)
 {
+	struct iommufd_attach_handle *curr;
+	int ret;

drivers/iommu/iommufd/iommufd_private.h static inline int iommufd_hwpt_attach_device(struct iommufd_hw_pagetable *hwpt, · line 528

Quoted 10 lines
drivers/iommu/iommufd/iommufd_private.h +6 -1
+
 	if (old->fault || hwpt->fault)
 		return iommufd_fault_domain_replace_dev(idev, hwpt, old);
 
-	return iommu_group_replace_domain(idev->igroup->group, hwpt->domain);
+	curr = iommufd_device_get_attach_handle(idev);
+
+	ret = iommufd_dev_replace_handle(idev, hwpt, old);
+	if (ret)
+		return ret;

These inline functions feel heavier after this rework..

I actually have the same patch for a different reason, yet haven't sent yet:
https://github.com/nicolinc/iommufd/commits/wip/iommufd_msi_p1-v1
iommu: Turn fault_data to iommufd private pointer
=>iommufd: Make attach_handle generic

I think we can align with each other on these functions with one common patch: my series requires the fault_data and attach_handle are common to all HWPTs, v.s. exclusive to an hwpt->fault.

Thanks
Nicolin

author
#22 lore raw

Re: [PATCH v6 04/14] iommufd: Always pass iommu_attach_handle to iommu core

Quoted 64 lines · Nicolin Chen

Hi Yi,

Quoted 42 lines · Yi Liu

The iommu_attach_handle is optional in the RID attach/replace API and the PASID attach APIs. But it is a mandatory argument for the PASID replace API. Without it, the PASID replace path cannot get the old domain. Hence, the PASID path (attach/replace) requires the attach handle. As iommufd is the major user of the RID attach/replace with iommu_attach_handle, this also makes the iommufd always pass the attach handle for the RID path as well. This keeps the RID and PASID path much aligned.

drivers/iommu/iommufd/iommufd_private.h +8 -2
index d5c83f10d83e..6cf9c1f10e85 100644
@@ -493,28 +493,42 @@ static inline int iommufd_hwpt_attach_device(struct iommufd_hw_pagetable *hwpt,
  	if (hwpt->fault)
  		return iommufd_fault_domain_attach_dev(hwpt, idev);
 
-	return iommu_attach_group(hwpt->domain, idev->igroup->group);
+	return iommufd_dev_attach_handle(hwpt, idev);
  }
 
  static inline void iommufd_hwpt_detach_device(struct iommufd_hw_pagetable *hwpt,
  					      struct iommufd_device *idev)
  {
+	struct iommufd_attach_handle *handle;
+
  	if (hwpt->fault) {
  		iommufd_fault_domain_detach_dev(hwpt, idev);
  		return;
  	}
 
-	iommu_detach_group(hwpt->domain, idev->igroup->group);
+	handle = iommufd_device_get_attach_handle(idev);
+	iommu_detach_group_handle(hwpt->domain, idev->igroup->group);
+	kfree(handle);
  }
 
  static inline int iommufd_hwpt_replace_device(struct iommufd_device *idev,
  					      struct iommufd_hw_pagetable *hwpt,
  					      struct iommufd_hw_pagetable *old)
  {
+	struct iommufd_attach_handle *curr;
+	int ret;

drivers/iommu/iommufd/iommufd_private.h static inline int iommufd_hwpt_attach_device(struct iommufd_hw_pagetable *hwpt, · line 528

Quoted 10 lines
drivers/iommu/iommufd/iommufd_private.h +6 -1
+
  	if (old->fault || hwpt->fault)
  		return iommufd_fault_domain_replace_dev(idev, hwpt, old);
 
-	return iommu_group_replace_domain(idev->igroup->group, hwpt->domain);
+	curr = iommufd_device_get_attach_handle(idev);
+
+	ret = iommufd_dev_replace_handle(idev, hwpt, old);
+	if (ret)
+		return ret;

These inline functions feel heavier after this rework..

I actually have the same patch for a different reason, yet haven't sent yet:
https://github.com/nicolinc/iommufd/commits/wip/iommufd_msi_p1-v1
iommu: Turn fault_data to iommufd private pointer
=>iommufd: Make attach_handle generic

Quoted 3 lines

I think we can align with each other on these functions with one common patch: my series requires the fault_data and attach_handle are common to all HWPTs, v.s. exclusive to an hwpt->fault.

yeah, I'm ok to move the helpers to be in device.c to avoid inline. I've three remark on your patch [1]. I'm now also considering to do the work in one patch, hence it might be better for review w.r.t the comment from Baolu [2].

[1] https://github.com/nicolinc/iommufd/commit/26f9a2a4a5e28574295ecfe057e949dcc9a58c7e#diff-c251419ce7f13e1e22e8fe08864a50d8541d6c9addaee27f806bf9786b4a4886R374
[2] https://lore.kernel.org/linux-iommu/[email protected]/

Signature · 2 lines
Regards,
Yi Liu
#23 lore raw

Re: [PATCH v6 04/14] iommufd: Always pass iommu_attach_handle to iommu core

Quoted 38 lines · Yi Liu
Quoted 38 lines
Quoted 19 lines

static inline void iommufd_hwpt_detach_device(struct iommufd_hw_pagetable *hwpt,
struct iommufd_device *idev)
{

drivers/iommu/iommufd/iommufd_private.h +6 -1
+	struct iommufd_attach_handle *handle;
+
  	if (hwpt->fault) {
  		iommufd_fault_domain_detach_dev(hwpt, idev);
  		return;
  	}
-	iommu_detach_group(hwpt->domain, idev->igroup->group);
+	handle = iommufd_device_get_attach_handle(idev);
+	iommu_detach_group_handle(hwpt->domain, idev->igroup->group);
+	kfree(handle);
  }
  static inline int iommufd_hwpt_replace_device(struct iommufd_device *idev,
  					      struct iommufd_hw_pagetable *hwpt,
  					      struct iommufd_hw_pagetable *old)
  {
+	struct iommufd_attach_handle *curr;

drivers/iommu/iommufd/iommufd_private.h static inline int iommufd_hwpt_attach_device(struct iommufd_hw_pagetable *hwpt, · line 528

Quoted 10 lines
drivers/iommu/iommufd/iommufd_private.h +7 -1
+	int ret;
+
  	if (old->fault || hwpt->fault)
  		return iommufd_fault_domain_replace_dev(idev, hwpt, old);
-	return iommu_group_replace_domain(idev->igroup->group, hwpt->domain);
+	curr = iommufd_device_get_attach_handle(idev);
+
+	ret = iommufd_dev_replace_handle(idev, hwpt, old);
+	if (ret)
+		return ret;

These inline functions feel heavier after this rework..

I actually have the same patch for a different reason, yet haven't sent yet:
https://github.com/nicolinc/iommufd/commits/wip/iommufd_msi_p1-v1
iommu: Turn fault_data to iommufd private pointer
=>iommufd: Make attach_handle generic

Quoted 8 lines
Quoted 3 lines

I think we can align with each other on these functions with one common patch: my series requires the fault_data and attach_handle are common to all HWPTs, v.s. exclusive to an hwpt->fault.

yeah, I'm ok to move the helpers to be in device.c to avoid inline. I've three remark on your patch [1]. I'm now also considering to do the work in one patch, hence it might be better for review w.r.t the comment from Baolu [2].

I replied: I moved attach_handle out of fault functions, leaving them to only handle fault specific routines.

Yet, you might want to fix the iommufd_device_get_attach_handle() in my patch to a normal function where NULL will be returned upon an IS_ERR(attach_handle) v.s. just a to_iommufd_handle, which I just realized a minute ago..

Thanks
Nic

maintainer
#24 lore raw

RE: [PATCH v6 04/14] iommufd: Always pass iommu_attach_handle to iommu core

In reply to Yi Liu
Quoted 18 lines

From: Liu, Yi L <[email protected]>
Sent: Thursday, December 19, 2024 9:28 PM

static inline int iommufd_hwpt_replace_device(struct iommufd_device *idev,
struct iommufd_hw_pagetable
*hwpt,
struct iommufd_hw_pagetable
*old)
{

drivers/iommu/iommufd/iommufd_private.h +3 -1
+	struct iommufd_attach_handle *curr;
+	int ret;
+
 	if (old->fault || hwpt->fault)
 		return iommufd_fault_domain_replace_dev(idev, hwpt, old);
 
-	return iommu_group_replace_domain(idev->igroup->group, hwpt-
Quoted 1 line

domain);

drivers/iommu/iommufd/iommufd_private.h static inline int iommufd_hwpt_attach_device(struct iommufd_hw_pagetable *hwpt, · line 532

Quoted 9 lines
drivers/iommu/iommufd/iommufd_private.h +8 -0
+	curr = iommufd_device_get_attach_handle(idev);
+
+	ret = iommufd_dev_replace_handle(idev, hwpt, old);
+	if (ret)
+		return ret;
+
+	kfree(curr);
+	return 0;
 }

It's not balanced to have handle freed in different locations (one in Fault domain specific handler and the other here).

It sounds clearer to remove the fault domain specific helper and have everything together in this function when you move it to a C file.

author
#25 lore raw

Re: [PATCH v6 04/14] iommufd: Always pass iommu_attach_handle to iommu core

Quoted 31 lines · Kevin
Quoted 18 lines

From: Liu, Yi L <[email protected]>
Sent: Thursday, December 19, 2024 9:28 PM

static inline int iommufd_hwpt_replace_device(struct iommufd_device *idev,
struct iommufd_hw_pagetable
*hwpt,
struct iommufd_hw_pagetable
*old)
{

drivers/iommu/iommufd/iommufd_private.h +3 -1
+	struct iommufd_attach_handle *curr;
+	int ret;
+
  	if (old->fault || hwpt->fault)
  		return iommufd_fault_domain_replace_dev(idev, hwpt, old);
 
-	return iommu_group_replace_domain(idev->igroup->group, hwpt-
Quoted 1 line

domain);

drivers/iommu/iommufd/iommufd_private.h static inline int iommufd_hwpt_attach_device(struct iommufd_hw_pagetable *hwpt, · line 532

Quoted 9 lines
drivers/iommu/iommufd/iommufd_private.h +8 -0
+	curr = iommufd_device_get_attach_handle(idev);
+
+	ret = iommufd_dev_replace_handle(idev, hwpt, old);
+	if (ret)
+		return ret;
+
+	kfree(curr);
+	return 0;
  }

It's not balanced to have handle freed in different locations (one in Fault domain specific handler and the other here).

Quoted 2 lines

It sounds clearer to remove the fault domain specific helper and have everything together in this function when you move it to a C file.

sure. I'm also coordinating with Nic about this since he also needs to make the handle code to be generic in both fault and non-fault path.

Signature · 2 lines
Regards,
Yi Liu
#26 lore raw

Re: [PATCH v6 04/14] iommufd: Always pass iommu_attach_handle to iommu core

Quoted 32 lines · Yi Liu
Quoted 31 lines · Kevin
Quoted 18 lines

From: Liu, Yi L <[email protected]>
Sent: Thursday, December 19, 2024 9:28 PM

static inline int iommufd_hwpt_replace_device(struct iommufd_device *idev,
struct iommufd_hw_pagetable
*hwpt,
struct iommufd_hw_pagetable
*old)
{

drivers/iommu/iommufd/iommufd_private.h +3 -1
+	struct iommufd_attach_handle *curr;
+	int ret;
+
  	if (old->fault || hwpt->fault)
  		return iommufd_fault_domain_replace_dev(idev, hwpt, old);
 
-	return iommu_group_replace_domain(idev->igroup->group, hwpt-
Quoted 1 line

domain);

drivers/iommu/iommufd/iommufd_private.h static inline int iommufd_hwpt_attach_device(struct iommufd_hw_pagetable *hwpt, · line 532

Quoted 9 lines
drivers/iommu/iommufd/iommufd_private.h +8 -0
+	curr = iommufd_device_get_attach_handle(idev);
+
+	ret = iommufd_dev_replace_handle(idev, hwpt, old);
+	if (ret)
+		return ret;
+
+	kfree(curr);
+	return 0;
  }

It's not balanced to have handle freed in different locations (one in Fault domain specific handler and the other here).

Quoted 5 lines
Quoted 2 lines

It sounds clearer to remove the fault domain specific helper and have everything together in this function when you move it to a C file.

sure. I'm also coordinating with Nic about this since he also needs to make the handle code to be generic in both fault and non-fault path.

Yea, it would look like the one in the MSI series [1]. A common flow attaches/detaches/replaces the handles. If it's on a fault patch, do the extra to enable/disable the fault specific stuff.

[1] https://lore.kernel.org/kvm/c708aedc678c63e2466b43ab9d4f8ac876e49aa1.1736550979.git.nicolinc@nvidia.com/

author
#27 lore raw

PATCH v6 5/14 iommufd: Pass pasid through the device attach/replace path

In reply to Yi Liu

Most of the core logic before conducting the actual device attach/ replace operation can be shared with pasid attach/replace. So pass pasid through the device attach/replace helpers to prepare adding pasid attach/replace.

So far the @pasid should only be IOMMU_NO_PASID. No functional change.

---
 drivers/iommu/iommufd/device.c          | 57 ++++++++++++++-----------
 drivers/iommu/iommufd/fault.c           | 16 ++++---
 drivers/iommu/iommufd/hw_pagetable.c    |  5 ++-
 drivers/iommu/iommufd/iommufd_private.h | 41 +++++++++++-------
 4 files changed, 72 insertions(+), 47 deletions(-)
drivers/iommu/iommufd/device.c +33 -24
index 0e1baf84e887..c22ef4077348 100644
@@ -294,9 +294,10 @@ u32 iommufd_device_to_id(struct iommufd_device *idev)
 EXPORT_SYMBOL_NS_GPL(iommufd_device_to_id, "IOMMUFD");
 
 /**
- * iommufd_device_get_attach_handle - Return the attach handle for the RID
+ * iommufd_device_get_attach_handle - Return the attach handle for the RID/PASID
  *
  * @idev: The device to get attach_handle
+ * @pasid: The pasid of the device to get attach_handle
  *
  * Currently there is no locking to synchronize threads that access the
  * returned handle with those attaching or replacing the domain which might
@@ -305,11 +306,11 @@ EXPORT_SYMBOL_NS_GPL(iommufd_device_to_id, "IOMMUFD");
  * Return valid attach_handle if there is, otherwise NULL.
  */
 struct iommufd_attach_handle *
-iommufd_device_get_attach_handle(struct iommufd_device *idev)
+iommufd_device_get_attach_handle(struct iommufd_device *idev, ioasid_t pasid)
 {
 	struct iommu_attach_handle *handle;
 
-	handle = iommu_attach_handle_get(idev->igroup->group, IOMMU_NO_PASID, 0);
+	handle = iommu_attach_handle_get(idev->igroup->group, pasid, 0);
 	if (IS_ERR(handle))
 		return NULL;
 
@@ -317,7 +318,8 @@ iommufd_device_get_attach_handle(struct iommufd_device *idev)
 }
 
 int iommufd_dev_attach_handle(struct iommufd_hw_pagetable *hwpt,
-			      struct iommufd_device *idev)
+			      struct iommufd_device *idev,
+			      ioasid_t pasid)
 {
 	struct iommufd_attach_handle *handle;
 	int ret;
@@ -327,6 +329,7 @@ int iommufd_dev_attach_handle(struct iommufd_hw_pagetable *hwpt,
 		return -ENOMEM;
 
 	handle->idev = idev;
+	WARN_ON(pasid != IOMMU_NO_PASID);
 	ret = iommu_attach_group_handle(hwpt->domain, idev->igroup->group,
 					&handle->handle);
 	if (ret)
@@ -336,6 +339,7 @@ int iommufd_dev_attach_handle(struct iommufd_hw_pagetable *hwpt,
 }
 
 int iommufd_dev_replace_handle(struct iommufd_device *idev,
+			       ioasid_t pasid,
 			       struct iommufd_hw_pagetable *hwpt,
 			       struct iommufd_hw_pagetable *old)
 {
@@ -347,6 +351,7 @@ int iommufd_dev_replace_handle(struct iommufd_device *idev,
 		return -ENOMEM;
 
 	handle->idev = idev;
+	WARN_ON(pasid != IOMMU_NO_PASID);
 	ret = iommu_replace_group_handle(idev->igroup->group,
 					 hwpt->domain, &handle->handle);
 	if (ret)
@@ -415,7 +420,8 @@ iommufd_device_attach_reserved_iova(struct iommufd_device *idev,
 }
 
 int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt,
-				struct iommufd_device *idev)
+				struct iommufd_device *idev,
+				ioasid_t pasid)
 {
 	struct iommufd_hwpt_paging *hwpt_paging = find_hwpt_paging(hwpt);
 	int rc;
@@ -441,7 +447,7 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt,
 	 * attachment.
 	 */
 	if (list_empty(&idev->igroup->device_list)) {
-		rc = iommufd_hwpt_attach_device(hwpt, idev);
+		rc = iommufd_hwpt_attach_device(hwpt, idev, pasid);
 		if (rc)
 			goto err_unresv;
 		idev->igroup->hwpt = hwpt;
@@ -459,7 +465,7 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt,
 }
 
 struct iommufd_hw_pagetable *
-iommufd_hw_pagetable_detach(struct iommufd_device *idev)
+iommufd_hw_pagetable_detach(struct iommufd_device *idev, ioasid_t pasid)
 {
 	struct iommufd_hw_pagetable *hwpt = idev->igroup->hwpt;
 	struct iommufd_hwpt_paging *hwpt_paging = find_hwpt_paging(hwpt);
@@ -467,7 +473,7 @@ 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)) {
-		iommufd_hwpt_detach_device(hwpt, idev);
+		iommufd_hwpt_detach_device(hwpt, idev, pasid);
 		idev->igroup->hwpt = NULL;
 	}
 	if (hwpt_paging)
@@ -479,12 +485,12 @@ iommufd_hw_pagetable_detach(struct iommufd_device *idev)
 }
 
 static struct iommufd_hw_pagetable *
-iommufd_device_do_attach(struct iommufd_device *idev,
+iommufd_device_do_attach(struct iommufd_device *idev, ioasid_t pasid,
 			 struct iommufd_hw_pagetable *hwpt)
 {
 	int rc;
 
-	rc = iommufd_hw_pagetable_attach(hwpt, idev);
+	rc = iommufd_hw_pagetable_attach(hwpt, idev, pasid);
 	if (rc)
 		return ERR_PTR(rc);
 	return NULL;
@@ -533,7 +539,7 @@ iommufd_group_do_replace_reserved_iova(struct iommufd_group *igroup,
 }
 
 static struct iommufd_hw_pagetable *
-iommufd_device_do_replace(struct iommufd_device *idev,
+iommufd_device_do_replace(struct iommufd_device *idev, ioasid_t pasid,
 			  struct iommufd_hw_pagetable *hwpt)
 {
 	struct iommufd_hwpt_paging *hwpt_paging = find_hwpt_paging(hwpt);
@@ -562,7 +568,7 @@ iommufd_device_do_replace(struct iommufd_device *idev,
 			goto err_unlock;
 	}
 
-	rc = iommufd_hwpt_replace_device(idev, hwpt, old_hwpt);
+	rc = iommufd_hwpt_replace_device(idev, pasid, hwpt, old_hwpt);
 	if (rc)
 		goto err_unresv;
 
@@ -595,7 +601,8 @@ iommufd_device_do_replace(struct iommufd_device *idev,
 }
 
 typedef struct iommufd_hw_pagetable *(*attach_fn)(
-	struct iommufd_device *idev, struct iommufd_hw_pagetable *hwpt);
+			struct iommufd_device *idev, ioasid_t pasid,
+			struct iommufd_hw_pagetable *hwpt);
 
 /*
  * When automatically managing the domains we search for a compatible domain in
@@ -603,7 +610,7 @@ typedef struct iommufd_hw_pagetable *(*attach_fn)(
  * Automatic domain selection will never pick a manually created domain.
  */
 static struct iommufd_hw_pagetable *
-iommufd_device_auto_get_domain(struct iommufd_device *idev,
+iommufd_device_auto_get_domain(struct iommufd_device *idev, ioasid_t pasid,
 			       struct iommufd_ioas *ioas, u32 *pt_id,
 			       attach_fn do_attach)
 {
@@ -632,7 +639,7 @@ iommufd_device_auto_get_domain(struct iommufd_device *idev,
 		hwpt = &hwpt_paging->common;
 		if (!iommufd_lock_obj(&hwpt->obj))
 			continue;
-		destroy_hwpt = (*do_attach)(idev, hwpt);
+		destroy_hwpt = (*do_attach)(idev, pasid, hwpt);
 		if (IS_ERR(destroy_hwpt)) {
 			iommufd_put_object(idev->ictx, &hwpt->obj);
 			/*
@@ -659,7 +666,7 @@ iommufd_device_auto_get_domain(struct iommufd_device *idev,
 	hwpt = &hwpt_paging->common;
 
 	if (!immediate_attach) {
-		destroy_hwpt = (*do_attach)(idev, hwpt);
+		destroy_hwpt = (*do_attach)(idev, pasid, hwpt);
 		if (IS_ERR(destroy_hwpt))
 			goto out_abort;
 	} else {
@@ -680,8 +687,9 @@ iommufd_device_auto_get_domain(struct iommufd_device *idev,
 	return destroy_hwpt;
 }
 
-static int iommufd_device_change_pt(struct iommufd_device *idev, u32 *pt_id,
-				    attach_fn do_attach)
+static int iommufd_device_change_pt(struct iommufd_device *idev,
+				    ioasid_t pasid,
+				    u32 *pt_id, attach_fn do_attach)
 {
 	struct iommufd_hw_pagetable *destroy_hwpt;
 	struct iommufd_object *pt_obj;
@@ -696,7 +704,7 @@ static int iommufd_device_change_pt(struct iommufd_device *idev, u32 *pt_id,
 		struct iommufd_hw_pagetable *hwpt =
 			container_of(pt_obj, struct iommufd_hw_pagetable, obj);
 
-		destroy_hwpt = (*do_attach)(idev, hwpt);
+		destroy_hwpt = (*do_attach)(idev, pasid, hwpt);
 		if (IS_ERR(destroy_hwpt))
 			goto out_put_pt_obj;
 		break;
@@ -705,8 +713,8 @@ static int iommufd_device_change_pt(struct iommufd_device *idev, u32 *pt_id,
 		struct iommufd_ioas *ioas =
 			container_of(pt_obj, struct iommufd_ioas, obj);
 
-		destroy_hwpt = iommufd_device_auto_get_domain(idev, ioas, pt_id,
-							      do_attach);
+		destroy_hwpt = iommufd_device_auto_get_domain(idev, pasid, ioas,
+							      pt_id, do_attach);
 		if (IS_ERR(destroy_hwpt))
 			goto out_put_pt_obj;
 		break;
@@ -743,7 +751,8 @@ int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id)
 {
 	int rc;
 
-	rc = iommufd_device_change_pt(idev, pt_id, &iommufd_device_do_attach);
+	rc = iommufd_device_change_pt(idev, IOMMU_NO_PASID, pt_id,
+				      &iommufd_device_do_attach);
 	if (rc)
 		return rc;
 
@@ -773,7 +782,7 @@ EXPORT_SYMBOL_NS_GPL(iommufd_device_attach, "IOMMUFD");
  */
 int iommufd_device_replace(struct iommufd_device *idev, u32 *pt_id)
 {
-	return iommufd_device_change_pt(idev, pt_id,
+	return iommufd_device_change_pt(idev, IOMMU_NO_PASID, pt_id,
 					&iommufd_device_do_replace);
 }
 EXPORT_SYMBOL_NS_GPL(iommufd_device_replace, "IOMMUFD");
@@ -789,7 +798,7 @@ void iommufd_device_detach(struct iommufd_device *idev)
 {
 	struct iommufd_hw_pagetable *hwpt;
 
-	hwpt = iommufd_hw_pagetable_detach(idev);
+	hwpt = iommufd_hw_pagetable_detach(idev, IOMMU_NO_PASID);
 	iommufd_hw_pagetable_put(idev->ictx, hwpt);
 	refcount_dec(&idev->obj.users);
 }
drivers/iommu/iommufd/fault.c +10 -6
index 19f6e2b84274..99d58df8f6db 100644
@@ -61,7 +61,8 @@ static void iommufd_fault_iopf_disable(struct iommufd_device *idev)
 }
 
 int iommufd_fault_domain_attach_dev(struct iommufd_hw_pagetable *hwpt,
-				    struct iommufd_device *idev)
+				    struct iommufd_device *idev,
+				    ioasid_t pasid)
 {
 	int ret;
 
@@ -72,7 +73,7 @@ int iommufd_fault_domain_attach_dev(struct iommufd_hw_pagetable *hwpt,
 	if (ret)
 		return ret;
 
-	ret = iommufd_dev_attach_handle(hwpt, idev);
+	ret = iommufd_dev_attach_handle(hwpt, idev, pasid);
 	if (ret)
 		iommufd_fault_iopf_disable(idev);
 
@@ -109,11 +110,13 @@ static void iommufd_auto_response_faults(struct iommufd_hw_pagetable *hwpt,
 }
 
 void iommufd_fault_domain_detach_dev(struct iommufd_hw_pagetable *hwpt,
-				     struct iommufd_device *idev)
+				     struct iommufd_device *idev,
+				     ioasid_t pasid)
 {
 	struct iommufd_attach_handle *handle;
 
-	handle = iommufd_device_get_attach_handle(idev);
+	handle = iommufd_device_get_attach_handle(idev, pasid);
+	WARN_ON(pasid != IOMMU_NO_PASID);
 	iommu_detach_group_handle(hwpt->domain, idev->igroup->group);
 	iommufd_auto_response_faults(hwpt, handle);
 	iommufd_fault_iopf_disable(idev);
@@ -121,6 +124,7 @@ void iommufd_fault_domain_detach_dev(struct iommufd_hw_pagetable *hwpt,
 }
 
 int iommufd_fault_domain_replace_dev(struct iommufd_device *idev,
+				     ioasid_t pasid,
 				     struct iommufd_hw_pagetable *hwpt,
 				     struct iommufd_hw_pagetable *old)
 {
@@ -135,9 +139,9 @@ int iommufd_fault_domain_replace_dev(struct iommufd_device *idev,
 			return ret;
 	}
 
-	curr = iommufd_device_get_attach_handle(idev);
+	curr = iommufd_device_get_attach_handle(idev, pasid);
 
-	ret = iommufd_dev_replace_handle(idev, hwpt, old);
+	ret = iommufd_dev_replace_handle(idev, pasid, hwpt, old);
 	if (ret) {
 		if (iopf_on)
 			iommufd_fault_iopf_disable(idev);
drivers/iommu/iommufd/hw_pagetable.c +3 -2
index 598be26a14e2..af2b72647d5a 100644
@@ -184,7 +184,8 @@ iommufd_hwpt_paging_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas,
 	 * sequence. Once those drivers are fixed this should be removed.
 	 */
 	if (immediate_attach) {
-		rc = iommufd_hw_pagetable_attach(hwpt, idev);
+		/* Sinc this is just a trick, so passing IOMMU_NO_PASID is enough */
+		rc = iommufd_hw_pagetable_attach(hwpt, idev, IOMMU_NO_PASID);
 		if (rc)
 			goto out_abort;
 	}
@@ -197,7 +198,7 @@ iommufd_hwpt_paging_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas,
 
 out_detach:
 	if (immediate_attach)
-		iommufd_hw_pagetable_detach(idev);
+		iommufd_hw_pagetable_detach(idev, IOMMU_NO_PASID);
 out_abort:
 	iommufd_object_abort_and_destroy(ictx, &hwpt->obj);
 	return ERR_PTR(rc);
drivers/iommu/iommufd/iommufd_private.h +26 -15

94 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#28 lore raw

RE: [PATCH v6 05/14] iommufd: Pass pasid through the device attach/replace path

drivers/iommu/iommufd/fault.c static void iommufd_auto_response_faults(struct iommufd_hw_pagetable *hwpt, · line 119

Quoted 13 lines

From: Liu, Yi L <[email protected]>
Sent: Thursday, December 19, 2024 9:28 PM
void iommufd_fault_domain_detach_dev(struct iommufd_hw_pagetable
*hwpt,

drivers/iommu/iommufd/fault.c +4 -2
-				     struct iommufd_device *idev)
+				     struct iommufd_device *idev,
+				     ioasid_t pasid)
 {
 	struct iommufd_attach_handle *handle;
 
-	handle = iommufd_device_get_attach_handle(idev);
+	handle = iommufd_device_get_attach_handle(idev, pasid);
+	WARN_ON(pasid != IOMMU_NO_PASID);

Nit: usually we warn before using it, though this will soon be deleted by following patch.

Quoted 10 lines
drivers/iommu/iommufd/hw_pagetable.c +1 -1
@@ -184,7 +184,8 @@ iommufd_hwpt_paging_alloc(struct iommufd_ctx
 *ictx, struct iommufd_ioas *ioas,
 	 * sequence. Once those drivers are fixed this should be removed.
 	 */
 	if (immediate_attach) {
-		rc = iommufd_hw_pagetable_attach(hwpt, idev);
+		/* Sinc this is just a trick, so passing IOMMU_NO_PASID is

enough */

(quoted diff) +1 -0
+		rc = iommufd_hw_pagetable_attach(hwpt, idev,

IOMMU_NO_PASID);

Not sure this comment helps. The ALLOC uAPI is defined against a device, not device+pasid. So it's implied to use IOMMU_NO_PASID. The said trick doesn't matter here.

While at it, @Jason, is immediate_attach still required now?

maintainer
#29 lore raw

Re: [PATCH v6 05/14] iommufd: Pass pasid through the device attach/replace path

Quoted 1 line · Kevin

While at it, @Jason, is immediate_attach still required now?

Sadly yes.

virtio-iommu hasn't been converted to domain_alloc_paging() yet and it has a domain finalize function called in attach. I think this is easy to fix.

arm-smmu-v2 is converted, but it still has the finalize function during attach only (I failed to fix this).

mtk_iommu and mtk_iommu_v1 both have "domain finalise" functions by simple grep.

Possibly more issues in the embedded drivers.

However the server drivers (smmuv3, intel, amd, riscv) are all now OK without it.

Jason

maintainer
#30 lore raw

RE: [PATCH v6 05/14] iommufd: Pass pasid through the device attach/replace path

Quoted 16 lines

From: Jason Gunthorpe <[email protected]>
Sent: Thursday, January 9, 2025 10:51 PM

Quoted 1 line · Kevin

While at it, @Jason, is immediate_attach still required now?

Sadly yes.

virtio-iommu hasn't been converted to domain_alloc_paging() yet and it has a domain finalize function called in attach. I think this is easy to fix.

arm-smmu-v2 is converted, but it still has the finalize function during attach only (I failed to fix this).

Quoted 8 lines

mtk_iommu and mtk_iommu_v1 both have "domain finalise" functions by simple grep.

Possibly more issues in the embedded drivers.

However the server drivers (smmuv3, intel, amd, riscv) are all now OK without it.

Thanks for the info. Looks it has to stay there for a longer time.

author
#31 lore raw

PATCH v6 6/14 iommufd: Mark PASID-compatible domain

In reply to Yi Liu

AMD IOMMU requires attaching PASID-compatible domains to PASID-capable devices. This includes the domains attached to RID and PASIDs. Related discussions in link [1] and [2]. ARM has similar requirement but does not need extra hint from iommufd, Intel does not have this requirement but can live up with it. Hence, iommufd is going to enforce this requirement as it's general requirement. Mark the PASID-capable domains to prepare for adding this enforcement when PASID support is added.

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

---
 drivers/iommu/iommufd/hw_pagetable.c    | 3 +++
 drivers/iommu/iommufd/iommufd_private.h | 1 +
 2 files changed, 4 insertions(+)
drivers/iommu/iommufd/hw_pagetable.c +3 -0
index af2b72647d5a..6fc848d3ef47 100644
@@ -132,6 +132,7 @@ iommufd_hwpt_paging_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas,
 	if (IS_ERR(hwpt_paging))
 		return ERR_CAST(hwpt_paging);
 	hwpt = &hwpt_paging->common;
+	hwpt->pasid_compat = flags & IOMMU_HWPT_ALLOC_PASID;
 
 	INIT_LIST_HEAD(&hwpt_paging->hwpt_item);
 	/* Pairs with iommufd_hw_pagetable_destroy() */
@@ -239,6 +240,7 @@ iommufd_hwpt_nested_alloc(struct iommufd_ctx *ictx,
 	if (IS_ERR(hwpt_nested))
 		return ERR_CAST(hwpt_nested);
 	hwpt = &hwpt_nested->common;
+	hwpt->pasid_compat = flags & IOMMU_HWPT_ALLOC_PASID;
 
 	refcount_inc(&parent->common.obj.users);
 	hwpt_nested->parent = parent;
@@ -293,6 +295,7 @@ iommufd_viommu_alloc_hwpt_nested(struct iommufd_viommu *viommu, u32 flags,
 	if (IS_ERR(hwpt_nested))
 		return ERR_CAST(hwpt_nested);
 	hwpt = &hwpt_nested->common;
+	hwpt->pasid_compat = flags & IOMMU_HWPT_ALLOC_PASID;
 
 	hwpt_nested->viommu = viommu;
 	refcount_inc(&viommu->obj.users);
drivers/iommu/iommufd/iommufd_private.h +1 -0
index 6d4e743ea8fe..21d33a784193 100644
@@ -276,6 +276,7 @@ struct iommufd_hw_pagetable {
 	struct iommufd_object obj;
 	struct iommu_domain *domain;
 	struct iommufd_fault *fault;
+	bool pasid_compat : 1;
 };
 
 struct iommufd_hwpt_paging {
Signature · 1 line
2.34.1
maintainer Reviewed-by on v6 6/14
#32 lore raw

RE: [PATCH v6 06/14] iommufd: Mark PASID-compatible domain

Quoted 10 lines

From: Liu, Yi L <[email protected]>
Sent: Thursday, December 19, 2024 9:28 PM

AMD IOMMU requires attaching PASID-compatible domains to PASID- capable
devices. This includes the domains attached to RID and PASIDs. Related discussions in link [1] and [2]. ARM has similar requirement but does not need extra hint from iommufd, Intel does not have this requirement but can live up with it. Hence, iommufd is going to enforce this requirement as it's general requirement.

Clearly general requirement is not a reason from the description. Instead we just want to lift it to be a general requirement as doing so is not harmful to other vendors.

Quoted 9 lines

Mark the PASID-capable domains
to prepare for adding this enforcement when PASID support is added.

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

maintainer
#33 lore raw

Re: [PATCH v6 06/14] iommufd: Mark PASID-compatible domain

In reply to Yi Liu
Quoted 4 lines · Yi Liu

AMD IOMMU requires attaching PASID-compatible domains to PASID-capable devices. This includes the domains attached to RID and PASIDs. Related discussions in link [1] and [2]. ARM has similar requirement but does not need extra hint from iommufd,

ARM does use the hint now, exactly the same as AMD, it is merged:

https://lore.kernel.org/all/[email protected]/

Jason

author
#34 lore raw

Re: [PATCH v6 06/14] iommufd: Mark PASID-compatible domain

Quoted 9 lines · Jason Gunthorpe
Quoted 4 lines · Yi Liu

AMD IOMMU requires attaching PASID-compatible domains to PASID-capable devices. This includes the domains attached to RID and PASIDs. Related discussions in link [1] and [2]. ARM has similar requirement but does not need extra hint from iommufd,

ARM does use the hint now, exactly the same as AMD, it is merged:

https://lore.kernel.org/all/[email protected]/

got it. Then I will mention both AMD and ARM have such a requirement, while Intel just treat it as nop.

Signature · 2 lines
Regards,
Yi Liu
author
#35 lore raw

PATCH v6 7/14 iommufd: Support pasid attach/replace

In reply to Yi Liu

This introduces three APIs for device drivers to manage pasid attach/ replace/detach.

int iommufd_device_pasid_attach(struct iommufd_device *idev,
ioasid_t pasid, u32 *pt_id);
int iommufd_device_pasid_replace(struct iommufd_device *idev,
ioasid_t pasid, u32 *pt_id);
void iommufd_device_pasid_detach(struct iommufd_device *idev,
ioasid_t pasid);

pasid operations have different implications when comparing to device operations:

- No connection to iommufd_group since pasid is a device capability and can be enabled only in singleton group;

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

- accordingly no sw_msi trick;

- immediated_attach is not supported, expecting that arm-smmu driver will already remove that requirement before supporting this pasid operation. This avoids unnecessary change in iommufd_hw_pagetable_alloc() to carry the pasid from device.c.

With above differences, this puts all pasid related logics into a new pasid.c file.

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

Since the attach is per PASID, this introduces a pasid_hwpts xarray to track the per-pasid attach data.

AMD requires using PASID-compatible domains for PASIDs, hence the hwpts directing the PASID path requires to flagged with IOMMU_HWPT_ALLOC_PASID.

---
 drivers/iommu/iommufd/Makefile          |   1 +
 drivers/iommu/iommufd/device.c          |  30 +++--
 drivers/iommu/iommufd/fault.c           |   6 +-
 drivers/iommu/iommufd/iommufd_private.h |  27 +++-
 drivers/iommu/iommufd/pasid.c           | 161 ++++++++++++++++++++++++
 include/linux/iommufd.h                 |   7 ++
 6 files changed, 215 insertions(+), 17 deletions(-)
 create mode 100644 drivers/iommu/iommufd/pasid.c
drivers/iommu/iommufd/Makefile +1 -0
index cb784da6cddc..a64a67b502ae 100644
@@ -7,6 +7,7 @@ iommufd-y := \
 	ioas.o \
 	main.o \
 	pages.o \
+	pasid.o \
 	vfio_compat.o \
 	viommu.o
 
drivers/iommu/iommufd/device.c +17 -13
index c22ef4077348..c1ff3dbe109c 100644
@@ -136,6 +136,7 @@ void iommufd_device_destroy(struct iommufd_object *obj)
 	struct iommufd_device *idev =
 		container_of(obj, struct iommufd_device, obj);
 
+	WARN_ON(!xa_empty(&idev->pasid_hwpts));
 	iommu_device_release_dma_owner(idev->dev);
 	iommufd_put_group(idev->igroup);
 	if (!iommufd_selftest_is_mock_dev(idev->dev))
@@ -217,6 +218,8 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx,
 	idev->igroup = igroup;
 	mutex_init(&idev->iopf_lock);
 
+	xa_init(&idev->pasid_hwpts);
+
 	/*
 	 * If the caller fails after this success it must call
 	 * iommufd_unbind_device() which is safe since we hold this refcount.
@@ -329,9 +332,12 @@ int iommufd_dev_attach_handle(struct iommufd_hw_pagetable *hwpt,
 		return -ENOMEM;
 
 	handle->idev = idev;
-	WARN_ON(pasid != IOMMU_NO_PASID);
-	ret = iommu_attach_group_handle(hwpt->domain, idev->igroup->group,
-					&handle->handle);
+	if (pasid == IOMMU_NO_PASID)
+		ret = iommu_attach_group_handle(hwpt->domain, idev->igroup->group,
+						&handle->handle);
+	else
+		ret = iommu_attach_device_pasid(hwpt->domain, idev->dev, pasid,
+						&handle->handle);
 	if (ret)
 		kfree(handle);
 
@@ -351,9 +357,12 @@ int iommufd_dev_replace_handle(struct iommufd_device *idev,
 		return -ENOMEM;
 
 	handle->idev = idev;
-	WARN_ON(pasid != IOMMU_NO_PASID);
-	ret = iommu_replace_group_handle(idev->igroup->group,
-					 hwpt->domain, &handle->handle);
+	if (pasid == IOMMU_NO_PASID)
+		ret = iommu_replace_group_handle(idev->igroup->group,
+						 hwpt->domain, &handle->handle);
+	else
+		ret = iommu_replace_device_pasid(hwpt->domain, idev->dev,
+						 pasid, &handle->handle);
 	if (ret)
 		kfree(handle);
 
@@ -600,10 +609,6 @@ iommufd_device_do_replace(struct iommufd_device *idev, ioasid_t pasid,
 	return ERR_PTR(rc);
 }
 
-typedef struct iommufd_hw_pagetable *(*attach_fn)(
-			struct iommufd_device *idev, ioasid_t pasid,
-			struct iommufd_hw_pagetable *hwpt);
-
 /*
  * When automatically managing the domains we search for a compatible domain in
  * the iopt and if one is found use it, otherwise create a new domain.
@@ -687,9 +692,8 @@ iommufd_device_auto_get_domain(struct iommufd_device *idev, ioasid_t pasid,
 	return destroy_hwpt;
 }
 
-static int iommufd_device_change_pt(struct iommufd_device *idev,
-				    ioasid_t pasid,
-				    u32 *pt_id, attach_fn do_attach)
+int iommufd_device_change_pt(struct iommufd_device *idev, ioasid_t pasid,
+			     u32 *pt_id, attach_fn do_attach)
 {
 	struct iommufd_hw_pagetable *destroy_hwpt;
 	struct iommufd_object *pt_obj;
drivers/iommu/iommufd/fault.c +4 -2
index 99d58df8f6db..96ca28ff31f7 100644
@@ -116,8 +116,10 @@ void iommufd_fault_domain_detach_dev(struct iommufd_hw_pagetable *hwpt,
 	struct iommufd_attach_handle *handle;
 
 	handle = iommufd_device_get_attach_handle(idev, pasid);
-	WARN_ON(pasid != IOMMU_NO_PASID);
-	iommu_detach_group_handle(hwpt->domain, idev->igroup->group);
+	if (pasid == IOMMU_NO_PASID)
+		iommu_detach_group_handle(hwpt->domain, idev->igroup->group);
+	else
+		iommu_detach_device_pasid(hwpt->domain, idev->dev, pasid);
 	iommufd_auto_response_faults(hwpt, handle);
 	iommufd_fault_iopf_disable(idev);
 	kfree(handle);
drivers/iommu/iommufd/iommufd_private.h +25 -2
index 21d33a784193..e9d6bd8b44bc 100644
@@ -400,6 +400,7 @@ struct iommufd_device {
 	struct list_head group_item;
 	/* always the physical device */
 	struct device *dev;
+	struct xarray pasid_hwpts;
 	bool enforce_cache_coherency;
 	/* protect iopf_enabled counter */
 	struct mutex iopf_lock;
@@ -417,6 +418,20 @@ iommufd_get_device(struct iommufd_ucmd *ucmd, u32 id)
 void iommufd_device_destroy(struct iommufd_object *obj);
 int iommufd_get_hw_info(struct iommufd_ucmd *ucmd);
 
+typedef struct iommufd_hw_pagetable *(*attach_fn)(
+			struct iommufd_device *idev, ioasid_t pasid,
+			struct iommufd_hw_pagetable *hwpt);
+
+int iommufd_device_change_pt(struct iommufd_device *idev, ioasid_t pasid,
+			     u32 *pt_id, attach_fn do_attach);
+
+struct iommufd_hw_pagetable *
+iommufd_device_pasid_do_attach(struct iommufd_device *idev, ioasid_t pasid,
+			       struct iommufd_hw_pagetable *hwpt);
+struct iommufd_hw_pagetable *
+iommufd_device_pasid_do_replace(struct iommufd_device *idev, ioasid_t pasid,
+				struct iommufd_hw_pagetable *hwpt);
+
 struct iommufd_access {
 	struct iommufd_object obj;
 	struct iommufd_ctx *ictx;
@@ -498,6 +513,9 @@ static inline int iommufd_hwpt_attach_device(struct iommufd_hw_pagetable *hwpt,
 					     struct iommufd_device *idev,
 					     ioasid_t pasid)
 {
+	if (pasid != IOMMU_NO_PASID && !hwpt->pasid_compat)
+		return -EINVAL;
+
 	if (hwpt->fault)
 		return iommufd_fault_domain_attach_dev(hwpt, idev, pasid);
 
@@ -516,8 +534,10 @@ static inline void iommufd_hwpt_detach_device(struct iommufd_hw_pagetable *hwpt,
 	}
 
 	handle = iommufd_device_get_attach_handle(idev, pasid);
-	WARN_ON(pasid != IOMMU_NO_PASID);
-	iommu_detach_group_handle(hwpt->domain, idev->igroup->group);
+	if (pasid == IOMMU_NO_PASID)
+		iommu_detach_group_handle(hwpt->domain, idev->igroup->group);
+	else
+		iommu_detach_device_pasid(hwpt->domain, idev->dev, pasid);
 	kfree(handle);
 }
 
@@ -529,6 +549,9 @@ static inline int iommufd_hwpt_replace_device(struct iommufd_device *idev,
 	struct iommufd_attach_handle *curr;
 	int ret;
 
+	if (pasid != IOMMU_NO_PASID && !hwpt->pasid_compat)
+		return -EINVAL;
+
 	if (old->fault || hwpt->fault)
 		return iommufd_fault_domain_replace_dev(idev, pasid,
 							hwpt, old);
drivers/iommu/iommufd/pasid.c new file +161 -0

161 lines not on this page · raw message

include/linux/iommufd.h +7 -0
index 11110c749200..af7e5a4bfcf2 100644
@@ -8,6 +8,7 @@
 
 #include <linux/err.h>
 #include <linux/errno.h>
+#include <linux/iommu.h>
 #include <linux/refcount.h>
 #include <linux/types.h>
 #include <linux/xarray.h>
@@ -56,6 +57,12 @@ int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id);
 int iommufd_device_replace(struct iommufd_device *idev, u32 *pt_id);
 void iommufd_device_detach(struct iommufd_device *idev);
 
+int iommufd_device_pasid_attach(struct iommufd_device *idev,
+				ioasid_t pasid, u32 *pt_id);
+int iommufd_device_pasid_replace(struct iommufd_device *idev,
+				 ioasid_t pasid, u32 *pt_id);
+void iommufd_device_pasid_detach(struct iommufd_device *idev, ioasid_t pasid);
+
 struct iommufd_ctx *iommufd_device_to_ictx(struct iommufd_device *idev);
 u32 iommufd_device_to_id(struct iommufd_device *idev);
 
Signature · 1 line
2.34.1
maintainer
#36 lore raw

RE: [PATCH v6 07/14] iommufd: Support pasid attach/replace

Quoted 14 lines

From: Liu, Yi L <[email protected]>
Sent: Thursday, December 19, 2024 9:28 PM

(quoted diff) +2 -0
+struct iommufd_hw_pagetable *
+iommufd_device_pasid_do_attach(struct iommufd_device *idev, ioasid_t

pasid,

drivers/iommu/iommufd/pasid.c new file +7 -0
+			       struct iommufd_hw_pagetable *hwpt)
+{
+	void *curr;
+	int rc;
+
+	refcount_inc(&hwpt->obj.users);
+	curr = xa_cmpxchg(&idev->pasid_hwpts, pasid, NULL, hwpt,

GFP_KERNEL);

drivers/iommu/iommufd/pasid.c line 25

Quoted 7 lines
drivers/iommu/iommufd/pasid.c new file +7 -0
+	if (curr) {
+		if (curr == hwpt)
+			rc = 0;
+		else
+			rc = xa_err(curr) ? : -EINVAL;
+		goto err_put_hwpt;
+	}

Refcount can be incremented at the end when the operation succeeds then no need to recover it in the err unwind and here direct returns.

author
#37 lore raw

PATCH v6 8/14 iommufd: Enforce PASID-compatible domain for RID

In reply to Yi Liu

Per the definition of IOMMU_HWPT_ALLOC_PASID, iommufd needs to enforce the RID to use PASID-compatible domain if PASID has been attached.

This enforcement requires a lock across the RID and PASID attach path, use the idev->igroup->lock for this sync.

---
 drivers/iommu/iommufd/iommufd_private.h | 18 ++++++++++++++++--
 drivers/iommu/iommufd/pasid.c           | 14 +++++++++++++-
 2 files changed, 29 insertions(+), 3 deletions(-)
drivers/iommu/iommufd/iommufd_private.h +16 -2
index e9d6bd8b44bc..158d8e6d5a9a 100644
@@ -513,7 +513,14 @@ static inline int iommufd_hwpt_attach_device(struct iommufd_hw_pagetable *hwpt,
 					     struct iommufd_device *idev,
 					     ioasid_t pasid)
 {
-	if (pasid != IOMMU_NO_PASID && !hwpt->pasid_compat)
+	lockdep_assert_held(&idev->igroup->lock);
+
+	if (pasid == IOMMU_NO_PASID &&
+	    !xa_empty(&idev->pasid_hwpts) && !hwpt->pasid_compat)
+		return -EINVAL;
+
+	if (pasid != IOMMU_NO_PASID &&
+	    (!idev->igroup->hwpt->pasid_compat || !hwpt->pasid_compat))
 		return -EINVAL;
 
 	if (hwpt->fault)
@@ -549,7 +556,14 @@ static inline int iommufd_hwpt_replace_device(struct iommufd_device *idev,
 	struct iommufd_attach_handle *curr;
 	int ret;
 
-	if (pasid != IOMMU_NO_PASID && !hwpt->pasid_compat)
+	lockdep_assert_held(&idev->igroup->lock);
+
+	if (pasid == IOMMU_NO_PASID &&
+	    !xa_empty(&idev->pasid_hwpts) && !hwpt->pasid_compat)
+		return -EINVAL;
+
+	if (pasid != IOMMU_NO_PASID &&
+	    (!idev->igroup->hwpt->pasid_compat || !hwpt->pasid_compat))
 		return -EINVAL;
 
 	if (old->fault || hwpt->fault)
drivers/iommu/iommufd/pasid.c +13 -1
index fcdfbc01dcbb..fdf97f1d71ae 100644
@@ -15,6 +15,8 @@ iommufd_device_pasid_do_attach(struct iommufd_device *idev, ioasid_t pasid,
 	int rc;
 
 	refcount_inc(&hwpt->obj.users);
+
+	mutex_lock(&idev->igroup->lock);
 	curr = xa_cmpxchg(&idev->pasid_hwpts, pasid, NULL, hwpt, GFP_KERNEL);
 	if (curr) {
 		if (curr == hwpt)
@@ -30,9 +32,11 @@ iommufd_device_pasid_do_attach(struct iommufd_device *idev, ioasid_t pasid,
 		goto err_put_hwpt;
 	}
 
+	mutex_unlock(&idev->igroup->lock);
 	return NULL;
 
 err_put_hwpt:
+	mutex_unlock(&idev->igroup->lock);
 	refcount_dec(&hwpt->obj.users);
 	return rc ? ERR_PTR(rc) : NULL;
 }
@@ -45,6 +49,8 @@ iommufd_device_pasid_do_replace(struct iommufd_device *idev, ioasid_t pasid,
 	int rc;
 
 	refcount_inc(&hwpt->obj.users);
+
+	mutex_lock(&idev->igroup->lock);
 	curr = xa_store(&idev->pasid_hwpts, pasid, hwpt, GFP_KERNEL);
 	rc = xa_err(curr);
 	if (rc)
@@ -70,10 +76,12 @@ iommufd_device_pasid_do_replace(struct iommufd_device *idev, ioasid_t pasid,
 		goto out_put_hwpt;
 	}
 
+	mutex_unlock(&idev->igroup->lock);
 	/* Caller must destroy old_hwpt */
 	return curr;
 
 out_put_hwpt:
+	mutex_unlock(&idev->igroup->lock);
 	refcount_dec(&hwpt->obj.users);
 	return rc ? ERR_PTR(rc) : NULL;
 }
@@ -152,10 +160,14 @@ void iommufd_device_pasid_detach(struct iommufd_device *idev, ioasid_t pasid)
 {
 	struct iommufd_hw_pagetable *hwpt;
 
+	mutex_lock(&idev->igroup->lock);
 	hwpt = xa_erase(&idev->pasid_hwpts, pasid);
-	if (WARN_ON(!hwpt))
+	if (WARN_ON(!hwpt)) {
+		mutex_unlock(&idev->igroup->lock);
 		return;
+	}
 	iommufd_hwpt_detach_device(hwpt, idev, pasid);
+	mutex_unlock(&idev->igroup->lock);
 	iommufd_hw_pagetable_put(idev->ictx, hwpt);
 }
 EXPORT_SYMBOL_NS_GPL(iommufd_device_pasid_detach, "IOMMUFD");
Signature · 1 line
2.34.1
maintainer
#38 lore raw

RE: [PATCH v6 08/14] iommufd: Enforce PASID-compatible domain for RID

Quoted 6 lines

From: Liu, Yi L <[email protected]>
Sent: Thursday, December 19, 2024 9:28 PM

Per the definition of IOMMU_HWPT_ALLOC_PASID, iommufd needs to enforce
the RID to use PASID-compatible domain if PASID has been attached.

And vice versa.

Quoted 37 lines

This enforcement requires a lock across the RID and PASID attach path, use the idev->igroup->lock for this sync.

---
 drivers/iommu/iommufd/iommufd_private.h | 18 ++++++++++++++++--
 drivers/iommu/iommufd/pasid.c           | 14 +++++++++++++-
 2 files changed, 29 insertions(+), 3 deletions(-)
drivers/iommu/iommufd/iommufd_private.h +8 -1
index e9d6bd8b44bc..158d8e6d5a9a 100644
@@ -513,7 +513,14 @@ static inline int iommufd_hwpt_attach_device(struct
 iommufd_hw_pagetable *hwpt,
 					     struct iommufd_device *idev,
 					     ioasid_t pasid)
 {
-	if (pasid != IOMMU_NO_PASID && !hwpt->pasid_compat)
+	lockdep_assert_held(&idev->igroup->lock);
+
+	if (pasid == IOMMU_NO_PASID &&
+	    !xa_empty(&idev->pasid_hwpts) && !hwpt->pasid_compat)
+		return -EINVAL;
+
+	if (pasid != IOMMU_NO_PASID &&
+	    (!idev->igroup->hwpt->pasid_compat || !hwpt->pasid_compat))
 		return -EINVAL;
 
drivers/iommu/iommufd/iommufd_private.h +0 -0
 	if (hwpt->fault)
@@ -549,7 +556,14 @@ static inline int
 iommufd_hwpt_replace_device(struct iommufd_device *idev,
 	struct iommufd_attach_handle *curr;
 	int ret;
 

drivers/iommu/iommufd/iommufd_private.h static inline int · line 568

Quoted 10 lines
drivers/iommu/iommufd/iommufd_private.h +8 -1
-	if (pasid != IOMMU_NO_PASID && !hwpt->pasid_compat)
+	lockdep_assert_held(&idev->igroup->lock);
+
+	if (pasid == IOMMU_NO_PASID &&
+	    !xa_empty(&idev->pasid_hwpts) && !hwpt->pasid_compat)
+		return -EINVAL;
+
+	if (pasid != IOMMU_NO_PASID &&
+	    (!idev->igroup->hwpt->pasid_compat || !hwpt->pasid_compat))
 		return -EINVAL;

Make above check into a helper as both attach/replace require it.

Quoted 74 lines
drivers/iommu/iommufd/iommufd_private.h +0 -0
 	if (old->fault || hwpt->fault)
drivers/iommu/iommufd/pasid.c +2 -0
index fcdfbc01dcbb..fdf97f1d71ae 100644
@@ -15,6 +15,8 @@ iommufd_device_pasid_do_attach(struct
 iommufd_device *idev, ioasid_t pasid,
 	int rc;
 
 	refcount_inc(&hwpt->obj.users);
+
+	mutex_lock(&idev->igroup->lock);
 	curr = xa_cmpxchg(&idev->pasid_hwpts, pasid, NULL, hwpt,
 GFP_KERNEL);
drivers/iommu/iommufd/pasid.c +2 -0
 	if (curr) {
 		if (curr == hwpt)
@@ -30,9 +32,11 @@ iommufd_device_pasid_do_attach(struct
 iommufd_device *idev, ioasid_t pasid,
 		goto err_put_hwpt;
 	}
 
+	mutex_unlock(&idev->igroup->lock);
 	return NULL;
 
 err_put_hwpt:
+	mutex_unlock(&idev->igroup->lock);
 	refcount_dec(&hwpt->obj.users);
 	return rc ? ERR_PTR(rc) : NULL;

}

drivers/iommu/iommufd/pasid.c +2 -0
@@ -45,6 +49,8 @@ iommufd_device_pasid_do_replace(struct
 iommufd_device *idev, ioasid_t pasid,
 	int rc;
 
 	refcount_inc(&hwpt->obj.users);
+
+	mutex_lock(&idev->igroup->lock);
 	curr = xa_store(&idev->pasid_hwpts, pasid, hwpt, GFP_KERNEL);
 	rc = xa_err(curr);
drivers/iommu/iommufd/pasid.c +2 -0
 	if (rc)
@@ -70,10 +76,12 @@ iommufd_device_pasid_do_replace(struct
 iommufd_device *idev, ioasid_t pasid,
 		goto out_put_hwpt;
 	}
 
+	mutex_unlock(&idev->igroup->lock);
 	/* Caller must destroy old_hwpt */
 	return curr;
 
 out_put_hwpt:
+	mutex_unlock(&idev->igroup->lock);
 	refcount_dec(&hwpt->obj.users);
 	return rc ? ERR_PTR(rc) : NULL;

}

drivers/iommu/iommufd/pasid.c +5 -1
@@ -152,10 +160,14 @@ void iommufd_device_pasid_detach(struct
 iommufd_device *idev, ioasid_t pasid)
 {
 	struct iommufd_hw_pagetable *hwpt;
 
+	mutex_lock(&idev->igroup->lock);
 	hwpt = xa_erase(&idev->pasid_hwpts, pasid);
-	if (WARN_ON(!hwpt))
+	if (WARN_ON(!hwpt)) {
+		mutex_unlock(&idev->igroup->lock);
 		return;
+	}
 	iommufd_hwpt_detach_device(hwpt, idev, pasid);
+	mutex_unlock(&idev->igroup->lock);
 	iommufd_hw_pagetable_put(idev->ictx, hwpt);
 }

EXPORT_SYMBOL_NS_GPL(iommufd_device_pasid_detach, "IOMMUFD");
--
2.34.1

author
#39 lore raw

PATCH v6 9/14 iommu/vt-d: Add IOMMU_HWPT_ALLOC_PASID support

In reply to Yi Liu

Intel iommu driver just treats it as a nop since Intel VT-d does not have special requirement on domains attached to either the PASID or RID of a PASID-capable device.

---
 drivers/iommu/intel/iommu.c  | 3 ++-
 drivers/iommu/intel/nested.c | 2 +-
 2 files changed, 3 insertions(+), 2 deletions(-)
drivers/iommu/intel/iommu.c +2 -1
index cd5e339fd5bb..0a622a89d876 100644
@@ -3347,7 +3347,8 @@ intel_iommu_domain_alloc_paging_flags(struct device *dev, u32 flags,
 	bool first_stage;
 
 	if (flags &
-	    (~(IOMMU_HWPT_ALLOC_NEST_PARENT | IOMMU_HWPT_ALLOC_DIRTY_TRACKING)))
+	    (~(IOMMU_HWPT_ALLOC_NEST_PARENT | IOMMU_HWPT_ALLOC_DIRTY_TRACKING |
+	       IOMMU_HWPT_ALLOC_PASID)))
 		return ERR_PTR(-EOPNOTSUPP);
 	if (nested_parent && !nested_supported(iommu))
 		return ERR_PTR(-EOPNOTSUPP);
drivers/iommu/intel/nested.c +1 -1
index aba92c00b427..6ac5c534bef4 100644
@@ -198,7 +198,7 @@ intel_iommu_domain_alloc_nested(struct device *dev, struct iommu_domain *parent,
 	struct dmar_domain *domain;
 	int ret;
 
-	if (!nested_supported(iommu) || flags)
+	if (!nested_supported(iommu) || flags & ~IOMMU_HWPT_ALLOC_PASID)
 		return ERR_PTR(-EOPNOTSUPP);
 
 	/* Must be nested domain */
Signature · 1 line
2.34.1
maintainer
#40 lore raw

Re: [PATCH v6 09/14] iommu/vt-d: Add IOMMU_HWPT_ALLOC_PASID support

Quoted 29 lines · Yi Liu

Intel iommu driver just treats it as a nop since Intel VT-d does not have special requirement on domains attached to either the PASID or RID of a PASID-capable device.

---
  drivers/iommu/intel/iommu.c  | 3 ++-
  drivers/iommu/intel/nested.c | 2 +-
  2 files changed, 3 insertions(+), 2 deletions(-)
drivers/iommu/intel/iommu.c +2 -1
index cd5e339fd5bb..0a622a89d876 100644
@@ -3347,7 +3347,8 @@ intel_iommu_domain_alloc_paging_flags(struct device *dev, u32 flags,
  	bool first_stage;
 
  	if (flags &
-	    (~(IOMMU_HWPT_ALLOC_NEST_PARENT | IOMMU_HWPT_ALLOC_DIRTY_TRACKING)))
+	    (~(IOMMU_HWPT_ALLOC_NEST_PARENT | IOMMU_HWPT_ALLOC_DIRTY_TRACKING |
+	       IOMMU_HWPT_ALLOC_PASID)))
  		return ERR_PTR(-EOPNOTSUPP);
  	if (nested_parent && !nested_supported(iommu))
  		return ERR_PTR(-EOPNOTSUPP);
drivers/iommu/intel/nested.c +0 -0
index aba92c00b427..6ac5c534bef4 100644

drivers/iommu/intel/nested.c intel_iommu_domain_alloc_nested(struct device *dev, struct iommu_domain *parent, · line 204

Quoted 8 lines
drivers/iommu/intel/nested.c +1 -1
@@ -198,7 +198,7 @@ intel_iommu_domain_alloc_nested(struct device *dev, struct iommu_domain *parent,
  	struct dmar_domain *domain;
  	int ret;
 
-	if (!nested_supported(iommu) || flags)
+	if (!nested_supported(iommu) || flags & ~IOMMU_HWPT_ALLOC_PASID)
  		return ERR_PTR(-EOPNOTSUPP);
 
  	/* Must be nested domain */

It's better to abort and fail a domain allocation when IOMMU_HWPT_ALLOC_PASID is set but the iommu lacks pasid support?

Another related consideration is the support for page faults in nested domains once PASID is available in user space. Would it be reasonable to support page faults for nested domains?

If so, perhaps it's time to open intel_iommu_domain_alloc_nested() to support IOMMU_HWPT_FAULT_ID_VALID when PRI is enabled on the device?

--
baolu

author
#41 lore raw

Re: [PATCH v6 09/14] iommu/vt-d: Add IOMMU_HWPT_ALLOC_PASID support

Quoted 33 lines · Baolu Lu
Quoted 32 lines · Yi Liu

Intel iommu driver just treats it as a nop since Intel VT-d does not have special requirement on domains attached to either the PASID or RID of a PASID-capable device.

---
  drivers/iommu/intel/iommu.c  | 3 ++-
  drivers/iommu/intel/nested.c | 2 +-

  2 files changed, 3 insertions(+), 2 deletions(-)
drivers/iommu/intel/iommu.c +0 -0
index cd5e339fd5bb..0a622a89d876 100644
@@ -3347,7 +3347,8 @@ intel_iommu_domain_alloc_paging_flags(struct device 
 *dev, u32 flags,
       bool first_stage;

      if (flags &
-        (~(IOMMU_HWPT_ALLOC_NEST_PARENT | IOMMU_HWPT_ALLOC_DIRTY_TRACKING)))
+        (~(IOMMU_HWPT_ALLOC_NEST_PARENT | IOMMU_HWPT_ALLOC_DIRTY_TRACKING | +           IOMMU_HWPT_ALLOC_PASID)))
          return ERR_PTR(-EOPNOTSUPP);
      if (nested_parent && !nested_supported(iommu))
          return ERR_PTR(-EOPNOTSUPP);

drivers/iommu/intel/nested.c +0 -0
index aba92c00b427..6ac5c534bef4 100644
@@ -198,7 +198,7 @@ intel_iommu_domain_alloc_nested(struct device *dev, 
 struct iommu_domain *parent,
       struct dmar_domain *domain;
Quoted 8 lines

drivers/iommu/intel/nested.c intel_iommu_domain_alloc_nested(struct device *dev, struct iommu_domain *parent, · line 204

Quoted 5 lines
drivers/iommu/intel/nested.c +1 -1
       int ret;
-    if (!nested_supported(iommu) || flags)
+    if (!nested_supported(iommu) || flags & ~IOMMU_HWPT_ALLOC_PASID)
           return ERR_PTR(-EOPNOTSUPP);
       /* Must be nested domain */

It's better to abort and fail a domain allocation when IOMMU_HWPT_ALLOC_PASID is set but the iommu lacks pasid support?

in concept, yes.

Quoted 3 lines

Another related consideration is the support for page faults in nested domains once PASID is available in user space. Would it be reasonable to support page faults for nested domains?

yeah, it's good to discuss it.

Quoted 2 lines

If so, perhaps it's time to open intel_iommu_domain_alloc_nested() to support IOMMU_HWPT_FAULT_ID_VALID when PRI is enabled on the device?

IMHO, the PRQ support for nested domains requires some more facility. PRQ can happen at either stage-1 or stage-2, iommu driver may need to tell it and forward to the correct domain (nested domain or parent domain). Or the stage-2 is always pinned just as the VFIO/iommufd does. Hence, any PRQ happens under nested translation should be due to stage-1.

Signature · 2 lines
Regards,
Yi Liu
maintainer
#42 lore raw

Re: [PATCH v6 09/14] iommu/vt-d: Add IOMMU_HWPT_ALLOC_PASID support

Quoted 6 lines · Yi Liu
Quoted 3 lines

Another related consideration is the support for page faults in nested domains once PASID is available in user space. Would it be reasonable to support page faults for nested domains?

yeah, it's good to discuss it.

Quoted 8 lines
Quoted 2 lines

If so, perhaps it's time to open intel_iommu_domain_alloc_nested() to support IOMMU_HWPT_FAULT_ID_VALID when PRI is enabled on the device?

IMHO, the PRQ support for nested domains requires some more facility. PRQ can happen at either stage-1 or stage-2, iommu driver may need to tell it and forward to the correct domain (nested domain or parent domain). Or the stage-2 is always pinned just as the VFIO/iommufd does. Hence, any PRQ happens under nested translation should be due to stage-1.

The parent domain is currently always pinned and does not yet support page faults. Therefore, when a page fault occurs within a nested domain, it apparently should be routed to user space...

If we decide to support page faults on the stage-2 domain in the future, we will need to figure out the correct destination of each page fault and route it accordingly, either to the parent domain or the user space nested domain. Hardware assistance would be beneficial, otherwise the software may need to traverse the parent domain, which is not performance friendly.

So, I don't see any reason why the iommu driver shouldn't be able to support iopf-capable nested domains. Have I missed anything?

--
baolu

author
#43 lore raw

Re: [PATCH v6 09/14] iommu/vt-d: Add IOMMU_HWPT_ALLOC_PASID support

Quoted 20 lines · Baolu Lu
Quoted 6 lines · Yi Liu
Quoted 3 lines

Another related consideration is the support for page faults in nested domains once PASID is available in user space. Would it be reasonable to support page faults for nested domains?

yeah, it's good to discuss it.

Quoted 8 lines
Quoted 2 lines

If so, perhaps it's time to open intel_iommu_domain_alloc_nested() to support IOMMU_HWPT_FAULT_ID_VALID when PRI is enabled on the device?

IMHO, the PRQ support for nested domains requires some more facility. PRQ can happen at either stage-1 or stage-2, iommu driver may need to tell it and forward to the correct domain (nested domain or parent domain). Or the stage-2 is always pinned just as the VFIO/iommufd does. Hence, any PRQ happens under nested translation should be due to stage-1.

The parent domain is currently always pinned and does not yet support page faults. Therefore, when a page fault occurs within a nested domain, it apparently should be routed to user space...

Quoted 6 lines

If we decide to support page faults on the stage-2 domain in the future, we will need to figure out the correct destination of each page fault and route it accordingly, either to the parent domain or the user space nested domain. Hardware assistance would be beneficial, otherwise the software may need to traverse the parent domain, which is not performance friendly.

this is my question. Is it still true the parent domain is always pinned after the below series? If yes, then it's fine to enable IOPF for nested domain.

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

Signature · 2 lines
Regards,
Yi Liu
maintainer
#44 lore raw

Re: [PATCH v6 09/14] iommu/vt-d: Add IOMMU_HWPT_ALLOC_PASID support

Quoted 28 lines · Yi Liu
Quoted 20 lines · Baolu Lu
Quoted 6 lines · Yi Liu
Quoted 3 lines

Another related consideration is the support for page faults in nested domains once PASID is available in user space. Would it be reasonable to support page faults for nested domains?

yeah, it's good to discuss it.

Quoted 8 lines
Quoted 2 lines

If so, perhaps it's time to open intel_iommu_domain_alloc_nested() to support IOMMU_HWPT_FAULT_ID_VALID when PRI is enabled on the device?

IMHO, the PRQ support for nested domains requires some more facility. PRQ can happen at either stage-1 or stage-2, iommu driver may need to tell it and forward to the correct domain (nested domain or parent domain). Or the stage-2 is always pinned just as the VFIO/iommufd does. Hence, any PRQ happens under nested translation should be due to stage-1.

The parent domain is currently always pinned and does not yet support page faults. Therefore, when a page fault occurs within a nested domain, it apparently should be routed to user space...

Quoted 6 lines

If we decide to support page faults on the stage-2 domain in the future, we will need to figure out the correct destination of each page fault and route it accordingly, either to the parent domain or the user space nested domain. Hardware assistance would be beneficial, otherwise the software may need to traverse the parent domain, which is not performance friendly.

Quoted 5 lines

this is my question. Is it still true the parent domain is always pinned after the below series? If yes, then it's fine to enable IOPF for nested domain.

https://lore.kernel.org/linux-iommu/20241015-jag-iopfv8-v4-0- [email protected]/

Then, perhaps we could enforce this in iommufd for a short-term purpose?

When allocating a hwpt in iommufd, we should enforce that the flags IOMMU_HWPT_ALLOC_NEST_PARENT and IOMMU_HWPT_FAULT_ID_VALID cannot be set at the same time.

---
baolu

author
#45 lore raw

Re: [PATCH v6 09/14] iommu/vt-d: Add IOMMU_HWPT_ALLOC_PASID support

Quoted 33 lines · Baolu Lu
Quoted 32 lines · Yi Liu
Quoted 20 lines · Baolu Lu
Quoted 6 lines · Yi Liu
Quoted 3 lines

Another related consideration is the support for page faults in nested domains once PASID is available in user space. Would it be reasonable to support page faults for nested domains?

yeah, it's good to discuss it.

Quoted 8 lines
Quoted 2 lines

If so, perhaps it's time to open intel_iommu_domain_alloc_nested() to support IOMMU_HWPT_FAULT_ID_VALID when PRI is enabled on the device?

IMHO, the PRQ support for nested domains requires some more facility. PRQ can happen at either stage-1 or stage-2, iommu driver may need to tell it and forward to the correct domain (nested domain or parent domain). Or the stage-2 is always pinned just as the VFIO/iommufd does. Hence, any PRQ happens under nested translation should be due to stage-1.

The parent domain is currently always pinned and does not yet support page faults. Therefore, when a page fault occurs within a nested domain, it apparently should be routed to user space...

Quoted 6 lines

If we decide to support page faults on the stage-2 domain in the future, we will need to figure out the correct destination of each page fault and route it accordingly, either to the parent domain or the user space nested domain. Hardware assistance would be beneficial, otherwise the software may need to traverse the parent domain, which is not performance friendly.

this is my question. Is it still true the parent domain is always pinned after the below series? If yes, then it's fine to enable IOPF for nested domain.

Quoted 7 lines
Quoted 1 line

https://lore.kernel.org/linux-iommu/20241015-jag-iopfv8-v4-0- [email protected]/

Then, perhaps we could enforce this in iommufd for a short-term purpose?

When allocating a hwpt in iommufd, we should enforce that the flags IOMMU_HWPT_ALLOC_NEST_PARENT and IOMMU_HWPT_FAULT_ID_VALID cannot be set at the same time.

Let's consult with other vendors. We need this enforcement because we lack a straightforward method to distinguish between PRIs in stage-1 and stage-2 under nested translation. If other vendors share this requirement, it would be appropriate to implement this enforcement in IOMMUFD. Otherwise, we may check it in intel iommu driver.

@arm and amd folks. :)

Signature · 2 lines
Regards,
Yi Liu
maintainer
#46 lore raw

Re: [PATCH v6 09/14] iommu/vt-d: Add IOMMU_HWPT_ALLOC_PASID support

Quoted 5 lines · Yi Liu

Let's consult with other vendors. We need this enforcement because we lack a straightforward method to distinguish between PRIs in stage-1 and stage-2 under nested translation. If other vendors share this requirement, it would be appropriate to implement this enforcement in IOMMUFD. Otherwise, we may check it in intel iommu driver.

I do expect HW to be able to distinguish S1/S2 faults for the purpose of PRI, otherwise you cannot use non-present pages in the S2 at all, which is something that is very desirable.

Eg AMD as a GN bit in the PAGE_SERVICE_REQUEST

ARM has a S2 bit in their F_TRANSLATION/etc

IMHO not being able to do this is an Intel limitation (that you should get your HW team to fix)

However, right now there is no driver or core implementation for any of this, so setting up a S2 with fault should be blocked in the core code.

Jason

author
#47 lore raw

Re: [PATCH v6 09/14] iommu/vt-d: Add IOMMU_HWPT_ALLOC_PASID support

Quoted 7 lines · Jason Gunthorpe
Quoted 5 lines · Yi Liu

Let's consult with other vendors. We need this enforcement because we lack a straightforward method to distinguish between PRIs in stage-1 and stage-2 under nested translation. If other vendors share this requirement, it would be appropriate to implement this enforcement in IOMMUFD. Otherwise, we may check it in intel iommu driver.

Quoted 7 lines

I do expect HW to be able to distinguish S1/S2 faults for the purpose of PRI, otherwise you cannot use non-present pages in the S2 at all, which is something that is very desirable.

Eg AMD as a GN bit in the PAGE_SERVICE_REQUEST

ARM has a S2 bit in their F_TRANSLATION/etc

thanks for this info.

Quoted 6 lines

IMHO not being able to do this is an Intel limitation (that you should get your HW team to fix)

However, right now there is no driver or core implementation for any of this, so setting up a S2 with fault should be blocked in the core code.

yes, as iommufd always pin and map user pages for the paging domain, so I don't think there will be any PRIs on S2 due to non-present pages. However, PRIs on S2 might occur due to insufficient permissions, potentially caused by userspace. Nonetheless, I am skeptical about the existence of such use cases. What do you think about it?

Signature · 2 lines
Regards,
Yi Liu
maintainer
#48 lore raw

Re: [PATCH v6 09/14] iommu/vt-d: Add IOMMU_HWPT_ALLOC_PASID support

Quoted 2 lines · Yi Liu

yes, as iommufd always pin and map user pages for the paging domain, so I don't think there will be any PRIs on S2 due to non-present pages.

That isn't strictly true, a misbehaving guest could deliberately setup a S1 that points to a non-present S2. Ideally this would cause no PRI into the guest and the VMM would fault the device.

Quoted 3 lines

However, PRIs on S2 might occur due to insufficient permissions, potentially caused by userspace. Nonetheless, I am skeptical about the existence of such use cases. What do you think about it?

I suspect it is "OK", we already have to be safe against a misbehaving guest creating a PRI storm, if a misbehaving guest does it via pointing to the wrong S2 or just not updating the S1 doesn't seem to make much difference..

IMHO it is desirable for the HW to provide a way to disambiguate the PRI.

Jason

author
#49 lore raw

Re: [PATCH v6 09/14] iommu/vt-d: Add IOMMU_HWPT_ALLOC_PASID support

Quoted 7 lines · Jason Gunthorpe
Quoted 2 lines · Yi Liu

yes, as iommufd always pin and map user pages for the paging domain, so I don't think there will be any PRIs on S2 due to non-present pages.

That isn't strictly true, a misbehaving guest could deliberately setup a S1 that points to a non-present S2. Ideally this would cause no PRI into the guest and the VMM would fault the device.

yes, malicious guest can surely do it.

Quoted 8 lines
Quoted 3 lines

However, PRIs on S2 might occur due to insufficient permissions, potentially caused by userspace. Nonetheless, I am skeptical about the existence of such use cases. What do you think about it?

I suspect it is "OK", we already have to be safe against a misbehaving guest creating a PRI storm, if a misbehaving guest does it via pointing to the wrong S2 or just not updating the S1 doesn't seem to make much difference..

ok, given these facts, we can consider iommufd does not support valid PRI on S2 for now. Hence adding the aforementioned enforcement is acceptable.

Quoted 2 lines

IMHO it is desirable for the HW to provide a way to disambiguate the PRI.

yes.

Regards,
Yi Liu

reviewer
#50 lore raw

Re: [PATCH v6 09/14] iommu/vt-d: Add IOMMU_HWPT_ALLOC_PASID support

In reply to Yi Liu
Quoted 42 lines · Yi Liu
Quoted 33 lines · Baolu Lu
Quoted 32 lines · Yi Liu
Quoted 20 lines · Baolu Lu
Quoted 6 lines · Yi Liu
Quoted 3 lines

Another related consideration is the support for page faults in nested domains once PASID is available in user space. Would it be reasonable to support page faults for nested domains?

yeah, it's good to discuss it.

Quoted 8 lines
Quoted 2 lines

If so, perhaps it's time to open intel_iommu_domain_alloc_nested() to support IOMMU_HWPT_FAULT_ID_VALID when PRI is enabled on the device?

IMHO, the PRQ support for nested domains requires some more facility. PRQ can happen at either stage-1 or stage-2, iommu driver may need to tell it and forward to the correct domain (nested domain or parent domain). Or the stage-2 is always pinned just as the VFIO/iommufd does. Hence, any PRQ happens under nested translation should be due to stage-1.

The parent domain is currently always pinned and does not yet support page faults. Therefore, when a page fault occurs within a nested domain, it apparently should be routed to user space...

Quoted 6 lines

If we decide to support page faults on the stage-2 domain in the future, we will need to figure out the correct destination of each page fault and route it accordingly, either to the parent domain or the user space nested domain. Hardware assistance would be beneficial, otherwise the software may need to traverse the parent domain, which is not performance friendly.

this is my question. Is it still true the parent domain is always pinned after the below series? If yes, then it's fine to enable IOPF for nested domain.

Quoted 7 lines
Quoted 1 line

https://lore.kernel.org/linux-iommu/20241015-jag-iopfv8-v4-0- [email protected]/

Then, perhaps we could enforce this in iommufd for a short-term purpose?

When allocating a hwpt in iommufd, we should enforce that the flags IOMMU_HWPT_ALLOC_NEST_PARENT and IOMMU_HWPT_FAULT_ID_VALID cannot be set at the same time.

Quoted 7 lines

Let's consult with other vendors. We need this enforcement because we lack a straightforward method to distinguish between PRIs in stage-1 and stage-2 under nested translation. If other vendors share this requirement, it would be appropriate to implement this enforcement in IOMMUFD. Otherwise, we may check it in intel iommu driver.

@arm and amd folks. :)

Yup, SMMUv3 is more or less in the same boat - we *could* reasonably manage unpinned S2 for non-PCI devices using the stall model where the F_TRANSLATION or F_PERMISSION event tells us all we need, but for ATS it's the same thing where by the time the fault has taken a round-trip through an ATS response and a PRI request, we've lost the details of exactly how it faulted (or if the PRI request was sent eagerly without a prior translation request, then we simply have no idea at all). Thus the prospective mechanism would be to inject a virtual PRI, wait until we see the guest issue a matching CMD_PRI_RESP, then sniff the IPA/GPA for the given input address out of the guest pagetables to see if there's anything to do at S2 as well/instead. Yuck.

Thanks,
Robin.

maintainer
#51 lore raw

Re: [PATCH v6 09/14] iommu/vt-d: Add IOMMU_HWPT_ALLOC_PASID support

Quoted 5 lines · Robin Murphy

Yup, SMMUv3 is more or less in the same boat - we *could* reasonably manage unpinned S2 for non-PCI devices using the stall model where the F_TRANSLATION or F_PERMISSION event tells us all we need, but for ATS it's the same thing where by the time the fault has taken a round-trip through an ATS response and a PRI request,

I belive there is interest in PCISIG about this topic.

Quoted 7 lines

we've lost the details of exactly how it faulted (or if the PRI request was sent eagerly without a prior translation request, then we simply have no idea at all). Thus the prospective mechanism would be to inject a virtual PRI, wait until we see the guest issue a matching CMD_PRI_RESP, then sniff the IPA/GPA for the given input address out of the guest pagetables to see if there's anything to do at S2 as well/instead. Yuck.

I vaugely recall AMD HW will run the PRI through the page tables to figure out if it is S1/S2... Vasant?

Jason

author
#52 lore raw

Re: [PATCH v6 09/14] iommu/vt-d: Add IOMMU_HWPT_ALLOC_PASID support

In reply to Robin Murphy
Quoted 49 lines · Robin Murphy
Quoted 48 lines · Yi Liu
Quoted 33 lines · Baolu Lu
Quoted 32 lines · Yi Liu
Quoted 20 lines · Baolu Lu
Quoted 6 lines · Yi Liu
Quoted 3 lines

Another related consideration is the support for page faults in nested domains once PASID is available in user space. Would it be reasonable to support page faults for nested domains?

yeah, it's good to discuss it.

Quoted 8 lines
Quoted 2 lines

If so, perhaps it's time to open intel_iommu_domain_alloc_nested() to support IOMMU_HWPT_FAULT_ID_VALID when PRI is enabled on the device?

IMHO, the PRQ support for nested domains requires some more facility. PRQ can happen at either stage-1 or stage-2, iommu driver may need to tell it and forward to the correct domain (nested domain or parent domain). Or the stage-2 is always pinned just as the VFIO/iommufd does. Hence, any PRQ happens under nested translation should be due to stage-1.

The parent domain is currently always pinned and does not yet support page faults. Therefore, when a page fault occurs within a nested domain, it apparently should be routed to user space...

Quoted 6 lines

If we decide to support page faults on the stage-2 domain in the future, we will need to figure out the correct destination of each page fault and route it accordingly, either to the parent domain or the user space nested domain. Hardware assistance would be beneficial, otherwise the software may need to traverse the parent domain, which is not performance friendly.

this is my question. Is it still true the parent domain is always pinned after the below series? If yes, then it's fine to enable IOPF for nested domain.

Quoted 7 lines
Quoted 1 line

https://lore.kernel.org/linux-iommu/20241015-jag-iopfv8-v4-0- [email protected]/

Then, perhaps we could enforce this in iommufd for a short-term purpose?

When allocating a hwpt in iommufd, we should enforce that the flags IOMMU_HWPT_ALLOC_NEST_PARENT and IOMMU_HWPT_FAULT_ID_VALID cannot be set at the same time.

Let's consult with other vendors. We need this enforcement because we lack a straightforward method to distinguish between PRIs in stage-1 and stage-2 under nested translation. If other vendors share this requirement, it would be appropriate to implement this enforcement in IOMMUFD. Otherwise, we may check it in intel iommu driver.

Quoted 3 lines
Quoted 1 line

@arm and amd folks. :)

Yup, SMMUv3 is more or less in the same boat - we *could* reasonably manage unpinned S2 for non-PCI devices using the stall model where the F_TRANSLATION or F_PERMISSION event tells us all we need, but for ATS it's the same thing where by the time the fault has taken a round-trip through an ATS response and a PRI request, we've lost the details of exactly how it faulted (or if the PRI request was sent eagerly without a prior translation request, then we simply have no idea at all). Thus the prospective mechanism would be to inject a virtual PRI, wait until we see the guest issue a matching CMD_PRI_RESP, then sniff the IPA/GPA for the given input address out of the guest pagetables to see if there's anything to do at S2 as well/instead. Yuck.

Thanks for the response, Robin. It seems we're in the same situation. :( Out of curiosity, will the vPRI response provide the IPA/GPA to the hypervisor? If not, the hypervisor will need to derive it from the GVA on its own, possibly requiring software to traverse the guest page table.

Signature · 2 lines
Regards,
Yi Liu
reviewer
#53 lore raw

Re: [PATCH v6 09/14] iommu/vt-d: Add IOMMU_HWPT_ALLOC_PASID support

Quoted 50 lines · Yi Liu
Quoted 49 lines · Robin Murphy
Quoted 48 lines · Yi Liu
Quoted 33 lines · Baolu Lu
Quoted 32 lines · Yi Liu
Quoted 20 lines · Baolu Lu
Quoted 6 lines · Yi Liu
Quoted 3 lines

Another related consideration is the support for page faults in nested domains once PASID is available in user space. Would it be reasonable to support page faults for nested domains?

yeah, it's good to discuss it.

Quoted 8 lines
Quoted 2 lines

If so, perhaps it's time to open intel_iommu_domain_alloc_nested() to support IOMMU_HWPT_FAULT_ID_VALID when PRI is enabled on the device?

IMHO, the PRQ support for nested domains requires some more facility. PRQ can happen at either stage-1 or stage-2, iommu driver may need to tell it and forward to the correct domain (nested domain or parent domain). Or the stage-2 is always pinned just as the VFIO/iommufd does. Hence, any PRQ happens under nested translation should be due to stage-1.

The parent domain is currently always pinned and does not yet support page faults. Therefore, when a page fault occurs within a nested domain, it apparently should be routed to user space...

Quoted 6 lines

If we decide to support page faults on the stage-2 domain in the future, we will need to figure out the correct destination of each page fault and route it accordingly, either to the parent domain or the user space nested domain. Hardware assistance would be beneficial, otherwise the software may need to traverse the parent domain, which is not performance friendly.

this is my question. Is it still true the parent domain is always pinned after the below series? If yes, then it's fine to enable IOPF for nested domain.

Quoted 7 lines
Quoted 1 line

https://lore.kernel.org/linux-iommu/20241015-jag-iopfv8-v4-0- [email protected]/

Then, perhaps we could enforce this in iommufd for a short-term purpose?

When allocating a hwpt in iommufd, we should enforce that the flags IOMMU_HWPT_ALLOC_NEST_PARENT and IOMMU_HWPT_FAULT_ID_VALID cannot be set at the same time.

Let's consult with other vendors. We need this enforcement because we lack a straightforward method to distinguish between PRIs in stage-1 and stage-2 under nested translation. If other vendors share this requirement, it would be appropriate to implement this enforcement in IOMMUFD. Otherwise, we may check it in intel iommu driver.

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

@arm and amd folks. :)

Yup, SMMUv3 is more or less in the same boat - we *could* reasonably manage unpinned S2 for non-PCI devices using the stall model where the F_TRANSLATION or F_PERMISSION event tells us all we need, but for ATS it's the same thing where by the time the fault has taken a round-trip through an ATS response and a PRI request, we've lost the details of exactly how it faulted (or if the PRI request was sent eagerly without a prior translation request, then we simply have no idea at all). Thus the prospective mechanism would be to inject a virtual PRI, wait until we see the guest issue a matching CMD_PRI_RESP, then sniff the IPA/GPA for the given input address out of the guest pagetables to see if there's anything to do at S2 as well/instead. Yuck.

Thanks for the response, Robin. It seems we're in the same situation. :( Out of curiosity, will the vPRI response provide the IPA/GPA to the hypervisor? If not, the hypervisor will need to derive it from the GVA on its own, possibly requiring software to traverse the guest page table.

No, the PRI response command only contains a Page Request Group Index, so the hypervisor would then have to look up all the outstanding requests with that index to retrieve their input addresses and then translate them. The SMMU architecture does technically accommodate a hardware-assisted translation feature (ATOS), but it's optional and rarely implemented - certainly Arm's implementations don't - so in reality that is indeed a going to mean software walks as well.

Thanks,
Robin.

author
#54 lore raw

Re: [PATCH v6 09/14] iommu/vt-d: Add IOMMU_HWPT_ALLOC_PASID support

Quoted 53 lines · Robin Murphy
Quoted 52 lines · Yi Liu
Quoted 51 lines · Robin Murphy
Quoted 49 lines · Yi Liu
Quoted 33 lines · Baolu Lu
Quoted 32 lines · Yi Liu
Quoted 20 lines · Baolu Lu
Quoted 6 lines · Yi Liu
Quoted 3 lines

Another related consideration is the support for page faults in nested domains once PASID is available in user space. Would it be reasonable to support page faults for nested domains?

yeah, it's good to discuss it.

Quoted 8 lines
Quoted 2 lines

If so, perhaps it's time to open intel_iommu_domain_alloc_nested() to support IOMMU_HWPT_FAULT_ID_VALID when PRI is enabled on the device?

IMHO, the PRQ support for nested domains requires some more facility. PRQ can happen at either stage-1 or stage-2, iommu driver may need to tell it and forward to the correct domain (nested domain or parent domain). Or the stage-2 is always pinned just as the VFIO/iommufd does. Hence, any PRQ happens under nested translation should be due to stage-1.

The parent domain is currently always pinned and does not yet support page faults. Therefore, when a page fault occurs within a nested domain, it apparently should be routed to user space...

Quoted 6 lines

If we decide to support page faults on the stage-2 domain in the future, we will need to figure out the correct destination of each page fault and route it accordingly, either to the parent domain or the user space nested domain. Hardware assistance would be beneficial, otherwise the software may need to traverse the parent domain, which is not performance friendly.

this is my question. Is it still true the parent domain is always pinned after the below series? If yes, then it's fine to enable IOPF for nested domain.

Quoted 7 lines
Quoted 1 line

https://lore.kernel.org/linux-iommu/20241015-jag-iopfv8-v4-0- [email protected]/

Then, perhaps we could enforce this in iommufd for a short-term purpose?

When allocating a hwpt in iommufd, we should enforce that the flags IOMMU_HWPT_ALLOC_NEST_PARENT and IOMMU_HWPT_FAULT_ID_VALID cannot be set at the same time.

Let's consult with other vendors. We need this enforcement because we lack a straightforward method to distinguish between PRIs in stage-1 and stage-2 under nested translation. If other vendors share this requirement, it would be appropriate to implement this enforcement in IOMMUFD. Otherwise, we may check it in intel iommu driver.

@arm and amd folks. :)

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Yup, SMMUv3 is more or less in the same boat - we *could* reasonably manage unpinned S2 for non-PCI devices using the stall model where the F_TRANSLATION or F_PERMISSION event tells us all we need, but for ATS it's the same thing where by the time the fault has taken a round-trip through an ATS response and a PRI request, we've lost the details of exactly how it faulted (or if the PRI request was sent eagerly without a prior translation request, then we simply have no idea at all). Thus the prospective mechanism would be to inject a virtual PRI, wait until we see the guest issue a matching CMD_PRI_RESP, then sniff the IPA/GPA for the given input address out of the guest pagetables to see if there's anything to do at S2 as well/instead. Yuck.

Thanks for the response, Robin. It seems we're in the same situation. :( Out of curiosity, will the vPRI response provide the IPA/GPA to the hypervisor? If not, the hypervisor will need to derive it from the GVA on its own, possibly requiring software to traverse the guest page table.

No, the PRI response command only contains a Page Request Group Index, so the hypervisor would then have to look up all the outstanding requests with that index to retrieve their input addresses and then translate them. The SMMU architecture does technically accommodate a hardware-assisted translation feature (ATOS), but it's optional and rarely implemented - certainly Arm's implementations don't - so in reality that is indeed a going to mean software walks as well.

got it. thanks for the explanation.:)

Signature · 2 lines
Regards,
Yi Liu
maintainer
#55 lore raw

RE: [PATCH v6 09/14] iommu/vt-d: Add IOMMU_HWPT_ALLOC_PASID support

In reply to Robin Murphy
Quoted 23 lines

From: Robin Murphy <[email protected]>
Sent: Thursday, February 13, 2025 6:25 PM

Quoted 14 lines · Yi Liu
Quoted 12 lines · Robin Murphy

Yup, SMMUv3 is more or less in the same boat - we *could* reasonably manage unpinned S2 for non-PCI devices using the stall model where the F_TRANSLATION or F_PERMISSION event tells us all we need, but for ATS it's the same thing where by the time the fault has taken a round-trip through an ATS response and a PRI request, we've lost the details of exactly how it faulted (or if the PRI request was sent eagerly without a prior translation request, then we simply have no idea at all). Thus the prospective mechanism would be to inject a virtual PRI, wait until we see the guest issue a matching CMD_PRI_RESP, then sniff the IPA/GPA for the given input address out of the guest pagetables to see if there's anything to do at S2 as well/instead. Yuck.

Quoted 4 lines

Thanks for the response, Robin. It seems we're in the same situation. :( Out of curiosity, will the vPRI response provide the IPA/GPA to the hypervisor? If not, the hypervisor will need to derive it from the GVA on its own, possibly requiring software to traverse the guest page table.

Quoted 8 lines

No, the PRI response command only contains a Page Request Group Index, so the hypervisor would then have to look up all the outstanding requests with that index to retrieve their input addresses and then translate them. The SMMU architecture does technically accommodate a hardware-assisted translation feature (ATOS), but it's optional and rarely implemented - certainly Arm's implementations don't - so in reality that is indeed a going to mean software walks as well.

In cases where software walks are inevitable, it's probably more efficient to do the walk early to figure out whether it's S1 or S2 fault (then only forward s1 fault to guest), than always injecting a virtual PRI to guest and then figuring out whether s2 should be fixed too after receiving vPRI response.

Software walks are costly. Prefer to not supporting S2 faulting in nested configuration until there is proper support from hw to report the level (even a simple hw-conducted walking is more efficient than doing it in sw).

author
#56 lore raw

Re: [PATCH v6 09/14] iommu/vt-d: Add IOMMU_HWPT_ALLOC_PASID support

Quoted 38 lines · Kevin
Quoted 23 lines

From: Robin Murphy <[email protected]>
Sent: Thursday, February 13, 2025 6:25 PM

Quoted 14 lines · Yi Liu
Quoted 12 lines · Robin Murphy

Yup, SMMUv3 is more or less in the same boat - we *could* reasonably manage unpinned S2 for non-PCI devices using the stall model where the F_TRANSLATION or F_PERMISSION event tells us all we need, but for ATS it's the same thing where by the time the fault has taken a round-trip through an ATS response and a PRI request, we've lost the details of exactly how it faulted (or if the PRI request was sent eagerly without a prior translation request, then we simply have no idea at all). Thus the prospective mechanism would be to inject a virtual PRI, wait until we see the guest issue a matching CMD_PRI_RESP, then sniff the IPA/GPA for the given input address out of the guest pagetables to see if there's anything to do at S2 as well/instead. Yuck.

Quoted 4 lines

Thanks for the response, Robin. It seems we're in the same situation. :( Out of curiosity, will the vPRI response provide the IPA/GPA to the hypervisor? If not, the hypervisor will need to derive it from the GVA on its own, possibly requiring software to traverse the guest page table.

Quoted 8 lines

No, the PRI response command only contains a Page Request Group Index, so the hypervisor would then have to look up all the outstanding requests with that index to retrieve their input addresses and then translate them. The SMMU architecture does technically accommodate a hardware-assisted translation feature (ATOS), but it's optional and rarely implemented - certainly Arm's implementations don't - so in reality that is indeed a going to mean software walks as well.

In cases where software walks are inevitable, it's probably more efficient to do the walk early to figure out whether it's S1 or S2 fault (then only forward s1 fault to guest), than always injecting a virtual PRI to guest and then figuring out whether s2 should be fixed too after receiving vPRI response.

Quoted 4 lines

Software walks are costly. Prefer to not supporting S2 faulting in nested configuration until there is proper support from hw to report the level (even a simple hw-conducted walking is more efficient than doing it in sw).

The AMD IOMMU specification includes a bit to indicate whether the PRI is S2 or S1. However, the AMD IOMMU driver treats GN==0 as an invalid PRI. Given the fact, looks like we don't have S2 PRI support in Linux so far.

Spec (Revision 3.09):
Table 74: PAGE_SERVICE_REQUEST PPR Log Buffer Entry Fields

GN: Guest/nested. 1=Address[63:12] is a GVA and PASID is valid. 0=Address[63:12] is a GPA and PASID should be ignored by software.

Code:
static bool ppr_is_valid(struct amd_iommu *iommu, u64 *raw)

{
	struct device *dev = iommu->iommu.dev;
	u16 devid = PPR_DEVID(raw[0]);

	if (!(PPR_FLAGS(raw[0]) & PPR_FLAG_GN)) {
		dev_dbg(dev, "PPR logged [Request ignored due to GN=0 
(device=%04x:%02x:%02x.%x "
			"pasid=0x%05llx address=0x%llx flags=0x%04llx tag=0x%03llx]\n",
			iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
			PPR_PASID(raw[0]), raw[1], PPR_FLAGS(raw[0]), PPR_TAG(raw[0]));
		return false;
	}

https://github.com/torvalds/linux/blob/master/drivers/iommu/amd/ppr.c#L86

Signature · 2 lines
Regards,
Yi Liu
maintainer
#57 lore raw

RE: [PATCH v6 09/14] iommu/vt-d: Add IOMMU_HWPT_ALLOC_PASID support

Quoted 45 lines

From: Liu, Yi L <[email protected]>
Sent: Wednesday, February 19, 2025 8:51 PM

Quoted 8 lines · Kevin
Quoted 8 lines

From: Robin Murphy <[email protected]>
Sent: Thursday, February 13, 2025 6:25 PM

Quoted 4 lines · Yi Liu
Quoted 3 lines · Robin Murphy

Yup, SMMUv3 is more or less in the same boat - we *could* reasonably manage unpinned S2 for non-PCI devices using the stall model where

the

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

F_TRANSLATION or F_PERMISSION event tells us all we need, but for

ATS

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

it's the same thing where by the time the fault has taken a round-trip through an ATS response and a PRI request, we've lost the details of exactly how it faulted (or if the PRI request was sent eagerly without a prior translation request, then we simply have no idea at all). Thus the prospective mechanism would be to inject a virtual PRI, wait until we see the guest issue a matching CMD_PRI_RESP, then sniff the

IPA/GPA

Quoted 23 lines
Quoted 8 lines
Quoted 7 lines
Quoted 2 lines

for the given input address out of the guest pagetables to see if there's anything to do at S2 as well/instead. Yuck.

Thanks for the response, Robin. It seems we're in the same situation. :( Out of curiosity, will the vPRI response provide the IPA/GPA to the hypervisor? If not, the hypervisor will need to derive it from the GVA on its own, possibly requiring software to traverse the guest page table.

Quoted 8 lines

No, the PRI response command only contains a Page Request Group Index, so the hypervisor would then have to look up all the outstanding requests with that index to retrieve their input addresses and then translate them. The SMMU architecture does technically accommodate a hardware-assisted translation feature (ATOS), but it's optional and rarely implemented - certainly Arm's implementations don't - so in reality that is indeed a going to mean software walks as well.

In cases where software walks are inevitable, it's probably more efficient to do the walk early to figure out whether it's S1 or S2 fault (then only forward s1 fault to guest), than always injecting a virtual PRI to guest and then figuring out whether s2 should be fixed too after receiving vPRI response.

Quoted 9 lines
Quoted 4 lines

Software walks are costly. Prefer to not supporting S2 faulting in nested configuration until there is proper support from hw to report the level (even a simple hw-conducted walking is more efficient than doing it in sw).

The AMD IOMMU specification includes a bit to indicate whether the PRI is S2 or S1. However, the AMD IOMMU driver treats GN==0 as an invalid PRI. Given the fact, looks like we don't have S2 PRI support in Linux so far.

that's for sure given we don't have such support in core yet. 😊

so the AMD driver treats GN=0 as an error given it shouldn't happen in supported configurations.

If someone wants to support GN=0 then he will need to add required logic in iommufd/iommu and then the AMD driver. For now it's safe to explicitly disallow S2 faulting in nesting.

maintainer
#58 lore raw

Re: [PATCH v6 09/14] iommu/vt-d: Add IOMMU_HWPT_ALLOC_PASID support

In reply to Baolu Lu
Quoted 10 lines · Baolu Lu

drivers/iommu/intel/nested.c intel_iommu_domain_alloc_nested(struct device *dev, struct iommu_domain *parent, · line 202

Quoted 7 lines
drivers/iommu/intel/nested.c +1 -1
@@ -198,7 +198,7 @@ intel_iommu_domain_alloc_nested(struct device *dev, struct iommu_domain *parent,
  	struct dmar_domain *domain;
  	int ret;
-	if (!nested_supported(iommu) || flags)
+	if (!nested_supported(iommu) || flags & ~IOMMU_HWPT_ALLOC_PASID)
  		return ERR_PTR(-EOPNOTSUPP);
  	/* Must be nested domain */

It's better to abort and fail a domain allocation when IOMMU_HWPT_ALLOC_PASID is set but the iommu lacks pasid support?

With multi-instances of iommus in the system you still want to succeed creating a PASID capable domain even on instances that don't support PASID since it may end up being used on an other instances too.

Jason

maintainer
#59 lore raw

Re: [PATCH v6 09/14] iommu/vt-d: Add IOMMU_HWPT_ALLOC_PASID support

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

drivers/iommu/intel/nested.c intel_iommu_domain_alloc_nested(struct device *dev, struct iommu_domain *parent, · line 202

Quoted 7 lines
drivers/iommu/intel/nested.c +1 -1
@@ -198,7 +198,7 @@ intel_iommu_domain_alloc_nested(struct device *dev, struct iommu_domain *parent,
   	struct dmar_domain *domain;
   	int ret;
-	if (!nested_supported(iommu) || flags)
+	if (!nested_supported(iommu) || flags & ~IOMMU_HWPT_ALLOC_PASID)
   		return ERR_PTR(-EOPNOTSUPP);
   	/* Must be nested domain */

It's better to abort and fail a domain allocation when IOMMU_HWPT_ALLOC_PASID is set but the iommu lacks pasid support?

Quoted 3 lines

With multi-instances of iommus in the system you still want to succeed creating a PASID capable domain even on instances that don't support PASID since it may end up being used on an other instances too.

Okay, so this flag is just a hint that "this domain will probably be attached to a pasid of a device", NOT a requirement of "this domain necessitates hardware support for the PASID feature".

---
baolu

maintainer
#60 lore raw

RE: [PATCH v6 09/14] iommu/vt-d: Add IOMMU_HWPT_ALLOC_PASID support

Quoted 16 lines

From: Baolu Lu <[email protected]>
Sent: Friday, January 10, 2025 10:41 AM

Quoted 2 lines · Jason Gunthorpe
Quoted 1 line · Baolu Lu

Review intel_iommu_domain_alloc_nested(struct device

Quoted 1 line
(quoted diff) +0 -0
@@ -198,7 +198,7 @@ intel_iommu_domain_alloc_nested(struct device

*dev, struct iommu_domain *parent,

Quoted 4 lines
Quoted 4 lines

drivers/iommu/intel/nested.c intel_iommu_domain_alloc_nested(struct device *dev, struct iommu_domain *parent,

Quoted 4 lines
drivers/iommu/intel/nested.c +1 -1
   	struct dmar_domain *domain;
   	int ret;
-	if (!nested_supported(iommu) || flags)
+	if (!nested_supported(iommu) || flags &

~IOMMU_HWPT_ALLOC_PASID)

Quoted 4 lines
Quoted 4 lines

drivers/iommu/intel/nested.c intel_iommu_domain_alloc_nested(struct device *dev, struct iommu_domain *parent, · line 204

Quoted 2 lines
drivers/iommu/intel/nested.c +0 -0
   		return ERR_PTR(-EOPNOTSUPP);
   	/* Must be nested domain */

It's better to abort and fail a domain allocation when IOMMU_HWPT_ALLOC_PASID is set but the iommu lacks pasid support?

Quoted 8 lines
Quoted 3 lines

With multi-instances of iommus in the system you still want to succeed creating a PASID capable domain even on instances that don't support PASID since it may end up being used on an other instances too.

Okay, so this flag is just a hint that "this domain will probably be attached to a pasid of a device", NOT a requirement of "this domain necessitates hardware support for the PASID feature".

And note that pasid capability in VT-d just decides whether to serve DMA request with PASID. It doesn't change the format. It's legacy Mode vs. scalable mode for deciding the format, which is always pasid compatible once scalable mode is selected.

author
#61 lore raw

Re: [PATCH v6 09/14] iommu/vt-d: Add IOMMU_HWPT_ALLOC_PASID support

Quoted 25 lines · Kevin
Quoted 16 lines

From: Baolu Lu <[email protected]>
Sent: Friday, January 10, 2025 10:41 AM

Quoted 2 lines · Jason Gunthorpe
Quoted 1 line · Baolu Lu

Review intel_iommu_domain_alloc_nested(struct device

Quoted 1 line
(quoted diff) +0 -0
@@ -198,7 +198,7 @@ intel_iommu_domain_alloc_nested(struct device

*dev, struct iommu_domain *parent,

Quoted 4 lines
Quoted 4 lines

drivers/iommu/intel/nested.c intel_iommu_domain_alloc_nested(struct device *dev, struct iommu_domain *parent,

Quoted 4 lines
drivers/iommu/intel/nested.c +1 -1
    	struct dmar_domain *domain;
    	int ret;
-	if (!nested_supported(iommu) || flags)
+	if (!nested_supported(iommu) || flags &

~IOMMU_HWPT_ALLOC_PASID)

Quoted 4 lines
Quoted 4 lines

drivers/iommu/intel/nested.c intel_iommu_domain_alloc_nested(struct device *dev, struct iommu_domain *parent, · line 204

Quoted 2 lines
drivers/iommu/intel/nested.c +0 -0
    		return ERR_PTR(-EOPNOTSUPP);
    	/* Must be nested domain */

It's better to abort and fail a domain allocation when IOMMU_HWPT_ALLOC_PASID is set but the iommu lacks pasid support?

Quoted 8 lines
Quoted 3 lines

With multi-instances of iommus in the system you still want to succeed creating a PASID capable domain even on instances that don't support PASID since it may end up being used on an other instances too.

Okay, so this flag is just a hint that "this domain will probably be attached to a pasid of a device", NOT a requirement of "this domain necessitates hardware support for the PASID feature".

Quoted 4 lines

And note that pasid capability in VT-d just decides whether to serve DMA request with PASID. It doesn't change the format. It's legacy Mode vs. scalable mode for deciding the format, which is always pasid compatible once scalable mode is selected.

yes, pasid compatible is more about page table format. And I'd prefer to treat this flag just as a nop since VT-d does not have the aforementioned pasid-compatible limitation. So I may not add too much words here. :)

Signature · 2 lines
Regards,
Yi Liu
maintainer
#62 lore raw

RE: [PATCH v6 09/14] iommu/vt-d: Add IOMMU_HWPT_ALLOC_PASID support

In reply to Yi Liu

drivers/iommu/intel/nested.c intel_iommu_domain_alloc_nested(struct device · line 204

Quoted 12 lines

From: Liu, Yi L <[email protected]>
Sent: Thursday, December 19, 2024 9:28 PM

drivers/iommu/intel/nested.c +1 -1
@@ -198,7 +198,7 @@ intel_iommu_domain_alloc_nested(struct device
 *dev, struct iommu_domain *parent,
 	struct dmar_domain *domain;
 	int ret;
 
-	if (!nested_supported(iommu) || flags)
+	if (!nested_supported(iommu) || flags &
 ~IOMMU_HWPT_ALLOC_PASID)
 		return ERR_PTR(-EOPNOTSUPP);

Hmm isn't it causing regression? Though we don't have qemu support ready, the kernel support has been there for quite some time so we're not sure any vIOMMU already developed to utilize this interface then will be broken on this change.

Actually this check is not necessary. I don't see any restriction on VT-d which requires setting this flag for nesting. Instead ARM/AMD would require clearing this bit for nesting, as in that case it's RID attaching to CD table and there is no pasid attaching in the kernel hence no choice of format.

maintainer
#63 lore raw

RE: [PATCH v6 09/14] iommu/vt-d: Add IOMMU_HWPT_ALLOC_PASID support

Quoted 10 lines

From: Tian, Kevin <[email protected]>
Sent: Friday, January 10, 2025 3:39 PM

Quoted 7 lines

From: Liu, Yi L <[email protected]>
Sent: Thursday, December 19, 2024 9:28 PM

drivers/iommu/intel/nested.c +0 -0
@@ -198,7 +198,7 @@ intel_iommu_domain_alloc_nested(struct device
 *dev, struct iommu_domain *parent,
 	struct dmar_domain *domain;
 	int ret;
 
Quoted 11 lines

drivers/iommu/intel/nested.c intel_iommu_domain_alloc_nested(struct device *dev, struct iommu_domain *parent, · line 202

Quoted 5 lines
drivers/iommu/intel/nested.c +1 -1
-	if (!nested_supported(iommu) || flags)
+	if (!nested_supported(iommu) || flags &
 ~IOMMU_HWPT_ALLOC_PASID)
 		return ERR_PTR(-EOPNOTSUPP);

Hmm isn't it causing regression? Though we don't have qemu support ready, the kernel support has been there for quite some time so we're not sure any vIOMMU already developed to utilize this interface then will be broken on this change.

Please ignore this comment. The change was to allow the new flag instead of requiring it.

maintainer
#64 lore raw

Re: [PATCH v6 09/14] iommu/vt-d: Add IOMMU_HWPT_ALLOC_PASID support

In reply to Yi Liu

drivers/iommu/intel/nested.c intel_iommu_domain_alloc_nested(struct device *dev, struct iommu_domain *parent, · line 201

Quoted 10 lines · Yi Liu
drivers/iommu/intel/nested.c +1 -1
index aba92c00b427..6ac5c534bef4 100644
@@ -198,7 +198,7 @@ intel_iommu_domain_alloc_nested(struct device *dev, struct iommu_domain *parent,
 	struct dmar_domain *domain;
 	int ret;
 
-	if (!nested_supported(iommu) || flags)
+	if (!nested_supported(iommu) || flags & ~IOMMU_HWPT_ALLOC_PASID)

ARM and AMD should reject ALLOC_PASID when combined with nesting, it cannot do that. So it would be OK for intel to reject it too

Jason

maintainer
#65 lore raw

RE: [PATCH v6 09/14] iommu/vt-d: Add IOMMU_HWPT_ALLOC_PASID support

Quoted 13 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, January 14, 2025 4:31 AM

drivers/iommu/intel/nested.c intel_iommu_domain_alloc_nested(struct device

Quoted 5 lines · Yi Liu
drivers/iommu/intel/nested.c +0 -0
index aba92c00b427..6ac5c534bef4 100644
@@ -198,7 +198,7 @@ intel_iommu_domain_alloc_nested(struct device

*dev, struct iommu_domain *parent,

Quoted 3 lines
drivers/iommu/intel/nested.c +0 -0
 	struct dmar_domain *domain;
 	int ret;
 
Quoted 7 lines

drivers/iommu/intel/nested.c intel_iommu_domain_alloc_nested(struct device · line 201

Quoted 2 lines
drivers/iommu/intel/nested.c +1 -1
-	if (!nested_supported(iommu) || flags)
+	if (!nested_supported(iommu) || flags &

~IOMMU_HWPT_ALLOC_PASID)

ARM and AMD should reject ALLOC_PASID when combined with nesting, it cannot do that. So it would be OK for intel to reject it too

But Intel does allow nest_hwpt attached to a PASID. It sounds clearer to me to have the flag required on all hwpt types attached to PASID than having inconsistent rules among types. Anyway the nested hwpt alloc logic is per viommu so having different rules among vendors is fine...

author
#66 lore raw

PATCH v6 10/14 iommufd: Allow allocating PASID-compatible domain

In reply to Yi Liu

The underlying framework has supported the PASID attach and related enforcement per the requirement of the IOMMU_HWPT_ALLOC_PASID flag. This extends iommufd to support PASID compatible domain requested by userspace or the PASID compatible domain allocated in the auto_domain path.

---
 drivers/iommu/iommufd/device.c       | 4 +++-
 drivers/iommu/iommufd/hw_pagetable.c | 7 ++++---
 2 files changed, 7 insertions(+), 4 deletions(-)
drivers/iommu/iommufd/device.c +3 -1
index c1ff3dbe109c..768eae6c3275 100644
@@ -662,7 +662,9 @@ iommufd_device_auto_get_domain(struct iommufd_device *idev, ioasid_t pasid,
 		goto out_unlock;
 	}
 
-	hwpt_paging = iommufd_hwpt_paging_alloc(idev->ictx, ioas, idev, 0,
+	hwpt_paging = iommufd_hwpt_paging_alloc(idev->ictx, ioas, idev,
+						pasid != IOMMU_NO_PASID ?
+						IOMMU_HWPT_ALLOC_PASID : 0,
 						immediate_attach, NULL);
 	if (IS_ERR(hwpt_paging)) {
 		destroy_hwpt = ERR_CAST(hwpt_paging);
drivers/iommu/iommufd/hw_pagetable.c +4 -3
index 6fc848d3ef47..7787d0931761 100644
@@ -111,7 +111,8 @@ iommufd_hwpt_paging_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas,
 {
 	const u32 valid_flags = IOMMU_HWPT_ALLOC_NEST_PARENT |
 				IOMMU_HWPT_ALLOC_DIRTY_TRACKING |
-				IOMMU_HWPT_FAULT_ID_VALID;
+				IOMMU_HWPT_FAULT_ID_VALID |
+				IOMMU_HWPT_ALLOC_PASID;
 	const struct iommu_ops *ops = dev_iommu_ops(idev->dev);
 	struct iommufd_hwpt_paging *hwpt_paging;
 	struct iommufd_hw_pagetable *hwpt;
@@ -228,7 +229,7 @@ iommufd_hwpt_nested_alloc(struct iommufd_ctx *ictx,
 	struct iommufd_hw_pagetable *hwpt;
 	int rc;
 
-	if ((flags & ~IOMMU_HWPT_FAULT_ID_VALID) ||
+	if ((flags & ~(IOMMU_HWPT_FAULT_ID_VALID | IOMMU_HWPT_ALLOC_PASID)) ||
 	    !user_data->len || !ops->domain_alloc_nested)
 		return ERR_PTR(-EOPNOTSUPP);
 	if (parent->auto_domain || !parent->nest_parent ||
@@ -283,7 +284,7 @@ iommufd_viommu_alloc_hwpt_nested(struct iommufd_viommu *viommu, u32 flags,
 	struct iommufd_hw_pagetable *hwpt;
 	int rc;
 
-	if (flags & ~IOMMU_HWPT_FAULT_ID_VALID)
+	if (flags & ~(IOMMU_HWPT_FAULT_ID_VALID | IOMMU_HWPT_ALLOC_PASID))
 		return ERR_PTR(-EOPNOTSUPP);
 	if (!user_data->len)
 		return ERR_PTR(-EOPNOTSUPP);
Signature · 1 line
2.34.1
author
#67 lore raw

PATCH v6 11/14 iommufd/selftest: Add set_dev_pasid in mock iommu

In reply to Yi Liu

The callback is needed to make pasid_attach/detach path complete for mock device. A nop is enough for set_dev_pasid.

A MOCK_FLAGS_DEVICE_PASID is added to indicate a pasid-capable mock device for the pasid test cases. Other test cases will still create a non-pasid mock device. While the mock iommu always pretends to be pasid-capable.

---
 drivers/iommu/iommufd/iommufd_test.h |  1 +
 drivers/iommu/iommufd/selftest.c     | 45 +++++++++++++++++++++++++---
 2 files changed, 42 insertions(+), 4 deletions(-)
drivers/iommu/iommufd/iommufd_test.h +1 -0
index a6b7a163f636..bdc979557272 100644
@@ -48,6 +48,7 @@ enum {
 enum {
 	MOCK_FLAGS_DEVICE_NO_DIRTY = 1 << 0,
 	MOCK_FLAGS_DEVICE_HUGE_IOVA = 1 << 1,
+	MOCK_FLAGS_DEVICE_PASID = 1 << 2,
 };
 
 enum {
drivers/iommu/iommufd/selftest.c +41 -4

115 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#68 lore raw

PATCH v6 12/14 iommufd/selftest: Add a helper to get test device

In reply to Yi Liu

There is need to get the selftest device (sobj->type == TYPE_IDEV) in multiple places, so have a helper to for it.

---
 drivers/iommu/iommufd/selftest.c | 32 +++++++++++++++++++++-----------
 1 file changed, 21 insertions(+), 11 deletions(-)
drivers/iommu/iommufd/selftest.c +21 -11
index 3f49ef72ba91..515435640e35 100644
@@ -982,29 +982,39 @@ static int iommufd_test_mock_domain(struct iommufd_ucmd *ucmd,
 	return rc;
 }
 
-/* Replace the mock domain with a manually allocated hw_pagetable */
-static int iommufd_test_mock_domain_replace(struct iommufd_ucmd *ucmd,
-					    unsigned int device_id, u32 pt_id,
-					    struct iommu_test_cmd *cmd)
+static struct selftest_obj *
+iommufd_test_get_self_test_device(struct iommufd_ctx *ictx, u32 id)
 {
 	struct iommufd_object *dev_obj;
 	struct selftest_obj *sobj;
-	int rc;
 
 	/*
 	 * Prefer to use the OBJ_SELFTEST because the destroy_rwsem will ensure
 	 * it doesn't race with detach, which is not allowed.
 	 */
-	dev_obj =
-		iommufd_get_object(ucmd->ictx, device_id, IOMMUFD_OBJ_SELFTEST);
+	dev_obj = iommufd_get_object(ictx, id, IOMMUFD_OBJ_SELFTEST);
 	if (IS_ERR(dev_obj))
-		return PTR_ERR(dev_obj);
+		return ERR_CAST(dev_obj);
 
 	sobj = to_selftest_obj(dev_obj);
 	if (sobj->type != TYPE_IDEV) {
-		rc = -EINVAL;
-		goto out_dev_obj;
+		iommufd_put_object(ictx, dev_obj);
+		return ERR_PTR(-EINVAL);
 	}
+	return sobj;
+}
+
+/* Replace the mock domain with a manually allocated hw_pagetable */
+static int iommufd_test_mock_domain_replace(struct iommufd_ucmd *ucmd,
+					    unsigned int device_id, u32 pt_id,
+					    struct iommu_test_cmd *cmd)
+{
+	struct selftest_obj *sobj;
+	int rc;
+
+	sobj = iommufd_test_get_self_test_device(ucmd->ictx, device_id);
+	if (IS_ERR(sobj))
+		return PTR_ERR(sobj);
 
 	rc = iommufd_device_replace(sobj->idev.idev, &pt_id);
 	if (rc)
@@ -1014,7 +1024,7 @@ static int iommufd_test_mock_domain_replace(struct iommufd_ucmd *ucmd,
 	rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd));
 
 out_dev_obj:
-	iommufd_put_object(ucmd->ictx, dev_obj);
+	iommufd_put_object(ucmd->ictx, &sobj->obj);
 	return rc;
 }
 
Signature · 1 line
2.34.1
author
#69 lore raw

PATCH v6 13/14 iommufd/selftest: Add test ops to test pasid attach/detach

In reply to Yi Liu

This adds 4 test ops for pasid attach/replace/detach testing. There are ops to attach/detach pasid, and also op to check the attached domain of a pasid.

---
 drivers/iommu/iommufd/iommufd_test.h |  30 ++++++
 drivers/iommu/iommufd/selftest.c     | 154 +++++++++++++++++++++++++++
 2 files changed, 184 insertions(+)
drivers/iommu/iommufd/iommufd_test.h +30 -0
index bdc979557272..b3af7db97bc0 100644
@@ -24,6 +24,10 @@ enum {
 	IOMMU_TEST_OP_MD_CHECK_IOTLB,
 	IOMMU_TEST_OP_TRIGGER_IOPF,
 	IOMMU_TEST_OP_DEV_CHECK_CACHE,
+	IOMMU_TEST_OP_PASID_ATTACH,
+	IOMMU_TEST_OP_PASID_REPLACE,
+	IOMMU_TEST_OP_PASID_DETACH,
+	IOMMU_TEST_OP_PASID_CHECK_DOMAIN,
 };
 
 enum {
@@ -146,6 +150,32 @@ struct iommu_test_cmd {
 			__u32 id;
 			__u32 cache;
 		} check_dev_cache;
+		struct {
+			__u32 pasid;
+			__u32 pt_id;
+			/* @id is stdev_id for IOMMU_TEST_OP_PASID_ATTACH
+			 * pasid#1024 is for special test, avoid use it
+			 * in normal case.
+			 */
+		} pasid_attach;
+		struct {
+			__u32 pasid;
+			__u32 pt_id;
+			/* @id is stdev_id for IOMMU_TEST_OP_PASID_ATTACH
+			 * pasid#1024 is for special test, avoid use it
+			 * in normal case.
+			 */
+		} pasid_replace;
+		struct {
+			__u32 pasid;
+			/* @id is stdev_id for IOMMU_TEST_OP_PASID_DETACH */
+		} pasid_detach;
+		struct {
+			__u32 pasid;
+			__u32 hwpt_id;
+			__u64 out_result_ptr;
+			/* @id is stdev_id for IOMMU_TEST_OP_HWPT_GET_DOMAIN */
+		} pasid_check;
 	};
 	__u32 last;
 };
drivers/iommu/iommufd/selftest.c +154 -0

182 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#70 lore raw

PATCH v6 14/14 iommufd/selftest: Add coverage for iommufd pasid attach/detach

In reply to Yi Liu

This tests iommufd pasid attach/replace/detach.

---
 tools/testing/selftests/iommu/iommufd.c       | 348 ++++++++++++++++++
 .../selftests/iommu/iommufd_fail_nth.c        |  39 +-
 tools/testing/selftests/iommu/iommufd_utils.h | 102 +++++
 3 files changed, 482 insertions(+), 7 deletions(-)
tools/testing/selftests/iommu/iommufd.c +348 -0
index a1b2b657999d..575a9289dfdb 100644
@@ -2956,4 +2956,352 @@ TEST_F(iommufd_viommu, vdevice_cache)
 	}
 }
 
+FIXTURE(iommufd_device_pasid)
+{
+	int fd;
+	uint32_t ioas_id;
+	uint32_t hwpt_id;
+	uint32_t stdev_id;
+	uint32_t device_id;
+	uint32_t no_pasid_stdev_id;
+	uint32_t no_pasid_device_id;
+};
+
+FIXTURE_VARIANT(iommufd_device_pasid)
+{
+	bool pasid_capable;
+};
+
+FIXTURE_SETUP(iommufd_device_pasid)
+{
+	self->fd = open("/dev/iommu", O_RDWR);
+	ASSERT_NE(-1, self->fd);
+	test_ioctl_ioas_alloc(&self->ioas_id);
+
+	test_cmd_mock_domain_flags(self->ioas_id,
+				   MOCK_FLAGS_DEVICE_PASID,
+				   &self->stdev_id, &self->hwpt_id,
+				   &self->device_id);
+	if (!variant->pasid_capable)
+		test_cmd_mock_domain_flags(self->ioas_id, 0,
+					   &self->no_pasid_stdev_id, NULL,
+					   &self->no_pasid_device_id);
+}
+
+FIXTURE_TEARDOWN(iommufd_device_pasid)
+{
+	teardown_iommufd(self->fd, _metadata);
+}
+
+FIXTURE_VARIANT_ADD(iommufd_device_pasid, no_pasid)
+{
+	.pasid_capable = false,
+};
+
+FIXTURE_VARIANT_ADD(iommufd_device_pasid, has_pasid)
+{
+	.pasid_capable = true,
+};
+
+TEST_F(iommufd_device_pasid, pasid_attach)
+{
+	struct iommu_hwpt_selftest data = {
+		.iotlb =  IOMMU_TEST_IOTLB_DEFAULT,
+	};
+	uint32_t nested_hwpt_id[3] = {};
+	uint32_t parent_hwpt_id = 0;
+	uint32_t fault_id, fault_fd;
+	uint32_t s2_hwpt_id = 0;
+	uint32_t iopf_hwpt_id;
+	uint32_t pasid = 100;
+	uint32_t auto_hwpt;
+	uint32_t viommu_id;
+	bool result;
+
+	/* Allocate two nested hwpts sharing one common parent hwpt */
+	test_cmd_hwpt_alloc(self->device_id, self->ioas_id,
+			    IOMMU_HWPT_ALLOC_NEST_PARENT,
+			    &parent_hwpt_id);
+	test_cmd_hwpt_alloc_nested(self->device_id, parent_hwpt_id,
+				   IOMMU_HWPT_ALLOC_PASID,
+				   &nested_hwpt_id[0],
+				   IOMMU_HWPT_DATA_SELFTEST,
+				   &data, sizeof(data));
+	test_cmd_hwpt_alloc_nested(self->device_id, parent_hwpt_id,
+				   IOMMU_HWPT_ALLOC_PASID,
+				   &nested_hwpt_id[1],
+				   IOMMU_HWPT_DATA_SELFTEST,
+				   &data, sizeof(data));
+
+	/* Faulte related preparation */
+	test_ioctl_fault_alloc(&fault_id, &fault_fd);
+	test_cmd_hwpt_alloc_iopf(self->device_id, parent_hwpt_id, fault_id,
+				 IOMMU_HWPT_FAULT_ID_VALID | IOMMU_HWPT_ALLOC_PASID,
+				 &iopf_hwpt_id,
+				 IOMMU_HWPT_DATA_SELFTEST, &data,
+				 sizeof(data));
+
+	/* Allocate a regular nested hwpt based on viommu */
+	test_cmd_viommu_alloc(self->device_id, parent_hwpt_id,
+			      IOMMU_VIOMMU_TYPE_SELFTEST,
+			      &viommu_id);
+	test_cmd_hwpt_alloc_nested(self->device_id, viommu_id,
+				   IOMMU_HWPT_ALLOC_PASID,
+				   &nested_hwpt_id[2],
+				   IOMMU_HWPT_DATA_SELFTEST, &data,
+				   sizeof(data));
+
+	test_cmd_hwpt_alloc(self->device_id, self->ioas_id,
+			    IOMMU_HWPT_ALLOC_PASID,
+			    &s2_hwpt_id);
+
+	/* Attach RID to non-pasid compat domain, */
+	test_cmd_mock_domain_replace(self->stdev_id, parent_hwpt_id);
+	/* then attach to pasid should fail */
+	test_err_pasid_attach(EINVAL, pasid, s2_hwpt_id, NULL);
+
+	/* Attach RID to pasid compat domain, */
+	test_cmd_mock_domain_replace(self->stdev_id, s2_hwpt_id);
+	/* then attach to pasid should succeed, */
+	test_cmd_pasid_attach(pasid, nested_hwpt_id[0], NULL);
+	/* but attach RID to non-pasid compat domain should fail now. */
+	test_err_mock_domain_replace(EINVAL, self->stdev_id, parent_hwpt_id);
+	test_cmd_pasid_detach(pasid);
+
+	if (!variant->pasid_capable) {
+		/*
+		 * PASID-compatible domain can be used by non-PASID-capable
+		 * device.
+		 */
+		test_cmd_mock_domain_replace(self->no_pasid_stdev_id, nested_hwpt_id[0]);
+		test_cmd_mock_domain_replace(self->no_pasid_stdev_id, self->ioas_id);
+		/*
+		 * Attach hwpt to pasid#100 of non-PASID-capable device,
+		 * should fail, no matter domain is pasid-comapt or not.
+		 */
+		EXPECT_ERRNO(EINVAL,
+			     _test_cmd_pasid_attach(self->fd, self->no_pasid_stdev_id,
+						    pasid, parent_hwpt_id, NULL));
+		EXPECT_ERRNO(EINVAL,
+			     _test_cmd_pasid_attach(self->fd, self->no_pasid_stdev_id,
+						    pasid, s2_hwpt_id, NULL));
+	}
+
+	/*
+	 * Attach non pasid compat hwpt to pasid-capable device, should
+	 * fail, and have null domain.
+	 */
+	test_err_pasid_attach(EINVAL, pasid, parent_hwpt_id, NULL);
+	ASSERT_EQ(0,
+		  test_cmd_pasid_check_domain(self->fd, self->stdev_id,
+					      pasid, 0, &result));
+	EXPECT_EQ(1, result);
+
+	/*
+	 * Attach ioas to pasid 100, should succeed, domain should
+	 * be valid.
+	 */
+	test_cmd_pasid_attach(pasid, self->ioas_id, &auto_hwpt);
+	ASSERT_EQ(0,
+		  test_cmd_pasid_check_domain(self->fd, self->stdev_id,
+					      pasid, auto_hwpt, &result));
+	EXPECT_EQ(1, result);
+
+	/*
+	 * Attach same ioas to pasid 100, should succeed.
+	 */
+	test_cmd_pasid_attach(pasid, self->ioas_id, &auto_hwpt);
+
+	/*
+	 * Try attach pasid 100 with another hwpt, should FAIL
+	 * as attach does not allow overwrite, use REPLACE instead.
+	 */
+	test_err_pasid_attach(EINVAL, pasid, nested_hwpt_id[0], NULL);
+
+	/*
+	 * Detach hwpt from pasid 100, and check if the pasid 100
+	 * has null domain. Should be done before the next attach.
+	 */
+	test_cmd_pasid_detach(pasid);
+	ASSERT_EQ(0,
+		  test_cmd_pasid_check_domain(self->fd, self->stdev_id,
+					      pasid, 0, &result));
+	EXPECT_EQ(1, result);
+
+	/*
+	 * Attach nested hwpt to pasid 100, should succeed, domain
+	 * should be valid.
+	 */
+	test_cmd_pasid_attach(pasid, nested_hwpt_id[0], NULL);
+	ASSERT_EQ(0,
+		  test_cmd_pasid_check_domain(self->fd, self->stdev_id,
+					      pasid, nested_hwpt_id[0],
+					      &result));
+	EXPECT_EQ(1, result);
+
+	/*
+	 * Try attach pasid 100 to same nested_hwpt_id[0], should succeed.
+	 */
+	test_cmd_pasid_attach(pasid, nested_hwpt_id[0], NULL);
+
+	/*
+	 * Detach hwpt from pasid 100, and check if the pasid 100
+	 * has null domain
+	 */
+	test_cmd_pasid_detach(pasid);
+	ASSERT_EQ(0,
+		  test_cmd_pasid_check_domain(self->fd, self->stdev_id,
+					      pasid, 0, &result));
+	EXPECT_EQ(1, result);
+
+	/* Replace tests */
+
+	pasid = 200;
+	/*
+	 * Replace pasid 200 without attaching it first, should
+	 * fail with -EINVAL.
+	 */
+	test_err_cmd_pasid_replace(EINVAL, pasid, s2_hwpt_id, NULL);
+
+	/*
+	 * Attach a s2 hwpt to pasid 200, should succeed, domain should
+	 * be valid.
+	 */
+	test_cmd_pasid_attach(pasid, s2_hwpt_id, NULL);
+	ASSERT_EQ(0,
+		  test_cmd_pasid_check_domain(self->fd, self->stdev_id,
+					      pasid, s2_hwpt_id,
+					      &result));
+	EXPECT_EQ(1, result);
+
+	/*
+	 * Replace pasid 200 with self->ioas_id, should succeed,
+	 * and have valid domain.
+	 */
+	test_cmd_pasid_replace(pasid, self->ioas_id, &auto_hwpt);
+	ASSERT_EQ(0,
+		  test_cmd_pasid_check_domain(self->fd, self->stdev_id,
+					      pasid, auto_hwpt,
+					      &result));
+	EXPECT_EQ(1, result);
+
+	/*
+	 * Replace a nested hwpt for pasid 200, should succeed,
+	 * and have valid domain.
+	 */
+	test_cmd_pasid_replace(pasid, nested_hwpt_id[0], NULL);
+	ASSERT_EQ(0,
+		  test_cmd_pasid_check_domain(self->fd, self->stdev_id,
+					      pasid, nested_hwpt_id[0],
+					      &result));
+	EXPECT_EQ(1, result);
+
+	/*
+	 * Replace with another nested hwpt for pasid 200, should
+	 * succeed, and have valid domain.
+	 */
+	test_cmd_pasid_replace(pasid, nested_hwpt_id[1], NULL);
+	ASSERT_EQ(0,
+		  test_cmd_pasid_check_domain(self->fd, self->stdev_id,
+					      pasid, nested_hwpt_id[1],
+					      &result));
+	EXPECT_EQ(1, result);
+
+	/*
+	 * Detach hwpt from pasid 200, and check if the pasid 200
+	 * has null domain.
+	 */
+	test_cmd_pasid_detach(pasid);
+	ASSERT_EQ(0,
+		  test_cmd_pasid_check_domain(self->fd, self->stdev_id,
+					      pasid, 0, &result));
+	EXPECT_EQ(1, result);
+
+	/* Negative Tests for pasid replace, use pasid 1024 */
+
+	/*
+	 * Attach a s2 hwpt to pasid 1024, should succeed, domain should
+	 * be valid.
+	 */
+	pasid = 1024;
+	test_cmd_pasid_attach(pasid, s2_hwpt_id, NULL);
+	ASSERT_EQ(0,
+		  test_cmd_pasid_check_domain(self->fd, self->stdev_id,
+					      pasid, s2_hwpt_id,
+					      &result));
+	EXPECT_EQ(1, result);
+
+	/*
+	 * Replace pasid 1024 with self->ioas_id, should fail,
+	 * but have the old valid domain. This is a designed
+	 * negative case, normally replace with self->ioas_id
+	 * could succeed.
+	 */
+	test_err_cmd_pasid_replace(ENOMEM, pasid, self->ioas_id, NULL);
+	ASSERT_EQ(0,
+		  test_cmd_pasid_check_domain(self->fd, self->stdev_id,
+					      pasid, s2_hwpt_id,
+					      &result));
+	EXPECT_EQ(1, result);
+
+	/*
+	 * Detach hwpt from pasid 1024, and check if the pasid 1024
+	 * has null domain.
+	 */
+	test_cmd_pasid_detach(pasid);
+	ASSERT_EQ(0,
+		  test_cmd_pasid_check_domain(self->fd, self->stdev_id,
+					      pasid, 0, &result));
+	EXPECT_EQ(1, result);
+
+	/* Attach to iopf-capable hwpt */
+
+	/*
+	 * Attach an iopf hwpt to pasid 2048, should succeed, domain should
+	 * be valid.
+	 */
+	pasid = 2048;
+	test_cmd_pasid_attach(pasid, iopf_hwpt_id, NULL);
+	ASSERT_EQ(0,
+		  test_cmd_pasid_check_domain(self->fd, self->stdev_id,
+					      pasid, iopf_hwpt_id,
+					      &result));
+	EXPECT_EQ(1, result);
+
+	/*
+	 * Replace with s2_hwpt_id for pasid 2048, should
+	 * succeed, and have valid domain.
+	 */
+	test_cmd_pasid_replace(pasid, s2_hwpt_id, NULL);
+	ASSERT_EQ(0,
+		  test_cmd_pasid_check_domain(self->fd, self->stdev_id,
+					      pasid, s2_hwpt_id,
+					      &result));
+	EXPECT_EQ(1, result);
+
+	/*
+	 * Detach hwpt from pasid 2048, and check if the pasid 2048
+	 * has null domain.
+	 */
+	test_cmd_pasid_detach(pasid);
+	ASSERT_EQ(0,
+		  test_cmd_pasid_check_domain(self->fd, self->stdev_id,
+					      pasid, 0, &result));
+	EXPECT_EQ(1, result);
+
+	test_ioctl_destroy(iopf_hwpt_id);
+	close(fault_fd);
+	test_ioctl_destroy(fault_id);
+
+	/* Detach the s2_hwpt_id from RID */
+	test_cmd_mock_domain_replace(self->stdev_id, self->ioas_id);
+
+	test_ioctl_destroy(nested_hwpt_id[0]);
+	test_ioctl_destroy(nested_hwpt_id[1]);
+	test_ioctl_destroy(nested_hwpt_id[2]);
+	test_ioctl_destroy(viommu_id);
+	test_ioctl_destroy(parent_hwpt_id);
+	test_ioctl_destroy(s2_hwpt_id);
+}
+
 TEST_HARNESS_MAIN
tools/testing/selftests/iommu/iommufd_fail_nth.c +32 -7
index 64b1f8e1b0cf..c9580724ca9c 100644
@@ -209,12 +209,16 @@ FIXTURE(basic_fail_nth)
 {
 	int fd;
 	uint32_t access_id;
+	uint32_t stdev_id;
+	uint32_t pasid;
 };
 
 FIXTURE_SETUP(basic_fail_nth)
 {
 	self->fd = -1;
 	self->access_id = 0;
+	self->stdev_id = 0;
+	self->pasid = 0; //test should use a non-zero value
 }
 
 FIXTURE_TEARDOWN(basic_fail_nth)
@@ -226,6 +230,8 @@ FIXTURE_TEARDOWN(basic_fail_nth)
 		rc = _test_cmd_destroy_access(self->access_id);
 		assert(rc == 0);
 	}
+	if (self->pasid && self->stdev_id)
+		_test_cmd_pasid_detach(self->fd, self->stdev_id, self->pasid);
 	teardown_iommufd(self->fd, _metadata);
 }
 
@@ -623,7 +629,6 @@ TEST_FAIL_NTH(basic_fail_nth, device)
 	uint32_t fault_hwpt_id;
 	uint32_t ioas_id;
 	uint32_t ioas_id2;
-	uint32_t stdev_id;
 	uint32_t idev_id;
 	uint32_t hwpt_id;
 	uint32_t viommu_id;
@@ -654,25 +659,29 @@ TEST_FAIL_NTH(basic_fail_nth, device)
 
 	fail_nth_enable();
 
-	if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, NULL,
-				  &idev_id))
+	if (_test_cmd_mock_domain_flags(self->fd, ioas_id,
+					MOCK_FLAGS_DEVICE_PASID,
+					&self->stdev_id, NULL, &idev_id))
 		return -1;
 
 	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, 0, &hwpt_id,
+	if (_test_cmd_hwpt_alloc(self->fd, idev_id, ioas_id, 0,
+				 IOMMU_HWPT_ALLOC_PASID, &hwpt_id,
 				 IOMMU_HWPT_DATA_NONE, 0, 0))
 		return -1;
 
-	if (_test_cmd_mock_domain_replace(self->fd, stdev_id, ioas_id2, NULL))
+	if (_test_cmd_mock_domain_replace(self->fd, self->stdev_id, ioas_id2, NULL))
 		return -1;
 
-	if (_test_cmd_mock_domain_replace(self->fd, stdev_id, hwpt_id, NULL))
+	if (_test_cmd_mock_domain_replace(self->fd, self->stdev_id, hwpt_id, NULL))
 		return -1;
 
 	if (_test_cmd_hwpt_alloc(self->fd, idev_id, ioas_id, 0,
-				 IOMMU_HWPT_ALLOC_NEST_PARENT, &hwpt_id,
+				 IOMMU_HWPT_ALLOC_NEST_PARENT |
+						IOMMU_HWPT_ALLOC_PASID,
+				 &hwpt_id,
 				 IOMMU_HWPT_DATA_NONE, 0, 0))
 		return -1;
 
@@ -692,6 +701,22 @@ TEST_FAIL_NTH(basic_fail_nth, device)
 				 IOMMU_HWPT_DATA_SELFTEST, &data, sizeof(data)))
 		return -1;
 
+	self->pasid = 200;
+
+	/* Tests for pasid attach/replace/detach */
+	if (_test_cmd_pasid_attach(self->fd, self->stdev_id,
+				   self->pasid, ioas_id, NULL)) {
+		self->pasid = 0;
+		return -1;
+	}
+
+	_test_cmd_pasid_replace(self->fd, self->stdev_id, self->pasid, ioas_id2, NULL);
+
+	if (_test_cmd_pasid_detach(self->fd, self->stdev_id, self->pasid))
+		return -1;
+
+	self->pasid = 0;
+
 	return 0;
 }
 
tools/testing/selftests/iommu/iommufd_utils.h +102 -0

105 lines not on this page · raw message

Signature · 1 line
2.34.1