Skip to contentSkip to conversation

drm/panel-edp: take i2c adapter module reference

PATCH In review Johan Hovold 3 participants 4 messages Jul 16 – Jul 17

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

PATCH 1/1 drm/panel-edp: take i2c adapter module reference

The i2c subsystem currently blocks during adapter deregistration whenever there are consumers holding a reference.

Switch to using of_get_i2c_adapter_by_node() which also takes a reference to the adapter module so that an attempt to unload the module while in use fails gracefully instead of blocking uninterruptibly.

---
 drivers/gpu/drm/panel/panel-edp.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/panel/panel-edp.c +3 -3
index aa27d6cd932e..1606b50513d1 100644
@@ -873,7 +873,7 @@ static int panel_edp_probe(struct device *dev, const struct panel_desc *desc,
 
 	ddc = of_parse_phandle(dev->of_node, "ddc-i2c-bus", 0);
 	if (ddc) {
-		panel->ddc = of_find_i2c_adapter_by_node(ddc);
+		panel->ddc = of_get_i2c_adapter_by_node(ddc);
 		of_node_put(ddc);
 
 		if (!panel->ddc)
@@ -938,7 +938,7 @@ static int panel_edp_probe(struct device *dev, const struct panel_desc *desc,
 	pm_runtime_disable(dev);
 err_finished_ddc_init:
 	if (panel->ddc && (!panel->aux || panel->ddc != &panel->aux->ddc))
-		put_device(&panel->ddc->dev);
+		i2c_put_adapter(panel->ddc);
 
 	return err;
 }
@@ -983,7 +983,7 @@ static void panel_edp_remove(struct device *dev)
 	pm_runtime_dont_use_autosuspend(dev);
 	pm_runtime_disable(dev);
 	if (panel->ddc && (!panel->aux || panel->ddc != &panel->aux->ddc))
-		put_device(&panel->ddc->dev);
+		i2c_put_adapter(panel->ddc);
 
 	drm_edid_free(panel->drm_edid);
 	panel->drm_edid = NULL;
Signature · 1 line
2.54.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] When the devicetree's `ddc-i2c-bus` property explicitly points to the DP AUX I2C adapter, the…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] When the devicetree's ddc-i2c-bus property explicitly points to the DP AUX I2C adapter, the module and device references acquired by of_get_i2c_adapter_by_node() are leaked during error cleanup and driver removal. --

--- Patch [1]: [PATCH] drm/panel-edp: take i2c adapter module reference --- commit 2c5917c76c861a6fb28736134884b2a3575fb31a
Author: Johan Hovold <[email protected]>

drm/panel-edp: take i2c adapter module reference

The i2c subsystem currently blocks during adapter deregistration whenever there are consumers holding a reference.

Switch to using of_get_i2c_adapter_by_node() which also takes a reference to the adapter module so that an attempt to unload the module while in use fails gracefully instead of blocking uninterruptibly.

Quoted 11 lines
drivers/gpu/drm/panel/panel-edp.c +1 -1
index f66474d3ef65e..a65bfe50be817 100644
@@ -874,7 +874,7 @@ static int panel_edp_probe(struct device *dev, const struct panel_desc *desc,
 
 	ddc = of_parse_phandle(dev->of_node, "ddc-i2c-bus", 0);
 	if (ddc) {
-		panel->ddc = of_find_i2c_adapter_by_node(ddc);
+		panel->ddc = of_get_i2c_adapter_by_node(ddc);
 		of_node_put(ddc);

[ ... ]

drivers/gpu/drm/panel/panel-edp.c static int panel_edp_probe(struct device *dev, const struct panel_desc *desc, · line 942

Quoted 6 lines
drivers/gpu/drm/panel/panel-edp.c +1 -1
@@ -939,7 +939,7 @@ static int panel_edp_probe(struct device *dev, const struct panel_desc *desc,
 	pm_runtime_disable(dev);
 err_finished_ddc_init:
 	if (panel->ddc && (!panel->aux || panel->ddc != &panel->aux->ddc))
-		put_device(&panel->ddc->dev);
+		i2c_put_adapter(panel->ddc);

[Severity: Medium]
If the devicetree's "ddc-i2c-bus" property explicitly points to the DP AUX I2C adapter node, panel->ddc will equal &panel->aux->ddc.

While the check panel->ddc != &panel->aux->ddc was already present, switching to of_get_i2c_adapter_by_node() means a module reference is now acquired here if the node is parsed from the devicetree.

Does skipping i2c_put_adapter() in this case cause a module reference leak when an error occurs during probe?

drivers/gpu/drm/panel/panel-edp.c static void panel_edp_remove(struct device *dev) · line 987

Quoted 9 lines
drivers/gpu/drm/panel/panel-edp.c +1 -1
 	return err;
 }
@@ -984,7 +984,7 @@ static void panel_edp_remove(struct device *dev)
 	pm_runtime_dont_use_autosuspend(dev);
 	pm_runtime_disable(dev);
 	if (panel->ddc && (!panel->aux || panel->ddc != &panel->aux->ddc))
-		put_device(&panel->ddc->dev);
+		i2c_put_adapter(panel->ddc);

[Severity: Medium]
Similarly, does this skip i2c_put_adapter() and leak the module reference during driver removal if the devicetree explicitly pointed to the AUX DDC adapter?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
reviewer Reviewed-by on v1 1/1
#2 lore raw
In reply to Johan Hovold

Hi,

Quoted 4 lines · Johan Hovold

The i2c subsystem currently blocks during adapter deregistration whenever there are consumers holding a reference.

Quoted 8 lines

Switch to using of_get_i2c_adapter_by_node() which also takes a reference to the adapter module so that an attempt to unload the module while in use fails gracefully instead of blocking uninterruptibly.

---
 drivers/gpu/drm/panel/panel-edp.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Worth adding any "Fixes:" tag? I guess this has been using of_find_i2c_adapter_by_node() for a long time...

Sashiko-bot review [1] seems right, even though it's pointing out something that isn't really a new problem and is very much a corner case where someone is crafting an intentionally ridiculous device tree. If you agree, I wouldn't mind a followup patch fixing that, too. ;-)

[1] https://lore.kernel.org/r/[email protected]/

#3 lore raw

Hi Doug,

Quoted 14 lines · Doug Anderson
Quoted 4 lines · Johan Hovold

The i2c subsystem currently blocks during adapter deregistration whenever there are consumers holding a reference.

Quoted 8 lines

Switch to using of_get_i2c_adapter_by_node() which also takes a reference to the adapter module so that an attempt to unload the module while in use fails gracefully instead of blocking uninterruptibly.

---
 drivers/gpu/drm/panel/panel-edp.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
Quoted 4 lines

Worth adding any "Fixes:" tag? I guess this has been using of_find_i2c_adapter_by_node() for a long time...

Yeah, this comes from panel-simple which has had this issue since it was merged in 2013. And that's a couple of years before the helper taking a module reference even existed.

I decided not to treat these as individual driver bugs, but perhaps the instances added after the helper was merged could have had a Fixes tag.

Quoted 7 lines

Sashiko-bot review [1] seems right, even though it's pointing out something that isn't really a new problem and is very much a corner case where someone is crafting an intentionally ridiculous device tree. If you agree, I wouldn't mind a followup patch fixing that, too. ;-)

[1] https://lore.kernel.org/r/[email protected]/

Right, I saw that one too but wrote it off as a preexisting issue.

I'll send a v2 of this one rebased on a fix for that instead.

Johan