Skip to contentSkip to conversation

drm/virtgpu: Use vblank timer

PATCH Applied Thomas Zimmermann 4 participants 16 messages Oct 8, 2025 – Oct 16, 2025

Subsystems (4)
lore
Share & tools
lore

Thomas Zimmermann

author maintainer
#0 lore raw

PATCH 1/1 drm/virtgpu: Use vblank timer

Use a vblank timer to simulate the vblank interrupt. The DRM vblank helpers provide an implementation on top of Linux' hrtimer. Virtgpu enables and disables the timer as part of the CRTC. The atomic_flush callback sets up the event. Like vblank interrupts, the vblank timer fires at the rate of the display refresh.

Most userspace limits its page flip rate according to the DRM vblank event. Virtgpu's virtual hardware does not provide vblank interrupts, so DRM sends each event ASAP. With the fast access times of virtual display memory, the event rate is much higher than the display mode's refresh rate; creating the next page flip almost immediately. This leads to excessive CPU overhead from even small display updates, such as moving the mouse pointer.

This problem affects virtgpu and all other virtual displays. See [1] for a discussion in the context of hypervdrm.

---
 drivers/gpu/drm/virtio/virtgpu_display.c | 29 ++++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)
drivers/gpu/drm/virtio/virtgpu_display.c +27 -2
index c3315935d8bc..ee134e46eeba 100644
@@ -32,6 +32,8 @@
 #include <drm/drm_gem_framebuffer_helper.h>
 #include <drm/drm_probe_helper.h>
 #include <drm/drm_simple_kms_helper.h>
+#include <drm/drm_vblank.h>
+#include <drm/drm_vblank_helper.h>
 
 #include "virtgpu_drv.h"
 
@@ -55,6 +57,7 @@ static const struct drm_crtc_funcs virtio_gpu_crtc_funcs = {
 	.reset                  = drm_atomic_helper_crtc_reset,
 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
 	.atomic_destroy_state   = drm_atomic_helper_crtc_destroy_state,
+	DRM_CRTC_VBLANK_TIMER_FUNCS,
 };
 
 static const struct drm_framebuffer_funcs virtio_gpu_fb_funcs = {
@@ -99,6 +102,7 @@ static void virtio_gpu_crtc_mode_set_nofb(struct drm_crtc *crtc)
 static void virtio_gpu_crtc_atomic_enable(struct drm_crtc *crtc,
 					  struct drm_atomic_state *state)
 {
+	drm_crtc_vblank_on(crtc);
 }
 
 static void virtio_gpu_crtc_atomic_disable(struct drm_crtc *crtc,
@@ -108,6 +112,8 @@ static void virtio_gpu_crtc_atomic_disable(struct drm_crtc *crtc,
 	struct virtio_gpu_device *vgdev = dev->dev_private;
 	struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
 
+	drm_crtc_vblank_off(crtc);
+
 	virtio_gpu_cmd_set_scanout(vgdev, output->index, 0, 0, 0, 0, 0);
 	virtio_gpu_notify(vgdev);
 }
@@ -121,9 +127,10 @@ static int virtio_gpu_crtc_atomic_check(struct drm_crtc *crtc,
 static void virtio_gpu_crtc_atomic_flush(struct drm_crtc *crtc,
 					 struct drm_atomic_state *state)
 {
-	struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
-									  crtc);
+	struct drm_device *dev = crtc->dev;
+	struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
 	struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
+	struct drm_pending_vblank_event *event;
 
 	/*
 	 * virtio-gpu can't do modeset and plane update operations
@@ -133,6 +140,20 @@ static void virtio_gpu_crtc_atomic_flush(struct drm_crtc *crtc,
 	 */
 	if (drm_atomic_crtc_needs_modeset(crtc_state))
 		output->needs_modeset = true;
+
+	spin_lock_irq(&dev->event_lock);
+
+	event = crtc_state->event;
+	crtc_state->event = NULL;
+
+	if (event) {
+		if (drm_crtc_vblank_get(crtc) == 0)
+			drm_crtc_arm_vblank_event(crtc, event);
+		else
+			drm_crtc_send_vblank_event(crtc, event);
+	}
+
+	spin_unlock_irq(&dev->event_lock);
 }
 
 static const struct drm_crtc_helper_funcs virtio_gpu_crtc_helper_funcs = {
@@ -356,6 +377,10 @@ int virtio_gpu_modeset_init(struct virtio_gpu_device *vgdev)
 	for (i = 0 ; i < vgdev->num_scanouts; ++i)
 		vgdev_output_init(vgdev, i);
 
+	ret = drm_vblank_init(vgdev->ddev, vgdev->num_scanouts);
+	if (ret)
+		return ret;
+
 	drm_mode_config_reset(vgdev->ddev);
 	return 0;
 }
Signature · 1 line
2.51.0
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 14 lines · Thomas Zimmermann

Use a vblank timer to simulate the vblank interrupt. The DRM vblank helpers provide an implementation on top of Linux' hrtimer. Virtgpu enables and disables the timer as part of the CRTC. The atomic_flush callback sets up the event. Like vblank interrupts, the vblank timer fires at the rate of the display refresh.

Most userspace limits its page flip rate according to the DRM vblank event. Virtgpu's virtual hardware does not provide vblank interrupts, so DRM sends each event ASAP. With the fast access times of virtual display memory, the event rate is much higher than the display mode's refresh rate; creating the next page flip almost immediately. This leads to excessive CPU overhead from even small display updates, such as moving the mouse pointer.

Quoted 5 lines

This problem affects virtgpu and all other virtual displays. See [1] for a discussion in the context of hypervdrm.

maintainer Reviewed-by Tested-by on v1 1/1
#2 lore raw
In reply to Thomas Zimmermann
Quoted 14 lines · Thomas Zimmermann

Use a vblank timer to simulate the vblank interrupt. The DRM vblank helpers provide an implementation on top of Linux' hrtimer. Virtgpu enables and disables the timer as part of the CRTC. The atomic_flush callback sets up the event. Like vblank interrupts, the vblank timer fires at the rate of the display refresh.

Most userspace limits its page flip rate according to the DRM vblank event. Virtgpu's virtual hardware does not provide vblank interrupts, so DRM sends each event ASAP. With the fast access times of virtual display memory, the event rate is much higher than the display mode's refresh rate; creating the next page flip almost immediately. This leads to excessive CPU overhead from even small display updates, such as moving the mouse pointer.

Quoted 8 lines

This problem affects virtgpu and all other virtual displays. See [1] for a discussion in the context of hypervdrm.

---
 drivers/gpu/drm/virtio/virtgpu_display.c | 29 ++++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)

This makes KDE Plasma boot splash animation play at a normal 60fps pace with virtio-gpu and virgl instead of 1000fps. Thanks for fixing this. Didn't expect vblank wasn't working properly. Will apply soon.

Signature · 2 lines
Best regards,
Dmitry
maintainer Applied
#3 lore raw
In reply to Thomas Zimmermann
Quoted 14 lines · Thomas Zimmermann

Use a vblank timer to simulate the vblank interrupt. The DRM vblank helpers provide an implementation on top of Linux' hrtimer. Virtgpu enables and disables the timer as part of the CRTC. The atomic_flush callback sets up the event. Like vblank interrupts, the vblank timer fires at the rate of the display refresh.

Most userspace limits its page flip rate according to the DRM vblank event. Virtgpu's virtual hardware does not provide vblank interrupts, so DRM sends each event ASAP. With the fast access times of virtual display memory, the event rate is much higher than the display mode's refresh rate; creating the next page flip almost immediately. This leads to excessive CPU overhead from even small display updates, such as moving the mouse pointer.

Quoted 8 lines

This problem affects virtgpu and all other virtual displays. See [1] for a discussion in the context of hypervdrm.

---
 drivers/gpu/drm/virtio/virtgpu_display.c | 29 ++++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)

Applied to misc-next, thanks.

Signature · 2 lines
Best regards,
Dmitry
#4 lore raw
In reply to Thomas Zimmermann

Hi Thomas,

Quoted 16 lines

Subject: [PATCH] drm/virtgpu: Use vblank timer

Use a vblank timer to simulate the vblank interrupt. The DRM vblank helpers provide an implementation on top of Linux' hrtimer. Virtgpu enables and disables the timer as part of the CRTC. The atomic_flush callback sets up the event. Like vblank interrupts, the vblank timer fires at the rate of the display refresh.

Most userspace limits its page flip rate according to the DRM vblank event. Virtgpu's virtual hardware does not provide vblank interrupts, so DRM sends each event ASAP. With the fast access times of virtual display memory, the event rate is much higher than the display mode's refresh rate; creating the next page flip almost immediately. This leads to excessive CPU overhead from even small display updates, such as moving the mouse pointer.

Quoted 2 lines

This problem affects virtgpu and all other virtual displays. See [1] for a discussion in the context of hypervdrm.

When running Virtio-gpu with some of the UIs (gtk, spice, etc) in Qemu, the Guest display updates are synchronized with a timer (or repaint callback in the case of Gtk) the UI layer provides particularly when blob=true. In other words, the Guest's atomic commit does not complete until the Host signals (via a fence) that the update (or flush) is done.

This is because when blob=true, the Guest's FB is accessed by the Host without making any copies. So, to avoid tearing, the Guest needs to be prevented from accessing its FB until the Host is done. Therefore, I think for virtio-gpu, the virtual vblank timer can only be helpful when blob=false as this is the only case where Guest's display updates are unbounded (and Host makes a copy of the Guest's FB).

I am not sure how this would affect virgl use-cases but Dmitry can help explain if the vblank timer would be useful there or not.

Thanks,
Vivek

Quoted 106 lines

devel/[email protected] amprd02.prod.outlook.com/ # [1]

---
 drivers/gpu/drm/virtio/virtgpu_display.c | 29 ++++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)
drivers/gpu/drm/virtio/virtgpu_display.c +3 -0
index c3315935d8bc..ee134e46eeba 100644
@@ -32,6 +32,8 @@
 #include <drm/drm_gem_framebuffer_helper.h>
 #include <drm/drm_probe_helper.h>
 #include <drm/drm_simple_kms_helper.h>
+#include <drm/drm_vblank.h>
+#include <drm/drm_vblank_helper.h>
 
 #include "virtgpu_drv.h"
 
@@ -55,6 +57,7 @@ static const struct drm_crtc_funcs virtio_gpu_crtc_funcs
 = {
 	.reset                  = drm_atomic_helper_crtc_reset,
 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
 	.atomic_destroy_state   = drm_atomic_helper_crtc_destroy_state,
+	DRM_CRTC_VBLANK_TIMER_FUNCS,
 };
 

static const struct drm_framebuffer_funcs virtio_gpu_fb_funcs = {

drivers/gpu/drm/virtio/virtgpu_display.c +1 -0
@@ -99,6 +102,7 @@ static void virtio_gpu_crtc_mode_set_nofb(struct
 drm_crtc *crtc)
 static void virtio_gpu_crtc_atomic_enable(struct drm_crtc *crtc,
 					  struct drm_atomic_state *state)
 {
+	drm_crtc_vblank_on(crtc);
 }
 

static void virtio_gpu_crtc_atomic_disable(struct drm_crtc *crtc,

drivers/gpu/drm/virtio/virtgpu_display.c +2 -0
@@ -108,6 +112,8 @@ static void virtio_gpu_crtc_atomic_disable(struct
 drm_crtc *crtc,
 	struct virtio_gpu_device *vgdev = dev->dev_private;
 	struct virtio_gpu_output *output =
 drm_crtc_to_virtio_gpu_output(crtc);
 
+	drm_crtc_vblank_off(crtc);
+
 	virtio_gpu_cmd_set_scanout(vgdev, output->index, 0, 0, 0, 0, 0);
drivers/gpu/drm/virtio/virtgpu_display.c +2 -2
 	virtio_gpu_notify(vgdev);
 }
@@ -121,9 +127,10 @@ static int virtio_gpu_crtc_atomic_check(struct
 drm_crtc *crtc,
 static void virtio_gpu_crtc_atomic_flush(struct drm_crtc *crtc,
 					 struct drm_atomic_state *state)
 {
-	struct drm_crtc_state *crtc_state =
 drm_atomic_get_new_crtc_state(state,
-									  crtc);
+	struct drm_device *dev = crtc->dev;
+	struct drm_crtc_state *crtc_state =
 drm_atomic_get_new_crtc_state(state, crtc);
 	struct virtio_gpu_output *output =

drm_crtc_to_virtio_gpu_output(crtc);

drivers/gpu/drm/virtio/virtgpu_display.c +15 -0
+	struct drm_pending_vblank_event *event;
 
 	/*
 	 * virtio-gpu can't do modeset and plane update operations
@@ -133,6 +140,20 @@ static void virtio_gpu_crtc_atomic_flush(struct
 drm_crtc *crtc,
 	 */
 	if (drm_atomic_crtc_needs_modeset(crtc_state))
 		output->needs_modeset = true;
+
+	spin_lock_irq(&dev->event_lock);
+
+	event = crtc_state->event;
+	crtc_state->event = NULL;
+
+	if (event) {
+		if (drm_crtc_vblank_get(crtc) == 0)
+			drm_crtc_arm_vblank_event(crtc, event);
+		else
+			drm_crtc_send_vblank_event(crtc, event);
+	}
+
+	spin_unlock_irq(&dev->event_lock);
 }
 

static const struct drm_crtc_helper_funcs virtio_gpu_crtc_helper_funcs = {

drivers/gpu/drm/virtio/virtgpu_display.c +4 -0
@@ -356,6 +377,10 @@ int virtio_gpu_modeset_init(struct virtio_gpu_device
 *vgdev)
 	for (i = 0 ; i < vgdev->num_scanouts; ++i)
 		vgdev_output_init(vgdev, i);
 
+	ret = drm_vblank_init(vgdev->ddev, vgdev->num_scanouts);
+	if (ret)
+		return ret;
+
 	drm_mode_config_reset(vgdev->ddev);
 	return 0;

}
--
2.51.0

maintainer
#5 lore raw
Quoted 26 lines · Vivek

Hi Thomas,

Quoted 16 lines

Subject: [PATCH] drm/virtgpu: Use vblank timer

Use a vblank timer to simulate the vblank interrupt. The DRM vblank helpers provide an implementation on top of Linux' hrtimer. Virtgpu enables and disables the timer as part of the CRTC. The atomic_flush callback sets up the event. Like vblank interrupts, the vblank timer fires at the rate of the display refresh.

Most userspace limits its page flip rate according to the DRM vblank event. Virtgpu's virtual hardware does not provide vblank interrupts, so DRM sends each event ASAP. With the fast access times of virtual display memory, the event rate is much higher than the display mode's refresh rate; creating the next page flip almost immediately. This leads to excessive CPU overhead from even small display updates, such as moving the mouse pointer.

Quoted 2 lines

This problem affects virtgpu and all other virtual displays. See [1] for a discussion in the context of hypervdrm.

When running Virtio-gpu with some of the UIs (gtk, spice, etc) in Qemu, the Guest display updates are synchronized with a timer (or repaint callback in the case of Gtk) the UI layer provides particularly when blob=true. In other words, the Guest's atomic commit does not complete until the Host signals (via a fence) that the update (or flush) is done.

Quoted 8 lines

This is because when blob=true, the Guest's FB is accessed by the Host without making any copies. So, to avoid tearing, the Guest needs to be prevented from accessing its FB until the Host is done. Therefore, I think for virtio-gpu, the virtual vblank timer can only be helpful when blob=false as this is the only case where Guest's display updates are unbounded (and Host makes a copy of the Guest's FB).

I am not sure how this would affect virgl use-cases but Dmitry can help explain if the vblank timer would be useful there or not.

The vblank timer should only limit framerate of virtio-gpu, I don't expect it will cause new problems. Do you see tearing using this patch?

Vblank timer makes a big performance improvement for virtio-gpu KMS, especially for a native context on QEMU. No tearing spotted with it.

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

Hi Dmitry,

Quoted 41 lines

Subject: Re: [PATCH] drm/virtgpu: Use vblank timer

Quoted 18 lines · Vivek

Hi Thomas,

Quoted 16 lines

Subject: [PATCH] drm/virtgpu: Use vblank timer

Use a vblank timer to simulate the vblank interrupt. The DRM vblank helpers provide an implementation on top of Linux' hrtimer. Virtgpu enables and disables the timer as part of the CRTC. The atomic_flush callback sets up the event. Like vblank interrupts, the vblank timer fires at the rate of the display refresh.

Most userspace limits its page flip rate according to the DRM vblank event. Virtgpu's virtual hardware does not provide vblank interrupts, so DRM sends each event ASAP. With the fast access times of virtual display memory, the event rate is much higher than the display mode's refresh rate; creating the next page flip almost immediately. This leads to excessive CPU overhead from even small display updates, such as moving the mouse pointer.

Quoted 3 lines
Quoted 2 lines

This problem affects virtgpu and all other virtual displays. See [1] for a discussion in the context of hypervdrm.

When running Virtio-gpu with some of the UIs (gtk, spice, etc) in Qemu,

the

Quoted 1 line

Guest display updates are synchronized with a timer (or repaint callback

in

Quoted 2 lines

the case of Gtk) the UI layer provides particularly when blob=true. In other words, the Guest's atomic commit does not complete until the Host

signals

Quoted 3 lines

(via a fence) that the update (or flush) is done.

This is because when blob=true, the Guest's FB is accessed by the Host

without

Quoted 1 line

making any copies. So, to avoid tearing, the Guest needs to be prevented

from

Quoted 1 line

accessing its FB until the Host is done. Therefore, I think for virtio-gpu, the

virtual

Quoted 1 line

vblank timer can only be helpful when blob=false as this is the only case

where

Quoted 1 line

Guest's display updates are unbounded (and Host makes a copy of the

Quoted 8 lines

Guest's FB).

Quoted 2 lines

I am not sure how this would affect virgl use-cases but Dmitry can help

explain if

Quoted 1 line

the vblank timer would be useful there or not.

The vblank timer should only limit framerate of virtio-gpu, I don't expect it will cause new problems. Do you see tearing using this patch?

AFAICS, having a vblank timer when blob=true would be redundant and will interfere with the synchronization mechanism because the Guest compositor would start a new repaint cycle without knowing if it OK to reuse the FB it had submitted previously. I have not yet tested this patch but I strongly believe it will cause tearing issues particularly when blob=true.

Quoted 3 lines

Vblank timer makes a big performance improvement for virtio-gpu KMS, especially for a native context on QEMU. No tearing spotted with it.

This means that the only remaining case to be tested is blob=true and virgl=false. I'll try to test it soon and let you know.

Thanks,
Vivek

Quoted 4 lines

--
Best regards,
Dmitry

maintainer
#7 lore raw
Quoted 58 lines · Vivek

Hi Dmitry,

Quoted 41 lines

Subject: Re: [PATCH] drm/virtgpu: Use vblank timer

Quoted 18 lines · Vivek

Hi Thomas,

Quoted 16 lines

Subject: [PATCH] drm/virtgpu: Use vblank timer

Use a vblank timer to simulate the vblank interrupt. The DRM vblank helpers provide an implementation on top of Linux' hrtimer. Virtgpu enables and disables the timer as part of the CRTC. The atomic_flush callback sets up the event. Like vblank interrupts, the vblank timer fires at the rate of the display refresh.

Most userspace limits its page flip rate according to the DRM vblank event. Virtgpu's virtual hardware does not provide vblank interrupts, so DRM sends each event ASAP. With the fast access times of virtual display memory, the event rate is much higher than the display mode's refresh rate; creating the next page flip almost immediately. This leads to excessive CPU overhead from even small display updates, such as moving the mouse pointer.

Quoted 3 lines
Quoted 2 lines

This problem affects virtgpu and all other virtual displays. See [1] for a discussion in the context of hypervdrm.

When running Virtio-gpu with some of the UIs (gtk, spice, etc) in Qemu,

the

Quoted 1 line

Guest display updates are synchronized with a timer (or repaint callback

in

Quoted 2 lines

the case of Gtk) the UI layer provides particularly when blob=true. In other words, the Guest's atomic commit does not complete until the Host

signals

Quoted 3 lines

(via a fence) that the update (or flush) is done.

This is because when blob=true, the Guest's FB is accessed by the Host

without

Quoted 1 line

making any copies. So, to avoid tearing, the Guest needs to be prevented

from

Quoted 1 line

accessing its FB until the Host is done. Therefore, I think for virtio-gpu, the

virtual

Quoted 1 line

vblank timer can only be helpful when blob=false as this is the only case

where

Quoted 1 line

Guest's display updates are unbounded (and Host makes a copy of the

Quoted 8 lines

Guest's FB).

Quoted 2 lines

I am not sure how this would affect virgl use-cases but Dmitry can help

explain if

Quoted 1 line

the vblank timer would be useful there or not.

The vblank timer should only limit framerate of virtio-gpu, I don't expect it will cause new problems. Do you see tearing using this patch?

AFAICS, having a vblank timer when blob=true would be redundant and will interfere with the synchronization mechanism because the Guest compositor would start a new repaint cycle without knowing if it OK to reuse the FB it had submitted previously. I have not yet tested this patch but I strongly believe it will cause tearing issues particularly when blob=true.

Quoted 1 line
Quoted 4 lines
Quoted 2 lines

Vblank timer makes a big performance improvement for virtio-gpu KMS, especially for a native context on QEMU. No tearing spotted with it.

This means that the only remaining case to be tested is blob=true and virgl=false. I'll try to test it soon and let you know.

Thanks, likely won't be a problem to disable the timer based on a use-case with additional driver params if it will cause problems. Please post your testing results here.

Signature · 2 lines
Best regards,
Dmitry
author maintainer
#8 lore raw
In reply to Kasireddy, Vivek

Hi

Am 09.10.25 um 05:23 schrieb Kasireddy, Vivek:

Quoted 18 lines

Hi Thomas,

Quoted 16 lines

Subject: [PATCH] drm/virtgpu: Use vblank timer

Use a vblank timer to simulate the vblank interrupt. The DRM vblank helpers provide an implementation on top of Linux' hrtimer. Virtgpu enables and disables the timer as part of the CRTC. The atomic_flush callback sets up the event. Like vblank interrupts, the vblank timer fires at the rate of the display refresh.

Most userspace limits its page flip rate according to the DRM vblank event. Virtgpu's virtual hardware does not provide vblank interrupts, so DRM sends each event ASAP. With the fast access times of virtual display memory, the event rate is much higher than the display mode's refresh rate; creating the next page flip almost immediately. This leads to excessive CPU overhead from even small display updates, such as moving the mouse pointer.

Quoted 3 lines
Quoted 2 lines

This problem affects virtgpu and all other virtual displays. See [1] for a discussion in the context of hypervdrm.

When running Virtio-gpu with some of the UIs (gtk, spice, etc) in Qemu, the

We still need a solution for the other UIs, including the kernel's fbcon output.

Quoted 4 lines

Guest display updates are synchronized with a timer (or repaint callback in the case of Gtk) the UI layer provides particularly when blob=true. In other words, the Guest's atomic commit does not complete until the Host signals (via a fence) that the update (or flush) is done.

I know little about virtgpu's hypervisor interfaces. Let's see if I get the full picture.

It's the fences list that's being processed at [1], right? This fence signals the completion of the display update. The plane update sends out a command to the host and then waits for the ack at [2]. The ack happend at [3], which calls into [1]. At [4] the guest are available again to be released. How long the display update takes is up to the host and the guest's scheduler.

That's definitely something different than the vblank, as vblanks happen at fixed intervals.

[1] https://elixir.bootlin.com/linux/v6.17.1/source/drivers/gpu/drm/virtio/virtgpu_fence.c#L120
[2] https://elixir.bootlin.com/linux/v6.17.1/source/drivers/gpu/drm/virtio/virtgpu_vq.c#L397
[3] https://elixir.bootlin.com/linux/v6.17.1/source/drivers/gpu/drm/virtio/virtgpu_vq.c#L56
[4] https://elixir.bootlin.com/linux/v6.17.1/source/drivers/gpu/drm/virtio/virtgpu_vq.c#L265

Quoted 6 lines

This is because when blob=true, the Guest's FB is accessed by the Host without making any copies. So, to avoid tearing, the Guest needs to be prevented from accessing its FB until the Host is done. Therefore, I think for virtio-gpu, the virtual vblank timer can only be helpful when blob=false as this is the only case where Guest's display updates are unbounded (and Host makes a copy of the Guest's FB).

Because virtgpu did not initialize vblanking, DRM always sent out a vblank event after the completed commit. [5] It's not synchronized to the display output. This means that virtgpu has always been subject to tearing and the guest always required to hold buffers until the host completed its display update. IOW adding vblank timers into the mix should not change the behavior of virtgpu. It just limits the output frequency. If GNOME's pageflip is synchronized to the vblank, it should immediately benefit.

The GTK repaint callback is an interface for applications. How does it affect, or is affected by, any of this?

[5] https://elixir.bootlin.com/linux/v6.17.1/source/drivers/gpu/drm/drm_atomic_helper.c#L2606

Best regards
Thomas

Quoted 112 lines

I am not sure how this would affect virgl use-cases but Dmitry can help explain if the vblank timer would be useful there or not.

Thanks,
Vivek

Quoted 105 lines

devel/[email protected] amprd02.prod.outlook.com/ # [1]

---
  drivers/gpu/drm/virtio/virtgpu_display.c | 29 ++++++++++++++++++++++--
  1 file changed, 27 insertions(+), 2 deletions(-)
drivers/gpu/drm/virtio/virtgpu_display.c +3 -0
index c3315935d8bc..ee134e46eeba 100644
@@ -32,6 +32,8 @@
  #include <drm/drm_gem_framebuffer_helper.h>
  #include <drm/drm_probe_helper.h>
  #include <drm/drm_simple_kms_helper.h>
+#include <drm/drm_vblank.h>
+#include <drm/drm_vblank_helper.h>
 
  #include "virtgpu_drv.h"
 
@@ -55,6 +57,7 @@ static const struct drm_crtc_funcs virtio_gpu_crtc_funcs
 = {
  	.reset                  = drm_atomic_helper_crtc_reset,
  	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  	.atomic_destroy_state   = drm_atomic_helper_crtc_destroy_state,
+	DRM_CRTC_VBLANK_TIMER_FUNCS,
  };
 
  static const struct drm_framebuffer_funcs virtio_gpu_fb_funcs = {
drivers/gpu/drm/virtio/virtgpu_display.c +1 -0
@@ -99,6 +102,7 @@ static void virtio_gpu_crtc_mode_set_nofb(struct
 drm_crtc *crtc)
  static void virtio_gpu_crtc_atomic_enable(struct drm_crtc *crtc,
  					  struct drm_atomic_state *state)
  {
+	drm_crtc_vblank_on(crtc);
  }
 

static void virtio_gpu_crtc_atomic_disable(struct drm_crtc *crtc,

drivers/gpu/drm/virtio/virtgpu_display.c +2 -0
@@ -108,6 +112,8 @@ static void virtio_gpu_crtc_atomic_disable(struct
 drm_crtc *crtc,
  	struct virtio_gpu_device *vgdev = dev->dev_private;
  	struct virtio_gpu_output *output =
 drm_crtc_to_virtio_gpu_output(crtc);
 
+	drm_crtc_vblank_off(crtc);
+
  	virtio_gpu_cmd_set_scanout(vgdev, output->index, 0, 0, 0, 0, 0);
  	virtio_gpu_notify(vgdev);
  }
drivers/gpu/drm/virtio/virtgpu_display.c +2 -2
@@ -121,9 +127,10 @@ static int virtio_gpu_crtc_atomic_check(struct
 drm_crtc *crtc,
  static void virtio_gpu_crtc_atomic_flush(struct drm_crtc *crtc,
  					 struct drm_atomic_state *state)
  {
-	struct drm_crtc_state *crtc_state =
 drm_atomic_get_new_crtc_state(state,
-									  crtc);
+	struct drm_device *dev = crtc->dev;
+	struct drm_crtc_state *crtc_state =
 drm_atomic_get_new_crtc_state(state, crtc);
  	struct virtio_gpu_output *output =

drm_crtc_to_virtio_gpu_output(crtc);

drivers/gpu/drm/virtio/virtgpu_display.c +15 -0
+	struct drm_pending_vblank_event *event;
 
  	/*
  	 * virtio-gpu can't do modeset and plane update operations
@@ -133,6 +140,20 @@ static void virtio_gpu_crtc_atomic_flush(struct
 drm_crtc *crtc,
  	 */
  	if (drm_atomic_crtc_needs_modeset(crtc_state))
  		output->needs_modeset = true;
+
+	spin_lock_irq(&dev->event_lock);
+
+	event = crtc_state->event;
+	crtc_state->event = NULL;
+
+	if (event) {
+		if (drm_crtc_vblank_get(crtc) == 0)
+			drm_crtc_arm_vblank_event(crtc, event);
+		else
+			drm_crtc_send_vblank_event(crtc, event);
+	}
+
+	spin_unlock_irq(&dev->event_lock);
  }
 
  static const struct drm_crtc_helper_funcs virtio_gpu_crtc_helper_funcs = {
drivers/gpu/drm/virtio/virtgpu_display.c +4 -0
@@ -356,6 +377,10 @@ int virtio_gpu_modeset_init(struct virtio_gpu_device
 *vgdev)
  	for (i = 0 ; i < vgdev->num_scanouts; ++i)
  		vgdev_output_init(vgdev, i);
 
+	ret = drm_vblank_init(vgdev->ddev, vgdev->num_scanouts);
+	if (ret)
+		return ret;
+
  	drm_mode_config_reset(vgdev->ddev);
  	return 0;

}
--
2.51.0

Signature · 7 lines
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman
HRB 36809 (AG Nuernberg)
#9 lore raw

Hi Thomas,

Quoted 41 lines

Subject: Re: [PATCH] drm/virtgpu: Use vblank timer

Hi

Am 09.10.25 um 05:23 schrieb Kasireddy, Vivek:

Quoted 18 lines

Hi Thomas,

Quoted 16 lines

Subject: [PATCH] drm/virtgpu: Use vblank timer

Use a vblank timer to simulate the vblank interrupt. The DRM vblank helpers provide an implementation on top of Linux' hrtimer. Virtgpu enables and disables the timer as part of the CRTC. The atomic_flush callback sets up the event. Like vblank interrupts, the vblank timer fires at the rate of the display refresh.

Most userspace limits its page flip rate according to the DRM vblank event. Virtgpu's virtual hardware does not provide vblank interrupts, so DRM sends each event ASAP. With the fast access times of virtual display memory, the event rate is much higher than the display mode's refresh rate; creating the next page flip almost immediately. This leads to excessive CPU overhead from even small display updates, such as moving the mouse pointer.

Quoted 3 lines
Quoted 2 lines

This problem affects virtgpu and all other virtual displays. See [1] for a discussion in the context of hypervdrm.

When running Virtio-gpu with some of the UIs (gtk, spice, etc) in Qemu,

the

We still need a solution for the other UIs, including the kernel's fbcon output.

Quoted 1 line

Guest display updates are synchronized with a timer (or repaint callback

in

Quoted 2 lines

the case of Gtk) the UI layer provides particularly when blob=true. In other words, the Guest's atomic commit does not complete until the Host

signals

Quoted 1 line

(via a fence) that the update (or flush) is done.

I know little about virtgpu's hypervisor interfaces. Let's see if I get the full picture.

Quoted 6 lines

It's the fences list that's being processed at [1], right? This fence signals the completion of the display update. The plane update sends out a command to the host and then waits for the ack at [2]. The ack happend at [3], which calls into [1]. At [4] the guest are available again to be released. How long the display update takes is up to the host and the guest's scheduler.

When the Guest submits a display update, it is made to wait (but only when blob=true) here:
https://elixir.bootlin.com/linux/v6.17.1/source/drivers/gpu/drm/virtio/virtgpu_plane.c#L222

So, in this case, the time it takes for the Guest's update to finish is mostly dependent on the Host's UI update submission.

Quoted 3 lines

That's definitely something different than the vblank, as vblanks happen at fixed intervals.

The Qemu UI timer also runs at regular fixed intervals, so the Guest updates would run at 30 or 60 FPS based on the timer frequency. And, in the case of Gtk, the repaint callback is indirectly tied to the Host's Vblank, and it also occurs at regular intervals when there are pending updates.

Quoted 25 lines

[1]
https://elixir.bootlin.com/linux/v6.17.1/source/drivers/gpu/drm/virtio/virtg
pu_fence.c#L120
[2]
https://elixir.bootlin.com/linux/v6.17.1/source/drivers/gpu/drm/virtio/virtg
pu_vq.c#L397
[3]
https://elixir.bootlin.com/linux/v6.17.1/source/drivers/gpu/drm/virtio/virtg
pu_vq.c#L56
[4]
https://elixir.bootlin.com/linux/v6.17.1/source/drivers/gpu/drm/virtio/virtg
pu_vq.c#L265

Quoted 2 lines

This is because when blob=true, the Guest's FB is accessed by the Host

without

Quoted 1 line

making any copies. So, to avoid tearing, the Guest needs to be prevented

from

Quoted 1 line

accessing its FB until the Host is done. Therefore, I think for virtio-gpu, the

virtual

Quoted 1 line

vblank timer can only be helpful when blob=false as this is the only case

where

Quoted 1 line

Guest's display updates are unbounded (and Host makes a copy of the

Quoted 8 lines

Guest's FB).

Because virtgpu did not initialize vblanking, DRM always sent out a vblank event after the completed commit. [5] It's not synchronized to the display output. This means that virtgpu has always been subject to tearing and the guest always required to hold buffers until the host completed its display update. IOW adding vblank timers into the mix should not change the behavior of virtgpu. It just limits the output

As I mentioned, the output or display update frequency is already limited when blob=true, so adding a vblank timer would be redundant in this case. Note that blob=true is a performance optimization where we create a dmabuf (on the Host) backed by Guest FB's memory that is shared with the UI layer. And, AFAIK, the only case where virtio-gpu updates are not limited is when blob=false. Even in this case, there would be no tearing issues seen because the Guest is made to wait until the Host makes a copy of its FB.

Quoted 5 lines

frequency. If GNOME's pageflip is synchronized to the vblank, it should immediately benefit.

The GTK repaint callback is an interface for applications. How does it affect, or is affected by, any of this?

So, virtio-gpu is almost always coupled with a UI (on the Host) in order to display the Guest's desktop content (fbcon and compositor's FB data) on the Host locally (Gtk, SDL, etc) or streamed to a remote system (Spice, Vnc, etc). And, the rate at which the UI updates are submitted (to the Host compositor for local UIs) is controlled by the UI timer.

However, in the case of Qemu Gtk UI, the UI timer is used as a backup as we mostly rely on the repaint callback to figure out when to submit updates. This is because the repaint callback is a more reliable mechanism to determine when it is appropriate to submit an update to the Host compositor.

And, until the UI's update is not submitted (and executed by the Host GPU and signaled via an EGL fence), the Guest's update fence is not signaled. We can reliably achieve 60 FPS this way (assuming the Host's refresh rate is 60) for Guest's display updates.

Thanks,
Vivek

Quoted 136 lines

[5]
https://elixir.bootlin.com/linux/v6.17.1/source/drivers/gpu/drm/drm_atomi
c_helper.c#L2606

Best regards
Thomas

Quoted 2 lines

I am not sure how this would affect virgl use-cases but Dmitry can help

explain if

Quoted 8 lines

the vblank timer would be useful there or not.

Thanks,
Vivek

Quoted 3 lines

devel/[email protected]

Quoted 3 lines
Quoted 3 lines

amprd02.prod.outlook.com/ # [1]

---
  drivers/gpu/drm/virtio/virtgpu_display.c | 29

++++++++++++++++++++++--

Quoted 14 lines
Quoted 14 lines
  1 file changed, 27 insertions(+), 2 deletions(-)
drivers/gpu/drm/virtio/virtgpu_display.c +2 -0
index c3315935d8bc..ee134e46eeba 100644
@@ -32,6 +32,8 @@
  #include <drm/drm_gem_framebuffer_helper.h>
  #include <drm/drm_probe_helper.h>
  #include <drm/drm_simple_kms_helper.h>
+#include <drm/drm_vblank.h>
+#include <drm/drm_vblank_helper.h>
 
Quoted 3 lines

drivers/gpu/drm/virtio/virtgpu_display.c static const struct drm_crtc_funcs

Quoted 3 lines
drivers/gpu/drm/virtio/virtgpu_display.c +0 -0
  #include "virtgpu_drv.h"
 
@@ -55,6 +57,7 @@ static const struct drm_crtc_funcs

virtio_gpu_crtc_funcs

Quoted 66 lines
Quoted 66 lines
drivers/gpu/drm/virtio/virtgpu_display.c +1 -0
 = {
  	.reset                  = drm_atomic_helper_crtc_reset,
  	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  	.atomic_destroy_state   = drm_atomic_helper_crtc_destroy_state,
+	DRM_CRTC_VBLANK_TIMER_FUNCS,
  };
 
  static const struct drm_framebuffer_funcs virtio_gpu_fb_funcs = {
drivers/gpu/drm/virtio/virtgpu_display.c +1 -0
@@ -99,6 +102,7 @@ static void virtio_gpu_crtc_mode_set_nofb(struct
 drm_crtc *crtc)
  static void virtio_gpu_crtc_atomic_enable(struct drm_crtc *crtc,
  					  struct drm_atomic_state *state)
  {
+	drm_crtc_vblank_on(crtc);
  }
 

static void virtio_gpu_crtc_atomic_disable(struct drm_crtc *crtc,

drivers/gpu/drm/virtio/virtgpu_display.c +2 -0
@@ -108,6 +112,8 @@ static void virtio_gpu_crtc_atomic_disable(struct
 drm_crtc *crtc,
  	struct virtio_gpu_device *vgdev = dev->dev_private;
  	struct virtio_gpu_output *output =
 drm_crtc_to_virtio_gpu_output(crtc);
 
+	drm_crtc_vblank_off(crtc);
+
  	virtio_gpu_cmd_set_scanout(vgdev, output->index, 0, 0, 0, 0, 0);
  	virtio_gpu_notify(vgdev);
  }
drivers/gpu/drm/virtio/virtgpu_display.c +2 -2
@@ -121,9 +127,10 @@ static int virtio_gpu_crtc_atomic_check(struct
 drm_crtc *crtc,
  static void virtio_gpu_crtc_atomic_flush(struct drm_crtc *crtc,
  					 struct drm_atomic_state *state)
  {
-	struct drm_crtc_state *crtc_state =
 drm_atomic_get_new_crtc_state(state,
-									  crtc);
+	struct drm_device *dev = crtc->dev;
+	struct drm_crtc_state *crtc_state =
 drm_atomic_get_new_crtc_state(state, crtc);
  	struct virtio_gpu_output *output =

drm_crtc_to_virtio_gpu_output(crtc);

drivers/gpu/drm/virtio/virtgpu_display.c +15 -0
+	struct drm_pending_vblank_event *event;
 
  	/*
  	 * virtio-gpu can't do modeset and plane update operations
@@ -133,6 +140,20 @@ static void virtio_gpu_crtc_atomic_flush(struct
 drm_crtc *crtc,
  	 */
  	if (drm_atomic_crtc_needs_modeset(crtc_state))
  		output->needs_modeset = true;
+
+	spin_lock_irq(&dev->event_lock);
+
+	event = crtc_state->event;
+	crtc_state->event = NULL;
+
+	if (event) {
+		if (drm_crtc_vblank_get(crtc) == 0)
+			drm_crtc_arm_vblank_event(crtc, event);
+		else
+			drm_crtc_send_vblank_event(crtc, event);
+	}
+
+	spin_unlock_irq(&dev->event_lock);
  }
 
Quoted 1 line
Quoted 1 line

static const struct drm_crtc_helper_funcs virtio_gpu_crtc_helper_funcs

= {

Quoted 1 line

drivers/gpu/drm/virtio/virtgpu_display.c int virtio_gpu_modeset_init(struct

Quoted 1 line
drivers/gpu/drm/virtio/virtgpu_display.c +0 -0
@@ -356,6 +377,10 @@ int virtio_gpu_modeset_init(struct

virtio_gpu_device

Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/virtio/virtgpu_display.c +0 -0
 *vgdev)
  	for (i = 0 ; i < vgdev->num_scanouts; ++i)
  		vgdev_output_init(vgdev, i);
 
Quoted 10 lines
Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_display.c +4 -0
+	ret = drm_vblank_init(vgdev->ddev, vgdev->num_scanouts);
+	if (ret)
+		return ret;
+
  	drm_mode_config_reset(vgdev->ddev);
  	return 0;

}
--
2.51.0

--
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman HRB 36809 (AG Nuernberg)

author maintainer
#10 lore raw

Hi

Am 10.10.25 um 06:25 schrieb Kasireddy, Vivek:
[...]

Quoted 8 lines
Quoted 6 lines

Because virtgpu did not initialize vblanking, DRM always sent out a vblank event after the completed commit. [5] It's not synchronized to the display output. This means that virtgpu has always been subject to tearing and the guest always required to hold buffers until the host completed its display update. IOW adding vblank timers into the mix should not change the behavior of virtgpu. It just limits the output

As I mentioned, the output or display update frequency is already limited when blob=true, so adding a vblank timer would be redundant in this case.

To summarize my understanding: virtgpu currently generates vblank events as soon as if finished updating the display. Which can be too fast for blob=false. For blob=true, the display update is (reasonably assumed to be) synchronized with the host update and thereby rate limited by the host's display output.

Adding the vblank timer: For blob=true, if a vblank timeout happens during an ongoing display update, it does not send an event. If the display update finishes before the vblank times out, it arms the vblank event and the next vblank timeout will send it out. The vblank timer thereby makes events appear at regular intervals instead of ASAP.

So why add the complexity of handling blob=true separately when it should integrate well with the existing vblank framework?

Please also note that it's not just about compositors. DRM's fbcon synchronizes its output to the vblank period. If no vblank is supported, it'll happily fire out display updates ASAP even for blob=true.

Best regards
Thomas

Quoted 164 lines

Note that blob=true is a performance optimization where we create a dmabuf (on the Host) backed by Guest FB's memory that is shared with the UI layer. And, AFAIK, the only case where virtio-gpu updates are not limited is when blob=false. Even in this case, there would be no tearing issues seen because the Guest is made to wait until the Host makes a copy of its FB.

Quoted 5 lines

frequency. If GNOME's pageflip is synchronized to the vblank, it should immediately benefit.

The GTK repaint callback is an interface for applications. How does it affect, or is affected by, any of this?

So, virtio-gpu is almost always coupled with a UI (on the Host) in order to display the Guest's desktop content (fbcon and compositor's FB data) on the Host locally (Gtk, SDL, etc) or streamed to a remote system (Spice, Vnc, etc). And, the rate at which the UI updates are submitted (to the Host compositor for local UIs) is controlled by the UI timer.

However, in the case of Qemu Gtk UI, the UI timer is used as a backup as we mostly rely on the repaint callback to figure out when to submit updates. This is because the repaint callback is a more reliable mechanism to determine when it is appropriate to submit an update to the Host compositor.

And, until the UI's update is not submitted (and executed by the Host GPU and signaled via an EGL fence), the Guest's update fence is not signaled. We can reliably achieve 60 FPS this way (assuming the Host's refresh rate is 60) for Guest's display updates.

Thanks,
Vivek

Quoted 133 lines

[5]
https://elixir.bootlin.com/linux/v6.17.1/source/drivers/gpu/drm/drm_atomi
c_helper.c#L2606

Best regards
Thomas

Quoted 1 line

I am not sure how this would affect virgl use-cases but Dmitry can help

explain if

Quoted 8 lines

the vblank timer would be useful there or not.

Thanks,
Vivek

Quoted 3 lines

devel/[email protected]

Quoted 3 lines
Quoted 3 lines

amprd02.prod.outlook.com/ # [1]

---
   drivers/gpu/drm/virtio/virtgpu_display.c | 29

++++++++++++++++++++++--

Quoted 14 lines
Quoted 14 lines
   1 file changed, 27 insertions(+), 2 deletions(-)
drivers/gpu/drm/virtio/virtgpu_display.c +2 -0
index c3315935d8bc..ee134e46eeba 100644
@@ -32,6 +32,8 @@
   #include <drm/drm_gem_framebuffer_helper.h>
   #include <drm/drm_probe_helper.h>
   #include <drm/drm_simple_kms_helper.h>
+#include <drm/drm_vblank.h>
+#include <drm/drm_vblank_helper.h>
 
Quoted 3 lines

drivers/gpu/drm/virtio/virtgpu_display.c static const struct drm_crtc_funcs

Quoted 3 lines
drivers/gpu/drm/virtio/virtgpu_display.c +0 -0
   #include "virtgpu_drv.h"
 
@@ -55,6 +57,7 @@ static const struct drm_crtc_funcs

virtio_gpu_crtc_funcs

Quoted 66 lines
Quoted 66 lines
drivers/gpu/drm/virtio/virtgpu_display.c +1 -0
 = {
   	.reset                  = drm_atomic_helper_crtc_reset,
   	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
   	.atomic_destroy_state   = drm_atomic_helper_crtc_destroy_state,
+	DRM_CRTC_VBLANK_TIMER_FUNCS,
   };
 
   static const struct drm_framebuffer_funcs virtio_gpu_fb_funcs = {
drivers/gpu/drm/virtio/virtgpu_display.c +1 -0
@@ -99,6 +102,7 @@ static void virtio_gpu_crtc_mode_set_nofb(struct
 drm_crtc *crtc)
   static void virtio_gpu_crtc_atomic_enable(struct drm_crtc *crtc,
   					  struct drm_atomic_state *state)
   {
+	drm_crtc_vblank_on(crtc);
   }
 

static void virtio_gpu_crtc_atomic_disable(struct drm_crtc *crtc,

drivers/gpu/drm/virtio/virtgpu_display.c +2 -0
@@ -108,6 +112,8 @@ static void virtio_gpu_crtc_atomic_disable(struct
 drm_crtc *crtc,
   	struct virtio_gpu_device *vgdev = dev->dev_private;
   	struct virtio_gpu_output *output =
 drm_crtc_to_virtio_gpu_output(crtc);
 
+	drm_crtc_vblank_off(crtc);
+
   	virtio_gpu_cmd_set_scanout(vgdev, output->index, 0, 0, 0, 0, 0);
   	virtio_gpu_notify(vgdev);
   }
drivers/gpu/drm/virtio/virtgpu_display.c +2 -2
@@ -121,9 +127,10 @@ static int virtio_gpu_crtc_atomic_check(struct
 drm_crtc *crtc,
   static void virtio_gpu_crtc_atomic_flush(struct drm_crtc *crtc,
   					 struct drm_atomic_state *state)
   {
-	struct drm_crtc_state *crtc_state =
 drm_atomic_get_new_crtc_state(state,
-									  crtc);
+	struct drm_device *dev = crtc->dev;
+	struct drm_crtc_state *crtc_state =
 drm_atomic_get_new_crtc_state(state, crtc);
   	struct virtio_gpu_output *output =

drm_crtc_to_virtio_gpu_output(crtc);

drivers/gpu/drm/virtio/virtgpu_display.c +15 -0
+	struct drm_pending_vblank_event *event;
 
   	/*
   	 * virtio-gpu can't do modeset and plane update operations
@@ -133,6 +140,20 @@ static void virtio_gpu_crtc_atomic_flush(struct
 drm_crtc *crtc,
   	 */
   	if (drm_atomic_crtc_needs_modeset(crtc_state))
   		output->needs_modeset = true;
+
+	spin_lock_irq(&dev->event_lock);
+
+	event = crtc_state->event;
+	crtc_state->event = NULL;
+
+	if (event) {
+		if (drm_crtc_vblank_get(crtc) == 0)
+			drm_crtc_arm_vblank_event(crtc, event);
+		else
+			drm_crtc_send_vblank_event(crtc, event);
+	}
+
+	spin_unlock_irq(&dev->event_lock);
   }
 
Quoted 1 line
Quoted 1 line

static const struct drm_crtc_helper_funcs virtio_gpu_crtc_helper_funcs

= {

Quoted 1 line

drivers/gpu/drm/virtio/virtgpu_display.c int virtio_gpu_modeset_init(struct

Quoted 1 line
drivers/gpu/drm/virtio/virtgpu_display.c +0 -0
@@ -356,6 +377,10 @@ int virtio_gpu_modeset_init(struct

virtio_gpu_device

Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/virtio/virtgpu_display.c +0 -0
 *vgdev)
   	for (i = 0 ; i < vgdev->num_scanouts; ++i)
   		vgdev_output_init(vgdev, i);
 
Quoted 10 lines
Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_display.c +4 -0
+	ret = drm_vblank_init(vgdev->ddev, vgdev->num_scanouts);
+	if (ret)
+		return ret;
+
   	drm_mode_config_reset(vgdev->ddev);
   	return 0;
   }

--
2.51.0

--
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman HRB 36809 (AG Nuernberg)

Signature · 7 lines
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman
HRB 36809 (AG Nuernberg)
#11 lore raw

Hi Thomas,

Quoted 12 lines

Subject: Re: [PATCH] drm/virtgpu: Use vblank timer

Hi

Am 10.10.25 um 06:25 schrieb Kasireddy, Vivek:
[...]

Quoted 6 lines
Quoted 6 lines

Because virtgpu did not initialize vblanking, DRM always sent out a vblank event after the completed commit. [5] It's not synchronized to the display output. This means that virtgpu has always been subject to tearing and the guest always required to hold buffers until the host completed its display update. IOW adding vblank timers into the mix should not change the behavior of virtgpu. It just limits the output

Quoted 8 lines
Quoted 2 lines

As I mentioned, the output or display update frequency is already limited when blob=true, so adding a vblank timer would be redundant in this case.

To summarize my understanding: virtgpu currently generates vblank events as soon as if finished updating the display. Which can be too fast for blob=false. For blob=true, the display update is (reasonably assumed to be) synchronized with the host update and thereby rate limited by the host's display output.

Yeah, that is mostly correct.

Quoted 9 lines

Adding the vblank timer: For blob=true, if a vblank timeout happens during an ongoing display update, it does not send an event. If the display update finishes before the vblank times out, it arms the vblank event and the next vblank timeout will send it out. The vblank timer thereby makes events appear at regular intervals instead of ASAP.

So why add the complexity of handling blob=true separately when it should integrate well with the existing vblank framework?

There are a few reasons for this:
- When blob=true, the most important goal is to prevent tearing (i.e, to ensure that Guest VM and Host are not accessing the Guest's FB simultaneously). As explained previously, just ensuring that this goal is met will automatically result in rate limited updates that are in sync with the Host compositor's repaint cycle (if using Gtk UI). So, given this, adding a vblank timer would not help in this case and instead would lead to updates that are no longer in sync with the Host compositor's repaint cycle.

- Qemu (and Qemu UI) also works with virtio-gpu driver that is part of Windows VMs where a vblank timer may or may not be available. So, it is easier to support this case by having a UI timer/repaint cb and making the Guest VM just wait until the update (or resource flush) is completed to ensure synchronization and rate limited updates.

- Even if a vblank timer is available for a Guest VM (Windows or Linux) its frequency may not match the UI update frequency, so there might either be wasteful updates or too few updates. For example, if the Host's rate is 30 FPS, but the Guest VM's vblank timer is running at 60 FPS, then roughly 30 frames would not be displayed each cycle IIUC.

Quoted 4 lines

Please also note that it's not just about compositors. DRM's fbcon synchronizes its output to the vblank period. If no vblank is supported, it'll happily fire out display updates ASAP even for blob=true.

IIUC, the fbcon updates would still be rate limited in the blob=true case as the vblank event would only be sent out after the atomic/plane update is finished right?

Thanks,
Vivek

Quoted 180 lines

Best regards
Thomas

Quoted 23 lines

Note that blob=true is a performance optimization where we create a dmabuf (on the Host) backed by Guest FB's memory that is shared with the UI layer. And, AFAIK, the only case where virtio-gpu updates are not limited is when blob=false. Even in this case, there would be no tearing issues seen because the Guest is made to wait until the Host makes a copy of its FB.

Quoted 5 lines

frequency. If GNOME's pageflip is synchronized to the vblank, it should immediately benefit.

The GTK repaint callback is an interface for applications. How does it affect, or is affected by, any of this?

So, virtio-gpu is almost always coupled with a UI (on the Host) in order to display the Guest's desktop content (fbcon and compositor's FB data) on the Host locally (Gtk, SDL, etc) or streamed to a remote system (Spice, Vnc, etc). And, the rate at which the UI updates are submitted (to the Host compositor for local UIs) is controlled by the UI timer.

However, in the case of Qemu Gtk UI, the UI timer is used as a backup as we mostly rely on the repaint callback to figure out when to submit updates. This is because the repaint callback is a more reliable mechanism to determine when it is appropriate to submit an update to the Host compositor.

Quoted 10 lines

And, until the UI's update is not submitted (and executed by the Host GPU and signaled via an EGL fence), the Guest's update fence is not signaled. We can reliably achieve 60 FPS this way (assuming the Host's refresh rate is 60) for Guest's display updates.

Thanks,
Vivek

Quoted 2 lines

[5]

https://elixir.bootlin.com/linux/v6.17.1/source/drivers/gpu/drm/drm_atomi

Quoted 9 lines
Quoted 9 lines

c_helper.c#L2606

Best regards
Thomas

Quoted 1 line

I am not sure how this would affect virgl use-cases but Dmitry can help

explain if

Quoted 2 lines

the vblank timer would be useful there or not.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

Thanks,
Vivek

Quoted 3 lines

devel/[email protected]

Quoted 107 lines
Quoted 107 lines
Quoted 3 lines
Quoted 3 lines

amprd02.prod.outlook.com/ # [1]

---
   drivers/gpu/drm/virtio/virtgpu_display.c | 29

++++++++++++++++++++++--

Quoted 14 lines
Quoted 14 lines
   1 file changed, 27 insertions(+), 2 deletions(-)
drivers/gpu/drm/virtio/virtgpu_display.c +2 -0
index c3315935d8bc..ee134e46eeba 100644
@@ -32,6 +32,8 @@
   #include <drm/drm_gem_framebuffer_helper.h>
   #include <drm/drm_probe_helper.h>
   #include <drm/drm_simple_kms_helper.h>
+#include <drm/drm_vblank.h>
+#include <drm/drm_vblank_helper.h>
 
Quoted 3 lines

drivers/gpu/drm/virtio/virtgpu_display.c static const struct drm_crtc_funcs

Quoted 3 lines
drivers/gpu/drm/virtio/virtgpu_display.c +0 -0
   #include "virtgpu_drv.h"
 
@@ -55,6 +57,7 @@ static const struct drm_crtc_funcs

virtio_gpu_crtc_funcs

Quoted 66 lines
Quoted 66 lines
drivers/gpu/drm/virtio/virtgpu_display.c +1 -0
 = {
   	.reset                  = drm_atomic_helper_crtc_reset,
   	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
   	.atomic_destroy_state   = drm_atomic_helper_crtc_destroy_state,
+	DRM_CRTC_VBLANK_TIMER_FUNCS,
   };
 
   static const struct drm_framebuffer_funcs virtio_gpu_fb_funcs = {
drivers/gpu/drm/virtio/virtgpu_display.c +1 -0
@@ -99,6 +102,7 @@ static void virtio_gpu_crtc_mode_set_nofb(struct
 drm_crtc *crtc)
   static void virtio_gpu_crtc_atomic_enable(struct drm_crtc *crtc,
   					  struct drm_atomic_state *state)
   {
+	drm_crtc_vblank_on(crtc);
   }
 

static void virtio_gpu_crtc_atomic_disable(struct drm_crtc *crtc,

drivers/gpu/drm/virtio/virtgpu_display.c +2 -0
@@ -108,6 +112,8 @@ static void virtio_gpu_crtc_atomic_disable(struct
 drm_crtc *crtc,
   	struct virtio_gpu_device *vgdev = dev->dev_private;
   	struct virtio_gpu_output *output =
 drm_crtc_to_virtio_gpu_output(crtc);
 
+	drm_crtc_vblank_off(crtc);
+
   	virtio_gpu_cmd_set_scanout(vgdev, output->index, 0, 0, 0, 0, 0);
   	virtio_gpu_notify(vgdev);
   }
drivers/gpu/drm/virtio/virtgpu_display.c +2 -2
@@ -121,9 +127,10 @@ static int virtio_gpu_crtc_atomic_check(struct
 drm_crtc *crtc,
   static void virtio_gpu_crtc_atomic_flush(struct drm_crtc *crtc,
   					 struct drm_atomic_state *state)
   {
-	struct drm_crtc_state *crtc_state =
 drm_atomic_get_new_crtc_state(state,
-									  crtc);
+	struct drm_device *dev = crtc->dev;
+	struct drm_crtc_state *crtc_state =
 drm_atomic_get_new_crtc_state(state, crtc);
   	struct virtio_gpu_output *output =

drm_crtc_to_virtio_gpu_output(crtc);

drivers/gpu/drm/virtio/virtgpu_display.c +15 -0
+	struct drm_pending_vblank_event *event;
 
   	/*
   	 * virtio-gpu can't do modeset and plane update operations
@@ -133,6 +140,20 @@ static void virtio_gpu_crtc_atomic_flush(struct
 drm_crtc *crtc,
   	 */
   	if (drm_atomic_crtc_needs_modeset(crtc_state))
   		output->needs_modeset = true;
+
+	spin_lock_irq(&dev->event_lock);
+
+	event = crtc_state->event;
+	crtc_state->event = NULL;
+
+	if (event) {
+		if (drm_crtc_vblank_get(crtc) == 0)
+			drm_crtc_arm_vblank_event(crtc, event);
+		else
+			drm_crtc_send_vblank_event(crtc, event);
+	}
+
+	spin_unlock_irq(&dev->event_lock);
   }
 
Quoted 1 line
Quoted 1 line

static const struct drm_crtc_helper_funcs virtio_gpu_crtc_helper_funcs

= {

Quoted 1 line

drivers/gpu/drm/virtio/virtgpu_display.c int virtio_gpu_modeset_init(struct

Quoted 1 line
drivers/gpu/drm/virtio/virtgpu_display.c +0 -0
@@ -356,6 +377,10 @@ int virtio_gpu_modeset_init(struct

virtio_gpu_device

Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/virtio/virtgpu_display.c +0 -0
 *vgdev)
   	for (i = 0 ; i < vgdev->num_scanouts; ++i)
   		vgdev_output_init(vgdev, i);
 
Quoted 10 lines
Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_display.c +4 -0
+	ret = drm_vblank_init(vgdev->ddev, vgdev->num_scanouts);
+	if (ret)
+		return ret;
+
   	drm_mode_config_reset(vgdev->ddev);
   	return 0;
   }

--
2.51.0

--

Quoted 8 lines
Quoted 8 lines

--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman HRB 36809 (AG Nuernberg)

--
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman HRB 36809 (AG Nuernberg)

author maintainer
#12 lore raw

Hi

Am 13.10.25 um 09:14 schrieb Kasireddy, Vivek:

Quoted 31 lines

Hi Thomas,

Quoted 12 lines

Subject: Re: [PATCH] drm/virtgpu: Use vblank timer

Hi

Am 10.10.25 um 06:25 schrieb Kasireddy, Vivek:
[...]

Quoted 6 lines
Quoted 6 lines

Because virtgpu did not initialize vblanking, DRM always sent out a vblank event after the completed commit. [5] It's not synchronized to the display output. This means that virtgpu has always been subject to tearing and the guest always required to hold buffers until the host completed its display update. IOW adding vblank timers into the mix should not change the behavior of virtgpu. It just limits the output

Quoted 7 lines
Quoted 2 lines

As I mentioned, the output or display update frequency is already limited when blob=true, so adding a vblank timer would be redundant in this case.

To summarize my understanding: virtgpu currently generates vblank events as soon as if finished updating the display. Which can be too fast for blob=false. For blob=true, the display update is (reasonably assumed to be) synchronized with the host update and thereby rate limited by the host's display output.

Yeah, that is mostly correct.

Quoted 8 lines

Adding the vblank timer: For blob=true, if a vblank timeout happens during an ongoing display update, it does not send an event. If the display update finishes before the vblank times out, it arms the vblank event and the next vblank timeout will send it out. The vblank timer thereby makes events appear at regular intervals instead of ASAP.

So why add the complexity of handling blob=true separately when it should integrate well with the existing vblank framework?

Quoted 9 lines

There are a few reasons for this:
- When blob=true, the most important goal is to prevent tearing (i.e, to ensure that Guest VM and Host are not accessing the Guest's FB simultaneously). As explained previously, just ensuring that this goal is met will automatically result in rate limited updates that are in sync with the Host compositor's repaint cycle (if using Gtk UI). So, given this, adding a vblank timer would not help in this case and instead would lead to updates that are no longer in sync with the Host compositor's repaint cycle.

No that's not the case. With the vblank timer, the output on the host is still synced to the host's repaint cycle. The guest's compositor still waits for it to finish before updating its own output.

Quoted 6 lines

- Qemu (and Qemu UI) also works with virtio-gpu driver that is part of Windows VMs where a vblank timer may or may not be available. So, it is easier to support this case by having a UI timer/repaint cb and making the Guest VM just wait until the update (or resource flush) is completed to ensure synchronization and rate limited updates.

Nothing in this patch affects the Windows driver. We're not going to remove or modify the host's repaint code.

Quoted 6 lines

- Even if a vblank timer is available for a Guest VM (Windows or Linux) its frequency may not match the UI update frequency, so there might either be wasteful updates or too few updates. For example, if the Host's rate is 30 FPS, but the Guest VM's vblank timer is running at 60 FPS, then roughly 30 frames would not be displayed each cycle IIUC.

If the host currently updates with only 30 Hz, it already does not show those additional 30 frames.

The userspace compositor only sends the next frame when the current frame has been completed; as signalled by the vblank event. That vblank event only happens after the previous display update completed. (It should have rather been called page-flip event instead.) If the host only updates at 30 Hz, there are currently at most 30 such events per second.  If we add a vblank timer, it'll fire at the guest display refresh rate (say 60 Hz), but only send these events when the previous update has been completed; which is 30 Hz.

Quoted 7 lines
Quoted 4 lines

Please also note that it's not just about compositors. DRM's fbcon synchronizes its output to the vblank period. If no vblank is supported, it'll happily fire out display updates ASAP even for blob=true.

IIUC, the fbcon updates would still be rate limited in the blob=true case as the vblank event would only be sent out after the atomic/plane update is finished right?

That's not the case. The DRM fbcon/fbdev code waits for a vblank timeout/interrust to occur [1]  but does NOT wait for the vblank event. To do so, it enables vblanks interrupts, waits for one to occur and then continues. [2] If the driver does not support vblank interrupts, it continues right away without waiting. [3]  The fbcon/fbdev support only uses a single buffer object for a its output. Hence this is where you actually get tearing even with blob=true. None of this can be changed much because of the way fbdev works.

[1] https://gitlab.freedesktop.org/drm/misc/kernel/-/blob/drm-misc-next-2025-10-02/drivers/gpu/drm/drm_fb_helper.c?ref_type=tags#L372
[2] https://gitlab.freedesktop.org/drm/misc/kernel/-/blob/drm-misc-next-2025-10-02/drivers/gpu/drm/drm_client_modeset.c?ref_type=tags#L1322
[3] https://gitlab.freedesktop.org/drm/misc/kernel/-/blob/drm-misc-next-2025-10-02/drivers/gpu/drm/drm_vblank.c?ref_type=tags#L1215

Best regards
Thomas

Quoted 181 lines

Thanks,
Vivek

Quoted 177 lines

Best regards
Thomas

Quoted 23 lines

Note that blob=true is a performance optimization where we create a dmabuf (on the Host) backed by Guest FB's memory that is shared with the UI layer. And, AFAIK, the only case where virtio-gpu updates are not limited is when blob=false. Even in this case, there would be no tearing issues seen because the Guest is made to wait until the Host makes a copy of its FB.

Quoted 5 lines

frequency. If GNOME's pageflip is synchronized to the vblank, it should immediately benefit.

The GTK repaint callback is an interface for applications. How does it affect, or is affected by, any of this?

So, virtio-gpu is almost always coupled with a UI (on the Host) in order to display the Guest's desktop content (fbcon and compositor's FB data) on the Host locally (Gtk, SDL, etc) or streamed to a remote system (Spice, Vnc, etc). And, the rate at which the UI updates are submitted (to the Host compositor for local UIs) is controlled by the UI timer.

However, in the case of Qemu Gtk UI, the UI timer is used as a backup as we mostly rely on the repaint callback to figure out when to submit updates. This is because the repaint callback is a more reliable mechanism to determine when it is appropriate to submit an update to the Host compositor.

Quoted 10 lines

And, until the UI's update is not submitted (and executed by the Host GPU and signaled via an EGL fence), the Guest's update fence is not signaled. We can reliably achieve 60 FPS this way (assuming the Host's refresh rate is 60) for Guest's display updates.

Thanks,
Vivek

Quoted 2 lines

[5]

https://elixir.bootlin.com/linux/v6.17.1/source/drivers/gpu/drm/drm_atomi

Quoted 7 lines
Quoted 7 lines

c_helper.c#L2606

Best regards
Thomas

Quoted 1 line

I am not sure how this would affect virgl use-cases but Dmitry can help

explain if

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

the vblank timer would be useful there or not.

Thanks,
Vivek

Quoted 3 lines

devel/[email protected]

Quoted 107 lines
Quoted 107 lines
Quoted 3 lines
Quoted 3 lines

amprd02.prod.outlook.com/ # [1]

---
    drivers/gpu/drm/virtio/virtgpu_display.c | 29

++++++++++++++++++++++--

Quoted 14 lines
Quoted 14 lines
    1 file changed, 27 insertions(+), 2 deletions(-)
drivers/gpu/drm/virtio/virtgpu_display.c +2 -0
index c3315935d8bc..ee134e46eeba 100644
@@ -32,6 +32,8 @@
    #include <drm/drm_gem_framebuffer_helper.h>
    #include <drm/drm_probe_helper.h>
    #include <drm/drm_simple_kms_helper.h>
+#include <drm/drm_vblank.h>
+#include <drm/drm_vblank_helper.h>
 
Quoted 3 lines

drivers/gpu/drm/virtio/virtgpu_display.c static const struct drm_crtc_funcs

Quoted 3 lines
drivers/gpu/drm/virtio/virtgpu_display.c +0 -0
    #include "virtgpu_drv.h"
 
@@ -55,6 +57,7 @@ static const struct drm_crtc_funcs

virtio_gpu_crtc_funcs

Quoted 66 lines
Quoted 66 lines
drivers/gpu/drm/virtio/virtgpu_display.c +1 -0
 = {
    	.reset                  = drm_atomic_helper_crtc_reset,
    	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
    	.atomic_destroy_state   = drm_atomic_helper_crtc_destroy_state,
+	DRM_CRTC_VBLANK_TIMER_FUNCS,
    };
 
    static const struct drm_framebuffer_funcs virtio_gpu_fb_funcs = {
drivers/gpu/drm/virtio/virtgpu_display.c +1 -0
@@ -99,6 +102,7 @@ static void virtio_gpu_crtc_mode_set_nofb(struct
 drm_crtc *crtc)
    static void virtio_gpu_crtc_atomic_enable(struct drm_crtc *crtc,
    					  struct drm_atomic_state *state)
    {
+	drm_crtc_vblank_on(crtc);
    }
 

static void virtio_gpu_crtc_atomic_disable(struct drm_crtc *crtc,

drivers/gpu/drm/virtio/virtgpu_display.c +2 -0
@@ -108,6 +112,8 @@ static void virtio_gpu_crtc_atomic_disable(struct
 drm_crtc *crtc,
    	struct virtio_gpu_device *vgdev = dev->dev_private;
    	struct virtio_gpu_output *output =
 drm_crtc_to_virtio_gpu_output(crtc);
 
+	drm_crtc_vblank_off(crtc);
+
    	virtio_gpu_cmd_set_scanout(vgdev, output->index, 0, 0, 0, 0, 0);
    	virtio_gpu_notify(vgdev);
    }
drivers/gpu/drm/virtio/virtgpu_display.c +2 -2
@@ -121,9 +127,10 @@ static int virtio_gpu_crtc_atomic_check(struct
 drm_crtc *crtc,
    static void virtio_gpu_crtc_atomic_flush(struct drm_crtc *crtc,
    					 struct drm_atomic_state *state)
    {
-	struct drm_crtc_state *crtc_state =
 drm_atomic_get_new_crtc_state(state,
-									  crtc);
+	struct drm_device *dev = crtc->dev;
+	struct drm_crtc_state *crtc_state =
 drm_atomic_get_new_crtc_state(state, crtc);
    	struct virtio_gpu_output *output =

drm_crtc_to_virtio_gpu_output(crtc);

drivers/gpu/drm/virtio/virtgpu_display.c +15 -0
+	struct drm_pending_vblank_event *event;
 
    	/*
    	 * virtio-gpu can't do modeset and plane update operations
@@ -133,6 +140,20 @@ static void virtio_gpu_crtc_atomic_flush(struct
 drm_crtc *crtc,
    	 */
    	if (drm_atomic_crtc_needs_modeset(crtc_state))
    		output->needs_modeset = true;
+
+	spin_lock_irq(&dev->event_lock);
+
+	event = crtc_state->event;
+	crtc_state->event = NULL;
+
+	if (event) {
+		if (drm_crtc_vblank_get(crtc) == 0)
+			drm_crtc_arm_vblank_event(crtc, event);
+		else
+			drm_crtc_send_vblank_event(crtc, event);
+	}
+
+	spin_unlock_irq(&dev->event_lock);
    }
 
Quoted 1 line
Quoted 1 line

static const struct drm_crtc_helper_funcs virtio_gpu_crtc_helper_funcs

= {

Quoted 1 line

drivers/gpu/drm/virtio/virtgpu_display.c int virtio_gpu_modeset_init(struct

Quoted 1 line
drivers/gpu/drm/virtio/virtgpu_display.c +0 -0
@@ -356,6 +377,10 @@ int virtio_gpu_modeset_init(struct

virtio_gpu_device

Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/virtio/virtgpu_display.c +0 -0
 *vgdev)
    	for (i = 0 ; i < vgdev->num_scanouts; ++i)
    		vgdev_output_init(vgdev, i);
 
Quoted 10 lines
Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_display.c +4 -0
+	ret = drm_vblank_init(vgdev->ddev, vgdev->num_scanouts);
+	if (ret)
+		return ret;
+
    	drm_mode_config_reset(vgdev->ddev);
    	return 0;
    }

--
2.51.0

--

Quoted 8 lines
Quoted 8 lines

--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman HRB 36809 (AG Nuernberg)

--
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman HRB 36809 (AG Nuernberg)

Signature · 7 lines
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman
HRB 36809 (AG Nuernberg)
#13 lore raw

Hi Thomas,

Quoted 41 lines

Subject: Re: [PATCH] drm/virtgpu: Use vblank timer

Quoted 10 lines
Quoted 10 lines

Am 10.10.25 um 06:25 schrieb Kasireddy, Vivek:
[...]

Quoted 7 lines
Quoted 6 lines

Because virtgpu did not initialize vblanking, DRM always sent out a vblank event after the completed commit. [5] It's not synchronized to the display output. This means that virtgpu has always been subject to tearing and the guest always required to hold buffers until the host completed its display update. IOW adding vblank timers into the mix should not change the behavior of virtgpu. It just limits the output

As I mentioned, the output or display update frequency is already

limited

Quoted 1 line
Quoted 1 line
Quoted 1 line

when blob=true, so adding a vblank timer would be redundant in this

case.

Quoted 1 line
Quoted 1 line

To summarize my understanding: virtgpu currently generates vblank

events

Quoted 14 lines
Quoted 4 lines

as soon as if finished updating the display. Which can be too fast for blob=false. For blob=true, the display update is (reasonably assumed to be) synchronized with the host update and thereby rate limited by the host's display output.

Yeah, that is mostly correct.

Quoted 8 lines

Adding the vblank timer: For blob=true, if a vblank timeout happens during an ongoing display update, it does not send an event. If the display update finishes before the vblank times out, it arms the vblank event and the next vblank timeout will send it out. The vblank timer thereby makes events appear at regular intervals instead of ASAP.

So why add the complexity of handling blob=true separately when it should integrate well with the existing vblank framework?

Quoted 9 lines

There are a few reasons for this:
- When blob=true, the most important goal is to prevent tearing (i.e, to ensure that Guest VM and Host are not accessing the Guest's FB simultaneously). As explained previously, just ensuring that this goal is met will automatically result in rate limited updates that are in sync with the Host compositor's repaint cycle (if using Gtk UI). So, given this, adding a vblank timer would not help in this case and instead would lead to updates that are no longer in sync with the Host compositor's repaint cycle.

Quoted 3 lines

No that's not the case. With the vblank timer, the output on the host is still synced to the host's repaint cycle. The guest's compositor still waits for it to finish before updating its own output.

My concern is that in this case, the Guest's compositor will be made to wait a bit too long before it starts its repaint cycle because the vblank event was not sent immediately when the Host's update was finished but is instead sent after the next vblank timeout. This is what I meant by the Guest's updates not staying in sync with the Host compositor's repaint cycle because the Guest compositor would not start drawing at the best possible time (which is indicated by the Host compositor via the repaint callback). More on this below.

Quoted 21 lines
Quoted 7 lines

- Qemu (and Qemu UI) also works with virtio-gpu driver that is part of Windows VMs where a vblank timer may or may not be available. So, it is easier to support this case by having a UI timer/repaint cb and making the Guest VM just wait until the update (or resource flush) is completed to ensure synchronization and rate limited updates.

Nothing in this patch affects the Windows driver. We're not going to remove or modify the host's repaint code.

Quoted 6 lines

- Even if a vblank timer is available for a Guest VM (Windows or Linux) its frequency may not match the UI update frequency, so there might either be wasteful updates or too few updates. For example, if the Host's rate is 30 FPS, but the Guest VM's vblank timer is running at 60 FPS, then roughly 30 frames would not be displayed each cycle IIUC.

If the host currently updates with only 30 Hz, it already does not show those additional 30 frames.

Quoted 8 lines

The userspace compositor only sends the next frame when the current frame has been completed; as signalled by the vblank event. That vblank event only happens after the previous display update completed. (It should have rather been called page-flip event instead.) If the host only updates at 30 Hz, there are currently at most 30 such events per second.  If we add a vblank timer, it'll fire at the guest display refresh rate (say 60 Hz), but only send these events when the previous update has been completed; which is 30 Hz.

Ok, as long as the Host's refresh rate is communicated with virtio-gpu, which is then used as the input to determine the vblank timer frequency, this should work. However, as mentioned above, I am still concerned about when the vblank event would be sent. For example, let us assume that we have 60 Hz as the Guest's refresh rate, which means the vblank event/ interrupt is expected to happen roughly every 16.66 ms. So, IIUC, we have:
0................16....................33................50...........

My specific concern is that if the Guest gets notified that the Host update is finished at let's say 18 ms mark above, I think the vblank event would not get sent out right away and instead would be sent out at the 33 ms mark right? If yes, I wonder if this pattern would continue to repeat itself impacting performance?

Quoted 9 lines
Quoted 8 lines
Quoted 5 lines

Please also note that it's not just about compositors. DRM's fbcon synchronizes its output to the vblank period. If no vblank is supported, it'll happily fire out display updates ASAP even for blob=true.

IIUC, the fbcon updates would still be rate limited in the blob=true case as the vblank event would only be sent out after the atomic/plane update is finished right?

Quoted 8 lines

That's not the case. The DRM fbcon/fbdev code waits for a vblank timeout/interrust to occur [1]  but does NOT wait for the vblank event. To do so, it enables vblanks interrupts, waits for one to occur and then continues. [2] If the driver does not support vblank interrupts, it continues right away without waiting. [3]  The fbcon/fbdev support only uses a single buffer object for a its output. Hence this is where you actually get tearing even with blob=true. None of this can be changed much because of the way fbdev works.

I have not looked into fbdev/fbcon closely but I did test Gnome + Xorg which uses the dirtyfb() method with a single FB for updates (instead of page flips with double buffering) when there is no hardware cursor available. And, AFAICT, the dirtyfb() method works with no tearing when blob=true because it just invokes the atomic commit (drm_atomic_commit) in a blocking manner, which means it just waits until the Host update is finished. I thought fbdev/fbcon worked in a similar way.

Thanks,
Vivek

Quoted 223 lines

[1]
https://gitlab.freedesktop.org/drm/misc/kernel/-/blob/drm-misc-next-2025-
10-02/drivers/gpu/drm/drm_fb_helper.c?ref_type=tags#L372
[2]
https://gitlab.freedesktop.org/drm/misc/kernel/-/blob/drm-misc-next-2025-
10-02/drivers/gpu/drm/drm_client_modeset.c?ref_type=tags#L1322
[3]
https://gitlab.freedesktop.org/drm/misc/kernel/-/blob/drm-misc-next-2025-
10-02/drivers/gpu/drm/drm_vblank.c?ref_type=tags#L1215

Best regards
Thomas

Quoted 9 lines

Thanks,
Vivek

Quoted 5 lines

Best regards
Thomas

Quoted 2 lines

Note that blob=true is a performance optimization where we create a dmabuf (on the Host) backed by Guest FB's memory that is shared with

the

Quoted 1 line
Quoted 1 line
Quoted 1 line

UI layer. And, AFAIK, the only case where virtio-gpu updates are not

limited

Quoted 1 line
Quoted 1 line
Quoted 1 line

is when blob=false. Even in this case, there would be no tearing issues

seen

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

because the Guest is made to wait until the Host makes a copy of its FB.

Quoted 5 lines

frequency. If GNOME's pageflip is synchronized to the vblank, it should immediately benefit.

The GTK repaint callback is an interface for applications. How does it affect, or is affected by, any of this?

So, virtio-gpu is almost always coupled with a UI (on the Host) in order to display the Guest's desktop content (fbcon and compositor's FB data) on the Host locally (Gtk, SDL, etc) or streamed to a remote system

(Spice,

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Vnc, etc). And, the rate at which the UI updates are submitted (to the Host compositor for local UIs) is controlled by the UI timer.

However, in the case of Qemu Gtk UI, the UI timer is used as a backup as we mostly rely on the repaint callback to figure out when to submit updates. This is because the repaint callback is a more reliable

mechanism

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

to determine when it is appropriate to submit an update to the Host compositor.

And, until the UI's update is not submitted (and executed by the Host GPU and signaled via an EGL fence), the Guest's update fence is not signaled. We can reliably achieve 60 FPS this way (assuming the Host's refresh rate is 60) for Guest's display updates.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

Thanks,
Vivek

Quoted 2 lines

[5]

https://elixir.bootlin.com/linux/v6.17.1/source/drivers/gpu/drm/drm_atomi

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

c_helper.c#L2606

Best regards
Thomas

Quoted 1 line

I am not sure how this would affect virgl use-cases but Dmitry can

help

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
Quoted 9 lines

explain if

Quoted 8 lines

the vblank timer would be useful there or not.

Thanks,
Vivek

Quoted 3 lines

devel/[email protected]

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 3 lines
Quoted 3 lines

amprd02.prod.outlook.com/ # [1]

---
    drivers/gpu/drm/virtio/virtgpu_display.c | 29

++++++++++++++++++++++--

Quoted 14 lines
Quoted 14 lines
    1 file changed, 27 insertions(+), 2 deletions(-)
drivers/gpu/drm/virtio/virtgpu_display.c +2 -0
index c3315935d8bc..ee134e46eeba 100644
@@ -32,6 +32,8 @@
    #include <drm/drm_gem_framebuffer_helper.h>
    #include <drm/drm_probe_helper.h>
    #include <drm/drm_simple_kms_helper.h>
+#include <drm/drm_vblank.h>
+#include <drm/drm_vblank_helper.h>
 
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

drivers/gpu/drm/virtio/virtgpu_display.c static const struct drm_crtc_funcs

Quoted 3 lines
drivers/gpu/drm/virtio/virtgpu_display.c +0 -0
    #include "virtgpu_drv.h"
 
@@ -55,6 +57,7 @@ static const struct drm_crtc_funcs

virtio_gpu_crtc_funcs

Quoted 3 lines
Quoted 3 lines
drivers/gpu/drm/virtio/virtgpu_display.c +0 -0
 = {
    	.reset                  = drm_atomic_helper_crtc_reset,
    	.atomic_duplicate_state =

drm_atomic_helper_crtc_duplicate_state,

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/gpu/drm/virtio/virtgpu_display.c static const struct drm_crtc_funcs · line 60

Quoted 1 line
drivers/gpu/drm/virtio/virtgpu_display.c +0 -0
    	.atomic_destroy_state   =

drm_atomic_helper_crtc_destroy_state,

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/gpu/drm/virtio/virtgpu_display.c static void

Quoted 5 lines
drivers/gpu/drm/virtio/virtgpu_display.c +1 -0
+	DRM_CRTC_VBLANK_TIMER_FUNCS,
    };
 
    static const struct drm_framebuffer_funcs virtio_gpu_fb_funcs = {
drivers/gpu/drm/virtio/virtgpu_display.c +0 -0
@@ -99,6 +102,7 @@ static void

virtio_gpu_crtc_mode_set_nofb(struct

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

drivers/gpu/drm/virtio/virtgpu_display.c static void

Quoted 9 lines
drivers/gpu/drm/virtio/virtgpu_display.c +1 -0
 drm_crtc *crtc)
    static void virtio_gpu_crtc_atomic_enable(struct drm_crtc *crtc,
    					  struct drm_atomic_state *state)
    {
+	drm_crtc_vblank_on(crtc);
    }
 

static void virtio_gpu_crtc_atomic_disable(struct drm_crtc *crtc,

drivers/gpu/drm/virtio/virtgpu_display.c +0 -0
@@ -108,6 +112,8 @@ static void

virtio_gpu_crtc_atomic_disable(struct

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/virtio/virtgpu_display.c static void · line 119

Quoted 8 lines
drivers/gpu/drm/virtio/virtgpu_display.c +2 -0
 drm_crtc *crtc,
    	struct virtio_gpu_device *vgdev = dev->dev_private;
    	struct virtio_gpu_output *output =
 drm_crtc_to_virtio_gpu_output(crtc);
 
+	drm_crtc_vblank_off(crtc);
+
    	virtio_gpu_cmd_set_scanout(vgdev, output->index, 0, 0, 0, 0,

0);

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/gpu/drm/virtio/virtgpu_display.c static int

Quoted 3 lines
    	virtio_gpu_notify(vgdev);
    }
drivers/gpu/drm/virtio/virtgpu_display.c +0 -0
@@ -121,9 +127,10 @@ static int

virtio_gpu_crtc_atomic_check(struct

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

drivers/gpu/drm/virtio/virtgpu_display.c static int · line 127

Quoted 7 lines
drivers/gpu/drm/virtio/virtgpu_display.c +0 -2
 drm_crtc *crtc,
    static void virtio_gpu_crtc_atomic_flush(struct drm_crtc *crtc,
    					 struct drm_atomic_state *state)
    {
-	struct drm_crtc_state *crtc_state =
 drm_atomic_get_new_crtc_state(state,
-

crtc);

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/gpu/drm/virtio/virtgpu_display.c static void

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_display.c +2 -0
+	struct drm_device *dev = crtc->dev;
+	struct drm_crtc_state *crtc_state =
 drm_atomic_get_new_crtc_state(state, crtc);
    	struct virtio_gpu_output *output =

drm_crtc_to_virtio_gpu_output(crtc);

drivers/gpu/drm/virtio/virtgpu_display.c +1 -0
+	struct drm_pending_vblank_event *event;
 
    	/*
    	 * virtio-gpu can't do modeset and plane update operations
@@ -133,6 +140,20 @@ static void

virtio_gpu_crtc_atomic_flush(struct

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
drivers/gpu/drm/virtio/virtgpu_display.c +14 -0
 drm_crtc *crtc,
    	 */
    	if (drm_atomic_crtc_needs_modeset(crtc_state))
    		output->needs_modeset = true;
+
+	spin_lock_irq(&dev->event_lock);
+
+	event = crtc_state->event;
+	crtc_state->event = NULL;
+
+	if (event) {
+		if (drm_crtc_vblank_get(crtc) == 0)
+			drm_crtc_arm_vblank_event(crtc, event);
+		else
+			drm_crtc_send_vblank_event(crtc, event);
+	}
+
+	spin_unlock_irq(&dev->event_lock);
    }
 
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

static const struct drm_crtc_helper_funcs

virtio_gpu_crtc_helper_funcs

Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
Quoted 26 lines

= {

Quoted 1 line

drivers/gpu/drm/virtio/virtgpu_display.c int virtio_gpu_modeset_init(struct

Quoted 1 line
drivers/gpu/drm/virtio/virtgpu_display.c +0 -0
@@ -356,6 +377,10 @@ int virtio_gpu_modeset_init(struct

virtio_gpu_device

Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/virtio/virtgpu_display.c +0 -0
 *vgdev)
    	for (i = 0 ; i < vgdev->num_scanouts; ++i)
    		vgdev_output_init(vgdev, i);
 
Quoted 10 lines
Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_display.c +4 -0
+	ret = drm_vblank_init(vgdev->ddev, vgdev->num_scanouts);
+	if (ret)
+		return ret;
+
    	drm_mode_config_reset(vgdev->ddev);
    	return 0;
    }

--
2.51.0

--
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman HRB 36809 (AG Nuernberg)

Quoted 9 lines
Quoted 9 lines

--
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman HRB 36809 (AG Nuernberg)

--
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman HRB 36809 (AG Nuernberg)

author maintainer
#14 lore raw

Hi,

for some reason, Dmitry already merged this patch into drm-misc-next. I therefore sent out another change to pick the correct handling of vblank events depending on the plane's buffer objects. You may want to take a look at [1]. Testing is welcome.

[1] https://lore.kernel.org/dri-devel/[email protected]/T/#u

Best regards
Thomas

Am 15.10.25 um 07:37 schrieb Kasireddy, Vivek:

Quoted 334 lines

Hi Thomas,

Quoted 39 lines

Subject: Re: [PATCH] drm/virtgpu: Use vblank timer

Quoted 9 lines
Quoted 9 lines

Am 10.10.25 um 06:25 schrieb Kasireddy, Vivek:
[...]

Quoted 7 lines
Quoted 6 lines

Because virtgpu did not initialize vblanking, DRM always sent out a vblank event after the completed commit. [5] It's not synchronized to the display output. This means that virtgpu has always been subject to tearing and the guest always required to hold buffers until the host completed its display update. IOW adding vblank timers into the mix should not change the behavior of virtgpu. It just limits the output

As I mentioned, the output or display update frequency is already

limited

Quoted 1 line
Quoted 1 line
Quoted 1 line

when blob=true, so adding a vblank timer would be redundant in this

case.

Quoted 1 line
Quoted 1 line

To summarize my understanding: virtgpu currently generates vblank

events

Quoted 14 lines
Quoted 4 lines

as soon as if finished updating the display. Which can be too fast for blob=false. For blob=true, the display update is (reasonably assumed to be) synchronized with the host update and thereby rate limited by the host's display output.

Yeah, that is mostly correct.

Quoted 8 lines

Adding the vblank timer: For blob=true, if a vblank timeout happens during an ongoing display update, it does not send an event. If the display update finishes before the vblank times out, it arms the vblank event and the next vblank timeout will send it out. The vblank timer thereby makes events appear at regular intervals instead of ASAP.

So why add the complexity of handling blob=true separately when it should integrate well with the existing vblank framework?

Quoted 9 lines

There are a few reasons for this:
- When blob=true, the most important goal is to prevent tearing (i.e, to ensure that Guest VM and Host are not accessing the Guest's FB simultaneously). As explained previously, just ensuring that this goal is met will automatically result in rate limited updates that are in sync with the Host compositor's repaint cycle (if using Gtk UI). So, given this, adding a vblank timer would not help in this case and instead would lead to updates that are no longer in sync with the Host compositor's repaint cycle.

Quoted 3 lines

No that's not the case. With the vblank timer, the output on the host is still synced to the host's repaint cycle. The guest's compositor still waits for it to finish before updating its own output.

My concern is that in this case, the Guest's compositor will be made to wait a bit too long before it starts its repaint cycle because the vblank event was not sent immediately when the Host's update was finished but is instead sent after the next vblank timeout. This is what I meant by the Guest's updates not staying in sync with the Host compositor's repaint cycle because the Guest compositor would not start drawing at the best possible time (which is indicated by the Host compositor via the repaint callback). More on this below.

Quoted 16 lines
Quoted 5 lines

- Qemu (and Qemu UI) also works with virtio-gpu driver that is part of Windows VMs where a vblank timer may or may not be available. So, it is easier to support this case by having a UI timer/repaint cb and making the Guest VM just wait until the update (or resource flush) is completed to ensure synchronization and rate limited updates.

Nothing in this patch affects the Windows driver. We're not going to remove or modify the host's repaint code.

Quoted 5 lines

- Even if a vblank timer is available for a Guest VM (Windows or Linux) its frequency may not match the UI update frequency, so there might either be wasteful updates or too few updates. For example, if the Host's rate is 30 FPS, but the Guest VM's vblank timer is running at 60 FPS, then roughly 30 frames would not be displayed each cycle IIUC.

If the host currently updates with only 30 Hz, it already does not show those additional 30 frames.

Quoted 8 lines

The userspace compositor only sends the next frame when the current frame has been completed; as signalled by the vblank event. That vblank event only happens after the previous display update completed. (It should have rather been called page-flip event instead.) If the host only updates at 30 Hz, there are currently at most 30 such events per second.  If we add a vblank timer, it'll fire at the guest display refresh rate (say 60 Hz), but only send these events when the previous update has been completed; which is 30 Hz.

Ok, as long as the Host's refresh rate is communicated with virtio-gpu, which is then used as the input to determine the vblank timer frequency, this should work. However, as mentioned above, I am still concerned about when the vblank event would be sent. For example, let us assume that we have 60 Hz as the Guest's refresh rate, which means the vblank event/ interrupt is expected to happen roughly every 16.66 ms. So, IIUC, we have:
0................16....................33................50...........

My specific concern is that if the Guest gets notified that the Host update is finished at let's say 18 ms mark above, I think the vblank event would not get sent out right away and instead would be sent out at the 33 ms mark right? If yes, I wonder if this pattern would continue to repeat itself impacting performance?

Quoted 6 lines
Quoted 6 lines
Quoted 3 lines

Please also note that it's not just about compositors. DRM's fbcon synchronizes its output to the vblank period. If no vblank is supported, it'll happily fire out display updates ASAP even for blob=true.

IIUC, the fbcon updates would still be rate limited in the blob=true case as the vblank event would only be sent out after the atomic/plane update is finished right?

Quoted 8 lines

That's not the case. The DRM fbcon/fbdev code waits for a vblank timeout/interrust to occur [1]  but does NOT wait for the vblank event. To do so, it enables vblanks interrupts, waits for one to occur and then continues. [2] If the driver does not support vblank interrupts, it continues right away without waiting. [3]  The fbcon/fbdev support only uses a single buffer object for a its output. Hence this is where you actually get tearing even with blob=true. None of this can be changed much because of the way fbdev works.

I have not looked into fbdev/fbcon closely but I did test Gnome + Xorg which uses the dirtyfb() method with a single FB for updates (instead of page flips with double buffering) when there is no hardware cursor available. And, AFAICT, the dirtyfb() method works with no tearing when blob=true because it just invokes the atomic commit (drm_atomic_commit) in a blocking manner, which means it just waits until the Host update is finished. I thought fbdev/fbcon worked in a similar way.

Thanks,
Vivek

Quoted 218 lines

[1]
https://gitlab.freedesktop.org/drm/misc/kernel/-/blob/drm-misc-next-2025-
10-02/drivers/gpu/drm/drm_fb_helper.c?ref_type=tags#L372
[2]
https://gitlab.freedesktop.org/drm/misc/kernel/-/blob/drm-misc-next-2025-
10-02/drivers/gpu/drm/drm_client_modeset.c?ref_type=tags#L1322
[3]
https://gitlab.freedesktop.org/drm/misc/kernel/-/blob/drm-misc-next-2025-
10-02/drivers/gpu/drm/drm_vblank.c?ref_type=tags#L1215

Best regards
Thomas

Quoted 8 lines

Thanks,
Vivek

Quoted 5 lines

Best regards
Thomas

Quoted 2 lines

Note that blob=true is a performance optimization where we create a dmabuf (on the Host) backed by Guest FB's memory that is shared with

the

Quoted 1 line
Quoted 1 line
Quoted 1 line

UI layer. And, AFAIK, the only case where virtio-gpu updates are not

limited

Quoted 1 line
Quoted 1 line
Quoted 1 line

is when blob=false. Even in this case, there would be no tearing issues

seen

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

because the Guest is made to wait until the Host makes a copy of its FB.

Quoted 5 lines

frequency. If GNOME's pageflip is synchronized to the vblank, it should immediately benefit.

The GTK repaint callback is an interface for applications. How does it affect, or is affected by, any of this?

So, virtio-gpu is almost always coupled with a UI (on the Host) in order to display the Guest's desktop content (fbcon and compositor's FB data) on the Host locally (Gtk, SDL, etc) or streamed to a remote system

(Spice,

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Vnc, etc). And, the rate at which the UI updates are submitted (to the Host compositor for local UIs) is controlled by the UI timer.

However, in the case of Qemu Gtk UI, the UI timer is used as a backup as we mostly rely on the repaint callback to figure out when to submit updates. This is because the repaint callback is a more reliable

mechanism

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

to determine when it is appropriate to submit an update to the Host compositor.

And, until the UI's update is not submitted (and executed by the Host GPU and signaled via an EGL fence), the Guest's update fence is not signaled. We can reliably achieve 60 FPS this way (assuming the Host's refresh rate is 60) for Guest's display updates.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Thanks,
Vivek

Quoted 2 lines

[5]

https://elixir.bootlin.com/linux/v6.17.1/source/drivers/gpu/drm/drm_atomi

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

c_helper.c#L2606

Best regards
Thomas

Quoted 1 line

I am not sure how this would affect virgl use-cases but Dmitry can

help

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

explain if

Quoted 8 lines

the vblank timer would be useful there or not.

Thanks,
Vivek

Quoted 3 lines

devel/[email protected]

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 3 lines
Quoted 3 lines

amprd02.prod.outlook.com/ # [1]

---
     drivers/gpu/drm/virtio/virtgpu_display.c | 29

++++++++++++++++++++++--

Quoted 14 lines
Quoted 14 lines
     1 file changed, 27 insertions(+), 2 deletions(-)
drivers/gpu/drm/virtio/virtgpu_display.c +2 -0
index c3315935d8bc..ee134e46eeba 100644
@@ -32,6 +32,8 @@
     #include <drm/drm_gem_framebuffer_helper.h>
     #include <drm/drm_probe_helper.h>
     #include <drm/drm_simple_kms_helper.h>
+#include <drm/drm_vblank.h>
+#include <drm/drm_vblank_helper.h>
 
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

drivers/gpu/drm/virtio/virtgpu_display.c static const struct drm_crtc_funcs

Quoted 3 lines
drivers/gpu/drm/virtio/virtgpu_display.c +0 -0
     #include "virtgpu_drv.h"
 
@@ -55,6 +57,7 @@ static const struct drm_crtc_funcs

virtio_gpu_crtc_funcs

Quoted 3 lines
Quoted 3 lines
drivers/gpu/drm/virtio/virtgpu_display.c +0 -0
 = {
     	.reset                  = drm_atomic_helper_crtc_reset,
     	.atomic_duplicate_state =

drm_atomic_helper_crtc_duplicate_state,

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/gpu/drm/virtio/virtgpu_display.c static const struct drm_crtc_funcs · line 60

Quoted 1 line
drivers/gpu/drm/virtio/virtgpu_display.c +0 -0
     	.atomic_destroy_state   =

drm_atomic_helper_crtc_destroy_state,

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/gpu/drm/virtio/virtgpu_display.c static void

Quoted 5 lines
drivers/gpu/drm/virtio/virtgpu_display.c +1 -0
+	DRM_CRTC_VBLANK_TIMER_FUNCS,
     };
 
     static const struct drm_framebuffer_funcs virtio_gpu_fb_funcs = {
drivers/gpu/drm/virtio/virtgpu_display.c +0 -0
@@ -99,6 +102,7 @@ static void

virtio_gpu_crtc_mode_set_nofb(struct

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

drivers/gpu/drm/virtio/virtgpu_display.c static void

Quoted 9 lines
drivers/gpu/drm/virtio/virtgpu_display.c +1 -0
 drm_crtc *crtc)
     static void virtio_gpu_crtc_atomic_enable(struct drm_crtc *crtc,
     					  struct drm_atomic_state *state)
     {
+	drm_crtc_vblank_on(crtc);
     }
 

static void virtio_gpu_crtc_atomic_disable(struct drm_crtc *crtc,

drivers/gpu/drm/virtio/virtgpu_display.c +0 -0
@@ -108,6 +112,8 @@ static void

virtio_gpu_crtc_atomic_disable(struct

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/virtio/virtgpu_display.c static void · line 119

Quoted 8 lines
drivers/gpu/drm/virtio/virtgpu_display.c +2 -0
 drm_crtc *crtc,
     	struct virtio_gpu_device *vgdev = dev->dev_private;
     	struct virtio_gpu_output *output =
 drm_crtc_to_virtio_gpu_output(crtc);
 
+	drm_crtc_vblank_off(crtc);
+
     	virtio_gpu_cmd_set_scanout(vgdev, output->index, 0, 0, 0, 0,

0);

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/gpu/drm/virtio/virtgpu_display.c static int

Quoted 3 lines
     	virtio_gpu_notify(vgdev);
     }
drivers/gpu/drm/virtio/virtgpu_display.c +0 -0
@@ -121,9 +127,10 @@ static int

virtio_gpu_crtc_atomic_check(struct

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

drivers/gpu/drm/virtio/virtgpu_display.c static int · line 127

Quoted 7 lines
drivers/gpu/drm/virtio/virtgpu_display.c +0 -2
 drm_crtc *crtc,
     static void virtio_gpu_crtc_atomic_flush(struct drm_crtc *crtc,
     					 struct drm_atomic_state *state)
     {
-	struct drm_crtc_state *crtc_state =
 drm_atomic_get_new_crtc_state(state,
-

crtc);

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/gpu/drm/virtio/virtgpu_display.c static void

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_display.c +2 -0
+	struct drm_device *dev = crtc->dev;
+	struct drm_crtc_state *crtc_state =
 drm_atomic_get_new_crtc_state(state, crtc);
     	struct virtio_gpu_output *output =

drm_crtc_to_virtio_gpu_output(crtc);

drivers/gpu/drm/virtio/virtgpu_display.c +1 -0
+	struct drm_pending_vblank_event *event;
 
     	/*
     	 * virtio-gpu can't do modeset and plane update operations
@@ -133,6 +140,20 @@ static void

virtio_gpu_crtc_atomic_flush(struct

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
drivers/gpu/drm/virtio/virtgpu_display.c +14 -0
 drm_crtc *crtc,
     	 */
     	if (drm_atomic_crtc_needs_modeset(crtc_state))
     		output->needs_modeset = true;
+
+	spin_lock_irq(&dev->event_lock);
+
+	event = crtc_state->event;
+	crtc_state->event = NULL;
+
+	if (event) {
+		if (drm_crtc_vblank_get(crtc) == 0)
+			drm_crtc_arm_vblank_event(crtc, event);
+		else
+			drm_crtc_send_vblank_event(crtc, event);
+	}
+
+	spin_unlock_irq(&dev->event_lock);
     }
 
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

static const struct drm_crtc_helper_funcs

virtio_gpu_crtc_helper_funcs

Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
Quoted 26 lines

= {

Quoted 1 line

drivers/gpu/drm/virtio/virtgpu_display.c int virtio_gpu_modeset_init(struct

Quoted 1 line
drivers/gpu/drm/virtio/virtgpu_display.c +0 -0
@@ -356,6 +377,10 @@ int virtio_gpu_modeset_init(struct

virtio_gpu_device

Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/virtio/virtgpu_display.c +0 -0
 *vgdev)
     	for (i = 0 ; i < vgdev->num_scanouts; ++i)
     		vgdev_output_init(vgdev, i);
 
Quoted 10 lines
Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_display.c +4 -0
+	ret = drm_vblank_init(vgdev->ddev, vgdev->num_scanouts);
+	if (ret)
+		return ret;
+
     	drm_mode_config_reset(vgdev->ddev);
     	return 0;
     }

--
2.51.0

--
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman HRB 36809 (AG Nuernberg)

Quoted 9 lines
Quoted 9 lines

--
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman HRB 36809 (AG Nuernberg)

--
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman HRB 36809 (AG Nuernberg)

Signature · 7 lines
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman
HRB 36809 (AG Nuernberg)
maintainer
#15 lore raw
Quoted 9 lines · Thomas Zimmermann

Hi,

for some reason, Dmitry already merged this patch into drm-misc-next. I therefore sent out another change to pick the correct handling of vblank events depending on the plane's buffer objects. You may want to take a look at [1]. Testing is welcome.

[1] https://lore.kernel.org/dri-devel/20251016145230.79270-1-
[email protected]/T/#u

This patch fixes a major performance problem with 3d contexts on QEMU that I was coincidentally investigating at the same time when you posted the patch. Thanks a lot for fixing the vblank issue. Will test the updated version.

Signature · 2 lines
Best regards,
Dmitry