[PATCH v2 5/7] iommufd/selftest: Add MOCK_FLAGS_DEVICE_NO_ATTACH

Nicolin Chen
2025-10-15T00:31:28Z

Add a new MOCK_FLAGS_DEVICE_NO_ATTACH flag to allow the mock_domain cmd to
bypass the attach step, as IOMMU_OPTION_SW_MSI_START/SIZE only allow users
to set prior to an IOAS/HWPT attachment.

Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
---
drivers/iommu/iommufd/iommufd_test.h | 1 +
drivers/iommu/iommufd/selftest.c | 17 ++++++++++++-----
2 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h
index 8fc618b2bcf96..7f7ffe5d670bb 100644
--- a/drivers/iommu/iommufd/iommufd_test.h
+++ b/drivers/iommu/iommufd/iommufd_test.h
@@ -54,6 +54,7 @@ enum {
MOCK_FLAGS_DEVICE_NO_DIRTY = 1 << 0,
MOCK_FLAGS_DEVICE_HUGE_IOVA = 1 << 1,
MOCK_FLAGS_DEVICE_PASID = 1 << 2,
+ MOCK_FLAGS_DEVICE_NO_ATTACH = 1 << 3,
};

enum {
diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c
index de178827a078a..ee5671d7e55d8 100644
--- a/drivers/iommu/iommufd/selftest.c
+++ b/drivers/iommu/iommufd/selftest.c
@@ -1088,6 +1088,7 @@ static struct mock_dev *mock_dev_create(unsigned long dev_flags)
{},
};
const u32 valid_flags = MOCK_FLAGS_DEVICE_NO_DIRTY |
+ MOCK_FLAGS_DEVICE_NO_ATTACH |
MOCK_FLAGS_DEVICE_HUGE_IOVA |
MOCK_FLAGS_DEVICE_PASID;
struct mock_dev *mdev;
@@ -1181,9 +1182,13 @@ static int iommufd_test_mock_domain(struct iommufd_ucmd *ucmd,
}
sobj->idev.idev = idev;

- rc = iommufd_device_attach(idev, IOMMU_NO_PASID, &pt_id);
- if (rc)
- goto out_unbind;
+ if (dev_flags & MOCK_FLAGS_DEVICE_NO_ATTACH) {
+ pt_id = 0;
+ } else {
+ rc = iommufd_device_attach(idev, IOMMU_NO_PASID, &pt_id);
+ if (rc)
+ goto out_unbind;
+ }

/* Userspace must destroy the device_id to destroy the object */
cmd->mock_domain.out_hwpt_id = pt_id;
@@ -1196,7 +1201,8 @@ static int iommufd_test_mock_domain(struct iommufd_ucmd *ucmd,
return 0;

out_detach:
- iommufd_device_detach(idev, IOMMU_NO_PASID);
+ if (!(dev_flags & MOCK_FLAGS_DEVICE_NO_ATTACH))
+ iommufd_device_detach(idev, IOMMU_NO_PASID);
out_unbind:
iommufd_device_unbind(idev);
out_mdev:
@@ -2024,7 +2030,8 @@ void iommufd_selftest_destroy(struct iommufd_object *obj)

switch (sobj->type) {
case TYPE_IDEV:
- iommufd_device_detach(sobj->idev.idev, IOMMU_NO_PASID);
+ if (!(sobj->idev.mock_dev->flags & MOCK_FLAGS_DEVICE_NO_ATTACH))
+ iommufd_device_detach(sobj->idev.idev, IOMMU_NO_PASID);
iommufd_device_unbind(sobj->idev.idev);
mock_dev_destroy(sobj->idev.mock_dev);
break;
--
2.43.0