Skip to contentSkip to conversation

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

PATCH Superseded Vivek Kasireddy 3 participants 24 messages Jan 23 – Jan 30 Arm/Amlogic Meson SoC support

v4 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

v4 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:
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 (8):
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
vfio: Document vfio_device_get_region_info()
vfio/region: Add a helper to get region index from memory region vfio/device: Add a helper to lookup VFIODevice from memory region vfio/device: Add support for creating dmabuf from multiple ranges
virtio-gpu: Rename udmabuf files and helpers to 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} | 127 ++++++++++++++++--
 hw/display/virtio-gpu.c                       |  31 ++++-
 hw/vfio/device.c                              |  56 ++++++++
 hw/vfio/region.c                              |  14 ++
 include/hw/vfio/vfio-device.h                 |  46 +++++++
 include/hw/virtio/virtio-gpu.h                |   6 +-
 9 files changed, 266 insertions(+), 27 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} (62%)
Signature · 1 line
2.50.1
#1 lore raw

PATCH v4 1/8 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 v4 2/8 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 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().

This is because the ram device's address space is not accessible to virtio-gpu directly and hence dma_memory_map() cannot be used. Therefore, we first need to identify the memory region associated with the DMA address and add the offset to the pointer returned from memory_region_get_ram_ptr() to obtain the host address.

Note that we take a reference on the memory region but we would still eventually call dma_memory_unmap() (to unref the mr).

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

Re: [PATCH v4 2/8] virtio-gpu: Find hva for Guest's DMA addr associated with a ram device

Quoted 7 lines · Vivek Kasireddy

If the Guest provides a DMA address that is associated with a ram device (such as a 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().

This is because the ram device's address space is not accessible to virtio-gpu directly and hence dma_memory_map() cannot be used.

This description is a bit confusing.

The first problem is with the phrase "the ram device's address space". The address space we are dealing with is always VIRTIO_DEVICE(g)->dma_as. More precisely it is a memory region, not address space.

The second problem is that it says the ram device's memory region is not directly accessible. It is usually true, but here we are directly accessing it anyway because the guest knows how to do so.

Regards,
Akihiko Odaki

Quoted 57 lines

Therefore, we first need to identify the memory region associated with the DMA address and add the offset to the pointer returned from memory_region_get_ram_ptr() to obtain the host address.

Note that we take a reference on the memory region but we would still eventually call dma_memory_unmap() (to unref the mr).

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);
#4 lore raw

RE: [PATCH v4 2/8] virtio-gpu: Find hva for Guest's DMA addr associated with a ram device

Hi Akihiko,

Quoted 9 lines

Subject: Re: [PATCH v4 2/8] virtio-gpu: Find hva for Guest's DMA addr associated with a ram device

Quoted 5 lines · Vivek Kasireddy

If the Guest provides a DMA address that is associated with a ram device (such as a 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().

Quoted 4 lines
Quoted 2 lines

This is because the ram device's address space is not accessible to virtio-gpu directly and hence dma_memory_map() cannot be used.

This description is a bit confusing.

I agree; the description does seem ambiguous.

Quoted 9 lines

The first problem is with the phrase "the ram device's address space". The address space we are dealing with is always VIRTIO_DEVICE(g)->dma_as. More precisely it is a memory region, not address space.

The second problem is that it says the ram device's memory region is not directly accessible. It is usually true, but here we are directly accessing it anyway because the guest knows how to do so.

Does this look better:
"virtio-gpu: Find hva for Guest's DMA addr associated with a ram device

If the Guest provides a DMA address that is associated with a ram device (such as a 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() in this case to get the hva because it can only be used with addresses that are associated with the system (or physical) memory region. Therefore, in order to handle addresses associated with ram 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."

Thanks,
Vivek

Quoted 65 lines

Regards,
Akihiko Odaki

hw/display/virtio-gpu.c static void

Quoted 23 lines

Therefore, we first need to identify the memory region associated with the DMA address and add the offset to the pointer returned from memory_region_get_ram_ptr() to obtain the host address.

Note that we take a reference on the memory region but we would still eventually call dma_memory_unmap() (to unref the mr).

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 +0 -0
index 3d58cebf4b..9930a21317 100644
@@ -798,6 +798,23 @@ static void

virtio_gpu_set_scanout_blob(VirtIOGPU *g,

hw/display/virtio-gpu.c static void virtio_gpu_set_scanout_blob(VirtIOGPU *g,

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

hw/display/virtio-gpu.c int virtio_gpu_create_mapping_iov(VirtIOGPU

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

hw/display/virtio-gpu.c int virtio_gpu_create_mapping_iov(VirtIOGPU *g,

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

Quoted 1 line
hw/display/virtio-gpu.c +0 -0
                                " element %d\n", __func__, e);
#5 lore raw

Re: [PATCH v4 2/8] virtio-gpu: Find hva for Guest's DMA addr associated with a ram device

Quoted 34 lines · Vivek

Hi Akihiko,

Quoted 9 lines

Subject: Re: [PATCH v4 2/8] virtio-gpu: Find hva for Guest's DMA addr associated with a ram device

Quoted 5 lines · Vivek Kasireddy

If the Guest provides a DMA address that is associated with a ram device (such as a 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().

Quoted 4 lines
Quoted 2 lines

This is because the ram device's address space is not accessible to virtio-gpu directly and hence dma_memory_map() cannot be used.

This description is a bit confusing.

I agree; the description does seem ambiguous.

Quoted 9 lines

The first problem is with the phrase "the ram device's address space". The address space we are dealing with is always VIRTIO_DEVICE(g)->dma_as. More precisely it is a memory region, not address space.

The second problem is that it says the ram device's memory region is not directly accessible. It is usually true, but here we are directly accessing it anyway because the guest knows how to do so.

Does this look better:
"virtio-gpu: Find hva for Guest's DMA addr associated with a ram device

If the Guest provides a DMA address that is associated with a ram device (such as a 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().

Quoted 8 lines

We cannot use dma_memory_map() in this case to get the hva because it can only be used with addresses that are associated with the system (or physical) memory region. Therefore, in order to handle addresses associated with ram 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.

Sorry but I don't think it's improved much. It still contains a phrase not well-established ("system (or physical) memory region").

The description of what the logic does is expanded, but it's not really useful; it says the logic is to add the offset and the retrieved pointer, but it's obvious if you read the code. So let's focus on "why" the logic is added instead of what the logic does.

Below is my analysis on "why":
First, let's stick to the phrase "direct access"; its meaning is established in include/system/memory.h.

With the concept of "direct access", the underlying problem being solved can be understood as follows: dma_memory_map() automatically determines what is directly accessible, and VFIO devices are considered not directly accessible because such accesses may be part of MMIO. But virtio-gpu is not doing MMIO, which leads to a disagreement with the function and virtio-gpu.

Regards,
Akihiko Odaki

#6 lore raw

RE: [PATCH v4 2/8] virtio-gpu: Find hva for Guest's DMA addr associated with a ram device

Hi Akihiko,

Quoted 47 lines

Subject: Re: [PATCH v4 2/8] virtio-gpu: Find hva for Guest's DMA addr associated with a ram device

Quoted 6 lines
Quoted 6 lines · Vivek Kasireddy
Quoted 4 lines

If the Guest provides a DMA address that is associated with a ram device (such as a 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().

Quoted 14 lines
Quoted 5 lines
Quoted 3 lines

This is because the ram device's address space is not accessible to virtio-gpu directly and hence dma_memory_map() cannot be used.

This description is a bit confusing.

I agree; the description does seem ambiguous.

Quoted 7 lines

The first problem is with the phrase "the ram device's address space". The address space we are dealing with is always VIRTIO_DEVICE(g)->dma_as. More precisely it is a memory region, not address space.

The second problem is that it says the ram device's memory region is

not

Quoted 4 lines
Quoted 2 lines

directly accessible. It is usually true, but here we are directly accessing it anyway because the guest knows how to do so.

Does this look better:
"virtio-gpu: Find hva for Guest's DMA addr associated with a ram

device

Quoted 5 lines

If the Guest provides a DMA address that is associated with a ram device (such as a 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().

Quoted 2 lines

We cannot use dma_memory_map() in this case to get the hva

because

Quoted 7 lines

it can only be used with addresses that are associated with the system (or physical) memory region. Therefore, in order to handle addresses associated with ram 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.

Quoted 2 lines

Sorry but I don't think it's improved much. It still contains a phrase not well-established ("system (or physical) memory region").

I think it is well-established given this line in include/system/memory.h:
/* address_space_map: map a physical memory region into a host virtual address and considering the fact that address_space_map() is called by dma_memory_map().

Quoted 10 lines

The description of what the logic does is expanded, but it's not really useful; it says the logic is to add the offset and the retrieved pointer, but it's obvious if you read the code. So let's focus on "why" the logic is added instead of what the logic does.

Below is my analysis on "why":
First, let's stick to the phrase "direct access"; its meaning is established in include/system/memory.h.

Quoted 7 lines

With the concept of "direct access", the underlying problem being solved can be understood as follows: dma_memory_map() automatically determines
what is directly accessible, and VFIO devices are considered not directly accessible because such accesses may be part of MMIO. But virtio-gpu is not doing MMIO, which leads to a disagreement with the function and virtio-gpu.

Here is another try:
"virtio-gpu: Find hva for Guest's DMA addr associated with a ram device

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() in this case to get the hva because it can only be used with addresses that are associated with memory regions that support direct access. Since VFIO device regions are not considered directly accessible (because they are mostly MMIO based), dma_memory_map() would fail to return the hva for such addresses when invoked by virtio-gpu.

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."

Thanks,
Vivek

Quoted 3 lines

Regards,
Akihiko Odaki

#7 lore raw

Re: [PATCH v4 2/8] virtio-gpu: Find hva for Guest's DMA addr associated with a ram device

Quoted 49 lines · Vivek

Hi Akihiko,

Quoted 47 lines

Subject: Re: [PATCH v4 2/8] virtio-gpu: Find hva for Guest's DMA addr associated with a ram device

Quoted 6 lines
Quoted 6 lines · Vivek Kasireddy
Quoted 4 lines

If the Guest provides a DMA address that is associated with a ram device (such as a 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().

Quoted 14 lines
Quoted 5 lines
Quoted 3 lines

This is because the ram device's address space is not accessible to virtio-gpu directly and hence dma_memory_map() cannot be used.

This description is a bit confusing.

I agree; the description does seem ambiguous.

Quoted 7 lines

The first problem is with the phrase "the ram device's address space". The address space we are dealing with is always VIRTIO_DEVICE(g)->dma_as. More precisely it is a memory region, not address space.

The second problem is that it says the ram device's memory region is

not

Quoted 4 lines
Quoted 2 lines

directly accessible. It is usually true, but here we are directly accessing it anyway because the guest knows how to do so.

Does this look better:
"virtio-gpu: Find hva for Guest's DMA addr associated with a ram

device

Quoted 5 lines

If the Guest provides a DMA address that is associated with a ram device (such as a 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().

Quoted 2 lines

We cannot use dma_memory_map() in this case to get the hva

because

Quoted 7 lines

it can only be used with addresses that are associated with the system (or physical) memory region. Therefore, in order to handle addresses associated with ram 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.

Quoted 6 lines
Quoted 2 lines

Sorry but I don't think it's improved much. It still contains a phrase not well-established ("system (or physical) memory region").

I think it is well-established given this line in include/system/memory.h:
/* address_space_map: map a physical memory region into a host virtual address and considering the fact that address_space_map() is called by dma_memory_map().

I guess not that particular phrase is wrong but the expression "it can only be used with addresses that are associated with the system (or physical) memory region" is not quite precise as whole.

address_space_map() or dma_memory_map() can actually be used with any memory region. It however creates a bounce buffer if not directly accessible.

Quoted 40 lines
Quoted 11 lines

The description of what the logic does is expanded, but it's not really useful; it says the logic is to add the offset and the retrieved pointer, but it's obvious if you read the code. So let's focus on "why" the logic is added instead of what the logic does.

Below is my analysis on "why":
First, let's stick to the phrase "direct access"; its meaning is established in include/system/memory.h.

Quoted 7 lines

With the concept of "direct access", the underlying problem being solved can be understood as follows: dma_memory_map() automatically determines
what is directly accessible, and VFIO devices are considered not directly accessible because such accesses may be part of MMIO. But virtio-gpu is not doing MMIO, which leads to a disagreement with the function and virtio-gpu.

Here is another try:
"virtio-gpu: Find hva for Guest's DMA addr associated with a ram device

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() in this case to get the hva because it can only be used with addresses that are associated with memory regions that support direct access. Since VFIO device regions are not considered directly accessible (because they are mostly MMIO based), dma_memory_map() would fail to return the hva for such addresses when invoked by virtio-gpu.

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.

Quoted 3 lines

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."

This is better. Can you also address the inaccuracy of the statement "We cannot use dma_memory_map()..." which I noted above?

Regards,
Akihiko Odaki

#8 lore raw

RE: [PATCH v4 2/8] virtio-gpu: Find hva for Guest's DMA addr associated with a ram device

Hi Akihiko,

Quoted 110 lines

Subject: Re: [PATCH v4 2/8] virtio-gpu: Find hva for Guest's DMA addr associated with a ram device

Quoted 9 lines
Quoted 9 lines
Quoted 5 lines
Quoted 5 lines · Vivek Kasireddy
Quoted 4 lines

If the Guest provides a DMA address that is associated with a ram device (such as a 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().

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

This is because the ram device's address space is not accessible to virtio-gpu directly and hence dma_memory_map() cannot be

used.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

This description is a bit confusing.

I agree; the description does seem ambiguous.

Quoted 2 lines

The first problem is with the phrase "the ram device's address

space".

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

The address space we are dealing with is always VIRTIO_DEVICE(g)->dma_as. More precisely it is a memory region,

not

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

address space.

The second problem is that it says the ram device's memory region is

not

Quoted 4 lines
Quoted 2 lines

directly accessible. It is usually true, but here we are directly accessing it anyway because the guest knows how to do so.

Does this look better:
"virtio-gpu: Find hva for Guest's DMA addr associated with a ram

device

Quoted 5 lines

If the Guest provides a DMA address that is associated with a ram device (such as a 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().

Quoted 2 lines

We cannot use dma_memory_map() in this case to get the hva

because

Quoted 7 lines

it can only be used with addresses that are associated with the system (or physical) memory region. Therefore, in order to handle addresses associated with ram 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.

Quoted 4 lines
Quoted 2 lines

Sorry but I don't think it's improved much. It still contains a phrase not well-established ("system (or physical) memory region").

I think it is well-established given this line in include/system/memory.h:
/* address_space_map: map a physical memory region into a host

virtual address

Quoted 2 lines

and considering the fact that address_space_map() is called by dma_memory_map().

I guess not that particular phrase is wrong but the expression "it can only be used with addresses that are associated with the system (or physical) memory region" is not quite precise as whole.

address_space_map() or dma_memory_map() can actually be used with any
memory region. It however creates a bounce buffer if not directly accessible.

Quoted 7 lines
Quoted 7 lines

The description of what the logic does is expanded, but it's not really useful; it says the logic is to add the offset and the retrieved pointer, but it's obvious if you read the code. So let's focus on "why" the logic is added instead of what the logic does.

Quoted 5 lines
Quoted 5 lines

Below is my analysis on "why":
First, let's stick to the phrase "direct access"; its meaning is established in include/system/memory.h.

With the concept of "direct access", the underlying problem being

solved

Quoted 8 lines
Quoted 6 lines

can be understood as follows: dma_memory_map() automatically determines
what is directly accessible, and VFIO devices are considered not directly accessible because such accesses may be part of MMIO. But virtio-gpu is not doing MMIO, which leads to a disagreement with the function and virtio-gpu.

Here is another try:
"virtio-gpu: Find hva for Guest's DMA addr associated with a ram

device

Quoted 5 lines

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().

Quoted 2 lines

We cannot use dma_memory_map() in this case to get the hva

because

Quoted 3 lines

it can only be used with addresses that are associated with memory regions that support direct access. Since VFIO device regions are not considered directly accessible (because they are mostly MMIO

based),

Quoted 10 lines

dma_memory_map() would fail to return the hva for such addresses when invoked by virtio-gpu.

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.

Quoted 6 lines
Quoted 3 lines

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."

This is better. Can you also address the inaccuracy of the statement "We cannot use dma_memory_map()..." which I noted above?

Just to confirm if my rewrite matches with what you have in mind, here is how I am planning to replace the 2nd paragraph that starts with "We cannot use dma_memory_map()" in the next version:
So, instead of
"We cannot use dma_memory_map() in this case to get the hva because
it can only be used with addresses that are associated with memory regions that support direct access. Since VFIO device regions are not considered directly accessible (because they are mostly MMIO based), dma_memory_map() would fail to return the hva for such addresses when invoked by virtio-gpu."

I am going to replace it with
"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."

Thanks,
Vivek

Quoted 3 lines

Regards,
Akihiko Odaki

#9 lore raw

Re: [PATCH v4 2/8] virtio-gpu: Find hva for Guest's DMA addr associated with a ram device

Quoted 129 lines · Vivek

Hi Akihiko,

Quoted 110 lines

Subject: Re: [PATCH v4 2/8] virtio-gpu: Find hva for Guest's DMA addr associated with a ram device

Quoted 9 lines
Quoted 9 lines
Quoted 5 lines
Quoted 5 lines · Vivek Kasireddy
Quoted 4 lines

If the Guest provides a DMA address that is associated with a ram device (such as a 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().

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

This is because the ram device's address space is not accessible to virtio-gpu directly and hence dma_memory_map() cannot be

used.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

This description is a bit confusing.

I agree; the description does seem ambiguous.

Quoted 2 lines

The first problem is with the phrase "the ram device's address

space".

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

The address space we are dealing with is always VIRTIO_DEVICE(g)->dma_as. More precisely it is a memory region,

not

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

address space.

The second problem is that it says the ram device's memory region is

not

Quoted 4 lines
Quoted 2 lines

directly accessible. It is usually true, but here we are directly accessing it anyway because the guest knows how to do so.

Does this look better:
"virtio-gpu: Find hva for Guest's DMA addr associated with a ram

device

Quoted 5 lines

If the Guest provides a DMA address that is associated with a ram device (such as a 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().

Quoted 2 lines

We cannot use dma_memory_map() in this case to get the hva

because

Quoted 7 lines

it can only be used with addresses that are associated with the system (or physical) memory region. Therefore, in order to handle addresses associated with ram 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.

Quoted 4 lines
Quoted 2 lines

Sorry but I don't think it's improved much. It still contains a phrase not well-established ("system (or physical) memory region").

I think it is well-established given this line in include/system/memory.h:
/* address_space_map: map a physical memory region into a host

virtual address

Quoted 2 lines

and considering the fact that address_space_map() is called by dma_memory_map().

I guess not that particular phrase is wrong but the expression "it can only be used with addresses that are associated with the system (or physical) memory region" is not quite precise as whole.

address_space_map() or dma_memory_map() can actually be used with any
memory region. It however creates a bounce buffer if not directly accessible.

Quoted 7 lines
Quoted 7 lines

The description of what the logic does is expanded, but it's not really useful; it says the logic is to add the offset and the retrieved pointer, but it's obvious if you read the code. So let's focus on "why" the logic is added instead of what the logic does.

Quoted 5 lines
Quoted 5 lines

Below is my analysis on "why":
First, let's stick to the phrase "direct access"; its meaning is established in include/system/memory.h.

With the concept of "direct access", the underlying problem being

solved

Quoted 8 lines
Quoted 6 lines

can be understood as follows: dma_memory_map() automatically determines
what is directly accessible, and VFIO devices are considered not directly accessible because such accesses may be part of MMIO. But virtio-gpu is not doing MMIO, which leads to a disagreement with the function and virtio-gpu.

Here is another try:
"virtio-gpu: Find hva for Guest's DMA addr associated with a ram

device

Quoted 5 lines

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().

Quoted 2 lines

We cannot use dma_memory_map() in this case to get the hva

because

Quoted 3 lines

it can only be used with addresses that are associated with memory regions that support direct access. Since VFIO device regions are not considered directly accessible (because they are mostly MMIO

based),

Quoted 10 lines

dma_memory_map() would fail to return the hva for such addresses when invoked by virtio-gpu.

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.

Quoted 6 lines
Quoted 3 lines

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."

This is better. Can you also address the inaccuracy of the statement "We cannot use dma_memory_map()..." which I noted above?

Just to confirm if my rewrite matches with what you have in mind, here is how I am planning to replace the 2nd paragraph that starts with "We cannot use dma_memory_map()" in the next version:
So, instead of
"We cannot use dma_memory_map() in this case to get the hva because
it can only be used with addresses that are associated with memory regions that support direct access. Since VFIO device regions are not considered directly accessible (because they are mostly MMIO based), dma_memory_map() would fail to return the hva for such addresses when invoked by virtio-gpu."

Quoted 7 lines

I am going to replace it with
"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."

That looks good to me. Thanks you for improving this.

Regards,
Akihiko Odaki

#10 lore raw

PATCH v4 3/8 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.

Cc Alex Williamson <[email protected]>
Cc Cédric Le Goater <[email protected]>
---
 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
Reviewed-by on v4 3/8
#11 lore raw

Re: [PATCH v4 3/8] vfio: Document vfio_device_get_region_info()

Quoted 24 lines · Vivek Kasireddy

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

Cc Alex Williamson <[email protected]>
Cc Cédric Le Goater <[email protected]>
---
  include/hw/vfio/vfio-device.h | 13 +++++++++++++
  1 file changed, 13 insertions(+)
include/hw/vfio/vfio-device.h +6 -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.

include/hw/vfio/vfio-device.h bool vfio_device_get_host_iommu_quirk_bypass_ro(VFIODevice *vbasedev, · line 293

Quoted 10 lines
include/hw/vfio/vfio-device.h +7 -0
+ *
+ * @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,

Thanks,

C.

#12 lore raw

PATCH v4 4/8 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..8943128dd2 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
#13 lore raw

PATCH v4 5/8 vfio/device: Add a helper to lookup VFIODevice from memory region

In reply to Vivek Kasireddy

Instead of iterating over all QOM devices to find the VFIODevice associated with a memory region, it is faster 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              | 12 ++++++++++++
 include/hw/vfio/vfio-device.h |  9 +++++++++
 2 files changed, 21 insertions(+)
hw/vfio/device.c +12 -0
index 973fc35b59..afeff0f034 100644
@@ -644,3 +644,15 @@ static VFIODeviceIOOps vfio_device_io_ops_ioctl = {
     .region_read = vfio_device_io_region_read,
     .region_write = vfio_device_io_region_write,
 };
+
+VFIODevice *vfio_device_lookup(MemoryRegion *mr)
+{
+    VFIODevice *vbasedev;
+
+    QLIST_FOREACH(vbasedev, &vfio_device_list, next) {
+        if (vbasedev->dev == mr->dev) {
+            return vbasedev;
+        }
+    }
+    return NULL;
+}
include/hw/vfio/vfio-device.h +9 -0
index 596c7f5a10..ccf2fd887c 100644
@@ -318,6 +318,15 @@ 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);
+
+/**
+ * Return the VFIO device associated with a given MemoryRegion.
+ *
+ * @mr: MemoryRegion to use
+ *
+ * Returns the VFIO device if found or NULL.
+ */
+VFIODevice *vfio_device_lookup(MemoryRegion *mr);
 #endif
 
 /* Returns 0 on success, or a negative errno. */
Signature · 1 line
2.50.1
#14 lore raw

PATCH v4 6/8 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.

Cc Alex Williamson <[email protected]>
Cc Cédric Le Goater <[email protected]>
---
 hw/vfio/device.c              | 44 +++++++++++++++++++++++++++++++++++
 include/hw/vfio/vfio-device.h | 14 +++++++++++
 2 files changed, 58 insertions(+)
hw/vfio/device.c +44 -0
index afeff0f034..90d7758d0d 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"
@@ -656,3 +657,46 @@ VFIODevice *vfio_device_lookup(MemoryRegion *mr)
     }
     return NULL;
 }
+
+int vfio_device_create_dmabuf_fd(VFIODevice *vbasedev,
+                                 struct iovec *iov, unsigned int iov_cnt)
+{
+    g_autofree struct vfio_device_feature *feature = NULL;
+    struct vfio_device_feature_dma_buf *dma_buf;
+    RAMBlock *rb, *first_rb = NULL;
+    ram_addr_t offset;
+    size_t argsz;
+    int i, index;
+
+    argsz = sizeof(*feature) + sizeof (*dma_buf) +
+            sizeof(struct vfio_region_dma_range) * iov_cnt;
+    feature = g_malloc0(argsz);
+    *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;
+
+    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) {
+            return -1;
+        }
+
+        index = vfio_get_region_index_from_mr(rb->mr);
+        if (index < 0) {
+            return -1;
+        }
+
+        dma_buf->region_index = index;
+        dma_buf->dma_ranges[i].offset = offset;
+        dma_buf->dma_ranges[i].length = iov[i].iov_len;
+    }
+
+    dma_buf->nr_ranges = iov_cnt;
+    dma_buf->open_flags = O_RDONLY | O_CLOEXEC;
+    return vfio_device_get_feature(vbasedev, feature);
+}
include/hw/vfio/vfio-device.h +14 -0
index ccf2fd887c..2f7e50f7ce 100644
@@ -327,6 +327,20 @@ int vfio_get_region_index_from_mr(MemoryRegion *mr);
  * Returns the VFIO device if found or NULL.
  */
 VFIODevice *vfio_device_lookup(MemoryRegion *mr);
+
+/**
+ * Create and return a dmabuf fd by first translating the addresses in the
+ * iovec array into VFIO region offsets and then invoking the
+ * VFIO_DEVICE_FEATURE_DMA_BUF feature.
+ *
+ * @vbasedev: #VFIODevice to use
+ * @iov: array of iovec entries associated with a buffer
+ * @iov_cnt: number of entries in the iovec array
+ *
+ * Returns the newly created dmabuf fd or -1 on error.
+ */
+int vfio_device_create_dmabuf_fd(VFIODevice *vbasedev,
+                                 struct iovec *iov, unsigned int iov_cnt);
 #endif
 
 /* Returns 0 on success, or a negative errno. */
Signature · 1 line
2.50.1
#15 lore raw

PATCH v4 7/8 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
#16 lore raw

PATCH v4 8/8 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 call vfio_create_dmabuf() which identifies the right VFIO device and eventually invokes the vfio_device_create_dmabuf_fd() API to have a dmabuf fd created.

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 use the VFIO device fd directly to create the CPU mapping.

While at it, remove the unnecessary rcu_read_lock/rcu_read_unlock from virtio_gpu_create_udmabuf() and also replace warn_report() with qemu_log_mask().

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 | 117 ++++++++++++++++++++++++++++++---
 2 files changed, 113 insertions(+), 9 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 +108 -9

164 lines not on this page · raw message

Signature · 1 line
2.50.1
#17 lore raw

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

Quoted 83 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 call vfio_create_dmabuf() which identifies the right VFIO device and eventually invokes the vfio_device_create_dmabuf_fd() API to have a dmabuf fd created.

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 use the VFIO device fd directly to create the CPU mapping.

While at it, remove the unnecessary rcu_read_lock/rcu_read_unlock from virtio_gpu_create_udmabuf() and also replace warn_report() with qemu_log_mask().

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 | 117 ++++++++++++++++++++++++++++++---
  2 files changed, 113 insertions(+), 9 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 +20 -0
index c34d4c85bc..2dfe70d7eb 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,38 @@
  #include "standard-headers/linux/udmabuf.h"
  #include "standard-headers/drm/drm_fourcc.h"
 
+static void vfio_create_dmabuf(struct virtio_gpu_simple_resource *res)
+{
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+    VFIODevice *vbasedev;
+    RAMBlock *first_rb;
+    ram_addr_t offset;
+
+    first_rb = qemu_ram_block_from_host(res->iov[0].iov_base, false, &offset);
+    if (!first_rb) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: Could not find ramblock\n", __func__);
+        return;
+    }
+
+    vbasedev = vfio_device_lookup(first_rb->mr);
+    if (!vbasedev) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: No VFIO device found to create dmabuf from\n",
+                      __func__);

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

Quoted 10 lines
hw/display/virtio-gpu-dmabuf.c +10 -0
+        return;
+    }
+
+    res->dmabuf_fd = vfio_device_create_dmabuf_fd(vbasedev,
+                                                  res->iov, res->iov_cnt);
+    if (res->dmabuf_fd < 0) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: VFIO_DEVICE_FEATURE_DMA_BUF: %s\n",
+                      __func__, strerror(errno));
+    }

This API design of vfio_device_create_dmabuf_fd() has some room of improvements.

First, this function requires to pass VFIODevice * which is derived from res->iov[0]. Since it is getting res->iov[0] anyway, such a parameter is redundant. vfio_device_create_dmabuf_fd() can call vfio_device_lookup() itself instead.

The second problem is the error handling. The error may not be caused by VFIO_DEVICE_FEATURE_DMA_BUF but may be caused by the guest passing regions that do not belong to one VFIO device. The two cases should be distinguished for a correct message.

Quoted 19 lines
hw/display/virtio-gpu-dmabuf.c +3 -3
+#endif
+}
+
  static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res)
  {
      struct udmabuf_create_list *list;
@@ -43,10 +76,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;
@@ -62,17 +92,84 @@ static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res)
 

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

Quoted 10 lines
hw/display/virtio-gpu-dmabuf.c +3 -2
      res->dmabuf_fd = ioctl(udmabuf, UDMABUF_CREATE_LIST, list);
      if (res->dmabuf_fd < 0) {
-        warn_report("%s: UDMABUF_CREATE_LIST: %s", __func__,
-                    strerror(errno));
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: UDMABUF_CREATE_LIST: %s\n",
+                      __func__, strerror(errno));
      }
      g_free(list);
  }

These changes of virtio_gpu_create_udmabuf() are independent so should go into separate patches with explanation.

Quoted 17 lines
hw/display/virtio-gpu-dmabuf.c +16 -0
+static void *vfio_dmabuf_mmap(struct virtio_gpu_simple_resource *res)
+{
+    struct vfio_region_info *info = NULL;
+    VFIODevice *vbasedev = NULL;
+    ram_addr_t offset, len = 0;
+    RAMBlock *first_rb, *rb;
+    void *map, *submap;
+    int i, ret = -1;
+
+    first_rb = qemu_ram_block_from_host(res->iov[0].iov_base, false, &offset);
+    if (!first_rb) {
+        return MAP_FAILED;
+    }
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+    vbasedev = vfio_device_lookup(first_rb->mr);
+#endif

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

Quoted 10 lines
hw/display/virtio-gpu-dmabuf.c +10 -0
+    if (!vbasedev) {
+        return MAP_FAILED;
+    }
+
+    /*
+     * 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, res->blob_size, PROT_READ, MAP_SHARED, vbasedev->fd, 0);

It should have PROT_NONE instead of PROT_READ to prevent accidental accesses to the placeholder mapping. Likewise, -1 should be passed instead of vbasedev->fd so that the placeholder mapping won't result in a mapping to real device.

Quoted 6 lines
hw/display/virtio-gpu-dmabuf.c +6 -0
+    if (map == MAP_FAILED) {
+        return map;
+    }
+
+    for (i = 0; i < res->iov_cnt; i++) {
+        rb = qemu_ram_block_from_host(res->iov[i].iov_base, false, &offset);

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

Quoted 10 lines
hw/display/virtio-gpu-dmabuf.c +10 -0
+        if (rb != first_rb) {
+            goto err;
+        }
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+        ret = vfio_get_region_index_from_mr(rb->mr);
+        if (ret < 0) {
+            goto err;
+        }
+
+        ret = vfio_device_get_region_info(vbasedev, ret, &info);

This function can take rb->mr and call vfio_device_lookup() and vfio_get_region_index_from_mr() internally to simplify the API as similar can be done for vfio_device_create_dmabuf_fd().

Quoted 57 lines
hw/display/virtio-gpu-dmabuf.c +30 -4
+#endif
+        if (ret < 0 || !info) {
+            goto err;
+        }
+
+        submap = mmap(map + len, res->iov[i].iov_len, PROT_READ,
+                      MAP_SHARED | MAP_FIXED, vbasedev->fd,
+                      info->offset + offset);
+        if (submap == MAP_FAILED) {
+            goto err;
+        }
+
+        len += res->iov[i].iov_len;
+    }
+    return map;
+err:
+    munmap(map, res->blob_size);
+    return MAP_FAILED;
+}
+
  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);
      if (res->remapped == MAP_FAILED) {
+        res->remapped = vfio_dmabuf_mmap(res);
+        if (res->remapped != MAP_FAILED) {
+            return;
+        }
          warn_report("%s: dmabuf mmap failed: %s", __func__,
                      strerror(errno));
          res->remapped = NULL;
@@ -139,8 +236,12 @@ void virtio_gpu_init_dmabuf(struct virtio_gpu_simple_resource *res)
      } else {
          virtio_gpu_create_udmabuf(res);
          if (res->dmabuf_fd < 0) {
-            return;
+            vfio_create_dmabuf(res);
+            if (res->dmabuf_fd < 0) {
+                return;
+            }
          }
+
          virtio_gpu_remap_dmabuf(res);
          if (!res->remapped) {
              return;
@@ -153,9 +254,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)
#18 lore raw

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

Hi Akihiko,

Quoted 105 lines

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

Quoted 27 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 call vfio_create_dmabuf() which identifies the right VFIO device and eventually invokes the vfio_device_create_dmabuf_fd() API to have a dmabuf fd created.

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 use the VFIO device fd directly to create the CPU mapping.

While at it, remove the unnecessary rcu_read_lock/rcu_read_unlock from virtio_gpu_create_udmabuf() and also replace warn_report() with qemu_log_mask().

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 | 117

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

hw/display/virtio-gpu-

Quoted 19 lines
  2 files changed, 113 insertions(+), 9 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 c34d4c85bc..2dfe70d7eb 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,38 @@
  #include "standard-headers/linux/udmabuf.h"
  #include "standard-headers/drm/drm_fourcc.h"
 
+static void vfio_create_dmabuf(struct virtio_gpu_simple_resource

*res)

hw/display/virtio-gpu-dmabuf.c

Quoted 7 lines
hw/display/virtio-gpu-dmabuf.c +7 -0
+{
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+    VFIODevice *vbasedev;
+    RAMBlock *first_rb;
+    ram_addr_t offset;
+
+    first_rb = qemu_ram_block_from_host(res->iov[0].iov_base, false,

&offset);

Quoted 11 lines
hw/display/virtio-gpu-dmabuf.c +11 -0
+    if (!first_rb) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: Could not find ramblock\n", __func__);
+        return;
+    }
+
+    vbasedev = vfio_device_lookup(first_rb->mr);
+    if (!vbasedev) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: No VFIO device found to create dmabuf from\n",
+                      __func__);

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

Quoted 10 lines
hw/display/virtio-gpu-dmabuf.c +10 -0
+        return;
+    }
+
+    res->dmabuf_fd = vfio_device_create_dmabuf_fd(vbasedev,
+                                                  res->iov, res->iov_cnt);
+    if (res->dmabuf_fd < 0) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: VFIO_DEVICE_FEATURE_DMA_BUF: %s\n",
+                      __func__, strerror(errno));
+    }

This API design of vfio_device_create_dmabuf_fd() has some room of improvements.

Quoted 5 lines

First, this function requires to pass VFIODevice * which is derived from res->iov[0]. Since it is getting res->iov[0] anyway, such a parameter is redundant. vfio_device_create_dmabuf_fd() can call vfio_device_lookup()
itself instead.

Yeah, I agree. I'll move the call to lookup into vfio_device_create_dmabuf_fd().

Quoted 6 lines

The second problem is the error handling. The error may not be caused by VFIO_DEVICE_FEATURE_DMA_BUF but may be caused by the guest passing
regions that do not belong to one VFIO device. The two cases should be distinguished for a correct message.

Ok, I'll add separate error messages.

Quoted 35 lines

hw/display/virtio-gpu-dmabuf.c

Quoted 5 lines
hw/display/virtio-gpu-dmabuf.c +3 -0
+#endif
+}
+
  static void virtio_gpu_create_udmabuf(struct

virtio_gpu_simple_resource *res)

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

Quoted 3 lines
hw/display/virtio-gpu-dmabuf.c +0 -0
  {
      struct udmabuf_create_list *list;
@@ -43,10 +76,7 @@ static void virtio_gpu_create_udmabuf(struct

virtio_gpu_simple_resource *res)

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

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

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

Quoted 6 lines
hw/display/virtio-gpu-dmabuf.c +0 -2
-        rcu_read_unlock();
-
          if (!rb || rb->fd < 0) {
              g_free(list);
              return;
@@ -62,17 +92,84 @@ static void virtio_gpu_create_udmabuf(struct

virtio_gpu_simple_resource *res)

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

Quoted 11 lines
hw/display/virtio-gpu-dmabuf.c +3 -2
      res->dmabuf_fd = ioctl(udmabuf, UDMABUF_CREATE_LIST, list);
      if (res->dmabuf_fd < 0) {
-        warn_report("%s: UDMABUF_CREATE_LIST: %s", __func__,
-                    strerror(errno));
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: UDMABUF_CREATE_LIST: %s\n",
+                      __func__, strerror(errno));
      }
      g_free(list);
  }
Quoted 3 lines

These changes of virtio_gpu_create_udmabuf() are independent so should
go into separate patches with explanation.

I called these out in the commit message and included them as they were trivial but I guess I could move them into separate patches.

Quoted 31 lines

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

Quoted 3 lines
hw/display/virtio-gpu-dmabuf.c +1 -0
+static void *vfio_dmabuf_mmap(struct virtio_gpu_simple_resource

*res)

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

Quoted 9 lines
hw/display/virtio-gpu-dmabuf.c +9 -0
+{
+    struct vfio_region_info *info = NULL;
+    VFIODevice *vbasedev = NULL;
+    ram_addr_t offset, len = 0;
+    RAMBlock *first_rb, *rb;
+    void *map, *submap;
+    int i, ret = -1;
+
+    first_rb = qemu_ram_block_from_host(res->iov[0].iov_base, false,

&offset);

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

Quoted 13 lines
hw/display/virtio-gpu-dmabuf.c +13 -0
+    if (!first_rb) {
+        return MAP_FAILED;
+    }
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+    vbasedev = vfio_device_lookup(first_rb->mr);
+#endif
+    if (!vbasedev) {
+        return MAP_FAILED;
+    }
+
+    /*
+     * We first reserve a contiguous chunk of address space for the entire
+     * dmabuf, then replace it with smaller mappings that correspond to

the

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

Quoted 3 lines
hw/display/virtio-gpu-dmabuf.c +3 -0
+     * individual segments of the dmabuf.
+     */
+    map = mmap(NULL, res->blob_size, PROT_READ, MAP_SHARED,
Quoted 6 lines

vbasedev->fd, 0);

It should have PROT_NONE instead of PROT_READ to prevent accidental accesses to the placeholder mapping.
Likewise, -1 should be passed instead of vbasedev->fd so that the placeholder mapping won't result in a mapping to real device.

I am OK with both suggestions. However, I don't see any problem if the placeholder mapping is associated with the real device. In other words, what specific problem do you see if we keep vbasedev->fd instead of -1?

Quoted 8 lines

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

Quoted 7 lines
hw/display/virtio-gpu-dmabuf.c +6 -0
+    if (map == MAP_FAILED) {
+        return map;
+    }
+
+    for (i = 0; i < res->iov_cnt; i++) {
+        rb = qemu_ram_block_from_host(res->iov[i].iov_base, false,

&offset);

Quoted 14 lines

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

Quoted 10 lines
hw/display/virtio-gpu-dmabuf.c +10 -0
+        if (rb != first_rb) {
+            goto err;
+        }
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+        ret = vfio_get_region_index_from_mr(rb->mr);
+        if (ret < 0) {
+            goto err;
+        }
+
+        ret = vfio_device_get_region_info(vbasedev, ret, &info);

This function can take rb->mr and call vfio_device_lookup() and vfio_get_region_index_from_mr() internally to simplify the API as similar can be done for vfio_device_create_dmabuf_fd().

Right, but vfio_device_get_region_info() is an already existing API that is called from various places that I am trying to leverage. I could create another API to wrap the calls to vfio_get_region_index_from_mr() and vfio_device_get_region_info() but not sure if that makes sense given that it would only have one caller.

Thanks,
Vivek

Quoted 62 lines
Quoted 12 lines
hw/display/virtio-gpu-dmabuf.c +11 -0
+#endif
+        if (ret < 0 || !info) {
+            goto err;
+        }
+
+        submap = mmap(map + len, res->iov[i].iov_len, PROT_READ,
+                      MAP_SHARED | MAP_FIXED, vbasedev->fd,
+                      info->offset + offset);
+        if (submap == MAP_FAILED) {
+            goto err;
+        }

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

Quoted 10 lines
hw/display/virtio-gpu-dmabuf.c +9 -0
+
+        len += res->iov[i].iov_len;
+    }
+    return map;
+err:
+    munmap(map, res->blob_size);
+    return MAP_FAILED;
+}
+
  static void virtio_gpu_remap_dmabuf(struct

virtio_gpu_simple_resource *res)

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

Quoted 12 lines
hw/display/virtio-gpu-dmabuf.c +4 -0
  {
      res->remapped = mmap(NULL, res->blob_size, PROT_READ,
                           MAP_SHARED, res->dmabuf_fd, 0);
      if (res->remapped == MAP_FAILED) {
+        res->remapped = vfio_dmabuf_mmap(res);
+        if (res->remapped != MAP_FAILED) {
+            return;
+        }
          warn_report("%s: dmabuf mmap failed: %s", __func__,
                      strerror(errno));
          res->remapped = NULL;
@@ -139,8 +236,12 @@ void virtio_gpu_init_dmabuf(struct

virtio_gpu_simple_resource *res)

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

Quoted 14 lines
hw/display/virtio-gpu-dmabuf.c +5 -1
      } else {
          virtio_gpu_create_udmabuf(res);
          if (res->dmabuf_fd < 0) {
-            return;
+            vfio_create_dmabuf(res);
+            if (res->dmabuf_fd < 0) {
+                return;
+            }
          }
+
          virtio_gpu_remap_dmabuf(res);
          if (!res->remapped) {
              return;
@@ -153,9 +254,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)

#19 lore raw

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

Quoted 157 lines · Vivek

Hi Akihiko,

Quoted 105 lines

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

Quoted 27 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 call vfio_create_dmabuf() which identifies the right VFIO device and eventually invokes the vfio_device_create_dmabuf_fd() API to have a dmabuf fd created.

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 use the VFIO device fd directly to create the CPU mapping.

While at it, remove the unnecessary rcu_read_lock/rcu_read_unlock from virtio_gpu_create_udmabuf() and also replace warn_report() with qemu_log_mask().

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 | 117

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

hw/display/virtio-gpu-

Quoted 19 lines
   2 files changed, 113 insertions(+), 9 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 c34d4c85bc..2dfe70d7eb 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,38 @@
   #include "standard-headers/linux/udmabuf.h"
   #include "standard-headers/drm/drm_fourcc.h"
 
+static void vfio_create_dmabuf(struct virtio_gpu_simple_resource

*res)

hw/display/virtio-gpu-dmabuf.c

Quoted 7 lines
hw/display/virtio-gpu-dmabuf.c +7 -0
+{
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+    VFIODevice *vbasedev;
+    RAMBlock *first_rb;
+    ram_addr_t offset;
+
+    first_rb = qemu_ram_block_from_host(res->iov[0].iov_base, false,

&offset);

Quoted 11 lines
hw/display/virtio-gpu-dmabuf.c +11 -0
+    if (!first_rb) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: Could not find ramblock\n", __func__);
+        return;
+    }
+
+    vbasedev = vfio_device_lookup(first_rb->mr);
+    if (!vbasedev) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: No VFIO device found to create dmabuf from\n",
+                      __func__);

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

Quoted 10 lines
hw/display/virtio-gpu-dmabuf.c +10 -0
+        return;
+    }
+
+    res->dmabuf_fd = vfio_device_create_dmabuf_fd(vbasedev,
+                                                  res->iov, res->iov_cnt);
+    if (res->dmabuf_fd < 0) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: VFIO_DEVICE_FEATURE_DMA_BUF: %s\n",
+                      __func__, strerror(errno));
+    }

This API design of vfio_device_create_dmabuf_fd() has some room of improvements.

Quoted 5 lines

First, this function requires to pass VFIODevice * which is derived from res->iov[0]. Since it is getting res->iov[0] anyway, such a parameter is redundant. vfio_device_create_dmabuf_fd() can call vfio_device_lookup()
itself instead.

Yeah, I agree. I'll move the call to lookup into vfio_device_create_dmabuf_fd().

Quoted 6 lines

The second problem is the error handling. The error may not be caused by VFIO_DEVICE_FEATURE_DMA_BUF but may be caused by the guest passing
regions that do not belong to one VFIO device. The two cases should be distinguished for a correct message.

Ok, I'll add separate error messages.

Quoted 35 lines

hw/display/virtio-gpu-dmabuf.c

Quoted 5 lines
hw/display/virtio-gpu-dmabuf.c +3 -0
+#endif
+}
+
   static void virtio_gpu_create_udmabuf(struct

virtio_gpu_simple_resource *res)

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

Quoted 3 lines
hw/display/virtio-gpu-dmabuf.c +0 -0
   {
       struct udmabuf_create_list *list;
@@ -43,10 +76,7 @@ static void virtio_gpu_create_udmabuf(struct

virtio_gpu_simple_resource *res)

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

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

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

Quoted 6 lines
hw/display/virtio-gpu-dmabuf.c +0 -2
-        rcu_read_unlock();
-
           if (!rb || rb->fd < 0) {
               g_free(list);
               return;
@@ -62,17 +92,84 @@ static void virtio_gpu_create_udmabuf(struct

virtio_gpu_simple_resource *res)

Quoted 11 lines
hw/display/virtio-gpu-dmabuf.c +3 -2
       res->dmabuf_fd = ioctl(udmabuf, UDMABUF_CREATE_LIST, list);
       if (res->dmabuf_fd < 0) {
-        warn_report("%s: UDMABUF_CREATE_LIST: %s", __func__,
-                    strerror(errno));
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: UDMABUF_CREATE_LIST: %s\n",
+                      __func__, strerror(errno));
       }
       g_free(list);
   }
Quoted 5 lines
Quoted 3 lines

These changes of virtio_gpu_create_udmabuf() are independent so should
go into separate patches with explanation.

I called these out in the commit message and included them as they were trivial but I guess I could move them into separate patches.

As docs/devel/submitting-a-patch.rst says,

Quoted 2 lines

A commit message that mentions "Also, ..." is often a good candidate for splitting into multiple patches.

I think there will be another round of review so I suggest taking the chance to do so.

Quoted 34 lines
Quoted 34 lines

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

Quoted 4 lines
hw/display/virtio-gpu-dmabuf.c +1 -0
+static void *vfio_dmabuf_mmap(struct virtio_gpu_simple_resource

*res)

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

Quoted 9 lines
hw/display/virtio-gpu-dmabuf.c +9 -0
+{
+    struct vfio_region_info *info = NULL;
+    VFIODevice *vbasedev = NULL;
+    ram_addr_t offset, len = 0;
+    RAMBlock *first_rb, *rb;
+    void *map, *submap;
+    int i, ret = -1;
+
+    first_rb = qemu_ram_block_from_host(res->iov[0].iov_base, false,

&offset);

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

Quoted 13 lines
hw/display/virtio-gpu-dmabuf.c +13 -0
+    if (!first_rb) {
+        return MAP_FAILED;
+    }
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+    vbasedev = vfio_device_lookup(first_rb->mr);
+#endif
+    if (!vbasedev) {
+        return MAP_FAILED;
+    }
+
+    /*
+     * We first reserve a contiguous chunk of address space for the entire
+     * dmabuf, then replace it with smaller mappings that correspond to

the

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

Quoted 3 lines
hw/display/virtio-gpu-dmabuf.c +3 -0
+     * individual segments of the dmabuf.
+     */
+    map = mmap(NULL, res->blob_size, PROT_READ, MAP_SHARED,

vbasedev->fd, 0);

Quoted 7 lines
Quoted 4 lines

It should have PROT_NONE instead of PROT_READ to prevent accidental accesses to the placeholder mapping.
Likewise, -1 should be passed instead of vbasedev->fd so that the placeholder mapping won't result in a mapping to real device.

I am OK with both suggestions. However, I don't see any problem if the placeholder mapping is associated with the real device. In other words, what specific problem do you see if we keep vbasedev->fd instead of -1?

These two suggestions are to avoid accidents. If the code is right, there will be no accesses to the head of the device memory. But if there is something wrong with it, these two changes will be safe guards. In other words, with these two changes, you will not have such an accidental access unless all of the following three happen:
1) Accidentally accessing the mapped page.
2) The protection is set something else PROT_NONE.
3) The mapping points to the device.

Each of these conditions make accidents rarer to occur.

Quoted 23 lines
Quoted 9 lines

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

Quoted 8 lines
hw/display/virtio-gpu-dmabuf.c +6 -0
+    if (map == MAP_FAILED) {
+        return map;
+    }
+
+    for (i = 0; i < res->iov_cnt; i++) {
+        rb = qemu_ram_block_from_host(res->iov[i].iov_base, false,

&offset);

Quoted 14 lines

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

Quoted 10 lines
hw/display/virtio-gpu-dmabuf.c +10 -0
+        if (rb != first_rb) {
+            goto err;
+        }
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+        ret = vfio_get_region_index_from_mr(rb->mr);
+        if (ret < 0) {
+            goto err;
+        }
+
+        ret = vfio_device_get_region_info(vbasedev, ret, &info);

This function can take rb->mr and call vfio_device_lookup() and vfio_get_region_index_from_mr() internally to simplify the API as similar can be done for vfio_device_create_dmabuf_fd().

Quoted 5 lines

Right, but vfio_device_get_region_info() is an already existing API that is called from various places that I am trying to leverage. I could create another API to wrap the calls to vfio_get_region_index_from_mr() and vfio_device_get_region_info() but not sure if that makes sense given that it would only have one caller.

vfio_device_get_region_info() should be regarded as an internal function of VFIO.

Thinking of that, the current API design that relies on vfio_device_lookup() to expose VFIODevice * is not optimal. We never want virtio-gpu or anyone else VFIO to mess up with it using functions declared in include/hw/vfio/vfio-device.h. Likewise, exposing vbasedev->fd introduces bunch of hazards.

I think this entire function is better moved to VFIO for better encapsulation. I also think it should be placed close to vfio_device_create_dmabuf_fd(). These functions are quite similar so are better reviewed togather. Any future event to change either of the function is also likely to require a change to the other, so placing them nearby will be convenient.

Regards,
Akihiko Odaki

#20 lore raw

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

Hi Akihiko,

Quoted 161 lines

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

Quoted 101 lines
Quoted 101 lines
Quoted 27 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 call vfio_create_dmabuf() which identifies the right VFIO device and eventually invokes the vfio_device_create_dmabuf_fd() API to have a dmabuf fd created.

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 use the VFIO device fd directly to create the CPU mapping.

While at it, remove the unnecessary rcu_read_lock/rcu_read_unlock from virtio_gpu_create_udmabuf() and also replace warn_report() with qemu_log_mask().

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 | 117

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

hw/display/virtio-gpu-

Quoted 19 lines
   2 files changed, 113 insertions(+), 9 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 c34d4c85bc..2dfe70d7eb 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,38 @@
   #include "standard-headers/linux/udmabuf.h"
   #include "standard-headers/drm/drm_fourcc.h"
 
+static void vfio_create_dmabuf(struct virtio_gpu_simple_resource

*res)

hw/display/virtio-gpu-dmabuf.c

Quoted 7 lines
hw/display/virtio-gpu-dmabuf.c +7 -0
+{
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+    VFIODevice *vbasedev;
+    RAMBlock *first_rb;
+    ram_addr_t offset;
+
+    first_rb = qemu_ram_block_from_host(res->iov[0].iov_base, false,

&offset);

Quoted 11 lines
hw/display/virtio-gpu-dmabuf.c +11 -0
+    if (!first_rb) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: Could not find ramblock\n", __func__);
+        return;
+    }
+
+    vbasedev = vfio_device_lookup(first_rb->mr);
+    if (!vbasedev) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: No VFIO device found to create dmabuf from\n",
+                      __func__);

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

Quoted 10 lines
hw/display/virtio-gpu-dmabuf.c +10 -0
+        return;
+    }
+
+    res->dmabuf_fd = vfio_device_create_dmabuf_fd(vbasedev,
+                                                  res->iov, res->iov_cnt);
+    if (res->dmabuf_fd < 0) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: VFIO_DEVICE_FEATURE_DMA_BUF: %s\n",
+                      __func__, strerror(errno));
+    }

This API design of vfio_device_create_dmabuf_fd() has some room of improvements.

Quoted 6 lines
Quoted 5 lines

First, this function requires to pass VFIODevice * which is derived from res->iov[0]. Since it is getting res->iov[0] anyway, such a parameter is redundant. vfio_device_create_dmabuf_fd() can call vfio_device_lookup()
itself instead.

Yeah, I agree. I'll move the call to lookup into

vfio_device_create_dmabuf_fd().

Quoted 3 lines
Quoted 3 lines

The second problem is the error handling. The error may not be caused

by

Quoted 3 lines
Quoted 3 lines

VFIO_DEVICE_FEATURE_DMA_BUF but may be caused by the guest passing
regions that do not belong to one VFIO device. The two cases should

be

Quoted 38 lines
Quoted 1 line

distinguished for a correct message.

Ok, I'll add separate error messages.

Quoted 35 lines

hw/display/virtio-gpu-dmabuf.c

Quoted 5 lines
hw/display/virtio-gpu-dmabuf.c +3 -0
+#endif
+}
+
   static void virtio_gpu_create_udmabuf(struct

virtio_gpu_simple_resource *res)

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

Quoted 3 lines
hw/display/virtio-gpu-dmabuf.c +0 -0
   {
       struct udmabuf_create_list *list;
@@ -43,10 +76,7 @@ static void virtio_gpu_create_udmabuf(struct

virtio_gpu_simple_resource *res)

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

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

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

Quoted 6 lines
hw/display/virtio-gpu-dmabuf.c +0 -2
-        rcu_read_unlock();
-
           if (!rb || rb->fd < 0) {
               g_free(list);
               return;
@@ -62,17 +92,84 @@ static void virtio_gpu_create_udmabuf(struct

virtio_gpu_simple_resource *res)

Quoted 11 lines
hw/display/virtio-gpu-dmabuf.c +3 -2
       res->dmabuf_fd = ioctl(udmabuf, UDMABUF_CREATE_LIST, list);
       if (res->dmabuf_fd < 0) {
-        warn_report("%s: UDMABUF_CREATE_LIST: %s", __func__,
-                    strerror(errno));
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: UDMABUF_CREATE_LIST: %s\n",
+                      __func__, strerror(errno));
       }
       g_free(list);
   }
Quoted 4 lines
Quoted 3 lines

These changes of virtio_gpu_create_udmabuf() are independent so should
go into separate patches with explanation.

I called these out in the commit message and included them as they

were

Quoted 8 lines
Quoted 1 line

trivial but I guess I could move them into separate patches.

As docs/devel/submitting-a-patch.rst says,

Quoted 2 lines

A commit message that mentions "Also, ..." is often a good candidate for splitting into multiple patches.

I think there will be another round of review so I suggest taking the chance to do so.

Ok, I'll split them into separate patches.

Quoted 55 lines
Quoted 16 lines
Quoted 16 lines

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

Quoted 5 lines
hw/display/virtio-gpu-dmabuf.c +1 -0
+static void *vfio_dmabuf_mmap(struct virtio_gpu_simple_resource

*res)

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

Quoted 9 lines
hw/display/virtio-gpu-dmabuf.c +9 -0
+{
+    struct vfio_region_info *info = NULL;
+    VFIODevice *vbasedev = NULL;
+    ram_addr_t offset, len = 0;
+    RAMBlock *first_rb, *rb;
+    void *map, *submap;
+    int i, ret = -1;
+
+    first_rb = qemu_ram_block_from_host(res->iov[0].iov_base, false,

&offset);

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
hw/display/virtio-gpu-dmabuf.c +4 -0
+    if (!first_rb) {
+        return MAP_FAILED;
+    }
+#if defined(VIRTIO_GPU_VFIO_BLOB)
Quoted 8 lines

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

Quoted 8 lines
hw/display/virtio-gpu-dmabuf.c +8 -0
+    vbasedev = vfio_device_lookup(first_rb->mr);
+#endif
+    if (!vbasedev) {
+        return MAP_FAILED;
+    }
+
+    /*
+     * We first reserve a contiguous chunk of address space for the

entire

Quoted 1 line
Quoted 1 line

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

Quoted 1 line
hw/display/virtio-gpu-dmabuf.c +1 -0
+     * dmabuf, then replace it with smaller mappings that correspond

to

Quoted 7 lines
Quoted 7 lines

the

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

Quoted 3 lines
hw/display/virtio-gpu-dmabuf.c +3 -0
+     * individual segments of the dmabuf.
+     */
+    map = mmap(NULL, res->blob_size, PROT_READ, MAP_SHARED,

vbasedev->fd, 0);

It should have PROT_NONE instead of PROT_READ to prevent

accidental

Quoted 6 lines
Quoted 3 lines

accesses to the placeholder mapping.
Likewise, -1 should be passed instead of vbasedev->fd so that the placeholder mapping won't result in a mapping to real device.

I am OK with both suggestions. However, I don't see any problem if the placeholder mapping is associated with the real device. In other words, what specific problem do you see if we keep vbasedev->fd instead of -1?

These two suggestions are to avoid accidents. If the code is right, there will be no accesses to the head of the device memory. But if there is something wrong with it, these two changes will be safe guards. In other words, with these two changes, you will not have such an accidental access unless all of the following three happen:
1) Accidentally accessing the mapped page.
2) The protection is set something else PROT_NONE.
3) The mapping points to the device.

Quoted 1 line

Each of these conditions make accidents rarer to occur.

Sounds good. I'll add these two suggestions.

Quoted 41 lines
Quoted 24 lines
Quoted 10 lines

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

Quoted 9 lines
hw/display/virtio-gpu-dmabuf.c +6 -0
+    if (map == MAP_FAILED) {
+        return map;
+    }
+
+    for (i = 0; i < res->iov_cnt; i++) {
+        rb = qemu_ram_block_from_host(res->iov[i].iov_base, false,

&offset);

Quoted 14 lines

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

Quoted 10 lines
hw/display/virtio-gpu-dmabuf.c +10 -0
+        if (rb != first_rb) {
+            goto err;
+        }
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+        ret = vfio_get_region_index_from_mr(rb->mr);
+        if (ret < 0) {
+            goto err;
+        }
+
+        ret = vfio_device_get_region_info(vbasedev, ret, &info);

This function can take rb->mr and call vfio_device_lookup() and vfio_get_region_index_from_mr() internally to simplify the API as similar can be done for vfio_device_create_dmabuf_fd().

Quoted 3 lines

Right, but vfio_device_get_region_info() is an already existing API that is called from various places that I am trying to leverage. I could create another API to wrap the calls to

vfio_get_region_index_from_mr()

Quoted 1 line

and vfio_device_get_region_info() but not sure if that makes sense

given

Quoted 1 line

that it would only have one caller.

vfio_device_get_region_info() should be regarded as an internal function of VFIO.

Thinking of that, the current API design that relies on vfio_device_lookup() to expose VFIODevice * is not optimal. We never want virtio-gpu or anyone else VFIO to mess up with it using functions declared in include/hw/vfio/vfio-device.h. Likewise, exposing vbasedev->fd introduces bunch of hazards.

Quoted 6 lines

I think this entire function is better moved to VFIO for better encapsulation. I also think it should be placed close to vfio_device_create_dmabuf_fd(). These functions are quite similar so are better reviewed togather. Any future event to change either of the function is also likely to require a change to the other, so placing them nearby will be convenient.

Ok, I'll try to rename and move this function to VFIO.

Thanks,
Vivek

Quoted 3 lines

Regards,
Akihiko Odaki

#21 lore raw

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

In reply to Vivek Kasireddy
Quoted 61 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 call vfio_create_dmabuf() which identifies the right VFIO device and eventually invokes the vfio_device_create_dmabuf_fd() API to have a dmabuf fd created.

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 use the VFIO device fd directly to create the CPU mapping.

While at it, remove the unnecessary rcu_read_lock/rcu_read_unlock from virtio_gpu_create_udmabuf() and also replace warn_report() with qemu_log_mask().

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 | 117 ++++++++++++++++++++++++++++++---
  2 files changed, 113 insertions(+), 9 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 +1 -0
index c34d4c85bc..2dfe70d7eb 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"

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

Quoted 4 lines
hw/display/virtio-gpu-dmabuf.c +1 -0
@@ -27,6 +28,38 @@
  #include "standard-headers/linux/udmabuf.h"
  #include "standard-headers/drm/drm_fourcc.h"
 
+static void vfio_create_dmabuf(struct virtio_gpu_simple_resource *res)

Could you please not use the 'vfio_' prefix ? Its confusing as there routines are part of the virtio component.

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

Quoted 10 lines
hw/display/virtio-gpu-dmabuf.c +10 -0
+{
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+    VFIODevice *vbasedev;
+    RAMBlock *first_rb;
+    ram_addr_t offset;
+
+    first_rb = qemu_ram_block_from_host(res->iov[0].iov_base, false, &offset);
+    if (!first_rb) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: Could not find ramblock\n", __func__);

Error handling is quite poor. Can't we have an 'Error *' parameter ?

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

Quoted 10 lines
hw/display/virtio-gpu-dmabuf.c +10 -0
+        return;
+    }
+
+    vbasedev = vfio_device_lookup(first_rb->mr);
+    if (!vbasedev) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: No VFIO device found to create dmabuf from\n",
+                      __func__);
+        return;
+    }

vfio_dmabuf_mmap() does the same sequence of code to query the VFIODevice. May be a common helper would help.

Quoted 15 lines
hw/display/virtio-gpu-dmabuf.c +11 -0
+
+    res->dmabuf_fd = vfio_device_create_dmabuf_fd(vbasedev,
+                                                  res->iov, res->iov_cnt);
+    if (res->dmabuf_fd < 0) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: VFIO_DEVICE_FEATURE_DMA_BUF: %s\n",
+                      __func__, strerror(errno));
+    }
+#endif
+}
+
  static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res)
  {
      struct udmabuf_create_list *list;

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

Quoted 7 lines
hw/display/virtio-gpu-dmabuf.c +0 -3
@@ -43,10 +76,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();
-

The rcu un/lock removal needs some explanations. It deserves a separate patch I would say.

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

Quoted 10 lines
hw/display/virtio-gpu-dmabuf.c +1 -2
          if (!rb || rb->fd < 0) {
              g_free(list);
              return;
@@ -62,17 +92,84 @@ 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));
+        qemu_log_mask(LOG_GUEST_ERROR,

Is it a guest error ?

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

Quoted 7 lines
hw/display/virtio-gpu-dmabuf.c +3 -0
+                      "%s: UDMABUF_CREATE_LIST: %s\n",
+                      __func__, strerror(errno));
      }
      g_free(list);
  }
 
+static void *vfio_dmabuf_mmap(struct virtio_gpu_simple_resource *res)

please use a "virtio" prefix.

Quoted 74 lines
hw/display/virtio-gpu-dmabuf.c +65 -0
+{
+    struct vfio_region_info *info = NULL;
+    VFIODevice *vbasedev = NULL;
+    ram_addr_t offset, len = 0;
+    RAMBlock *first_rb, *rb;
+    void *map, *submap;
+    int i, ret = -1;
+
+    first_rb = qemu_ram_block_from_host(res->iov[0].iov_base, false, &offset);
+    if (!first_rb) {
+        return MAP_FAILED;
+    }
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+    vbasedev = vfio_device_lookup(first_rb->mr);
+#endif
+    if (!vbasedev) {
+        return MAP_FAILED;
+    }
+
+    /*
+     * 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, res->blob_size, PROT_READ, MAP_SHARED, vbasedev->fd, 0);
+    if (map == MAP_FAILED) {
+        return map;
+    }
+
+    for (i = 0; i < res->iov_cnt; i++) {
+        rb = qemu_ram_block_from_host(res->iov[i].iov_base, false, &offset);
+        if (rb != first_rb) {
+            goto err;
+        }
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+        ret = vfio_get_region_index_from_mr(rb->mr);
+        if (ret < 0) {
+            goto err;
+        }
+
+        ret = vfio_device_get_region_info(vbasedev, ret, &info);
+#endif
+        if (ret < 0 || !info) {
+            goto err;
+        }
+
+        submap = mmap(map + len, res->iov[i].iov_len, PROT_READ,
+                      MAP_SHARED | MAP_FIXED, vbasedev->fd,
+                      info->offset + offset);
+        if (submap == MAP_FAILED) {
+            goto err;
+        }
+
+        len += res->iov[i].iov_len;
+    }
+    return map;
+err:
+    munmap(map, res->blob_size);
+    return MAP_FAILED;
+}
+
  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);
      if (res->remapped == MAP_FAILED) {
+        res->remapped = vfio_dmabuf_mmap(res);
+        if (res->remapped != MAP_FAILED) {
+            return;
+        }
          warn_report("%s: dmabuf mmap failed: %s", __func__,
                      strerror(errno));
          res->remapped = NULL;

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

Quoted 3 lines
hw/display/virtio-gpu-dmabuf.c +0 -0
@@ -139,8 +236,12 @@ void virtio_gpu_init_dmabuf(struct virtio_gpu_simple_resource *res)
      } else {
          virtio_gpu_create_udmabuf(res);
          if (res->dmabuf_fd < 0) {

Why test dmabuf_fd ? Can virtio_gpu_init_dmabuf() be called mulmtiple times ?

Thanks,

C.

Quoted 21 lines
hw/display/virtio-gpu-dmabuf.c +6 -4
-            return;
+            vfio_create_dmabuf(res);
+            if (res->dmabuf_fd < 0) {
+                return;
+            }
          }
+
          virtio_gpu_remap_dmabuf(res);
          if (!res->remapped) {
              return;
@@ -153,9 +254,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)
#22 lore raw

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

Hi Cedric,

Quoted 70 lines

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

Quoted 27 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 call vfio_create_dmabuf() which identifies the right VFIO device and eventually invokes the vfio_device_create_dmabuf_fd() API to have a dmabuf fd created.

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 use the VFIO device fd directly to create the CPU mapping.

While at it, remove the unnecessary rcu_read_lock/rcu_read_unlock from virtio_gpu_create_udmabuf() and also replace warn_report() with qemu_log_mask().

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 | 117

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

hw/display/virtio-gpu-

Quoted 19 lines
  2 files changed, 113 insertions(+), 9 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 15 lines

index c34d4c85bc..2dfe70d7eb 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"
@@ -27,6 +28,38 @@
  #include "standard-headers/linux/udmabuf.h"
  #include "standard-headers/drm/drm_fourcc.h"
 
Quoted 5 lines

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

Quoted 1 line
hw/display/virtio-gpu-dmabuf.c +1 -0
+static void vfio_create_dmabuf(struct virtio_gpu_simple_resource

*res)

Could you please not use the 'vfio_' prefix ? Its confusing as there routines are part of the virtio component.

Ok, I'll use a different prefix.

Quoted 10 lines

hw/display/virtio-gpu-dmabuf.c

Quoted 9 lines
hw/display/virtio-gpu-dmabuf.c +7 -0
+{
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+    VFIODevice *vbasedev;
+    RAMBlock *first_rb;
+    ram_addr_t offset;
+
+    first_rb = qemu_ram_block_from_host(res->iov[0].iov_base, false,

&offset);

Quoted 5 lines

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

Quoted 3 lines
hw/display/virtio-gpu-dmabuf.c +3 -0
+    if (!first_rb) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: Could not find ramblock\n", __func__);

Error handling is quite poor. Can't we have an 'Error *' parameter ?

Ok, I'll improve the error handling by introducing Error * here.

Quoted 12 lines

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

Quoted 11 lines
hw/display/virtio-gpu-dmabuf.c +10 -0
+        return;
+    }
+
+    vbasedev = vfio_device_lookup(first_rb->mr);
+    if (!vbasedev) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: No VFIO device found to create dmabuf from\n",
+                      __func__);
+        return;
+    }
Quoted 3 lines

vfio_dmabuf_mmap() does the same sequence of code to query the VFIODevice.
May be a common helper would help.

Sure, I'll try to figure out a common helper.

Quoted 23 lines

hw/display/virtio-gpu-dmabuf.c

Quoted 13 lines
hw/display/virtio-gpu-dmabuf.c +11 -0
+
+    res->dmabuf_fd = vfio_device_create_dmabuf_fd(vbasedev,
+                                                  res->iov, res->iov_cnt);
+    if (res->dmabuf_fd < 0) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: VFIO_DEVICE_FEATURE_DMA_BUF: %s\n",
+                      __func__, strerror(errno));
+    }
+#endif
+}
+
  static void virtio_gpu_create_udmabuf(struct

virtio_gpu_simple_resource *res)

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

Quoted 3 lines
hw/display/virtio-gpu-dmabuf.c +0 -0
  {
      struct udmabuf_create_list *list;
@@ -43,10 +76,7 @@ static void virtio_gpu_create_udmabuf(struct

virtio_gpu_simple_resource *res)

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

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

&offset);

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

Quoted 2 lines
hw/display/virtio-gpu-dmabuf.c +0 -2
-        rcu_read_unlock();
-

The rcu un/lock removal needs some explanations. It deserves a separate patch
I would say.

Ok, I'll split these changes out into separate patches.

Quoted 8 lines

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

Quoted 6 lines
hw/display/virtio-gpu-dmabuf.c +0 -0
          if (!rb || rb->fd < 0) {
              g_free(list);
              return;
@@ -62,17 +92,84 @@ static void virtio_gpu_create_udmabuf(struct

virtio_gpu_simple_resource *res)

Quoted 1 line
Quoted 7 lines

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

Quoted 5 lines
      res->dmabuf_fd = ioctl(udmabuf, UDMABUF_CREATE_LIST, list);
      if (res->dmabuf_fd < 0) {
hw/display/virtio-gpu-dmabuf.c +1 -2
-        warn_report("%s: UDMABUF_CREATE_LIST: %s", __func__,
-                    strerror(errno));
+        qemu_log_mask(LOG_GUEST_ERROR,

Is it a guest error ?

IIUC, it can be considered a guest error as it failed to create a dmabuf on the Host using the udmabuf driver and Guest provided addresses.

Quoted 11 lines

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

Quoted 8 lines
hw/display/virtio-gpu-dmabuf.c +3 -0
+                      "%s: UDMABUF_CREATE_LIST: %s\n",
+                      __func__, strerror(errno));
      }
      g_free(list);
  }
 
+static void *vfio_dmabuf_mmap(struct virtio_gpu_simple_resource

*res)

please use a "virtio" prefix.

As suggested by Akihiko, I am considering moving this routine to VFIO in order to avoid exposing VFIODevice or VFIODevice->fd outside of VFIO.

Quoted 81 lines

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

Quoted 10 lines
hw/display/virtio-gpu-dmabuf.c +9 -0
+{
+    struct vfio_region_info *info = NULL;
+    VFIODevice *vbasedev = NULL;
+    ram_addr_t offset, len = 0;
+    RAMBlock *first_rb, *rb;
+    void *map, *submap;
+    int i, ret = -1;
+
+    first_rb = qemu_ram_block_from_host(res->iov[0].iov_base, false,

&offset);

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

Quoted 13 lines
hw/display/virtio-gpu-dmabuf.c +13 -0
+    if (!first_rb) {
+        return MAP_FAILED;
+    }
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+    vbasedev = vfio_device_lookup(first_rb->mr);
+#endif
+    if (!vbasedev) {
+        return MAP_FAILED;
+    }
+
+    /*
+     * We first reserve a contiguous chunk of address space for the entire
+     * dmabuf, then replace it with smaller mappings that correspond to

the

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

Quoted 3 lines
hw/display/virtio-gpu-dmabuf.c +3 -0
+     * individual segments of the dmabuf.
+     */
+    map = mmap(NULL, res->blob_size, PROT_READ, MAP_SHARED,

vbasedev->fd, 0);

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 +6 -0
+    if (map == MAP_FAILED) {
+        return map;
+    }
+
+    for (i = 0; i < res->iov_cnt; i++) {
+        rb = qemu_ram_block_from_host(res->iov[i].iov_base, false,

&offset);

Quoted 21 lines
hw/display/virtio-gpu-dmabuf.c +21 -0
+        if (rb != first_rb) {
+            goto err;
+        }
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+        ret = vfio_get_region_index_from_mr(rb->mr);
+        if (ret < 0) {
+            goto err;
+        }
+
+        ret = vfio_device_get_region_info(vbasedev, ret, &info);
+#endif
+        if (ret < 0 || !info) {
+            goto err;
+        }
+
+        submap = mmap(map + len, res->iov[i].iov_len, PROT_READ,
+                      MAP_SHARED | MAP_FIXED, vbasedev->fd,
+                      info->offset + offset);
+        if (submap == MAP_FAILED) {
+            goto err;
+        }

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

Quoted 10 lines
hw/display/virtio-gpu-dmabuf.c +9 -0
+
+        len += res->iov[i].iov_len;
+    }
+    return map;
+err:
+    munmap(map, res->blob_size);
+    return MAP_FAILED;
+}
+
  static void virtio_gpu_remap_dmabuf(struct

virtio_gpu_simple_resource *res)

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

Quoted 12 lines
  {
      res->remapped = mmap(NULL, res->blob_size, PROT_READ,
                           MAP_SHARED, res->dmabuf_fd, 0);
      if (res->remapped == MAP_FAILED) {
hw/display/virtio-gpu-dmabuf.c +4 -0
+        res->remapped = vfio_dmabuf_mmap(res);
+        if (res->remapped != MAP_FAILED) {
+            return;
+        }
          warn_report("%s: dmabuf mmap failed: %s", __func__,
                      strerror(errno));
          res->remapped = NULL;
@@ -139,8 +236,12 @@ void virtio_gpu_init_dmabuf(struct

virtio_gpu_simple_resource *res)

Quoted 6 lines

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

Quoted 3 lines
hw/display/virtio-gpu-dmabuf.c +0 -0
      } else {
          virtio_gpu_create_udmabuf(res);
          if (res->dmabuf_fd < 0) {

Why test dmabuf_fd ? Can virtio_gpu_init_dmabuf() be called mulmtiple times ?

Yes, virtio_gpu_init_dmabuf() can be called multiple times as the Guest VM creates/uses new resources/Guest blobs, which can happen frequently. Are you suggesting that it is better to make virtio_gpu_create_udmabuf() return res->dmabuf_fd explicitly as opposed to the current indirect approach?

Thanks,
Vivek

Quoted 30 lines

Thanks,

C.

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

Quoted 11 lines
hw/display/virtio-gpu-dmabuf.c +5 -1
-            return;
+            vfio_create_dmabuf(res);
+            if (res->dmabuf_fd < 0) {
+                return;
+            }
          }
+
          virtio_gpu_remap_dmabuf(res);
          if (!res->remapped) {
              return;
@@ -153,9 +254,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)

#23 lore raw

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

Hi,

Quoted 17 lines
Quoted 17 lines

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

Quoted 12 lines
   {
       res->remapped = mmap(NULL, res->blob_size, PROT_READ,
                            MAP_SHARED, res->dmabuf_fd, 0);
       if (res->remapped == MAP_FAILED) {
hw/display/virtio-gpu-dmabuf.c +4 -0
+        res->remapped = vfio_dmabuf_mmap(res);
+        if (res->remapped != MAP_FAILED) {
+            return;
+        }
           warn_report("%s: dmabuf mmap failed: %s", __func__,
                       strerror(errno));
           res->remapped = NULL;
@@ -139,8 +236,12 @@ void virtio_gpu_init_dmabuf(struct

virtio_gpu_simple_resource *res)

Quoted 3 lines
hw/display/virtio-gpu-dmabuf.c +0 -0
       } else {
           virtio_gpu_create_udmabuf(res);
           if (res->dmabuf_fd < 0) {
Quoted 6 lines
Quoted 2 lines

Why test dmabuf_fd ? Can virtio_gpu_init_dmabuf() be called mulmtiple times ?

Yes, virtio_gpu_init_dmabuf() can be called multiple times as the Guest VM creates/uses new resources/Guest blobs, which can happen frequently. Are you suggesting that it is better to make virtio_gpu_create_udmabuf() return res->dmabuf_fd explicitly as opposed to the current indirect approach?

Yes. Good idea. It would be improve reading.

Thanks,

C.