drm_crtc_vblank_off() turns off the vblank interrupt even while someone
holds a vblank reference and waits for a vblank. It then wakes up the
vblank wait queue. DRM_IOCTL_WAIT_VBLANK stops waiting at this point,
because its wait condition also checks whether vblank is still enabled.

drm_crtc_wait_one_vblank() and drm_crtc_wait_one_vblank_interruptible()
only check whether the vblank count has changed. Once vblank is off, the
count doesn't change, so the woken waiter sleeps again until the wait
times out after one second and warns "vblank wait timed out". This
happens, for example, when the fbdev damage worker or FBIO_WAITFORVSYNC
waits for a vblank while the driver turns the CRTC off for suspend or a
GPU reset.

Make __drm_crtc_wait_one_vblank(), which implements both functions, also
stop waiting when vblank is turned off, and return -EINVAL if no vblank
passed, the same error that drm_vblank_get() returns when vblank is off.
The wait condition is checked before going to sleep, so this also covers
vblank being turned off between drm_vblank_get() and the wait. Waits
that see a vblank, time out or are interrupted behave as before.

Fixes: e8450f51a4b3 ("drm/irq: Implement a generic vblank_wait function")
Cc: Thomas Zimmermann <[email protected]>
Assisted-by: Copilot:claude-opus-5.5
Signed-off-by: Prike Liang <[email protected]>
---
 drivers/gpu/drm/drm_vblank.c | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)

diff --git a/drivers/gpu/drm/drm_vblank.c b/drivers/gpu/drm/drm_vblank.c
index c3c9afc08ed2..5e247663978a 100644
--- a/drivers/gpu/drm/drm_vblank.c
+++ b/drivers/gpu/drm/drm_vblank.c
@@ -1315,21 +1315,29 @@ static int __drm_crtc_wait_one_vblank(struct drm_crtc 
*crtc, bool interruptible)
 
        if (interruptible)
                timeout = wait_event_interruptible_timeout(vblank->queue,
-                                                          last != 
drm_vblank_count(dev, pipe),
+                                                          last != 
drm_vblank_count(dev, pipe) ||
+                                                          
!READ_ONCE(vblank->enabled),
                                                           timeout);
        else
                timeout = wait_event_timeout(vblank->queue,
-                                            last != drm_vblank_count(dev, 
pipe),
+                                            last != drm_vblank_count(dev, 
pipe) ||
+                                            !READ_ONCE(vblank->enabled),
                                             timeout);
 
        drm_WARN(dev, timeout == 0, "vblank wait timed out on crtc %i\n", pipe);
 
-       drm_vblank_put(dev, pipe);
-
        if (timeout < 0)
-               return timeout;
+               ret = timeout;
+       else if (!timeout)
+               ret = -ETIMEDOUT;
+       else if (last == drm_vblank_count(dev, pipe))
+               ret = -EINVAL; /* drm_crtc_vblank_off() ended the wait */
+       else
+               ret = 0;
 
-       return timeout ? 0 : -ETIMEDOUT;
+       drm_vblank_put(dev, pipe);
+
+       return ret;
 }
 
 /**
@@ -1338,7 +1346,8 @@ static int __drm_crtc_wait_one_vblank(struct drm_crtc 
*crtc, bool interruptible)
  *
  * This waits for one vblank to pass on @crtc, using the irq driver interfaces.
  * It is a failure to call this when the vblank irq for @crtc is disabled, e.g.
- * due to lack of driver support or because the crtc is off.
+ * due to lack of driver support or because the crtc is off. The wait also 
fails
+ * if drm_crtc_vblank_off() disables the vblank irq before a vblank passes.
  *
  * Returns: 0 on success, negative error on failures.
  */
-- 
2.34.1

Reply via email to