Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] drm/bridge: dw-dp: dw_dp_hpd_work() retrains link before
  parsing sink DPCD
- [Medium] drm/bridge: dw-dp: unlocked link state check and TOCTOU in
  dw_dp_link_retrain()
--

commit ecb979561e6b36e93516c81e2368f1af68040e73
Author: Yury Smirnov <[email protected]>

drm/bridge: synopsys: dw-dp: Retrain on hotplug with pre-enabled link

This commit retrains an already-enabled DisplayPort link on a long HPD
pulse when a sink is detected before sending the DRM hotplug event.

> diff --git a/drivers/gpu/drm/bridge/synopsys/dw-dp.c 
> b/drivers/gpu/drm/bridge/synopsys/dw-dp.c
> index bf3a53463394f..b245d62009139 100644
> --- a/drivers/gpu/drm/bridge/synopsys/dw-dp.c
> +++ b/drivers/gpu/drm/bridge/synopsys/dw-dp.c
> @@ -2394,6 +2394,12 @@ static void dw_dp_hpd_work(struct work_struct *work)
>                               dev_warn(dp->dev, "Retrain link failed\n");
>               }
>       } else {
> +             if (dp->link.enabled && dw_dp_hpd_detect(dp)) {
> +                     ret = dw_dp_link_retrain(dp);
> +                     if (ret)
> +                             dev_warn(dp->dev, "Retrain link failed\n");
> +             }
> +
>               drm_helper_hpd_irq_event(dp->bridge.dev);
>       }
>  }

[Severity: High]
Should dw_dp_link_retrain() run after drm_helper_hpd_irq_event() rather
than before it?

Connector detection in drm_helper_hpd_irq_event() invokes
dw_dp_bridge_detect() -> dw_dp_hpd_detect_link() -> dw_dp_link_parse(),
which calls dw_dp_link_reset() (zeroing link->rate, link->lanes, and
link->caps) and then reads the sink's DPCD capabilities.

If a previous unplug detection failed after dw_dp_link_reset() (for
example, on a DP branch device or USB-C adapter when sink_count dropped to
0 and dw_dp_link_parse() returned -ENODEV), dp->link.rate and
dp->link.lanes are still 0 when the sink is replugged.  Calling
dw_dp_link_retrain() first then runs dw_dp_phy_configure(dp, 0, 0, false),
putting the PHY into P3 powerdown with all lanes disabled and failing link
training before dw_dp_link_parse() has re-read the DPCD.

Even when dp->link was not zeroed, retraining before
drm_helper_hpd_irq_event() trains against stale DPCD capabilities without
transitioning the replugged sink to DP_SET_POWER_D0 via
drm_dp_link_power_up(), and the subsequent dw_dp_link_parse() call in
drm_helper_hpd_irq_event() immediately resets dp->link and overwrites any
downgraded link->rate.

[Severity: Medium]
Can dw_dp_link_retrain() race with a concurrent atomic disable and train a
link whose PHY has already been powered off?

dw_dp_hpd_work() checks dp->link.enabled and dw_dp_link_retrain() checks
dw_dp_needs_link_retrain() before acquiring
dev->mode_config.connection_mutex, and neither is rechecked after acquiring
the lock:

CPU0 (kworker)                    CPU1 (modeset)
dw_dp_hpd_work()
  if (dp->link.enabled && ...)
  dw_dp_link_retrain()
    dw_dp_needs_link_retrain()
                                  dw_dp_bridge_atomic_disable()
                                    dw_dp_link_disable()
                                      dw_dp_phy_xmit_enable(dp, 0)
                                      phy_power_off(dp->phy)
                                      link->enabled = false
    drm_modeset_lock(&connection_mutex)
    dw_dp_link_train()
      dw_dp_phy_configure()       <-- configures powered-off PHY

Rechecking dp->link.enabled and dw_dp_needs_link_retrain() after locking
connection_mutex would close this window.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/20261114-synopsys-dw-dp-improvements-v12-0-d42d3dc5b...@collabora.com?part=23

Reply via email to