drm/virtio: fix GEM object leaks on error paths
Subsystems (4)
v2 cover drm/virtio: fix GEM object leaks on error paths
v1 fixed one drm_gem_object_release() misuse in virtio_gpu_gem_create(). Dmitry asked for the other occurrences the Sashiko bot pointed at [1], so v2 turns it into a series.
Patches 1-3 replace drm_gem_object_release() with drm_gem_object_put() on the GEM creation error paths. Patch 1 is unchanged from v1.
Patch 4 adds the missing drm_gem_object_release() before kfree(vram) in virtio_gpu_vram_create().
Only compile-tested (x86_64, CONFIG_DRM_VIRTIO_GPU=m).
[1] https://lore.kernel.org/all/[email protected]/
Changes in v2:
- Add patches 2-4 covering the other occurrences reported by the bot
(Dmitry Osipenko)
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Junrui Luo (4):
drm/virtio: fix object leak when drm_gem_handle_create() fails drm/virtio: fix object leak in virtio_gpu_resource_create_ioctl() drm/virtio: fix object leaks in virtio_gpu_resource_create_blob_ioctl() drm/virtio: release the GEM object on virtio_gpu_vram_create() errors
drivers/gpu/drm/virtio/virtgpu_gem.c | 2 +- drivers/gpu/drm/virtio/virtgpu_ioctl.c | 6 +++--- drivers/gpu/drm/virtio/virtgpu_vram.c | 17 +++++++++-------- 3 files changed, 13 insertions(+), 12 deletions(-)
---
base-commit: 587858367581b9c55c3690f4e63382ad622719d4
change-id: 20260915-fixes-8c6917e90665
Best regards,
Signature · 1 line
Junrui Luo <[email protected]>
PATCH v2 1/4 drm/virtio: fix object leak when drm_gem_handle_create() fails
virtio_gpu_gem_create() owns the reference taken by virtio_gpu_object_create(). On the drm_gem_handle_create() error path it calls drm_gem_object_release() instead of dropping that reference.
drm_gem_object_release() is the inverse of drm_gem_object_init() and does not touch the reference count or call obj->funcs->free(), so it is only correct as the last step of a destructor, as in virtio_gpu_cleanup_object(). Using it here leaves the bo at refcount 1 with no remaining reference, so virtio_gpu_free_object() never runs and the shmem pages, sg table and virtio_gpu_object are leaked. Since virtio_gpu_object_create() has already set bo->created, VIRTIO_GPU_CMD_RESOURCE_UNREF is not queued either, leaking the host-side resource and the resource id.
drm_gem_handle_create_tail() drops the handle reference on all of its internal error paths, so the caller only has to drop its own. Use drm_gem_object_put(), matching the success path below.
--- drivers/gpu/drm/virtio/virtgpu_gem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/virtio/virtgpu_gem.c +1 -1
@@ -45,7 +45,7 @@ static int virtio_gpu_gem_create(struct drm_file *file, ret =drm_gem_handle_create (file, &obj->base.base, &handle);if (ret) { -drm_gem_object_release (&obj->base.base); +drm_gem_object_put (&obj->base.base);return ret; }
Signature · 1 line
2.51.2
[email protected]
AI reviewRe: [PATCH v2 1/4] drm/virtio: fix object leak when drm_gem_handle_create() fails
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 16 lines
From: Junrui Luo <[email protected]>
virtio_gpu_gem_create()owns the reference taken byvirtio_gpu_object_create(). On thedrm_gem_handle_create()error path it callsdrm_gem_object_release()instead of dropping that reference.
drm_gem_object_release()is the inverse ofdrm_gem_object_init()and does not touch the reference count or callobj->funcs->free(), so it is only correct as the last step of a destructor, as invirtio_gpu_cleanup_object(). Using it here leaves the bo at refcount 1 with no remaining reference, sovirtio_gpu_free_object()never runs and the shmem pages, sg table andvirtio_gpu_objectare leaked. Sincevirtio_gpu_object_create()has already setbo->created,VIRTIO_GPU_CMD_RESOURCE_UNREFis not queued either, leaking the host-side resource and the resource id.
Quoted 8 lines
drm_gem_handle_create_tail()drops the handle reference on all of its internal error paths, so the caller only has to drop its own. Usedrm_gem_object_put(), matching the success path below.Fixes dc5698e80cf7 ("Add virtio gpu driver.")Reported-by Yuhao Jiang <[email protected]>Assisted-by Claude:claude-opus-5Signed-off-by Junrui Luo <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v2 2/4 drm/virtio: fix object leak in virtio_gpu_resource_create_ioctl()
In reply to Junrui Luovirtio_gpu_resource_create_ioctl() calls drm_gem_object_release() on the drm_gem_handle_create() error path instead of dropping the reference it owns, so obj->funcs->free() never runs and the virtio_gpu_object, its pages and sg table, the resource id and the host-side resource are leaked.
Use drm_gem_object_put() instead.
--- drivers/gpu/drm/virtio/virtgpu_ioctl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/virtio/virtgpu_ioctl.c +1 -1
@@ -185,7 +185,7 @@ static int virtio_gpu_resource_create_ioctl(struct drm_device *dev, void *data, ret =drm_gem_handle_create (file, obj, &handle);if (ret) { -drm_gem_object_release (obj); +drm_gem_object_put (obj);return ret; }
Signature · 1 line
2.51.2
[email protected]
AI reviewRe: [PATCH v2 2/4] drm/virtio: fix object leak in virtio_gpu_resource_create_ioctl()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines
From: Junrui Luo <[email protected]>
virtio_gpu_resource_create_ioctl()callsdrm_gem_object_release()on thedrm_gem_handle_create()error path instead of dropping the reference it owns, soobj->funcs->free()never runs and thevirtio_gpu_object, its pages and sg table, the resource id and the host-side resource are leaked.
Quoted 5 lines
Use
drm_gem_object_put()instead.Fixes 62fb7a5e1096 ("virtio-gpu: add 3d/virgl support")Assisted-by Claude:claude-opus-5Signed-off-by Junrui Luo <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v2 3/4 drm/virtio: fix object leaks in virtio_gpu_resource_create_blob_ioctl()
In reply to Junrui Luovirtio_gpu_resource_create_blob_ioctl() calls drm_gem_object_release() on both the virtio_gpu_resource_assign_uuid() and drm_gem_handle_create() error paths instead of dropping the reference it owns, so obj->funcs->free() never runs and the virtio_gpu_object, the resource id and the host-side resource are leaked.
Use drm_gem_object_put() instead.
--- drivers/gpu/drm/virtio/virtgpu_ioctl.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/virtio/virtgpu_ioctl.c +2 -2
@@ -557,14 +557,14 @@ static int virtio_gpu_resource_create_blob_ioctl(struct drm_device *dev,if (params.blob_flags &VIRTGPU_BLOB_FLAG_USE_CROSS_DEVICE ) { ret =virtio_gpu_resource_assign_uuid (vgdev, bo);if (ret) { -drm_gem_object_release (obj); +drm_gem_object_put (obj);return ret; } } ret =drm_gem_handle_create (file, obj, &handle);if (ret) { -drm_gem_object_release (obj); +drm_gem_object_put (obj);return ret; }
Signature · 1 line
2.51.2
[email protected]
AI reviewRe: [PATCH v2 3/4] drm/virtio: fix object leaks in virtio_gpu_resource_create_blob_ioctl()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines
From: Junrui Luo <[email protected]>
virtio_gpu_resource_create_blob_ioctl()callsdrm_gem_object_release()on both thevirtio_gpu_resource_assign_uuid()anddrm_gem_handle_create()error paths instead of dropping the reference it owns, soobj->funcs->free()never runs and thevirtio_gpu_object, the resource id and the host-side resource are leaked.
Quoted 5 lines
Use
drm_gem_object_put()instead.Fixes 897b4d1acaf5 ("drm/virtio: implement blob resources: resource create blob ioctl")Assisted-by Claude:claude-opus-5Signed-off-by Junrui Luo <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH v2 4/4 drm/virtio: release the GEM object on virtio_gpu_vram_create() errors
In reply to Junrui Luovirtio_gpu_vram_create() frees the object with a bare kfree(vram) on both error paths after drm_gem_private_object_init() has run, and on the second one after drm_gem_create_mmap_offset() has linked obj->vma_node into the device's VMA offset manager. The freed object stays in that interval tree, so a later lookup or insertion walks freed memory, and the dma_resv and gpuva lock are never destroyed.
Call drm_gem_object_release() before kfree() on both paths.
--- drivers/gpu/drm/virtio/virtgpu_vram.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-)
drivers/gpu/drm/virtio/virtgpu_vram.c +9 -8
@@ -215,16 +215,12 @@ int virtio_gpu_vram_create(struct virtio_gpu_device *vgdev,/* Create fake offset */ ret =drm_gem_create_mmap_offset (obj); -if (ret) { -kfree (vram); -return ret; - } +if (ret) + goto err_release_obj; ret =virtio_gpu_resource_id_get (vgdev, &vram->base.hw_res_handle); -if (ret) { -kfree (vram); -return ret; - } +if (ret) + goto err_release_obj;virtio_gpu_cmd_resource_create_blob (vgdev, &vram->base, params,NULL ,0 );
@@ -240,6 +236,11 @@ int virtio_gpu_vram_create(struct virtio_gpu_device *vgdev, *bo_ptr = &vram->base;return 0 ; + +err_release_obj: +drm_gem_object_release (obj); +kfree (vram); +return ret; }void virtio_gpu_vram_map_deferred (struct virtio_gpu_object_vram *vram)
Signature · 1 line
2.51.2
[email protected]
AI reviewRe: [PATCH v2 4/4] drm/virtio: release the GEM object on virtio_gpu_vram_create() errors
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines
From: Junrui Luo <[email protected]>
virtio_gpu_vram_create()frees the object with a bare kfree(vram) on both error paths afterdrm_gem_private_object_init()has run, and on the second one afterdrm_gem_create_mmap_offset()has linkedobj->vma_nodeinto the device's VMA offset manager. The freed object stays in that interval tree, so a later lookup or insertion walks freed memory, and thedma_resvand gpuva lock are never destroyed.
Quoted 5 lines
Call
drm_gem_object_release()beforekfree()on both paths.Fixes 16845c5d5409 ("drm/virtio: implement blob resources: implement vram object")Assisted-by Claude:claude-opus-5Signed-off-by Junrui Luo <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Quoted 31 lines · On 9/15/26 10:39, Junrui Luo via B4 Relay wrote:
v1 fixed one
drm_gem_object_release()misuse invirtio_gpu_gem_create(). Dmitry asked for the other occurrences the Sashiko bot pointed at [1], so v2 turns it into a series.Patches 1-3 replace
drm_gem_object_release()withdrm_gem_object_put()on the GEM creation error paths. Patch 1 is unchanged from v1.Patch 4 adds the missing
drm_gem_object_release()before kfree(vram) invirtio_gpu_vram_create().Only compile-tested (
x86_64,CONFIG_DRM_VIRTIO_GPU=m).[1] https://lore.kernel.org/all/[email protected]/
Changes in v2:
- Add patches 2-4 covering the other occurrences reported by the bot
(Dmitry Osipenko)
- Link to v1: https://lore.kernel.org/r/[email protected]Signed-off-by Junrui Luo <[email protected]>---
Junrui Luo (4):
drm/virtio: fix object leak whendrm_gem_handle_create()fails drm/virtio: fix object leak invirtio_gpu_resource_create_ioctl()drm/virtio: fix object leaks invirtio_gpu_resource_create_blob_ioctl()drm/virtio: release the GEM object onvirtio_gpu_vram_create()errorsdrivers/gpu/drm/virtio/virtgpu_gem.c | 2 +- drivers/gpu/drm/virtio/virtgpu_ioctl.c | 6 +++--- drivers/gpu/drm/virtio/virtgpu_vram.c | 17 +++++++++-------- 3 files changed, 13 insertions(+), 12 deletions(-)
Quoted 5 lines
---
base-commit:587858367581b9c55c3690f4e63382ad622719d4
change-id: 20260915-fixes-8c6917e90665Best regards,
Applied to misc-fixes, thanks!
Signature · 2 lines
Best regards, Dmitry