Skip to contentSkip to conversation

drm/vblank: Stop drm_crtc_wait_one_vblank() when vblank is turned off

PATCH In review Prike Liang 1 participant 4 messages Nov 26

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/3 drm/vblank: Stop drm_crtc_wait_one_vblank() when vblank is turned off

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() only checks 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() 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 or time out behave as before.

The two cases differ in that drm_crtc_wait_one_vblank() warns "vblank not available" if drm_vblank_get() fails, but doesn't warn if drm_crtc_vblank_off() ends the wait. That is intentional: it's a failure to call the function while vblank is off, but a waiter, such as the fbdev damage worker, can't keep the CRTC from being turned off meanwhile. The error only tells the caller that no more vblanks are coming.

Fixes e8450f51a4b3 ("drm/irq: Implement a generic vblank_wait function")
Cc Thomas Zimmermann <[email protected]>
Cc Ville Syrjälä <[email protected]>
Assisted-by Copilot:claude-opus-5.5
---
 drivers/gpu/drm/drm_vblank.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/gpu/drm/drm_vblank.c +13 -3
index f90fb2d13e42..02abed5324ce 100644
@@ -1303,7 +1303,9 @@ EXPORT_SYMBOL(drm_crtc_vblank_put);
  *
  * 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 terminates
+ * early with an error if drm_crtc_vblank_off() disables the vblank irq before
+ * a vblank passes.
  *
  * Returns: 0 on success, negative error on failures.
  */
@@ -1323,14 +1325,22 @@ int drm_crtc_wait_one_vblank(struct drm_crtc *crtc)
 	last = drm_vblank_count(dev, pipe);
 
 	ret = wait_event_timeout(vblank->queue,
-				 last != drm_vblank_count(dev, pipe),
+				 last != drm_vblank_count(dev, pipe) ||
+				 !READ_ONCE(vblank->enabled),
 				 msecs_to_jiffies(1000));
 
 	drm_WARN(dev, ret == 0, "vblank wait timed out on crtc %i\n", pipe);
 
+	if (!ret)
+		ret = -ETIMEDOUT;
+	else if (last == drm_vblank_count(dev, pipe))
+		ret = -EINVAL; /* drm_crtc_vblank_off() ended the wait */
+	else
+		ret = 0;
+
 	drm_vblank_put(dev, pipe);
 
-	return ret ? 0 : -ETIMEDOUT;
+	return ret;
 }
 EXPORT_SYMBOL(drm_crtc_wait_one_vblank);
 
Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/3 drm/vblank: Add drm_crtc_wait_one_vblank_interruptible()

drm_crtc_wait_one_vblank() waits uninterruptibly and only gives up after one second. That doesn't suit a wait that userspace asks for, such as
FBIO_WAITFORVSYNC: if no vblank interrupt arrives, the task sleeps in D state for the whole second and can't be interrupted, not even by SIGKILL.

Add drm_crtc_wait_one_vblank_interruptible(), a DRM-internal variant of drm_crtc_wait_one_vblank() that a signal can interrupt. It returns -ERESTARTSYS in that case and otherwise behaves like drm_crtc_wait_one_vblank(), which includes ending the wait when vblank is turned off. Move the wait to __drm_crtc_wait_one_vblank(), which implements both functions.

drm_crtc_wait_one_vblank() doesn't change, and drivers keep using it. The new function has no user yet. The next patch uses it in drm_client_modeset_wait_for_vblank().

Cc Thomas Zimmermann <[email protected]>
Cc Ville Syrjälä <[email protected]>
Assisted-by Copilot:claude-opus-5.5
---
 drivers/gpu/drm/drm_internal.h |  1 +
 drivers/gpu/drm/drm_vblank.c   | 66 ++++++++++++++++++++++++----------
 2 files changed, 48 insertions(+), 19 deletions(-)
drivers/gpu/drm/drm_internal.h +1 -0
index 815486557f51..3f2120e8e010 100644
@@ -128,6 +128,7 @@ void drm_vblank_disable_and_save(struct drm_device *dev, unsigned int pipe);
 int drm_vblank_get(struct drm_device *dev, unsigned int pipe);
 void drm_vblank_put(struct drm_device *dev, unsigned int pipe);
 u64 drm_vblank_count(struct drm_device *dev, unsigned int pipe);
+int drm_crtc_wait_one_vblank_interruptible(struct drm_crtc *crtc);
 
 /* drm_vblank_work.c */
 static inline void drm_vblank_flush_worker(struct drm_vblank_crtc *vblank)
drivers/gpu/drm/drm_vblank.c +47 -19

92 lines not on this page · raw message

Signature · 1 line
2.34.1
#2 lore raw

PATCH 3/3 drm/client: Make the FBIO_WAITFORVSYNC vblank wait interruptible

In reply to Prike Liang

FBIO_WAITFORVSYNC calls drm_client_modeset_wait_for_vblank(), which waits with drm_crtc_wait_one_vblank(). That wait is uninterruptible and only gives up after one second. If no vblank interrupt arrives, for example while the driver resets the display hardware, the caller sleeps in D state for the whole second and can't be interrupted, not even by SIGKILL.

DRM_IOCTL_WAIT_VBLANK waits interruptibly, and so do fbdev drivers that implement FBIO_WAITFORVSYNC themselves, such as atyfb and matroxfb.

Use drm_crtc_wait_one_vblank_interruptible() in drm_client_modeset_wait_for_vblank(). Return -ERESTARTSYS when a signal interrupts the wait, so that the ioctl returns to userspace or is restarted. Timeouts and disabled CRTCs are still not reported.

The fbdev damage worker also calls drm_client_modeset_wait_for_vblank(), but it runs in a kernel thread, which doesn't receive signals, so its wait doesn't change.

Cc Thomas Zimmermann <[email protected]>
Cc Ville Syrjälä <[email protected]>
Assisted-by Copilot:claude-opus-5.5
---
 drivers/gpu/drm/drm_client_modeset.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/gpu/drm/drm_client_modeset.c +6 -4
index 67f90e5d1512..122ca1bf98e6 100644
@@ -1301,10 +1301,11 @@ EXPORT_SYMBOL(drm_client_modeset_dpms);
  *
  * Block the caller until the given CRTC has seen a VBLANK. Do nothing
  * if the CRTC is disabled. If there's another DRM master present, fail
- * with -EBUSY.
+ * with -EBUSY. A signal interrupts the wait.
  *
  * Returns:
- * 0 on success, or negative error code otherwise.
+ * 0 on success, -ERESTARTSYS if a signal interrupted the wait, or
+ * negative error code otherwise.
  */
 int drm_client_modeset_wait_for_vblank(struct drm_client_dev *client, unsigned int crtc_index)
 {
@@ -1328,13 +1329,14 @@ int drm_client_modeset_wait_for_vblank(struct drm_client_dev *client, unsigned i
 	 */
 	ret = drm_crtc_vblank_get(crtc);
 	if (!ret) {
-		drm_crtc_wait_one_vblank(crtc);
+		ret = drm_crtc_wait_one_vblank_interruptible(crtc);
 		drm_crtc_vblank_put(crtc);
 	}
 
 	drm_master_internal_release(dev);
 
-	return 0;
+	/* Only report interruption by a signal, not timeouts. */
+	return ret == -ERESTARTSYS ? ret : 0;
 }
 EXPORT_SYMBOL(drm_client_modeset_wait_for_vblank);
 
Signature · 1 line
2.34.1
#3 lore raw

RE: [PATCH 3/3] drm/client: Make the FBIO_WAITFORVSYNC vblank wait interruptible

AMD General

This patch series addresses the vblank timeout issue when vblank is off, as well as cases interrupted by userspace signals. If there are no further comments, could someone please help pick these up for drm-misc-next? Thanks!

Regards,
Prike

Quoted 75 lines

-----Original Message-----
From: Liang, Prike <[email protected]>
Sent: Thursday, November 26, 2026 4:55 PM
To: [email protected]; [email protected]
Cc: [email protected]; Liang, Prike <[email protected]>; Ville Syrjälä <[email protected]>
Subject: [PATCH 3/3] drm/client: Make the FBIO_WAITFORVSYNC vblank wait interruptible

FBIO_WAITFORVSYNC calls drm_client_modeset_wait_for_vblank(), which waits with drm_crtc_wait_one_vblank(). That wait is uninterruptible and only gives up after one second. If no vblank interrupt arrives, for example while the driver resets the display hardware, the caller sleeps in D state for the whole second and can't be interrupted, not even by SIGKILL.

DRM_IOCTL_WAIT_VBLANK waits interruptibly, and so do fbdev drivers that implement FBIO_WAITFORVSYNC themselves, such as atyfb and matroxfb.

Use drm_crtc_wait_one_vblank_interruptible() in drm_client_modeset_wait_for_vblank(). Return -ERESTARTSYS when a signal interrupts the wait, so that the ioctl returns to userspace or is restarted. Timeouts and disabled CRTCs are still not reported.

The fbdev damage worker also calls drm_client_modeset_wait_for_vblank(), but it runs in a kernel thread, which doesn't receive signals, so its wait doesn't change.

Cc Thomas Zimmermann <[email protected]>
Cc Ville Syrjälä <[email protected]>
Assisted-by Copilot:claude-opus-5.5
---
 drivers/gpu/drm/drm_client_modeset.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/gpu/drm/drm_client_modeset.c +3 -2
index 67f90e5d1512..122ca1bf98e6 100644
@@ -1301,10 +1301,11 @@ EXPORT_SYMBOL(drm_client_modeset_dpms);
  *
  * Block the caller until the given CRTC has seen a VBLANK. Do nothing
  * if the CRTC is disabled. If there's another DRM master present, fail
- * with -EBUSY.
+ * with -EBUSY. A signal interrupts the wait.
  *
  * Returns:
- * 0 on success, or negative error code otherwise.
+ * 0 on success, -ERESTARTSYS if a signal interrupted the wait, or
+ * negative error code otherwise.
  */
 int drm_client_modeset_wait_for_vblank(struct drm_client_dev *client, unsigned int

crtc_index) { @@ -1328,13 +1329,14 @@ int drm_client_modeset_wait_for_vblank(struct drm_client_dev *client, unsigned i

       */
      ret = drm_crtc_vblank_get(crtc);
      if (!ret) {
drivers/gpu/drm/drm_client_modeset.c +3 -3
-             drm_crtc_wait_one_vblank(crtc);
+             ret = drm_crtc_wait_one_vblank_interruptible(crtc);
              drm_crtc_vblank_put(crtc);
      }
 
      drm_master_internal_release(dev);
 
-     return 0;
+     /* Only report interruption by a signal, not timeouts. */
+     return ret == -ERESTARTSYS ? ret : 0;
 }
 EXPORT_SYMBOL(drm_client_modeset_wait_for_vblank);
 
--

2.34.1