Skip to contentSkip to conversation

drm/bridge: display-connector: fix i2c adapter leak on probe failure

PATCH In review Johan Hovold 3 participants 5 messages Jul 17

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/bridge: display-connector: fix i2c adapter leak on probe failure

Make sure to drop the i2c adapter device and module references on failed regulator and enable gpio lookup (and regulator enable) during probe (e.g. on probe deferral).

Note that of_get_i2c_adapter_by_node() does not return error pointers, but i2c_put_adapter() can handle a NULL argument so the error pointer check at remove works even if it is misleading.

Fixes 2e2bf3a5584d ("drm/bridge: display-connector: add DP support")
Fixes 6de79dd3a920 ("drm/bridge: display-connector: add ddc-en gpio support")
Fixes 6eb6b6f0a012 ("drm/bridge: display-connector: handle hdmi-pwr supply")
Cc Tomi Valkeinen <[email protected]>
Cc H. Nikolaus Schaller <[email protected]>
Cc Dmitry Baryshkov <[email protected]>
Cc Laurent Pinchart <[email protected]>
---
 drivers/gpu/drm/bridge/display-connector.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/gpu/drm/bridge/display-connector.c +13 -3
index 16c0631adeb1..c38de5518211 100644
@@ -208,6 +208,13 @@ static int display_connector_get_supply(struct platform_device *pdev,
 	return PTR_ERR_OR_ZERO(conn->supply);
 }
 
+static void display_connector_put_ddc(void *_ddc)
+{
+	struct i2c_adapter *ddc = _ddc;
+
+	i2c_put_adapter(ddc);
+}
+
 static int display_connector_probe(struct platform_device *pdev)
 {
 	struct display_connector *conn;
@@ -328,6 +335,12 @@ static int display_connector_probe(struct platform_device *pdev)
 			of_node_put(phandle);
 			if (!conn->bridge.ddc)
 				return -EPROBE_DEFER;
+
+			ret = devm_add_action_or_reset(&pdev->dev,
+						       display_connector_put_ddc,
+						       conn->bridge.ddc);
+			if (ret)
+				return ret;
 		} else {
 			dev_dbg(&pdev->dev,
 				"No I2C bus specified, disabling EDID readout\n");
@@ -403,9 +416,6 @@ static void display_connector_remove(struct platform_device *pdev)
 		regulator_disable(conn->supply);
 
 	drm_bridge_remove(&conn->bridge);
-
-	if (!IS_ERR(conn->bridge.ddc))
-		i2c_put_adapter(conn->bridge.ddc);
 }
 
 static const struct of_device_id display_connector_match[] = {
Signature · 1 line
2.54.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Missing cancellation of scheduled work (conn->hpd_work) in the driver's remove path. --

--- Patch [1]: [PATCH] drm/bridge: display-connector: fix i2c adapter leak on probe failure --- commit 4febab97b0e18e24c294244f2f96e39bc66cf4b3
Author: Johan Hovold <[email protected]>

drm/bridge: display-connector: fix i2c adapter leak on probe failure

This commit ensures the i2c adapter device and module references are dropped on failed regulator and enable gpio lookup during probe by adding a devres action.

Quoted 4 lines
drivers/gpu/drm/bridge/display-connector.c +0 -0
index d1e74dd2e0518..8415992fcb106 100644

[ ... ]

drivers/gpu/drm/bridge/display-connector.c static void display_connector_remove(struct platform_device *pdev) · line 454

Quoted 4 lines
drivers/gpu/drm/bridge/display-connector.c +0 -0
@@ -439,9 +452,6 @@ static void display_connector_remove(struct platform_device *pdev)
 		regulator_disable(conn->supply);
 
 	drm_bridge_remove(&conn->bridge);

[Severity: High]
This isn't a bug introduced by this patch, but does display_connector_remove() need to cancel conn->hpd_work before returning?

If the display-connector module is unbound concurrently with the DRM master calling display_connector_hpd_enable(), the hpd_work work item is scheduled. Without an explicit cancel_work_sync(&conn->hpd_work) here in the remove path, could the work item execute after the devres subsystem has freed the conn structure? This would result in a use-after-free when the work item accesses the freed memory and calls drm_bridge_hpd_notify() on a removed bridge.

Quoted 4 lines
drivers/gpu/drm/bridge/display-connector.c +0 -3
-
-	if (!IS_ERR(conn->bridge.ddc))
-		i2c_put_adapter(conn->bridge.ddc);
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Johan Hovold

Hi Johan,

Thank you for the patch.

Quoted 7 lines · Johan Hovold

Make sure to drop the i2c adapter device and module references on failed regulator and enable gpio lookup (and regulator enable) during probe (e.g. on probe deferral).

Note that of_get_i2c_adapter_by_node() does not return error pointers, but i2c_put_adapter() can handle a NULL argument so the error pointer check at remove works even if it is misleading.

I've already sent a fix for this, see

https://lore.kernel.org/dri-devel/[email protected]/

Quoted 54 lines
Fixes 2e2bf3a5584d ("drm/bridge: display-connector: add DP support")
Fixes 6de79dd3a920 ("drm/bridge: display-connector: add ddc-en gpio support")
Fixes 6eb6b6f0a012 ("drm/bridge: display-connector: handle hdmi-pwr supply")
Cc Tomi Valkeinen <[email protected]>
Cc H. Nikolaus Schaller <[email protected]>
Cc Dmitry Baryshkov <[email protected]>
Cc Laurent Pinchart <[email protected]>
---
 drivers/gpu/drm/bridge/display-connector.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/gpu/drm/bridge/display-connector.c +13 -3
index 16c0631adeb1..c38de5518211 100644
@@ -208,6 +208,13 @@ static int display_connector_get_supply(struct platform_device *pdev,
 	return PTR_ERR_OR_ZERO(conn->supply);
 }
 
+static void display_connector_put_ddc(void *_ddc)
+{
+	struct i2c_adapter *ddc = _ddc;
+
+	i2c_put_adapter(ddc);
+}
+
 static int display_connector_probe(struct platform_device *pdev)
 {
 	struct display_connector *conn;
@@ -328,6 +335,12 @@ static int display_connector_probe(struct platform_device *pdev)
 			of_node_put(phandle);
 			if (!conn->bridge.ddc)
 				return -EPROBE_DEFER;
+
+			ret = devm_add_action_or_reset(&pdev->dev,
+						       display_connector_put_ddc,
+						       conn->bridge.ddc);
+			if (ret)
+				return ret;
 		} else {
 			dev_dbg(&pdev->dev,
 				"No I2C bus specified, disabling EDID readout\n");
@@ -403,9 +416,6 @@ static void display_connector_remove(struct platform_device *pdev)
 		regulator_disable(conn->supply);
 
 	drm_bridge_remove(&conn->bridge);
-
-	if (!IS_ERR(conn->bridge.ddc))
-		i2c_put_adapter(conn->bridge.ddc);
 }
 
 static const struct of_device_id display_connector_match[] = {
Signature · 3 lines
Regards,

Laurent Pinchart
#3 lore raw
Quoted 5 lines · Laurent Pinchart
Quoted 4 lines · Johan Hovold

Make sure to drop the i2c adapter device and module references on failed regulator and enable gpio lookup (and regulator enable) during probe (e.g. on probe deferral).

Quoted 7 lines
Quoted 3 lines

Note that of_get_i2c_adapter_by_node() does not return error pointers, but i2c_put_adapter() can handle a NULL argument so the error pointer check at remove works even if it is misleading.

I've already sent a fix for this, see

https://lore.kernel.org/dri-devel/[email protected]/

Ah, ok. If that series is blocked perhaps you can send the fix separately.

Johan

#4 lore raw
Quoted 10 lines · Johan Hovold
Quoted 9 lines · Laurent Pinchart
Quoted 7 lines · Johan Hovold

Make sure to drop the i2c adapter device and module references on failed regulator and enable gpio lookup (and regulator enable) during probe (e.g. on probe deferral).

Note that of_get_i2c_adapter_by_node() does not return error pointers, but i2c_put_adapter() can handle a NULL argument so the error pointer check at remove works even if it is misleading.

Quoted 6 lines
Quoted 3 lines

I've already sent a fix for this, see

https://lore.kernel.org/dri-devel/[email protected]/

Ah, ok. If that series is blocked perhaps you can send the fix separately.

Done.

https://lore.kernel.org/dri-devel/20260717184836.2017386-1-laurent.pinchart+renesas@ideasonboard.com

Signature · 3 lines
Regards,

Laurent Pinchart