drm/fb-helper: Only consider active CRTCs for vblank sync
Subsystems (2)
Thomas Zimmermann
maintainerPATCH v3 1/1 drm/fb-helper: Only consider active CRTCs for vblank sync
Only synchronize fbdev output to the vblank of an active CRTC. 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.
Atomic and non-atomic drivers require distinct code paths. As for other fbdev operations, implement both and select the correct one at runtime.
Not finding an active CRTC is not a bug. Do not wait in this case, but flush the display update as before.
v3:
- drop excessive state validation (Jani)
- acquire plane and CRTC mutices (Sashiko)
v2:
- move look-up code into separate helper
- support drivers with legacy modesetting
v1:
- see https://lore.kernel.org/dri-devel/[email protected]/
--- drivers/gpu/drm/drm_fb_helper.c | 73 ++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_fb_helper.c +72 -1
@@ -225,16 +225,87 @@ static void drm_fb_helper_resume_worker(struct work_struct *work)console_unlock (); } +static int find_crtc_index_atomic (struct drm_fb_helper *helper) +{ +struct drm_device *dev = helper->dev; +struct drm_plane *plane; +int crtc_index = -EINVAL ; + +drm_for_each_plane (plane, dev) { +const struct drm_plane_state *plane_state; + +if (plane->type !=DRM_PLANE_TYPE_PRIMARY ) +continue ; + +drm_modeset_lock (&plane->mutex,NULL ); + plane_state = plane->state; + +if (plane_state->fb == helper->fb && plane_state->crtc) { +struct drm_crtc *crtc = plane_state->crtc; + +drm_modeset_lock (&crtc->mutex,NULL ); +if (crtc->state->active) + crtc_index = crtc->index; +drm_modeset_unlock (&crtc->mutex); + } +drm_modeset_unlock (&plane->mutex); + +if (crtc_index >=0 ) +break ; + } + +return crtc_index; +} + +static int find_crtc_index_legacy (struct drm_fb_helper *helper) +{ +struct drm_device *dev = helper->dev; +struct drm_crtc *crtc; + +drm_for_each_crtc (crtc, dev) { +struct drm_plane *plane = crtc->primary; + +if (!crtc->enabled) +continue ; +if (!plane || plane->fb != helper->fb) +continue ;/* CRTC doesn't display fbdev emulation */ + +return crtc->index; + } + +return -EINVAL ; +} + +static int drm_fb_helper_find_crtc_index (struct drm_fb_helper *helper) +{ +struct drm_device *dev = helper->dev; +int crtc_index; + +mutex_lock (&dev->mode_config.mutex); + +if (drm_drv_uses_atomic_modeset (dev)) + crtc_index =find_crtc_index_atomic (helper); +else + crtc_index =find_crtc_index_legacy (helper); + +mutex_unlock (&dev->mode_config.mutex); + +return crtc_index; +} +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; +int crtc_index;unsigned long flags;int ret;mutex_lock (&helper->lock); -drm_client_modeset_wait_for_vblank (&helper->client,0 ); + crtc_index =drm_fb_helper_find_crtc_index (helper); +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))
Signature · 1 line
2.54.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Acquiring multiple modeset locks sequentially without an acquire context causes AB-BA deadlocks…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Acquiring multiple modeset locks sequentially without an acquire context causes AB-BA deadlocks with concurrent atomic commits and triggers lockdep splats. --
commit 5bf4951cc945bdc22bbaea8b5337320cd5b7047d
Author: Thomas Zimmermann <[email protected]>
drm/fb-helper: Only consider active CRTCs for vblank sync
This commit synchronizes fbdev output only to the vblank of an active CRTC to prevent vblank wait timeouts. It implements separate atomic and non-atomic code paths to select the correct active CRTC at runtime.
Quoted 4 lines
drivers/gpu/drm/drm_fb_helper.c +0
-0
[ ... ]
Quoted 9 lines
drivers/gpu/drm/drm_fb_helper.c +9
-0+static int find_crtc_index_atomic (struct drm_fb_helper *helper) +{ +struct drm_device *dev = helper->dev; +struct drm_plane *plane; +int crtc_index = -EINVAL ; + +drm_for_each_plane (plane, dev) { +const struct drm_plane_state *plane_state; +
drivers/gpu/drm/drm_fb_helper.c static void drm_fb_helper_resume_worker(struct work_struct *work) · line 246
Quoted 10 lines
drivers/gpu/drm/drm_fb_helper.c +10
-0+if (plane->type !=DRM_PLANE_TYPE_PRIMARY ) +continue ; + +drm_modeset_lock (&plane->mutex,NULL ); + plane_state = plane->state; + +if (plane_state->fb == helper->fb && plane_state->crtc) { +struct drm_crtc *crtc = plane_state->crtc; + +drm_modeset_lock (&crtc->mutex,NULL );
[Severity: High]
Can this sequence cause an AB-BA deadlock with concurrent atomic commits?
Because drm_modeset_lock() is called here on both plane->mutex and crtc->mutex with a NULL acquire context, it falls back to standard blocking mutex acquisition without the wait/wound deadlock avoidance mechanism.
If a concurrent atomic commit thread from a userspace compositor acquires crtc->mutex and then attempts to acquire plane->mutex, could this permanently hang both threads since the fbdev worker cannot be wound or back off?
Quoted 12 lines
drivers/gpu/drm/drm_fb_helper.c +12
-0+if (crtc->state->active) + crtc_index = crtc->index; +drm_modeset_unlock (&crtc->mutex); + } +drm_modeset_unlock (&plane->mutex); + +if (crtc_index >=0 ) +break ; + } + +return crtc_index; +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1