Skip to contentSkip to conversation

drm bridge hotplug

PATCH New Luca Ceresoli 2 participants 50 messages Nov 25

Subsystems (8)
lore
Share & tools
lore

Luca Ceresoli

author reviewer
#0 lore raw

v4 cover drm bridge hotplug

Hello,

this series adds support for devices with a DRM pipeline whose final components, including one or more bridges, can be hot-plugged and hot-unplugged.

For more about the use case see the v1 cover letter:
https://lore.kernel.org/lkml/[email protected]/

Design
======

The drm_bridge_connector is nowadays the recommended way to implement DRM connectors when a chain of bridges is used.

This series proposes a small helper (drm_hotplug_helper) which drivers can use to make their encoder able to:

* receive hotplug-relevant events
* add a drm_bridge_connector when a new bridge is added making the pipeline complete in the hardware
* remove the drm_bridge_connector when a bridge is removed
* start/stop HPD polling when the connector is added/removed (despite the name, HPD is pretty much orthogonal to bridge hotplug)

Series layout
=============

There are various preparation steps before the last patches introduce bridge hotplug itself.

A. Add a dynamic variant of drmm_connector_hdmi_init() and non-drmm
hdmi-cec API
(needed for the bridge-connector to allocate the connector dynamically)

1 drm/display: hdmi-cec-helper: use drmm_ prefix for drmm action 2 drm/display: hdmi-cec-helper: add non-drmm API variant
3 drm/display: hdmi-cec-notifier-helper: use drmm_ prefix for drmm action 4 drm/display: hdmi-cec-notifier-helper: add non-drmm API variant 5 drm/connector: split drmm_connector_hdmi_init() in 3 parts 6 drm/connector: add drm_connector_hdmi_dynamic_init()

B. bridge-connector: use a dynamic drm_connector

7 drm/display: bridge-connector: split allocation code from initialization code 8 drm/display: bridge-connector: hoist error management to common code 9 drm/display: bridge-connector: use a dynamic connector
10 drm/display: bridge-connector: add APIs to add/remove the connector dynamically

C. Misc preparation work

11 drm/bridge: constify of_drm_find_and_get_bridge() device_node parameter 12 drm/bridge: initialize chain_node list head on allocation
13 drm/bridge: initialize chain_node list head on detach and attach errors 14 drm/bridge: ti-sn65dsi86: protect device resources on unplug
15 drm/atomic: drm_atomic_private_obj_fini: protect private_obj removal from list 16 drm/probe-helper: add per-connector polling enable/disable

D. Add notifier mechanism to let common code (the bridge-connector)
take actions on hotplug events

17 drm: event-notifier: add mechanism to notify about hotplug events 18 drm/bridge: notify about added and detached bridges 19 drm/mipi-dsi: notify about DSI attach

E. Let bridges return their next bridge
(allows to know when the pipeline is complete)

20 drm/bridge: add drm_bridge_get_next() and supporting func 21 drm/panel: implement .get_next_bridge
22 drm/bridge: display-connector: implement .get_next_bridge 23 drm/bridge: ti-sn65dsi83: implement .get_next_bridge 24 drm/bridge: ti-sn65dsi86: implement .get_next_bridge 25 drm/bridge: samsung-dsim: implement .get_next_bridge

F. drm_bridge: stop pipeline when a bridge is removed

26 drm/bridge: add drm_bridge_detach_from()
27 drm/atomic: add drm_atomic_shutdown_encoder()
28 drm/tests: atomic: add tests for drm_atomic_shutdown_encoder() 29 drm/bridge: clear bridge->encoder on detach
30 drm/bridge: shutdown encoder and detach downstream bridges on bridge unplug

G. Implement bridge hotplug in bridge-connector, enable it in a driver

31 drm: drm_hotplug_helper: new helper to implement bridge hotplug 32 drm/mxsfb/lcdif: enable bridge hotplug

== Grand plan

This is part of the work to support hotplug of DRM bridges.

Here's the work breakdown (➜ marks the current series):

1. … add refcounting to DRM bridges struct drm_bridge,
based on devm_drm_bridge_alloc()
A. ✔ add new alloc API and refcounting (v6.16)
B. ✔ convert all bridge drivers to new API (v6.17)
C. ✔ kunit tests (v6.17)
D. ✔ add get/put to drm_bridge_add/remove() + attach/detach()
and warn on old allocation pattern (v6.17)
E. … add get/put on drm_bridge accessors
1. ✔ drm_bridge_chain_get_first_bridge(), add cleanup action (v6.18)
2. ✔ drm_bridge_get_prev_bridge() (v6.18)
3. ✔ drm_bridge_get_next_bridge() (v6.19)
4. ✔ drm_for_each_bridge_in_chain() (v6.19)
5. ✔ drm_bridge_connector_init (v6.19)
6. ✔ protect encoder bridge chain with a mutex (v7.2)
7. ✔ of_drm_find_bridge
a. ✔ add of_drm_get_bridge() (v7.0),
convert basic direct users (v7.0-v7.1)
b. ✔ convert direct of_drm_get_bridge() users, part 2 (v7.0) c. ✔ convert direct of_drm_get_bridge() users, part 3 (v7.0) d. ✔ convert direct of_drm_get_bridge() users, part 4 (v7.1-v7.2) e. ✔ bridge-only drm_of_find_panel_or_bridge() users (v7.2)
8. … panel_bridge lifetime
a. ✔ cleanup DRM_PANEL in bridge drivers (v7.4)
b. ✔ embed a drm_bridge in every drm_panel (v7.4)
c. ✔ remove *_of_get_bridge(): non-OF drivers (v7.5) d. ✔ remove drmm_of_get_bridge() (v7.5)
e. remove devm_drm_of_get_bridge(), part 1
f. remove devm_drm_of_get_bridge(), part 2
9. ✔ enforce drm_bridge_add before drm_bridge_attach (v6.19)
F. ✔ debugfs improvements
1. ✔ add top-level 'bridges' file (v6.16)
2. ✔ show refcount and list lingering bridges (v6.19)
2. ✔ handle gracefully atomic updates during bridge removal
A. ✔ Add drm_bridge_enter/exit() to protect device resources (v7.0) B. ✔ Add drm_bridge_clear_and_put() (v7.1)
3. ✕ DSI host-device driver interaction
4. ✔ removing the need for the "always-disconnected" connector
5. ✔ Migrate i.MX LCDIF driver to bridge-connector (v7.2)
6. ➜ DRM bridge hotplug
A. ➜ Bridge hotplug management in the DRM core
1. ✔ bridge-connector: attach encoder to the connector (v7.2)
2. ➜ drm bridge hotplug
B. Device tree description

---
Changes in v4:
- Fix UAF in HPD support by adding a new API in drm_probe_helper to disable
HPD per-connector (implemented in new patch 16, used in patch 32)
- Fix locking bug in HPD support
- Fix OF node leak in HPD support
- Link to v3: https://patch.msgid.link/[email protected]

Changes in v3:
- Handle use cases where the first bridge after the encoder is hotpluggable
(in v1 and v2 the first bridge was required to be always present)
as well as all cases where the first bridge is a DSI host
- Removed "samsung-dsim: move drm_bridge_add() call to probe", not
needed anymore
- Handle hot-pluggable bridges supporting HPD, add TI SN65DSI86 DSI-to-DP
- Fully implement dynamic connector in drm_bridge_connector (CEC helpers
were missing in v2)
- Fixed shutdown to not shutdown the whole card
- as a consequence, removed (not needed anymore)
"drm/atomic: move [...] from drm_atomic_helper to drm_atomic"
- Many smaller fixes and improvements
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- Rewrote: added new drm_hotplug_helper, dropped most changes to drm_bridge_connector
- Added get_next_bridge func, dropped is_tail func, and stop
using -EPROBE_DEFER to mean a next bridge is needed but currently absent
- Rebased on drm-misc-next, which required a few reworks
- Fix dynconn mutex locking
- Fix sashiko-reported bugs
- Drop "drm/display: bridge-connector: store the drm_device pointer", not strongly needed, and adapt the remaining patches
- Simplify handle_hpd code types
- Lots of other smaller improvements
- Link to v1: https://patch.msgid.link/[email protected]

---
Luca Ceresoli (32):
drm/display: hdmi-cec-helper: use drmm_ prefix for drmm action drm/display: hdmi-cec-helper: add non-drmm API variant drm/display: hdmi-cec-notifier-helper: use drmm_ prefix for drmm action drm/display: hdmi-cec-notifier-helper: add non-drmm API variant drm/connector: split drmm_connector_hdmi_init() in 3 parts drm/connector: add drm_connector_hdmi_dynamic_init()
drm/display: bridge-connector: split allocation code from initialization code drm/display: bridge-connector: hoist error management to common code drm/display: bridge-connector: use a dynamic connector drm/display: bridge-connector: add APIs to add/remove the connector dynamically drm/bridge: constify of_drm_find_and_get_bridge() device_node parameter drm/bridge: initialize chain_node list head on allocation drm/bridge: initialize chain_node list head on detach and attach errors drm/bridge: ti-sn65dsi86: protect device resources on unplug drm/atomic: drm_atomic_private_obj_fini: protect private_obj removal from list drm/probe-helper: add per-connector polling enable/disable
drm: event-notifier: add mechanism to notify about hotplug events drm/bridge: notify about added and detached bridges
drm/mipi-dsi: notify about DSI attach
drm/bridge: add drm_bridge_get_next() and supporting func drm/panel: implement .get_next_bridge
drm/bridge: display-connector: implement .get_next_bridge drm/bridge: ti-sn65dsi83: implement .get_next_bridge
drm/bridge: ti-sn65dsi86: implement .get_next_bridge
drm/bridge: samsung-dsim: implement .get_next_bridge
drm/bridge: add drm_bridge_detach_from()
drm/atomic: add drm_atomic_shutdown_encoder()
drm/tests: atomic: add tests for drm_atomic_shutdown_encoder() drm/bridge: clear bridge->encoder on detach
drm/bridge: shutdown encoder and detach downstream bridges on bridge unplug
drm: drm_hotplug_helper: new helper to implement bridge hotplug drm/mxsfb/lcdif: enable bridge hotplug

 MAINTAINERS                                        |  16 +
 drivers/gpu/drm/Kconfig                            |   5 +
 drivers/gpu/drm/Makefile                           |   2 +
 drivers/gpu/drm/bridge/display-connector.c         |   7 +
 drivers/gpu/drm/bridge/samsung-dsim.c              |   9 +
 drivers/gpu/drm/bridge/ti-sn65dsi83.c              |   9 +
 drivers/gpu/drm/bridge/ti-sn65dsi86.c              |  85 +++++-
 drivers/gpu/drm/display/Kconfig                    |   7 +
 drivers/gpu/drm/display/Makefile                   |   2 +
 drivers/gpu/drm/display/drm_bridge_connector.c     | 233 +++++++++++----
 drivers/gpu/drm/display/drm_hdmi_cec_helper.c      |  62 ++--
 .../gpu/drm/display/drm_hdmi_cec_notifier_helper.c |  34 ++-
 drivers/gpu/drm/display/drm_hotplug_helper.c       | 325 +++++++++++++++++++++
 drivers/gpu/drm/drm_atomic.c                       | 151 ++++++++++
 drivers/gpu/drm/drm_bridge.c                       |  87 +++++-
 drivers/gpu/drm/drm_connector.c                    | 131 ++++++---
 drivers/gpu/drm/drm_hotplug_event_notifier.c       |  62 ++++
 drivers/gpu/drm/drm_mipi_dsi.c                     |   3 +
 drivers/gpu/drm/drm_panel.c                        |   8 +-
 drivers/gpu/drm/drm_probe_helper.c                 |  92 ++++++
 drivers/gpu/drm/mxsfb/Kconfig                      |   2 +-
 drivers/gpu/drm/mxsfb/lcdif_drv.c                  |  42 +--
 drivers/gpu/drm/tests/drm_bridge_test.c            | 209 +++++++++++++
 include/drm/display/drm_hdmi_cec_helper.h          |  21 ++
 include/drm/drm_atomic.h                           |   1 +
 include/drm/drm_bridge.h                           |  27 +-
 include/drm/drm_bridge_connector.h                 |   4 +
 include/drm/drm_connector.h                        |   6 +
 include/drm/drm_hotplug_event_notifier.h           |  52 ++++
 include/drm/drm_hotplug_helper.h                   |  30 ++
 include/drm/drm_probe_helper.h                     |   2 +
 31 files changed, 1567 insertions(+), 159 deletions(-)

---
base-commit: 479e381e77dcde0dfcc9d51e89ff3273da199804
change-id: 20260515-drm-bridge-hotplug-46265d3a2f85

Best regards,

Signature · 3 lines
Luca Ceresoli, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
author reviewer
#1 lore raw

PATCH v4 1/32 drm/display: hdmi-cec-helper: use drmm_ prefix for drmm action

For bridge hotplug drmm allocations are not suitable because a connector may have to be added and removed multiple times in the lifetime of a card.

In preparation to support hotpluggable HDMI bridges (and the drm_bridge_connector) we need a non-drmm variant of the CEC adapter (de)registration API, with an explicit unregister API.

To differentiate the new explicit unregister API from the existing drmm cleanup action, use the drmm_ prefix for the latter.

---

New patch in v3.

---
 drivers/gpu/drm/display/drm_hdmi_cec_helper.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/display/drm_hdmi_cec_helper.c +2 -2
index ef6dbaa59dee..b79c41703d53 100644
@@ -64,7 +64,7 @@ static void drm_connector_hdmi_cec_adapter_phys_addr_set(struct drm_connector *c
 	cec_s_phys_addr(data->adapter, addr, false);
 }
 
-static void drm_connector_hdmi_cec_adapter_unregister(struct drm_device *dev, void *res)
+static void drmm_connector_hdmi_cec_adapter_unregister(struct drm_device *dev, void *res)
 {
 	struct drm_connector *connector = res;
 	struct drm_connector_hdmi_cec_data *data = connector->cec.data;
@@ -145,7 +145,7 @@ int drmm_connector_hdmi_cec_register(struct drm_connector *connector,
 	mutex_unlock(&connector->cec.mutex);
 
 	return drmm_add_action_or_reset(connector->dev,
-					drm_connector_hdmi_cec_adapter_unregister,
+					drmm_connector_hdmi_cec_adapter_unregister,
 					connector);
 
 err_delete_adapter:
Signature · 1 line
2.56.1
author reviewer
#2 lore raw

PATCH v4 2/32 drm/display: hdmi-cec-helper: add non-drmm API variant

In reply to Luca Ceresoli

For bridge hotplug drmm allocations are not suitable because a connector may have to be added and removed multiple times in the lifetime of a card.

In preparation to support hotpluggable HDMI bridges (and the drm_bridge_connector) add a non-drmm variant of the CEC adapter (de)registration API, with an explicit unregister API.

---
 drivers/gpu/drm/display/drm_hdmi_cec_helper.c | 60 ++++++++++++++++++---------
 include/drm/display/drm_hdmi_cec_helper.h     |  6 +++
 2 files changed, 47 insertions(+), 19 deletions(-)
drivers/gpu/drm/display/drm_hdmi_cec_helper.c +41 -19
index b79c41703d53..81b271659ad9 100644
@@ -67,15 +67,8 @@ static void drm_connector_hdmi_cec_adapter_phys_addr_set(struct drm_connector *c
 static void drmm_connector_hdmi_cec_adapter_unregister(struct drm_device *dev, void *res)
 {
 	struct drm_connector *connector = res;
-	struct drm_connector_hdmi_cec_data *data = connector->cec.data;
-
-	cec_unregister_adapter(data->adapter);
-
-	if (data->funcs->uninit)
-		data->funcs->uninit(connector);
 
-	kfree(data);
-	connector->cec.data = NULL;
+	drm_connector_hdmi_cec_unregister(connector);
 }
 
 static struct drm_connector_cec_funcs drm_connector_hdmi_cec_adapter_funcs = {
@@ -83,11 +76,11 @@ static struct drm_connector_cec_funcs drm_connector_hdmi_cec_adapter_funcs = {
 	.phys_addr_set = drm_connector_hdmi_cec_adapter_phys_addr_set,
 };
 
-int drmm_connector_hdmi_cec_register(struct drm_connector *connector,
-				     const struct drm_connector_hdmi_cec_funcs *funcs,
-				     const char *name,
-				     u8 available_las,
-				     struct device *dev)
+int drm_connector_hdmi_cec_register(struct drm_connector *connector,
+				    const struct drm_connector_hdmi_cec_funcs *funcs,
+				    const char *name,
+				    u8 available_las,
+				    struct device *dev)
 {
 	struct drm_connector_hdmi_cec_data *data;
 	struct cec_connector_info conn_info;
@@ -125,9 +118,8 @@ int drmm_connector_hdmi_cec_register(struct drm_connector *connector,
 		goto err_delete_adapter;
 
 	/*
-	 * NOTE: the CEC adapter will be unregistered by drmm cleanup from
-	 * drm_managed_release(), which is called from drm_dev_release()
-	 * during device unbind.
+	 * NOTE: the CEC adapter will be unregistered by
+	 * cec_unregister_adapter().
 	 *
 	 * However, the CEC framework cleans up the CEC adapter only when the
 	 * last user has closed its file descriptor, so we don't need to handle
@@ -144,9 +136,7 @@ int drmm_connector_hdmi_cec_register(struct drm_connector *connector,
 
 	mutex_unlock(&connector->cec.mutex);
 
-	return drmm_add_action_or_reset(connector->dev,
-					drmm_connector_hdmi_cec_adapter_unregister,
-					connector);
+	return 0;
 
 err_delete_adapter:
 	cec_delete_adapter(cec_adap);
@@ -160,6 +150,38 @@ int drmm_connector_hdmi_cec_register(struct drm_connector *connector,
 
 	return ret;
 }
+EXPORT_SYMBOL(drm_connector_hdmi_cec_register);
+
+void drm_connector_hdmi_cec_unregister(struct drm_connector *connector)
+{
+	struct drm_connector_hdmi_cec_data *data = connector->cec.data;
+
+	cec_unregister_adapter(data->adapter);
+
+	if (data->funcs->uninit)
+		data->funcs->uninit(connector);
+
+	kfree(data);
+	connector->cec.data = NULL;
+}
+EXPORT_SYMBOL(drm_connector_hdmi_cec_unregister);
+
+int drmm_connector_hdmi_cec_register(struct drm_connector *connector,
+				     const struct drm_connector_hdmi_cec_funcs *funcs,
+				     const char *name,
+				     u8 available_las,
+				     struct device *dev)
+{
+	int ret;
+
+	ret = drm_connector_hdmi_cec_register(connector, funcs, name, available_las, dev);
+	if (ret)
+		return ret;
+
+	return drmm_add_action_or_reset(connector->dev,
+					drmm_connector_hdmi_cec_adapter_unregister,
+					connector);
+}
 EXPORT_SYMBOL(drmm_connector_hdmi_cec_register);
 
 void drm_connector_hdmi_cec_received_msg(struct drm_connector *connector,
include/drm/display/drm_hdmi_cec_helper.h +6 -0
index fd8f4d2f02c1..7e76a163e3a5 100644
@@ -39,6 +39,12 @@ struct drm_connector_hdmi_cec_funcs {
 			u32 signal_free_time, struct cec_msg *msg);
 };
 
+int drm_connector_hdmi_cec_register(struct drm_connector *connector,
+				    const struct drm_connector_hdmi_cec_funcs *funcs,
+				    const char *name,
+				    u8 available_las,
+				    struct device *dev);
+void drm_connector_hdmi_cec_unregister(struct drm_connector *connector);
 int drmm_connector_hdmi_cec_register(struct drm_connector *connector,
 				     const struct drm_connector_hdmi_cec_funcs *funcs,
 				     const char *name,
Signature · 1 line
2.56.1
#3 lore raw

Re: [PATCH v4 02/32] drm/display: hdmi-cec-helper: add non-drmm API variant

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] drm/display: missing lock and clear in drm_connector_hdmi_cec_unregister() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/display: missing lock and clear in drm_connector_hdmi_cec_unregister()
--

commit 6c19735a9b5d351aeeed042f0b7e6649c04d3e13
Author: Luca Ceresoli <[email protected]>

drm/display: hdmi-cec-helper: add non-drmm API variant

This commit adds non-drmm drm_connector_hdmi_cec_register() and drm_connector_hdmi_cec_unregister() helpers to support hotpluggable HDMI bridges.

Quoted 4 lines
drivers/gpu/drm/display/drm_hdmi_cec_helper.c +0 -0
index b79c41703d539..81b271659ad9c 100644

[ ... ]

Quoted 9 lines
drivers/gpu/drm/display/drm_hdmi_cec_helper.c +5 -0
@@ -160,6 +150,38 @@ int drmm_connector_hdmi_cec_register(struct drm_connector *connector,
 
 	return ret;
 }
+EXPORT_SYMBOL(drm_connector_hdmi_cec_register);
+
+void drm_connector_hdmi_cec_unregister(struct drm_connector *connector)
+{
+	struct drm_connector_hdmi_cec_data *data = connector->cec.data;

drivers/gpu/drm/display/drm_hdmi_cec_helper.c int drmm_connector_hdmi_cec_register(struct drm_connector *connector, · line 167

Quoted 10 lines
drivers/gpu/drm/display/drm_hdmi_cec_helper.c +10 -0
+
+	cec_unregister_adapter(data->adapter);
+
+	if (data->funcs->uninit)
+		data->funcs->uninit(connector);
+
+	kfree(data);
+	connector->cec.data = NULL;
+}
+EXPORT_SYMBOL(drm_connector_hdmi_cec_unregister);

[Severity: High]
Does drm_connector_hdmi_cec_unregister() need to hold connector->cec.mutex and clear connector->cec.funcs?

drm_connector_cec_phys_addr_invalidate() and drm_connector_cec_phys_addr_set() serialize on connector->cec.mutex and check only connector->cec.funcs before invoking the callback, which then dereferences connector->cec.data->adapter. Because drm_connector_hdmi_cec_unregister() frees data without holding connector->cec.mutex and leaves connector->cec.funcs populated while connector->cec.data is NULL, a concurrent or subsequent hotplug path such as drm_atomic_helper_connector_hdmi_hotplug() can trigger a use-after-free or NULL pointer dereference on data.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author reviewer
#4 lore raw

PATCH v4 3/32 drm/display: hdmi-cec-notifier-helper: use drmm_ prefix for drmm action

In reply to Luca Ceresoli

For bridge hotplug drmm allocations are not suitable because a connector may have to be added and removed multiple times in the lifetime of a card.

In preparation to support hotpluggable HDMI bridges (and the drm_bridge_connector) we need a non-drmm variant of the CEC notifier (de)registration API, with an explicit unregister API.

To differentiate the new explicit unregister API from the existing drmm cleanup action, use the drmm_ prefix for the latter.

---

New patch in v3.

---
 drivers/gpu/drm/display/drm_hdmi_cec_notifier_helper.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/display/drm_hdmi_cec_notifier_helper.c +2 -2
index 31b8e4a93e24..47662bdc5d08 100644
@@ -25,7 +25,7 @@ static void drm_connector_hdmi_cec_notifier_phys_addr_set(struct drm_connector *
 	cec_notifier_set_phys_addr(connector->cec.data, addr);
 }
 
-static void drm_connector_hdmi_cec_notifier_unregister(struct drm_device *dev, void *res)
+static void drmm_connector_hdmi_cec_notifier_unregister(struct drm_device *dev, void *res)
 {
 	struct drm_connector *connector = res;
 
@@ -59,7 +59,7 @@ int drmm_connector_hdmi_cec_notifier_register(struct drm_connector *connector,
 	mutex_unlock(&connector->cec.mutex);
 
 	return drmm_add_action_or_reset(connector->dev,
-					drm_connector_hdmi_cec_notifier_unregister,
+					drmm_connector_hdmi_cec_notifier_unregister,
 					connector);
 }
 EXPORT_SYMBOL(drmm_connector_hdmi_cec_notifier_register);
Signature · 1 line
2.56.1
author reviewer
#5 lore raw

PATCH v4 4/32 drm/display: hdmi-cec-notifier-helper: add non-drmm API variant

In reply to Luca Ceresoli

For bridge hotplug drmm allocations are not suitable because a connector may have to be added and removed multiple times in the lifetime of a card.

In preparation to support hotpluggable HDMI bridges (and the drm_bridge_connector) add a non-drmm variant of the CEC notifier (de)registration API, with an explicit unregister API.

---

New patch in v3.

---
 .../gpu/drm/display/drm_hdmi_cec_notifier_helper.c | 30 ++++++++++++++++++----
 include/drm/display/drm_hdmi_cec_helper.h          | 15 +++++++++++
 2 files changed, 40 insertions(+), 5 deletions(-)
drivers/gpu/drm/display/drm_hdmi_cec_notifier_helper.c +25 -5
index 47662bdc5d08..2f30844fcdad 100644
@@ -29,8 +29,7 @@ static void drmm_connector_hdmi_cec_notifier_unregister(struct drm_device *dev,
 {
 	struct drm_connector *connector = res;
 
-	cec_notifier_conn_unregister(connector->cec.data);
-	connector->cec.data = NULL;
+	drm_connector_hdmi_cec_notifier_unregister(connector);
 }
 
 static const struct drm_connector_cec_funcs drm_connector_cec_notifier_funcs = {
@@ -38,9 +37,9 @@ static const struct drm_connector_cec_funcs drm_connector_cec_notifier_funcs = {
 	.phys_addr_set = drm_connector_hdmi_cec_notifier_phys_addr_set,
 };
 
-int drmm_connector_hdmi_cec_notifier_register(struct drm_connector *connector,
-					      const char *port_name,
-					      struct device *dev)
+int drm_connector_hdmi_cec_notifier_register(struct drm_connector *connector,
+					     const char *port_name,
+					     struct device *dev)
 {
 	struct cec_connector_info conn_info;
 	struct cec_notifier *notifier;
@@ -58,6 +57,27 @@ int drmm_connector_hdmi_cec_notifier_register(struct drm_connector *connector,
 
 	mutex_unlock(&connector->cec.mutex);
 
+	return 0;
+}
+EXPORT_SYMBOL(drm_connector_hdmi_cec_notifier_register);
+
+void drm_connector_hdmi_cec_notifier_unregister(struct drm_connector *connector)
+{
+	cec_notifier_conn_unregister(connector->cec.data);
+	connector->cec.data = NULL;
+}
+EXPORT_SYMBOL(drm_connector_hdmi_cec_notifier_unregister);
+
+int drmm_connector_hdmi_cec_notifier_register(struct drm_connector *connector,
+					      const char *port_name,
+					      struct device *dev)
+{
+	int ret;
+
+	ret = drm_connector_hdmi_cec_notifier_register(connector, port_name, dev);
+	if (ret)
+		return ret;
+
 	return drmm_add_action_or_reset(connector->dev,
 					drmm_connector_hdmi_cec_notifier_unregister,
 					connector);
include/drm/display/drm_hdmi_cec_helper.h +15 -0
index 7e76a163e3a5..8d648532eeb7 100644
@@ -63,10 +63,25 @@ void drm_connector_hdmi_cec_transmit_attempt_done(struct drm_connector *connecto
 						  u8 status);
 
 #if IS_ENABLED(CONFIG_DRM_DISPLAY_HDMI_CEC_NOTIFIER_HELPER)
+int drm_connector_hdmi_cec_notifier_register(struct drm_connector *connector,
+					     const char *port_name,
+					     struct device *dev);
+void drm_connector_hdmi_cec_notifier_unregister(struct drm_connector *connector);
 int drmm_connector_hdmi_cec_notifier_register(struct drm_connector *connector,
 					      const char *port_name,
 					      struct device *dev);
 #else
+static inline int drm_connector_hdmi_cec_notifier_register(struct drm_connector *connector,
+							   const char *port_name,
+							   struct device *dev)
+{
+	return 0;
+}
+
+static inline void drm_connector_hdmi_cec_notifier_unregister(struct drm_connector *connector)
+{
+}
+
 static inline int drmm_connector_hdmi_cec_notifier_register(struct drm_connector *connector,
 							    const char *port_name,
 							    struct device *dev)
Signature · 1 line
2.56.1
#6 lore raw

Re: [PATCH v4 04/32] drm/display: hdmi-cec-notifier-helper: add non-drmm API variant

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] drm/display: missing cec.mutex in drm_connector_hdmi_cec_notifier_unregister() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] drm/display: missing cec.mutex in drm_connector_hdmi_cec_notifier_unregister()
--

commit 524d66733b09456c82258b5e235e9f87be3e61c7
Author: Luca Ceresoli <[email protected]>

drm/display: hdmi-cec-notifier-helper: add non-drmm API variant

This commit adds non-drmm register and unregister helpers for the HDMI CEC notifier so that hotpluggable HDMI bridges can dynamically register and unregister their CEC notifiers.

Quoted 4 lines
drivers/gpu/drm/display/drm_hdmi_cec_notifier_helper.c +0 -0
index 47662bdc5d08f..2f30844fcdad9 100644

[ ... ]

Quoted 4 lines
drivers/gpu/drm/display/drm_hdmi_cec_notifier_helper.c +0 -0
@@ -58,6 +57,27 @@ int drmm_connector_hdmi_cec_notifier_register(struct drm_connector *connector,
 
 	mutex_unlock(&connector->cec.mutex);
 

drivers/gpu/drm/display/drm_hdmi_cec_notifier_helper.c int drmm_connector_hdmi_cec_notifier_register(struct drm_connector *connector, · line 69

Quoted 10 lines
drivers/gpu/drm/display/drm_hdmi_cec_notifier_helper.c +10 -0
+	return 0;
+}
+EXPORT_SYMBOL(drm_connector_hdmi_cec_notifier_register);
+
+void drm_connector_hdmi_cec_notifier_unregister(struct drm_connector *connector)
+{
+	cec_notifier_conn_unregister(connector->cec.data);
+	connector->cec.data = NULL;
+}
+EXPORT_SYMBOL(drm_connector_hdmi_cec_notifier_unregister);

[Severity: Medium]
Should drm_connector_hdmi_cec_notifier_unregister() take connector->cec.mutex and also clear connector->cec.funcs?

drm_connector_hdmi_cec_notifier_register() sets connector->cec.data and connector->cec.funcs under connector->cec.mutex, and drm_connector_cec_phys_addr_set() and drm_connector_cec_phys_addr_invalidate() hold the same mutex while calling those callbacks. When a hotpluggable bridge unregisters its notifier at runtime while connector detection runs concurrently, the lack of locking allows a use-after-free on struct cec_notifier:

CPU0 CPU1
drm_connector_hdmi_cec_notifier_unregister()
cec_notifier_conn_unregister(connector->cec.data)

    cec_notifier_put(n)  [frees n]
                                        drm_connector_cec_phys_addr_set()
                                          mutex_lock(&connector->cec.mutex)
                                          ...->phys_addr_set()
                                            cec_notifier_set_phys_addr(
                                              connector->cec.data, ...)
                                              mutex_lock(&n->lock)
                                                <-- use-after-free

connector->cec.data = NULL;

Leaving connector->cec.funcs non-NULL also allows subsequent physical address updates on the connector to keep invoking the notifier callbacks with a NULL connector->cec.data pointer.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
author reviewer
#7 lore raw

PATCH v4 5/32 drm/connector: split drmm_connector_hdmi_init() in 3 parts

In reply to Luca Ceresoli

In preparation for adding hotpluggable bridges we need connectors to be created dynamically, both regular connectors and HDMI ones.

For non-HDMI connectors drm_connector_init() already has a dynamic variant, but there is none for HDMI. Creating one would be easy by creating a mostly-identical copy of drmm_connector_hdmi_init(), but it is a long function so there would be a lot of duplicated code.

drmm_connector_hdmi_init() currently has 3 sections:

1. sanity checks
2. call drmm_connector_init()
3. initialize HDMI-specific fields not initialized at step 2

For the dynamic variant, sectons 1 and 3 would be an exact copy, while section 2 needs to be different.

To avoid code duplication, split parts 1 and 3 to subfunctions. The next commit will introduce the dynamic variant.

No functional changes. Just moving code around.

---

Changes in v2:
- expanded commit message to clarify rationale
- rebased on current drm-misc-next, which required a full rewrite after the signature of drmm_connector_hdmi_init() changed in commit 400c9ede1ea4 ("drm/connector: Add drmm_connector_hdmi_init() with new signature")
- renamed drm_connector_hdmi_init() to drm_connector_hdmi_initialize() as or it would look like an HDMI version of drm_connector_init()

---
 drivers/gpu/drm/drm_connector.c | 88 +++++++++++++++++++++++++----------------
 1 file changed, 55 insertions(+), 33 deletions(-)
drivers/gpu/drm/drm_connector.c +55 -33
index da0b16f25ac4..20120b6a05d5 100644
@@ -542,37 +542,11 @@ int drmm_connector_init(struct drm_device *dev,
 }
 EXPORT_SYMBOL(drmm_connector_init);
 
-/**
- * drmm_connector_hdmi_init - Init a preallocated HDMI connector
- * @dev: DRM device
- * @connector: A pointer to the HDMI connector to init
- * @funcs: callbacks for this connector
- * @hdmi_funcs: HDMI-related callbacks and capabilities for this connector
- * @connector_type: user visible type of the connector
- * @ddc: optional pointer to the associated ddc adapter
- *
- * Initialises a preallocated HDMI connector. Connectors can be
- * subclassed as part of driver connector objects.
- *
- * Cleanup is automatically handled with a call to
- * drm_connector_cleanup() in a DRM-managed action.
- *
- * The connector structure should be allocated with drmm_kzalloc().
- *
- * The @drm_connector_funcs.destroy hook must be NULL.
- *
- * Returns:
- * Zero on success, error code on failure.
- */
-int drmm_connector_hdmi_init(struct drm_device *dev,
-			     struct drm_connector *connector,
-			     const struct drm_connector_funcs *funcs,
-			     const struct drm_connector_hdmi_funcs *hdmi_funcs,
-			     int connector_type,
-			     struct i2c_adapter *ddc)
+static int drm_connector_hdmi_sanity_checks(struct drm_device *dev,
+					    struct drm_connector *connector,
+					    const struct drm_connector_hdmi_funcs *hdmi_funcs,
+					    int connector_type)
 {
-	int ret;
-
 	if (!hdmi_funcs)
 		return -EINVAL;
 
@@ -613,9 +587,15 @@ int drmm_connector_hdmi_init(struct drm_device *dev,
 	      connector_type == DRM_MODE_CONNECTOR_HDMIB))
 		return -EINVAL;
 
-	ret = drmm_connector_init(dev, connector, funcs, connector_type, ddc);
-	if (ret)
-		return ret;
+	return 0;
+}
+
+/* Initialize HDMI-specific resources of a connector */
+static int drm_connector_hdmi_initialize(struct drm_device *dev,
+					 struct drm_connector *connector,
+					 const struct drm_connector_hdmi_funcs *hdmi_funcs)
+{
+	int ret;
 
 	/* TODO: remove after conversion to new drmm_connector_hdmi_init() */
 	connector->hdmi.supported_formats = hdmi_funcs->supported_formats;
@@ -680,6 +660,48 @@ int drmm_connector_hdmi_init(struct drm_device *dev,
 
 	return 0;
 }
+
+/**
+ * drmm_connector_hdmi_init - Init a preallocated HDMI connector
+ * @dev: DRM device
+ * @connector: A pointer to the HDMI connector to init
+ * @funcs: callbacks for this connector
+ * @hdmi_funcs: HDMI-related callbacks and capabilities for this connector
+ * @connector_type: user visible type of the connector
+ * @ddc: optional pointer to the associated ddc adapter
+ *
+ * Initialises a preallocated HDMI connector. Connectors can be
+ * subclassed as part of driver connector objects.
+ *
+ * Cleanup is automatically handled with a call to
+ * drm_connector_cleanup() in a DRM-managed action.
+ *
+ * The connector structure should be allocated with drmm_kzalloc().
+ *
+ * The @drm_connector_funcs.destroy hook must be NULL.
+ *
+ * Returns:
+ * Zero on success, error code on failure.
+ */
+int drmm_connector_hdmi_init(struct drm_device *dev,
+			     struct drm_connector *connector,
+			     const struct drm_connector_funcs *funcs,
+			     const struct drm_connector_hdmi_funcs *hdmi_funcs,
+			     int connector_type,
+			     struct i2c_adapter *ddc)
+{
+	int ret;
+
+	ret = drm_connector_hdmi_sanity_checks(dev, connector, hdmi_funcs, connector_type);
+	if (ret)
+		return ret;
+
+	ret = drmm_connector_init(dev, connector, funcs, connector_type, ddc);
+	if (ret)
+		return ret;
+
+	return drm_connector_hdmi_initialize(dev, connector, hdmi_funcs);
+}
 EXPORT_SYMBOL(drmm_connector_hdmi_init);
 
 /**
Signature · 1 line
2.56.1
author reviewer
#8 lore raw

PATCH v4 6/32 drm/connector: add drm_connector_hdmi_dynamic_init()

In reply to Luca Ceresoli

In preparation for adding hotpluggable bridges into the drm_bridge_connector, we need connectors to be created dynamically, both regular connectors and HDMI ones. drm_connector_init() already has a dynamic variant, add one for HDMI connectors too.

---

Changes in v3:
- fix error management dead code
- fix kerneldoc

Changes in v2:
- rebased on current drm-misc-next: now drm_connector_hdmi_init() can fail, so call drm_connector_cleanup(connector) if that happens

---
 drivers/gpu/drm/drm_connector.c | 43 +++++++++++++++++++++++++++++++++++++++++
 include/drm/drm_connector.h     |  6 ++++++
 2 files changed, 49 insertions(+)
drivers/gpu/drm/drm_connector.c +43 -0
index 20120b6a05d5..021f2e120fdc 100644
@@ -704,6 +704,49 @@ int drmm_connector_hdmi_init(struct drm_device *dev,
 }
 EXPORT_SYMBOL(drmm_connector_hdmi_init);
 
+/**
+ * drm_connector_hdmi_dynamic_init - Init a preallocated dynamic HDMI connector
+ * @dev: DRM device
+ * @connector: A pointer to the HDMI connector to init
+ * @funcs: callbacks for this connector
+ * @hdmi_funcs: HDMI-related callbacks and capabilities for this connector
+ * @connector_type: user visible type of the connector
+ * @ddc: optional pointer to the associated ddc adapter
+ *
+ * Initialises a preallocated dynamic HDMI connector. Connectors can be
+ * subclassed as part of driver connector objects.
+ *
+ * See drm_connector_dynamic_init(), the same constraints apply here. This
+ * is just the HDMI version.
+ *
+ * Returns:
+ * Zero on success, error code on failure.
+ */
+int drm_connector_hdmi_dynamic_init(struct drm_device *dev,
+				    struct drm_connector *connector,
+				    const struct drm_connector_funcs *funcs,
+				    const struct drm_connector_hdmi_funcs *hdmi_funcs,
+				    int connector_type,
+				    struct i2c_adapter *ddc)
+{
+	int ret;
+
+	ret = drm_connector_hdmi_sanity_checks(dev, connector, hdmi_funcs, connector_type);
+	if (ret)
+		return ret;
+
+	ret = drm_connector_dynamic_init(dev, connector, funcs, connector_type, ddc);
+	if (ret)
+		return ret;
+
+	ret = drm_connector_hdmi_initialize(dev, connector, hdmi_funcs);
+	if (ret)
+		drm_connector_cleanup(connector);
+
+	return ret;
+}
+EXPORT_SYMBOL(drm_connector_hdmi_dynamic_init);
+
 /**
  * drmm_connector_hdmi_ini2 - Init a preallocated HDMI connector
  * @dev: DRM device
include/drm/drm_connector.h +6 -0
index 07218ab26c9e..47de3b6d7218 100644
@@ -2743,6 +2743,12 @@ int drmm_connector_hdmi_init(struct drm_device *dev,
 			     const struct drm_connector_hdmi_funcs *hdmi_funcs,
 			     int connector_type,
 			     struct i2c_adapter *ddc);
+int drm_connector_hdmi_dynamic_init(struct drm_device *dev,
+				    struct drm_connector *connector,
+				    const struct drm_connector_funcs *funcs,
+				    const struct drm_connector_hdmi_funcs *hdmi_funcs,
+				    int connector_type,
+				    struct i2c_adapter *ddc);
 int drmm_connector_hdmi_ini2(struct drm_device *dev,
 			     struct drm_connector *connector,
 			     const char *vendor, const char *product,
Signature · 1 line
2.56.1
#9 lore raw

Re: [PATCH v4 06/32] drm/connector: add drm_connector_hdmi_dynamic_init()

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] drm/connector: dynamic HDMI init creates non-refcounted drm_property post-reg --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/connector: dynamic HDMI init creates non-refcounted drm_property post-reg
--

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

drm/connector: add drm_connector_hdmi_dynamic_init()

This commit adds drm_connector_hdmi_dynamic_init() to initialize dynamically created HDMI connectors for hotpluggable bridges.

Quoted 40 lines
drivers/gpu/drm/drm_connector.c +32 -0
index 20120b6a05d52..021f2e120fdc7 100644
@@ -704,6 +704,49 @@ int drmm_connector_hdmi_init(struct drm_device *dev,
 }
 EXPORT_SYMBOL(drmm_connector_hdmi_init);
 
+/**
+ * drm_connector_hdmi_dynamic_init - Init a preallocated dynamic HDMI connector
+ * @dev: DRM device
+ * @connector: A pointer to the HDMI connector to init
+ * @funcs: callbacks for this connector
+ * @hdmi_funcs: HDMI-related callbacks and capabilities for this connector
+ * @connector_type: user visible type of the connector
+ * @ddc: optional pointer to the associated ddc adapter
+ *
+ * Initialises a preallocated dynamic HDMI connector. Connectors can be
+ * subclassed as part of driver connector objects.
+ *
+ * See drm_connector_dynamic_init(), the same constraints apply here. This
+ * is just the HDMI version.
+ *
+ * Returns:
+ * Zero on success, error code on failure.
+ */
+int drm_connector_hdmi_dynamic_init(struct drm_device *dev,
+				    struct drm_connector *connector,
+				    const struct drm_connector_funcs *funcs,
+				    const struct drm_connector_hdmi_funcs *hdmi_funcs,
+				    int connector_type,
+				    struct i2c_adapter *ddc)
+{
+	int ret;
+
+	ret = drm_connector_hdmi_sanity_checks(dev, connector, hdmi_funcs, connector_type);
+	if (ret)
+		return ret;
+
+	ret = drm_connector_dynamic_init(dev, connector, funcs, connector_type, ddc);

drivers/gpu/drm/drm_connector.c int drmm_connector_hdmi_init(struct drm_device *dev, · line 748

Quoted 10 lines
drivers/gpu/drm/drm_connector.c +10 -0
+	if (ret)
+		return ret;
+
+	ret = drm_connector_hdmi_initialize(dev, connector, hdmi_funcs);
+	if (ret)
+		drm_connector_cleanup(connector);
+
+	return ret;
+}
+EXPORT_SYMBOL(drm_connector_hdmi_dynamic_init);

[Severity: High]
Can drm_connector_hdmi_initialize() be called safely after drm_dev_register() has registered the DRM device?

On a fresh connector, drm_connector_hdmi_initialize() calls drm_connector_attach_max_bpc_property() and drm_connector_attach_color_format_property(), which create new drm_property objects via drm_property_create_range() and drm_property_create_enum(). Because DRM properties are non-refcounted mode objects (obj_free_cb is NULL), calling drm_property_create() -> drm_mode_object_add() when dev->registered is true triggers:

drivers/gpu/drm/drm_mode_object.c:__drm_mode_object_add() {
	WARN_ON(!dev->driver->load && dev->registered && !obj_free_cb);
}

In addition, drm_property_create() adds the property to dev->mode_config.property_list without locking and publishes it in dev->mode_config.object_idr before initializing its fields and enum_list, allowing a concurrent drm_mode_getproperty_ioctl() to observe an uninitialized property. Finally, drm_connector_cleanup() does not destroy connector->max_bpc_property or connector->color_format_property, so those properties and their mode object IDs are leaked if drm_connector_attach_color_format_property() fails or each time an HDMI connector is hot-unplugged and re-plugged.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
author reviewer
#10 lore raw

PATCH v4 7/32 drm/display: bridge-connector: split allocation code from initialization code

In reply to Luca Ceresoli

Currently drm_bridge_connector_init() does two things:

* allocate and initialize the drm_bridge_connector (which embeds a drm_connector) using drmm
* initialize and register the embedded drm_connector

For bridge hotplug drmm allocations are not suitable because a connector may have to be added and removed multiple times in the lifetime of a card.

In preparation to support that, split out from drm_bridge_connector_init() the code to allocate the drm_bridge_connector, so new (de)allocation code can reuse all the initialization code.

---

Changes in v3:
- Slightly tweak commit title

---
 drivers/gpu/drm/display/drm_bridge_connector.c | 62 ++++++++++++++++----------
 1 file changed, 38 insertions(+), 24 deletions(-)
drivers/gpu/drm/display/drm_bridge_connector.c +38 -24
index 2202e6044444..19cacae617dc 100644
@@ -837,27 +837,11 @@ static void drm_bridge_connector_put_bridges(struct drm_device *dev, void *data)
 	drm_bridge_put(bridge_connector->bridge_hdmi_cec);
 }
 
-/**
- * drm_bridge_connector_init - Initialise a connector for a chain of bridges
- * @drm: the DRM device
- * @encoder: the encoder where the bridge chain starts
- *
- * Create a new &drm_bridge_connector for the @drm device. The connector is
- * allocated, initialised, registered with the @drm device and attached to
- * @encoder.
- *
- * The connector is associated with a chain of bridges that starts at
- * the @encoder. All bridges in the chain shall report bridge operation flags
- * (&drm_bridge->ops) and bridge output type (&drm_bridge->type), and none of
- * them may create a DRM connector directly.
- *
- * Returns a pointer to the new connector on success, or a negative error
- * pointer otherwise.
- */
-struct drm_connector *drm_bridge_connector_init(struct drm_device *drm,
-						struct drm_encoder *encoder)
+static struct drm_connector *
+drm_bridge_connector_initialize(struct drm_bridge_connector *bridge_connector,
+				struct drm_device *drm,
+				struct drm_encoder *encoder)
 {
-	struct drm_bridge_connector *bridge_connector;
 	struct drm_connector *connector;
 	struct i2c_adapter *ddc = NULL;
 	struct drm_bridge *panel_bridge __free(drm_bridge_put) = NULL;
@@ -865,10 +849,6 @@ struct drm_connector *drm_bridge_connector_init(struct drm_device *drm,
 	int connector_type;
 	int ret;
 
-	bridge_connector = drmm_kzalloc(drm, sizeof(*bridge_connector), GFP_KERNEL);
-	if (!bridge_connector)
-		return ERR_PTR(-ENOMEM);
-
 	ret = drmm_add_action(drm, drm_bridge_connector_put_bridges, bridge_connector);
 	if (ret)
 		return ERR_PTR(ret);
@@ -1154,4 +1134,38 @@ struct drm_connector *drm_bridge_connector_init(struct drm_device *drm,
 
 	return connector;
 }
+
+/**
+ * drm_bridge_connector_init - Initialise a connector for a chain of bridges
+ * @drm: the DRM device
+ * @encoder: the encoder where the bridge chain starts
+ *
+ * Create a new &drm_bridge_connector for the @drm device. The connector is
+ * allocated, initialised, registered with the @drm device and attached to
+ * @encoder.
+ *
+ * The connector is associated with a chain of bridges that starts at
+ * the @encoder. All bridges in the chain shall report bridge operation flags
+ * (&drm_bridge->ops) and bridge output type (&drm_bridge->type), and none of
+ * them may create a DRM connector directly.
+ *
+ * Returns a pointer to the new connector on success, or a negative error
+ * pointer otherwise.
+ */
+struct drm_connector *drm_bridge_connector_init(struct drm_device *drm,
+						struct drm_encoder *encoder)
+{
+	struct drm_bridge_connector *bridge_connector;
+	struct drm_connector *connector;
+
+	bridge_connector = drmm_kzalloc(drm, sizeof(*bridge_connector), GFP_KERNEL);
+	if (!bridge_connector)
+		return ERR_PTR(-ENOMEM);
+
+	connector = drm_bridge_connector_initialize(bridge_connector, drm, encoder);
+	if (IS_ERR(connector))
+		return connector;
+
+	return connector;
+}
 EXPORT_SYMBOL_GPL(drm_bridge_connector_init);
Signature · 1 line
2.56.1
author reviewer
#11 lore raw

PATCH v4 8/32 drm/display: bridge-connector: hoist error management to common code

In reply to Luca Ceresoli

In prepataion to add more error management code common to the HDMI and non-HDMI branches, move error management to be common to both cases.

---
 drivers/gpu/drm/display/drm_bridge_connector.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/gpu/drm/display/drm_bridge_connector.c +2 -4
index 19cacae617dc..f4a71801e8a3 100644
@@ -1057,15 +1057,13 @@ drm_bridge_connector_initialize(struct drm_bridge_connector *bridge_connector,
 					       &drm_bridge_connector_funcs,
 					       &bridge_connector->hdmi_funcs,
 					       connector_type, ddc);
-		if (ret)
-			return ERR_PTR(ret);
 	} else {
 		ret = drmm_connector_init(drm, connector,
 					  &drm_bridge_connector_funcs,
 					  connector_type, ddc);
-		if (ret)
-			return ERR_PTR(ret);
 	}
+	if (ret)
+		return ret;
 
 	if (bridge_connector->bridge_hdmi_audio ||
 	    bridge_connector->bridge_dp_audio) {
Signature · 1 line
2.56.1
#12 lore raw

Re: [PATCH v4 08/32] drm/display: bridge-connector: hoist error management to common code

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] drm/bridge-connector: int return in drm_bridge_connector_initialize() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/bridge-connector: int return in drm_bridge_connector_initialize()
--

commit a701ba5bb1c19c7b9fcfc98010ada24b2c5ec042
Author: Luca Ceresoli <[email protected]>

drm/display: bridge-connector: hoist error management to common code

This commit moves the error check for drmm_connector_hdmi_init() and drmm_connector_init() out of the if/else branches in drm_bridge_connector_initialize().

Quoted 9 lines
drivers/gpu/drm/display/drm_bridge_connector.c +0 -1
index 19cacae617dce..f4a71801e8a3b 100644
@@ -1057,15 +1057,13 @@ drm_bridge_connector_initialize(struct drm_bridge_connector *bridge_connector,
 					       &drm_bridge_connector_funcs,
 					       &bridge_connector->hdmi_funcs,
 					       connector_type, ddc);
-		if (ret)

drivers/gpu/drm/display/drm_bridge_connector.c drm_bridge_connector_initialize(struct drm_bridge_connector *bridge_connector, · line 1066

Quoted 10 lines
drivers/gpu/drm/display/drm_bridge_connector.c +2 -3
-			return ERR_PTR(ret);
 	} else {
 		ret = drmm_connector_init(drm, connector,
 					  &drm_bridge_connector_funcs,
 					  connector_type, ddc);
-		if (ret)
-			return ERR_PTR(ret);
 	}
+	if (ret)
+		return ret;

[Severity: High]
Should this return ERR_PTR(ret) instead of ret? drm_bridge_connector_initialize() returns a struct drm_connector *, so returning an int fails to build with -Werror=int-conversion (and breaks git bisect before commit "drm/display: bridge-connector: use a dynamic connector" restores ERR_PTR(ret) later in the series).

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
author reviewer
#13 lore raw

PATCH v4 9/32 drm/display: bridge-connector: use a dynamic connector

In reply to Luca Ceresoli

Currently the drm_bridge_connector uses drmm functions to add the drm_connector. For bridge hotplug drmm allocations are not suitable because a connector may have to be added and removed multiple times in the lifetime of a card.

In preparation for bridge hotplug, use the dynamic variants of drm_connector[_hdmi]_init() and the non-drmm versions of drmm_connector_hdmi_cec[_notifier]_register() so the drm_connector can be removed without removing the whole card.

---

Changes in v4:
- Add fwnode_handle_put(connector->fwnode) to fix OF node leak if the OF node is removed by a DT overlay

Changes in v3:
- convert CEC code to non-drmm too
- fix connector state error management
- put the connector on error paths after the dynamic_init() step
- fix drmm-allocation leak on drm_bridge_connector_init() error path

---
 drivers/gpu/drm/display/drm_bridge_connector.c | 149 ++++++++++++++++++-------
 1 file changed, 110 insertions(+), 39 deletions(-)
drivers/gpu/drm/display/drm_bridge_connector.c +110 -39
index f4a71801e8a3..d81cfc7aa295 100644
@@ -132,11 +132,27 @@ struct drm_bridge_connector {
 	 * bridge connector.
 	 */
 	struct drm_connector_hdmi_funcs hdmi_funcs;
+	/** @has_cec_adapter: true if a CEC adapter is registered */
+	bool has_cec_adapter;
+	/** @has_cec_notifier true if a CEC notifier is registered */
+	bool has_cec_notifier;
 };
 
 #define to_drm_bridge_connector(x) \
 	container_of(x, struct drm_bridge_connector, base)
 
+static void drm_bridge_connector_put_bridges(struct drm_bridge_connector *bridge_connector)
+{
+	drm_bridge_put(bridge_connector->bridge_edid);
+	drm_bridge_put(bridge_connector->bridge_hpd);
+	drm_bridge_put(bridge_connector->bridge_detect);
+	drm_bridge_put(bridge_connector->bridge_modes);
+	drm_bridge_put(bridge_connector->bridge_hdmi);
+	drm_bridge_put(bridge_connector->bridge_hdmi_audio);
+	drm_bridge_put(bridge_connector->bridge_dp_audio);
+	drm_bridge_put(bridge_connector->bridge_hdmi_cec);
+}
+
 /* -----------------------------------------------------------------------------
  * Bridge Connector Hot-Plug Handling
  */
@@ -267,6 +283,14 @@ drm_bridge_connector_color_format(const struct drm_connector_state *conn_state)
 	return conn_state->color_format;
 }
 
+static void drm_bridge_connector_dynconn_destroy(struct drm_connector *connector)
+{
+	struct drm_bridge_connector *bridge_connector = to_drm_bridge_connector(connector);
+
+	drm_connector_cleanup(connector);
+	drm_bridge_connector_put_bridges(bridge_connector);
+}
+
 static const struct drm_connector_funcs drm_bridge_connector_funcs = {
 	.fill_modes = drm_helper_probe_single_connector_modes,
 	.atomic_create_state = drm_bridge_connector_create_state,
@@ -275,6 +299,7 @@ static const struct drm_connector_funcs drm_bridge_connector_funcs = {
 	.debugfs_init = drm_bridge_connector_debugfs_init,
 	.oob_hotplug_event = drm_bridge_connector_oob_hotplug_event,
 	.color_format = drm_bridge_connector_color_format,
+	.destroy = drm_bridge_connector_dynconn_destroy,
 };
 
 /* -----------------------------------------------------------------------------
@@ -823,20 +848,6 @@ static const struct drm_connector_hdmi_cec_funcs drm_bridge_connector_hdmi_cec_f
  * Bridge Connector Initialisation
  */
 
-static void drm_bridge_connector_put_bridges(struct drm_device *dev, void *data)
-{
-	struct drm_bridge_connector *bridge_connector = (struct drm_bridge_connector *)data;
-
-	drm_bridge_put(bridge_connector->bridge_edid);
-	drm_bridge_put(bridge_connector->bridge_hpd);
-	drm_bridge_put(bridge_connector->bridge_detect);
-	drm_bridge_put(bridge_connector->bridge_modes);
-	drm_bridge_put(bridge_connector->bridge_hdmi);
-	drm_bridge_put(bridge_connector->bridge_hdmi_audio);
-	drm_bridge_put(bridge_connector->bridge_dp_audio);
-	drm_bridge_put(bridge_connector->bridge_hdmi_cec);
-}
-
 static struct drm_connector *
 drm_bridge_connector_initialize(struct drm_bridge_connector *bridge_connector,
 				struct drm_device *drm,
@@ -849,10 +860,6 @@ drm_bridge_connector_initialize(struct drm_bridge_connector *bridge_connector,
 	int connector_type;
 	int ret;
 
-	ret = drmm_add_action(drm, drm_bridge_connector_put_bridges, bridge_connector);
-	if (ret)
-		return ERR_PTR(ret);
-
 	bridge_connector->encoder = encoder;
 
 	/*
@@ -1053,17 +1060,21 @@ drm_bridge_connector_initialize(struct drm_bridge_connector *bridge_connector,
 				drm_bridge_connector_scrambler_disable;
 		}
 
-		ret = drmm_connector_hdmi_init(drm, connector,
-					       &drm_bridge_connector_funcs,
-					       &bridge_connector->hdmi_funcs,
-					       connector_type, ddc);
+		ret = drm_connector_hdmi_dynamic_init(drm, connector,
+						      &drm_bridge_connector_funcs,
+						      &bridge_connector->hdmi_funcs,
+						      connector_type, ddc);
 	} else {
-		ret = drmm_connector_init(drm, connector,
-					  &drm_bridge_connector_funcs,
-					  connector_type, ddc);
+		ret = drm_connector_dynamic_init(drm, connector,
+						 &drm_bridge_connector_funcs,
+						 connector_type, ddc);
+	}
+	if (ret) {
+		drm_bridge_connector_put_bridges(bridge_connector);
+		return ERR_PTR(ret);
 	}
-	if (ret)
-		return ret;
+
+	/* From now on the connector is referenced and has to be put */
 
 	if (bridge_connector->bridge_hdmi_audio ||
 	    bridge_connector->bridge_dp_audio) {
@@ -1084,35 +1095,48 @@ drm_bridge_connector_initialize(struct drm_bridge_connector *bridge_connector,
 						    bridge->hdmi_audio_spdif_playback,
 						    bridge->hdmi_audio_dai_port);
 		if (ret)
-			return ERR_PTR(ret);
+			goto err_put_connector;
 	}
 
 	if (bridge_connector->bridge_hdmi_cec &&
 	    bridge_connector->bridge_hdmi_cec->ops & DRM_BRIDGE_OP_HDMI_CEC_NOTIFIER) {
 		struct drm_bridge *bridge = bridge_connector->bridge_hdmi_cec;
 
-		ret = drmm_connector_hdmi_cec_notifier_register(connector,
-								NULL,
-								bridge->hdmi_cec_dev);
+		ret = drm_connector_hdmi_cec_notifier_register(connector,
+							       NULL,
+							       bridge->hdmi_cec_dev);
 		if (ret)
-			return ERR_PTR(ret);
+			goto err_put_connector;
+
+		bridge_connector->has_cec_notifier = true;
 	}
 
 	if (bridge_connector->bridge_hdmi_cec &&
 	    bridge_connector->bridge_hdmi_cec->ops & DRM_BRIDGE_OP_HDMI_CEC_ADAPTER) {
 		struct drm_bridge *bridge = bridge_connector->bridge_hdmi_cec;
 
-		ret = drmm_connector_hdmi_cec_register(connector,
-						       &drm_bridge_connector_hdmi_cec_funcs,
-						       bridge->hdmi_cec_adapter_name,
-						       bridge->hdmi_cec_available_las,
-						       bridge->hdmi_cec_dev);
+		ret = drm_connector_hdmi_cec_register(connector,
+						      &drm_bridge_connector_hdmi_cec_funcs,
+						      bridge->hdmi_cec_adapter_name,
+						      bridge->hdmi_cec_available_las,
+						      bridge->hdmi_cec_dev);
 		if (ret)
-			return ERR_PTR(ret);
+			goto err_unregister_cec_notifier;
+
+		bridge_connector->has_cec_adapter = true;
 	}
 
 	drm_connector_helper_add(connector, &drm_bridge_connector_helper_funcs);
 
+	if (!connector->state) {
+		struct drm_connector_state *state = drm_bridge_connector_create_state(connector);
+		if (IS_ERR(state)) {
+			ret = PTR_ERR(state);
+			goto err_unregister_cec_adapter;
+		}
+		connector->state = state;
+	}
+
 	if (bridge_connector->bridge_hpd)
 		connector->polled = DRM_CONNECTOR_POLL_HPD;
 	else if (bridge_connector->bridge_detect)
@@ -1128,9 +1152,51 @@ drm_bridge_connector_initialize(struct drm_bridge_connector *bridge_connector,
 
 	ret = drm_connector_attach_encoder(connector, encoder);
 	if (ret)
-		return ERR_PTR(ret);
+		goto err_unregister_cec_adapter;
+
+	ret = drm_connector_dynamic_register(connector);
+	if (ret)
+		goto err_unregister_cec_adapter;
 
 	return connector;
+
+err_unregister_cec_adapter:
+	if (bridge_connector->has_cec_adapter)
+		drm_connector_hdmi_cec_unregister(connector);
+err_unregister_cec_notifier:
+	if (bridge_connector->has_cec_notifier)
+		drm_connector_hdmi_cec_notifier_unregister(connector);
+err_put_connector:
+	drm_connector_put(connector);
+
+	return ERR_PTR(ret);
+}
+
+static void drm_bridge_connector_fini(struct drm_bridge_connector *bridge_connector)
+{
+	struct drm_connector *connector = &bridge_connector->base;
+
+	drm_connector_unregister(connector);
+	if (bridge_connector->has_cec_adapter)
+		drm_connector_hdmi_cec_unregister(connector);
+	if (bridge_connector->has_cec_notifier)
+		drm_connector_hdmi_cec_notifier_unregister(connector);
+
+#ifdef CONFIG_OF
+	if (connector->fwnode) {
+		fwnode_handle_put(connector->fwnode);
+		connector->fwnode = NULL;
+	}
+#endif
+
+	drm_connector_put(connector);
+}
+
+static void drmm_bridge_connector_fini(struct drm_device *dev, void *res)
+{
+	struct drm_bridge_connector *bridge_connector = (struct drm_bridge_connector *)res;
+
+	drm_bridge_connector_fini(bridge_connector);
 }
 
 /**
@@ -1155,6 +1221,7 @@ struct drm_connector *drm_bridge_connector_init(struct drm_device *drm,
 {
 	struct drm_bridge_connector *bridge_connector;
 	struct drm_connector *connector;
+	int ret;
 
 	bridge_connector = drmm_kzalloc(drm, sizeof(*bridge_connector), GFP_KERNEL);
 	if (!bridge_connector)
@@ -1164,6 +1231,10 @@ struct drm_connector *drm_bridge_connector_init(struct drm_device *drm,
 	if (IS_ERR(connector))
 		return connector;
 
+	ret = drmm_add_action_or_reset(drm, drmm_bridge_connector_fini, bridge_connector);
+	if (ret)
+		return ERR_PTR(ret);
+
 	return connector;
 }
 EXPORT_SYMBOL_GPL(drm_bridge_connector_init);
Signature · 1 line
2.56.1
#14 lore raw

Re: [PATCH v4 09/32] drm/display: bridge-connector: use a dynamic connector

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] drm/display: bridge-connector: double teardown in drmm_bridge_connector_fini() - [Medium]…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] drm/display: bridge-connector: double teardown in drmm_bridge_connector_fini()
- [Medium] drm/display: bridge-connector: reference leaks in drm_bridge_connector_initialize()
- [Low] drm/display: bridge-connector: missing colon in @has_cec_notifier kernel-doc
--

commit 17489de6cf9da07d5d4da90bca09f1c4add46b17
Author: Luca Ceresoli <[email protected]>

drm/display: bridge-connector: use a dynamic connector

This commit switches drm_bridge_connector initialization from drmm-managed connector and CEC helpers to dynamic connector initialization and explicit teardown in preparation for bridge hotplug.

drivers/gpu/drm/display/drm_bridge_connector.c struct drm_bridge_connector { · line 139

Quoted 13 lines
drivers/gpu/drm/display/drm_bridge_connector.c +4 -0
index f4a71801e8a3b..d81cfc7aa295b 100644
@@ -132,11 +132,27 @@ struct drm_bridge_connector {
 	 * bridge connector.
 	 */
 	struct drm_connector_hdmi_funcs hdmi_funcs;
+	/** @has_cec_adapter: true if a CEC adapter is registered */
+	bool has_cec_adapter;
+	/** @has_cec_notifier true if a CEC notifier is registered */
+	bool has_cec_notifier;
 };

[Severity: Low]
A colon is missing after @has_cec_notifier, which triggers a kernel-doc warning for struct drm_bridge_connector.

[ ... ]

Quoted 29 lines
drivers/gpu/drm/display/drm_bridge_connector.c +6 -11
@@ -849,10 +860,6 @@ drm_bridge_connector_initialize(struct drm_bridge_connector *bridge_connector,
 	int connector_type;
 	int ret;
 
-	ret = drmm_add_action(drm, drm_bridge_connector_put_bridges, bridge_connector);
-	if (ret)
-		return ERR_PTR(ret);
-
 	bridge_connector->encoder = encoder;
 
 	/*
@@ -1053,17 +1060,21 @@ drm_bridge_connector_initialize(struct drm_bridge_connector *bridge_connector,
 				drm_bridge_connector_scrambler_disable;
 		}
 
-		ret = drmm_connector_hdmi_init(drm, connector,
-					       &drm_bridge_connector_funcs,
-					       &bridge_connector->hdmi_funcs,
-					       connector_type, ddc);
+		ret = drm_connector_hdmi_dynamic_init(drm, connector,
+						      &drm_bridge_connector_funcs,
+						      &bridge_connector->hdmi_funcs,
+						      connector_type, ddc);
 	} else {
-		ret = drmm_connector_init(drm, connector,
-					  &drm_bridge_connector_funcs,
-					  connector_type, ddc);
+		ret = drm_connector_dynamic_init(drm, connector,
+						 &drm_bridge_connector_funcs,

drivers/gpu/drm/display/drm_bridge_connector.c drm_bridge_connector_initialize(struct drm_bridge_connector *bridge_connector, · line 1077

Quoted 10 lines
drivers/gpu/drm/display/drm_bridge_connector.c +7 -2
+						 connector_type, ddc);
+	}
+	if (ret) {
+		drm_bridge_connector_put_bridges(bridge_connector);
+		return ERR_PTR(ret);
 	}
-	if (ret)
-		return ret;
+
+	/* From now on the connector is referenced and has to be put */

[Severity: Medium]
Does removing the upfront drmm_add_action() leak bridge_connector->bridge_* and connector->fwnode references when drm_bridge_connector_initialize() fails earlier?

During drm_for_each_bridge_in_chain(), the loop acquires references with drm_bridge_get() and fwnode_handle_get(), but any early return inside the loop or at the connector_type == DRM_MODE_CONNECTOR_Unknown check returns directly without calling drm_bridge_connector_put_bridges() or fwnode_handle_put(connector->fwnode). In addition, if drm_connector_dynamic_init() fails (or drm_connector_hdmi_dynamic_init() fails before reaching drm_connector_cleanup()), the error block above puts the bridges but still leaks connector->fwnode.

[ ... ]

Quoted 37 lines
drivers/gpu/drm/display/drm_bridge_connector.c +26 -0
+static void drm_bridge_connector_fini(struct drm_bridge_connector *bridge_connector)
+{
+	struct drm_connector *connector = &bridge_connector->base;
+
+	drm_connector_unregister(connector);
+	if (bridge_connector->has_cec_adapter)
+		drm_connector_hdmi_cec_unregister(connector);
+	if (bridge_connector->has_cec_notifier)
+		drm_connector_hdmi_cec_notifier_unregister(connector);
+
+#ifdef CONFIG_OF
+	if (connector->fwnode) {
+		fwnode_handle_put(connector->fwnode);
+		connector->fwnode = NULL;
+	}
+#endif
+
+	drm_connector_put(connector);
+}
+
+static void drmm_bridge_connector_fini(struct drm_device *dev, void *res)
+{
+	struct drm_bridge_connector *bridge_connector = (struct drm_bridge_connector *)res;
+
+	drm_bridge_connector_fini(bridge_connector);
 }
 
 /**
@@ -1155,6 +1221,7 @@ struct drm_connector *drm_bridge_connector_init(struct drm_device *drm,
 {
 	struct drm_bridge_connector *bridge_connector;
 	struct drm_connector *connector;
+	int ret;
 
 	bridge_connector = drmm_kzalloc(drm, sizeof(*bridge_connector), GFP_KERNEL);
 	if (!bridge_connector)

drivers/gpu/drm/display/drm_bridge_connector.c struct drm_connector *drm_bridge_connector_init(struct drm_device *drm, · line 1239

Quoted 9 lines
drivers/gpu/drm/display/drm_bridge_connector.c +4 -0
@@ -1164,6 +1231,10 @@ struct drm_connector *drm_bridge_connector_init(struct drm_device *drm,
 	if (IS_ERR(connector))
 		return connector;
 
+	ret = drmm_add_action_or_reset(drm, drmm_bridge_connector_fini, bridge_connector);
+	if (ret)
+		return ERR_PTR(ret);
+
 	return connector;
 }

[Severity: High]
Can drmm_bridge_connector_fini() tear down a connector that has already been destroyed by drm_mode_config_cleanup()?

Drivers such as omapdrm, tegra, exynos, imx/dcss and kmb create connectors with drm_bridge_connector_init() and explicitly call drm_mode_config_cleanup() before drm_dev_put(). Because the connector is on dev->mode_config.connector_list with a single reference, drm_mode_config_cleanup() drops that reference and flushes connector_free_work, running drm_bridge_connector_dynconn_destroy() -> drm_connector_cleanup(), which destroys connector->mutex and zeroes *connector (including connector->cec.data). When drm_dev_put() then runs drmm_bridge_connector_fini(), drm_connector_unregister() locks the destroyed connector->mutex and drm_connector_hdmi_cec_unregister() dereferences NULL connector->cec.data; once commit "drm/display:
bridge-connector: add APIs to add/remove the connector dynamically" adds kfree(bridge_connector) to drm_bridge_connector_dynconn_destroy(), drmm_bridge_connector_fini() also becomes a use-after-free on bridge_connector.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
author reviewer
#15 lore raw

PATCH v4 10/32 drm/display: bridge-connector: add APIs to add/remove the connector dynamically

In reply to Luca Ceresoli

drm_bridge_connector_init() adds a drmm-allocated connector. For bridge hotplug drmm allocations are not suitable because a connector may have to be added and removed multiple times in the lifetime of a card.

In preparation for bridge hotplug, add APIs to add and remove a connector using regular non-managed allocations.

For the dynamic connector, this requires the kfree() the allocated struct drm_bridge_connector in the destroy func. However that func will be called even when using the pre-existing drmm API, leading to a double free (kfree() in the destroy callback + drmm).

One option to avoid this issue is introducing two mostly identical drm_connector_funcs instances, one with .destroy and one without. But that would be an annoying code duplication. Instead take a different approach:
always allocate using non-drmm kzalloc_obj(), so that deallocation always happen in destroy->kfree().

---

Uhm, maybe the change from drmm_kzalloc to kzalloc_obj and addition of kfree to the destroy callback should eb a separate commit?

---
 drivers/gpu/drm/display/drm_bridge_connector.c | 24 +++++++++++++++++++++++-
 include/drm/drm_bridge_connector.h             |  4 ++++
 2 files changed, 27 insertions(+), 1 deletion(-)
drivers/gpu/drm/display/drm_bridge_connector.c +23 -1
index d81cfc7aa295..07385f87b2e8 100644
@@ -289,6 +289,7 @@ static void drm_bridge_connector_dynconn_destroy(struct drm_connector *connector
 
 	drm_connector_cleanup(connector);
 	drm_bridge_connector_put_bridges(bridge_connector);
+	kfree(bridge_connector);
 }
 
 static const struct drm_connector_funcs drm_bridge_connector_funcs = {
@@ -1223,7 +1224,7 @@ struct drm_connector *drm_bridge_connector_init(struct drm_device *drm,
 	struct drm_connector *connector;
 	int ret;
 
-	bridge_connector = drmm_kzalloc(drm, sizeof(*bridge_connector), GFP_KERNEL);
+	bridge_connector = kzalloc_obj(*bridge_connector);
 	if (!bridge_connector)
 		return ERR_PTR(-ENOMEM);
 
@@ -1238,3 +1239,24 @@ struct drm_connector *drm_bridge_connector_init(struct drm_device *drm,
 	return connector;
 }
 EXPORT_SYMBOL_GPL(drm_bridge_connector_init);
+
+struct drm_connector *drm_bridge_connector_dynamic_add(struct drm_device *drm,
+						       struct drm_encoder *encoder)
+{
+	struct drm_bridge_connector *bridge_connector;
+
+	bridge_connector = kzalloc_obj(*bridge_connector);
+	if (!bridge_connector)
+		return ERR_PTR(-ENOMEM);
+
+	return drm_bridge_connector_initialize(bridge_connector, drm, encoder);
+}
+EXPORT_SYMBOL_GPL(drm_bridge_connector_dynamic_add);
+
+void drm_bridge_connector_dynamic_remove(struct drm_connector *connector)
+{
+	struct drm_bridge_connector *bridge_connector = to_drm_bridge_connector(connector);
+
+	drm_bridge_connector_fini(bridge_connector);
+}
+EXPORT_SYMBOL_GPL(drm_bridge_connector_dynamic_remove);
include/drm/drm_bridge_connector.h +4 -0
index 69630815fb09..de6ec91dbfab 100644
@@ -9,8 +9,12 @@
 struct drm_connector;
 struct drm_device;
 struct drm_encoder;
+struct drm_bridge_connector;
 
 struct drm_connector *drm_bridge_connector_init(struct drm_device *drm,
 						struct drm_encoder *encoder);
+struct drm_connector *drm_bridge_connector_dynamic_add(struct drm_device *drm,
+						       struct drm_encoder *encoder);
+void drm_bridge_connector_dynamic_remove(struct drm_connector *connector);
 
 #endif /* __DRM_BRIDGE_CONNECTOR_H__ */
Signature · 1 line
2.56.1
#16 lore raw

Re: [PATCH v4 10/32] drm/display: bridge-connector: add APIs to add/remove the connector dynamically

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [High] drm/display: bridge_connector UAF when mode_config cleanup precedes drmm - [Medium] drm/display…

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [High] drm/display: bridge_connector UAF when mode_config cleanup precedes drmm
- [Medium] drm/display: bridge_connector memory leak on early init failure
- [Medium] drm/display: bridge and fwnode ref leaks in drm_bridge_connector_initialize()
- [Low] drm/display: missing kernel-doc, unused forward decl, and duplicated init
--

commit a02e04d142ea4a8a0fe3098841fdb782ae21bfc9
Author: Luca Ceresoli <[email protected]>

drm/display: bridge-connector: add APIs to add/remove the connector dynamically

This commit switches drm_bridge_connector allocation from drmm_kzalloc() to kzalloc_obj() freed in the connector destroy callback, and adds drm_bridge_connector_dynamic_add() and drm_bridge_connector_dynamic_remove().

drivers/gpu/drm/display/drm_bridge_connector.c static void drm_bridge_connector_dynconn_destroy(struct drm_connector *connector · line 295

Quoted 12 lines
drivers/gpu/drm/display/drm_bridge_connector.c +1 -0
index d81cfc7aa295b..07385f87b2e8d 100644
@@ -289,6 +289,7 @@ static void drm_bridge_connector_dynconn_destroy(struct drm_connector *connector
 
 	drm_connector_cleanup(connector);
 	drm_bridge_connector_put_bridges(bridge_connector);
+	kfree(bridge_connector);
 }
 
 static const struct drm_connector_funcs drm_bridge_connector_funcs = {

[Severity: High]
Can freeing bridge_connector in drm_bridge_connector_dynconn_destroy() cause a use-after-free in drmm_bridge_connector_fini() when a driver using drm_bridge_connector_init() runs drm_mode_config_cleanup() before drm_dev_put()?

drm_bridge_connector_init() registers drmm_bridge_connector_fini() with a raw bridge_connector pointer without taking an extra connector reference. Drivers such as omapdrm (in omapdrm_cleanup()) and imx/dcss (in dcss_kms_detach()) call drm_mode_config_cleanup() before drm_dev_put(). drm_mode_config_cleanup() drops the connector's sole reference and flushes connector_free_work, invoking drm_bridge_connector_dynconn_destroy() and freeing bridge_connector. When drm_dev_put() later runs drmm actions, drmm_bridge_connector_fini() calls drm_bridge_connector_fini() on the freed bridge_connector, locking the destroyed connector->mutex and calling drm_connector_put() on freed memory.

Conversely, when drmm_mode_config_init() is used so that drmm_bridge_connector_fini() runs before drm_mode_config_cleanup(), if the connector is still bound to an active CRTC (refcount 2), drmm_bridge_connector_fini() only drops the refcount to 1. drm_mode_config_cleanup() then drops the remaining reference from 1 to 0, and drm_connector_cleanup() -> __drm_atomic_helper_connector_destroy_state() calls drm_connector_put(state->connector) on a connector whose refcount is already 0.

drivers/gpu/drm/display/drm_bridge_connector.c struct drm_connector *drm_bridge_connector_init(struct drm_device *drm, · line 1241

Quoted 13 lines
drivers/gpu/drm/display/drm_bridge_connector.c +1 -1
@@ -1223,7 +1224,7 @@ struct drm_connector *drm_bridge_connector_init(struct drm_device *drm,
 	struct drm_connector *connector;
 	int ret;
 
-	bridge_connector = drmm_kzalloc(drm, sizeof(*bridge_connector), GFP_KERNEL);
+	bridge_connector = kzalloc_obj(*bridge_connector);
 	if (!bridge_connector)
 		return ERR_PTR(-ENOMEM);
 
@@ -1238,3 +1239,24 @@ struct drm_connector *drm_bridge_connector_init(struct drm_device *drm,
 	return connector;
 }
 EXPORT_SYMBOL_GPL(drm_bridge_connector_init);

[Severity: Medium]
Does switching from drmm_kzalloc() to kzalloc_obj() leak bridge_connector in both drm_bridge_connector_init() and drm_bridge_connector_dynamic_add() when drm_bridge_connector_initialize() fails before connector initialization succeeds?

kfree(bridge_connector) is only called in drm_bridge_connector_dynconn_destroy() via drm_connector_put(). If drm_bridge_connector_initialize() returns an error during bridge chain inspection, when connector_type is DRM_MODE_CONNECTOR_Unknown, or when drm_connector_hdmi_dynamic_init() / drm_connector_dynamic_init() fails:

drivers/gpu/drm/display/drm_bridge_connector.c:drm_bridge_connector_initialize() {
	...
	if (ret) {
		drm_bridge_connector_put_bridges(bridge_connector);
		return ERR_PTR(ret);
	}
	...
}

drm_connector_put() is never called on those paths, so bridge_connector is never freed.

drivers/gpu/drm/display/drm_bridge_connector.c struct drm_connector *drm_bridge_connector_init(struct drm_device *drm, · line 1254

Quoted 13 lines
drivers/gpu/drm/display/drm_bridge_connector.c +13 -0
+
+struct drm_connector *drm_bridge_connector_dynamic_add(struct drm_device *drm,
+						       struct drm_encoder *encoder)
+{
+	struct drm_bridge_connector *bridge_connector;
+
+	bridge_connector = kzalloc_obj(*bridge_connector);
+	if (!bridge_connector)
+		return ERR_PTR(-ENOMEM);
+
+	return drm_bridge_connector_initialize(bridge_connector, drm, encoder);
+}
+EXPORT_SYMBOL_GPL(drm_bridge_connector_dynamic_add);

[Severity: Medium]
Does drm_bridge_connector_initialize() also leak bridge and fwnode references on its early error paths?

During the drm_for_each_bridge_in_chain() loop, drm_bridge_connector_initialize() takes references with drm_bridge_get() and fwnode_handle_get(), but returns ERR_PTR() directly on validation errors or when connector_type == DRM_MODE_CONNECTOR_Unknown without calling drm_bridge_connector_put_bridges() or fwnode_handle_put(connector->fwnode):

drivers/gpu/drm/display/drm_bridge_connector.c:drm_bridge_connector_initialize() {
		...
		if (bridge->ops & DRM_BRIDGE_OP_HDMI_CEC_ADAPTER) {
			if (bridge_connector->bridge_hdmi_cec)
				return ERR_PTR(-EBUSY);

			bridge_connector->bridge_hdmi_cec = drm_bridge_get(bridge);

			if (!bridge->funcs->hdmi_cec_enable ||
			    !bridge->funcs->hdmi_cec_log_addr ||
			    !bridge->funcs->hdmi_cec_transmit)
				return ERR_PTR(-EINVAL);
		}
		...
#ifdef CONFIG_OF
		if (drm_bridge_is_last(bridge) && bridge->of_node)
			connector->fwnode = fwnode_handle_get(of_fwnode_handle(bridge->of_node));
#endif
		...
	}

	if (connector_type == DRM_MODE_CONNECTOR_Unknown)
		return ERR_PTR(-EINVAL);
	...
}

In addition, if drm_connector_dynamic_init() or drm_connector_hdmi_dynamic_init() fails before drm_connector_cleanup() can run, the error block calls drm_bridge_connector_put_bridges() but still leaks connector->fwnode.

Quoted 7 lines
include/drm/drm_bridge_connector.h +0 -0
index 69630815fb093..de6ec91dbfabd 100644
@@ -9,8 +9,12 @@
 struct drm_connector;
 struct drm_device;

include/drm/drm_bridge_connector.h line 20

Quoted 10 lines
include/drm/drm_bridge_connector.h +4 -0
 struct drm_encoder;
+struct drm_bridge_connector;
 
 struct drm_connector *drm_bridge_connector_init(struct drm_device *drm,
 						struct drm_encoder *encoder);
+struct drm_connector *drm_bridge_connector_dynamic_add(struct drm_device *drm,
+						       struct drm_encoder *encoder);
+void drm_bridge_connector_dynamic_remove(struct drm_connector *connector);
 
 #endif /* __DRM_BRIDGE_CONNECTOR_H__ */

[Severity: Low]
This isn't a bug, but struct drm_bridge_connector is internal to drm_bridge_connector.c and unused in this header. Also, Documentation/gpu/drm-kms-helpers.rst pulls in drm_bridge_connector.c with the :export: directive, so the newly exported drm_bridge_connector_dynamic_add() and drm_bridge_connector_dynamic_remove() functions should have kernel-doc comments, and drm_bridge_connector_init() could call drm_bridge_connector_dynamic_add() (or move the allocation into drm_bridge_connector_initialize()) to avoid duplicating the allocation and initialization sequence.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
author reviewer
#17 lore raw

PATCH v4 11/32 drm/bridge: constify of_drm_find_and_get_bridge() device_node parameter

In reply to Luca Ceresoli

The @np argument does not need to be modified. Similarly to what of_drm_get_bridge_by_endpoint() already does, make it const. This allows passing a const pointer.

---

New patch in v3, needed for the v3 code of "drm: drm_hotplug_helper: new helper to implement bridge hotplug"

---
 drivers/gpu/drm/drm_bridge.c | 2 +-
 include/drm/drm_bridge.h     | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_bridge.c +1 -1
index 3cd6757dffb8..76242165292f 100644
@@ -1663,7 +1663,7 @@ EXPORT_SYMBOL_GPL(drm_bridge_hpd_notify);
  * RETURNS:
  * drm_bridge control struct on success, NULL on failure
  */
-struct drm_bridge *of_drm_find_and_get_bridge(struct device_node *np)
+struct drm_bridge *of_drm_find_and_get_bridge(const struct device_node *np)
 {
 	struct drm_bridge *bridge;
 
include/drm/drm_bridge.h +1 -1
index 12ca7f6fb524..d6ef020c4cc9 100644
@@ -1274,7 +1274,7 @@ int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
 		      enum drm_bridge_attach_flags flags);
 
 #ifdef CONFIG_OF
-struct drm_bridge *of_drm_find_and_get_bridge(struct device_node *np);
+struct drm_bridge *of_drm_find_and_get_bridge(const struct device_node *np);
 struct drm_bridge *of_drm_find_bridge(struct device_node *np);
 struct drm_bridge *of_drm_get_bridge_by_endpoint(const struct device_node *np,
 						 int port, int endpoint);
Signature · 1 line
2.56.1
#18 lore raw

Re: [PATCH v4 11/32] drm/bridge: constify of_drm_find_and_get_bridge() device_node parameter

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] drm/bridge: non-const np in !CONFIG_OF of_drm_find_and_get_bridge() stub --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] drm/bridge: non-const np in !CONFIG_OF of_drm_find_and_get_bridge() stub
--

commit 7679a18d386b4f07d4526f35667dbb513f3a913d
Author: Luca Ceresoli <[email protected]>

drm/bridge: constify of_drm_find_and_get_bridge() device_node parameter

This commit changes the device_node parameter of of_drm_find_and_get_bridge() to const so callers can pass a const pointer.

include/drm/drm_bridge.h int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge, · line 1280

Quoted 13 lines
include/drm/drm_bridge.h +1 -1
index 12ca7f6fb5243..d6ef020c4cc9b 100644
@@ -1274,7 +1274,7 @@ int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
 		      enum drm_bridge_attach_flags flags);
 
 #ifdef CONFIG_OF
-struct drm_bridge *of_drm_find_and_get_bridge(struct device_node *np);
+struct drm_bridge *of_drm_find_and_get_bridge(const struct device_node *np);
 struct drm_bridge *of_drm_find_bridge(struct device_node *np);
 struct drm_bridge *of_drm_get_bridge_by_endpoint(const struct device_node *np,
 						 int port, int endpoint);

[Severity: Low]
Should the !CONFIG_OF stub of of_drm_find_and_get_bridge() in the #else branch right below also take a const struct device_node *np?

include/drm/drm_bridge.h:

    #else
    static inline struct drm_bridge *of_drm_find_and_get_bridge(struct device_node *np)
    {
    	return NULL;
    }

Without const on the stub, passing a const struct device_node * in a !CONFIG_OF build fails to compile with a discarded-qualifiers error.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
author reviewer
#19 lore raw

PATCH v4 12/32 drm/bridge: initialize chain_node list head on allocation

In reply to Luca Ceresoli

In preparation to add a check to detect whether a bridge is not yet attached, ensure the chain_node list_head is always empty [as in list_empty()] since it is allocated, until it is attached.

---
 drivers/gpu/drm/drm_bridge.c | 1 +
 1 file changed, 1 insertion(+)
drivers/gpu/drm/drm_bridge.c +1 -0
index 76242165292f..14ed4ac52e71 100644
@@ -419,6 +419,7 @@ void *__devm_drm_bridge_alloc(struct device *dev, size_t size, size_t offset,
 		return ERR_PTR(-ENOMEM);
 
 	bridge = container + offset;
+	INIT_LIST_HEAD(&bridge->chain_node);
 	INIT_LIST_HEAD(&bridge->list);
 	bridge->container = container;
 	bridge->funcs = funcs;
Signature · 1 line
2.56.1
author reviewer
#20 lore raw

PATCH v4 13/32 drm/bridge: initialize chain_node list head on detach and attach errors

In reply to Luca Ceresoli

When a bridge is detached it is removed from the encoder bridge_chain list, but the bridge::chain_node list head is not cleared. This is going to be problematic with the upcoming hotplug bridge support because if a bridge is detached from the encoder chain but not yet removed, when later detaching it the encoder code may think it is still attached, thus trying to detach it twice.

Avoid this by clearing the list head on detach, so there's a clear and simple way to know when a bridge is not attached anymore.

Do the same in the error management code in drm_bridge_attach(), so that chain_node is always empty [as in list_empty()] when it is not (yet|anymore) in the bridge chain.

---
 drivers/gpu/drm/drm_bridge.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_bridge.c +2 -2
index 14ed4ac52e71..310f03bdb744 100644
@@ -679,7 +679,7 @@ int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
 	bridge->dev = NULL;
 	bridge->encoder = NULL;
 	mutex_lock(&encoder->bridge_chain_mutex);
-	list_del(&bridge->chain_node);
+	list_del_init(&bridge->chain_node);
 	mutex_unlock(&encoder->bridge_chain_mutex);
 
 	if (ret != -EPROBE_DEFER)
@@ -709,7 +709,7 @@ void drm_bridge_detach(struct drm_bridge *bridge)
 	if (bridge->funcs->detach)
 		bridge->funcs->detach(bridge);
 
-	list_del(&bridge->chain_node);
+	list_del_init(&bridge->chain_node);
 	bridge->dev = NULL;
 	drm_bridge_put(bridge);
 }
Signature · 1 line
2.56.1
author reviewer
#21 lore raw

PATCH v4 14/32 drm/bridge: ti-sn65dsi86: protect device resources on unplug

In reply to Luca Ceresoli

To support hot-unplug of this bridge we need to protect access to device resources in case device removal happens concurrently to other code.

Some care is needed for the case when drm_bridge_unplug() happens before ti_sn_bridge_atomic_post_disable(), so the latter cannot release supplier resources taken by ti_sn_bridge_atomic_pre_enable(): runtime PM (and in turn regulators) and refclk.

To ensure those resources are released exactly once on device removal:

* move the code to release them to a dedicated function
* register that function when the resources are taken in ti_sn_bridge_atomic_pre_enable()
* if ti_sn_bridge_atomic_post_disable() happens before drm_bridge_unplug() (typical non-hot-unplug case):
* ti_sn_bridge_atomic_post_disable() can enter the critical section

     (drm_bridge_enter() returns 0) -> it releases and executes the
     devres action

* if ti_sn_bridge_atomic_post_disable() happens after drm_bridge_unplug() (typical hot-unplug case):
* ti_sn_bridge_remove() -> drm_bridge_unplug() defuses ti_sn_bridge_atomic_post_disable() so it cannot execute the devres action
* the devres action is executed just after ti_sn_bridge_remove()

---

Changes in v4:
- protect from register access in ti_sn_bridge_detect() and ti_sn_bridge_edid_read() too

New patch in v3

This commit basically replicates the same change done on the ti-sn65dsi83 driver, now upstream:
https://lore.kernel.org/all/20251112-drm-bridge-atomic-vs-remove-v3-2-85db717ce094@bootlin.com/

---
 drivers/gpu/drm/bridge/ti-sn65dsi86.c | 74 +++++++++++++++++++++++++++++++----
 1 file changed, 67 insertions(+), 7 deletions(-)
drivers/gpu/drm/bridge/ti-sn65dsi86.c +67 -7
index 48b83df9aed6..38318113a534 100644
@@ -827,9 +827,15 @@ static void ti_sn_bridge_atomic_disable(struct drm_bridge *bridge,
 					struct drm_atomic_commit *state)
 {
 	struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);
+	int idx;
+
+	if (!drm_bridge_enter(bridge, &idx))
+		return;
 
 	/* disable video stream */
 	regmap_update_bits(pdata->regmap, SN_ENH_FRAME_REG, VSTREAM_ENABLE, 0);
+
+	drm_bridge_exit(idx);
 }
 
 static void ti_sn_bridge_set_dsi_rate(struct ti_sn65dsi86 *pdata,
@@ -1099,11 +1105,16 @@ static void ti_sn_bridge_atomic_enable(struct drm_bridge *bridge,
 	int ret = -EINVAL;
 	int max_dp_lanes;
 	unsigned int bpp;
+	int idx;
+
+	if (!drm_bridge_enter(bridge, &idx))
+		return;
 
 	connector = drm_atomic_get_new_connector_for_encoder(state,
 							     bridge->encoder);
 	if (!connector) {
 		dev_err_ratelimited(pdata->dev, "Could not get the connector\n");
+		drm_bridge_exit(idx);
 		return;
 	}
 
@@ -1167,6 +1178,7 @@ static void ti_sn_bridge_atomic_enable(struct drm_bridge *bridge,
 	}
 	if (ret) {
 		DRM_DEV_ERROR(pdata->dev, "%s (%d)\n", last_err_str, ret);
+		drm_bridge_exit(idx);
 		return;
 	}
 
@@ -1176,26 +1188,57 @@ static void ti_sn_bridge_atomic_enable(struct drm_bridge *bridge,
 	/* enable video stream */
 	regmap_update_bits(pdata->regmap, SN_ENH_FRAME_REG, VSTREAM_ENABLE,
 			   VSTREAM_ENABLE);
+
+	drm_bridge_exit(idx);
+}
+
+/*
+ * Release resources taken by ti_sn_bridge_atomic_pre_enable().
+ *
+ * Invoked by ti_sn_bridge_atomic_post_disable() normally, or by devres
+ * after ti_sn_bridge_remove() in case this happens befora
+ * atomic_post_disable.
+ */
+static void ti_sn_bridge_release_resources(void *data)
+{
+	struct ti_sn65dsi86 *pdata = (struct ti_sn65dsi86 *)data;
+
+	if (!pdata->refclk)
+		ti_sn65dsi86_disable_comms(pdata);
+
+	pm_runtime_put_sync(pdata->dev);
 }
 
 static void ti_sn_bridge_atomic_pre_enable(struct drm_bridge *bridge,
 					   struct drm_atomic_commit *state)
 {
 	struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);
+	int idx;
+
+	if (!drm_bridge_enter(bridge, &idx))
+		return;
 
 	pm_runtime_get_sync(pdata->dev);
 
 	if (!pdata->refclk)
 		ti_sn65dsi86_enable_comms(pdata, state);
 
+	devm_add_action(pdata->dev, ti_sn_bridge_release_resources, pdata);
+
 	/* td7: min 100 us after enable before DSI data */
 	usleep_range(100, 110);
+
+	drm_bridge_exit(idx);
 }
 
 static void ti_sn_bridge_atomic_post_disable(struct drm_bridge *bridge,
 					     struct drm_atomic_commit *state)
 {
 	struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);
+	int idx;
+
+	if (!drm_bridge_enter(bridge, &idx))
+		return;
 
 	/* semi auto link training mode OFF */
 	regmap_write(pdata->regmap, SN_ML_TX_MODE_REG, 0);
@@ -1204,17 +1247,21 @@ static void ti_sn_bridge_atomic_post_disable(struct drm_bridge *bridge,
 	/* disable DP PLL */
 	regmap_write(pdata->regmap, SN_PLL_ENABLE_REG, 0);
 
-	if (!pdata->refclk)
-		ti_sn65dsi86_disable_comms(pdata);
+	devm_release_action(pdata->dev, ti_sn_bridge_release_resources, pdata);
 
-	pm_runtime_put_sync(pdata->dev);
+	drm_bridge_exit(idx);
 }
 
 static enum drm_connector_status
 ti_sn_bridge_detect(struct drm_bridge *bridge, struct drm_connector *connector)
 {
 	struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);
+	enum drm_connector_status status = connector_status_disconnected;
 	int val = 0;
+	int idx;
+
+	if (!drm_bridge_enter(bridge, &idx))
+		return status;
 
 	/*
 	 * Runtime reference is grabbed in ti_sn_bridge_hpd_enable()
@@ -1225,16 +1272,29 @@ ti_sn_bridge_detect(struct drm_bridge *bridge, struct drm_connector *connector)
 
 	regmap_read(pdata->regmap, SN_HPD_DISABLE_REG, &val);
 
-	return val & HPD_DEBOUNCED_STATE ? connector_status_connected
-					 : connector_status_disconnected;
+	status = val & HPD_DEBOUNCED_STATE ? connector_status_connected
+					   : connector_status_disconnected;
+
+	drm_bridge_exit(idx);
+
+	return status;
 }
 
 static const struct drm_edid *ti_sn_bridge_edid_read(struct drm_bridge *bridge,
 						     struct drm_connector *connector)
 {
 	struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);
+	const struct drm_edid *edid;
+	int idx;
 
-	return drm_edid_read_ddc(connector, &pdata->aux.ddc);
+	if (!drm_bridge_enter(bridge, &idx))
+		return NULL;
+
+	edid = drm_edid_read_ddc(connector, &pdata->aux.ddc);
+
+	drm_bridge_exit(idx);
+
+	return edid;
 }
 
 static void ti_sn65dsi86_debugfs_init(struct drm_bridge *bridge, struct dentry *root)
@@ -1475,7 +1535,7 @@ static void ti_sn_bridge_remove(struct auxiliary_device *adev)
 	if (!pdata)
 		return;
 
-	drm_bridge_remove(&pdata->bridge);
+	drm_bridge_unplug(&pdata->bridge);
 
 	of_node_put(pdata->host_node);
 }
Signature · 1 line
2.56.1
#22 lore raw

Re: [PATCH v4 14/32] drm/bridge: ti-sn65dsi86: protect device resources on unplug

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] drm/bridge: ti-sn65dsi86: release action registered on wrong device - [High] drm/bridge…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] drm/bridge: ti-sn65dsi86: release action registered on wrong device
- [High] drm/bridge: ti-sn65dsi86: unprotected HPD and debugfs callbacks on unplug
- [Medium] drm/bridge: unchecked devm_add_action() in ti_sn_bridge_atomic_pre_enable()
--

commit c57970c3929d58f60b46d5db47153169c16eb129
Author: Luca Ceresoli <[email protected]>

drm/bridge: ti-sn65dsi86: protect device resources on unplug

This commit wraps hardware accesses in bridge callbacks with drm_bridge_enter() and drm_bridge_exit(), uses a devres action to release resources acquired in ti_sn_bridge_atomic_pre_enable(), and switches ti_sn_bridge_remove() to drm_bridge_unplug().

Quoted 4 lines
drivers/gpu/drm/bridge/ti-sn65dsi86.c +0 -0
index 48b83df9aed61..38318113a534f 100644

[ ... ]

Quoted 1 line
drivers/gpu/drm/bridge/ti-sn65dsi86.c +0 -0
@@ -1176,26 +1188,57 @@ static void ti_sn_bridge_atomic_enable(struct drm_bridge *bridge,

[ ... ]

Quoted 15 lines
drivers/gpu/drm/bridge/ti-sn65dsi86.c +10 -0
+static void ti_sn_bridge_release_resources(void *data)
+{
+	struct ti_sn65dsi86 *pdata = (struct ti_sn65dsi86 *)data;
+
+	if (!pdata->refclk)
+		ti_sn65dsi86_disable_comms(pdata);
+
+	pm_runtime_put_sync(pdata->dev);
+}
 
 static void ti_sn_bridge_atomic_pre_enable(struct drm_bridge *bridge,
 					   struct drm_atomic_commit *state)
 {
 	struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);
+	int idx;

drivers/gpu/drm/bridge/ti-sn65dsi86.c static void ti_sn_bridge_atomic_enable(struct drm_bridge *bridge, · line 1226

Quoted 10 lines
drivers/gpu/drm/bridge/ti-sn65dsi86.c +4 -0
+
+	if (!drm_bridge_enter(bridge, &idx))
+		return;
 
 	pm_runtime_get_sync(pdata->dev);
 
 	if (!pdata->refclk)
 		ti_sn65dsi86_enable_comms(pdata, state);
 
+	devm_add_action(pdata->dev, ti_sn_bridge_release_resources, pdata);

[Severity: Medium]
Does an allocation failure in devm_add_action() leak the runtime PM reference and comms state?

If devm_add_action() fails with -ENOMEM, ti_sn_bridge_release_resources() is never queued, so a later ti_sn_bridge_atomic_post_disable() hits WARN_ON(IS_ERR(dr)) in devm_release_action() and skips releasing the resources. Using devm_add_action_or_reset() would run ti_sn_bridge_release_resources() immediately on failure.

Quoted 19 lines
drivers/gpu/drm/bridge/ti-sn65dsi86.c +7 -0
+
 	/* td7: min 100 us after enable before DSI data */
 	usleep_range(100, 110);
+
+	drm_bridge_exit(idx);
 }
 
 static void ti_sn_bridge_atomic_post_disable(struct drm_bridge *bridge,
 					     struct drm_atomic_commit *state)
 {
 	struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);
+	int idx;
+
+	if (!drm_bridge_enter(bridge, &idx))
+		return;
 
 	/* semi auto link training mode OFF */
 	regmap_write(pdata->regmap, SN_ML_TX_MODE_REG, 0);

drivers/gpu/drm/bridge/ti-sn65dsi86.c static void ti_sn_bridge_atomic_post_disable(struct drm_bridge *bridge, · line 1250

Quoted 6 lines
drivers/gpu/drm/bridge/ti-sn65dsi86.c +1 -2
@@ -1204,17 +1247,21 @@ static void ti_sn_bridge_atomic_post_disable(struct drm_bridge *bridge,
 	/* disable DP PLL */
 	regmap_write(pdata->regmap, SN_PLL_ENABLE_REG, 0);
 
-	if (!pdata->refclk)
-		ti_sn65dsi86_disable_comms(pdata);
+	devm_release_action(pdata->dev, ti_sn_bridge_release_resources, pdata);

[Severity: High]
Should ti_sn_bridge_release_resources() be registered on &pdata->bridge_aux->dev instead of pdata->dev?

During probe, ti_sn_aux_probe() creates pdata->bridge_aux via ti_sn65dsi86_add_aux_device() -> __devm_auxiliary_device_create(), which queues auxiliary_device_destroy() on pdata->dev. When ti_sn_bridge_atomic_pre_enable() later registers ti_sn_bridge_release_resources() on pdata->dev at modeset time, that action is queued after auxiliary_device_destroy().

Because devres_release_all(pdata->dev) runs actions in reverse order on parent I2C device unbind, ti_sn_bridge_release_resources() drops the runtime PM reference and powers down the bridge before auxiliary_device_destroy() invokes ti_sn_bridge_remove() -> drm_bridge_unplug(&pdata->bridge). Bridge callbacks can then still enter drm_bridge_enter() while the chip is powered off, and a concurrent ti_sn_bridge_atomic_post_disable() hits WARN_ON(IS_ERR(dr)) in devm_release_action() once devres_release_all(pdata->dev) has detached the action list.

Conversely, if the bridge auxiliary device is unbound directly (for example during ti_sn65dsi86_exit(), which unregisters ti_sn_bridge_driver before the I2C driver), ti_sn_bridge_remove() calls drm_bridge_unplug() while ti_sn_bridge_release_resources() stays attached to pdata->dev, preventing a subsequent ti_sn_bridge_atomic_post_disable() from releasing the runtime PM reference.

[ ... ]

drivers/gpu/drm/bridge/ti-sn65dsi86.c static void ti_sn_bridge_remove(struct auxiliary_device *adev) · line 1538

Quoted 6 lines
drivers/gpu/drm/bridge/ti-sn65dsi86.c +1 -1
@@ -1475,7 +1535,7 @@ static void ti_sn_bridge_remove(struct auxiliary_device *adev)
 	if (!pdata)
 		return;
 
-	drm_bridge_remove(&pdata->bridge);
+	drm_bridge_unplug(&pdata->bridge);

[Severity: High]
Are ti_sn_bridge_hpd_enable(), ti_sn_bridge_hpd_disable() and status_show() still exposed to device resource accesses or runtime PM leaks on unplug?

In DisplayPort mode with an IRQ, ti_sn_bridge_hpd_enable() takes a runtime PM reference with pm_runtime_get_sync(pdata->dev) and accesses pdata->regmap until ti_sn_bridge_hpd_disable() runs:

drivers/gpu/drm/bridge/ti-sn65dsi86.c:ti_sn_bridge_hpd_enable() {
	...
	pm_runtime_get_sync(pdata->dev);
	...
	if (client->irq) {
		ret = regmap_set_bits(pdata->regmap, SN_IRQ_EVENTS_EN_REG,
				      HPD_REMOVAL_EN | HPD_INSERTION_EN);
	...
}

Neither HPD callback checks drm_bridge_enter(), and ti_sn_bridge_hpd_enable() does not register a devres cleanup action for its runtime PM reference. If the bridge is unplugged while HPD is enabled, the runtime PM reference is leaked across device unbind (keeping regulators and refclk enabled), or a subsequent HPD disable accesses pdata->regmap after drm_bridge_unplug() completes.

Similarly, status_show() in connector debugfs calls pm_runtime_get_sync(pdata->dev) and regmap_read(pdata->regmap, ...) without drm_bridge_enter() and drm_bridge_exit() protection.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
author reviewer
#23 lore raw

PATCH v4 15/32 drm/atomic: drm_atomic_private_obj_fini: protect private_obj removal from list

In reply to Luca Ceresoli

Currently drm_bridge_detach() expects that the bridge private_obj is not locked by a drm_modeset_acquire_ctx, and it warns in case that happens:

drm_bridge_detach()
-> drm_atomic_private_obj_fini()

     -> list_del(&obj->head) // removes priv_obj from
                             // dev->mode_config.privobj_list
     -> obj->funcs->atomic_destroy_state()
     -> drm_modeset_lock_fini(&obj->lock)
        -> WARN_ON(!list_empty(&lock->head)) // warn if priv_obj->lock
	                                     // is still in ctx->locked

The expectation is not respected when introducing bridge hot-plugging. In such case the warning triggers if the bridge is being removed concurrently to an operation that locks the private object using a drm_modeset_acquire_ctx, such as in this execution scenario:

CPU0:
drm_mode_obj_get_properties_ioctl() // userspace request -> DRM_MODESET_LOCK_ALL_BEGIN()
. -> drm_for_each_privobj() // loop on dev->mode_config.privobj_list
. - lock the privobj mutex
. - add priv_obj->lock to ctx->locked
. (list of locks to be released later)
.
. CPU1:
. drm_bridge_detach() // bridge hot-unplug
. -> WARN triggers!
.
-> DRM_MODESET_LOCK_ALL_END()
-> for each lock in ctx->locked
- remove priv_obj->lock from ctx->locked
- unlock the privobj mutex

Fix this by using DRM_MODESET_LOCK_ALL_BEGIN/END() around the list removal in drm_atomic_private_obj_fini() to ensure the two threads above (CPU0/CPU1) cannot happen concurrently.

---

Changes in v4:
- fixed conflicts after commit 6386ba453468 ("drm/atomic: Fix private obj error handling")

Notes:
* This patch was sent separately in the past [0]
* The issue is still happening with hotplug without this patch
* The issue has also been reported by sashiko when reviewing v2 [1]

[0] https://lore.kernel.org/dri-devel/20260324-drm-bridge-atomic-vs-remove-private_obj-v3-1-64deefe84044@bootlin.com/
[1] https://lore.kernel.org/all/[email protected]/

---
 drivers/gpu/drm/drm_atomic.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/gpu/drm/drm_atomic.c +6 -0
index 1c3fe07b327e..096f85419b36 100644
@@ -1106,6 +1106,9 @@ EXPORT_SYMBOL(drm_atomic_private_obj_init);
 void
 drm_atomic_private_obj_fini(struct drm_private_obj *obj)
 {
+	struct drm_modeset_acquire_ctx ctx;
+	int ret = 0;
+
 	/*
 	 * Some callers ignore drm_atomic_private_obj_init() failures
 	 * and unconditionally register a teardown action that calls
@@ -1117,7 +1120,10 @@ drm_atomic_private_obj_fini(struct drm_private_obj *obj)
 	if (list_empty(&obj->head))
 		return;
 
+	DRM_MODESET_LOCK_ALL_BEGIN(obj->dev, ctx, 0, ret);
 	list_del_init(&obj->head);
+	DRM_MODESET_LOCK_ALL_END(obj->dev, ctx, ret);
+
 	obj->funcs->atomic_destroy_state(obj, obj->state);
 	drm_modeset_lock_fini(&obj->lock);
 }
Signature · 1 line
2.56.1
#24 lore raw

Re: [PATCH v4 15/32] drm/atomic: drm_atomic_private_obj_fini: protect private_obj removal from list

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] drm/atomic: race on obj->lock after unlock in drm_atomic_private_obj_fini() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/atomic: race on obj->lock after unlock in drm_atomic_private_obj_fini()
--

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

drm/atomic: drm_atomic_private_obj_fini: protect private_obj removal from list

This commit wraps the list_del_init(&obj->head) call in drm_atomic_private_obj_fini() with DRM_MODESET_LOCK_ALL_BEGIN() and DRM_MODESET_LOCK_ALL_END() to serialize private object removal against concurrent modeset locking.

Quoted 15 lines
drivers/gpu/drm/drm_atomic.c +3 -0
index 1c3fe07b327ec..096f85419b369 100644
@@ -1106,6 +1106,9 @@ EXPORT_SYMBOL(drm_atomic_private_obj_init);
 void
 drm_atomic_private_obj_fini(struct drm_private_obj *obj)
 {
+	struct drm_modeset_acquire_ctx ctx;
+	int ret = 0;
+
 	/*
 	 * Some callers ignore drm_atomic_private_obj_init() failures
 	 * and unconditionally register a teardown action that calls

drivers/gpu/drm/drm_atomic.c drm_atomic_private_obj_fini(struct drm_private_obj *obj) · line 1129

Quoted 10 lines
drivers/gpu/drm/drm_atomic.c +3 -0
@@ -1117,7 +1120,10 @@ drm_atomic_private_obj_fini(struct drm_private_obj *obj)
 	if (list_empty(&obj->head))
 		return;
 
+	DRM_MODESET_LOCK_ALL_BEGIN(obj->dev, ctx, 0, ret);
 	list_del_init(&obj->head);
+	DRM_MODESET_LOCK_ALL_END(obj->dev, ctx, ret);
+
 	obj->funcs->atomic_destroy_state(obj, obj->state);
 	drm_modeset_lock_fini(&obj->lock);
 }

[Severity: High]
Can a thread that already observed obj before list_del_init(&obj->head) acquire obj->lock as soon as DRM_MODESET_LOCK_ALL_END() drops it, racing with atomic_destroy_state() and drm_modeset_lock_fini()?

For example, if drm_modeset_lock_all_ctx() hits -EDEADLK on obj->lock (because another atomic operation holds obj->lock), modeset_lock() saves &obj->lock in ctx->contended and drm_modeset_backoff() drops all held locks, including connection_mutex, before slow-locking contended:

CPU0 CPU1
drm_mode_obj_get_properties_ioctl()
DRM_MODESET_LOCK_ALL_BEGIN()

    drm_modeset_lock_all_ctx()
      drm_modeset_lock(&obj->lock)
        ctx->contended = &obj->lock
        returns -EDEADLK
  DRM_MODESET_LOCK_ALL_END()
    drm_modeset_backoff()
      contended = ctx->contended
      drm_modeset_drop_locks()
                                      drm_bridge_detach()
                                        drm_atomic_private_obj_fini()
                                          DRM_MODESET_LOCK_ALL_BEGIN()
                                          list_del_init(&obj->head)
                                          DRM_MODESET_LOCK_ALL_END()
      modeset_lock(contended, ...)
        list_add(&obj->lock.head,
                 &ctx->locked)
                                          atomic_destroy_state()
                                          drm_modeset_lock_fini(&obj->lock)
                                            WARN_ON(!list_empty(&lock->head))
                                        ...
                                        drm_bridge_put() -> kfree(bridge)
    drm_modeset_drop_locks()
      /* touches freed obj->lock */  <-- use-after-free

The same race occurs if another thread blocks directly on obj->lock without holding connection_mutex, such as drm_atomic_get_bridge_state() while bridge->chain_node is still on encoder->bridge_chain (since drm_bridge_detach() unlinks chain_node after drm_atomic_private_obj_fini()), or __drm_state_dump(). When DRM_MODESET_LOCK_ALL_END() unlocks obj->lock, the waiter wakes up and duplicates or reads obj->state while atomic_destroy_state() frees it.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
author reviewer
#25 lore raw

PATCH v4 16/32 drm/probe-helper: add per-connector polling enable/disable

In reply to Luca Ceresoli

With the upcoming DRM bridge hotplug support, drm_connectors will be added and removed at any time after initial card probing.

This means the existing calls to drm_kms_helper_poll_init() at probe time will not enable HPD on connectors added later on (and potentially not start the polling work), so late connectors cannot perform HPD polling.

Similarly, drm_kms_helper_poll_fini() cannot disable HPD for hot-unplugged connectors without disabling it for other connectors.

Add drm_kms_helper_poll_enable_connector() and drm_kms_helper_poll_disable_connector() to enable or disable HPD for a single connector, and also arm the work when an enabled connector requests polling and stop on disable it unless other connectors still need it.

Polling must still be initialized once per device with drm_kms_helper_poll_init() and finally torn down with drm_kms_helper_poll_fini().

---

New patch in v4

---
 drivers/gpu/drm/drm_probe_helper.c | 92 ++++++++++++++++++++++++++++++++++++++
 include/drm/drm_probe_helper.h     |  2 +
 2 files changed, 94 insertions(+)
drivers/gpu/drm/drm_probe_helper.c +92 -0
index 856b40fdaac4..e468971ab642 100644
@@ -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;
@@ -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);
+
+	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);
+
 /**
  * drm_kms_helper_poll_init - initialize and enable output polling
  * @dev: drm_device
include/drm/drm_probe_helper.h +2 -0
index 840ae5f798c2..ae58c1b078fe 100644
@@ -26,7 +26,9 @@ void drm_kms_helper_hotplug_event(struct drm_device *dev);
 void drm_kms_helper_connector_hotplug_event(struct drm_connector *connector);
 
 void drm_kms_helper_poll_disable(struct drm_device *dev);
+void drm_kms_helper_poll_disable_connector(struct drm_connector *connector);
 void drm_kms_helper_poll_enable(struct drm_device *dev);
+void drm_kms_helper_poll_enable_connector(struct drm_connector *connector);
 void drm_kms_helper_poll_reschedule(struct drm_device *dev);
 bool drm_kms_helper_is_poll_worker(void);
 
Signature · 1 line
2.56.1
#26 lore raw

Re: [PATCH v4 16/32] drm/probe-helper: add per-connector polling enable/disable

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…

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.

Quoted 26 lines
drivers/gpu/drm/drm_probe_helper.c +11 -0
index 856b40fdaac4a..e468971ab6429 100644
@@ -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;

drivers/gpu/drm/drm_probe_helper.c static void drm_kms_helper_disable_hpd(struct drm_device *dev) · line 263

Quoted 10 lines
drivers/gpu/drm/drm_probe_helper.c +7 -0
+
+	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().

Quoted 66 lines
drivers/gpu/drm/drm_probe_helper.c +55 -0
@@ -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

drivers/gpu/drm/drm_probe_helper.c void drm_kms_helper_poll_disable(struct drm_device *dev) · line 1012

Quoted 10 lines
drivers/gpu/drm/drm_probe_helper.c +10 -0
+ * 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().

drivers/gpu/drm/drm_probe_helper.c void drm_kms_helper_poll_disable(struct drm_device *dev) · line 1020

Quoted 8 lines
drivers/gpu/drm/drm_probe_helper.c +8 -0
+
+	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.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
author reviewer
#27 lore raw

PATCH v4 17/32 drm: event-notifier: add mechanism to notify about hotplug events

In reply to Luca Ceresoli

In preparation for supporting DRM bridge hotplug, add an event notifier to allow interested parties to be notified about events they need to react to.

For the initial implementation of bridge hotplug, a few events are needed:

* A bridge was added (happening in drm_bridge.c)
* A bridge was detached (happening in drm_bridge.c)
* A MIPI device was attached to a MIPI host (happening in drm_mipi_dsi.c)

Being originated from different modules, implement the event notifier mechanism in a new module with minimal dependencies that event producers can easily use to send events.

---

Changes in v3:
- Renamed to clarify the scope is only about hotplug
- Extend enum drm_event_notifier_event documentation
- Add DRM_BRIDGE_ADDED event to support non-DSI (and some DSI too) bridges
- Reword event description in a more concise way
- Make DRM_HOTPLUG_EVENT_NOTIFIER a bool, not a tristate: DRM_MIPI_DSI cannot be made a tristate with current code, and it depends on DRM_HOTPLUG_EVENT_NOTIFIER
- Use EXPORT_SYMBOL_GPL instead of EXPORT_SYMBOL to avoid GPL-bypass of the notifier API

Changes in v2:
- added missing include

---
 MAINTAINERS                                  |  8 ++++
 drivers/gpu/drm/Kconfig                      |  3 ++
 drivers/gpu/drm/Makefile                     |  2 +
 drivers/gpu/drm/drm_hotplug_event_notifier.c | 62 ++++++++++++++++++++++++++++
 include/drm/drm_hotplug_event_notifier.h     | 52 +++++++++++++++++++++++
 5 files changed, 127 insertions(+)
MAINTAINERS +8 -0
index 0afa391498d0..a5e2bf15e59f 100644
@@ -9320,6 +9320,14 @@ F:	include/drm/drm_gpusvm.h
 F:	include/drm/drm_pagemap.h
 F:	include/drm/drm_pagemap_util.h
 
+DRM HOTPLUG EVENT NOTIFIER
+M:	Luca Ceresoli <[email protected]>
+L:	[email protected]
+S:	Maintained
+T:	git https://gitlab.freedesktop.org/drm/misc/kernel.git
+F:	drivers/gpu/drm/drm_hotplug_event_notifier.c
+F:	include/drm/drm_hotplug_event_notifier.h
+
 DRM LOG
 M:	Jocelyn Falempe <[email protected]>
 M:	Javier Martinez Canillas <[email protected]>
drivers/gpu/drm/Kconfig +3 -0
index 61111d50b1c6..5268bf47b2e3 100644
@@ -33,6 +33,9 @@ endmenu
 
 if DRM
 
+config DRM_HOTPLUG_EVENT_NOTIFIER
+	bool
+
 config DRM_MIPI_DBI
 	tristate
 	depends on DRM
drivers/gpu/drm/Makefile +2 -0
index 2c6bc093a923..e4268c5b771a 100644
@@ -95,6 +95,8 @@ drm-$(CONFIG_DRM_DRAW) += drm_draw.o
 drm-$(CONFIG_DRM_RAS) += drm_ras.o drm_ras_nl.o drm_ras_genl_family.o
 obj-$(CONFIG_DRM)	+= drm.o
 
+obj-$(CONFIG_DRM_HOTPLUG_EVENT_NOTIFIER) += drm_hotplug_event_notifier.o
+
 obj-$(CONFIG_DRM_PANEL) += drm_panel.o
 obj-$(CONFIG_DRM_PANEL_ORIENTATION_QUIRKS) += drm_panel_orientation_quirks.o
 obj-$(CONFIG_DRM_PANEL_BACKLIGHT_QUIRKS) += drm_panel_backlight_quirks.o
drivers/gpu/drm/drm_hotplug_event_notifier.c new file +62 -0

62 lines not on this page · raw message

include/drm/drm_hotplug_event_notifier.h new file +52 -0

52 lines not on this page · raw message

Signature · 1 line
2.56.1
author reviewer
#28 lore raw

PATCH v4 18/32 drm/bridge: notify about added and detached bridges

In reply to Luca Ceresoli

In preparation to support DRM bridge hotplug, let the drm_bridge code emit an event when a bridge is added and when it is detached, so that this event can trigger the actions needed to (de)configure the pipeline and (un)register the connector as appropriate.

---

Changes in v3:
- also notify on added bridges (for non-DSI bridges)
- use new v3 notifier API name

---
 drivers/gpu/drm/Kconfig      | 1 +
 drivers/gpu/drm/drm_bridge.c | 6 ++++++
 2 files changed, 7 insertions(+)
drivers/gpu/drm/Kconfig +1 -0
index 5268bf47b2e3..105dd46a0a45 100644
@@ -17,6 +17,7 @@ menuconfig DRM
 # device and dmabuf fd. Let's make sure that is available for our userspace.
 	select KCMP
 	select VIDEO
+	select DRM_HOTPLUG_EVENT_NOTIFIER
 	help
 	  Kernel-level support for the Direct Rendering Infrastructure (DRI)
 	  introduced in XFree86 4.0. If you say Y here, you need to select
drivers/gpu/drm/drm_bridge.c +6 -0
index 310f03bdb744..a50709ac8b06 100644
@@ -34,6 +34,7 @@
 #include <drm/drm_debugfs.h>
 #include <drm/drm_edid.h>
 #include <drm/drm_encoder.h>
+#include <drm/drm_hotplug_event_notifier.h>
 #include <drm/drm_file.h>
 #include <drm/drm_of.h>
 #include <drm/drm_print.h>
@@ -475,6 +476,8 @@ void drm_bridge_add(struct drm_bridge *bridge)
 	mutex_lock(&bridge_lock);
 	list_add_tail(&bridge->list, &bridge_list);
 	mutex_unlock(&bridge_lock);
+
+	drm_hotplug_event_notifier_notify(DRM_BRIDGE_ADDED, bridge);
 }
 EXPORT_SYMBOL(drm_bridge_add);
 
@@ -710,6 +713,9 @@ void drm_bridge_detach(struct drm_bridge *bridge)
 		bridge->funcs->detach(bridge);
 
 	list_del_init(&bridge->chain_node);
+
+	drm_hotplug_event_notifier_notify(DRM_BRIDGE_DETACHED, bridge);
+
 	bridge->dev = NULL;
 	drm_bridge_put(bridge);
 }
Signature · 1 line
2.56.1
author reviewer
#29 lore raw

PATCH v4 19/32 drm/mipi-dsi: notify about DSI attach

In reply to Luca Ceresoli

In preparation to support DRM bridge hotplug, let the drm_mipi_dsi code emit an event when a DSI device is attached to the corresponding DSI host, so that this event can trigger the actions needed to configure the pipeline and register the connector as appropriate.

---

Changes in v3:
- use new v3 notifier API name
- fix commit message

---
 drivers/gpu/drm/Kconfig        | 1 +
 drivers/gpu/drm/drm_mipi_dsi.c | 3 +++
 2 files changed, 4 insertions(+)
drivers/gpu/drm/Kconfig +1 -0
index 105dd46a0a45..3ebd9388bdda 100644
@@ -45,6 +45,7 @@ config DRM_MIPI_DBI
 config DRM_MIPI_DSI
 	bool
 	depends on DRM
+	select DRM_HOTPLUG_EVENT_NOTIFIER
 
 config DRM_KMS_HELPER
 	tristate
drivers/gpu/drm/drm_mipi_dsi.c +3 -0
index 3ac1dd5ad640..ae49f3aa1b2b 100644
@@ -34,6 +34,7 @@
 #include <linux/slab.h>
 
 #include <drm/display/drm_dsc.h>
+#include <drm/drm_hotplug_event_notifier.h>
 #include <drm/drm_mipi_dsi.h>
 #include <drm/drm_print.h>
 
@@ -386,6 +387,8 @@ int mipi_dsi_attach(struct mipi_dsi_device *dsi)
 
 	dsi->attached = true;
 
+	drm_hotplug_event_notifier_notify(DRM_MIPI_DSI_ATTACHED, dsi);
+
 	return 0;
 }
 EXPORT_SYMBOL(mipi_dsi_attach);
Signature · 1 line
2.56.1
author reviewer
#30 lore raw

PATCH v4 20/32 drm/bridge: add drm_bridge_get_next() and supporting func

In reply to Luca Ceresoli

For bridge hotplug we need to successfully probe a card with an incomplete bridge chain, i.e. a chain whose last bridge currently in bridge_chain needs another bridge at its output. Such a card would have no connector, and be able to add one as soon as the following bridges are added up to the bridge that requires no further ones (like a panel or a connector_bridge).

So we need a way to know whether the pipeline is complete in the hardware (all bridges probed), in order to complete it in software (attach all bridges not yet attached). Currently common DRM code has no way to know that.

Add drm_bridge_get_next() and a supporting get_next_bridge func so each bridge can expose its next bridge, and whether there's supposed to be one.

A subsequent commit will use this function to detect whether the pipeline is complete in the hardware or not.

---

Changes in v3:
- fix and improve kerneldoc

New patch in v2, replacing the is_tail patch in v1

---
 drivers/gpu/drm/drm_bridge.c | 30 ++++++++++++++++++++++++++++++
 include/drm/drm_bridge.h     | 25 +++++++++++++++++++++++++
 2 files changed, 55 insertions(+)
drivers/gpu/drm/drm_bridge.c +30 -0
index a50709ac8b06..e0e0dd7fc12b 100644
@@ -720,6 +720,36 @@ void drm_bridge_detach(struct drm_bridge *bridge)
 	drm_bridge_put(bridge);
 }
 
+/**
+ * drm_bridge_get_next - return the bridge at this bridge's output port
+ * @bridge: the bridge to query
+ *
+ * Return the next bridge, i.e. the bridge that is connected at the output
+ * port of @bridge. Unlike drm_bridge_get_next_bridge(), the next bridge
+ * might or not be in the encoder chain.
+ *
+ * Returns:
+ * * Pointer to a bridge connected to the output port of this bridge,
+ *   with refcount incremented; call drm_bridge_put() when done
+ * * ERR_PTR(-ENODEV): this bridge has an output port where a next bridge
+ *                     needs to be present for video output, but the next
+ *                     bridge is not currently available
+ * * NULL: this bridge does not have an output port where a next bridge
+ *         is expected
+ * * ERR_PTR(-ENOENT): the bridge does not implement the func
+ * * Another negative error returned by the bridge func
+ */
+struct drm_bridge *drm_bridge_get_next(struct drm_bridge *bridge)
+{
+	if (!(bridge->ops & DRM_BRIDGE_OP_GET_NEXT_BRIDGE)) {
+		drm_warn_once(bridge->dev, "get_next_bridge func not implemented!");
+		return ERR_PTR(-ENOENT);
+	}
+
+	return bridge->funcs->get_next_bridge(bridge);
+}
+EXPORT_SYMBOL(drm_bridge_get_next);
+
 /**
  * DOC: bridge operations
  *
include/drm/drm_bridge.h +25 -0
index d6ef020c4cc9..cfa0aff1de4f 100644
@@ -62,6 +62,25 @@ enum drm_bridge_attach_flags {
  * struct drm_bridge_funcs - drm_bridge control functions
  */
 struct drm_bridge_funcs {
+	/**
+	 * @get_next_bridge:
+	 *
+	 * Return a pointer to the bridge connected at the output port of
+	 * this bridge.
+	 *
+	 * Returns:
+	 * * Pointer to a bridge connected to the output port of this bridge,
+	 *   with refcount incremented; call drm_bridge_put() when done
+	 * * ERR_PTR(-ENODEV): this bridge has an output port where a next
+	 *                     bridge needs to be present for video output,
+	 *                     but the next bridge is not currently
+	 *                     available
+	 * * NULL: this bridge does not have an output port where a next
+	 *         bridge is expected (e.g. a panel)
+	 * * Another negative error returned by the bridge func
+	 */
+	struct drm_bridge *(*get_next_bridge)(struct drm_bridge *bridge);
+
 	/**
 	 * @attach:
 	 *
@@ -1021,6 +1040,11 @@ enum drm_bridge_ops {
 	 * &drm_bridge_funcs->hdmi_clear_spd_infoframe callbacks.
 	 */
 	DRM_BRIDGE_OP_HDMI_SPD_INFOFRAME = BIT(10),
+	/**
+	 * @DRM_BRIDGE_OP_GET_NEXT_BRIDGE: The bridge implements the
+	 * &drm_bridge_funcs->get_next_bridge callback.
+	 */
+	DRM_BRIDGE_OP_GET_NEXT_BRIDGE = BIT(11),
 };
 
 /**
@@ -1272,6 +1296,7 @@ void drm_bridge_remove(struct drm_bridge *bridge);
 int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
 		      struct drm_bridge *previous,
 		      enum drm_bridge_attach_flags flags);
+struct drm_bridge *drm_bridge_get_next(struct drm_bridge *bridge);
 
 #ifdef CONFIG_OF
 struct drm_bridge *of_drm_find_and_get_bridge(const struct device_node *np);
Signature · 1 line
2.56.1
author reviewer
#31 lore raw

PATCH v4 21/32 drm/panel: implement .get_next_bridge

In reply to Luca Ceresoli

This bridge never has a next bridge. Add get_next_callback func to expose this.

---
 drivers/gpu/drm/drm_panel.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_panel.c +7 -1
index 26d9378563f5..ed8d6c43b6fb 100644
@@ -70,6 +70,11 @@ static const struct drm_connector_funcs panel_bridge_connector_funcs = {
 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
 };
 
+static struct drm_bridge *drm_panel_bridge_get_next_bridge(struct drm_bridge *bridge)
+{
+	return NULL;
+}
+
 static int panel_bridge_attach(struct drm_bridge *bridge,
 			       struct drm_encoder *encoder,
 			       enum drm_bridge_attach_flags flags)
@@ -220,6 +225,7 @@ static void panel_bridge_debugfs_init(struct drm_bridge *bridge,
 }
 
 static const struct drm_bridge_funcs panel_bridge_bridge_funcs = {
+	.get_next_bridge = drm_panel_bridge_get_next_bridge,
 	.attach = panel_bridge_attach,
 	.detach = panel_bridge_detach,
 	.atomic_pre_enable = panel_bridge_atomic_pre_enable,
@@ -516,7 +522,7 @@ void drm_panel_add(struct drm_panel *panel)
 	mutex_unlock(&panel_lock);
 
 	panel->bridge.of_node = panel->dev->of_node;
-	panel->bridge.ops = DRM_BRIDGE_OP_MODES;
+	panel->bridge.ops = DRM_BRIDGE_OP_MODES | DRM_BRIDGE_OP_GET_NEXT_BRIDGE;
 	panel->bridge.pre_enable_prev_first = panel->prepare_prev_first;
 
 	drm_bridge_add(&panel->bridge);
Signature · 1 line
2.56.1
author reviewer
#32 lore raw

PATCH v4 22/32 drm/bridge: display-connector: implement .get_next_bridge

In reply to Luca Ceresoli

This bridge never has a next bridge. Add get_next_callback func to expose this.

---
 drivers/gpu/drm/bridge/display-connector.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/gpu/drm/bridge/display-connector.c +7 -0
index eb3412ec30a0..8b16ad89c7f1 100644
@@ -36,6 +36,11 @@ to_display_connector(struct drm_bridge *bridge)
 	return container_of(bridge, struct display_connector, bridge);
 }
 
+static struct drm_bridge *display_connector_get_next_bridge(struct drm_bridge *bridge)
+{
+	return NULL;
+}
+
 static int display_connector_attach(struct drm_bridge *bridge,
 				    struct drm_encoder *encoder,
 				    enum drm_bridge_attach_flags flags)
@@ -214,6 +219,7 @@ static u32 *display_connector_get_input_bus_fmts(struct drm_bridge *bridge,
 }
 
 static const struct drm_bridge_funcs display_connector_bridge_funcs = {
+	.get_next_bridge = display_connector_get_next_bridge,
 	.attach = display_connector_attach,
 	.destroy = display_connector_destroy,
 	.detect = display_connector_bridge_detect,
@@ -412,6 +418,7 @@ static int display_connector_probe(struct platform_device *pdev)
 
 	conn->bridge.of_node = pdev->dev.of_node;
 
+	conn->bridge.ops = DRM_BRIDGE_OP_GET_NEXT_BRIDGE;
 	if (conn->bridge.ddc)
 		conn->bridge.ops |= DRM_BRIDGE_OP_EDID
 				 |  DRM_BRIDGE_OP_DETECT;
Signature · 1 line
2.56.1
author reviewer
#33 lore raw

PATCH v4 23/32 drm/bridge: ti-sn65dsi83: implement .get_next_bridge

In reply to Luca Ceresoli

Add get_next_callback func to expose the next bridge.

---

Changes in v3:
- add missing drm_bridge_get

---
 drivers/gpu/drm/bridge/ti-sn65dsi83.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/gpu/drm/bridge/ti-sn65dsi83.c +9 -0
index 77ef1613c5f5..9e9cf0dda92d 100644
@@ -295,6 +295,13 @@ static struct sn65dsi83 *bridge_to_sn65dsi83(struct drm_bridge *bridge)
 	return container_of(bridge, struct sn65dsi83, bridge);
 }
 
+static struct drm_bridge *sn65dsi83_get_next_bridge(struct drm_bridge *bridge)
+{
+	struct sn65dsi83 *ctx = bridge_to_sn65dsi83(bridge);
+
+	return ctx->panel_bridge ? drm_bridge_get(ctx->panel_bridge) : ERR_PTR(-ENODEV);
+}
+
 static int sn65dsi83_attach(struct drm_bridge *bridge,
 			    struct drm_encoder *encoder,
 			    enum drm_bridge_attach_flags flags)
@@ -795,6 +802,7 @@ sn65dsi83_atomic_get_input_bus_fmts(struct drm_bridge *bridge,
 }
 
 static const struct drm_bridge_funcs sn65dsi83_funcs = {
+	.get_next_bridge	= sn65dsi83_get_next_bridge,
 	.attach			= sn65dsi83_attach,
 	.detach			= sn65dsi83_detach,
 	.atomic_enable		= sn65dsi83_atomic_enable,
@@ -1045,6 +1053,7 @@ static int sn65dsi83_probe(struct i2c_client *client)
 	ctx->bridge.of_node = dev->of_node;
 	ctx->bridge.pre_enable_prev_first = true;
 	ctx->bridge.type = DRM_MODE_CONNECTOR_LVDS;
+	ctx->bridge.ops = DRM_BRIDGE_OP_GET_NEXT_BRIDGE;
 	drm_bridge_add(&ctx->bridge);
 
 	ret = sn65dsi83_host_attach(ctx);
Signature · 1 line
2.56.1
#34 lore raw

Re: [PATCH v4 23/32] drm/bridge: ti-sn65dsi83: implement .get_next_bridge

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] drm/bridge: ti-sn65dsi83: use-after-free in sn65dsi83_get_next_bridge() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/bridge: ti-sn65dsi83: use-after-free in sn65dsi83_get_next_bridge()
--

commit 07f44b5de19c58e65f65432a2a999004dd04f3ae
Author: Luca Ceresoli <[email protected]>

drm/bridge: ti-sn65dsi83: implement .get_next_bridge

This commit implements the .get_next_bridge callback for the ti-sn65dsi83 bridge driver to return a reference to ctx->panel_bridge.

Quoted 5 lines
drivers/gpu/drm/bridge/ti-sn65dsi83.c +0 -0
index 77ef1613c5f51..9e9cf0dda92d8 100644

drivers/gpu/drm/bridge/ti-sn65dsi83.c static struct sn65dsi83 *bridge_to_sn65dsi83(struct drm_bridge *bridge) · line 303

Quoted 9 lines
drivers/gpu/drm/bridge/ti-sn65dsi83.c +6 -0
@@ -295,6 +295,13 @@ static struct sn65dsi83 *bridge_to_sn65dsi83(struct drm_bridge *bridge)
 	return container_of(bridge, struct sn65dsi83, bridge);
 }
 
+static struct drm_bridge *sn65dsi83_get_next_bridge(struct drm_bridge *bridge)
+{
+	struct sn65dsi83 *ctx = bridge_to_sn65dsi83(bridge);
+
+	return ctx->panel_bridge ? drm_bridge_get(ctx->panel_bridge) : ERR_PTR(-ENODEV);
+}

[Severity: High]
Can sn65dsi83_get_next_bridge() cause a use-after-free on ctx->panel_bridge?

sn65dsi83_parse_dt() acquires ctx->panel_bridge via devm_drm_of_get_bridge() -> drm_of_find_panel_or_bridge():

drivers/gpu/drm/drm_panel.c:drm_of_find_panel_or_bridge() {
	...
	if (bridge) {
		...
		*bridge = of_drm_find_bridge(remote);
		if (*bridge)
			return 0;
	}
	...
}

When the remote DT node is a non-panel bridge, of_drm_find_bridge() drops the bridge reference before returning, so ctx->panel_bridge holds no reference. If the downstream bridge is later removed and freed while sn65dsi83 stays bound, drm_bridge_get(ctx->panel_bridge) increments the refcount on freed memory and returns a dangling pointer.

Even when the remote node is a drm_panel, devm_drm_panel_bridge_add() is registered on ctx->dev after devm_drm_bridge_alloc(), so on unbind devres puts ctx->panel_bridge before ctx->bridge is destroyed without clearing ctx->panel_bridge; a concurrent holder of ctx->bridge calling drm_bridge_get_next() can then access ctx->panel_bridge after its reference was dropped. Acquiring the downstream bridge with of_drm_get_bridge_by_endpoint() and storing it in ctx->bridge.next_bridge (or releasing it in a bridge .destroy callback) would keep the reference alive for the lifetime of ctx->bridge.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=23
author reviewer
#35 lore raw

PATCH v4 24/32 drm/bridge: ti-sn65dsi86: implement .get_next_bridge

In reply to Luca Ceresoli

Add get_next_callback func to expose the next bridge.

---

Changes in v3:
- add missing drm_bridge_get

---
 drivers/gpu/drm/bridge/ti-sn65dsi86.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/gpu/drm/bridge/ti-sn65dsi86.c +10 -1
index 38318113a534..6dadcd7eb8d1 100644
@@ -743,6 +743,13 @@ static int ti_sn_attach_host(struct auxiliary_device *adev, struct ti_sn65dsi86
 	return devm_mipi_dsi_attach(&adev->dev, dsi);
 }
 
+static struct drm_bridge *sn65dsi86_get_next_bridge(struct drm_bridge *bridge)
+{
+	struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);
+
+	return pdata->next_bridge ? drm_bridge_get(pdata->next_bridge) : ERR_PTR(-ENODEV);
+}
+
 static int ti_sn_bridge_attach(struct drm_bridge *bridge,
 			       struct drm_encoder *encoder,
 			       enum drm_bridge_attach_flags flags)
@@ -1353,6 +1360,7 @@ static void ti_sn_bridge_hpd_disable(struct drm_bridge *bridge)
 }
 
 static const struct drm_bridge_funcs ti_sn_bridge_funcs = {
+	.get_next_bridge = sn65dsi86_get_next_bridge,
 	.attach = ti_sn_bridge_attach,
 	.detach = ti_sn_bridge_detach,
 	.mode_valid = ti_sn_bridge_mode_valid,
@@ -1491,8 +1499,9 @@ static int ti_sn_bridge_probe(struct auxiliary_device *adev,
 	pdata->bridge.type = pdata->next_bridge->type == DRM_MODE_CONNECTOR_DisplayPort
 			   ? DRM_MODE_CONNECTOR_DisplayPort : DRM_MODE_CONNECTOR_eDP;
 
+	pdata->bridge.ops = DRM_BRIDGE_OP_GET_NEXT_BRIDGE;
 	if (pdata->bridge.type == DRM_MODE_CONNECTOR_DisplayPort) {
-		pdata->bridge.ops = DRM_BRIDGE_OP_EDID | DRM_BRIDGE_OP_DETECT;
+		pdata->bridge.ops |= DRM_BRIDGE_OP_EDID | DRM_BRIDGE_OP_DETECT;
 		if (client->irq)
 			pdata->bridge.ops |= DRM_BRIDGE_OP_HPD;
 		/*
Signature · 1 line
2.56.1
#36 lore raw

Re: [PATCH v4 24/32] drm/bridge: ti-sn65dsi86: implement .get_next_bridge

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] drm/bridge: ti-sn65dsi86: UAF on pdata->next_bridge in sn65dsi86_get_next_bridge() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/bridge: ti-sn65dsi86: UAF on pdata->next_bridge in sn65dsi86_get_next_bridge()
--

commit 79f96bd174206efa5c1019cb2a4980bed689b63c
Author: Luca Ceresoli <[email protected]>

drm/bridge: ti-sn65dsi86: implement .get_next_bridge

This commit implements the .get_next_bridge callback and sets DRM_BRIDGE_OP_GET_NEXT_BRIDGE to expose the downstream bridge in the ti-sn65dsi86 driver.

Quoted 5 lines
drivers/gpu/drm/bridge/ti-sn65dsi86.c +0 -0
index 38318113a534f..6dadcd7eb8d18 100644

drivers/gpu/drm/bridge/ti-sn65dsi86.c static int ti_sn_attach_host(struct auxiliary_device *adev, struct ti_sn65dsi86 · line 751

Quoted 9 lines
drivers/gpu/drm/bridge/ti-sn65dsi86.c +6 -0
@@ -743,6 +743,13 @@ static int ti_sn_attach_host(struct auxiliary_device *adev, struct ti_sn65dsi86
 	return devm_mipi_dsi_attach(&adev->dev, dsi);
 }
 
+static struct drm_bridge *sn65dsi86_get_next_bridge(struct drm_bridge *bridge)
+{
+	struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);
+
+	return pdata->next_bridge ? drm_bridge_get(pdata->next_bridge) : ERR_PTR(-ENODEV);
+}

[Severity: High]
Can sn65dsi86_get_next_bridge() pass a dangling or ERR_PTR pointer to drm_bridge_get()?

ti_sn_bridge_probe() obtains pdata->next_bridge with devm_drm_of_get_bridge(&adev->dev, np, 1, 0). For a non-panel downstream bridge (such as a DisplayPort connector bridge), devm_drm_of_get_bridge() does not hold a reference to the returned bridge, so pdata->next_bridge becomes a dangling pointer if the downstream bridge is removed and freed.

Even for a panel bridge, its devres release is bound to the child auxiliary device (&adev->dev), whereas pdata->bridge lives on the parent I2C device and neither ti_sn_bridge_remove() nor the probe error path clears pdata->next_bridge. In addition, ti_sn_bridge_probe() stores the result of devm_drm_of_get_bridge() directly into pdata->next_bridge before checking IS_ERR(), so a failed re-probe leaves an ERR_PTR in pdata->next_bridge that passes the non-NULL check here and is dereferenced by drm_bridge_get().

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=24
author reviewer
#37 lore raw

PATCH v4 25/32 drm/bridge: samsung-dsim: implement .get_next_bridge

In reply to Luca Ceresoli

Add get_next_callback func to expose the next bridge.

---

Changes in v3:
- add missing drm_bridge_get

---
 drivers/gpu/drm/bridge/samsung-dsim.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/gpu/drm/bridge/samsung-dsim.c +9 -0
index 480e568dd2c9..a00902871b79 100644
@@ -1821,6 +1821,13 @@ static void samsung_dsim_mode_set(struct drm_bridge *bridge,
 	drm_mode_copy(&dsi->mode, adjusted_mode);
 }
 
+static struct drm_bridge *samsung_dsim_get_next_bridge(struct drm_bridge *bridge)
+{
+	struct samsung_dsim *dsi = bridge_to_dsi(bridge);
+
+	return dsi->bridge.next_bridge ? drm_bridge_get(dsi->bridge.next_bridge) : ERR_PTR(-ENODEV);
+}
+
 static int samsung_dsim_attach(struct drm_bridge *bridge,
 			       struct drm_encoder *encoder,
 			       enum drm_bridge_attach_flags flags)
@@ -1832,6 +1839,7 @@ static int samsung_dsim_attach(struct drm_bridge *bridge,
 }
 
 static const struct drm_bridge_funcs samsung_dsim_bridge_funcs = {
+	.get_next_bridge		= samsung_dsim_get_next_bridge,
 	.atomic_duplicate_state		= drm_atomic_helper_bridge_duplicate_state,
 	.atomic_destroy_state		= drm_atomic_helper_bridge_destroy_state,
 	.atomic_create_state			= drm_atomic_helper_bridge_create_state,
@@ -2183,6 +2191,7 @@ int samsung_dsim_probe(struct platform_device *pdev)
 
 	dsi->bridge.of_node = dev->of_node;
 	dsi->bridge.type = DRM_MODE_CONNECTOR_DSI;
+	dsi->bridge.ops = DRM_BRIDGE_OP_GET_NEXT_BRIDGE;
 
 	/* DE_LOW: i.MX8M Mini/Nano LCDIF-DSIM glue logic inverts HS/VS/DE */
 	if (dsi->plat_data->hw_type == DSIM_TYPE_IMX8MM)
Signature · 1 line
2.56.1
#38 lore raw

Re: [PATCH v4 25/32] drm/bridge: samsung-dsim: implement .get_next_bridge

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] drm/bridge: samsung-dsim: next_bridge set after drm_bridge_add() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] drm/bridge: samsung-dsim: next_bridge set after drm_bridge_add()
--

commit 1b84a9a9872c3d15595f65a9e6b8efef31c355b5
Author: Luca Ceresoli <[email protected]>

drm/bridge: samsung-dsim: implement .get_next_bridge

This commit implements the .get_next_bridge callback and sets DRM_BRIDGE_OP_GET_NEXT_BRIDGE in the samsung-dsim bridge driver.

Quoted 5 lines
drivers/gpu/drm/bridge/samsung-dsim.c +0 -0
index 480e568dd2c9d..a00902871b79e 100644

drivers/gpu/drm/bridge/samsung-dsim.c static void samsung_dsim_mode_set(struct drm_bridge *bridge, · line 1829

Quoted 9 lines
drivers/gpu/drm/bridge/samsung-dsim.c +6 -0
@@ -1821,6 +1821,13 @@ static void samsung_dsim_mode_set(struct drm_bridge *bridge,
 	drm_mode_copy(&dsi->mode, adjusted_mode);
 }
 
+static struct drm_bridge *samsung_dsim_get_next_bridge(struct drm_bridge *bridge)
+{
+	struct samsung_dsim *dsi = bridge_to_dsi(bridge);
+
+	return dsi->bridge.next_bridge ? drm_bridge_get(dsi->bridge.next_bridge) : ERR_PTR(-ENODEV);
+}

[Severity: Medium]
Can samsung_dsim_get_next_bridge() observe dsi->bridge.next_bridge as NULL when drm_bridge_add() notifies listeners?

In samsung_dsim_host_attach(), drm_bridge_add(&dsi->bridge) is called before dsi->bridge.next_bridge is assigned:

drivers/gpu/drm/bridge/samsung-dsim.c:samsung_dsim_host_attach() {
	...
	drm_bridge_add(&dsi->bridge);

	if (!(device->mode_flags & MIPI_DSI_MODE_VIDEO)) {
		ret = samsung_dsim_register_te_irq(dsi, &device->dev);
		if (ret)
			goto err_remove_bridge;
	}

	// The next bridge can be used by host_ops->attach
	dsi->bridge.next_bridge = drm_bridge_get(next_bridge);
	...
}

Because drm_bridge_add() publishes &dsi->bridge on bridge_list and synchronously emits DRM_BRIDGE_ADDED, a hotplug handler such as drm_hotplug_helper_bridge_notif() runs before dsi->bridge.next_bridge is populated. Calling drm_bridge_get_next() from that notification returns ERR_PTR(-ENODEV), and drm_hotplug_helper_try_complete() -> drm_bridge_attach() -> samsung_dsim_attach() passes NULL to drm_bridge_attach(), failing with -EINVAL and logging a DRM_ERROR().

Assigning dsi->bridge.next_bridge before calling drm_bridge_add() (and cleaning it up on error) would ensure it is valid as soon as the bridge is published.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=25
author reviewer
#39 lore raw

PATCH v4 26/32 drm/bridge: add drm_bridge_detach_from()

In reply to Luca Ceresoli

Supporting bridge hotplug requires the ability to detach all bridges from the one that got removed to the end of the pipeline. Add a function for this.

Take care to not try detaching non-attached bridges. This is needed because when two or more bridges are removed not in the backwards order, drm_encoder_cleanup_from() is called more than once for bridges closer to the panel.

---

This replaces "drm/encoder: add drm_encoder_cleanup_from()" from v2 as discussed with Maxime in v1:
https://lore.kernel.org/all/20260609-hypnotic-honeybee-of-adventure-d89b7c@houat/

---
 drivers/gpu/drm/drm_bridge.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
drivers/gpu/drm/drm_bridge.c +34 -0
index e0e0dd7fc12b..c00d5276e55b 100644
@@ -504,6 +504,40 @@ int devm_drm_bridge_add(struct device *dev, struct drm_bridge *bridge)
 }
 EXPORT_SYMBOL(devm_drm_bridge_add);
 
+/**
+ * drm_bridge_detach_from - detach a given bridge and all the following
+ *                          from an encoder chain
+ * @bridge: first bridge to remove
+ *
+ * Removes from an encoder all the bridges starting with a given bridge
+ * and until the end of the chain.
+ *
+ * Does nothing if the bridge is not attached to an encoder chain.
+ */
+static void drm_bridge_detach_from(struct drm_bridge *bridge)
+{
+	struct drm_encoder *encoder = bridge->encoder;
+	struct drm_bridge *next;
+	LIST_HEAD(tmplist);
+
+	/*
+	 * We need the bridge_chain_mutex to modify the chain, but
+	 * drm_bridge_detach() will call DRM_MODESET_LOCK_ALL_BEGIN() (in
+	 * drm_modeset_lock_fini()), resulting in a possible ABBA circular
+	 * deadlock. Avoid it by first moving all the bridges to a
+	 * temporary list holding the lock, and then calling
+	 * drm_bridge_detach() without the lock.
+	 */
+	mutex_lock(&encoder->bridge_chain_mutex);
+	if (!list_empty(&bridge->chain_node))
+		list_for_each_entry_safe_from(bridge, next, &encoder->bridge_chain, chain_node)
+			list_move_tail(&bridge->chain_node, &tmplist);
+	mutex_unlock(&encoder->bridge_chain_mutex);
+
+	while (!list_empty(&tmplist))
+		drm_bridge_detach(list_first_entry(&tmplist, struct drm_bridge, chain_node));
+}
+
 /**
  * drm_bridge_remove - unregister a bridge
  *
Signature · 1 line
2.56.1
#40 lore raw

Re: [PATCH v4 26/32] drm/bridge: add drm_bridge_detach_from()

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] drm/bridge: stack list corruption and infinite loop in drm_bridge_detach_from() - [Medium]…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] drm/bridge: stack list corruption and infinite loop in drm_bridge_detach_from()
- [Medium] drm/bridge: unused static drm_bridge_detach_from() breaks -Werror bisect
--

commit 2c3d13618a66d24a86a5bd8331f35f64a19c35c9
Author: Luca Ceresoli <[email protected]>

drm/bridge: add drm_bridge_detach_from()

This commit adds a helper to detach a bridge and all subsequent bridges in an encoder's bridge chain.

Quoted 9 lines
drivers/gpu/drm/drm_bridge.c +1 -0
index e0e0dd7fc12bb..c00d5276e55b3 100644
@@ -504,6 +504,40 @@ int devm_drm_bridge_add(struct device *dev, struct drm_bridge *bridge)
 }
 EXPORT_SYMBOL(devm_drm_bridge_add);
 
+/**

drivers/gpu/drm/drm_bridge.c int devm_drm_bridge_add(struct device *dev, struct drm_bridge *bridge) · line 517

Quoted 10 lines
drivers/gpu/drm/drm_bridge.c +10 -0
+ * drm_bridge_detach_from - detach a given bridge and all the following
+ *                          from an encoder chain
+ * @bridge: first bridge to remove
+ *
+ * Removes from an encoder all the bridges starting with a given bridge
+ * and until the end of the chain.
+ *
+ * Does nothing if the bridge is not attached to an encoder chain.
+ */
+static void drm_bridge_detach_from(struct drm_bridge *bridge)

[Severity: Medium]
Does defining drm_bridge_detach_from() as static without any callers in this patch break bisection under CONFIG_WERROR=y or CONFIG_DRM_WERROR=y?

drivers/gpu/drm/Makefile enables -Wunused, so compiling drm_bridge.o fails with -Werror=unused-function until the later commit "drm/bridge: shutdown encoder and detach downstream bridges on bridge unplug" adds the caller in drm_bridge_remove(). Marking it __maybe_unused here (or introducing it in the patch that uses it) avoids the build failure.

Also, the commit message still refers to the v2 name drm_encoder_cleanup_from() instead of drm_bridge_detach_from().

Quoted 12 lines
drivers/gpu/drm/drm_bridge.c +12 -0
+{
+	struct drm_encoder *encoder = bridge->encoder;
+	struct drm_bridge *next;
+	LIST_HEAD(tmplist);
+
+	/*
+	 * We need the bridge_chain_mutex to modify the chain, but
+	 * drm_bridge_detach() will call DRM_MODESET_LOCK_ALL_BEGIN() (in
+	 * drm_modeset_lock_fini()), resulting in a possible ABBA circular
+	 * deadlock. Avoid it by first moving all the bridges to a
+	 * temporary list holding the lock, and then calling
+	 * drm_bridge_detach() without the lock.

drivers/gpu/drm/drm_bridge.c int devm_drm_bridge_add(struct device *dev, struct drm_bridge *bridge) · line 539

Quoted 10 lines
drivers/gpu/drm/drm_bridge.c +10 -0
+	 */
+	mutex_lock(&encoder->bridge_chain_mutex);
+	if (!list_empty(&bridge->chain_node))
+		list_for_each_entry_safe_from(bridge, next, &encoder->bridge_chain, chain_node)
+			list_move_tail(&bridge->chain_node, &tmplist);
+	mutex_unlock(&encoder->bridge_chain_mutex);
+
+	while (!list_empty(&tmplist))
+		drm_bridge_detach(list_first_entry(&tmplist, struct drm_bridge, chain_node));
+}

[Severity: High]
Can moving bridge->chain_node onto a stack-local tmplist cause list corruption and an infinite loop if a downstream bridge is detached while drm_bridge_detach_from() is already running?

list_move_tail(&bridge->chain_node, &tmplist) removes each bridge from encoder->bridge_chain, but leaves bridge->chain_node non-empty while linked into tmplist on the first caller's stack. drm_bridge_detach() only runs list_del_init(&bridge->chain_node) after sleeping in drm_atomic_private_obj_fini() and calling bridge->funcs->detach() without encoder->bridge_chain_mutex held:

CPU0 CPU1
drm_bridge_detach_from(bridgeA)
mutex_lock(&bridge_chain_mutex)
list_move_tail(bridgeA, &tmplist0)
list_move_tail(bridgeB, &tmplist0)
mutex_unlock(&bridge_chain_mutex)
drm_bridge_detach(bridgeA)

    drm_atomic_private_obj_fini()
                                      drm_bridge_detach_from(bridgeB)
                                        mutex_lock(&bridge_chain_mutex)
                                        if (!list_empty(&bridgeB->chain_node))
                                          list_for_each_entry_safe_from(
                                            bridgeB, ...,
                                            &encoder->bridge_chain, ...)
                                            <-- walks tmplist0, never hits
                                                &encoder->bridge_chain

Because bridgeB->chain_node is on CPU0's tmplist0 rather than encoder->bridge_chain, CPU1's list_for_each_entry_safe_from() walks around tmplist0 (including the stack list head itself as a struct drm_bridge), never reaches &encoder->bridge_chain, and corrupts CPU0's stack list while looping forever under encoder->bridge_chain_mutex.

In addition, if drm_bridge_detach() ever takes its early return on WARN_ON(!bridge->dev), bridge->chain_node is not unlinked and while (!list_empty(&tmplist)) spins forever.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=26
author reviewer
#41 lore raw

PATCH v4 27/32 drm/atomic: add drm_atomic_shutdown_encoder()

In reply to Luca Ceresoli

To support bridge hotplug we need to stop the components of a card that are affected by the removal of a bridge: the connector, the encoder and possibly the CRTC (unless it is still in use after the removal due to cloning).

drm_atomic_helper_shutdown() does it, but it disables the entire card, including unrelated CRTCs for multi-CRTC cards. Moreover it is in the drm_kms_helper module, which makes it unaccessible from the core code where we need this.

Add drm_atomic_shutdown_encoder() to drm_atomic code, doing what we need.

---

New patch in v3

---
 drivers/gpu/drm/drm_atomic.c | 145 +++++++++++++++++++++++++++++++++++++++++++
 include/drm/drm_atomic.h     |   1 +
 2 files changed, 146 insertions(+)
drivers/gpu/drm/drm_atomic.c +145 -0
index 096f85419b36..536813562f6f 100644
@@ -2207,6 +2207,151 @@ int __drm_atomic_helper_set_config(struct drm_mode_set *set,
 }
 EXPORT_SYMBOL(__drm_atomic_helper_set_config);
 
+static int drm_atomic_disable_encoder(struct drm_device *drm,
+				      struct drm_encoder *encoder,
+				      struct drm_modeset_acquire_ctx *ctx)
+{
+	struct drm_connector_list_iter conn_iter;
+	struct drm_connector *connector;
+	struct drm_crtc *crtc = NULL;
+	struct drm_atomic_commit *commit;
+	struct drm_crtc_state *crtc_state;
+	bool crtc_still_used = false;
+	int ret;
+
+	drm_connector_list_iter_begin(drm, &conn_iter);
+	drm_for_each_connector_iter(connector, &conn_iter) {
+		if (!connector->state ||
+		    connector->state->best_encoder != encoder ||
+		    !connector->state->crtc)
+			continue;
+
+		crtc = connector->state->crtc;
+		break;
+	}
+	drm_connector_list_iter_end(&conn_iter);
+
+	if (!crtc)
+		return 0;
+
+	commit = drm_atomic_commit_alloc(drm);
+	if (!commit)
+		return -ENOMEM;
+
+	commit->acquire_ctx = ctx;
+
+	crtc_state = drm_atomic_get_crtc_state(commit, crtc);
+	if (IS_ERR(crtc_state)) {
+		ret = PTR_ERR(crtc_state);
+		goto free;
+	}
+
+	drm_connector_list_iter_begin(drm, &conn_iter);
+	drm_for_each_connector_iter(connector, &conn_iter) {
+		struct drm_connector_state *conn_state;
+
+		if (!connector->state || connector->state->crtc != crtc)
+			continue;
+
+		if (connector->state->best_encoder != encoder) {
+			crtc_still_used = true;
+			continue;
+		}
+
+		conn_state = drm_atomic_get_connector_state(commit, connector);
+		if (IS_ERR(conn_state)) {
+			ret = PTR_ERR(conn_state);
+			drm_connector_list_iter_end(&conn_iter);
+			goto free;
+		}
+	}
+	drm_connector_list_iter_end(&conn_iter);
+
+	if (crtc_still_used) {
+		struct drm_connector_state *conn_state;
+		int i;
+
+		/* Some connector still uses @crtc, keep it and its planes enabled */
+		for_each_new_connector_in_state(commit, connector, conn_state, i) {
+			ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
+			if (ret < 0)
+				goto free;
+		}
+	} else {
+		struct drm_connector_state *conn_state;
+		struct drm_plane_state *plane_state;
+		struct drm_plane *plane;
+		int i;
+
+		crtc_state->active = false;
+
+		ret = drm_atomic_set_mode_prop_for_crtc(crtc_state, NULL);
+		if (ret < 0)
+			goto free;
+
+		ret = drm_atomic_add_affected_planes(commit, crtc);
+		if (ret < 0)
+			goto free;
+
+		ret = drm_atomic_add_affected_connectors(commit, crtc);
+		if (ret < 0)
+			goto free;
+
+		for_each_new_connector_in_state(commit, connector, conn_state, i) {
+			ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
+			if (ret < 0)
+				goto free;
+		}
+
+		for_each_new_plane_in_state(commit, plane, plane_state, i) {
+			ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
+			if (ret < 0)
+				goto free;
+
+			drm_atomic_set_fb_for_plane(plane_state, NULL);
+		}
+	}
+
+	ret = drm_atomic_commit(commit);
+free:
+	drm_atomic_commit_put(commit);
+	return ret;
+}
+
+/**
+ * drm_atomic_shutdown_encoder - shutdown the pipeline behind an encoder
+ * @encoder: encoder whose pipeline should be shut down
+ *
+ * Stop the output currently driven by @encoder. Also shutdown the
+ * connected CRTC unless other connectors are still attached to it
+ * (e.g. cloning).
+ */
+void drm_atomic_shutdown_encoder(struct drm_encoder *encoder)
+{
+	struct drm_modeset_acquire_ctx ctx;
+	struct drm_device *dev;
+	int ret;
+
+	if (!encoder)
+		return;
+
+	dev = encoder->dev;
+	if (!dev)
+		return;
+
+	if (!drm_drv_uses_atomic_modeset(dev))
+		return;
+
+	DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);
+
+	ret = drm_atomic_disable_encoder(dev, encoder, &ctx);
+	if (ret)
+		drm_err(dev, "Disabling CRTC for encoder %s failed with %i\n", encoder->name, ret);
+
+	DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
+}
+EXPORT_SYMBOL(drm_atomic_shutdown_encoder);
+
 void drm_atomic_private_obj_print_state(struct drm_printer *p,
 					const struct drm_private_state *state)
 {
include/drm/drm_atomic.h +1 -0
index c8d0f18fd0fd..9e7bd94922ac 100644
@@ -1411,5 +1411,6 @@ drm_atomic_get_old_bridge_state(const struct drm_atomic_commit *state,
 struct drm_bridge_state *
 drm_atomic_get_new_bridge_state(const struct drm_atomic_commit *state,
 				struct drm_bridge *bridge);
+void drm_atomic_shutdown_encoder(struct drm_encoder *encoder);
 
 #endif /* DRM_ATOMIC_H_ */
Signature · 1 line
2.56.1
author reviewer
#42 lore raw

PATCH v4 28/32 drm/tests: atomic: add tests for drm_atomic_shutdown_encoder()

In reply to Luca Ceresoli

Add KUnit tests to exercise the newly added drm_atomic_shutdown_encoder(), including in the case of a card with multiple CRTCs.

---

New patch in v3

---
 drivers/gpu/drm/tests/drm_bridge_test.c | 209 ++++++++++++++++++++++++++++++++
 1 file changed, 209 insertions(+)
drivers/gpu/drm/tests/drm_bridge_test.c +209 -0
index f56209c0ab1c..06d08d1316ae 100644
@@ -5,6 +5,7 @@
 #include <linux/cleanup.h>
 #include <linux/media-bus-format.h>
 
+#include <drm/drm_atomic.h>
 #include <drm/drm_atomic_helper.h>
 #include <drm/drm_atomic_state_helper.h>
 #include <drm/drm_atomic_uapi.h>
@@ -951,6 +952,213 @@ static struct kunit_suite drm_bridge_alloc_test_suite = {
 	.test_cases = drm_bridge_alloc_tests,
 };
 
+/*
+ * Two CRTC/encoder/bridge/connector pipelines on the same DRM device, to
+ * check that drm_atomic_shutdown_encoder() only stops one.
+ */
+struct drm_bridge_two_priv {
+	struct drm_device drm;
+	struct drm_plane *plane[2];
+	struct drm_crtc *crtc[2];
+	struct drm_encoder encoder[2];
+	struct drm_bridge_priv *bridge[2];
+	struct drm_connector *connector[2];
+};
+
+static const struct drm_bridge_funcs drm_test_bridge_atomic_no_destroy_funcs = {
+	.atomic_enable		= drm_test_bridge_atomic_enable,
+	.atomic_disable		= drm_test_bridge_atomic_disable,
+	.atomic_destroy_state	= drm_atomic_helper_bridge_destroy_state,
+	.atomic_duplicate_state	= drm_atomic_helper_bridge_duplicate_state,
+	.atomic_create_state	= drm_atomic_helper_bridge_create_state,
+};
+
+static struct drm_bridge_two_priv *
+drm_test_bridge_two_init(struct kunit *test)
+{
+	struct drm_bridge_two_priv *priv;
+	struct drm_device *drm;
+	struct device *dev;
+	int i, ret;
+
+	dev = drm_kunit_helper_alloc_device(test);
+	if (IS_ERR(dev))
+		return ERR_CAST(dev);
+
+	priv = drm_kunit_helper_alloc_drm_device(test, dev, struct drm_bridge_two_priv,
+						 drm, DRIVER_MODESET | DRIVER_ATOMIC);
+	if (IS_ERR(priv))
+		return priv;
+
+	drm = &priv->drm;
+
+	for (i = 0; i < 2; i++) {
+		priv->bridge[i] = devm_drm_bridge_alloc(dev, struct drm_bridge_priv, bridge,
+							&drm_test_bridge_atomic_no_destroy_funcs);
+		if (IS_ERR(priv->bridge[i]))
+			return ERR_CAST(priv->bridge[i]);
+
+		priv->plane[i] = drm_kunit_helper_create_primary_plane(test, drm,
+								       NULL, NULL, NULL, 0, NULL);
+		if (IS_ERR(priv->plane[i]))
+			return ERR_CAST(priv->plane[i]);
+
+		priv->crtc[i] = drm_kunit_helper_create_crtc(test, drm,
+							     priv->plane[i], NULL, NULL, NULL);
+		if (IS_ERR(priv->crtc[i]))
+			return ERR_CAST(priv->crtc[i]);
+
+		ret = drmm_encoder_init(drm, &priv->encoder[i], NULL, DRM_MODE_ENCODER_TMDS, NULL);
+		if (ret)
+			return ERR_PTR(ret);
+
+		priv->encoder[i].possible_crtcs = drm_crtc_mask(priv->crtc[i]);
+
+		priv->bridge[i]->bridge.type = DRM_MODE_CONNECTOR_VIRTUAL;
+
+		ret = drm_kunit_bridge_add(test, &priv->bridge[i]->bridge);
+		if (ret)
+			return ERR_PTR(ret);
+
+		ret = drm_bridge_attach(&priv->encoder[i], &priv->bridge[i]->bridge, NULL, 0);
+		if (ret)
+			return ERR_PTR(ret);
+
+		priv->connector[i] = drm_bridge_connector_init(drm, &priv->encoder[i]);
+		if (IS_ERR(priv->connector[i]))
+			return ERR_CAST(priv->connector[i]);
+	}
+
+	drm_mode_config_reset(drm);
+
+	return priv;
+}
+
+static int drm_test_enable_pipeline(struct kunit *test,
+				    struct drm_device *drm,
+				    struct drm_crtc *crtc,
+				    struct drm_connector *connector,
+				    const struct drm_display_mode *mode)
+{
+	struct drm_modeset_acquire_ctx ctx;
+	int ret;
+
+	drm_modeset_acquire_init(&ctx, 0);
+
+retry:
+	ret = drm_kunit_helper_enable_crtc_connector(test, drm, crtc, connector, mode, &ctx);
+	if (ret == -EDEADLK) {
+		drm_modeset_backoff(&ctx);
+		goto retry;
+	}
+
+	drm_modeset_drop_locks(&ctx);
+	drm_modeset_acquire_fini(&ctx);
+
+	return ret;
+}
+
+/*
+ * Test that drm_atomic_shutdown_encoder() disables the CRTC feeding the
+ * encoder and detaches its connector.
+ */
+static void drm_test_drm_atomic_shutdown_encoder(struct kunit *test)
+{
+	struct drm_bridge_init_priv *priv;
+	struct drm_display_mode *mode;
+	struct drm_bridge_priv *bridge_priv;
+	int ret;
+
+	priv = drm_test_bridge_init(test, &drm_test_bridge_atomic_funcs);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv);
+
+	mode = drm_kunit_display_mode_from_cea_vic(test, &priv->drm, 16);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, mode);
+
+	ret = drm_test_enable_pipeline(test, &priv->drm, priv->crtc, priv->connector, mode);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	bridge_priv = priv->test_bridge;
+	KUNIT_ASSERT_EQ(test, bridge_priv->enable_count, 1);
+	KUNIT_ASSERT_EQ(test, bridge_priv->disable_count, 0);
+	KUNIT_ASSERT_PTR_EQ(test, priv->connector->state->crtc, priv->crtc);
+
+	drm_atomic_shutdown_encoder(&priv->encoder);
+
+	KUNIT_EXPECT_EQ(test, bridge_priv->disable_count, 1);
+	KUNIT_EXPECT_NULL(test, priv->connector->state->crtc);
+	KUNIT_EXPECT_FALSE(test, priv->crtc->state->active);
+}
+
+/*
+ * Test that drm_atomic_shutdown_encoder() is a no-op when the encoder is not
+ * driving any output.
+ */
+static void drm_test_drm_atomic_shutdown_encoder_disabled(struct kunit *test)
+{
+	struct drm_bridge_init_priv *priv;
+	struct drm_bridge_priv *bridge_priv;
+
+	priv = drm_test_bridge_init(test, &drm_test_bridge_atomic_funcs);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv);
+
+	bridge_priv = priv->test_bridge;
+
+	drm_atomic_shutdown_encoder(&priv->encoder);
+
+	KUNIT_EXPECT_EQ(test, bridge_priv->enable_count, 0);
+	KUNIT_EXPECT_EQ(test, bridge_priv->disable_count, 0);
+	KUNIT_EXPECT_NULL(test, priv->connector->state->crtc);
+}
+
+/*
+ * Test that drm_atomic_shutdown_encoder() only stops the pipeline of the
+ * given encoder and leaves the other CRTC untouched.
+ */
+static void drm_test_drm_atomic_shutdown_encoder_two_crtcs(struct kunit *test)
+{
+	struct drm_bridge_two_priv *priv;
+	struct drm_display_mode *mode;
+	int ret;
+
+	priv = drm_test_bridge_two_init(test);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv);
+
+	mode = drm_kunit_display_mode_from_cea_vic(test, &priv->drm, 16);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, mode);
+
+	ret = drm_test_enable_pipeline(test, &priv->drm, priv->crtc[0], priv->connector[0], mode);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	ret = drm_test_enable_pipeline(test, &priv->drm, priv->crtc[1], priv->connector[1], mode);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	KUNIT_ASSERT_EQ(test, priv->bridge[0]->enable_count, 1);
+	KUNIT_ASSERT_EQ(test, priv->bridge[1]->enable_count, 1);
+
+	drm_atomic_shutdown_encoder(&priv->encoder[0]);
+
+	KUNIT_EXPECT_EQ(test, priv->bridge[0]->disable_count, 1);
+	KUNIT_EXPECT_NULL(test, priv->connector[0]->state->crtc);
+	KUNIT_EXPECT_FALSE(test, priv->crtc[0]->state->active);
+
+	KUNIT_EXPECT_EQ(test, priv->bridge[1]->disable_count, 0);
+	KUNIT_EXPECT_PTR_EQ(test, priv->connector[1]->state->crtc, priv->crtc[1]);
+	KUNIT_EXPECT_TRUE(test, priv->crtc[1]->state->active);
+}
+
+static struct kunit_case drm_atomic_shutdown_encoder_tests[] = {
+	KUNIT_CASE(drm_test_drm_atomic_shutdown_encoder),
+	KUNIT_CASE(drm_test_drm_atomic_shutdown_encoder_disabled),
+	KUNIT_CASE(drm_test_drm_atomic_shutdown_encoder_two_crtcs),
+	{ }
+};
+
+static struct kunit_suite drm_atomic_shutdown_encoder_test_suite = {
+	.name = "drm_atomic_shutdown_encoder",
+	.test_cases = drm_atomic_shutdown_encoder_tests,
+};
+
 /**
  * drm_test_bridge_chain_verify_fmt - Verify bridge chain format selection
  * @test: pointer to KUnit test object
@@ -1381,6 +1589,7 @@ kunit_test_suites(
 	&drm_bridge_get_current_state_test_suite,
 	&drm_bridge_helper_reset_crtc_test_suite,
 	&drm_bridge_alloc_test_suite,
+	&drm_atomic_shutdown_encoder_test_suite,
 	&drm_bridge_bus_fmt_test_suite,
 );
 
Signature · 1 line
2.56.1
author reviewer
#43 lore raw

PATCH v4 29/32 drm/bridge: clear bridge->encoder on detach

In reply to Luca Ceresoli

Attaching a bridge to an encoder rightfully sets bridge->encoder to point to the encoder. It also sets it to NULL in the error management path.

However drm_bridge_detach() does not set bridge->encoder to NULL.

This will create a potential NULL pointer dereference in a following commit (part of when introducing DRM bridge hotplug) because drm_bridge_remove() will have to detach bridges attached to an encoder, so it needs to use 'if (bridge->encoder)' to know whether the bridge is attached to an encoder. For this to work, the bridge->encoder pointer must NULL whenever the bridge is not attached to an encoder.

---

Issue found by sashiko:
https://lore.kernel.org/all/[email protected]/

---
 drivers/gpu/drm/drm_bridge.c | 1 +
 1 file changed, 1 insertion(+)
drivers/gpu/drm/drm_bridge.c +1 -0
index c00d5276e55b..d82e81b26ace 100644
@@ -751,6 +751,7 @@ void drm_bridge_detach(struct drm_bridge *bridge)
 	drm_hotplug_event_notifier_notify(DRM_BRIDGE_DETACHED, bridge);
 
 	bridge->dev = NULL;
+	bridge->encoder = NULL;
 	drm_bridge_put(bridge);
 }
 
Signature · 1 line
2.56.1
author reviewer
#44 lore raw

PATCH v4 30/32 drm/bridge: shutdown encoder and detach downstream bridges on bridge unplug

In reply to Luca Ceresoli

With the upcoming support for DRM bridge hot(un)plugging, bridges can be removed at any time. When this happens, shutdown the affected pipeline components and detach from the encoder chain the bridge being removed along with all the following ones.

---

changes in v3:
- do not shutdown the whole card
- adapt to use drm_bridge_detach_from()

---
 drivers/gpu/drm/drm_bridge.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/gpu/drm/drm_bridge.c +9 -0
index d82e81b26ace..d942c776db1a 100644
@@ -29,6 +29,7 @@
 #include <linux/mutex.h>
 #include <linux/srcu.h>
 
+#include <drm/drm_atomic.h>
 #include <drm/drm_atomic_state_helper.h>
 #include <drm/drm_bridge.h>
 #include <drm/drm_debugfs.h>
@@ -547,9 +548,17 @@ static void drm_bridge_detach_from(struct drm_bridge *bridge)
  * it won't be found by users via of_drm_find_and_get_bridge(), and add it
  * to the lingering bridge list, to keep track of it until its allocated
  * memory is eventually freed.
+ *
+ * If the bridge is currently attached, also shut down the encoder pipeline
+ * and detach this bridge and the following ones.
  */
 void drm_bridge_remove(struct drm_bridge *bridge)
 {
+	if (bridge->encoder) {
+		drm_atomic_shutdown_encoder(bridge->encoder);
+		drm_bridge_detach_from(bridge);
+	}
+
 	mutex_lock(&bridge_lock);
 	list_move_tail(&bridge->list, &bridge_lingering_list);
 	mutex_unlock(&bridge_lock);
Signature · 1 line
2.56.1
#45 lore raw

Re: [PATCH v4 30/32] drm/bridge: shutdown encoder and detach downstream bridges on bridge unplug

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [High] drm/bridge: UAF of shared encoder-bridge container in drm_bridge_remove() - [High] drm/bridge…

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [High] drm/bridge: UAF of shared encoder-bridge container in drm_bridge_remove()
- [High] drm/bridge: TOCTOU race and NULL deref on encoder in drm_bridge_remove()
- [High] drm/bridge: cross-thread stack tmplist corruption in drm_bridge_detach_from()
- [Medium] drm/bridge: bridge left on bridge_list during detach in drm_bridge_remove()
--

commit ed101139b3ca92ec58e58cfca1356ce33f6b56c4
Author: Luca Ceresoli <[email protected]>

drm/bridge: shutdown encoder and detach downstream bridges on bridge unplug

This commit shuts down the encoder pipeline and detaches the removed bridge and any downstream bridges from the encoder chain when drm_bridge_remove() is called on an attached bridge.

Quoted 4 lines
drivers/gpu/drm/drm_bridge.c +0 -0
index d82e81b26ace2..d942c776db1a9 100644

[ ... ]

Quoted 4 lines
drivers/gpu/drm/drm_bridge.c +0 -0
@@ -547,9 +548,17 @@ static void drm_bridge_detach_from(struct drm_bridge *bridge)
  * it won't be found by users via of_drm_find_and_get_bridge(), and add it
  * to the lingering bridge list, to keep track of it until its allocated
  * memory is eventually freed.

drivers/gpu/drm/drm_bridge.c static void drm_bridge_detach_from(struct drm_bridge *bridge) · line 560

Quoted 10 lines
drivers/gpu/drm/drm_bridge.c +7 -0
+ *
+ * If the bridge is currently attached, also shut down the encoder pipeline
+ * and detach this bridge and the following ones.
  */
 void drm_bridge_remove(struct drm_bridge *bridge)
 {
+	if (bridge->encoder) {
+		drm_atomic_shutdown_encoder(bridge->encoder);
+		drm_bridge_detach_from(bridge);
+	}

[Severity: High]
Does detaching the bridge in drm_bridge_remove() cause a use-after-free of the encoder in drivers that embed struct drm_bridge and struct drm_encoder in the same devm_drm_bridge_alloc() container, such as tidss and meson?

drivers/gpu/drm/tidss/tidss_encoder.c:tidss_encoder_create() {
	t_enc = devm_drm_bridge_alloc(tidss->dev, struct tidss_encoder,
				      bridge, &tidss_bridge_funcs);
	if (IS_ERR(t_enc))
		return PTR_ERR(t_enc);

	ret = drm_encoder_init(&tidss->ddev, &t_enc->encoder,
			       &tidss_encoder_funcs, encoder_type, NULL);
    ...
}

In tidss, devm_drm_dev_alloc() and drmm_mode_config_init() are registered before tidss_encoder_create() calls devm_drm_bridge_alloc(), drm_encoder_init(), devm_drm_bridge_add(), and drm_bridge_attach().

On driver unbind or post-encoder probe failure, devres releases actions in reverse order: drm_bridge_remove_void() now detaches t_enc->bridge and drops both the attach and add references, and then drm_bridge_put_void() drops the final allocation reference. __drm_bridge_free() calls kfree(bridge->container), freeing t_enc while t_enc->encoder is still on mode_config.encoder_list. When devm_drm_dev_init_release() later runs drm_mode_config_cleanup(), it calls drm_encoder_cleanup() on the freed t_enc->encoder.

[Severity: High]
Can a concurrent upstream bridge removal clear bridge->encoder while drm_atomic_shutdown_encoder() is sleeping, causing a NULL pointer dereference in drm_bridge_detach_from()?

CPU0 (downstream bridge B) CPU1 (upstream bridge A)
drm_bridge_remove(B)
if (B->encoder)

    drm_atomic_shutdown_encoder()
                                  drm_bridge_remove(A)
                                    ...
                                    drm_bridge_detach_from(A)
                                      drm_bridge_detach(B)
                                        B->encoder = NULL
    drm_bridge_detach_from(B)
      encoder = B->encoder
      mutex_lock(&encoder->bridge_chain_mutex)
        <-- NULL pointer dereference

drm_bridge_detach_from() re-reads bridge->encoder without a NULL check before locking encoder->bridge_chain_mutex. Similarly, if drm_bridge_remove() races with drm_encoder_cleanup(), the encoder and its mutex can be destroyed and freed before drm_bridge_detach_from() locks it; and if it races with drm_bridge_attach() after bridge->encoder is assigned but before bridge->chain_node is added to encoder->bridge_chain, drm_bridge_detach_from() sees list_empty(&bridge->chain_node) and leaves the removed bridge attached.

[Severity: High]
Can concurrent calls to drm_bridge_detach_from() on two bridges in the same chain corrupt the stack list head in the upstream caller?

drivers/gpu/drm/drm_bridge.c:drm_bridge_detach_from() {
    ...
	mutex_lock(&encoder->bridge_chain_mutex);
	if (!list_empty(&bridge->chain_node))
		list_for_each_entry_safe_from(bridge, next, &encoder->bridge_chain, chain_node)
			list_move_tail(&bridge->chain_node, &tmplist);
	mutex_unlock(&encoder->bridge_chain_mutex);

	while (!list_empty(&tmplist))
		drm_bridge_detach(list_first_entry(&tmplist, struct drm_bridge, chain_node));
}

When drm_bridge_detach_from() moves bridge A and downstream bridge B onto its stack-allocated tmplist and drops encoder->bridge_chain_mutex, B's chain_node is still non-empty (linked into that stack tmplist) and B->encoder is still non-NULL until drm_bridge_detach(B) reaches list_del_init(&B->chain_node):

CPU0 (upstream bridge A) CPU1 (downstream bridge B)
drm_bridge_detach_from(A)
mutex_lock(&bridge_chain_mutex)
/* moves A and B to CPU0 tmplist */
mutex_unlock(&bridge_chain_mutex)
drm_bridge_detach(A)

    drm_atomic_private_obj_fini()
                                  drm_bridge_remove(B)
                                    ...
                                    drm_bridge_detach_from(B)
                                      mutex_lock(&bridge_chain_mutex)
                                      if (!list_empty(&B->chain_node))
                                        list_for_each_entry_safe_from(...)
                                          list_move_tail(..., &tmplist)
                                            <-- walks CPU0 tmplist past
                                                its stack list head

Because B->chain_node is on CPU0's tmplist rather than encoder->bridge_chain, CPU1's list_for_each_entry_safe_from() never sees &encoder->bridge_chain as the termination head. It moves CPU0's stack tmplist head onto CPU1's tmplist and passes a bogus struct drm_bridge pointer derived from CPU0's stack frame to drm_bridge_detach().

drivers/gpu/drm/drm_bridge.c static void drm_bridge_detach_from(struct drm_bridge *bridge) · line 564

Quoted 4 lines
drivers/gpu/drm/drm_bridge.c +1 -0
+
 	mutex_lock(&bridge_lock);
 	list_move_tail(&bridge->list, &bridge_lingering_list);
 	mutex_unlock(&bridge_lock);

[Severity: Medium]
Should bridge->list be moved to bridge_lingering_list before shutting down the encoder and detaching the bridge?

Leaving the bridge on bridge_list until after drm_bridge_detach_from() means the bridge is still discoverable via of_drm_find_and_get_bridge() or drm_bridge_get_next() while drm_bridge_detach() emits DRM_BRIDGE_DETACHED and clears bridge->dev and bridge->encoder, allowing a concurrent lookup to find and re-attach the bridge right before drm_bridge_remove() moves it to bridge_lingering_list and destroys its HPD mutexes.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=30
author reviewer
#46 lore raw

PATCH v4 31/32 drm: drm_hotplug_helper: new helper to implement bridge hotplug

In reply to Luca Ceresoli

Add a new helper to support cards implementing hotpluggable drm_bridges.

drmm_hotplug_helper_init() registers to get notified of relevant events and react by creating a bridge (if the pipeline is complete in the hardware) and destroying it on bridge removal.

---

Changes in v3:
- Do not require the first bridge is non-hotpluggable
- Support non-DSI bridges too
- Add callback to report connector add/remove
- Do not store the drm_device pointer, get it from the encoder
- use new v3 notifier API name

Changes in v2:
- Add callback to report connector addition/removal
- Sanity-check drmm_hotplug_helper_init() parameters
- Reformat kdoc comment

---
 MAINTAINERS                                  |   8 +
 drivers/gpu/drm/display/Kconfig              |   7 +
 drivers/gpu/drm/display/Makefile             |   2 +
 drivers/gpu/drm/display/drm_hotplug_helper.c | 325 +++++++++++++++++++++++++++
 include/drm/drm_hotplug_helper.h             |  30 +++
 5 files changed, 372 insertions(+)
MAINTAINERS +8 -0
index a5e2bf15e59f..18ecb7bb39f4 100644
@@ -8219,6 +8219,14 @@ F:	Documentation/devicetree/bindings/npu/rockchip,rk3588-rknn-core.yaml
 F:	drivers/accel/rocket/
 F:	include/uapi/drm/rocket_accel.h
 
+DRM BRIDGE HOTPLUG HELPER
+M:	Luca Ceresoli <[email protected]>
+S:	Maintained
+T:	git https://gitlab.freedesktop.org/drm/misc/kernel.git
+F:	Documentation/devicetree/bindings/display/bridge/
+F:	drivers/gpu/drm/display/drm_hotplug_helper.c
+F:	include/drm/drm_hotplug_helper.h
+
 DRM COMPUTE ACCELERATORS DRIVERS AND FRAMEWORK
 M:	Oded Gabbay <[email protected]>
 L:	[email protected]
drivers/gpu/drm/display/Kconfig +7 -0
index df09cf9a8ca1..e086e2fd08a4 100644
@@ -22,6 +22,13 @@ config DRM_BRIDGE_CONNECTOR
 	help
 	  DRM connector implementation terminating DRM bridge chains.
 
+config DRM_HOTPLUG_HELPER
+	bool
+	depends on OF
+	select DRM_BRIDGE_CONNECTOR
+	help
+	  Helper code to implement a card supporting hotpluggable bridges.
+
 config DRM_DISPLAY_DP_AUX_CEC
 	bool "Enable DisplayPort CEC-Tunneling-over-AUX HDMI support"
 	select DRM_DISPLAY_DP_HELPER
drivers/gpu/drm/display/Makefile +2 -0
index 0ff4a1ad0222..ce6dbd65833b 100644
@@ -5,6 +5,8 @@ obj-$(CONFIG_DRM_DISPLAY_DP_AUX_BUS) += drm_dp_aux_bus.o
 drm_display_helper-y := drm_display_helper_mod.o
 drm_display_helper-$(CONFIG_DRM_BRIDGE_CONNECTOR) += \
 	drm_bridge_connector.o
+drm_display_helper-$(CONFIG_DRM_HOTPLUG_HELPER) += \
+	drm_hotplug_helper.o
 drm_display_helper-$(CONFIG_DRM_DISPLAY_DP_HELPER) += \
 	drm_dp_dual_mode_helper.o \
 	drm_dp_helper.o \
drivers/gpu/drm/display/drm_hotplug_helper.c new file +325 -0

325 lines not on this page · raw message

include/drm/drm_hotplug_helper.h new file +30 -0
new file mode 100644
index 000000000000..ba14d547f8ff
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2026 GE HealthCare
+ * Author: Luca Ceresoli <[email protected]>
+ */
+
+#ifndef __DRM_HOTPLUG_HELPER_H__
+#define __DRM_HOTPLUG_HELPER_H__
+
+/**
+ * enum drm_hotplug_helper_event - DRM hotplug helper connector events
+ */
+enum drm_hotplug_helper_event {
+	/** @DRM_HOTPLUG_HELPER_CONNECTOR_ADDED: A drm_connector has just been added */
+	DRM_HOTPLUG_HELPER_CONNECTOR_ADDED,
+	/** @DRM_HOTPLUG_HELPER_CONNECTOR_REMOVING: A drm_connector is about to be removed */
+	DRM_HOTPLUG_HELPER_CONNECTOR_REMOVING,
+};
+
+/**
+ * drm_hotplug_helper_event_cb - Callback to report connector addition and removal.
+ */
+typedef void (*drm_hotplug_helper_event_cb)(enum drm_hotplug_helper_event event,
+					    struct drm_connector *connector);
+
+struct drm_hotplug_helper *drmm_hotplug_helper_init(struct drm_encoder *encoder,
+						    drm_hotplug_helper_event_cb event_cb,
+						    struct device_node *next_bridge_ep);
+
+#endif /* __DRM_HOTPLUG_HELPER_H__ */
Signature · 1 line
2.56.1
#47 lore raw

Re: [PATCH v4 31/32] drm: drm_hotplug_helper: new helper to implement bridge hotplug

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [High] drm: invalid direct attach() call in drm_hotplug_helper_try_complete() - [Medium] drm…

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider:
- [High] drm: invalid direct attach() call in drm_hotplug_helper_try_complete()
- [Medium] drm: drmm_hotplug_helper_init() returns NULL on success
- [Medium] drm: race with bridge detach in drm_hotplug_helper_try_complete()
- [Low] drm: missing forward declarations and broken kdoc in drm_hotplug_helper.h
- [Low] drm: MAINTAINERS hotplug helper entry claims all bridge DT bindings
--

commit 965c3e7b932068825241acd70d8d77673075709c
Author: Luca Ceresoli <[email protected]>

drm: drm_hotplug_helper: new helper to implement bridge hotplug

This commit adds drmm_hotplug_helper_init() to listen for DRM bridge hotplug events, attach the encoder's bridge chain, and dynamically add or remove a drm_bridge_connector as the pipeline completes or breaks.

Quoted 9 lines
MAINTAINERS +1 -0
index a5e2bf15e59f2..18ecb7bb39f40 100644
@@ -8219,6 +8219,14 @@ F:	Documentation/devicetree/bindings/npu/rockchip,rk3588-rknn-core.yaml
 F:	drivers/accel/rocket/
 F:	include/uapi/drm/rocket_accel.h
 
+DRM BRIDGE HOTPLUG HELPER

MAINTAINERS F: Documentation/devicetree/bindings/npu/rockchip,rk3588-rknn-core.yaml · line 8232

Quoted 10 lines
MAINTAINERS +7 -0
+M:	Luca Ceresoli <[email protected]>
+S:	Maintained
+T:	git https://gitlab.freedesktop.org/drm/misc/kernel.git
+F:	Documentation/devicetree/bindings/display/bridge/
+F:	drivers/gpu/drm/display/drm_hotplug_helper.c
+F:	include/drm/drm_hotplug_helper.h
+
 DRM COMPUTE ACCELERATORS DRIVERS AND FRAMEWORK
 M:	Oded Gabbay <[email protected]>
 L:	[email protected]

[Severity: Low]
Should the Documentation/devicetree/bindings/display/bridge/ line be dropped here? That path covers all bridge chip bindings (already listed under DRM DRIVERS FOR BRIDGE CHIPS), whereas drm_hotplug_helper is an in-kernel helper with no devicetree bindings of its own.

Quoted 5 lines
drivers/gpu/drm/display/drm_hotplug_helper.c new file +0 -0
new file mode 100644
index 0000000000000..ea32bdb0af8f3

[ ... ]

Quoted 7 lines
drivers/gpu/drm/display/drm_hotplug_helper.c new file +7 -0
+static void drm_hotplug_helper_try_complete(struct drm_hotplug_helper *hotplug_helper)
+{
+	struct device *dev = hotplug_helper->encoder->dev->dev;
+	int err;
+
+	WARN_ON(!mutex_is_locked(&hotplug_helper->event_mutex));
+

drivers/gpu/drm/display/drm_hotplug_helper.c line 161

Quoted 10 lines
drivers/gpu/drm/display/drm_hotplug_helper.c new file +10 -0
+	/*
+	 * drm_connector already present, the new bridge must be for
+	 * another card
+	 *
+	 */
+	if (hotplug_helper->connector)
+		return;
+
+	struct drm_bridge *last_bridge __free(drm_bridge_put) =
+		drm_bridge_chain_get_last_bridge(hotplug_helper->encoder);

[Severity: Medium]
Can drm_bridge_chain_get_last_bridge() race with concurrent bridge removal in drm_bridge_detach_from()?

drm_hotplug_helper_try_complete() (and drm_hotplug_helper_bridge_chain_is_complete()) reads encoder->bridge_chain under hotplug_helper->event_mutex without holding encoder->bridge_chain_mutex. Meanwhile, drm_bridge_remove() detaches bridges before notifying DRM_BRIDGE_DETACHED:

CPU0 CPU1
drm_bridge_remove(last_bridge)
drm_bridge_detach_from()

    lock(&encoder->bridge_chain_mutex)
                                      drm_hotplug_helper_try_complete()
                                        drm_bridge_chain_get_last_bridge()
                                          list_last_entry_or_null(...)
    list_move_tail(..., &tmplist)
    unlock(&encoder->bridge_chain_mutex)
    drm_bridge_detach(last_bridge)
      list_del_init(&chain_node)
                                        last_bridge->funcs->attach(...)
                                          drm_bridge_attach(..., last_bridge)
                                            list_add(&new->chain_node,
                                                     &last_bridge->chain_node)
                                                     <-- attaches to detached
                                                         bridge / corrupts
                                                         tmplist
      drm_hotplug_event_notifier_notify()
      last_bridge->dev = NULL
      last_bridge->encoder = NULL

Because drm_bridge_detach() only clears bridge->dev and bridge->encoder after the DRM_BRIDGE_DETACHED notification returns, drm_bridge_attach() with previous = last_bridge passes its validation checks and links the new bridge onto last_bridge->chain_node while last_bridge is on tmplist or already detached from encoder->bridge_chain.

drivers/gpu/drm/display/drm_hotplug_helper.c line 174

Quoted 13 lines
drivers/gpu/drm/display/drm_hotplug_helper.c new file +13 -0
+
+	if (last_bridge) {
+		/*
+		 * Some bridge(s) already in the chain, propagate the
+		 * attach call chain from the current chain tail to newly
+		 * hotplugged bridge(s)
+		 */
+		dev_dbg(dev, "propagate attach after %ps\n", last_bridge->funcs);
+
+		err = last_bridge->funcs->attach(last_bridge, hotplug_helper->encoder,
+						 DRM_BRIDGE_ATTACH_NO_CONNECTOR);
+		if (err)
+			return;

[Severity: High]
Is it valid to call last_bridge->funcs->attach() directly on a bridge that is already attached?

First, drm_bridge_funcs.attach is optional (drm_bridge_attach() checks if (bridge->funcs->attach)), so dereferencing last_bridge->funcs->attach unconditionally will cause a NULL pointer dereference if the tail bridge does not implement .attach.

Second, last_bridge is already attached to encoder->bridge_chain and has not been detached. Re-invoking its .attach callback on subsequent DRM_BRIDGE_ADDED or DRM_MIPI_DSI_ATTACHED notifications violates the 1:1 attach/detach lifecycle and re-runs per-bridge attach initialization on an already-initialized bridge. For example, ti_sn_bridge_attach() in drivers/gpu/drm/bridge/ti-sn65dsi86.c calls drm_dp_aux_register(&pdata->aux) before attaching pdata->next_bridge and unwinds with drm_dp_aux_unregister(&pdata->aux) if attaching next_bridge fails, so calling .attach() again attempts to re-register an already-registered DP AUX device and unregisters it on failure while the bridge remains attached.

[ ... ]

Quoted 4 lines
drivers/gpu/drm/display/drm_hotplug_helper.c new file +4 -0
+struct drm_hotplug_helper *drmm_hotplug_helper_init(struct drm_encoder *encoder,
+						    drm_hotplug_helper_event_cb event_cb,
+						    struct device_node *first_bridge_ep)
+{

[ ... ]

drivers/gpu/drm/display/drm_hotplug_helper.c line 325

Quoted 6 lines
drivers/gpu/drm/display/drm_hotplug_helper.c new file +6 -0
+	guard(mutex)(&hotplug_helper->event_mutex);
+	drm_hotplug_helper_try_complete(hotplug_helper);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(drmm_hotplug_helper_init);

[Severity: Medium]
Should drmm_hotplug_helper_init() return hotplug_helper instead of 0? Returning NULL on success contradicts the documented ERR_PTR() contract and causes callers checking !ret or IS_ERR_OR_NULL() to treat every successful initialization as a failure.

Quoted 26 lines
include/drm/drm_hotplug_helper.h new file +20 -0
new file mode 100644
index 0000000000000..ba14d547f8ff8
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2026 GE HealthCare
+ * Author: Luca Ceresoli <[email protected]>
+ */
+
+#ifndef __DRM_HOTPLUG_HELPER_H__
+#define __DRM_HOTPLUG_HELPER_H__
+
+/**
+ * enum drm_hotplug_helper_event - DRM hotplug helper connector events
+ */
+enum drm_hotplug_helper_event {
+	/** @DRM_HOTPLUG_HELPER_CONNECTOR_ADDED: A drm_connector has just been added */
+	DRM_HOTPLUG_HELPER_CONNECTOR_ADDED,
+	/** @DRM_HOTPLUG_HELPER_CONNECTOR_REMOVING: A drm_connector is about to be removed */
+	DRM_HOTPLUG_HELPER_CONNECTOR_REMOVING,
+};
+
+/**

include/drm/drm_hotplug_helper.h line 30

Quoted 10 lines
include/drm/drm_hotplug_helper.h new file +10 -0
+ * drm_hotplug_helper_event_cb - Callback to report connector addition and removal.
+ */
+typedef void (*drm_hotplug_helper_event_cb)(enum drm_hotplug_helper_event event,
+					    struct drm_connector *connector);
+
+struct drm_hotplug_helper *drmm_hotplug_helper_init(struct drm_encoder *encoder,
+						    drm_hotplug_helper_event_cb event_cb,
+						    struct device_node *next_bridge_ep);
+
+#endif /* __DRM_HOTPLUG_HELPER_H__ */

[Severity: Low]
Does include/drm/drm_hotplug_helper.h build cleanly under CONFIG_DRM_HEADER_TEST=y?

struct drm_connector, struct drm_encoder, and struct device_node are used in the parameter lists without forward declarations, causing -Wvisibility warnings when the header is compiled standalone. In addition, the kernel-doc comment for drm_hotplug_helper_event_cb is missing the typedef prefix ("typedef drm_hotplug_helper_event_cb - ...") and @event / @connector descriptions required by scripts/kernel-doc, and the third parameter of drmm_hotplug_helper_init() is named next_bridge_ep here instead of first_bridge_ep as in the definition.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=31
author reviewer
#48 lore raw

PATCH v4 32/32 drm/mxsfb/lcdif: enable bridge hotplug

In reply to Luca Ceresoli

Switch to the new drm_hotplug_helper() to handle bridge hotplugging:

* use the new drm_hotplug_helper to handle hotplug, including finding the first bridge and attaching the bridge chain
* use the connector add/remove callback to enable/disable HPD on added/removed connectors
* move drm_kms_helper_poll_init() before creating the drm_hotplug_helper so that HPD polling is initialized before connectors can be hotplugged

---

Changes in v4:
- fix UAF when hot-plugged connectors implement HPD by using the new per-connector HPD enable/disable API

Changes in v3:
- Adapt to the v3 version of the drm_hotplug_helper, which also handles HPD

Changes in v2:
- Use the hotplug helper callback to enable HPD on the hotplugged connector

---
 drivers/gpu/drm/mxsfb/Kconfig     |  2 +-
 drivers/gpu/drm/mxsfb/lcdif_drv.c | 42 +++++++++++++++++++--------------------
 2 files changed, 22 insertions(+), 22 deletions(-)
drivers/gpu/drm/mxsfb/Kconfig +1 -1
index 31db7a824a93..ada9a9334ea4 100644
@@ -34,7 +34,7 @@ config DRM_IMX_LCDIF
 	select DRM_PANEL
 	select DRM_PANEL_BRIDGE
 	select DRM_DISPLAY_HELPER
-	select DRM_BRIDGE_CONNECTOR
+	select DRM_HOTPLUG_HELPER
 	help
 	  Choose this option if you have an LCDIFv3 LCD controller.
 	  Those devices are found in various i.MX SoC (i.MX8MP,
drivers/gpu/drm/mxsfb/lcdif_drv.c +21 -21

85 lines not on this page · raw message

Signature · 1 line
2.56.1
#49 lore raw

Re: [PATCH v4 32/32] drm/mxsfb/lcdif: enable bridge hotplug

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [High] drm/mxsfb/lcdif: missing connector when last bridge lacks get_next_bridge() - [High]…

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [High] drm/mxsfb/lcdif: missing connector when last bridge lacks get_next_bridge()
- [High] drm/mxsfb/lcdif: missing client hotplug notification in lcdif_hotplug_event()
- [High] drm: double bridge attach in drm_hotplug_helper_try_complete()
- [Medium] drm/mxsfb/lcdif: poll_init and drmm_hotplug_helper_init lifetime mismatch
--

commit d0177b7f727becda395d8ff3eb1f0fa798651116
Author: Luca Ceresoli <[email protected]>

drm/mxsfb/lcdif: enable bridge hotplug

This commit switches the i.MX LCDIF driver to drmm_hotplug_helper_init() to manage bridge attachment and dynamic connector creation, and moves drm_kms_helper_poll_init() earlier in lcdif_load() so per-connector HPD polling can be toggled from the hotplug callback.

Quoted 4 lines
drivers/gpu/drm/mxsfb/lcdif_drv.c +0 -0
index 2be5a76e936f6..591653b39310c 100644

[ ... ]

Quoted 6 lines
drivers/gpu/drm/mxsfb/lcdif_drv.c +2 -0
@@ -44,6 +44,19 @@ static const struct drm_mode_config_helper_funcs lcdif_mode_config_helpers = {
 	.atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
 };
 
+static void lcdif_hotplug_event(enum drm_hotplug_helper_event event,
+				struct drm_connector *connector)

drivers/gpu/drm/mxsfb/lcdif_drv.c static const struct drm_mode_config_helper_funcs lcdif_mode_config_helpers = { · line 58

Quoted 10 lines
drivers/gpu/drm/mxsfb/lcdif_drv.c +10 -0
+{
+	switch (event) {
+	case DRM_HOTPLUG_HELPER_CONNECTOR_ADDED:
+		drm_kms_helper_poll_enable_connector(connector);
+	break;
+	case DRM_HOTPLUG_HELPER_CONNECTOR_REMOVING:
+		drm_kms_helper_poll_disable_connector(connector);
+		break;
+	}
+}

[Severity: High]
Does lcdif_hotplug_event() miss notifying in-kernel DRM clients and running initial HPD detection when a connector is added after lcdif_load()?

With drmm_hotplug_helper_init(), lcdif_attach_bridge() no longer defers probe while waiting for downstream bridges, so drm_helper_hpd_irq_event() and drm_client_setup() can run before any connector exists. When the bridge chain completes later, drm_bridge_connector_dynamic_add() registers the connector (emitting a sysfs uevent without calling drm_client_dev_hotplug()), and drm_kms_helper_poll_enable_connector() does not schedule output_poll_work for fixed panels (polled == 0) or HPD-IRQ connectors (polled == DRM_CONNECTOR_POLL_HPD). As a result, fbdev/fbcon is never set up and HPD-IRQ connectors stay in connector_status_unknown.

[ ... ]

Quoted 10 lines
drivers/gpu/drm/mxsfb/lcdif_drv.c +0 -6
@@ -80,16 +86,10 @@ static int lcdif_attach_bridge(struct lcdif_drm_private *lcdif)
 
 		encoder->possible_crtcs = drm_crtc_mask(&lcdif->crtc);
 
-		ret = drm_bridge_attach(encoder, bridge, NULL, DRM_BRIDGE_ATTACH_NO_CONNECTOR);
-		if (ret)
-			return dev_err_probe(dev, ret,
-					     "Failed to attach bridge for endpoint%u\n",
-					     of_ep.id);
-

drivers/gpu/drm/mxsfb/lcdif_drv.c static int lcdif_attach_bridge(struct lcdif_drm_private *lcdif) · line 94

Quoted 10 lines
drivers/gpu/drm/mxsfb/lcdif_drv.c +4 -4
-		connector = drm_bridge_connector_init(lcdif->drm, encoder);
-		if (IS_ERR(connector))
-			return dev_err_probe(dev, PTR_ERR(connector),
-					     "Failed to init bridge_connector for endpoint%u\n",
+		hotplug_helper = drmm_hotplug_helper_init(encoder, lcdif_hotplug_event, ep);
+		if (IS_ERR(hotplug_helper))
+			return dev_err_probe(dev, PTR_ERR(hotplug_helper),
+					     "Failed to init hotplug_helper for endpoint%u\n",
 					     of_ep.id);
 	}

[Severity: High]
Will drmm_hotplug_helper_init() fail to create a connector on LCDIF pipelines whose last bridge does not implement DRM_BRIDGE_OP_GET_NEXT_BRIDGE?

Previously, lcdif_attach_bridge() unconditionally created the connector with drm_bridge_connector_init() after drm_bridge_attach(). Now connector creation in drm_hotplug_helper_try_complete() is gated on drm_hotplug_helper_bridge_chain_is_complete(), which calls drm_bridge_get_next() on the last bridge and only returns true when that returns NULL. On i.MX8MP boards using tc358767 as a DisplayPort terminal bridge (such as imx8mp-dhcom-pdk3-overlay-732-100-x36.dtso), tc358767 does not set DRM_BRIDGE_OP_GET_NEXT_BRIDGE, so drm_bridge_get_next() warns and returns ERR_PTR(-ENOENT) and the connector is never created.

[Severity: High]
Can switching to drmm_hotplug_helper_init() cause last_bridge->funcs->attach() to be called a second time on an already-attached bridge?

When a downstream bridge in a multi-bridge chain is removed (or when the terminal bridge lacks DRM_BRIDGE_OP_GET_NEXT_BRIDGE so hotplug_helper->connector stays NULL), the upstream bridge remains attached as last_bridge in encoder->bridge_chain. On the next DRM_BRIDGE_ADDED or DRM_MIPI_DSI_ATTACHED notification, drm_hotplug_helper_try_complete() hits the if (last_bridge) branch:

drivers/gpu/drm/display/drm_hotplug_helper.c:drm_hotplug_helper_try_complete() {
    ...
	if (last_bridge) {
		dev_dbg(dev, "propagate attach after %ps\n", last_bridge->funcs);

		err = last_bridge->funcs->attach(last_bridge, hotplug_helper->encoder,
						 DRM_BRIDGE_ATTACH_NO_CONNECTOR);
    ...
}

Besides dereferencing last_bridge->funcs->attach without a NULL check, re-invoking funcs->attach() on an already-attached bridge (such as ti-sn65dsi86 or tc358767, which call drm_dp_aux_register() in their attach callbacks before attaching the next bridge) re-registers an initialized drm_dp_aux i2c adapter kobject and unregisters the active DP AUX channel if downstream attachment fails.

drivers/gpu/drm/mxsfb/lcdif_drv.c static int lcdif_load(struct drm_device *drm) · line 183

Quoted 9 lines
drivers/gpu/drm/mxsfb/lcdif_drv.c +2 -0
@@ -176,6 +176,8 @@ static int lcdif_load(struct drm_device *drm)
 	/* Start with vertical blanking interrupt reporting disabled. */
 	drm_crtc_vblank_off(&lcdif->crtc);
 
+	drm_kms_helper_poll_init(drm);
+
 	ret = lcdif_attach_bridge(lcdif);
 	if (ret)
 		return ret;

[Severity: Medium]
Does pairing unmanaged drm_kms_helper_poll_init() with drmm_hotplug_helper_init() leave polling uncleaned on probe failure and out of order with hotplug teardown on unbind?

If lcdif_attach_bridge(), drm_mode_config_create_initial_state() or IRQ setup fails in lcdif_load(), lcdif_probe() jumps to err_free without calling lcdif_unload() or drm_kms_helper_poll_fini(). On driver unbind, lcdif_unload() runs drm_kms_helper_poll_fini() (setting poll_enabled = false) before final drm_dev_put() releases drmm_hotplug_helper_init(); if userspace holds an open DRM fd across unbind, a bridge hotplug event before the fd closes will call lcdif_hotplug_event() -> drm_kms_helper_poll_enable_connector() and hit drm_WARN_ON_ONCE(dev, !dev->mode_config.poll_enabled). Using drmm_kms_helper_poll_init() before lcdif_attach_bridge() and dropping drm_kms_helper_poll_fini() from lcdif_unload() would align both lifetimes.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=32