* [PATCH v2] iommu: Honor iommufd uapi for zero entry_num args
@ 2026-11-12 15:10 Pranjal Shrivastava
2026-11-13 17:54 ` Nicolin Chen
0 siblings, 1 reply; 8+ messages in thread
From: Pranjal Shrivastava @ 2026-11-12 15:10 UTC (permalink / raw)
To: Jason Gunthorpe, Nicolin Chen
Cc: Kevin Tian, Will Deacon, Robin Murphy, Joerg Roedel, Peter Shier,
iommu, 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() 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().
Signed-off-by: Pranjal Shrivastava <praan@google.com>
---
.../iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c | 15 +++++----------
include/linux/iommu.h | 5 +++--
2 files changed, 8 insertions(+), 12 deletions(-)
diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c
index ab1078a97d80..74210867f204 100644
--- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c
+++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c
@@ -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;
/*
diff --git a/include/linux/iommu.h b/include/linux/iommu.h
index 05f7cee1375e..5b5e82b21d0b 100644
--- a/include/linux/iommu.h
+++ b/include/linux/iommu.h
@@ -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 *
--
2.56.0.567.gdb80ae8cd5-goog
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v2] iommu: Honor iommufd uapi for zero entry_num args
2026-11-12 15:10 [PATCH v2] iommu: Honor iommufd uapi for zero entry_num args Pranjal Shrivastava
@ 2026-11-13 17:54 ` Nicolin Chen
2026-11-16 11:27 ` Pranjal Shrivastava
0 siblings, 1 reply; 8+ messages in thread
From: Nicolin Chen @ 2026-11-13 17:54 UTC (permalink / raw)
To: Pranjal Shrivastava
Cc: Jason Gunthorpe, Kevin Tian, Will Deacon, Robin Murphy,
Joerg Roedel, Peter Shier, iommu
On Thu, Nov 12, 2026 at 03:10:39PM +0000, Pranjal Shrivastava wrote:
> @@ -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;
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 "linux-kernel@vger.kernel.org" when sending
a patch, as linux-iommu doesn't trigger Sashiko scan.
Nicolin
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2] iommu: Honor iommufd uapi for zero entry_num args
2026-11-13 17:54 ` Nicolin Chen
@ 2026-11-16 11:27 ` Pranjal Shrivastava
2026-11-16 13:08 ` Jason Gunthorpe
0 siblings, 1 reply; 8+ messages in thread
From: Pranjal Shrivastava @ 2026-11-16 11:27 UTC (permalink / raw)
To: Nicolin Chen
Cc: Jason Gunthorpe, Kevin Tian, Will Deacon, Robin Murphy,
Joerg Roedel, Peter Shier, iommu
On Fri, Nov 13, 2026 at 09:54:45AM -0800, Nicolin Chen wrote:
> On Thu, Nov 12, 2026 at 03:10:39PM +0000, Pranjal Shrivastava wrote:
> > @@ -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;
>
> TBH, this doesn't seem very necessary..
>
> 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.
> 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?)
> * __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().
> Also, please don't omit "linux-kernel@vger.kernel.org" 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/
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2] iommu: Honor iommufd uapi for zero entry_num args
2026-11-16 11:27 ` Pranjal Shrivastava
@ 2026-11-16 13:08 ` Jason Gunthorpe
2026-11-16 20:21 ` Pranjal Shrivastava
0 siblings, 1 reply; 8+ messages in thread
From: Jason Gunthorpe @ 2026-11-16 13:08 UTC (permalink / raw)
To: Pranjal Shrivastava
Cc: Nicolin Chen, Kevin Tian, Will Deacon, Robin Murphy, Joerg Roedel,
Peter Shier, iommu
On Mon, Nov 16, 2026 at 11:27:20AM +0000, Pranjal Shrivastava wrote:
> On Fri, Nov 13, 2026 at 09:54:45AM -0800, Nicolin Chen wrote:
> > On Thu, Nov 12, 2026 at 03:10:39PM +0000, Pranjal Shrivastava wrote:
> > > @@ -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;
> >
> > TBH, this doesn't seem very necessary..
> >
> > 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
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2] iommu: Honor iommufd uapi for zero entry_num args
2026-11-16 13:08 ` Jason Gunthorpe
@ 2026-11-16 20:21 ` Pranjal Shrivastava
2026-11-16 20:23 ` Jason Gunthorpe
0 siblings, 1 reply; 8+ messages in thread
From: Pranjal Shrivastava @ 2026-11-16 20:21 UTC (permalink / raw)
To: Jason Gunthorpe
Cc: Nicolin Chen, Kevin Tian, Will Deacon, Robin Murphy, Joerg Roedel,
Peter Shier, iommu
On Mon, Nov 16, 2026 at 09:08:32AM -0400, Jason Gunthorpe wrote:
> On Mon, Nov 16, 2026 at 11:27:20AM +0000, Pranjal Shrivastava wrote:
> > On Fri, Nov 13, 2026 at 09:54:45AM -0800, Nicolin Chen wrote:
> > > On Thu, Nov 12, 2026 at 03:10:39PM +0000, Pranjal Shrivastava wrote:
> > > > @@ -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;
> > >
> > > TBH, this doesn't seem very necessary..
> > >
> > > 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
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2] iommu: Honor iommufd uapi for zero entry_num args
2026-11-16 20:21 ` Pranjal Shrivastava
@ 2026-11-16 20:23 ` Jason Gunthorpe
2026-11-16 20:31 ` Pranjal Shrivastava
0 siblings, 1 reply; 8+ messages in thread
From: Jason Gunthorpe @ 2026-11-16 20:23 UTC (permalink / raw)
To: Pranjal Shrivastava
Cc: Nicolin Chen, Kevin Tian, Will Deacon, Robin Murphy, Joerg Roedel,
Peter Shier, iommu
On Mon, Nov 16, 2026 at 08:21:11PM +0000, Pranjal Shrivastava wrote:
> On Mon, Nov 16, 2026 at 09:08:32AM -0400, Jason Gunthorpe wrote:
> > On Mon, Nov 16, 2026 at 11:27:20AM +0000, Pranjal Shrivastava wrote:
> > > On Fri, Nov 13, 2026 at 09:54:45AM -0800, Nicolin Chen wrote:
> > > > On Thu, Nov 12, 2026 at 03:10:39PM +0000, Pranjal Shrivastava wrote:
> > > > > @@ -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;
> > > >
> > > > TBH, this doesn't seem very necessary..
> > > >
> > > > 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..
No, you can't create most of these viommu types without giving actual
data.
The comment is unclear, it should be looking for -EOPNOTSUPP.
> 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
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2] iommu: Honor iommufd uapi for zero entry_num args
2026-11-16 20:23 ` Jason Gunthorpe
@ 2026-11-16 20:31 ` Pranjal Shrivastava
2026-11-16 23:41 ` Jason Gunthorpe
0 siblings, 1 reply; 8+ messages in thread
From: Pranjal Shrivastava @ 2026-11-16 20:31 UTC (permalink / raw)
To: Jason Gunthorpe
Cc: Nicolin Chen, Kevin Tian, Will Deacon, Robin Murphy, Joerg Roedel,
Peter Shier, iommu
On Mon, Nov 16, 2026 at 04:23:54PM -0400, Jason Gunthorpe wrote:
> On Mon, Nov 16, 2026 at 08:21:11PM +0000, Pranjal Shrivastava wrote:
> > On Mon, Nov 16, 2026 at 09:08:32AM -0400, Jason Gunthorpe wrote:
> > > On Mon, Nov 16, 2026 at 11:27:20AM +0000, Pranjal Shrivastava wrote:
> > > > On Fri, Nov 13, 2026 at 09:54:45AM -0800, Nicolin Chen wrote:
> > > > > On Thu, Nov 12, 2026 at 03:10:39PM +0000, Pranjal Shrivastava wrote:
> > > > > > @@ -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;
> > > > >
> > > > > TBH, this doesn't seem very necessary..
> > > > >
> > > > > 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..
>
> No, you can't create most of these viommu types without giving actual
> data.
>
> The comment is unclear, it should be looking for -EOPNOTSUPP.
>
> > 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
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2] iommu: Honor iommufd uapi for zero entry_num args
2026-11-16 20:31 ` Pranjal Shrivastava
@ 2026-11-16 23:41 ` Jason Gunthorpe
0 siblings, 0 replies; 8+ messages in thread
From: Jason Gunthorpe @ 2026-11-16 23:41 UTC (permalink / raw)
To: Pranjal Shrivastava
Cc: Nicolin Chen, Kevin Tian, Will Deacon, Robin Murphy, Joerg Roedel,
Peter Shier, iommu
On Mon, Nov 16, 2026 at 08:31:35PM +0000, Pranjal Shrivastava wrote:
> > > 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?
Yes
Jason
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-11-16 23:41 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-12 15:10 [PATCH v2] iommu: Honor iommufd uapi for zero entry_num args Pranjal Shrivastava
2026-11-13 17:54 ` Nicolin Chen
2026-11-16 11:27 ` Pranjal Shrivastava
2026-11-16 13:08 ` Jason Gunthorpe
2026-11-16 20:21 ` Pranjal Shrivastava
2026-11-16 20:23 ` Jason Gunthorpe
2026-11-16 20:31 ` Pranjal Shrivastava
2026-11-16 23:41 ` Jason Gunthorpe
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.