Skip to contentSkip to conversation

drm/fb_helper: Determine proper crtc_index within drm_fb_helper_fb_dirty

PATCH In review H. Nikolaus Schaller 4 participants 12 messages Jun 18 – Jun 23

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

PATCH 1/1 drm/fb_helper: Determine proper crtc_index within drm_fb_helper_fb_dirty

The call to drm_client_modeset_wait_for_vblank() currently hardcodes the crtc_index to 0. This is incorrect when CRTC 0 is inactive and not generating vblank interrupts.

When this occurs, the console is flooded with vblank timeout warnings:

[   77.201354] WARNING: drivers/gpu/drm/drm_vblank.c:1320 at drm_crtc_wait_one_vblank+0x194/0x1cc [drm], CPU#1: kworker/1:7/1867
[   77.201354] omapdrm omapdrm.0: [drm] vblank wait timed out on crtc 0

This behavior was observed on a PandaBoard-ES when only the HDMI port (CRTC 1) is connected, while the LCD (CRTC 0) and DVI port (CRTC 2) remain disconnected. The root cause is the hardcoded crtc_index = 0.

Fix this by locating the matching CRTCs. If multiple CRTCs match, the last active one is selected. If no matching CRTC is found, the vblank wait is skipped entirely.

Fixes d8c4bddcd8bcb ("drm/fb-helper: Synchronize dirty worker with vblank")
---
 drivers/gpu/drm/drm_fb_helper.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_fb_helper.c +15 -1
index 1541fc8a9ac2d..70999cdc98b32 100644
@@ -232,9 +232,23 @@ static void drm_fb_helper_fb_dirty(struct drm_fb_helper *helper)
 	struct drm_clip_rect clip_copy;
 	unsigned long flags;
 	int ret;
+	unsigned int crtc_index = 0;
+	struct drm_crtc *crtc;
 
 	mutex_lock(&helper->lock);
-	drm_client_modeset_wait_for_vblank(&helper->client, 0);
+
+	drm_for_each_crtc(crtc, dev) {
+		if (crtc->primary && crtc->primary->state &&
+		    crtc->primary->state->fb == helper->fb &&
+		    crtc->state && crtc->state->active) {
+			crtc_index = drm_crtc_index(crtc) + 1;
+			break;
+		}
+	}
+
+	if (crtc_index > 0)
+		drm_client_modeset_wait_for_vblank(&helper->client, crtc_index - 1);
+
 	mutex_unlock(&helper->lock);
 
 	if (drm_WARN_ON_ONCE(dev, !helper->funcs->fb_dirty))
Signature · 1 line
2.50.1 (Apple Git-155)
#1 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] Lockless access to `crtc->state` and `crtc->primary->state` can lead to a Use-After-Free due to…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Lockless access to crtc->state and crtc->primary->state can lead to a Use-After-Free due to concurrent atomic commits.
- [High] The patch breaks vblank synchronization for legacy (non-atomic) DRM drivers.
- [Medium] The logic selects the first matching CRTC instead of the last active one, contradicting the commit message. --

commit 0337b26b606b4d905d72ea2be31bcb984fe62c6e
Author: H. Nikolaus Schaller <[email protected]>

drm/fb_helper: Determine proper crtc_index within drm_fb_helper_fb_dirty

This commit updates the dirty framebuffer worker to search for a matching active CRTC instead of hardcoding the wait for vblank to CRTC index 0. It aims to avoid vblank timeouts when CRTC 0 is inactive.

Quoted 3 lines
drivers/gpu/drm/drm_fb_helper.c +0 -0

[ ... ]

Quoted 4 lines
drivers/gpu/drm/drm_fb_helper.c +0 -0
@@ -232,9 +232,23 @@ static void drm_fb_helper_fb_dirty(struct drm_fb_helper *helper)
 	struct drm_clip_rect clip_copy;
 	unsigned long flags;
 	int ret;

drivers/gpu/drm/drm_fb_helper.c static void drm_fb_helper_fb_dirty(struct drm_fb_helper *helper) · line 243

Quoted 10 lines
drivers/gpu/drm/drm_fb_helper.c +7 -1
+	unsigned int crtc_index = 0;
+	struct drm_crtc *crtc;
 
 	mutex_lock(&helper->lock);
-	drm_client_modeset_wait_for_vblank(&helper->client, 0);
+
+	drm_for_each_crtc(crtc, dev) {
+		if (crtc->primary && crtc->primary->state &&
+		    crtc->primary->state->fb == helper->fb &&
+		    crtc->state && crtc->state->active) {

[Severity: High]
Can this lockless access to crtc->state and crtc->primary->state result in a use-after-free?

The drm_fb_helper_fb_dirty() function holds helper->lock, but does not hold the DRM modeset locks required to protect these state pointers. During a concurrent atomic modeset commit, the DRM core allocates new state objects, swaps them into place, and frees the old objects via kfree().

If this code reads an old state pointer and gets preempted, could it then dereference the pointer after it has been freed by the concurrent commit?

[Severity: High]
Does this break vblank synchronization for legacy non-atomic DRM drivers?

Legacy drivers do not use the atomic modesetting infrastructure, so their crtc->state and crtc->primary->state pointers remain NULL. Since the condition above requires these pointers to be non-NULL, the loop will skip the vblank wait entirely for legacy drivers. This skips the intended rate-limiting behavior and could lead to unbounded damage worker execution.

drivers/gpu/drm/drm_fb_helper.c static void drm_fb_helper_fb_dirty(struct drm_fb_helper *helper) · line 245

Quoted 2 lines
drivers/gpu/drm/drm_fb_helper.c +2 -0
+			crtc_index = drm_crtc_index(crtc) + 1;
+			break;

[Severity: Medium]
The commit message states "If multiple CRTCs match, the last active one is selected." Does the break statement here cause the first active CRTC to be selected instead, bypassing the remaining matches in the list?

Quoted 7 lines
drivers/gpu/drm/drm_fb_helper.c +6 -0
+		}
+	}
+
+	if (crtc_index > 0)
+		drm_client_modeset_wait_for_vblank(&helper->client, crtc_index - 1);
+
 	mutex_unlock(&helper->lock);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/5118b37e579cec220f2d2b6ab0bb1ecfb67f0d5c.1781803617.git.hns@goldelico.com?part=1
#2 lore raw
Quoted 30 lines

Am 18.06.2026 um 19:43 schrieb [email protected]:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Lockless access to crtc->state and crtc->primary->state can lead to a Use-After-Free due to concurrent atomic commits.
- [High] The patch breaks vblank synchronization for legacy (non-atomic) DRM drivers.
- [Medium] The logic selects the first matching CRTC instead of the last active one, contradicting the commit message. --

commit 0337b26b606b4d905d72ea2be31bcb984fe62c6e
Author: H. Nikolaus Schaller <[email protected]>

drm/fb_helper: Determine proper crtc_index within drm_fb_helper_fb_dirty

This commit updates the dirty framebuffer worker to search for a matching active CRTC instead of hardcoding the wait for vblank to CRTC index 0. It aims to avoid vblank timeouts when CRTC 0 is inactive.

Quoted 3 lines
drivers/gpu/drm/drm_fb_helper.c +0 -0

[ ... ]

Quoted 7 lines
drivers/gpu/drm/drm_fb_helper.c +2 -0
@@ -232,9 +232,23 @@ static void drm_fb_helper_fb_dirty(struct drm_fb_helper *helper)
 struct drm_clip_rect clip_copy;
 unsigned long flags;
 int ret;
+ unsigned int crtc_index = 0;
+ struct drm_crtc *crtc;
 
Quoted 11 lines

drivers/gpu/drm/drm_fb_helper.c static void drm_fb_helper_fb_dirty(struct drm_fb_helper *helper) · line 243

Quoted 7 lines
drivers/gpu/drm/drm_fb_helper.c +5 -1
 mutex_lock(&helper->lock);
- drm_client_modeset_wait_for_vblank(&helper->client, 0);
+
+ drm_for_each_crtc(crtc, dev) {
+ if (crtc->primary && crtc->primary->state &&
+    crtc->primary->state->fb == helper->fb &&
+    crtc->state && crtc->state->active) {

[Severity: High]
Can this lockless access to crtc->state and crtc->primary->state result in a use-after-free?

No idea...

Quoted 3 lines

The drm_fb_helper_fb_dirty() function holds helper->lock, but does not hold the DRM modeset locks required to protect these state pointers. During a

Which locks are exactly recommended to add? What is their name/pointer?

Quoted 10 lines

concurrent atomic modeset commit, the DRM core allocates new state objects, swaps them into place, and frees the old objects via kfree().

If this code reads an old state pointer and gets preempted, could it then dereference the pointer after it has been freed by the concurrent commit?

[Severity: High]
Does this break vblank synchronization for legacy non-atomic DRM drivers?

Quoted 5 lines

Legacy drivers do not use the atomic modesetting infrastructure, so their crtc->state and crtc->primary->state pointers remain NULL. Since the condition above requires these pointers to be non-NULL, the loop will skip the vblank wait entirely for legacy drivers. This skips the intended rate-limiting behavior and could lead to unbounded damage worker execution.

What is the solution? And is this important.

Before d8c4bddcd8bcb ("drm/fb-helper: Synchronize dirty worker with vblank")

there was also no rate-limiting behavior.

Quoted 8 lines

drivers/gpu/drm/drm_fb_helper.c static void drm_fb_helper_fb_dirty(struct drm_fb_helper *helper) · line 245

Quoted 3 lines
drivers/gpu/drm/drm_fb_helper.c +2 -0
+ crtc_index = drm_crtc_index(crtc) + 1;
+ break;

[Severity: Medium]
The commit message states "If multiple CRTCs match, the last active one is selected." Does the break statement here cause the first active CRTC to be selected instead, bypassing the remaining matches in the list?

Yes, that is correct. The description is indeed wrong.

Quoted 11 lines
Quoted 8 lines
drivers/gpu/drm/drm_fb_helper.c +6 -0
+ }
+ }
+
+ if (crtc_index > 0)
+ drm_client_modeset_wait_for_vblank(&helper->client, crtc_index - 1);
+
 mutex_unlock(&helper->lock);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/5118b37e579cec220f2d2b6ab0bb1ecfb67f0d5c.1781803617.git.hns@goldelico.com?part=1
#3 lore raw

(cc Icenowy)

Hi,

there are a number of similar reports. Does the attached patch fix the problem. It picks up what you do, but in a more correct way.

Best regards
Thomas

Am 18.06.26 um 19:56 schrieb H. Nikolaus Schaller:

Quoted 81 lines
Quoted 29 lines

Am 18.06.2026 um 19:43 schrieb [email protected]:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Lockless access to crtc->state and crtc->primary->state can lead to a Use-After-Free due to concurrent atomic commits.
- [High] The patch breaks vblank synchronization for legacy (non-atomic) DRM drivers.
- [Medium] The logic selects the first matching CRTC instead of the last active one, contradicting the commit message. --

commit 0337b26b606b4d905d72ea2be31bcb984fe62c6e
Author: H. Nikolaus Schaller <[email protected]>

drm/fb_helper: Determine proper crtc_index within drm_fb_helper_fb_dirty

This commit updates the dirty framebuffer worker to search for a matching active CRTC instead of hardcoding the wait for vblank to CRTC index 0. It aims to avoid vblank timeouts when CRTC 0 is inactive.

Quoted 3 lines
drivers/gpu/drm/drm_fb_helper.c +0 -0

[ ... ]

Quoted 7 lines
drivers/gpu/drm/drm_fb_helper.c +2 -0
@@ -232,9 +232,23 @@ static void drm_fb_helper_fb_dirty(struct drm_fb_helper *helper)
 struct drm_clip_rect clip_copy;
 unsigned long flags;
 int ret;
+ unsigned int crtc_index = 0;
+ struct drm_crtc *crtc;
 
Quoted 10 lines

drivers/gpu/drm/drm_fb_helper.c static void drm_fb_helper_fb_dirty(struct drm_fb_helper *helper) · line 243

Quoted 7 lines
drivers/gpu/drm/drm_fb_helper.c +5 -1
 mutex_lock(&helper->lock);
- drm_client_modeset_wait_for_vblank(&helper->client, 0);
+
+ drm_for_each_crtc(crtc, dev) {
+ if (crtc->primary && crtc->primary->state &&
+    crtc->primary->state->fb == helper->fb &&
+    crtc->state && crtc->state->active) {

[Severity: High]
Can this lockless access to crtc->state and crtc->primary->state result in a use-after-free?

No idea...

Quoted 2 lines

The drm_fb_helper_fb_dirty() function holds helper->lock, but does not hold the DRM modeset locks required to protect these state pointers. During a

Which locks are exactly recommended to add? What is their name/pointer?

Quoted 6 lines

concurrent atomic modeset commit, the DRM core allocates new state objects, swaps them into place, and frees the old objects via kfree().

If this code reads an old state pointer and gets preempted, could it then dereference the pointer after it has been freed by the concurrent commit?

Quoted 7 lines

[Severity: High]
Does this break vblank synchronization for legacy non-atomic DRM drivers? Legacy drivers do not use the atomic modesetting infrastructure, so their crtc->state and crtc->primary->state pointers remain NULL. Since the condition above requires these pointers to be non-NULL, the loop will skip the vblank wait entirely for legacy drivers. This skips the intended rate-limiting behavior and could lead to unbounded damage worker execution.

What is the solution? And is this important.

Before d8c4bddcd8bcb ("drm/fb-helper: Synchronize dirty worker with vblank")

there was also no rate-limiting behavior.

Quoted 6 lines

drivers/gpu/drm/drm_fb_helper.c static void drm_fb_helper_fb_dirty(struct drm_fb_helper *helper) · line 245

Quoted 2 lines
drivers/gpu/drm/drm_fb_helper.c +2 -0
+ crtc_index = drm_crtc_index(crtc) + 1;
+ break;

[Severity: Medium]
The commit message states "If multiple CRTCs match, the last active one is selected." Does the break statement here cause the first active CRTC to be selected instead, bypassing the remaining matches in the list?

Yes, that is correct. The description is indeed wrong.

Quoted 9 lines
Quoted 7 lines
drivers/gpu/drm/drm_fb_helper.c +6 -0
+ }
+ }
+
+ if (crtc_index > 0)
+ drm_client_modeset_wait_for_vblank(&helper->client, crtc_index - 1);
+
 mutex_unlock(&helper->lock);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/5118b37e579cec220f2d2b6ab0bb1ecfb67f0d5c.1781803617.git.hns@goldelico.com?part=1
Signature · 6 lines
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstr. 146, 90461 Nürnberg, Germany, www.suse.com
GF: Jochen Jaser, Andrew McDonald, Werner Knoblich, (HRB 36809, AG Nürnberg)

From 9f94e19d887ed4e456ded81d075e93005bd60d1b Mon Sep 17 00:00:00 2001
From: Thomas Zimmermann <[email protected]>
Date: Fri, 19 Jun 2026 10:31:38 +0200
Subject: [PATCH] drm/fb-helper: Only consider active CRTCs for vblank sync

Only synchronize fbdev output to the vblank of active CRTCs. Go over the list of CRTCs and pick the first that matches. Fixes warnings as the one shown below

[   77.201354] WARNING: drivers/gpu/drm/drm_vblank.c:1320 at drm_crtc_wait_one_vblank+0x194/0x1cc [drm], CPU#1: kworker/1:7/1867
[   77.201354] omapdrm omapdrm.0: [drm] vblank wait timed out on crtc 0

This currently happens if the fbdev output is not on CRTC 0.

The patch disables vblank sync on the few drivers that still do not implement atomic modesetting. It's not at all a critical feature though.

Fixes d8c4bddcd8bcb ("drm/fb-helper: Synchronize dirty worker with vblank")
---
 drivers/gpu/drm/drm_fb_helper.c | 32 +++++++++++++++++++++++++++++---
 1 file changed, 29 insertions(+), 3 deletions(-)
drivers/gpu/drm/drm_fb_helper.c +29 -3
index 7b11a582f8ec..37e4d33a6d49 100644
@@ -230,12 +230,38 @@ static void drm_fb_helper_fb_dirty(struct drm_fb_helper *helper)
 	struct drm_device *dev = helper->dev;
 	struct drm_clip_rect *clip = &helper->damage_clip;
 	struct drm_clip_rect clip_copy;
+	struct drm_plane *plane;
+	int crtc_index = -1;
 	unsigned long flags;
 	int ret;
 
-	mutex_lock(&helper->lock);
-	drm_client_modeset_wait_for_vblank(&helper->client, 0);
-	mutex_unlock(&helper->lock);
+	if (drm_drv_uses_atomic_modeset(dev)) {
+		mutex_lock(&helper->lock);
+		mutex_lock(&dev->mode_config.mutex);
+
+		drm_for_each_plane(plane, dev) {
+			const struct drm_plane_state *plane_state;
+			const struct drm_crtc *crtc;
+
+			if (plane->type != DRM_PLANE_TYPE_PRIMARY)
+				continue; /* not a primary plane */
+
+			plane_state = plane->state;
+			if (plane_state->fb != helper->fb || !plane_state->crtc)
+				continue; /* plane doesn't display fbdev emulation */
+
+			crtc = plane_state->crtc;
+			if (crtc->state->active) {
+				crtc_index = crtc->index;
+				break;
+			}
+		}
+		mutex_unlock(&dev->mode_config.mutex);
+
+		if (crtc_index >= 0)
+			drm_client_modeset_wait_for_vblank(&helper->client, crtc_index);
+		mutex_unlock(&helper->lock);
+	}
 
 	if (drm_WARN_ON_ONCE(dev, !helper->funcs->fb_dirty))
 		return;
Signature · 1 line
2.54.0
#4 lore raw
Quoted 7 lines · 在 2026-06-19五的 10:50 +0200,Thomas Zimmermann写道:

(cc Icenowy)

Hi,

there are a number of similar reports. Does the attached patch fix the
problem. It picks up what you do, but in a more correct way.

Yes, this patch looks okay, but I wonder whether the crtc index query should be done in drm_client_modeset_wait_for_vblank() instead.

I will test this patch this weekend.

Thanks,
Icenow

Quoted 113 lines

Best regards
Thomas

Am 18.06.26 um 19:56 schrieb H. Nikolaus Schaller:

Quoted 108 lines
Quoted 39 lines

Am 18.06.2026 um 19:43 schrieb [email protected]:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Lockless access to crtc->state and `crtc->primary-

Quoted 1 line

state` can lead to a Use-After-Free due to concurrent atomic

commits.
- [High] The patch breaks vblank synchronization for legacy (non- atomic) DRM drivers.
- [Medium] The logic selects the first matching CRTC instead of the last active one, contradicting the commit message. --

commit 0337b26b606b4d905d72ea2be31bcb984fe62c6e
Author: H. Nikolaus Schaller <[email protected]>

drm/fb_helper: Determine proper crtc_index within
drm_fb_helper_fb_dirty

This commit updates the dirty framebuffer worker to search for a matching
active CRTC instead of hardcoding the wait for vblank to CRTC index 0. It
aims to avoid vblank timeouts when CRTC 0 is inactive.

Quoted 4 lines
drivers/gpu/drm/drm_fb_helper.c +0 -0

[ ... ]

Quoted 8 lines
drivers/gpu/drm/drm_fb_helper.c +2 -0
@@ -232,9 +232,23 @@ static void drm_fb_helper_fb_dirty(struct
 drm_fb_helper *helper)
 struct drm_clip_rect clip_copy;
 unsigned long flags;
 int ret;
+ unsigned int crtc_index = 0;
+ struct drm_crtc *crtc;
 
Quoted 11 lines

drivers/gpu/drm/drm_fb_helper.c static void drm_fb_helper_fb_dirty(struct · line 244

Quoted 7 lines
drivers/gpu/drm/drm_fb_helper.c +5 -1
 mutex_lock(&helper->lock);
- drm_client_modeset_wait_for_vblank(&helper->client, 0);
+
+ drm_for_each_crtc(crtc, dev) {
+ if (crtc->primary && crtc->primary->state &&
+    crtc->primary->state->fb == helper->fb &&
+    crtc->state && crtc->state->active) {

[Severity: High]
Can this lockless access to crtc->state and crtc->primary->state result in a
use-after-free?

No idea...

Quoted 4 lines

The drm_fb_helper_fb_dirty() function holds helper->lock, but does not hold
the DRM modeset locks required to protect these state pointers. During a

Which locks are exactly recommended to add? What is their name/pointer?

Quoted 9 lines

concurrent atomic modeset commit, the DRM core allocates new state objects,
swaps them into place, and frees the old objects via kfree().

If this code reads an old state pointer and gets preempted, could it then
dereference the pointer after it has been freed by the concurrent commit?

Quoted 12 lines

[Severity: High]
Does this break vblank synchronization for legacy non-atomic DRM drivers?
Legacy drivers do not use the atomic modesetting infrastructure, so their
crtc->state and crtc->primary->state pointers remain NULL. Since the condition
above requires these pointers to be non-NULL, the loop will skip the vblank
wait entirely for legacy drivers. This skips the intended rate- limiting
behavior and could lead to unbounded damage worker execution.

What is the solution? And is this important.

Before d8c4bddcd8bcb ("drm/fb-helper: Synchronize dirty worker with vblank")

there was also no rate-limiting behavior.

Quoted 8 lines

drivers/gpu/drm/drm_fb_helper.c static void drm_fb_helper_fb_dirty(struct · line 246

Quoted 2 lines
drivers/gpu/drm/drm_fb_helper.c +2 -0
+ crtc_index = drm_crtc_index(crtc) + 1;
+ break;

[Severity: Medium]
The commit message states "If multiple CRTCs match, the last active one is
selected." Does the break statement here cause the first active CRTC to be
selected instead, bypassing the remaining matches in the list?

Yes, that is correct. The description is indeed wrong.

Quoted 11 lines
Quoted 8 lines
drivers/gpu/drm/drm_fb_helper.c +6 -0
+ }
+ }
+
+ if (crtc_index > 0)
+ drm_client_modeset_wait_for_vblank(&helper->client,
 crtc_index - 1);
+
 mutex_unlock(&helper->lock);
Signature · 2 lines
Sashiko AI review ·
https://sashiko.dev/#/patchset/5118b37e579cec220f2d2b6ab0bb1ecfb67f0d5c.1781803617.git.hns@goldelico.com?part=1
#5 lore raw

Hi

Am 19.06.26 um 18:59 schrieb Icenowy Zheng:

Quoted 10 lines · 在 2026-06-19五的 10:50 +0200,Thomas Zimmermann写道:
Quoted 7 lines

(cc Icenowy)

Hi,

there are a number of similar reports. Does the attached patch fix the
problem. It picks up what you do, but in a more correct way.

Yes, this patch looks okay, but I wonder whether the crtc index query should be done in drm_client_modeset_wait_for_vblank() instead.

There's another call to this helper from the ioctl helper. It has a lengthy comment about using CRTC 0 only and how these indices don't make much sense with fbdev. I'll look into generalizing the code a bit, but I'd rather not fully integrate the logic into drm_client_modeset_wait_for_vblank().

Best regards
Thomas

Quoted 117 lines

I will test this patch this weekend.

Thanks,
Icenow

Quoted 111 lines

Best regards
Thomas

Am 18.06.26 um 19:56 schrieb H. Nikolaus Schaller:

Quoted 107 lines
Quoted 38 lines

Am 18.06.2026 um 19:43 schrieb [email protected]:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Lockless access to crtc->state and `crtc->primary-

Quoted 1 line

state` can lead to a Use-After-Free due to concurrent atomic

commits.
- [High] The patch breaks vblank synchronization for legacy (non- atomic) DRM drivers.
- [Medium] The logic selects the first matching CRTC instead of the last active one, contradicting the commit message. --

commit 0337b26b606b4d905d72ea2be31bcb984fe62c6e
Author: H. Nikolaus Schaller <[email protected]>

drm/fb_helper: Determine proper crtc_index within
drm_fb_helper_fb_dirty

This commit updates the dirty framebuffer worker to search for a matching
active CRTC instead of hardcoding the wait for vblank to CRTC index 0. It
aims to avoid vblank timeouts when CRTC 0 is inactive.

Quoted 4 lines
drivers/gpu/drm/drm_fb_helper.c +0 -0

[ ... ]

Quoted 8 lines
drivers/gpu/drm/drm_fb_helper.c +2 -0
@@ -232,9 +232,23 @@ static void drm_fb_helper_fb_dirty(struct
 drm_fb_helper *helper)
 struct drm_clip_rect clip_copy;
 unsigned long flags;
 int ret;
+ unsigned int crtc_index = 0;
+ struct drm_crtc *crtc;
 
Quoted 11 lines

drivers/gpu/drm/drm_fb_helper.c static void drm_fb_helper_fb_dirty(struct · line 244

Quoted 7 lines
drivers/gpu/drm/drm_fb_helper.c +5 -1
 mutex_lock(&helper->lock);
- drm_client_modeset_wait_for_vblank(&helper->client, 0);
+
+ drm_for_each_crtc(crtc, dev) {
+ if (crtc->primary && crtc->primary->state &&
+    crtc->primary->state->fb == helper->fb &&
+    crtc->state && crtc->state->active) {

[Severity: High]
Can this lockless access to crtc->state and crtc->primary->state result in a
use-after-free?

No idea...

Quoted 4 lines

The drm_fb_helper_fb_dirty() function holds helper->lock, but does not hold
the DRM modeset locks required to protect these state pointers. During a

Which locks are exactly recommended to add? What is their name/pointer?

Quoted 9 lines

concurrent atomic modeset commit, the DRM core allocates new state objects,
swaps them into place, and frees the old objects via kfree().

If this code reads an old state pointer and gets preempted, could it then
dereference the pointer after it has been freed by the concurrent commit?

Quoted 12 lines

[Severity: High]
Does this break vblank synchronization for legacy non-atomic DRM drivers?
Legacy drivers do not use the atomic modesetting infrastructure, so their
crtc->state and crtc->primary->state pointers remain NULL. Since the condition
above requires these pointers to be non-NULL, the loop will skip the vblank
wait entirely for legacy drivers. This skips the intended rate- limiting
behavior and could lead to unbounded damage worker execution.

What is the solution? And is this important.

Before d8c4bddcd8bcb ("drm/fb-helper: Synchronize dirty worker with vblank")

there was also no rate-limiting behavior.

Quoted 8 lines

drivers/gpu/drm/drm_fb_helper.c static void drm_fb_helper_fb_dirty(struct · line 246

Quoted 2 lines
drivers/gpu/drm/drm_fb_helper.c +2 -0
+ crtc_index = drm_crtc_index(crtc) + 1;
+ break;

[Severity: Medium]
The commit message states "If multiple CRTCs match, the last active one is
selected." Does the break statement here cause the first active CRTC to be
selected instead, bypassing the remaining matches in the list?

Yes, that is correct. The description is indeed wrong.

Quoted 11 lines
Quoted 8 lines
drivers/gpu/drm/drm_fb_helper.c +6 -0
+ }
+ }
+
+ if (crtc_index > 0)
+ drm_client_modeset_wait_for_vblank(&helper->client,
 crtc_index - 1);
+
 mutex_unlock(&helper->lock);
Signature · 2 lines
Sashiko AI review ·
https://sashiko.dev/#/patchset/5118b37e579cec220f2d2b6ab0bb1ecfb67f0d5c.1781803617.git.hns@goldelico.com?part=1
Signature · 6 lines
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstr. 146, 90461 Nürnberg, Germany, www.suse.com
GF: Jochen Jaser, Andrew McDonald, Werner Knoblich, (HRB 36809, AG Nürnberg)
#6 lore raw
Quoted 12 lines · 在 2026-06-22一的 11:11 +0200,Thomas Zimmermann写道:

Hi

Am 19.06.26 um 18:59 schrieb Icenowy Zheng:

Quoted 9 lines · 在 2026-06-19五的 10:50 +0200,Thomas Zimmermann写道:
Quoted 8 lines

(cc Icenowy)

Hi,

there are a number of similar reports. Does the attached patch fix
the
problem. It picks up what you do, but in a more correct way.

Quoted 8 lines
Quoted 3 lines

Yes, this patch looks okay, but I wonder whether the crtc index query
should be done in drm_client_modeset_wait_for_vblank() instead.

There's another call to this helper from the ioctl helper. It has a lengthy comment about using CRTC 0 only and how these indices don't make
much sense with fbdev. I'll look into generalizing the code a bit,

I think both codepaths are based on the same (wrong) assumption that CRTC 0 is always the first used CRTC, so that codepath should also be fixed; integrating the logic into drm_client_modeset_wait_for_vblank() will not affect drivers that use CRTC 0 first, but will make drivers that have fixed CRTC preference work with FBIO_WAITFORVSYNC too (which currently do not work).

Thanks,
Icenowy

Quoted 141 lines

but
I'd rather not fully integrate the logic into
drm_client_modeset_wait_for_vblank().

Best regards
Thomas

Quoted 134 lines

I will test this patch this weekend.

Thanks,
Icenow

Quoted 128 lines

Best regards
Thomas

Am 18.06.26 um 19:56 schrieb H. Nikolaus Schaller:

Quoted 124 lines
Quoted 52 lines

Am 18.06.2026 um 19:43 schrieb [email protected]:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Lockless access to crtc->state and `crtc->primary-

Quoted 2 lines

state` can lead to a Use-After-Free due to concurrent atomic

commits.
- [High] The patch breaks vblank synchronization for legacy (non-
atomic) DRM drivers.
- [Medium] The logic selects the first matching CRTC instead of
the last active one, contradicting the commit message. --

commit 0337b26b606b4d905d72ea2be31bcb984fe62c6e
Author: H. Nikolaus Schaller <[email protected]>

drm/fb_helper: Determine proper crtc_index within
drm_fb_helper_fb_dirty

This commit updates the dirty framebuffer worker to search for a
matching
active CRTC instead of hardcoding the wait for vblank to CRTC index 0. It
aims to avoid vblank timeouts when CRTC 0 is inactive.

Quoted 4 lines
drivers/gpu/drm/drm_fb_helper.c +0 -0

[ ... ]

Quoted 9 lines
drivers/gpu/drm/drm_fb_helper.c +0 -0
@@ -232,9 +232,23 @@ static void

drm_fb_helper_fb_dirty(struct
drm_fb_helper *helper)
struct drm_clip_rect clip_copy;
unsigned long flags;
int ret;
+ unsigned int crtc_index = 0;
+ struct drm_crtc *crtc;

Quoted 7 lines

mutex_lock(&helper->lock);
- drm_client_modeset_wait_for_vblank(&helper->client, 0); +
+ drm_for_each_crtc(crtc, dev) {
+ if (crtc->primary && crtc->primary->state &&
+    crtc->primary->state->fb == helper->fb &&
+    crtc->state && crtc->state->active) {

[Severity: High]
Can this lockless access to crtc->state and crtc->primary-

Quoted 3 lines
Quoted 1 line

state

result in a
use-after-free?

No idea...

Quoted 5 lines

The drm_fb_helper_fb_dirty() function holds helper->lock, but does not hold
the DRM modeset locks required to protect these state pointers.
During a

Which locks are exactly recommended to add? What is their name/pointer?

Quoted 20 lines

concurrent atomic modeset commit, the DRM core allocates new state objects,
swaps them into place, and frees the old objects via kfree().

If this code reads an old state pointer and gets preempted, could
it then
dereference the pointer after it has been freed by the concurrent
commit?

[Severity: High]
Does this break vblank synchronization for legacy non-atomic DRM
drivers?
Legacy drivers do not use the atomic modesetting infrastructure,
so their
crtc->state and crtc->primary->state pointers remain NULL. Since

Quoted 8 lines

the condition
above requires these pointers to be non-NULL, the loop will skip
the vblank
wait entirely for legacy drivers. This skips the intended rate-
limiting
behavior and could lead to unbounded damage worker execution.

What is the solution? And is this important.

Before d8c4bddcd8bcb ("drm/fb-helper: Synchronize dirty worker with
vblank")

there was also no rate-limiting behavior.

Quoted 10 lines
Quoted 2 lines

+ crtc_index = drm_crtc_index(crtc) + 1;
+ break;

[Severity: Medium]
The commit message states "If multiple CRTCs match, the last active one is
selected." Does the break statement here cause the first active
CRTC to be
selected instead, bypassing the remaining matches in the list?

Yes, that is correct. The description is indeed wrong.

Quoted 11 lines
Quoted 8 lines

+ }
+ }
+
+ if (crtc_index > 0)
+ drm_client_modeset_wait_for_vblank(&helper->client, crtc_index - 1);
+
mutex_unlock(&helper->lock);

Signature · 2 lines
Sashiko AI review ·
https://sashiko.dev/#/patchset/5118b37e579cec220f2d2b6ab0bb1ecfb67f0d5c.1781803617.git.hns@goldelico.com?part=1
#7 lore raw

Hi

Am 22.06.26 um 11:47 schrieb Icenowy Zheng:

Quoted 20 lines · 在 2026-06-22一的 11:11 +0200,Thomas Zimmermann写道:
Quoted 12 lines

Hi

Am 19.06.26 um 18:59 schrieb Icenowy Zheng:

Quoted 9 lines · 在 2026-06-19五的 10:50 +0200,Thomas Zimmermann写道:
Quoted 8 lines

(cc Icenowy)

Hi,

there are a number of similar reports. Does the attached patch fix
the
problem. It picks up what you do, but in a more correct way.

Quoted 7 lines
Quoted 3 lines

Yes, this patch looks okay, but I wonder whether the crtc index query
should be done in drm_client_modeset_wait_for_vblank() instead.

There's another call to this helper from the ioctl helper. It has a lengthy comment about using CRTC 0 only and how these indices don't make
much sense with fbdev. I'll look into generalizing the code a bit,

Quoted 6 lines

I think both codepaths are based on the same (wrong) assumption that CRTC 0 is always the first used CRTC, so that codepath should also be fixed; integrating the logic into drm_client_modeset_wait_for_vblank() will not affect drivers that use CRTC 0 first, but will make drivers that have fixed CRTC preference work with FBIO_WAITFORVSYNC too (which currently do not work).

DRM's fbdev emulation is legacy code and most likely not used besides the kernel's console. Until we have a bug reports against the ioctl, there nothing to fix here.

Interfaces for drm_client_ are supposed to work with any client. Integrating that look-up code there might make other clients unhappy.

Best regards
Thomas

Quoted 144 lines

Thanks,
Icenowy

Quoted 140 lines

but
I'd rather not fully integrate the logic into
drm_client_modeset_wait_for_vblank().

Best regards
Thomas

Quoted 133 lines

I will test this patch this weekend.

Thanks,
Icenow

Quoted 128 lines

Best regards
Thomas

Am 18.06.26 um 19:56 schrieb H. Nikolaus Schaller:

Quoted 124 lines
Quoted 52 lines

Am 18.06.2026 um 19:43 schrieb [email protected]:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Lockless access to crtc->state and `crtc->primary-

Quoted 2 lines

state` can lead to a Use-After-Free due to concurrent atomic

commits.
- [High] The patch breaks vblank synchronization for legacy (non-
atomic) DRM drivers.
- [Medium] The logic selects the first matching CRTC instead of
the last active one, contradicting the commit message. --

commit 0337b26b606b4d905d72ea2be31bcb984fe62c6e
Author: H. Nikolaus Schaller <[email protected]>

drm/fb_helper: Determine proper crtc_index within
drm_fb_helper_fb_dirty

This commit updates the dirty framebuffer worker to search for a
matching
active CRTC instead of hardcoding the wait for vblank to CRTC index 0. It
aims to avoid vblank timeouts when CRTC 0 is inactive.

Quoted 4 lines
drivers/gpu/drm/drm_fb_helper.c +0 -0

[ ... ]

Quoted 9 lines
drivers/gpu/drm/drm_fb_helper.c +0 -0
@@ -232,9 +232,23 @@ static void

drm_fb_helper_fb_dirty(struct
drm_fb_helper *helper)
struct drm_clip_rect clip_copy;
unsigned long flags;
int ret;
+ unsigned int crtc_index = 0;
+ struct drm_crtc *crtc;

Quoted 7 lines

mutex_lock(&helper->lock);
- drm_client_modeset_wait_for_vblank(&helper->client, 0); +
+ drm_for_each_crtc(crtc, dev) {
+ if (crtc->primary && crtc->primary->state &&
+    crtc->primary->state->fb == helper->fb &&
+    crtc->state && crtc->state->active) {

[Severity: High]
Can this lockless access to crtc->state and crtc->primary-

Quoted 3 lines
Quoted 1 line

state

result in a
use-after-free?

No idea...

Quoted 5 lines

The drm_fb_helper_fb_dirty() function holds helper->lock, but does not hold
the DRM modeset locks required to protect these state pointers.
During a

Which locks are exactly recommended to add? What is their name/pointer?

Quoted 20 lines

concurrent atomic modeset commit, the DRM core allocates new state objects,
swaps them into place, and frees the old objects via kfree().

If this code reads an old state pointer and gets preempted, could
it then
dereference the pointer after it has been freed by the concurrent
commit?

[Severity: High]
Does this break vblank synchronization for legacy non-atomic DRM
drivers?
Legacy drivers do not use the atomic modesetting infrastructure,
so their
crtc->state and crtc->primary->state pointers remain NULL. Since

Quoted 8 lines

the condition
above requires these pointers to be non-NULL, the loop will skip
the vblank
wait entirely for legacy drivers. This skips the intended rate-
limiting
behavior and could lead to unbounded damage worker execution.

What is the solution? And is this important.

Before d8c4bddcd8bcb ("drm/fb-helper: Synchronize dirty worker with
vblank")

there was also no rate-limiting behavior.

Quoted 10 lines
Quoted 2 lines

+ crtc_index = drm_crtc_index(crtc) + 1;
+ break;

[Severity: Medium]
The commit message states "If multiple CRTCs match, the last active one is
selected." Does the break statement here cause the first active
CRTC to be
selected instead, bypassing the remaining matches in the list?

Yes, that is correct. The description is indeed wrong.

Quoted 11 lines
Quoted 8 lines

+ }
+ }
+
+ if (crtc_index > 0)
+ drm_client_modeset_wait_for_vblank(&helper->client, crtc_index - 1);
+
mutex_unlock(&helper->lock);

Signature · 2 lines
Sashiko AI review ·
https://sashiko.dev/#/patchset/5118b37e579cec220f2d2b6ab0bb1ecfb67f0d5c.1781803617.git.hns@goldelico.com?part=1
Signature · 6 lines
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstr. 146, 90461 Nürnberg, Germany, www.suse.com
GF: Jochen Jaser, Andrew McDonald, Werner Knoblich, (HRB 36809, AG Nürnberg)
#8 lore raw
In reply to Thomas Zimmermann

Hi Thomas,

Quoted 7 lines

Am 19.06.2026 um 10:50 schrieb Thomas Zimmermann <[email protected]>:

(cc Icenowy)

Hi,

there are a number of similar reports. Does the attached patch fix the problem. It picks up what you do, but in a more correct way.

Yes, reverting my version makes the issue come back and after applying yours it is again gone. At least in the scenarios I have tested for the PandaBoard-ES (boot/reboot, unplug/replug, second display or not).

Should I take your corrections and resubmit or how to proceed?

Best regards,
Nikolaus

#9 lore raw

Hi

Am 19.06.26 um 19:17 schrieb H. Nikolaus Schaller:

Quoted 5 lines

Hi Thomas,

Quoted 2 lines

Am 19.06.2026 um 10:50 schrieb Thomas Zimmermann <[email protected]>:

Quoted 8 lines
Quoted 5 lines

(cc Icenowy)

Hi,

there are a number of similar reports. Does the attached patch fix the problem. It picks up what you do, but in a more correct way.

Yes, reverting my version makes the issue come back and after applying yours it is again gone. At least in the scenarios I have tested for the PandaBoard-ES (boot/reboot, unplug/replug, second display or not).

Should I take your corrections and resubmit or how to proceed?

Several people have reported the problem and asked for a fix. I'll send the patch out for review. I'll add you as Co-developed-by, if you like.

Best regards
Thomas

Quoted 5 lines

Best regards,
Nikolaus

Signature · 6 lines
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstr. 146, 90461 Nürnberg, Germany, www.suse.com
GF: Jochen Jaser, Andrew McDonald, Werner Knoblich, (HRB 36809, AG Nürnberg)
#10 lore raw

Hi Thomas,

Quoted 12 lines

Am 22.06.2026 um 07:58 schrieb Thomas Zimmermann <[email protected]>:

Hi

Am 19.06.26 um 19:17 schrieb H. Nikolaus Schaller:

Quoted 7 lines

Hi Thomas,

Quoted 4 lines

Am 19.06.2026 um 10:50 schrieb Thomas Zimmermann <[email protected]>:

(cc Icenowy)

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Hi,

there are a number of similar reports. Does the attached patch fix the problem. It picks up what you do, but in a more correct way.

Yes, reverting my version makes the issue come back and after applying yours it is again gone. At least in the scenarios I have tested for the PandaBoard-ES (boot/reboot, unplug/replug, second display or not).

Should I take your corrections and resubmit or how to proceed?

Several people have reported the problem and asked for a fix. I'll send the patch out for review. I'll add you as Co-developed-by, if you like.

Yes, that is fine.

I have successfully tested v2 and will reply there.

Thanks and BR,
Nikolaus

#11 lore raw
In reply to Thomas Zimmermann
Quoted 7 lines · 在 2026-06-19五的 10:50 +0200,Thomas Zimmermann写道:

(cc Icenowy)

Hi,

there are a number of similar reports. Does the attached patch fix the
problem. It picks up what you do, but in a more correct way.

Well in fact I think the correct practice might be combining both -- use crtc index in modeset + skip non-enabled CRTCs.

But anyway the attached patch is tested by me on loongson driver, if you're going to send it, feel free to add:

```
Tested-by: Icenowy Zheng <[email protected]>
```

In addition, a bug report with loongson driver exists on the Debian bugtracker, the Debian kernel team wants the fix to have:

```
Closes: https://bugs.debian.org/1138033
```

Thanks,
Icenowy

Quoted 113 lines

Best regards
Thomas

Am 18.06.26 um 19:56 schrieb H. Nikolaus Schaller:

Quoted 108 lines
Quoted 39 lines

Am 18.06.2026 um 19:43 schrieb [email protected]:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Lockless access to crtc->state and `crtc->primary-

Quoted 1 line

state` can lead to a Use-After-Free due to concurrent atomic

commits.
- [High] The patch breaks vblank synchronization for legacy (non- atomic) DRM drivers.
- [Medium] The logic selects the first matching CRTC instead of the last active one, contradicting the commit message. --

commit 0337b26b606b4d905d72ea2be31bcb984fe62c6e
Author: H. Nikolaus Schaller <[email protected]>

drm/fb_helper: Determine proper crtc_index within
drm_fb_helper_fb_dirty

This commit updates the dirty framebuffer worker to search for a matching
active CRTC instead of hardcoding the wait for vblank to CRTC index 0. It
aims to avoid vblank timeouts when CRTC 0 is inactive.

Quoted 4 lines
drivers/gpu/drm/drm_fb_helper.c +0 -0

[ ... ]

Quoted 8 lines
drivers/gpu/drm/drm_fb_helper.c +2 -0
@@ -232,9 +232,23 @@ static void drm_fb_helper_fb_dirty(struct
 drm_fb_helper *helper)
 struct drm_clip_rect clip_copy;
 unsigned long flags;
 int ret;
+ unsigned int crtc_index = 0;
+ struct drm_crtc *crtc;
 
Quoted 11 lines

drivers/gpu/drm/drm_fb_helper.c static void drm_fb_helper_fb_dirty(struct · line 244

Quoted 7 lines
drivers/gpu/drm/drm_fb_helper.c +5 -1
 mutex_lock(&helper->lock);
- drm_client_modeset_wait_for_vblank(&helper->client, 0);
+
+ drm_for_each_crtc(crtc, dev) {
+ if (crtc->primary && crtc->primary->state &&
+    crtc->primary->state->fb == helper->fb &&
+    crtc->state && crtc->state->active) {

[Severity: High]
Can this lockless access to crtc->state and crtc->primary->state result in a
use-after-free?

No idea...

Quoted 4 lines

The drm_fb_helper_fb_dirty() function holds helper->lock, but does not hold
the DRM modeset locks required to protect these state pointers. During a

Which locks are exactly recommended to add? What is their name/pointer?

Quoted 9 lines

concurrent atomic modeset commit, the DRM core allocates new state objects,
swaps them into place, and frees the old objects via kfree().

If this code reads an old state pointer and gets preempted, could it then
dereference the pointer after it has been freed by the concurrent commit?

Quoted 12 lines

[Severity: High]
Does this break vblank synchronization for legacy non-atomic DRM drivers?
Legacy drivers do not use the atomic modesetting infrastructure, so their
crtc->state and crtc->primary->state pointers remain NULL. Since the condition
above requires these pointers to be non-NULL, the loop will skip the vblank
wait entirely for legacy drivers. This skips the intended rate- limiting
behavior and could lead to unbounded damage worker execution.

What is the solution? And is this important.

Before d8c4bddcd8bcb ("drm/fb-helper: Synchronize dirty worker with vblank")

there was also no rate-limiting behavior.

Quoted 8 lines

drivers/gpu/drm/drm_fb_helper.c static void drm_fb_helper_fb_dirty(struct · line 246

Quoted 2 lines
drivers/gpu/drm/drm_fb_helper.c +2 -0
+ crtc_index = drm_crtc_index(crtc) + 1;
+ break;

[Severity: Medium]
The commit message states "If multiple CRTCs match, the last active one is
selected." Does the break statement here cause the first active CRTC to be
selected instead, bypassing the remaining matches in the list?

Yes, that is correct. The description is indeed wrong.

Quoted 11 lines
Quoted 8 lines
drivers/gpu/drm/drm_fb_helper.c +6 -0
+ }
+ }
+
+ if (crtc_index > 0)
+ drm_client_modeset_wait_for_vblank(&helper->client,
 crtc_index - 1);
+
 mutex_unlock(&helper->lock);
Signature · 2 lines
Sashiko AI review ·
https://sashiko.dev/#/patchset/5118b37e579cec220f2d2b6ab0bb1ecfb67f0d5c.1781803617.git.hns@goldelico.com?part=1