drm/msm/dpu: count the pending frame before kicking it off
Subsystems (4)
PATCH v2 1/1 drm/msm/dpu: count the pending frame before kicking it off
dpu_crtc_commit_kickoff() increments frame_pending only after dpu_encoder_kickoff() has sent the frame to the hardware. If the frame completes before the increment, dpu_crtc_frame_event_work() sees frame_pending == 0 and ignores the frame-done event. The increment that follows is never matched, so the count stays one too high until dpu_crtc_disable() resets it.
With frame_pending stuck at 1 or more:
- dpu_encoder_resource_control(FRAME_DONE) sees a pending count above 1 on every frame and never queues delayed_off_work. A video-mode encoder therefore never reaches ENTER_IDLE and keeps its vsync interrupt registered for as long as the display is on.
- dpu_crtc_frame_event_work() never sees the count drop to zero, so dpu_core_perf_crtc_release_bw() is never called.
On an X1E80100 laptop with a 120 Hz eDP panel this left the vsync interrupt firing 120 times a second on an idle desktop: 14,407 vblank callbacks against 21 commits in 120 s, with no vblank events requested by userspace. Every frame-done took the frame_event_more_pending path (591 of 591 in one trace) and the encoder never entered idle. After a suspend cycle reset the count, the same desktop showed 3 commits, 3 frame_event_done, 3 encoder idle transitions and 25 MDSS interrupts in 60 s.
Until commit ad06972d5365 ("drm/msm/dpu: Reorder encoder kickoff for CWB") the increment came before the kickoff. Move it back: count the frame in each kickoff path just before its encoders are kicked off. A clone-mode commit that fails its encoder lookup is then never counted.
---
Changes in v2:
- Count the frame inside each kickoff path, after the clone-mode encoder lookup, so the clone-mode error path has nothing to undo. v1 counted first and undid it with atomic_dec() on that path; if a frame-done dropped the count to 1 in between, that atomic_dec() took it to 0 without calling dpu_core_perf_crtc_release_bw() (Sashiko review).
- Link to v1: https://lore.kernel.org/r/[email protected]
Tested on a Dell XPS 13 9345 (X1E80100, 120 Hz eDP, video mode). v1 on v7.3-rc6 based kernels: the count no longer leaks in normal use. A 120 s trace 50 minutes after boot showed 22 commits, 22 frame_event_done and 22 encoder idle transitions. About 2 hours after boot there were 27,479 vblanks against 16,693 commits over 6,040 s, where a leaked count gives about 725,000. v2 on a v7.3-rc7 based kernel: a 60 s trace under load showed 5,648 commits, 5,649 frame_event_done and no frame_event_more_pending. Not tested: clone mode or writeback, which this machine does not have.
Jun Nie's "drm/msm/dpu: delete timeout timer when encoder is not busy"
[1] hits the same window: the frame-done interrupt arriving before dpu_crtc_commit_kickoff() gets past dpu_encoder_kickoff().
[1] https://lore.kernel.org/r/[email protected]
drivers/gpu/drm/msm/disp/dpu1/dpu_crtc.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-)
drivers/gpu/drm/msm/disp/dpu1/dpu_crtc.c +17 -6
@@ -1037,6 +1037,19 @@ static int _dpu_crtc_wait_for_frame_done(struct drm_crtc *crtc)return rc; } +/* Must precede the kickoff: the frame-done can be handled before it returns. */ +static void dpu_crtc_count_pending_frame (struct drm_crtc *crtc) +{ +struct dpu_crtc *dpu_crtc =to_dpu_crtc (crtc); + +if (atomic_inc_return (&dpu_crtc->frame_pending) ==1 ) { +/* acquire bandwidth and other resources */ +DRM_DEBUG_ATOMIC ("crtc%d first commit\n" , crtc->base.id); + }else { +DRM_DEBUG_ATOMIC ("crtc%d commit\n" , crtc->base.id); + } +} +static int dpu_crtc_kickoff_clone_mode (struct drm_crtc *crtc) {struct drm_encoder *encoder;
@@ -1062,6 +1075,8 @@ static int dpu_crtc_kickoff_clone_mode(struct drm_crtc *crtc)dpu_vbif_clear_errors (dpu_kms); +dpu_crtc_count_pending_frame (crtc); +/* * Kickoff real time encoder last as it's the encoder that * will do the flush
@@ -1119,6 +1134,8 @@ void dpu_crtc_commit_kickoff(struct drm_crtc *crtc)dpu_vbif_clear_errors (dpu_kms); +dpu_crtc_count_pending_frame (crtc); +drm_for_each_encoder_mask (encoder, crtc->dev, crtc->state->encoder_mask) {dpu_encoder_kickoff (encoder);
@@ -1126,12 +1143,6 @@ void dpu_crtc_commit_kickoff(struct drm_crtc *crtc) } } -if (atomic_inc_return (&dpu_crtc->frame_pending) ==1 ) { -/* acquire bandwidth and other resources */ -DRM_DEBUG_ATOMIC ("crtc%d first commit\n" , crtc->base.id); - }else -DRM_DEBUG_ATOMIC ("crtc%d commit\n" , crtc->base.id); - dpu_crtc->play_count++;reinit_completion (&dpu_crtc->frame_done_comp);
base-commit: 24a19b953e2a3d0ea5a8c4951a0ab8f32720c907
Signature · 1 line
2.56.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] drm/msm/dpu: unbalanced frame_pending decrements in clone mode --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] drm/msm/dpu: unbalanced frame_pending decrements in clone mode
--
commit 8cc8441c63a33addc5000cc2ffb1005c0a8221b2
Author: Michael Burch <[email protected]>
drm/msm/dpu: count the pending frame before kicking it off
This commit moves the frame_pending counter increment to precede the encoder kickoff to fix an issue where a frame completing quickly could miss the increment. It introduces dpu_crtc_count_pending_frame() and calls it before kickoffs in both clone mode and normal mode.
Quoted 4 lines
drivers/gpu/drm/msm/disp/dpu1/dpu_crtc.c +0
-0
[ ... ]
drivers/gpu/drm/msm/disp/dpu1/dpu_crtc.c
Quoted 12 lines
drivers/gpu/drm/msm/disp/dpu1/dpu_crtc.c +2
-0@@ -1062,6 +1075,8 @@ static int dpu_crtc_kickoff_clone_mode(struct drm_crtc *crtc)dpu_vbif_clear_errors (dpu_kms); +dpu_crtc_count_pending_frame (crtc); +/* * Kickoff real time encoder last as it's the encoder that * will do the flush drivers/gpu/drm/msm/disp/dpu1/dpu_crtc.c +0
-0*/ dpu_encoder_kickoff (wb_encoder);dpu_encoder_kickoff (rt_encoder);
[Severity: High]
Does this code introduce an unbalanced decrement of frame_pending?
In dpu_crtc_kickoff_clone_mode(), dpu_crtc_count_pending_frame() increments frame_pending once per CRTC commit. However, the function subsequently kicks off both wb_encoder and rt_encoder independently.
When they complete, both encoders independently trigger dpu_encoder_frame_done_callback(), queueing two separate DPU_ENCODER_FRAME_EVENT_DONE events to the CRTC:
drivers/gpu/drm/msm/disp/dpu1/dpu_encoder.c:dpu_encoder_frame_done_callback() {
if (!dpu_enc->frame_busy_mask[0]) {
...
dpu_encoder_resource_control(drm_enc, DPU_ENC_RC_EVENT_FRAME_DONE);
if (dpu_enc->crtc)
dpu_crtc_frame_event_cb(dpu_enc->crtc, event);
}
}The worker then drops the counter on each DONE event:
drivers/gpu/drm/msm/disp/dpu1/dpu_crtc.c:dpu_crtc_frame_event_work() {
...
} else if (atomic_dec_return(&dpu_crtc->frame_pending) == 0) {
/* release bandwidth and other resources */
trace_dpu_crtc_frame_event_done(DRMID(crtc), fevent->event);
dpu_core_perf_crtc_release_bw(crtc);
}
...
}If multiple clone-mode commits are interleaved, could the second event from an earlier commit arrive after the next commit sets frame_pending to 1?
This would cause the worker to decrement the count to 0 and release system memory bandwidth prematurely before the real-time encoder has actually finished rendering the next commit. This might lead to FIFO underflows, visual artifacts, or display hangs.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 14 lines · On Tue, Oct 13, 2026, [email protected] wrote:
drivers/gpu/drm/msm/disp/dpu1/dpu_crtc.c static int dpu_crtc_kickoff_clone_mode(struct drm_crtc *crtc)
Quoted 8 lines
drivers/gpu/drm/msm/disp/dpu1/dpu_crtc.c +2
-0+dpu_crtc_count_pending_frame (crtc); +/* * Kickoff real time encoder last as it's the encoder that * will do the flush */ dpu_encoder_kickoff (wb_encoder);dpu_encoder_kickoff (rt_encoder);Does this code introduce an unbalanced decrement of
frame_pending?In
dpu_crtc_kickoff_clone_mode(),dpu_crtc_count_pending_frame()incrementsframe_pendingonce per CRTC commit. However, the function subsequently kicks off bothwb_encoderandrt_encoderindependently.
[...]
Quoted 2 lines
If multiple clone-mode commits are interleaved, could the second event from an earlier commit arrive after the next commit sets
frame_pendingto 1?
Yes, the accounting is unbalanced in clone mode, but it is not introduced by this patch. Without it, dpu_crtc_commit_kickoff() also counts one frame per commit, and both the writeback and the real-time encoder report FRAME_EVENT_DONE (and, on a frame-done timeout, FRAME_EVENT_ERROR) to the CRTC. The patch changes where the frame is counted, not how many times, so clone mode needs a separate fix.
The encoder already identifies the writeback encoder of a clone-mode
CRTC: dpu_encoder_kickoff() skips its flush when drm_enc->encoder_type == DRM_MODE_ENCODER_VIRTUAL && dpu_enc->cwb_mask. Applying the same test in dpu_encoder_frame_done_callback() and dpu_encoder_frame_done_timeout(), so that only the real-time encoder reports to the CRTC, would keep one event per counted frame. The writeback job is still completed through drm_writeback_signal_completion(). Jessica, does that fit the CWB design?
I cannot test this here. dpu_encoder_get_clones() pairs writeback only with DSI encoders, so on X1E80100, where the panel is eDP, no encoder can share a CRTC with writeback and the DRM core rejects such a commit. That also corrects my test notes: this machine does expose a Writeback connector (WB_2); what it cannot do is clone mode.
Michael