Skip to contentSkip to conversation

media: imx: Unregister csc/scaler only if registered

PATCH In review Ezequiel Garcia 2 participants 5 messages Jan 4, 2021 – Jan 5, 2021

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

PATCH v2 1/3 media: imx: Unregister csc/scaler only if registered

The csc/scaler device pointer (imxmd->m2m_vdev) is assigned after the imx media device v4l2-async probe completes, therefore we need to check if the device is non-NULL before trying to unregister it.

This can be the case if the non-completed imx media device is unbinded (or the driver is removed), leading to a kernel oops.

Fixes a8ef0488cc59 ("media: imx: add csc/scaler mem2mem device")

---
v2:
* As noted by Philipp, set m2m_vdev to NULL if the csc/scaler registration fails.

---
 drivers/staging/media/imx/imx-media-dev.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/staging/media/imx/imx-media-dev.c +6 -1
index 6d2205461e56..338b8bd0bb07 100644
@@ -53,6 +53,7 @@ static int imx6_media_probe_complete(struct v4l2_async_notifier *notifier)
 	imxmd->m2m_vdev = imx_media_csc_scaler_device_init(imxmd);
 	if (IS_ERR(imxmd->m2m_vdev)) {
 		ret = PTR_ERR(imxmd->m2m_vdev);
+		imxmd->m2m_vdev = NULL;
 		goto unlock;
 	}
 
@@ -107,10 +108,14 @@ static int imx_media_remove(struct platform_device *pdev)
 
 	v4l2_info(&imxmd->v4l2_dev, "Removing imx-media\n");
 
+	if (imxmd->m2m_vdev) {
+		imx_media_csc_scaler_device_unregister(imxmd->m2m_vdev);
+		imxmd->m2m_vdev = NULL;
+	}
+
 	v4l2_async_notifier_unregister(&imxmd->notifier);
 	imx_media_unregister_ipu_internal_subdevs(imxmd);
 	v4l2_async_notifier_cleanup(&imxmd->notifier);
-	imx_media_csc_scaler_device_unregister(imxmd->m2m_vdev);
 	media_device_unregister(&imxmd->md);
 	v4l2_device_unregister(&imxmd->v4l2_dev);
 	media_device_cleanup(&imxmd->md);
Signature · 1 line
2.29.2
#1 lore raw

PATCH v2 2/3 media: imx: Fix csc/scaler unregister

The csc/scaler device private struct is released by ipu_csc_scaler_video_device_release(), which can be called by video_unregister_device() if there are no users of the underlying struct video device.

Therefore, the mutex can't be held when calling video_unregister_device() as its memory may be freed by it, leading to a kernel oops.

Fortunately, the fix is quite simple as no locking is needed when calling video_unregister_device(): v4l2-core already has its own internal locking, and the structures are also properly refcounted.

Fixes a8ef0488cc59 ("media: imx: add csc/scaler mem2mem device")
---
 drivers/staging/media/imx/imx-media-csc-scaler.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/staging/media/imx/imx-media-csc-scaler.c +0 -4
index fab1155a5958..63a0204502a8 100644
@@ -869,11 +869,7 @@ void imx_media_csc_scaler_device_unregister(struct imx_media_video_dev *vdev)
 	struct ipu_csc_scaler_priv *priv = vdev_to_priv(vdev);
 	struct video_device *vfd = priv->vdev.vfd;
 
-	mutex_lock(&priv->mutex);
-
 	video_unregister_device(vfd);
-
-	mutex_unlock(&priv->mutex);
 }
 
 struct imx_media_video_dev *
Signature · 1 line
2.29.2
#2 lore raw

PATCH v2 3/3 media: imx: Clean capture unregister

In reply to Ezequiel Garcia

No locking is needed to call video_unregister_device(). Drop it.

Also, drop the superfluous video_is_registered() call, which is done by video_unregister_device(), and re-order media_entity_cleanup() and video_unregister_device() calls.

---
v2:
* As suggested by Philipp, write a more complete commit description.

---
 drivers/staging/media/imx/imx-media-capture.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
drivers/staging/media/imx/imx-media-capture.c +2 -8
index c1931eb2540e..e10ce103a5b4 100644
@@ -816,14 +816,8 @@ void imx_media_capture_device_unregister(struct imx_media_video_dev *vdev)
 	struct capture_priv *priv = to_capture_priv(vdev);
 	struct video_device *vfd = priv->vdev.vfd;
 
-	mutex_lock(&priv->mutex);
-
-	if (video_is_registered(vfd)) {
-		video_unregister_device(vfd);
-		media_entity_cleanup(&vfd->entity);
-	}
-
-	mutex_unlock(&priv->mutex);
+	media_entity_cleanup(&vfd->entity);
+	video_unregister_device(vfd);
 }
 EXPORT_SYMBOL_GPL(imx_media_capture_device_unregister);
 
Signature · 1 line
2.29.2
maintainer Reviewed-by on v2 3/3
#3 lore raw

Re: [PATCH v2 3/3] media: imx: Clean capture unregister

Quoted 7 lines · Ezequiel Garcia

No locking is needed to call video_unregister_device(). Drop it.

Also, drop the superfluous video_is_registered() call, which is done by video_unregister_device(), and re-order media_entity_cleanup() and video_unregister_device() calls.

regards
Philipp

maintainer Reviewed-by on v2 1/3
#4 lore raw
In reply to Ezequiel Garcia
Quoted 10 lines · Ezequiel Garcia

The csc/scaler device pointer (imxmd->m2m_vdev) is assigned after the imx media device v4l2-async probe completes, therefore we need to check if the device is non-NULL before trying to unregister it.

This can be the case if the non-completed imx media device is unbinded (or the driver is removed), leading to a kernel oops.

Fixes a8ef0488cc59 ("media: imx: add csc/scaler mem2mem device")

regards
Philipp