media: imx: Unregister csc/scaler only if registered
PATCH 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.
--- drivers/staging/media/imx/imx-media-dev.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/staging/media/imx/imx-media-dev.c +5 -1
@@ -107,10 +107,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
PATCH 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.
--- 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
@@ -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
Re: [PATCH 2/3] media: imx: Fix csc/scaler unregister
Quoted 26 lines · Ezequiel Garcia
The csc/scaler device private struct is released by
ipu_csc_scaler_video_device_release(), which can be called byvideo_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")Signed-off-by Ezequiel Garcia <[email protected]>--- 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
-0@@ -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);
drivers/staging/media/imx/imx-media-csc-scaler.c void imx_media_csc_scaler_device_unregister(struct imx_media_video_dev *vdev) · line 875
Quoted 10 lines
drivers/staging/media/imx/imx-media-csc-scaler.c +0
-4struct video_device *vfd = priv->vdev.vfd; -mutex_lock (&priv->mutex); -video_unregister_device (vfd); - -mutex_unlock (&priv->mutex); } struct imx_media_video_dev *
regards
Philipp
PATCH 3/3 media: imx: Clean capture unregister
In reply to Ezequiel GarciaNo locking is needed to call video_unregister_device(). Drop it.
--- 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
@@ -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
Philipp Zabel
maintainerRe: [PATCH 3/3] media: imx: Clean capture unregister
Quoted 1 line · Ezequiel Garcia
No locking is needed to call
video_unregister_device(). Drop it.
The commit message should probably also mention that the superfluous video_is_registered() check is dropped and media_entity_cleanup() and video_unregister_device() are reordered.
Quoted 15 lines
Signed-off-by Ezequiel Garcia <[email protected]>--- drivers/staging/media/imx/imx-media-capture.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)drivers/staging/media/imx/imx-media-capture.c +0
-2@@ -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); -
drivers/staging/media/imx/imx-media-capture.c void imx_media_capture_device_unregister(struct imx_media_video_dev *vdev) · line 822
Quoted 10 lines
drivers/staging/media/imx/imx-media-capture.c +2
-6-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);
regards
Philipp
Philipp Zabel
maintainerHi Ezequiel,
Quoted 19 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")Signed-off-by Ezequiel Garcia <[email protected]>--- drivers/staging/media/imx/imx-media-dev.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)drivers/staging/media/imx/imx-media-dev.c +0
-0
drivers/staging/media/imx/imx-media-dev.c static int imx_media_remove(struct platform_device *pdev) · line 110
Quoted 4 lines
drivers/staging/media/imx/imx-media-dev.c +1
-0@@ -107,10 +107,14 @@ static int imx_media_remove(struct platform_device *pdev)v4l2_info (&imxmd->v4l2_dev,"Removing imx-media\n" ); +if (imxmd->m2m_vdev) {
Thank you, it's even worse. If imx_media_csc_scaler_device_init() fails in imx6_media_probe_complete(), imxmd->m2m_vdev contains an error value.
So either this should check
if (!IS_ERR_OR_NULL(imxmd->m2m_vdev))
or (probably better) probe_complete should be changed as well.
drivers/staging/media/imx/imx-media-dev.c static int imx_media_remove(struct platform_device *pdev) · line 120
Quoted 11 lines
drivers/staging/media/imx/imx-media-dev.c +4
-1+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);
regards
Philipp
Hello Philipp,
Thanks for the quick review.
Quoted 30 lines · Philipp Zabel
Hi Ezequiel,
Quoted 19 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")Signed-off-by Ezequiel Garcia <[email protected]>---
drivers/staging/media/imx/imx-media-dev.c | 6 +++++-1 file changed, 5 insertions(+), 1 deletion(-)drivers/staging/media/imx/imx-media-dev.c +0
-0@@ -107,10 +107,14 @@ static int imx_media_remove(struct platform_device *pdev)Quoted 4 lines
v4l2_info(&imxmd->v4l2_dev, "Removing imx-media\n");
+ if (imxmd->m2m_vdev) {Thank you, it's even worse. If
imx_media_csc_scaler_device_init()fails inimx6_media_probe_complete(),imxmd->m2m_vdevcontains an error value.
Quoted 6 lines
So either this should check
if (!IS_ERR_OR_NULL(imxmd->m2m_vdev))
or (probably better)
probe_completeshould be changed as well.
Good catch. I guess the simplest solution for that is to clear the m2m_vdev pointer in imx6_media_probe_complete, if it fails.
Thanks,
Ezequiel