dm_arm_vblank_event_pre_programming() takes a vblank reference for a
page flip or cursor update that carries an event, and the handler that
later sends the event drops that reference again. The return value of
drm_crtc_vblank_get() is ignored, though. If vblank is off, the event
is armed anyway, and the handler that eventually sends it drops a
reference that was never taken, which unbalances the vblank refcount.

That can't happen today: DM only turns vblank off for a CRTC that it
disables, and a disabled CRTC has no active planes. The next patch also
turns vblank off while a GPU reset has the streams stopped, though.
Page flips that don't modify the DC state skip
amdgpu_dm_commit_streams() and only take dc_lock after this point, so
they still get here while the reset holds it.

Send the event right away when no vblank reference can be taken, like
drm_crtc_vblank_off() does for queued vblank events. No vblank interrupt
could deliver it in that case anyway.

Add a KUnit test for it.

Cc: Harry Wentland <[email protected]>
Cc: Leo Li <[email protected]>
Assisted-by: Copilot:claude-opus-5.5
Signed-off-by: Prike Liang <[email protected]>
---
 .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c | 15 ++++++++--
 .../display/amdgpu_dm/tests/amdgpu_dm_test.c  | 29 +++++++++++++++++++
 2 files changed, 42 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c 
b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c
index 33c73ca9cff1..d3882ffcb5d4 100644
--- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c
+++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c
@@ -3825,6 +3825,11 @@ EXPORT_IF_KUNIT(dm_arm_vblank_event);
  * Grab a reference on the vblank counter if a page flip or cursor update is to
  * be programmed. Do this before programming so the HW is not in any
  * idle-optimized state (such as PSR).
+ *
+ * If vblank is off, no vblank interrupt can deliver the event, and the
+ * handlers that send it would drop a reference that was never taken. Send
+ * the event right away instead, like drm_crtc_vblank_off() does for queued
+ * vblank events.
  */
 STATIC_IFN_KUNIT void dm_arm_vblank_event_pre_programming(struct amdgpu_crtc 
*acrtc,
                                                          struct dm_crtc_state 
*acrtc_state,
@@ -3836,8 +3841,14 @@ STATIC_IFN_KUNIT void 
dm_arm_vblank_event_pre_programming(struct amdgpu_crtc *ac
        if (!acrtc->base.state->event || acrtc_state->active_planes == 0)
                return;
 
-       if (pflip_update || cursor_update)
-               drm_crtc_vblank_get(&acrtc->base);
+       if (!pflip_update && !cursor_update)
+               return;
+
+       if (drm_crtc_vblank_get(&acrtc->base)) {
+               drm_crtc_send_vblank_event(&acrtc->base,
+                                          acrtc->base.state->event);
+               acrtc->base.state->event = NULL;
+       }
 }
 EXPORT_IF_KUNIT(dm_arm_vblank_event_pre_programming);
 
diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/tests/amdgpu_dm_test.c 
b/drivers/gpu/drm/amd/display/amdgpu_dm/tests/amdgpu_dm_test.c
index b9272b28f53c..ebec1260c510 100644
--- a/drivers/gpu/drm/amd/display/amdgpu_dm/tests/amdgpu_dm_test.c
+++ b/drivers/gpu/drm/amd/display/amdgpu_dm/tests/amdgpu_dm_test.c
@@ -1812,6 +1812,34 @@ static void 
dm_test_arm_vblank_pre_programming_update(struct kunit *test)
        drm_crtc_vblank_put(&ctx->acrtc->base);
 }
 
+/**
+ * dm_test_arm_vblank_pre_programming_vblank_off - Test the event is sent with 
vblank off
+ * @test: The KUnit test context
+ *
+ * No vblank interrupt can deliver the event, and the handlers that send it
+ * must not drop a vblank reference that was never taken.
+ */
+static void dm_test_arm_vblank_pre_programming_vblank_off(struct kunit *test)
+{
+       struct dm_test_vblank_ctx *ctx = dm_test_vblank_ctx_alloc(test);
+       struct drm_vblank_crtc *vblank;
+
+       KUNIT_ASSERT_EQ(test, drm_vblank_init(&ctx->adev->ddev, 1), 0);
+       vblank = drm_crtc_vblank_crtc(&ctx->acrtc->base);
+       drm_crtc_vblank_off(&ctx->acrtc->base);
+
+       /* Sending an event that has no file frees it. */
+       ctx->acrtc_state->base.event = kzalloc_obj(*ctx->event, GFP_KERNEL);
+       KUNIT_ASSERT_NOT_NULL(test, ctx->acrtc_state->base.event);
+
+       dm_test_arm_vblank_pre_programming(ctx, true, false);
+
+       KUNIT_EXPECT_NULL(test, ctx->acrtc_state->base.event);
+       /* Only drm_crtc_vblank_off() holds a reference. */
+       KUNIT_EXPECT_EQ(test, atomic_read(&vblank->refcount), 1);
+       kfree(ctx->acrtc_state->base.event);
+}
+
 /**
  * dm_test_arm_vblank_event_no_event - Test a commit without an event is a 
no-op
  * @test: The KUnit test context
@@ -5298,6 +5326,7 @@ static struct kunit_case amdgpu_dm_tests[] = {
        KUNIT_CASE(dm_test_arm_vblank_pre_programming_no_event),
        KUNIT_CASE(dm_test_arm_vblank_pre_programming_no_planes),
        KUNIT_CASE(dm_test_arm_vblank_pre_programming_update),
+       KUNIT_CASE(dm_test_arm_vblank_pre_programming_vblank_off),
        /* dm_update_pflip_irq_state */
        KUNIT_CASE(dm_test_update_pflip_irq_state_dcn),
        KUNIT_CASE(dm_test_update_pflip_irq_state_dce),
-- 
2.34.1

Reply via email to