Skip to contentSkip to conversation

iommu: Honor iommufd uapi for zero entry_num args

PATCH Superseded Pranjal Shrivastava 3 participants 4 messages Sep 2 – Sep 24 IOMMU

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 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() incorrectly rejects it by returning -EINVAL, breaking the uAPI.

Fix this by returning 0 instead of -EINVAL when entry_num is 0.

Fixes 4f2e59ccb698 ("iommu: Add iommu_copy_struct_from_full_user_array helper")
---
 include/linux/iommu.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/iommu.h +1 -1
index ac43b8b93f14..cf16f9649645 100644
@@ -569,7 +569,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.55.0.966.g6673acef38-goog
#1 lore raw
Quoted 8 lines · Pranjal Shrivastava

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."

Quoted 6 lines

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() incorrectly rejects it by returning -EINVAL, breaking the uAPI.

Fix this by returning 0 instead of -EINVAL when entry_num is 0.

I think the caller of this API should be aware of that and check the entry_num. I have submitted an SMMU patch doing so:
https://lore.kernel.org/linux-iommu/5223275dbc8ef00af233f3fee01efa09e45f26b5.1788127877.git.nicolinc@nvidia.com/

Not very sure this API should be changed though..

Nicolin

#2 lore raw
Quoted 14 lines · Nicolin Chen
Quoted 13 lines · Pranjal Shrivastava

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() incorrectly rejects it by returning -EINVAL, breaking the uAPI.

Quoted 7 lines
Quoted 1 line

Fix this by returning 0 instead of -EINVAL when entry_num is 0.

I think the caller of this API should be aware of that and check the entry_num. I have submitted an SMMU patch doing so:
https://lore.kernel.org/linux-iommu/5223275dbc8ef00af233f3fee01efa09e45f26b5.1788127877.git.nicolinc@nvidia.com/

Not very sure this API should be changed though..

Ack. Yes, I remember the smmuv3 fix. However, I think other IOMMUs would trip over this while adding support for invalidation. If not this API, should we add a note above the cache_invalidate op to remind them?

Furthermore, users of the item-by-item helper (like intel/nested.c) get entry_num == 0 support naturally because their loop:

	for (index = 0; index < array->entry_num; index++)

simply skips execution.

I'm not sure if we should be returning -EINVAL for 0 because nothing's *really* invalid (and we check index >= src_array->entry_num at places where it is). It's like getting -EINVAL from copy_from_user for len=0.

I'm okay either way but it feels wrong to have specific users to check if (entry_num == 0). Would like to discuss this behaviour?

- Praan

#3 lore raw
Quoted 6 lines · Pranjal Shrivastava

I'm not sure if we should be returning -EINVAL for 0 because nothing's *really* invalid (and we check index >= src_array->entry_num at places where it is). It's like getting -EINVAL from copy_from_user for len=0.

I'm okay either way but it feels wrong to have specific users to check if (entry_num == 0). Would like to discuss this behaviour?

That makes sense, but this should be respun on top of the accepted one and remove the redundant arm check..

Signature · 1 line
Jason