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 v1 6/7] vfio/device: Add support for VFIO_DEVICE_FEATURE_DMA_BUF
Date: Fri,  3 Oct 2025 16:35:59 -0700	[thread overview]
Message-ID: <20251003234138.85820-7-vivek.kasireddy@intel.com> (raw)
In-Reply-To: <20251003234138.85820-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              | 43 +++++++++++++++++++++++++++++++++++
 include/hw/vfio/vfio-device.h |  3 +++
 2 files changed, 46 insertions(+)

diff --git a/hw/vfio/device.c b/hw/vfio/device.c
index 64f8750389..49070929ac 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"
@@ -592,3 +593,45 @@ static VFIODeviceIOOps vfio_device_io_ops_ioctl = {
     .region_read = vfio_device_io_region_read,
     .region_write = vfio_device_io_region_write,
 };
+
+int vfio_device_create_dmabuf(VFIODevice *vdev,
+                              struct iovec *iov, unsigned int iov_cnt)
+{
+    g_autofree struct vfio_device_feature *feature;
+    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++) {
+        rcu_read_lock();
+        rb = qemu_ram_block_from_host(iov[i].iov_base, false, &offset);
+        rcu_read_unlock();
+
+        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 ioctl(vdev->fd, VFIO_DEVICE_FEATURE, feature);
+}
diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h
index bdb106c937..74b3c4eef7 100644
--- a/include/hw/vfio/vfio-device.h
+++ b/include/hw/vfio/vfio-device.h
@@ -279,6 +279,9 @@ int vfio_device_get_irq_info(VFIODevice *vbasedev, int index,
                                 struct vfio_irq_info *info);
 
 int vfio_get_region_index_from_mr(MemoryRegion *mr);
+
+int vfio_device_create_dmabuf(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-10-03 23:45 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-03 23:35 [PATCH v1 0/7] vfio: Implement VFIO_DEVICE_FEATURE_DMA_BUF and use it in virtio-gpu Vivek Kasireddy
2025-10-03 23:35 ` [PATCH v1 1/7] virtio-gpu: Recreate the resource's dmabuf if new backing is attached Vivek Kasireddy
2025-10-03 23:35 ` [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob resources Vivek Kasireddy
2025-10-10  4:19   ` Akihiko Odaki
2025-10-13  6:54     ` Kasireddy, Vivek
2025-10-03 23:35 ` [PATCH v1 3/7] virtio-gpu: Find hva for Guest's DMA addr associated with a ram device Vivek Kasireddy
2025-10-03 23:35 ` [PATCH v1 4/7] vfio/region: Add a helper to get region index from memory region Vivek Kasireddy
2025-10-06  8:26   ` Cédric Le Goater
2025-10-07  4:50     ` Kasireddy, Vivek
2025-10-07  9:05       ` Cédric Le Goater
2025-10-03 23:35 ` [PATCH v1 5/7] linux-headers: Update vfio.h to include VFIO_DEVICE_FEATURE_DMA_BUF Vivek Kasireddy
2025-10-03 23:35 ` Vivek Kasireddy [this message]
2025-10-06  8:24   ` [PATCH v1 6/7] vfio/device: Add support for VFIO_DEVICE_FEATURE_DMA_BUF Cédric Le Goater
2025-10-07  4:48     ` Kasireddy, Vivek
2025-10-03 23:36 ` [PATCH v1 7/7] virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO devices Vivek Kasireddy
2025-10-06 15:59   ` Cédric Le Goater
2025-10-07  4:53     ` Kasireddy, Vivek
2025-10-07  6:51     ` Cédric Le Goater
2025-10-10  4:53   ` Akihiko Odaki
2025-10-13  7:00     ` Kasireddy, Vivek
2025-10-06 15:28 ` [PATCH v1 0/7] vfio: Implement VFIO_DEVICE_FEATURE_DMA_BUF and use it in virtio-gpu Cédric Le Goater
2025-10-07  4:51   ` Kasireddy, Vivek

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=20251003234138.85820-7-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).