[PATCH] virtio-gpu: fix memory leaks in virgl and rutabaga

Tom Desnoyers posted 1 patch 1 month, 2 weeks ago
Failed in applying to current master (apply log)
There is a newer version of this series
hw/display/virtio-gpu-rutabaga.c | 1 +
hw/display/virtio-gpu-virgl.c    | 1 +
2 files changed, 2 insertions(+)
[PATCH] virtio-gpu: fix memory leaks in virgl and rutabaga
Posted by Tom Desnoyers 1 month, 2 weeks ago
When a gpu buffer backed by a guest-memory blob is unreferenced, virgl
and rutabaga free the resource but not its addrs field, leaking memory
on every unref.

In virtio-gpu-virgl.c, free res->base.addrs before freeing the resource.
In virtio-gpu-rutabaga.c, call virtio_gpu_cleanup_mapping() before
freeing the resource.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4634
Signed-off-by: Tom Desnoyers <tom.ds1@gmx.com>
---
 hw/display/virtio-gpu-rutabaga.c | 1 +
 hw/display/virtio-gpu-virgl.c    | 1 +
 2 files changed, 2 insertions(+)

diff --git a/hw/display/virtio-gpu-rutabaga.c b/hw/display/virtio-gpu-rutabaga.c
index 3aafa4dc66..7907aea85a 100644
--- a/hw/display/virtio-gpu-rutabaga.c
+++ b/hw/display/virtio-gpu-rutabaga.c
@@ -164,6 +164,7 @@ virtio_gpu_rutabaga_resource_unref(VirtIOGPU *g,
     }
 
     QTAILQ_REMOVE(&g->reslist, res, next);
+    virtio_gpu_cleanup_mapping(g, res);
     g_free(res);
 }
 
diff --git a/hw/display/virtio-gpu-virgl.c b/hw/display/virtio-gpu-virgl.c
index f45571060f..b5b160c840 100644
--- a/hw/display/virtio-gpu-virgl.c
+++ b/hw/display/virtio-gpu-virgl.c
@@ -458,6 +458,7 @@ virtio_gpu_virgl_resource_unref(VirtIOGPU *g,
 
     QTAILQ_REMOVE(&g->reslist, &res->base, next);
 
+    g_free(res->base.addrs);
     g_free(res);
 
     return 0;
-- 
2.55.0
Re: [PATCH] virtio-gpu: fix memory leaks in virgl and rutabaga
Posted by Akihiko Odaki 1 month, 2 weeks ago
On 2026/10/06 22:55, Tom Desnoyers wrote:
> When a gpu buffer backed by a guest-memory blob is unreferenced, virgl
> and rutabaga free the resource but not its addrs field, leaking memory
> on every unref.
> 
> In virtio-gpu-virgl.c, free res->base.addrs before freeing the resource.
> In virtio-gpu-rutabaga.c, call virtio_gpu_cleanup_mapping() before
> freeing the resource.
> 
> Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4634
> Signed-off-by: Tom Desnoyers <tom.ds1@gmx.com>
> ---
>   hw/display/virtio-gpu-rutabaga.c | 1 +
>   hw/display/virtio-gpu-virgl.c    | 1 +
>   2 files changed, 2 insertions(+)
> 
> diff --git a/hw/display/virtio-gpu-rutabaga.c b/hw/display/virtio-gpu-rutabaga.c
> index 3aafa4dc66..7907aea85a 100644
> --- a/hw/display/virtio-gpu-rutabaga.c
> +++ b/hw/display/virtio-gpu-rutabaga.c
> @@ -164,6 +164,7 @@ virtio_gpu_rutabaga_resource_unref(VirtIOGPU *g,
>       }
>   
>       QTAILQ_REMOVE(&g->reslist, res, next);
> +    virtio_gpu_cleanup_mapping(g, res);
>       g_free(res);
>   }
>   
> diff --git a/hw/display/virtio-gpu-virgl.c b/hw/display/virtio-gpu-virgl.c
> index f45571060f..b5b160c840 100644
> --- a/hw/display/virtio-gpu-virgl.c
> +++ b/hw/display/virtio-gpu-virgl.c
> @@ -458,6 +458,7 @@ virtio_gpu_virgl_resource_unref(VirtIOGPU *g,
>   
>       QTAILQ_REMOVE(&g->reslist, &res->base, next);
>   
> +    g_free(res->base.addrs);

Thanks for the patch. For the virtio-gpu-virgl.c changes, I think it 
would be better to update virtio_gpu_virgl_resource_unref() to use the 
virtio_gpu_cleanup_mapping() helper.

Previously, the virgl code only called virtio_gpu_cleanup_mapping_iov(), 
so there was little benefit to using the higher-level helper. However, 
since this fix requires us to free res->base.addrs as well, replacing 
both the _iov() call and this new g_free() call with a single call to 
virtio_gpu_cleanup_mapping() makes the code cleaner.

Regards,
Akihiko Odaki

>       g_free(res);
>   
>       return 0;
[PATCH v2] virtio-gpu: fix memory leaks in virgl and rutabaga
Posted by Tom Desnoyers 1 month, 1 week ago
When a gpu buffer backed by a guest-memory blob is unreferenced, virgl
and rutabaga free the resource but not its addrs field, leaking memory
on every unref.

In virtio-gpu-virgl.c, call virtio_gpu_cleanup_mapping() instead of
virtio_gpu_cleanup_mapping_iov(). In virtio-gpu-rutabaga.c, call
virtio_gpu_cleanup_mapping() before freeing the resource.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4634
Signed-off-by: Tom Desnoyers <tom.ds1@gmx.com>
---
v2:
- replace virtio_gpu_cleanup_mapping_iov() by virtio_gpu_cleanup_mapping() (Akihiko Odaki)

v1:
https://lore.kernel.org/qemu-devel/20261006135504.416083-1-tom.ds1@gmx.com

 hw/display/virtio-gpu-rutabaga.c | 2 ++
 hw/display/virtio-gpu-virgl.c    | 6 +++---
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/hw/display/virtio-gpu-rutabaga.c b/hw/display/virtio-gpu-rutabaga.c
index 3aafa4dc66..e5c993c758 100644
--- a/hw/display/virtio-gpu-rutabaga.c
+++ b/hw/display/virtio-gpu-rutabaga.c
@@ -164,6 +164,8 @@ virtio_gpu_rutabaga_resource_unref(VirtIOGPU *g,
     }
 
     QTAILQ_REMOVE(&g->reslist, res, next);
+
+    virtio_gpu_cleanup_mapping(g, res);
     g_free(res);
 }
 
diff --git a/hw/display/virtio-gpu-virgl.c b/hw/display/virtio-gpu-virgl.c
index f45571060f..f71f287d94 100644
--- a/hw/display/virtio-gpu-virgl.c
+++ b/hw/display/virtio-gpu-virgl.c
@@ -451,13 +451,13 @@ virtio_gpu_virgl_resource_unref(VirtIOGPU *g,
     virgl_renderer_resource_detach_iov(res->base.resource_id,
                                        &res_iovs,
                                        &num_iovs);
-    if (res_iovs != NULL && num_iovs != 0) {
-        virtio_gpu_cleanup_mapping_iov(g, res_iovs, num_iovs);
-    }
+    res->base.iov = res_iovs;
+    res->base.iov_cnt = num_iovs;
     virgl_renderer_resource_unref(res->base.resource_id);
 
     QTAILQ_REMOVE(&g->reslist, &res->base, next);
 
+    virtio_gpu_cleanup_mapping(g, &res->base);
     g_free(res);
 
     return 0;
-- 
2.56.0
Re: [PATCH v2] virtio-gpu: fix memory leaks in virgl and rutabaga
Posted by marcandre.lureau@redhat.com 1 month, 1 week ago
On Mon, 12 Oct 2026 18:57:54 +0200, Tom Desnoyers <tom.ds1@gmx.com> wrote:
> When a gpu buffer backed by a guest-memory blob is unreferenced, virgl
> and rutabaga free the resource but not its addrs field, leaking memory
> on every unref.
> 
> In virtio-gpu-virgl.c, call virtio_gpu_cleanup_mapping() instead of
> virtio_gpu_cleanup_mapping_iov(). In virtio-gpu-rutabaga.c, call
> virtio_gpu_cleanup_mapping() before freeing the resource.
> 
> [...]

Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>

-- 
Marc-André Lureau <marcandre.lureau@redhat.com>