Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] drm/amd/display: active_vblank_irq_count leak in
  dm_gpureset_toggle_vblank()
- [High] drm/amd/display: race and UAF on acrtc->base.state in
  dm_gpureset_toggle_vblank()
- [Medium] drm/amd/display: dm_gpureset_toggle_vblank() enables vblank
  on DPMS-off CRTC
- [Medium] drm/amd/display: missing vblank rollback in dm_suspend() on
  reset error path
--

commit 55fe8aa3c39160c8faed0a29af7b0acd9dcda97b
Author: Prike Liang <[email protected]>

drm/amd/display: turn vblank off in DRM across GPU reset

This commit adds dm_gpureset_toggle_vblank() to call drm_crtc_vblank_off()
before stopping streams in dm_suspend() and drm_crtc_vblank_on_config()
after restoring them in dm_resume() during a GPU reset, along with KUnit
tests.

> 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 4184d8fda10ea..b39c975e0e33a 100644
> --- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c
> +++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c
> @@ -1494,6 +1494,56 @@ STATIC_IFN_KUNIT void 
> dm_gpureset_toggle_interrupts(struct amdgpu_device *adev,
>  }
>  EXPORT_IF_KUNIT(dm_gpureset_toggle_interrupts);
>  
> +/**
> + * dm_gpureset_toggle_vblank() - Turn DRM vblank off or on around a GPU reset
> + * @adev: amdgpu device
> + * @state: DC state whose streams the reset stops and restores
> + * @enable: false before the streams are stopped, true once they are restored
> + *
> + * The GPU reset path stops and restores the streams through DC directly and
> + * bypasses manage_dm_interrupts(). Turn vblank off in DRM like a CRTC
> + * disable does, so that drm_crtc_vblank_get() fails while no vblank
> + * interrupt can arrive, instead of letting vblank waiters such as the fbdev
> + * damage worker sleep until they time out.
> + */
> +STATIC_IFN_KUNIT void dm_gpureset_toggle_vblank(struct amdgpu_device *adev,
> +                                             struct dc_state *state, bool 
> enable)
> +{
> +     struct drm_vblank_crtc_config config;
> +     struct amdgpu_crtc *acrtc;
> +     int i;
> +
> +     for (i = 0; i < state->stream_count; i++) {
> +             int otg_inst = state->stream_status[i].primary_otg_inst;
> +
> +             acrtc = amdgpu_dm_get_crtc_by_otg_inst(adev, otg_inst);
> +             if (!acrtc)
> +                     continue;
> +
> +             if (!enable) {
> +                     drm_crtc_vblank_off(&acrtc->base);
> +                     continue;
> +             }

[Severity: High]
Does turning vblank off during reset leak dm->active_vblank_irq_count and
leave acrtc->ism out of idle when vblank was enabled before the reset?

Before the reset starts, enabling vblank queues vblank_control_work with
enable = true to amdgpu_dm_crtc_vblank_control_worker(), which increments
dm->active_vblank_irq_count and sends DM_ISM_EVENT_EXIT_IDLE_REQUESTED.

When dm_suspend() calls drm_crtc_vblank_off() during reset,
drm_vblank_disable_and_save() calls amdgpu_dm_crtc_set_vblank(crtc, false)
and sets vblank->enabled = false, but amdgpu_dm_crtc_set_vblank() returns
early without queuing vblank_control_work because amdgpu_in_reset(adev) is
true.  drm_crtc_vblank_off() then wakes any waiters (such as
drm_fb_helper_damage_work), which drop their vblank references.  When
dm_resume() later calls drm_crtc_vblank_on_config(), vblank->refcount is 0
and config.offdelay_ms is non-zero, so vblank->enabled stays false and no
matching disable work is ever queued.  Because
dm_ism_commit_idle_optimization_state() requires
dm->active_vblank_irq_count == 0, display idle power optimizations remain
blocked after the reset.

> +
> +             /*
> +              * A modeset waiting for dc_lock may be turning the CRTC off,
> +              * and amdgpu_dm_crtc_set_vblank() refuses to enable vblank on
> +              * it then. Leave its vblank off.
> +              */
> +             if (!acrtc->base.enabled)
> +                     continue;
> +
> +             /* drm_crtc_vblank_off() cleared the timestamping constants. */
> +             drm_calc_timestamping_constants(&acrtc->base,
> +                                             
> &acrtc->base.state->adjusted_mode);

[Severity: High]
Can reading acrtc->base.state->adjusted_mode here race with a concurrent
atomic commit and cause a use-after-free?

acrtc->base.state is protected by acrtc->base.mutex, while dm_resume() only
holds dm->dc_lock.  Since 1a2b3c4d5e6f is not blocking DRM clients during
recovery (ebc3351cc4a9 ("drm/amdgpu: remove suspend/resume DRM clients
during GPU recovery")), a non-modeset commit on the active CRTC that
updates no enabled planes (planes_count == 0 with
acrtc_state->active_planes > 0 in amdgpu_dm_commit_planes()) never takes
dm->dc_lock:

CPU0                                  CPU1
drm_atomic_helper_commit()
  drm_atomic_helper_swap_state()
    /* swaps acrtc->base.state */
                                      dm_resume()
                                        dm_gpureset_toggle_vblank(..., true)
                                          mode = 
&acrtc->base.state->adjusted_mode
  commit_tail()
    amdgpu_dm_atomic_commit_tail()
    drm_atomic_commit_put()
      amdgpu_dm_crtc_destroy_state()
        kfree(old_crtc_state)
                                          drm_calc_timestamping_constants(..., 
mode)
                                            <-- use-after-free

Even for a modeset commit turning the CRTC off that later waits on
dm->dc_lock in amdgpu_dm_commit_streams(), drm_atomic_helper_swap_state()
installs the disabled new_crtc_state (with adjusted_mode.crtc_clock == 0)
before commit_tail() runs drm_atomic_helper_update_legacy_modeset_state()
to clear acrtc->base.enabled, so dm_gpureset_toggle_vblank() can still see
acrtc->base.enabled == true while reading the zeroed new_crtc_state.
Could dm_gpureset_toggle_vblank() derive the timing from the cached
dc_stream_state in state->streams[i] instead of dereferencing
acrtc->base.state?

> +
> +             /* Keep the off delay that manage_dm_interrupts() configured. */
> +             config = drm_crtc_vblank_crtc(&acrtc->base)->config;
> +             drm_crtc_vblank_on_config(&acrtc->base, &config);
> +     }
> +}
> +EXPORT_IF_KUNIT(dm_gpureset_toggle_vblank);

[Severity: Medium]
Can a concurrent DPMS-off commit waiting on dm->dc_lock leave DRM vblank
enabled on an inactive CRTC?

For a DPMS-off commit (new_crtc_state->enable == true,
new_crtc_state->active == false),
drm_atomic_helper_update_legacy_modeset_state() keeps acrtc->base.enabled
as true.  In amdgpu_dm_commit_streams(), the commit first calls
manage_dm_interrupts(adev, acrtc, NULL) (turning vblank off) and then
blocks on mutex_lock(&dm->dc_lock) to disable the cursor before
remove_stream() sets acrtc->otg_inst = -1:

drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c:amdgpu_dm_commit_streams() {
    ...
                if (old_crtc_state->active &&
                    (!new_crtc_state->active ||
                     drm_atomic_crtc_needs_modeset(new_crtc_state))) {
                        manage_dm_interrupts(adev, acrtc, NULL);
                        amdgpu_dm_ism_flush(&acrtc->ism);
                        dc_stream_release(dm_old_crtc_state->stream);
                }
    ...
                if (old_crtc_state->active && !new_crtc_state->active) {
                        struct dc_cursor_position position;

                        memset(&position, 0, sizeof(position));
                        mutex_lock(&dm->dc_lock);
    ...
}

While the commit is blocked there, acrtc->otg_inst still matches the cached
stream and acrtc->base.enabled is true, so dm_gpureset_toggle_vblank()
calls drm_crtc_vblank_on_config().  When dm_resume() drops dm->dc_lock, the
DPMS-off commit finishes removing the stream without calling
manage_dm_interrupts() again, leaving drm_crtc_vblank_get() succeeding on a
CRTC with no active stream.

> @@ -1628,6 +1678,7 @@ static int dm_suspend(struct amdgpu_ip_block *ip_block)
>                       return -ENOMEM;
>               }
>  
> +             dm_gpureset_toggle_vblank(adev, dm->cached_dc_state, false);
>               dm_gpureset_toggle_interrupts(adev, dm->cached_dc_state, false);
>  
>               res = amdgpu_dm_commit_zero_streams(dm->dc);

[Severity: Medium]
If amdgpu_dm_commit_zero_streams() fails right after this, dm_suspend()
frees dm->cached_dc_state and returns -EINVAL without re-enabling vblank
(or interrupts).  Because amdgpu_ip_block_suspend() leaves
ip_block->status.hw set to true on error, amdgpu_device_ip_resume_phase3()
skips dm_resume(), leaving DRM vblank permanently off on the active CRTCs.
Should dm_gpureset_toggle_vblank() and dm_gpureset_toggle_interrupts() be
rolled back on that error path, or called after
amdgpu_dm_commit_zero_streams() succeeds?

-- 
Sashiko AI review · 
https://sashiko.dev/#/patchset/[email protected]?part=5

Reply via email to