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

From: Runyu Xiao

Date: Fri Sep 18 2026 - 02:26:29 EST


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@xxxxxxxxxxxxxxx
Link: https://lore.kernel.org/linux-media/20260910081608.1264077-1-runyu.xiao@xxxxxxxxxx/
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@xxxxxxxxxx>
Assisted-by: LLM Codex
Signed-off-by: Runyu Xiao <runyu.xiao@xxxxxxxxxx>
---
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