Skip to content

Commit e9d36c0

Browse files
nicolincjgunthorpe
authored andcommitted
iommu: Add iommu_copy_struct_from_user helper
Wrap up the data type/pointer/len sanity and a copy_struct_from_user call for iommu drivers to copy driver specific data via struct iommu_user_data. And expect it to be used in the domain_alloc_user op for example. Link: https://lore.kernel.org/r/20231026043938.63898-9-yi.l.liu@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> Reviewed-by: Kevin Tian <kevin.tian@intel.com> Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
1 parent bd529db commit e9d36c0

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

include/linux/iommu.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,46 @@ struct iommu_user_data {
280280
size_t len;
281281
};
282282

283+
/**
284+
* __iommu_copy_struct_from_user - Copy iommu driver specific user space data
285+
* @dst_data: Pointer to an iommu driver specific user data that is defined in
286+
* include/uapi/linux/iommufd.h
287+
* @src_data: Pointer to a struct iommu_user_data for user space data info
288+
* @data_type: The data type of the @dst_data. Must match with @src_data.type
289+
* @data_len: Length of current user data structure, i.e. sizeof(struct _dst)
290+
* @min_len: Initial length of user data structure for backward compatibility.
291+
* This should be offsetofend using the last member in the user data
292+
* struct that was initially added to include/uapi/linux/iommufd.h
293+
*/
294+
static inline int __iommu_copy_struct_from_user(
295+
void *dst_data, const struct iommu_user_data *src_data,
296+
unsigned int data_type, size_t data_len, size_t min_len)
297+
{
298+
if (src_data->type != data_type)
299+
return -EINVAL;
300+
if (WARN_ON(!dst_data || !src_data))
301+
return -EINVAL;
302+
if (src_data->len < min_len || data_len < src_data->len)
303+
return -EINVAL;
304+
return copy_struct_from_user(dst_data, data_len, src_data->uptr,
305+
src_data->len);
306+
}
307+
308+
/**
309+
* iommu_copy_struct_from_user - Copy iommu driver specific user space data
310+
* @kdst: Pointer to an iommu driver specific user data that is defined in
311+
* include/uapi/linux/iommufd.h
312+
* @user_data: Pointer to a struct iommu_user_data for user space data info
313+
* @data_type: The data type of the @kdst. Must match with @user_data->type
314+
* @min_last: The last memember of the data structure @kdst points in the
315+
* initial version.
316+
* Return 0 for success, otherwise -error.
317+
*/
318+
#define iommu_copy_struct_from_user(kdst, user_data, data_type, min_last) \
319+
__iommu_copy_struct_from_user(kdst, user_data, data_type, \
320+
sizeof(*kdst), \
321+
offsetofend(typeof(*kdst), min_last))
322+
283323
/**
284324
* struct iommu_ops - iommu ops and capabilities
285325
* @capable: check capability

0 commit comments

Comments
 (0)