Skip to contentSkip to conversation

iommufd/device: Enforce reserved IOVA also when attached to hwpt_nested

PATCH Superseded Nicolin Chen 3 participants 5 messages Aug 6, 2024

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 iommufd/device: Enforce reserved IOVA also when attached to hwpt_nested

Currently, device reserved regions are only enforced when the device is attached to an hwpt_paging. In other words, if the device gets attached to an hwpt_nested directly, the parent hwpt_paging of the hwpt_nested's would not enforce those reserved IOVAs. This works for most of reserved region types, but not for IOMMU_RESV_SW_MSI, which is a unique software defined window, required by a nesting case too to setup an MSI doorbell on the parent stage-2 hwpt/domain.

Kevin pointed out that:
1) there is no usage using up closely the entire IOVA space yet,
2) guest may change the viommu mode to switch between nested and paging then VMM has to take all devices' reserved regions into consideration anyway, when composing the GPA space.

So it would be actually convenient for us to also enforce reserved IOVA onto the parent hwpt_paging, when attaching a device to an hwpt_nested.

Repurpose the existing attach/replace_paging helpers to attach device's reserved IOVAs exclusively. Allow a common hwpt input to support both a hwpt_paging type and a hwpt_nested type.

Rework the to_hwpt_paging helper, which is only used by these reserved IOVA functions, to allow an IOMMUFD_OBJ_HWPT_NESTED hwpt to redirect to its parent hwpt_paging. And add another hwpt_to_ioas helper to get the IOAS pointer. Return a NULL in these two helpers for any potential new HWPT type, and make a NOP in those reserved IOVA functions accordingly.

---

Changelog
v2:
* Corrected the ioas comparisons for future hwpt type that returns NULL by the to_hwpt_paging helper.
v1:
https://lore.kernel.org/all/[email protected]/

 drivers/iommu/iommufd/device.c          | 77 ++++++++++++++-----------
 drivers/iommu/iommufd/iommufd_private.h | 15 ++++-
 2 files changed, 56 insertions(+), 36 deletions(-)
drivers/iommu/iommufd/device.c +42 -35
index 3214a4c17c6b3..949b69c9f3b2d 100644
@@ -327,13 +327,18 @@ static int iommufd_group_setup_msi(struct iommufd_group *igroup,
 	return 0;
 }
 
-static int iommufd_hwpt_paging_attach(struct iommufd_hwpt_paging *hwpt_paging,
-				      struct iommufd_device *idev)
+static int
+iommufd_device_attach_reserved_iova(struct iommufd_device *idev,
+				    struct iommufd_hw_pagetable *hwpt)
 {
+	struct iommufd_hwpt_paging *hwpt_paging = to_hwpt_paging(hwpt);
 	int rc;
 
 	lockdep_assert_held(&idev->igroup->lock);
 
+	if (!hwpt_paging)
+		return 0;
+
 	rc = iopt_table_enforce_dev_resv_regions(&hwpt_paging->ioas->iopt,
 						 idev->dev,
 						 &idev->igroup->sw_msi_start);
@@ -351,6 +356,18 @@ static int iommufd_hwpt_paging_attach(struct iommufd_hwpt_paging *hwpt_paging,
 	return 0;
 }
 
+static void
+iommufd_device_remove_reserved_iova(struct iommufd_device *idev,
+				    struct iommufd_hw_pagetable *hwpt)
+{
+	struct iommufd_hwpt_paging *hwpt_paging = to_hwpt_paging(hwpt);
+
+	if (!hwpt_paging)
+		return;
+
+	iopt_remove_reserved_iova(&hwpt_paging->ioas->iopt, idev->dev);
+}
+
 int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt,
 				struct iommufd_device *idev)
 {
@@ -363,11 +380,9 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt,
 		goto err_unlock;
 	}
 
-	if (hwpt_is_paging(hwpt)) {
-		rc = iommufd_hwpt_paging_attach(to_hwpt_paging(hwpt), idev);
-		if (rc)
-			goto err_unlock;
-	}
+	rc = iommufd_device_attach_reserved_iova(idev, hwpt);
+	if (rc)
+		goto err_unlock;
 
 	/*
 	 * Only attach to the group once for the first device that is in the
@@ -387,9 +402,7 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt,
 	mutex_unlock(&idev->igroup->lock);
 	return 0;
 err_unresv:
-	if (hwpt_is_paging(hwpt))
-		iopt_remove_reserved_iova(&to_hwpt_paging(hwpt)->ioas->iopt,
-					  idev->dev);
+	iommufd_device_remove_reserved_iova(idev, hwpt);
 err_unlock:
 	mutex_unlock(&idev->igroup->lock);
 	return rc;
@@ -406,9 +419,7 @@ iommufd_hw_pagetable_detach(struct iommufd_device *idev)
 		iommufd_hwpt_detach_device(hwpt, idev);
 		idev->igroup->hwpt = NULL;
 	}
-	if (hwpt_is_paging(hwpt))
-		iopt_remove_reserved_iova(&to_hwpt_paging(hwpt)->ioas->iopt,
-					  idev->dev);
+	iommufd_device_remove_reserved_iova(idev, hwpt);
 	mutex_unlock(&idev->igroup->lock);
 
 	/* Caller must destroy hwpt */
@@ -429,28 +440,31 @@ iommufd_device_do_attach(struct iommufd_device *idev,
 
 static void
 iommufd_group_remove_reserved_iova(struct iommufd_group *igroup,
-				   struct iommufd_hwpt_paging *hwpt_paging)
+				   struct iommufd_hw_pagetable *hwpt)
 {
 	struct iommufd_device *cur;
 
 	lockdep_assert_held(&igroup->lock);
 
 	list_for_each_entry(cur, &igroup->device_list, group_item)
-		iopt_remove_reserved_iova(&hwpt_paging->ioas->iopt, cur->dev);
+		iommufd_device_remove_reserved_iova(cur, hwpt);
 }
 
 static int
-iommufd_group_do_replace_paging(struct iommufd_group *igroup,
-				struct iommufd_hwpt_paging *hwpt_paging)
+iommufd_group_do_replace_reserved_iova(struct iommufd_group *igroup,
+				       struct iommufd_hw_pagetable *hwpt)
 {
-	struct iommufd_hw_pagetable *old_hwpt = igroup->hwpt;
+	struct iommufd_hwpt_paging *hwpt_paging = to_hwpt_paging(hwpt);
 	struct iommufd_device *cur;
 	int rc;
 
 	lockdep_assert_held(&igroup->lock);
 
-	if (!hwpt_is_paging(old_hwpt) ||
-	    hwpt_paging->ioas != to_hwpt_paging(old_hwpt)->ioas) {
+	if (!hwpt_paging)
+		return 0;
+
+	if (hwpt_to_ioas(hwpt) &&
+	    hwpt_to_ioas(hwpt) != hwpt_to_ioas(igroup->hwpt)) {
 		list_for_each_entry(cur, &igroup->device_list, group_item) {
 			rc = iopt_table_enforce_dev_resv_regions(
 				&hwpt_paging->ioas->iopt, cur->dev, NULL);
@@ -465,7 +479,7 @@ iommufd_group_do_replace_paging(struct iommufd_group *igroup,
 	return 0;
 
 err_unresv:
-	iommufd_group_remove_reserved_iova(igroup, hwpt_paging);
+	iommufd_group_remove_reserved_iova(igroup, hwpt);
 	return rc;
 }
 
@@ -491,22 +505,17 @@ iommufd_device_do_replace(struct iommufd_device *idev,
 	}
 
 	old_hwpt = igroup->hwpt;
-	if (hwpt_is_paging(hwpt)) {
-		rc = iommufd_group_do_replace_paging(igroup,
-						     to_hwpt_paging(hwpt));
-		if (rc)
-			goto err_unlock;
-	}
+	rc = iommufd_group_do_replace_reserved_iova(igroup, hwpt);
+	if (rc)
+		goto err_unlock;
 
 	rc = iommufd_hwpt_replace_device(idev, hwpt, old_hwpt);
 	if (rc)
 		goto err_unresv;
 
-	if (hwpt_is_paging(old_hwpt) &&
-	    (!hwpt_is_paging(hwpt) ||
-	     to_hwpt_paging(hwpt)->ioas != to_hwpt_paging(old_hwpt)->ioas))
-		iommufd_group_remove_reserved_iova(igroup,
-						   to_hwpt_paging(old_hwpt));
+	if (hwpt_to_ioas(old_hwpt) &&
+	    hwpt_to_ioas(old_hwpt) != hwpt_to_ioas(hwpt))
+		iommufd_group_remove_reserved_iova(igroup, old_hwpt);
 
 	igroup->hwpt = hwpt;
 
@@ -524,9 +533,7 @@ iommufd_device_do_replace(struct iommufd_device *idev,
 	/* Caller must destroy old_hwpt */
 	return old_hwpt;
 err_unresv:
-	if (hwpt_is_paging(hwpt))
-		iommufd_group_remove_reserved_iova(igroup,
-						   to_hwpt_paging(hwpt));
+	iommufd_group_remove_reserved_iova(igroup, hwpt);
 err_unlock:
 	mutex_unlock(&idev->igroup->lock);
 	return ERR_PTR(rc);
drivers/iommu/iommufd/iommufd_private.h +14 -1
index 92efe30a8f0d0..42f5b7f7ec250 100644
@@ -321,7 +321,20 @@ static inline bool hwpt_is_paging(struct iommufd_hw_pagetable *hwpt)
 static inline struct iommufd_hwpt_paging *
 to_hwpt_paging(struct iommufd_hw_pagetable *hwpt)
 {
-	return container_of(hwpt, struct iommufd_hwpt_paging, common);
+	switch (hwpt->obj.type) {
+	case IOMMUFD_OBJ_HWPT_PAGING:
+		return container_of(hwpt, struct iommufd_hwpt_paging, common);
+	case IOMMUFD_OBJ_HWPT_NESTED:
+		return container_of(hwpt, struct iommufd_hwpt_nested, common)->parent;
+	default:
+		return NULL;
+	}
+}
+
+static inline struct iommufd_ioas *
+hwpt_to_ioas(struct iommufd_hw_pagetable *hwpt)
+{
+	return to_hwpt_paging(hwpt) ? to_hwpt_paging(hwpt)->ioas : NULL;
 }
 
 static inline struct iommufd_hwpt_paging *
Signature · 1 line
2.43.0
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 10 lines

From: Nicolin Chen <[email protected]>
Sent: Tuesday, August 6, 2024 1:02 PM

drivers/iommu/iommufd/device.c +3 -1
+iommufd_group_do_replace_reserved_iova(struct iommufd_group *igroup,
+				       struct iommufd_hw_pagetable *hwpt)
 {
-	struct iommufd_hw_pagetable *old_hwpt = igroup->hwpt;
+	struct iommufd_hwpt_paging *hwpt_paging =

to_hwpt_paging(hwpt);

drivers/iommu/iommufd/device.c +0 -0
 	struct iommufd_device *cur;

drivers/iommu/iommufd/device.c iommufd_device_do_attach(struct iommufd_device *idev, · line 466

Quoted 10 lines
drivers/iommu/iommufd/device.c +4 -2
 	int rc;
 
 	lockdep_assert_held(&igroup->lock);
 
-	if (!hwpt_is_paging(old_hwpt) ||
-	    hwpt_paging->ioas != to_hwpt_paging(old_hwpt)->ioas) {
+	if (!hwpt_paging)
+		return 0;
+
+	if (hwpt_to_ioas(hwpt) &&

this check is always true when hwpt_paging is valid.

#2 lore raw
Quoted 23 lines · Kevin
Quoted 14 lines

From: Nicolin Chen <[email protected]>
Sent: Tuesday, August 6, 2024 1:02 PM

drivers/iommu/iommufd/device.c +3 -1
+iommufd_group_do_replace_reserved_iova(struct iommufd_group *igroup,
+                                    struct iommufd_hw_pagetable *hwpt)
 {
-     struct iommufd_hw_pagetable *old_hwpt = igroup->hwpt;
+     struct iommufd_hwpt_paging *hwpt_paging =
to_hwpt_paging(hwpt);
      struct iommufd_device *cur;
      int rc;

      lockdep_assert_held(&igroup->lock);

drivers/iommu/iommufd/device.c iommufd_device_do_attach(struct iommufd_device *idev, · line 466

Quoted 6 lines
drivers/iommu/iommufd/device.c +4 -2
-     if (!hwpt_is_paging(old_hwpt) ||
-         hwpt_paging->ioas != to_hwpt_paging(old_hwpt)->ioas) {
+     if (!hwpt_paging)
+             return 0;
+
+     if (hwpt_to_ioas(hwpt) &&

this check is always true when hwpt_paging is valid.

Quoted 1 line

I can drop it.

Thanks
Nicolin

maintainer
#3 lore raw
In reply to Nicolin Chen
Quoted 49 lines · Nicolin Chen

Currently, device reserved regions are only enforced when the device is attached to an hwpt_paging. In other words, if the device gets attached to an hwpt_nested directly, the parent hwpt_paging of the hwpt_nested's would not enforce those reserved IOVAs. This works for most of reserved region types, but not for IOMMU_RESV_SW_MSI, which is a unique software defined window, required by a nesting case too to setup an MSI doorbell on the parent stage-2 hwpt/domain.

Kevin pointed out that:
1) there is no usage using up closely the entire IOVA space yet,
2) guest may change the viommu mode to switch between nested and paging then VMM has to take all devices' reserved regions into consideration anyway, when composing the GPA space.

So it would be actually convenient for us to also enforce reserved IOVA onto the parent hwpt_paging, when attaching a device to an hwpt_nested.

Repurpose the existing attach/replace_paging helpers to attach device's reserved IOVAs exclusively. Allow a common hwpt input to support both a hwpt_paging type and a hwpt_nested type.

Rework the to_hwpt_paging helper, which is only used by these reserved IOVA functions, to allow an IOMMUFD_OBJ_HWPT_NESTED hwpt to redirect to its parent hwpt_paging. And add another hwpt_to_ioas helper to get the IOAS pointer. Return a NULL in these two helpers for any potential new HWPT type, and make a NOP in those reserved IOVA functions accordingly.

---

Changelog
v2:
* Corrected the ioas comparisons for future hwpt type that returns NULL by the to_hwpt_paging helper.
v1:
https://lore.kernel.org/all/[email protected]/

 drivers/iommu/iommufd/device.c          | 77 ++++++++++++++-----------
 drivers/iommu/iommufd/iommufd_private.h | 15 ++++-
 2 files changed, 56 insertions(+), 36 deletions(-)
drivers/iommu/iommufd/device.c +0 -0
index 3214a4c17c6b3..949b69c9f3b2d 100644
@@ -327,13 +327,18 @@ static int iommufd_group_setup_msi(struct iommufd_group *igroup,
 	return 0;

drivers/iommu/iommufd/device.c static int iommufd_group_setup_msi(struct iommufd_group *igroup, · line 335

Quoted 10 lines
drivers/iommu/iommufd/device.c +4 -2
 }
 
-static int iommufd_hwpt_paging_attach(struct iommufd_hwpt_paging *hwpt_paging,
-				      struct iommufd_device *idev)
+static int
+iommufd_device_attach_reserved_iova(struct iommufd_device *idev,
+				    struct iommufd_hw_pagetable *hwpt)
 {
+	struct iommufd_hwpt_paging *hwpt_paging = to_hwpt_paging(hwpt);
 	int rc;

This seems like the wrong place to put these types, a big point point of the struct iommufd_hwpt_paging was to mark functions that should only be operating on a paging hwpt.

The caller is expected to figure out what it is doing.

drivers/iommu/iommufd/device.c int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, · line 370

Quoted 12 lines
drivers/iommu/iommufd/device.c +0 -5
 int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt,
 				struct iommufd_device *idev)
 {
@@ -363,11 +380,9 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt,
 		goto err_unlock;
 	}
 
-	if (hwpt_is_paging(hwpt)) {
-		rc = iommufd_hwpt_paging_attach(to_hwpt_paging(hwpt), idev);
-		if (rc)
-			goto err_unlock;
-	}

Like we had here, so it would be a bit nicer to write it as more like:

hwpt_paging = to_hwpt_paging(hwpt);

if (hwpt_paging) {
	rc = iommufd_hwpt_paging_attach(hwpt_paging, idev);
}

Then we can keep the clearer labeling of the function signatures.

Quoted 4 lines
drivers/iommu/iommufd/device.c +0 -0
@@ -321,7 +321,20 @@ static inline bool hwpt_is_paging(struct iommufd_hw_pagetable *hwpt)
 static inline struct iommufd_hwpt_paging *
 to_hwpt_paging(struct iommufd_hw_pagetable *hwpt)
 {

drivers/iommu/iommufd/device.c static inline bool hwpt_is_paging(struct iommufd_hw_pagetable *hwpt) · line 332

Quoted 10 lines
drivers/iommu/iommufd/device.c +9 -1
-	return container_of(hwpt, struct iommufd_hwpt_paging, common);
+	switch (hwpt->obj.type) {
+	case IOMMUFD_OBJ_HWPT_PAGING:
+		return container_of(hwpt, struct iommufd_hwpt_paging, common);
+	case IOMMUFD_OBJ_HWPT_NESTED:
+		return container_of(hwpt, struct iommufd_hwpt_nested, common)->parent;
+	default:
+		return NULL;
+	}
+}

There are alot of existing callers of this, I think it should get a new function to do this behavior and it would only be used in a few places.

It is not OK for a NESTING to get into most of the places that are already calling this.

Jason

#4 lore raw
Quoted 30 lines · Jason Gunthorpe

drivers/iommu/iommufd/device.c static int iommufd_group_setup_msi(struct iommufd_group *igroup, · line 335

Quoted 8 lines
drivers/iommu/iommufd/device.c +4 -2
-static int iommufd_hwpt_paging_attach(struct iommufd_hwpt_paging *hwpt_paging,
-				      struct iommufd_device *idev)
+static int
+iommufd_device_attach_reserved_iova(struct iommufd_device *idev,
+				    struct iommufd_hw_pagetable *hwpt)
 {
+	struct iommufd_hwpt_paging *hwpt_paging = to_hwpt_paging(hwpt);
 	int rc;

This seems like the wrong place to put these types, a big point point of the struct iommufd_hwpt_paging was to mark functions that should only be operating on a paging hwpt.

The caller is expected to figure out what it is doing.

drivers/iommu/iommufd/device.c int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, · line 370

Quoted 12 lines

int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt,

drivers/iommu/iommufd/device.c +0 -5
 				struct iommufd_device *idev)
 {
@@ -363,11 +380,9 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt,
 		goto err_unlock;
 	}
 
-	if (hwpt_is_paging(hwpt)) {
-		rc = iommufd_hwpt_paging_attach(to_hwpt_paging(hwpt), idev);
-		if (rc)
-			goto err_unlock;
-	}

Like we had here, so it would be a bit nicer to write it as more like:

Quoted 6 lines

hwpt_paging = to_hwpt_paging(hwpt);

if (hwpt_paging) {
	rc = iommufd_hwpt_paging_attach(hwpt_paging, idev);
}

Then we can keep the clearer labeling of the function signatures.

Okay. Will keep the hwpt_paging type in the function signatures.

Quoted 19 lines
Quoted 4 lines
drivers/iommu/iommufd/iommufd_private.h +0 -0
@@ -321,7 +321,20 @@ static inline bool hwpt_is_paging(struct iommufd_hw_pagetable *hwpt)
 static inline struct iommufd_hwpt_paging *
 to_hwpt_paging(struct iommufd_hw_pagetable *hwpt)
 {

drivers/iommu/iommufd/iommufd_private.h static inline bool hwpt_is_paging(struct iommufd_hw_pagetable *hwpt) · line 332

Quoted 10 lines
drivers/iommu/iommufd/iommufd_private.h +9 -1
-	return container_of(hwpt, struct iommufd_hwpt_paging, common);
+	switch (hwpt->obj.type) {
+	case IOMMUFD_OBJ_HWPT_PAGING:
+		return container_of(hwpt, struct iommufd_hwpt_paging, common);
+	case IOMMUFD_OBJ_HWPT_NESTED:
+		return container_of(hwpt, struct iommufd_hwpt_nested, common)->parent;
+	default:
+		return NULL;
+	}
+}

There are alot of existing callers of this, I think it should get a new function to do this behavior and it would only be used in a few places.

Quoted 2 lines

It is not OK for a NESTING to get into most of the places that are already calling this.

OK. Let's add a find_hwpt_paging(struct iommufd_hw_pagetable *);

Thanks
Nicolin