* [PATCH] iommu: Honor iommufd uapi for zero entry_num args
@ 2026-09-02 12:43 Pranjal Shrivastava
2026-09-02 15:13 ` Nicolin Chen
0 siblings, 1 reply; 3+ messages in thread
From: Pranjal Shrivastava @ 2026-09-02 12:43 UTC (permalink / raw)
To: iommu
Cc: Joerg Roedel, Will Deacon, Jason Gunthorpe, Kevin Tian,
Nicolin Chen, Samiullah Khawaja, Peter Shier, 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.
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")
Signed-off-by: Pranjal Shrivastava <praan@google.com>
---
include/linux/iommu.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/include/linux/iommu.h b/include/linux/iommu.h
index ac43b8b93f14..cf16f9649645 100644
--- a/include/linux/iommu.h
+++ b/include/linux/iommu.h
@@ -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 *
--
2.55.0.966.g6673acef38-goog
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] iommu: Honor iommufd uapi for zero entry_num args
2026-09-02 12:43 [PATCH] iommu: Honor iommufd uapi for zero entry_num args Pranjal Shrivastava
@ 2026-09-02 15:13 ` Nicolin Chen
2026-09-02 16:04 ` Pranjal Shrivastava
0 siblings, 1 reply; 3+ messages in thread
From: Nicolin Chen @ 2026-09-02 15:13 UTC (permalink / raw)
To: Pranjal Shrivastava
Cc: iommu, Joerg Roedel, Will Deacon, Jason Gunthorpe, Kevin Tian,
Samiullah Khawaja, Peter Shier
On Wed, Sep 02, 2026 at 12:43:33PM +0000, Pranjal Shrivastava wrote:
> 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.
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] iommu: Honor iommufd uapi for zero entry_num args
2026-09-02 15:13 ` Nicolin Chen
@ 2026-09-02 16:04 ` Pranjal Shrivastava
0 siblings, 0 replies; 3+ messages in thread
From: Pranjal Shrivastava @ 2026-09-02 16:04 UTC (permalink / raw)
To: Nicolin Chen
Cc: iommu, Joerg Roedel, Will Deacon, Jason Gunthorpe, Kevin Tian,
Samiullah Khawaja, Peter Shier
On Wed, Sep 02, 2026 at 08:13:44AM -0700, Nicolin Chen wrote:
> On Wed, Sep 02, 2026 at 12:43:33PM +0000, Pranjal Shrivastava wrote:
> > 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.
>
> 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
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-09-02 16:04 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-02 12:43 [PATCH] iommu: Honor iommufd uapi for zero entry_num args Pranjal Shrivastava
2026-09-02 15:13 ` Nicolin Chen
2026-09-02 16:04 ` Pranjal Shrivastava
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox