On 10/7/26 22:09, Vivek Kasireddy wrote:
In order to mmap a dmabuf, we first need to create a placeholder
mapping that would fit the entire size of the dmabuf. This mapping
would then be replaced with smaller mappings of individual dmabuf
segments.
Cc: Alex Williamson <[email protected]>
Cc: Cédric Le Goater <[email protected]>
Signed-off-by: Vivek Kasireddy <[email protected]>
---
include/hw/vfio/vfio-device.h | 15 ++++++++++
hw/vfio/device.c | 55 +++++++++++++++++++++++++++++++++++
hw/vfio/dmabuf-stubs.c | 7 +++++
3 files changed, 77 insertions(+)
diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h
index c05430be8a..4b8fb62056 100644
--- a/include/hw/vfio/vfio-device.h
+++ b/include/hw/vfio/vfio-device.h
@@ -345,6 +345,21 @@ int vfio_device_get_irq_info(VFIODevice *vbasedev, int
index,
*/
int vfio_device_create_dmabuf_fd(struct iovec *iov, unsigned int iov_cnt,
Error **errp);
+
+/**
+ * Mmap a dmabuf by first translating the addresses in the iovec
+ * array into VFIO region offsets and then creating a placeholder
+ * mapping that would be replaced later with mappings that
+ * correspond to the dmabuf segments.
+ *
+ * @iov: array of iovec entries associated with the dmabuf
+ * @iov_cnt: number of entries in the iovec array
+ * @errp: pointer to Error*, to store an error if it happens.
+ *
+ * Returns the mapped pointer on success and NULL on error.
+ */
+void *vfio_device_mmap_dmabuf(struct iovec *iov, unsigned int iov_cnt,
+ Error **errp);
#endif
/* Returns 0 on success, or a negative errno. */
diff --git a/hw/vfio/device.c b/hw/vfio/device.c
index d95503a5f1..5bc0443764 100644
--- a/hw/vfio/device.c
+++ b/hw/vfio/device.c
@@ -776,3 +776,58 @@ int vfio_device_create_dmabuf_fd(struct iovec *iov,
unsigned int iov_cnt,
}
return ret;
}
+
+void *vfio_device_mmap_dmabuf(struct iovec *iov, unsigned int iov_cnt,
+ Error **errp)
+{
+ size_t total_size = iov_size(iov, iov_cnt);
+ struct vfio_region_info *info = NULL;
+ ram_addr_t offset, len = 0;
+ VFIODevice *vbasedev;
+ VFIORegion *region;
+ RAMBlock *first_rb;
+ void *map, *submap;
+ int i;
+
+ if (!vfio_device_lookup(iov, &vbasedev, &first_rb, errp)) {
+ return NULL;
+ }
+
+ /*
+ * We first reserve a contiguous chunk of address space for the entire
+ * dmabuf, then replace it with smaller mappings that correspond to the
+ * individual segments of the dmabuf.
+ */
+ map = mmap(NULL, total_size, PROT_NONE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+ if (map == MAP_FAILED) {
+ error_setg_errno(errp, errno, "Could not reserve placeholder mapping");
+ return NULL;
+ }
+
+ for (i = 0; i < iov_cnt; i++) {
+ if (!vfio_region_lookup(iov[i].iov_base, ®ion,
+ first_rb, &offset, errp)) {
+ goto err;
+ }
+
+ if (vfio_device_get_region_info(vbasedev, region->nr, &info)) {
+ error_setg(errp, "Cannot find region info for dmabuf segment\n");
no \n
+ goto err;
+ }
+
+ submap = mmap(map + len, iov[i].iov_len, PROT_READ,
+ MAP_SHARED | MAP_FIXED, vbasedev->fd,
+ info->offset + offset);
+ if (submap == MAP_FAILED) {
+ error_setg_errno(errp, errno, "Could not mmap dmabuf segment\n");
again.
+ goto err;
+ }
+ len += iov[i].iov_len;
+ }
+ return map;
+err:
+ munmap(map, total_size);
+ error_prepend(errp, "VFIO dmabuf mmap failed: ");
+ return NULL;
+}
+
diff --git a/hw/vfio/dmabuf-stubs.c b/hw/vfio/dmabuf-stubs.c
index fb5318e929..b9e03a162a 100644
--- a/hw/vfio/dmabuf-stubs.c
+++ b/hw/vfio/dmabuf-stubs.c
@@ -15,3 +15,10 @@ int vfio_device_create_dmabuf_fd(struct iovec *iov, unsigned
int iov_cnt,
error_setg(errp, "VFIO dmabuf support is not enabled");
return VFIO_DMABUF_CREATE_ERR_UNSPEC;
}
+
+void *vfio_device_mmap_dmabuf(struct iovec *iov, unsigned int iov_cnt,
+ Error **errp)
+{
+ error_setg(errp, "VFIO mmap dmabuf support is not enabled");
+ return NULL;
+}