Skip to contentSkip to conversation

iommu: Clarify uAPI errno for unsupported data types

PATCH In review Pranjal Shrivastava 2 participants 2 messages Nov 19

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

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
index e68b46485095..51c2b13cf280 100644
@@ -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
index df72724242bf..b024688b0bb5 100644
@@ -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
index 037e54f3d2ba..dfe7cdb1b12f 100644
@@ -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
index ef5043752a36..253ac566aaf7 100644
@@ -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
index 010f47f7d3d2..79424a8c1b5a 100644
@@ -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
index 964df8f73610..1e58ab4c292e 100644
@@ -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
Acked-by on v3 1/1
#1 lore raw
Quoted 7 lines · Pranjal Shrivastava

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.

Quoted 6 lines

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.