Skip to contentSkip to conversation

iommu: Honor iommufd uapi for zero entry_num args

PATCH In review Pranjal Shrivastava 3 participants 8 messages Nov 12 – Nov 16

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

PATCH v2 1/1 iommu: Honor iommufd uapi for zero entry_num args

The IOMMU_HWPT_INVALIDATE ioctl uAPI explicitly allows an empty invalidation request array by setting entry_num == 0. The uAPI documentation in include/uapi/linux/iommufd.h mentions:

" An empty invalidation request array by setting @entry_num==0
is allowed, and @entry_len and @data_uptr would be ignored in this case."

While the core iommufd_hwpt_invalidate() handler honors this by skipping its bounds checks, the generic array copy helper iommu_copy_struct_from_full_user_array() still rejects it by returning -EINVAL, forcing arm_vsmmu_cache_invalidate() to special-case it.

Return 0 instead of -EINVAL when entry_num is 0 and drop the now redundant checks in arm_vsmmu_cache_invalidate().

---
 .../iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c   | 15 +++++----------
 include/linux/iommu.h                             |  5 +++--
 2 files changed, 8 insertions(+), 12 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c +5 -10
index ab1078a97d80..74210867f204 100644
@@ -491,15 +491,6 @@ 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;
-		goto out;
-	}
-
-	/* A zero-length array only probes the type, validated above */
-	if (!array->entry_num)
-		return 0;
-
 	/*
 	 * The core re-invokes this op for the remaining requests, so copy one
 	 * cmdq batch worth of commands into a fixed on-stack buffer rather than
@@ -509,7 +500,11 @@ int arm_vsmmu_cache_invalidate(struct iommufd_viommu *viommu,
 	ret = iommu_copy_struct_from_full_user_array(
 		cmds, sizeof(*cmds), &batch,
 		IOMMU_VIOMMU_INVALIDATE_DATA_ARM_SMMUV3);
-	if (ret)
+	/*
+	 * A zero-length array only probes the data type, which the copy has
+	 * validated; skip issuing a bare CMD_SYNC for it.
+	 */
+	if (ret || !batch.entry_num)
 		goto out;
 
 	/*
include/linux/iommu.h +3 -2
index 05f7cee1375e..5b5e82b21d0b 100644
@@ -556,7 +556,8 @@ static inline int __iommu_copy_struct_from_user_array(
  * @data_type: The data type of the @kdst. Must match with @user_array->type
  *
  * Copy the entire user array. kdst must have room for kdst_entry_size *
- * user_array->entry_num bytes. Return 0 for success, otherwise -error.
+ * user_array->entry_num bytes. An empty array copies nothing, though its
+ * @data_type is still validated. Return 0 for success, otherwise -error.
  */
 static inline int
 iommu_copy_struct_from_full_user_array(void *kdst, size_t kdst_entry_size,
@@ -569,7 +570,7 @@ iommu_copy_struct_from_full_user_array(void *kdst, size_t kdst_entry_size,
 	if (user_array->type != data_type)
 		return -EINVAL;
 	if (!user_array->entry_num)
-		return -EINVAL;
+		return 0;
 	if (likely(user_array->entry_len == kdst_entry_size)) {
 		if (copy_from_user(kdst, user_array->uptr,
 				   user_array->entry_num *
Signature · 1 line
2.56.0.567.gdb80ae8cd5-goog
#1 lore raw
Quoted 11 lines · Pranjal Shrivastava
include/linux/iommu.h +2 -1
@@ -556,7 +556,8 @@ static inline int __iommu_copy_struct_from_user_array(
  * @data_type: The data type of the @kdst. Must match with @user_array->type
  *
  * Copy the entire user array. kdst must have room for kdst_entry_size *
- * user_array->entry_num bytes. Return 0 for success, otherwise -error.
+ * user_array->entry_num bytes. An empty array copies nothing, though its
+ * @data_type is still validated. Return 0 for success, otherwise -error.
  */
 static inline int
 iommu_copy_struct_from_full_user_array(void *kdst, size_t kdst_entry_size,

include/linux/iommu.h iommu_copy_struct_from_full_user_array(void *kdst, size_t kdst_entry_size, · line 573

Quoted 5 lines
include/linux/iommu.h +1 -1
@@ -569,7 +570,7 @@ iommu_copy_struct_from_full_user_array(void *kdst, size_t kdst_entry_size,
 	if (user_array->type != data_type)
 		return -EINVAL;
 	if (!user_array->entry_num)
-		return -EINVAL;
+		return 0;

TBH, this doesn't seem very necessary..

Though I don't have a problem so long as Jason is okay with it..

Yet, this alone would make things inconsistent:
* The other caller in iommufd selftest isn't updated.
* __iommu_copy_struct_from_user_array still rejects !entry_num, and its callers (intel and selftest) aren't updated either.

Also, please don't omit "[email protected]" when sending a patch, as linux-iommu doesn't trigger Sashiko scan.

Nicolin

#2 lore raw
Quoted 20 lines · Nicolin Chen
Quoted 11 lines · Pranjal Shrivastava
include/linux/iommu.h +2 -1
@@ -556,7 +556,8 @@ static inline int __iommu_copy_struct_from_user_array(
  * @data_type: The data type of the @kdst. Must match with @user_array->type
  *
  * Copy the entire user array. kdst must have room for kdst_entry_size *
- * user_array->entry_num bytes. Return 0 for success, otherwise -error.
+ * user_array->entry_num bytes. An empty array copies nothing, though its
+ * @data_type is still validated. Return 0 for success, otherwise -error.
  */
 static inline int
 iommu_copy_struct_from_full_user_array(void *kdst, size_t kdst_entry_size,

include/linux/iommu.h iommu_copy_struct_from_full_user_array(void *kdst, size_t kdst_entry_size, · line 573

Quoted 5 lines
include/linux/iommu.h +1 -1
@@ -569,7 +570,7 @@ iommu_copy_struct_from_full_user_array(void *kdst, size_t kdst_entry_size,
 	if (user_array->type != data_type)
 		return -EINVAL;
 	if (!user_array->entry_num)
-		return -EINVAL;
+		return 0;

TBH, this doesn't seem very necessary..

Quoted 2 lines

Though I don't have a problem so long as Jason is okay with it..

I know it looks unnecessary in-tree, but zero-length is the documented way to check if a @data_type is supported. Intel and the selftest check entry_num before their single-entry copy, but arm, the only caller of the full-array helper, didn't and returned -EINVAL for that probe until your patch [1]. I've seen an issue downstream where a driver called the full-array helper on the array as-is, and the probe failed with -EINVAL.

Quoted 2 lines

Yet, this alone would make things inconsistent:
* The other caller in iommufd selftest isn't updated.

The selftest hasn't used the full-array helper since your patch [2] switched it to iommu_copy_struct_from_user_array(), arm is the only caller now (unless I'm missing something?)

Quoted 2 lines

* __iommu_copy_struct_from_user_array still rejects !entry_num, and its callers (intel and selftest) aren't updated either.

That one copies the entry at @index, and an empty array has no entry, so its callers have to check entry_num before indexing regardless. The full-array helper is more like a zero-length copy_from_user().

Quoted 2 lines

Also, please don't omit "[email protected]" when sending a patch, as linux-iommu doesn't trigger Sashiko scan.

Ack, will keep LKML in Cc.

Thanks,
Praan

[1] https://lore.kernel.org/all/5223275dbc8ef00af233f3fee01efa09e45f26b5.1788127877.git.nicolinc@nvidia.com/
[2] https://lore.kernel.org/all/535f3b97e16c437f1df0c988a96229f623142ee9.1788127877.git.nicolinc@nvidia.com/

#3 lore raw
Quoted 21 lines · Pranjal Shrivastava
Quoted 20 lines · Nicolin Chen
Quoted 11 lines · Pranjal Shrivastava
include/linux/iommu.h +2 -1
@@ -556,7 +556,8 @@ static inline int __iommu_copy_struct_from_user_array(
  * @data_type: The data type of the @kdst. Must match with @user_array->type
  *
  * Copy the entire user array. kdst must have room for kdst_entry_size *
- * user_array->entry_num bytes. Return 0 for success, otherwise -error.
+ * user_array->entry_num bytes. An empty array copies nothing, though its
+ * @data_type is still validated. Return 0 for success, otherwise -error.
  */
 static inline int
 iommu_copy_struct_from_full_user_array(void *kdst, size_t kdst_entry_size,

include/linux/iommu.h iommu_copy_struct_from_full_user_array(void *kdst, size_t kdst_entry_size, · line 573

Quoted 5 lines
include/linux/iommu.h +1 -1
@@ -569,7 +570,7 @@ iommu_copy_struct_from_full_user_array(void *kdst, size_t kdst_entry_size,
 	if (user_array->type != data_type)
 		return -EINVAL;
 	if (!user_array->entry_num)
-		return -EINVAL;
+		return 0;

TBH, this doesn't seem very necessary..

Quoted 5 lines
Quoted 2 lines

Though I don't have a problem so long as Jason is okay with it..

I know it looks unnecessary in-tree, but zero-length is the documented way to check if a @data_type is supported.

Is it? I though it was put in 0 length and look for EOPNOTSUPP, success is not expected.

Jason

#4 lore raw
Quoted 22 lines · Jason Gunthorpe
Quoted 21 lines · Pranjal Shrivastava
Quoted 20 lines · Nicolin Chen
Quoted 11 lines · Pranjal Shrivastava
include/linux/iommu.h +2 -1
@@ -556,7 +556,8 @@ static inline int __iommu_copy_struct_from_user_array(
  * @data_type: The data type of the @kdst. Must match with @user_array->type
  *
  * Copy the entire user array. kdst must have room for kdst_entry_size *
- * user_array->entry_num bytes. Return 0 for success, otherwise -error.
+ * user_array->entry_num bytes. An empty array copies nothing, though its
+ * @data_type is still validated. Return 0 for success, otherwise -error.
  */
 static inline int
 iommu_copy_struct_from_full_user_array(void *kdst, size_t kdst_entry_size,

include/linux/iommu.h iommu_copy_struct_from_full_user_array(void *kdst, size_t kdst_entry_size, · line 573

Quoted 5 lines
include/linux/iommu.h +1 -1
@@ -569,7 +570,7 @@ iommu_copy_struct_from_full_user_array(void *kdst, size_t kdst_entry_size,
 	if (user_array->type != data_type)
 		return -EINVAL;
 	if (!user_array->entry_num)
-		return -EINVAL;
+		return 0;

TBH, this doesn't seem very necessary..

Quoted 9 lines
Quoted 5 lines
Quoted 2 lines

Though I don't have a problem so long as Jason is okay with it..

I know it looks unnecessary in-tree, but zero-length is the documented way to check if a @data_type is supported.

Is it? I though it was put in 0 length and look for EOPNOTSUPP, success is not expected.

Reading the uAPI comment [1] gives me that understanding:

/*
* 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.
*/

Hence, for a supported data_type I expect success..
Also, the selftests check exactly that [2]. An unsupported data_type gets -EINVAL from the drivers today, not -EOPNOTSUPP (happy to switch if you prefer).

Thanks,
Praan

[1] https://elixir.bootlin.com/linux/v7.4-rc1/source/include/uapi/linux/iommufd.h#L987
[2] https://elixir.bootlin.com/linux/v7.4-rc1/source/tools/testing/selftests/iommu/iommufd.c#L532

#5 lore raw
Quoted 35 lines · Pranjal Shrivastava
Quoted 22 lines · Jason Gunthorpe
Quoted 21 lines · Pranjal Shrivastava
Quoted 20 lines · Nicolin Chen
Quoted 11 lines · Pranjal Shrivastava
include/linux/iommu.h +2 -1
@@ -556,7 +556,8 @@ static inline int __iommu_copy_struct_from_user_array(
  * @data_type: The data type of the @kdst. Must match with @user_array->type
  *
  * Copy the entire user array. kdst must have room for kdst_entry_size *
- * user_array->entry_num bytes. Return 0 for success, otherwise -error.
+ * user_array->entry_num bytes. An empty array copies nothing, though its
+ * @data_type is still validated. Return 0 for success, otherwise -error.
  */
 static inline int
 iommu_copy_struct_from_full_user_array(void *kdst, size_t kdst_entry_size,

include/linux/iommu.h iommu_copy_struct_from_full_user_array(void *kdst, size_t kdst_entry_size, · line 573

Quoted 5 lines
include/linux/iommu.h +1 -1
@@ -569,7 +570,7 @@ iommu_copy_struct_from_full_user_array(void *kdst, size_t kdst_entry_size,
 	if (user_array->type != data_type)
 		return -EINVAL;
 	if (!user_array->entry_num)
-		return -EINVAL;
+		return 0;

TBH, this doesn't seem very necessary..

Quoted 9 lines
Quoted 5 lines
Quoted 2 lines

Though I don't have a problem so long as Jason is okay with it..

I know it looks unnecessary in-tree, but zero-length is the documented way to check if a @data_type is supported.

Is it? I though it was put in 0 length and look for EOPNOTSUPP, success is not expected.

Reading the uAPI comment [1] gives me that understanding:

Quoted 8 lines

/*
* 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.
*/

Hence, for a supported data_type I expect success..

No, you can't create most of these viommu types without giving actual data.

The comment is unclear, it should be looking for -EOPNOTSUPP.

Quoted 3 lines

Also, the selftests check exactly that [2]. An unsupported data_type gets -EINVAL from the drivers today, not -EOPNOTSUPP (happy to switch if you prefer).

EINVAL should only mean that the data inside the struct is invalid, but everything else is understood. If the enum itself is not known that has to be EOPNOTSUPP

Thanks,
Jason

#6 lore raw
Quoted 50 lines · Jason Gunthorpe
Quoted 35 lines · Pranjal Shrivastava
Quoted 22 lines · Jason Gunthorpe
Quoted 21 lines · Pranjal Shrivastava
Quoted 20 lines · Nicolin Chen
Quoted 11 lines · Pranjal Shrivastava
include/linux/iommu.h +2 -1
@@ -556,7 +556,8 @@ static inline int __iommu_copy_struct_from_user_array(
  * @data_type: The data type of the @kdst. Must match with @user_array->type
  *
  * Copy the entire user array. kdst must have room for kdst_entry_size *
- * user_array->entry_num bytes. Return 0 for success, otherwise -error.
+ * user_array->entry_num bytes. An empty array copies nothing, though its
+ * @data_type is still validated. Return 0 for success, otherwise -error.
  */
 static inline int
 iommu_copy_struct_from_full_user_array(void *kdst, size_t kdst_entry_size,

include/linux/iommu.h iommu_copy_struct_from_full_user_array(void *kdst, size_t kdst_entry_size, · line 573

Quoted 5 lines
include/linux/iommu.h +1 -1
@@ -569,7 +570,7 @@ iommu_copy_struct_from_full_user_array(void *kdst, size_t kdst_entry_size,
 	if (user_array->type != data_type)
 		return -EINVAL;
 	if (!user_array->entry_num)
-		return -EINVAL;
+		return 0;

TBH, this doesn't seem very necessary..

Quoted 9 lines
Quoted 5 lines
Quoted 2 lines

Though I don't have a problem so long as Jason is okay with it..

I know it looks unnecessary in-tree, but zero-length is the documented way to check if a @data_type is supported.

Is it? I though it was put in 0 length and look for EOPNOTSUPP, success is not expected.

Reading the uAPI comment [1] gives me that understanding:

Quoted 8 lines

/*
* 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.
*/

Hence, for a supported data_type I expect success..

No, you can't create most of these viommu types without giving actual data.

The comment is unclear, it should be looking for -EOPNOTSUPP.

Quoted 8 lines
Quoted 3 lines

Also, the selftests check exactly that [2]. An unsupported data_type gets -EINVAL from the drivers today, not -EOPNOTSUPP (happy to switch if you prefer).

EINVAL should only mean that the data inside the struct is invalid, but everything else is understood. If the enum itself is not known that has to be EOPNOTSUPP

I see! I'll respin the patch to return -EOPNOTSUPP for an unknown data_type (copy helpers, intel/selftest invalidation ops and the selftests) and fix the uAPI comment to say the probe should look for -EOPNOTSUPP. Does that sound fine?

Thanks,
Praan

#7 lore raw
Quoted 9 lines · Pranjal Shrivastava
Quoted 8 lines
Quoted 3 lines

Also, the selftests check exactly that [2]. An unsupported data_type gets -EINVAL from the drivers today, not -EOPNOTSUPP (happy to switch if you prefer).

EINVAL should only mean that the data inside the struct is invalid, but everything else is understood. If the enum itself is not known that has to be EOPNOTSUPP

Quoted 4 lines

I see! I'll respin the patch to return -EOPNOTSUPP for an unknown data_type (copy helpers, intel/selftest invalidation ops and the selftests) and fix the uAPI comment to say the probe should look for -EOPNOTSUPP. Does that sound fine?

Yes

Jason