virtio-gpu: Fix scanout dmabuf cleanup during resource destruction
PATCH 1/1 virtio-gpu: Fix scanout dmabuf cleanup during resource destruction
From: Dongwon Kim <[email protected]>
When a virtio-gpu resource is destroyed, any associated udmabuf must be properly torn down. Currently, the code may leave dangling references to dmabuf file descriptors in the scanout primary buffers.
This patch updates virtio_gpu_fini_udmabuf to:
1. Iterate through all active scanouts.
2. Identify dmabufs that match the resource's file descriptor.
3. Close the dmabuf and invalidate the resource's FD reference to prevent use-after-free or double-close scenarios.
4. Finally, trigger the underlying udmabuf destruction.
This ensures that the display backend does not attempt to access memory or FDs that have been released by the guest or the host.
--- include/hw/virtio/virtio-gpu.h | 3 ++- hw/display/virtio-gpu-udmabuf.c | 25 ++++++++++++++++++------- hw/display/virtio-gpu.c | 2 +- 3 files changed, 21 insertions(+), 9 deletions(-)
include/hw/virtio/virtio-gpu.h +2 -1
@@ -357,7 +357,8 @@ bool virtio_gpu_scanout_blob_to_fb(struct virtio_gpu_framebuffer *fb,/* virtio-gpu-udmabuf.c */ bool virtio_gpu_have_udmabuf (void );void virtio_gpu_init_udmabuf (struct virtio_gpu_simple_resource *res); -void virtio_gpu_fini_udmabuf (struct virtio_gpu_simple_resource *res); +void virtio_gpu_fini_udmabuf (VirtIOGPU *g, +struct virtio_gpu_simple_resource *res); int virtio_gpu_update_dmabuf(VirtIOGPU *g, uint32_t scanout_id, struct virtio_gpu_simple_resource *res,
hw/display/virtio-gpu-udmabuf.c +18 -7
@@ -151,13 +151,6 @@ void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res) res->blob = pdata; } -void virtio_gpu_fini_udmabuf (struct virtio_gpu_simple_resource *res) -{ -if (res->remapped) { -virtio_gpu_destroy_udmabuf (res); - } -} -static void virtio_gpu_free_dmabuf (VirtIOGPU *g,VGPUDMABuf *dmabuf) {struct virtio_gpu_scanout *scanout;
@@ -169,6 +162,24 @@ static void virtio_gpu_free_dmabuf(VirtIOGPU *g, VGPUDMABuf *dmabuf)g_free (dmabuf); } +void virtio_gpu_fini_udmabuf (VirtIOGPU *g,struct virtio_gpu_simple_resource *res) +{ +int max_outputs = g->parent_obj.conf.max_outputs; +int i; + +for (i =0 ; i < max_outputs; i++) { +VGPUDMABuf *dmabuf = g->dmabuf.primary[i]; + +if (dmabuf && (res->dmabuf_fd !=-1 ) && +qemu_dmabuf_get_fds (dmabuf->buf,NULL )[0 ] == res->dmabuf_fd) { +qemu_dmabuf_close (dmabuf->buf); + res->dmabuf_fd =-1 ; + } + } + +virtio_gpu_destroy_udmabuf (res); +} +static VGPUDMABuf *virtio_gpu_create_dmabuf(VirtIOGPU *g, uint32_t scanout_id,
hw/display/virtio-gpu.c +1 -1
@@ -902,7 +902,7 @@ void virtio_gpu_cleanup_mapping(VirtIOGPU *g, res->addrs =NULL ;if (res->blob) { -virtio_gpu_fini_udmabuf (res); +virtio_gpu_fini_udmabuf (g, res); } }
Signature · 1 line
2.43.0
Hi
Quoted 14 lines · On Tue, Mar 3, 2026 at 2:06 AM <[email protected]> wrote:
From: Dongwon Kim <[email protected]>
When a virtio-gpu resource is destroyed, any associated udmabuf must be properly torn down. Currently, the code may leave dangling references to dmabuf file descriptors in the scanout primary buffers.
This patch updates
virtio_gpu_fini_udmabufto:
1. Iterate through all active scanouts.
2. Identify dmabufs that match the resource's file descriptor.
3. Close the dmabuf and invalidate the resource's FD reference to prevent use-after-free or double-close scenarios.
4. Finally, trigger the underlying udmabuf destruction.
Quoted 7 lines
This ensures that the display backend does not attempt to access memory or FDs that have been released by the guest or the host.
Cc Gerd Hoffmann <[email protected]>Cc Marc-André Lureau <[email protected]>Signed-off-by Vivek Kasireddy <[email protected]>Signed-off-by Dongwon Kim <[email protected]>
Quoted 41 lines
--- include/hw/virtio/virtio-gpu.h | 3 ++- hw/display/virtio-gpu-udmabuf.c | 25 ++++++++++++++++++------- hw/display/virtio-gpu.c | 2 +- 3 files changed, 21 insertions(+), 9 deletions(-)include/hw/virtio/virtio-gpu.h +2
-1@@ -357,7 +357,8 @@ bool virtio_gpu_scanout_blob_to_fb(struct virtio_gpu_framebuffer *fb,/* virtio-gpu-udmabuf.c */ bool virtio_gpu_have_udmabuf (void );void virtio_gpu_init_udmabuf (struct virtio_gpu_simple_resource *res); -void virtio_gpu_fini_udmabuf (struct virtio_gpu_simple_resource *res); +void virtio_gpu_fini_udmabuf (VirtIOGPU *g, +struct virtio_gpu_simple_resource *res); int virtio_gpu_update_dmabuf(VirtIOGPU *g, uint32_t scanout_id, struct virtio_gpu_simple_resource *res,hw/display/virtio-gpu-udmabuf.c +0
-7@@ -151,13 +151,6 @@ void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res) res->blob = pdata; } -void virtio_gpu_fini_udmabuf (struct virtio_gpu_simple_resource *res) -{ -if (res->remapped) { -virtio_gpu_destroy_udmabuf (res); - } -} -static void virtio_gpu_free_dmabuf (VirtIOGPU *g,VGPUDMABuf *dmabuf) {struct virtio_gpu_scanout *scanout;@@ -169,6 +162,24 @@ static void virtio_gpu_free_dmabuf(VirtIOGPU *g, VGPUDMABuf *dmabuf)g_free (dmabuf); }
hw/display/virtio-gpu-udmabuf.c static void virtio_gpu_free_dmabuf(VirtIOGPU *g, VGPUDMABuf *dmabuf) · line 173
Quoted 10 lines
hw/display/virtio-gpu-udmabuf.c +9
-0+void virtio_gpu_fini_udmabuf (VirtIOGPU *g,struct virtio_gpu_simple_resource *res) +{ +int max_outputs = g->parent_obj.conf.max_outputs; +int i; + +for (i =0 ; i < max_outputs; i++) { +VGPUDMABuf *dmabuf = g->dmabuf.primary[i]; + + if (dmabuf && (res->dmabuf_fd != -1) &&
Maybe add qemu_dmabuf_get_numplanes() > 0 ?
hw/display/virtio-gpu-udmabuf.c static void virtio_gpu_free_dmabuf(VirtIOGPU *g, VGPUDMABuf *dmabuf) · line 176
Quoted 3 lines
hw/display/virtio-gpu-udmabuf.c +3
-0+ qemu_dmabuf_get_fds(dmabuf->buf, NULL)[0] == res->dmabuf_fd) { +qemu_dmabuf_close (dmabuf->buf); + res->dmabuf_fd =-1 ;
I am not really happy about that we close the underlying fd here before the next destroy, but I don't have an immediate solution
Quoted 26 lines
hw/display/virtio-gpu-udmabuf.c +6
-0+ } + } + +virtio_gpu_destroy_udmabuf (res); +} +static VGPUDMABuf *virtio_gpu_create_dmabuf(VirtIOGPU *g, uint32_t scanout_id,hw/display/virtio-gpu.c +1
-2@@ -902,7 +902,7 @@ void virtio_gpu_cleanup_mapping(VirtIOGPU *g, res->addrs =NULL ;if (res->blob) { -virtio_gpu_fini_udmabuf (res); +virtio_gpu_fini_udmabuf (g, res); } } --2.43.0
Signature · 1 line
Marc-André Lureau
Alex Bennée
AppliedQuoted 9 lines · Marc-André Lureau
Hi
Quoted 6 lines · On Tue, Mar 3, 2026 at 2:06 AM <[email protected]> wrote:
From: Dongwon Kim <[email protected]>
When a virtio-gpu resource is destroyed, any associated udmabuf must be properly torn down. Currently, the code may leave dangling references to dmabuf file descriptors in the scanout primary buffers.
Queued to virtio-gpu/next, thanks.
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro
Hi Marc-André,
Quoted 79 lines
-----Original Message-----
From: Marc-André Lureau <[email protected]>
Sent: Tuesday, March 3, 2026 8:28 AM
To: Kim, Dongwon <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] virtio-gpu: Fix scanout dmabuf cleanup during resource destructionHi
Quoted 14 lines · On Tue, Mar 3, 2026 at 2:06 AM <[email protected]> wrote:
From: Dongwon Kim <[email protected]>
When a virtio-gpu resource is destroyed, any associated udmabuf must be properly torn down. Currently, the code may leave dangling references to dmabuf file descriptors in the scanout primary buffers.
This patch updates
virtio_gpu_fini_udmabufto:
1. Iterate through all active scanouts.
2. Identify dmabufs that match the resource's file descriptor.
3. Close the dmabuf and invalidate the resource's FD reference to prevent use-after-free or double-close scenarios.
4. Finally, trigger the underlying udmabuf destruction.Quoted 7 lines
This ensures that the display backend does not attempt to access memory or FDs that have been released by the guest or the host.
Cc Gerd Hoffmann <[email protected]>Cc Marc-André Lureau <[email protected]>Signed-off-by Vivek Kasireddy <[email protected]>Signed-off-by Dongwon Kim <[email protected]>hw/display/virtio-gpu-udmabuf.c void virtio_gpu_init_udmabuf(struct
Quoted 26 lines
--- include/hw/virtio/virtio-gpu.h | 3 ++- hw/display/virtio-gpu-udmabuf.c | 25 ++++++++++++++++++------- hw/display/virtio-gpu.c | 2 +- 3 files changed, 21 insertions(+), 9 deletions(-)(quoted diff) +0
-0
b/include/hw/virtio/virtio-gpu.hindex58e0f91fda..65312f869d100644include/hw/virtio/virtio-gpu.h +2
-1@@ -357,7 +357,8 @@ bool virtio_gpu_scanout_blob_to_fb(structvirtio_gpu_framebuffer *fb,/* virtio-gpu-udmabuf.c */ bool virtio_gpu_have_udmabuf(void);void virtio_gpu_init_udmabuf (struct virtio_gpu_simple_resource *res); -void virtio_gpu_fini_udmabuf (struct virtio_gpu_simple_resource *res); +void virtio_gpu_fini_udmabuf (VirtIOGPU *g, +struct virtio_gpu_simple_resource *res); int virtio_gpu_update_dmabuf(VirtIOGPU *g, uint32_t scanout_id,struct
virtio_gpu_simple_resource*res,(quoted diff) +0
-0
b/hw/display/virtio-gpu-udmabuf.cindexd804f321aa..bd5b44f5fb100644hw/display/virtio-gpu-udmabuf.c +0
-0@@ -151,13 +151,6 @@ void virtio_gpu_init_udmabuf(struct
virtio_gpu_simple_resource*res)Quoted 17 lines
hw/display/virtio-gpu-udmabuf.c +0
-7res->blob = pdata; } -void virtio_gpu_fini_udmabuf (struct virtio_gpu_simple_resource *res) -{ -if (res->remapped) { -virtio_gpu_destroy_udmabuf (res); - } -} -static void virtio_gpu_free_dmabuf (VirtIOGPU *g,VGPUDMABuf *dmabuf) { struct virtio_gpu_scanout *scanout; @@ -169,6 +162,24 @@ staticvoid
virtio_gpu_free_dmabuf(VirtIOGPU *g, VGPUDMABuf *dmabuf)g_free(dmabuf);}
Quoted 11 lines
hw/display/virtio-gpu-udmabuf.c static void virtio_gpu_free_dmabuf(VirtIOGPU *g, VGPUDMABuf *dmabuf) · line 173
Quoted 9 lines
hw/display/virtio-gpu-udmabuf.c +9
-0+void virtio_gpu_fini_udmabuf (VirtIOGPU *g,struct +virtio_gpu_simple_resource *res) { +int max_outputs = g->parent_obj.conf.max_outputs; +int i; + +for (i =0 ; i < max_outputs; i++) { +VGPUDMABuf *dmabuf = g->dmabuf.primary[i]; + + if (dmabuf && (res->dmabuf_fd != -1) &&Maybe add
qemu_dmabuf_get_numplanes()> 0 ?
Do you want me to add this condition and resubmit v2 of this patch? I saw this patch has already been in the queue.
Quoted 7 lines
hw/display/virtio-gpu-udmabuf.c static void virtio_gpu_free_dmabuf(VirtIOGPU *g, VGPUDMABuf *dmabuf) · line 176
Quoted 4 lines
hw/display/virtio-gpu-udmabuf.c +3
-0+ qemu_dmabuf_get_fds(dmabuf->buf, NULL)[0] == res->dmabuf_fd) { +qemu_dmabuf_close (dmabuf->buf); + res->dmabuf_fd =-1 ;I am not really happy about that we close the underlying fd here before the next destroy, but I don't have an immediate solution
Yeah, I just thought this would be the best for now.
Quoted 23 lines
Quoted 23 lines
hw/display/virtio-gpu-udmabuf.c +6
-0+ } + } + +virtio_gpu_destroy_udmabuf (res); +} +static VGPUDMABuf *virtio_gpu_create_dmabuf (VirtIOGPU *g, uint32_t scanout_id, diff --git
a/hw/display/virtio-gpu.cb/hw/display/virtio-gpu.cindex643e91ca2a..b2af861f0d100644hw/display/virtio-gpu.c +1
-1@@ -902,7 +902,7 @@ void virtio_gpu_cleanup_mapping(VirtIOGPU *g, res->addrs =NULL ;if (res->blob) { -virtio_gpu_fini_udmabuf (res); +virtio_gpu_fini_udmabuf (g, res); } }
Quoted 8 lines
Quoted 4 lines
--
2.43.0--
Marc-André Lureau
Thanks,
Quoted 93 lines · Dongwon
Hi Marc-André,
Quoted 79 lines
-----Original Message-----
From: Marc-André Lureau <[email protected]>
Sent: Tuesday, March 3, 2026 8:28 AM
To: Kim, Dongwon <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] virtio-gpu: Fix scanout dmabuf cleanup during resource destructionHi
Quoted 14 lines · On Tue, Mar 3, 2026 at 2:06 AM <[email protected]> wrote:
From: Dongwon Kim <[email protected]>
When a virtio-gpu resource is destroyed, any associated udmabuf must be properly torn down. Currently, the code may leave dangling references to dmabuf file descriptors in the scanout primary buffers.
This patch updates
virtio_gpu_fini_udmabufto:
1. Iterate through all active scanouts.
2. Identify dmabufs that match the resource's file descriptor.
3. Close the dmabuf and invalidate the resource's FD reference to prevent use-after-free or double-close scenarios.
4. Finally, trigger the underlying udmabuf destruction.Quoted 7 lines
This ensures that the display backend does not attempt to access memory or FDs that have been released by the guest or the host.
Cc Gerd Hoffmann <[email protected]>Cc Marc-André Lureau <[email protected]>Signed-off-by Vivek Kasireddy <[email protected]>Signed-off-by Dongwon Kim <[email protected]>hw/display/virtio-gpu-udmabuf.c void virtio_gpu_init_udmabuf(struct
Quoted 26 lines
--- include/hw/virtio/virtio-gpu.h | 3 ++- hw/display/virtio-gpu-udmabuf.c | 25 ++++++++++++++++++------- hw/display/virtio-gpu.c | 2 +- 3 files changed, 21 insertions(+), 9 deletions(-)(quoted diff) +0
-0
b/include/hw/virtio/virtio-gpu.hindex58e0f91fda..65312f869d100644include/hw/virtio/virtio-gpu.h +2
-1@@ -357,7 +357,8 @@ bool virtio_gpu_scanout_blob_to_fb(structvirtio_gpu_framebuffer *fb,/* virtio-gpu-udmabuf.c */ bool virtio_gpu_have_udmabuf(void);void virtio_gpu_init_udmabuf (struct virtio_gpu_simple_resource *res); -void virtio_gpu_fini_udmabuf (struct virtio_gpu_simple_resource *res); +void virtio_gpu_fini_udmabuf (VirtIOGPU *g, +struct virtio_gpu_simple_resource *res); int virtio_gpu_update_dmabuf(VirtIOGPU *g, uint32_t scanout_id,struct
virtio_gpu_simple_resource*res,(quoted diff) +0
-0
b/hw/display/virtio-gpu-udmabuf.cindexd804f321aa..bd5b44f5fb100644hw/display/virtio-gpu-udmabuf.c +0
-0@@ -151,13 +151,6 @@ void virtio_gpu_init_udmabuf(struct
virtio_gpu_simple_resource*res)Quoted 17 lines
hw/display/virtio-gpu-udmabuf.c +0
-7res->blob = pdata; } -void virtio_gpu_fini_udmabuf (struct virtio_gpu_simple_resource *res) -{ -if (res->remapped) { -virtio_gpu_destroy_udmabuf (res); - } -} -static void virtio_gpu_free_dmabuf (VirtIOGPU *g,VGPUDMABuf *dmabuf) { struct virtio_gpu_scanout *scanout; @@ -169,6 +162,24 @@ staticvoid
virtio_gpu_free_dmabuf(VirtIOGPU *g, VGPUDMABuf *dmabuf)g_free(dmabuf);}
Quoted 11 lines
hw/display/virtio-gpu-udmabuf.c static void virtio_gpu_free_dmabuf(VirtIOGPU *g, VGPUDMABuf *dmabuf) · line 173
Quoted 9 lines
hw/display/virtio-gpu-udmabuf.c +9
-0+void virtio_gpu_fini_udmabuf (VirtIOGPU *g,struct +virtio_gpu_simple_resource *res) { +int max_outputs = g->parent_obj.conf.max_outputs; +int i; + +for (i =0 ; i < max_outputs; i++) { +VGPUDMABuf *dmabuf = g->dmabuf.primary[i]; + + if (dmabuf && (res->dmabuf_fd != -1) &&Maybe add
qemu_dmabuf_get_numplanes()> 0 ?
Quoted 2 lines
Do you want me to add this condition and resubmit v2 of this patch? I saw this patch has already been in the queue.
If you send v2 I can swap it out.
Quoted 44 lines
Quoted 8 lines
hw/display/virtio-gpu-udmabuf.c static void virtio_gpu_free_dmabuf(VirtIOGPU *g, VGPUDMABuf *dmabuf) · line 176
Quoted 5 lines
hw/display/virtio-gpu-udmabuf.c +3
-0+ qemu_dmabuf_get_fds(dmabuf->buf, NULL)[0] == res->dmabuf_fd) { +qemu_dmabuf_close (dmabuf->buf); + res->dmabuf_fd =-1 ;I am not really happy about that we close the underlying fd here before the next destroy, but I don't have an immediate solution
Yeah, I just thought this would be the best for now.
Quoted 23 lines
Quoted 23 lines
hw/display/virtio-gpu-udmabuf.c +6
-0+ } + } + +virtio_gpu_destroy_udmabuf (res); +} +static VGPUDMABuf *virtio_gpu_create_dmabuf (VirtIOGPU *g, uint32_t scanout_id, diff --git
a/hw/display/virtio-gpu.cb/hw/display/virtio-gpu.cindex643e91ca2a..b2af861f0d100644hw/display/virtio-gpu.c +1
-1@@ -902,7 +902,7 @@ void virtio_gpu_cleanup_mapping(VirtIOGPU *g, res->addrs =NULL ;if (res->blob) { -virtio_gpu_fini_udmabuf (res); +virtio_gpu_fini_udmabuf (g, res); } }Quoted 8 lines
Quoted 4 lines
--
2.43.0--
Marc-André LureauThanks,
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro
Quoted 95 lines · Alex Bennée
Quoted 93 lines · Dongwon
Hi Marc-André,
Quoted 90 lines
-----Original Message-----
From: Marc-André Lureau <[email protected]>
Sent: Tuesday, March 3, 2026 8:28 AM
To: Kim, Dongwon <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] virtio-gpu: Fix scanout dmabuf cleanup during resource destructionHi
Quoted 14 lines · On Tue, Mar 3, 2026 at 2:06 AM <[email protected]> wrote:
From: Dongwon Kim <[email protected]>
When a virtio-gpu resource is destroyed, any associated udmabuf must be properly torn down. Currently, the code may leave dangling references to dmabuf file descriptors in the scanout primary buffers.
This patch updates
virtio_gpu_fini_udmabufto:
1. Iterate through all active scanouts.
2. Identify dmabufs that match the resource's file descriptor.
3. Close the dmabuf and invalidate the resource's FD reference to prevent use-after-free or double-close scenarios.
4. Finally, trigger the underlying udmabuf destruction.Quoted 7 lines
This ensures that the display backend does not attempt to access memory or FDs that have been released by the guest or the host.
Cc Gerd Hoffmann <[email protected]>Cc Marc-André Lureau <[email protected]>Signed-off-by Vivek Kasireddy <[email protected]>Signed-off-by Dongwon Kim <[email protected]>hw/display/virtio-gpu-udmabuf.c void virtio_gpu_init_udmabuf(struct
Quoted 26 lines
--- include/hw/virtio/virtio-gpu.h | 3 ++- hw/display/virtio-gpu-udmabuf.c | 25 ++++++++++++++++++------- hw/display/virtio-gpu.c | 2 +- 3 files changed, 21 insertions(+), 9 deletions(-)(quoted diff) +0
-0
b/include/hw/virtio/virtio-gpu.hindex58e0f91fda..65312f869d100644include/hw/virtio/virtio-gpu.h +2
-1@@ -357,7 +357,8 @@ bool virtio_gpu_scanout_blob_to_fb(structvirtio_gpu_framebuffer *fb,/* virtio-gpu-udmabuf.c */ bool virtio_gpu_have_udmabuf(void);void virtio_gpu_init_udmabuf (struct virtio_gpu_simple_resource *res); -void virtio_gpu_fini_udmabuf (struct virtio_gpu_simple_resource *res); +void virtio_gpu_fini_udmabuf (VirtIOGPU *g, +struct virtio_gpu_simple_resource *res); int virtio_gpu_update_dmabuf(VirtIOGPU *g, uint32_t scanout_id,struct
virtio_gpu_simple_resource*res,(quoted diff) +0
-0
b/hw/display/virtio-gpu-udmabuf.cindexd804f321aa..bd5b44f5fb100644hw/display/virtio-gpu-udmabuf.c +0
-0@@ -151,13 +151,6 @@ void virtio_gpu_init_udmabuf(struct
virtio_gpu_simple_resource*res)Quoted 17 lines
hw/display/virtio-gpu-udmabuf.c +0
-7res->blob = pdata; } -void virtio_gpu_fini_udmabuf (struct virtio_gpu_simple_resource *res) -{ -if (res->remapped) { -virtio_gpu_destroy_udmabuf (res); - } -} -static void virtio_gpu_free_dmabuf (VirtIOGPU *g,VGPUDMABuf *dmabuf) { struct virtio_gpu_scanout *scanout; @@ -169,6 +162,24 @@ staticvoid
virtio_gpu_free_dmabuf(VirtIOGPU *g, VGPUDMABuf *dmabuf)g_free(dmabuf);}
hw/display/virtio-gpu-udmabuf.c static void virtio_gpu_free_dmabuf(VirtIOGPU *g, VGPUDMABuf *dmabuf) · line 173
Quoted 9 lines
hw/display/virtio-gpu-udmabuf.c +9
-0+void virtio_gpu_fini_udmabuf (VirtIOGPU *g,struct +virtio_gpu_simple_resource *res) { +int max_outputs = g->parent_obj.conf.max_outputs; +int i; + +for (i =0 ; i < max_outputs; i++) { +VGPUDMABuf *dmabuf = g->dmabuf.primary[i]; + + if (dmabuf && (res->dmabuf_fd != -1) &&Maybe add
qemu_dmabuf_get_numplanes()> 0 ?
Quoted 4 lines
Quoted 2 lines
Do you want me to add this condition and resubmit v2 of this patch? I saw this patch has already been in the queue.
If you send v2 I can swap it out.
I also noted you need to fix the stub:
void virtio_gpu_fini_udmabuf(VirtIOGPU *g, struct virtio_gpu_simple_resource *res)
{
/* nothing (stub) */
}Quoted 44 lines
Quoted 44 lines
Quoted 8 lines
hw/display/virtio-gpu-udmabuf.c static void virtio_gpu_free_dmabuf(VirtIOGPU *g, VGPUDMABuf *dmabuf) · line 176
Quoted 5 lines
hw/display/virtio-gpu-udmabuf.c +3
-0+ qemu_dmabuf_get_fds(dmabuf->buf, NULL)[0] == res->dmabuf_fd) { +qemu_dmabuf_close (dmabuf->buf); + res->dmabuf_fd =-1 ;I am not really happy about that we close the underlying fd here before the next destroy, but I don't have an immediate solution
Yeah, I just thought this would be the best for now.
Quoted 23 lines
Quoted 23 lines
hw/display/virtio-gpu-udmabuf.c +6
-0+ } + } + +virtio_gpu_destroy_udmabuf (res); +} +static VGPUDMABuf *virtio_gpu_create_dmabuf (VirtIOGPU *g, uint32_t scanout_id, diff --git
a/hw/display/virtio-gpu.cb/hw/display/virtio-gpu.cindex643e91ca2a..b2af861f0d100644hw/display/virtio-gpu.c +1
-1@@ -902,7 +902,7 @@ void virtio_gpu_cleanup_mapping(VirtIOGPU *g, res->addrs =NULL ;if (res->blob) { -virtio_gpu_fini_udmabuf (res); +virtio_gpu_fini_udmabuf (g, res); } }Quoted 8 lines
Quoted 4 lines
--
2.43.0--
Marc-André LureauThanks,
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro
PATCH v2 1/1 virtio-gpu: Fix scanout dmabuf cleanup during resource destruction
In reply to [email protected]From: Dongwon Kim <[email protected]>
When a virtio-gpu resource is destroyed, any associated udmabuf must be properly torn down. Currently, the code may leave dangling references to dmabuf file descriptors in the scanout primary buffers.
This patch updates virtio_gpu_fini_udmabuf to:
1. Iterate through all active scanouts.
2. Identify dmabufs that match the resource's file descriptor.
3. Close the dmabuf and invalidate the resource's FD reference to prevent use-after-free or double-close scenarios.
4. Finally, trigger the underlying udmabuf destruction.
This ensures that the display backend does not attempt to access memory or FDs that have been released by the guest or the host.
v2: - Corrected virtio_gpu_fini_udmabuf in stub
(Alex Bennée)
- Make sure that qemu dmabuf has at least one plane before Comparing fds
(Marc-André Lureau)
--- include/hw/virtio/virtio-gpu.h | 3 ++- hw/display/virtio-gpu-udmabuf-stubs.c | 2 +- hw/display/virtio-gpu-udmabuf.c | 27 ++++++++++++++++++++------- hw/display/virtio-gpu.c | 2 +- 4 files changed, 24 insertions(+), 10 deletions(-)
include/hw/virtio/virtio-gpu.h +2 -1
@@ -357,7 +357,8 @@ bool virtio_gpu_scanout_blob_to_fb(struct virtio_gpu_framebuffer *fb,/* virtio-gpu-udmabuf.c */ bool virtio_gpu_have_udmabuf (void );void virtio_gpu_init_udmabuf (struct virtio_gpu_simple_resource *res); -void virtio_gpu_fini_udmabuf (struct virtio_gpu_simple_resource *res); +void virtio_gpu_fini_udmabuf (VirtIOGPU *g, +struct virtio_gpu_simple_resource *res); int virtio_gpu_update_dmabuf(VirtIOGPU *g, uint32_t scanout_id, struct virtio_gpu_simple_resource *res,
hw/display/virtio-gpu-udmabuf-stubs.c +1 -1
@@ -12,7 +12,7 @@ void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)/* nothing (stub) */ } -void virtio_gpu_fini_udmabuf (struct virtio_gpu_simple_resource *res) +void virtio_gpu_fini_udmabuf (VirtIOGPU *g,struct virtio_gpu_simple_resource *res) {/* nothing (stub) */ }
hw/display/virtio-gpu-udmabuf.c +20 -7
@@ -151,13 +151,6 @@ void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res) res->blob = pdata; } -void virtio_gpu_fini_udmabuf (struct virtio_gpu_simple_resource *res) -{ -if (res->remapped) { -virtio_gpu_destroy_udmabuf (res); - } -} -static void virtio_gpu_free_dmabuf (VirtIOGPU *g,VGPUDMABuf *dmabuf) {struct virtio_gpu_scanout *scanout;
@@ -169,6 +162,26 @@ static void virtio_gpu_free_dmabuf(VirtIOGPU *g, VGPUDMABuf *dmabuf)g_free (dmabuf); } +void virtio_gpu_fini_udmabuf (VirtIOGPU *g,struct virtio_gpu_simple_resource *res) +{ +int max_outputs = g->parent_obj.conf.max_outputs; +int i; + +for (i =0 ; i < max_outputs; i++) { +VGPUDMABuf *dmabuf = g->dmabuf.primary[i]; + +if (dmabuf && +qemu_dmabuf_get_num_planes (dmabuf->buf) >0 && +qemu_dmabuf_get_fds (dmabuf->buf,NULL )[0 ] == res->dmabuf_fd && + res->dmabuf_fd !=-1 ) { +qemu_dmabuf_close (dmabuf->buf); + res->dmabuf_fd =-1 ; + } + } + +virtio_gpu_destroy_udmabuf (res); +} +static VGPUDMABuf *virtio_gpu_create_dmabuf(VirtIOGPU *g, uint32_t scanout_id,
hw/display/virtio-gpu.c +1 -1
@@ -902,7 +902,7 @@ void virtio_gpu_cleanup_mapping(VirtIOGPU *g, res->addrs =NULL ;if (res->blob) { -virtio_gpu_fini_udmabuf (res); +virtio_gpu_fini_udmabuf (g, res); } }
Signature · 1 line
2.43.0
Alex Bennée
AppliedQuoted 13 lines · [email protected] writes:
From: Dongwon Kim <[email protected]>
When a virtio-gpu resource is destroyed, any associated udmabuf must be properly torn down. Currently, the code may leave dangling references to dmabuf file descriptors in the scanout primary buffers.
This patch updates
virtio_gpu_fini_udmabufto:
1. Iterate through all active scanouts.
2. Identify dmabufs that match the resource's file descriptor.
3. Close the dmabuf and invalidate the resource's FD reference to prevent use-after-free or double-close scenarios.
4. Finally, trigger the underlying udmabuf destruction.
Quoted 2 lines
This ensures that the display backend does not attempt to access memory or FDs that have been released by the guest or the host.
Queued to virtio-gpu/next, thanks.
Quoted 7 lines
v2: - Corrected
virtio_gpu_fini_udmabufin stub
(Alex Bennée)- Make sure that qemu dmabuf has at least one plane before Comparing fds
(Marc-André Lureau)
FYI usually we put version information under the --- so it doesn't pollute the commit message when things are applied.
Quoted 97 lines
Cc Alex Bennée <[email protected]>Cc Gerd Hoffmann <[email protected]>Cc Marc-André Lureau <[email protected]>Cc Vivek Kasireddy <[email protected]>Signed-off-by Dongwon Kim <[email protected]>--- include/hw/virtio/virtio-gpu.h | 3 ++- hw/display/virtio-gpu-udmabuf-stubs.c | 2 +- hw/display/virtio-gpu-udmabuf.c | 27 ++++++++++++++++++++------- hw/display/virtio-gpu.c | 2 +- 4 files changed, 24 insertions(+), 10 deletions(-)include/hw/virtio/virtio-gpu.h +2
-1@@ -357,7 +357,8 @@ bool virtio_gpu_scanout_blob_to_fb(struct virtio_gpu_framebuffer *fb,/* virtio-gpu-udmabuf.c */ bool virtio_gpu_have_udmabuf (void );void virtio_gpu_init_udmabuf (struct virtio_gpu_simple_resource *res); -void virtio_gpu_fini_udmabuf (struct virtio_gpu_simple_resource *res); +void virtio_gpu_fini_udmabuf (VirtIOGPU *g, +struct virtio_gpu_simple_resource *res); int virtio_gpu_update_dmabuf(VirtIOGPU *g, uint32_t scanout_id, struct virtio_gpu_simple_resource *res,hw/display/virtio-gpu-udmabuf-stubs.c +1
-1@@ -12,7 +12,7 @@ void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)/* nothing (stub) */ } -void virtio_gpu_fini_udmabuf (struct virtio_gpu_simple_resource *res) +void virtio_gpu_fini_udmabuf (VirtIOGPU *g,struct virtio_gpu_simple_resource *res) {/* nothing (stub) */ }hw/display/virtio-gpu-udmabuf.c +20
-7@@ -151,13 +151,6 @@ void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res) res->blob = pdata; } -void virtio_gpu_fini_udmabuf (struct virtio_gpu_simple_resource *res) -{ -if (res->remapped) { -virtio_gpu_destroy_udmabuf (res); - } -} -static void virtio_gpu_free_dmabuf (VirtIOGPU *g,VGPUDMABuf *dmabuf) {struct virtio_gpu_scanout *scanout;@@ -169,6 +162,26 @@ static void virtio_gpu_free_dmabuf(VirtIOGPU *g, VGPUDMABuf *dmabuf)g_free (dmabuf); } +void virtio_gpu_fini_udmabuf (VirtIOGPU *g,struct virtio_gpu_simple_resource *res) +{ +int max_outputs = g->parent_obj.conf.max_outputs; +int i; + +for (i =0 ; i < max_outputs; i++) { +VGPUDMABuf *dmabuf = g->dmabuf.primary[i]; + +if (dmabuf && +qemu_dmabuf_get_num_planes (dmabuf->buf) >0 && +qemu_dmabuf_get_fds (dmabuf->buf,NULL )[0 ] == res->dmabuf_fd && + res->dmabuf_fd !=-1 ) { +qemu_dmabuf_close (dmabuf->buf); + res->dmabuf_fd =-1 ; + } + } + +virtio_gpu_destroy_udmabuf (res); +} +static VGPUDMABuf *virtio_gpu_create_dmabuf(VirtIOGPU *g, uint32_t scanout_id,hw/display/virtio-gpu.c +1
-1@@ -902,7 +902,7 @@ void virtio_gpu_cleanup_mapping(VirtIOGPU *g, res->addrs =NULL ;if (res->blob) { -virtio_gpu_fini_udmabuf (res); +virtio_gpu_fini_udmabuf (g, res); } }
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro