Skip to contentSkip to conversation

media: mediatek: fix VPU device leaks on probe

PATCH Superseded Johan Hovold 3 participants 9 messages Sep 24, 2025 – Oct 28, 2025

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover media: mediatek: fix VPU device leaks on probe

This series fixes VPU device leaks during probe of the mdp and two codec drivers.

Included is also a minor documentation update to make it clear that the VPU lookup helper returns the device with an incremented refcount.

Johan

Johan Hovold (3):
media: mediatek: vcodec: fix device leak on codec init
media: mediatek: mdp: fix device leak on probe
media: mediatek: amend vpu_get_plat_device() documentation

 drivers/media/platform/mediatek/mdp/mtk_mdp_core.c  |  7 +++++--
 .../mediatek/vcodec/common/mtk_vcodec_fw_vpu.c      | 13 +++++++++++++
 drivers/media/platform/mediatek/vpu/mtk_vpu.h       |  2 +-
 3 files changed, 19 insertions(+), 3 deletions(-)
Signature · 1 line
2.49.1
#1 lore raw

PATCH 1/3 media: mediatek: vcodec: fix device leak on codec init

Make sure to drop the reference taken when looking up the VPU firmware device during codec init on probe failure (e.g. probe deferral) and on driver unbind.

Fixes 590577a4e525 ("[media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver")
Fixes 4e855a6efa54 ("[media] vcodec: mediatek: Add Mediatek V4L2 Video Encoder Driver")
Cc Tiffany Lin <[email protected]>
---
 .../mediatek/vcodec/common/mtk_vcodec_fw_vpu.c      | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vpu.c +13 -0
index d7027d600208..9237738d7632 100644
@@ -83,12 +83,20 @@ static const struct mtk_vcodec_fw_ops mtk_vcodec_vpu_msg = {
 	.release = mtk_vcodec_vpu_release,
 };
 
+static void mtk_vcodec_vpu_put_device(void *_dev)
+{
+	struct device *dev = _dev;
+
+	put_device(dev);
+}
+
 struct mtk_vcodec_fw *mtk_vcodec_fw_vpu_init(void *priv, enum mtk_vcodec_fw_use fw_use)
 {
 	struct platform_device *fw_pdev;
 	struct platform_device *plat_dev;
 	struct mtk_vcodec_fw *fw;
 	enum rst_id rst_id;
+	int ret;
 
 	if (fw_use == ENCODER) {
 		struct mtk_vcodec_enc_dev *enc_dev = priv;
@@ -111,6 +119,11 @@ struct mtk_vcodec_fw *mtk_vcodec_fw_vpu_init(void *priv, enum mtk_vcodec_fw_use
 		return ERR_PTR(-EINVAL);
 	}
 
+	ret = devm_add_action_or_reset(&plat_dev->dev, mtk_vcodec_vpu_put_device,
+				       &fw_pdev->dev);
+	if (ret)
+		return ERR_PTR(ret);
+
 	if (fw_use == DECODER)
 		vpu_wdt_reg_handler(fw_pdev, mtk_vcodec_vpu_reset_dec_handler, priv, rst_id);
 	else
Signature · 1 line
2.49.1
#2 lore raw

PATCH 2/3 media: mediatek: mdp: fix device leak on probe

In reply to Johan Hovold

Make sure to drop the reference taken when looking up the VPU firmware device during probe on probe failure (e.g. probe deferral) and on driver unbind.

Fixes c8eb2d7e8202 ("[media] media: Add Mediatek MDP Driver")
Cc Minghsiu Tsai <[email protected]>
---
 drivers/media/platform/mediatek/mdp/mtk_mdp_core.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/media/platform/mediatek/mdp/mtk_mdp_core.c +5 -2
index 80fdc6ff57e0..5c7dcf4090f4 100644
@@ -198,7 +198,7 @@ static int mtk_mdp_probe(struct platform_device *pdev)
 				  VPU_RST_MDP);
 	if (ret) {
 		dev_err(&pdev->dev, "Failed to register reset handler\n");
-		goto err_m2m_register;
+		goto err_put_vpu;
 	}
 
 	platform_set_drvdata(pdev, mdp);
@@ -206,7 +206,7 @@ static int mtk_mdp_probe(struct platform_device *pdev)
 	ret = vb2_dma_contig_set_max_seg_size(&pdev->dev, DMA_BIT_MASK(32));
 	if (ret) {
 		dev_err(&pdev->dev, "Failed to set vb2 dma mag seg size\n");
-		goto err_m2m_register;
+		goto err_put_vpu;
 	}
 
 	pm_runtime_enable(dev);
@@ -214,6 +214,8 @@ static int mtk_mdp_probe(struct platform_device *pdev)
 
 	return 0;
 
+err_put_vpu:
+	put_device(&mdp->vpu_dev->dev);
 err_m2m_register:
 	v4l2_device_unregister(&mdp->v4l2_dev);
 
@@ -241,6 +243,7 @@ static void mtk_mdp_remove(struct platform_device *pdev)
 	struct mtk_mdp_comp *comp, *comp_temp;
 
 	pm_runtime_disable(&pdev->dev);
+	put_device(&mdp->vpu_dev->dev);
 	vb2_dma_contig_clear_max_seg_size(&pdev->dev);
 	mtk_mdp_unregister_m2m_device(mdp);
 	v4l2_device_unregister(&mdp->v4l2_dev);
Signature · 1 line
2.49.1
#3 lore raw

PATCH 3/3 media: mediatek: amend vpu_get_plat_device() documentation

In reply to Johan Hovold

Add a comment to the vpu_get_plat_device() documentation to make it clear that the VPU platform device is returned with an incremented reference count (which needs to be dropped after use).

---
 drivers/media/platform/mediatek/vpu/mtk_vpu.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/mediatek/vpu/mtk_vpu.h +1 -1
index da05f3e74081..5808311d2b15 100644
@@ -120,7 +120,7 @@ int vpu_ipi_send(struct platform_device *pdev,
  *		device for using VPU API.
  *
  * Return: Return NULL if it is failed.
- * otherwise it is VPU's platform device
+ * otherwise it is VPU's platform device with incremented reference count
  **/
 struct platform_device *vpu_get_plat_device(struct platform_device *pdev);
 
Signature · 1 line
2.49.1
Reviewed-by on the v1 series
#4 lore raw
In reply to Johan Hovold
Quoted 7 lines · Johan Hovold

This series fixes VPU device leaks during probe of the mdp and two codec drivers.

Included is also a minor documentation update to make it clear that the VPU lookup helper returns the device with an incremented refcount.

Quoted 7 lines

Johan

Johan Hovold (3):
media: mediatek: vcodec: fix device leak on codec init
media: mediatek: mdp: fix device leak on probe
media: mediatek: amend vpu_get_plat_device() documentation

Whole series is

Quoted 10 lines
 drivers/media/platform/mediatek/mdp/mtk_mdp_core.c  |  7 +++++--
 .../mediatek/vcodec/common/mtk_vcodec_fw_vpu.c      | 13 +++++++++++++
 drivers/media/platform/mediatek/vpu/mtk_vpu.h       |  2 +-
 3 files changed, 19 insertions(+), 3 deletions(-)

--
2.49.1

#5 lore raw
In reply to Johan Hovold
Quoted 5 lines · Johan Hovold

This series fixes VPU device leaks during probe of the mdp and two codec drivers.

Included is also a minor documentation update to make it clear that the VPU lookup helper returns the device with an incremented refcount.

Can these be picked up for 6.19?

Johan

#6 lore raw

Hi,

Le lundi 27 octobre 2025 à 14:37 +0100, Johan Hovold a écrit :

Quoted 8 lines · Johan Hovold
Quoted 5 lines

This series fixes VPU device leaks during probe of the mdp and two codec drivers.

Included is also a minor documentation update to make it clear that the VPU lookup helper returns the device with an incremented refcount.

Can these be picked up for 6.19?

They are picked already, please check in the tree,

https://gitlab.freedesktop.org/linux-media/media-committers/-/commits/next

Nicolas

#7 lore raw
Quoted 5 lines · Nicolas Dufresne

Le lundi 27 octobre 2025 à 14:37 +0100, Johan Hovold a écrit :

Quoted 4 lines · Johan Hovold
Quoted 3 lines

This series fixes VPU device leaks during probe of the mdp and two codec drivers.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Included is also a minor documentation update to make it clear that the VPU lookup helper returns the device with an incremented refcount.

Can these be picked up for 6.19?

They are picked already, please check in the tree,

https://gitlab.freedesktop.org/linux-media/media-committers/-/commits/next

I can't seem to find them in that branch either, and they are not in linux-next.

(I seem to have trimmed my original message too much so that the patch summaries were not included in my reminder. Perhaps you are thinking of the related mtk-mdp3 fix?)

But I do see an incomplete attempt at a fix of the reference leak in that branch by someone else in commit cdd0f118ef87 ("media: mediatek:
vcodec: Fix a reference leak in mtk_vcodec_fw_vpu_init()")

Can that commit be dropped in favour of this series or shall I rebase on top of it?

Johan

#8 lore raw
Quoted 22 lines · Johan Hovold
Quoted 5 lines · Nicolas Dufresne

Le lundi 27 octobre 2025 à 14:37 +0100, Johan Hovold a écrit :

Quoted 4 lines · Johan Hovold
Quoted 3 lines

This series fixes VPU device leaks during probe of the mdp and two codec drivers.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Included is also a minor documentation update to make it clear that the VPU lookup helper returns the device with an incremented refcount.

Can these be picked up for 6.19?

They are picked already, please check in the tree,

https://gitlab.freedesktop.org/linux-media/media-committers/-/commits/next

I can't seem to find them in that branch either, and they are not in linux-next.

(I seem to have trimmed my original message too much so that the patch summaries were not included in my reminder. Perhaps you are thinking of the related mtk-mdp3 fix?)

Quoted 6 lines

But I do see an incomplete attempt at a fix of the reference leak in that branch by someone else in commit cdd0f118ef87 ("media: mediatek:
vcodec: Fix a reference leak in mtk_vcodec_fw_vpu_init()")

Can that commit be dropped in favour of this series or shall I rebase on top of it?

Scratch that. The merged commit is correct. I'll respin this series on top.

Johan