qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Vivek Kasireddy <vivek.kasireddy@intel.com>
To: qemu-devel@nongnu.org
Cc: "Vivek Kasireddy" <vivek.kasireddy@intel.com>,
	"Alex Williamson" <alex.williamson@redhat.com>,
	"Cédric Le Goater" <clg@redhat.com>
Subject: [PATCH v2 07/10] vfio/device: Add support for VFIO_DEVICE_FEATURE_DMA_BUF
Date: Sat,  8 Nov 2025 21:33:50 -0800	[thread overview]
Message-ID: <20251109053801.2267149-8-vivek.kasireddy@intel.com> (raw)
In-Reply-To: <20251109053801.2267149-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.williamson@redhat.com>
Cc: Cédric Le Goater <clg@redhat.com>
Signed-off-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
---
 hw/vfio/device.c              | 40 +++++++++++++++++++++++++++++++++++
 include/hw/vfio/vfio-device.h | 14 ++++++++++++
 2 files changed, 54 insertions(+)

diff --git a/hw/vfio/device.c b/hw/vfio/device.c
index 9ff73f9941..5417142482 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,42 @@ 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;
+    ram_addr_t offset;
+    RAMBlock *rb;
+    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;
+
+    for (i = 0; i < iov_cnt; i++) {
+        rb = qemu_ram_block_from_host(iov[i].iov_base, false, &offset);
+        if (!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



  parent reply	other threads:[~2025-11-09  5:40 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-09  5:33 [PATCH v2 00/10] vfio: Implement VFIO_DEVICE_FEATURE_DMA_BUF and use it in virtio-gpu Vivek Kasireddy
2025-11-09  5:33 ` [PATCH v2 01/10] virtio-gpu: Recreate the resource's dmabuf if new backing is attached Vivek Kasireddy
2025-11-10  4:59   ` Akihiko Odaki
2025-11-09  5:33 ` [PATCH v2 02/10] virtio-gpu: Find hva for Guest's DMA addr associated with a ram device Vivek Kasireddy
2025-11-10  5:00   ` Akihiko Odaki
2025-11-11  4:45   ` Akihiko Odaki
2025-11-12  4:30     ` Kasireddy, Vivek
2025-11-12  8:14       ` Akihiko Odaki
2025-11-13  3:39         ` Kasireddy, Vivek
2025-11-13  4:08           ` Akihiko Odaki
2025-11-17  4:14             ` Kasireddy, Vivek
2025-11-17  6:56               ` Akihiko Odaki
2025-11-18  5:26                 ` Kasireddy, Vivek
2025-11-18  5:37                   ` Akihiko Odaki
2025-11-19  5:42                     ` Kasireddy, Vivek
2025-11-19  5:55                       ` Akihiko Odaki
2025-11-21  6:56                         ` Kasireddy, Vivek
2025-11-21  7:08                           ` Akihiko Odaki
2025-11-09  5:33 ` [PATCH v2 03/10] vfio: Document vfio_device_get_region_info() Vivek Kasireddy
2025-11-09  5:33 ` [PATCH v2 04/10] vfio/region: Add a helper to get region index from memory region Vivek Kasireddy
2025-11-09  5:33 ` [PATCH v2 05/10] vfio/device: Add a helper to lookup VFIODevice " Vivek Kasireddy
2025-11-09  5:33 ` [PATCH v2 06/10] linux-headers: Update vfio.h to include VFIO_DEVICE_FEATURE_DMA_BUF Vivek Kasireddy
2025-11-09  6:49   ` Leon Romanovsky
2025-11-17  9:02     ` Cédric Le Goater
2025-11-09  5:33 ` Vivek Kasireddy [this message]
2025-11-09  5:33 ` [PATCH v2 08/10] virtio-gpu: Rename udmabuf files and helpers to dmabuf Vivek Kasireddy
2025-11-10  5:00   ` Akihiko Odaki
2025-11-09  5:33 ` [PATCH v2 09/10] virtio-gpu-dmabuf: Introduce qemu_iovec_same_memory_regions() Vivek Kasireddy
2025-11-10  5:19   ` Akihiko Odaki
2025-11-12  4:24     ` Kasireddy, Vivek
2025-11-12  8:21       ` Akihiko Odaki
2025-11-13  3:14         ` Kasireddy, Vivek
2025-11-13  3:28           ` Akihiko Odaki
2025-11-09  5:33 ` [PATCH v2 10/10] virtio-gpu-dmabuf: Create dmabuf for blobs associated with VFIO devices Vivek Kasireddy
2025-11-10  4:55   ` Akihiko Odaki
2025-11-12  4:26     ` Kasireddy, Vivek
2025-11-12  8:17       ` Akihiko Odaki
2025-11-13  3:17         ` Kasireddy, Vivek
2025-11-13  3:31           ` Akihiko Odaki
2025-11-17  4:19             ` Kasireddy, Vivek
2025-11-17  7:13               ` Akihiko Odaki
2025-11-18  5:22                 ` Kasireddy, Vivek
2025-11-18  6:02                   ` Akihiko Odaki
2025-11-19  5:33                     ` Kasireddy, Vivek
2025-11-17  9:04 ` [PATCH v2 00/10] vfio: Implement VFIO_DEVICE_FEATURE_DMA_BUF and use it in virtio-gpu Cédric Le Goater

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=20251109053801.2267149-8-vivek.kasireddy@intel.com \
    --to=vivek.kasireddy@intel.com \
    --cc=alex.williamson@redhat.com \
    --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).