Skip to contentSkip to conversation

drm/mediatek: fix probe resource leaks

PATCH In review Johan Hovold 3 participants 10 messages Sep 23, 2025 – Nov 17, 2025

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

cover drm/mediatek: fix probe resource leaks

This series fixes various probe resource leaks in the mediatek drm drivers that were found through inspection.

Johan

Johan Hovold (5):
drm/mediatek: fix probe resource leaks drm/mediatek: fix probe memory leak drm/mediatek: fix probe device leaks drm/mediatek: mtk_hdmi: fix probe device leaks drm/mediatek: ovl_adaptor: fix probe device leaks

 drivers/gpu/drm/mediatek/mtk_ddp_comp.c       | 33 ++++++++++++++++---
 drivers/gpu/drm/mediatek/mtk_ddp_comp.h       |  2 +-
 .../gpu/drm/mediatek/mtk_disp_ovl_adaptor.c   | 12 +++++++
 drivers/gpu/drm/mediatek/mtk_drm_drv.c        |  4 +--
 drivers/gpu/drm/mediatek/mtk_hdmi.c           | 15 +++++++++
 5 files changed, 58 insertions(+), 8 deletions(-)
Signature · 1 line
2.49.1
#1 lore raw

PATCH 1/5 drm/mediatek: fix probe resource leaks

Make sure to unmap and release the component iomap and clock on probe failure (e.g. probe deferral) and on driver unbind.

Note that unlike of_iomap(), devm_of_iomap() also checks whether the region is already mapped.

Fixes 119f5173628a ("drm/mediatek: Add DRM Driver for Mediatek SoC MT8173.")
---
 drivers/gpu/drm/mediatek/mtk_ddp_comp.c | 20 ++++++++++++++++----
 drivers/gpu/drm/mediatek/mtk_ddp_comp.h |  2 +-
 drivers/gpu/drm/mediatek/mtk_drm_drv.c  |  4 ++--
 3 files changed, 19 insertions(+), 7 deletions(-)
drivers/gpu/drm/mediatek/mtk_ddp_comp.c +16 -4
index ac6620e10262..0264017806ad 100644
@@ -621,15 +621,20 @@ int mtk_find_possible_crtcs(struct drm_device *drm, struct device *dev)
 	return ret;
 }
 
-int mtk_ddp_comp_init(struct device_node *node, struct mtk_ddp_comp *comp,
+static void mtk_ddp_comp_clk_put(void *_clk)
+{
+	struct clk *clk = _clk;
+
+	clk_put(clk);
+}
+
+int mtk_ddp_comp_init(struct device *dev, struct device_node *node, struct mtk_ddp_comp *comp,
 		      unsigned int comp_id)
 {
 	struct platform_device *comp_pdev;
 	enum mtk_ddp_comp_type type;
 	struct mtk_ddp_comp_dev *priv;
-#if IS_REACHABLE(CONFIG_MTK_CMDQ)
 	int ret;
-#endif
 
 	if (comp_id >= DDP_COMPONENT_DRM_ID_MAX)
 		return -EINVAL;
@@ -670,11 +675,18 @@ int mtk_ddp_comp_init(struct device_node *node, struct mtk_ddp_comp *comp,
 	if (!priv)
 		return -ENOMEM;
 
-	priv->regs = of_iomap(node, 0);
+	priv->regs = devm_of_iomap(dev, node, 0, NULL);
+	if (IS_ERR(priv->regs))
+		return PTR_ERR(priv->regs);
+
 	priv->clk = of_clk_get(node, 0);
 	if (IS_ERR(priv->clk))
 		return PTR_ERR(priv->clk);
 
+	ret = devm_add_action_or_reset(dev, mtk_ddp_comp_clk_put, priv->clk);
+	if (ret)
+		return ret;
+
 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
 	ret = cmdq_dev_get_client_reg(comp->dev, &priv->cmdq_reg, 0);
 	if (ret)
drivers/gpu/drm/mediatek/mtk_ddp_comp.h +1 -1
index 7289b3dcf22f..3f3d43f4330d 100644
@@ -350,7 +350,7 @@ static inline void mtk_ddp_comp_encoder_index_set(struct mtk_ddp_comp *comp)
 int mtk_ddp_comp_get_id(struct device_node *node,
 			enum mtk_ddp_comp_type comp_type);
 int mtk_find_possible_crtcs(struct drm_device *drm, struct device *dev);
-int mtk_ddp_comp_init(struct device_node *comp_node, struct mtk_ddp_comp *comp,
+int mtk_ddp_comp_init(struct device *dev, struct device_node *comp_node, struct mtk_ddp_comp *comp,
 		      unsigned int comp_id);
 enum mtk_ddp_comp_type mtk_ddp_comp_get_type(unsigned int comp_id);
 void mtk_ddp_write(struct cmdq_pkt *cmdq_pkt, unsigned int value,
drivers/gpu/drm/mediatek/mtk_drm_drv.c +2 -2
index eb5537f0ac90..384b0510272c 100644
@@ -1133,7 +1133,7 @@ static int mtk_drm_probe(struct platform_device *pdev)
 							    (void *)private->mmsys_dev,
 							    sizeof(*private->mmsys_dev));
 		private->ddp_comp[DDP_COMPONENT_DRM_OVL_ADAPTOR].dev = &ovl_adaptor->dev;
-		mtk_ddp_comp_init(NULL, &private->ddp_comp[DDP_COMPONENT_DRM_OVL_ADAPTOR],
+		mtk_ddp_comp_init(dev, NULL, &private->ddp_comp[DDP_COMPONENT_DRM_OVL_ADAPTOR],
 				  DDP_COMPONENT_DRM_OVL_ADAPTOR);
 		component_match_add(dev, &match, compare_dev, &ovl_adaptor->dev);
 	}
@@ -1199,7 +1199,7 @@ static int mtk_drm_probe(struct platform_device *pdev)
 						   node);
 		}
 
-		ret = mtk_ddp_comp_init(node, &private->ddp_comp[comp_id], comp_id);
+		ret = mtk_ddp_comp_init(dev, node, &private->ddp_comp[comp_id], comp_id);
 		if (ret) {
 			of_node_put(node);
 			goto err_node;
Signature · 1 line
2.49.1
#2 lore raw

PATCH 2/5 drm/mediatek: fix probe memory leak

In reply to Johan Hovold

The Mediatek DRM driver allocates private data for components without a platform driver but as the lifetime is tied to each component device, the memory is never freed.

Tie the allocation lifetime to the DRM platform device so that the memory is released on probe failure (e.g. probe deferral) and when the driver is unbound.

Fixes c0d36de868a6 ("drm/mediatek: Move clk info from struct mtk_ddp_comp to sub driver private data")
---
 drivers/gpu/drm/mediatek/mtk_ddp_comp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/mediatek/mtk_ddp_comp.c +1 -1
index 0264017806ad..31d67a131c50 100644
@@ -671,7 +671,7 @@ int mtk_ddp_comp_init(struct device *dev, struct device_node *node, struct mtk_d
 	    type == MTK_DSI)
 		return 0;
 
-	priv = devm_kzalloc(comp->dev, sizeof(*priv), GFP_KERNEL);
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
 	if (!priv)
 		return -ENOMEM;
 
Signature · 1 line
2.49.1
#3 lore raw

PATCH 3/5 drm/mediatek: fix probe device leaks

In reply to Johan Hovold

Make sure to drop the reference taken to each component device during probe on probe failure (e.g. probe deferral) and on driver unbind.

Fixes 6ea6f8276725 ("drm/mediatek: Use correct device pointer to get CMDQ client register")
Cc Chun-Kuang Hu <[email protected]>
---
 drivers/gpu/drm/mediatek/mtk_ddp_comp.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/gpu/drm/mediatek/mtk_ddp_comp.c +11 -0
index 31d67a131c50..9672ea1f91a2 100644
@@ -621,6 +621,13 @@ int mtk_find_possible_crtcs(struct drm_device *drm, struct device *dev)
 	return ret;
 }
 
+static void mtk_ddp_comp_put_device(void *_dev)
+{
+	struct device *dev = _dev;
+
+	put_device(dev);
+}
+
 static void mtk_ddp_comp_clk_put(void *_clk)
 {
 	struct clk *clk = _clk;
@@ -656,6 +663,10 @@ int mtk_ddp_comp_init(struct device *dev, struct device_node *node, struct mtk_d
 	}
 	comp->dev = &comp_pdev->dev;
 
+	ret = devm_add_action_or_reset(dev, mtk_ddp_comp_put_device, comp->dev);
+	if (ret)
+		return ret;
+
 	if (type == MTK_DISP_AAL ||
 	    type == MTK_DISP_BLS ||
 	    type == MTK_DISP_CCORR ||
Signature · 1 line
2.49.1
#4 lore raw

PATCH 4/5 drm/mediatek: mtk_hdmi: fix probe device leaks

In reply to Johan Hovold

Make sure to drop the references to the DDC adapter and CEC device taken during probe on probe failure (e.g. probe deferral) and on driver unbind.

Fixes 8f83f26891e1 ("drm/mediatek: Add HDMI support")
Cc Jie Qiu <[email protected]>
---
 drivers/gpu/drm/mediatek/mtk_hdmi.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
drivers/gpu/drm/mediatek/mtk_hdmi.c +15 -0
index b766dd5e6c8d..306e2c907311 100644
@@ -1345,6 +1345,13 @@ static const struct drm_bridge_funcs mtk_hdmi_bridge_funcs = {
 	.edid_read = mtk_hdmi_bridge_edid_read,
 };
 
+static void mtk_hdmi_put_device(void *_dev)
+{
+	struct device *dev = _dev;
+
+	put_device(dev);
+}
+
 static int mtk_hdmi_get_cec_dev(struct mtk_hdmi *hdmi, struct device *dev, struct device_node *np)
 {
 	struct platform_device *cec_pdev;
@@ -1369,6 +1376,10 @@ static int mtk_hdmi_get_cec_dev(struct mtk_hdmi *hdmi, struct device *dev, struc
 	}
 	of_node_put(cec_np);
 
+	ret = devm_add_action_or_reset(dev, mtk_hdmi_put_device, &cec_pdev->dev);
+	if (ret)
+		return ret;
+
 	/*
 	 * The mediatek,syscon-hdmi property contains a phandle link to the
 	 * MMSYS_CONFIG device and the register offset of the HDMI_SYS_CFG
@@ -1423,6 +1434,10 @@ static int mtk_hdmi_dt_parse_pdata(struct mtk_hdmi *hdmi,
 	if (!hdmi->ddc_adpt)
 		return dev_err_probe(dev, -EINVAL, "Failed to get ddc i2c adapter by node\n");
 
+	ret = devm_add_action_or_reset(dev, mtk_hdmi_put_device, &hdmi->ddc_adpt->dev);
+	if (ret)
+		return ret;
+
 	ret = mtk_hdmi_get_cec_dev(hdmi, dev, np);
 	if (ret)
 		return ret;
Signature · 1 line
2.49.1
#5 lore raw

PATCH 5/5 drm/mediatek: ovl_adaptor: fix probe device leaks

In reply to Johan Hovold

Make sure to drop the references taken to the component devices by of_find_device_by_node() during probe on probe failure (e.g. probe deferral) and on driver unbind.

Fixes 453c3364632a ("drm/mediatek: Add ovl_adaptor support for MT8195")
Cc Nancy.Lin <[email protected]>
---
 drivers/gpu/drm/mediatek/mtk_disp_ovl_adaptor.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/gpu/drm/mediatek/mtk_disp_ovl_adaptor.c +12 -0
index fe97bb97e004..c0af3e3b51d5 100644
@@ -527,6 +527,13 @@ bool mtk_ovl_adaptor_is_comp_present(struct device_node *node)
 	       type == OVL_ADAPTOR_TYPE_PADDING;
 }
 
+static void ovl_adaptor_put_device(void *_dev)
+{
+	struct device *dev = _dev;
+
+	put_device(dev);
+}
+
 static int ovl_adaptor_comp_init(struct device *dev, struct component_match **match)
 {
 	struct mtk_disp_ovl_adaptor *priv = dev_get_drvdata(dev);
@@ -560,6 +567,11 @@ static int ovl_adaptor_comp_init(struct device *dev, struct component_match **ma
 		if (!comp_pdev)
 			return -EPROBE_DEFER;
 
+		ret = devm_add_action_or_reset(dev, ovl_adaptor_put_device,
+					       &comp_pdev->dev);
+		if (ret)
+			return ret;
+
 		priv->ovl_adaptor_comp[id] = &comp_pdev->dev;
 
 		drm_of_component_match_add(dev, match, component_compare_of, node);
Signature · 1 line
2.49.1
#7 lore raw
In reply to Johan Hovold
Quoted 2 lines · Johan Hovold

This series fixes various probe resource leaks in the mediatek drm drivers that were found through inspection.

Can these be picked up (for 6.19) as well?

Johan

#8 lore raw

Hi Chun-Kuang and Philipp,

Quoted 5 lines · Johan Hovold
Quoted 2 lines · Johan Hovold

This series fixes various probe resource leaks in the mediatek drm drivers that were found through inspection.

Can these be picked up (for 6.19) as well?

It's been another three weeks and we're at rc6 so sending another reminder. Can these please be picked up for 6.19?

Johan

maintainer
#9 lore raw

Hi, Johan:

Johan Hovold <[email protected]> 於 2025年11月17日 週一 上午10:33寫道:

Quoted 4 lines

Hi Chun-Kuang and Philipp,

On Mon, Oct 27, 2025 at 02:34:58PM +0100, Johan Hovold wrote:

Quoted 8 lines
Quoted 5 lines · Johan Hovold
Quoted 2 lines

This series fixes various probe resource leaks in the mediatek drm drivers that were found through inspection.

Can these be picked up (for 6.19) as well?

It's been another three weeks and we're at rc6 so sending another reminder. Can these please be picked up for 6.19?

For the whole series, applied to mediatek-drm-next [1], thanks.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/chunkuang.hu/linux.git/log/?h=mediatek-drm-next

Regards,
Chun-Kuang.

Quoted 2 lines

Johan