[PATCH v6 11/11] virtio-gpu-dmabuf: Create dmabuf for blobs associated with VFIO devices

Vivek Kasireddy posted 11 patches 13 hours ago
Maintainers: Paolo Bonzini <pbonzini@redhat.com>, "Alex Bennée" <alex.bennee@linaro.org>, Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp>, Dmitry Osipenko <dmitry.osipenko@collabora.com>, "Michael S. Tsirkin" <mst@redhat.com>, Alex Williamson <alex@shazbot.org>, "Cédric Le Goater" <clg@redhat.com>
[PATCH v6 11/11] virtio-gpu-dmabuf: Create dmabuf for blobs associated with VFIO devices
Posted by Vivek Kasireddy 13 hours ago
In addition to memfd, a blob resource can also have its backing
storage in a VFIO device region. Since, there is no effective way
to determine where the backing storage is located, we first try to
create a dmabuf assuming it is in memfd. If that fails, we try to
create a dmabuf assuming it is in VFIO device region.

So, we first call virtio_gpu_create_udmabuf() to check if the blob's
backing storage is located in a memfd or not. If it is not, we invoke
the vfio_device_create_dmabuf() API which identifies the right VFIO
device and eventually creates a dmabuf fd.

Note that in virtio_gpu_remap_dmabuf(), we first try to test if
the VFIO dmabuf exporter supports mmap or not. If it doesn't, we
fall back to the vfio_device_mmap_dmabuf() API to get a mapping
created for the dmabuf.

Cc: Marc-André Lureau <marcandre.lureau@redhat.com>
Cc: Alex Bennée <alex.bennee@linaro.org>
Cc: Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp>
Cc: Dmitry Osipenko <dmitry.osipenko@collabora.com>
Cc: Alex Williamson <alex@shazbot.org>
Cc: Cédric Le Goater <clg@redhat.com>
Signed-off-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
---
 hw/display/Kconfig             |  5 ++++
 hw/display/virtio-gpu-dmabuf.c | 50 ++++++++++++++++++++++++++++------
 2 files changed, 47 insertions(+), 8 deletions(-)

diff --git a/hw/display/Kconfig b/hw/display/Kconfig
index 1e95ab28ef..0d090f25f5 100644
--- a/hw/display/Kconfig
+++ b/hw/display/Kconfig
@@ -106,6 +106,11 @@ config VIRTIO_VGA
     depends on VIRTIO_PCI
     select VGA
 
+config VIRTIO_GPU_VFIO_BLOB
+    bool
+    default y
+    depends on VFIO
+
 config VHOST_USER_GPU
     bool
     default y
diff --git a/hw/display/virtio-gpu-dmabuf.c b/hw/display/virtio-gpu-dmabuf.c
index d9b2ecaf31..54d010b995 100644
--- a/hw/display/virtio-gpu-dmabuf.c
+++ b/hw/display/virtio-gpu-dmabuf.c
@@ -18,6 +18,7 @@
 #include "ui/console.h"
 #include "hw/virtio/virtio-gpu.h"
 #include "hw/virtio/virtio-gpu-pixman.h"
+#include "hw/vfio/vfio-device.h"
 #include "trace.h"
 #include "system/ramblock.h"
 #include "system/hostmem.h"
@@ -27,6 +28,27 @@
 #include "standard-headers/linux/udmabuf.h"
 #include "standard-headers/drm/drm_fourcc.h"
 
+static bool virtio_gpu_create_vfio_dmabuf(struct virtio_gpu_simple_resource *r,
+                                          Error **errp)
+{
+    bool ret = false;
+#if defined(CONFIG_VIRTIO_GPU_VFIO_BLOB)
+    Error *local_err = NULL;
+    int fd;
+
+    if (!vfio_device_create_dmabuf(r->iov, r->iov_cnt, &fd, &local_err)) {
+        error_report_err(*errp);
+        *errp = NULL;
+        error_propagate(errp, local_err);
+        return false;
+    }
+
+    ret = true;
+    r->dmabuf_fd = fd;
+#endif
+    return ret;
+}
+
 static bool virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res,
                                       Error **errp)
 {
@@ -73,16 +95,28 @@ static bool virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res,
 static bool virtio_gpu_remap_dmabuf(struct virtio_gpu_simple_resource *res,
                                     Error **errp)
 {
+    Error *local_err = NULL;
+    bool ret = true;
     void *map;
 
     map = mmap(NULL, res->blob_size, PROT_READ, MAP_SHARED, res->dmabuf_fd, 0);
     if (map == MAP_FAILED) {
         error_setg_errno(errp, errno, "dmabuf mmap failed");
-        res->remapped = NULL;
-        return false;
+        map = NULL;
+        ret = false;
+#if defined(CONFIG_VIRTIO_GPU_VFIO_BLOB)
+        if (vfio_device_mmap_dmabuf(res->iov, res->iov_cnt, &map,
+                                    res->blob_size, &local_err)) {
+            ret = true;
+        } else {
+            error_report_err(*errp);
+            *errp = NULL;
+        }
+#endif
+        error_propagate(errp, local_err);
     }
     res->remapped = map;
-    return true;
+    return ret;
 }
 
 static void virtio_gpu_destroy_dmabuf(struct virtio_gpu_simple_resource *res)
@@ -145,8 +179,10 @@ void virtio_gpu_init_dmabuf(struct virtio_gpu_simple_resource *res)
         pdata = res->iov[0].iov_base;
     } else {
         if (!virtio_gpu_create_udmabuf(res, &local_err)) {
-            if (local_err) {
-                error_report_err(local_err);
+            if (!virtio_gpu_create_vfio_dmabuf(res, &local_err)) {
+                if (local_err) {
+                    error_report_err(local_err);
+                }
             }
             return;
         }
@@ -163,9 +199,7 @@ void virtio_gpu_init_dmabuf(struct virtio_gpu_simple_resource *res)
 
 void virtio_gpu_fini_dmabuf(struct virtio_gpu_simple_resource *res)
 {
-    if (res->remapped) {
-        virtio_gpu_destroy_dmabuf(res);
-    }
+    virtio_gpu_destroy_dmabuf(res);
 }
 
 static void virtio_gpu_free_dmabuf(VirtIOGPU *g, VGPUDMABuf *dmabuf)
-- 
2.50.1


Re: [PATCH v6 11/11] virtio-gpu-dmabuf: Create dmabuf for blobs associated with VFIO devices
Posted by Akihiko Odaki 8 hours ago
On 2026/02/10 15:59, Vivek Kasireddy wrote:
> In addition to memfd, a blob resource can also have its backing
> storage in a VFIO device region. Since, there is no effective way
> to determine where the backing storage is located, we first try to
> create a dmabuf assuming it is in memfd. If that fails, we try to
> create a dmabuf assuming it is in VFIO device region.
> 
> So, we first call virtio_gpu_create_udmabuf() to check if the blob's
> backing storage is located in a memfd or not. If it is not, we invoke
> the vfio_device_create_dmabuf() API which identifies the right VFIO
> device and eventually creates a dmabuf fd.
> 
> Note that in virtio_gpu_remap_dmabuf(), we first try to test if
> the VFIO dmabuf exporter supports mmap or not. If it doesn't, we
> fall back to the vfio_device_mmap_dmabuf() API to get a mapping
> created for the dmabuf.
> 
> Cc: Marc-André Lureau <marcandre.lureau@redhat.com>
> Cc: Alex Bennée <alex.bennee@linaro.org>
> Cc: Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp>
> Cc: Dmitry Osipenko <dmitry.osipenko@collabora.com>
> Cc: Alex Williamson <alex@shazbot.org>
> Cc: Cédric Le Goater <clg@redhat.com>
> Signed-off-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
> ---
>   hw/display/Kconfig             |  5 ++++
>   hw/display/virtio-gpu-dmabuf.c | 50 ++++++++++++++++++++++++++++------
>   2 files changed, 47 insertions(+), 8 deletions(-)
> 
> diff --git a/hw/display/Kconfig b/hw/display/Kconfig
> index 1e95ab28ef..0d090f25f5 100644
> --- a/hw/display/Kconfig
> +++ b/hw/display/Kconfig
> @@ -106,6 +106,11 @@ config VIRTIO_VGA
>       depends on VIRTIO_PCI
>       select VGA
>   
> +config VIRTIO_GPU_VFIO_BLOB
> +    bool
> +    default y
> +    depends on VFIO
> +
>   config VHOST_USER_GPU
>       bool
>       default y
> diff --git a/hw/display/virtio-gpu-dmabuf.c b/hw/display/virtio-gpu-dmabuf.c
> index d9b2ecaf31..54d010b995 100644
> --- a/hw/display/virtio-gpu-dmabuf.c
> +++ b/hw/display/virtio-gpu-dmabuf.c
> @@ -18,6 +18,7 @@
>   #include "ui/console.h"
>   #include "hw/virtio/virtio-gpu.h"
>   #include "hw/virtio/virtio-gpu-pixman.h"
> +#include "hw/vfio/vfio-device.h"
>   #include "trace.h"
>   #include "system/ramblock.h"
>   #include "system/hostmem.h"
> @@ -27,6 +28,27 @@
>   #include "standard-headers/linux/udmabuf.h"
>   #include "standard-headers/drm/drm_fourcc.h"
>   
> +static bool virtio_gpu_create_vfio_dmabuf(struct virtio_gpu_simple_resource *r,
> +                                          Error **errp)
> +{
> +    bool ret = false;
> +#if defined(CONFIG_VIRTIO_GPU_VFIO_BLOB)
> +    Error *local_err = NULL;
> +    int fd;
> +
> +    if (!vfio_device_create_dmabuf(r->iov, r->iov_cnt, &fd, &local_err)) {
> +        error_report_err(*errp);
> +        *errp = NULL;
> +        error_propagate(errp, local_err);
> +        return false;
> +    }
> +
> +    ret = true;
> +    r->dmabuf_fd = fd;
> +#endif
> +    return ret;
> +}
> +
>   static bool virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res,
>                                         Error **errp)
>   {
> @@ -73,16 +95,28 @@ static bool virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res,
>   static bool virtio_gpu_remap_dmabuf(struct virtio_gpu_simple_resource *res,
>                                       Error **errp)
>   {
> +    Error *local_err = NULL;
> +    bool ret = true;
>       void *map;
>   
>       map = mmap(NULL, res->blob_size, PROT_READ, MAP_SHARED, res->dmabuf_fd, 0);
>       if (map == MAP_FAILED) {
>           error_setg_errno(errp, errno, "dmabuf mmap failed");
> -        res->remapped = NULL;
> -        return false;
> +        map = NULL;
> +        ret = false;
> +#if defined(CONFIG_VIRTIO_GPU_VFIO_BLOB)
> +        if (vfio_device_mmap_dmabuf(res->iov, res->iov_cnt, &map,
> +                                    res->blob_size, &local_err)) {
> +            ret = true;
> +        } else {
> +            error_report_err(*errp);
> +            *errp = NULL;
> +        }
> +#endif
> +        error_propagate(errp, local_err);
>       }
>       res->remapped = map;
> -    return true;
> +    return ret;
>   }
>   
>   static void virtio_gpu_destroy_dmabuf(struct virtio_gpu_simple_resource *res)
> @@ -145,8 +179,10 @@ void virtio_gpu_init_dmabuf(struct virtio_gpu_simple_resource *res)
>           pdata = res->iov[0].iov_base;
>       } else {
>           if (!virtio_gpu_create_udmabuf(res, &local_err)) {

virtio_gpu_create_udmabuf() will log "Could not find valid ramblock" 
even for VFIO, which shouldn't happen.

This logic should look like as follows:

virtio_gpu_create_udmabuf(res);
if (it turned out the memory is incompatible with udmabuf) {
     virtio_gpu_create_vfio_dmabuf(res);
     if (it turned out the memory is not backed by VFIO) {
         qemu_log_mask(LOG_GUEST_ERROR, "incompatible\n");
         return;
     }
}

Regards,
Akihiko Odaki

> -            if (local_err) {
> -                error_report_err(local_err);
> +            if (!virtio_gpu_create_vfio_dmabuf(res, &local_err)) {
> +                if (local_err) {
> +                    error_report_err(local_err);
> +                }
>               }
>               return;
>           }
> @@ -163,9 +199,7 @@ void virtio_gpu_init_dmabuf(struct virtio_gpu_simple_resource *res)
>   
>   void virtio_gpu_fini_dmabuf(struct virtio_gpu_simple_resource *res)
>   {
> -    if (res->remapped) {
> -        virtio_gpu_destroy_dmabuf(res);
> -    }
> +    virtio_gpu_destroy_dmabuf(res);
>   }
>   
>   static void virtio_gpu_free_dmabuf(VirtIOGPU *g, VGPUDMABuf *dmabuf)