On Mon, Jan 06, 2025 at 10:46:21AM -0800, Nicolin Chen wrote:
On Mon, Jan 06, 2025 at 11:01:32AM +0800, Baolu Lu wrote:
Nit: I think it would be more readable to add a check in the vevent reporting helper.
diff --git a/drivers/iommu/iommufd/driver.c b/drivers/iommu/iommufd/driver.c index 77c34f8791ef..ccada0ada5ff 100644 --- a/drivers/iommu/iommufd/driver.c +++ b/drivers/iommu/iommufd/driver.c @@ -86,6 +86,9 @@ int iommufd_viommu_report_event(struct iommufd_viommu *viommu, if (WARN_ON_ONCE(!data_len || !event_data)) return -EINVAL;
if (WARN_ON_ONCE(type != IOMMU_VEVENTQ_TYPE_ARM_SMMUV3))
return -EINVAL;
Hmm, that's a good point I think.
down_read(&viommu->veventqs_rwsem); veventq = iommufd_viommu_find_veventq(viommu, type);
^ |
We actually have been missing a type validation entirely, so the type could have been rejected by this function. Perhaps we should add a static list of supported types to struct iommufd_viommu_ops for drivers to report so that then the core could reject from the first place during a vEVENTQ allocation.
I added something like this. Will send a v5.
diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c index 0c7a5894ba07..348179f3cf2a 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c @@ -399,9 +399,15 @@ static int arm_vsmmu_cache_invalidate(struct iommufd_viommu *viommu, return ret; }
+static bool arm_vsmmu_supports_veventq(unsigned int type) +{ + return type == IOMMU_VIOMMU_TYPE_ARM_SMMUV3; +} + static const struct iommufd_viommu_ops arm_vsmmu_ops = { .alloc_domain_nested = arm_vsmmu_alloc_domain_nested, .cache_invalidate = arm_vsmmu_cache_invalidate, + .supports_veventq = arm_vsmmu_supports_veventq, };
struct iommufd_viommu *arm_vsmmu_alloc(struct device *dev, diff --git a/drivers/iommu/iommufd/eventq.c b/drivers/iommu/iommufd/eventq.c index bf0c04f571a7..b5be629f38ed 100644 --- a/drivers/iommu/iommufd/eventq.c +++ b/drivers/iommu/iommufd/eventq.c @@ -555,6 +555,11 @@ int iommufd_veventq_alloc(struct iommufd_ucmd *ucmd) viommu = iommufd_get_viommu(ucmd, cmd->viommu_id); if (IS_ERR(viommu)) return PTR_ERR(viommu); + + if (!viommu->ops || !viommu->ops->supports_veventq || + !viommu->ops->supports_veventq(cmd->type)) + return -EOPNOTSUPP; + down_write(&viommu->veventqs_rwsem);
if (iommufd_viommu_find_veventq(viommu, cmd->type)) { diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h index 6b3b46e382f8..55e71dca3664 100644 --- a/include/linux/iommufd.h +++ b/include/linux/iommufd.h @@ -117,6 +117,7 @@ struct iommufd_viommu { * array->entry_num to report the number of handled requests. * The data structure of the array entry must be defined in * include/uapi/linux/iommufd.h + * @supports_veventq: Whether the vIOMMU supports a given vEVENTQ type */ struct iommufd_viommu_ops { void (*destroy)(struct iommufd_viommu *viommu); @@ -125,6 +126,7 @@ struct iommufd_viommu_ops { const struct iommu_user_data *user_data); int (*cache_invalidate)(struct iommufd_viommu *viommu, struct iommu_user_data_array *array); + bool (*supports_veventq)(unsigned int type); };
#if IS_ENABLED(CONFIG_IOMMUFD)