Skip to contentSkip to conversation

media: tree-wide: Add missing media_entity_cleanup() calls

PATCH In review Biren Pandya 3 participants 25 messages Jun 14 – Jul 21

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

v3 cover media: tree-wide: Add missing media_entity_cleanup() calls

This series fixes a widespread API violation across the media subsystem.

The media_entity_pads_init() function initializes the media entity, but many drivers forget to call media_entity_cleanup() in their error paths and remove functions.

This series adds the missing cleanup calls to prevent potential memory leaks and ensure proper resource management.

Fixes tags have been carefully audited. Tags older than 2017 or pointing to directory rename commits have been intentionally omitted to avoid spamming the stable trees for ancient/moved drivers where the bug predates the EOL LTS kernels.

Generated and validated automatically.

Biren Pandya (23):
media: i2c: cx25840-core: Add missing media_entity_cleanup()
media: i2c: max9286: Add missing media_entity_cleanup()
media: i2c: msp3400-driver: Add missing media_entity_cleanup()
media: i2c: mt9v011: Add missing media_entity_cleanup()
media: i2c: saa7115: Add missing media_entity_cleanup()
media: platform: stm32-csi: Add missing media_entity_cleanup()
media: platform: sun4i_csi: Add missing media_entity_cleanup()
media: platform: microchip-isc-scaler: Add missing
media_entity_cleanup()
media: platform: cdns-csi2tx: Add missing media_entity_cleanup()
media: platform: rcar-csi2: Add missing media_entity_cleanup()
media: platform: csisp: Add missing media_entity_cleanup()
media: platform: rcar-core: Add missing media_entity_cleanup()
media: platform: rzg2l-core: Add missing media_entity_cleanup()
media: platform: cfe: Add missing media_entity_cleanup()
media: platform: j721e-csi2rx: Add missing media_entity_cleanup()
media: platform: hantro_drv: Add missing media_entity_cleanup()
media: pci: saa7134: Add missing media_entity_cleanup()
media: usb: cx231xx: Add missing media_entity_cleanup()
media: usb: em28xx: Add missing media_entity_cleanup()
media: usb: au0828: Add missing media_entity_cleanup()
media: usb: dvb-usb-v2: Add missing media_entity_cleanup()
media: dvb-frontends: au8522_decoder: Add missing
media_entity_cleanup()

 drivers/media/dvb-frontends/au8522_decoder.c  |  2 ++
 drivers/media/i2c/cx25840/cx25840-core.c      |  2 ++
 drivers/media/i2c/max9286.c                   |  5 ++++-
 drivers/media/i2c/msp3400-driver.c            |  2 ++
 drivers/media/i2c/mt9v011.c                   |  3 +++
 drivers/media/i2c/saa7115.c                   |  2 ++
 drivers/media/pci/saa7134/saa7134-core.c      | 18 ++++++++++++----
 drivers/media/platform/cadence/cdns-csi2tx.c  |  5 ++++-
 .../platform/microchip/microchip-isc-base.c   |  1 +
 .../platform/microchip/microchip-isc-scaler.c |  7 +++++++
 .../media/platform/microchip/microchip-isc.h  |  1 +
 .../media/platform/raspberrypi/rp1-cfe/cfe.c  |  2 ++
 drivers/media/platform/renesas/rcar-csi2.c    |  2 ++
 .../media/platform/renesas/rcar-isp/csisp.c   |  2 ++
 .../platform/renesas/rcar-vin/rcar-core.c     |  2 ++
 .../platform/renesas/rzg2l-cru/rzg2l-core.c   |  2 ++
 drivers/media/platform/st/stm32/stm32-csi.c   |  2 ++
 .../platform/sunxi/sun4i-csi/sun4i_csi.c      | 10 +++++++--
 .../platform/ti/j721e-csi2rx/j721e-csi2rx.c   |  6 +++++-
 .../media/platform/verisilicon/hantro_drv.c   | 12 ++++++++---
 drivers/media/usb/au0828/au0828-video.c       | 10 ++++++++-
 drivers/media/usb/cx231xx/cx231xx-video.c     |  8 ++++++-
 drivers/media/usb/dvb-usb-v2/mxl111sf.c       | 21 ++++++++++++++++++-
 drivers/media/usb/em28xx/em28xx-video.c       | 13 ++++++++++--
 24 files changed, 123 insertions(+), 17 deletions(-)
Signature · 1 line
2.50.1 (Apple Git-155)
#1 lore raw

PATCH v3 1/22 media: i2c: cx25840-core: Add missing media_entity_cleanup()

The media_entity_pads_init() function initializes the media entity, but the driver forgets to call media_entity_cleanup() in the error paths and remove function. Add the missing calls to fix the API violation and prevent potential future memory leaks.

---
 drivers/media/i2c/cx25840/cx25840-core.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/media/i2c/cx25840/cx25840-core.c +2 -0
index 69d5cc648c0f..a3d7473854a3 100644
@@ -3962,6 +3962,7 @@ static int cx25840_probe(struct i2c_client *client)
 		int err = state->hdl.error;
 
 		v4l2_ctrl_handler_free(&state->hdl);
+		media_entity_cleanup(&sd->entity);
 		return err;
 	}
 	if (!is_cx2583x(state))
@@ -3986,6 +3987,7 @@ static void cx25840_remove(struct i2c_client *client)
 	cx25840_ir_remove(sd);
 	v4l2_device_unregister_subdev(sd);
 	v4l2_ctrl_handler_free(&state->hdl);
+	media_entity_cleanup(&sd->entity);
 }
 
 static const struct i2c_device_id cx25840_id[] = {
Signature · 1 line
2.50.1 (Apple Git-155)
#2 lore raw

PATCH v3 2/22 media: i2c: max9286: Add missing media_entity_cleanup()

In reply to Biren Pandya

The media_entity_pads_init() function initializes the media entity, but the driver forgets to call media_entity_cleanup() in the error paths and remove function. Add the missing calls to fix the API violation and prevent potential future memory leaks.

Fixes 66d8c9d2422d ("media: i2c: Add MAX9286 driver")
---
 drivers/media/i2c/max9286.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/media/i2c/max9286.c +4 -1
index ac0712ce1e65..31598fa34c6c 100644
@@ -1062,7 +1062,7 @@ static int max9286_v4l2_register(struct max9286_priv *priv)
 	priv->sd.state_lock = priv->ctrls.lock;
 	ret = v4l2_subdev_init_finalize(&priv->sd);
 	if (ret)
-		goto err_async;
+		goto err_entity;
 
 	ret = v4l2_async_register_subdev(&priv->sd);
 	if (ret < 0) {
@@ -1074,6 +1074,8 @@ static int max9286_v4l2_register(struct max9286_priv *priv)
 
 err_subdev:
 	v4l2_subdev_cleanup(&priv->sd);
+err_entity:
+	media_entity_cleanup(&priv->sd.entity);
 err_async:
 	v4l2_ctrl_handler_free(&priv->ctrls);
 	max9286_v4l2_notifier_unregister(priv);
@@ -1087,6 +1089,7 @@ static void max9286_v4l2_unregister(struct max9286_priv *priv)
 	v4l2_ctrl_handler_free(&priv->ctrls);
 	v4l2_async_unregister_subdev(&priv->sd);
 	max9286_v4l2_notifier_unregister(priv);
+	media_entity_cleanup(&priv->sd.entity);
 }
 
 /* -----------------------------------------------------------------------------
Signature · 1 line
2.50.1 (Apple Git-155)
#3 lore raw

PATCH v3 3/22 media: i2c: msp3400-driver: Add missing media_entity_cleanup()

In reply to Biren Pandya

The media_entity_pads_init() function initializes the media entity, but the driver forgets to call media_entity_cleanup() in the error paths and remove function. Add the missing calls to fix the API violation and prevent potential future memory leaks.

---
 drivers/media/i2c/msp3400-driver.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/media/i2c/msp3400-driver.c +2 -0
index 4c0b0ad68c08..d977a6039e6c 100644
@@ -812,6 +812,7 @@ static int msp_probe(struct i2c_client *client)
 		int err = hdl->error;
 
 		v4l2_ctrl_handler_free(hdl);
+		media_entity_cleanup(&sd->entity);
 		return err;
 	}
 
@@ -865,6 +866,7 @@ static void msp_remove(struct i2c_client *client)
 	msp_reset(client);
 
 	v4l2_ctrl_handler_free(&state->hdl);
+	media_entity_cleanup(&state->sd.entity);
 }
 
 /* ----------------------------------------------------------------------- */
Signature · 1 line
2.50.1 (Apple Git-155)
#4 lore raw

PATCH v3 4/22 media: i2c: mt9v011: Add missing media_entity_cleanup()

In reply to Biren Pandya

The media_entity_pads_init() function initializes the media entity, but the driver forgets to call media_entity_cleanup() in the error paths and remove function. Add the missing calls to fix the API violation and prevent potential future memory leaks.

---
 drivers/media/i2c/mt9v011.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/media/i2c/mt9v011.c +3 -0
index 055b7915260a..b7ef8bff727d 100644
@@ -520,6 +520,7 @@ static int mt9v011_probe(struct i2c_client *c)
 	    (version != MT9V011_REV_B_VERSION)) {
 		v4l2_info(sd, "*** unknown micron chip detected (0x%04x).\n",
 			  version);
+		media_entity_cleanup(&sd->entity);
 		return -EINVAL;
 	}
 
@@ -542,6 +543,7 @@ static int mt9v011_probe(struct i2c_client *c)
 
 		v4l2_err(sd, "control initialization error %d\n", ret);
 		v4l2_ctrl_handler_free(&core->ctrls);
+		media_entity_cleanup(&sd->entity);
 		return ret;
 	}
 	core->sd.ctrl_handler = &core->ctrls;
@@ -577,6 +579,7 @@ static void mt9v011_remove(struct i2c_client *c)
 
 	v4l2_device_unregister_subdev(sd);
 	v4l2_ctrl_handler_free(&core->ctrls);
+	media_entity_cleanup(&sd->entity);
 }
 
 /* ----------------------------------------------------------------------- */
Signature · 1 line
2.50.1 (Apple Git-155)
#5 lore raw

PATCH v3 5/22 media: i2c: saa7115: Add missing media_entity_cleanup()

In reply to Biren Pandya

The media_entity_pads_init() function initializes the media entity, but the driver forgets to call media_entity_cleanup() in the error paths and remove function. Add the missing calls to fix the API violation and prevent potential future memory leaks.

---
 drivers/media/i2c/saa7115.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/media/i2c/saa7115.c +2 -0
index 48d6730d9271..64adfff3001b 100644
@@ -1867,6 +1867,7 @@ static int saa711x_probe(struct i2c_client *client)
 		int err = hdl->error;
 
 		v4l2_ctrl_handler_free(hdl);
+		media_entity_cleanup(&sd->entity);
 		return err;
 	}
 	v4l2_ctrl_auto_cluster(2, &state->agc, 0, true);
@@ -1925,6 +1926,7 @@ static void saa711x_remove(struct i2c_client *client)
 
 	v4l2_device_unregister_subdev(sd);
 	v4l2_ctrl_handler_free(sd->ctrl_handler);
+	media_entity_cleanup(&sd->entity);
 }
 
 static const struct i2c_device_id saa711x_id[] = {
Signature · 1 line
2.50.1 (Apple Git-155)
#6 lore raw

PATCH v3 6/22 media: platform: stm32-csi: Add missing media_entity_cleanup()

In reply to Biren Pandya

The media_entity_pads_init() (or media_entity_init()) function initializes the media entity, but the driver forgets to call media_entity_cleanup() in the error paths and remove function. Add the missing calls to fix the API violation and prevent potential future memory leaks.

Fixes dcb0f4c16be5 ("media: stm32: csi: addition of the STM32 CSI driver")
---
 drivers/media/platform/st/stm32/stm32-csi.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/media/platform/st/stm32/stm32-csi.c +2 -0
index fd2b6dfbd44c..0847fd347d56 100644
@@ -1060,6 +1060,7 @@ static int stm32_csi_probe(struct platform_device *pdev)
 
 err_cleanup:
 	v4l2_async_nf_cleanup(&csidev->notifier);
+	media_entity_cleanup(&csidev->sd.entity);
 	return ret;
 }
 
@@ -1068,6 +1069,7 @@ static void stm32_csi_remove(struct platform_device *pdev)
 	struct stm32_csi_dev *csidev = platform_get_drvdata(pdev);
 
 	v4l2_async_unregister_subdev(&csidev->sd);
+	media_entity_cleanup(&csidev->sd.entity);
 
 	pm_runtime_disable(&pdev->dev);
 }
Signature · 1 line
2.50.1 (Apple Git-155)
maintainer Reviewed-by on v3 6/22
#7 lore raw

Re: [PATCH v3 06/22] media: platform: stm32-csi: Add missing media_entity_cleanup()

Hi,

thanks for your patch.

Quoted 24 lines · Biren Pandya

The media_entity_pads_init() (or media_entity_init()) function initializes the media entity, but the driver forgets to call media_entity_cleanup() in the error paths and remove function. Add the missing calls to fix the API violation and prevent potential future memory leaks.

Fixes dcb0f4c16be5 ("media: stm32: csi: addition of the STM32 CSI driver")
---
 drivers/media/platform/st/stm32/stm32-csi.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/media/platform/st/stm32/stm32-csi.c +1 -0
index fd2b6dfbd44c..0847fd347d56 100644
@@ -1060,6 +1060,7 @@ static int stm32_csi_probe(struct platform_device *pdev)
 
 err_cleanup:
 	v4l2_async_nf_cleanup(&csidev->notifier);
+	media_entity_cleanup(&csidev->sd.entity);
 	return ret;
 }
 

drivers/media/platform/st/stm32/stm32-csi.c static void stm32_csi_remove(struct platform_device *pdev) · line 1075

Quoted 7 lines
drivers/media/platform/st/stm32/stm32-csi.c +1 -0
@@ -1068,6 +1069,7 @@ static void stm32_csi_remove(struct platform_device *pdev)
 	struct stm32_csi_dev *csidev = platform_get_drvdata(pdev);
 
 	v4l2_async_unregister_subdev(&csidev->sd);
+	media_entity_cleanup(&csidev->sd.entity);
 
 	pm_runtime_disable(&pdev->dev);
 }

Regards,
Alain

Quoted 3 lines
Signature · 1 line
2.50.1 (Apple Git-155)
#8 lore raw

PATCH v3 7/22 media: platform: sun4i_csi: Add missing media_entity_cleanup() + fix UAF

In reply to Biren Pandya

This patch adds missing calls to media_entity_cleanup() in the probe error paths and the remove function to properly release media entities.

Additionally, this patch completely restructures the error rollback cascades in sun4i_csi_probe() and sun4i_csi_notify_complete(). Previously, early initialization failures in probe would return directly, permanently leaking IDA internal structures initialized by media_device_init(). Furthermore, failures in the async notifier complete callback would return without unregistering the exposed video device, leading to a Use-After-Free. A v4l2_async_nf_cleanup() leak was also fixed in the probe path.

---
 drivers/media/platform/sunxi/sun4i-csi/sun4i_csi.c | 56 +++++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 37 insertions(+), 19 deletions(-)
drivers/media/platform/sunxi/sun4i-csi/sun4i_csi.c +37 -19
index e53a07b770b7..2e5dfc938ac6 100644
@@ -85,11 +85,11 @@ static int sun4i_csi_notify_complete(struct v4l2_async_notifier *notifier)
 
 	ret = sun4i_csi_v4l2_register(csi);
 	if (ret < 0)
-		return ret;
+		goto err_unregister_subdev;
 
 	ret = media_device_register(&csi->mdev);
 	if (ret)
-		return ret;
+		goto err_unregister_vdev;
 
 	/* Create link from subdev to main device */
 	ret = media_create_pad_link(&subdev->entity, CSI_SUBDEV_SOURCE,
@@ -114,6 +114,10 @@ static int sun4i_csi_notify_complete(struct v4l2_async_notifier *notifier)
 
 err_clean_media:
 	media_device_unregister(&csi->mdev);
+err_unregister_vdev:
+	vb2_video_unregister_device(&csi->vdev);
+err_unregister_subdev:
+	v4l2_device_unregister_subdev(subdev);
 
 	return ret;
 }
@@ -187,37 +191,45 @@ static int sun4i_csi_probe(struct platform_device *pdev)
 	csi->v4l.mdev = &csi->mdev;
 
 	csi->regs = devm_platform_ioremap_resource(pdev, 0);
-	if (IS_ERR(csi->regs))
-		return PTR_ERR(csi->regs);
+	if (IS_ERR(csi->regs)) {
+		ret = PTR_ERR(csi->regs);
+		goto err_clean_media_dev;
+	}
 
 	irq = platform_get_irq(pdev, 0);
-	if (irq < 0)
-		return irq;
+	if (irq < 0) {
+		ret = irq;
+		goto err_clean_media_dev;
+	}
 
 	csi->bus_clk = devm_clk_get(&pdev->dev, "bus");
 	if (IS_ERR(csi->bus_clk)) {
 		dev_err(&pdev->dev, "Couldn't get our bus clock\n");
-		return PTR_ERR(csi->bus_clk);
+		ret = PTR_ERR(csi->bus_clk);
+		goto err_clean_media_dev;
 	}
 
 	if (csi->traits->has_isp) {
 		csi->isp_clk = devm_clk_get(&pdev->dev, "isp");
 		if (IS_ERR(csi->isp_clk)) {
 			dev_err(&pdev->dev, "Couldn't get our ISP clock\n");
-			return PTR_ERR(csi->isp_clk);
+			ret = PTR_ERR(csi->isp_clk);
+			goto err_clean_media_dev;
 		}
 	}
 
 	csi->ram_clk = devm_clk_get(&pdev->dev, "ram");
 	if (IS_ERR(csi->ram_clk)) {
 		dev_err(&pdev->dev, "Couldn't get our ram clock\n");
-		return PTR_ERR(csi->ram_clk);
+		ret = PTR_ERR(csi->ram_clk);
+		goto err_clean_media_dev;
 	}
 
 	csi->rst = devm_reset_control_get(&pdev->dev, NULL);
 	if (IS_ERR(csi->rst)) {
 		dev_err(&pdev->dev, "Couldn't get our reset line\n");
-		return PTR_ERR(csi->rst);
+		ret = PTR_ERR(csi->rst);
+		goto err_clean_media_dev;
 	}
 
 	/* Initialize subdev */
@@ -236,37 +248,41 @@ static int sun4i_csi_probe(struct platform_device *pdev)
 	ret = media_entity_pads_init(&subdev->entity, CSI_SUBDEV_PADS,
 				     csi->subdev_pads);
 	if (ret < 0)
-		return ret;
+		goto err_clean_media_dev;
 
 	csi->vdev_pad.flags = MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_MUST_CONNECT;
 	vdev->entity.ops = &sun4i_csi_video_entity_ops;
 	ret = media_entity_pads_init(&vdev->entity, 1, &csi->vdev_pad);
 	if (ret < 0)
-		return ret;
+		goto err_clean_subdev_entity;
 
 	ret = sun4i_csi_dma_register(csi, irq);
 	if (ret)
-		goto err_clean_pad;
+		goto err_clean_vdev_entity;
 
 	ret = sun4i_csi_notifier_init(csi);
 	if (ret)
-		goto err_unregister_media;
+		goto err_unregister_dma;
 
 	ret = v4l2_async_nf_register(&csi->notifier);
 	if (ret) {
 		dev_err(csi->dev, "Couldn't register our notifier.\n");
-		goto err_unregister_media;
+		goto err_clean_notifier;
 	}
 
 	pm_runtime_enable(&pdev->dev);
 
 	return 0;
 
-err_unregister_media:
-	media_device_unregister(&csi->mdev);
+err_clean_notifier:
+	v4l2_async_nf_cleanup(&csi->notifier);
+err_unregister_dma:
 	sun4i_csi_dma_unregister(csi);
-
-err_clean_pad:
+err_clean_vdev_entity:
+	media_entity_cleanup(&vdev->entity);
+err_clean_subdev_entity:
+	media_entity_cleanup(&subdev->entity);
+err_clean_media_dev:
 	media_device_cleanup(&csi->mdev);
 
 	return ret;
@@ -282,6 +298,8 @@ static void sun4i_csi_remove(struct platform_device *pdev)
 	vb2_video_unregister_device(&csi->vdev);
 	media_device_unregister(&csi->mdev);
 	sun4i_csi_dma_unregister(csi);
+	media_entity_cleanup(&csi->subdev.entity);
+	media_entity_cleanup(&csi->vdev.entity);
 	media_device_cleanup(&csi->mdev);
 }
 
#9 lore raw

PATCH v3 8/22 media: platform: microchip-isc-scaler: Add missing media_entity_cleanup()

In reply to Biren Pandya

The media_entity_pads_init() (or media_entity_init()) function initializes the media entity, but the driver forgets to call media_entity_cleanup() in the error paths and remove function. Add the missing calls to fix the API violation and prevent potential future memory leaks.

Fixes 920b2665f392 ("media: microchip: microchip-isc: prepare for media controller support")
---
 drivers/media/platform/microchip/microchip-isc-scaler.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/media/platform/microchip/microchip-isc-scaler.c +7 -0
index e83463543e21..c368c66d9c8b 100644
@@ -222,6 +222,7 @@ int isc_scaler_init(struct isc_device *isc)
 	ret = v4l2_device_register_subdev(&isc->v4l2_dev, &isc->scaler_sd);
 	if (ret < 0) {
 		dev_err(isc->dev, "scaler sd failed to register subdev\n");
+		media_entity_cleanup(&isc->scaler_sd.entity);
 		return ret;
 	}
 
@@ -229,6 +230,12 @@ int isc_scaler_init(struct isc_device *isc)
 }
 EXPORT_SYMBOL_GPL(isc_scaler_init);
 
+void isc_scaler_unregister(struct isc_device *isc)
+{
+	media_entity_cleanup(&isc->scaler_sd.entity);
+}
+EXPORT_SYMBOL_GPL(isc_scaler_unregister);
+
 int isc_scaler_link(struct isc_device *isc)
 {
 	int ret;
Signature · 6 lines
2.50.1 (Apple Git-155)


 drivers/media/platform/microchip/microchip-isc-base.c | 1 +
 drivers/media/platform/microchip/microchip-isc.h      | 1 +
 2 files changed, 2 insertions(+)
drivers/media/platform/microchip/microchip-isc-base.c +1 -0
index a7cdc743fda7..27e180aff9a2 100644
@@ -1949,6 +1949,7 @@ EXPORT_SYMBOL_GPL(isc_mc_init);
 
 void isc_mc_cleanup(struct isc_device *isc)
 {
+	isc_scaler_unregister(isc);
 	media_entity_cleanup(&isc->video_dev.entity);
 	media_device_cleanup(&isc->mdev);
 }
drivers/media/platform/microchip/microchip-isc.h +1 -0
index ad4e98a1dd8f..40a54d7d7e10 100644
@@ -392,6 +392,7 @@ void microchip_isc_clk_cleanup(struct isc_device *isc);
 
 int isc_scaler_link(struct isc_device *isc);
 int isc_scaler_init(struct isc_device *isc);
+void isc_scaler_unregister(struct isc_device *isc);
 int isc_mc_init(struct isc_device *isc, u32 ver);
 void isc_mc_cleanup(struct isc_device *isc);
 
Signature · 1 line
2.50.1 (Apple Git-155)
#10 lore raw

PATCH v3 9/22 media: platform: cdns-csi2tx: Add missing media_entity_cleanup()

In reply to Biren Pandya

The media_entity_pads_init() (or media_entity_init()) function initializes the media entity, but the driver forgets to call media_entity_cleanup() in the error paths and remove function. Add the missing calls to fix the API violation and prevent potential future memory leaks.

Fixes 6f684d4fcce5 ("media: v4l: cadence: Add Cadence MIPI-CSI2 TX driver")
---
 drivers/media/platform/cadence/cdns-csi2tx.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/media/platform/cadence/cdns-csi2tx.c +4 -1
index 629b0fa838a2..df1598091d57 100644
@@ -620,7 +620,7 @@ static int csi2tx_probe(struct platform_device *pdev)
 
 	ret = v4l2_async_register_subdev(&csi2tx->subdev);
 	if (ret < 0)
-		goto err_free_priv;
+		goto err_entity_cleanup;
 
 	dev_info(&pdev->dev,
 		 "Probed CSI2TX with %u/%u lanes, %u streams, %s D-PHY\n",
@@ -629,6 +629,8 @@ static int csi2tx_probe(struct platform_device *pdev)
 
 	return 0;
 
+err_entity_cleanup:
+	media_entity_cleanup(&csi2tx->subdev.entity);
 err_free_priv:
 	kfree(csi2tx);
 	return ret;
@@ -639,6 +641,7 @@ static void csi2tx_remove(struct platform_device *pdev)
 	struct csi2tx_priv *csi2tx = platform_get_drvdata(pdev);
 
 	v4l2_async_unregister_subdev(&csi2tx->subdev);
+	media_entity_cleanup(&csi2tx->subdev.entity);
 	kfree(csi2tx);
 }
 
Signature · 1 line
2.50.1 (Apple Git-155)
#11 lore raw

PATCH v3 10/22 media: platform: rcar-csi2: Add missing media_entity_cleanup()

In reply to Biren Pandya

The media_entity_pads_init() (or media_entity_init()) function initializes the media entity, but the driver forgets to call media_entity_cleanup() in the error paths and remove function. Add the missing calls to fix the API violation and prevent potential future memory leaks.

---
 drivers/media/platform/renesas/rcar-csi2.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/media/platform/renesas/rcar-csi2.c +2 -0
index 7305cc4a04cb..c4e35cac97f4 100644
@@ -2634,6 +2634,7 @@ static int rcsi2_probe(struct platform_device *pdev)
 error_async:
 	v4l2_async_nf_unregister(&priv->notifier);
 	v4l2_async_nf_cleanup(&priv->notifier);
+	media_entity_cleanup(&priv->subdev.entity);
 
 	return ret;
 }
@@ -2646,6 +2647,7 @@ static void rcsi2_remove(struct platform_device *pdev)
 	v4l2_async_nf_cleanup(&priv->notifier);
 	v4l2_async_unregister_subdev(&priv->subdev);
 	v4l2_subdev_cleanup(&priv->subdev);
+	media_entity_cleanup(&priv->subdev.entity);
 
 	pm_runtime_disable(&pdev->dev);
 }
Signature · 1 line
2.50.1 (Apple Git-155)
#12 lore raw

PATCH v3 11/22 media: platform: csisp: Add missing media_entity_cleanup()

In reply to Biren Pandya

The media_entity_pads_init() (or media_entity_init()) function initializes the media entity, but the driver forgets to call media_entity_cleanup() in the error paths and remove function. Add the missing calls to fix the API violation and prevent potential future memory leaks.

---
 drivers/media/platform/renesas/rcar-isp/csisp.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/media/platform/renesas/rcar-isp/csisp.c +2 -0
index 8fb2cc3b5650..51db5ad25c47 100644
@@ -556,6 +556,7 @@ static int risp_probe(struct platform_device *pdev)
 	v4l2_async_nf_cleanup(&isp->notifier);
 error_pm:
 	pm_runtime_disable(&pdev->dev);
+	media_entity_cleanup(&isp->subdev.entity);
 
 	return ret;
 }
@@ -569,6 +570,7 @@ static void risp_remove(struct platform_device *pdev)
 
 	v4l2_async_unregister_subdev(&isp->subdev);
 	v4l2_subdev_cleanup(&isp->subdev);
+	media_entity_cleanup(&isp->subdev.entity);
 
 	pm_runtime_disable(&pdev->dev);
 }
Signature · 1 line
2.50.1 (Apple Git-155)
#13 lore raw

PATCH v3 12/22 media: platform: rcar-vin: Add missing media_entity_cleanup() + fix UAF

In reply to Biren Pandya

This patch adds a missing call to media_entity_cleanup() in the probe error path and remove function to properly release media entities.

Additionally, it fixes a severe Use-After-Free vulnerability in rvin_group_notify_complete(). If the registration of one of the video nodes (or the subsequent link setup) failed, the function returned directly. This skipped the cleanup of previously registered video nodes and the media_device, exposing them to userspace.

If the probe function then aborted and tore down the parent structures, any subsequent userspace access to the stranded /dev/videoX nodes would result in a Use-After-Free.

This patch adds a proper cascading rollback block to unregister the video devices and the media device before returning the error.

---
 drivers/media/platform/renesas/rcar-vin/rcar-core.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)
drivers/media/platform/renesas/rcar-vin/rcar-core.c +18 -3
index c8d564aa1eba..b8e4c8be431d 100644
@@ -219,7 +219,7 @@ static int rvin_group_notify_complete(struct v4l2_async_notifier *notifier)
 	ret = v4l2_device_register_subdev_nodes(&vin->v4l2_dev);
 	if (ret) {
 		vin_err(vin, "Failed to register subdev nodes\n");
-		return ret;
+		goto err_unregister_media;
 	}
 
 	/* Register all video nodes for the group. */
@@ -228,11 +228,24 @@ static int rvin_group_notify_complete(struct v4l2_async_notifier *notifier)
 		    !video_is_registered(&vin->group->vin[i]->vdev)) {
 			ret = rvin_v4l2_register(vin->group->vin[i]);
 			if (ret)
-				return ret;
+				goto err_unregister_vdevs;
 		}
 	}
 
-	return vin->group->link_setup(vin->group);
+	ret = vin->group->link_setup(vin->group);
+	if (ret)
+		goto err_unregister_vdevs;
+
+	return 0;
+
+err_unregister_vdevs:
+	for (i = 0; i < RCAR_VIN_NUM; i++) {
+		if (vin->group->vin[i] && video_is_registered(&vin->group->vin[i]->vdev))
+			rvin_v4l2_unregister(vin->group->vin[i]);
+	}
+err_unregister_media:
+	media_device_unregister(&vin->group->mdev);
+	return ret;
 }
 
 static void rvin_group_notify_unbind(struct v4l2_async_notifier *notifier,
@@ -1250,6 +1263,7 @@ static int rcar_vin_probe(struct platform_device *pdev)
 	rvin_id_put(vin);
 err_dma:
 	rvin_dma_unregister(vin);
+	media_entity_cleanup(&vin->vdev.entity);
 
 	return ret;
 }
@@ -1274,6 +1288,7 @@ static void rcar_vin_remove(struct platform_device *pdev)
 	rvin_id_put(vin);
 
 	rvin_dma_unregister(vin);
+	media_entity_cleanup(&vin->vdev.entity);
 }
 
 static DEFINE_SIMPLE_DEV_PM_OPS(rvin_pm_ops, rvin_suspend, rvin_resume);
#14 lore raw

PATCH v3 13/22 media: platform: rzg2l-cru: Add missing media_entity_cleanup() + fix UAF

In reply to Biren Pandya

This patch adds a missing call to media_entity_cleanup() in the probe error path and remove function to properly release media entities.

Additionally, it fixes a severe Use-After-Free vulnerability in rzg2l_cru_group_notify_complete(). If media_create_pad_link() failed after successfully registering the video device and IP subdev, the function returned directly without unregistering them.

If the probe function then tore down the parent structures, any subsequent userspace access to the stranded video node would result in a Use-After-Free.

This patch adds a cascading rollback block to unregister the video device and IP subdev on failure.

---
 drivers/media/platform/renesas/rzg2l-cru/rzg2l-core.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/media/platform/renesas/rzg2l-cru/rzg2l-core.c +12 -4
index 3c5fbd857371..3ccd895c09fe 100644
@@ -50,12 +50,12 @@ static int rzg2l_cru_group_notify_complete(struct v4l2_async_notifier *notifier)
 	ret = v4l2_device_register_subdev_nodes(&cru->v4l2_dev);
 	if (ret) {
 		dev_err(cru->dev, "Failed to register subdev nodes\n");
-		return ret;
+		goto err_unregister_ip_subdev;
 	}
 
 	ret = rzg2l_cru_video_register(cru);
 	if (ret)
-		return ret;
+		goto err_unregister_ip_subdev;
 
 	/*
 	 * CRU can be connected either to CSI2 or PARALLEL device
@@ -71,7 +71,7 @@ static int rzg2l_cru_group_notify_complete(struct v4l2_async_notifier *notifier)
 	if (ret) {
 		dev_err(cru->dev, "Error creating link from %s to %s\n",
 			source->name, sink->name);
-		return ret;
+		goto err_unregister_video;
 	}
 	cru->ip.remote = cru->csi.subdev;
 
@@ -84,10 +84,16 @@ static int rzg2l_cru_group_notify_complete(struct v4l2_async_notifier *notifier)
 	if (ret) {
 		dev_err(cru->dev, "Error creating link from %s to %s\n",
 			source->name, sink->name);
-		return ret;
+		goto err_unregister_video;
 	}
 
 	return 0;
+
+err_unregister_video:
+	rzg2l_cru_video_unregister(cru);
+err_unregister_ip_subdev:
+	rzg2l_cru_ip_subdev_unregister(cru);
+	return ret;
 }
 
 static void rzg2l_cru_group_notify_unbind(struct v4l2_async_notifier *notifier,
@@ -300,6 +306,7 @@ static int rzg2l_cru_probe(struct platform_device *pdev)
 
 error_dma_unregister:
 	rzg2l_cru_dma_unregister(cru);
+	media_entity_cleanup(&cru->vdev.entity);
 
 	return ret;
 }
@@ -316,6 +323,7 @@ static void rzg2l_cru_remove(struct platform_device *pdev)
 	mutex_destroy(&cru->mdev_lock);
 
 	rzg2l_cru_dma_unregister(cru);
+	media_entity_cleanup(&cru->vdev.entity);
 }
 
 static const u16 rzg3e_cru_regs[] = {
#15 lore raw

PATCH v3 14/22 media: platform: cfe: Add missing media_entity_cleanup()

In reply to Biren Pandya

The media_entity_pads_init() (or media_entity_init()) function initializes the media entity, but the driver forgets to call media_entity_cleanup() in the error paths and remove function. Add the missing calls to fix the API violation and prevent potential future memory leaks.

Fixes 6edb685abb2a ("media: raspberrypi: Add support for RP1-CFE")
---
 drivers/media/platform/raspberrypi/rp1-cfe/cfe.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/media/platform/raspberrypi/rp1-cfe/cfe.c +2 -0
index 8375ed3e97b9..17c523d32db7 100644
@@ -2039,6 +2039,7 @@ static int cfe_register_node(struct cfe_device *cfe, int id)
 	if (ret) {
 		cfe_err(cfe, "Unable to register video device %s\n",
 			vdev->name);
+		media_entity_cleanup(&vdev->entity);
 		return ret;
 	}
 
@@ -2064,6 +2065,7 @@ static void cfe_unregister_nodes(struct cfe_device *cfe)
 		if (check_state(cfe, NODE_REGISTERED, i)) {
 			clear_state(cfe, NODE_REGISTERED, i);
 			video_unregister_device(&node->video_dev);
+			media_entity_cleanup(&node->video_dev.entity);
 		}
 	}
 }
Signature · 1 line
2.50.1 (Apple Git-155)
#16 lore raw

PATCH v3 15/22 media: platform: j721e-csi2rx: Add missing media_entity_cleanup()

In reply to Biren Pandya

The media_entity_pads_init() (or media_entity_init()) function initializes the media entity, but the driver forgets to call media_entity_cleanup() in the error paths and remove function. Add the missing calls to fix the API violation and prevent potential future memory leaks.

Fixes b4a3d877dc92 ("media: ti: Add CSI2RX support for J721E")
---
 drivers/media/platform/ti/j721e-csi2rx/j721e-csi2rx.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/media/platform/ti/j721e-csi2rx/j721e-csi2rx.c +5 -1
index b75aa363d1bf..b88e3ac3dac8 100644
@@ -1079,13 +1079,16 @@ static int ti_csi2rx_v4l2_init(struct ti_csi2rx_dev *csi)
 	csi->v4l2_dev.mdev = mdev;
 
 	ret = v4l2_device_register(csi->dev, &csi->v4l2_dev);
-	if (ret)
+	if (ret) {
+		media_entity_cleanup(&csi->vdev.entity);
 		return ret;
+	}
 
 	ret = media_device_register(mdev);
 	if (ret) {
 		v4l2_device_unregister(&csi->v4l2_dev);
 		media_device_cleanup(mdev);
+		media_entity_cleanup(&csi->vdev.entity);
 		return ret;
 	}
 
@@ -1105,6 +1108,7 @@ static void ti_csi2rx_cleanup_v4l2(struct ti_csi2rx_dev *csi)
 	media_device_unregister(&csi->mdev);
 	v4l2_device_unregister(&csi->v4l2_dev);
 	media_device_cleanup(&csi->mdev);
+	media_entity_cleanup(&csi->vdev.entity);
 }
 
 static void ti_csi2rx_cleanup_subdev(struct ti_csi2rx_dev *csi)
Signature · 1 line
2.50.1 (Apple Git-155)
#17 lore raw

PATCH v3 16/22 media: platform: hantro_drv: Add missing media_entity_cleanup()

In reply to Biren Pandya

The media_entity_pads_init() (or media_entity_init()) function initializes the media entity, but the driver forgets to call media_entity_cleanup() in the error paths and remove function. Add the missing calls to fix the API violation and prevent potential future memory leaks.

---
 drivers/media/platform/verisilicon/hantro_drv.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/media/platform/verisilicon/hantro_drv.c +9 -3
index 2e81877f640f..ad71c0402ef3 100644
@@ -771,8 +771,10 @@ static int hantro_register_entity(struct media_device *mdev,
 		return ret;
 
 	ret = media_device_register_entity(mdev, entity);
-	if (ret)
+	if (ret) {
+		media_entity_cleanup(entity);
 		return ret;
+	}
 
 	return 0;
 }
@@ -860,12 +862,13 @@ static int hantro_attach_func(struct hantro_dev *vpu,
 
 err_rel_entity2:
 	media_device_unregister_entity(&func->sink);
-
+	media_entity_cleanup(&func->sink);
 err_rel_entity1:
 	media_device_unregister_entity(&func->proc);
-
+	media_entity_cleanup(&func->proc);
 err_rel_entity0:
 	media_device_unregister_entity(&func->vdev.entity);
+	media_entity_cleanup(&func->vdev.entity);
 	return ret;
 }
 
@@ -878,6 +881,9 @@ static void hantro_detach_func(struct hantro_func *func)
 	media_device_unregister_entity(&func->sink);
 	media_device_unregister_entity(&func->proc);
 	media_device_unregister_entity(&func->vdev.entity);
+	media_entity_cleanup(&func->sink);
+	media_entity_cleanup(&func->proc);
+	media_entity_cleanup(&func->vdev.entity);
 }
 
 static int hantro_add_func(struct hantro_dev *vpu, unsigned int funcid)
Signature · 1 line
2.50.1 (Apple Git-155)
maintainer Reviewed-by on v3 16/22
#18 lore raw

Re: [PATCH v3 16/22] media: platform: hantro_drv: Add missing media_entity_cleanup()

Le 14/06/2026 à 22:28, Biren Pandya a écrit :

Quoted 6 lines

The media_entity_pads_init() (or media_entity_init()) function initializes the media entity, but the driver forgets to call media_entity_cleanup() in the error paths and remove function. Add the missing calls to fix the API violation and prevent potential future memory leaks.

Thanks

Quoted 46 lines
---
  drivers/media/platform/verisilicon/hantro_drv.c | 12 +++++++++---
  1 file changed, 9 insertions(+), 3 deletions(-)
drivers/media/platform/verisilicon/hantro_drv.c +9 -3
index 2e81877f640f..ad71c0402ef3 100644
@@ -771,8 +771,10 @@ static int hantro_register_entity(struct media_device *mdev,
  		return ret;
 
  	ret = media_device_register_entity(mdev, entity);
-	if (ret)
+	if (ret) {
+		media_entity_cleanup(entity);
  		return ret;
+	}
 
  	return 0;
  }
@@ -860,12 +862,13 @@ static int hantro_attach_func(struct hantro_dev *vpu,
 
  err_rel_entity2:
  	media_device_unregister_entity(&func->sink);
-
+	media_entity_cleanup(&func->sink);
  err_rel_entity1:
  	media_device_unregister_entity(&func->proc);
-
+	media_entity_cleanup(&func->proc);
  err_rel_entity0:
  	media_device_unregister_entity(&func->vdev.entity);
+	media_entity_cleanup(&func->vdev.entity);
  	return ret;
  }
 
@@ -878,6 +881,9 @@ static void hantro_detach_func(struct hantro_func *func)
  	media_device_unregister_entity(&func->sink);
  	media_device_unregister_entity(&func->proc);
  	media_device_unregister_entity(&func->vdev.entity);
+	media_entity_cleanup(&func->sink);
+	media_entity_cleanup(&func->proc);
+	media_entity_cleanup(&func->vdev.entity);
  }
 
  static int hantro_add_func(struct hantro_dev *vpu, unsigned int funcid)
#19 lore raw

PATCH v3 17/22 media: pci: saa7134: Add missing media_entity_cleanup()

In reply to Biren Pandya

The media_entity_pads_init() (or media_entity_init()) function initializes the media entity, but the driver forgets to call media_entity_cleanup() in the error paths and remove function. Add the missing calls to fix the API violation and prevent potential future memory leaks.

---
 drivers/media/pci/saa7134/saa7134-core.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
drivers/media/pci/saa7134/saa7134-core.c +14 -4
index 2f5b258d682b..c0e1e8ad55c8 100644
@@ -798,8 +798,10 @@ static void saa7134_media_release(struct saa7134_dev *dev)
 #ifdef CONFIG_MEDIA_CONTROLLER
 	int i;
 
-	for (i = 0; i < SAA7134_INPUT_MAX + 1; i++)
+	for (i = 0; i < SAA7134_INPUT_MAX + 1; i++) {
 		media_device_unregister_entity(&dev->input_ent[i]);
+		media_entity_cleanup(&dev->input_ent[i]);
+	}
 #endif
 }
 
@@ -836,8 +838,10 @@ static void saa7134_create_entities(struct saa7134_dev *dev)
 			pr_err("failed to initialize demod pad!\n");
 
 		ret = media_device_register_entity(dev->media_dev, &dev->demod);
-		if (ret < 0)
+		if (ret < 0) {
 			pr_err("failed to register demod entity!\n");
+			media_entity_cleanup(&dev->demod);
+		}
 
 		dev->decoder = &dev->demod;
 	} else {
@@ -904,8 +908,10 @@ static void saa7134_create_entities(struct saa7134_dev *dev)
 			pr_err("failed to initialize input pad[%d]!\n", i);
 
 		ret = media_device_register_entity(dev->media_dev, ent);
-		if (ret < 0)
+		if (ret < 0) {
 			pr_err("failed to register input entity %d!\n", i);
+			media_entity_cleanup(ent);
+		}
 	}
 
 	/* Create input for Radio RF connector */
@@ -923,8 +929,10 @@ static void saa7134_create_entities(struct saa7134_dev *dev)
 			pr_err("failed to initialize input pad[%d]!\n", i);
 
 		ret = media_device_register_entity(dev->media_dev, ent);
-		if (ret < 0)
+		if (ret < 0) {
 			pr_err("failed to register input entity %d!\n", i);
+			media_entity_cleanup(ent);
+		}
 	}
 }
 #endif
@@ -956,6 +964,7 @@ static void saa7134_unregister_video(struct saa7134_dev *dev)
 			vb2_video_unregister_device(dev->video_dev);
 		else
 			video_device_release(dev->video_dev);
+		media_entity_cleanup(&dev->video_dev->entity);
 		dev->video_dev = NULL;
 	}
 	if (dev->vbi_dev) {
@@ -963,6 +972,7 @@ static void saa7134_unregister_video(struct saa7134_dev *dev)
 			vb2_video_unregister_device(dev->vbi_dev);
 		else
 			video_device_release(dev->vbi_dev);
+		media_entity_cleanup(&dev->vbi_dev->entity);
 		dev->vbi_dev = NULL;
 	}
 	if (dev->radio_dev) {
Signature · 1 line
2.50.1 (Apple Git-155)
#20 lore raw

PATCH v3 18/22 media: usb: cx231xx: Add missing media_entity_cleanup()

In reply to Biren Pandya

The media_entity_pads_init() (or media_entity_init()) function initializes the media entity, but the driver forgets to call media_entity_cleanup() in the error paths and remove function. Add the missing calls to fix the API violation and prevent potential future memory leaks.

---
 drivers/media/usb/cx231xx/cx231xx-video.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/media/usb/cx231xx/cx231xx-video.c +7 -1
index 2cd4e333bc4b..2b1ad969e58c 100644
@@ -1006,8 +1006,10 @@ void cx231xx_v4l2_create_entities(struct cx231xx *dev)
 			pr_err("failed to initialize input pad[%d]!\n", i);
 
 		ret = media_device_register_entity(dev->media_dev, ent);
-		if (ret < 0)
+		if (ret < 0) {
 			pr_err("failed to register input entity %d!\n", i);
+			media_entity_cleanup(ent);
+		}
 	}
 #endif
 }
@@ -1558,6 +1560,7 @@ void cx231xx_release_analog_resources(struct cx231xx *dev)
 			video_device_node_name(&dev->vbi_dev));
 		video_unregister_device(&dev->vbi_dev);
 	}
+	media_entity_cleanup(&dev->vbi_dev.entity);
 	if (video_is_registered(&dev->vdev)) {
 		dev_info(dev->dev, "V4L2 device %s deregistered\n",
 			video_device_node_name(&dev->vdev));
@@ -1567,6 +1570,7 @@ void cx231xx_release_analog_resources(struct cx231xx *dev)
 
 		video_unregister_device(&dev->vdev);
 	}
+	media_entity_cleanup(&dev->vdev.entity);
 	v4l2_ctrl_handler_free(&dev->ctrl_handler);
 	v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
 }
@@ -1839,6 +1843,7 @@ int cx231xx_register_analog_devices(struct cx231xx *dev)
 		dev_err(dev->dev,
 			"unable to register video device (error=%i).\n",
 			ret);
+		media_entity_cleanup(&dev->vdev.entity);
 		return ret;
 	}
 
@@ -1895,6 +1900,7 @@ int cx231xx_register_analog_devices(struct cx231xx *dev)
 				    vbi_nr[dev->devno]);
 	if (ret < 0) {
 		dev_err(dev->dev, "unable to register vbi device\n");
+		media_entity_cleanup(&dev->vbi_dev.entity);
 		return ret;
 	}
 
Signature · 1 line
2.50.1 (Apple Git-155)
#21 lore raw

PATCH v3 19/22 media: usb: em28xx: Add missing media_entity_cleanup()

In reply to Biren Pandya

The media_entity_pads_init() (or media_entity_init()) function initializes the media entity, but the driver forgets to call media_entity_cleanup() in the error paths and remove function. Add the missing calls to fix the API violation and prevent potential future memory leaks.

---
 drivers/media/usb/em28xx/em28xx-video.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/media/usb/em28xx/em28xx-video.c +11 -2
index 4a0ce9c5ee4b..70baf93aa8f8 100644
@@ -1006,6 +1006,7 @@ static void em28xx_v4l2_media_release(struct em28xx *dev)
 		if (!INPUT(i)->type)
 			return;
 		media_device_unregister_entity(&dev->input_ent[i]);
+		media_entity_cleanup(&dev->input_ent[i]);
 	}
 #endif
 }
@@ -1136,9 +1137,11 @@ static void em28xx_v4l2_create_entities(struct em28xx *dev)
 				"failed to initialize input pad[%d]!\n", i);
 
 		ret = media_device_register_entity(dev->media_dev, ent);
-		if (ret < 0)
+		if (ret < 0) {
 			dev_err(&dev->intf->dev,
 				"failed to register input entity %d!\n", i);
+			media_entity_cleanup(ent);
+		}
 	}
 
 	if (dev->board.decoder == EM28XX_BUILTIN) {
@@ -1157,8 +1160,10 @@ static void em28xx_v4l2_create_entities(struct em28xx *dev)
 			dev_err(&dev->intf->dev, "failed to initialize decoder pads %d!\n", ret);
 
 		ret = media_device_register_entity(dev->media_dev, v4l2->decoder);
-		if (ret < 0)
+		if (ret < 0) {
 			dev_err(&dev->intf->dev, "failed to register decoder entity %d!\n", ret);
+			media_entity_cleanup(v4l2->decoder);
+		}
 	}
 
 #endif
@@ -2403,11 +2408,13 @@ static int em28xx_v4l2_fini(struct em28xx *dev)
 			 video_device_node_name(&v4l2->vbi_dev));
 		video_unregister_device(&v4l2->vbi_dev);
 	}
+	media_entity_cleanup(&v4l2->vbi_dev.entity);
 	if (video_is_registered(&v4l2->vdev)) {
 		dev_info(&dev->intf->dev, "V4L2 device %s deregistered\n",
 			 video_device_node_name(&v4l2->vdev));
 		video_unregister_device(&v4l2->vdev);
 	}
+	media_entity_cleanup(&v4l2->vdev.entity);
 
 	v4l2_ctrl_handler_free(&v4l2->ctrl_handler);
 	v4l2_device_unregister(&v4l2->v4l2_dev);
@@ -2961,6 +2968,7 @@ static int em28xx_v4l2_init(struct em28xx *dev)
 	if (ret) {
 		dev_err(&dev->intf->dev,
 			"unable to register video device (error=%i).\n", ret);
+		media_entity_cleanup(&v4l2->vdev.entity);
 		goto unregister_dev;
 	}
 
@@ -2995,6 +3003,7 @@ static int em28xx_v4l2_init(struct em28xx *dev)
 		if (ret < 0) {
 			dev_err(&dev->intf->dev,
 				"unable to register vbi device\n");
+			media_entity_cleanup(&v4l2->vbi_dev.entity);
 			goto unregister_dev;
 		}
 	}
Signature · 1 line
2.50.1 (Apple Git-155)
#22 lore raw

PATCH v3 20/22 media: usb: au0828: Add missing media_entity_cleanup()

In reply to Biren Pandya

The media_entity_pads_init() (or media_entity_init()) function initializes the media entity, but the driver forgets to call media_entity_cleanup() in the error paths and remove function. Add the missing calls to fix the API violation and prevent potential future memory leaks.

---
 drivers/media/usb/au0828/au0828-video.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/media/usb/au0828/au0828-video.c +9 -1
index 3c53105f3d2b..fec4eafd6e30 100644
@@ -628,6 +628,7 @@ void au0828_usb_v4l2_media_release(struct au0828_dev *dev)
 		if (AUVI_INPUT(i).type == AU0828_VMUX_UNDEFINED)
 			return;
 		media_device_unregister_entity(&dev->input_ent[i]);
+		media_entity_cleanup(&dev->input_ent[i]);
 	}
 #endif
 }
@@ -934,7 +935,9 @@ int au0828_analog_unregister(struct au0828_dev *dev)
 
 	mutex_lock(&au0828_sysfs_lock);
 	vb2_video_unregister_device(&dev->vdev);
+	media_entity_cleanup(&dev->vdev.entity);
 	vb2_video_unregister_device(&dev->vbi_dev);
+	media_entity_cleanup(&dev->vbi_dev.entity);
 	mutex_unlock(&au0828_sysfs_lock);
 
 	v4l2_device_disconnect(&dev->v4l2_dev);
@@ -1907,8 +1910,10 @@ static void au0828_analog_create_entities(struct au0828_dev *dev)
 			pr_err("failed to initialize input pad[%d]!\n", i);
 
 		ret = media_device_register_entity(dev->media_dev, ent);
-		if (ret < 0)
+		if (ret < 0) {
 			pr_err("failed to register input entity %d!\n", i);
+			media_entity_cleanup(ent);
+		}
 	}
 #endif
 }
@@ -2026,6 +2031,7 @@ int au0828_analog_register(struct au0828_dev *dev,
 	if (retval != 0) {
 		dprintk(1, "unable to register video device (error = %d).\n",
 			retval);
+		media_entity_cleanup(&dev->vdev.entity);
 		return -ENODEV;
 	}
 
@@ -2055,6 +2061,8 @@ int au0828_analog_register(struct au0828_dev *dev,
 
 err_reg_vbi_dev:
 	vb2_video_unregister_device(&dev->vdev);
+	media_entity_cleanup(&dev->vdev.entity);
+	media_entity_cleanup(&dev->vbi_dev.entity);
 	return ret;
 }
 
Signature · 1 line
2.50.1 (Apple Git-155)
#23 lore raw

PATCH v3 21/22 media: usb: dvb-usb-v2: Add missing media_entity_cleanup()

In reply to Biren Pandya

The media_entity_pads_init() (or media_entity_init()) function initializes the media entity, but the driver forgets to call media_entity_cleanup() in the error paths and remove function. Add the missing calls to fix the API violation and prevent potential future memory leaks.

---
 drivers/media/usb/dvb-usb-v2/mxl111sf.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
drivers/media/usb/dvb-usb-v2/mxl111sf.c +20 -1
index 870ac3c8b085..0524685e220e 100644
@@ -900,8 +900,21 @@ static int mxl111sf_attach_tuner(struct dvb_usb_adapter *adap)
 		return ret;
 
 	ret = media_device_register_entity(mdev, &state->tuner);
-	if (ret)
+	if (ret) {
+		media_entity_cleanup(&state->tuner);
 		return ret;
+	}
+#endif
+	return 0;
+}
+
+static int mxl111sf_detach_tuner(struct dvb_usb_adapter *adap)
+{
+	struct mxl111sf_state *state = adap_to_priv(adap);
+
+#ifdef CONFIG_MEDIA_CONTROLLER_DVB
+	media_device_unregister_entity(&state->tuner);
+	media_entity_cleanup(&state->tuner);
 #endif
 	return 0;
 }
@@ -1093,6 +1106,7 @@ static struct dvb_usb_device_properties mxl111sf_props_dvbt = {
 	.i2c_algo          = &mxl111sf_i2c_algo,
 	.frontend_attach   = mxl111sf_frontend_attach_dvbt,
 	.tuner_attach      = mxl111sf_attach_tuner,
+	.tuner_detach      = mxl111sf_detach_tuner,
 	.init              = mxl111sf_init,
 	.streaming_ctrl    = mxl111sf_ep4_streaming_ctrl,
 	.get_stream_config = mxl111sf_get_stream_config_dvbt,
@@ -1135,6 +1149,7 @@ static struct dvb_usb_device_properties mxl111sf_props_atsc = {
 	.i2c_algo          = &mxl111sf_i2c_algo,
 	.frontend_attach   = mxl111sf_frontend_attach_atsc,
 	.tuner_attach      = mxl111sf_attach_tuner,
+	.tuner_detach      = mxl111sf_detach_tuner,
 	.init              = mxl111sf_init,
 	.streaming_ctrl    = mxl111sf_ep6_streaming_ctrl,
 	.get_stream_config = mxl111sf_get_stream_config_atsc,
@@ -1177,6 +1192,7 @@ static struct dvb_usb_device_properties mxl111sf_props_mh = {
 	.i2c_algo          = &mxl111sf_i2c_algo,
 	.frontend_attach   = mxl111sf_frontend_attach_mh,
 	.tuner_attach      = mxl111sf_attach_tuner,
+	.tuner_detach      = mxl111sf_detach_tuner,
 	.init              = mxl111sf_init,
 	.streaming_ctrl    = mxl111sf_ep5_streaming_ctrl,
 	.get_stream_config = mxl111sf_get_stream_config_mh,
@@ -1246,6 +1262,7 @@ static struct dvb_usb_device_properties mxl111sf_props_atsc_mh = {
 	.i2c_algo          = &mxl111sf_i2c_algo,
 	.frontend_attach   = mxl111sf_frontend_attach_atsc_mh,
 	.tuner_attach      = mxl111sf_attach_tuner,
+	.tuner_detach      = mxl111sf_detach_tuner,
 	.init              = mxl111sf_init,
 	.streaming_ctrl    = mxl111sf_streaming_ctrl_atsc_mh,
 	.get_stream_config = mxl111sf_get_stream_config_atsc_mh,
@@ -1325,6 +1342,7 @@ static struct dvb_usb_device_properties mxl111sf_props_mercury = {
 	.i2c_algo          = &mxl111sf_i2c_algo,
 	.frontend_attach   = mxl111sf_frontend_attach_mercury,
 	.tuner_attach      = mxl111sf_attach_tuner,
+	.tuner_detach      = mxl111sf_detach_tuner,
 	.init              = mxl111sf_init,
 	.streaming_ctrl    = mxl111sf_streaming_ctrl_mercury,
 	.get_stream_config = mxl111sf_get_stream_config_mercury,
@@ -1396,6 +1414,7 @@ static struct dvb_usb_device_properties mxl111sf_props_mercury_mh = {
 	.i2c_algo          = &mxl111sf_i2c_algo,
 	.frontend_attach   = mxl111sf_frontend_attach_mercury_mh,
 	.tuner_attach      = mxl111sf_attach_tuner,
+	.tuner_detach      = mxl111sf_detach_tuner,
 	.init              = mxl111sf_init,
 	.streaming_ctrl    = mxl111sf_streaming_ctrl_mercury_mh,
 	.get_stream_config = mxl111sf_get_stream_config_mercury_mh,
Signature · 1 line
2.50.1 (Apple Git-155)
#24 lore raw

PATCH v3 22/22 media: dvb-frontends: au8522_decoder: Add missing media_entity_cleanup()

In reply to Biren Pandya

The media_entity_pads_init() (or media_entity_init()) function initializes the media entity, but the driver forgets to call media_entity_cleanup() in the error paths and remove function. Add the missing calls to fix the API violation and prevent potential future memory leaks.

---
 drivers/media/dvb-frontends/au8522_decoder.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/media/dvb-frontends/au8522_decoder.c +2 -0
index 58b959b272c6..9df590603152 100644
@@ -741,6 +741,7 @@ static int au8522_probe(struct i2c_client *client)
 	if (hdl->error) {
 		int err = hdl->error;
 
+		media_entity_cleanup(&sd->entity);
 		v4l2_ctrl_handler_free(hdl);
 		au8522_release_state(state);
 		return err;
@@ -764,6 +765,7 @@ static void au8522_remove(struct i2c_client *client)
 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
 	v4l2_device_unregister_subdev(sd);
 	v4l2_ctrl_handler_free(sd->ctrl_handler);
+	media_entity_cleanup(&sd->entity);
 	au8522_release_state(to_state(sd));
 }
 
Signature · 1 line
2.50.1 (Apple Git-155)