[14/17] iommufd/selftest: Add IOMMU_TEST_OP_MOCK_DOMAIN_REPLACE test op

Message ID 20230209043153.14964-15-yi.l.liu@intel.com
State New
Headers
Series Add Intel VT-d nested translation |

Commit Message

Yi Liu Feb. 9, 2023, 4:31 a.m. UTC
  From: Nicolin Chen <nicolinc@nvidia.com>

This allows to detach/attach the mock_dev to a specified domain/hwpt.

Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
Signed-off-by: Yi Liu <yi.l.liu@intel.com>
---
 drivers/iommu/iommufd/device.c       |  1 -
 drivers/iommu/iommufd/iommufd_test.h |  5 +++
 drivers/iommu/iommufd/selftest.c     | 54 ++++++++++++++++++++++++++++
 3 files changed, 59 insertions(+), 1 deletion(-)
  

Comments

Nicolin Chen Feb. 14, 2023, 6:36 p.m. UTC | #1
On Wed, Feb 08, 2023 at 08:31:50PM -0800, Yi Liu wrote:
> From: Nicolin Chen <nicolinc@nvidia.com>
> 
> This allows to detach/attach the mock_dev to a specified domain/hwpt.
> 
> Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
> Signed-off-by: Yi Liu <yi.l.liu@intel.com>
> ---
>  drivers/iommu/iommufd/device.c       |  1 -
>  drivers/iommu/iommufd/iommufd_test.h |  5 +++
>  drivers/iommu/iommufd/selftest.c     | 54 ++++++++++++++++++++++++++++
>  3 files changed, 59 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
> index 826441c6005d..bf803975062c 100644
> --- a/drivers/iommu/iommufd/device.c
> +++ b/drivers/iommu/iommufd/device.c
> @@ -964,7 +964,6 @@ EXPORT_SYMBOL_NS_GPL(iommufd_access_rw, IOMMUFD);
>   * Creating a real iommufd_device is too hard, bypass creating a iommufd_device
>   * and go directly to attaching a domain.
>   */
> -
>  int iommufd_device_selftest_attach(struct iommufd_ctx *ictx,
>                                    struct iommufd_hw_pagetable *hwpt)
>  {

Just found last night that I forgot to drop this line removal.

Let's fix it in v2 :)

Thanks
Nic
  

Patch

diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
index 826441c6005d..bf803975062c 100644
--- a/drivers/iommu/iommufd/device.c
+++ b/drivers/iommu/iommufd/device.c
@@ -964,7 +964,6 @@  EXPORT_SYMBOL_NS_GPL(iommufd_access_rw, IOMMUFD);
  * Creating a real iommufd_device is too hard, bypass creating a iommufd_device
  * and go directly to attaching a domain.
  */
-
 int iommufd_device_selftest_attach(struct iommufd_ctx *ictx,
 				   struct iommufd_hw_pagetable *hwpt)
 {
diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h
index 1605ff2b1a90..a73ab51afe79 100644
--- a/drivers/iommu/iommufd/iommufd_test.h
+++ b/drivers/iommu/iommufd/iommufd_test.h
@@ -10,6 +10,7 @@ 
 enum {
 	IOMMU_TEST_OP_ADD_RESERVED = 1,
 	IOMMU_TEST_OP_MOCK_DOMAIN,
+	IOMMU_TEST_OP_MOCK_DOMAIN_REPLACE,
 	IOMMU_TEST_OP_MD_CHECK_MAP,
 	IOMMU_TEST_OP_MD_CHECK_REFS,
 	IOMMU_TEST_OP_CREATE_ACCESS,
@@ -53,6 +54,10 @@  struct iommu_test_cmd {
 			__u32 out_device_id;
 			__u32 out_hwpt_id;
 		} mock_domain;
+		struct {
+			__u32 device_id;
+			__u32 hwpt_id;
+		} mock_domain_replace;
 		struct {
 			__aligned_u64 iova;
 			__aligned_u64 length;
diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c
index 5f841d1d9e96..3623a42648d5 100644
--- a/drivers/iommu/iommufd/selftest.c
+++ b/drivers/iommu/iommufd/selftest.c
@@ -98,6 +98,7 @@  struct selftest_obj {
 	union {
 		struct {
 			struct iommufd_hw_pagetable *hwpt;
+			struct iommufd_ioas *ioas;
 			struct iommufd_ctx *ictx;
 			struct device mock_dev;
 		} idev;
@@ -308,6 +309,7 @@  static int iommufd_test_mock_domain(struct iommufd_ucmd *ucmd,
 	}
 	sobj->idev.ictx = ucmd->ictx;
 	sobj->type = TYPE_IDEV;
+	sobj->idev.ioas = ioas;
 	sobj->idev.mock_dev.bus = &mock_bus;
 	sobj->idev.mock_dev.iommu = &iommu;
 
@@ -341,6 +343,56 @@  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,
+					    struct iommu_test_cmd *cmd)
+{
+	struct iommufd_object *dev_obj, *hwpt_obj;
+	struct iommufd_hw_pagetable *hwpt;
+	struct selftest_obj *sobj;
+	int rc;
+
+	hwpt_obj = iommufd_get_object(ucmd->ictx,
+				      cmd->mock_domain_replace.hwpt_id,
+				      IOMMUFD_OBJ_HW_PAGETABLE);
+	if (IS_ERR(hwpt_obj))
+		return PTR_ERR(hwpt_obj);
+
+	hwpt = container_of(hwpt_obj, struct iommufd_hw_pagetable, obj);
+
+	dev_obj = iommufd_get_object(ucmd->ictx,
+				     cmd->mock_domain_replace.device_id,
+				     IOMMUFD_OBJ_SELFTEST);
+	if (IS_ERR(dev_obj)) {
+		rc = PTR_ERR(dev_obj);
+		goto out_hwpt_obj;
+	}
+
+	sobj = container_of(dev_obj, struct selftest_obj, obj);
+	if (sobj->type != TYPE_IDEV) {
+		rc = -EINVAL;
+		goto out_dev_obj;
+	}
+
+	iommufd_device_selftest_detach(sobj->idev.ictx, sobj->idev.hwpt);
+
+	rc = iommufd_device_selftest_attach(ucmd->ictx, hwpt);
+	if (rc)
+		goto out_reattach;
+	sobj->idev.hwpt = hwpt;
+
+	rc = 0;
+	goto out_dev_obj;
+
+out_reattach:
+	iommufd_device_selftest_attach(ucmd->ictx, sobj->idev.hwpt);
+out_dev_obj:
+	iommufd_put_object(dev_obj);
+out_hwpt_obj:
+	iommufd_put_object(hwpt_obj);
+	return rc;
+}
+
 /* Add an additional reserved IOVA to the IOAS */
 static int iommufd_test_add_reserved(struct iommufd_ucmd *ucmd,
 				     unsigned int mockpt_id,
@@ -847,6 +899,8 @@  int iommufd_test(struct iommufd_ucmd *ucmd)
 						 cmd->add_reserved.length);
 	case IOMMU_TEST_OP_MOCK_DOMAIN:
 		return iommufd_test_mock_domain(ucmd, cmd);
+	case IOMMU_TEST_OP_MOCK_DOMAIN_REPLACE:
+		return iommufd_test_mock_domain_replace(ucmd, cmd);
 	case IOMMU_TEST_OP_MD_CHECK_MAP:
 		return iommufd_test_md_check_pa(
 			ucmd, cmd->id, cmd->check_map.iova,