Skip to content

drm/vblank: timer: Fix race condition in enable_vblank

Thomas Zimmermann dri-devel sashiko-reviews stable 2 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 Latest2 patches 1 replyIn review
  2. v12 patches 0 repliesSuperseded

Changes in v2 from the cover letter

- handle uninitialized timer in drm_crtc_vblank_cancel_timer()

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

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/2drm/vblank: timer: Do not block within critical sectiondrivers/gpu/drm/drm_vblank.c+12 -220 replies
2/2drm/vblank: Cancel vblank timer outside of critical sectiondrivers/gpu/drm/drm_vblank.c drivers/gpu/drm/drm_vblank_helper.c drivers/gpu/drm/vmwgfx/vmwgfx_ldu.c +6 more+96 -120 replies

Reviews trailers per patch

None of the 2 patches has a Reviewed-by or Acked-by of their own; the series has one for all of them.

PatchZack Rusin
SeriesR-b
1/2
2/2

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v2 thread

Cover letter [PATCH v2 0/2] drm/vblank: timer: Fix race condition in enable_vblank

Busy-waiting in drm_crtc_vblank_start_timer() until a previous vblank timer has completed can deadlock. Rework how the vblank timer's start and cancel helpers interact with the timer function.

Patch 1 fixes the possible deadlock by removing the busy-wait loop from drm_crtc_vblank_start_timer(). To avoid possible interference between _start_timer()'s hrtimer_start() and hrtimer_forward_now() in the timer function, extend the interval_lock around both. Both, _start_timer() and the timer function, can still overlap, but hrtimer initialization should work reliably. At most, the first vblank event after changing the interval might be off.

Fix the possible overlap by canceling the vblank timer's internal hrtimer. Do this outside of the vblank code's critical section after switching the vblank timer off. With the hrtimer being canceled, the next vblank event will be sent at the correct time.

Tested with bochs on qemu.

Additional notes: there have been reports about vblank timers not happening in time, such as [1]. We suspected that might happen due to guest systems running under high CPU load. But the problem could also be caused by the deadlock fixed in patch 1.

v2:
- handle uninitialized timer in drm_crtc_vblank_cancel_timer()

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

Thomas Zimmermann (2):
drm/vblank: timer: Do not block within critical section drm/vblank: Cancel vblank timer outside of critical section

 drivers/gpu/drm/drm_vblank.c         | 90 ++++++++++++++++++----------
 drivers/gpu/drm/drm_vblank_helper.c  | 16 +++++
 drivers/gpu/drm/vmwgfx/vmwgfx_ldu.c  |  3 +-
 drivers/gpu/drm/vmwgfx/vmwgfx_scrn.c |  2 +
 drivers/gpu/drm/vmwgfx/vmwgfx_stdu.c |  2 +
 drivers/gpu/drm/vmwgfx/vmwgfx_vkms.c |  2 +-
 include/drm/drm_crtc.h               |  9 +++
 include/drm/drm_vblank.h             | 16 +++++
 include/drm/drm_vblank_helper.h      |  2 +
 9 files changed, 108 insertions(+), 34 deletions(-)

base-commit: 7d502136b31169f6b5575f7f89c3da60cdf7891d

Signature · 1 line
2.56.0