Re: [PATCH v5 17/29] iommufd: Add mmap interface

Jason Gunthorpe
2025-05-30T16:47:24Z

On Sat, May 17, 2025 at 08:21:34PM -0700, Nicolin Chen wrote:
> +/*
> + * Helpers for IOMMU driver to alloc/destroy an mmapable area for a structure.
> + *
> + * To support an mmappable MMIO region, kernel driver must first register it to
> + * iommufd core to allocate an @out_offset, in the context of an driver-struct
> + * allocation (e.g. viommu_alloc op). Then, it should report to user space this
> + * @out_offset and the @length of the MMIO region for mmap syscall.
> + */
> +#define iommufd_viommu_alloc_mmap(viommu, member, mmio, length, out_offset) \
> + ({ \
> + static_assert(__same_type(struct iommufd_viommu, \
> + viommu->member)); \
> + static_assert(offsetof(typeof(*viommu), member.obj) == 0); \
> + _iommufd_alloc_mmap(viommu->member.ictx, &viommu->member.obj, \
> + mmio, length, out_offset); \
> + })

Why is this like this? It's weird, just

static inline int iommufd_alloc_viommu_mmap(struct iommufd_viommu *viommu,
phys_addr_t mmio_addr, size_t length,
unsigned long *offset);

> +/* The vm_pgoff must be pre-allocated from mt_mmap, and given to user space */
> +static int iommufd_fops_mmap(struct file *filp, struct vm_area_struct *vma)
> +{
> + struct iommufd_ctx *ictx = filp->private_data;
> + size_t length = vma->vm_end - vma->vm_start;
> + struct iommufd_mmap *immap;
> + int rc;
> +
> + if (!PAGE_ALIGNED(length))
> + return -EINVAL;
> + if (!(vma->vm_flags & VM_SHARED))
> + return -EINVAL;
> + if (vma->vm_flags & VM_EXEC)
> + return -EPERM;
> +
> + /* vma->vm_pgoff carries an index to an mtree entry (immap) */
> + immap = mtree_load(&ictx->mt_mmap, vma->vm_pgoff);
> + if (!immap)
> + return -ENXIO;
> + /* Validate the vm_pgoff and length against the registered region */
> + if (vma->vm_pgoff != immap->startp)
> + return -ENXIO;

This check seems redundant

Jason