Skip to content

Commit 5a1c759

Browse files
nicolincjgunthorpe
authored andcommitted
iommu/arm-smmu-v3: Do not bother impl_ops if IOMMU_VIOMMU_TYPE_ARM_SMMUV3
When viommu type is IOMMU_VIOMMU_TYPE_ARM_SMMUV3, always return or init the standard struct arm_vsmmu, instead of going through impl_ops that must have its own viommu type than the standard IOMMU_VIOMMU_TYPE_ARM_SMMUV3. Given that arm_vsmmu_init() is called after arm_smmu_get_viommu_size(), any unsupported viommu->type must be a corruption. And it must be a driver bug that its vsmmu_size and vsmmu_init ops aren't paired. Warn these two cases. Link: https://patch.msgid.link/r/[email protected] Suggested-by: Will Deacon <[email protected]> Acked-by: Will Deacon <[email protected]> Reviewed-by: Pranjal Shrivastava <[email protected]> Signed-off-by: Nicolin Chen <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]>
1 parent ab6bc44 commit 5a1c759

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -420,14 +420,13 @@ size_t arm_smmu_get_viommu_size(struct device *dev,
420420
!(smmu->features & ARM_SMMU_FEAT_S2FWB))
421421
return 0;
422422

423-
if (smmu->impl_ops && smmu->impl_ops->vsmmu_size &&
424-
viommu_type == smmu->impl_ops->vsmmu_type)
425-
return smmu->impl_ops->vsmmu_size;
423+
if (viommu_type == IOMMU_VIOMMU_TYPE_ARM_SMMUV3)
424+
return VIOMMU_STRUCT_SIZE(struct arm_vsmmu, core);
426425

427-
if (viommu_type != IOMMU_VIOMMU_TYPE_ARM_SMMUV3)
426+
if (!smmu->impl_ops || !smmu->impl_ops->vsmmu_size ||
427+
viommu_type != smmu->impl_ops->vsmmu_type)
428428
return 0;
429-
430-
return VIOMMU_STRUCT_SIZE(struct arm_vsmmu, core);
429+
return smmu->impl_ops->vsmmu_size;
431430
}
432431

433432
int arm_vsmmu_init(struct iommufd_viommu *viommu,
@@ -447,12 +446,18 @@ int arm_vsmmu_init(struct iommufd_viommu *viommu,
447446
/* FIXME Move VMID allocation from the S2 domain allocation to here */
448447
vsmmu->vmid = s2_parent->s2_cfg.vmid;
449448

450-
if (smmu->impl_ops && smmu->impl_ops->vsmmu_init &&
451-
viommu->type == smmu->impl_ops->vsmmu_type)
452-
return smmu->impl_ops->vsmmu_init(vsmmu, user_data);
449+
if (viommu->type == IOMMU_VIOMMU_TYPE_ARM_SMMUV3) {
450+
viommu->ops = &arm_vsmmu_ops;
451+
return 0;
452+
}
453453

454-
viommu->ops = &arm_vsmmu_ops;
455-
return 0;
454+
/*
455+
* Unsupported type should be rejected by arm_smmu_get_viommu_size.
456+
* Seeing one here indicates a kernel bug or some data corruption.
457+
*/
458+
if (WARN_ON(viommu->type != smmu->impl_ops->vsmmu_type))
459+
return -EOPNOTSUPP;
460+
return smmu->impl_ops->vsmmu_init(vsmmu, user_data);
456461
}
457462

458463
int arm_vmaster_report_event(struct arm_smmu_vmaster *vmaster, u64 *evt)

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4703,6 +4703,7 @@ static void arm_smmu_impl_remove(void *data)
47034703
static struct arm_smmu_device *arm_smmu_impl_probe(struct arm_smmu_device *smmu)
47044704
{
47054705
struct arm_smmu_device *new_smmu = ERR_PTR(-ENODEV);
4706+
const struct arm_smmu_impl_ops *ops;
47064707
int ret;
47074708

47084709
if (smmu->impl_dev && (smmu->options & ARM_SMMU_OPT_TEGRA241_CMDQV))
@@ -4713,11 +4714,24 @@ static struct arm_smmu_device *arm_smmu_impl_probe(struct arm_smmu_device *smmu)
47134714
if (IS_ERR(new_smmu))
47144715
return new_smmu;
47154716

4717+
ops = new_smmu->impl_ops;
4718+
if (ops) {
4719+
/* vsmmu_size and vsmmu_init ops must be paired */
4720+
if (WARN_ON(!ops->vsmmu_size != !ops->vsmmu_init)) {
4721+
ret = -EINVAL;
4722+
goto err_remove;
4723+
}
4724+
}
4725+
47164726
ret = devm_add_action_or_reset(new_smmu->dev, arm_smmu_impl_remove,
47174727
new_smmu);
47184728
if (ret)
47194729
return ERR_PTR(ret);
47204730
return new_smmu;
4731+
4732+
err_remove:
4733+
arm_smmu_impl_remove(new_smmu);
4734+
return ERR_PTR(ret);
47214735
}
47224736

47234737
static int arm_smmu_device_probe(struct platform_device *pdev)

0 commit comments

Comments
 (0)