Re: [PATCH v3 1/2] media: atomisp: fix memory leak in atomisp_pci_probe()

From: Andy Shevchenko

Date: Wed Jun 17 2026 - 05:52:16 EST


On Tue, Jun 16, 2026 at 09:43:18PM +0800, Dawei Feng wrote:
> atomisp_initialize_modules() creates CSI2 and ISP subdev media entities
> before atomisp_pci_probe() registers them. Its counterpart,
> atomisp_uninitialize_modules(), only releases part of that module-owned
> state and leaves some media entity cleanup to the entity unregister path.
>
> That ownership split is incomplete for probe error paths. If
> atomisp_pci_probe() fails after module initialization but before all
> entities are registered, the unwind path cannot rely on unregister
> helpers to release media entity state whose lifetime started in module
> initialization. The CSI2 and ISP subdev media entities can therefore be
> left allocated.
>
> Refactor the cleanup boundary so module cleanup releases media entities
> created by module initialization, while unregister helpers only undo
> registered V4L2 and media device state. Move CSI2 and ISP subdev media
> entity cleanup into atomisp_mipi_csi2_cleanup() and the new
> atomisp_subdev_cleanup(), and run media_device_cleanup() after module
> cleanup in the probe unwind and remove paths.
>
> If atomisp_mipi_csi2_init() itself fails, it has already unwound its
> partial setup, so return the error directly. Only the later
> atomisp_subdev_init() failure path needs to clean up CSI2 from the
> caller.

...

> void atomisp_mipi_csi2_cleanup(struct atomisp_device *isp)
> {
> + unsigned int i;
> +
> + for (i = 0; i < ATOMISP_CAMERA_NR_PORTS; i++)

In case you need a new version

for (unsigned int i = 0; i < ATOMISP_CAMERA_NR_PORTS; i++)

> + media_entity_cleanup(&isp->csi2_port[i].subdev.entity);
> }

Or maybe Sakari can tweak this whilst applying.

--
With Best Regards,
Andy Shevchenko