Skip to contentSkip to conversation

vfio: Create dmabuf from multiple VFIO ranges and use it in virtio-gpu

PATCH Superseded Vivek Kasireddy 2 participants 25 messages Feb 10 – Feb 23 Arm/Amlogic Meson SoC support

v6 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

v6 cover vfio: Create dmabuf from multiple VFIO ranges and use it in virtio-gpu

The virtio-gpu driver running in the Guest VM can create Guest blob resources (by importing dmabufs) that are backed by System RAM. This is made possible by making use of memfd memory backend and udmabuf driver on the Host side. However, in order to create Guest blobs that are backed by vfio-pci device regions (which happens when virtio-gpu imports dmabufs from devices that have local memory such as dGPU VFs), we have to add support for creating dmabuf from multiple VFIO device ranges (same region) and leverage it in virtio-gpu.

So, while creating the blobs we first try to create a dmabuf by calling into the udmabuf driver assuming that the backing memory is memfd based. If that fails, and if it is determined that the blob is backed by a vfio-pci device region, we would then call into the vfio-pci driver to have a dmabuf fd created.

Changelog:
v5 -> v6:
- Drop the additional error_setg() call in virtio_gpu_create_udmabuf() and only have LOG_GUEST_ERROR for invalid ramblocks (Akihiko)
- Merge the patch that introduces vfio_device_lookup() helper with the one that adds support for vfio_device_create_dmabuf() (Cedric)
- Use only one 'Error **' parameter instead of two while creating and mmapping the dmabuf (Cedric)
- Collect R-b tags from Akihiko

v4 -> v5:
- Improve commit message for the patch that replaces dma_memory_map() with virtio_gpu_dma_memory_map() (Akihiko)
- Create separate patches to remove rcu_read_lock/unlock and use g_autofree for list pointer in virtio_gpu_create_udmabuf() (Akihiko, Cedric)
- Create a separate patch to improve error handling (Cedric)
- Make vfio_device_lookup() a common helper local to vfio/device.c (Cedric, Akihiko)
- Move the dmabuf mmap helper to vfio/device.c (Akihiko)
- Replace PROT_READ with PROT_NONE and vbasedev->fd with -1 while creating the placeholder mapping in dmabuf mmap helper (Akihiko)

v3 -> v4:
- Drop the patch that introduces ram_block_is_memfd_backed()
- Drop the additional check in virtio_gpu_create_udmabuf() that checks for memfd backed ram blocks (Akihiko)
- Remove rcu_read_lock/unlock and replace warn_report() with qemu_log_mask() in virtio_gpu_create_udmabuf()
- Improve the commit message and logic in the patch that adds support for vfio_device_create_dmabuf_fd()
- Rebase on top of vfio-next

v2 -> v3:
- Use memory_region_get_ram_ptr() to obtain hva for both memfd and VFIO backed memory regions (Akihiko)
- Drop the patch that introduced qemu_iovec_same_memory_regions() helper and move the "same memory region" check into virtio_gpu_create_udmabuf(), vfio_device_create_dmabuf_fd() (Akihiko)
- Refactor virtio_gpu_init_dmabuf() to not rely on helpers such as memory_region_is_ram_device() to identify memory regions and instead call virtio_gpu_create_udmabuf() unconditionally (Akihiko)
- Add a patch to introduce ram_block_is_memfd_backed() helper

v1 -> v2:
- Drop the patch that uses res->blob_size instead of res->blob to identify blob resources (Akihiko)
- Remove the res->dmabuf_fd < 0 check while attaching backing to a resource (Akihiko)
- Remove cmd->cmd_hdr.type != VIRTIO_GPU_CMD_RESOURCE_CREATE_BLOB check while attaching backing (Akihiko)
- Improve vfio_get_region_index_from_mr() and add documentation (Cedric)
- Remove rcu_read_lock/unlock around qemu_ram_block_from_host() (Akihiko, Cedric)
- Improve, document and rename vfio_device_create_dmabuf() to vfio_device_create_dmabuf_fd() (Cedric)
- Add a new helper to lookup VFIO device from memory region (Cedric)
- Document vfio_device_get_region_info() (Cedric)
- Ensure that info variable (in vfio_dmabuf_mmap()) is initialized before use (Cedric)
- Rename udmabuf files and helpers to dmabuf (Akihiko)
- Remove the redundant check for virtio_gpu_have_udmabuf() in virtio_gpu_init_dmabuf() (Akihiko)
- Add a helper to check whether all the entries of a dmabuf belong to a single memory region or not (Akihiko)

RFC -> v1:
- Create the CPU mapping using vfio device fd if the dmabuf exporter (vfio-pci) does not provide mmap() support (Akihiko)
- Log a warning with LOG_GUEST_ERROR instead of warn_report() when dmabuf cannot be created using Guest provided addresses (Akihiko)
- Use address_space_translate() instead of gpa2hva() to obtain the Host addresses (Akihiko)
- Rearrange the patches and improve the commit messages (Akihiko)
- Fix compilation error when VFIO is not enabled (Alex)
- Add a new helper to obtain VFIO region index from memory region
- Move vfio_device_create_dmabuf() to hw/vfio/device.c

Tested with an SRIOV enabled Intel dGPU (B60) by running Gnome Wayland (in the VM) and Qemu with the following (relevant) parameters:
-device vfio-pci,host=0000:03:00.1
-device virtio-vga,max_outputs=1,xres=1920,yres=1080,blob=true -display gtk,gl=on

Associated virtio-gpu kernel driver series (merged):
https://lore.kernel.org/dri-devel/[email protected]/

---
Cc: Marc-André Lureau <[email protected]>
Cc: Alex Bennée <[email protected]>
Cc: Akihiko Odaki <[email protected]>
Cc: Dmitry Osipenko <[email protected]>
Cc: Alex Williamson <[email protected]>
Cc: Cédric Le Goater <[email protected]>
Cc: Dongwon Kim <[email protected]>

Vivek Kasireddy (11):
virtio-gpu: Recreate the resource's dmabuf if new backing is attached
virtio-gpu: Find hva for Guest's DMA addr associated with a ram device
virtio-gpu: Rename udmabuf files and helpers to dmabuf
virtio-gpu-dmabuf: Remove rcu_read_lock/unlock from
virtio_gpu_create_udmabuf()
virtio-gpu-dmabuf: Use g_autofree for the list pointer
virtio-gpu-dmabuf: Improve error handling by introducing 'Error **'
vfio: Document vfio_device_get_region_info()
vfio/region: Add a helper to get region index from memory region vfio/device: Add support for creating dmabuf from multiple ranges vfio/device: Add a helper to mmap a dmabuf
virtio-gpu-dmabuf: Create dmabuf for blobs associated with VFIO
devices

 hw/display/Kconfig                            |   5 +
 hw/display/meson.build                        |   4 +-
 ...abuf-stubs.c => virtio-gpu-dmabuf-stubs.c} |   4 +-
 ...rtio-gpu-udmabuf.c => virtio-gpu-dmabuf.c} | 100 ++++++++----
 hw/display/virtio-gpu.c                       |  31 +++-
 hw/vfio/device.c                              | 146 ++++++++++++++++++
 hw/vfio/region.c                              |  14 ++
 include/hw/vfio/vfio-device.h                 |  53 +++++++
 include/hw/virtio/virtio-gpu.h                |   6 +-
 9 files changed, 322 insertions(+), 41 deletions(-)
 rename hw/display/{virtio-gpu-udmabuf-stubs.c => virtio-gpu-dmabuf-stubs.c} (79%)
 rename hw/display/{virtio-gpu-udmabuf.c => virtio-gpu-dmabuf.c} (67%)
Signature · 1 line
2.50.1
#1 lore raw

PATCH v6 1/11 virtio-gpu: Recreate the resource's dmabuf if new backing is attached

There are cases when a blob resource's backing might get detached and re-attached again such as when the underlying object is getting migrated in the Guest. In these situations, we need to obtain a new dmabuf fd, which can be done by calling virtio_gpu_init_udmabuf().

Cc Marc-André Lureau <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Cédric Le Goater <[email protected]>
---
 hw/display/virtio-gpu.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/display/virtio-gpu.c +4 -0
index f23eec6862..3d58cebf4b 100644
@@ -937,6 +937,10 @@ virtio_gpu_resource_attach_backing(VirtIOGPU *g,
         cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
         return;
     }
+
+    if (res->blob_size) {
+        virtio_gpu_init_udmabuf(res);
+    }
 }
 
 static void
Signature · 1 line
2.50.1
#2 lore raw

PATCH v6 2/11 virtio-gpu: Find hva for Guest's DMA addr associated with a ram device

In reply to Vivek Kasireddy

If the Guest provides a DMA address that is associated with a ram device (such as a VFIO PCI device region and not its system memory), then we can obtain the hva (host virtual address) by invoking address_space_translate() followed by memory_region_get_ram_ptr().

We cannot use dma_memory_map() because for memory regions that do not support direct access, it would create bounce buffers instead of returning the actual hva, which is not desirable here. And, since VFIO device regions are not considered directly accessible (because they are mostly MMIO based), virtio-gpu cannot invoke dma_memory_map() to obtain the hva in this case.

Therefore, in order to handle addresses associated with VFIO devices, we need to use the address_space_translate() API to first identify the right memory region and the appropriate offset within that region and then use memory_region_get_ram_ptr() to get the hva. This approach also works for addresses associated with the system memory region.

Note that, although we take an explicit reference on the memory region, we would still rely on dma_memory_unmap() to drop that reference when the dma mapping is eventually unmapped.

Cc Marc-André Lureau <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Cédric Le Goater <[email protected]>
---
 hw/display/virtio-gpu.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)
hw/display/virtio-gpu.c +18 -3
index 3d58cebf4b..9930a21317 100644
@@ -798,6 +798,23 @@ static void virtio_gpu_set_scanout_blob(VirtIOGPU *g,
                               &fb, res, &ss.r, &cmd->error);
 }
 
+static void *virtio_gpu_dma_memory_map(VirtIOGPU *g,
+                                       struct virtio_gpu_ctrl_command *cmd,
+                                       uint64_t a, hwaddr *len)
+{
+    MemoryRegion *mr;
+    hwaddr xlat;
+
+    mr = address_space_translate(VIRTIO_DEVICE(g)->dma_as, a, &xlat, len,
+                                 DMA_DIRECTION_TO_DEVICE,
+                                 MEMTXATTRS_UNSPECIFIED);
+    if (memory_region_is_ram(mr)) {
+        memory_region_ref(mr);
+        return memory_region_get_ram_ptr(mr) + xlat;
+    }
+    return NULL;
+}
+
 int virtio_gpu_create_mapping_iov(VirtIOGPU *g,
                                   uint32_t nr_entries, uint32_t offset,
                                   struct virtio_gpu_ctrl_command *cmd,
@@ -839,9 +856,7 @@ int virtio_gpu_create_mapping_iov(VirtIOGPU *g,
 
         do {
             len = l;
-            map = dma_memory_map(VIRTIO_DEVICE(g)->dma_as, a, &len,
-                                 DMA_DIRECTION_TO_DEVICE,
-                                 MEMTXATTRS_UNSPECIFIED);
+            map = virtio_gpu_dma_memory_map(g, cmd, a, &len);
             if (!map) {
                 qemu_log_mask(LOG_GUEST_ERROR, "%s: failed to map MMIO memory for"
                               " element %d\n", __func__, e);
Signature · 1 line
2.50.1
#3 lore raw

PATCH v6 3/11 virtio-gpu: Rename udmabuf files and helpers to dmabuf

In reply to Vivek Kasireddy

This is prep-work for adding the ability to create dmabuf fds from VFIO devices in addition to udmabuf.

Cc Marc-André Lureau <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Cédric Le Goater <[email protected]>
---
 hw/display/meson.build                               |  4 ++--
 ...gpu-udmabuf-stubs.c => virtio-gpu-dmabuf-stubs.c} |  4 ++--
 .../{virtio-gpu-udmabuf.c => virtio-gpu-dmabuf.c}    | 12 ++++++------
 hw/display/virtio-gpu.c                              |  8 ++++----
 include/hw/virtio/virtio-gpu.h                       |  6 +++---
 5 files changed, 17 insertions(+), 17 deletions(-)
 rename hw/display/{virtio-gpu-udmabuf-stubs.c => virtio-gpu-dmabuf-stubs.c} (79%)
 rename hw/display/{virtio-gpu-udmabuf.c => virtio-gpu-dmabuf.c} (94%)
hw/display/meson.build +2 -2
index 90e6c041bd..f5f92b1068 100644
@@ -69,9 +69,9 @@ if config_all_devices.has_key('CONFIG_VIRTIO_GPU')
   virtio_gpu_ss.add(when: 'CONFIG_VIRTIO_GPU',
                     if_true: [files('virtio-gpu-base.c', 'virtio-gpu.c'), pixman])
   if host_os == 'linux'
-    virtio_gpu_ss.add(files('virtio-gpu-udmabuf.c'))
+    virtio_gpu_ss.add(files('virtio-gpu-dmabuf.c'))
   else
-    virtio_gpu_ss.add(files('virtio-gpu-udmabuf-stubs.c'))
+    virtio_gpu_ss.add(files('virtio-gpu-dmabuf-stubs.c'))
   endif
   virtio_gpu_ss.add(when: 'CONFIG_VHOST_USER_GPU', if_true: files('vhost-user-gpu.c'))
   hw_display_modules += {'virtio-gpu': virtio_gpu_ss}
hw/display/virtio-gpu-dmabuf-stubs.c renamed from hw/display/virtio-gpu-udmabuf-stubs.c +2 -2
similarity index 79%
rename from hw/display/virtio-gpu-udmabuf-stubs.c
rename to hw/display/virtio-gpu-dmabuf-stubs.c
index f692e13510..01067e246d 100644
@@ -7,12 +7,12 @@ bool virtio_gpu_have_udmabuf(void)
     return false;
 }
 
-void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)
+void virtio_gpu_init_dmabuf(struct virtio_gpu_simple_resource *res)
 {
     /* nothing (stub) */
 }
 
-void virtio_gpu_fini_udmabuf(struct virtio_gpu_simple_resource *res)
+void virtio_gpu_fini_dmabuf(struct virtio_gpu_simple_resource *res)
 {
     /* nothing (stub) */
 }
hw/display/virtio-gpu-dmabuf.c renamed from hw/display/virtio-gpu-udmabuf.c +6 -6
similarity index 94%
rename from hw/display/virtio-gpu-udmabuf.c
rename to hw/display/virtio-gpu-dmabuf.c
index d804f321aa..c34d4c85bc 100644
@@ -68,7 +68,7 @@ static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res)
     g_free(list);
 }
 
-static void virtio_gpu_remap_udmabuf(struct virtio_gpu_simple_resource *res)
+static void virtio_gpu_remap_dmabuf(struct virtio_gpu_simple_resource *res)
 {
     res->remapped = mmap(NULL, res->blob_size, PROT_READ,
                          MAP_SHARED, res->dmabuf_fd, 0);
@@ -79,7 +79,7 @@ static void virtio_gpu_remap_udmabuf(struct virtio_gpu_simple_resource *res)
     }
 }
 
-static void virtio_gpu_destroy_udmabuf(struct virtio_gpu_simple_resource *res)
+static void virtio_gpu_destroy_dmabuf(struct virtio_gpu_simple_resource *res)
 {
     if (res->remapped) {
         munmap(res->remapped, res->blob_size);
@@ -128,7 +128,7 @@ bool virtio_gpu_have_udmabuf(void)
     return memfd_backend;
 }
 
-void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)
+void virtio_gpu_init_dmabuf(struct virtio_gpu_simple_resource *res)
 {
     void *pdata = NULL;
 
@@ -141,7 +141,7 @@ void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)
         if (res->dmabuf_fd < 0) {
             return;
         }
-        virtio_gpu_remap_udmabuf(res);
+        virtio_gpu_remap_dmabuf(res);
         if (!res->remapped) {
             return;
         }
@@ -151,10 +151,10 @@ void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)
     res->blob = pdata;
 }
 
-void virtio_gpu_fini_udmabuf(struct virtio_gpu_simple_resource *res)
+void virtio_gpu_fini_dmabuf(struct virtio_gpu_simple_resource *res)
 {
     if (res->remapped) {
-        virtio_gpu_destroy_udmabuf(res);
+        virtio_gpu_destroy_dmabuf(res);
     }
 }
 
hw/display/virtio-gpu.c +4 -4
index 9930a21317..bc4d33c4dc 100644
@@ -360,7 +360,7 @@ static void virtio_gpu_resource_create_blob(VirtIOGPU *g,
         return;
     }
 
-    virtio_gpu_init_udmabuf(res);
+    virtio_gpu_init_dmabuf(res);
     QTAILQ_INSERT_HEAD(&g->reslist, res, next);
 }
 
@@ -917,7 +917,7 @@ void virtio_gpu_cleanup_mapping(VirtIOGPU *g,
     res->addrs = NULL;
 
     if (res->blob) {
-        virtio_gpu_fini_udmabuf(res);
+        virtio_gpu_fini_dmabuf(res);
     }
 }
 
@@ -954,7 +954,7 @@ virtio_gpu_resource_attach_backing(VirtIOGPU *g,
     }
 
     if (res->blob_size) {
-        virtio_gpu_init_udmabuf(res);
+        virtio_gpu_init_dmabuf(res);
     }
 }
 
@@ -1440,7 +1440,7 @@ static int virtio_gpu_blob_load(QEMUFile *f, void *opaque, size_t size,
             return -EINVAL;
         }
 
-        virtio_gpu_init_udmabuf(res);
+        virtio_gpu_init_dmabuf(res);
 
         resource_id = qemu_get_be32(f);
     }
include/hw/virtio/virtio-gpu.h +3 -3
index 58e0f91fda..e3d351a2d4 100644
@@ -354,10 +354,10 @@ bool virtio_gpu_scanout_blob_to_fb(struct virtio_gpu_framebuffer *fb,
                                    struct virtio_gpu_set_scanout_blob *ss,
                                    uint64_t blob_size);
 
-/* virtio-gpu-udmabuf.c */
+/* virtio-gpu-dmabuf.c */
 bool virtio_gpu_have_udmabuf(void);
-void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res);
-void virtio_gpu_fini_udmabuf(struct virtio_gpu_simple_resource *res);
+void virtio_gpu_init_dmabuf(struct virtio_gpu_simple_resource *res);
+void virtio_gpu_fini_dmabuf(struct virtio_gpu_simple_resource *res);
 int virtio_gpu_update_dmabuf(VirtIOGPU *g,
                              uint32_t scanout_id,
                              struct virtio_gpu_simple_resource *res,
Signature · 1 line
2.50.1
#4 lore raw

PATCH v6 4/11 virtio-gpu-dmabuf: Remove rcu_read_lock/unlock from virtio_gpu_create_udmabuf()

In reply to Vivek Kasireddy

There is no need to wrap the call to qemu_ram_block_from_host() with rcu_read_lock/unlock as we already hold a reference to the memory region that owns the ram block. Note that, the reference was obtained via dma_memory_map() while creating the mapping.

Cc Marc-André Lureau <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Cédric Le Goater <[email protected]>
---
 hw/display/virtio-gpu-dmabuf.c | 3 ---
 1 file changed, 3 deletions(-)
hw/display/virtio-gpu-dmabuf.c +0 -3
index c34d4c85bc..0782bcf3a1 100644
@@ -43,10 +43,7 @@ static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res)
                      sizeof(struct udmabuf_create_item) * res->iov_cnt);
 
     for (i = 0; i < res->iov_cnt; i++) {
-        rcu_read_lock();
         rb = qemu_ram_block_from_host(res->iov[i].iov_base, false, &offset);
-        rcu_read_unlock();
-
         if (!rb || rb->fd < 0) {
             g_free(list);
             return;
Signature · 1 line
2.50.1
#5 lore raw

PATCH v6 5/11 virtio-gpu-dmabuf: Use g_autofree for the list pointer

In reply to Vivek Kasireddy

Avoid the manual g_free() calls for the list pointer by using g_autofree in virtio_gpu_create_udmabuf().

Cc Marc-André Lureau <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Cédric Le Goater <[email protected]>
---
 hw/display/virtio-gpu-dmabuf.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
hw/display/virtio-gpu-dmabuf.c +1 -3
index 0782bcf3a1..8d67ef7c2a 100644
@@ -29,7 +29,7 @@
 
 static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res)
 {
-    struct udmabuf_create_list *list;
+    g_autofree struct udmabuf_create_list *list = NULL;
     RAMBlock *rb;
     ram_addr_t offset;
     int udmabuf, i;
@@ -45,7 +45,6 @@ static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res)
     for (i = 0; i < res->iov_cnt; i++) {
         rb = qemu_ram_block_from_host(res->iov[i].iov_base, false, &offset);
         if (!rb || rb->fd < 0) {
-            g_free(list);
             return;
         }
 
@@ -62,7 +61,6 @@ static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res)
         warn_report("%s: UDMABUF_CREATE_LIST: %s", __func__,
                     strerror(errno));
     }
-    g_free(list);
 }
 
 static void virtio_gpu_remap_dmabuf(struct virtio_gpu_simple_resource *res)
Signature · 1 line
2.50.1
#6 lore raw

PATCH v6 6/11 virtio-gpu-dmabuf: Improve error handling by introducing 'Error **'

In reply to Vivek Kasireddy

Make the error handling more robust in virtio_gpu_init_udmabuf() by passing in 'Error **' parameter to capture errors from virtio_gpu_create_udmabuf() and virtio_gpu_remap_dmabuf(). And, since they now take in 'Error **' parameter, have them return a bool to adhere to best practices.

Cc Marc-André Lureau <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Cédric Le Goater <[email protected]>
---
 hw/display/virtio-gpu-dmabuf.c | 45 ++++++++++++++++++++++------------
 1 file changed, 30 insertions(+), 15 deletions(-)
hw/display/virtio-gpu-dmabuf.c +30 -15
index 8d67ef7c2a..d9b2ecaf31 100644
@@ -27,7 +27,8 @@
 #include "standard-headers/linux/udmabuf.h"
 #include "standard-headers/drm/drm_fourcc.h"
 
-static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res)
+static bool virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res,
+                                      Error **errp)
 {
     g_autofree struct udmabuf_create_list *list = NULL;
     RAMBlock *rb;
@@ -36,7 +37,8 @@ static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res)
 
     udmabuf = udmabuf_fd();
     if (udmabuf < 0) {
-        return;
+        error_setg(errp, "udmabuf device not available");
+        return false;
     }
 
     list = g_malloc0(sizeof(struct udmabuf_create_list) +
@@ -45,7 +47,10 @@ static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res)
     for (i = 0; i < res->iov_cnt; i++) {
         rb = qemu_ram_block_from_host(res->iov[i].iov_base, false, &offset);
         if (!rb || rb->fd < 0) {
-            return;
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: Could not find valid ramblock\n",
+                          __func__);
+            return false;
         }
 
         list->list[i].memfd  = rb->fd;
@@ -58,20 +63,26 @@ static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res)
 
     res->dmabuf_fd = ioctl(udmabuf, UDMABUF_CREATE_LIST, list);
     if (res->dmabuf_fd < 0) {
-        warn_report("%s: UDMABUF_CREATE_LIST: %s", __func__,
-                    strerror(errno));
+        error_setg_errno(errp, -res->dmabuf_fd,
+                         "Could not create dmabuf fd via udmabuf driver");
+        return false;
     }
+    return true;
 }
 
-static void virtio_gpu_remap_dmabuf(struct virtio_gpu_simple_resource *res)
+static bool virtio_gpu_remap_dmabuf(struct virtio_gpu_simple_resource *res,
+                                    Error **errp)
 {
-    res->remapped = mmap(NULL, res->blob_size, PROT_READ,
-                         MAP_SHARED, res->dmabuf_fd, 0);
-    if (res->remapped == MAP_FAILED) {
-        warn_report("%s: dmabuf mmap failed: %s", __func__,
-                    strerror(errno));
+    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;
     }
+    res->remapped = map;
+    return true;
 }
 
 static void virtio_gpu_destroy_dmabuf(struct virtio_gpu_simple_resource *res)
@@ -125,6 +136,7 @@ bool virtio_gpu_have_udmabuf(void)
 
 void virtio_gpu_init_dmabuf(struct virtio_gpu_simple_resource *res)
 {
+    Error *local_err = NULL;
     void *pdata = NULL;
 
     res->dmabuf_fd = -1;
@@ -132,12 +144,15 @@ void virtio_gpu_init_dmabuf(struct virtio_gpu_simple_resource *res)
         res->iov[0].iov_len < 4096) {
         pdata = res->iov[0].iov_base;
     } else {
-        virtio_gpu_create_udmabuf(res);
-        if (res->dmabuf_fd < 0) {
+        if (!virtio_gpu_create_udmabuf(res, &local_err)) {
+            if (local_err) {
+                error_report_err(local_err);
+            }
             return;
         }
-        virtio_gpu_remap_dmabuf(res);
-        if (!res->remapped) {
+
+        if (!virtio_gpu_remap_dmabuf(res, &local_err)) {
+            error_report_err(local_err);
             return;
         }
         pdata = res->remapped;
Signature · 1 line
2.50.1
#7 lore raw

Re: [PATCH v6 06/11] virtio-gpu-dmabuf: Improve error handling by introducing 'Error **'

Quoted 54 lines · Vivek Kasireddy

Make the error handling more robust in virtio_gpu_init_udmabuf() by passing in 'Error **' parameter to capture errors from virtio_gpu_create_udmabuf() and virtio_gpu_remap_dmabuf(). And, since they now take in 'Error **' parameter, have them return a bool to adhere to best practices.

Cc Marc-André Lureau <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Cédric Le Goater <[email protected]>
---
  hw/display/virtio-gpu-dmabuf.c | 45 ++++++++++++++++++++++------------
  1 file changed, 30 insertions(+), 15 deletions(-)
hw/display/virtio-gpu-dmabuf.c +8 -3
index 8d67ef7c2a..d9b2ecaf31 100644
@@ -27,7 +27,8 @@
  #include "standard-headers/linux/udmabuf.h"
  #include "standard-headers/drm/drm_fourcc.h"
 
-static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res)
+static bool virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res,
+                                      Error **errp)
  {
      g_autofree struct udmabuf_create_list *list = NULL;
      RAMBlock *rb;
@@ -36,7 +37,8 @@ static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res)
 
      udmabuf = udmabuf_fd();
      if (udmabuf < 0) {
-        return;
+        error_setg(errp, "udmabuf device not available");
+        return false;
      }
 
      list = g_malloc0(sizeof(struct udmabuf_create_list) +
@@ -45,7 +47,10 @@ static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res)
      for (i = 0; i < res->iov_cnt; i++) {
          rb = qemu_ram_block_from_host(res->iov[i].iov_base, false, &offset);
          if (!rb || rb->fd < 0) {
-            return;
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: Could not find valid ramblock\n",
+                          __func__);
+            return false;
          }
 
          list->list[i].memfd  = rb->fd;

hw/display/virtio-gpu-dmabuf.c static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res) · line 67

Quoted 7 lines
hw/display/virtio-gpu-dmabuf.c +2 -2
@@ -58,20 +63,26 @@ static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res)
 
      res->dmabuf_fd = ioctl(udmabuf, UDMABUF_CREATE_LIST, list);
      if (res->dmabuf_fd < 0) {
-        warn_report("%s: UDMABUF_CREATE_LIST: %s", __func__,
-                    strerror(errno));
+        error_setg_errno(errp, -res->dmabuf_fd,
+                         "Could not create dmabuf fd via udmabuf driver");

This is a guest error since it indicates that the guest specified wrong addresses.

Regards,
Akihiko Odaki

Quoted 55 lines
hw/display/virtio-gpu-dmabuf.c +20 -10
+        return false;
      }
+    return true;
  }
 
-static void virtio_gpu_remap_dmabuf(struct virtio_gpu_simple_resource *res)
+static bool virtio_gpu_remap_dmabuf(struct virtio_gpu_simple_resource *res,
+                                    Error **errp)
  {
-    res->remapped = mmap(NULL, res->blob_size, PROT_READ,
-                         MAP_SHARED, res->dmabuf_fd, 0);
-    if (res->remapped == MAP_FAILED) {
-        warn_report("%s: dmabuf mmap failed: %s", __func__,
-                    strerror(errno));
+    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;
      }
+    res->remapped = map;
+    return true;
  }
 
  static void virtio_gpu_destroy_dmabuf(struct virtio_gpu_simple_resource *res)
@@ -125,6 +136,7 @@ bool virtio_gpu_have_udmabuf(void)
 
  void virtio_gpu_init_dmabuf(struct virtio_gpu_simple_resource *res)
  {
+    Error *local_err = NULL;
      void *pdata = NULL;
 
      res->dmabuf_fd = -1;
@@ -132,12 +144,15 @@ void virtio_gpu_init_dmabuf(struct virtio_gpu_simple_resource *res)
          res->iov[0].iov_len < 4096) {
          pdata = res->iov[0].iov_base;
      } else {
-        virtio_gpu_create_udmabuf(res);
-        if (res->dmabuf_fd < 0) {
+        if (!virtio_gpu_create_udmabuf(res, &local_err)) {
+            if (local_err) {
+                error_report_err(local_err);
+            }
              return;
          }
-        virtio_gpu_remap_dmabuf(res);
-        if (!res->remapped) {
+
+        if (!virtio_gpu_remap_dmabuf(res, &local_err)) {
+            error_report_err(local_err);
              return;
          }
          pdata = res->remapped;
#8 lore raw

Re: [PATCH v6 06/11] virtio-gpu-dmabuf: Improve error handling by introducing 'Error **'

Quoted 44 lines · Akihiko Odaki
Quoted 43 lines · Vivek Kasireddy

Make the error handling more robust in virtio_gpu_init_udmabuf() by passing in 'Error **' parameter to capture errors from virtio_gpu_create_udmabuf() and virtio_gpu_remap_dmabuf(). And, since they now take in 'Error **' parameter, have them return a bool to adhere to best practices.

Cc Marc-André Lureau <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Cédric Le Goater <[email protected]>

---
  hw/display/virtio-gpu-dmabuf.c | 45 ++++++++++++++++++++++------------

  1 file changed, 30 insertions(+), 15 deletions(-)
hw/display/virtio-gpu-dmabuf.c +2 -1
index 8d67ef7c2a..d9b2ecaf31 100644
@@ -27,7 +27,8 @@
   #include "standard-headers/linux/udmabuf.h"
   #include "standard-headers/drm/drm_fourcc.h"
-static void virtio_gpu_create_udmabuf(struct 
 virtio_gpu_simple_resource *res)
+static bool virtio_gpu_create_udmabuf(struct 
 virtio_gpu_simple_resource *res,
+                                      Error **errp)
   {
       g_autofree struct udmabuf_create_list *list = NULL;

      RAMBlock *rb;

hw/display/virtio-gpu-dmabuf.c +2 -1
@@ -36,7 +37,8 @@ static void virtio_gpu_create_udmabuf(struct 
 virtio_gpu_simple_resource *res)
       udmabuf = udmabuf_fd();
       if (udmabuf < 0) {
-        return;
+        error_setg(errp, "udmabuf device not available");
+        return false;
       }

      list = g_malloc0(sizeof(struct udmabuf_create_list) +

hw/display/virtio-gpu-dmabuf.c +0 -0
@@ -45,7 +47,10 @@ static void virtio_gpu_create_udmabuf(struct 
 virtio_gpu_simple_resource *res)
Quoted 8 lines

hw/display/virtio-gpu-dmabuf.c static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res) · line 53

Quoted 8 lines
hw/display/virtio-gpu-dmabuf.c +4 -1
       for (i = 0; i < res->iov_cnt; i++) {
           rb = qemu_ram_block_from_host(res->iov[i].iov_base, false, &offset);
           if (!rb || rb->fd < 0) {
-            return;
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: Could not find valid ramblock\n",
+                          __func__);
+            return false;

include/qapi/error.h suggests the following error handling pattern:

Quoted 6 lines

Call a function, receive an error from it, and pass it to the caller
- when the function returns a value that indicates failure, say false:

    if (!foo(arg, errp)) {
        handle the error...
    }

Returning false without passing an error to the caller breaks it so please don't do that.

Quoted 73 lines

hw/display/virtio-gpu-dmabuf.c static void virtio_gpu_create_udmabuf(struct · line 68

Quoted 11 lines
hw/display/virtio-gpu-dmabuf.c +2 -2
           }
           list->list[i].memfd  = rb->fd;
@@ -58,20 +63,26 @@ static void virtio_gpu_create_udmabuf(struct 
 virtio_gpu_simple_resource *res)
       res->dmabuf_fd = ioctl(udmabuf, UDMABUF_CREATE_LIST, list);
       if (res->dmabuf_fd < 0) {
-        warn_report("%s: UDMABUF_CREATE_LIST: %s", __func__,
-                    strerror(errno));
+        error_setg_errno(errp, -res->dmabuf_fd,
+                         "Could not create dmabuf fd via udmabuf 
 driver");

This is a guest error since it indicates that the guest specified wrong addresses.

Regards,
Akihiko Odaki

Quoted 55 lines
hw/display/virtio-gpu-dmabuf.c +12 -6
+        return false;
       }
+    return true;
   }
-static void virtio_gpu_remap_dmabuf(struct virtio_gpu_simple_resource 
 *res)
+static bool virtio_gpu_remap_dmabuf(struct virtio_gpu_simple_resource 
 *res,
+                                    Error **errp)
   {
-    res->remapped = mmap(NULL, res->blob_size, PROT_READ,
-                         MAP_SHARED, res->dmabuf_fd, 0);
-    if (res->remapped == MAP_FAILED) {
-        warn_report("%s: dmabuf mmap failed: %s", __func__,
-                    strerror(errno));
+    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;
       }
+    res->remapped = map;
+    return true;

  }
  static void virtio_gpu_destroy_dmabuf(struct virtio_gpu_simple_resource *res)

hw/display/virtio-gpu-dmabuf.c +8 -4
@@ -125,6 +136,7 @@ bool virtio_gpu_have_udmabuf(void)
   void virtio_gpu_init_dmabuf(struct virtio_gpu_simple_resource *res)
   {
+    Error *local_err = NULL;
       void *pdata = NULL;
       res->dmabuf_fd = -1;
@@ -132,12 +144,15 @@ void virtio_gpu_init_dmabuf(struct 
 virtio_gpu_simple_resource *res)
           res->iov[0].iov_len < 4096) {
           pdata = res->iov[0].iov_base;
       } else {
-        virtio_gpu_create_udmabuf(res);
-        if (res->dmabuf_fd < 0) {
+        if (!virtio_gpu_create_udmabuf(res, &local_err)) {
+            if (local_err) {
+                error_report_err(local_err);
+            }
               return;
           }
-        virtio_gpu_remap_dmabuf(res);
-        if (!res->remapped) {
+
+        if (!virtio_gpu_remap_dmabuf(res, &local_err)) {
+            error_report_err(local_err);
               return;
           }

          pdata = res->remapped;

#9 lore raw

RE: [PATCH v6 06/11] virtio-gpu-dmabuf: Improve error handling by introducing 'Error **'

Hi Akihiko,

Quoted 66 lines

Subject: Re: [PATCH v6 06/11] virtio-gpu-dmabuf: Improve error handling by introducing 'Error **'

Quoted 6 lines
Quoted 6 lines

Make the error handling more robust in virtio_gpu_init_udmabuf() by passing in 'Error **' parameter to capture errors from virtio_gpu_create_udmabuf() and virtio_gpu_remap_dmabuf(). And, since they now take in 'Error **' parameter, have them return a bool to adhere to best practices.

Quoted 9 lines
Quoted 9 lines
Cc Marc-André Lureau <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Cédric Le Goater <[email protected]>

---
  hw/display/virtio-gpu-dmabuf.c | 45 ++++++++++++++++++++++-------

-----

Quoted 30 lines
Quoted 30 lines
  1 file changed, 30 insertions(+), 15 deletions(-)
hw/display/virtio-gpu- renamed from hw/display/virtio-gpu-dmabuf.c +0 -0

dmabuf.c
index 8d67ef7c2a..d9b2ecaf31 100644

hw/display/virtio-gpu-dmabuf.c +2 -1
@@ -27,7 +27,8 @@
   #include "standard-headers/linux/udmabuf.h"
   #include "standard-headers/drm/drm_fourcc.h"
-static void virtio_gpu_create_udmabuf(struct
 virtio_gpu_simple_resource *res)
+static bool virtio_gpu_create_udmabuf(struct
 virtio_gpu_simple_resource *res,
+                                      Error **errp)
   {
       g_autofree struct udmabuf_create_list *list = NULL;

      RAMBlock *rb;

hw/display/virtio-gpu-dmabuf.c +2 -1
@@ -36,7 +37,8 @@ static void virtio_gpu_create_udmabuf(struct
 virtio_gpu_simple_resource *res)
       udmabuf = udmabuf_fd();
       if (udmabuf < 0) {
-        return;
+        error_setg(errp, "udmabuf device not available");
+        return false;
       }

      list = g_malloc0(sizeof(struct udmabuf_create_list) +

hw/display/virtio-gpu-dmabuf.c +0 -0
@@ -45,7 +47,10 @@ static void virtio_gpu_create_udmabuf(struct
 virtio_gpu_simple_resource *res)
       for (i = 0; i < res->iov_cnt; i++) {
Quoted 8 lines

hw/display/virtio-gpu-dmabuf.c static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res) · line 53

Quoted 8 lines
hw/display/virtio-gpu-dmabuf.c +4 -1
           rb = qemu_ram_block_from_host(res->iov[i].iov_base, false,
 &offset);
           if (!rb || rb->fd < 0) {
-            return;
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: Could not find valid ramblock\n",
+                          __func__);
+            return false;

include/qapi/error.h suggests the following error handling pattern:

Quoted 6 lines

Call a function, receive an error from it, and pass it to the caller
- when the function returns a value that indicates failure, say false:

    if (!foo(arg, errp)) {
        handle the error...
    }
Quoted 2 lines

Returning false without passing an error to the caller breaks it so please don't do that.

That's why I added an additional check in the caller:

                if (local_err) {                                                                                                                                             
                    error_report_err(local_err);                                                                                                                             
                }

Having said that, I am not sure what is the right thing to do here. IMO, the proper way would be to add error_setg() but you mentioned doing that would be incorrect given that this is a Guest error. So, how should I proceed?

Quoted 15 lines
Quoted 15 lines

hw/display/virtio-gpu-dmabuf.c static void virtio_gpu_create_udmabuf(struct · line 68

Quoted 12 lines
hw/display/virtio-gpu-dmabuf.c +0 -0
           }

          list->list[i].memfd  = rb->fd;

hw/display/virtio-gpu-dmabuf.c +2 -2
@@ -58,20 +63,26 @@ static void virtio_gpu_create_udmabuf(struct
 virtio_gpu_simple_resource *res)
       res->dmabuf_fd = ioctl(udmabuf, UDMABUF_CREATE_LIST, list);
       if (res->dmabuf_fd < 0) {
-        warn_report("%s: UDMABUF_CREATE_LIST: %s", __func__,
-                    strerror(errno));
+        error_setg_errno(errp, -res->dmabuf_fd,
+                         "Could not create dmabuf fd via udmabuf
 driver");

This is a guest error since it indicates that the guest specified wrong addresses.

I don't have a strong opinion here but my understanding is that this should not be considered a Guest error because the rb (which we obtained using Guest addresses) is valid at this point, so the UDMABUF_CREATE_LIST IOCTL failure should be treated as a Host error.
Regardless, it is not clear how we can precisely determine who (Guest or Host) is responsible for the failure here and in some other places.

Thanks,
Vivek

Quoted 65 lines
Quoted 9 lines

Regards,
Akihiko Odaki

Quoted 5 lines
hw/display/virtio-gpu-dmabuf.c +2 -1
+        return false;
       }
+    return true;
   }
-static void virtio_gpu_remap_dmabuf(struct

virtio_gpu_simple_resource

Quoted 2 lines

hw/display/virtio-gpu-dmabuf.c static void virtio_gpu_create_udmabuf(struct · line 74

Quoted 2 lines
hw/display/virtio-gpu-dmabuf.c +1 -0
 *res)
+static bool virtio_gpu_remap_dmabuf(struct

virtio_gpu_simple_resource

Quoted 11 lines

hw/display/virtio-gpu-dmabuf.c static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res)

Quoted 11 lines
hw/display/virtio-gpu-dmabuf.c +4 -5
 *res,
+                                    Error **errp)
   {
-    res->remapped = mmap(NULL, res->blob_size, PROT_READ,
-                         MAP_SHARED, res->dmabuf_fd, 0);
-    if (res->remapped == MAP_FAILED) {
-        warn_report("%s: dmabuf mmap failed: %s", __func__,
-                    strerror(errno));
+    void *map;
+
+    map = mmap(NULL, res->blob_size, PROT_READ, MAP_SHARED,

res-

Quoted 13 lines
Quoted 12 lines
hw/display/virtio-gpu-dmabuf.c +5 -0
 >dmabuf_fd, 0);
+    if (map == MAP_FAILED) {
+        error_setg_errno(errp, errno, "dmabuf mmap failed");
           res->remapped = NULL;
+        return false;
       }
+    res->remapped = map;
+    return true;

  }
  static void virtio_gpu_destroy_dmabuf(struct
virtio_gpu_simple_resource *res)

hw/display/virtio-gpu-dmabuf.c +0 -0
@@ -125,6 +136,7 @@ bool virtio_gpu_have_udmabuf(void)
Quoted 1 line

  void virtio_gpu_init_dmabuf(struct virtio_gpu_simple_resource

*res)

Quoted 26 lines
Quoted 25 lines

  {
+    Error *local_err = NULL;
      void *pdata = NULL;
      res->dmabuf_fd = -1;

hw/display/virtio-gpu-dmabuf.c +7 -4
@@ -132,12 +144,15 @@ void virtio_gpu_init_dmabuf(struct
 virtio_gpu_simple_resource *res)
           res->iov[0].iov_len < 4096) {
           pdata = res->iov[0].iov_base;
       } else {
-        virtio_gpu_create_udmabuf(res);
-        if (res->dmabuf_fd < 0) {
+        if (!virtio_gpu_create_udmabuf(res, &local_err)) {
+            if (local_err) {
+                error_report_err(local_err);
+            }
               return;
           }
-        virtio_gpu_remap_dmabuf(res);
-        if (!res->remapped) {
+
+        if (!virtio_gpu_remap_dmabuf(res, &local_err)) {
+            error_report_err(local_err);
               return;
           }

          pdata = res->remapped;

#10 lore raw

RE: [PATCH v6 06/11] virtio-gpu-dmabuf: Improve error handling by introducing 'Error **'

Hi Akihiko,

Quoted 69 lines

Subject: RE: [PATCH v6 06/11] virtio-gpu-dmabuf: Improve error handling by introducing 'Error **'

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Make the error handling more robust in virtio_gpu_init_udmabuf() by passing in 'Error **' parameter to capture errors from virtio_gpu_create_udmabuf() and virtio_gpu_remap_dmabuf(). And, since they now take in 'Error **' parameter, have them return a bool to adhere to best practices.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Cc Marc-André Lureau <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Cédric Le Goater <[email protected]>

---
  hw/display/virtio-gpu-dmabuf.c | 45 ++++++++++++++++++++++------

-

Quoted 48 lines

-----

Quoted 30 lines
Quoted 30 lines
  1 file changed, 30 insertions(+), 15 deletions(-)
hw/display/virtio-gpu- renamed from hw/display/virtio-gpu-dmabuf.c +0 -0

dmabuf.c
index 8d67ef7c2a..d9b2ecaf31 100644

hw/display/virtio-gpu-dmabuf.c +2 -1
@@ -27,7 +27,8 @@
   #include "standard-headers/linux/udmabuf.h"
   #include "standard-headers/drm/drm_fourcc.h"
-static void virtio_gpu_create_udmabuf(struct
 virtio_gpu_simple_resource *res)
+static bool virtio_gpu_create_udmabuf(struct
 virtio_gpu_simple_resource *res,
+                                      Error **errp)
   {
       g_autofree struct udmabuf_create_list *list = NULL;

      RAMBlock *rb;

hw/display/virtio-gpu-dmabuf.c +2 -1
@@ -36,7 +37,8 @@ static void virtio_gpu_create_udmabuf(struct
 virtio_gpu_simple_resource *res)
       udmabuf = udmabuf_fd();
       if (udmabuf < 0) {
-        return;
+        error_setg(errp, "udmabuf device not available");
+        return false;
       }

      list = g_malloc0(sizeof(struct udmabuf_create_list) +

hw/display/virtio-gpu-dmabuf.c +0 -0
@@ -45,7 +47,10 @@ static void virtio_gpu_create_udmabuf(struct
 virtio_gpu_simple_resource *res)
       for (i = 0; i < res->iov_cnt; i++) {
Quoted 8 lines

hw/display/virtio-gpu-dmabuf.c static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res) · line 53

Quoted 8 lines
hw/display/virtio-gpu-dmabuf.c +4 -1
           rb = qemu_ram_block_from_host(res->iov[i].iov_base, false,
 &offset);
           if (!rb || rb->fd < 0) {
-            return;
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: Could not find valid ramblock\n",
+                          __func__);
+            return false;

include/qapi/error.h suggests the following error handling pattern:

Quoted 6 lines

Call a function, receive an error from it, and pass it to the caller
- when the function returns a value that indicates failure, say false:

    if (!foo(arg, errp)) {
        handle the error...
    }
Quoted 2 lines

Returning false without passing an error to the caller breaks it so please don't do that.

Quoted 8 lines

That's why I added an additional check in the caller:

                if (local_err) {
                    error_report_err(local_err);
                }

Having said that, I am not sure what is the right thing to do here. IMO, the proper way would be to add error_setg() but you mentioned doing that would be incorrect given that this is a Guest error. So, how should I proceed?

Any further thoughts/comments here and below?

Thanks,
Vivek

Quoted 95 lines
Quoted 16 lines
Quoted 16 lines

hw/display/virtio-gpu-dmabuf.c static void virtio_gpu_create_udmabuf(struct · line 68

Quoted 13 lines
hw/display/virtio-gpu-dmabuf.c +0 -0
           }

          list->list[i].memfd  = rb->fd;

hw/display/virtio-gpu-dmabuf.c +2 -2
@@ -58,20 +63,26 @@ static void virtio_gpu_create_udmabuf(struct
 virtio_gpu_simple_resource *res)
       res->dmabuf_fd = ioctl(udmabuf, UDMABUF_CREATE_LIST, list);
       if (res->dmabuf_fd < 0) {
-        warn_report("%s: UDMABUF_CREATE_LIST: %s", __func__,
-                    strerror(errno));
+        error_setg_errno(errp, -res->dmabuf_fd,
+                         "Could not create dmabuf fd via udmabuf
 driver");

This is a guest error since it indicates that the guest specified wrong addresses.

I don't have a strong opinion here but my understanding is that this should
not be considered a Guest error because the rb (which we obtained using Guest addresses) is valid at this point, so the UDMABUF_CREATE_LIST IOCTL
failure should be treated as a Host error.
Regardless, it is not clear how we can precisely determine who (Guest or Host)
is responsible for the failure here and in some other places.

Thanks,
Vivek

Quoted 65 lines
Quoted 9 lines

Regards,
Akihiko Odaki

Quoted 5 lines
hw/display/virtio-gpu-dmabuf.c +2 -1
+        return false;
       }
+    return true;
   }
-static void virtio_gpu_remap_dmabuf(struct

virtio_gpu_simple_resource

Quoted 2 lines

hw/display/virtio-gpu-dmabuf.c static void virtio_gpu_create_udmabuf(struct · line 74

Quoted 2 lines
hw/display/virtio-gpu-dmabuf.c +1 -0
 *res)
+static bool virtio_gpu_remap_dmabuf(struct

virtio_gpu_simple_resource

Quoted 11 lines

hw/display/virtio-gpu-dmabuf.c static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res)

Quoted 11 lines
hw/display/virtio-gpu-dmabuf.c +4 -5
 *res,
+                                    Error **errp)
   {
-    res->remapped = mmap(NULL, res->blob_size, PROT_READ,
-                         MAP_SHARED, res->dmabuf_fd, 0);
-    if (res->remapped == MAP_FAILED) {
-        warn_report("%s: dmabuf mmap failed: %s", __func__,
-                    strerror(errno));
+    void *map;
+
+    map = mmap(NULL, res->blob_size, PROT_READ, MAP_SHARED,

res-

Quoted 13 lines
Quoted 12 lines
hw/display/virtio-gpu-dmabuf.c +5 -0
 >dmabuf_fd, 0);
+    if (map == MAP_FAILED) {
+        error_setg_errno(errp, errno, "dmabuf mmap failed");
           res->remapped = NULL;
+        return false;
       }
+    res->remapped = map;
+    return true;

  }
  static void virtio_gpu_destroy_dmabuf(struct
virtio_gpu_simple_resource *res)

hw/display/virtio-gpu-dmabuf.c +0 -0
@@ -125,6 +136,7 @@ bool virtio_gpu_have_udmabuf(void)
Quoted 1 line

  void virtio_gpu_init_dmabuf(struct virtio_gpu_simple_resource

*res)

Quoted 26 lines
Quoted 25 lines

  {
+    Error *local_err = NULL;
      void *pdata = NULL;
      res->dmabuf_fd = -1;

hw/display/virtio-gpu-dmabuf.c +7 -4
@@ -132,12 +144,15 @@ void virtio_gpu_init_dmabuf(struct
 virtio_gpu_simple_resource *res)
           res->iov[0].iov_len < 4096) {
           pdata = res->iov[0].iov_base;
       } else {
-        virtio_gpu_create_udmabuf(res);
-        if (res->dmabuf_fd < 0) {
+        if (!virtio_gpu_create_udmabuf(res, &local_err)) {
+            if (local_err) {
+                error_report_err(local_err);
+            }
               return;
           }
-        virtio_gpu_remap_dmabuf(res);
-        if (!res->remapped) {
+
+        if (!virtio_gpu_remap_dmabuf(res, &local_err)) {
+            error_report_err(local_err);
               return;
           }

          pdata = res->remapped;

#11 lore raw

Re: [PATCH v6 06/11] virtio-gpu-dmabuf: Improve error handling by introducing 'Error **'

Quoted 79 lines · Vivek

Hi Akihiko,

Quoted 69 lines

Subject: RE: [PATCH v6 06/11] virtio-gpu-dmabuf: Improve error handling by introducing 'Error **'

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Make the error handling more robust in virtio_gpu_init_udmabuf() by passing in 'Error **' parameter to capture errors from virtio_gpu_create_udmabuf() and virtio_gpu_remap_dmabuf(). And, since they now take in 'Error **' parameter, have them return a bool to adhere to best practices.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Cc Marc-André Lureau <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Cédric Le Goater <[email protected]>
---
   hw/display/virtio-gpu-dmabuf.c | 45 ++++++++++++++++++++++------

-

Quoted 48 lines

-----

Quoted 30 lines
Quoted 30 lines
   1 file changed, 30 insertions(+), 15 deletions(-)
hw/display/virtio-gpu- renamed from hw/display/virtio-gpu-dmabuf.c +0 -0

dmabuf.c
index 8d67ef7c2a..d9b2ecaf31 100644

hw/display/virtio-gpu-dmabuf.c +2 -1
@@ -27,7 +27,8 @@
   #include "standard-headers/linux/udmabuf.h"
   #include "standard-headers/drm/drm_fourcc.h"
-static void virtio_gpu_create_udmabuf(struct
 virtio_gpu_simple_resource *res)
+static bool virtio_gpu_create_udmabuf(struct
 virtio_gpu_simple_resource *res,
+                                      Error **errp)
   {
       g_autofree struct udmabuf_create_list *list = NULL;

      RAMBlock *rb;

hw/display/virtio-gpu-dmabuf.c +2 -1
@@ -36,7 +37,8 @@ static void virtio_gpu_create_udmabuf(struct
 virtio_gpu_simple_resource *res)
       udmabuf = udmabuf_fd();
       if (udmabuf < 0) {
-        return;
+        error_setg(errp, "udmabuf device not available");
+        return false;
       }
       list = g_malloc0(sizeof(struct udmabuf_create_list) +
@@ -45,7 +47,10 @@ static void virtio_gpu_create_udmabuf(struct
 virtio_gpu_simple_resource *res)
       for (i = 0; i < res->iov_cnt; i++) {
Quoted 8 lines

hw/display/virtio-gpu-dmabuf.c static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res) · line 53

Quoted 8 lines
hw/display/virtio-gpu-dmabuf.c +4 -1
           rb = qemu_ram_block_from_host(res->iov[i].iov_base, false,
 &offset);
           if (!rb || rb->fd < 0) {
-            return;
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: Could not find valid ramblock\n",
+                          __func__);
+            return false;

include/qapi/error.h suggests the following error handling pattern:

Quoted 6 lines

Call a function, receive an error from it, and pass it to the caller
- when the function returns a value that indicates failure, say false:

    if (!foo(arg, errp)) {
        handle the error...
    }
Quoted 2 lines

Returning false without passing an error to the caller breaks it so please don't do that.

Quoted 8 lines

That's why I added an additional check in the caller:

                 if (local_err) {
                     error_report_err(local_err);
                 }

Having said that, I am not sure what is the right thing to do here. IMO, the proper way would be to add error_setg() but you mentioned doing that would be incorrect given that this is a Guest error. So, how should I proceed?

Quoted 1 line

Any further thoughts/comments here and below?

Sorry, I missed them.

Here, this function should not require its caller to have such an additional check. Ideally, the idiom described in include/qapi/error.h should work.

include/qapi/error.h says:

Quoted 7 lines

- Whenever practical, also return a value that indicates success / failure. This can make the error checking more concise, and can avoid useless error object creation and destruction. Note that we still have many functions returning void. We recommend
• bool-valued functions return true on success / false on failure,
• pointer-valued functions return non-null / null pointer, and
• integer-valued functions return non-negative / negative.

Since this function is deriving a file descriptor, it should return one. The error condition should be represented by a negative value. It is a common to return -errno and let the caller distinguish error conditions, but the error conditions we care cannot properly represented with -errno, so there should be an enum for the two error conditions: invalid iov or anything else.

Quoted 29 lines

Thanks,
Vivek

Quoted 25 lines
Quoted 16 lines
Quoted 16 lines

hw/display/virtio-gpu-dmabuf.c static void virtio_gpu_create_udmabuf(struct · line 68

Quoted 13 lines
hw/display/virtio-gpu-dmabuf.c +0 -0
           }

          list->list[i].memfd  = rb->fd;

hw/display/virtio-gpu-dmabuf.c +2 -2
@@ -58,20 +63,26 @@ static void virtio_gpu_create_udmabuf(struct
 virtio_gpu_simple_resource *res)
       res->dmabuf_fd = ioctl(udmabuf, UDMABUF_CREATE_LIST, list);
       if (res->dmabuf_fd < 0) {
-        warn_report("%s: UDMABUF_CREATE_LIST: %s", __func__,
-                    strerror(errno));
+        error_setg_errno(errp, -res->dmabuf_fd,
+                         "Could not create dmabuf fd via udmabuf
 driver");

This is a guest error since it indicates that the guest specified wrong addresses.

I don't have a strong opinion here but my understanding is that this should
not be considered a Guest error because the rb (which we obtained using Guest addresses) is valid at this point, so the UDMABUF_CREATE_LIST IOCTL
failure should be treated as a Host error.
Regardless, it is not clear how we can precisely determine who (Guest or Host)
is responsible for the failure here and in some other places.

rb->fd may still contain something other than memfd.

Assume anything the guest passes can be invalid. iov the guest passes can be whatever you do not expect.

Regards,
Akihiko Odaki

Quoted 71 lines
Quoted 70 lines

Thanks,
Vivek

Quoted 65 lines
Quoted 9 lines

Regards,
Akihiko Odaki

Quoted 5 lines
hw/display/virtio-gpu-dmabuf.c +2 -1
+        return false;
       }
+    return true;
   }
-static void virtio_gpu_remap_dmabuf(struct

virtio_gpu_simple_resource

Quoted 2 lines

hw/display/virtio-gpu-dmabuf.c static void virtio_gpu_create_udmabuf(struct · line 74

Quoted 2 lines
hw/display/virtio-gpu-dmabuf.c +1 -0
 *res)
+static bool virtio_gpu_remap_dmabuf(struct

virtio_gpu_simple_resource

Quoted 11 lines

hw/display/virtio-gpu-dmabuf.c static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res)

Quoted 11 lines
hw/display/virtio-gpu-dmabuf.c +4 -5
 *res,
+                                    Error **errp)
   {
-    res->remapped = mmap(NULL, res->blob_size, PROT_READ,
-                         MAP_SHARED, res->dmabuf_fd, 0);
-    if (res->remapped == MAP_FAILED) {
-        warn_report("%s: dmabuf mmap failed: %s", __func__,
-                    strerror(errno));
+    void *map;
+
+    map = mmap(NULL, res->blob_size, PROT_READ, MAP_SHARED,

res-

Quoted 13 lines

hw/display/virtio-gpu-dmabuf.c bool virtio_gpu_have_udmabuf(void) · line 136

Quoted 13 lines
hw/display/virtio-gpu-dmabuf.c +5 -0
 > dmabuf_fd, 0);
+    if (map == MAP_FAILED) {
+        error_setg_errno(errp, errno, "dmabuf mmap failed");
           res->remapped = NULL;
+        return false;
       }
+    res->remapped = map;
+    return true;

  }
  static void virtio_gpu_destroy_dmabuf(struct
virtio_gpu_simple_resource *res)

hw/display/virtio-gpu-dmabuf.c +0 -0
@@ -125,6 +136,7 @@ bool virtio_gpu_have_udmabuf(void)
   void virtio_gpu_init_dmabuf(struct virtio_gpu_simple_resource

*res)

Quoted 26 lines
Quoted 25 lines
hw/display/virtio-gpu-dmabuf.c +8 -4
   {
+    Error *local_err = NULL;
       void *pdata = NULL;
       res->dmabuf_fd = -1;
@@ -132,12 +144,15 @@ void virtio_gpu_init_dmabuf(struct
 virtio_gpu_simple_resource *res)
           res->iov[0].iov_len < 4096) {
           pdata = res->iov[0].iov_base;
       } else {
-        virtio_gpu_create_udmabuf(res);
-        if (res->dmabuf_fd < 0) {
+        if (!virtio_gpu_create_udmabuf(res, &local_err)) {
+            if (local_err) {
+                error_report_err(local_err);
+            }
               return;
           }
-        virtio_gpu_remap_dmabuf(res);
-        if (!res->remapped) {
+
+        if (!virtio_gpu_remap_dmabuf(res, &local_err)) {
+            error_report_err(local_err);
               return;
           }

          pdata = res->remapped;

#12 lore raw

RE: [PATCH v6 06/11] virtio-gpu-dmabuf: Improve error handling by introducing 'Error **'

Hi Akihiko,

Quoted 102 lines

Subject: Re: [PATCH v6 06/11] virtio-gpu-dmabuf: Improve error handling by introducing 'Error **'

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Make the error handling more robust in

virtio_gpu_init_udmabuf()

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

by passing in 'Error **' parameter to capture errors from virtio_gpu_create_udmabuf() and virtio_gpu_remap_dmabuf(). And, since they now take in 'Error **' parameter, have them return a bool to adhere to best practices.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Cc Marc-André Lureau <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Cédric Le Goater <[email protected]>
---
   hw/display/virtio-gpu-dmabuf.c | 45 ++++++++++++++++++++++-

-----

Quoted 5 lines
Quoted 5 lines

-

Quoted 4 lines

-----

Quoted 3 lines

hw/display/virtio-

Quoted 3 lines
   1 file changed, 30 insertions(+), 15 deletions(-)
hw/display/virtio- renamed from hw/display/virtio-gpu-dmabuf.c +0 -0

gpu-

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

dmabuf.c
index 8d67ef7c2a..d9b2ecaf31 100644

hw/display/virtio-gpu-dmabuf.c +0 -1
@@ -27,7 +27,8 @@
   #include "standard-headers/linux/udmabuf.h"
   #include "standard-headers/drm/drm_fourcc.h"
-static void virtio_gpu_create_udmabuf(struct
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

hw/display/virtio-gpu-dmabuf.c static void

Quoted 8 lines
hw/display/virtio-gpu-dmabuf.c +2 -0
 virtio_gpu_simple_resource *res)
+static bool virtio_gpu_create_udmabuf(struct
 virtio_gpu_simple_resource *res,
+                                      Error **errp)
   {
       g_autofree struct udmabuf_create_list *list = NULL;

      RAMBlock *rb;

hw/display/virtio-gpu-dmabuf.c +0 -0
@@ -36,7 +37,8 @@ static void

virtio_gpu_create_udmabuf(struct

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

hw/display/virtio-gpu-dmabuf.c static void

Quoted 9 lines
hw/display/virtio-gpu-dmabuf.c +2 -1
 virtio_gpu_simple_resource *res)
       udmabuf = udmabuf_fd();
       if (udmabuf < 0) {
-        return;
+        error_setg(errp, "udmabuf device not available");
+        return false;
       }
       list = g_malloc0(sizeof(struct udmabuf_create_list) +
@@ -45,7 +47,10 @@ static void

virtio_gpu_create_udmabuf(struct

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

hw/display/virtio-gpu-dmabuf.c static void · line 49

Quoted 3 lines
hw/display/virtio-gpu-dmabuf.c +0 -0
 virtio_gpu_simple_resource *res)
       for (i = 0; i < res->iov_cnt; i++) {
           rb = qemu_ram_block_from_host(res->iov[i].iov_base,

false,

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 7 lines

hw/display/virtio-gpu-dmabuf.c static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res) · line 53

Quoted 7 lines
hw/display/virtio-gpu-dmabuf.c +4 -1
 &offset);
           if (!rb || rb->fd < 0) {
-            return;
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: Could not find valid ramblock\n",
+                          __func__);
+            return false;

include/qapi/error.h suggests the following error handling pattern:

Quoted 6 lines

Call a function, receive an error from it, and pass it to the caller
- when the function returns a value that indicates failure, say false:

    if (!foo(arg, errp)) {
        handle the error...
    }
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

Returning false without passing an error to the caller breaks it so please don't do that.

That's why I added an additional check in the caller:

                 if (local_err) {
                     error_report_err(local_err);
                 }

Having said that, I am not sure what is the right thing to do here. IMO, the proper way would be to add error_setg() but you

mentioned

Quoted 3 lines
Quoted 2 lines

doing that would be incorrect given that this is a Guest error. So, how should I proceed?

Any further thoughts/comments here and below?

Sorry, I missed them.

Here, this function should not require its caller to have such an additional check. Ideally, the idiom described in include/qapi/error.h should work.

include/qapi/error.h says:

Quoted 7 lines

- Whenever practical, also return a value that indicates success / failure. This can make the error checking more concise, and can avoid useless error object creation and destruction. Note that we still have many functions returning void. We recommend . bool-valued functions return true on success / false on failure, . pointer-valued functions return non-null / null pointer, and . integer-valued functions return non-negative / negative.

Quoted 6 lines

Since this function is deriving a file descriptor, it should return one. The error condition should be represented by a negative value. It is a common to return -errno and let the caller distinguish error conditions, but the error conditions we care cannot properly represented with -errno, so there should be an enum for the two error conditions: invalid iov or anything else.

Ok, I'll look into adding an enum but I feel like this is getting unnecessarily complex particularly with the introduction of 'Error *'. I think it might be easier to just not use 'Error *' in virtio_gpu_create_udmabuf() and elsewhere.

Quoted 25 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

hw/display/virtio-gpu-dmabuf.c static void

Quoted 2 lines
hw/display/virtio-gpu-dmabuf.c +0 -0
           list->list[i].memfd  = rb->fd;
@@ -58,20 +63,26 @@ static void

virtio_gpu_create_udmabuf(struct

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

hw/display/virtio-gpu-dmabuf.c static void · line 64

Quoted 2 lines
hw/display/virtio-gpu-dmabuf.c +0 -0
 virtio_gpu_simple_resource *res)
       res->dmabuf_fd = ioctl(udmabuf, UDMABUF_CREATE_LIST,

list);

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

hw/display/virtio-gpu-dmabuf.c static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res)

Quoted 6 lines
hw/display/virtio-gpu-dmabuf.c +2 -2
       if (res->dmabuf_fd < 0) {
-        warn_report("%s: UDMABUF_CREATE_LIST: %s", __func__,
-                    strerror(errno));
+        error_setg_errno(errp, -res->dmabuf_fd,
+                         "Could not create dmabuf fd via udmabuf
 driver");

This is a guest error since it indicates that the guest specified

wrong

Quoted 4 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line

addresses.

I don't have a strong opinion here but my understanding is that this should
not be considered a Guest error because the rb (which we obtained

using

Quoted 1 line
Quoted 1 line

Guest addresses) is valid at this point, so the

UDMABUF_CREATE_LIST

Quoted 3 lines
Quoted 3 lines

IOCTL
failure should be treated as a Host error. Regardless, it is not clear how we can precisely determine who

Quoted 8 lines

(Guest or

Quoted 2 lines
Quoted 2 lines

Host)
is responsible for the failure here and in some other places.

rb->fd may still contain something other than memfd.

Assume anything the guest passes can be invalid. iov the guest passes can be whatever you do not expect.

Right, but my point is that, for example, if UDMABUF_CREATE_LIST IOCTL failure is due to -ENOMEM, then how can we distinguish this case and categorize it as Host error?

And, similarly, rb->fd being something other than memfd does lead to UDMABUF_CREATE_LIST IOCTL failure and this needs to be categorized as Guest error. So, can we rely on errno being -EINVAL or -EBADFD to identify Guest errors and categorize all other errors as Host errors?

Thanks,
Vivek

Quoted 76 lines

Regards,
Akihiko Odaki

Quoted 17 lines
Quoted 17 lines

Thanks,
Vivek

Quoted 13 lines
Quoted 9 lines

Regards,
Akihiko Odaki

Quoted 5 lines
hw/display/virtio-gpu-dmabuf.c +2 -1
+        return false;
       }
+    return true;
   }
-static void virtio_gpu_remap_dmabuf(struct

virtio_gpu_simple_resource

Quoted 2 lines

hw/display/virtio-gpu-dmabuf.c static void · line 74

Quoted 2 lines
hw/display/virtio-gpu-dmabuf.c +1 -0
 *res)
+static bool virtio_gpu_remap_dmabuf(struct

virtio_gpu_simple_resource

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

hw/display/virtio-gpu-dmabuf.c static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res)

Quoted 11 lines
hw/display/virtio-gpu-dmabuf.c +4 -5
 *res,
+                                    Error **errp)
   {
-    res->remapped = mmap(NULL, res->blob_size, PROT_READ,
-                         MAP_SHARED, res->dmabuf_fd, 0);
-    if (res->remapped == MAP_FAILED) {
-        warn_report("%s: dmabuf mmap failed: %s", __func__,
-                    strerror(errno));
+    void *map;
+
+    map = mmap(NULL, res->blob_size, PROT_READ,

MAP_SHARED,

Quoted 43 lines
Quoted 42 lines
Quoted 41 lines

res-

Quoted 13 lines

hw/display/virtio-gpu-dmabuf.c bool virtio_gpu_have_udmabuf(void) · line 136

Quoted 13 lines
hw/display/virtio-gpu-dmabuf.c +5 -0
 > dmabuf_fd, 0);
+    if (map == MAP_FAILED) {
+        error_setg_errno(errp, errno, "dmabuf mmap failed");
           res->remapped = NULL;
+        return false;
       }
+    res->remapped = map;
+    return true;

  }
  static void virtio_gpu_destroy_dmabuf(struct
virtio_gpu_simple_resource *res)

hw/display/virtio-gpu-dmabuf.c +0 -0
@@ -125,6 +136,7 @@ bool virtio_gpu_have_udmabuf(void)
   void virtio_gpu_init_dmabuf(struct virtio_gpu_simple_resource

*res)

Quoted 26 lines
Quoted 25 lines
hw/display/virtio-gpu-dmabuf.c +8 -4
   {
+    Error *local_err = NULL;
       void *pdata = NULL;
       res->dmabuf_fd = -1;
@@ -132,12 +144,15 @@ void virtio_gpu_init_dmabuf(struct
 virtio_gpu_simple_resource *res)
           res->iov[0].iov_len < 4096) {
           pdata = res->iov[0].iov_base;
       } else {
-        virtio_gpu_create_udmabuf(res);
-        if (res->dmabuf_fd < 0) {
+        if (!virtio_gpu_create_udmabuf(res, &local_err)) {
+            if (local_err) {
+                error_report_err(local_err);
+            }
               return;
           }
-        virtio_gpu_remap_dmabuf(res);
-        if (!res->remapped) {
+
+        if (!virtio_gpu_remap_dmabuf(res, &local_err)) {
+            error_report_err(local_err);
               return;
           }

          pdata = res->remapped;

#13 lore raw

Re: [PATCH v6 06/11] virtio-gpu-dmabuf: Improve error handling by introducing 'Error **'

Quoted 110 lines · Vivek

Hi Akihiko,

Quoted 102 lines

Subject: Re: [PATCH v6 06/11] virtio-gpu-dmabuf: Improve error handling by introducing 'Error **'

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Make the error handling more robust in

virtio_gpu_init_udmabuf()

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

by passing in 'Error **' parameter to capture errors from virtio_gpu_create_udmabuf() and virtio_gpu_remap_dmabuf(). And, since they now take in 'Error **' parameter, have them return a bool to adhere to best practices.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Cc Marc-André Lureau <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Cédric Le Goater <[email protected]>
---
    hw/display/virtio-gpu-dmabuf.c | 45 ++++++++++++++++++++++-

-----

Quoted 5 lines
Quoted 5 lines

-

Quoted 4 lines

-----

Quoted 3 lines

hw/display/virtio-

Quoted 3 lines
    1 file changed, 30 insertions(+), 15 deletions(-)
hw/display/virtio- renamed from hw/display/virtio-gpu-dmabuf.c +0 -0

gpu-

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

dmabuf.c
index 8d67ef7c2a..d9b2ecaf31 100644

hw/display/virtio-gpu-dmabuf.c +0 -1
@@ -27,7 +27,8 @@
    #include "standard-headers/linux/udmabuf.h"
    #include "standard-headers/drm/drm_fourcc.h"
-static void virtio_gpu_create_udmabuf(struct
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

hw/display/virtio-gpu-dmabuf.c static void

Quoted 8 lines
hw/display/virtio-gpu-dmabuf.c +2 -0
 virtio_gpu_simple_resource *res)
+static bool virtio_gpu_create_udmabuf(struct
 virtio_gpu_simple_resource *res,
+                                      Error **errp)
    {
        g_autofree struct udmabuf_create_list *list = NULL;

      RAMBlock *rb;

hw/display/virtio-gpu-dmabuf.c +0 -0
@@ -36,7 +37,8 @@ static void

virtio_gpu_create_udmabuf(struct

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

hw/display/virtio-gpu-dmabuf.c static void

Quoted 9 lines
hw/display/virtio-gpu-dmabuf.c +2 -1
 virtio_gpu_simple_resource *res)
        udmabuf = udmabuf_fd();
        if (udmabuf < 0) {
-        return;
+        error_setg(errp, "udmabuf device not available");
+        return false;
        }
        list = g_malloc0(sizeof(struct udmabuf_create_list) +
@@ -45,7 +47,10 @@ static void

virtio_gpu_create_udmabuf(struct

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

hw/display/virtio-gpu-dmabuf.c static void · line 49

Quoted 3 lines
hw/display/virtio-gpu-dmabuf.c +0 -0
 virtio_gpu_simple_resource *res)
        for (i = 0; i < res->iov_cnt; i++) {
            rb = qemu_ram_block_from_host(res->iov[i].iov_base,

false,

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 7 lines

hw/display/virtio-gpu-dmabuf.c static void · line 55

Quoted 7 lines
hw/display/virtio-gpu-dmabuf.c +4 -1
 &offset);
            if (!rb || rb->fd < 0) {
-            return;
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: Could not find valid ramblock\n",
+                          __func__);
+            return false;

include/qapi/error.h suggests the following error handling pattern:

Quoted 6 lines

Call a function, receive an error from it, and pass it to the caller
- when the function returns a value that indicates failure, say false:

    if (!foo(arg, errp)) {
        handle the error...
    }
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

Returning false without passing an error to the caller breaks it so please don't do that.

That's why I added an additional check in the caller:

                  if (local_err) {
                      error_report_err(local_err);
                  }

Having said that, I am not sure what is the right thing to do here. IMO, the proper way would be to add error_setg() but you

mentioned

Quoted 3 lines
Quoted 2 lines

doing that would be incorrect given that this is a Guest error. So, how should I proceed?

Any further thoughts/comments here and below?

Sorry, I missed them.

Here, this function should not require its caller to have such an additional check. Ideally, the idiom described in include/qapi/error.h should work.

include/qapi/error.h says:

Quoted 7 lines

- Whenever practical, also return a value that indicates success / failure. This can make the error checking more concise, and can avoid useless error object creation and destruction. Note that we still have many functions returning void. We recommend . bool-valued functions return true on success / false on failure, . pointer-valued functions return non-null / null pointer, and . integer-valued functions return non-negative / negative.

Quoted 6 lines

Since this function is deriving a file descriptor, it should return one. The error condition should be represented by a negative value. It is a common to return -errno and let the caller distinguish error conditions, but the error conditions we care cannot properly represented with -errno, so there should be an enum for the two error conditions: invalid iov or anything else.

Quoted 3 lines

Ok, I'll look into adding an enum but I feel like this is getting unnecessarily complex particularly with the introduction of 'Error *'. I think it might be easier to just not use 'Error *' in virtio_gpu_create_udmabuf() and elsewhere.

Error * is just a convenient mechanism to propagate error messages. You will want it to report the "anything else invalid iov" condition because you will have no idea what it is without Error *.

Quoted 34 lines
Quoted 26 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

hw/display/virtio-gpu-dmabuf.c static void

Quoted 3 lines
hw/display/virtio-gpu-dmabuf.c +0 -0
            list->list[i].memfd  = rb->fd;
@@ -58,20 +63,26 @@ static void

virtio_gpu_create_udmabuf(struct

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

hw/display/virtio-gpu-dmabuf.c static void · line 64

Quoted 2 lines
hw/display/virtio-gpu-dmabuf.c +0 -0
 virtio_gpu_simple_resource *res)
        res->dmabuf_fd = ioctl(udmabuf, UDMABUF_CREATE_LIST,

list);

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

hw/display/virtio-gpu-dmabuf.c static void · line 68

Quoted 6 lines
hw/display/virtio-gpu-dmabuf.c +2 -2
        if (res->dmabuf_fd < 0) {
-        warn_report("%s: UDMABUF_CREATE_LIST: %s", __func__,
-                    strerror(errno));
+        error_setg_errno(errp, -res->dmabuf_fd,
+                         "Could not create dmabuf fd via udmabuf
 driver");

This is a guest error since it indicates that the guest specified

wrong

Quoted 4 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line

addresses.

I don't have a strong opinion here but my understanding is that this should
not be considered a Guest error because the rb (which we obtained

using

Quoted 1 line
Quoted 1 line

Guest addresses) is valid at this point, so the

UDMABUF_CREATE_LIST

Quoted 3 lines
Quoted 3 lines

IOCTL
failure should be treated as a Host error. Regardless, it is not clear how we can precisely determine who

Quoted 8 lines

(Guest or

Quoted 2 lines
Quoted 2 lines

Host)
is responsible for the failure here and in some other places.

rb->fd may still contain something other than memfd.

Assume anything the guest passes can be invalid. iov the guest passes can be whatever you do not expect.

Quoted 8 lines

Right, but my point is that, for example, if UDMABUF_CREATE_LIST IOCTL failure is due to -ENOMEM, then how can we distinguish this case and categorize it as Host error?

And, similarly, rb->fd being something other than memfd does lead to UDMABUF_CREATE_LIST IOCTL failure and this needs to be categorized as Guest error. So, can we rely on errno being -EINVAL or -EBADFD to identify Guest errors and categorize all other errors as Host errors?

Yes. As far as I can tell, the ioctl will receive an invalid argument or bad file descriptor if and only if the guest makes an error, so -EINVAL or -EBADFD should be used.

Perhaps we may get these errors if the kernel has a bug or the hardware is faulty, but it's fine to ignore the possibility. Whatever can happen if QEMU and the platform are unreliable and it's impossible to recover errors in such a scenario. We are effectively drawing a security and reliability boundary here; anything inside is trusted and reliable and anything outside is not. The kernel is inside and the iov is from the outside.

Regards,
Akihiko Odaki

Quoted 81 lines

Thanks,
Vivek

Quoted 76 lines

Regards,
Akihiko Odaki

Quoted 17 lines
Quoted 17 lines

Thanks,
Vivek

Quoted 13 lines
Quoted 9 lines

Regards,
Akihiko Odaki

Quoted 5 lines
hw/display/virtio-gpu-dmabuf.c +2 -1
+        return false;
        }
+    return true;
    }
-static void virtio_gpu_remap_dmabuf(struct

virtio_gpu_simple_resource

Quoted 2 lines

hw/display/virtio-gpu-dmabuf.c static void · line 74

Quoted 2 lines
hw/display/virtio-gpu-dmabuf.c +1 -0
 *res)
+static bool virtio_gpu_remap_dmabuf(struct

virtio_gpu_simple_resource

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

hw/display/virtio-gpu-dmabuf.c static void · line 80

Quoted 11 lines
hw/display/virtio-gpu-dmabuf.c +4 -5
 *res,
+                                    Error **errp)
    {
-    res->remapped = mmap(NULL, res->blob_size, PROT_READ,
-                         MAP_SHARED, res->dmabuf_fd, 0);
-    if (res->remapped == MAP_FAILED) {
-        warn_report("%s: dmabuf mmap failed: %s", __func__,
-                    strerror(errno));
+    void *map;
+
+    map = mmap(NULL, res->blob_size, PROT_READ,

MAP_SHARED,

Quoted 43 lines
Quoted 42 lines
Quoted 41 lines

res-

Quoted 13 lines

hw/display/virtio-gpu-dmabuf.c bool virtio_gpu_have_udmabuf(void) · line 136

Quoted 13 lines
hw/display/virtio-gpu-dmabuf.c +5 -0
 > dmabuf_fd, 0);
+    if (map == MAP_FAILED) {
+        error_setg_errno(errp, errno, "dmabuf mmap failed");
            res->remapped = NULL;
+        return false;
        }
+    res->remapped = map;
+    return true;

  }
  static void virtio_gpu_destroy_dmabuf(struct
virtio_gpu_simple_resource *res)

hw/display/virtio-gpu-dmabuf.c +0 -0
@@ -125,6 +136,7 @@ bool virtio_gpu_have_udmabuf(void)
    void virtio_gpu_init_dmabuf(struct virtio_gpu_simple_resource

*res)

Quoted 26 lines
Quoted 25 lines
hw/display/virtio-gpu-dmabuf.c +8 -4
    {
+    Error *local_err = NULL;
        void *pdata = NULL;
        res->dmabuf_fd = -1;
@@ -132,12 +144,15 @@ void virtio_gpu_init_dmabuf(struct
 virtio_gpu_simple_resource *res)
            res->iov[0].iov_len < 4096) {
            pdata = res->iov[0].iov_base;
        } else {
-        virtio_gpu_create_udmabuf(res);
-        if (res->dmabuf_fd < 0) {
+        if (!virtio_gpu_create_udmabuf(res, &local_err)) {
+            if (local_err) {
+                error_report_err(local_err);
+            }
                return;
            }
-        virtio_gpu_remap_dmabuf(res);
-        if (!res->remapped) {
+
+        if (!virtio_gpu_remap_dmabuf(res, &local_err)) {
+            error_report_err(local_err);
                return;
            }
            pdata = res->remapped;
#14 lore raw

PATCH v6 7/11 vfio: Document vfio_device_get_region_info()

In reply to Vivek Kasireddy

Add documentation for vfio_device_get_region_info() and clarify the expectations around its usage.

---
 include/hw/vfio/vfio-device.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)
include/hw/vfio/vfio-device.h +13 -0
index 35a5ec6d92..828a31c006 100644
@@ -275,6 +275,19 @@ bool vfio_device_get_host_iommu_quirk_bypass_ro(VFIODevice *vbasedev,
 int vfio_device_get_feature(VFIODevice *vbasedev,
                             struct vfio_device_feature *feature);
 
+/**
+ * Return the region info for a given region index. The region info includes
+ * details such as size, offset, and capabilities. Note that the returned
+ * info pointer is either a cached copy or newly allocated by
+ * vfio_device_get_region_info(), so the caller is not expected to allocate
+ * or free it.
+ *
+ * @vbasedev: #VFIODevice to use
+ * @index: region index
+ * @info: pointer to store the region info
+ *
+ * Returns 0 on success or a negative value on error.
+ */
 int vfio_device_get_region_info(VFIODevice *vbasedev, int index,
                                 struct vfio_region_info **info);
 int vfio_device_get_region_info_type(VFIODevice *vbasedev, uint32_t type,
Signature · 1 line
2.50.1
#15 lore raw

PATCH v6 8/11 vfio/region: Add a helper to get region index from memory region

In reply to Vivek Kasireddy

Having a way to figure out the region index (or bar) associated with a memory region is helpful in various scenarios. For example, this capability can be useful in retrieving the region info needed for mapping a part of a VFIO region or creating a dmabuf.

Cc Alex Williamson <[email protected]>
Cc Cédric Le Goater <[email protected]>
---
 hw/vfio/region.c              | 14 ++++++++++++++
 include/hw/vfio/vfio-device.h | 10 ++++++++++
 2 files changed, 24 insertions(+)
hw/vfio/region.c +14 -0
index ab39d77574..9c3dc19ee1 100644
@@ -461,3 +461,17 @@ void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled)
     trace_vfio_region_mmaps_set_enabled(memory_region_name(region->mem),
                                         enabled);
 }
+
+int vfio_get_region_index_from_mr(MemoryRegion *mr)
+{
+    VFIORegion *region;
+
+    while (mr->container) {
+        if (mr->ops == &vfio_region_ops) {
+            region = mr->opaque;
+            return region->nr;
+        }
+        mr = mr->container;
+    }
+    return -1;
+}
include/hw/vfio/vfio-device.h +10 -0
index 828a31c006..596c7f5a10 100644
@@ -308,6 +308,16 @@ bool vfio_device_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_t
 
 int vfio_device_get_irq_info(VFIODevice *vbasedev, int index,
                                 struct vfio_irq_info *info);
+
+/**
+ * Return the region index associated with a given MemoryRegion. The index
+ * can be useful in retrieving region info.
+ *
+ * @mr: MemoryRegion to use
+ *
+ * Returns the region index or -1 on error.
+ */
+int vfio_get_region_index_from_mr(MemoryRegion *mr);
 #endif
 
 /* Returns 0 on success, or a negative errno. */
Signature · 1 line
2.50.1
#16 lore raw

PATCH v6 9/11 vfio/device: Add support for creating dmabuf from multiple ranges

In reply to Vivek Kasireddy

In order to create a dmabuf associated with a buffer that spans multiple ranges, 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 so that we can invoke the VFIO_DEVICE_FEATURE_DMA_BUF feature to create the dmabuf.

Also, instead of iterating over all QOM devices to find the VFIODevice associated with a memory region, introduce a helper to just use the vfio_device_list to lookup the VFIODevice.

Cc Alex Williamson <[email protected]>
Cc Cédric Le Goater <[email protected]>
---
 hw/vfio/device.c              | 81 +++++++++++++++++++++++++++++++++++
 include/hw/vfio/vfio-device.h | 14 ++++++
 2 files changed, 95 insertions(+)
hw/vfio/device.c +81 -0
index 973fc35b59..a7603600f3 100644
@@ -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/core/iommu.h"
@@ -644,3 +645,83 @@ static VFIODeviceIOOps vfio_device_io_ops_ioctl = {
     .region_read = vfio_device_io_region_read,
     .region_write = vfio_device_io_region_write,
 };
+
+static bool vfio_device_lookup(struct iovec *iov, VFIODevice **vbasedevp,
+                               Error **errp)
+{
+    VFIODevice *vbasedev;
+    RAMBlock *first_rb;
+    ram_addr_t offset;
+
+    first_rb = qemu_ram_block_from_host(iov[0].iov_base, false, &offset);
+    if (!first_rb) {
+        error_setg(errp, "Could not find first ramblock\n");
+        return false;
+    }
+
+    QLIST_FOREACH(vbasedev, &vfio_device_list, next) {
+        if (vbasedev->dev == first_rb->mr->dev) {
+            *vbasedevp = vbasedev;
+            return true;
+        }
+    }
+    error_setg(errp, "No VFIO device found to create dmabuf from\n");
+    return false;
+}
+
+bool vfio_device_create_dmabuf(struct iovec *iov, unsigned int iov_cnt,
+                               int *fd, Error **errp)
+{
+    g_autofree struct vfio_device_feature *feature = NULL;
+    struct vfio_device_feature_dma_buf *dma_buf;
+    RAMBlock *rb, *first_rb;
+    VFIODevice *vbasedev;
+    ram_addr_t offset;
+    int i, index;
+    size_t argsz;
+
+    argsz = sizeof(*feature) + sizeof (*dma_buf) +
+            sizeof(struct vfio_region_dma_range) * iov_cnt;
+    feature = g_malloc0(argsz);
+    *feature = (struct vfio_device_feature) {
+        .argsz = argsz,
+        .flags = VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_DMA_BUF,
+    };
+    dma_buf = (struct vfio_device_feature_dma_buf *)feature->data;
+
+    if (!vfio_device_lookup(iov, &vbasedev, errp)) {
+        return false;
+    }
+
+    for (i = 0; i < iov_cnt; i++) {
+        rb = qemu_ram_block_from_host(iov[i].iov_base, false, &offset);
+        if (i == 0) {
+            first_rb = rb;
+        }
+        if (!rb || rb != first_rb) {
+            error_setg(errp, "Cannot create dmabuf with different regions\n");
+            return false;
+        }
+
+        index = vfio_get_region_index_from_mr(rb->mr);
+        if (index < 0) {
+            error_setg(errp, "Cannot find region index for dmabuf segment\n");
+            return false;
+        }
+
+        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;
+
+    *fd = vfio_device_get_feature(vbasedev, feature);
+    if (*fd < 0) {
+        error_setg_errno(errp, -(*fd),
+                         "Could not create dmabuf fd via VFIO device");
+        return false;
+    }
+    return true;
+}
include/hw/vfio/vfio-device.h +14 -0
index 596c7f5a10..003bac10b5 100644
@@ -318,6 +318,20 @@ int vfio_device_get_irq_info(VFIODevice *vbasedev, int index,
  * Returns the region index or -1 on error.
  */
 int vfio_get_region_index_from_mr(MemoryRegion *mr);
+
+/**
+ * Create 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.
+ *
+ * @iov: array of iovec entries associated with a buffer
+ * @iov_cnt: number of entries in the iovec array
+ * @fd: pointer to store the resulting dmabuf fd
+ *
+ * Returns true on success and false in case of an error.
+ */
+bool vfio_device_create_dmabuf(struct iovec *iov, unsigned int iov_cnt,
+                               int *fd, Error **errp);
 #endif
 
 /* Returns 0 on success, or a negative errno. */
Signature · 1 line
2.50.1
#17 lore raw

PATCH v6 10/11 vfio/device: Add a helper to mmap a dmabuf

In reply to Vivek Kasireddy

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]>
---
 hw/vfio/device.c              | 65 +++++++++++++++++++++++++++++++++++
 include/hw/vfio/vfio-device.h | 16 +++++++++
 2 files changed, 81 insertions(+)
hw/vfio/device.c +65 -0
index a7603600f3..ef05ec8ade 100644
@@ -725,3 +725,68 @@ bool vfio_device_create_dmabuf(struct iovec *iov, unsigned int iov_cnt,
     }
     return true;
 }
+
+bool vfio_device_mmap_dmabuf(struct iovec *iov, unsigned int iov_cnt,
+                             void **addr, size_t total_size, Error **errp)
+{
+    struct vfio_region_info *info = NULL;
+    VFIODevice *vbasedev = NULL;
+    ram_addr_t offset, len = 0;
+    RAMBlock *first_rb, *rb;
+    void *map, *submap;
+    int i, index;
+
+    if (!vfio_device_lookup(iov, &vbasedev, errp)) {
+        return false;
+    }
+
+    /*
+     * 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 false;
+    }
+
+    for (i = 0; i < iov_cnt; i++) {
+        rb = qemu_ram_block_from_host(iov[i].iov_base, false, &offset);
+        if (i == 0) {
+            first_rb = rb;
+        }
+        if (!rb || rb != first_rb) {
+            error_setg(errp, "Cannot mmap dmabuf with different regions\n");
+            goto err;
+        }
+
+        index = vfio_get_region_index_from_mr(rb->mr);
+        if (index < 0) {
+            error_setg(errp, "Cannot find region index for dmabuf segment\n");
+            goto err;
+        }
+
+        if (!vfio_device_get_region_info(vbasedev, index, &info)) {
+            error_setg(errp, "Cannot find region info for dmabuf segment\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(errp, "Could not mmap dmabuf segment\n");
+            goto err;
+        }
+
+        len += iov[i].iov_len;
+    }
+    *addr = map;
+    return true;
+err:
+    munmap(map, total_size);
+    error_prepend(errp, "VFIO dmabuf mmap failed: ");
+    return false;
+}
+
include/hw/vfio/vfio-device.h +16 -0
index 003bac10b5..b8e0801b94 100644
@@ -332,6 +332,22 @@ int vfio_get_region_index_from_mr(MemoryRegion *mr);
  */
 bool vfio_device_create_dmabuf(struct iovec *iov, unsigned int iov_cnt,
                                int *fd, 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
+ * @addr: pointer to store the resulting mmap address
+ * @total_size: total size of the dmabuf
+ *
+ * Returns true on success and false in case of an error.
+ */
+bool vfio_device_mmap_dmabuf(struct iovec *iov, unsigned int iov_cnt,
+                             void **addr, size_t total_size, Error **errp);
 #endif
 
 /* Returns 0 on success, or a negative errno. */
Signature · 1 line
2.50.1
#18 lore raw

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

In reply to Vivek Kasireddy

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 <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Cédric Le Goater <[email protected]>
---
 hw/display/Kconfig             |  5 ++++
 hw/display/virtio-gpu-dmabuf.c | 50 ++++++++++++++++++++++++++++------
 2 files changed, 47 insertions(+), 8 deletions(-)
hw/display/Kconfig +5 -0
index 1e95ab28ef..0d090f25f5 100644
@@ -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
hw/display/virtio-gpu-dmabuf.c +42 -8

87 lines not on this page · raw message

Signature · 1 line
2.50.1
#19 lore raw

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

Quoted 117 lines · Vivek Kasireddy

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 <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Cédric Le Goater <[email protected]>
---
  hw/display/Kconfig             |  5 ++++
  hw/display/virtio-gpu-dmabuf.c | 50 ++++++++++++++++++++++++++++------
  2 files changed, 47 insertions(+), 8 deletions(-)
hw/display/Kconfig +5 -0
index 1e95ab28ef..0d090f25f5 100644
@@ -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
hw/display/virtio-gpu-dmabuf.c +37 -3
index d9b2ecaf31..54d010b995 100644
@@ -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)

hw/display/virtio-gpu-dmabuf.c void virtio_gpu_init_dmabuf(struct virtio_gpu_simple_resource *res) · line 181

Quoted 3 lines
hw/display/virtio-gpu-dmabuf.c +0 -0
@@ -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

Quoted 20 lines
hw/display/virtio-gpu-dmabuf.c +5 -5
-            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)
#20 lore raw

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

Hi Akihiko,

Quoted 129 lines

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

Quoted 23 lines · Vivek Kasireddy

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 <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Cédric Le Goater <[email protected]>
Quoted 3 lines
---
  hw/display/Kconfig             |  5 ++++
  hw/display/virtio-gpu-dmabuf.c | 50

++++++++++++++++++++++++++++------

hw/display/virtio-gpu-

Quoted 19 lines
  2 files changed, 47 insertions(+), 8 deletions(-)
hw/display/Kconfig +5 -0
index 1e95ab28ef..0d090f25f5 100644
@@ -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
hw/display/virtio-gpu- renamed from hw/display/virtio-gpu-dmabuf.c +0 -0

dmabuf.c

Quoted 12 lines

index d9b2ecaf31..54d010b995 100644

hw/display/virtio-gpu-dmabuf.c +1 -0
@@ -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"

hw/display/virtio-gpu-dmabuf.c line 31

Quoted 4 lines
hw/display/virtio-gpu-dmabuf.c +1 -0
@@ -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,

hw/display/virtio-gpu-dmabuf.c

Quoted 8 lines
hw/display/virtio-gpu-dmabuf.c +8 -0
+                                          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))

{

hw/display/virtio-gpu-dmabuf.c

Quoted 13 lines
hw/display/virtio-gpu-dmabuf.c +12 -0
+        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,

hw/display/virtio-gpu-dmabuf.c static bool virtio_gpu_create_udmabuf(struct

Quoted 3 lines
hw/display/virtio-gpu-dmabuf.c +0 -0
                                        Error **errp)
  {
@@ -73,16 +95,28 @@ static bool virtio_gpu_create_udmabuf(struct

virtio_gpu_simple_resource *res,

hw/display/virtio-gpu-dmabuf.c static bool virtio_gpu_create_udmabuf(struct · line 95

Quoted 1 line
hw/display/virtio-gpu-dmabuf.c +0 -0
  static bool virtio_gpu_remap_dmabuf(struct

virtio_gpu_simple_resource *res,

hw/display/virtio-gpu-dmabuf.c static bool virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res,

Quoted 7 lines
hw/display/virtio-gpu-dmabuf.c +2 -0
                                      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);

Quoted 13 lines
hw/display/virtio-gpu-dmabuf.c +9 -2
      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;

hw/display/virtio-gpu-dmabuf.c static bool virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res,

Quoted 10 lines
hw/display/virtio-gpu-dmabuf.c +4 -1
+        }
+#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)

Quoted 8 lines

hw/display/virtio-gpu-dmabuf.c void virtio_gpu_init_dmabuf(struct

Quoted 1 line
hw/display/virtio-gpu-dmabuf.c +0 -0
@@ -145,8 +179,10 @@ void virtio_gpu_init_dmabuf(struct

virtio_gpu_simple_resource *res)

hw/display/virtio-gpu-dmabuf.c void virtio_gpu_init_dmabuf(struct virtio_gpu_simple_resource *res) · line 181

Quoted 3 lines
hw/display/virtio-gpu-dmabuf.c +0 -0
          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.

AFAICS, it won't log a failure here because rb->fd is valid in the VFIO case especially after 8cfaf22668 ("Create dmabuf for PCI BAR per region"). The failure would occur in UDMABUF_CREATE_LIST IOCTL most likely because rb->fd is not memfd in this case.

Quoted 9 lines

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;

I think this is mostly how I have it right now except that I am using error_report_err() instead of qemu_log_mask(). Do you want me to only use qemu_log_mask() when we cannot create a dmabuf via udmabuf and vfio?
As you can see, I am using a mix of error_report_err() and qemu_log_mask() after Cedric suggested to improve error handling by using 'Error *'.

On a slightly different note, do you (or Cedric/anyone) know how to
properly include CONFIG_DEVICES header to fix the following error:
../hw/display/virtio-gpu-dmabuf.c: In function 'virtio_gpu_create_vfio_dmabuf':
../hw/display/virtio-gpu-dmabuf.c:35:13: error: attempt to use poisoned 'CONFIG_VIRTIO_GPU_VFIO_BLOB'
35 | #if defined(CONFIG_VIRTIO_GPU_VFIO_BLOB)

Looks like I am missing some key detail as I have tried different things but have not been able to fix this compilation error.

Thanks,
Vivek

Quoted 28 lines
     }
}

Regards,
Akihiko Odaki

hw/display/virtio-gpu-dmabuf.c void virtio_gpu_init_dmabuf(struct

Quoted 10 lines
hw/display/virtio-gpu-dmabuf.c +4 -2
-            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)

hw/display/virtio-gpu-dmabuf.c void virtio_gpu_init_dmabuf(struct virtio_gpu_simple_resource *res)

Quoted 10 lines
hw/display/virtio-gpu-dmabuf.c +1 -3
  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)

#21 lore raw

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

Quoted 137 lines · Vivek

Hi Akihiko,

Quoted 135 lines

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

Quoted 23 lines · Vivek Kasireddy

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 <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Cédric Le Goater <[email protected]>
Quoted 3 lines
---
   hw/display/Kconfig             |  5 ++++
   hw/display/virtio-gpu-dmabuf.c | 50

++++++++++++++++++++++++++++------

hw/display/virtio-gpu-

Quoted 19 lines
   2 files changed, 47 insertions(+), 8 deletions(-)
hw/display/Kconfig +5 -0
index 1e95ab28ef..0d090f25f5 100644
@@ -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
hw/display/virtio-gpu- renamed from hw/display/virtio-gpu-dmabuf.c +0 -0

dmabuf.c

Quoted 12 lines

index d9b2ecaf31..54d010b995 100644

hw/display/virtio-gpu-dmabuf.c +1 -0
@@ -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"

hw/display/virtio-gpu-dmabuf.c line 31

Quoted 4 lines
hw/display/virtio-gpu-dmabuf.c +1 -0
@@ -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,

hw/display/virtio-gpu-dmabuf.c

Quoted 8 lines
hw/display/virtio-gpu-dmabuf.c +8 -0
+                                          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))

{

hw/display/virtio-gpu-dmabuf.c

Quoted 13 lines
hw/display/virtio-gpu-dmabuf.c +12 -0
+        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,

hw/display/virtio-gpu-dmabuf.c static bool virtio_gpu_create_udmabuf(struct

Quoted 3 lines
hw/display/virtio-gpu-dmabuf.c +0 -0
                                         Error **errp)
   {
@@ -73,16 +95,28 @@ static bool virtio_gpu_create_udmabuf(struct

virtio_gpu_simple_resource *res,

hw/display/virtio-gpu-dmabuf.c static bool virtio_gpu_create_udmabuf(struct · line 95

Quoted 1 line
hw/display/virtio-gpu-dmabuf.c +0 -0
   static bool virtio_gpu_remap_dmabuf(struct

virtio_gpu_simple_resource *res,

hw/display/virtio-gpu-dmabuf.c static bool virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res,

Quoted 7 lines
hw/display/virtio-gpu-dmabuf.c +2 -0
                                       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);

Quoted 13 lines
hw/display/virtio-gpu-dmabuf.c +9 -2
       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;

hw/display/virtio-gpu-dmabuf.c static bool virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res,

Quoted 10 lines
hw/display/virtio-gpu-dmabuf.c +4 -1
+        }
+#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)

hw/display/virtio-gpu-dmabuf.c void virtio_gpu_init_dmabuf(struct

Quoted 1 line
hw/display/virtio-gpu-dmabuf.c +0 -0
@@ -145,8 +179,10 @@ void virtio_gpu_init_dmabuf(struct

virtio_gpu_simple_resource *res)

Quoted 3 lines
hw/display/virtio-gpu-dmabuf.c +0 -0
           pdata = res->iov[0].iov_base;
       } else {
           if (!virtio_gpu_create_udmabuf(res, &local_err)) {
Quoted 6 lines
Quoted 2 lines

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

AFAICS, it won't log a failure here because rb->fd is valid in the VFIO case especially after 8cfaf22668 ("Create dmabuf for PCI BAR per region"). The failure would occur in UDMABUF_CREATE_LIST IOCTL most likely because rb->fd is not memfd in this case.

It is still nice to ensure that the log will never be emitted in the VFIO case without relying on the internal behavior of the VFIO device code.

Quoted 10 lines
Quoted 10 lines

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;
Quoted 4 lines

I think this is mostly how I have it right now except that I am using error_report_err() instead of qemu_log_mask(). Do you want me to only use qemu_log_mask() when we cannot create a dmabuf via udmabuf and vfio?

More precisely, use LOG_GUEST_ERROR if and only if creating a DMA-BUF failed:
- in *both* virtio_gpu_create_udmabuf() and virtio_gpu_create_vfio_dmabuf()
- and it is due to a guest's fault.

Quoted 11 lines

As you can see, I am using a mix of error_report_err() and qemu_log_mask() after Cedric suggested to improve error handling by using 'Error *'.

On a slightly different note, do you (or Cedric/anyone) know how to
properly include CONFIG_DEVICES header to fix the following error:
../hw/display/virtio-gpu-dmabuf.c: In function 'virtio_gpu_create_vfio_dmabuf':
../hw/display/virtio-gpu-dmabuf.c:35:13: error: attempt to use poisoned 'CONFIG_VIRTIO_GPU_VFIO_BLOB'
35 | #if defined(CONFIG_VIRTIO_GPU_VFIO_BLOB)

Looks like I am missing some key detail as I have tried different things but have not been able to fix this compilation error.

I think you are supposed to add stubs. See hw/vfio/iommufd-stubs.c for example.

Regards,
Akihiko Odaki

#22 lore raw

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

Hi Akihiko,

Quoted 139 lines

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

Quoted 64 lines
Quoted 64 lines
Quoted 23 lines

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 <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Cédric Le Goater <[email protected]>
Quoted 3 lines
---
   hw/display/Kconfig             |  5 ++++
   hw/display/virtio-gpu-dmabuf.c | 50

++++++++++++++++++++++++++++------

hw/display/virtio-gpu-

Quoted 19 lines
   2 files changed, 47 insertions(+), 8 deletions(-)
hw/display/Kconfig +5 -0
index 1e95ab28ef..0d090f25f5 100644
@@ -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
hw/display/virtio-gpu- renamed from hw/display/virtio-gpu-dmabuf.c +0 -0

dmabuf.c

Quoted 12 lines

index d9b2ecaf31..54d010b995 100644

hw/display/virtio-gpu-dmabuf.c +1 -0
@@ -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"

hw/display/virtio-gpu-dmabuf.c line 31

Quoted 4 lines
hw/display/virtio-gpu-dmabuf.c +1 -0
@@ -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,

Quoted 8 lines
Quoted 8 lines

hw/display/virtio-gpu-dmabuf.c

Quoted 8 lines
hw/display/virtio-gpu-dmabuf.c +8 -0
+                                          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))

Quoted 15 lines
Quoted 15 lines

{

hw/display/virtio-gpu-dmabuf.c

Quoted 13 lines
hw/display/virtio-gpu-dmabuf.c +12 -0
+        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,

Quoted 3 lines
Quoted 3 lines

hw/display/virtio-gpu-dmabuf.c static bool

Quoted 3 lines
hw/display/virtio-gpu-dmabuf.c +0 -0
                                         Error **errp)
   {
@@ -73,16 +95,28 @@ static bool

virtio_gpu_create_udmabuf(struct

Quoted 37 lines
Quoted 37 lines

virtio_gpu_simple_resource *res,

hw/display/virtio-gpu-dmabuf.c static bool · line 95

Quoted 1 line
hw/display/virtio-gpu-dmabuf.c +0 -0
   static bool virtio_gpu_remap_dmabuf(struct

virtio_gpu_simple_resource *res,

hw/display/virtio-gpu-dmabuf.c static bool virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res,

Quoted 7 lines
hw/display/virtio-gpu-dmabuf.c +2 -0
                                       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);

Quoted 13 lines
hw/display/virtio-gpu-dmabuf.c +9 -2
       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;

hw/display/virtio-gpu-dmabuf.c static bool virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res,

Quoted 10 lines
hw/display/virtio-gpu-dmabuf.c +4 -1
+        }
+#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)

hw/display/virtio-gpu-dmabuf.c void virtio_gpu_init_dmabuf(struct

Quoted 1 line
hw/display/virtio-gpu-dmabuf.c +0 -0
@@ -145,8 +179,10 @@ void virtio_gpu_init_dmabuf(struct

virtio_gpu_simple_resource *res)

Quoted 7 lines
Quoted 6 lines

hw/display/virtio-gpu-dmabuf.c void virtio_gpu_init_dmabuf(struct virtio_gpu_simple_resource *res) · line 181

Quoted 3 lines
hw/display/virtio-gpu-dmabuf.c +0 -0
           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.

AFAICS, it won't log a failure here because rb->fd is valid in the VFIO

Quoted 8 lines

case

Quoted 3 lines

especially after 8cfaf22668 ("Create dmabuf for PCI BAR per region"). The failure would occur in UDMABUF_CREATE_LIST IOCTL most likely because rb->fd is not memfd in this case.

It is still nice to ensure that the log will never be emitted in the VFIO case without relying on the internal behavior of the VFIO device code.

Ok, got it.

Quoted 16 lines
Quoted 11 lines
Quoted 11 lines

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;
Quoted 4 lines

I think this is mostly how I have it right now except that I am using error_report_err() instead of qemu_log_mask(). Do you want me to only use qemu_log_mask() when we cannot create a dmabuf via udmabuf and vfio?

Quoted 5 lines

More precisely, use LOG_GUEST_ERROR if and only if creating a DMA- BUF
failed:
- in *both* virtio_gpu_create_udmabuf() and virtio_gpu_create_vfio_dmabuf()

Ok, but should we also set the 'Error *' when this failure occurs?

Quoted 1 line

- and it is due to a guest's fault.

I think the only case where we can consider Guest is at fault is invalid rb/addr (i.e, qemu_ram_block_from_host() returning NULL). Can we limit it to just this particular case because in all other failure cases (for example, UDMABUF_CREATE_LIST IOCTL failure), I don't see any obvious way to deduce whether Guest or Host is at fault?

Quoted 11 lines
Quoted 2 lines

As you can see, I am using a mix of error_report_err() and

qemu_log_mask()

Quoted 5 lines

after Cedric suggested to improve error handling by using 'Error *'.

On a slightly different note, do you (or Cedric/anyone) know how to properly include CONFIG_DEVICES header to fix the following error:
../hw/display/virtio-gpu-dmabuf.c: In function

'virtio_gpu_create_vfio_dmabuf':

Quoted 1 line

../hw/display/virtio-gpu-dmabuf.c:35:13: error: attempt to use

poisoned 'CONFIG_VIRTIO_GPU_VFIO_BLOB'

Quoted 8 lines
Quoted 3 lines

35 | #if defined(CONFIG_VIRTIO_GPU_VFIO_BLOB)

Looks like I am missing some key detail as I have tried different things

but

Quoted 1 line

have not been able to fix this compilation error.

I think you are supposed to add stubs. See hw/vfio/iommufd-stubs.c for example.

I have tried adding stubs following the example of:
cca621c782 ("intel_iommu_accel: Check for compatibility with IOMMUFD backed device when x-flts=on")
but it doesn't help. I still get the following errors:
In file included from ../hw/display/virtio-gpu-dmabuf.c:21:
/media/myvdi/build/git-libs-64/cedric_qemu/qemu/hw/display/virtio-gpu-vfio-dmabuf.h:14:10: error: '#include' expects '"FILENAME"' or '<FILENAME>'
14 | #include CONFIG_DEVICES

      |          ^~~~~~~~~~~~~~

/media/myvdi/build/git-libs-64/cedric_qemu/qemu/hw/display/virtio-gpu-vfio-dmabuf.h:16:8: error: attempt to use poisoned 'CONFIG_VIRTIO_GPU_VFIO_BLOB'
16 | #ifdef CONFIG_VIRTIO_GPU_VFIO_BLOB
| ^
In file included from /media/myvdi/build/git-libs-64/cedric_qemu/qemu/include/exec/poison.h:7,

                 from /media/myvdi/build/git-libs-64/cedric_qemu/qemu/include/qemu/osdep.h:38,
                 from ../hw/display/virtio-gpu-dmabuf.c:14:

./config-poison.h:233:20: note: poisoned here
233 | #pragma GCC poison CONFIG_VIRTIO_GPU_VFIO_BLOB

Thanks,
Vivek

Quoted 3 lines

Regards,
Akihiko Odaki

#23 lore raw

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

Quoted 172 lines · Vivek

Hi Akihiko,

Quoted 139 lines

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

Quoted 64 lines
Quoted 64 lines
Quoted 23 lines

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 <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Cédric Le Goater <[email protected]>
Quoted 3 lines
---
    hw/display/Kconfig             |  5 ++++
    hw/display/virtio-gpu-dmabuf.c | 50

++++++++++++++++++++++++++++------

hw/display/virtio-gpu-

Quoted 19 lines
    2 files changed, 47 insertions(+), 8 deletions(-)
hw/display/Kconfig +5 -0
index 1e95ab28ef..0d090f25f5 100644
@@ -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
hw/display/virtio-gpu- renamed from hw/display/virtio-gpu-dmabuf.c +0 -0

dmabuf.c

Quoted 12 lines

index d9b2ecaf31..54d010b995 100644

hw/display/virtio-gpu-dmabuf.c +1 -0
@@ -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"

hw/display/virtio-gpu-dmabuf.c line 31

Quoted 4 lines
hw/display/virtio-gpu-dmabuf.c +1 -0
@@ -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,

Quoted 8 lines
Quoted 8 lines

hw/display/virtio-gpu-dmabuf.c

Quoted 8 lines
hw/display/virtio-gpu-dmabuf.c +8 -0
+                                          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))

Quoted 15 lines
Quoted 15 lines

{

hw/display/virtio-gpu-dmabuf.c

Quoted 13 lines
hw/display/virtio-gpu-dmabuf.c +12 -0
+        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,

Quoted 3 lines
Quoted 3 lines

hw/display/virtio-gpu-dmabuf.c static bool

Quoted 3 lines
hw/display/virtio-gpu-dmabuf.c +0 -0
                                          Error **errp)
    {
@@ -73,16 +95,28 @@ static bool

virtio_gpu_create_udmabuf(struct

Quoted 37 lines
Quoted 37 lines

virtio_gpu_simple_resource *res,

hw/display/virtio-gpu-dmabuf.c static bool · line 95

Quoted 1 line
hw/display/virtio-gpu-dmabuf.c +0 -0
    static bool virtio_gpu_remap_dmabuf(struct

virtio_gpu_simple_resource *res,

hw/display/virtio-gpu-dmabuf.c static bool virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res,

Quoted 7 lines
hw/display/virtio-gpu-dmabuf.c +2 -0
                                        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);

Quoted 13 lines
hw/display/virtio-gpu-dmabuf.c +9 -2
        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;

hw/display/virtio-gpu-dmabuf.c static bool virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res,

Quoted 10 lines
hw/display/virtio-gpu-dmabuf.c +4 -1
+        }
+#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)

hw/display/virtio-gpu-dmabuf.c void virtio_gpu_init_dmabuf(struct

Quoted 1 line
hw/display/virtio-gpu-dmabuf.c +0 -0
@@ -145,8 +179,10 @@ void virtio_gpu_init_dmabuf(struct

virtio_gpu_simple_resource *res)

Quoted 7 lines
Quoted 6 lines

hw/display/virtio-gpu-dmabuf.c void virtio_gpu_init_dmabuf(struct virtio_gpu_simple_resource *res) · line 181

Quoted 3 lines
hw/display/virtio-gpu-dmabuf.c +0 -0
            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.

AFAICS, it won't log a failure here because rb->fd is valid in the VFIO

Quoted 8 lines

case

Quoted 3 lines

especially after 8cfaf22668 ("Create dmabuf for PCI BAR per region"). The failure would occur in UDMABUF_CREATE_LIST IOCTL most likely because rb->fd is not memfd in this case.

It is still nice to ensure that the log will never be emitted in the VFIO case without relying on the internal behavior of the VFIO device code.

Ok, got it.

Quoted 16 lines
Quoted 11 lines
Quoted 11 lines

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;
Quoted 4 lines

I think this is mostly how I have it right now except that I am using error_report_err() instead of qemu_log_mask(). Do you want me to only use qemu_log_mask() when we cannot create a dmabuf via udmabuf and vfio?

Quoted 5 lines

More precisely, use LOG_GUEST_ERROR if and only if creating a DMA- BUF
failed:
- in *both* virtio_gpu_create_udmabuf() and virtio_gpu_create_vfio_dmabuf()

Quoted 8 lines

Ok, but should we also set the 'Error *' when this failure occurs?

Quoted 1 line

- and it is due to a guest's fault.

I think the only case where we can consider Guest is at fault is invalid rb/addr (i.e, qemu_ram_block_from_host() returning NULL). Can we limit it to just this particular case because in all other failure cases (for example, UDMABUF_CREATE_LIST IOCTL failure), I don't see any obvious way to deduce whether Guest or Host is at fault?

We should assume that the guest is at fault. In general, when a feature is missing and the guest tries to use it (e.g., by accessing MMIO registers present only with the feature), there is a possibility that the user misconfigured the VM. But we just emit LOG_GUEST_ERROR for such cases.

Quoted 15 lines
Quoted 15 lines
Quoted 3 lines

As you can see, I am using a mix of error_report_err() and

qemu_log_mask()

Quoted 5 lines

after Cedric suggested to improve error handling by using 'Error *'.

On a slightly different note, do you (or Cedric/anyone) know how to properly include CONFIG_DEVICES header to fix the following error:
../hw/display/virtio-gpu-dmabuf.c: In function

'virtio_gpu_create_vfio_dmabuf':

Quoted 1 line

../hw/display/virtio-gpu-dmabuf.c:35:13: error: attempt to use

poisoned 'CONFIG_VIRTIO_GPU_VFIO_BLOB'

Quoted 3 lines

35 | #if defined(CONFIG_VIRTIO_GPU_VFIO_BLOB)

Looks like I am missing some key detail as I have tried different things

Quoted 8 lines
Quoted 5 lines

but

Quoted 1 line

have not been able to fix this compilation error.

I think you are supposed to add stubs. See hw/vfio/iommufd-stubs.c for example.

I have tried adding stubs following the example of:
cca621c782 ("intel_iommu_accel: Check for compatibility with IOMMUFD backed device when x-flts=on") but it doesn't help. I still get the following errors:

You are looking at the wrong example. It uses target-specific macros because intel_iommu_accel is specific to i386; virtio-gpu isn't. Refer to hw/vfio/iommufd-stubs.c, which I mentioned. For the user-side example, refer to migration/cpr.c.

Quoted 4 lines

In file included from ../hw/display/virtio-gpu-dmabuf.c:21:
/media/myvdi/build/git-libs-64/cedric_qemu/qemu/hw/display/virtio-gpu-vfio-dmabuf.h:14:10: error: '#include' expects '"FILENAME"' or '<FILENAME>'
14 | #include CONFIG_DEVICES
| ^~~~~~~~~~~~~~

CONFIG_DEVICES is a target-specific macro. Directly include the relevant header as migration/cpr.c includes hw/vfio/vfio-device.h.

Quoted 8 lines

/media/myvdi/build/git-libs-64/cedric_qemu/qemu/hw/display/virtio-gpu-vfio-dmabuf.h:16:8: error: attempt to use poisoned 'CONFIG_VIRTIO_GPU_VFIO_BLOB'
16 | #ifdef CONFIG_VIRTIO_GPU_VFIO_BLOB
| ^
In file included from /media/myvdi/build/git-libs-64/cedric_qemu/qemu/include/exec/poison.h:7,

                  from /media/myvdi/build/git-libs-64/cedric_qemu/qemu/include/qemu/osdep.h:38,
                  from ../hw/display/virtio-gpu-dmabuf.c:14:

./config-poison.h:233:20: note: poisoned here
233 | #pragma GCC poison CONFIG_VIRTIO_GPU_VFIO_BLOB

CONFIG_VIRTIO_GPU_VFIO_BLOB should be removed. It should always use the necessary symbols as migration/cpr.c always uses vmstate_cpr_vfio_devices.

Regards,
Akihiko Odaki

#24 lore raw

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

Hi Akihiko,

Quoted 244 lines

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

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

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

Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 14 lines

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 <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Cédric Le Goater <[email protected]>
Quoted 3 lines
---
    hw/display/Kconfig             |  5 ++++
    hw/display/virtio-gpu-dmabuf.c | 50

++++++++++++++++++++++++++++------

Quoted 10 lines
    2 files changed, 47 insertions(+), 8 deletions(-)
hw/display/Kconfig +0 -0
index 1e95ab28ef..0d090f25f5 100644
@@ -106,6 +106,11 @@ config VIRTIO_VGA
        depends on VIRTIO_PCI
        select VGA
 
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

hw/display/virtio-

Quoted 9 lines
hw/display/Kconfig +5 -0
+config VIRTIO_GPU_VFIO_BLOB
+    bool
+    default y
+    depends on VFIO
+
    config VHOST_USER_GPU
        bool
        default y
hw/display/virtio- renamed from hw/display/virtio-gpu-dmabuf.c +0 -0

gpu-

Quoted 48 lines
Quoted 48 lines
Quoted 18 lines
Quoted 18 lines

dmabuf.c

Quoted 12 lines

index d9b2ecaf31..54d010b995 100644

hw/display/virtio-gpu-dmabuf.c +1 -0
@@ -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"

hw/display/virtio-gpu-dmabuf.c line 31

Quoted 4 lines
hw/display/virtio-gpu-dmabuf.c +1 -0
@@ -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,

Quoted 8 lines
Quoted 8 lines

hw/display/virtio-gpu-dmabuf.c

Quoted 8 lines
hw/display/virtio-gpu-dmabuf.c +8 -0
+                                          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))

Quoted 15 lines
Quoted 15 lines

{

hw/display/virtio-gpu-dmabuf.c

Quoted 13 lines
hw/display/virtio-gpu-dmabuf.c +12 -0
+        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,

Quoted 3 lines
Quoted 3 lines

hw/display/virtio-gpu-dmabuf.c static bool

Quoted 3 lines
hw/display/virtio-gpu-dmabuf.c +0 -0
                                          Error **errp)
    {
@@ -73,16 +95,28 @@ static bool

virtio_gpu_create_udmabuf(struct

Quoted 2 lines
Quoted 2 lines

virtio_gpu_simple_resource *res,

Quoted 1 line
hw/display/virtio-gpu-dmabuf.c +0 -0
    static bool virtio_gpu_remap_dmabuf(struct
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

virtio_gpu_simple_resource *res,

Quoted 7 lines
hw/display/virtio-gpu-dmabuf.c +2 -0
                                        Error **errp)
    {
+    Error *local_err = NULL;
+    bool ret = true;
        void *map;
 
        map = mmap(NULL, res->blob_size, PROT_READ,

MAP_SHARED,

Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 25 lines

res->dmabuf_fd, 0);

Quoted 13 lines
hw/display/virtio-gpu-dmabuf.c +9 -2
        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;

hw/display/virtio-gpu-dmabuf.c static bool virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res,

Quoted 10 lines
hw/display/virtio-gpu-dmabuf.c +4 -1
+        }
+#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)

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

hw/display/virtio-gpu-dmabuf.c void virtio_gpu_init_dmabuf(struct

Quoted 1 line
hw/display/virtio-gpu-dmabuf.c +0 -0
@@ -145,8 +179,10 @@ void virtio_gpu_init_dmabuf(struct

virtio_gpu_simple_resource *res)

hw/display/virtio-gpu-dmabuf.c void virtio_gpu_init_dmabuf(struct virtio_gpu_simple_resource *res) · line 181

Quoted 3 lines
hw/display/virtio-gpu-dmabuf.c +0 -0
            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"

Quoted 33 lines
Quoted 10 lines
Quoted 2 lines
Quoted 1 line

even for VFIO, which shouldn't happen.

AFAICS, it won't log a failure here because rb->fd is valid in the VFIO

case

Quoted 3 lines

especially after 8cfaf22668 ("Create dmabuf for PCI BAR per region"). The failure would occur in UDMABUF_CREATE_LIST IOCTL most likely because rb->fd is not memfd in this case.

It is still nice to ensure that the log will never be emitted in the VFIO case without relying on the internal behavior of the VFIO device code.

Ok, got it.

Quoted 16 lines
Quoted 11 lines
Quoted 11 lines

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;
Quoted 4 lines

I think this is mostly how I have it right now except that I am using error_report_err() instead of qemu_log_mask(). Do you want me to only use qemu_log_mask() when we cannot create a dmabuf via udmabuf and vfio?

Quoted 5 lines

More precisely, use LOG_GUEST_ERROR if and only if creating a DMA- BUF
failed:
- in *both* virtio_gpu_create_udmabuf() and virtio_gpu_create_vfio_dmabuf()

Quoted 4 lines

Ok, but should we also set the 'Error *' when this failure occurs?

Quoted 1 line

- and it is due to a guest's fault.

I think the only case where we can consider Guest is at fault is invalid

rb/addr

Quoted 1 line

(i.e, qemu_ram_block_from_host() returning NULL). Can we limit it to

just this

Quoted 1 line

particular case because in all other failure cases (for example,

UDMABUF_CREATE_LIST

Quoted 1 line

IOCTL failure), I don't see any obvious way to deduce whether Guest or

Host

Quoted 1 line

is at fault?

We should assume that the guest is at fault. In general, when a feature is missing and the guest tries to use it (e.g., by accessing MMIO registers present only with the feature), there is a possibility that the user misconfigured the VM. But we just emit LOG_GUEST_ERROR for such
cases.

Quoted 11 lines
Quoted 11 lines
Quoted 3 lines

As you can see, I am using a mix of error_report_err() and

qemu_log_mask()

Quoted 5 lines

after Cedric suggested to improve error handling by using 'Error *'.

On a slightly different note, do you (or Cedric/anyone) know how to properly include CONFIG_DEVICES header to fix the following error:
../hw/display/virtio-gpu-dmabuf.c: In function

'virtio_gpu_create_vfio_dmabuf':

Quoted 1 line

../hw/display/virtio-gpu-dmabuf.c:35:13: error: attempt to use

Quoted 8 lines
Quoted 8 lines

poisoned 'CONFIG_VIRTIO_GPU_VFIO_BLOB'

Quoted 3 lines

35 | #if defined(CONFIG_VIRTIO_GPU_VFIO_BLOB)

Looks like I am missing some key detail as I have tried different things

but

Quoted 1 line

have not been able to fix this compilation error.

I think you are supposed to add stubs. See hw/vfio/iommufd-stubs.c

for

Quoted 3 lines
Quoted 1 line

example.

I have tried adding stubs following the example of:
cca621c782 ("intel_iommu_accel: Check for compatibility with

IOMMUFD backed device when x-flts=on")

Quoted 1 line

but it doesn't help. I still get the following errors:

You are looking at the wrong example. It uses target-specific macros because intel_iommu_accel is specific to i386; virtio-gpu isn't. Refer to hw/vfio/iommufd-stubs.c, which I mentioned. For the user-side example, refer to migration/cpr.c.

Quoted 2 lines

In file included from ../hw/display/virtio-gpu-dmabuf.c:21:
/media/myvdi/build/git-libs-64/cedric_qemu/qemu/hw/display/virtio-

gpu-vfio-dmabuf.h:14:10: error: '#include' expects '"FILENAME"' or '<FILENAME>'

Quoted 2 lines

14 | #include CONFIG_DEVICES
| ^~~~~~~~~~~~~~

CONFIG_DEVICES is a target-specific macro. Directly include the relevant header as migration/cpr.c includes hw/vfio/vfio-device.h.

Quoted 1 line

/media/myvdi/build/git-libs-64/cedric_qemu/qemu/hw/display/virtio-

gpu-vfio-dmabuf.h:16:8: error: attempt to use poisoned 'CONFIG_VIRTIO_GPU_VFIO_BLOB'

Quoted 3 lines

16 | #ifdef CONFIG_VIRTIO_GPU_VFIO_BLOB
| ^
In file included from /media/myvdi/build/git-libs-

64/cedric_qemu/qemu/include/exec/poison.h:7,

Quoted 1 line

from /media/myvdi/build/git-libs-

64/cedric_qemu/qemu/include/qemu/osdep.h:38,

Quoted 8 lines
Quoted 3 lines
                  from ../hw/display/virtio-gpu-dmabuf.c:14:

./config-poison.h:233:20: note: poisoned here
233 | #pragma GCC poison CONFIG_VIRTIO_GPU_VFIO_BLOB

CONFIG_VIRTIO_GPU_VFIO_BLOB should be removed. It should always use the
necessary symbols as migration/cpr.c always uses vmstate_cpr_vfio_devices.

Thank you very much for your suggestion. It now works as expected. I'll send v7 shortly that would address your other feedback.

Thanks,
Vivek

Quoted 3 lines

Regards,
Akihiko Odaki