Skip to contentSkip to conversation

virtio-gpu: fix memory leaks in virgl and rutabaga

PATCH Superseded Tom Desnoyers 3 participants 4 messages Oct 7 – Oct 13

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 virtio-gpu: fix memory leaks in virgl and rutabaga

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

---
 hw/display/virtio-gpu-rutabaga.c | 1 +
 hw/display/virtio-gpu-virgl.c    | 1 +
 2 files changed, 2 insertions(+)
hw/display/virtio-gpu-rutabaga.c +1 -0
index 3aafa4dc66..7907aea85a 100644
@@ -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);
 }
 
hw/display/virtio-gpu-virgl.c +1 -0
index f45571060f..b5b160c840 100644
@@ -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;
Signature · 1 line
2.55.0
#1 lore raw
Quoted 32 lines · Tom Desnoyers

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

---
  hw/display/virtio-gpu-rutabaga.c | 1 +
  hw/display/virtio-gpu-virgl.c    | 1 +
  2 files changed, 2 insertions(+)
hw/display/virtio-gpu-rutabaga.c +1 -0
index 3aafa4dc66..7907aea85a 100644
@@ -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);
  }
 
hw/display/virtio-gpu-virgl.c +0 -0
index f45571060f..b5b160c840 100644

hw/display/virtio-gpu-virgl.c virtio_gpu_virgl_resource_unref(VirtIOGPU *g, · line 461

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

Quoted 3 lines
hw/display/virtio-gpu-virgl.c +0 -0
      g_free(res);
 
      return 0;
#2 lore raw

PATCH v2 1/1 virtio-gpu: fix memory leaks in virgl and rutabaga

In reply to Tom Desnoyers

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

---
v2:
- replace virtio_gpu_cleanup_mapping_iov() by virtio_gpu_cleanup_mapping() (Akihiko Odaki)

v1:
https://lore.kernel.org/qemu-devel/[email protected]

 hw/display/virtio-gpu-rutabaga.c | 2 ++
 hw/display/virtio-gpu-virgl.c    | 6 +++---
 2 files changed, 5 insertions(+), 3 deletions(-)
hw/display/virtio-gpu-rutabaga.c +2 -0
index 3aafa4dc66..e5c993c758 100644
@@ -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);
 }
 
hw/display/virtio-gpu-virgl.c +3 -3
index f45571060f..f71f287d94 100644
@@ -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;
Signature · 1 line
2.56.0
Reviewed-by on v2 1/1
#3 lore raw
Quoted 9 lines · Tom Desnoyers

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.

[...]

Signature · 1 line
Marc-André Lureau <[email protected]>