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

Nicolin Chen
2025-05-30T17:18:08Z

On Fri, May 30, 2025 at 01:29:45PM -0300, Jason Gunthorpe wrote:
> 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

I was trying to follow the pattern of the other two macros that
asks for driver-level structure. But it seems not necessary, as
we have iommufd_hw_queue_alloc taking the core-level viommu..

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

OK.

> > +/* 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

Hmm, I was trying to follow your remarks:
"This needs to validate that vm_pgoff is at the start of the immap"
https://lore.kernel.org/all/20250515164717.GL382960@nvidia.com/

I also added a selftest coverage for this check.

Thanks
Nicolin