Linux kernel and device drivers for NXP i.MX platforms
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Luca Ceresoli" <luca.ceresoli@bootlin.com>
Cc: Frank.Li@kernel.org, imx@lists.linux.dev
Subject: Re: [PATCH 36/37] drm/display: bridge-connector: handle bridge hotplug
Date: Tue, 19 May 2026 11:15:35 +0000	[thread overview]
Message-ID: <20260519111537.BE07CC2BCB3@smtp.kernel.org> (raw)
In-Reply-To: <20260519-drm-bridge-hotplug-v1-36-45e2bdb3dfb4@bootlin.com>

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [Critical] `drmm_bridge_connector_init()` returns NULL instead of the allocated pointer on success.
- [High] Unchecked execution of the optional `attach` callback causes NULL pointer dereference.
- [High] TOCTOU race condition in `dynconn` creation leads to memory leaks and duplicate connector registrations.
- [High] Dynamic connector destruction on hot-unplug leaks `drmm` actions, leading to a Use-After-Free.
- [High] Lockless traversal of `encoder->bridge_chain` risks list corruption and Use-After-Free.
--

commit 31a28cb0ac7316fddfb3b8148c8e28f4da0bce4f
Author: Luca Ceresoli <luca.ceresoli@bootlin.com>

drm/display: bridge-connector: handle bridge hotplug

This commit updates the DRM bridge connector infrastructure to handle bridge
hot-plugging by creating and destroying the DRM connector dynamically. It
introduces the drmm_bridge_connector_init() API, which registers a notifier
to react to attach/detach events and manage the connector lifecycle.

> diff --git a/drivers/gpu/drm/display/drm_bridge_connector.c b/drivers/gpu/drm/display/drm_bridge_connector.c
> index b83da529cc7af..2ce6c04e2fc7a 100644
> --- a/drivers/gpu/drm/display/drm_bridge_connector.c
> +++ b/drivers/gpu/drm/display/drm_bridge_connector.c

[ ... ]

> @@ -1037,6 +1042,19 @@ static int drm_bridge_connector_init_hdmi_audio_cec(struct drm_bridge_connector_
>  	return 0;
>  }
>  
> +static bool drm_bridge_connector_pipeline_is_complete(struct drm_bridge_connector *bridge_connector)
> +{
> +	struct drm_bridge *last_bridge __free(drm_bridge_put) =
> +		drm_bridge_chain_get_last_bridge(bridge_connector->encoder);

Is it safe to call drm_bridge_chain_get_last_bridge() here without holding
bridge_connector->encoder->bridge_chain_mutex? It seems this can be called
asynchronously from hotplug event notifiers, potentially traversing the
list while it is being modified.

> +
> +	if (!last_bridge || !drm_bridge_is_tail(last_bridge)) {
> +		drm_dbg_driver(bridge_connector->drm, "pipeline not (yet) fully connected");
> +		return false;
> +	}
> +
> +	return true;
> +}

[ ... ]

> @@ -1153,6 +1171,79 @@ static int drm_bridge_connector_add_connector(struct drm_bridge_connector *bridg
>  	return ret;
>  }
>  
> +/*
> + * Propagate the attach chain and possibly add a drm_connector after a new
> + * drm_bridge is hot-plugged.
> + *
> + * The connector is added only if the pipeline is now complete. This could
> + * not be the case for various reasons:
> + *
> + * - the new bridge is just unrelated to our encoder
> + * - the new bridge is not be the next one in the pipeline
> + * - the new bridge is the next in the pipeline but the pipeline is not yet
> + *   complete
> + *
> + * All these cases are normal, not an error.
> + */
> +static void drm_bridge_connector_try_complete(struct drm_bridge_connector *bridge_connector)
> +{
> +	int err;
> +
> +	/*
> +	 * drm_connector already present, the new bridge must be for
> +	 * another card
> +	 */
> +	if (bridge_connector->dynconn)

Could there be a race condition here? If multiple hotplug events are processed
concurrently, two threads could observe dynconn as NULL here outside the
lock, and both proceed to allocate and assign it in
drm_bridge_connector_add_connector().

Would it be better to re-verify bridge_connector->dynconn after acquiring
dynconn_mutex inside drm_bridge_connector_add_connector()?

> +		return;
> +
> +	/* Propagate the attach call chain to newly hotplugged bridge(s) */
> +	struct drm_bridge *last_bridge __free(drm_bridge_put) =
> +		drm_bridge_chain_get_last_bridge(bridge_connector->encoder);

As mentioned above, should this list traversal be protected by
bridge_connector->encoder->bridge_chain_mutex?

> +
> +	/* Encoder chain empty? */
> +	if (!last_bridge)
> +		return;
> +
> +	err = last_bridge->funcs->attach(last_bridge, bridge_connector->encoder,
> +					 DRM_BRIDGE_ATTACH_NO_CONNECTOR);

What happens if the last bridge does not implement the attach callback?

Since attach is optional in struct drm_bridge_funcs, should there be a check
for last_bridge->funcs->attach before calling it to avoid a NULL pointer
dereference?

> +	if (err)
> +		return;
> +
> +	/* Add the connector if the pipeline is now complete */
> +	if (drm_bridge_connector_pipeline_is_complete(bridge_connector))
> +		drm_bridge_connector_add_connector(bridge_connector);
> +}
> +
> +static int drm_bridge_connector_handle_event(struct notifier_block *nb,
> +					     unsigned long event, void *data)
> +{
> +	struct drm_bridge_connector *bridge_connector =
> +		container_of(nb, struct drm_bridge_connector, drm_event_nb);
> +
> +	switch (event) {
> +	case DRM_MIPI_DSI_ATTACHED:
> +		/* One or more bridges hot-plugged, try adding the drm_connector */
> +		drm_bridge_connector_try_complete(bridge_connector);
> +		break;
> +	case DRM_BRIDGE_DETACHED:
> +	{
> +		/*
> +		 * A bridge was unplugged, remove the drm_connector if it's
> +		 * part of the same pipeline
> +		 */
> +		struct drm_bridge *bridge = (struct drm_bridge *)data;
> +
> +		if (bridge_connector->dynconn &&
> +		    bridge->encoder == bridge_connector->encoder)
> +			drm_bridge_connector_dynconn_release(bridge_connector);

Does drm_bridge_connector_dynconn_release() safely clean up the drmm actions
registered in drm_bridge_connector_init_hdmi_audio_cec()?

Since drmm_connector_hdmi_cec_register() and similar tie cleanup actions to
the drm_device, releasing the connector here and freeing the dynconn
structure might leave those actions active, potentially causing a
use-after-free when the device is eventually destroyed.

> +		break;
> +	}
> +	default:
> +	}
> +
> +	return NOTIFY_DONE;
> +}

[ ... ]

> +struct drm_bridge_connector *drmm_bridge_connector_init(struct drm_device *drm,
> +							struct drm_encoder *encoder)
> +{
> +	struct drm_bridge_connector *bridge_connector;
> +	int ret;
> +
> +	bridge_connector = drmm_kzalloc(drm, sizeof(*bridge_connector), GFP_KERNEL);
> +	if (!bridge_connector)
> +		return ERR_PTR(-ENOMEM);
> +
> +	mutex_init(&bridge_connector->dynconn_mutex);
> +	bridge_connector->drm = drm;
> +	bridge_connector->encoder = encoder;
> +	bridge_connector->drm_event_nb.notifier_call = drm_bridge_connector_handle_event;
> +
> +	if (drm_bridge_connector_pipeline_is_complete(bridge_connector)) {
> +		ret = drm_bridge_connector_add_connector(bridge_connector);
> +		if (ret)
> +			return ERR_PTR(ret);
> +	}
> +
> +	ret = drmm_add_action_or_reset(drm, drm_bridge_connector_fini, bridge_connector);
> +	if (ret)
> +		return ERR_PTR(ret);
> +
> +	ret = drm_event_notifier_register(&bridge_connector->drm_event_nb);
> +	if (ret)
> +		return ERR_PTR(ret);
> +
> +	ret = drmm_add_action_or_reset(drm, drm_bridge_connector_notifier_unregister,
> +				       &bridge_connector->drm_event_nb);
> +	if (ret)
> +		return ERR_PTR(ret);
> +
> +	return 0;

Should this return the bridge_connector pointer instead of 0? Callers using
IS_ERR() will treat 0 (NULL) as success and potentially dereference it.

> +}
> +EXPORT_SYMBOL_GPL(drmm_bridge_connector_init);

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260519-drm-bridge-hotplug-v1-0-45e2bdb3dfb4@bootlin.com?part=36

  reply	other threads:[~2026-05-19 11:15 UTC|newest]

Thread overview: 59+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-19 10:37 [PATCH 00/37] drm bridge hotplug Luca Ceresoli
2026-05-19 10:37 ` [PATCH 01/37] drm/connector: split drmm_connector_hdmi_init() in 3 parts Luca Ceresoli
2026-05-19 10:37 ` [PATCH 02/37] drm/connector: add drm_connector_hdmi_dynamic_init() Luca Ceresoli
2026-05-19 11:04   ` sashiko-bot
2026-05-19 10:37 ` [PATCH 03/37] drm/display: bridge-connector: rename variable for consistency Luca Ceresoli
2026-05-19 10:37 ` [PATCH 04/37] drm/display: bridge-connector: store the drm_device pointer Luca Ceresoli
2026-05-19 10:37 ` [PATCH 05/37] drm/display: bridge-connector: split code creating the connector to a subfunction Luca Ceresoli
2026-05-19 10:37 ` [PATCH 06/37] drm/display: bridge-connector: use a drm_bridge_connector internally, not a drm_connector Luca Ceresoli
2026-05-19 10:37 ` [PATCH 07/37] drm/display: bridge-connector: extract drm_bridge_connector_get_bridges() Luca Ceresoli
2026-05-19 11:01   ` sashiko-bot
2026-05-19 10:37 ` [PATCH 08/37] drm/display: bridge-connector: return int from drm_bridge_connector_get_bridges() Luca Ceresoli
2026-05-19 10:58   ` sashiko-bot
2026-05-19 10:37 ` [PATCH 09/37] drm/display: bridge-connector: extract drm_bridge_connector_init_hdmi_audio_cec() Luca Ceresoli
2026-05-19 10:37 ` [PATCH 10/37] drm/display: bridge-connector: return int from drm_bridge_connector_init_hdmi_audio_cec() Luca Ceresoli
2026-05-19 10:37 ` [PATCH 11/37] drm/display: bridge-connector: return int from drm_bridge_connector_add_connector() Luca Ceresoli
2026-05-19 10:37 ` [PATCH 12/37] drm/display: bridge-connector: hoist error management to common code Luca Ceresoli
2026-05-19 10:37 ` [PATCH 13/37] drm/display: bridge-connector: move drm_bridge_connector_put_bridges() definition eariler Luca Ceresoli
2026-05-19 10:37 ` [PATCH 14/37] drm/display: bridge-connector: add non-drmm variant of drm_bridge_connector_put_bridges() Luca Ceresoli
2026-05-19 10:37 ` [PATCH 15/37] drm/display: bridge-connector: allocate the connector dynamically Luca Ceresoli
2026-05-19 11:15   ` sashiko-bot
2026-05-19 10:37 ` [PATCH 16/37] drm/display: bridge-connector: move per-connector fields to the dynamic connector Luca Ceresoli
2026-05-19 11:17   ` sashiko-bot
2026-05-19 10:37 ` [PATCH 17/37] drm/display: bridge-connector: protect dynconn creation and destruction with a mutex Luca Ceresoli
2026-05-19 11:18   ` sashiko-bot
2026-05-19 10:37 ` [PATCH 18/37] drm/bridge: samsung-dsim: remove the panel_bridge on host_detach Luca Ceresoli
2026-05-19 10:37 ` [PATCH 19/37] drm/bridge: samsung-dsim: move drm_bridge_add() call to probe Luca Ceresoli
2026-05-19 11:16   ` sashiko-bot
2026-05-19 10:37 ` [PATCH 20/37] drm/bridge: samsung-dsim: attach: return -EPROBE_DEFER is next bridge not yet available Luca Ceresoli
2026-05-19 11:13   ` sashiko-bot
2026-05-19 10:37 ` [PATCH 21/37] drm/bridge: initialize chain_node list head on allocation Luca Ceresoli
2026-05-19 10:37 ` [PATCH 22/37] drm/bridge: initialize chain_node list head on detach and attach errors Luca Ceresoli
2026-05-19 11:17   ` sashiko-bot
2026-05-19 10:37 ` [PATCH 23/37] drm/encoder: add drm_encoder_cleanup_from() Luca Ceresoli
2026-05-19 11:14   ` sashiko-bot
2026-05-19 10:37 ` [PATCH 24/37] drm/atomic: move drm_atomic_helper_disable_all() and drm_atomic_helper_shutdown() from drm_atomic_helper to drm_atomic Luca Ceresoli
2026-05-19 10:57   ` sashiko-bot
2026-05-19 10:37 ` [PATCH 25/37] drm/bridge: shutdown and cleanup on bridge unplug Luca Ceresoli
2026-05-19 11:09   ` sashiko-bot
2026-05-19 10:37 ` [PATCH 26/37] drm: event-notifier: add mechanism to notify about hotplug events Luca Ceresoli
2026-05-19 11:06   ` sashiko-bot
2026-05-19 10:37 ` [PATCH 27/37] drm/bridge: notify about detached bridges Luca Ceresoli
2026-05-19 11:32   ` sashiko-bot
2026-05-19 10:37 ` [PATCH 28/37] drm/mipi-dsi: turn DRM_MIPI_DSI into a tristate Luca Ceresoli
2026-05-19 11:07   ` sashiko-bot
2026-05-19 10:37 ` [PATCH 29/37] drm/mipi-dsi: notify about DSI attach Luca Ceresoli
2026-05-19 11:13   ` sashiko-bot
2026-05-19 10:37 ` [PATCH 30/37] drm/bridge: add drm_bridge_is_tail() to know whether a bridge completes the pipeline Luca Ceresoli
2026-05-19 10:59   ` sashiko-bot
2026-05-19 10:37 ` [PATCH 31/37] drm/bridge: panel: implement .is_tail Luca Ceresoli
2026-05-19 15:12   ` Neil Armstrong
2026-05-19 10:37 ` [PATCH 32/37] drm/bridge: display-connector: " Luca Ceresoli
2026-05-19 10:37 ` [PATCH 33/37] drm/bridge: samsung-dsim: " Luca Ceresoli
2026-05-19 10:37 ` [PATCH 34/37] drm/bridge: ti-sn65dsi83: " Luca Ceresoli
2026-05-19 10:37 ` [PATCH 35/37] drm/bridge: drm_bridge_attach(): don't fail on -EPROBE_DEFER Luca Ceresoli
2026-05-19 11:21   ` sashiko-bot
2026-05-19 10:37 ` [PATCH 36/37] drm/display: bridge-connector: handle bridge hotplug Luca Ceresoli
2026-05-19 11:15   ` sashiko-bot [this message]
2026-05-19 10:37 ` [PATCH 37/37] drm/mxsfb/lcdif: enable " Luca Ceresoli
2026-05-19 11:33   ` sashiko-bot

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=20260519111537.BE07CC2BCB3@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=Frank.Li@kernel.org \
    --cc=imx@lists.linux.dev \
    --cc=luca.ceresoli@bootlin.com \
    --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