Skip to content

Commit c2fa4d4

Browse files
LuBaolujoergroedel
authored andcommitted
iommufd/selftest: Put iopf enablement in domain attach path
Update iopf enablement in the iommufd mock device driver to use the new method, similar to the arm-smmu-v3 driver. Enable iopf support when any domain with an iopf_handler is attached, and disable it when the domain is removed. Add a refcount in the mock device state structure to keep track of the number of domains set to the device and PASIDs that require iopf. Signed-off-by: Lu Baolu <[email protected]> Reviewed-by: Kevin Tian <[email protected]> Reviewed-by: Yi Liu <[email protected]> Reviewed-by: Nicolin Chen <[email protected]> Tested-by: Zhangfei Gao <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Joerg Roedel <[email protected]>
1 parent 17fce9d commit c2fa4d4

File tree

1 file changed

+47
-10
lines changed

1 file changed

+47
-10
lines changed

drivers/iommu/iommufd/selftest.c

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ enum {
5858
MOCK_PFN_HUGE_IOVA = _MOCK_PFN_START << 2,
5959
};
6060

61+
static int mock_dev_enable_iopf(struct device *dev, struct iommu_domain *domain);
62+
static void mock_dev_disable_iopf(struct device *dev, struct iommu_domain *domain);
63+
6164
/*
6265
* Syzkaller has trouble randomizing the correct iova to use since it is linked
6366
* to the map ioctl's output, and it has no ide about that. So, simplify things.
@@ -168,6 +171,8 @@ struct mock_dev {
168171
int id;
169172
u32 cache[MOCK_DEV_CACHE_NUM];
170173
atomic_t pasid_1024_fake_error;
174+
unsigned int iopf_refcount;
175+
struct iommu_domain *domain;
171176
};
172177

173178
static inline struct mock_dev *to_mock_dev(struct device *dev)
@@ -221,6 +226,13 @@ static int mock_domain_nop_attach(struct iommu_domain *domain,
221226
up_write(&mdev->viommu_rwsem);
222227
}
223228

229+
rc = mock_dev_enable_iopf(dev, domain);
230+
if (rc)
231+
return rc;
232+
233+
mock_dev_disable_iopf(dev, mdev->domain);
234+
mdev->domain = domain;
235+
224236
return 0;
225237
}
226238

@@ -229,6 +241,7 @@ static int mock_domain_set_dev_pasid_nop(struct iommu_domain *domain,
229241
struct iommu_domain *old)
230242
{
231243
struct mock_dev *mdev = to_mock_dev(dev);
244+
int rc;
232245

233246
/*
234247
* Per the first attach with pasid 1024, set the
@@ -256,6 +269,12 @@ static int mock_domain_set_dev_pasid_nop(struct iommu_domain *domain,
256269
}
257270
}
258271

272+
rc = mock_dev_enable_iopf(dev, domain);
273+
if (rc)
274+
return rc;
275+
276+
mock_dev_disable_iopf(dev, old);
277+
259278
return 0;
260279
}
261280

@@ -610,22 +629,42 @@ static void mock_domain_page_response(struct device *dev, struct iopf_fault *evt
610629
{
611630
}
612631

613-
static int mock_dev_enable_feat(struct device *dev, enum iommu_dev_features feat)
632+
static int mock_dev_enable_iopf(struct device *dev, struct iommu_domain *domain)
614633
{
615-
if (feat != IOMMU_DEV_FEAT_IOPF || !mock_iommu_iopf_queue)
634+
struct mock_dev *mdev = to_mock_dev(dev);
635+
int ret;
636+
637+
if (!domain || !domain->iopf_handler)
638+
return 0;
639+
640+
if (!mock_iommu_iopf_queue)
616641
return -ENODEV;
617642

618-
return iopf_queue_add_device(mock_iommu_iopf_queue, dev);
643+
if (mdev->iopf_refcount) {
644+
mdev->iopf_refcount++;
645+
return 0;
646+
}
647+
648+
ret = iopf_queue_add_device(mock_iommu_iopf_queue, dev);
649+
if (ret)
650+
return ret;
651+
652+
mdev->iopf_refcount = 1;
653+
654+
return 0;
619655
}
620656

621-
static int mock_dev_disable_feat(struct device *dev, enum iommu_dev_features feat)
657+
static void mock_dev_disable_iopf(struct device *dev, struct iommu_domain *domain)
622658
{
623-
if (feat != IOMMU_DEV_FEAT_IOPF || !mock_iommu_iopf_queue)
624-
return -ENODEV;
659+
struct mock_dev *mdev = to_mock_dev(dev);
625660

626-
iopf_queue_remove_device(mock_iommu_iopf_queue, dev);
661+
if (!domain || !domain->iopf_handler)
662+
return;
627663

628-
return 0;
664+
if (--mdev->iopf_refcount)
665+
return;
666+
667+
iopf_queue_remove_device(mock_iommu_iopf_queue, dev);
629668
}
630669

631670
static void mock_viommu_destroy(struct iommufd_viommu *viommu)
@@ -770,8 +809,6 @@ static const struct iommu_ops mock_ops = {
770809
.device_group = generic_device_group,
771810
.probe_device = mock_probe_device,
772811
.page_response = mock_domain_page_response,
773-
.dev_enable_feat = mock_dev_enable_feat,
774-
.dev_disable_feat = mock_dev_disable_feat,
775812
.user_pasid_table = true,
776813
.viommu_alloc = mock_viommu_alloc,
777814
.default_domain_ops =

0 commit comments

Comments
 (0)