Skip to contentSkip to conversation

drm/virtio: Fix handling CONFIG_DRM_VIRTIO_GPU_KMS option

PATCH Superseded Dmitry Osipenko 3 participants 4 messages Mar 6, 2023

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Dmitry Osipenko

author maintainer
#0 lore raw

PATCH v2 1/1 drm/virtio: Fix handling CONFIG_DRM_VIRTIO_GPU_KMS option

VirtIO-GPU got a new config option for disabling KMS. There were two problems left unnoticed during review when the new option was added:

1. The IS_ENABLED(CONFIG_DRM_VIRTIO_GPU_KMS) check in the code was inverted, hence KMS was disabled when it should be enabled and vice versa.

2. The disabled KMS crashed kernel with a NULL dereference in drm_kms_helper_hotplug_event(), which shall not be invoked with a disabled KMS.

Fix the inverted config option check in the code and skip handling the VIRTIO_GPU_EVENT_DISPLAY sent by host when KMS is disabled in guest to fix the crash.

Fixes 72122c69d717 ("drm/virtio: Add option to disable KMS support")

---

Changelog:

v2: - Moved the "has_edid" under the "num_scanouts" condition, like was
suggested by Gerd Hoffmann.

 drivers/gpu/drm/virtio/virtgpu_kms.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
drivers/gpu/drm/virtio/virtgpu_kms.c +8 -6
index 874ad6c2621a..15f2519988e7 100644
@@ -43,11 +43,13 @@ static void virtio_gpu_config_changed_work_func(struct work_struct *work)
 	virtio_cread_le(vgdev->vdev, struct virtio_gpu_config,
 			events_read, &events_read);
 	if (events_read & VIRTIO_GPU_EVENT_DISPLAY) {
-		if (vgdev->has_edid)
-			virtio_gpu_cmd_get_edids(vgdev);
-		virtio_gpu_cmd_get_display_info(vgdev);
-		virtio_gpu_notify(vgdev);
-		drm_helper_hpd_irq_event(vgdev->ddev);
+		if (vgdev->num_scanouts) {
+			if (vgdev->has_edid)
+				virtio_gpu_cmd_get_edids(vgdev);
+			virtio_gpu_cmd_get_display_info(vgdev);
+			virtio_gpu_notify(vgdev);
+			drm_helper_hpd_irq_event(vgdev->ddev);
+		}
 		events_clear |= VIRTIO_GPU_EVENT_DISPLAY;
 	}
 	virtio_cwrite_le(vgdev->vdev, struct virtio_gpu_config,
@@ -224,7 +226,7 @@ int virtio_gpu_init(struct virtio_device *vdev, struct drm_device *dev)
 	vgdev->num_scanouts = min_t(uint32_t, num_scanouts,
 				    VIRTIO_GPU_MAX_SCANOUTS);
 
-	if (IS_ENABLED(CONFIG_DRM_VIRTIO_GPU_KMS) || !vgdev->num_scanouts) {
+	if (!IS_ENABLED(CONFIG_DRM_VIRTIO_GPU_KMS) || !vgdev->num_scanouts) {
 		DRM_INFO("KMS disabled\n");
 		vgdev->num_scanouts = 0;
 		vgdev->has_edid = false;
Signature · 1 line
2.39.2
maintainer Acked-by on v2 1/1
#1 lore raw
Quoted 14 lines · Dmitry Osipenko

VirtIO-GPU got a new config option for disabling KMS. There were two problems left unnoticed during review when the new option was added:

1. The IS_ENABLED(CONFIG_DRM_VIRTIO_GPU_KMS) check in the code was inverted, hence KMS was disabled when it should be enabled and vice versa.

2. The disabled KMS crashed kernel with a NULL dereference in drm_kms_helper_hotplug_event(), which shall not be invoked with a disabled KMS.

Fix the inverted config option check in the code and skip handling the VIRTIO_GPU_EVENT_DISPLAY sent by host when KMS is disabled in guest to fix the crash.

Quoted 8 lines
Fixes 72122c69d717 ("drm/virtio: Add option to disable KMS support")

---

Changelog:

v2: - Moved the "has_edid" under the "num_scanouts" condition, like was
suggested by Gerd Hoffmann.

#2 lore raw
In reply to Dmitry Osipenko
Quoted 14 lines · Dmitry Osipenko

VirtIO-GPU got a new config option for disabling KMS. There were two problems left unnoticed during review when the new option was added:

1. The IS_ENABLED(CONFIG_DRM_VIRTIO_GPU_KMS) check in the code was inverted, hence KMS was disabled when it should be enabled and vice versa.

2. The disabled KMS crashed kernel with a NULL dereference in drm_kms_helper_hotplug_event(), which shall not be invoked with a disabled KMS.

Fix the inverted config option check in the code and skip handling the VIRTIO_GPU_EVENT_DISPLAY sent by host when KMS is disabled in guest to fix the crash.

Quoted 9 lines
Fixes 72122c69d717 ("drm/virtio: Add option to disable KMS support")

---

Changelog:

v2: - Moved the "has_edid" under the "num_scanouts" condition, like was
suggested by Gerd Hoffmann.

Hi Dmitry, I think there's more than one piece like that in the driver.

Quoted 9 lines
 drivers/gpu/drm/virtio/virtgpu_kms.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
drivers/gpu/drm/virtio/virtgpu_kms.c +0 -0
index 874ad6c2621a..15f2519988e7 100644
@@ -43,11 +43,13 @@ static void virtio_gpu_config_changed_work_func(struct work_struct *work)
 	virtio_cread_le(vgdev->vdev, struct virtio_gpu_config,

drivers/gpu/drm/virtio/virtgpu_kms.c static void virtio_gpu_config_changed_work_func(struct work_struct *work) · line 48

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_kms.c +3 -5
 			events_read, &events_read);
 	if (events_read & VIRTIO_GPU_EVENT_DISPLAY) {
-		if (vgdev->has_edid)
-			virtio_gpu_cmd_get_edids(vgdev);
-		virtio_gpu_cmd_get_display_info(vgdev);
-		virtio_gpu_notify(vgdev);
-		drm_helper_hpd_irq_event(vgdev->ddev);
+		if (vgdev->num_scanouts) {
+			if (vgdev->has_edid)
+				virtio_gpu_cmd_get_edids(vgdev);

Worth doing the same thing in virtio_gpu_init()? Aka move the has_edid && get_edids within the num_scanouts if block.

HTH
Emil

author maintainer
#3 lore raw
Quoted 41 lines · Emil Velikov
Quoted 14 lines · Dmitry Osipenko

VirtIO-GPU got a new config option for disabling KMS. There were two problems left unnoticed during review when the new option was added:

1. The IS_ENABLED(CONFIG_DRM_VIRTIO_GPU_KMS) check in the code was inverted, hence KMS was disabled when it should be enabled and vice versa.

2. The disabled KMS crashed kernel with a NULL dereference in drm_kms_helper_hotplug_event(), which shall not be invoked with a disabled KMS.

Fix the inverted config option check in the code and skip handling the VIRTIO_GPU_EVENT_DISPLAY sent by host when KMS is disabled in guest to fix the crash.

Quoted 9 lines
Fixes 72122c69d717 ("drm/virtio: Add option to disable KMS support")

---

Changelog:

v2: - Moved the "has_edid" under the "num_scanouts" condition, like was
suggested by Gerd Hoffmann.

Hi Dmitry, I think there's more than one piece like that in the driver.

Quoted 14 lines
 drivers/gpu/drm/virtio/virtgpu_kms.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
drivers/gpu/drm/virtio/virtgpu_kms.c +0 -3
index 874ad6c2621a..15f2519988e7 100644
@@ -43,11 +43,13 @@ static void virtio_gpu_config_changed_work_func(struct work_struct *work)
 	virtio_cread_le(vgdev->vdev, struct virtio_gpu_config,
 			events_read, &events_read);
 	if (events_read & VIRTIO_GPU_EVENT_DISPLAY) {
-		if (vgdev->has_edid)
-			virtio_gpu_cmd_get_edids(vgdev);
-		virtio_gpu_cmd_get_display_info(vgdev);
Quoted 8 lines

drivers/gpu/drm/virtio/virtgpu_kms.c static void virtio_gpu_config_changed_work_func(struct work_struct *work) · line 48

Quoted 5 lines
drivers/gpu/drm/virtio/virtgpu_kms.c +3 -2
-		virtio_gpu_notify(vgdev);
-		drm_helper_hpd_irq_event(vgdev->ddev);
+		if (vgdev->num_scanouts) {
+			if (vgdev->has_edid)
+				virtio_gpu_cmd_get_edids(vgdev);

Worth doing the same thing in virtio_gpu_init()? Aka move the has_edid && get_edids within the num_scanouts if block.

Good catch, that could be done for consistency.

Signature · 2 lines
Best regards,
Dmitry