iommu: Clarify uAPI errno for unsupported data types
Subsystems (5)
PATCH v3 1/1 iommu: Clarify uAPI errno for unsupported data types
The IOMMU_HWPT_INVALIDATE uAPI says that an empty request array "can be used to check if the given @data_type is supported or not by kernel", but not what to look for. And the iommu_copy_struct_*() helpers and the Intel VT-d, ARM SMMUv3 and selftest invalidation ops reject unsupported data types with -EINVAL, which means that the request was understood but a field is not correct.
Return -EOPNOTSUPP for an unsupported data type instead and document it as what the probe looks for. Since arm_vsmmu_alloc_domain_nested() uses the copy helper, it now returns -EOPNOTSUPP, like the other nested domain allocators. Update and extend the selftests accordingly.
--- .../arm/arm-smmu-v3/arm-smmu-v3-iommufd.c | 2 +- drivers/iommu/intel/nested.c | 2 +- drivers/iommu/iommufd/selftest.c | 4 ++-- include/linux/iommu.h | 6 +++--- include/uapi/linux/iommufd.h | 3 ++- tools/testing/selftests/iommu/iommufd.c | 18 ++++++++++++++++-- 6 files changed, 25 insertions(+), 10 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c +1 -1
@@ -493,7 +493,7 @@ int arm_vsmmu_cache_invalidate(struct iommufd_viommu *viommu,static_assert (sizeof (*cmds) ==2 *sizeof (u64));if (array->type !=IOMMU_VIOMMU_INVALIDATE_DATA_ARM_SMMUV3 ) { - ret = -EINVAL ; + ret = -EOPNOTSUPP ; goto out; }
drivers/iommu/intel/nested.c +1 -1
@@ -99,7 +99,7 @@ static int intel_nested_cache_invalidate_user(struct iommu_domain *domain,int ret =0 ;if (array->type !=IOMMU_HWPT_INVALIDATE_DATA_VTD_S1 ) { - ret = -EINVAL ; + ret = -EOPNOTSUPP ; goto out; }
drivers/iommu/iommufd/selftest.c +2 -2
@@ -642,7 +642,7 @@ static int mock_viommu_cache_invalidate(struct iommufd_viommu *viommu,int i;if (array->type !=IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST ) { - rc = -EINVAL ; + rc = -EOPNOTSUPP ; goto out; }
@@ -876,7 +876,7 @@ mock_domain_cache_invalidate_user(struct iommu_domain *domain,int i;if (array->type !=IOMMU_HWPT_INVALIDATE_DATA_SELFTEST ) { - rc = -EINVAL ; + rc = -EOPNOTSUPP ; goto out; }
include/linux/iommu.h +3 -3
@@ -470,7 +470,7 @@ static inline int __iommu_copy_struct_from_user(if (WARN_ON (!dst_data || !src_data))return -EINVAL ;if (src_data->type != data_type) -return -EINVAL ; +return -EOPNOTSUPP ;if (src_data->len < min_len || data_len < src_data->len)return -EINVAL ; return copy_struct_from_user(dst_data, data_len, src_data->uptr,
@@ -567,7 +567,7 @@ iommu_copy_struct_from_full_user_array(void *kdst, size_t kdst_entry_size,int ret;if (user_array->type != data_type) -return -EINVAL ; +return -EOPNOTSUPP ;if (!user_array->entry_num)return -EINVAL ;if (likely (user_array->entry_len == kdst_entry_size)) {
@@ -609,7 +609,7 @@ __iommu_copy_struct_to_user(const struct iommu_user_data *dst_data,if (WARN_ON (!dst_data || !src_data))return -EINVAL ;if (dst_data->type != data_type) -return -EINVAL ; +return -EOPNOTSUPP ;if (dst_data->len < min_len || data_len < dst_data->len)return -EINVAL ; return copy_struct_to_user(dst_data->uptr, dst_data->len, src_data,
include/uapi/linux/iommufd.h +2 -1
@@ -986,7 +986,8 @@ struct iommu_viommu_arm_smmuv3_invalidate {* * An empty invalidation request array by setting @entry_num==0 is allowed, and * @entry_len and @data_uptr would be ignored in this case. This can be used to -* check if the given @data_type is supported or not by kernel. +* check if the given @data_type is supported or not by kernel: the ioctl fails +* with -EOPNOTSUPP for an unsupported @data_type. */ struct iommu_hwpt_invalidate {__u32 size;
tools/testing/selftests/iommu/iommufd.c +16 -2
@@ -536,9 +536,16 @@ TEST_F(iommufd_ioas, alloc_hwpt_nested) sizeof(*inv_reqs), &num_inv);assert (!num_inv); +/* Negative test: Invalid data_type with zero-length array */ + num_inv =0 ; +test_err_hwpt_invalidate (EOPNOTSUPP , nested_hwpt_id[0 ], inv_reqs, +IOMMU_HWPT_INVALIDATE_DATA_SELFTEST_INVALID , +sizeof (*inv_reqs), &num_inv); +assert (!num_inv); +/* Negative test: Invalid data_type */ num_inv =1 ; -test_err_hwpt_invalidate (EINVAL , nested_hwpt_id[0 ], inv_reqs, +test_err_hwpt_invalidate (EOPNOTSUPP , nested_hwpt_id[0 ], inv_reqs,IOMMU_HWPT_INVALIDATE_DATA_SELFTEST_INVALID ,sizeof (*inv_reqs), &num_inv);assert (!num_inv);
@@ -3090,9 +3097,16 @@ TEST_F(iommufd_viommu, vdevice_cache) &num_inv);assert (!num_inv); +/* Negative test: Invalid data_type with zero-length array */ + num_inv =0 ; +test_err_viommu_invalidate (EOPNOTSUPP , viommu_id, inv_reqs, +IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST_INVALID , +sizeof (*inv_reqs), &num_inv); +assert (!num_inv); +/* Negative test: Invalid data_type */ num_inv =1 ; -test_err_viommu_invalidate (EINVAL , viommu_id, inv_reqs, +test_err_viommu_invalidate (EOPNOTSUPP , viommu_id, inv_reqs,IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST_INVALID ,sizeof (*inv_reqs), &num_inv);assert (!num_inv);
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
Quoted 7 lines · Pranjal Shrivastava
The
IOMMU_HWPT_INVALIDATEuAPI says that an empty request array "can be used to check if the given @data_type is supported or not by kernel", but not what to look for. And the iommu_copy_struct_*() helpers and the Intel VT-d, ARM SMMUv3 and selftest invalidation ops reject unsupported data types with-EINVAL, which means that the request was understood but a field is not correct.
Quoted 6 lines
Return
-EOPNOTSUPPfor an unsupported data type instead and document it as what the probe looks for. Sincearm_vsmmu_alloc_domain_nested()uses the copy helper, it now returns-EOPNOTSUPP, like the other nested domain allocators. Update and extend the selftests accordingly.Signed-off-by Pranjal Shrivastava <[email protected]>