dri-devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Maxime Ripard" <mripard@kernel.org>
Cc: dri-devel@lists.freedesktop.org
Subject: Re: [PATCH v8 02/39] drm/bridge-helper: Don't unlock the connection mutex after a backoff
Date: Fri, 06 Nov 2026 13:39:57 +0000	[thread overview]
Message-ID: <sashiko-outbox-201483@kernel.org> (raw)
In-Reply-To: <20261106-drm-state-readout-v8-2-cbe78624e1bc@kernel.org>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] drm/bridge-helper: connection_mutex dropped in
  drm_bridge_helper_reset_crtc()
- [Low] drm/bridge-helper: inaccurate backoff description in commit
  message
--

commit 9e2a98821e3fc25f6def63b07ed345888ea3f8d2
Author: Maxime Ripard <mripard@kernel.org>

drm/bridge-helper: Don't unlock the connection mutex after a backoff

This commit stops drm_bridge_helper_reset_crtc() from unlocking
connection_mutex on exit, re-acquires connection_mutex after
drm_atomic_get_connector_for_encoder(), and validates the connector's
best_encoder and crtc before resetting the CRTC.

> If either of those functions detects a deadlock, it backs off and drops
> every lock held by the context, including the connection mutex taken by
> drm_bridge_helper_reset_crtc() itself. The function however still
> unlocked the connection mutex unconditionally on its way out, unlocking
> a mutex it wasn't holding anymore.
> 
> The callers already have to release the context locks through
> drm_modeset_drop_locks() and to handle the -EDEADLK error, so we can
> leave the connection mutex release to them. We still need to take it
> back after drm_atomic_get_connector_for_encoder() returns, since it may
> have backed off, before looking at the connector state.

[Severity: Low]
Is this description of how connection_mutex gets dropped accurate?

Neither drm_atomic_get_connector_for_encoder() nor
drm_atomic_helper_reset_crtc() calls drm_modeset_backoff() on -EDEADLK;
drm_modeset_lock() only records ctx->contended and returns -EDEADLK for
the caller to back off.  Instead, as the new code comment notes,
drm_atomic_get_connector_for_encoder() unconditionally calls
drm_modeset_unlock(&dev->mode_config.connection_mutex) on its normal exit
path, which is why connection_mutex was already unlocked at the old
goto out label and why it must be re-acquired before inspecting
connector->state.

> diff --git a/drivers/gpu/drm/drm_bridge_helper.c b/drivers/gpu/drm/drm_bridge_helper.c
> index 0a3c8fee66b32..2efa4e1bc4b71 100644
> --- a/drivers/gpu/drm/drm_bridge_helper.c
> +++ b/drivers/gpu/drm/drm_bridge_helper.c
[ ... ]
> @@ -33,26 +37,45 @@ int drm_bridge_helper_reset_crtc(struct drm_bridge *bridge,
>  	struct drm_crtc *crtc;
>  	int ret;
>  
> +	/*
> +	 * Every lock taken with @ctx is released either by a backoff or
> +	 * by the caller through drm_modeset_drop_locks(), so we must not
> +	 * unlock the connection mutex ourselves.
> +	 */
>  	ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx);
>  	if (ret)
>  		return ret;
>  
>  	connector = drm_atomic_get_connector_for_encoder(encoder, ctx);
> -	if (IS_ERR(connector)) {
> -		ret = PTR_ERR(connector);
> -		goto out;
> -	}
> +	if (IS_ERR(connector))
> +		return PTR_ERR(connector);
> +
> +	/*
> +	 * drm_atomic_get_connector_for_encoder() drops the connection
> +	 * mutex on its way out, whether or not it was held through @ctx,
> +	 * so we need to take it back before looking at the connector
> +	 * state.
> +	 */
> +	ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx);
> +	if (ret)
> +		return ret;
> +
> +	if (!connector->state)
> +		return -EINVAL;
>  
> -	if (!connector->state) {
> -		ret = -EINVAL;
> -		goto out;
> -	}
> +	/*
> +	 * A concurrent atomic commit may have run while the connection
> +	 * mutex was released above, and re-routed the connector to
> +	 * another encoder or CRTC. Make sure the connector is still
> +	 * tied to our encoder before resetting anything.
> +	 */
> +	if (connector->state->best_encoder != encoder)
> +		return -EINVAL;

[Severity: Medium]
Why lock connection_mutex before drm_atomic_get_connector_for_encoder()
if that helper immediately unlocks it on exit?

drivers/gpu/drm/drm_atomic.c:drm_atomic_get_connector_for_encoder() {
	ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx);
	if (ret)
		return ERR_PTR(ret);
	...
	drm_modeset_unlock(&dev->mode_config.connection_mutex);

	return out_connector;
}

Because drm_atomic_get_connector_for_encoder() drops connection_mutex
unconditionally, the first drm_modeset_lock() in
drm_bridge_helper_reset_crtc() is immediately undone.  If connection_mutex
was already held in ctx on entry (for example, slow-locked via
drm_modeset_backoff() after the second drm_modeset_lock() returned
-EDEADLK), drm_atomic_get_connector_for_encoder() drops that slow-locked
mutex and removes it from ctx->locked, so the second drm_modeset_lock()
acquires it again on the normal path and can hit -EDEADLK again.

Dropping and re-acquiring connection_mutex also opens a window where a
concurrent atomic commit can move encoder to a different connector,
causing the connector->state->best_encoder != encoder check to fail with
-EINVAL even though encoder is still active on the new connector.  If
drm_atomic_get_connector_for_encoder() leaves connection_mutex locked in
ctx for the caller to drop via drm_modeset_drop_locks(), connection_mutex
stays held across the lookup without a second lock acquisition or race
window.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261106-drm-state-readout-v8-0-cbe78624e1bc@kernel.org?part=2

  reply	other threads:[~2026-11-06 13:40 UTC|newest]

Thread overview: 66+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-06 13:14 [PATCH v8 00/39] drm: Implement state readout support Maxime Ripard
2026-11-06 13:14 ` [PATCH v8 01/39] drm/ingenic: Convert to devm_drm_bridge_alloc() Maxime Ripard
2026-11-06 13:33   ` sashiko-bot
2026-11-06 13:55   ` Luca Ceresoli
2026-11-06 13:59     ` Maxime Ripard
2026-11-06 13:14 ` [PATCH v8 02/39] drm/bridge-helper: Don't unlock the connection mutex after a backoff Maxime Ripard
2026-11-06 13:39   ` sashiko-bot [this message]
2026-11-06 13:14 ` [PATCH v8 03/39] drm/tests: hdmi-state-helper: Handle EDEADLK in the 420-only test Maxime Ripard
2026-11-06 13:14 ` [PATCH v8 04/39] drm/tests: hdmi-state-helper: Handle EDEADLK in the color format test Maxime Ripard
2026-11-06 13:32   ` sashiko-bot
2026-11-06 13:14 ` [PATCH v8 05/39] drm/colorop: Export drm_atomic_helper_colorop_create_state() Maxime Ripard
2026-11-06 13:14 ` [PATCH v8 06/39] drm/atomic: Fix private obj error handling Maxime Ripard
2026-11-06 13:29   ` sashiko-bot
2026-11-06 13:14 ` [PATCH v8 07/39] drm/atomic: Add drm_private_obj name Maxime Ripard
2026-11-06 13:35   ` sashiko-bot
2026-11-19 15:20   ` Luca Ceresoli
2026-11-06 13:14 ` [PATCH v8 08/39] drm/bridge: Switch print_state to use bridge name Maxime Ripard
2026-11-19 15:25   ` Luca Ceresoli
2026-11-06 13:14 ` [PATCH v8 09/39] drm/bridge: Add drm_private_obj_is_bridge() Maxime Ripard
2026-11-06 13:14 ` [PATCH v8 10/39] drm/fb-dma-helper: Handle framebuffers without a GEM object Maxime Ripard
2026-11-06 13:32   ` sashiko-bot
2026-11-06 13:14 ` [PATCH v8 11/39] drm/gem-atomic-helper: " Maxime Ripard
2026-11-06 13:14 ` [PATCH v8 12/39] drm/atomic: Export drm_atomic_*_print_state Maxime Ripard
2026-11-06 13:14 ` [PATCH v8 13/39] drm/atomic_sro: Create drm_atomic_sro_state container Maxime Ripard
2026-11-06 13:14 ` [PATCH v8 14/39] drm/atomic_sro: Create kernel parameter to force or disable readout Maxime Ripard
2026-11-06 13:31   ` sashiko-bot
2026-11-06 13:14 ` [PATCH v8 15/39] drm/atomic_sro: Add atomic state readout infrastructure Maxime Ripard
2026-11-06 13:14 ` [PATCH v8 16/39] drm/atomic_sro: Add function to install state into drm objects Maxime Ripard
2026-11-06 13:33   ` sashiko-bot
2026-11-06 13:14 ` [PATCH v8 17/39] drm/atomic_sro: Create documentation Maxime Ripard
2026-11-06 13:14 ` [PATCH v8 18/39] drm/bridge: Handle bridges with hardware state readout Maxime Ripard
2026-11-06 13:33   ` sashiko-bot
2026-11-06 13:14 ` [PATCH v8 19/39] drm/mode-config: Read out hardware state in drm_mode_config_create_state Maxime Ripard
2026-11-06 13:33   ` sashiko-bot
2026-11-06 13:14 ` [PATCH v8 20/39] drm/atomic_sro: Provide helpers to implement hardware state readout Maxime Ripard
2026-11-06 13:44   ` sashiko-bot
2026-11-06 13:14 ` [PATCH v8 21/39] drm/mode-config: Introduce hw_reset hook Maxime Ripard
2026-11-06 13:14 ` [PATCH v8 22/39] drm/modeset-helper: Add drm_mode_config_helper_hw_reset() Maxime Ripard
2026-11-06 13:14 ` [PATCH v8 23/39] drm/atomic_helper: Compare actual and readout states once the commit is done Maxime Ripard
2026-11-06 13:14 ` [PATCH v8 24/39] drm/atomic-state-helper: Provide comparison macros Maxime Ripard
2026-11-06 13:14 ` [PATCH v8 25/39] drm/atomic-state-helper: Provide atomic_compare_state helpers Maxime Ripard
2026-11-06 13:44   ` sashiko-bot
2026-11-06 13:14 ` [PATCH v8 26/39] drm/atomic-helper: Document State Read-Out as a state initialization approach Maxime Ripard
2026-11-06 13:14 ` [PATCH v8 27/39] drm/tests: kunit: Add a drm_printer helper Maxime Ripard
2026-11-06 13:14 ` [PATCH v8 28/39] drm/tests: Add tests for the atomic state readout Maxime Ripard
2026-11-06 13:44   ` sashiko-bot
2026-11-06 13:14 ` [PATCH v8 29/39] drm/encoder: Create atomic_sro_get_current_crtc hook Maxime Ripard
2026-11-06 13:14 ` [PATCH v8 30/39] drm/bridge: display-connector: Implement readout support Maxime Ripard
2026-11-06 13:50   ` sashiko-bot
2026-11-06 13:14 ` [PATCH v8 31/39] drm/bridge_connector: Implement hw readout for connector Maxime Ripard
2026-11-06 13:14 ` [PATCH v8 32/39] drm/tidss: crtc: Add atomic_print_state implementation Maxime Ripard
2026-11-17  7:24   ` Tomi Valkeinen
2026-11-06 13:14 ` [PATCH v8 33/39] drm/tidss: dispc: Improve mode checking logs Maxime Ripard
2026-11-17  7:25   ` Tomi Valkeinen
2026-11-06 13:14 ` [PATCH v8 34/39] drm/tidss: Implement hw_reset Maxime Ripard
2026-11-06 13:50   ` sashiko-bot
2026-11-06 13:14 ` [PATCH v8 35/39] drm/tidss: Implement readout support Maxime Ripard
2026-11-06 14:06   ` sashiko-bot
2026-11-06 13:14 ` [PATCH v8 36/39] drm/tidss: encoder: Implement atomic_sro_get_current_crtc Maxime Ripard
2026-11-06 13:14 ` [PATCH v8 37/39] drm/bridge: sii902x: Implement hw state readout Maxime Ripard
2026-11-06 13:14 ` [PATCH v8 38/39] drm/panel: Implement state readout support Maxime Ripard
2026-11-06 13:45   ` sashiko-bot
2026-11-06 13:14 ` [PATCH v8 39/39] fixup! drm/tidss: Implement " Maxime Ripard
2026-11-06 13:57   ` sashiko-bot
2026-11-17 15:17 ` [PATCH v8 00/39] drm: Implement state " Tomi Valkeinen
2026-11-18 12:37   ` Tomi Valkeinen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-201483@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=mripard@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox