Thank you for your contribution! Sashiko AI review found 9 potential
issues to consider:
- [Medium] drm/bridge: trilinear: dptx_deinit() attach race leaves HPD
  work uncancelled
- [Medium] drm/bridge: trilinear: UAF in device_link_del() after
  consumer device removal
- [Medium] drm/bridge: trilinear: device_link_del() outside dp->lock
  races with purge
- [Medium] drm/bridge: trilinear: 10s poll in dptx_wait_phy_dp_mode()
  stalls suspend
- [Medium] drm/bridge: trilinear: HPD work queued before
  drm_bridge_hpd_enable()
- [Medium] drm/bridge: trilinear: dptx_train_loop() lacks timeout/HPD
  abort under lock
- [Medium] drm/bridge: trilinear: undivided stream clock rate in
  dptx_ctrl_stream_on()
- [Low] drm/bridge: trilinear: 32-bit overflow in
  dptx_hw_stream_timing_set()
- [Low] drm/bridge: undocumented dev_set_drvdata() in
  trilinear_dptx_probe()
--

commit ce7e7d7a9064fd3b813169fe3cb4592f1803c9c6
Author: Luo Liu <[email protected]>

drm/bridge: trilinear: add the DPTX core and build glue

This commit adds the Trilinear DisplayPort transmitter core driver (probe,
hotplug workers, stream state machine, and PM/teardown helpers) along with
its Kconfig and Makefile entries.

> diff --git a/drivers/gpu/drm/bridge/trilinear/dptx_core.c 
> b/drivers/gpu/drm/bridge/trilinear/dptx_core.c
> new file mode 100644
> index 0000000000000..b4d3248ba5a83
> --- /dev/null
> +++ b/drivers/gpu/drm/bridge/trilinear/dptx_core.c

[ ... ]

> +     dp->hw->ops->stream_timing(dp->hw, stream_id, mode,
> +                                dp->config.bpp, dp->mode.lane_cnt,
> +                                dp->mode.link_rate, dp->pixel_per_cycle);

[Severity: Low]
Can the secondary data window calculation in dptx_hw_stream_timing_set()
overflow 32-bit arithmetic?

drivers/gpu/drm/bridge/trilinear/dptx_hw.c:dptx_hw_stream_timing_set() {
        ...
        dptx_hw_write(hw, DPTX_SECONDARY_DATA_WINDOW(stream_id),
                      (mode->htotal - mode->hdisplay) * (link_rate / 40) *
                      9 / mode->clock);
        ...
}

Because mode->htotal and mode->hdisplay are u16 and link_rate is u32,
the numerator is computed in 32-bit unsigned arithmetic.  At HBR2
(540000 kHz), any mode with horizontal blanking of at least 35350 pixels
wraps modulo 2^32 before dividing by mode->clock, misprogramming the
secondary data packet window.

Promoting the numerator to u64 with mul_u32_u32() and div_u64() avoids
the overflow.

[ ... ]

> +     pixel_clock = (unsigned long)mode->clock * 1000;
> +     ret = dp->hw->ops->stream_clock_rate_set(dp->hw, stream_id, 
> pixel_clock);

[Severity: Medium]
Should pixel_clock be divided by dp->pixel_per_cycle before calling
stream_clock_rate_set()?

For non-YUV420 modes above DPTX_MAX_FREQ (600 MHz), dptx_pick_ppc()
selects 2 pixels per cycle and dptx_hw_stream_timing_set() programs
DPTX_USER_PIXEL_COUNT to 2, but stream_clock_rate_set() still passes the
undivided pixel clock (up to 1.2 GHz) directly to clk_set_rate().

Unless the platform clock tree or adapter divides the video clock
internally when 2ppc is enabled, clk_set_rate() can fail, clamp to
600 MHz, or clock the video input at twice the required rate, breaking
output for modes above 600 MHz.

[ ... ]

> +     for (i = 0; i < DP_MAX_TRAINING_LOOP; i++) {
> +             for (j = 0; j <= 3; j++) {
> +                     if (!(dp->state & DPTX_STATE_READY)) {
> +                             ret = -ENODEV;
> +                             goto err_restore;
> +                     }
> +
> +                     ret = dp->hw->ops->link_train(dp->hw, dp->phy,
> +                                                   mode->bw_code,
> +                                                   mode->lane_cnt,
> +                                                   DPTX_MAX_LANES);
> +                     if (!ret) {
> +                             if (dp->active_stream_cnt &&
> +                                 (mode->bw_code != prev_bw ||
> +                                  mode->lane_cnt != prev_lanes))
> +                                     dptx_active_streams(dp);
> +                             return 0;
> +                     }
> +
> +                     ret = dptx_mode_configure(dp, mode->pclock, bw_code,
> +                                               bpp, true);
> +                     if (ret < 0)
> +                             break;

[Severity: Medium]
Can dptx_train_loop() hold dp->lock through repeated AUX timeouts if the
sink is unplugged or stops responding during link training?

Both callers (dptx_core_on() and dptx_hpd_irq_work_func()) hold dp->lock
across dptx_train_loop(), so DPTX_STATE_READY cannot change inside the
loop.  When link_train() fails with -ETIMEDOUT, the loop ignores the
error code and HPD state, downshifting through every link rate across
DP_MAX_TRAINING_LOOP iterations.

Because drm_dp_dpcd_access() retries each failed DPCD access 32 times,
this stalls dp->lock for hundreds of milliseconds (up to ~1.5 s if the
4 ms AUX or 100 ms PHY-ready poll ceiling is hit), blocking disconnect
handling, suspend, and modeset callbacks, and masks -ETIMEDOUT as -EIO.

Aborting the loop when !dptx_get_hpd_state(dp) or when link_train()
returns a transport error other than -EIO avoids the stall.

[ ... ]

> +static bool dptx_wait_phy_dp_mode(struct dptx_dev *dp)
> +{
> +     enum phy_mode mode;
> +
> +     read_poll_timeout(phy_get_mode, mode,
> +                       mode == PHY_MODE_DP || dptx_hpd_wait_aborted(dp),
> +                       100 * USEC_PER_MSEC,
> +                       DP_HPD_MAX_TRIES * 100 * USEC_PER_MSEC, false,
> +                       dp->phy);

[Severity: Medium]
Can this 10-second read_poll_timeout() in dptx_hpd_event_work_func()
stall system suspend freezing?

hpd_event_work runs on system_freezable_wq.  During suspend,
try_to_freeze_tasks() waits for all WQ_FREEZABLE workqueues to drain
before device suspend callbacks run dptx_pm_prepare() to set
DPTX_STATE_SUSPENDED, so dptx_hpd_wait_aborted() stays false.

If the PHY transition depends on userspace or another freezable
workqueue, the poll blocks freezing for the full 10 seconds (or causes
freezing to fail if freeze_timeout_msecs is shorter).  Re-arming
hpd_event_work with a 100 ms delay and a retry counter instead of
sleeping inside the worker avoids blocking suspend and teardown.

[ ... ]

> +static void dptx_replay_deferred_hotplug(struct dptx_dev *dp)
> +{
> +     guard(mutex)(&dp->lock);
> +
> +     if (!dptx_deferred_hotplug_pending(dp))
> +             return;
> +
> +     if (dptx_get_hpd_state(dp)) {
> +             queue_delayed_work(system_freezable_wq, &dp->hpd_event_work, 0);
> +             return;
> +     }
> +
> +     if (!dp->oob_hpd || phy_get_mode(dp->phy) != PHY_MODE_DP)
> +             return;
> +
> +     if (!dp->hw->ops->sink_present(dp->hw))
> +             return;
> +
> +     dp->force_hpd = connector_status_connected;
> +
> +     queue_delayed_work(system_freezable_wq, &dp->hpd_event_work, 0);
> +}

[Severity: Medium]
Can the deferred hotplug queued here be silently dropped before
bridge->hpd_cb is installed?

dptx_bridge_attach() calls dptx_replay_deferred_hotplug() with zero
delay, which runs before drm_bridge_connector_init() and
drm_kms_helper_poll_init() -> drm_bridge_hpd_enable() register
bridge->hpd_cb.  Because bridge->dev is already set,
dptx_hpd_event_work_func() calls drm_bridge_hpd_notify() while
bridge->hpd_cb is still NULL and the event is lost.

Since dptx_bridge_funcs does not implement .hpd_enable, the dropped
hotplug is never replayed once HPD callbacks are wired up, leaving
connector->status as unknown when no in-kernel DRM client probes at
boot.  Replaying deferred hotplug from .hpd_enable avoids losing the
initial plug event.

[ ... ]

> +static void dptx_deinit(struct dptx_dev *dp)
> +{
> +     struct device_link *link = NULL;
> +
> +     scoped_guard(mutex, &dp->lock) {
> +             if (!(dp->state & DPTX_STATE_CONFIGURED))
> +                     return;
> +
> +             WRITE_ONCE(dp->hpd_stopped, true);
> +             dptx_irq_off(dp);
> +     }
> +
> +     dptx_cancel_hpd_works(dp);
> +
> +     scoped_guard(mutex, &dp->lock) {
> +             dp->released = true;

[Severity: Medium]
Can a concurrent dptx_bridge_attach() race with dptx_deinit() and leave
hpd_event_work running after teardown?

Because trilinear_dptx_remove() and dptx_dev_release() run before
devm_drm_bridge_add() removes the bridge from bridge_list, a display
controller can attach during the unlocked window around
dptx_cancel_hpd_works(), while dp->released is still false:

CPU0                              CPU1
dptx_deinit()
  lock(&dp->lock)
  dp->hpd_stopped = true
  unlock(&dp->lock)
  dptx_cancel_hpd_works()
                                  dptx_bridge_attach()
                                    lock(&dp->lock)
                                    /* dp->released is false */
                                    dp->hpd_stopped = false
                                    dp->attached = true
                                    unlock(&dp->lock)
                                    replay_deferred_hotplug()
                                      queue_delayed_work(hpd_event_work)
  lock(&dp->lock)
  dp->released = true
  ...
  unlock(&dp->lock)
devres_release_all()
  drm_bridge_remove()
  devm_phy_release()
                                  dptx_hpd_event_work_func()
                                    phy_get_mode(dp->phy) <-- use-after-free

The worker can also call dptx_notify_hpd() after drm_bridge_remove()
destroys bridge->hpd_mutex, and because dp->hpd_stopped stays false,
dptx_hpd_notify() can continue queueing HPD work after teardown.

[ ... ]

> +             link = dp->link;
> +             dp->link = NULL;
> +     }
> +
> +     if (link)
> +             device_link_del(link);
> +}

[Severity: Medium]
Can device_link_del(link) here (and in dptx_bridge_detach()) trigger a
use-after-free if the consumer device is removed first?

dptx_bridge_attach() caches a raw struct device_link * in dp->link.  If
the display controller device (bridge->dev->dev) is removed while a
client still holds /dev/dri/cardN open, device_del() ->
device_links_purge() frees that link before deferred
drm_mode_config_cleanup() runs dptx_bridge_detach(), and without
clearing dp->link.

A subsequent dptx_deinit() or dptx_bridge_detach() then passes the
freed pointer to device_link_del().  Using device_link_remove() with the
consumer and supplier devices avoids dereferencing a stale link pointer.

[Severity: Medium]
Can releasing dp->lock before calling device_link_del(link) race with
device_links_purge() on the other teardown path?

Both dptx_deinit() and dptx_bridge_detach() clear dp->link under
dp->lock and call device_link_del(link) after unlocking:

CPU0                              CPU1
dptx_bridge_detach()
  lock(&dp->lock)
  link = dp->link
  dp->link = NULL
  unlock(&dp->lock)
                                  device_del(dp->dev)
                                    trilinear_dptx_remove()
                                      dptx_deinit()
                                        /* sees dp->link == NULL */
                                    device_links_purge(dp->dev)
                                      __device_link_del(&link->kref)
  device_link_del(link)  <-- use-after-free

The symmetric race occurs when dptx_deinit() clears dp->link first and
dptx_bridge_detach() returns to consumer device_del().  Removing the
link while still holding dp->lock prevents the other path from reaching
device_links_purge() first.

[ ... ]

> +     dev_set_drvdata(dev, dp);

[Severity: Low]
Should trilinear_dptx_probe() leave dev_set_drvdata() to the platform
glue driver, or document that it takes ownership of dev->driver_data?

trilinear_dptx_probe() returns struct dptx_dev *, all companion helpers
take dp explicitly, and nothing in drivers/gpu/drm/bridge/trilinear/
calls dev_get_drvdata().  Silently overwriting drvdata breaks platform
drivers that store their own private data before calling
trilinear_dptx_probe().

Note that commit "drm/cix: add the Sky1 DisplayPort platform glue"
currently relies on this side effect instead of calling
platform_set_drvdata().

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=4

Reply via email to