[v10,06/69] drm/probe-helper: Introduce .force_ctx() connector callback

Message ID 20260731-dw-hdmi-qp-scramb-v10-6-294364b2cf15@collabora.com (mailing list archive)
State New
Headers
Series Add HDMI 2.0 support to DW HDMI QP TX |

Commit Message

Cristian Ciocaltea July 31, 2026, 4:19 p.m. UTC
The .force() connector callback is invoked by
drm_helper_probe_single_connector_modes() while the modeset acquire
context it created is live, but the hook signature provides no way to
pass it down, e.g. to support implementations needing to lock further
objects.

Add a .force_ctx() variant to drm_connector_helper_funcs, taking the
acquire context and returning an error code, so that -EDEADLK propagates
into the existing retry path.  When provided, it takes precedence over
drm_connector_funcs' .force(), which is kept for existing users.

This is a prerequisite for SCDC status synchronization in the HDMI
connector framework.

Signed-off-by: Cristian Ciocaltea <cristian.ciocaltea@collabora.com>
---
 drivers/gpu/drm/drm_probe_helper.c       | 14 +++++++++++++-
 include/drm/drm_connector.h              |  6 ++++++
 include/drm/drm_modeset_helper_vtables.h | 28 ++++++++++++++++++++++++++++
 3 files changed, 47 insertions(+), 1 deletion(-)
  

Comments

sashiko-bot@kernel.org July 31, 2026, 5:06 p.m. UTC | #1
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Modifying `connector->status` before `force_ctx()` can return `-EDEADLK` causes subsequent retries to incorrectly evaluate `old_status == connector->status`, silently skipping the hotplug event.
--

--- Patch [6]: [PATCH v10 06/69] drm/probe-helper: Introduce .force_ctx() connector callback ---
commit b08054d3073ea6707b0fe9e517dde416f7e5c8b6
Author: Cristian Ciocaltea <cristian.ciocaltea@collabora.com>

drm/probe-helper: Introduce .force_ctx() connector callback

This commit introduces a force_ctx() variant to the connector helper
functions, taking a modeset acquire context and returning an error code
so that -EDEADLK can propagate. It takes precedence over the existing
force() callback when both are implemented.

> diff --git a/drivers/gpu/drm/drm_probe_helper.c b/drivers/gpu/drm/drm_probe_helper.c
> index e26ead5b5e589..918255bcfa096 100644
> --- a/drivers/gpu/drm/drm_probe_helper.c
> +++ b/drivers/gpu/drm/drm_probe_helper.c

[ ... ]

> @@ -592,8 +593,19 @@ int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
>  			connector->status = connector_status_connected;
>  		else
>  			connector->status = connector_status_disconnected;
> -		if (connector->funcs->force)
> +
> +		if (funcs && funcs->force_ctx) {
> +			ret = funcs->force_ctx(connector, &ctx);
> +			if (ret == -EDEADLK) {
> +				drm_modeset_backoff(&ctx);
> +				goto retry;

[Severity: Medium]
Does modifying connector->status before force_ctx() cause hotplug events
to be dropped on lock contention?

If funcs->force_ctx() returns -EDEADLK, the code branches to the retry
label. During the retry loop, old_status is assigned the current
connector->status:

drm_helper_probe_single_connector_modes() {
retry:
        ...
        old_status = connector->status;

Since connector->status was already updated prior to the failed attempt,
old_status will equal the new status. When the lock is finally acquired
after the retry, the old_status != connector->status check later in the
function evaluates to false, causing the hotplug event detection logic
to be silently skipped.
  
Maxime Ripard Aug. 19, 2026, 9:53 a.m. UTC | #2
On Fri, 31 Jul 2026 19:19:13 +0300, Cristian Ciocaltea wrote:
> The .force() connector callback is invoked by
> drm_helper_probe_single_connector_modes() while the modeset acquire
> context it created is live, but the hook signature provides no way to
> pass it down, e.g. to support implementations needing to lock further
> objects.
> 
> [ ... ]

Reviewed-by: Maxime Ripard <mripard@kernel.org>

Thanks!
Maxime
  

Patch

diff --git a/drivers/gpu/drm/drm_probe_helper.c b/drivers/gpu/drm/drm_probe_helper.c
index e26ead5b5e58..918255bcfa09 100644
--- a/drivers/gpu/drm/drm_probe_helper.c
+++ b/drivers/gpu/drm/drm_probe_helper.c
@@ -559,6 +559,7 @@  static int __drm_helper_update_and_validate(struct drm_connector *connector,
 int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
 					    uint32_t maxX, uint32_t maxY)
 {
+	const struct drm_connector_helper_funcs *funcs = connector->helper_private;
 	struct drm_device *dev = connector->dev;
 	struct drm_display_mode *mode;
 	int count = 0, ret;
@@ -592,8 +593,19 @@  int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
 			connector->status = connector_status_connected;
 		else
 			connector->status = connector_status_disconnected;
-		if (connector->funcs->force)
+
+		if (funcs && funcs->force_ctx) {
+			ret = funcs->force_ctx(connector, &ctx);
+			if (ret == -EDEADLK) {
+				drm_modeset_backoff(&ctx);
+				goto retry;
+			} else if (ret < 0) {
+				drm_dbg_kms(dev, "[CONNECTOR:%d:%s] force_ctx failed: %d\n",
+					    connector->base.id, connector->name, ret);
+			}
+		} else if (connector->funcs->force) {
 			connector->funcs->force(connector);
+		}
 	} else {
 		ret = drm_helper_probe_detect(connector, &ctx, true);
 
diff --git a/include/drm/drm_connector.h b/include/drm/drm_connector.h
index 2a49c4d55f77..a6de3e63b462 100644
--- a/include/drm/drm_connector.h
+++ b/include/drm/drm_connector.h
@@ -1603,6 +1603,12 @@  struct drm_connector_funcs {
 	 * the sysfs interfaces or on the kernel cmdline. In that case the
 	 * @detect callback isn't called.
 	 *
+	 * New drivers should implement
+	 * &drm_connector_helper_funcs.force_ctx instead, which gets passed
+	 * the modeset acquire context and thus allows taking additional
+	 * locks.  It takes precedence over this callback when both are
+	 * implemented.
+	 *
 	 * FIXME:
 	 *
 	 * Note that this hook is only called by the probe helper. It's not in
diff --git a/include/drm/drm_modeset_helper_vtables.h b/include/drm/drm_modeset_helper_vtables.h
index ca6268945c28..cf5fe5ba2b18 100644
--- a/include/drm/drm_modeset_helper_vtables.h
+++ b/include/drm/drm_modeset_helper_vtables.h
@@ -921,6 +921,34 @@  struct drm_connector_helper_funcs {
 			  struct drm_modeset_acquire_ctx *ctx,
 			  bool force);
 
+	/**
+	 * @force_ctx:
+	 *
+	 * This function is called to update internal encoder state when the
+	 * connector is forced to a certain state by userspace, either through
+	 * the sysfs interfaces or on the kernel cmdline. In that case the
+	 * @detect_ctx callback isn't called.
+	 *
+	 * This is the atomic version of &drm_connector_funcs.force.  When
+	 * both are implemented, this one takes precedence and
+	 * &drm_connector_funcs.force is not called.
+	 *
+	 * To avoid races against concurrent connector state updates, the
+	 * helper libraries always call this with ctx set to a valid context,
+	 * and &drm_mode_config.connection_mutex will always be locked with
+	 * the ctx parameter set to this ctx. This allows taking additional
+	 * locks as required.
+	 *
+	 * RETURNS:
+	 *
+	 * Zero on success, or a negative error code otherwise.  This includes
+	 * -EDEADLK when a lock acquisition needs to be restarted, in which
+	 * case the helpers take care of backing off and calling this function
+	 * again.
+	 */
+	int (*force_ctx)(struct drm_connector *connector,
+			 struct drm_modeset_acquire_ctx *ctx);
+
 	/**
 	 * @mode_valid:
 	 *