Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] drm/probe-helper: unbalanced HPD callbacks in per-connector
  poll helpers
- [High] drm/probe-helper: drm_kms_helper_poll_disable_connector()
  leaves poll active
- [Low] drm/probe-helper: duplicated HPD helper logic in
  drm_kms_helper_enable_hpd()
--

commit 3fcc8be33037c845672586431b3e9d911af66acb
Author: Luca Ceresoli <[email protected]>

drm/probe-helper: add per-connector polling enable/disable

This commit adds drm_kms_helper_poll_enable_connector() and
drm_kms_helper_poll_disable_connector() to enable and disable output
polling and HPD for individual connectors added or removed dynamically.

> diff --git a/drivers/gpu/drm/drm_probe_helper.c 
> b/drivers/gpu/drm/drm_probe_helper.c
> index 856b40fdaac4a..e468971ab6429 100644
> --- a/drivers/gpu/drm/drm_probe_helper.c
> +++ b/drivers/gpu/drm/drm_probe_helper.c
> @@ -224,6 +224,14 @@ drm_connector_mode_valid(struct drm_connector *connector,
>       return ret;
>  }
>  
> +static void drm_kms_helper_disable_hpd_connector(struct drm_connector 
> *connector)
> +{
> +     const struct drm_connector_helper_funcs *funcs = 
> connector->helper_private;
> +
> +     if (funcs && funcs->disable_hpd)
> +             funcs->disable_hpd(connector);
> +}
> +
>  static void drm_kms_helper_disable_hpd(struct drm_device *dev)
>  {
>       struct drm_connector *connector;
> @@ -240,6 +248,16 @@ static void drm_kms_helper_disable_hpd(struct drm_device 
> *dev)
>       drm_connector_list_iter_end(&conn_iter);
>  }
>  
> +static bool drm_kms_helper_enable_hpd_connector(struct drm_connector 
> *connector)
> +{
> +     const struct drm_connector_helper_funcs *funcs = 
> connector->helper_private;
> +
> +     if (funcs && funcs->enable_hpd)
> +             funcs->enable_hpd(connector);
> +
> +     return (connector->polled & (DRM_CONNECTOR_POLL_CONNECT | 
> DRM_CONNECTOR_POLL_DISCONNECT));
> +}
> +
>  static bool drm_kms_helper_enable_hpd(struct drm_device *dev)
>  {
>       bool poll = false;

[Severity: Low]
This isn't a bug, but drm_kms_helper_disable_hpd() and
drm_kms_helper_enable_hpd() right below these new helpers still open-code
the same per-connector callback and connector->polled checks instead of
calling drm_kms_helper_disable_hpd_connector() and
drm_kms_helper_enable_hpd_connector().

> @@ -316,6 +334,32 @@ void drm_kms_helper_poll_enable(struct drm_device *dev)
>  }
>  EXPORT_SYMBOL(drm_kms_helper_poll_enable);
>  
> +/**
> + * drm_kms_helper_poll_enable_connector - enable output polling for a 
> connector
> + * @connector: the connector
> + *
> + * Enables output polling @connector and reschdules the polling work if
> + * needed. Intended for connectors which are added after initial probing.
> + *
> + * This must be called after polling has been initialized by
> + * drm_kms_helper_poll_init(). Safe to call while polling is already
> + * running.
> + */
> +void drm_kms_helper_poll_enable_connector(struct drm_connector *connector)
> +{
> +     struct drm_device *dev = connector->dev;
> +
> +     if (drm_WARN_ON_ONCE(dev, !dev->mode_config.poll_enabled) || 
> !drm_kms_helper_poll)
> +             return;
> +
> +     if (drm_kms_helper_enable_hpd_connector(connector) ||
> +         dev->mode_config.delayed_event)
> +             reschedule_output_poll_work(dev);
> +
> +     dev->mode_config.poll_running = true;
> +}
> +EXPORT_SYMBOL(drm_kms_helper_poll_enable_connector);
> +
>  /**
>   * drm_kms_helper_poll_reschedule - reschedule the output polling work
>   * @dev: drm_device
> @@ -927,6 +971,54 @@ void drm_kms_helper_poll_disable(struct drm_device *dev)
>  }
>  EXPORT_SYMBOL(drm_kms_helper_poll_disable);
>  
> +static bool drm_kms_helper_has_other_connectors(struct drm_connector 
> *connector)
> +{
> +     struct drm_device *dev = connector->dev;
> +     struct drm_connector *curr;
> +     struct drm_connector_list_iter conn_iter;
> +     bool remaining = false;
> +
> +     drm_connector_list_iter_begin(dev, &conn_iter);
> +     drm_for_each_connector_iter(curr, &conn_iter) {
> +             if (curr != connector &&
> +                 curr->registration_state == DRM_CONNECTOR_REGISTERED) {
> +                     remaining = true;
> +                     break;
> +             }
> +     }
> +     drm_connector_list_iter_end(&conn_iter);
> +
> +     return remaining;
> +}
> +
> +/**
> + * drm_kms_helper_poll_disable_connector - disable output polling for a 
> connector
> + * @connector: the connector
> + *
> + * Disables output polling for @connector. If there are no other registered
> + * connectors, also disables the output poll work.
> + *
> + * Polling stays initialized; call drm_kms_helper_poll_fini() to tear it
> + * down. Does nothing if output polling is not initialized by
> + * drm_kms_helper_poll_init().
> + */
> +void drm_kms_helper_poll_disable_connector(struct drm_connector *connector)
> +{
> +     struct drm_device *dev = connector->dev;
> +
> +     if (!dev->mode_config.poll_enabled)
> +             return;
> +
> +     drm_kms_helper_disable_hpd_connector(connector);

[Severity: High]
Can drm_kms_helper_poll_enable_connector() and
drm_kms_helper_poll_disable_connector() cause unbalanced
enable_hpd()/disable_hpd() callbacks on connectors?

struct drm_connector_helper_funcs documents that drivers do not need to
track HPD state because the DRM core guarantees enable_hpd() and
disable_hpd() calls are balanced.  However, no per-connector HPD state is
recorded here:

1. drm_kms_helper_poll_disable_connector() calls
   drm_kms_helper_disable_hpd_connector() without checking
   dev->mode_config.poll_running.  If poll_running is false (for example
   because drm_kms_helper_poll was false or polling was disabled for
   suspend via drm_kms_helper_poll_disable()), disable_hpd() runs even
   though HPD is not enabled.

2. A connector remains on dev->mode_config.connector_list after
   drm_kms_helper_poll_disable_connector() until its last reference drops
   in drm_connector_cleanup().  If another connector keeps poll_running
   true and drm_kms_helper_poll_disable() or drm_kms_helper_poll_fini()
   runs while an atomic state or client still holds a reference to the
   disabled connector, drm_kms_helper_disable_hpd() walks connector_list
   and calls disable_hpd() on it a second time.

3. Conversely, when disabling the last connector clears poll_running, a
   later drm_helper_probe_single_connector_modes() on a newly registered
   connector calls drm_kms_helper_poll_enable(dev), which enables HPD on
   all connectors on connector_list and sets poll_running = true before
   the caller invokes drm_kms_helper_poll_enable_connector(), calling
   enable_hpd() twice.  And if poll_running was false with multiple
   connectors present, drm_kms_helper_poll_enable_connector() sets
   device-wide poll_running = true after enabling only one connector,
   causing a subsequent drm_kms_helper_poll_enable(dev) to return early
   and leave HPD disabled on the other connectors.

Unbalanced enable_hpd()/disable_hpd() calls trigger WARN(bridge->hpd_cb)
in drm_bridge_hpd_enable() and cause runtime PM or IRQ count underflows
in bridge drivers such as ti_sn_bridge_hpd_disable() and
display_connector_hpd_disable().

> +
> +     if (dev->mode_config.poll_running &&
> +         !drm_kms_helper_has_other_connectors(connector)) {
> +             cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
> +             dev->mode_config.poll_running = false;
> +     }
> +}
> +EXPORT_SYMBOL(drm_kms_helper_poll_disable_connector);

[Severity: High]
Does drm_kms_helper_poll_disable_connector() actually stop or synchronize
with output_poll_work for a polled connector when another connector is
registered on the device?

drm_kms_helper_has_other_connectors() returns true whenever any other
registered connector exists, even if that other connector does not use
polling (polled == 0 or DRM_CONNECTOR_POLL_HPD), so output_poll_work is
left running when no remaining connector needs polling.

More importantly, when another registered connector exists,
drm_kms_helper_poll_disable_connector() neither waits for an in-flight
output_poll_execute() nor marks @connector as poll-disabled.  Because
output_poll_execute() iterates dev->mode_config.connector_list and checks
only connector->force and connector->polled, it can run
drm_helper_probe_detect() on @connector concurrently with bridge teardown
and keep repolling it every DRM_OUTPUT_POLL_PERIOD until its final
connector reference is dropped.

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

Reply via email to