[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20230724110406.107212-12-yi.l.liu@intel.com>
Date: Mon, 24 Jul 2023 04:04:00 -0700
From: Yi Liu <yi.l.liu@...el.com>
To: joro@...tes.org, alex.williamson@...hat.com, jgg@...dia.com,
kevin.tian@...el.com, robin.murphy@....com,
baolu.lu@...ux.intel.com
Cc: cohuck@...hat.com, eric.auger@...hat.com, nicolinc@...dia.com,
kvm@...r.kernel.org, mjrosato@...ux.ibm.com,
chao.p.peng@...ux.intel.com, yi.l.liu@...el.com,
yi.y.sun@...ux.intel.com, peterx@...hat.com, jasowang@...hat.com,
shameerali.kolothum.thodi@...wei.com, lulu@...hat.com,
suravee.suthikulpanit@....com, iommu@...ts.linux.dev,
linux-kernel@...r.kernel.org, linux-kselftest@...r.kernel.org,
zhenzhong.duan@...el.com
Subject: [PATCH v3 11/17] iommufd/selftest: Add IOMMU_TEST_OP_DEV_[ADD|DEL]_RESERVED to add/del reserved regions to selftest device
This makes test tool able to specify reserved regions to the selftest
device and hence able to validate the ioctl IOMMU_RESV_IOVA_RANGES.
Signed-off-by: Yi Liu <yi.l.liu@...el.com>
---
drivers/iommu/iommufd/iommufd_test.h | 6 ++
drivers/iommu/iommufd/selftest.c | 82 ++++++++++++++++++++++++++++
2 files changed, 88 insertions(+)
diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h
index 3f3644375bf1..b7f4bee2ea1b 100644
--- a/drivers/iommu/iommufd/iommufd_test.h
+++ b/drivers/iommu/iommufd/iommufd_test.h
@@ -19,6 +19,8 @@ enum {
IOMMU_TEST_OP_SET_TEMP_MEMORY_LIMIT,
IOMMU_TEST_OP_MOCK_DOMAIN_REPLACE,
IOMMU_TEST_OP_ACCESS_REPLACE_IOAS,
+ IOMMU_TEST_OP_DEV_ADD_RESERVED,
+ IOMMU_TEST_OP_DEV_DEL_RESERVED,
};
enum {
@@ -50,6 +52,10 @@ struct iommu_test_cmd {
__aligned_u64 start;
__aligned_u64 length;
} add_reserved;
+ struct {
+ __aligned_u64 start;
+ __aligned_u64 length;
+ } add_dev_reserved;
struct {
__u32 out_stdev_id;
__u32 out_hwpt_id;
diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c
index 72d0c37e0b5e..271c6c261eb4 100644
--- a/drivers/iommu/iommufd/selftest.c
+++ b/drivers/iommu/iommufd/selftest.c
@@ -93,6 +93,8 @@ enum selftest_obj_type {
struct mock_dev {
struct device dev;
+ struct rw_semaphore reserved_rwsem;
+ struct rb_root_cached reserved_itree;
};
struct selftest_obj {
@@ -385,6 +387,10 @@ static struct mock_dev *mock_dev_create(void)
mutex_init(&mdev->dev.iommu->lock);
mdev->dev.iommu->iommu_dev = &mock_iommu_device;
+ /* Reserve range related fields */
+ init_rwsem(&mdev->reserved_rwsem);
+ mdev->reserved_itree = RB_ROOT_CACHED;
+
rc = device_add(&mdev->dev);
if (rc)
goto err_dev_iommu;
@@ -407,8 +413,20 @@ static struct mock_dev *mock_dev_create(void)
return ERR_PTR(rc);
}
+static void mock_dev_remove_reserved_iovas(struct mock_dev *mdev)
+{
+ struct interval_tree_node *node;
+
+ while ((node = interval_tree_iter_first(&mdev->reserved_itree, 0,
+ ULONG_MAX))) {
+ interval_tree_remove(node, &mdev->reserved_itree);
+ kfree(node);
+ }
+}
+
static void mock_dev_destroy(struct mock_dev *mdev)
{
+ mock_dev_remove_reserved_iovas(mdev);
iommu_group_remove_device(&mdev->dev);
device_del(&mdev->dev);
kfree(mdev->dev.iommu);
@@ -536,6 +554,64 @@ static int iommufd_test_add_reserved(struct iommufd_ucmd *ucmd,
return rc;
}
+static int mock_dev_add_reserved_iova(struct mock_dev *mdev,
+ unsigned long start, unsigned long last)
+{
+ struct interval_tree_node *reserved;
+
+ if (interval_tree_iter_first(&mdev->reserved_itree, start, last))
+ return -EADDRINUSE;
+
+ reserved = kzalloc(sizeof(*reserved), GFP_KERNEL_ACCOUNT);
+ if (!reserved)
+ return -ENOMEM;
+ reserved->start = start;
+ reserved->last = last;
+ interval_tree_insert(reserved, &mdev->reserved_itree);
+ return 0;
+}
+
+/* Add a reserved IOVA region to a device */
+static int iommufd_test_dev_add_reserved(struct iommufd_ucmd *ucmd,
+ unsigned int device_id,
+ unsigned long start, size_t length)
+{
+ struct selftest_obj *sobj;
+ struct mock_dev *mdev;
+ int rc;
+
+ sobj = iommufd_test_get_device(ucmd->ictx, device_id);
+ if (IS_ERR(sobj))
+ return PTR_ERR(sobj);
+
+ mdev = sobj->idev.mock_dev;
+ down_write(&mdev->reserved_rwsem);
+ rc = mock_dev_add_reserved_iova(sobj->idev.mock_dev,
+ start, start + length - 1);
+ up_write(&mdev->reserved_rwsem);
+ iommufd_put_object(&sobj->obj);
+ return rc;
+}
+
+/* Remove all reserved IOVA regions of a device */
+static int iommufd_test_dev_del_reserved(struct iommufd_ucmd *ucmd,
+ unsigned int device_id)
+{
+ struct selftest_obj *sobj;
+ struct mock_dev *mdev;
+
+ sobj = iommufd_test_get_device(ucmd->ictx, device_id);
+ if (IS_ERR(sobj))
+ return PTR_ERR(sobj);
+
+ mdev = sobj->idev.mock_dev;
+ down_write(&mdev->reserved_rwsem);
+ mock_dev_remove_reserved_iovas(mdev);
+ up_write(&mdev->reserved_rwsem);
+ iommufd_put_object(&sobj->obj);
+ return 0;
+}
+
/* Check that every pfn under each iova matches the pfn under a user VA */
static int iommufd_test_md_check_pa(struct iommufd_ucmd *ucmd,
unsigned int mockpt_id, unsigned long iova,
@@ -1025,6 +1101,12 @@ int iommufd_test(struct iommufd_ucmd *ucmd)
return iommufd_test_add_reserved(ucmd, cmd->id,
cmd->add_reserved.start,
cmd->add_reserved.length);
+ case IOMMU_TEST_OP_DEV_ADD_RESERVED:
+ return iommufd_test_dev_add_reserved(ucmd, cmd->id,
+ cmd->add_dev_reserved.start,
+ cmd->add_dev_reserved.length);
+ case IOMMU_TEST_OP_DEV_DEL_RESERVED:
+ return iommufd_test_dev_del_reserved(ucmd, cmd->id);
case IOMMU_TEST_OP_MOCK_DOMAIN:
return iommufd_test_mock_domain(ucmd, cmd);
case IOMMU_TEST_OP_MOCK_DOMAIN_REPLACE:
--
2.34.1
Powered by blists - more mailing lists