Thank you for your contribution! Sashiko AI review found 8 potential
issues to consider:
- [High] drm/bridge: trilinear: dptx_bridge_hpd_enable() skipped with
  downstream HPD
- [Medium] drm/bridge: trilinear: dptx_bridge_atomic_check() gates on
  crtc active
- [Medium] drm/bridge: trilinear: UAF of dp->dev in dptx_bridge_detach()
- [Medium] drm/bridge: trilinear: UAF of dp->dev in
  dptx_bridge_mode_set()
- [Medium] drm/bridge: trilinear: stale connector_state->max_bpc in
  dptx_cal_bpc()
- [Medium] drm/bridge: trilinear: detect_task echo filter bypassed with
  next_bridge
- [Low] drm/bridge: trilinear: dptx_format_ok() ignores connector
  ycbcr_420_allowed
- [Low] drm/bridge: trilinear: dptx_bridge_get_modes() skips clearing
  stale EDID
--

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

drm/bridge: trilinear: add the DPTX DRM bridge

This patch adds the DRM bridge implementation for the Trilinear DisplayPort
transmitter, including connector detection, EDID mode reading, mode
validation, atomic check/enable/disable, and bridge attach/detach helpers.

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

[ ... ]

> +static enum drm_connector_status dptx_bridge_detect(struct drm_bridge 
> *bridge,
> +                                                 struct drm_connector 
> *connector)
> +{
> +     struct dptx_dev *dp = bridge_to_dp(bridge);
> +     enum drm_connector_status status;
> +
> +     status = dp->ops->detect_status(dp);
> +     WRITE_ONCE(dp->detect_task, current);
> +
> +     return status;
> +}
> +
> +static void dptx_hpd_notify(struct dptx_dev *dp,
> +                         enum drm_connector_status hpd_state)
> +{
> +     if (READ_ONCE(dp->detect_task) == current) {
> +             WRITE_ONCE(dp->detect_task, NULL);
> +             return;
> +     }
> +
> +     if (READ_ONCE(dp->notify_task) == current) {
> +             WRITE_ONCE(dp->notify_task, NULL);
> +             return;
> +     }
> +
> +     if (!dp->oob_hpd)
> +             return;
> +
> +     guard(mutex)(&dp->lock);
> +
> +     if (dp->hpd_stopped ||
> +         hpd_state == connector_status_unknown)
> +             return;
> +
> +     dp->force_hpd = hpd_state;
> +
> +     if (!(dp->state & DPTX_STATE_SUSPENDED)) {
> +             queue_delayed_work(system_freezable_wq, &dp->hpd_event_work, 0);
> +             if (hpd_state == connector_status_connected)
> +                     queue_delayed_work(system_freezable_wq,
> +                                        &dp->hpd_irq_work,
> +                                        msecs_to_jiffies(20));
> +     }
> +}

[Severity: Medium]
Can chaining a downstream bridge that advertises DRM_BRIDGE_OP_DETECT bypass
the detect_task echo filter?

drm_bridge_connector_init() selects the last bridge in the chain advertising
DRM_BRIDGE_OP_DETECT as bridge_detect.  When dp->oob_hpd is true and
dp->next_bridge sets DRM_BRIDGE_OP_DETECT, drm_bridge_connector_detect_ctx()
calls detect on dp->next_bridge instead of dptx_bridge_detect(), and then
broadcasts hpd_notify to every bridge in the chain.

Because dp->detect_task is never set, dptx_hpd_notify() treats every
connector probe as a new out-of-band hotplug event and queues hpd_event_work
and hpd_irq_work.  If dp->next_bridge does not also set DRM_BRIDGE_OP_HPD
(leaving dp->bridge as bridge_hpd), hpd_event_work triggers
drm_bridge_connector_handle_hpd(), which clears dp->notify_task before
drm_kms_helper_connector_hotplug_event() reprobes the connector, creating an
infinite hotplug and workqueue loop.

[ ... ]

> +static int dptx_bridge_get_modes(struct drm_bridge *bridge,
> +                              struct drm_connector *connector)
> +{
> +     struct dptx_dev *dp = bridge_to_dp(bridge);
> +     const struct drm_edid *edid;
> +     struct drm_display_mode *mode;
> +     int ret;
> +
> +     scoped_guard(mutex, &dp->lock) {
> +             if (dp->released ||
> +                 (dp->state & DPTX_STATE_SUSPENDED) ||
> +                 !(dp->state & DPTX_STATE_INITIALIZED) ||
> +                 !(dp->state & DPTX_STATE_CONNECTED))
> +                     return 0;
> +
> +             edid = drm_edid_read(connector);
> +     }

[Severity: Low]
Should this early return clear the connector's EDID with
drm_edid_connector_update(connector, NULL)?

drm_helper_probe_single_connector_modes() only clears the EDID property when
connector->status is connector_status_disconnected.  When a connector is
forced on (DRM_FORCE_ON) after unplugging a sink, or probed while suspended
(connector_status_unknown), returning 0 here skips the later
drm_edid_connector_update(connector, NULL) call and leaves stale
connector->edid_blob_ptr and connector->display_info from the previous sink
while connector->modes is replaced with fallback modes.

Clearing the EDID outside dp->lock on this early-return path would match
drm_bridge_connector_get_modes_edid().

[ ... ]

> +static int dptx_cal_bpc(struct drm_connector_state *connector_state,
> +                     int format, int bpc)
> +{
> +     int max_bpc = connector_state->max_bpc;
> +
> +     if (dptx_bt2020_colorspace(connector_state))
> +             max_bpc = min(max_bpc, 10);
> +
> +     bpc = min(bpc, max_bpc);

[Severity: Medium]
Can connector_state->max_bpc be stale here after hotplugging a different
sink?

drm_atomic_check_only() runs drm_atomic_connector_check() (which refreshes
connector_state->max_bpc from display_info.bpc) only for connectors already
in the atomic state before driver atomic_check().  On a CRTC-only modeset
(for example changing MODE_ID without modifying connector properties), the
connector is added later by drm_atomic_add_affected_connectors() with its
previous max_bpc intact.

After swapping a 6-bpc sink for an 8-bpc or 10-bpc sink, a stale max_bpc of
6 causes dptx_cal_bpc() to reject valid YCbCr 4:2:0/4:2:2 modes and clamp
RGB to 6 bpc; swapping from 10-bpc to 8-bpc allows 10-bpc YCbCr 4:2:2 to be
selected for an 8-bpc sink.  Computing max_bpc directly from
connector->display_info.bpc (defaulting to 8 when 0) and clamping by
connector_state->max_requested_bpc avoids stale values.

[ ... ]

> +static bool dptx_format_ok(struct dptx_dev *dp,
> +                        const struct drm_display_info *info,
> +                        const struct drm_display_mode *mode,
> +                        const struct drm_display_mode *adjusted_mode,
> +                        int info_formats, int format)
> +{
> +     if (format == DRM_OUTPUT_COLOR_FORMAT_YCBCR420) {
> +             if (!dp->hw->ops->vsc_supported(dp->hw))
> +                     return false;
> +
> +             /*
> +              * Y420VDB sets the sink-wide 420 bit for every mode. This
> +              * VIC has to be 420-only or 420-also. 420 is one pixel
> +              * per cycle.
> +              */
> +             if (!drm_mode_is_420(info, mode))
> +                     return false;
> +
> +             return adjusted_mode->clock <= DPTX_MAX_FREQ;
> +     }

[Severity: Low]
Does dptx_format_ok() need to check conn_state->connector->ycbcr_420_allowed
before allowing YCbCr 4:2:0?

drm_bridge_connector_init() clears connector->ycbcr_420_allowed when any
bridge in the encoder's chain (such as a downstream bridge attached at
dp->next_bridge) does not support YCbCr 4:2:0.  Because dptx_format_ok()
does not check connector->ycbcr_420_allowed, a 420-also mode whose clock
exceeds RGB/4:2:2 link bandwidth can still be accepted and programmed as
DP_PIXELFORMAT_YUV420, producing broken or blank output.

[ ... ]

> +static int dptx_bridge_mode_set(struct dptx_dev *dp,
> +                             struct drm_crtc_state *crtc_state,
> +                             struct drm_connector_state *conn_state)
> +{
> +     struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
> +     int color_format;
> +     int link_rate;
> +     int bw_code;
> +     int bpc;
> +     int bpp;
> +     int ret;
> +
> +     ret = dp->ops->detect(dp);
> +     if (ret)
> +             return ret;
> +
> +     guard(mutex)(&dp->lock);
> +
> +     ret = dptx_pick_config(dp, conn_state, &crtc_state->mode,
> +                            adjusted_mode, &color_format, &bpc, &bpp);
> +     if (ret)
> +             return ret;
> +
> +     dp->config.format = dptx_drm_format_to_dp(color_format);
> +     dp->config.colorspace = conn_state->colorspace;
> +     dp->config.content_type = conn_state->content_type;
> +     dp->config.bpc = bpc;
> +     dp->config.bpp = bpp;
> +     drm_mode_copy(&dp->adjusted_mode, adjusted_mode);
> +     dp->pixel_per_cycle = dptx_pick_ppc(adjusted_mode, dp->config.format);
> +
> +     link_rate = dptx_pick_link_rate(dp, adjusted_mode, bpp);
> +     bw_code = drm_dp_link_rate_to_bw_code(link_rate);
> +
> +     ret = dp->ops->mode_configure(dp, adjusted_mode->clock, bw_code,
> +                                   bpp, false);
> +     if (ret)
> +             return ret;
> +
> +     dev_dbg(dp->dev,
> +             "%dx%d@%d fmt=%d bpc=%d bpp=%d link=%u lanes=%u ppc=%u\n",
> +             adjusted_mode->hdisplay, adjusted_mode->vdisplay,
> +             drm_mode_vrefresh(adjusted_mode), dp->config.format,
> +             bpc, bpp, bw_code, dp->mode.lane_cnt,
> +             dp->pixel_per_cycle);
> +
> +     return 0;
> +}

[Severity: Medium]
Can concurrent platform device removal free dp->dev before dev_dbg(dp->dev,
...) runs?

struct dptx_dev stores dp->dev without taking a get_device() reference, and
dptx_bridge_mode_set() does not re-check dp->released after reacquiring
dp->lock:

CPU0                              CPU1
dptx_bridge_mode_set()
  dp->ops->detect(dp)
    /* drops dp->lock */
                                  platform_device_unregister()
                                    dptx_deinit()
                                      dp->released = true
                                    device_del(dp->dev)
                                    put_device(dp->dev)  /* freed */
  guard(mutex)(&dp->lock)
  ...
  dev_dbg(dp->dev, ...)  <-- use-after-free

Checking dp->released right after acquiring dp->lock (or pinning dp->dev
with get_device()/put_device() for the lifetime of the bridge) would close
this window.

> +
> +static int dptx_bridge_atomic_check(struct drm_bridge *bridge,
> +                                 struct drm_bridge_state *bridge_state,
> +                                 struct drm_crtc_state *crtc_state,
> +                                 struct drm_connector_state *conn_state)
> +{
> +     struct dptx_dev *dp = bridge_to_dp(bridge);
> +     int bpc;
> +     int bpp;
> +     int ret;
> +     bool vsc_supported;
> +
> +     if (!conn_state->crtc)
> +             return 0;
> +
> +     if (!crtc_state->active)
> +             return 0;
> +
> +     if (!dptx_adjust_mode(&crtc_state->mode, &crtc_state->adjusted_mode))
> +             return -EINVAL;

[Severity: Medium]
Returning early on !crtc_state->active looks like it violates the DRM atomic
DPMS contract -- shouldn't this gate on crtc_state->enable instead?

include/drm/drm_crtc.h specifies that drivers must not consult
crtc_state->active in atomic_check to reject commits, because DPMS ON on an
already-enabled CRTC is expected to succeed.  When userspace commits a
modeset with enable=true and active=false, returning early here skips
dptx_adjust_mode() and dptx_pick_config(), so mode_fixup() in
drm_atomic_helper.c runs the CRTC's mode_fixup() on the unadjusted timings.

On the subsequent DPMS ON commit (active=true, mode_changed=false),
dptx_bridge_atomic_check() then runs and can reject DPMS ON with -EINVAL
(for an unsupported mode or after a sink swap during DPMS OFF), or mutate
adjusted_mode->htotal/clock/flags while the CRTC's mode_fixup() is skipped,
leaving CRTC and DPTX programmed with mismatched timings.  Adjusting and
validating when crtc_state->enable is true (and gating link revalidation on
mode_changed || connectors_changed) avoids both problems.

[ ... ]

> +static void dptx_bridge_hpd_enable(struct drm_bridge *bridge)
> +{
> +     struct dptx_dev *dp = bridge_to_dp(bridge);
> +
> +     dp->ops->replay_deferred_hotplug(dp);
> +}

[Severity: High]
Will dptx_bridge_hpd_enable() ever be called when a downstream bridge
advertises DRM_BRIDGE_OP_HPD?

When dp->next_bridge is a USB-C HPD bridge such as drm_aux_hpd_bridge
(which sets DRM_BRIDGE_OP_HPD with no .hpd_enable hook),
drm_bridge_connector_init() overwrites bridge_connector->bridge_hpd with
dp->next_bridge because it sits later in encoder->bridge_chain.
drm_bridge_connector_enable_hpd() then only calls drm_bridge_hpd_enable() on
bridge_connector->bridge_hpd, so dptx_bridge_hpd_enable() never runs at
boot.

With dp->oob_hpd set, any early HPD notification before connector HPD is
enabled is dropped while the controller's own HPD pin is unwired, leaving an
already-connected USB-C DisplayPort sink reported as disconnected until it
is unplugged and replugged.

> +
> +static void dptx_bridge_detach(struct drm_bridge *bridge)
> +{
> +     struct dptx_dev *dp = bridge_to_dp(bridge);
> +     struct device *consumer;
> +
> +     /* Stop workers before the connector goes away. */
> +     dp->ops->quiesce_hpd(dp);
> +
> +     scoped_guard(mutex, &dp->lock) {
> +             dp->attached = false;
> +
> +             if (!dp->released)
> +                     dp->hw->ops->aux_unregister(dp->hw);
> +
> +             consumer = dp->link_consumer;
> +             dp->link_consumer = NULL;
> +     }
> +
> +     if (consumer)
> +             device_link_remove(consumer, dp->dev);
> +}

[Severity: Medium]
Can device_link_remove(consumer, dp->dev) dereference a freed dp->dev once
dp->lock is dropped?

dp->link_consumer is cleared unconditionally without checking !dp->released,
and struct dptx_dev holds no get_device() reference on dp->dev:

CPU0                              CPU1
dptx_bridge_detach()
  lock(&dp->lock)
  consumer = dp->link_consumer
  dp->link_consumer = NULL
  unlock(&dp->lock)
                                  platform_device_unregister()
                                    dptx_deinit()
                                      /* dp->link_consumer is NULL */
                                    device_del(dp->dev)
                                      device_links_purge(dp->dev)
                                    put_device(dp->dev)  /* freed */
  device_link_remove(consumer,
                     dp->dev)  <-- use-after-free

Guarding the link removal with !dp->released while holding dp->lock
(matching device_link_add() in dptx_bridge_attach()), or taking a
get_device()/put_device() reference on dp->dev, would prevent this
use-after-free.

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

Reply via email to