mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/4] drm/virtio: fix GEM object leaks on error paths
@ 2026-09-15  7:39 Junrui Luo via B4 Relay
  2026-09-15  7:39 ` [PATCH v2 1/4] drm/virtio: fix object leak when drm_gem_handle_create() fails Junrui Luo via B4 Relay
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Junrui Luo via B4 Relay @ 2026-09-15  7:39 UTC (permalink / raw)
  To: David Airlie, Gerd Hoffmann, Dmitry Osipenko, Gurchetan Singh,
	Chia-I Wu, Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann,
	Simona Vetter, Michael S. Tsirkin, Dave Airlie
  Cc: dri-devel, virtualization, linux-kernel, Junrui Luo, Yuhao Jiang

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/55240372-c265-4b38-be94-f29a58ddbb22@collabora.com/

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/20260815-virtgpu-gem-create-leak-v1-1-a4e9fb18caa3@outlook.com

Signed-off-by: Junrui Luo <moonafterrain@outlook.com>
---
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,
-- 
Junrui Luo <moonafterrain@outlook.com>



^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 1/4] drm/virtio: fix object leak when drm_gem_handle_create() fails
  2026-09-15  7:39 [PATCH v2 0/4] drm/virtio: fix GEM object leaks on error paths Junrui Luo via B4 Relay
@ 2026-09-15  7:39 ` Junrui Luo via B4 Relay
  2026-09-15  7:39 ` [PATCH v2 2/4] drm/virtio: fix object leak in virtio_gpu_resource_create_ioctl() Junrui Luo via B4 Relay
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Junrui Luo via B4 Relay @ 2026-09-15  7:39 UTC (permalink / raw)
  To: David Airlie, Gerd Hoffmann, Dmitry Osipenko, Gurchetan Singh,
	Chia-I Wu, Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann,
	Simona Vetter, Michael S. Tsirkin, Dave Airlie
  Cc: dri-devel, virtualization, linux-kernel, Junrui Luo, Yuhao Jiang

From: Junrui Luo <moonafterrain@outlook.com>

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.

Fixes: dc5698e80cf7 ("Add virtio gpu driver.")
Reported-by: Yuhao Jiang <danisjiang@gmail.com>
Assisted-by: Claude:claude-opus-5
Signed-off-by: Junrui Luo <moonafterrain@outlook.com>
---
 drivers/gpu/drm/virtio/virtgpu_gem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/virtio/virtgpu_gem.c b/drivers/gpu/drm/virtio/virtgpu_gem.c
index 66c3f6f74e9c..d2f0b8a3f172 100644
--- a/drivers/gpu/drm/virtio/virtgpu_gem.c
+++ b/drivers/gpu/drm/virtio/virtgpu_gem.c
@@ -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;
 	}
 

-- 
2.51.2



^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 2/4] drm/virtio: fix object leak in virtio_gpu_resource_create_ioctl()
  2026-09-15  7:39 [PATCH v2 0/4] drm/virtio: fix GEM object leaks on error paths Junrui Luo via B4 Relay
  2026-09-15  7:39 ` [PATCH v2 1/4] drm/virtio: fix object leak when drm_gem_handle_create() fails Junrui Luo via B4 Relay
@ 2026-09-15  7:39 ` Junrui Luo via B4 Relay
  2026-09-15  7:39 ` [PATCH v2 3/4] drm/virtio: fix object leaks in virtio_gpu_resource_create_blob_ioctl() Junrui Luo via B4 Relay
  2026-09-15  7:39 ` [PATCH v2 4/4] drm/virtio: release the GEM object on virtio_gpu_vram_create() errors Junrui Luo via B4 Relay
  3 siblings, 0 replies; 5+ messages in thread
From: Junrui Luo via B4 Relay @ 2026-09-15  7:39 UTC (permalink / raw)
  To: David Airlie, Gerd Hoffmann, Dmitry Osipenko, Gurchetan Singh,
	Chia-I Wu, Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann,
	Simona Vetter, Michael S. Tsirkin, Dave Airlie
  Cc: dri-devel, virtualization, linux-kernel, Junrui Luo

From: Junrui Luo <moonafterrain@outlook.com>

virtio_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.

Fixes: 62fb7a5e1096 ("virtio-gpu: add 3d/virgl support")
Assisted-by: Claude:claude-opus-5
Signed-off-by: Junrui Luo <moonafterrain@outlook.com>
---
 drivers/gpu/drm/virtio/virtgpu_ioctl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/virtio/virtgpu_ioctl.c b/drivers/gpu/drm/virtio/virtgpu_ioctl.c
index 3d8e4ccdb7c1..d16f07abb266 100644
--- a/drivers/gpu/drm/virtio/virtgpu_ioctl.c
+++ b/drivers/gpu/drm/virtio/virtgpu_ioctl.c
@@ -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;
 	}
 

-- 
2.51.2



^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 3/4] drm/virtio: fix object leaks in virtio_gpu_resource_create_blob_ioctl()
  2026-09-15  7:39 [PATCH v2 0/4] drm/virtio: fix GEM object leaks on error paths Junrui Luo via B4 Relay
  2026-09-15  7:39 ` [PATCH v2 1/4] drm/virtio: fix object leak when drm_gem_handle_create() fails Junrui Luo via B4 Relay
  2026-09-15  7:39 ` [PATCH v2 2/4] drm/virtio: fix object leak in virtio_gpu_resource_create_ioctl() Junrui Luo via B4 Relay
@ 2026-09-15  7:39 ` Junrui Luo via B4 Relay
  2026-09-15  7:39 ` [PATCH v2 4/4] drm/virtio: release the GEM object on virtio_gpu_vram_create() errors Junrui Luo via B4 Relay
  3 siblings, 0 replies; 5+ messages in thread
From: Junrui Luo via B4 Relay @ 2026-09-15  7:39 UTC (permalink / raw)
  To: David Airlie, Gerd Hoffmann, Dmitry Osipenko, Gurchetan Singh,
	Chia-I Wu, Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann,
	Simona Vetter, Michael S. Tsirkin, Dave Airlie
  Cc: dri-devel, virtualization, linux-kernel, Junrui Luo

From: Junrui Luo <moonafterrain@outlook.com>

virtio_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.

Fixes: 897b4d1acaf5 ("drm/virtio: implement blob resources: resource create blob ioctl")
Assisted-by: Claude:claude-opus-5
Signed-off-by: Junrui Luo <moonafterrain@outlook.com>
---
 drivers/gpu/drm/virtio/virtgpu_ioctl.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/virtio/virtgpu_ioctl.c b/drivers/gpu/drm/virtio/virtgpu_ioctl.c
index d16f07abb266..fcdb07a37972 100644
--- a/drivers/gpu/drm/virtio/virtgpu_ioctl.c
+++ b/drivers/gpu/drm/virtio/virtgpu_ioctl.c
@@ -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;
 	}
 

-- 
2.51.2



^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 4/4] drm/virtio: release the GEM object on virtio_gpu_vram_create() errors
  2026-09-15  7:39 [PATCH v2 0/4] drm/virtio: fix GEM object leaks on error paths Junrui Luo via B4 Relay
                   ` (2 preceding siblings ...)
  2026-09-15  7:39 ` [PATCH v2 3/4] drm/virtio: fix object leaks in virtio_gpu_resource_create_blob_ioctl() Junrui Luo via B4 Relay
@ 2026-09-15  7:39 ` Junrui Luo via B4 Relay
  3 siblings, 0 replies; 5+ messages in thread
From: Junrui Luo via B4 Relay @ 2026-09-15  7:39 UTC (permalink / raw)
  To: David Airlie, Gerd Hoffmann, Dmitry Osipenko, Gurchetan Singh,
	Chia-I Wu, Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann,
	Simona Vetter, Michael S. Tsirkin, Dave Airlie
  Cc: dri-devel, virtualization, linux-kernel, Junrui Luo

From: Junrui Luo <moonafterrain@outlook.com>

virtio_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.

Fixes: 16845c5d5409 ("drm/virtio: implement blob resources: implement vram object")
Assisted-by: Claude:claude-opus-5
Signed-off-by: Junrui Luo <moonafterrain@outlook.com>
---
 drivers/gpu/drm/virtio/virtgpu_vram.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/drivers/gpu/drm/virtio/virtgpu_vram.c b/drivers/gpu/drm/virtio/virtgpu_vram.c
index 5b4a3ab81cd5..01241ce4d07c 100644
--- a/drivers/gpu/drm/virtio/virtgpu_vram.c
+++ b/drivers/gpu/drm/virtio/virtgpu_vram.c
@@ -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)

-- 
2.51.2



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-09-15  7:39 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-15  7:39 [PATCH v2 0/4] drm/virtio: fix GEM object leaks on error paths Junrui Luo via B4 Relay
2026-09-15  7:39 ` [PATCH v2 1/4] drm/virtio: fix object leak when drm_gem_handle_create() fails Junrui Luo via B4 Relay
2026-09-15  7:39 ` [PATCH v2 2/4] drm/virtio: fix object leak in virtio_gpu_resource_create_ioctl() Junrui Luo via B4 Relay
2026-09-15  7:39 ` [PATCH v2 3/4] drm/virtio: fix object leaks in virtio_gpu_resource_create_blob_ioctl() Junrui Luo via B4 Relay
2026-09-15  7:39 ` [PATCH v2 4/4] drm/virtio: release the GEM object on virtio_gpu_vram_create() errors Junrui Luo via B4 Relay

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®