Skip to contentSkip to conversation

vfio: Implement VFIO_DEVICE_FEATURE_DMA_BUF and use it in virtio-gpu

PATCH Superseded Vivek Kasireddy 3 participants 44 messages Oct 3, 2025 – Oct 24, 2025

v1 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

cover vfio: Implement VFIO_DEVICE_FEATURE_DMA_BUF 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 implement VFIO_DEVICE_FEATURE_DMA_BUF and leverage it in virtio-gpu.

So, while creating the blobs we use memory_region_is_ram_device() to figure out if the blob is backed by memfd or a vfio-pci device. If it is determined that the blob is backed by vfio-pci device region, instead of calling into udmabuf driver to create dmabuf we would now call into vfio-pci driver to have a dmabuf created on the Host.

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 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 vfio-pci kernel driver series:
https://lore.kernel.org/dri-devel/[email protected]/
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: Leon Romanovsky <[email protected]>
Cc: Leon Romanovsky <[email protected]>
Cc: Jason Gunthorpe <[email protected]>
Cc: Dongwon Kim <[email protected]>

Vivek Kasireddy (7):
virtio-gpu: Recreate the resource's dmabuf if new backing is attached
virtio-gpu: Don't rely on res->blob to identify blob resources
virtio-gpu: Find hva for Guest's DMA addr associated with a ram device vfio/region: Add a helper to get region index from memory region
linux-headers: Update vfio.h to include VFIO_DEVICE_FEATURE_DMA_BUF vfio/device: Add support for VFIO_DEVICE_FEATURE_DMA_BUF
virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO
devices

 hw/display/Kconfig              |   5 ++
 hw/display/virtio-gpu-udmabuf.c | 143 ++++++++++++++++++++++++++++++--
 hw/display/virtio-gpu.c         |  53 +++++++++---
 hw/vfio/device.c                |  43 ++++++++++
 hw/vfio/region.c                |  14 ++++
 include/hw/vfio/vfio-device.h   |   5 ++
 linux-headers/linux/vfio.h      |  25 ++++++
 7 files changed, 270 insertions(+), 18 deletions(-)
Signature · 1 line
2.50.1
#1 lore raw

PATCH 1/7 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]>
---
 hw/display/virtio-gpu.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/display/virtio-gpu.c +4 -0
index de35902213..70e8757128 100644
@@ -937,6 +937,10 @@ virtio_gpu_resource_attach_backing(VirtIOGPU *g,
         cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
         return;
     }
+
+    if (res->blob_size && res->dmabuf_fd < 0) {
+        virtio_gpu_init_udmabuf(res);
+    }
 }
 
 static void
Signature · 1 line
2.50.1
#2 lore raw

Re: [PATCH v1 1/7] virtio-gpu: Recreate the resource's dmabuf if new backing is attached

Quoted 18 lines · Vivek Kasireddy

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

Quoted 1 line
Cc Marc-André Lureau <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
---
  hw/display/virtio-gpu.c | 4 ++++
  1 file changed, 4 insertions(+)
hw/display/virtio-gpu.c +0 -0
index de35902213..70e8757128 100644

hw/display/virtio-gpu.c virtio_gpu_resource_attach_backing(VirtIOGPU *g, · line 941

Quoted 5 lines
hw/display/virtio-gpu.c +2 -0
@@ -937,6 +937,10 @@ virtio_gpu_resource_attach_backing(VirtIOGPU *g,
          cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
          return;
      }
+
+    if (res->blob_size && res->dmabuf_fd < 0) {

Checking res->dmabuf_fd should be unnecessary because it is always < 0 when !res->iov && res->blob_size.

virtio_gpu_cleanup_mapping() is the function that sets res->iov to NULL, and it ensures res->dmabuf_fd < 0 by calling virtio_gpu_fini_udmabuf() when res->blob_size != 0.

#3 lore raw

RE: [PATCH v1 1/7] virtio-gpu: Recreate the resource's dmabuf if new backing is attached

Quoted 15 lines · Vivek Kasireddy
Quoted 13 lines

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

Quoted 1 line
Cc Marc-André Lureau <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
---
  hw/display/virtio-gpu.c | 4 ++++
  1 file changed, 4 insertions(+)
Quoted 13 lines

hw/display/virtio-gpu.c virtio_gpu_resource_attach_backing(VirtIOGPU *g, · line 941

Quoted 10 lines
hw/display/virtio-gpu.c +2 -0
index de35902213..70e8757128 100644
@@ -937,6 +937,10 @@ virtio_gpu_resource_attach_backing(VirtIOGPU *g,
          cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
          return;
      }
+
+    if (res->blob_size && res->dmabuf_fd < 0) {

Checking res->dmabuf_fd should be unnecessary because it is always < 0 when !res->iov && res->blob_size.

I agree, it is unnecessary. I'll remove it.

Thanks,
Vivek

Quoted 4 lines

virtio_gpu_cleanup_mapping() is the function that sets res->iov to NULL, and it ensures res->dmabuf_fd < 0 by calling virtio_gpu_fini_udmabuf() when res->blob_size != 0.

#4 lore raw

PATCH 2/7 virtio-gpu: Don't rely on res->blob to identify blob resources

In reply to Vivek Kasireddy

The res->blob pointer may not be valid (non-NULL) for some blobs where the backing storage is not memfd based. Therefore, we cannot use it to determine if a resource is a blob or not. Instead, we could use res->blob_size to make this determination as it is non-zero for blob resources regardless of where their backing storage is located.

Cc Marc-André Lureau <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
---
 hw/display/virtio-gpu.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)
hw/display/virtio-gpu.c +11 -8
index 70e8757128..df696611a4 100644
@@ -57,7 +57,7 @@ void virtio_gpu_update_cursor_data(VirtIOGPU *g,
     }
 
     if (res->blob_size) {
-        if (res->blob_size < (s->current_cursor->width *
+        if (!res->blob || res->blob_size < (s->current_cursor->width *
                               s->current_cursor->height * 4)) {
             return;
         }
@@ -144,7 +144,7 @@ virtio_gpu_find_check_resource(VirtIOGPU *g, uint32_t resource_id,
     }
 
     if (require_backing) {
-        if (!res->iov || (!res->image && !res->blob)) {
+        if (!res->iov || (!res->image && !res->blob_size)) {
             qemu_log_mask(LOG_GUEST_ERROR, "%s: no backing storage %d\n",
                           caller, resource_id);
             if (error) {
@@ -446,7 +446,7 @@ static void virtio_gpu_transfer_to_host_2d(VirtIOGPU *g,
 
     res = virtio_gpu_find_check_resource(g, t2d.resource_id, true,
                                          __func__, &cmd->error);
-    if (!res || res->blob) {
+    if (!res || res->blob_size) {
         return;
     }
 
@@ -509,7 +509,7 @@ static void virtio_gpu_resource_flush(VirtIOGPU *g,
         return;
     }
 
-    if (res->blob) {
+    if (res->blob_size) {
         for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
             scanout = &g->parent_obj.scanout[i];
             if (scanout->resource_id == res->resource_id &&
@@ -540,7 +540,7 @@ static void virtio_gpu_resource_flush(VirtIOGPU *g,
         }
     }
 
-    if (!res->blob &&
+    if (!res->blob_size &&
         (rf.r.x > res->width ||
         rf.r.y > res->height ||
         rf.r.width > res->width ||
@@ -636,7 +636,7 @@ static bool virtio_gpu_do_set_scanout(VirtIOGPU *g,
 
     g->parent_obj.enable = 1;
 
-    if (res->blob) {
+    if (res->blob_size) {
         if (console_has_gl(scanout->con)) {
             if (!virtio_gpu_update_dmabuf(g, scanout_id, res, fb, r)) {
                 virtio_gpu_update_scanout(g, scanout_id, res, fb, r);
@@ -647,13 +647,16 @@ static bool virtio_gpu_do_set_scanout(VirtIOGPU *g,
             return true;
         }
 
+        if (!res->blob) {
+            return false;
+        }
         data = res->blob;
     } else {
         data = (uint8_t *)pixman_image_get_data(res->image);
     }
 
     /* create a surface for this scanout */
-    if ((res->blob && !console_has_gl(scanout->con)) ||
+    if ((res->blob_size && !console_has_gl(scanout->con)) ||
         !scanout->ds ||
         surface_data(scanout->ds) != data + fb->offset ||
         scanout->width != r->width ||
@@ -901,7 +904,7 @@ void virtio_gpu_cleanup_mapping(VirtIOGPU *g,
     g_free(res->addrs);
     res->addrs = NULL;
 
-    if (res->blob) {
+    if (res->blob_size) {
         virtio_gpu_fini_udmabuf(res);
     }
 }
Signature · 1 line
2.50.1
#5 lore raw

Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob resources

Quoted 6 lines · Vivek Kasireddy

The res->blob pointer may not be valid (non-NULL) for some blobs where the backing storage is not memfd based. Therefore, we cannot use it to determine if a resource is a blob or not. Instead, we could use res->blob_size to make this determination as it is non-zero for blob resources regardless of where their backing storage is located.

I think this patch is no longer necessary since now you add code to mmap() VFIO storage with "[PATCH v1 7/7] virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO devices".

#6 lore raw

RE: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob resources

Hi Akihiko,

Quoted 11 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob resources

Quoted 6 lines · Vivek Kasireddy

The res->blob pointer may not be valid (non-NULL) for some blobs where the backing storage is not memfd based. Therefore, we cannot use it to determine if a resource is a blob or not. Instead, we could use res->blob_size to make this determination as it is non-zero for blob resources regardless of where their backing storage is located.

Quoted 3 lines

I think this patch is no longer necessary since now you add code to mmap() VFIO storage with "[PATCH v1 7/7] virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO devices".

Right, but given that mmap() can still fail for various reasons and this use-case can work as long as dmabuf creation succeeds, I think it makes sense to not rely on res->blob to determine if a resource is blob or not.

Thanks,
Vivek

#7 lore raw

Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob resources

Quoted 13 lines · Vivek

Hi Akihiko,

Quoted 11 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob resources

Quoted 6 lines · Vivek Kasireddy

The res->blob pointer may not be valid (non-NULL) for some blobs where the backing storage is not memfd based. Therefore, we cannot use it to determine if a resource is a blob or not. Instead, we could use res->blob_size to make this determination as it is non-zero for blob resources regardless of where their backing storage is located.

Quoted 6 lines
Quoted 3 lines

I think this patch is no longer necessary since now you add code to mmap() VFIO storage with "[PATCH v1 7/7] virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO devices".

Right, but given that mmap() can still fail for various reasons and this use-case can work as long as dmabuf creation succeeds, I think it makes sense to not rely on res->blob to determine if a resource is blob or not.

I think the code will be simpler by making resource creation fail when mmap() fails, and I am concerned that the guest may mulfunction with such an incomplete resource.

To motivate the proposed patch, there should be a use-case that requires to have a resource without mmap(), not one that "can work" a resource without mmap(). It is extraneous complexity otherwise.

Such a use case should be explained in the patch message and perhaps also with a comment in the code. The current patch message needs an update as it sounds like it is unnecessary when theere is code to mmap() VFIO-based backing storage, which this series has already gained.

Regards,
Akihiko Odaki

#8 lore raw

RE: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob resources

Hi Akihiko,

Quoted 19 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob resources

Quoted 2 lines
Quoted 2 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify

blob

Quoted 13 lines
Quoted 13 lines

resources

Quoted 6 lines · Vivek Kasireddy

The res->blob pointer may not be valid (non-NULL) for some blobs where the backing storage is not memfd based. Therefore, we cannot use it to determine if a resource is a blob or not. Instead, we could use res->blob_size to make this determination as it is non-zero for blob resources regardless of where their backing storage is located.

I think this patch is no longer necessary since now you add code to mmap() VFIO storage with "[PATCH v1 7/7] virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO devices".

Quoted 7 lines
Quoted 3 lines

Right, but given that mmap() can still fail for various reasons and this use-case can work as long as dmabuf creation succeeds, I think it makes sense to not rely on res->blob to determine if a resource is blob or not.

I think the code will be simpler by making resource creation fail when mmap() fails, and I am concerned that the guest may mulfunction with such an incomplete resource.

AFAICT, mmap() is a slow, optional path except for the cursor (which needs further improvement). So, failing resource creation when mmap() fails does not seem like a good idea to me given the fact that supporting mmap() is considered optional for dmabuf providers. And, even with vfio, mmap() can be blocked for various reasons by the kernel driver IIUC.

Quoted 9 lines

To motivate the proposed patch, there should be a use-case that requires to have a resource without mmap(), not one that "can work" a resource without mmap(). It is extraneous complexity otherwise.

Such a use case should be explained in the patch message and perhaps also with a comment in the code. The current patch message needs an update as it sounds like it is unnecessary when theere is code to mmap() VFIO-based backing storage, which this series has already gained.

Although VFIO supports mmap(), it is not guaranteed to work in all cases and with a different dmabuf provider (in the future), it may not be possible at all.

Thanks,
Vivek

Quoted 3 lines

Regards,
Akihiko Odaki

#9 lore raw

Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob resources

Quoted 25 lines · Vivek

Hi Akihiko,

Quoted 23 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob resources

Quoted 2 lines
Quoted 2 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify

blob

Quoted 16 lines
Quoted 10 lines

resources

Quoted 6 lines · Vivek Kasireddy

The res->blob pointer may not be valid (non-NULL) for some blobs where the backing storage is not memfd based. Therefore, we cannot use it to determine if a resource is a blob or not. Instead, we could use res->blob_size to make this determination as it is non-zero for blob resources regardless of where their backing storage is located.

Quoted 3 lines

I think this patch is no longer necessary since now you add code to mmap() VFIO storage with "[PATCH v1 7/7] virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO devices".

Right, but given that mmap() can still fail for various reasons and this use-case can work as long as dmabuf creation succeeds, I think it makes sense to not rely on res->blob to determine if a resource is blob or not.

Quoted 8 lines
Quoted 3 lines

I think the code will be simpler by making resource creation fail when mmap() fails, and I am concerned that the guest may mulfunction with such an incomplete resource.

AFAICT, mmap() is a slow, optional path except for the cursor (which needs further improvement). So, failing resource creation when mmap() fails does not seem like a good idea to me given the fact that supporting mmap() is considered optional for dmabuf providers. And, even with vfio, mmap() can be blocked for various reasons by the kernel driver IIUC.

Being slow doesn't matter, and it is not just for the cursor, but there are several other functions modified; I guess the resulting resource will be unusable except for accelerations like virgl.

Quoted 6 lines
Quoted 6 lines

To motivate the proposed patch, there should be a use-case that requires to have a resource without mmap(), not one that "can work" a resource without mmap(). It is extraneous complexity otherwise.

Quoted 7 lines
Quoted 4 lines

Such a use case should be explained in the patch message and perhaps also with a comment in the code. The current patch message needs an update as it sounds like it is unnecessary when theere is code to mmap() VFIO-based backing storage, which this series has already gained.

Although VFIO supports mmap(), it is not guaranteed to work in all cases and with a different dmabuf provider (in the future), it may not be possible at all.

The fact that mmap() is being optional for DMA-BUF and VFIO is insufficient, but what matters here is whether a DMA-BUF that lacks mmap() is usable for graphics.

Reading the cover letter, I suppose you are importing a mmap-incapable DMA-BUF exported by a dGPU, and the imported DMA-BUF is used with virgl or something. Explaining the use case will show that there is a mmap-incapable DMA-BUF usable for graphics.

Regards,
Akihiko Odaki

#10 lore raw

Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob resources

Quoted 26 lines · Akihiko Odaki
Quoted 25 lines · Vivek

Hi Akihiko,

Quoted 23 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob resources

Quoted 2 lines
Quoted 2 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify

blob

Quoted 16 lines
Quoted 10 lines

resources

Quoted 6 lines · Vivek Kasireddy

The res->blob pointer may not be valid (non-NULL) for some blobs where the backing storage is not memfd based. Therefore, we cannot use it to determine if a resource is a blob or not. Instead, we could use res->blob_size to make this determination as it is non-zero for blob resources regardless of where their backing storage is located.

Quoted 3 lines

I think this patch is no longer necessary since now you add code to mmap() VFIO storage with "[PATCH v1 7/7] virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO devices".

Right, but given that mmap() can still fail for various reasons and this use-case can work as long as dmabuf creation succeeds, I think it makes sense to not rely on res->blob to determine if a resource is blob or not.

Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

I think the code will be simpler by making resource creation fail when mmap() fails, and I am concerned that the guest may mulfunction with such an incomplete resource.

AFAICT, mmap() is a slow, optional path except for the cursor (which needs further improvement). So, failing resource creation when mmap() fails does not seem like a good idea to me given the fact that supporting mmap() is considered optional for dmabuf providers. And, even with vfio, mmap() can be blocked for various reasons by the kernel driver IIUC.

Reviewing the code again, I don't think mmap() can fail with the current version of this series.

udmabuf obviously always supports mmap().

For VFIO, checking memory_region_is_ram_device() ensures that VFIO supports mmap(); memory_region_init_ram_device_ptr() is called from vfio_region_mmap(), which is only called when VFIO supports mmap().

The use of ram_device needs to be removed if it is really wanted to create resources with VFIO devices lacking mmap(). This patch is unnecessary otherwise.

Quoted 23 lines

Being slow doesn't matter, and it is not just for the cursor, but there are several other functions modified; I guess the resulting resource will be unusable except for accelerations like virgl.

Quoted 6 lines
Quoted 6 lines

To motivate the proposed patch, there should be a use-case that requires to have a resource without mmap(), not one that "can work" a resource without mmap(). It is extraneous complexity otherwise.

Quoted 7 lines
Quoted 4 lines

Such a use case should be explained in the patch message and perhaps also with a comment in the code. The current patch message needs an update as it sounds like it is unnecessary when theere is code to mmap() VFIO-based backing storage, which this series has already gained.

Although VFIO supports mmap(), it is not guaranteed to work in all cases and with a different dmabuf provider (in the future), it may not be possible at all.

The fact that mmap() is being optional for DMA-BUF and VFIO is insufficient, but what matters here is whether a DMA-BUF that lacks mmap() is usable for graphics.

Reading the cover letter, I suppose you are importing a mmap-incapable DMA-BUF exported by a dGPU, and the imported DMA-BUF is used with virgl or something. Explaining the use case will show that there is a mmap- incapable DMA-BUF usable for graphics.

Regards,
Akihiko Odaki

#11 lore raw

RE: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob resources

Hi Akihiko,

Quoted 45 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob resources

Quoted 33 lines · Akihiko Odaki
Quoted 32 lines · Vivek

Hi Akihiko,

Quoted 30 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob
resources

Quoted 3 lines
Quoted 3 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify

blob

Quoted 10 lines
Quoted 10 lines

resources

Quoted 6 lines · Vivek Kasireddy

The res->blob pointer may not be valid (non-NULL) for some blobs where the backing storage is not memfd based. Therefore, we cannot use it to determine if a resource is a blob or not. Instead, we could use res->blob_size to make this determination as it is non-zero for blob resources regardless of where their backing storage is located.

Quoted 8 lines
Quoted 3 lines

I think this patch is no longer necessary since now you add code to mmap() VFIO storage with "[PATCH v1 7/7] virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO devices".

Right, but given that mmap() can still fail for various reasons and this
use-case can work as long as dmabuf creation succeeds, I think it makes sense to not rely on res->blob to determine if a resource is blob or not.

I think the code will be simpler by making resource creation fail when mmap() fails, and I am concerned that the guest may mulfunction with such an incomplete resource.

Quoted 7 lines
Quoted 7 lines

AFAICT, mmap() is a slow, optional path except for the cursor (which needs
further improvement). So, failing resource creation when mmap() fails does not seem like a good idea to me given the fact that supporting mmap()
is considered optional for dmabuf providers. And, even with vfio, mmap() can be blocked for various reasons by the kernel driver IIUC.

Quoted 8 lines

Reviewing the code again, I don't think mmap() can fail with the current version of this series.

udmabuf obviously always supports mmap().

For VFIO, checking memory_region_is_ram_device() ensures that VFIO supports mmap(); memory_region_init_ram_device_ptr() is called from vfio_region_mmap(), which is only called when VFIO supports mmap().

My point is not whether a dmabuf provider provides support for mmap() or not but about the fact that mmap() can fail (for various reasons because it is not a guarantee) making res->blob NULL. But we are incorrectly using res->blob pointer to determine whether a resource is a blob (and usable) or not which doesn't make sense because even if res->blob is NULL, the resource is still valid and usable via the dmabuf fd, which is the preferred, accelerated path.

Thanks,
Vivek

Quoted 36 lines

The use of ram_device needs to be removed if it is really wanted to create resources with VFIO devices lacking mmap(). This patch is unnecessary otherwise.

Quoted 31 lines

Being slow doesn't matter, and it is not just for the cursor, but there are several other functions modified; I guess the resulting resource will be unusable except for accelerations like virgl.

Quoted 6 lines
Quoted 6 lines

To motivate the proposed patch, there should be a use-case that requires to have a resource without mmap(), not one that "can work" a resource without mmap(). It is extraneous complexity otherwise.

Quoted 8 lines
Quoted 4 lines

Such a use case should be explained in the patch message and perhaps also with a comment in the code. The current patch message needs an update as it sounds like it is unnecessary when theere is code to mmap() VFIO-based backing storage, which this series has already gained.

Although VFIO supports mmap(), it is not guaranteed to work in all cases and with a different dmabuf provider (in the future), it may not be possible
at all.

The fact that mmap() is being optional for DMA-BUF and VFIO is insufficient, but what matters here is whether a DMA-BUF that lacks mmap() is usable for graphics.

Reading the cover letter, I suppose you are importing a mmap-incapable DMA-BUF exported by a dGPU, and the imported DMA-BUF is used with virgl or something. Explaining the use case will show that there is a mmap- incapable DMA-BUF usable for graphics.

Regards,
Akihiko Odaki

#12 lore raw

Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob resources

Quoted 55 lines · Vivek

Hi Akihiko,

Quoted 45 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob resources

Quoted 33 lines · Akihiko Odaki
Quoted 32 lines · Vivek

Hi Akihiko,

Quoted 30 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob
resources

Quoted 3 lines
Quoted 3 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify

blob

Quoted 10 lines
Quoted 10 lines

resources

Quoted 6 lines · Vivek Kasireddy

The res->blob pointer may not be valid (non-NULL) for some blobs where the backing storage is not memfd based. Therefore, we cannot use it to determine if a resource is a blob or not. Instead, we could use res->blob_size to make this determination as it is non-zero for blob resources regardless of where their backing storage is located.

Quoted 8 lines
Quoted 3 lines

I think this patch is no longer necessary since now you add code to mmap() VFIO storage with "[PATCH v1 7/7] virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO devices".

Right, but given that mmap() can still fail for various reasons and this
use-case can work as long as dmabuf creation succeeds, I think it makes sense to not rely on res->blob to determine if a resource is blob or not.

I think the code will be simpler by making resource creation fail when mmap() fails, and I am concerned that the guest may mulfunction with such an incomplete resource.

Quoted 7 lines
Quoted 7 lines

AFAICT, mmap() is a slow, optional path except for the cursor (which needs
further improvement). So, failing resource creation when mmap() fails does not seem like a good idea to me given the fact that supporting mmap()
is considered optional for dmabuf providers. And, even with vfio, mmap() can be blocked for various reasons by the kernel driver IIUC.

Quoted 8 lines

Reviewing the code again, I don't think mmap() can fail with the current version of this series.

udmabuf obviously always supports mmap().

For VFIO, checking memory_region_is_ram_device() ensures that VFIO supports mmap(); memory_region_init_ram_device_ptr() is called from vfio_region_mmap(), which is only called when VFIO supports mmap().

Quoted 7 lines

My point is not whether a dmabuf provider provides support for mmap() or not but about the fact that mmap() can fail (for various reasons because it is not a guarantee) making res->blob NULL. But we are incorrectly using res->blob pointer to determine whether a resource is a blob (and usable) or not which doesn't make sense because even if res->blob is NULL, the resource is still valid and usable via the dmabuf fd, which is the preferred, accelerated path.

Failing to mmap something that is already mmap-ed to another address is very unrealistic and I can't really think of a possibility of such a failure aside bugs.

If this condition (a valid resource with a NULL res->blob) could only happen due to a bug, then, in my opinion, marking such a resource as invalid is actually a more defensive and desirable approach. If a core operation like mmap fails unexpectedly on a resource that should support it, continuing to treat it as functional could lead to worse, harder-to-debug failures later in the pipeline. And I hope it can be done without complicating the code (or it even simplifies).

Regards,
Akihiko Odaki

#13 lore raw

RE: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob resources

Hi Akihiko,

Quoted 64 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob resources

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob
resources

Quoted 1 line
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify

blob

Quoted 5 lines
Quoted 5 lines

resources

Quoted 2 lines · Vivek Kasireddy

The res->blob pointer may not be valid (non-NULL) for some blobs where the backing storage is not memfd based. Therefore, we

cannot

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

use it to determine if a resource is a blob or not. Instead, we could use res->blob_size to make this determination as it is non-zero for blob resources regardless of where their backing storage is located.

I think this patch is no longer necessary since now you add code to mmap() VFIO storage with "[PATCH v1 7/7] virtio-gpu-udmabuf:

Create

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

dmabuf for blobs associated with VFIO devices".

Right, but given that mmap() can still fail for various reasons and this
use-case can work as long as dmabuf creation succeeds, I think it

makes

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

sense to not rely on res->blob to determine if a resource is blob or not.

I think the code will be simpler by making resource creation fail when mmap() fails, and I am concerned that the guest may mulfunction

with

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 1 line

such an incomplete resource.

AFAICT, mmap() is a slow, optional path except for the cursor (which needs
further improvement). So, failing resource creation when mmap() fails does not seem like a good idea to me given the fact that supporting mmap()
is considered optional for dmabuf providers. And, even with vfio,

mmap()

Quoted 5 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line

can be blocked for various reasons by the kernel driver IIUC.

Reviewing the code again, I don't think mmap() can fail with the current version of this series.

Quoted 7 lines
Quoted 5 lines

udmabuf obviously always supports mmap().

For VFIO, checking memory_region_is_ram_device() ensures that VFIO supports mmap(); memory_region_init_ram_device_ptr() is called from vfio_region_mmap(), which is only called when VFIO supports mmap().

My point is not whether a dmabuf provider provides support for mmap() or not but about the fact that mmap() can fail (for various reasons

because

Quoted 5 lines

it is not a guarantee) making res->blob NULL. But we are incorrectly using res->blob pointer to determine whether a resource is a blob (and usable) or not which doesn't make sense because even if res->blob is NULL, the resource is still valid and usable via the dmabuf fd, which is the preferred, accelerated path.

Quoted 3 lines

Failing to mmap something that is already mmap-ed to another address is very unrealistic and I can't really think of a possibility of such a failure aside bugs.

The fact that it is already mmap'd to another address would only be true for VFIO devices but as I mentioned previously, we cannot make such assumptions with other (future) dmabuf providers.

Quoted 5 lines

If this condition (a valid resource with a NULL res->blob) could only happen due to a bug, then, in my opinion, marking such a resource as invalid is actually a more defensive and desirable approach. If a core operation like mmap fails unexpectedly on a resource that should support

But mmap is not considered as a core operation for dmabuf. It is considered optional by dmabuf providers. For example, although very unlikely, it might be possible that support for mmap() can be removed from udmabuf driver driver for some reason. And, when this happens, the only adverse effect would be that gl=off would not work, which is not great but definitely not catastrophic.

Thanks,
Vivek

Quoted 6 lines

it, continuing to treat it as functional could lead to worse, harder-to-debug failures later in the pipeline. And I hope it can be done without complicating the code (or it even simplifies).

Regards,
Akihiko Odaki

#14 lore raw

Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob resources

Quoted 66 lines · Vivek

Hi Akihiko,

Quoted 64 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob resources

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob
resources

Quoted 1 line
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify

blob

Quoted 5 lines
Quoted 5 lines

resources

Quoted 2 lines · Vivek Kasireddy

The res->blob pointer may not be valid (non-NULL) for some blobs where the backing storage is not memfd based. Therefore, we

cannot

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

use it to determine if a resource is a blob or not. Instead, we could use res->blob_size to make this determination as it is non-zero for blob resources regardless of where their backing storage is located.

I think this patch is no longer necessary since now you add code to mmap() VFIO storage with "[PATCH v1 7/7] virtio-gpu-udmabuf:

Create

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

dmabuf for blobs associated with VFIO devices".

Right, but given that mmap() can still fail for various reasons and this
use-case can work as long as dmabuf creation succeeds, I think it

makes

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

sense to not rely on res->blob to determine if a resource is blob or not.

I think the code will be simpler by making resource creation fail when mmap() fails, and I am concerned that the guest may mulfunction

with

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 1 line

such an incomplete resource.

AFAICT, mmap() is a slow, optional path except for the cursor (which needs
further improvement). So, failing resource creation when mmap() fails does not seem like a good idea to me given the fact that supporting mmap()
is considered optional for dmabuf providers. And, even with vfio,

mmap()

Quoted 5 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line

can be blocked for various reasons by the kernel driver IIUC.

Reviewing the code again, I don't think mmap() can fail with the current version of this series.

Quoted 7 lines
Quoted 5 lines

udmabuf obviously always supports mmap().

For VFIO, checking memory_region_is_ram_device() ensures that VFIO supports mmap(); memory_region_init_ram_device_ptr() is called from vfio_region_mmap(), which is only called when VFIO supports mmap().

My point is not whether a dmabuf provider provides support for mmap() or not but about the fact that mmap() can fail (for various reasons

because

Quoted 5 lines

it is not a guarantee) making res->blob NULL. But we are incorrectly using res->blob pointer to determine whether a resource is a blob (and usable) or not which doesn't make sense because even if res->blob is NULL, the resource is still valid and usable via the dmabuf fd, which is the preferred, accelerated path.

Quoted 6 lines
Quoted 3 lines

Failing to mmap something that is already mmap-ed to another address is very unrealistic and I can't really think of a possibility of such a failure aside bugs.

The fact that it is already mmap'd to another address would only be true for VFIO devices but as I mentioned previously, we cannot make such assumptions with other (future) dmabuf providers.

It is true for udmabuf, though the memfds that back udmabuf are directly mapped instead; I don't think the indirection of udmabuf makes any difference.

If it's only for future DMA-BUF exporter, it is better to make the change when the exporter is actually added, or we are adding code that cannot be tested right now and may or may not work when such an exporter is added.

Quoted 11 lines
Quoted 6 lines

If this condition (a valid resource with a NULL res->blob) could only happen due to a bug, then, in my opinion, marking such a resource as invalid is actually a more defensive and desirable approach. If a core operation like mmap fails unexpectedly on a resource that should support

But mmap is not considered as a core operation for dmabuf. It is considered optional by dmabuf providers. For example, although very unlikely, it might be possible that support for mmap() can be removed from udmabuf driver driver for some reason. And, when this happens, the only adverse effect would be that gl=off would not work, which is not great but definitely not catastrophic.

We should be able to safely assume it never happens due to the "no regressions" rule of Linux. If a userspace program breaks due to a UAPI change, the UAPI change is a breaking change, which kernel developers carefully avoid.

Existing QEMU versions will break if such a change happens. Perhaps the no regressions policy may be exempted if the relevant userspace program is so broken that it depends on something never meant to be stable, but it is not the case.

Regards,
Akihiko Odaki

#15 lore raw

RE: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob resources

Hi Akihiko,

Quoted 93 lines
Quoted 10 lines
Quoted 10 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob resources

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob
resources

Quoted 1 line
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify

blob

Quoted 4 lines
Quoted 4 lines

resources

Quoted 1 line · Vivek Kasireddy

The res->blob pointer may not be valid (non-NULL) for some

blobs

Quoted 48 lines
Quoted 48 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

where the backing storage is not memfd based. Therefore, we

cannot

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

use it to determine if a resource is a blob or not. Instead, we could use res->blob_size to make this determination as it is non-zero for blob resources regardless of where their backing storage is located.

I think this patch is no longer necessary since now you add code to mmap() VFIO storage with "[PATCH v1 7/7] virtio-gpu-udmabuf:

Create

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

dmabuf for blobs associated with VFIO devices".

Right, but given that mmap() can still fail for various reasons and this
use-case can work as long as dmabuf creation succeeds, I think it

makes

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

sense to not rely on res->blob to determine if a resource is blob or not.

I think the code will be simpler by making resource creation fail when mmap() fails, and I am concerned that the guest may mulfunction

with

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 1 line

such an incomplete resource.

AFAICT, mmap() is a slow, optional path except for the cursor (which needs
further improvement). So, failing resource creation when mmap() fails does not seem like a good idea to me given the fact that supporting mmap()
is considered optional for dmabuf providers. And, even with vfio,

mmap()

Quoted 5 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line

can be blocked for various reasons by the kernel driver IIUC.

Reviewing the code again, I don't think mmap() can fail with the current version of this series.

Quoted 7 lines
Quoted 5 lines

udmabuf obviously always supports mmap().

For VFIO, checking memory_region_is_ram_device() ensures that VFIO supports mmap(); memory_region_init_ram_device_ptr() is called from vfio_region_mmap(), which is only called when VFIO supports mmap().

My point is not whether a dmabuf provider provides support for mmap() or not but about the fact that mmap() can fail (for various reasons

because

Quoted 5 lines

it is not a guarantee) making res->blob NULL. But we are incorrectly using res->blob pointer to determine whether a resource is a blob (and usable) or not which doesn't make sense because even if res->blob is NULL, the resource is still valid and usable via the dmabuf fd, which is the preferred, accelerated path.

Quoted 5 lines
Quoted 3 lines

Failing to mmap something that is already mmap-ed to another address is very unrealistic and I can't really think of a possibility of such a failure aside bugs.

The fact that it is already mmap'd to another address would only be true for VFIO devices but as I mentioned previously, we cannot make such

assumptions

Quoted 1 line

with other (future) dmabuf providers.

It is true for udmabuf, though the memfds that back udmabuf are directly mapped instead; I don't think the indirection of udmabuf makes any difference.

If it's only for future DMA-BUF exporter, it is better to make the change when the exporter is actually added, or we are adding code that cannot be tested right now and may or may not work when such an exporter is added.

Quoted 10 lines
Quoted 6 lines

If this condition (a valid resource with a NULL res->blob) could only happen due to a bug, then, in my opinion, marking such a resource as invalid is actually a more defensive and desirable approach. If a core operation like mmap fails unexpectedly on a resource that should support

But mmap is not considered as a core operation for dmabuf. It is considered optional by dmabuf providers. For example, although very unlikely, it might be possible that support for mmap() can be removed from udmabuf driver driver for some reason. And, when this happens, the only adverse effect

Quoted 6 lines

would

Quoted 1 line

be that gl=off would not work, which is not great but definitely not

catastrophic.

We should be able to safely assume it never happens due to the "no regressions" rule of Linux. If a userspace program breaks due to a UAPI

I help maintain the udmabuf driver in the kernel and AFAICT, that rule does not apply here because udmabuf driver providing support for mmap() cannot be considered UAPI because it is not providing any direct (user visible) interface to invoke mmap() as described here:
https://docs.kernel.org/admin-guide/reporting-regressions.html#what-is-a-regression-and-what-is-the-no-regressions-rule

Quoted 4 lines

change, the UAPI change is a breaking change, which kernel developers carefully avoid.

Existing QEMU versions will break if such a change happens. Perhaps the

Qemu should not have assumed that udmabuf driver (or any dmabuf provider) would always support mmap(), because for dmabuf providers like udmabuf, supporting mmap() is optional as mentioned here:
https://elixir.bootlin.com/linux/v6.17/source/include/linux/dma-buf.h#L269

And, as mentioned previously, Qemu would not break if a dmabuf provider does not support mmap() because the preferred, fd based fast path (gl=on) would still be available.

Thanks,
Vivek

Quoted 6 lines

no regressions policy may be exempted if the relevant userspace program is so broken that it depends on something never meant to be stable, but it is not the case.

Regards,
Akihiko Odaki

#16 lore raw

Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob resources

Quoted 99 lines · Vivek

Hi Akihiko,

Quoted 97 lines
Quoted 10 lines
Quoted 10 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob resources

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob
resources

Quoted 1 line
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify

blob

Quoted 4 lines
Quoted 4 lines

resources

Quoted 1 line · Vivek Kasireddy

The res->blob pointer may not be valid (non-NULL) for some

blobs

Quoted 48 lines
Quoted 48 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

where the backing storage is not memfd based. Therefore, we

cannot

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

use it to determine if a resource is a blob or not. Instead, we could use res->blob_size to make this determination as it is non-zero for blob resources regardless of where their backing storage is located.

I think this patch is no longer necessary since now you add code to mmap() VFIO storage with "[PATCH v1 7/7] virtio-gpu-udmabuf:

Create

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

dmabuf for blobs associated with VFIO devices".

Right, but given that mmap() can still fail for various reasons and this
use-case can work as long as dmabuf creation succeeds, I think it

makes

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

sense to not rely on res->blob to determine if a resource is blob or not.

I think the code will be simpler by making resource creation fail when mmap() fails, and I am concerned that the guest may mulfunction

with

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 1 line

such an incomplete resource.

AFAICT, mmap() is a slow, optional path except for the cursor (which needs
further improvement). So, failing resource creation when mmap() fails does not seem like a good idea to me given the fact that supporting mmap()
is considered optional for dmabuf providers. And, even with vfio,

mmap()

Quoted 5 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line

can be blocked for various reasons by the kernel driver IIUC.

Reviewing the code again, I don't think mmap() can fail with the current version of this series.

Quoted 7 lines
Quoted 5 lines

udmabuf obviously always supports mmap().

For VFIO, checking memory_region_is_ram_device() ensures that VFIO supports mmap(); memory_region_init_ram_device_ptr() is called from vfio_region_mmap(), which is only called when VFIO supports mmap().

My point is not whether a dmabuf provider provides support for mmap() or not but about the fact that mmap() can fail (for various reasons

because

Quoted 5 lines

it is not a guarantee) making res->blob NULL. But we are incorrectly using res->blob pointer to determine whether a resource is a blob (and usable) or not which doesn't make sense because even if res->blob is NULL, the resource is still valid and usable via the dmabuf fd, which is the preferred, accelerated path.

Quoted 5 lines
Quoted 3 lines

Failing to mmap something that is already mmap-ed to another address is very unrealistic and I can't really think of a possibility of such a failure aside bugs.

The fact that it is already mmap'd to another address would only be true for VFIO devices but as I mentioned previously, we cannot make such

assumptions

Quoted 1 line

with other (future) dmabuf providers.

It is true for udmabuf, though the memfds that back udmabuf are directly mapped instead; I don't think the indirection of udmabuf makes any difference.

If it's only for future DMA-BUF exporter, it is better to make the change when the exporter is actually added, or we are adding code that cannot be tested right now and may or may not work when such an exporter is added.

Quoted 10 lines
Quoted 6 lines

If this condition (a valid resource with a NULL res->blob) could only happen due to a bug, then, in my opinion, marking such a resource as invalid is actually a more defensive and desirable approach. If a core operation like mmap fails unexpectedly on a resource that should support

But mmap is not considered as a core operation for dmabuf. It is considered optional by dmabuf providers. For example, although very unlikely, it might be possible that support for mmap() can be removed from udmabuf driver driver for some reason. And, when this happens, the only adverse effect

would

Quoted 1 line

be that gl=off would not work, which is not great but definitely not

catastrophic.

Quoted 7 lines
Quoted 2 lines

We should be able to safely assume it never happens due to the "no regressions" rule of Linux. If a userspace program breaks due to a UAPI

I help maintain the udmabuf driver in the kernel and AFAICT, that rule does not apply here because udmabuf driver providing support for mmap() cannot be considered UAPI because it is not providing any direct (user visible) interface to invoke mmap() as described here:
https://docs.kernel.org/admin-guide/reporting-regressions.html#what-is-a-regression-and-what-is-the-no-regressions-rule

I suppose that the result of open("/dev/udmabuf", O_RDWR) and the UDMABUF_CREATE_LIST ioctl is always compatible with mmap(). Not sure what you mean by "direct" interface, but they are all plain userspace interfaces.

I suggest asking people maintaining the udmabuf and the DMA-BUF infrastructure if you know them.

Quoted 5 lines
Quoted 5 lines

change, the UAPI change is a breaking change, which kernel developers carefully avoid.

Existing QEMU versions will break if such a change happens. Perhaps the

Quoted 8 lines

Qemu should not have assumed that udmabuf driver (or any dmabuf provider) would always support mmap(), because for dmabuf providers like udmabuf, supporting mmap() is optional as mentioned here:
https://elixir.bootlin.com/linux/v6.17/source/include/linux/dma-buf.h#L269

And, as mentioned previously, Qemu would not break if a dmabuf provider does not support mmap() because the preferred, fd based fast path (gl=on) would still be available.

Just clarification; QEMU will not break in that case only if this patch is applied, am I right?

Regards,
Akihiko Odaki

#17 lore raw

RE: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob resources

Hi Akihiko,

Quoted 139 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to

identify

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

blob resources

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

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob
resources

Quoted 2 lines
Quoted 2 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob

to

Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 1 line
Quoted 1 line

identify

blob

Quoted 4 lines
Quoted 4 lines

resources

Quoted 1 line · Vivek Kasireddy

The res->blob pointer may not be valid (non-NULL) for some

blobs

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

where the backing storage is not memfd based. Therefore,

we

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

cannot

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

use it to determine if a resource is a blob or not. Instead, we
could use res->blob_size to make this determination as it is
non-zero for blob resources regardless of where their backing
storage is located.

I think this patch is no longer necessary since now you add

code to

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

mmap() VFIO storage with "[PATCH v1 7/7] virtio-gpu-

udmabuf:

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

Create

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

dmabuf for blobs associated with VFIO devices".

Right, but given that mmap() can still fail for various reasons

and

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

this
use-case can work as long as dmabuf creation succeeds, I think

it

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

makes

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

sense to not rely on res->blob to determine if a resource is blob

or

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

not.

I think the code will be simpler by making resource creation fail

when

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

mmap() fails, and I am concerned that the guest may

mulfunction

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

with

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

such an incomplete resource.

AFAICT, mmap() is a slow, optional path except for the cursor

(which

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

needs
further improvement). So, failing resource creation when mmap()

fails

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

does not seem like a good idea to me given the fact that

supporting

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

mmap()
is considered optional for dmabuf providers. And, even with vfio,

mmap()

Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line

can be blocked for various reasons by the kernel driver IIUC.

Reviewing the code again, I don't think mmap() can fail with the

current

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

version of this series.

udmabuf obviously always supports mmap().

For VFIO, checking memory_region_is_ram_device() ensures that

VFIO

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

supports mmap(); memory_region_init_ram_device_ptr() is called

from

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

vfio_region_mmap(), which is only called when VFIO supports

mmap().

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

My point is not whether a dmabuf provider provides support for

mmap()

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

or not but about the fact that mmap() can fail (for various reasons

because

Quoted 1 line

it is not a guarantee) making res->blob NULL. But we are incorrectly

using

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

res->blob pointer to determine whether a resource is a blob (and

usable)

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

or not which doesn't make sense because even if res->blob is NULL,

the

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

resource is still valid and usable via the dmabuf fd, which is the

preferred,

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

accelerated path.

Failing to mmap something that is already mmap-ed to another

address is

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 2 lines

very unrealistic and I can't really think of a possibility of such a failure aside bugs.

The fact that it is already mmap'd to another address would only be

true for

Quoted 5 lines
Quoted 5 lines
Quoted 1 line

VFIO devices but as I mentioned previously, we cannot make such

assumptions

Quoted 1 line

with other (future) dmabuf providers.

It is true for udmabuf, though the memfds that back udmabuf are

directly

Quoted 6 lines
Quoted 6 lines

mapped instead; I don't think the indirection of udmabuf makes any difference.

If it's only for future DMA-BUF exporter, it is better to make the change when the exporter is actually added, or we are adding code that cannot be tested right now and may or may not work when such an

exporter

Quoted 8 lines
Quoted 8 lines

is added.

Quoted 6 lines
Quoted 6 lines

If this condition (a valid resource with a NULL res->blob) could only happen due to a bug, then, in my opinion, marking such a resource as invalid is actually a more defensive and desirable approach. If a core operation like mmap fails unexpectedly on a resource that should

support

Quoted 1 line
Quoted 1 line
Quoted 1 line

But mmap is not considered as a core operation for dmabuf. It is

considered

Quoted 1 line
Quoted 1 line
Quoted 1 line

optional by dmabuf providers. For example, although very unlikely, it

might

Quoted 1 line
Quoted 1 line
Quoted 1 line

be possible that support for mmap() can be removed from udmabuf

driver

Quoted 8 lines
Quoted 7 lines
Quoted 1 line

driver for some reason. And, when this happens, the only adverse effect

would

Quoted 1 line

be that gl=off would not work, which is not great but definitely not

catastrophic.

We should be able to safely assume it never happens due to the "no regressions" rule of Linux. If a userspace program breaks due to a UAPI

I help maintain the udmabuf driver in the kernel and AFAICT, that rule

does

Quoted 1 line

not apply here because udmabuf driver providing support for mmap()

cannot

Quoted 1 line

be considered UAPI because it is not providing any direct (user visible)

interface

Quoted 2 lines

to invoke mmap() as described here:
https://docs.kernel.org/admin-guide/reporting-regressions.html#what-is-

a-regression-and-what-is-the-no-regressions-rule

Quoted 7 lines

I suppose that the result of open("/dev/udmabuf", O_RDWR) and the UDMABUF_CREATE_LIST ioctl is always compatible with mmap(). Not sure what you mean by "direct" interface, but they are all plain userspace interfaces.

I suggest asking people maintaining the udmabuf and the DMA-BUF infrastructure if you know them.

I am one of the two maintainers of udmabuf driver and I am also involved in the discussions with dmabuf maintainers regarding upstreaming of vfio-pci dmabuf feature in the kernel. And, based on these interactions, it is clear that mmap() is completely optional for dmabuf providers and userspace should never make any assumptions about whether mmap() (for dmabuf) works or not. Here is one reference:
https://lore.kernel.org/dri-devel/[email protected]/

Quoted 14 lines
Quoted 7 lines
Quoted 6 lines

change, the UAPI change is a breaking change, which kernel developers carefully avoid.

Existing QEMU versions will break if such a change happens. Perhaps the

Qemu should not have assumed that udmabuf driver (or any dmabuf

provider)

Quoted 1 line

would always support mmap(), because for dmabuf providers like

udmabuf,

Quoted 2 lines

supporting mmap() is optional as mentioned here:
https://elixir.bootlin.com/linux/v6.17/source/include/linux/dma-

buf.h#L269

Quoted 1 line
Quoted 7 lines
Quoted 1 line

And, as mentioned previously, Qemu would not break if a dmabuf

provider

Quoted 1 line

does not support mmap() because the preferred, fd based fast path

(gl=on)

Quoted 1 line

would still be available.

Just clarification; QEMU will not break in that case only if this patch is applied, am I right?

Yes, that's right. It will not break after this patch is applied because we would no longer rely on res->blob pointer (which is a basically a return value of mmap) to identify or determine the usability/validity of blob resources.

Thanks,
Vivek

Quoted 3 lines

Regards,
Akihiko Odaki

#18 lore raw

Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob resources

Quoted 148 lines · Vivek

Hi Akihiko,

Quoted 139 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to

identify

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

blob resources

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

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob
resources

Quoted 2 lines
Quoted 2 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob

to

Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 1 line
Quoted 1 line

identify

blob

Quoted 4 lines
Quoted 4 lines

resources

On 2025/10/04 8:35, Vivek Kasireddy wrote:
The res->blob pointer may not be valid (non-NULL) for some

blobs

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

where the backing storage is not memfd based. Therefore,

we

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

cannot

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

use it to determine if a resource is a blob or not. Instead, we
could use res->blob_size to make this determination as it is
non-zero for blob resources regardless of where their backing
storage is located.

I think this patch is no longer necessary since now you add

code to

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

mmap() VFIO storage with "[PATCH v1 7/7] virtio-gpu-

udmabuf:

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

Create

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

dmabuf for blobs associated with VFIO devices".

Right, but given that mmap() can still fail for various reasons

and

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

this
use-case can work as long as dmabuf creation succeeds, I think

it

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

makes

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

sense to not rely on res->blob to determine if a resource is blob

or

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

not.

I think the code will be simpler by making resource creation fail

when

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

mmap() fails, and I am concerned that the guest may

mulfunction

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

with

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

such an incomplete resource.

AFAICT, mmap() is a slow, optional path except for the cursor

(which

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

needs
further improvement). So, failing resource creation when mmap()

fails

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

does not seem like a good idea to me given the fact that

supporting

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

mmap()
is considered optional for dmabuf providers. And, even with vfio,

mmap()

Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line

can be blocked for various reasons by the kernel driver IIUC.

Reviewing the code again, I don't think mmap() can fail with the

current

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

version of this series.

udmabuf obviously always supports mmap().

For VFIO, checking memory_region_is_ram_device() ensures that

VFIO

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

supports mmap(); memory_region_init_ram_device_ptr() is called

from

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

vfio_region_mmap(), which is only called when VFIO supports

mmap().

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

My point is not whether a dmabuf provider provides support for

mmap()

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

or not but about the fact that mmap() can fail (for various reasons

because

Quoted 1 line

it is not a guarantee) making res->blob NULL. But we are incorrectly

using

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

res->blob pointer to determine whether a resource is a blob (and

usable)

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

or not which doesn't make sense because even if res->blob is NULL,

the

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

resource is still valid and usable via the dmabuf fd, which is the

preferred,

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

accelerated path.

Failing to mmap something that is already mmap-ed to another

address is

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 2 lines

very unrealistic and I can't really think of a possibility of such a failure aside bugs.

The fact that it is already mmap'd to another address would only be

true for

Quoted 5 lines
Quoted 5 lines
Quoted 1 line

VFIO devices but as I mentioned previously, we cannot make such

assumptions

Quoted 1 line

with other (future) dmabuf providers.

It is true for udmabuf, though the memfds that back udmabuf are

directly

Quoted 6 lines
Quoted 6 lines

mapped instead; I don't think the indirection of udmabuf makes any difference.

If it's only for future DMA-BUF exporter, it is better to make the change when the exporter is actually added, or we are adding code that cannot be tested right now and may or may not work when such an

exporter

Quoted 8 lines
Quoted 8 lines

is added.

Quoted 6 lines
Quoted 6 lines

If this condition (a valid resource with a NULL res->blob) could only happen due to a bug, then, in my opinion, marking such a resource as invalid is actually a more defensive and desirable approach. If a core operation like mmap fails unexpectedly on a resource that should

support

Quoted 1 line
Quoted 1 line
Quoted 1 line

But mmap is not considered as a core operation for dmabuf. It is

considered

Quoted 1 line
Quoted 1 line
Quoted 1 line

optional by dmabuf providers. For example, although very unlikely, it

might

Quoted 1 line
Quoted 1 line
Quoted 1 line

be possible that support for mmap() can be removed from udmabuf

driver

Quoted 8 lines
Quoted 7 lines
Quoted 1 line

driver for some reason. And, when this happens, the only adverse effect

would

Quoted 1 line

be that gl=off would not work, which is not great but definitely not

catastrophic.

We should be able to safely assume it never happens due to the "no regressions" rule of Linux. If a userspace program breaks due to a UAPI

I help maintain the udmabuf driver in the kernel and AFAICT, that rule

does

Quoted 1 line

not apply here because udmabuf driver providing support for mmap()

cannot

Quoted 1 line

be considered UAPI because it is not providing any direct (user visible)

interface

Quoted 2 lines

to invoke mmap() as described here:
https://docs.kernel.org/admin-guide/reporting-regressions.html#what-is-

a-regression-and-what-is-the-no-regressions-rule

Quoted 7 lines

I suppose that the result of open("/dev/udmabuf", O_RDWR) and the UDMABUF_CREATE_LIST ioctl is always compatible with mmap(). Not sure what you mean by "direct" interface, but they are all plain userspace interfaces.

I suggest asking people maintaining the udmabuf and the DMA-BUF infrastructure if you know them.

Quoted 7 lines

I am one of the two maintainers of udmabuf driver and I am also involved in the discussions with dmabuf maintainers regarding upstreaming of vfio-pci dmabuf feature in the kernel. And, based on these interactions, it is clear that mmap() is completely optional for dmabuf providers and userspace should never make any assumptions about whether mmap() (for dmabuf) works or not. Here is one reference:
https://lore.kernel.org/dri-devel/[email protected]/

That adds nothing more than you have already told in the past discussion in this thread.

I started this thread because I found that the current implementation only exercises udmabuf and VFIO devices that is known to implement mmap(). On the other hand, the cited email discusses VFIO devices that are not known to implement mmap() or future DMA-BUF exporters in general. It says:

Quoted 4 lines

Just wanted to confirm that it's entirely legit to not implement dma-buf mmap. Same for the in-kernel vmap functions. Especially for really funny buffers like these it's just not a good idea, and the dma-buf interfaces are intentionally "everything is optional".

This doesn't apply to udmabuf and VFIO devices we are concerned with. udmabuf is obviously not "funny buffers", and can be vmapped. Besides, the udmabuf already implements mmap() and *removing* it can break userspace unlike a DMA-BUF exporter that never implemented mmap(), which was discussed in the cited email.

Speaking of VFIO devices, the code already checks it's mmapped by calling memory_region_is_ram_device() and yet this patch tries to allow blobs that cannot be mmapped, which is inconsistent.

So, the discussion should be summarized as the following two questions:
1. Can the kernel remove an existing mmap() implementation that is known to be depended by the userspace (udmabuf)?
2. Can the kernel let an mmap() implementation fail though it once worked (VFIO)?

The cited email doesn't answer either of them.

Regards,
Akihiko Odaki

#19 lore raw

RE: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob resources

Hi Akihiko,

Quoted 194 lines
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to

identify

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

blob resources

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

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob

to

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

identify blob
resources

Quoted 1 line
Quoted 9 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res-

blob

Quoted 7 lines

to

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 1 line

identify

blob

Quoted 4 lines

resources

On 2025/10/04 8:35, Vivek Kasireddy wrote:
The res->blob pointer may not be valid (non-NULL) for

some

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

blobs

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

where the backing storage is not memfd based. Therefore,

we

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

cannot

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

use it to determine if a resource is a blob or not. Instead,

we

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

could use res->blob_size to make this determination as it

is

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

non-zero for blob resources regardless of where their

backing

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

storage is located.

I think this patch is no longer necessary since now you add

code to

Quoted 8 lines
Quoted 8 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

mmap() VFIO storage with "[PATCH v1 7/7] virtio-gpu-

udmabuf:

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

Create

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

dmabuf for blobs associated with VFIO devices".
Right, but given that mmap() can still fail for various reasons

and

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

this
use-case can work as long as dmabuf creation succeeds, I

think

Quoted 3 lines
Quoted 3 lines

it

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

makes

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

sense to not rely on res->blob to determine if a resource is

blob

Quoted 4 lines
Quoted 4 lines

or

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

not.

I think the code will be simpler by making resource creation

fail

Quoted 9 lines
Quoted 9 lines

when

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

mmap() fails, and I am concerned that the guest may

mulfunction

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

with

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

such an incomplete resource.

AFAICT, mmap() is a slow, optional path except for the cursor

(which

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

needs
further improvement). So, failing resource creation when

mmap()

Quoted 5 lines
Quoted 5 lines

fails

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

does not seem like a good idea to me given the fact that

supporting

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

mmap()
is considered optional for dmabuf providers. And, even with

vfio,

Quoted 39 lines
Quoted 39 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

mmap()

Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line

can be blocked for various reasons by the kernel driver IIUC.

Reviewing the code again, I don't think mmap() can fail with the

current

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

version of this series.

udmabuf obviously always supports mmap().

For VFIO, checking memory_region_is_ram_device() ensures that

VFIO

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

supports mmap(); memory_region_init_ram_device_ptr() is called

from

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

vfio_region_mmap(), which is only called when VFIO supports

mmap().

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

My point is not whether a dmabuf provider provides support for

mmap()

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

or not but about the fact that mmap() can fail (for various reasons

because

Quoted 1 line

it is not a guarantee) making res->blob NULL. But we are incorrectly

using

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

res->blob pointer to determine whether a resource is a blob (and

usable)

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

or not which doesn't make sense because even if res->blob is NULL,

the

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

resource is still valid and usable via the dmabuf fd, which is the

preferred,

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

accelerated path.

Failing to mmap something that is already mmap-ed to another

address is

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 2 lines

very unrealistic and I can't really think of a possibility of such a failure aside bugs.

The fact that it is already mmap'd to another address would only be

true for

Quoted 4 lines
Quoted 4 lines
Quoted 1 line

VFIO devices but as I mentioned previously, we cannot make such

assumptions

Quoted 1 line

with other (future) dmabuf providers.

Quoted 7 lines
Quoted 7 lines
Quoted 1 line
Quoted 1 line

It is true for udmabuf, though the memfds that back udmabuf are

directly

Quoted 5 lines
Quoted 5 lines

mapped instead; I don't think the indirection of udmabuf makes any difference.

If it's only for future DMA-BUF exporter, it is better to make the change when the exporter is actually added, or we are adding code

that

Quoted 8 lines
Quoted 8 lines
Quoted 1 line
Quoted 1 line

cannot be tested right now and may or may not work when such an

exporter

Quoted 6 lines
Quoted 6 lines

is added.

Quoted 4 lines
Quoted 4 lines

If this condition (a valid resource with a NULL res->blob) could only happen due to a bug, then, in my opinion, marking such a resource

as

Quoted 10 lines
Quoted 10 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

invalid is actually a more defensive and desirable approach. If a core operation like mmap fails unexpectedly on a resource that should

support

Quoted 1 line
Quoted 1 line
Quoted 1 line

But mmap is not considered as a core operation for dmabuf. It is

considered

Quoted 1 line
Quoted 1 line
Quoted 1 line

optional by dmabuf providers. For example, although very unlikely, it

might

Quoted 1 line
Quoted 1 line
Quoted 1 line

be possible that support for mmap() can be removed from udmabuf

driver

Quoted 1 line
Quoted 1 line
Quoted 1 line

driver for some reason. And, when this happens, the only adverse

effect

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

would

Quoted 1 line

be that gl=off would not work, which is not great but definitely not

catastrophic.

We should be able to safely assume it never happens due to the "no regressions" rule of Linux. If a userspace program breaks due to a UAPI

Quoted 8 lines
Quoted 8 lines
Quoted 1 line

I help maintain the udmabuf driver in the kernel and AFAICT, that rule

does

Quoted 1 line

not apply here because udmabuf driver providing support for mmap()

cannot

Quoted 1 line

be considered UAPI because it is not providing any direct (user visible)

interface

Quoted 2 lines

to invoke mmap() as described here:
https://docs.kernel.org/admin-guide/reporting-regressions.html#what-

is-

Quoted 9 lines
Quoted 9 lines

a-regression-and-what-is-the-no-regressions-rule

I suppose that the result of open("/dev/udmabuf", O_RDWR) and the UDMABUF_CREATE_LIST ioctl is always compatible with mmap(). Not sure what you mean by "direct" interface, but they are all plain userspace interfaces.

I suggest asking people maintaining the udmabuf and the DMA-BUF infrastructure if you know them.

Quoted 7 lines

I am one of the two maintainers of udmabuf driver and I am also involved in the discussions with dmabuf maintainers regarding upstreaming of vfio-pci dmabuf feature in the kernel. And, based on these interactions, it is clear that mmap() is completely optional for dmabuf providers and userspace should never make any assumptions about whether mmap() (for dmabuf) works or not. Here is one reference:
https://lore.kernel.org/dri-devel/[email protected]/

That adds nothing more than you have already told in the past discussion in this thread.

I started this thread because I found that the current implementation only exercises udmabuf and VFIO devices that is known to implement mmap(). On the other hand, the cited email discusses VFIO devices that are not known to implement mmap() or future DMA-BUF exporters in general. It says:

Quoted 4 lines

Just wanted to confirm that it's entirely legit to not implement dma-buf mmap. Same for the in-kernel vmap functions. Especially for really funny buffers like these it's just not a good idea, and the dma-buf interfaces are intentionally "everything is optional".

This doesn't apply to udmabuf and VFIO devices we are concerned with. udmabuf is obviously not "funny buffers", and can be vmapped. Besides, the udmabuf already implements mmap() and *removing* it can break userspace unlike a DMA-BUF exporter that never implemented mmap(), which
was discussed in the cited email.

Speaking of VFIO devices, the code already checks it's mmapped by calling memory_region_is_ram_device() and yet this patch tries to allow blobs that cannot be mmapped, which is inconsistent.

Quoted 5 lines

So, the discussion should be summarized as the following two questions:
1. Can the kernel remove an existing mmap() implementation that is known to be depended by the userspace (udmabuf)?
2. Can the kernel let an mmap() implementation fail though it once worked (VFIO)?

IMO, the answer to both these questions is yes because mmap for dmabufs was optional to begin with. And, just because it works in most cases doesn't mean it would work with 100% certainty in all cases at all times. In other words, we should reasonably assume that mmap() can fail sometimes and when it does fail, it should not take down the fast, h/w accelerated path, which is what happens currently.

AFAICS, you are putting undue emphasis on mmap() capability for dmabufs. It is simply not important for GPU based use-cases because it is a very slow (and sometimes practically unusable) path (60 FPS vs 1 FPS for VFIO based buffers in my testing), which is why it was made optional for dmabuf providers.

And, what this patch is essentially doing is just making sure that the fast path continues to work even when the slow path is not available (or fails) for some reason. But IIUC, your argument is that if the slow path cannot work, then the fast path should also remain unavailable, which is what happens without this patch.

So, the status quo is not that great but I don't think I have any further arguments to add to this discussion at this time.

Quoted 2 lines

The cited email doesn't answer either of them.

I am not sure who can provide definitive answers to these questions but I'd like to move on by dropping this patch. I guess we could revisit this discussion when new use-cases (or potential failures) arise or more information becomes available.

Thanks,
Vivek

Quoted 3 lines

Regards,
Akihiko Odaki

#20 lore raw

RE: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob resources

In reply to Akihiko Odaki

Hi Akihiko,

Quoted 53 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify blob resources

Quoted 3 lines · Vivek

Hi Akihiko,

Quoted 1 line

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify

blob

Quoted 22 lines
Quoted 22 lines

resources

Quoted 2 lines
Quoted 2 lines

Subject: Re: [PATCH v1 2/7] virtio-gpu: Don't rely on res->blob to identify

blob

Quoted 16 lines
Quoted 10 lines

resources

Quoted 6 lines · Vivek Kasireddy

The res->blob pointer may not be valid (non-NULL) for some blobs where the backing storage is not memfd based. Therefore, we cannot use it to determine if a resource is a blob or not. Instead, we could use res->blob_size to make this determination as it is non-zero for blob resources regardless of where their backing storage is located.

Quoted 3 lines

I think this patch is no longer necessary since now you add code to mmap() VFIO storage with "[PATCH v1 7/7] virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO devices".

Right, but given that mmap() can still fail for various reasons and this use-case can work as long as dmabuf creation succeeds, I think it makes sense to not rely on res->blob to determine if a resource is blob or not.

Quoted 8 lines
Quoted 3 lines

I think the code will be simpler by making resource creation fail when mmap() fails, and I am concerned that the guest may mulfunction with such an incomplete resource.

AFAICT, mmap() is a slow, optional path except for the cursor (which needs further improvement). So, failing resource creation when mmap() fails does not seem like a good idea to me given the fact that supporting mmap() is considered optional for dmabuf providers. And, even with vfio, mmap() can be blocked for various reasons by the kernel driver IIUC.

Being slow doesn't matter, and it is not just for the cursor, but there are several other functions modified; I guess the resulting resource will be unusable except for accelerations like virgl.

Quoted 10 lines
Quoted 10 lines

To motivate the proposed patch, there should be a use-case that requires to have a resource without mmap(), not one that "can work" a resource without mmap(). It is extraneous complexity otherwise.

Such a use case should be explained in the patch message and perhaps also with a comment in the code. The current patch message needs an update as it sounds like it is unnecessary when theere is code to mmap() VFIO-based backing storage, which this series has already gained.

Quoted 7 lines
Quoted 3 lines

Although VFIO supports mmap(), it is not guaranteed to work in all cases and with a different dmabuf provider (in the future), it may not be possible at all.

The fact that mmap() is being optional for DMA-BUF and VFIO is insufficient, but what matters here is whether a DMA-BUF that lacks mmap() is usable for graphics.

Yes, it is definitely usable. AFAICS, res->blob pointer is only used when gl=off. But given that most environments nowadays support OpenGL, most users would prefer gl=on, which means the resource's dmabuf fd would be used (by the UI) instead of res->blob pointer.

Quoted 5 lines

Reading the cover letter, I suppose you are importing a mmap-incapable DMA-BUF exported by a dGPU, and the imported DMA-BUF is used with virgl or something. Explaining the use case will show that there is a mmap-incapable DMA-BUF usable for graphics.

The dmabuf provider in this case (vfio-pci dmabuf feature) does not support mmap(), but the blob resource/dmabuf can still be accessed via the fd when gl=on (by Gtk, Spice UI). Please find more details about my use-case here:
https://lore.kernel.org/dri-devel/[email protected]/

Thanks,
Vivek

Quoted 3 lines

Regards,
Akihiko Odaki

#21 lore raw

PATCH 3/7 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 figure out if it belongs to a ram device or not and decide how to obtain the host address accordingly.

Note that we take a reference on the memory region if it belongs to a ram device but we would still call dma_memory_unmap() later (to unref mr) regardless of how we obtained the hva.

Cc Marc-André Lureau <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
---
 hw/display/virtio-gpu.c | 30 +++++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)
hw/display/virtio-gpu.c +27 -3
index df696611a4..22bbe76809 100644
@@ -801,6 +801,32 @@ 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 = NULL;
+    hwaddr xlat;
+
+    if (cmd->cmd_hdr.type != VIRTIO_GPU_CMD_RESOURCE_CREATE_BLOB) {
+        return dma_memory_map(VIRTIO_DEVICE(g)->dma_as, a, len,
+                              DMA_DIRECTION_TO_DEVICE,
+                              MEMTXATTRS_UNSPECIFIED);
+    }
+
+    mr = address_space_translate(VIRTIO_DEVICE(g)->dma_as, a, &xlat, len,
+                                 DMA_DIRECTION_TO_DEVICE,
+                                 MEMTXATTRS_UNSPECIFIED);
+    if (memory_region_is_ram_device(mr)) {
+        memory_region_ref(mr);
+        return memory_region_get_ram_ptr(mr) + xlat;
+    }
+
+    return dma_memory_map(VIRTIO_DEVICE(g)->dma_as, a, len,
+                          DMA_DIRECTION_TO_DEVICE,
+                          MEMTXATTRS_UNSPECIFIED);
+}
+
 int virtio_gpu_create_mapping_iov(VirtIOGPU *g,
                                   uint32_t nr_entries, uint32_t offset,
                                   struct virtio_gpu_ctrl_command *cmd,
@@ -842,9 +868,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
#22 lore raw

Re: [PATCH v1 3/7] virtio-gpu: Find hva for Guest's DMA addr associated with a ram device

Quoted 30 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. Therefore, we first need to identify the memory region associated with the DMA address and figure out if it belongs to a ram device or not and decide how to obtain the host address accordingly.

Note that we take a reference on the memory region if it belongs to a ram device but we would still call dma_memory_unmap() later (to unref mr) regardless of how we obtained the hva.

Cc Marc-André Lureau <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
---
  hw/display/virtio-gpu.c | 30 +++++++++++++++++++++++++++---
  1 file changed, 27 insertions(+), 3 deletions(-)
hw/display/virtio-gpu.c +0 -0
index df696611a4..22bbe76809 100644
@@ -801,6 +801,32 @@ static void virtio_gpu_set_scanout_blob(VirtIOGPU *g,
                                &fb, res, &ss.r, &cmd->error);

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

Quoted 10 lines
hw/display/virtio-gpu.c +8 -0
  }
 
+static void *virtio_gpu_dma_memory_map(VirtIOGPU *g,
+                                       struct virtio_gpu_ctrl_command *cmd,
+                                       uint64_t a, hwaddr *len)
+{
+    MemoryRegion *mr = NULL;
+    hwaddr xlat;
+
+    if (cmd->cmd_hdr.type != VIRTIO_GPU_CMD_RESOURCE_CREATE_BLOB) {

I wonder what this conditional for. I think this breaks mapping VFIO with VIRTIO_GPU_CMD_RESOURCE_ATTACH_BACKING.

Quoted 32 lines
hw/display/virtio-gpu.c +19 -3
+        return dma_memory_map(VIRTIO_DEVICE(g)->dma_as, a, len,
+                              DMA_DIRECTION_TO_DEVICE,
+                              MEMTXATTRS_UNSPECIFIED);
+    }
+
+    mr = address_space_translate(VIRTIO_DEVICE(g)->dma_as, a, &xlat, len,
+                                 DMA_DIRECTION_TO_DEVICE,
+                                 MEMTXATTRS_UNSPECIFIED);
+    if (memory_region_is_ram_device(mr)) {
+        memory_region_ref(mr);
+        return memory_region_get_ram_ptr(mr) + xlat;
+    }
+
+    return dma_memory_map(VIRTIO_DEVICE(g)->dma_as, a, len,
+                          DMA_DIRECTION_TO_DEVICE,
+                          MEMTXATTRS_UNSPECIFIED);
+}
+
  int virtio_gpu_create_mapping_iov(VirtIOGPU *g,
                                    uint32_t nr_entries, uint32_t offset,
                                    struct virtio_gpu_ctrl_command *cmd,
@@ -842,9 +868,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);
#23 lore raw

RE: [PATCH v1 3/7] virtio-gpu: Find hva for Guest's DMA addr associated with a ram device

Hi Akihiko,

Quoted 47 lines

Subject: Re: [PATCH v1 3/7] virtio-gpu: Find hva for Guest's DMA addr associated with a ram device

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

Quoted 29 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. Therefore, we first need to identify the memory region associated with the DMA address and figure out if it belongs to a ram device or not and decide how to obtain the host address accordingly.

Note that we take a reference on the memory region if it belongs to a ram device but we would still call dma_memory_unmap() later (to unref mr) regardless of how we obtained the hva.

Cc Marc-André Lureau <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
---
  hw/display/virtio-gpu.c | 30 +++++++++++++++++++++++++++---
  1 file changed, 27 insertions(+), 3 deletions(-)
hw/display/virtio-gpu.c +0 -0
index df696611a4..22bbe76809 100644
@@ -801,6 +801,32 @@ 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 = NULL;
+    hwaddr xlat;
+
+    if (cmd->cmd_hdr.type != VIRTIO_GPU_CMD_RESOURCE_CREATE_BLOB)

{

Quoted 2 lines

I wonder what this conditional for. I think this breaks mapping VFIO with VIRTIO_GPU_CMD_RESOURCE_ATTACH_BACKING.

You are right, it does break attach_backing cmd. I'll remove it.

Thanks,
Vivek

Quoted 34 lines
Quoted 23 lines
hw/display/virtio-gpu.c +18 -0
+        return dma_memory_map(VIRTIO_DEVICE(g)->dma_as, a, len,
+                              DMA_DIRECTION_TO_DEVICE,
+                              MEMTXATTRS_UNSPECIFIED);
+    }
+
+    mr = address_space_translate(VIRTIO_DEVICE(g)->dma_as, a, &xlat, len,
+                                 DMA_DIRECTION_TO_DEVICE,
+                                 MEMTXATTRS_UNSPECIFIED);
+    if (memory_region_is_ram_device(mr)) {
+        memory_region_ref(mr);
+        return memory_region_get_ram_ptr(mr) + xlat;
+    }
+
+    return dma_memory_map(VIRTIO_DEVICE(g)->dma_as, a, len,
+                          DMA_DIRECTION_TO_DEVICE,
+                          MEMTXATTRS_UNSPECIFIED);
+}
+
  int virtio_gpu_create_mapping_iov(VirtIOGPU *g,
                                    uint32_t nr_entries, uint32_t offset,
                                    struct virtio_gpu_ctrl_command *cmd,

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

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

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

PATCH 4/7 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 |  2 ++
 2 files changed, 16 insertions(+)
hw/vfio/region.c +14 -0
index b165ab0b93..8837afc97f 100644
@@ -398,3 +398,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 = mr->opaque;
+
+    if (mr->ops != &vfio_region_ops) {
+        mr = mr->container;
+        if (mr->ops != &vfio_region_ops) {
+            return -1;
+        }
+	region = mr->opaque;
+    }
+    return region->nr;
+}
include/hw/vfio/vfio-device.h +2 -0
index 7e9aed6d3c..bdb106c937 100644
@@ -277,6 +277,8 @@ 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);
+
+int vfio_get_region_index_from_mr(MemoryRegion *mr);
 #endif
 
 /* Returns 0 on success, or a negative errno. */
Signature · 1 line
2.50.1
#25 lore raw

Re: [PATCH v1 4/7] vfio/region: Add a helper to get region index from memory region

Hello Vivek,

Quoted 18 lines · 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 |  2 ++
  2 files changed, 16 insertions(+)
hw/vfio/region.c +0 -0
index b165ab0b93..8837afc97f 100644

hw/vfio/region.c void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled) · line 407

Quoted 10 lines
hw/vfio/region.c +7 -0
@@ -398,3 +398,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 = mr->opaque;
+
+    if (mr->ops != &vfio_region_ops) {
+        mr = mr->container;

May be start the routine with :

    while (mr->container) {
        mr = mr->container;
    }

hw/vfio/region.c void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled) · line 409

Quoted 2 lines
hw/vfio/region.c +2 -0
+        if (mr->ops != &vfio_region_ops) {
+            return -1;

I think I would prefer returning a 'VFIORegion *' which should be NULL in case of error. Looks cleaner.

Quoted 10 lines
hw/vfio/region.c +5 -0
+        }
+	region = mr->opaque;
+    }
+    return region->nr;
+}
include/hw/vfio/vfio-device.h +0 -0
index 7e9aed6d3c..bdb106c937 100644

include/hw/vfio/vfio-device.h bool vfio_device_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_t · line 281

Quoted 5 lines
include/hw/vfio/vfio-device.h +2 -0
@@ -277,6 +277,8 @@ 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);
+
+int vfio_get_region_index_from_mr(MemoryRegion *mr);

Please add documentation.

Thanks,

C.

Quoted 3 lines
include/hw/vfio/vfio-device.h +0 -0
  #endif
 
  /* Returns 0 on success, or a negative errno. */
#26 lore raw

RE: [PATCH v1 4/7] vfio/region: Add a helper to get region index from memory region

Hi Cedric,

Quoted 39 lines

Subject: Re: [PATCH v1 4/7] vfio/region: Add a helper to get region index from memory region

Hello Vivek,

hw/vfio/region.c void vfio_region_mmaps_set_enabled(VFIORegion

Quoted 18 lines · 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 |  2 ++
  2 files changed, 16 insertions(+)
hw/vfio/region.c +0 -0
index b165ab0b93..8837afc97f 100644
@@ -398,3 +398,17 @@ void vfio_region_mmaps_set_enabled(VFIORegion

*region, bool enabled)

hw/vfio/region.c void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled) · line 407

Quoted 11 lines
hw/vfio/region.c +7 -0
      trace_vfio_region_mmaps_set_enabled(memory_region_name(region-
 mem),
                                          enabled);
  }
+
+int vfio_get_region_index_from_mr(MemoryRegion *mr)
+{
+    VFIORegion *region = mr->opaque;
+
+    if (mr->ops != &vfio_region_ops) {
+        mr = mr->container;

May be start the routine with :

Quoted 3 lines
     while (mr->container) {
         mr = mr->container;
     }

Ok, makes sense.

Quoted 6 lines

hw/vfio/region.c

Quoted 3 lines
hw/vfio/region.c +2 -0
+        if (mr->ops != &vfio_region_ops) {
+            return -1;

I think I would prefer returning a 'VFIORegion *' which should be NULL in case of error. Looks cleaner.

Given that VFIORegion type is no longer exposed to other subsystems, I am guessing you meant adding two helpers: internal helper that would return VFIORegion * and an external routine that would call the internal helper and return region->nr?

Quoted 14 lines

include/hw/vfio/vfio-device.h bool vfio_device_has_region_cap(VFIODevice

Quoted 12 lines
hw/vfio/region.c +5 -0
+        }
+	region = mr->opaque;
+    }
+    return region->nr;
+}
include/hw/vfio/vfio-device.h +0 -0
index 7e9aed6d3c..bdb106c937 100644
@@ -277,6 +277,8 @@ bool vfio_device_has_region_cap(VFIODevice

*vbasedev, int region, uint16_t cap_t

Quoted 1 line
Quoted 5 lines

include/hw/vfio/vfio-device.h bool vfio_device_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_t · line 281

Quoted 4 lines

int vfio_device_get_irq_info(VFIODevice *vbasedev, int index,

                                  struct vfio_irq_info *info);
include/hw/vfio/vfio-device.h +2 -0
+
+int vfio_get_region_index_from_mr(MemoryRegion *mr);

Please add documentation.

Sure, will do.

Thanks,
Vivek

Quoted 9 lines

Thanks,

C.

Quoted 3 lines
  #endif

/* Returns 0 on success, or a negative errno. */

#27 lore raw

Re: [PATCH v1 4/7] vfio/region: Add a helper to get region index from memory region

Quoted 50 lines · Vivek

Hi Cedric,

Quoted 39 lines

Subject: Re: [PATCH v1 4/7] vfio/region: Add a helper to get region index from memory region

Hello Vivek,

hw/vfio/region.c void vfio_region_mmaps_set_enabled(VFIORegion

Quoted 18 lines · 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 |  2 ++
   2 files changed, 16 insertions(+)
hw/vfio/region.c +0 -0
index b165ab0b93..8837afc97f 100644
@@ -398,3 +398,17 @@ void vfio_region_mmaps_set_enabled(VFIORegion

*region, bool enabled)

hw/vfio/region.c void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled) · line 407

Quoted 11 lines
hw/vfio/region.c +7 -0
       trace_vfio_region_mmaps_set_enabled(memory_region_name(region-
 mem),
                                           enabled);
   }
+
+int vfio_get_region_index_from_mr(MemoryRegion *mr)
+{
+    VFIORegion *region = mr->opaque;
+
+    if (mr->ops != &vfio_region_ops) {
+        mr = mr->container;

May be start the routine with :

Quoted 3 lines
      while (mr->container) {
          mr = mr->container;
      }

Ok, makes sense.

Quoted 4 lines
Quoted 3 lines
hw/vfio/region.c +2 -0
+        if (mr->ops != &vfio_region_ops) {
+            return -1;
Quoted 6 lines
Quoted 2 lines

I think I would prefer returning a 'VFIORegion *' which should be NULL in case of error. Looks cleaner.

Given that VFIORegion type is no longer exposed to other subsystems, I am guessing you meant adding two helpers: internal helper that would return VFIORegion * and an external routine that would call the internal helper and return region->nr?

After seeing how vfio_get_region_index_from_mr() was used in vfio_dmabuf_mmap(), I think it is fine to keep your proposal unchanged. we can refine it later.

Thanks,

C.

Quoted 35 lines
Quoted 15 lines

include/hw/vfio/vfio-device.h bool vfio_device_has_region_cap(VFIODevice

Quoted 13 lines
hw/vfio/region.c +5 -0
+        }
+	region = mr->opaque;
+    }
+    return region->nr;
+}
include/hw/vfio/vfio-device.h +0 -0
index 7e9aed6d3c..bdb106c937 100644
@@ -277,6 +277,8 @@ bool vfio_device_has_region_cap(VFIODevice

*vbasedev, int region, uint16_t cap_t

Quoted 1 line
Quoted 5 lines

include/hw/vfio/vfio-device.h bool vfio_device_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_t · line 281

Quoted 4 lines

int vfio_device_get_irq_info(VFIODevice *vbasedev, int index,

                                   struct vfio_irq_info *info);
include/hw/vfio/vfio-device.h +2 -0
+
+int vfio_get_region_index_from_mr(MemoryRegion *mr);

Please add documentation.

Sure, will do.

Thanks,
Vivek

Quoted 9 lines

Thanks,

C.

Quoted 3 lines
   #endif
   /* Returns 0 on success, or a negative errno. */
#28 lore raw

PATCH 5/7 linux-headers: Update vfio.h to include VFIO_DEVICE_FEATURE_DMA_BUF

In reply to Vivek Kasireddy
---
 linux-headers/linux/vfio.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
linux-headers/linux/vfio.h +25 -0
index 79bf8c0cc5..d2177c9cba 100644
@@ -1468,6 +1468,31 @@ struct vfio_device_feature_bus_master {
 };
 #define VFIO_DEVICE_FEATURE_BUS_MASTER 10
 
+/**
+ * Upon VFIO_DEVICE_FEATURE_GET create a dma_buf fd for the
+ * regions selected.
+ *
+ * open_flags are the typical flags passed to open(2), eg O_RDWR, O_CLOEXEC,
+ * etc. offset/length specify a slice of the region to create the dmabuf from.
+ * nr_ranges is the total number of (P2P DMA) ranges that comprise the dmabuf.
+ *
+ * Return: The fd number on success, -1 and errno is set on failure.
+ */
+#define VFIO_DEVICE_FEATURE_DMA_BUF 11
+
+struct vfio_region_dma_range {
+	__u64 offset;
+	__u64 length;
+};
+
+struct vfio_device_feature_dma_buf {
+	__u32	region_index;
+	__u32	open_flags;
+	__u32   flags;
+	__u32   nr_ranges;
+	struct vfio_region_dma_range dma_ranges[];
+};
+
 /* -------- API for Type1 VFIO IOMMU -------- */
 
 /**
Signature · 1 line
2.50.1
#29 lore raw

PATCH 6/7 vfio/device: Add support for VFIO_DEVICE_FEATURE_DMA_BUF

In reply to Vivek Kasireddy

In order to implement VFIO_DEVICE_FEATURE_DMA_BUF, we first need to identify the VFIO region and index the buffer (represented by iovec) belongs to and then translate its addresses to offsets within that region.

The qemu_ram_block_from_host() API gives us both the region and the offset info we need to populate the dma ranges in order to invoke this feature.

Cc Alex Williamson <[email protected]>
Cc Cédric Le Goater <[email protected]>
---
 hw/vfio/device.c              | 43 +++++++++++++++++++++++++++++++++++
 include/hw/vfio/vfio-device.h |  3 +++
 2 files changed, 46 insertions(+)
hw/vfio/device.c +43 -0
index 64f8750389..49070929ac 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/hw.h"
@@ -592,3 +593,45 @@ static VFIODeviceIOOps vfio_device_io_ops_ioctl = {
     .region_read = vfio_device_io_region_read,
     .region_write = vfio_device_io_region_write,
 };
+
+int vfio_device_create_dmabuf(VFIODevice *vdev,
+                              struct iovec *iov, unsigned int iov_cnt)
+{
+    g_autofree struct vfio_device_feature *feature;
+    struct vfio_device_feature_dma_buf *dma_buf;
+    ram_addr_t offset;
+    RAMBlock *rb;
+    size_t argsz;
+    int i, index;
+
+    argsz = sizeof(*feature) + sizeof (*dma_buf) +
+            sizeof(struct vfio_region_dma_range) * iov_cnt;
+    feature = g_malloc0(argsz);
+    dma_buf = (struct vfio_device_feature_dma_buf *)feature->data;
+
+    for (i = 0; i < iov_cnt; i++) {
+        rcu_read_lock();
+        rb = qemu_ram_block_from_host(iov[i].iov_base, false, &offset);
+        rcu_read_unlock();
+
+        if (!rb) {
+            return -1;
+        }
+
+        index = vfio_get_region_index_from_mr(rb->mr);
+        if (index < 0) {
+            return -1;
+        }
+
+        dma_buf->region_index = index;
+        dma_buf->dma_ranges[i].offset = offset;
+        dma_buf->dma_ranges[i].length = iov[i].iov_len;
+    }
+
+    dma_buf->nr_ranges = iov_cnt;
+    dma_buf->open_flags = O_RDONLY | O_CLOEXEC;
+    feature->argsz = argsz;
+    feature->flags = VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_DMA_BUF;
+
+    return ioctl(vdev->fd, VFIO_DEVICE_FEATURE, feature);
+}
include/hw/vfio/vfio-device.h +3 -0
index bdb106c937..74b3c4eef7 100644
@@ -279,6 +279,9 @@ int vfio_device_get_irq_info(VFIODevice *vbasedev, int index,
                                 struct vfio_irq_info *info);
 
 int vfio_get_region_index_from_mr(MemoryRegion *mr);
+
+int vfio_device_create_dmabuf(VFIODevice *vbasedev,
+                              struct iovec *iov, unsigned int iov_cnt);
 #endif
 
 /* Returns 0 on success, or a negative errno. */
Signature · 1 line
2.50.1
#30 lore raw

Re: [PATCH v1 6/7] vfio/device: Add support for VFIO_DEVICE_FEATURE_DMA_BUF

Hello Vivek,

Quoted 30 lines · Vivek Kasireddy

In order to implement VFIO_DEVICE_FEATURE_DMA_BUF, we first need to identify the VFIO region and index the buffer (represented by iovec) belongs to and then translate its addresses to offsets within that region.

The qemu_ram_block_from_host() API gives us both the region and the offset info we need to populate the dma ranges in order to invoke this feature.

Cc Alex Williamson <[email protected]>
Cc Cédric Le Goater <[email protected]>
---
  hw/vfio/device.c              | 43 +++++++++++++++++++++++++++++++++++
  include/hw/vfio/vfio-device.h |  3 +++
  2 files changed, 46 insertions(+)
hw/vfio/device.c +1 -0
index 64f8750389..49070929ac 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/hw.h"

hw/vfio/device.c static VFIODeviceIOOps vfio_device_io_ops_ioctl = { · line 597

Quoted 5 lines
hw/vfio/device.c +2 -0
@@ -592,3 +593,45 @@ static VFIODeviceIOOps vfio_device_io_ops_ioctl = {
      .region_read = vfio_device_io_region_read,
      .region_write = vfio_device_io_region_write,
  };
+
+int vfio_device_create_dmabuf(VFIODevice *vdev,

a 'vbasedev' name is preferred for VFIODevice variables/parameters.

hw/vfio/device.c static VFIODeviceIOOps vfio_device_io_ops_ioctl = { · line 600

Quoted 3 lines
hw/vfio/device.c +3 -0
+                              struct iovec *iov, unsigned int iov_cnt)
+{
+    g_autofree struct vfio_device_feature *feature;

g_autofree variables should be set: 'feature = NULL'

hw/vfio/device.c static VFIODeviceIOOps vfio_device_io_ops_ioctl = { · line 613

Quoted 13 lines
hw/vfio/device.c +13 -0
+    struct vfio_device_feature_dma_buf *dma_buf;
+    ram_addr_t offset;
+    RAMBlock *rb;
+    size_t argsz;
+    int i, index;
+
+    argsz = sizeof(*feature) + sizeof (*dma_buf) +
+            sizeof(struct vfio_region_dma_range) * iov_cnt;
+    feature = g_malloc0(argsz);
+    dma_buf = (struct vfio_device_feature_dma_buf *)feature->data;
+
+    for (i = 0; i < iov_cnt; i++) {
+        rcu_read_lock();

Is it needed ?

hw/vfio/device.c static VFIODeviceIOOps vfio_device_io_ops_ioctl = { · line 618

Quoted 5 lines
hw/vfio/device.c +5 -0
+        rb = qemu_ram_block_from_host(iov[i].iov_base, false, &offset);
+        rcu_read_unlock();
+
+        if (!rb) {
+            return -1;

wouldn't an errno be more appropriate ?

Quoted 8 lines
hw/vfio/device.c +8 -0
+        }
+
+        index = vfio_get_region_index_from_mr(rb->mr);
+        if (index < 0) {
+            return -1;
+        }
+
+        dma_buf->region_index = index;

hw/vfio/device.c static VFIODeviceIOOps vfio_device_io_ops_ioctl = { · line 636

Quoted 10 lines
hw/vfio/device.c +10 -0
+        dma_buf->dma_ranges[i].offset = offset;
+        dma_buf->dma_ranges[i].length = iov[i].iov_len;
+    }
+
+    dma_buf->nr_ranges = iov_cnt;
+    dma_buf->open_flags = O_RDONLY | O_CLOEXEC;
+    feature->argsz = argsz;
+    feature->flags = VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_DMA_BUF;
+
+    return ioctl(vdev->fd, VFIO_DEVICE_FEATURE, feature);

Please use :

   return vbasedev->io_ops->device_feature(vbasedev, feature);

This would be returning an errno. Callers should be aware.

include/hw/vfio/vfio-device.h int vfio_device_get_irq_info(VFIODevice *vbasedev, int index, · line 284

Quoted 12 lines
hw/vfio/device.c +1 -0
+}
include/hw/vfio/vfio-device.h +3 -0
index bdb106c937..74b3c4eef7 100644
@@ -279,6 +279,9 @@ int vfio_device_get_irq_info(VFIODevice *vbasedev, int index,
                                  struct vfio_irq_info *info);
 
  int vfio_get_region_index_from_mr(MemoryRegion *mr);
+
+int vfio_device_create_dmabuf(VFIODevice *vbasedev,
+                              struct iovec *iov, unsigned int iov_cnt);

Since this is an external routine, please add documentation.

Thanks,

C.

Quoted 3 lines
include/hw/vfio/vfio-device.h +0 -0
  #endif
 
  /* Returns 0 on success, or a negative errno. */
#31 lore raw

RE: [PATCH v1 6/7] vfio/device: Add support for VFIO_DEVICE_FEATURE_DMA_BUF

Hi Cedric,

Quoted 37 lines

Subject: Re: [PATCH v1 6/7] vfio/device: Add support for VFIO_DEVICE_FEATURE_DMA_BUF

Hello Vivek,

hw/vfio/device.c static VFIODeviceIOOps vfio_device_io_ops_ioctl =

Quoted 30 lines · Vivek Kasireddy

In order to implement VFIO_DEVICE_FEATURE_DMA_BUF, we first need to identify the VFIO region and index the buffer (represented by iovec) belongs to and then translate its addresses to offsets within that region.

The qemu_ram_block_from_host() API gives us both the region and the offset info we need to populate the dma ranges in order to invoke this feature.

Cc Alex Williamson <[email protected]>
Cc Cédric Le Goater <[email protected]>
---
  hw/vfio/device.c              | 43 +++++++++++++++++++++++++++++++++++
  include/hw/vfio/vfio-device.h |  3 +++
  2 files changed, 46 insertions(+)
hw/vfio/device.c +1 -0
index 64f8750389..49070929ac 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/hw.h"
@@ -592,3 +593,45 @@ static VFIODeviceIOOps vfio_device_io_ops_ioctl =

{

Quoted 7 lines

hw/vfio/device.c static VFIODeviceIOOps vfio_device_io_ops_ioctl = { · line 597

Quoted 5 lines
hw/vfio/device.c +2 -0
      .region_read = vfio_device_io_region_read,
      .region_write = vfio_device_io_region_write,
  };
+
+int vfio_device_create_dmabuf(VFIODevice *vdev,

a 'vbasedev' name is preferred for VFIODevice variables/parameters.

Ok, will make the change.

Quoted 6 lines

hw/vfio/device.c static VFIODeviceIOOps vfio_device_io_ops_ioctl = { · line 600

Quoted 4 lines
hw/vfio/device.c +3 -0
+                              struct iovec *iov, unsigned int iov_cnt)
+{
+    g_autofree struct vfio_device_feature *feature;

g_autofree variables should be set: 'feature = NULL'

Ok, got it.

Quoted 16 lines
Quoted 4 lines
hw/vfio/device.c +3 -0
+    struct vfio_device_feature_dma_buf *dma_buf;
+    ram_addr_t offset;
+    RAMBlock *rb;

hw/vfio/device.c static VFIODeviceIOOps vfio_device_io_ops_ioctl = { · line 613

Quoted 10 lines
hw/vfio/device.c +10 -0
+    size_t argsz;
+    int i, index;
+
+    argsz = sizeof(*feature) + sizeof (*dma_buf) +
+            sizeof(struct vfio_region_dma_range) * iov_cnt;
+    feature = g_malloc0(argsz);
+    dma_buf = (struct vfio_device_feature_dma_buf *)feature->data;
+
+    for (i = 0; i < iov_cnt; i++) {
+        rcu_read_lock();

Is it needed ?

Looks like it is indeed not needed. I'll remove it in the next version.

Quoted 8 lines

hw/vfio/device.c static VFIODeviceIOOps vfio_device_io_ops_ioctl = { · line 618

Quoted 6 lines
hw/vfio/device.c +5 -0
+        rb = qemu_ram_block_from_host(iov[i].iov_base, false, &offset);
+        rcu_read_unlock();
+
+        if (!rb) {
+            return -1;

wouldn't an errno be more appropriate ?

It would. I'll add it.

Quoted 18 lines
Quoted 7 lines
hw/vfio/device.c +6 -0
+        }
+
+        index = vfio_get_region_index_from_mr(rb->mr);
+        if (index < 0) {
+            return -1;
+        }

hw/vfio/device.c static VFIODeviceIOOps vfio_device_io_ops_ioctl = {

Quoted 10 lines
hw/vfio/device.c +10 -0
+
+        dma_buf->region_index = index;
+        dma_buf->dma_ranges[i].offset = offset;
+        dma_buf->dma_ranges[i].length = iov[i].iov_len;
+    }
+
+    dma_buf->nr_ranges = iov_cnt;
+    dma_buf->open_flags = O_RDONLY | O_CLOEXEC;
+    feature->argsz = argsz;
+    feature->flags = VFIO_DEVICE_FEATURE_GET |

VFIO_DEVICE_FEATURE_DMA_BUF;

Quoted 7 lines

hw/vfio/device.c static VFIODeviceIOOps vfio_device_io_ops_ioctl = · line 636

Quoted 2 lines
hw/vfio/device.c +2 -0
+
+    return ioctl(vdev->fd, VFIO_DEVICE_FEATURE, feature);

Please use :

    return vbasedev->io_ops->device_feature(vbasedev, feature);

This would be returning an errno. Callers should be aware.

Makes sense and it seems a bit cleaner.

Quoted 10 lines

include/hw/vfio/vfio-device.h int vfio_device_get_irq_info(VFIODevice *vbasedev,

Quoted 7 lines
hw/vfio/device.c +1 -0
+}
include/hw/vfio/vfio-device.h +0 -0
index bdb106c937..74b3c4eef7 100644
@@ -279,6 +279,9 @@ int vfio_device_get_irq_info(VFIODevice *vbasedev,

int index,

Quoted 2 lines
include/hw/vfio/vfio-device.h +0 -0
                                  struct vfio_irq_info *info);
 
Quoted 7 lines

include/hw/vfio/vfio-device.h int vfio_device_get_irq_info(VFIODevice *vbasedev, int index, · line 284

Quoted 4 lines
include/hw/vfio/vfio-device.h +3 -0
  int vfio_get_region_index_from_mr(MemoryRegion *mr);
+
+int vfio_device_create_dmabuf(VFIODevice *vbasedev,
+                              struct iovec *iov, unsigned int iov_cnt);

Since this is an external routine, please add documentation.

Will do.

Thanks,
Vivek

Quoted 10 lines

Thanks,

C.

Quoted 3 lines
include/hw/vfio/vfio-device.h +0 -0
  #endif
 
  /* Returns 0 on success, or a negative errno. */
#32 lore raw

PATCH 7/7 virtio-gpu-udmabuf: 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. Therefore, we first need to figure out if the blob is backed by a VFIO device region or a memfd before we can call the right API to get a dmabuf fd created.

So, once we have the ramblock and the associated mr, we rely on memory_region_is_ram_device() to tell us where the backing storage is located. If the blob resource is VFIO backed, we try to find the right VFIO device that contains the blob and then invoke the API vfio_device_create_dmabuf().

Note that in virtio_gpu_remap_udmabuf(), 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.

Cc Marc-André Lureau <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
---
 hw/display/Kconfig              |   5 ++
 hw/display/virtio-gpu-udmabuf.c | 143 ++++++++++++++++++++++++++++++--
 2 files changed, 141 insertions(+), 7 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-udmabuf.c +136 -7

186 lines not on this page · raw message

Signature · 1 line
2.50.1
#33 lore raw

Re: [PATCH v1 7/7] virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO devices

Quoted 54 lines · Vivek Kasireddy

In addition to memfd, a blob resource can also have its backing storage in a VFIO device region. Therefore, we first need to figure out if the blob is backed by a VFIO device region or a memfd before we can call the right API to get a dmabuf fd created.

So, once we have the ramblock and the associated mr, we rely on memory_region_is_ram_device() to tell us where the backing storage is located. If the blob resource is VFIO backed, we try to find the right VFIO device that contains the blob and then invoke the API vfio_device_create_dmabuf().

Note that in virtio_gpu_remap_udmabuf(), 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.

Cc Marc-André Lureau <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
---
  hw/display/Kconfig              |   5 ++
  hw/display/virtio-gpu-udmabuf.c | 143 ++++++++++++++++++++++++++++++--
  2 files changed, 141 insertions(+), 7 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-udmabuf.c +1 -0
index d804f321aa..bd06b4f300 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-udmabuf.c line 35

Quoted 8 lines
hw/display/virtio-gpu-udmabuf.c +5 -0
@@ -27,6 +28,33 @@
  #include "standard-headers/linux/udmabuf.h"
  #include "standard-headers/drm/drm_fourcc.h"
 
+static void vfio_create_dmabuf(VFIODevice *vdev,
+                               struct virtio_gpu_simple_resource *res)
+{
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+    res->dmabuf_fd = vfio_device_create_dmabuf(vdev, res->iov, res->iov_cnt);

I didn't realize an fd was returned until this patch. I'd suggest renaming vfio_device_create_dmabuf() to vfio_device_create_dmabuf_fd(), or something explicit IMO.

Quoted 4 lines
hw/display/virtio-gpu-udmabuf.c +4 -0
+    if (res->dmabuf_fd < 0) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: VFIO_DEVICE_FEATURE_DMA_BUF: %s\n",
+                      __func__, strerror(errno));

hw/display/virtio-gpu-udmabuf.c line 49

Quoted 10 lines
hw/display/virtio-gpu-udmabuf.c +10 -0
+    }
+#endif
+}
+
+static VFIODevice *vfio_device_lookup(MemoryRegion *mr)
+{
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+    VFIODevice *vdev;
+
+    QLIST_FOREACH(vdev, &vfio_device_list, next) {

Hmm, I'm not sure we want to expose the VFIOdevice list to other subsystems. I understand the need, and it's faster than iterating over QOM devices, but I’d prefer that an API be provided for this purpose.

I missed how much vfio_device_list has proliferated. Needs a check.

Quoted 45 lines
hw/display/virtio-gpu-udmabuf.c +37 -1
+        if (vdev->dev == mr->dev) {
+            return vdev;
+        }
+    }
+#endif
+    return NULL;
+}
+
  static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res)
  {
      struct udmabuf_create_list *list;
@@ -68,11 +96,73 @@ 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 *vfio_dmabuf_mmap(struct virtio_gpu_simple_resource *res,
+                              VFIODevice *vdev)
+{
+    struct vfio_region_info *info;
+    ram_addr_t offset, len = 0;
+    void *map, *submap;
+    int i, ret = -1;
+    RAMBlock *rb;
+
+    /*
+     * 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, vdev->fd, 0);
+    if (map == MAP_FAILED) {
+        return map;
+    }
+
+    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) {
+            goto err;
+        }
+
+#if defined(VIRTIO_GPU_VFIO_BLOB)

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

Quoted 10 lines
hw/display/virtio-gpu-udmabuf.c +10 -0
+        ret = vfio_get_region_index_from_mr(rb->mr);
+        if (ret < 0) {
+            goto err;
+        }
+
+        ret = vfio_device_get_region_info(vdev, ret, &info);
+#endif
+        if (ret < 0) {
+            goto err;
+        }

"hmm" again. Not this patch fault but we lack proper documentation for the VFIO API. Something to work on. Since this patch is using vfio_device_get_region_info() could you please add documentation for it ?

Thanks,

C.

Quoted 104 lines
hw/display/virtio-gpu-udmabuf.c +68 -6
+        submap = mmap(map + len, res->iov[i].iov_len, PROT_READ,
+                      MAP_SHARED | MAP_FIXED, vdev->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_udmabuf(struct virtio_gpu_simple_resource *res,
+                                     VFIODevice *vdev)
  {
      res->remapped = mmap(NULL, res->blob_size, PROT_READ,
                           MAP_SHARED, res->dmabuf_fd, 0);
      if (res->remapped == MAP_FAILED) {
+        if (vdev) {
+            res->remapped = vfio_dmabuf_mmap(res, vdev);
+            if (res->remapped != MAP_FAILED) {
+                return;
+            }
+        }
          warn_report("%s: dmabuf mmap failed: %s", __func__,
                      strerror(errno));
          res->remapped = NULL;
@@ -130,18 +220,59 @@ bool virtio_gpu_have_udmabuf(void)
 
  void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)
  {
+    VFIODevice *vdev = NULL;
      void *pdata = NULL;
+    ram_addr_t offset;
+    RAMBlock *rb;
 
      res->dmabuf_fd = -1;
      if (res->iov_cnt == 1 &&
          res->iov[0].iov_len < 4096) {
          pdata = res->iov[0].iov_base;
      } else {
-        virtio_gpu_create_udmabuf(res);
-        if (res->dmabuf_fd < 0) {
+        rcu_read_lock();
+        rb = qemu_ram_block_from_host(res->iov[0].iov_base, false, &offset);
+        rcu_read_unlock();
+
+        if (!rb) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: Could not find ram block for host address\n",
+                          __func__);
              return;
          }
-        virtio_gpu_remap_udmabuf(res);
+
+        if (memory_region_is_ram_device(rb->mr)) {
+            vdev = vfio_device_lookup(rb->mr);
+            if (!vdev) {
+                qemu_log_mask(LOG_GUEST_ERROR,
+                              "%s: Could not find device to create dmabuf\n",
+                              __func__);
+                return;
+            }
+
+            vfio_create_dmabuf(vdev, res);
+            if (res->dmabuf_fd < 0) {
+                qemu_log_mask(LOG_GUEST_ERROR,
+                              "%s: Could not create dmabuf from vfio device\n",
+                              __func__);
+                return;
+            }
+        } else if (memory_region_is_ram(rb->mr) && virtio_gpu_have_udmabuf()) {
+            virtio_gpu_create_udmabuf(res);
+            if (res->dmabuf_fd < 0) {
+                qemu_log_mask(LOG_GUEST_ERROR,
+                              "%s: Could not create dmabuf from memfd\n",
+                              __func__);
+                return;
+            }
+        } else {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: memory region cannot be used to create dmabuf\n",
+                          __func__);
+            return;
+        }
+
+        virtio_gpu_remap_udmabuf(res, vdev);
          if (!res->remapped) {
              return;
          }
@@ -153,9 +284,7 @@ void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)
 
  void virtio_gpu_fini_udmabuf(struct virtio_gpu_simple_resource *res)
  {
-    if (res->remapped) {
-        virtio_gpu_destroy_udmabuf(res);
-    }
+    virtio_gpu_destroy_udmabuf(res);
  }
 
  static void virtio_gpu_free_dmabuf(VirtIOGPU *g, VGPUDMABuf *dmabuf)
#34 lore raw

RE: [PATCH v1 7/7] virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO devices

Hi Cedric,

Quoted 63 lines

Subject: Re: [PATCH v1 7/7] virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO devices

Quoted 15 lines · Vivek Kasireddy

In addition to memfd, a blob resource can also have its backing storage in a VFIO device region. Therefore, we first need to figure out if the blob is backed by a VFIO device region or a memfd before we can call the right API to get a dmabuf fd created.

So, once we have the ramblock and the associated mr, we rely on memory_region_is_ram_device() to tell us where the backing storage is located. If the blob resource is VFIO backed, we try to find the right VFIO device that contains the blob and then invoke the API vfio_device_create_dmabuf().

Note that in virtio_gpu_remap_udmabuf(), 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.

Quoted 8 lines
Cc Marc-André Lureau <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
---
  hw/display/Kconfig              |   5 ++
  hw/display/virtio-gpu-udmabuf.c | 143

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

hw/display/virtio-gpu-

Quoted 19 lines
  2 files changed, 141 insertions(+), 7 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-udmabuf.c +0 -0

udmabuf.c

Quoted 15 lines

index d804f321aa..bd06b4f300 100644

hw/display/virtio-gpu-udmabuf.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,33 @@
  #include "standard-headers/linux/udmabuf.h"
  #include "standard-headers/drm/drm_fourcc.h"
 
Quoted 10 lines

hw/display/virtio-gpu-udmabuf.c

Quoted 6 lines
hw/display/virtio-gpu-udmabuf.c +5 -0
+static void vfio_create_dmabuf(VFIODevice *vdev,
+                               struct virtio_gpu_simple_resource *res)
+{
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+    res->dmabuf_fd = vfio_device_create_dmabuf(vdev, res->iov, res-
 iov_cnt);

I didn't realize an fd was returned until this patch. I'd suggest renaming vfio_device_create_dmabuf() to vfio_device_create_dmabuf_fd(), or something explicit IMO.

Yeah, makes sense. I'll change the name.

Quoted 11 lines
Quoted 11 lines
hw/display/virtio-gpu-udmabuf.c +10 -0
+    if (res->dmabuf_fd < 0) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: VFIO_DEVICE_FEATURE_DMA_BUF: %s\n",
+                      __func__, strerror(errno));
+    }
+#endif
+}
+
+static VFIODevice *vfio_device_lookup(MemoryRegion *mr)
+{
Quoted 8 lines

hw/display/virtio-gpu-udmabuf.c line 49

Quoted 4 lines
hw/display/virtio-gpu-udmabuf.c +4 -0
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+    VFIODevice *vdev;
+
+    QLIST_FOREACH(vdev, &vfio_device_list, next) {

Hmm, I'm not sure we want to expose the VFIOdevice list to other subsystems. I understand the need, and it's faster than iterating over QOM devices, but I'd prefer that an API be provided for this purpose.

Ok, I'll add a new API for this purpose.

Quoted 60 lines

I missed how much vfio_device_list has proliferated. Needs a check.

hw/display/virtio-gpu-udmabuf.c

Quoted 9 lines
hw/display/virtio-gpu-udmabuf.c +8 -0
+        if (vdev->dev == mr->dev) {
+            return vdev;
+        }
+    }
+#endif
+    return NULL;
+}
+
  static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource

*res)

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

Quoted 3 lines
hw/display/virtio-gpu-udmabuf.c +0 -0
  {
      struct udmabuf_create_list *list;
hw/display/virtio-gpu-udmabuf.c +0 -0
@@ -68,11 +96,73 @@ static void virtio_gpu_create_udmabuf(struct

virtio_gpu_simple_resource *res)

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

Quoted 4 lines
hw/display/virtio-gpu-udmabuf.c +0 -1
      g_free(list);
  }
 
-static void virtio_gpu_remap_udmabuf(struct virtio_gpu_simple_resource

*res)

Quoted 37 lines
hw/display/virtio-gpu-udmabuf.c +36 -0
+static void *vfio_dmabuf_mmap(struct virtio_gpu_simple_resource *res,
+                              VFIODevice *vdev)
+{
+    struct vfio_region_info *info;
+    ram_addr_t offset, len = 0;
+    void *map, *submap;
+    int i, ret = -1;
+    RAMBlock *rb;
+
+    /*
+     * 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, vdev-
 fd, 0);
+    if (map == MAP_FAILED) {
+        return map;
+    }
+
+    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) {
+            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(vdev, ret, &info);
+#endif
Quoted 8 lines

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

Quoted 3 lines
hw/display/virtio-gpu-udmabuf.c +3 -0
+        if (ret < 0) {
+            goto err;
+        }

"hmm" again. Not this patch fault but we lack proper documentation for the VFIO API. Something to work on. Since this patch is using vfio_device_get_region_info() could you please add documentation for it ?

Sure, I'll add documentation for this and other routines you mentioned, in the next version.

Thanks,
Vivek

Quoted 115 lines

Thanks,

C.

Quoted 6 lines
hw/display/virtio-gpu-udmabuf.c +6 -0
+        submap = mmap(map + len, res->iov[i].iov_len, PROT_READ,
+                      MAP_SHARED | MAP_FIXED, vdev->fd,
+                      info->offset + offset);
+        if (submap == MAP_FAILED) {
+            goto err;
+        }

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

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

*res,

Quoted 49 lines
hw/display/virtio-gpu-udmabuf.c +26 -3
+                                     VFIODevice *vdev)
  {
      res->remapped = mmap(NULL, res->blob_size, PROT_READ,
                           MAP_SHARED, res->dmabuf_fd, 0);
      if (res->remapped == MAP_FAILED) {
+        if (vdev) {
+            res->remapped = vfio_dmabuf_mmap(res, vdev);
+            if (res->remapped != MAP_FAILED) {
+                return;
+            }
+        }
          warn_report("%s: dmabuf mmap failed: %s", __func__,
                      strerror(errno));
          res->remapped = NULL;
@@ -130,18 +220,59 @@ bool virtio_gpu_have_udmabuf(void)
 
  void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)
  {
+    VFIODevice *vdev = NULL;
      void *pdata = NULL;
+    ram_addr_t offset;
+    RAMBlock *rb;
 
      res->dmabuf_fd = -1;
      if (res->iov_cnt == 1 &&
          res->iov[0].iov_len < 4096) {
          pdata = res->iov[0].iov_base;
      } else {
-        virtio_gpu_create_udmabuf(res);
-        if (res->dmabuf_fd < 0) {
+        rcu_read_lock();
+        rb = qemu_ram_block_from_host(res->iov[0].iov_base, false, &offset);
+        rcu_read_unlock();
+
+        if (!rb) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: Could not find ram block for host address\n",
+                          __func__);
              return;
          }
-        virtio_gpu_remap_udmabuf(res);
+
+        if (memory_region_is_ram_device(rb->mr)) {
+            vdev = vfio_device_lookup(rb->mr);
+            if (!vdev) {
+                qemu_log_mask(LOG_GUEST_ERROR,
+                              "%s: Could not find device to create dmabuf\n",
+                              __func__);
+                return;

hw/display/virtio-gpu-udmabuf.c bool virtio_gpu_have_udmabuf(void) · line 260

Quoted 10 lines
hw/display/virtio-gpu-udmabuf.c +10 -0
+            }
+
+            vfio_create_dmabuf(vdev, res);
+            if (res->dmabuf_fd < 0) {
+                qemu_log_mask(LOG_GUEST_ERROR,
+                              "%s: Could not create dmabuf from vfio device\n",
+                              __func__);
+                return;
+            }
+        } else if (memory_region_is_ram(rb->mr) &&

virtio_gpu_have_udmabuf()) {

hw/display/virtio-gpu-udmabuf.c void virtio_gpu_init_udmabuf(struct

Quoted 19 lines
hw/display/virtio-gpu-udmabuf.c +15 -0
+            virtio_gpu_create_udmabuf(res);
+            if (res->dmabuf_fd < 0) {
+                qemu_log_mask(LOG_GUEST_ERROR,
+                              "%s: Could not create dmabuf from memfd\n",
+                              __func__);
+                return;
+            }
+        } else {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: memory region cannot be used to create dmabuf\n",
+                          __func__);
+            return;
+        }
+
+        virtio_gpu_remap_udmabuf(res, vdev);
          if (!res->remapped) {
              return;
          }
@@ -153,9 +284,7 @@ void virtio_gpu_init_udmabuf(struct

virtio_gpu_simple_resource *res)

Quoted 10 lines
hw/display/virtio-gpu-udmabuf.c +1 -3
  void virtio_gpu_fini_udmabuf(struct virtio_gpu_simple_resource *res)
  {
-    if (res->remapped) {
-        virtio_gpu_destroy_udmabuf(res);
-    }
+    virtio_gpu_destroy_udmabuf(res);
  }
 
  static void virtio_gpu_free_dmabuf(VirtIOGPU *g, VGPUDMABuf *dmabuf)
#35 lore raw

Re: [PATCH v1 7/7] virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO devices

In reply to Cédric Le Goater
Quoted 149 lines · Cédric Le Goater
Quoted 53 lines · Vivek Kasireddy

In addition to memfd, a blob resource can also have its backing storage in a VFIO device region. Therefore, we first need to figure out if the blob is backed by a VFIO device region or a memfd before we can call the right API to get a dmabuf fd created.

So, once we have the ramblock and the associated mr, we rely on memory_region_is_ram_device() to tell us where the backing storage is located. If the blob resource is VFIO backed, we try to find the right VFIO device that contains the blob and then invoke the API vfio_device_create_dmabuf().

Note that in virtio_gpu_remap_udmabuf(), 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.

Cc Marc-André Lureau <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>

---
  hw/display/Kconfig              |   5 ++ hw/display/virtio-gpu-udmabuf.c | 143 ++++++++++++++++++++++++++++++--

  2 files changed, 141 insertions(+), 7 deletions(-)
hw/display/Kconfig +0 -0
index 1e95ab28ef..0d090f25f5 100644
@@ -106,6 +106,11 @@ config VIRTIO_VGA

      depends on VIRTIO_PCI
      select VGA

hw/display/Kconfig +5 -0
+config VIRTIO_GPU_VFIO_BLOB
+    bool
+    default y
+    depends on VFIO
+

  config VHOST_USER_GPU
      bool
      default y

hw/display/virtio-gpu-udmabuf.c +1 -0
index d804f321aa..bd06b4f300 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-udmabuf.c line 34

Quoted 7 lines
hw/display/virtio-gpu-udmabuf.c +5 -0
@@ -27,6 +28,33 @@
   #include "standard-headers/linux/udmabuf.h"
   #include "standard-headers/drm/drm_fourcc.h"
+static void vfio_create_dmabuf(VFIODevice *vdev,
+                               struct virtio_gpu_simple_resource *res)
+{
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+    res->dmabuf_fd = vfio_device_create_dmabuf(vdev, res->iov, res->iov_cnt);

I didn't realize an fd was returned until this patch. I'd suggest renaming vfio_device_create_dmabuf() to vfio_device_create_dmabuf_fd(), or something explicit IMO.

Quoted 4 lines
hw/display/virtio-gpu-udmabuf.c +4 -0
+    if (res->dmabuf_fd < 0) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: VFIO_DEVICE_FEATURE_DMA_BUF: %s\n",
+                      __func__, strerror(errno));

hw/display/virtio-gpu-udmabuf.c line 49

Quoted 10 lines
hw/display/virtio-gpu-udmabuf.c +10 -0
+    }
+#endif
+}
+
+static VFIODevice *vfio_device_lookup(MemoryRegion *mr)
+{
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+    VFIODevice *vdev;
+
+    QLIST_FOREACH(vdev, &vfio_device_list, next) {

Hmm, I'm not sure we want to expose the VFIOdevice list to other subsystems. I understand the need, and it's faster than iterating over QOM devices, but I’d prefer that an API be provided for this purpose.

I missed how much vfio_device_list has proliferated. Needs a check.

Quoted 44 lines
hw/display/virtio-gpu-udmabuf.c +37 -1
+        if (vdev->dev == mr->dev) {
+            return vdev;
+        }
+    }
+#endif
+    return NULL;
+}
+
   static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res)
   {
       struct udmabuf_create_list *list;
@@ -68,11 +96,73 @@ 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 *vfio_dmabuf_mmap(struct virtio_gpu_simple_resource *res,
+                              VFIODevice *vdev)
+{
+    struct vfio_region_info *info;
+    ram_addr_t offset, len = 0;
+    void *map, *submap;
+    int i, ret = -1;
+    RAMBlock *rb;
+
+    /*
+     * 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, vdev->fd, 0);
+    if (map == MAP_FAILED) {
+        return map;
+    }
+
+    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) {
+            goto err;
+        }
+
+#if defined(VIRTIO_GPU_VFIO_BLOB)

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

Quoted 10 lines
hw/display/virtio-gpu-udmabuf.c +10 -0
+        ret = vfio_get_region_index_from_mr(rb->mr);
+        if (ret < 0) {
+            goto err;
+        }
+
+        ret = vfio_device_get_region_info(vdev, ret, &info);
+#endif
+        if (ret < 0) {
+            goto err;
+        }

"hmm" again. Not this patch fault but we lack proper documentation for the VFIO API. Something to work on. Since this patch is using vfio_device_get_region_info() could you please add documentation for it ?

Quoted 9 lines

Thanks,

C.

Quoted 3 lines
hw/display/virtio-gpu-udmabuf.c +3 -0
+        submap = mmap(map + len, res->iov[i].iov_len, PROT_READ,
+                      MAP_SHARED | MAP_FIXED, vdev->fd,
+                      info->offset + offset);

One more thing which needs a fix :

'info' is uninitialized if !VIRTIO_GPU_VFIO_BLOB.

Thanks,

C.

Quoted 98 lines
Quoted 97 lines
hw/display/virtio-gpu-udmabuf.c +23 -0
+        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_udmabuf(struct virtio_gpu_simple_resource *res,
+                                     VFIODevice *vdev)
   {
       res->remapped = mmap(NULL, res->blob_size, PROT_READ,
                            MAP_SHARED, res->dmabuf_fd, 0);
       if (res->remapped == MAP_FAILED) {
+        if (vdev) {
+            res->remapped = vfio_dmabuf_mmap(res, vdev);
+            if (res->remapped != MAP_FAILED) {
+                return;
+            }
+        }
           warn_report("%s: dmabuf mmap failed: %s", __func__,
                       strerror(errno));
           res->remapped = NULL;
@@ -130,18 +220,59 @@ bool virtio_gpu_have_udmabuf(void)
   void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)
   {
+    VFIODevice *vdev = NULL;
       void *pdata = NULL;
+    ram_addr_t offset;
+    RAMBlock *rb;
       res->dmabuf_fd = -1;

      if (res->iov_cnt == 1 &&
          res->iov[0].iov_len < 4096) {
          pdata = res->iov[0].iov_base;
      } else {
-        virtio_gpu_create_udmabuf(res);
-        if (res->dmabuf_fd < 0) {
+        rcu_read_lock();
+        rb = qemu_ram_block_from_host(res->iov[0].iov_base, false, &offset); +        rcu_read_unlock();
+
+        if (!rb) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: Could not find ram block for host address\n", +                          __func__);
              return;
          }
-        virtio_gpu_remap_udmabuf(res);

hw/display/virtio-gpu-udmabuf.c +33 -0
+
+        if (memory_region_is_ram_device(rb->mr)) {
+            vdev = vfio_device_lookup(rb->mr);
+            if (!vdev) {
+                qemu_log_mask(LOG_GUEST_ERROR,
+                              "%s: Could not find device to create dmabuf\n",
+                              __func__);
+                return;
+            }
+
+            vfio_create_dmabuf(vdev, res);
+            if (res->dmabuf_fd < 0) {
+                qemu_log_mask(LOG_GUEST_ERROR,
+                              "%s: Could not create dmabuf from vfio device\n",
+                              __func__);
+                return;
+            }
+        } else if (memory_region_is_ram(rb->mr) && virtio_gpu_have_udmabuf()) {
+            virtio_gpu_create_udmabuf(res);
+            if (res->dmabuf_fd < 0) {
+                qemu_log_mask(LOG_GUEST_ERROR,
+                              "%s: Could not create dmabuf from memfd\n",
+                              __func__);
+                return;
+            }
+        } else {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: memory region cannot be used to create dmabuf\n",
+                          __func__);
+            return;
+        }
+
+        virtio_gpu_remap_udmabuf(res, vdev);

          if (!res->remapped) {
              return;
          }

hw/display/virtio-gpu-udmabuf.c +1 -3
@@ -153,9 +284,7 @@ void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)
   void virtio_gpu_fini_udmabuf(struct virtio_gpu_simple_resource *res)
   {
-    if (res->remapped) {
-        virtio_gpu_destroy_udmabuf(res);
-    }
+    virtio_gpu_destroy_udmabuf(res);
   }
   static void virtio_gpu_free_dmabuf(VirtIOGPU *g, VGPUDMABuf *dmabuf)
#36 lore raw

Re: [PATCH v1 7/7] virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO devices

In reply to Vivek Kasireddy
Quoted 11 lines · Vivek Kasireddy

In addition to memfd, a blob resource can also have its backing storage in a VFIO device region. Therefore, we first need to figure out if the blob is backed by a VFIO device region or a memfd before we can call the right API to get a dmabuf fd created.

So, once we have the ramblock and the associated mr, we rely on memory_region_is_ram_device() to tell us where the backing storage is located. If the blob resource is VFIO backed, we try to find the right VFIO device that contains the blob and then invoke the API vfio_device_create_dmabuf().

Quoted 3 lines

Note that in virtio_gpu_remap_udmabuf(), 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.

It is odd to handle VFIO DMA-BUF in a function named "udmabuf". The function and source file need to be renamed.

Quoted 91 lines
Cc Marc-André Lureau <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
---
  hw/display/Kconfig              |   5 ++
  hw/display/virtio-gpu-udmabuf.c | 143 ++++++++++++++++++++++++++++++--
  2 files changed, 141 insertions(+), 7 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-udmabuf.c +41 -1
index d804f321aa..bd06b4f300 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,33 @@
  #include "standard-headers/linux/udmabuf.h"
  #include "standard-headers/drm/drm_fourcc.h"
 
+static void vfio_create_dmabuf(VFIODevice *vdev,
+                               struct virtio_gpu_simple_resource *res)
+{
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+    res->dmabuf_fd = vfio_device_create_dmabuf(vdev, 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 VFIODevice *vfio_device_lookup(MemoryRegion *mr)
+{
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+    VFIODevice *vdev;
+
+    QLIST_FOREACH(vdev, &vfio_device_list, next) {
+        if (vdev->dev == mr->dev) {
+            return vdev;
+        }
+    }
+#endif
+    return NULL;
+}
+
  static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res)
  {
      struct udmabuf_create_list *list;
@@ -68,11 +96,73 @@ 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 *vfio_dmabuf_mmap(struct virtio_gpu_simple_resource *res,
+                              VFIODevice *vdev)
+{
+    struct vfio_region_info *info;
+    ram_addr_t offset, len = 0;
+    void *map, *submap;
+    int i, ret = -1;
+    RAMBlock *rb;
+
+    /*
+     * 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.

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

Quoted 10 lines
hw/display/virtio-gpu-udmabuf.c +10 -0
+     */
+    map = mmap(NULL, res->blob_size, PROT_READ, MAP_SHARED, vdev->fd, 0);
+    if (map == MAP_FAILED) {
+        return map;
+    }
+
+    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();

I don't think this RCU lock is necessary. The documentation of qemu_ram_block_from_host() says:

Quoted 5 lines

By the time this function returns, the returned pointer is not protected by RCU anymore. If the caller is not within an RCU critical section and does not hold the BQL, it must have other means of protecting the pointer, such as a reference to the memory region that owns the RAMBlock.

This function is called with the BQL held, and a reference to the memory region is also taken in virtio_gpu_dma_memory_map().

Quoted 82 lines
hw/display/virtio-gpu-udmabuf.c +59 -3
+
+        if (!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(vdev, ret, &info);
+#endif
+        if (ret < 0) {
+            goto err;
+        }
+
+        submap = mmap(map + len, res->iov[i].iov_len, PROT_READ,
+                      MAP_SHARED | MAP_FIXED, vdev->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_udmabuf(struct virtio_gpu_simple_resource *res,
+                                     VFIODevice *vdev)
  {
      res->remapped = mmap(NULL, res->blob_size, PROT_READ,
                           MAP_SHARED, res->dmabuf_fd, 0);
      if (res->remapped == MAP_FAILED) {
+        if (vdev) {
+            res->remapped = vfio_dmabuf_mmap(res, vdev);
+            if (res->remapped != MAP_FAILED) {
+                return;
+            }
+        }
          warn_report("%s: dmabuf mmap failed: %s", __func__,
                      strerror(errno));
          res->remapped = NULL;
@@ -130,18 +220,59 @@ bool virtio_gpu_have_udmabuf(void)
 
  void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)
  {
+    VFIODevice *vdev = NULL;
      void *pdata = NULL;
+    ram_addr_t offset;
+    RAMBlock *rb;
 
      res->dmabuf_fd = -1;
      if (res->iov_cnt == 1 &&
          res->iov[0].iov_len < 4096) {
          pdata = res->iov[0].iov_base;
      } else {
-        virtio_gpu_create_udmabuf(res);
-        if (res->dmabuf_fd < 0) {
+        rcu_read_lock();
+        rb = qemu_ram_block_from_host(res->iov[0].iov_base, false, &offset);
+        rcu_read_unlock();
+
+        if (!rb) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: Could not find ram block for host address\n",
+                          __func__);
              return;
          }
-        virtio_gpu_remap_udmabuf(res);
+
+        if (memory_region_is_ram_device(rb->mr)) {
+            vdev = vfio_device_lookup(rb->mr);
+            if (!vdev) {
+                qemu_log_mask(LOG_GUEST_ERROR,
+                              "%s: Could not find device to create dmabuf\n",
+                              __func__);
+                return;

hw/display/virtio-gpu-udmabuf.c bool virtio_gpu_have_udmabuf(void) · line 260

Quoted 10 lines
hw/display/virtio-gpu-udmabuf.c +10 -0
+            }
+
+            vfio_create_dmabuf(vdev, res);
+            if (res->dmabuf_fd < 0) {
+                qemu_log_mask(LOG_GUEST_ERROR,
+                              "%s: Could not create dmabuf from vfio device\n",
+                              __func__);
+                return;
+            }
+        } else if (memory_region_is_ram(rb->mr) && virtio_gpu_have_udmabuf()) {

memory_region_is_ram_device() and memory_region_is_ram() should be called for all iov elements, not just the first one.

Calling virtio_gpu_have_udmabuf() here is redundant since virtio_gpu_device_realize() already calls it.

Quoted 29 lines
hw/display/virtio-gpu-udmabuf.c +16 -3
+            virtio_gpu_create_udmabuf(res);
+            if (res->dmabuf_fd < 0) {
+                qemu_log_mask(LOG_GUEST_ERROR,
+                              "%s: Could not create dmabuf from memfd\n",
+                              __func__);
+                return;
+            }
+        } else {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: memory region cannot be used to create dmabuf\n",
+                          __func__);
+            return;
+        }
+
+        virtio_gpu_remap_udmabuf(res, vdev);
          if (!res->remapped) {
              return;
          }
@@ -153,9 +284,7 @@ void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)
 
  void virtio_gpu_fini_udmabuf(struct virtio_gpu_simple_resource *res)
  {
-    if (res->remapped) {
-        virtio_gpu_destroy_udmabuf(res);
-    }
+    virtio_gpu_destroy_udmabuf(res);
  }
 
  static void virtio_gpu_free_dmabuf(VirtIOGPU *g, VGPUDMABuf *dmabuf)
#37 lore raw

RE: [PATCH v1 7/7] virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO devices

Hi Akihiko,

Quoted 15 lines

Subject: Re: [PATCH v1 7/7] virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO devices

Quoted 11 lines · Vivek Kasireddy

In addition to memfd, a blob resource can also have its backing storage in a VFIO device region. Therefore, we first need to figure out if the blob is backed by a VFIO device region or a memfd before we can call the right API to get a dmabuf fd created.

So, once we have the ramblock and the associated mr, we rely on memory_region_is_ram_device() to tell us where the backing storage is located. If the blob resource is VFIO backed, we try to find the right VFIO device that contains the blob and then invoke the API vfio_device_create_dmabuf().

Quoted 6 lines
Quoted 3 lines

Note that in virtio_gpu_remap_udmabuf(), 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.

It is odd to handle VFIO DMA-BUF in a function named "udmabuf". The function and source file need to be renamed.

Ok, makes sense. I'll rename it accordingly.

Quoted 112 lines
Quoted 10 lines
Cc Marc-André Lureau <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
---
  hw/display/Kconfig              |   5 ++
  hw/display/virtio-gpu-udmabuf.c | 143

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

hw/display/virtio-gpu-

Quoted 19 lines
  2 files changed, 141 insertions(+), 7 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-udmabuf.c +0 -0

udmabuf.c

Quoted 34 lines

index d804f321aa..bd06b4f300 100644

hw/display/virtio-gpu-udmabuf.c +19 -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,33 @@
  #include "standard-headers/linux/udmabuf.h"
  #include "standard-headers/drm/drm_fourcc.h"
 
+static void vfio_create_dmabuf(VFIODevice *vdev,
+                               struct virtio_gpu_simple_resource *res)
+{
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+    res->dmabuf_fd = vfio_device_create_dmabuf(vdev, 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 VFIODevice *vfio_device_lookup(MemoryRegion *mr)
+{
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+    VFIODevice *vdev;
+

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

Quoted 10 lines
hw/display/virtio-gpu-udmabuf.c +9 -0
+    QLIST_FOREACH(vdev, &vfio_device_list, next) {
+        if (vdev->dev == mr->dev) {
+            return vdev;
+        }
+    }
+#endif
+    return NULL;
+}
+
  static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource

*res)

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

Quoted 3 lines
hw/display/virtio-gpu-udmabuf.c +0 -0
  {
      struct udmabuf_create_list *list;
hw/display/virtio-gpu-udmabuf.c +0 -0
@@ -68,11 +96,73 @@ static void virtio_gpu_create_udmabuf(struct

virtio_gpu_simple_resource *res)

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

Quoted 4 lines
hw/display/virtio-gpu-udmabuf.c +0 -1
      g_free(list);
  }
 
-static void virtio_gpu_remap_udmabuf(struct virtio_gpu_simple_resource

*res)

Quoted 14 lines
hw/display/virtio-gpu-udmabuf.c +14 -0
+static void *vfio_dmabuf_mmap(struct virtio_gpu_simple_resource *res,
+                              VFIODevice *vdev)
+{
+    struct vfio_region_info *info;
+    ram_addr_t offset, len = 0;
+    void *map, *submap;
+    int i, ret = -1;
+    RAMBlock *rb;
+
+    /*
+     * 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.
+     */

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

Quoted 10 lines
hw/display/virtio-gpu-udmabuf.c +9 -0
+    map = mmap(NULL, res->blob_size, PROT_READ, MAP_SHARED, vdev-
 fd, 0);
+    if (map == MAP_FAILED) {
+        return map;
+    }
+
+    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();

I don't think this RCU lock is necessary. The documentation of qemu_ram_block_from_host() says:

Quoted 8 lines
Quoted 5 lines

By the time this function returns, the returned pointer is not protected by RCU anymore. If the caller is not within an RCU critical section and does not hold the BQL, it must have other means of protecting the pointer, such as a reference to the memory region that owns the RAMBlock.

This function is called with the BQL held, and a reference to the memory region is also taken in virtio_gpu_dma_memory_map().

I agree. I'll remove the RCU lock.

Quoted 96 lines
Quoted 24 lines
hw/display/virtio-gpu-udmabuf.c +23 -0
+
+        if (!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(vdev, ret, &info);
+#endif
+        if (ret < 0) {
+            goto err;
+        }
+
+        submap = mmap(map + len, res->iov[i].iov_len, PROT_READ,
+                      MAP_SHARED | MAP_FIXED, vdev->fd,
+                      info->offset + offset);
+        if (submap == MAP_FAILED) {
+            goto err;
+        }

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

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

*res,

Quoted 49 lines
hw/display/virtio-gpu-udmabuf.c +26 -3
+                                     VFIODevice *vdev)
  {
      res->remapped = mmap(NULL, res->blob_size, PROT_READ,
                           MAP_SHARED, res->dmabuf_fd, 0);
      if (res->remapped == MAP_FAILED) {
+        if (vdev) {
+            res->remapped = vfio_dmabuf_mmap(res, vdev);
+            if (res->remapped != MAP_FAILED) {
+                return;
+            }
+        }
          warn_report("%s: dmabuf mmap failed: %s", __func__,
                      strerror(errno));
          res->remapped = NULL;
@@ -130,18 +220,59 @@ bool virtio_gpu_have_udmabuf(void)
 
  void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)
  {
+    VFIODevice *vdev = NULL;
      void *pdata = NULL;
+    ram_addr_t offset;
+    RAMBlock *rb;
 
      res->dmabuf_fd = -1;
      if (res->iov_cnt == 1 &&
          res->iov[0].iov_len < 4096) {
          pdata = res->iov[0].iov_base;
      } else {
-        virtio_gpu_create_udmabuf(res);
-        if (res->dmabuf_fd < 0) {
+        rcu_read_lock();
+        rb = qemu_ram_block_from_host(res->iov[0].iov_base, false, &offset);
+        rcu_read_unlock();
+
+        if (!rb) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: Could not find ram block for host address\n",
+                          __func__);
              return;
          }
-        virtio_gpu_remap_udmabuf(res);
+
+        if (memory_region_is_ram_device(rb->mr)) {
+            vdev = vfio_device_lookup(rb->mr);
+            if (!vdev) {
+                qemu_log_mask(LOG_GUEST_ERROR,
+                              "%s: Could not find device to create dmabuf\n",
+                              __func__);
+                return;

hw/display/virtio-gpu-udmabuf.c bool virtio_gpu_have_udmabuf(void) · line 260

Quoted 10 lines
hw/display/virtio-gpu-udmabuf.c +10 -0
+            }
+
+            vfio_create_dmabuf(vdev, res);
+            if (res->dmabuf_fd < 0) {
+                qemu_log_mask(LOG_GUEST_ERROR,
+                              "%s: Could not create dmabuf from vfio device\n",
+                              __func__);
+                return;
+            }
+        } else if (memory_region_is_ram(rb->mr) &&

virtio_gpu_have_udmabuf()) {

Quoted 2 lines

memory_region_is_ram_device() and memory_region_is_ram() should be called for all iov elements, not just the first one.

I am not sure if it is enforced anywhere but I don't think a dmabuf's segments (or entries) can refer to multiple memory regions. AFAIK, the buffer associated with a dmabuf exists entirely within a single memory region. And, when it needs to be migrated, it is moved completely.

So, given that the goal here is to identify the region the dmabuf is referring to, I think just using the first iov element to make this determination is sufficient.

Quoted 3 lines

Calling virtio_gpu_have_udmabuf() here is redundant since virtio_gpu_device_realize() already calls it.

Ok, I'll remove it.

Thanks,
Vivek

Quoted 31 lines

hw/display/virtio-gpu-udmabuf.c void virtio_gpu_init_udmabuf(struct

Quoted 20 lines
hw/display/virtio-gpu-udmabuf.c +15 -0
+            virtio_gpu_create_udmabuf(res);
+            if (res->dmabuf_fd < 0) {
+                qemu_log_mask(LOG_GUEST_ERROR,
+                              "%s: Could not create dmabuf from memfd\n",
+                              __func__);
+                return;
+            }
+        } else {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: memory region cannot be used to create dmabuf\n",
+                          __func__);
+            return;
+        }
+
+        virtio_gpu_remap_udmabuf(res, vdev);
          if (!res->remapped) {
              return;
          }
@@ -153,9 +284,7 @@ void virtio_gpu_init_udmabuf(struct

virtio_gpu_simple_resource *res)

Quoted 10 lines
hw/display/virtio-gpu-udmabuf.c +1 -3
  void virtio_gpu_fini_udmabuf(struct virtio_gpu_simple_resource *res)
  {
-    if (res->remapped) {
-        virtio_gpu_destroy_udmabuf(res);
-    }
+    virtio_gpu_destroy_udmabuf(res);
  }
 
  static void virtio_gpu_free_dmabuf(VirtIOGPU *g, VGPUDMABuf *dmabuf)
#38 lore raw

Re: [PATCH v1 7/7] virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO devices

Quoted 243 lines · Vivek

Hi Akihiko,

Quoted 15 lines

Subject: Re: [PATCH v1 7/7] virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO devices

Quoted 11 lines · Vivek Kasireddy

In addition to memfd, a blob resource can also have its backing storage in a VFIO device region. Therefore, we first need to figure out if the blob is backed by a VFIO device region or a memfd before we can call the right API to get a dmabuf fd created.

So, once we have the ramblock and the associated mr, we rely on memory_region_is_ram_device() to tell us where the backing storage is located. If the blob resource is VFIO backed, we try to find the right VFIO device that contains the blob and then invoke the API vfio_device_create_dmabuf().

Quoted 6 lines
Quoted 3 lines

Note that in virtio_gpu_remap_udmabuf(), 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.

It is odd to handle VFIO DMA-BUF in a function named "udmabuf". The function and source file need to be renamed.

Ok, makes sense. I'll rename it accordingly.

Quoted 112 lines
Quoted 10 lines
Cc Marc-André Lureau <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
---
   hw/display/Kconfig              |   5 ++
   hw/display/virtio-gpu-udmabuf.c | 143

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

hw/display/virtio-gpu-

Quoted 19 lines
   2 files changed, 141 insertions(+), 7 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-udmabuf.c +0 -0

udmabuf.c

Quoted 34 lines

index d804f321aa..bd06b4f300 100644

hw/display/virtio-gpu-udmabuf.c +19 -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,33 @@
   #include "standard-headers/linux/udmabuf.h"
   #include "standard-headers/drm/drm_fourcc.h"
 
+static void vfio_create_dmabuf(VFIODevice *vdev,
+                               struct virtio_gpu_simple_resource *res)
+{
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+    res->dmabuf_fd = vfio_device_create_dmabuf(vdev, 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 VFIODevice *vfio_device_lookup(MemoryRegion *mr)
+{
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+    VFIODevice *vdev;
+

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

Quoted 10 lines
hw/display/virtio-gpu-udmabuf.c +9 -0
+    QLIST_FOREACH(vdev, &vfio_device_list, next) {
+        if (vdev->dev == mr->dev) {
+            return vdev;
+        }
+    }
+#endif
+    return NULL;
+}
+
   static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource

*res)

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

Quoted 3 lines
hw/display/virtio-gpu-udmabuf.c +0 -0
   {
       struct udmabuf_create_list *list;
hw/display/virtio-gpu-udmabuf.c +0 -0
@@ -68,11 +96,73 @@ static void virtio_gpu_create_udmabuf(struct

virtio_gpu_simple_resource *res)

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

Quoted 4 lines
hw/display/virtio-gpu-udmabuf.c +0 -1
       g_free(list);
   }
 
-static void virtio_gpu_remap_udmabuf(struct virtio_gpu_simple_resource

*res)

Quoted 14 lines
hw/display/virtio-gpu-udmabuf.c +14 -0
+static void *vfio_dmabuf_mmap(struct virtio_gpu_simple_resource *res,
+                              VFIODevice *vdev)
+{
+    struct vfio_region_info *info;
+    ram_addr_t offset, len = 0;
+    void *map, *submap;
+    int i, ret = -1;
+    RAMBlock *rb;
+
+    /*
+     * 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.
+     */

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

Quoted 10 lines
hw/display/virtio-gpu-udmabuf.c +9 -0
+    map = mmap(NULL, res->blob_size, PROT_READ, MAP_SHARED, vdev-
 fd, 0);
+    if (map == MAP_FAILED) {
+        return map;
+    }
+
+    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();

I don't think this RCU lock is necessary. The documentation of qemu_ram_block_from_host() says:

Quoted 8 lines
Quoted 5 lines

By the time this function returns, the returned pointer is not protected by RCU anymore. If the caller is not within an RCU critical section and does not hold the BQL, it must have other means of protecting the pointer, such as a reference to the memory region that owns the RAMBlock.

This function is called with the BQL held, and a reference to the memory region is also taken in virtio_gpu_dma_memory_map().

I agree. I'll remove the RCU lock.

Quoted 96 lines
Quoted 24 lines
hw/display/virtio-gpu-udmabuf.c +23 -0
+
+        if (!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(vdev, ret, &info);
+#endif
+        if (ret < 0) {
+            goto err;
+        }
+
+        submap = mmap(map + len, res->iov[i].iov_len, PROT_READ,
+                      MAP_SHARED | MAP_FIXED, vdev->fd,
+                      info->offset + offset);
+        if (submap == MAP_FAILED) {
+            goto err;
+        }

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

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

*res,

Quoted 49 lines
hw/display/virtio-gpu-udmabuf.c +26 -3
+                                     VFIODevice *vdev)
   {
       res->remapped = mmap(NULL, res->blob_size, PROT_READ,
                            MAP_SHARED, res->dmabuf_fd, 0);
       if (res->remapped == MAP_FAILED) {
+        if (vdev) {
+            res->remapped = vfio_dmabuf_mmap(res, vdev);
+            if (res->remapped != MAP_FAILED) {
+                return;
+            }
+        }
           warn_report("%s: dmabuf mmap failed: %s", __func__,
                       strerror(errno));
           res->remapped = NULL;
@@ -130,18 +220,59 @@ bool virtio_gpu_have_udmabuf(void)
 
   void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)
   {
+    VFIODevice *vdev = NULL;
       void *pdata = NULL;
+    ram_addr_t offset;
+    RAMBlock *rb;
 
       res->dmabuf_fd = -1;
       if (res->iov_cnt == 1 &&
           res->iov[0].iov_len < 4096) {
           pdata = res->iov[0].iov_base;
       } else {
-        virtio_gpu_create_udmabuf(res);
-        if (res->dmabuf_fd < 0) {
+        rcu_read_lock();
+        rb = qemu_ram_block_from_host(res->iov[0].iov_base, false, &offset);
+        rcu_read_unlock();
+
+        if (!rb) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: Could not find ram block for host address\n",
+                          __func__);
               return;
           }
-        virtio_gpu_remap_udmabuf(res);
+
+        if (memory_region_is_ram_device(rb->mr)) {
+            vdev = vfio_device_lookup(rb->mr);
+            if (!vdev) {
+                qemu_log_mask(LOG_GUEST_ERROR,
+                              "%s: Could not find device to create dmabuf\n",
+                              __func__);
+                return;

hw/display/virtio-gpu-udmabuf.c bool virtio_gpu_have_udmabuf(void) · line 260

Quoted 10 lines
hw/display/virtio-gpu-udmabuf.c +10 -0
+            }
+
+            vfio_create_dmabuf(vdev, res);
+            if (res->dmabuf_fd < 0) {
+                qemu_log_mask(LOG_GUEST_ERROR,
+                              "%s: Could not create dmabuf from vfio device\n",
+                              __func__);
+                return;
+            }
+        } else if (memory_region_is_ram(rb->mr) &&

virtio_gpu_have_udmabuf()) {

Quoted 7 lines
Quoted 2 lines

memory_region_is_ram_device() and memory_region_is_ram() should be called for all iov elements, not just the first one.

I am not sure if it is enforced anywhere but I don't think a dmabuf's segments (or entries) can refer to multiple memory regions. AFAIK, the buffer associated with a dmabuf exists entirely within a single memory region. And, when it needs to be migrated, it is moved completely.

It should be explicitly enforced. Please refer to section "Unexpected Device Accesses" in: docs/devel/secure-coding-practices.rst

#39 lore raw

RE: [PATCH v1 7/7] virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO devices

Hi Akihiko,

Quoted 255 lines

Subject: Re: [PATCH v1 7/7] virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO devices

Quoted 92 lines · Vivek

Hi Akihiko,

Quoted 15 lines

Subject: Re: [PATCH v1 7/7] virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO devices

Quoted 11 lines · Vivek Kasireddy

In addition to memfd, a blob resource can also have its backing storage in a VFIO device region. Therefore, we first need to figure out if the blob is backed by a VFIO device region or a memfd before we can call the right API to get a dmabuf fd created.

So, once we have the ramblock and the associated mr, we rely on memory_region_is_ram_device() to tell us where the backing storage is located. If the blob resource is VFIO backed, we try to find the right VFIO device that contains the blob and then invoke the API vfio_device_create_dmabuf().

Quoted 6 lines
Quoted 3 lines

Note that in virtio_gpu_remap_udmabuf(), 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.

It is odd to handle VFIO DMA-BUF in a function named "udmabuf". The function and source file need to be renamed.

Ok, makes sense. I'll rename it accordingly.

Quoted 67 lines
Quoted 10 lines
Cc Marc-André Lureau <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
---
   hw/display/Kconfig              |   5 ++
   hw/display/virtio-gpu-udmabuf.c | 143

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

hw/display/virtio-gpu-

Quoted 19 lines
   2 files changed, 141 insertions(+), 7 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-udmabuf.c +0 -0

udmabuf.c

Quoted 36 lines

index d804f321aa..bd06b4f300 100644

hw/display/virtio-gpu-udmabuf.c +21 -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,33 @@
   #include "standard-headers/linux/udmabuf.h"
   #include "standard-headers/drm/drm_fourcc.h"
 
+static void vfio_create_dmabuf(VFIODevice *vdev,
+                               struct virtio_gpu_simple_resource *res)
+{
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+    res->dmabuf_fd = vfio_device_create_dmabuf(vdev, 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 VFIODevice *vfio_device_lookup(MemoryRegion *mr)
+{
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+    VFIODevice *vdev;
+
+    QLIST_FOREACH(vdev, &vfio_device_list, next) {
+        if (vdev->dev == mr->dev) {
Quoted 8 lines
Quoted 8 lines

hw/display/virtio-gpu-udmabuf.c

Quoted 8 lines
hw/display/virtio-gpu-udmabuf.c +7 -0
+            return vdev;
+        }
+    }
+#endif
+    return NULL;
+}
+
   static void virtio_gpu_create_udmabuf(struct

virtio_gpu_simple_resource

Quoted 9 lines
Quoted 9 lines

*res)

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

Quoted 3 lines
hw/display/virtio-gpu-udmabuf.c +0 -0
   {
       struct udmabuf_create_list *list;
hw/display/virtio-gpu-udmabuf.c +0 -0
@@ -68,11 +96,73 @@ static void virtio_gpu_create_udmabuf(struct

virtio_gpu_simple_resource *res)

Quoted 4 lines
hw/display/virtio-gpu-udmabuf.c +0 -1
       g_free(list);
   }
 
-static void virtio_gpu_remap_udmabuf(struct

virtio_gpu_simple_resource

Quoted 13 lines
Quoted 13 lines

*res)

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

Quoted 12 lines
hw/display/virtio-gpu-udmabuf.c +12 -0
+static void *vfio_dmabuf_mmap(struct virtio_gpu_simple_resource *res,
+                              VFIODevice *vdev)
+{
+    struct vfio_region_info *info;
+    ram_addr_t offset, len = 0;
+    void *map, *submap;
+    int i, ret = -1;
+    RAMBlock *rb;
+
+    /*
+     * We first reserve a contiguous chunk of address space for the entire
+     * dmabuf, then replace it with smaller mappings that correspond to

the

Quoted 11 lines
Quoted 11 lines

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

Quoted 11 lines
hw/display/virtio-gpu-udmabuf.c +10 -0
+     * individual segments of the dmabuf.
+     */
+    map = mmap(NULL, res->blob_size, PROT_READ, MAP_SHARED, vdev-
 fd, 0);
+    if (map == MAP_FAILED) {
+        return map;
+    }
+
+    for (i = 0; i < res->iov_cnt; i++) {
+        rcu_read_lock();
+        rb = qemu_ram_block_from_host(res->iov[i].iov_base, false,

&offset);

Quoted 40 lines
Quoted 4 lines

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

Quoted 1 line
hw/display/virtio-gpu-udmabuf.c +1 -0
+        rcu_read_unlock();

I don't think this RCU lock is necessary. The documentation of qemu_ram_block_from_host() says:

Quoted 8 lines
Quoted 5 lines

By the time this function returns, the returned pointer is not protected by RCU anymore. If the caller is not within an RCU critical section and does not hold the BQL, it must have other means of protecting the pointer, such as a reference to the memory region that owns the RAMBlock.

This function is called with the BQL held, and a reference to the memory region is also taken in virtio_gpu_dma_memory_map().

I agree. I'll remove the RCU lock.

Quoted 26 lines
Quoted 26 lines
hw/display/virtio-gpu-udmabuf.c +25 -0
+
+        if (!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(vdev, ret, &info);
+#endif
+        if (ret < 0) {
+            goto err;
+        }
+
+        submap = mmap(map + len, res->iov[i].iov_len, PROT_READ,
+                      MAP_SHARED | MAP_FIXED, vdev->fd,
+                      info->offset + offset);
+        if (submap == MAP_FAILED) {
+            goto err;
+        }
+
+        len += res->iov[i].iov_len;
Quoted 8 lines
Quoted 8 lines

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

Quoted 8 lines
hw/display/virtio-gpu-udmabuf.c +8 -0
+    }
+    return map;
+err:
+    munmap(map, res->blob_size);
+    return MAP_FAILED;
+}
+
+static void virtio_gpu_remap_udmabuf(struct

virtio_gpu_simple_resource

Quoted 24 lines
Quoted 24 lines

*res,

Quoted 23 lines
hw/display/virtio-gpu-udmabuf.c +10 -0
+                                     VFIODevice *vdev)
   {
       res->remapped = mmap(NULL, res->blob_size, PROT_READ,
                            MAP_SHARED, res->dmabuf_fd, 0);
       if (res->remapped == MAP_FAILED) {
+        if (vdev) {
+            res->remapped = vfio_dmabuf_mmap(res, vdev);
+            if (res->remapped != MAP_FAILED) {
+                return;
+            }
+        }
           warn_report("%s: dmabuf mmap failed: %s", __func__,
                       strerror(errno));
           res->remapped = NULL;
@@ -130,18 +220,59 @@ bool virtio_gpu_have_udmabuf(void)
 
   void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)
   {
+    VFIODevice *vdev = NULL;
       void *pdata = NULL;
+    ram_addr_t offset;
+    RAMBlock *rb;
 
Quoted 9 lines
Quoted 9 lines

hw/display/virtio-gpu-udmabuf.c bool virtio_gpu_have_udmabuf(void)

Quoted 9 lines
hw/display/virtio-gpu-udmabuf.c +2 -2
       res->dmabuf_fd = -1;
       if (res->iov_cnt == 1 &&
           res->iov[0].iov_len < 4096) {
           pdata = res->iov[0].iov_base;
       } else {
-        virtio_gpu_create_udmabuf(res);
-        if (res->dmabuf_fd < 0) {
+        rcu_read_lock();
+        rb = qemu_ram_block_from_host(res->iov[0].iov_base, false,

&offset);

Quoted 31 lines
Quoted 31 lines
Quoted 17 lines
hw/display/virtio-gpu-udmabuf.c +14 -1
+        rcu_read_unlock();
+
+        if (!rb) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: Could not find ram block for host address\n",
+                          __func__);
               return;
           }
-        virtio_gpu_remap_udmabuf(res);
+
+        if (memory_region_is_ram_device(rb->mr)) {
+            vdev = vfio_device_lookup(rb->mr);
+            if (!vdev) {
+                qemu_log_mask(LOG_GUEST_ERROR,
+                              "%s: Could not find device to create dmabuf\n",
+                              __func__);
+                return;

hw/display/virtio-gpu-udmabuf.c bool virtio_gpu_have_udmabuf(void)

Quoted 10 lines
hw/display/virtio-gpu-udmabuf.c +10 -0
+            }
+
+            vfio_create_dmabuf(vdev, res);
+            if (res->dmabuf_fd < 0) {
+                qemu_log_mask(LOG_GUEST_ERROR,
+                              "%s: Could not create dmabuf from vfio device\n",
+                              __func__);
+                return;
+            }
+        } else if (memory_region_is_ram(rb->mr) &&

virtio_gpu_have_udmabuf()) {

memory_region_is_ram_device() and memory_region_is_ram() should be called for all iov elements, not just the first one.

Quoted 8 lines
Quoted 5 lines

I am not sure if it is enforced anywhere but I don't think a dmabuf's segments (or entries) can refer to multiple memory regions. AFAIK, the buffer associated with a dmabuf exists entirely within a single memory region. And, when it needs to be migrated, it is moved completely.

It should be explicitly enforced. Please refer to section "Unexpected Device Accesses" in: docs/devel/secure-coding-practices.rst

Ok, I'll add a helper function to check whether all the entries belong to a single memory region or not.

Thanks,
Vivek

#40 lore raw

Re: [PATCH v1 7/7] virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO devices

Quoted 263 lines · Vivek

Hi Akihiko,

Quoted 261 lines

Subject: Re: [PATCH v1 7/7] virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO devices

Quoted 92 lines · Vivek

Hi Akihiko,

Quoted 15 lines

Subject: Re: [PATCH v1 7/7] virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO devices

Quoted 11 lines · Vivek Kasireddy

In addition to memfd, a blob resource can also have its backing storage in a VFIO device region. Therefore, we first need to figure out if the blob is backed by a VFIO device region or a memfd before we can call the right API to get a dmabuf fd created.

So, once we have the ramblock and the associated mr, we rely on memory_region_is_ram_device() to tell us where the backing storage is located. If the blob resource is VFIO backed, we try to find the right VFIO device that contains the blob and then invoke the API vfio_device_create_dmabuf().

Quoted 6 lines
Quoted 3 lines

Note that in virtio_gpu_remap_udmabuf(), 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.

It is odd to handle VFIO DMA-BUF in a function named "udmabuf". The function and source file need to be renamed.

Ok, makes sense. I'll rename it accordingly.

Quoted 67 lines
Quoted 10 lines
Cc Marc-André Lureau <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
---
    hw/display/Kconfig              |   5 ++
    hw/display/virtio-gpu-udmabuf.c | 143

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

hw/display/virtio-gpu-

Quoted 19 lines
    2 files changed, 141 insertions(+), 7 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-udmabuf.c +0 -0

udmabuf.c

Quoted 36 lines

index d804f321aa..bd06b4f300 100644

hw/display/virtio-gpu-udmabuf.c +21 -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,33 @@
    #include "standard-headers/linux/udmabuf.h"
    #include "standard-headers/drm/drm_fourcc.h"
 
+static void vfio_create_dmabuf(VFIODevice *vdev,
+                               struct virtio_gpu_simple_resource *res)
+{
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+    res->dmabuf_fd = vfio_device_create_dmabuf(vdev, 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 VFIODevice *vfio_device_lookup(MemoryRegion *mr)
+{
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+    VFIODevice *vdev;
+
+    QLIST_FOREACH(vdev, &vfio_device_list, next) {
+        if (vdev->dev == mr->dev) {
Quoted 8 lines
Quoted 8 lines

hw/display/virtio-gpu-udmabuf.c

Quoted 8 lines
hw/display/virtio-gpu-udmabuf.c +7 -0
+            return vdev;
+        }
+    }
+#endif
+    return NULL;
+}
+
    static void virtio_gpu_create_udmabuf(struct

virtio_gpu_simple_resource

Quoted 9 lines
Quoted 9 lines

*res)

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

Quoted 3 lines
hw/display/virtio-gpu-udmabuf.c +0 -0
    {
        struct udmabuf_create_list *list;
hw/display/virtio-gpu-udmabuf.c +0 -0
@@ -68,11 +96,73 @@ static void virtio_gpu_create_udmabuf(struct

virtio_gpu_simple_resource *res)

Quoted 4 lines
hw/display/virtio-gpu-udmabuf.c +0 -1
        g_free(list);
    }
 
-static void virtio_gpu_remap_udmabuf(struct

virtio_gpu_simple_resource

Quoted 13 lines
Quoted 13 lines

*res)

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

Quoted 12 lines
hw/display/virtio-gpu-udmabuf.c +12 -0
+static void *vfio_dmabuf_mmap(struct virtio_gpu_simple_resource *res,
+                              VFIODevice *vdev)
+{
+    struct vfio_region_info *info;
+    ram_addr_t offset, len = 0;
+    void *map, *submap;
+    int i, ret = -1;
+    RAMBlock *rb;
+
+    /*
+     * We first reserve a contiguous chunk of address space for the entire
+     * dmabuf, then replace it with smaller mappings that correspond to

the

Quoted 11 lines
Quoted 11 lines

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

Quoted 11 lines
hw/display/virtio-gpu-udmabuf.c +10 -0
+     * individual segments of the dmabuf.
+     */
+    map = mmap(NULL, res->blob_size, PROT_READ, MAP_SHARED, vdev-
 fd, 0);
+    if (map == MAP_FAILED) {
+        return map;
+    }
+
+    for (i = 0; i < res->iov_cnt; i++) {
+        rcu_read_lock();
+        rb = qemu_ram_block_from_host(res->iov[i].iov_base, false,

&offset);

Quoted 40 lines
Quoted 4 lines

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

Quoted 1 line
hw/display/virtio-gpu-udmabuf.c +1 -0
+        rcu_read_unlock();

I don't think this RCU lock is necessary. The documentation of qemu_ram_block_from_host() says:

Quoted 8 lines
Quoted 5 lines

By the time this function returns, the returned pointer is not protected by RCU anymore. If the caller is not within an RCU critical section and does not hold the BQL, it must have other means of protecting the pointer, such as a reference to the memory region that owns the RAMBlock.

This function is called with the BQL held, and a reference to the memory region is also taken in virtio_gpu_dma_memory_map().

I agree. I'll remove the RCU lock.

Quoted 26 lines
Quoted 26 lines
hw/display/virtio-gpu-udmabuf.c +25 -0
+
+        if (!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(vdev, ret, &info);
+#endif
+        if (ret < 0) {
+            goto err;
+        }
+
+        submap = mmap(map + len, res->iov[i].iov_len, PROT_READ,
+                      MAP_SHARED | MAP_FIXED, vdev->fd,
+                      info->offset + offset);
+        if (submap == MAP_FAILED) {
+            goto err;
+        }
+
+        len += res->iov[i].iov_len;
Quoted 8 lines
Quoted 8 lines

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

Quoted 8 lines
hw/display/virtio-gpu-udmabuf.c +8 -0
+    }
+    return map;
+err:
+    munmap(map, res->blob_size);
+    return MAP_FAILED;
+}
+
+static void virtio_gpu_remap_udmabuf(struct

virtio_gpu_simple_resource

Quoted 24 lines
Quoted 24 lines

*res,

Quoted 23 lines
hw/display/virtio-gpu-udmabuf.c +10 -0
+                                     VFIODevice *vdev)
    {
        res->remapped = mmap(NULL, res->blob_size, PROT_READ,
                             MAP_SHARED, res->dmabuf_fd, 0);
        if (res->remapped == MAP_FAILED) {
+        if (vdev) {
+            res->remapped = vfio_dmabuf_mmap(res, vdev);
+            if (res->remapped != MAP_FAILED) {
+                return;
+            }
+        }
            warn_report("%s: dmabuf mmap failed: %s", __func__,
                        strerror(errno));
            res->remapped = NULL;
@@ -130,18 +220,59 @@ bool virtio_gpu_have_udmabuf(void)
 
    void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)
    {
+    VFIODevice *vdev = NULL;
        void *pdata = NULL;
+    ram_addr_t offset;
+    RAMBlock *rb;
 
Quoted 9 lines
Quoted 9 lines

hw/display/virtio-gpu-udmabuf.c bool virtio_gpu_have_udmabuf(void)

Quoted 9 lines
hw/display/virtio-gpu-udmabuf.c +2 -2
        res->dmabuf_fd = -1;
        if (res->iov_cnt == 1 &&
            res->iov[0].iov_len < 4096) {
            pdata = res->iov[0].iov_base;
        } else {
-        virtio_gpu_create_udmabuf(res);
-        if (res->dmabuf_fd < 0) {
+        rcu_read_lock();
+        rb = qemu_ram_block_from_host(res->iov[0].iov_base, false,

&offset);

Quoted 36 lines
Quoted 29 lines
Quoted 17 lines
hw/display/virtio-gpu-udmabuf.c +14 -1
+        rcu_read_unlock();
+
+        if (!rb) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: Could not find ram block for host address\n",
+                          __func__);
                return;
            }
-        virtio_gpu_remap_udmabuf(res);
+
+        if (memory_region_is_ram_device(rb->mr)) {
+            vdev = vfio_device_lookup(rb->mr);
+            if (!vdev) {
+                qemu_log_mask(LOG_GUEST_ERROR,
+                              "%s: Could not find device to create dmabuf\n",
+                              __func__);
+                return;

hw/display/virtio-gpu-udmabuf.c bool virtio_gpu_have_udmabuf(void)

Quoted 10 lines
hw/display/virtio-gpu-udmabuf.c +10 -0
+            }
+
+            vfio_create_dmabuf(vdev, res);
+            if (res->dmabuf_fd < 0) {
+                qemu_log_mask(LOG_GUEST_ERROR,
+                              "%s: Could not create dmabuf from vfio device\n",
+                              __func__);
+                return;
+            }
+        } else if (memory_region_is_ram(rb->mr) &&

virtio_gpu_have_udmabuf()) {

Quoted 2 lines

memory_region_is_ram_device() and memory_region_is_ram() should be called for all iov elements, not just the first one.

I am not sure if it is enforced anywhere but I don't think a dmabuf's segments (or entries) can refer to multiple memory regions. AFAIK, the buffer associated with a dmabuf exists entirely within a single memory region. And, when it needs to be migrated, it is moved completely.

Quoted 4 lines
Quoted 2 lines

It should be explicitly enforced. Please refer to section "Unexpected Device Accesses" in: docs/devel/secure-coding-practices.rst

Ok, I'll add a helper function to check whether all the entries belong to a single memory region or not.

Entries do not have to belong to a single memory region. The existing code allows creating a blob composed of multiple memfds so ideally the behavior should be preserved.

I also think it's better to test the requirement in vfio_device_create_dmabuf() and virtio_gpu_create_udmabuf() instead of adding another function. This way, you can avoid enumerating entries again and prevent a mistake to forget checking memory regions before creating DMA-BUF.

Regards,
Akihiko Odaki

#41 lore raw

RE: [PATCH v1 7/7] virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO devices

Hi Akihiko,

Quoted 272 lines

Subject: Re: [PATCH v1 7/7] virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO devices

Quoted 119 lines · Vivek

Hi Akihiko,

Quoted 117 lines

Subject: Re: [PATCH v1 7/7] virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO devices

Quoted 92 lines · Vivek

Hi Akihiko,

Quoted 15 lines

Subject: Re: [PATCH v1 7/7] virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO devices

Quoted 11 lines · Vivek Kasireddy

In addition to memfd, a blob resource can also have its backing storage in a VFIO device region. Therefore, we first need to figure out if the blob is backed by a VFIO device region or a memfd before we can call the right API to get a dmabuf fd created.

So, once we have the ramblock and the associated mr, we rely on memory_region_is_ram_device() to tell us where the backing storage is located. If the blob resource is VFIO backed, we try to find the right VFIO device that contains the blob and then invoke the API vfio_device_create_dmabuf().

Quoted 6 lines
Quoted 3 lines

Note that in virtio_gpu_remap_udmabuf(), 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.

It is odd to handle VFIO DMA-BUF in a function named "udmabuf". The function and source file need to be renamed.

Ok, makes sense. I'll rename it accordingly.

Quoted 67 lines
Quoted 10 lines
Cc Marc-André Lureau <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Dmitry Osipenko <[email protected]>
---
    hw/display/Kconfig              |   5 ++
    hw/display/virtio-gpu-udmabuf.c | 143

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

hw/display/virtio-gpu-

Quoted 19 lines
    2 files changed, 141 insertions(+), 7 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-udmabuf.c +0 -0

udmabuf.c

Quoted 36 lines

index d804f321aa..bd06b4f300 100644

hw/display/virtio-gpu-udmabuf.c +21 -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,33 @@
    #include "standard-headers/linux/udmabuf.h"
    #include "standard-headers/drm/drm_fourcc.h"
 
+static void vfio_create_dmabuf(VFIODevice *vdev,
+                               struct virtio_gpu_simple_resource *res)
+{
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+    res->dmabuf_fd = vfio_device_create_dmabuf(vdev, 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 VFIODevice *vfio_device_lookup(MemoryRegion *mr)
+{
+#if defined(VIRTIO_GPU_VFIO_BLOB)
+    VFIODevice *vdev;
+
+    QLIST_FOREACH(vdev, &vfio_device_list, next) {
+        if (vdev->dev == mr->dev) {
Quoted 8 lines
Quoted 8 lines

hw/display/virtio-gpu-udmabuf.c

Quoted 8 lines
hw/display/virtio-gpu-udmabuf.c +7 -0
+            return vdev;
+        }
+    }
+#endif
+    return NULL;
+}
+
    static void virtio_gpu_create_udmabuf(struct

virtio_gpu_simple_resource

Quoted 9 lines
Quoted 9 lines

*res)

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

Quoted 3 lines
hw/display/virtio-gpu-udmabuf.c +0 -0
    {
        struct udmabuf_create_list *list;
hw/display/virtio-gpu-udmabuf.c +0 -0
@@ -68,11 +96,73 @@ static void virtio_gpu_create_udmabuf(struct

virtio_gpu_simple_resource *res)

Quoted 4 lines
hw/display/virtio-gpu-udmabuf.c +0 -1
        g_free(list);
    }
 
-static void virtio_gpu_remap_udmabuf(struct

virtio_gpu_simple_resource

Quoted 2 lines
Quoted 2 lines

*res)

Quoted 1 line
hw/display/virtio-gpu-udmabuf.c +1 -0
+static void *vfio_dmabuf_mmap(struct virtio_gpu_simple_resource

*res,

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

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

Quoted 10 lines
hw/display/virtio-gpu-udmabuf.c +10 -0
+                              VFIODevice *vdev)
+{
+    struct vfio_region_info *info;
+    ram_addr_t offset, len = 0;
+    void *map, *submap;
+    int i, ret = -1;
+    RAMBlock *rb;
+
+    /*
+     * We first reserve a contiguous chunk of address space for the

entire

Quoted 5 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line

hw/display/virtio-gpu-udmabuf.c static void virtio_gpu_create_udmabuf(struct · line 110

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

the

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

vdev-

Quoted 75 lines
Quoted 75 lines
Quoted 8 lines
Quoted 8 lines

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

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

&offset);

Quoted 40 lines
Quoted 4 lines

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

Quoted 1 line
hw/display/virtio-gpu-udmabuf.c +1 -0
+        rcu_read_unlock();

I don't think this RCU lock is necessary. The documentation of qemu_ram_block_from_host() says:

Quoted 8 lines
Quoted 5 lines

By the time this function returns, the returned pointer is not protected by RCU anymore. If the caller is not within an RCU critical section and does not hold the BQL, it must have other means of protecting the pointer, such as a reference to the memory region that owns the RAMBlock.

This function is called with the BQL held, and a reference to the memory region is also taken in virtio_gpu_dma_memory_map().

I agree. I'll remove the RCU lock.

Quoted 26 lines
Quoted 26 lines
hw/display/virtio-gpu-udmabuf.c +25 -0
+
+        if (!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(vdev, ret, &info);
+#endif
+        if (ret < 0) {
+            goto err;
+        }
+
+        submap = mmap(map + len, res->iov[i].iov_len, PROT_READ,
+                      MAP_SHARED | MAP_FIXED, vdev->fd,
+                      info->offset + offset);
+        if (submap == MAP_FAILED) {
+            goto err;
+        }
+
+        len += res->iov[i].iov_len;
Quoted 8 lines
Quoted 8 lines

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

Quoted 8 lines
hw/display/virtio-gpu-udmabuf.c +8 -0
+    }
+    return map;
+err:
+    munmap(map, res->blob_size);
+    return MAP_FAILED;
+}
+
+static void virtio_gpu_remap_udmabuf(struct

virtio_gpu_simple_resource

Quoted 17 lines
Quoted 17 lines

*res,

Quoted 16 lines
hw/display/virtio-gpu-udmabuf.c +7 -0
+                                     VFIODevice *vdev)
    {
        res->remapped = mmap(NULL, res->blob_size, PROT_READ,
                             MAP_SHARED, res->dmabuf_fd, 0);
        if (res->remapped == MAP_FAILED) {
+        if (vdev) {
+            res->remapped = vfio_dmabuf_mmap(res, vdev);
+            if (res->remapped != MAP_FAILED) {
+                return;
+            }
+        }
            warn_report("%s: dmabuf mmap failed: %s", __func__,
                        strerror(errno));
            res->remapped = NULL;
@@ -130,18 +220,59 @@ bool virtio_gpu_have_udmabuf(void)
 
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

hw/display/virtio-gpu-udmabuf.c bool virtio_gpu_have_udmabuf(void) · line 221

Quoted 1 line
hw/display/virtio-gpu-udmabuf.c +0 -0
    void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource

*res)

Quoted 45 lines
Quoted 45 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
hw/display/virtio-gpu-udmabuf.c +3 -0
    {
+    VFIODevice *vdev = NULL;
        void *pdata = NULL;
+    ram_addr_t offset;
+    RAMBlock *rb;
 
Quoted 9 lines
Quoted 9 lines

hw/display/virtio-gpu-udmabuf.c bool virtio_gpu_have_udmabuf(void)

Quoted 9 lines
hw/display/virtio-gpu-udmabuf.c +2 -2
        res->dmabuf_fd = -1;
        if (res->iov_cnt == 1 &&
            res->iov[0].iov_len < 4096) {
            pdata = res->iov[0].iov_base;
        } else {
-        virtio_gpu_create_udmabuf(res);
-        if (res->dmabuf_fd < 0) {
+        rcu_read_lock();
+        rb = qemu_ram_block_from_host(res->iov[0].iov_base, false,

&offset);

Quoted 29 lines
Quoted 29 lines
Quoted 17 lines
hw/display/virtio-gpu-udmabuf.c +14 -1
+        rcu_read_unlock();
+
+        if (!rb) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: Could not find ram block for host address\n",
+                          __func__);
                return;
            }
-        virtio_gpu_remap_udmabuf(res);
+
+        if (memory_region_is_ram_device(rb->mr)) {
+            vdev = vfio_device_lookup(rb->mr);
+            if (!vdev) {
+                qemu_log_mask(LOG_GUEST_ERROR,
+                              "%s: Could not find device to create dmabuf\n",
+                              __func__);
+                return;

hw/display/virtio-gpu-udmabuf.c bool virtio_gpu_have_udmabuf(void)

Quoted 10 lines
hw/display/virtio-gpu-udmabuf.c +10 -0
+            }
+
+            vfio_create_dmabuf(vdev, res);
+            if (res->dmabuf_fd < 0) {
+                qemu_log_mask(LOG_GUEST_ERROR,
+                              "%s: Could not create dmabuf from vfio device\n",
+                              __func__);
+                return;
+            }
+        } else if (memory_region_is_ram(rb->mr) &&

virtio_gpu_have_udmabuf()) {

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

memory_region_is_ram_device() and memory_region_is_ram() should

be

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 1 line

called for all iov elements, not just the first one.

I am not sure if it is enforced anywhere but I don't think a dmabuf's segments (or entries) can refer to multiple memory regions. AFAIK, the buffer associated with a dmabuf exists entirely within a single memory region. And, when it needs to be migrated, it is moved completely.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

It should be explicitly enforced. Please refer to section "Unexpected Device Accesses" in: docs/devel/secure-coding-practices.rst

Ok, I'll add a helper function to check whether all the entries belong to a single memory region or not.

Entries do not have to belong to a single memory region. The existing code allows creating a blob composed of multiple memfds so ideally the behavior should be preserved.

The ability to create a blob/dmabuf from multiple memfds does not exist by design and AFAICT, it never actually happens practically.

Quoted 3 lines

I also think it's better to test the requirement in vfio_device_create_dmabuf() and virtio_gpu_create_udmabuf() instead of

Since its the res->iov entries that need to be checked, I am thinking of adding a helper in util/iov.c and having both vfio_device_create_dmabuf() and virtio_gpu_create_udmabuf() call the helper function.

Thanks,
Vivek

Quoted 6 lines

adding another function. This way, you can avoid enumerating entries again and prevent a mistake to forget checking memory regions before creating DMA-BUF.

Regards,
Akihiko Odaki

#42 lore raw
In reply to Vivek Kasireddy

Vivek,

Quoted 27 lines · Vivek Kasireddy

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 implement VFIO_DEVICE_FEATURE_DMA_BUF and leverage it in virtio-gpu.

So, while creating the blobs we use memory_region_is_ram_device() to figure out if the blob is backed by memfd or a vfio-pci device. If it is determined that the blob is backed by vfio-pci device region, instead of calling into udmabuf driver to create dmabuf we would now call into vfio-pci driver to have a dmabuf created on the Host.

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

Quoted 1 line

Tested with an SRIOV enabled Intel dGPU by running Gnome Wayland

Could you please be more precise on the HW ?

Quoted 32 lines

(in the VM) and Qemu with the following (relevant) parameters:

(quoted diff) +0 -3
-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 vfio-pci kernel driver series:
https://lore.kernel.org/dri-devel/[email protected]/
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: Leon Romanovsky <[email protected]>
Cc: Leon Romanovsky <[email protected]>
Cc: Jason Gunthorpe <[email protected]>
Cc: Dongwon Kim <[email protected]>

Vivek Kasireddy (7):
virtio-gpu: Recreate the resource's dmabuf if new backing is attached
virtio-gpu: Don't rely on res->blob to identify blob resources
virtio-gpu: Find hva for Guest's DMA addr associated with a ram device vfio/region: Add a helper to get region index from memory region
linux-headers: Update vfio.h to include VFIO_DEVICE_FEATURE_DMA_BUF vfio/device: Add support for VFIO_DEVICE_FEATURE_DMA_BUF
virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO
devices

Quoted 9 lines
  hw/display/Kconfig              |   5 ++
  hw/display/virtio-gpu-udmabuf.c | 143 ++++++++++++++++++++++++++++++--
  hw/display/virtio-gpu.c         |  53 +++++++++---
  hw/vfio/device.c                |  43 ++++++++++
  hw/vfio/region.c                |  14 ++++
  include/hw/vfio/vfio-device.h   |   5 ++
  linux-headers/linux/vfio.h      |  25 ++++++
  7 files changed, 270 insertions(+), 18 deletions(-)

Please cc: Alex and I on all patches.

Thanks,

C.

#43 lore raw

Hi Cedric,

Quoted 33 lines

Subject: Re: [PATCH v1 0/7] vfio: Implement VFIO_DEVICE_FEATURE_DMA_BUF and use it in virtio-gpu

Vivek,

Quoted 27 lines · Vivek Kasireddy

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 implement VFIO_DEVICE_FEATURE_DMA_BUF and leverage it in virtio-gpu.

So, while creating the blobs we use memory_region_is_ram_device() to figure out if the blob is backed by memfd or a vfio-pci device. If it is determined that the blob is backed by vfio-pci device region, instead of calling into udmabuf driver to create dmabuf we would now call into vfio-pci driver to have a dmabuf created on the Host.

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

Quoted 3 lines
Quoted 1 line

Tested with an SRIOV enabled Intel dGPU by running Gnome Wayland

Could you please be more precise on the HW ?

Ok, I'll mention the specific model.

Quoted 45 lines
Quoted 10 lines

(in the VM) and Qemu with the following (relevant) parameters:

(quoted diff) +0 -3
-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 vfio-pci kernel driver series:
https://lore.kernel.org/dri-devel/[email protected]/
Associated virtio-gpu kernel driver series (merged):
https://lore.kernel.org/dri-devel/20241126031643.3490496-1-

[email protected]/

Quoted 23 lines

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

Vivek Kasireddy (7):
virtio-gpu: Recreate the resource's dmabuf if new backing is attached
virtio-gpu: Don't rely on res->blob to identify blob resources
virtio-gpu: Find hva for Guest's DMA addr associated with a ram device vfio/region: Add a helper to get region index from memory region
linux-headers: Update vfio.h to include VFIO_DEVICE_FEATURE_DMA_BUF vfio/device: Add support for VFIO_DEVICE_FEATURE_DMA_BUF
virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO
devices

Quoted 2 lines
  hw/display/Kconfig              |   5 ++
  hw/display/virtio-gpu-udmabuf.c | 143

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

Quoted 7 lines
  hw/display/virtio-gpu.c         |  53 +++++++++---
  hw/vfio/device.c                |  43 ++++++++++
  hw/vfio/region.c                |  14 ++++
  include/hw/vfio/vfio-device.h   |   5 ++
  linux-headers/linux/vfio.h      |  25 ++++++
  7 files changed, 270 insertions(+), 18 deletions(-)
Quoted 1 line

Please cc: Alex and I on all patches.

Sure, will do.

Thanks,
Vivek

Quoted 4 lines

Thanks,

C.