[PATCH v3 14/23] iommufd: Add mmap interface

From: Nicolin Chen
Date: Thu May 01 2025 - 19:05:47 EST


For vIOMMU passing through HW resources to user space (VMs), allowing a
guest VM to control the passed through the HW directly by R/W accessing
the hardware MMIO registers, add an mmap infrastructure to map a region
of hardware MMIO pages to user space.

Maintain an mt_mmap per ictx to store iommufd_mmap structure pointers,
for pfn and owner validations. To allow IOMMU drivers to add and delete
mmappable regions onto/from the mt_mmap, add a pair of vIOMMU helpers:
iommufd_viommu_alloc_mmap() and iommufd_viommu_destroy_mmap().

Signed-off-by: Nicolin Chen <nicolinc@xxxxxxxxxx>
---
drivers/iommu/iommufd/iommufd_private.h | 11 +++++
include/linux/iommufd.h | 40 +++++++++++++++
drivers/iommu/iommufd/driver.c | 46 +++++++++++++++++
drivers/iommu/iommufd/main.c | 65 +++++++++++++++++++++++++
4 files changed, 162 insertions(+)

diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h
index 595d5e7021c4..cbeef1161283 100644
--- a/drivers/iommu/iommufd/iommufd_private.h
+++ b/drivers/iommu/iommufd/iommufd_private.h
@@ -7,6 +7,7 @@
#include <linux/iommu.h>
#include <linux/iommufd.h>
#include <linux/iova_bitmap.h>
+#include <linux/maple_tree.h>
#include <linux/rwsem.h>
#include <linux/uaccess.h>
#include <linux/xarray.h>
@@ -44,6 +45,7 @@ struct iommufd_ctx {
struct xarray groups;
wait_queue_head_t destroy_wait;
struct rw_semaphore ioas_creation_lock;
+ struct maple_tree mt_mmap;

struct mutex sw_msi_lock;
struct list_head sw_msi_list;
@@ -55,6 +57,15 @@ struct iommufd_ctx {
struct iommufd_ioas *vfio_ioas;
};

+/* Entry for iommufd_ctx::mt_mmap */
+struct iommufd_mmap {
+ struct iommufd_object *owner;
+
+ /* inclusive pfn range for mmap() */
+ unsigned long pfn_start;
+ unsigned long pfn_end;
+};
+
/*
* The IOVA to PFN map. The map automatically copies the PFNs into multiple
* domains and permits sharing of PFNs between io_pagetable instances. This
diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h
index cbe9910dbdff..e06b6f6704c1 100644
--- a/include/linux/iommufd.h
+++ b/include/linux/iommufd.h
@@ -239,6 +239,11 @@ int _iommufd_object_depend(struct iommufd_object *obj_dependent,
struct iommufd_object *obj_depended);
void _iommufd_object_undepend(struct iommufd_object *obj_dependent,
struct iommufd_object *obj_depended);
+int _iommufd_alloc_mmap(struct iommufd_ctx *ictx, struct iommufd_object *owner,
+ phys_addr_t base, size_t size, unsigned long *immap_id);
+void _iommufd_destroy_mmap(struct iommufd_ctx *ictx,
+ struct iommufd_object *owner,
+ unsigned long immap_id);
struct device *iommufd_viommu_find_dev(struct iommufd_viommu *viommu,
unsigned long vdev_id);
int iommufd_viommu_get_vdev_id(struct iommufd_viommu *viommu,
@@ -271,6 +276,20 @@ _iommufd_object_undepend(struct iommufd_object *obj_dependent,
{
}

+static inline int _iommufd_alloc_mmap(struct iommufd_ctx *ictx,
+ struct iommufd_object *owner,
+ phys_addr_t base, size_t size,
+ unsigned long *immap_id)
+{
+ return -EOPNOTSUPP;
+}
+
+static inline void _iommufd_destroy_mmap(struct iommufd_ctx *ictx,
+ struct iommufd_object *owner,
+ unsigned long immap_id)
+{
+}
+
static inline struct device *
iommufd_viommu_find_dev(struct iommufd_viommu *viommu, unsigned long vdev_id)
{
@@ -387,4 +406,25 @@ static inline int iommufd_viommu_report_event(struct iommufd_viommu *viommu,
_iommufd_object_undepend(&vqueue_dependent->member.obj, \
&vqueue_depended->member.obj); \
})
+
+/*
+ * Helpers for IOMMU driver to alloc/destroy an mmapable area for a structure.
+ * Driver should report the @out_immap_id to user space for an mmap() syscall
+ */
+#define iommufd_viommu_alloc_mmap(viommu, member, base, size, out_immap_id) \
+ ({ \
+ 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, \
+ base, size, out_immap_id); \
+ })
+#define iommufd_viommu_destroy_mmap(viommu, member, immap_id) \
+ ({ \
+ static_assert(__same_type(struct iommufd_viommu, \
+ viommu->member)); \
+ static_assert(offsetof(typeof(*viommu), member.obj) == 0); \
+ _iommufd_destroy_mmap(viommu->member.ictx, &viommu->member.obj,\
+ immap_id); \
+ })
#endif
diff --git a/drivers/iommu/iommufd/driver.c b/drivers/iommu/iommufd/driver.c
index 0bcf0438d255..58dfc7bff210 100644
--- a/drivers/iommu/iommufd/driver.c
+++ b/drivers/iommu/iommufd/driver.c
@@ -78,6 +78,52 @@ void _iommufd_object_undepend(struct iommufd_object *obj_dependent,
}
EXPORT_SYMBOL_NS_GPL(_iommufd_object_undepend, "IOMMUFD");

+/* Driver should use a per-structure helper in include/linux/iommufd.h */
+int _iommufd_alloc_mmap(struct iommufd_ctx *ictx, struct iommufd_object *owner,
+ phys_addr_t base, size_t size, unsigned long *immap_id)
+{
+ struct iommufd_mmap *immap;
+ int rc;
+
+ if (WARN_ON_ONCE(!immap_id))
+ return -EINVAL;
+ if (!PAGE_ALIGNED(base))
+ return -EINVAL;
+ if (!size || !PAGE_ALIGNED(size))
+ return -EINVAL;
+
+ immap = kzalloc(sizeof(*immap), GFP_KERNEL);
+ if (!immap)
+ return -ENOMEM;
+ immap->owner = owner;
+ immap->pfn_start = base >> PAGE_SHIFT;
+ immap->pfn_end = immap->pfn_start + (size >> PAGE_SHIFT) - 1;
+
+ rc = mtree_alloc_range(&ictx->mt_mmap, immap_id, immap, sizeof(immap),
+ 0, LONG_MAX >> PAGE_SHIFT, GFP_KERNEL);
+ if (rc < 0) {
+ kfree(immap);
+ return rc;
+ }
+
+ /* mmap() syscall will right-shift the immap_id to vma->vm_pgoff */
+ *immap_id <<= PAGE_SHIFT;
+ return 0;
+}
+EXPORT_SYMBOL_NS_GPL(_iommufd_alloc_mmap, "IOMMUFD");
+
+/* Driver should use a per-structure helper in include/linux/iommufd.h */
+void _iommufd_destroy_mmap(struct iommufd_ctx *ictx,
+ struct iommufd_object *owner, unsigned long immap_id)
+{
+ struct iommufd_mmap *immap;
+
+ immap = mtree_erase(&ictx->mt_mmap, immap_id >> PAGE_SHIFT);
+ WARN_ON_ONCE(!immap || immap->owner != owner);
+ kfree(immap);
+}
+EXPORT_SYMBOL_NS_GPL(_iommufd_destroy_mmap, "IOMMUFD");
+
/* Caller should xa_lock(&viommu->vdevs) to protect the return value */
struct device *iommufd_viommu_find_dev(struct iommufd_viommu *viommu,
unsigned long vdev_id)
diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c
index 23ed58f1f7b1..040d9ff68f53 100644
--- a/drivers/iommu/iommufd/main.c
+++ b/drivers/iommu/iommufd/main.c
@@ -213,6 +213,7 @@ static int iommufd_fops_open(struct inode *inode, struct file *filp)
xa_init_flags(&ictx->objects, XA_FLAGS_ALLOC1 | XA_FLAGS_ACCOUNT);
xa_init(&ictx->groups);
ictx->file = filp;
+ mt_init_flags(&ictx->mt_mmap, MT_FLAGS_ALLOC_RANGE);
init_waitqueue_head(&ictx->destroy_wait);
mutex_init(&ictx->sw_msi_lock);
INIT_LIST_HEAD(&ictx->sw_msi_list);
@@ -410,11 +411,75 @@ static long iommufd_fops_ioctl(struct file *filp, unsigned int cmd,
return ret;
}

+static void iommufd_fops_vma_open(struct vm_area_struct *vma)
+{
+ struct iommufd_mmap *immap = vma->vm_private_data;
+
+ refcount_inc(&immap->owner->users);
+}
+
+static void iommufd_fops_vma_close(struct vm_area_struct *vma)
+{
+ struct iommufd_mmap *immap = vma->vm_private_data;
+
+ refcount_dec(&immap->owner->users);
+}
+
+static const struct vm_operations_struct iommufd_vma_ops = {
+ .open = iommufd_fops_vma_open,
+ .close = iommufd_fops_vma_close,
+};
+
+/*
+ * Kernel driver must first do iommufd_ctx_alloc_mmap() to register an mmappable
+ * MMIO region to the iommufd core to receive an "immap_id". Then, driver should
+ * report to user space this immap_id and the size of the registered MMIO region
+ * for @vm_pgoff and @size of an mmap() call, via an IOMMU_VIOMMU_ALLOC ioctl in
+ * the output fields of its driver-type data structure.
+ *
+ * Note the @size is allowed to be smaller than the registered size as a partial
+ * mmap starting from the registered base address.
+ */
+static int iommufd_fops_mmap(struct file *filp, struct vm_area_struct *vma)
+{
+ struct iommufd_ctx *ictx = filp->private_data;
+ size_t size = vma->vm_end - vma->vm_start;
+ struct iommufd_mmap *immap;
+ int rc;
+
+ if (!PAGE_ALIGNED(size))
+ return -EINVAL;
+ if (!(vma->vm_flags & VM_SHARED))
+ return -EINVAL;
+ if (vma->vm_flags & VM_EXEC)
+ return -EPERM;
+
+ /* vm_pgoff carries an index (immap_id) to an mtree entry (immap) */
+ immap = mtree_load(&ictx->mt_mmap, vma->vm_pgoff);
+ if (!immap)
+ return -ENXIO;
+ if (size >> PAGE_SHIFT > immap->pfn_end - immap->pfn_start + 1)
+ return -ENXIO;
+
+ vma->vm_pgoff = 0;
+ vma->vm_private_data = immap;
+ vma->vm_ops = &iommufd_vma_ops;
+ vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
+ vm_flags_set(vma, VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP | VM_IO);
+
+ rc = remap_pfn_range(vma, vma->vm_start, immap->pfn_start, size,
+ vma->vm_page_prot);
+ if (!rc) /* vm_ops.open won't be called for mmap itself. */
+ refcount_inc(&immap->owner->users);
+ return rc;
+}
+
static const struct file_operations iommufd_fops = {
.owner = THIS_MODULE,
.open = iommufd_fops_open,
.release = iommufd_fops_release,
.unlocked_ioctl = iommufd_fops_ioctl,
+ .mmap = iommufd_fops_mmap,
};

/**
--
2.43.0