Skip to contentSkip to conversation

media: i2c: ov5648: Fix a data race on state->streaming

PATCH In review Ginger Li 3 participants 4 messages Sep 23 – Sep 26

Subsystems (2)
lore
Share & tools
lore

Ginger Li

author
#0 lore raw

PATCH 1/1 media: i2c: ov5648: Fix a data race on state->streaming

ov5648_s_stream() updates state->streaming after it has released sensor->mutex, while ov5648_resume() reads that field with sensor->mutex held. A concurrent resume can therefore observe a stale streaming state and leave the sensor in standby while the software believes that it is streaming, or the other way around.

Keep the update inside the sensor->mutex protected section that already programs the standby mode.

Fixes e43ccb0a045f ("media: i2c: Add support for the OV5648 image sensor")
---
 drivers/media/i2c/ov5648.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/i2c/ov5648.c +2 -2
@@ -2145,12 +2145,12 @@ static int ov5648_s_stream(struct v4l2_subdev *subdev,
 
 	mutex_lock(&sensor->mutex);
 	ret = ov5648_sw_standby(sensor, !enable);
+	if (!ret)
+		state->streaming = !!enable;
 	mutex_unlock(&sensor->mutex);
 
 	if (ret)
 		return ret;
-
-	state->streaming = !!enable;
 
 	if (!enable)
 		pm_runtime_put(sensor->dev);
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw

Quoting Ginger Li (2026-09-23 02:56:31)

Quoted 19 lines

ov5648_s_stream() updates state->streaming after it has released sensor->mutex, while ov5648_resume() reads that field with sensor->mutex held. A concurrent resume can therefore observe a stale streaming state and leave the sensor in standby while the software believes that it is streaming, or the other way around.

Keep the update inside the sensor->mutex protected section that already programs the standby mode.

Fixes e43ccb0a045f ("media: i2c: Add support for the OV5648 image sensor")
---
 drivers/media/i2c/ov5648.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

drivers/media/i2c/ov5648.c static int ov5648_s_stream(struct v4l2_subdev *subdev, · line 2149

Quoted 5 lines
drivers/media/i2c/ov5648.c +2 -0
@@ -2145,12 +2145,12 @@ static int ov5648_s_stream(struct v4l2_subdev *subdev,
 
        mutex_lock(&sensor->mutex);
        ret = ov5648_sw_standby(sensor, !enable);
+       if (!ret)
+               state->streaming = !!enable;

I see streaming is accessed in ov5648_suspend and ov5648_resume with the lock held indeed, so I think this makes sense:

Quoted 12 lines
drivers/media/i2c/ov5648.c +0 -2
        mutex_unlock(&sensor->mutex);
 
        if (ret)
                return ret;
-
-       state->streaming = !!enable;
 
        if (!enable)
                pm_runtime_put(sensor->dev);
Signature · 1 line
2.43.0
maintainer
#2 lore raw
In reply to Ginger Li

Hi Ginger,

Quoted 5 lines · Ginger Li

ov5648_s_stream() updates state->streaming after it has released sensor->mutex, while ov5648_resume() reads that field with sensor->mutex held. A concurrent resume can therefore observe a stale streaming state and leave the sensor in standby while the software believes that it is streaming, or the other way around.

Please wrap at 75 characters next time, I've fixed it this time.

Signature · 1 line
Sakari Ailus
author
#3 lore raw

Hi Sakari,

Thanks a lot. I will pay attention next time.

Best regards,
Ginger

Quoted 14 lines · Sakari Ailus

Hi Ginger,

Quoted 5 lines · Ginger Li

ov5648_s_stream() updates state->streaming after it has released sensor->mutex, while ov5648_resume() reads that field with sensor->mutex held. A concurrent resume can therefore observe a stale streaming state and leave the sensor in standby while the software believes that it is streaming, or the other way around.

Please wrap at 75 characters next time, I've fixed it this time.

--
Sakari Ailus