Skip to contentSkip to conversation

media: i2c: ov7740: Fix remove() teardown and PM runtime

PATCH In review Biren Pandya 2 participants 7 messages Jun 15 – Jul 8 1 message not in the archive

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

cover media: i2c: ov7740: Fix remove() teardown and PM runtime

This series addresses two severe issues in the teardown path of the ov7740 image sensor driver.

The first patch addresses a kernel panic caused by a use-after-destroy on the driver's mutex, exacerbated by a double-free of both the mutex and the V4L2 control handler.

The second patch modernizes the runtime PM sleep sequence by properly checking the return value of PM wake operations before issuing I2C power down sequences, preventing unbalanced PM tracking or hangs on disconnected devices.

Biren Pandya (2):
media: i2c: ov7740: Fix use-after-destroy in remove()
media: i2c: ov7740: Fix unchecked pm_runtime_get_sync() in remove()

 drivers/media/i2c/ov7740.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
Signature · 1 line
2.50.1 (Apple Git-155)
#1 lore raw

PATCH 1/2 media: i2c: ov7740: Fix use-after-destroy in remove()

The ov7740_remove() function had a severe teardown order bug where it destroyed the driver's mutex before freeing the V4L2 control handler which relies on that mutex, leading to a use-after-destroy kernel panic. Furthermore, the driver explicitly called v4l2_ctrl_handler_free() and mutex_destroy() sequentially, but then called ov7740_free_controls() which invokes both of them a second time, resulting in a double-free.

This patch fixes the issue by unregistering the subdevice first, and relying exclusively on ov7740_free_controls() to safely tear down the mutex and control handler in the correct order.

Fixes 39c5c4471b8d ("media: i2c: Add the ov7740 image sensor driver")
---
 drivers/media/i2c/ov7740.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/media/i2c/ov7740.c +1 -3
index 632fb80469be..62c124a1353a 100644
@@ -1116,10 +1116,8 @@ static void ov7740_remove(struct i2c_client *client)
 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
 	struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
 
-	mutex_destroy(&ov7740->mutex);
-	v4l2_ctrl_handler_free(ov7740->subdev.ctrl_handler);
-	media_entity_cleanup(&ov7740->subdev.entity);
 	v4l2_async_unregister_subdev(sd);
+	media_entity_cleanup(&ov7740->subdev.entity);
 	ov7740_free_controls(ov7740);
 
 	pm_runtime_get_sync(&client->dev);
Signature · 1 line
2.50.1 (Apple Git-155)
maintainer
#2 lore raw

Re: [PATCH 1/2] media: i2c: ov7740: Fix use-after-destroy in remove()

Hi Biren,

Quoted 7 lines · Biren Pandya

The ov7740_remove() function had a severe teardown order bug where it destroyed the driver's mutex before freeing the V4L2 control handler which relies on that mutex, leading to a use-after-destroy kernel panic. Furthermore, the driver explicitly called v4l2_ctrl_handler_free() and mutex_destroy() sequentially, but then called ov7740_free_controls() which invokes both of them a second time, resulting in a double-free.

Quoted 6 lines

This patch fixes the issue by unregistering the subdevice first, and relying exclusively on ov7740_free_controls() to safely tear down the mutex and control handler in the correct order.

Fixes 39c5c4471b8d ("media: i2c: Add the ov7740 image sensor driver")

Can you add Cc: stable... for v2, please?

Quoted 20 lines
---
 drivers/media/i2c/ov7740.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/media/i2c/ov7740.c +1 -3
index 632fb80469be..62c124a1353a 100644
@@ -1116,10 +1116,8 @@ static void ov7740_remove(struct i2c_client *client)
 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
 	struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
 
-	mutex_destroy(&ov7740->mutex);
-	v4l2_ctrl_handler_free(ov7740->subdev.ctrl_handler);
-	media_entity_cleanup(&ov7740->subdev.entity);
 	v4l2_async_unregister_subdev(sd);
+	media_entity_cleanup(&ov7740->subdev.entity);
 	ov7740_free_controls(ov7740);
 
 	pm_runtime_get_sync(&client->dev);
Signature · 3 lines
Kind regards,

Sakari Ailus
#3 lore raw

PATCH 2/2 media: i2c: ov7740: Fix unchecked pm_runtime_get_sync() in remove()

In reply to Biren Pandya

The ov7740_remove() function unconditionally called pm_runtime_get_sync() but completely ignored the return value. If the device was already in an error state or disconnected, this could lead to an unbalanced PM runtime usage count or attempt to communicate with an unresponsive device.

Replace pm_runtime_get_sync() with pm_runtime_resume_and_get() to ensure the device actually resumed before attempting to issue I2C power-off commands, and safely put the PM runtime usage counter.

---
 drivers/media/i2c/ov7740.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/media/i2c/ov7740.c +6 -3
index 62c124a1353a..6b81882da307 100644
@@ -1115,17 +1115,20 @@ static void ov7740_remove(struct i2c_client *client)
 {
 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
 	struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
+	int ret;
 
 	v4l2_async_unregister_subdev(sd);
 	media_entity_cleanup(&ov7740->subdev.entity);
 	ov7740_free_controls(ov7740);
 
-	pm_runtime_get_sync(&client->dev);
+	ret = pm_runtime_resume_and_get(&client->dev);
 	pm_runtime_disable(&client->dev);
 	pm_runtime_set_suspended(&client->dev);
-	pm_runtime_put_noidle(&client->dev);
 
-	ov7740_set_power(ov7740, 0);
+	if (ret >= 0) {
+		ov7740_set_power(ov7740, 0);
+		pm_runtime_put_noidle(&client->dev);
+	}
 }
 
 static int __maybe_unused ov7740_runtime_suspend(struct device *dev)
Signature · 1 line
2.50.1 (Apple Git-155)
maintainer
#4 lore raw

Re: [PATCH 2/2] media: i2c: ov7740: Fix unchecked pm_runtime_get_sync() in remove()

Hi Biren,

Quoted 29 lines · Biren Pandya

The ov7740_remove() function unconditionally called pm_runtime_get_sync() but completely ignored the return value. If the device was already in an error state or disconnected, this could lead to an unbalanced PM runtime usage count or attempt to communicate with an unresponsive device.

Replace pm_runtime_get_sync() with pm_runtime_resume_and_get() to ensure the device actually resumed before attempting to issue I2C power-off commands, and safely put the PM runtime usage counter.

---
 drivers/media/i2c/ov7740.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/media/i2c/ov7740.c +1 -1
index 62c124a1353a..6b81882da307 100644
@@ -1115,17 +1115,20 @@ static void ov7740_remove(struct i2c_client *client)
 {
 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
 	struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
+	int ret;
 
 	v4l2_async_unregister_subdev(sd);
 	media_entity_cleanup(&ov7740->subdev.entity);
 	ov7740_free_controls(ov7740);
 
-	pm_runtime_get_sync(&client->dev);

drivers/media/i2c/ov7740.c static void ov7740_remove(struct i2c_client *client) · line 1131

Quoted 10 lines
drivers/media/i2c/ov7740.c +5 -2
+	ret = pm_runtime_resume_and_get(&client->dev);
 	pm_runtime_disable(&client->dev);
 	pm_runtime_set_suspended(&client->dev);
-	pm_runtime_put_noidle(&client->dev);
 
-	ov7740_set_power(ov7740, 0);
+	if (ret >= 0) {
+		ov7740_set_power(ov7740, 0);
+		pm_runtime_put_noidle(&client->dev);
+	}

Could you do what's done in other drivers, i.e. disable Runtime PM and then see if the device is active and if so, power it off?

Signature · 3 lines
Regards,

Sakari Ailus

A message that is not in the archive (sent off-list or not yet received): [email protected]

#6 lore raw

PATCH v2 1/2 media: i2c: ov7740: fix unbalanced pm_runtime_get_sync in remove

The ov7740_remove() function unconditionally called pm_runtime_get_sync() but completely ignored the return value. If the device was already in an error state or disconnected, this could lead to an unbalanced PM runtime usage count or attempt to communicate with an unresponsive device.

Disable Runtime PM first, then see if the device is active, and if so, power it off.

---
v2: disabled PM before conditionally powering off as per standard pattern (Sakari).

---
 drivers/media/i2c/ov7740.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/media/i2c/ov7740.c +2 -4
index c2e02f1918169..2d29147c0f647 100644
@@ -1122,12 +1122,10 @@ static void ov7740_remove(struct i2c_client *client)
 	v4l2_async_unregister_subdev(sd);
 	ov7740_free_controls(ov7740);
 
-	pm_runtime_get_sync(&client->dev);
 	pm_runtime_disable(&client->dev);
+	if (!pm_runtime_status_suspended(&client->dev))
+		ov7740_set_power(ov7740, 0);
 	pm_runtime_set_suspended(&client->dev);
-	pm_runtime_put_noidle(&client->dev);
-
-	ov7740_set_power(ov7740, 0);
 }
 
 static int __maybe_unused ov7740_runtime_suspend(struct device *dev)
Signature · 1 line
2.50.1 (Apple Git-155)
#7 lore raw

PATCH v2 2/2 media: i2c: ov7740: fix use-after-destroy in remove

In reply to a message not in the archive

The ov7740_remove() function had a severe teardown order bug where it destroyed the driver's mutex before freeing the V4L2 control handler which relies on that mutex, leading to a use-after-destroy kernel panic. Furthermore, the driver explicitly called v4l2_ctrl_handler_free() and mutex_destroy() sequentially, but then called ov7740_free_controls() which invokes both of them a second time, resulting in a double-free.

This patch fixes the issue by unregistering the subdevice first, and relying exclusively on ov7740_free_controls() to safely tear down the mutex and control handler in the correct order.

Fixes 39c5c4471b8d ("media: i2c: Add the ov7740 image sensor driver")

---
v2: added Fixes and Cc stable tags.

---
 drivers/media/i2c/ov7740.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/media/i2c/ov7740.c +1 -3
index 2d29147c0f647..b4e14171556f9 100644
@@ -1116,10 +1116,8 @@ static void ov7740_remove(struct i2c_client *client)
 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
 	struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
 
-	mutex_destroy(&ov7740->mutex);
-	v4l2_ctrl_handler_free(ov7740->subdev.ctrl_handler);
-	media_entity_cleanup(&ov7740->subdev.entity);
 	v4l2_async_unregister_subdev(sd);
+	media_entity_cleanup(&ov7740->subdev.entity);
 	ov7740_free_controls(ov7740);
 
 	pm_runtime_disable(&client->dev);
Signature · 1 line
2.50.1 (Apple Git-155)