[PATCH] media: ov5640: Initialize mutex before probe cleanup

Runyu Xiao posted 1 patch 2 weeks, 1 day ago
There is a newer version of this series
drivers/media/i2c/ov5640.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
[PATCH] media: ov5640: Initialize mutex before probe cleanup
Posted by Runyu Xiao 2 weeks, 1 day ago
ov5640_probe() can jump to entity_cleanup when regulator setup fails,
before sensor->lock is initialized. The cleanup path unconditionally
destroys the mutex, so this error path passes an uninitialized mutex to
mutex_destroy().

Initialize the mutex after media_entity_pads_init() succeeds and before
ov5640_get_regulators(). This keeps earlier direct-return paths outside the
mutex lifetime while ensuring that every path reaching mutex_destroy() has
initialized the mutex.

Fixes: 19a81c1426c1 ("[media] add Omnivision OV5640 sensor driver")
Cc: stable@vger.kernel.org

Found by PatchProof Module 4 candidates
PILOT-4c6b03072e508260 and PILOT-4f9a36d855eb7c54.

No runtime reproducer was available; the failure path was verified by
inspection and the affected object was built successfully.

Tested-by: Runyu Xiao <runyu.xiao@seu.edu.cn>
Assisted-by: LLM Codex
Signed-off-by: Runyu Xiao <runyu.xiao@seu.edu.cn>
---
 drivers/media/i2c/ov5640.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/media/i2c/ov5640.c b/drivers/media/i2c/ov5640.c
index 8deb5f550..36c8bb345 100644
--- a/drivers/media/i2c/ov5640.c
+++ b/drivers/media/i2c/ov5640.c
@@ -3930,12 +3930,12 @@ static int ov5640_probe(struct i2c_client *client)
 	if (ret)
 		return ret;
 
+	mutex_init(&sensor->lock);
+
 	ret = ov5640_get_regulators(sensor);
 	if (ret)
 		goto entity_cleanup;
 
-	mutex_init(&sensor->lock);
-
 	ret = ov5640_init_controls(sensor);
 	if (ret)
 		goto entity_cleanup;
-- 
2.34.1
Re: [PATCH] media: ov5640: Initialize mutex before probe cleanup
Posted by Frank Li 1 week, 6 days ago
On Thu, Sep 10, 2026 at 04:16:08PM +0800, Runyu Xiao wrote:
> ov5640_probe() can jump to entity_cleanup when regulator setup fails,
> before sensor->lock is initialized. The cleanup path unconditionally
> destroys the mutex, so this error path passes an uninitialized mutex to
> mutex_destroy().
>
> Initialize the mutex after media_entity_pads_init() succeeds and before
> ov5640_get_regulators(). This keeps earlier direct-return paths outside the
> mutex lifetime while ensuring that every path reaching mutex_destroy() has
> initialized the mutex.
>
> Fixes: 19a81c1426c1 ("[media] add Omnivision OV5640 sensor driver")
> Cc: stable@vger.kernel.org
>
> Found by PatchProof Module 4 candidates
> PILOT-4c6b03072e508260 and PILOT-4f9a36d855eb7c54.
>
> No runtime reproducer was available; the failure path was verified by
> inspection and the affected object was built successfully.
>
> Tested-by: Runyu Xiao <runyu.xiao@seu.edu.cn>
> Assisted-by: LLM Codex
> Signed-off-by: Runyu Xiao <runyu.xiao@seu.edu.cn>
> ---
>  drivers/media/i2c/ov5640.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/media/i2c/ov5640.c b/drivers/media/i2c/ov5640.c
> index 8deb5f550..36c8bb345 100644
> --- a/drivers/media/i2c/ov5640.c
> +++ b/drivers/media/i2c/ov5640.c
> @@ -3930,12 +3930,12 @@ static int ov5640_probe(struct i2c_client *client)
>  	if (ret)
>  		return ret;
>
> +	mutex_init(&sensor->lock);
> +

Please use devm_mutex_init(); mutex need do tear down work if enable lock
debug.

Frank

>  	ret = ov5640_get_regulators(sensor);
>  	if (ret)
>  		goto entity_cleanup;
>
> -	mutex_init(&sensor->lock);
> -
>  	ret = ov5640_init_controls(sensor);
>  	if (ret)
>  		goto entity_cleanup;
> --
> 2.34.1
>
Re: [PATCH] media: ov5640: Initialize mutex before probe cleanup
Posted by Sakari Ailus 1 week, 6 days ago
Hi Runyu,

On Thu, Sep 10, 2026 at 04:16:08PM +0800, Runyu Xiao wrote:
> ov5640_probe() can jump to entity_cleanup when regulator setup fails,
> before sensor->lock is initialized. The cleanup path unconditionally
> destroys the mutex, so this error path passes an uninitialized mutex to
> mutex_destroy().
> 
> Initialize the mutex after media_entity_pads_init() succeeds and before
> ov5640_get_regulators(). This keeps earlier direct-return paths outside the
> mutex lifetime while ensuring that every path reaching mutex_destroy() has
> initialized the mutex.

A single sentence should be enough to describe what this patch does.

> 
> Fixes: 19a81c1426c1 ("[media] add Omnivision OV5640 sensor driver")
> Cc: stable@vger.kernel.org
> 
> Found by PatchProof Module 4 candidates
> PILOT-4c6b03072e508260 and PILOT-4f9a36d855eb7c54.
> 
> No runtime reproducer was available; the failure path was verified by
> inspection and the affected object was built successfully.

This paragraph I'd drop, too. Please keep the tags together.

> 
> Tested-by: Runyu Xiao <runyu.xiao@seu.edu.cn>
> Assisted-by: LLM Codex
> Signed-off-by: Runyu Xiao <runyu.xiao@seu.edu.cn>
> ---
>  drivers/media/i2c/ov5640.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/media/i2c/ov5640.c b/drivers/media/i2c/ov5640.c
> index 8deb5f550..36c8bb345 100644
> --- a/drivers/media/i2c/ov5640.c
> +++ b/drivers/media/i2c/ov5640.c
> @@ -3930,12 +3930,12 @@ static int ov5640_probe(struct i2c_client *client)
>  	if (ret)
>  		return ret;
>  
> +	mutex_init(&sensor->lock);
> +
>  	ret = ov5640_get_regulators(sensor);
>  	if (ret)
>  		goto entity_cleanup;
>  
> -	mutex_init(&sensor->lock);
> -
>  	ret = ov5640_init_controls(sensor);
>  	if (ret)
>  		goto entity_cleanup;

-- 
Regards,

Sakari Ailus
[PATCH v2] media: ov5640: Initialize mutex before probe cleanup
Posted by Runyu Xiao 1 week ago
ov5640_probe() can jump to entity_cleanup when regulator setup fails,
before sensor->lock is initialized. The cleanup path unconditionally
destroys the mutex, so this error path passes an uninitialized mutex to
mutex_destroy().

Initialize the mutex after media_entity_pads_init() succeeds and before
ov5640_get_regulators(). This keeps earlier direct-return paths outside
the mutex lifetime while ensuring that every path reaching entity_cleanup
has initialized the mutex.

Use devm_mutex_init() so the mutex is destroyed by the device-managed
cleanup path when lock debugging is enabled. Remove the manual
mutex_destroy() calls from probe cleanup and remove.

Fixes: 19a81c1426c1 ("[media] add Omnivision OV5640 sensor driver")
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/linux-media/20260910081608.1264077-1-runyu.xiao@seu.edu.cn/
Found by PatchProof Module 4 candidates
PILOT-4c6b03072e508260 and PILOT-4f9a36d855eb7c54.

No runtime reproducer was available; the failure path was verified by
inspection and the affected object was built successfully.

Tested-by: Runyu Xiao <runyu.xiao@seu.edu.cn>
Assisted-by: LLM Codex
Signed-off-by: Runyu Xiao <runyu.xiao@seu.edu.cn>
---
v2:
- Use devm_mutex_init() and remove manual mutex destruction as requested
  by the reviewer.

 drivers/media/i2c/ov5640.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/media/i2c/ov5640.c b/drivers/media/i2c/ov5640.c
index 8deb5f550..7f3af6a59 100644
--- a/drivers/media/i2c/ov5640.c
+++ b/drivers/media/i2c/ov5640.c
@@ -3930,11 +3930,13 @@ static int ov5640_probe(struct i2c_client *client)
 	if (ret)
 		return ret;
 
-	ret = ov5640_get_regulators(sensor);
+	ret = devm_mutex_init(dev, &sensor->lock);
 	if (ret)
 		goto entity_cleanup;
 
-	mutex_init(&sensor->lock);
+	ret = ov5640_get_regulators(sensor);
+	if (ret)
+		goto entity_cleanup;
 
 	ret = ov5640_init_controls(sensor);
 	if (ret)
@@ -3972,7 +3974,6 @@ static int ov5640_probe(struct i2c_client *client)
 	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
 entity_cleanup:
 	media_entity_cleanup(&sensor->sd.entity);
-	mutex_destroy(&sensor->lock);
 	return ret;
 }
 
@@ -3990,7 +3991,6 @@ static void ov5640_remove(struct i2c_client *client)
 	v4l2_async_unregister_subdev(&sensor->sd);
 	media_entity_cleanup(&sensor->sd.entity);
 	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
-	mutex_destroy(&sensor->lock);
 }
 
 static const struct dev_pm_ops ov5640_pm_ops = {
-- 
2.34.1