Skip to content

Commit 7778511

Browse files
nicolincjgunthorpe
authored andcommitted
iommu: Add iommu_copy_struct_from_user_array helper
Wrap up the data pointer/num sanity and __iommu_copy_struct_from_user() call for iommu drivers to copy driver specific data at a specific location in the struct iommu_user_data_array. And expect it to be used in cache_invalidate_user ops for example. Link: https://lore.kernel.org/r/20240111041015.47920-4-yi.l.liu@intel.com Reviewed-by: Kevin Tian <kevin.tian@intel.com> Signed-off-by: Nicolin Chen <nicolinc@nvidia.com> Co-developed-by: Yi Liu <yi.l.liu@intel.com> Signed-off-by: Yi Liu <yi.l.liu@intel.com> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
1 parent 8c6eaba commit 7778511

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

include/linux/iommu.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,57 @@ static inline int __iommu_copy_struct_from_user(
341341
sizeof(*kdst), \
342342
offsetofend(typeof(*kdst), min_last))
343343

344+
/**
345+
* __iommu_copy_struct_from_user_array - Copy iommu driver specific user space
346+
* data from an iommu_user_data_array
347+
* @dst_data: Pointer to an iommu driver specific user data that is defined in
348+
* include/uapi/linux/iommufd.h
349+
* @src_array: Pointer to a struct iommu_user_data_array for a user space array
350+
* @data_type: The data type of the @dst_data. Must match with @src_array.type
351+
* @index: Index to the location in the array to copy user data from
352+
* @data_len: Length of current user data structure, i.e. sizeof(struct _dst)
353+
* @min_len: Initial length of user data structure for backward compatibility.
354+
* This should be offsetofend using the last member in the user data
355+
* struct that was initially added to include/uapi/linux/iommufd.h
356+
*/
357+
static inline int __iommu_copy_struct_from_user_array(
358+
void *dst_data, const struct iommu_user_data_array *src_array,
359+
unsigned int data_type, unsigned int index, size_t data_len,
360+
size_t min_len)
361+
{
362+
struct iommu_user_data src_data;
363+
364+
if (WARN_ON(!src_array || index >= src_array->entry_num))
365+
return -EINVAL;
366+
if (!src_array->entry_num)
367+
return -EINVAL;
368+
src_data.uptr = src_array->uptr + src_array->entry_len * index;
369+
src_data.len = src_array->entry_len;
370+
src_data.type = src_array->type;
371+
372+
return __iommu_copy_struct_from_user(dst_data, &src_data, data_type,
373+
data_len, min_len);
374+
}
375+
376+
/**
377+
* iommu_copy_struct_from_user_array - Copy iommu driver specific user space
378+
* data from an iommu_user_data_array
379+
* @kdst: Pointer to an iommu driver specific user data that is defined in
380+
* include/uapi/linux/iommufd.h
381+
* @user_array: Pointer to a struct iommu_user_data_array for a user space
382+
* array
383+
* @data_type: The data type of the @kdst. Must match with @user_array->type
384+
* @index: Index to the location in the array to copy user data from
385+
* @min_last: The last member of the data structure @kdst points in the
386+
* initial version.
387+
* Return 0 for success, otherwise -error.
388+
*/
389+
#define iommu_copy_struct_from_user_array(kdst, user_array, data_type, index, \
390+
min_last) \
391+
__iommu_copy_struct_from_user_array( \
392+
kdst, user_array, data_type, index, sizeof(*(kdst)), \
393+
offsetofend(typeof(*(kdst)), min_last))
394+
344395
/**
345396
* struct iommu_ops - iommu ops and capabilities
346397
* @capable: check capability

0 commit comments

Comments
 (0)