From: Vivek Kasireddy <vivek.kasireddy@intel.com>
To: qemu-devel@nongnu.org
Cc: "Vivek Kasireddy" <vivek.kasireddy@intel.com>,
"Alex Williamson" <alex@shazbot.org>,
"Cédric Le Goater" <clg@redhat.com>
Subject: [PATCH v3 6/9] vfio/device: Add support for VFIO_DEVICE_FEATURE_DMA_BUF
Date: Fri, 21 Nov 2025 22:46:27 -0800 [thread overview]
Message-ID: <20251122064936.2948632-7-vivek.kasireddy@intel.com> (raw)
In-Reply-To: <20251122064936.2948632-1-vivek.kasireddy@intel.com>
In order to implement VFIO_DEVICE_FEATURE_DMA_BUF, we first need
to identify the VFIO region and index the buffer (represented by
iovec) belongs to and then translate its addresses to offsets
within that region.
The qemu_ram_block_from_host() API gives us both the region and the
offset info we need to populate the dma ranges in order to invoke
this feature.
Cc: Alex Williamson <alex@shazbot.org>
Cc: Cédric Le Goater <clg@redhat.com>
Signed-off-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
---
hw/vfio/device.c | 45 +++++++++++++++++++++++++++++++++++
include/hw/vfio/vfio-device.h | 14 +++++++++++
2 files changed, 59 insertions(+)
diff --git a/hw/vfio/device.c b/hw/vfio/device.c
index 9ff73f9941..0679e55e8c 100644
--- a/hw/vfio/device.c
+++ b/hw/vfio/device.c
@@ -21,6 +21,7 @@
#include "qemu/osdep.h"
#include <sys/ioctl.h>
+#include "system/ramblock.h"
#include "hw/vfio/vfio-device.h"
#include "hw/vfio/pci.h"
#include "hw/hw.h"
@@ -615,3 +616,47 @@ VFIODevice *vfio_device_lookup(MemoryRegion *mr)
}
return NULL;
}
+
+int vfio_device_create_dmabuf_fd(VFIODevice *vbasedev,
+ struct iovec *iov, unsigned int iov_cnt)
+{
+ g_autofree struct vfio_device_feature *feature = NULL;
+ struct vfio_device_feature_dma_buf *dma_buf;
+ RAMBlock *rb, *first_rb;
+ ram_addr_t offset;
+ size_t argsz;
+ int i, index;
+
+ argsz = sizeof(*feature) + sizeof (*dma_buf) +
+ sizeof(struct vfio_region_dma_range) * iov_cnt;
+ feature = g_malloc0(argsz);
+ dma_buf = (struct vfio_device_feature_dma_buf *)feature->data;
+
+ first_rb = qemu_ram_block_from_host(iov[0].iov_base, false, &offset);
+ if (!first_rb) {
+ return -1;
+ }
+
+ for (i = 0; i < iov_cnt; i++) {
+ rb = qemu_ram_block_from_host(iov[i].iov_base, false, &offset);
+ if (rb != first_rb) {
+ return -1;
+ }
+
+ index = vfio_get_region_index_from_mr(rb->mr);
+ if (index < 0) {
+ return -1;
+ }
+
+ dma_buf->region_index = index;
+ dma_buf->dma_ranges[i].offset = offset;
+ dma_buf->dma_ranges[i].length = iov[i].iov_len;
+ }
+
+ dma_buf->nr_ranges = iov_cnt;
+ dma_buf->open_flags = O_RDONLY | O_CLOEXEC;
+ feature->argsz = argsz;
+ feature->flags = VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_DMA_BUF;
+
+ return vbasedev->io_ops->device_feature(vbasedev, feature);
+}
diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h
index 2f8087f133..7fc2912f15 100644
--- a/include/hw/vfio/vfio-device.h
+++ b/include/hw/vfio/vfio-device.h
@@ -309,6 +309,20 @@ int vfio_get_region_index_from_mr(MemoryRegion *mr);
* Returns the VFIO device if found or NULL.
*/
VFIODevice *vfio_device_lookup(MemoryRegion *mr);
+
+/**
+ * Create and return a dmabuf fd by first translating the addresses in the
+ * iovec array into VFIO region offsets and then invoking the
+ * VFIO_DEVICE_FEATURE_DMA_BUF feature.
+ *
+ * @vbasedev: #VFIODevice to use
+ * @iov: array of iovec entries associated with a buffer
+ * @iov_cnt: number of entries in the iovec array
+ *
+ * Returns the newly created dmabuf fd or -1 on error.
+ */
+int vfio_device_create_dmabuf_fd(VFIODevice *vbasedev,
+ struct iovec *iov, unsigned int iov_cnt);
#endif
/* Returns 0 on success, or a negative errno. */
--
2.50.1
next prev parent reply other threads:[~2025-11-22 7:47 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-22 6:46 [PATCH v3 0/9] vfio: Implement VFIO_DEVICE_FEATURE_DMA_BUF and use it in virtio-gpu Vivek Kasireddy
2025-11-22 6:46 ` [PATCH v3 1/9] virtio-gpu: Recreate the resource's dmabuf if new backing is attached Vivek Kasireddy
2025-11-22 6:46 ` [PATCH v3 2/9] virtio-gpu: Find hva for Guest's DMA addr associated with a ram device Vivek Kasireddy
2025-11-22 6:46 ` [PATCH v3 3/9] vfio: Document vfio_device_get_region_info() Vivek Kasireddy
2025-11-22 6:46 ` [PATCH v3 4/9] vfio/region: Add a helper to get region index from memory region Vivek Kasireddy
2025-11-22 6:46 ` [PATCH v3 5/9] vfio/device: Add a helper to lookup VFIODevice " Vivek Kasireddy
2025-11-22 6:46 ` Vivek Kasireddy [this message]
2025-11-22 6:46 ` [PATCH v3 7/9] virtio-gpu: Rename udmabuf files and helpers to dmabuf Vivek Kasireddy
2025-11-22 6:46 ` [PATCH v3 8/9] virtio-gpu-dmabuf: Introduce ram_block_is_memfd_backed() helper Vivek Kasireddy
2025-11-22 6:46 ` [PATCH v3 9/9] virtio-gpu-dmabuf: Create dmabuf for blobs associated with VFIO devices Vivek Kasireddy
2025-11-22 7:25 ` Akihiko Odaki
2025-11-23 6:17 ` Kasireddy, Vivek
2025-11-23 8:36 ` Akihiko Odaki
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251122064936.2948632-7-vivek.kasireddy@intel.com \
--to=vivek.kasireddy@intel.com \
--cc=alex@shazbot.org \
--cc=clg@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).