Skip to contentSkip to conversation

Virtio-GPU S4 support

PATCH Superseded [email protected] 3 participants 19 messages Sep 24, 2025 – Oct 1, 2025

Subsystems (3)

v4 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v4 cover Virtio-GPU S4 support

From: Dongwon Kim <[email protected]>

This patch series introduces support for handling hibernation (S4) in the virtio-gpu driver by implementing .freeze and .restore hooks, along with a PM notifier to restore GPU resources upon resume.

Patch 1 adds virtgpu_freeze and virtgpu_restore functions, which manage virtio queue teardown before suspend/hibernation and reinitialization during resume.

Patch 2 implements a mechanism to track and restore virtio_gpu_object instances, addressing the issue where QEMU discards resources during virtio-gpu-reset triggered by hibernation.

Patch 3 adds a PM notifier that triggers resubmission of GPU objects to QEMU when the guest resumes from hibernation.

These changes ensure that the virtio-gpu driver can reliably restore virtio-gpu resources after hibernation, preventing resource loss that leads to system's instability.

v2: 10ms sleep is added in virtgpu_freeze to avoid the situation
the driver is locked up during resumption.

v3: Plain 10ms delay (v2) is replaced with wait calls which wait until
the virtio queue is empty.
(Dmitry Osipenko)

v4: New version of patchset only covers S4 case because loss of resources
in S3 case can be avoided by skipping virtio-gpu-reset in QEMU (hw/display/virtio-gpu.c). For this, virtio-gpu-pci device should be attached to PCIE bus AND a PCIE option, 'x-pcie-pm-no-soft-reset' needs to be enabled when launching QEMU
(e.g. -device virtio-gpu-pci,bus=port,x-pcie-pm-no-soft-reset=true)

Dongwon Kim (3):
drm/virtio: Freeze and restore hooks to support suspend and resume drm/virtio: Add support for saving and restoring virtio_gpu_objects drm/virtio: Restore Virt-GPU res on hibernation resume via PM notifier

 drivers/gpu/drm/virtio/virtgpu_drv.c    | 60 ++++++++++++++++++++-
 drivers/gpu/drm/virtio/virtgpu_drv.h    | 13 +++++
 drivers/gpu/drm/virtio/virtgpu_kms.c    | 49 ++++++++++++++---
 drivers/gpu/drm/virtio/virtgpu_object.c | 71 +++++++++++++++++++++++++
 4 files changed, 186 insertions(+), 7 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v4 1/3 drm/virtio: Freeze and restore hooks to support S3/S4

From: Dongwon Kim <[email protected]>

Implementing functions for .freeze and .restore hooks for virtio-gpu driver Just like other virtio-devices, all virtio gueues are removed then recreated during suspend/hiberation and resume cycle.

v2: 10ms sleep was added in virtgpu_freeze to avoid the situation
the driver is locked up during resumption.

v3: Plain 10ms delay was replaced with wait calls which wait until
the virtio queue is empty.
(Dmitry Osipenko)

---
 drivers/gpu/drm/virtio/virtgpu_drv.c | 60 +++++++++++++++++++++++++++-
 drivers/gpu/drm/virtio/virtgpu_drv.h |  1 +
 drivers/gpu/drm/virtio/virtgpu_kms.c | 23 ++++++++---
 3 files changed, 77 insertions(+), 7 deletions(-)
drivers/gpu/drm/virtio/virtgpu_drv.c +59 -1
index 71c6ccad4b99..676893e90a9f 100644
@@ -163,6 +163,60 @@ static unsigned int features[] = {
 	VIRTIO_GPU_F_RESOURCE_BLOB,
 	VIRTIO_GPU_F_CONTEXT_INIT,
 };
+
+#ifdef CONFIG_PM_SLEEP
+static int virtgpu_freeze(struct virtio_device *vdev)
+{
+	struct drm_device *dev = vdev->priv;
+	struct virtio_gpu_device *vgdev = dev->dev_private;
+	int error;
+
+	error = drm_mode_config_helper_suspend(dev);
+	if (error) {
+		DRM_ERROR("suspend error %d\n", error);
+		return error;
+	}
+
+	flush_work(&vgdev->obj_free_work);
+	flush_work(&vgdev->ctrlq.dequeue_work);
+	flush_work(&vgdev->cursorq.dequeue_work);
+	flush_work(&vgdev->config_changed_work);
+
+	wait_event(vgdev->ctrlq.ack_queue,
+		   vgdev->ctrlq.vq->num_free == vgdev->ctrlq.vq->num_max);
+
+	wait_event(vgdev->cursorq.ack_queue,
+		   vgdev->cursorq.vq->num_free == vgdev->cursorq.vq->num_max);
+
+	vdev->config->del_vqs(vdev);
+
+	return 0;
+}
+
+static int virtgpu_restore(struct virtio_device *vdev)
+{
+	struct drm_device *dev = vdev->priv;
+	struct virtio_gpu_device *vgdev = dev->dev_private;
+	int error;
+
+	error = virtio_gpu_find_vqs(vgdev);
+	if (error) {
+		DRM_ERROR("failed to find virt queues\n");
+		return error;
+	}
+
+	virtio_device_ready(vdev);
+
+	error = drm_mode_config_helper_resume(dev);
+	if (error) {
+		DRM_ERROR("resume error %d\n", error);
+		return error;
+	}
+
+	return 0;
+}
+#endif
+
 static struct virtio_driver virtio_gpu_driver = {
 	.feature_table = features,
 	.feature_table_size = ARRAY_SIZE(features),
@@ -171,7 +225,11 @@ static struct virtio_driver virtio_gpu_driver = {
 	.probe = virtio_gpu_probe,
 	.remove = virtio_gpu_remove,
 	.shutdown = virtio_gpu_shutdown,
-	.config_changed = virtio_gpu_config_changed
+	.config_changed = virtio_gpu_config_changed,
+#ifdef CONFIG_PM_SLEEP
+	.freeze = virtgpu_freeze,
+	.restore = virtgpu_restore,
+#endif
 };
 
 static int __init virtio_gpu_driver_init(void)
drivers/gpu/drm/virtio/virtgpu_drv.h +1 -0
index f17660a71a3e..1279f998c8e0 100644
@@ -300,6 +300,7 @@ void virtio_gpu_deinit(struct drm_device *dev);
 void virtio_gpu_release(struct drm_device *dev);
 int virtio_gpu_driver_open(struct drm_device *dev, struct drm_file *file);
 void virtio_gpu_driver_postclose(struct drm_device *dev, struct drm_file *file);
+int virtio_gpu_find_vqs(struct virtio_gpu_device *vgdev);
 
 /* virtgpu_gem.c */
 int virtio_gpu_gem_object_open(struct drm_gem_object *obj,
drivers/gpu/drm/virtio/virtgpu_kms.c +17 -6
index 1c15cbf326b7..cbebe19c3fb3 100644
@@ -114,15 +114,28 @@ static void virtio_gpu_get_capsets(struct virtio_gpu_device *vgdev,
 	vgdev->num_capsets = num_capsets;
 }
 
-int virtio_gpu_init(struct virtio_device *vdev, struct drm_device *dev)
+int virtio_gpu_find_vqs(struct virtio_gpu_device *vgdev)
 {
 	struct virtqueue_info vqs_info[] = {
 		{ "control", virtio_gpu_ctrl_ack },
 		{ "cursor", virtio_gpu_cursor_ack },
 	};
-	struct virtio_gpu_device *vgdev;
-	/* this will expand later */
 	struct virtqueue *vqs[2];
+	int ret;
+
+	ret = virtio_find_vqs(vgdev->vdev, 2, vqs, vqs_info, NULL);
+	if (ret)
+		return ret;
+
+	vgdev->ctrlq.vq = vqs[0];
+	vgdev->cursorq.vq = vqs[1];
+
+	return 0;
+}
+
+int virtio_gpu_init(struct virtio_device *vdev, struct drm_device *dev)
+{
+	struct virtio_gpu_device *vgdev;
 	u32 num_scanouts, num_capsets;
 	int ret = 0;
 
@@ -206,13 +219,11 @@ int virtio_gpu_init(struct virtio_device *vdev, struct drm_device *dev)
 	DRM_INFO("features: %ccontext_init\n",
 		 vgdev->has_context_init ? '+' : '-');
 
-	ret = virtio_find_vqs(vgdev->vdev, 2, vqs, vqs_info, NULL);
+	ret = virtio_gpu_find_vqs(vgdev);
 	if (ret) {
 		DRM_ERROR("failed to find virt queues\n");
 		goto err_vqs;
 	}
-	vgdev->ctrlq.vq = vqs[0];
-	vgdev->cursorq.vq = vqs[1];
 	ret = virtio_gpu_alloc_vbufs(vgdev);
 	if (ret) {
 		DRM_ERROR("failed to alloc vbufs\n");
Signature · 1 line
2.34.1
maintainer Tested-by on v4 1/3
#2 lore raw

Re: [PATCH v4 1/3] drm/virtio: Freeze and restore hooks to support S3/S4

Quoted 153 lines · On 9/24/25 22:47, [email protected] wrote:

From: Dongwon Kim <[email protected]>

Implementing functions for .freeze and .restore hooks for virtio-gpu driver Just like other virtio-devices, all virtio gueues are removed then recreated during suspend/hiberation and resume cycle.

v2: 10ms sleep was added in virtgpu_freeze to avoid the situation
the driver is locked up during resumption.

v3: Plain 10ms delay was replaced with wait calls which wait until
the virtio queue is empty.
(Dmitry Osipenko)

---
 drivers/gpu/drm/virtio/virtgpu_drv.c | 60 +++++++++++++++++++++++++++-
 drivers/gpu/drm/virtio/virtgpu_drv.h |  1 +
 drivers/gpu/drm/virtio/virtgpu_kms.c | 23 ++++++++---
 3 files changed, 77 insertions(+), 7 deletions(-)
drivers/gpu/drm/virtio/virtgpu_drv.c +59 -1
index 71c6ccad4b99..676893e90a9f 100644
@@ -163,6 +163,60 @@ static unsigned int features[] = {
 	VIRTIO_GPU_F_RESOURCE_BLOB,
 	VIRTIO_GPU_F_CONTEXT_INIT,
 };
+
+#ifdef CONFIG_PM_SLEEP
+static int virtgpu_freeze(struct virtio_device *vdev)
+{
+	struct drm_device *dev = vdev->priv;
+	struct virtio_gpu_device *vgdev = dev->dev_private;
+	int error;
+
+	error = drm_mode_config_helper_suspend(dev);
+	if (error) {
+		DRM_ERROR("suspend error %d\n", error);
+		return error;
+	}
+
+	flush_work(&vgdev->obj_free_work);
+	flush_work(&vgdev->ctrlq.dequeue_work);
+	flush_work(&vgdev->cursorq.dequeue_work);
+	flush_work(&vgdev->config_changed_work);
+
+	wait_event(vgdev->ctrlq.ack_queue,
+		   vgdev->ctrlq.vq->num_free == vgdev->ctrlq.vq->num_max);
+
+	wait_event(vgdev->cursorq.ack_queue,
+		   vgdev->cursorq.vq->num_free == vgdev->cursorq.vq->num_max);
+
+	vdev->config->del_vqs(vdev);
+
+	return 0;
+}
+
+static int virtgpu_restore(struct virtio_device *vdev)
+{
+	struct drm_device *dev = vdev->priv;
+	struct virtio_gpu_device *vgdev = dev->dev_private;
+	int error;
+
+	error = virtio_gpu_find_vqs(vgdev);
+	if (error) {
+		DRM_ERROR("failed to find virt queues\n");
+		return error;
+	}
+
+	virtio_device_ready(vdev);
+
+	error = drm_mode_config_helper_resume(dev);
+	if (error) {
+		DRM_ERROR("resume error %d\n", error);
+		return error;
+	}
+
+	return 0;
+}
+#endif
+
 static struct virtio_driver virtio_gpu_driver = {
 	.feature_table = features,
 	.feature_table_size = ARRAY_SIZE(features),
@@ -171,7 +225,11 @@ static struct virtio_driver virtio_gpu_driver = {
 	.probe = virtio_gpu_probe,
 	.remove = virtio_gpu_remove,
 	.shutdown = virtio_gpu_shutdown,
-	.config_changed = virtio_gpu_config_changed
+	.config_changed = virtio_gpu_config_changed,
+#ifdef CONFIG_PM_SLEEP
+	.freeze = virtgpu_freeze,
+	.restore = virtgpu_restore,
+#endif
 };
 
 static int __init virtio_gpu_driver_init(void)
drivers/gpu/drm/virtio/virtgpu_drv.h +1 -0
index f17660a71a3e..1279f998c8e0 100644
@@ -300,6 +300,7 @@ void virtio_gpu_deinit(struct drm_device *dev);
 void virtio_gpu_release(struct drm_device *dev);
 int virtio_gpu_driver_open(struct drm_device *dev, struct drm_file *file);
 void virtio_gpu_driver_postclose(struct drm_device *dev, struct drm_file *file);
+int virtio_gpu_find_vqs(struct virtio_gpu_device *vgdev);
 
 /* virtgpu_gem.c */
 int virtio_gpu_gem_object_open(struct drm_gem_object *obj,
drivers/gpu/drm/virtio/virtgpu_kms.c +16 -4
index 1c15cbf326b7..cbebe19c3fb3 100644
@@ -114,15 +114,28 @@ static void virtio_gpu_get_capsets(struct virtio_gpu_device *vgdev,
 	vgdev->num_capsets = num_capsets;
 }
 
-int virtio_gpu_init(struct virtio_device *vdev, struct drm_device *dev)
+int virtio_gpu_find_vqs(struct virtio_gpu_device *vgdev)
 {
 	struct virtqueue_info vqs_info[] = {
 		{ "control", virtio_gpu_ctrl_ack },
 		{ "cursor", virtio_gpu_cursor_ack },
 	};
-	struct virtio_gpu_device *vgdev;
-	/* this will expand later */
 	struct virtqueue *vqs[2];
+	int ret;
+
+	ret = virtio_find_vqs(vgdev->vdev, 2, vqs, vqs_info, NULL);
+	if (ret)
+		return ret;
+
+	vgdev->ctrlq.vq = vqs[0];
+	vgdev->cursorq.vq = vqs[1];
+
+	return 0;
+}
+
+int virtio_gpu_init(struct virtio_device *vdev, struct drm_device *dev)
+{
+	struct virtio_gpu_device *vgdev;
 	u32 num_scanouts, num_capsets;
 	int ret = 0;
 
@@ -206,13 +219,11 @@ int virtio_gpu_init(struct virtio_device *vdev, struct drm_device *dev)
 	DRM_INFO("features: %ccontext_init\n",
 		 vgdev->has_context_init ? '+' : '-');
 
-	ret = virtio_find_vqs(vgdev->vdev, 2, vqs, vqs_info, NULL);

drivers/gpu/drm/virtio/virtgpu_kms.c int virtio_gpu_init(struct virtio_device *vdev, struct drm_device *dev) · line 229

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_kms.c +1 -2
+	ret = virtio_gpu_find_vqs(vgdev);
 	if (ret) {
 		DRM_ERROR("failed to find virt queues\n");
 		goto err_vqs;
 	}
-	vgdev->ctrlq.vq = vqs[0];
-	vgdev->cursorq.vq = vqs[1];
 	ret = virtio_gpu_alloc_vbufs(vgdev);
 	if (ret) {
 		DRM_ERROR("failed to alloc vbufs\n");
Signature · 2 lines
Best regards,
Dmitry
#3 lore raw

PATCH v4 2/3 drm/virtio: Add support for saving and restoring virtio_gpu_objects

In reply to [email protected]

From: Dongwon Kim <[email protected]>

When the host KVM/QEMU resumes from hibernation, it loses all graphics resources previously submitted by the guest OS, as the QEMU process is terminated during the suspend-resume cycle. This leads to invalid resource errors when the guest OS attempts to interact with the host using those resources after resumption.

To resolve this, the virtio-gpu driver now tracks all active virtio_gpu_objects and provides a mechanism to restore them by re-submitting the objects to QEMU when needed (e.g., during resume from hibernation).

v2: - Attach backing is done if bo->attached was set before

v3: - Restoration of virtio-gpu resources is no longer triggered via .restore;
instead, it is handled by a PM notifier only during hibernation.

Cc Dmitry Osipenko <[email protected]>
Cc Vivek Kasireddy <[email protected]>
---
 drivers/gpu/drm/virtio/virtgpu_drv.h    | 10 ++++
 drivers/gpu/drm/virtio/virtgpu_kms.c    |  1 +
 drivers/gpu/drm/virtio/virtgpu_object.c | 71 +++++++++++++++++++++++++
 3 files changed, 82 insertions(+)
drivers/gpu/drm/virtio/virtgpu_drv.h +10 -0
index 1279f998c8e0..55f836378237 100644
@@ -126,6 +126,12 @@ struct virtio_gpu_object_array {
 	struct drm_gem_object *objs[] __counted_by(total);
 };
 
+struct virtio_gpu_object_restore {
+	struct virtio_gpu_object *bo;
+	struct virtio_gpu_object_params params;
+	struct list_head node;
+};
+
 struct virtio_gpu_vbuffer;
 struct virtio_gpu_device;
 
@@ -265,6 +271,7 @@ struct virtio_gpu_device {
 	struct work_struct obj_free_work;
 	spinlock_t obj_free_lock;
 	struct list_head obj_free_list;
+	struct list_head obj_restore;
 
 	struct virtio_gpu_drv_capset *capsets;
 	uint32_t num_capsets;
@@ -479,6 +486,9 @@ bool virtio_gpu_is_shmem(struct virtio_gpu_object *bo);
 
 int virtio_gpu_resource_id_get(struct virtio_gpu_device *vgdev,
 			       uint32_t *resid);
+
+int virtio_gpu_object_restore_all(struct virtio_gpu_device *vgdev);
+
 /* virtgpu_prime.c */
 int virtio_gpu_resource_assign_uuid(struct virtio_gpu_device *vgdev,
 				    struct virtio_gpu_object *bo);
drivers/gpu/drm/virtio/virtgpu_kms.c +1 -0
index cbebe19c3fb3..08f8e71a7072 100644
@@ -162,6 +162,7 @@ int virtio_gpu_init(struct virtio_device *vdev, struct drm_device *dev)
 	vgdev->fence_drv.context = dma_fence_context_alloc(1);
 	spin_lock_init(&vgdev->fence_drv.lock);
 	INIT_LIST_HEAD(&vgdev->fence_drv.fences);
+	INIT_LIST_HEAD(&vgdev->obj_restore);
 	INIT_LIST_HEAD(&vgdev->cap_cache);
 	INIT_WORK(&vgdev->config_changed_work,
 		  virtio_gpu_config_changed_work_func);
drivers/gpu/drm/virtio/virtgpu_object.c +71 -0

92 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer Tested-by on v4 2/3
#4 lore raw

Re: [PATCH v4 2/3] drm/virtio: Add support for saving and restoring virtio_gpu_objects

Quoted 163 lines · On 9/24/25 22:47, [email protected] wrote:

From: Dongwon Kim <[email protected]>

When the host KVM/QEMU resumes from hibernation, it loses all graphics resources previously submitted by the guest OS, as the QEMU process is terminated during the suspend-resume cycle. This leads to invalid resource errors when the guest OS attempts to interact with the host using those resources after resumption.

To resolve this, the virtio-gpu driver now tracks all active virtio_gpu_objects and provides a mechanism to restore them by re-submitting the objects to QEMU when needed (e.g., during resume from hibernation).

v2: - Attach backing is done if bo->attached was set before

v3: - Restoration of virtio-gpu resources is no longer triggered via .restore;
instead, it is handled by a PM notifier only during hibernation.

Cc Dmitry Osipenko <[email protected]>
Cc Vivek Kasireddy <[email protected]>
---
 drivers/gpu/drm/virtio/virtgpu_drv.h    | 10 ++++
 drivers/gpu/drm/virtio/virtgpu_kms.c    |  1 +
 drivers/gpu/drm/virtio/virtgpu_object.c | 71 +++++++++++++++++++++++++
 3 files changed, 82 insertions(+)
drivers/gpu/drm/virtio/virtgpu_drv.h +10 -0
index 1279f998c8e0..55f836378237 100644
@@ -126,6 +126,12 @@ struct virtio_gpu_object_array {
 	struct drm_gem_object *objs[] __counted_by(total);
 };
 
+struct virtio_gpu_object_restore {
+	struct virtio_gpu_object *bo;
+	struct virtio_gpu_object_params params;
+	struct list_head node;
+};
+
 struct virtio_gpu_vbuffer;
 struct virtio_gpu_device;
 
@@ -265,6 +271,7 @@ struct virtio_gpu_device {
 	struct work_struct obj_free_work;
 	spinlock_t obj_free_lock;
 	struct list_head obj_free_list;
+	struct list_head obj_restore;
 
 	struct virtio_gpu_drv_capset *capsets;
 	uint32_t num_capsets;
@@ -479,6 +486,9 @@ bool virtio_gpu_is_shmem(struct virtio_gpu_object *bo);
 
 int virtio_gpu_resource_id_get(struct virtio_gpu_device *vgdev,
 			       uint32_t *resid);
+
+int virtio_gpu_object_restore_all(struct virtio_gpu_device *vgdev);
+
 /* virtgpu_prime.c */
 int virtio_gpu_resource_assign_uuid(struct virtio_gpu_device *vgdev,
 				    struct virtio_gpu_object *bo);
drivers/gpu/drm/virtio/virtgpu_kms.c +1 -0
index cbebe19c3fb3..08f8e71a7072 100644
@@ -162,6 +162,7 @@ int virtio_gpu_init(struct virtio_device *vdev, struct drm_device *dev)
 	vgdev->fence_drv.context = dma_fence_context_alloc(1);
 	spin_lock_init(&vgdev->fence_drv.lock);
 	INIT_LIST_HEAD(&vgdev->fence_drv.fences);
+	INIT_LIST_HEAD(&vgdev->obj_restore);
 	INIT_LIST_HEAD(&vgdev->cap_cache);
 	INIT_WORK(&vgdev->config_changed_work,
 		  virtio_gpu_config_changed_work_func);
drivers/gpu/drm/virtio/virtgpu_object.c +61 -0
index e6363c887500..28fdfc70fa49 100644
@@ -61,6 +61,38 @@ static void virtio_gpu_resource_id_put(struct virtio_gpu_device *vgdev, uint32_t
 		ida_free(&vgdev->resource_ida, id - 1);
 }
 
+static void virtio_gpu_object_add_restore_list(struct virtio_gpu_device *vgdev,
+					       struct virtio_gpu_object *bo,
+					       struct virtio_gpu_object_params *params)
+{
+	struct virtio_gpu_object_restore *new;
+
+	new = kvmalloc(sizeof(*new), GFP_KERNEL);
+	if (!new) {
+		DRM_ERROR("Fail to allocate virtio_gpu_object_restore");
+		return;
+	}
+
+	new->bo = bo;
+	memcpy(&new->params, params, sizeof(*params));
+
+	list_add_tail(&new->node, &vgdev->obj_restore);
+}
+
+static void virtio_gpu_object_del_restore_list(struct virtio_gpu_device *vgdev,
+					       struct virtio_gpu_object *bo)
+{
+	struct virtio_gpu_object_restore *curr, *tmp;
+
+	list_for_each_entry_safe(curr, tmp, &vgdev->obj_restore, node) {
+		if (bo == curr->bo) {
+			list_del(&curr->node);
+			kvfree(curr);
+			break;
+		}
+	}
+}
+
 void virtio_gpu_cleanup_object(struct virtio_gpu_object *bo)
 {
 	struct virtio_gpu_device *vgdev = bo->base.base.dev->dev_private;
@@ -84,6 +116,7 @@ void virtio_gpu_cleanup_object(struct virtio_gpu_object *bo)
 		drm_gem_object_release(&bo->base.base);
 		kfree(bo);
 	}
+	virtio_gpu_object_del_restore_list(vgdev, bo);
 }
 
 static void virtio_gpu_free_object(struct drm_gem_object *obj)
@@ -258,6 +291,8 @@ int virtio_gpu_object_create(struct virtio_gpu_device *vgdev,
 		virtio_gpu_object_attach(vgdev, bo, ents, nents);
 	}
 
+	/* add submitted object to restore list */
+	virtio_gpu_object_add_restore_list(vgdev, bo, params);
 	*bo_ptr = bo;
 	return 0;
 
@@ -271,3 +306,39 @@ int virtio_gpu_object_create(struct virtio_gpu_device *vgdev,
 	drm_gem_shmem_free(shmem_obj);
 	return ret;
 }
+
+int virtio_gpu_object_restore_all(struct virtio_gpu_device *vgdev)
+{
+	struct virtio_gpu_object_restore *curr, *tmp;
+	struct virtio_gpu_mem_entry *ents;
+	unsigned int nents;
+	int ret;
+
+	list_for_each_entry_safe(curr, tmp, &vgdev->obj_restore, node) {
+		ret = virtio_gpu_object_shmem_init(vgdev, curr->bo, &ents, &nents);
+		if (ret)
+			break;
+
+		if (curr->params.blob) {
+			virtio_gpu_cmd_resource_create_blob(vgdev, curr->bo, &curr->params,
+							    ents, nents);
+		} else if (curr->params.virgl) {
+			virtio_gpu_cmd_resource_create_3d(vgdev, curr->bo, &curr->params,
+							  NULL, NULL);
+
+			if (curr->bo->attached) {
+				curr->bo->attached = false;
+				virtio_gpu_object_attach(vgdev, curr->bo, ents, nents);
+			}
+		} else {
+			virtio_gpu_cmd_create_resource(vgdev, curr->bo, &curr->params,

drivers/gpu/drm/virtio/virtgpu_object.c int virtio_gpu_object_create(struct virtio_gpu_device *vgdev, · line 344

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_object.c +10 -0
+						       NULL, NULL);
+			if (curr->bo->attached) {
+				curr->bo->attached = false;
+				virtio_gpu_object_attach(vgdev, curr->bo, ents, nents);
+			}
+		}
+	}
+
+	return ret;
+}
Signature · 2 lines
Best regards,
Dmitry
#5 lore raw

Re: [PATCH v4 2/3] drm/virtio: Add support for saving and restoring virtio_gpu_objects

In reply to [email protected]
Quoted 44 lines · On 24.09.25 21:47, [email protected] wrote:

From: Dongwon Kim <[email protected]>

When the host KVM/QEMU resumes from hibernation, it loses all graphics resources previously submitted by the guest OS, as the QEMU process is terminated during the suspend-resume cycle. This leads to invalid resource errors when the guest OS attempts to interact with the host using those resources after resumption.

To resolve this, the virtio-gpu driver now tracks all active virtio_gpu_objects and provides a mechanism to restore them by re-submitting the objects to QEMU when needed (e.g., during resume from hibernation).

v2: - Attach backing is done if bo->attached was set before

v3: - Restoration of virtio-gpu resources is no longer triggered via .restore;
instead, it is handled by a PM notifier only during hibernation.

Cc Dmitry Osipenko <[email protected]>
Cc Vivek Kasireddy <[email protected]>
---
  drivers/gpu/drm/virtio/virtgpu_drv.h    | 10 ++++
  drivers/gpu/drm/virtio/virtgpu_kms.c    |  1 +
  drivers/gpu/drm/virtio/virtgpu_object.c | 71 +++++++++++++++++++++++++
  3 files changed, 82 insertions(+)
drivers/gpu/drm/virtio/virtgpu_drv.h +6 -0
index 1279f998c8e0..55f836378237 100644
@@ -126,6 +126,12 @@ struct virtio_gpu_object_array {
  	struct drm_gem_object *objs[] __counted_by(total);
  };
 
+struct virtio_gpu_object_restore {
+	struct virtio_gpu_object *bo;
+	struct virtio_gpu_object_params params;
+	struct list_head node;
+};
+
  struct virtio_gpu_vbuffer;
  struct virtio_gpu_device;
 

drivers/gpu/drm/virtio/virtgpu_drv.h struct virtio_gpu_device { · line 274

Quoted 4 lines
drivers/gpu/drm/virtio/virtgpu_drv.h +1 -0
@@ -265,6 +271,7 @@ struct virtio_gpu_device {
  	struct work_struct obj_free_work;
  	spinlock_t obj_free_lock;
  	struct list_head obj_free_list;
+	struct list_head obj_restore;

I am not very familiar with the code but I am curious do we not a lock to keep the list same?

Quoted 69 lines
drivers/gpu/drm/virtio/virtgpu_drv.h +3 -0
  	struct virtio_gpu_drv_capset *capsets;
  	uint32_t num_capsets;
@@ -479,6 +486,9 @@ bool virtio_gpu_is_shmem(struct virtio_gpu_object *bo);
 
  int virtio_gpu_resource_id_get(struct virtio_gpu_device *vgdev,
  			       uint32_t *resid);
+
+int virtio_gpu_object_restore_all(struct virtio_gpu_device *vgdev);
+
  /* virtgpu_prime.c */
  int virtio_gpu_resource_assign_uuid(struct virtio_gpu_device *vgdev,
  				    struct virtio_gpu_object *bo);
drivers/gpu/drm/virtio/virtgpu_kms.c +1 -0
index cbebe19c3fb3..08f8e71a7072 100644
@@ -162,6 +162,7 @@ int virtio_gpu_init(struct virtio_device *vdev, struct drm_device *dev)
  	vgdev->fence_drv.context = dma_fence_context_alloc(1);
  	spin_lock_init(&vgdev->fence_drv.lock);
  	INIT_LIST_HEAD(&vgdev->fence_drv.fences);
+	INIT_LIST_HEAD(&vgdev->obj_restore);
  	INIT_LIST_HEAD(&vgdev->cap_cache);
  	INIT_WORK(&vgdev->config_changed_work,
  		  virtio_gpu_config_changed_work_func);
drivers/gpu/drm/virtio/virtgpu_object.c +32 -0
index e6363c887500..28fdfc70fa49 100644
@@ -61,6 +61,38 @@ static void virtio_gpu_resource_id_put(struct virtio_gpu_device *vgdev, uint32_t
  		ida_free(&vgdev->resource_ida, id - 1);
  }
 
+static void virtio_gpu_object_add_restore_list(struct virtio_gpu_device *vgdev,
+					       struct virtio_gpu_object *bo,
+					       struct virtio_gpu_object_params *params)
+{
+	struct virtio_gpu_object_restore *new;
+
+	new = kvmalloc(sizeof(*new), GFP_KERNEL);
+	if (!new) {
+		DRM_ERROR("Fail to allocate virtio_gpu_object_restore");
+		return;
+	}
+
+	new->bo = bo;
+	memcpy(&new->params, params, sizeof(*params));
+
+	list_add_tail(&new->node, &vgdev->obj_restore);
+}
+
+static void virtio_gpu_object_del_restore_list(struct virtio_gpu_device *vgdev,
+					       struct virtio_gpu_object *bo)
+{
+	struct virtio_gpu_object_restore *curr, *tmp;
+
+	list_for_each_entry_safe(curr, tmp, &vgdev->obj_restore, node) {
+		if (bo == curr->bo) {
+			list_del(&curr->node);
+			kvfree(curr);
+			break;
+		}
+	}
+}
+
  void virtio_gpu_cleanup_object(struct virtio_gpu_object *bo)
  {
  	struct virtio_gpu_device *vgdev = bo->base.base.dev->dev_private;

drivers/gpu/drm/virtio/virtgpu_object.c void virtio_gpu_cleanup_object(struct virtio_gpu_object *bo) · line 119

Quoted 4 lines
drivers/gpu/drm/virtio/virtgpu_object.c +1 -0
@@ -84,6 +116,7 @@ void virtio_gpu_cleanup_object(struct virtio_gpu_object *bo)
  		drm_gem_object_release(&bo->base.base);
  		kfree(bo);
  	}
+	virtio_gpu_object_del_restore_list(vgdev, bo);

Is there a possibility to hitting use after free here ?  I see kfree(bo) before this.

Regards,

Nirmoy

Quoted 52 lines
drivers/gpu/drm/virtio/virtgpu_object.c +38 -0
  }
 
  static void virtio_gpu_free_object(struct drm_gem_object *obj)
@@ -258,6 +291,8 @@ int virtio_gpu_object_create(struct virtio_gpu_device *vgdev,
  		virtio_gpu_object_attach(vgdev, bo, ents, nents);
  	}
 
+	/* add submitted object to restore list */
+	virtio_gpu_object_add_restore_list(vgdev, bo, params);
  	*bo_ptr = bo;
  	return 0;
 
@@ -271,3 +306,39 @@ int virtio_gpu_object_create(struct virtio_gpu_device *vgdev,
  	drm_gem_shmem_free(shmem_obj);
  	return ret;
  }
+
+int virtio_gpu_object_restore_all(struct virtio_gpu_device *vgdev)
+{
+	struct virtio_gpu_object_restore *curr, *tmp;
+	struct virtio_gpu_mem_entry *ents;
+	unsigned int nents;
+	int ret;
+
+	list_for_each_entry_safe(curr, tmp, &vgdev->obj_restore, node) {
+		ret = virtio_gpu_object_shmem_init(vgdev, curr->bo, &ents, &nents);
+		if (ret)
+			break;
+
+		if (curr->params.blob) {
+			virtio_gpu_cmd_resource_create_blob(vgdev, curr->bo, &curr->params,
+							    ents, nents);
+		} else if (curr->params.virgl) {
+			virtio_gpu_cmd_resource_create_3d(vgdev, curr->bo, &curr->params,
+							  NULL, NULL);
+
+			if (curr->bo->attached) {
+				curr->bo->attached = false;
+				virtio_gpu_object_attach(vgdev, curr->bo, ents, nents);
+			}
+		} else {
+			virtio_gpu_cmd_create_resource(vgdev, curr->bo, &curr->params,
+						       NULL, NULL);
+			if (curr->bo->attached) {
+				curr->bo->attached = false;
+				virtio_gpu_object_attach(vgdev, curr->bo, ents, nents);
+			}
+		}
+	}
+
+	return ret;
+}
maintainer
#6 lore raw

Re: [PATCH v4 2/3] drm/virtio: Add support for saving and restoring virtio_gpu_objects

On 10/1/25 16:13, Nirmoy Das wrote:
...

Quoted 10 lines
Quoted 7 lines

  struct virtio_gpu_vbuffer;
  struct virtio_gpu_device;
  @@ -265,6 +271,7 @@ struct virtio_gpu_device {
      struct work_struct obj_free_work;
      spinlock_t obj_free_lock;
      struct list_head obj_free_list;
+    struct list_head obj_restore;

I am not very familiar with the code but I am curious do we not a lock to keep the list same?

There should be a lock to protect list

...

Quoted 4 lines
Quoted 4 lines

  void virtio_gpu_cleanup_object(struct virtio_gpu_object *bo) {
      struct virtio_gpu_device *vgdev = bo->base.base.dev->dev_private;

(quoted diff) +0 -0
@@ -84,6 +116,7 @@ void virtio_gpu_cleanup_object(struct
Quoted 8 lines

drivers/gpu/drm/virtio/virtgpu_object.c void virtio_gpu_cleanup_object(struct virtio_gpu_object *bo) · line 119

Quoted 5 lines
drivers/gpu/drm/virtio/virtgpu_object.c +1 -0
 virtio_gpu_object *bo)
           drm_gem_object_release(&bo->base.base);
           kfree(bo);
       }
+    virtio_gpu_object_del_restore_list(vgdev, bo);

Is there a possibility to hitting use after free here ?  I see kfree(bo) before this.

It's UAF bug here. Thanks for the review.

Signature · 2 lines
Best regards,
Dmitry
#7 lore raw

RE: [PATCH v4 2/3] drm/virtio: Add support for saving and restoring virtio_gpu_objects

Hi Nirmoy and Dmitry,

Quoted 13 lines

Subject: Re: [PATCH v4 2/3] drm/virtio: Add support for saving and restoring virtio_gpu_objects

On 10/1/25 16:13, Nirmoy Das wrote:
...

Quoted 8 lines
Quoted 7 lines

  struct virtio_gpu_vbuffer;
  struct virtio_gpu_device;
  @@ -265,6 +271,7 @@ struct virtio_gpu_device {
      struct work_struct obj_free_work;
      spinlock_t obj_free_lock;
      struct list_head obj_free_list;
+    struct list_head obj_restore;

Quoted 4 lines
Quoted 2 lines

I am not very familiar with the code but I am curious do we not a lock to keep the list same?

There should be a lock to protect list

[Kim, Dongwon] Ok, I will take a look and make a proper change there.

Quoted 12 lines

...

Quoted 10 lines
Quoted 9 lines

  void virtio_gpu_cleanup_object(struct virtio_gpu_object *bo) {
      struct virtio_gpu_device *vgdev = bo->base.base.dev->dev_private; @@ -84,6 +116,7 @@ void virtio_gpu_cleanup_object(struct virtio_gpu_object *bo) drm_gem_object_release(&bo->base.base); kfree(bo);
      }
+    virtio_gpu_object_del_restore_list(vgdev, bo);

Quoted 4 lines
Quoted 2 lines

Is there a possibility to hitting use after free here ?  I see kfree(bo) before this.

It's UAF bug here. Thanks for the review.

[Kim, Dongwon] Same here. I will take care of it. Thanks for looking into this.

Quoted 4 lines

--
Best regards,
Dmitry

#8 lore raw

Re: [PATCH v4 2/3] drm/virtio: Add support for saving and restoring virtio_gpu_objects

In reply to [email protected]
Quoted 162 lines · On 24.09.25 21:47, [email protected] wrote:

From: Dongwon Kim <[email protected]>

When the host KVM/QEMU resumes from hibernation, it loses all graphics resources previously submitted by the guest OS, as the QEMU process is terminated during the suspend-resume cycle. This leads to invalid resource errors when the guest OS attempts to interact with the host using those resources after resumption.

To resolve this, the virtio-gpu driver now tracks all active virtio_gpu_objects and provides a mechanism to restore them by re-submitting the objects to QEMU when needed (e.g., during resume from hibernation).

v2: - Attach backing is done if bo->attached was set before

v3: - Restoration of virtio-gpu resources is no longer triggered via .restore;
instead, it is handled by a PM notifier only during hibernation.

Cc Dmitry Osipenko <[email protected]>
Cc Vivek Kasireddy <[email protected]>
---
  drivers/gpu/drm/virtio/virtgpu_drv.h    | 10 ++++
  drivers/gpu/drm/virtio/virtgpu_kms.c    |  1 +
  drivers/gpu/drm/virtio/virtgpu_object.c | 71 +++++++++++++++++++++++++
  3 files changed, 82 insertions(+)
drivers/gpu/drm/virtio/virtgpu_drv.h +10 -0
index 1279f998c8e0..55f836378237 100644
@@ -126,6 +126,12 @@ struct virtio_gpu_object_array {
  	struct drm_gem_object *objs[] __counted_by(total);
  };
 
+struct virtio_gpu_object_restore {
+	struct virtio_gpu_object *bo;
+	struct virtio_gpu_object_params params;
+	struct list_head node;
+};
+
  struct virtio_gpu_vbuffer;
  struct virtio_gpu_device;
 
@@ -265,6 +271,7 @@ struct virtio_gpu_device {
  	struct work_struct obj_free_work;
  	spinlock_t obj_free_lock;
  	struct list_head obj_free_list;
+	struct list_head obj_restore;
 
  	struct virtio_gpu_drv_capset *capsets;
  	uint32_t num_capsets;
@@ -479,6 +486,9 @@ bool virtio_gpu_is_shmem(struct virtio_gpu_object *bo);
 
  int virtio_gpu_resource_id_get(struct virtio_gpu_device *vgdev,
  			       uint32_t *resid);
+
+int virtio_gpu_object_restore_all(struct virtio_gpu_device *vgdev);
+
  /* virtgpu_prime.c */
  int virtio_gpu_resource_assign_uuid(struct virtio_gpu_device *vgdev,
  				    struct virtio_gpu_object *bo);
drivers/gpu/drm/virtio/virtgpu_kms.c +1 -0
index cbebe19c3fb3..08f8e71a7072 100644
@@ -162,6 +162,7 @@ int virtio_gpu_init(struct virtio_device *vdev, struct drm_device *dev)
  	vgdev->fence_drv.context = dma_fence_context_alloc(1);
  	spin_lock_init(&vgdev->fence_drv.lock);
  	INIT_LIST_HEAD(&vgdev->fence_drv.fences);
+	INIT_LIST_HEAD(&vgdev->obj_restore);
  	INIT_LIST_HEAD(&vgdev->cap_cache);
  	INIT_WORK(&vgdev->config_changed_work,
  		  virtio_gpu_config_changed_work_func);
drivers/gpu/drm/virtio/virtgpu_object.c +60 -0
index e6363c887500..28fdfc70fa49 100644
@@ -61,6 +61,38 @@ static void virtio_gpu_resource_id_put(struct virtio_gpu_device *vgdev, uint32_t
  		ida_free(&vgdev->resource_ida, id - 1);
  }
 
+static void virtio_gpu_object_add_restore_list(struct virtio_gpu_device *vgdev,
+					       struct virtio_gpu_object *bo,
+					       struct virtio_gpu_object_params *params)
+{
+	struct virtio_gpu_object_restore *new;
+
+	new = kvmalloc(sizeof(*new), GFP_KERNEL);
+	if (!new) {
+		DRM_ERROR("Fail to allocate virtio_gpu_object_restore");
+		return;
+	}
+
+	new->bo = bo;
+	memcpy(&new->params, params, sizeof(*params));
+
+	list_add_tail(&new->node, &vgdev->obj_restore);
+}
+
+static void virtio_gpu_object_del_restore_list(struct virtio_gpu_device *vgdev,
+					       struct virtio_gpu_object *bo)
+{
+	struct virtio_gpu_object_restore *curr, *tmp;
+
+	list_for_each_entry_safe(curr, tmp, &vgdev->obj_restore, node) {
+		if (bo == curr->bo) {
+			list_del(&curr->node);
+			kvfree(curr);
+			break;
+		}
+	}
+}
+
  void virtio_gpu_cleanup_object(struct virtio_gpu_object *bo)
  {
  	struct virtio_gpu_device *vgdev = bo->base.base.dev->dev_private;
@@ -84,6 +116,7 @@ void virtio_gpu_cleanup_object(struct virtio_gpu_object *bo)
  		drm_gem_object_release(&bo->base.base);
  		kfree(bo);
  	}
+	virtio_gpu_object_del_restore_list(vgdev, bo);
  }
 
  static void virtio_gpu_free_object(struct drm_gem_object *obj)
@@ -258,6 +291,8 @@ int virtio_gpu_object_create(struct virtio_gpu_device *vgdev,
  		virtio_gpu_object_attach(vgdev, bo, ents, nents);
  	}
 
+	/* add submitted object to restore list */
+	virtio_gpu_object_add_restore_list(vgdev, bo, params);
  	*bo_ptr = bo;
  	return 0;
 
@@ -271,3 +306,39 @@ int virtio_gpu_object_create(struct virtio_gpu_device *vgdev,
  	drm_gem_shmem_free(shmem_obj);
  	return ret;
  }
+
+int virtio_gpu_object_restore_all(struct virtio_gpu_device *vgdev)
+{
+	struct virtio_gpu_object_restore *curr, *tmp;
+	struct virtio_gpu_mem_entry *ents;
+	unsigned int nents;
+	int ret;
+
+	list_for_each_entry_safe(curr, tmp, &vgdev->obj_restore, node) {
+		ret = virtio_gpu_object_shmem_init(vgdev, curr->bo, &ents, &nents);
+		if (ret)
+			break;
+
+		if (curr->params.blob) {
+			virtio_gpu_cmd_resource_create_blob(vgdev, curr->bo, &curr->params,
+							    ents, nents);
+		} else if (curr->params.virgl) {
+			virtio_gpu_cmd_resource_create_3d(vgdev, curr->bo, &curr->params,
+							  NULL, NULL);
+
+			if (curr->bo->attached) {
+				curr->bo->attached = false;
+				virtio_gpu_object_attach(vgdev, curr->bo, ents, nents);
+			}
+		} else {

drivers/gpu/drm/virtio/virtgpu_object.c int virtio_gpu_object_create(struct virtio_gpu_device *vgdev, · line 343

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_object.c +10 -0
+			virtio_gpu_cmd_create_resource(vgdev, curr->bo, &curr->params,
+						       NULL, NULL);
+			if (curr->bo->attached) {
+				curr->bo->attached = false;
+				virtio_gpu_object_attach(vgdev, curr->bo, ents, nents);
+			}
+		}
+	}
+
+	return ret;

this should be "return 0" otherwise on success it is returning uninitialized "ret" value.

Quoted 1 line
drivers/gpu/drm/virtio/virtgpu_object.c +1 -0
+}
maintainer
#9 lore raw

Re: [PATCH v4 2/3] drm/virtio: Add support for saving and restoring virtio_gpu_objects

In reply to [email protected]
Quoted 8 lines · On 9/24/25 22:47, [email protected] wrote:

}

drivers/gpu/drm/virtio/virtgpu_object.c +6 -0
+static void virtio_gpu_object_add_restore_list(struct virtio_gpu_device *vgdev,
+					       struct virtio_gpu_object *bo,
+					       struct virtio_gpu_object_params *params)
+{
+	struct virtio_gpu_object_restore *new;
+

drivers/gpu/drm/virtio/virtgpu_object.c static void virtio_gpu_resource_id_put(struct virtio_gpu_device *vgdev, uint32_t · line 79

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_object.c +10 -0
+	new = kvmalloc(sizeof(*new), GFP_KERNEL);
+	if (!new) {
+		DRM_ERROR("Fail to allocate virtio_gpu_object_restore");
+		return;
+	}
+
+	new->bo = bo;
+	memcpy(&new->params, params, sizeof(*params));
+
+	list_add_tail(&new->node, &vgdev->obj_restore);

Would be good if you could embed struct virtio_gpu_object_restore into BO itself and don't have extra kmalloc.

Signature · 2 lines
Best regards,
Dmitry
#10 lore raw

PATCH v4 3/3 drm/virtio: Restore Virt-GPU res on hibernation resume via PM notifier

In reply to [email protected]

From: Dongwon Kim <[email protected]>

Register a PM notifier in virtio-gpu to handle suspend/hibernate events. On PM_POST_HIBERNATION, it resubmits all GPU objects so that resources can properly recovered in QEMU after resume.

---
 drivers/gpu/drm/virtio/virtgpu_drv.h |  2 ++
 drivers/gpu/drm/virtio/virtgpu_kms.c | 25 +++++++++++++++++++++++++
 2 files changed, 27 insertions(+)
drivers/gpu/drm/virtio/virtgpu_drv.h +2 -0
index 55f836378237..88a94e974088 100644
@@ -278,6 +278,8 @@ struct virtio_gpu_device {
 	uint64_t capset_id_mask;
 	struct list_head cap_cache;
 
+	struct notifier_block pm_nb;
+
 	/* protects uuid state when exporting */
 	spinlock_t resource_export_lock;
 	/* protects map state and host_visible_mm */
drivers/gpu/drm/virtio/virtgpu_kms.c +25 -0
index 08f8e71a7072..e1b2cee30fa3 100644
@@ -26,9 +26,12 @@
 #include <linux/virtio.h>
 #include <linux/virtio_config.h>
 #include <linux/virtio_ring.h>
+#include <linux/suspend.h>
+#include <linux/pm_runtime.h>
 
 #include <drm/drm_file.h>
 #include <drm/drm_managed.h>
+#include <drm/drm_atomic_helper.h>
 
 #include "virtgpu_drv.h"
 
@@ -133,6 +136,21 @@ int virtio_gpu_find_vqs(struct virtio_gpu_device *vgdev)
 	return 0;
 }
 
+static int virtio_gpu_pm_notifier(struct notifier_block *nb, unsigned long mode,
+				  void *data)
+{
+	struct virtio_gpu_device *vgdev = container_of(nb, struct virtio_gpu_device, pm_nb);
+	int ret;
+
+	if (mode == PM_POST_HIBERNATION) {
+		ret = virtio_gpu_object_restore_all(vgdev);
+		if (ret)
+			DRM_ERROR("Failed to resubmit virtio-gpu objects\n");
+	}
+
+	return NOTIFY_DONE;
+}
+
 int virtio_gpu_init(struct virtio_device *vdev, struct drm_device *dev)
 {
 	struct virtio_gpu_device *vgdev;
@@ -268,6 +286,12 @@ int virtio_gpu_init(struct virtio_device *vdev, struct drm_device *dev)
 		wait_event_timeout(vgdev->resp_wq, !vgdev->display_info_pending,
 				   5 * HZ);
 	}
+
+	vgdev->pm_nb.notifier_call = virtio_gpu_pm_notifier;
+	ret = register_pm_notifier(&vgdev->pm_nb);
+	if (ret)
+		goto err_scanouts;
+
 	return 0;
 
 err_scanouts:
@@ -299,6 +323,7 @@ void virtio_gpu_deinit(struct drm_device *dev)
 	flush_work(&vgdev->config_changed_work);
 	virtio_reset_device(vgdev->vdev);
 	vgdev->vdev->config->del_vqs(vgdev->vdev);
+	unregister_pm_notifier(&vgdev->pm_nb);
 }
 
 void virtio_gpu_release(struct drm_device *dev)
Signature · 1 line
2.34.1
maintainer Tested-by on v4 3/3
#11 lore raw

Re: [PATCH v4 3/3] drm/virtio: Restore Virt-GPU res on hibernation resume via PM notifier

Quoted 80 lines · On 9/24/25 22:47, [email protected] wrote:

From: Dongwon Kim <[email protected]>

Register a PM notifier in virtio-gpu to handle suspend/hibernate events. On PM_POST_HIBERNATION, it resubmits all GPU objects so that resources can properly recovered in QEMU after resume.

---
 drivers/gpu/drm/virtio/virtgpu_drv.h |  2 ++
 drivers/gpu/drm/virtio/virtgpu_kms.c | 25 +++++++++++++++++++++++++
 2 files changed, 27 insertions(+)
drivers/gpu/drm/virtio/virtgpu_drv.h +2 -0
index 55f836378237..88a94e974088 100644
@@ -278,6 +278,8 @@ struct virtio_gpu_device {
 	uint64_t capset_id_mask;
 	struct list_head cap_cache;
 
+	struct notifier_block pm_nb;
+
 	/* protects uuid state when exporting */
 	spinlock_t resource_export_lock;
 	/* protects map state and host_visible_mm */
drivers/gpu/drm/virtio/virtgpu_kms.c +24 -0
index 08f8e71a7072..e1b2cee30fa3 100644
@@ -26,9 +26,12 @@
 #include <linux/virtio.h>
 #include <linux/virtio_config.h>
 #include <linux/virtio_ring.h>
+#include <linux/suspend.h>
+#include <linux/pm_runtime.h>
 
 #include <drm/drm_file.h>
 #include <drm/drm_managed.h>
+#include <drm/drm_atomic_helper.h>
 
 #include "virtgpu_drv.h"
 
@@ -133,6 +136,21 @@ int virtio_gpu_find_vqs(struct virtio_gpu_device *vgdev)
 	return 0;
 }
 
+static int virtio_gpu_pm_notifier(struct notifier_block *nb, unsigned long mode,
+				  void *data)
+{
+	struct virtio_gpu_device *vgdev = container_of(nb, struct virtio_gpu_device, pm_nb);
+	int ret;
+
+	if (mode == PM_POST_HIBERNATION) {
+		ret = virtio_gpu_object_restore_all(vgdev);
+		if (ret)
+			DRM_ERROR("Failed to resubmit virtio-gpu objects\n");
+	}
+
+	return NOTIFY_DONE;
+}
+
 int virtio_gpu_init(struct virtio_device *vdev, struct drm_device *dev)
 {
 	struct virtio_gpu_device *vgdev;
@@ -268,6 +286,12 @@ int virtio_gpu_init(struct virtio_device *vdev, struct drm_device *dev)
 		wait_event_timeout(vgdev->resp_wq, !vgdev->display_info_pending,
 				   5 * HZ);
 	}
+
+	vgdev->pm_nb.notifier_call = virtio_gpu_pm_notifier;
+	ret = register_pm_notifier(&vgdev->pm_nb);
+	if (ret)
+		goto err_scanouts;
+
 	return 0;
 
 err_scanouts:

drivers/gpu/drm/virtio/virtgpu_kms.c void virtio_gpu_deinit(struct drm_device *dev) · line 329

Quoted 7 lines
drivers/gpu/drm/virtio/virtgpu_kms.c +1 -0
@@ -299,6 +323,7 @@ void virtio_gpu_deinit(struct drm_device *dev)
 	flush_work(&vgdev->config_changed_work);
 	virtio_reset_device(vgdev->vdev);
 	vgdev->vdev->config->del_vqs(vgdev->vdev);
+	unregister_pm_notifier(&vgdev->pm_nb);
 }
 
 void virtio_gpu_release(struct drm_device *dev)
Signature · 2 lines
Best regards,
Dmitry
maintainer
#12 lore raw

Re: [PATCH v4 3/3] drm/virtio: Restore Virt-GPU res on hibernation resume via PM notifier

In reply to [email protected]
Quoted 5 lines · On 9/24/25 22:47, [email protected] wrote:
drivers/gpu/drm/virtio/virtgpu_kms.c +0 -0
index 08f8e71a7072..e1b2cee30fa3 100644

drivers/gpu/drm/virtio/virtgpu_kms.c line 34

Quoted 9 lines
drivers/gpu/drm/virtio/virtgpu_kms.c +3 -0
@@ -26,9 +26,12 @@
 #include <linux/virtio.h>
 #include <linux/virtio_config.h>
 #include <linux/virtio_ring.h>
+#include <linux/suspend.h>
+#include <linux/pm_runtime.h>
 
 #include <drm/drm_file.h>
 #include <drm/drm_managed.h>
+#include <drm/drm_atomic_helper.h>

There is no need to include drm_atomic_helper.h, AFAICT. Please correct this in v5 in addition to addressing comments from Nirmoy Das.

Signature · 2 lines
Best regards,
Dmitry
#13 lore raw

RE: [PATCH v4 3/3] drm/virtio: Restore Virt-GPU res on hibernation resume via PM notifier

Hi Dmitry,

Quoted 16 lines

Subject: Re: [PATCH v4 3/3] drm/virtio: Restore Virt-GPU res on hibernation resume via PM notifier

Quoted 12 lines · On 9/24/25 22:47, [email protected] wrote:
drivers/gpu/drm/virtio/virtgpu_kms.c +2 -0
index 08f8e71a7072..e1b2cee30fa3 100644
@@ -26,9 +26,12 @@
 #include <linux/virtio.h>
 #include <linux/virtio_config.h>
 #include <linux/virtio_ring.h>
+#include <linux/suspend.h>
+#include <linux/pm_runtime.h>
 
Quoted 6 lines

drivers/gpu/drm/virtio/virtgpu_kms.c line 34

Quoted 3 lines
drivers/gpu/drm/virtio/virtgpu_kms.c +1 -0
 #include <drm/drm_file.h>
 #include <drm/drm_managed.h>
+#include <drm/drm_atomic_helper.h>

There is no need to include drm_atomic_helper.h, AFAICT. Please correct this in v5 in addition to addressing comments from Nirmoy Das.

[Kim, Dongwon] Yes, I will do so in v5. Thanks

Quoted 4 lines

--
Best regards,
Dmitry

#14 lore raw

RE: [PATCH v4 3/3] drm/virtio: Restore Virt-GPU res on hibernation resume via PM notifier

In reply to Dmitry Osipenko

Dmitry,

I got your point. That is actually a good idea I think.
I will move params and list node into struct virtio_gpu_object {} and make other changes for it as well.

Quoted 26 lines

Subject: Re: [PATCH v4 3/3] drm/virtio: Restore Virt-GPU res on hibernation resume via PM notifier

Quoted 6 lines · On 9/24/25 22:47, [email protected] wrote:
drivers/gpu/drm/virtio/virtgpu_kms.c +0 -0
index 08f8e71a7072..e1b2cee30fa3 100644

drivers/gpu/drm/virtio/virtgpu_kms.c line 34

Quoted 9 lines
drivers/gpu/drm/virtio/virtgpu_kms.c +3 -0
@@ -26,9 +26,12 @@
 #include <linux/virtio.h>
 #include <linux/virtio_config.h>
 #include <linux/virtio_ring.h>
+#include <linux/suspend.h>
+#include <linux/pm_runtime.h>
 
 #include <drm/drm_file.h>
 #include <drm/drm_managed.h>
+#include <drm/drm_atomic_helper.h>

There is no need to include drm_atomic_helper.h, AFAICT. Please correct this in v5 in addition to addressing comments from Nirmoy Das.

--
Best regards,
Dmitry

maintainer
#15 lore raw
In reply to [email protected]
Quoted 40 lines · On 9/24/25 22:47, [email protected] wrote:

From: Dongwon Kim <[email protected]>

This patch series introduces support for handling hibernation (S4) in the virtio-gpu driver by implementing .freeze and .restore hooks, along with a PM notifier to restore GPU resources upon resume.

Patch 1 adds virtgpu_freeze and virtgpu_restore functions, which manage virtio queue teardown before suspend/hibernation and reinitialization during resume.

Patch 2 implements a mechanism to track and restore virtio_gpu_object instances, addressing the issue where QEMU discards resources during virtio-gpu-reset triggered by hibernation.

Patch 3 adds a PM notifier that triggers resubmission of GPU objects to QEMU when the guest resumes from hibernation.

These changes ensure that the virtio-gpu driver can reliably restore virtio-gpu resources after hibernation, preventing resource loss that leads to system's instability.

v2: 10ms sleep is added in virtgpu_freeze to avoid the situation
the driver is locked up during resumption.

v3: Plain 10ms delay (v2) is replaced with wait calls which wait until
the virtio queue is empty.
(Dmitry Osipenko)

v4: New version of patchset only covers S4 case because loss of resources
in S3 case can be avoided by skipping virtio-gpu-reset in QEMU (hw/display/virtio-gpu.c). For this, virtio-gpu-pci device should be attached to PCIE bus AND a PCIE option, 'x-pcie-pm-no-soft-reset' needs to be enabled when launching QEMU
(e.g. -device virtio-gpu-pci,bus=port,x-pcie-pm-no-soft-reset=true)

Dongwon Kim (3):
drm/virtio: Freeze and restore hooks to support suspend and resume drm/virtio: Add support for saving and restoring virtio_gpu_objects drm/virtio: Restore Virt-GPU res on hibernation resume via PM notifier

Quoted 6 lines
 drivers/gpu/drm/virtio/virtgpu_drv.c    | 60 ++++++++++++++++++++-
 drivers/gpu/drm/virtio/virtgpu_drv.h    | 13 +++++
 drivers/gpu/drm/virtio/virtgpu_kms.c    | 49 ++++++++++++++---
 drivers/gpu/drm/virtio/virtgpu_object.c | 71 +++++++++++++++++++++++++
 4 files changed, 186 insertions(+), 7 deletions(-)

Hi,

Hibernation works well for me with v4, going to apply patches soon.

Re x-pcie-pm-no-soft-reset=true, apparently it's not enough to have it for S3. I'm still seeing virtio-gpu being reset with S3 and this reset comes from a guest kernel writing PCI reg, resetting PCI device on kernel resume. Will look further into it.

Signature · 2 lines
Best regards,
Dmitry
#16 lore raw

Hi Dmitry,

Quoted 53 lines

Subject: Re: [PATCH v4 0/3] Virtio-GPU S4 support

Quoted 42 lines · On 9/24/25 22:47, [email protected] wrote:

From: Dongwon Kim <[email protected]>

This patch series introduces support for handling hibernation (S4) in the virtio-gpu driver by implementing .freeze and .restore hooks, along with a PM notifier to restore GPU resources upon resume.

Patch 1 adds virtgpu_freeze and virtgpu_restore functions, which manage virtio queue teardown before suspend/hibernation and reinitialization during resume.

Patch 2 implements a mechanism to track and restore virtio_gpu_object instances, addressing the issue where QEMU discards resources during virtio-gpu-reset triggered by hibernation.

Patch 3 adds a PM notifier that triggers resubmission of GPU objects to QEMU when the guest resumes from hibernation.

These changes ensure that the virtio-gpu driver can reliably restore virtio-gpu resources after hibernation, preventing resource loss that leads to system's instability.

v2: 10ms sleep is added in virtgpu_freeze to avoid the situation
the driver is locked up during resumption.

v3: Plain 10ms delay (v2) is replaced with wait calls which wait until
the virtio queue is empty.
(Dmitry Osipenko)

v4: New version of patchset only covers S4 case because loss of resources
in S3 case can be avoided by skipping virtio-gpu-reset in QEMU (hw/display/virtio-gpu.c). For this, virtio-gpu-pci device should be attached to PCIE bus AND a PCIE option, 'x-pcie-pm-no-soft-reset' needs to be enabled when launching QEMU
(e.g. -device
virtio-gpu-pci,bus=port,x-pcie-pm-no-soft-reset=true)

Dongwon Kim (3):
drm/virtio: Freeze and restore hooks to support suspend and resume drm/virtio: Add support for saving and restoring virtio_gpu_objects drm/virtio: Restore Virt-GPU res on hibernation resume via PM
notifier

Quoted 7 lines
 drivers/gpu/drm/virtio/virtgpu_drv.c    | 60 ++++++++++++++++++++-
 drivers/gpu/drm/virtio/virtgpu_drv.h    | 13 +++++
 drivers/gpu/drm/virtio/virtgpu_kms.c    | 49 ++++++++++++++---
 drivers/gpu/drm/virtio/virtgpu_object.c | 71
+++++++++++++++++++++++++
 4 files changed, 186 insertions(+), 7 deletions(-)
Quoted 8 lines

Hi,

Hibernation works well for me with v4, going to apply patches soon.

Re x-pcie-pm-no-soft-reset=true, apparently it's not enough to have it for S3. I'm still seeing virtio-gpu being reset with S3 and this reset comes from a guest kernel writing PCI reg, resetting PCI device on kernel resume. Will look further into it.

[Kim, Dongwon] That option only works when you set up virtio-gpu as a PCIE device. I am wondering if you set up your PCIE bus. This are params I added for it. -device pcie-root-port,id=root_port1,bus=pcie.0 \
-device virtio-vga,bus=root_port1,x-pcie-pm-no-soft-reset=true \

Quoted 4 lines

--
Best regards,
Dmitry

#17 lore raw

Dmitry,

I had an assumption that you are using -machine q35 already but If not, you will need this too for enabling PCIE bus.

Quoted 83 lines

-----Original Message-----
From: Kim, Dongwon
Sent: Tuesday, September 30, 2025 8:09 PM
To: 'Dmitry Osipenko' <[email protected]>; dri- [email protected]
Subject: RE: [PATCH v4 0/3] Virtio-GPU S4 support

Hi Dmitry,

Quoted 35 lines

Subject: Re: [PATCH v4 0/3] Virtio-GPU S4 support

Quoted 32 lines · On 9/24/25 22:47, [email protected] wrote:

From: Dongwon Kim <[email protected]>

This patch series introduces support for handling hibernation (S4) in the virtio-gpu driver by implementing .freeze and .restore hooks, along with a PM notifier to restore GPU resources upon resume.

Patch 1 adds virtgpu_freeze and virtgpu_restore functions, which manage virtio queue teardown before suspend/hibernation and reinitialization during resume.

Patch 2 implements a mechanism to track and restore virtio_gpu_object instances, addressing the issue where QEMU discards resources during virtio-gpu-reset triggered by hibernation.

Patch 3 adds a PM notifier that triggers resubmission of GPU objects to QEMU when the guest resumes from hibernation.

These changes ensure that the virtio-gpu driver can reliably restore virtio-gpu resources after hibernation, preventing resource loss that leads to system's instability.

v2: 10ms sleep is added in virtgpu_freeze to avoid the situation
the driver is locked up during resumption.

v3: Plain 10ms delay (v2) is replaced with wait calls which wait until
the virtio queue is empty.
(Dmitry Osipenko)

v4: New version of patchset only covers S4 case because loss of resources
in S3 case can be avoided by skipping virtio-gpu-reset in QEMU (hw/display/virtio-gpu.c). For this, virtio-gpu-pci device should be attached to PCIE bus AND a PCIE option, 'x-pcie-pm-no-soft-reset'

needs

Quoted 18 lines
Quoted 10 lines

to be enabled when launching QEMU
(e.g. -device
virtio-gpu-pci,bus=port,x-pcie-pm-no-soft-reset=true)

Dongwon Kim (3):
drm/virtio: Freeze and restore hooks to support suspend and resume drm/virtio: Add support for saving and restoring virtio_gpu_objects drm/virtio: Restore Virt-GPU res on hibernation resume via PM
notifier

Quoted 7 lines
 drivers/gpu/drm/virtio/virtgpu_drv.c    | 60 ++++++++++++++++++++-
 drivers/gpu/drm/virtio/virtgpu_drv.h    | 13 +++++
 drivers/gpu/drm/virtio/virtgpu_kms.c    | 49 ++++++++++++++---
 drivers/gpu/drm/virtio/virtgpu_object.c | 71
+++++++++++++++++++++++++
 4 files changed, 186 insertions(+), 7 deletions(-)
Quoted 5 lines

Hi,

Hibernation works well for me with v4, going to apply patches soon.

Re x-pcie-pm-no-soft-reset=true, apparently it's not enough to have it for

S3.

Quoted 3 lines

I'm still seeing virtio-gpu being reset with S3 and this reset comes from a guest kernel writing PCI reg, resetting PCI device on kernel resume. Will look further into it.

[Kim, Dongwon] That option only works when you set up virtio-gpu as a PCIE device.
I am wondering if you set up your PCIE bus. This are params I added for it. -device pcie-root-port,id=root_port1,bus=pcie.0 \ -device virtio- vga,bus=root_port1,x-pcie-pm-no-soft-reset=true \

Quoted 4 lines

--
Best regards,
Dmitry

maintainer
#18 lore raw
Quoted 83 lines · Dongwon

Dmitry,

I had an assumption that you are using -machine q35 already but If not, you will need this too for enabling PCIE bus.

Quoted 78 lines

-----Original Message-----
From: Kim, Dongwon
Sent: Tuesday, September 30, 2025 8:09 PM
To: 'Dmitry Osipenko' <[email protected]>; dri- [email protected]
Subject: RE: [PATCH v4 0/3] Virtio-GPU S4 support

Hi Dmitry,

Quoted 35 lines

Subject: Re: [PATCH v4 0/3] Virtio-GPU S4 support

Quoted 32 lines · On 9/24/25 22:47, [email protected] wrote:

From: Dongwon Kim <[email protected]>

This patch series introduces support for handling hibernation (S4) in the virtio-gpu driver by implementing .freeze and .restore hooks, along with a PM notifier to restore GPU resources upon resume.

Patch 1 adds virtgpu_freeze and virtgpu_restore functions, which manage virtio queue teardown before suspend/hibernation and reinitialization during resume.

Patch 2 implements a mechanism to track and restore virtio_gpu_object instances, addressing the issue where QEMU discards resources during virtio-gpu-reset triggered by hibernation.

Patch 3 adds a PM notifier that triggers resubmission of GPU objects to QEMU when the guest resumes from hibernation.

These changes ensure that the virtio-gpu driver can reliably restore virtio-gpu resources after hibernation, preventing resource loss that leads to system's instability.

v2: 10ms sleep is added in virtgpu_freeze to avoid the situation
the driver is locked up during resumption.

v3: Plain 10ms delay (v2) is replaced with wait calls which wait until
the virtio queue is empty.
(Dmitry Osipenko)

v4: New version of patchset only covers S4 case because loss of resources
in S3 case can be avoided by skipping virtio-gpu-reset in QEMU (hw/display/virtio-gpu.c). For this, virtio-gpu-pci device should be attached to PCIE bus AND a PCIE option, 'x-pcie-pm-no-soft-reset'

needs

Quoted 18 lines
Quoted 10 lines

to be enabled when launching QEMU
(e.g. -device
virtio-gpu-pci,bus=port,x-pcie-pm-no-soft-reset=true)

Dongwon Kim (3):
drm/virtio: Freeze and restore hooks to support suspend and resume drm/virtio: Add support for saving and restoring virtio_gpu_objects drm/virtio: Restore Virt-GPU res on hibernation resume via PM
notifier

Quoted 7 lines
 drivers/gpu/drm/virtio/virtgpu_drv.c    | 60 ++++++++++++++++++++-
 drivers/gpu/drm/virtio/virtgpu_drv.h    | 13 +++++
 drivers/gpu/drm/virtio/virtgpu_kms.c    | 49 ++++++++++++++---
 drivers/gpu/drm/virtio/virtgpu_object.c | 71
+++++++++++++++++++++++++
 4 files changed, 186 insertions(+), 7 deletions(-)
Quoted 5 lines

Hi,

Hibernation works well for me with v4, going to apply patches soon.

Re x-pcie-pm-no-soft-reset=true, apparently it's not enough to have it for

S3.

Quoted 3 lines

I'm still seeing virtio-gpu being reset with S3 and this reset comes from a guest kernel writing PCI reg, resetting PCI device on kernel resume. Will look further into it.

[Kim, Dongwon] That option only works when you set up virtio-gpu as a PCIE device.
I am wondering if you set up your PCIE bus. This are params I added for it. -device pcie-root-port,id=root_port1,bus=pcie.0 \ -device virtio- vga,bus=root_port1,x-pcie-pm-no-soft-reset=true \

Indeed, adding PCIE bus makes it work. There is no reset in S3 and virgl continues to work after resume. Thanks a lot for the clarification.

Though, adding hostmem to virtio-vga-gl, needed for venus and native contexts, doesn't work with a PCIE bus - VM hangs on boot. Perhaps more config options needed.

Signature · 2 lines
Best regards,
Dmitry