Skip to contentSkip to conversation

media: v4l2-ctrls: Don't reset handler's error in v4l2_ctrl_handler_free()

PATCH Superseded Sakari Ailus 3 participants 6 messages May 8, 2025 – May 21, 2025 Media (V4L/DVB)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 media: v4l2-ctrls: Don't reset handler's error in v4l2_ctrl_handler_free()

It's a common pattern in drivers to free the control handler's resources and then return the handler's error code on drivers' error handling paths. Alas, the v4l2_ctrl_handler_free() function also zeroes the error field, effectively indicating successful return to the caller.

There's no apparent need to touch the error field while releasing the control handler's resources and cleaning up stale pointers. Not touching the handler's error field is a more certain way to address this problem than changing all the users, in which case the pattern would be likely to re-emerge in new drivers.

Do just that, don't touch the control handler's error field in v4l2_ctrl_handler_free().

---
Hi all,

This is currently affecting the following drivers:

	drivers/media/i2c/imx334.c
	drivers/media/i2c/imx335.c
	drivers/media/i2c/imx412.c
	drivers/media/i2c/ov2740.c
	drivers/media/i2c/ov5675.c
	drivers/media/i2c/ov9282.c
	drivers/media/pci/tw68/tw68-core.c
	drivers/media/platform/samsung/s5p-jpeg/jpeg-core.c
	drivers/media/platform/samsung/s3c-camif/camif-capture.c
	drivers/media/platform/verisilicon/hantro_drv.c
	drivers/media/test-drivers/visl/visl-core.c

The fact that one of them is a test driver tells a lot. :-)

The patch is untested. If we agree to do this, the patch should probably be cc'd to stable, too.

- Sakari

 drivers/media/v4l2-core/v4l2-ctrls-core.c | 1 -
 1 file changed, 1 deletion(-)
drivers/media/v4l2-core/v4l2-ctrls-core.c +0 -1
index 90d25329661e..932aedc26049 100644
@@ -1661,7 +1661,6 @@ void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
 	kvfree(hdl->buckets);
 	hdl->buckets = NULL;
 	hdl->cached = NULL;
-	hdl->error = 0;
 	mutex_unlock(hdl->lock);
 	mutex_destroy(&hdl->_lock);
 }
Signature · 1 line
2.39.5
#1 lore raw
Quoted 5 lines · Sakari Ailus

It's a common pattern in drivers to free the control handler's resources and then return the handler's error code on drivers' error handling paths. Alas, the v4l2_ctrl_handler_free() function also zeroes the error field, effectively indicating successful return to the caller.

Quoted 8 lines

There's no apparent need to touch the error field while releasing the control handler's resources and cleaning up stale pointers. Not touching the handler's error field is a more certain way to address this problem than changing all the users, in which case the pattern would be likely to re-emerge in new drivers.

Do just that, don't touch the control handler's error field in v4l2_ctrl_handler_free().

I think with this change the control framework documentation should be
updated: Documentation/driver-api/media/v4l2-controls.rst

There is a code example on line 197 that can be converted to the new approach.

Also document that the error field is not touched by v4l2_ctrl_handler_free() in include/media/v4l2-ctrls.h.

Quoted 7 lines

---
Hi all,

This is currently affecting the following drivers:

Quoted 11 lines
	drivers/media/i2c/imx334.c
	drivers/media/i2c/imx335.c
	drivers/media/i2c/imx412.c
	drivers/media/i2c/ov2740.c
	drivers/media/i2c/ov5675.c
	drivers/media/i2c/ov9282.c
	drivers/media/pci/tw68/tw68-core.c
	drivers/media/platform/samsung/s5p-jpeg/jpeg-core.c
	drivers/media/platform/samsung/s3c-camif/camif-capture.c
	drivers/media/platform/verisilicon/hantro_drv.c
	drivers/media/test-drivers/visl/visl-core.c

also:

drivers/input/touchscreen/sur40.c
drivers/media/i2c/adv748x

In any case, this looks good to me.

Regards,

Hans

Quoted 23 lines

The fact that one of them is a test driver tells a lot. :-)

The patch is untested. If we agree to do this, the patch should probably be cc'd to stable, too.

- Sakari

 drivers/media/v4l2-core/v4l2-ctrls-core.c | 1 -
 1 file changed, 1 deletion(-)
drivers/media/v4l2-core/v4l2-ctrls-core.c +0 -1
index 90d25329661e..932aedc26049 100644
@@ -1661,7 +1661,6 @@ void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
 	kvfree(hdl->buckets);
 	hdl->buckets = NULL;
 	hdl->cached = NULL;
-	hdl->error = 0;
 	mutex_unlock(hdl->lock);
 	mutex_destroy(&hdl->_lock);
 }
#2 lore raw
Quoted 12 lines · Hans Verkuil
Quoted 11 lines · Sakari Ailus

It's a common pattern in drivers to free the control handler's resources and then return the handler's error code on drivers' error handling paths. Alas, the v4l2_ctrl_handler_free() function also zeroes the error field, effectively indicating successful return to the caller.

There's no apparent need to touch the error field while releasing the control handler's resources and cleaning up stale pointers. Not touching the handler's error field is a more certain way to address this problem than changing all the users, in which case the pattern would be likely to re-emerge in new drivers.

Quoted 8 lines
Quoted 2 lines

Do just that, don't touch the control handler's error field in v4l2_ctrl_handler_free().

I think with this change the control framework documentation should be
updated: Documentation/driver-api/media/v4l2-controls.rst

There is a code example on line 197 that can be converted to the new approach.

And I think section 1.4 in that rst file should also explicitly mention that v4l2_ctrl_handler_free doesn't touch the error field.

Regards,

Hans

Quoted 59 lines

Also document that the error field is not touched by v4l2_ctrl_handler_free() in include/media/v4l2-ctrls.h.

Quoted 7 lines

---
Hi all,

This is currently affecting the following drivers:

Quoted 11 lines
	drivers/media/i2c/imx334.c
	drivers/media/i2c/imx335.c
	drivers/media/i2c/imx412.c
	drivers/media/i2c/ov2740.c
	drivers/media/i2c/ov5675.c
	drivers/media/i2c/ov9282.c
	drivers/media/pci/tw68/tw68-core.c
	drivers/media/platform/samsung/s5p-jpeg/jpeg-core.c
	drivers/media/platform/samsung/s3c-camif/camif-capture.c
	drivers/media/platform/verisilicon/hantro_drv.c
	drivers/media/test-drivers/visl/visl-core.c

also:

drivers/input/touchscreen/sur40.c
drivers/media/i2c/adv748x

In any case, this looks good to me.

Regards,

Hans

Quoted 23 lines

The fact that one of them is a test driver tells a lot. :-)

The patch is untested. If we agree to do this, the patch should probably be cc'd to stable, too.

- Sakari

 drivers/media/v4l2-core/v4l2-ctrls-core.c | 1 -
 1 file changed, 1 deletion(-)
drivers/media/v4l2-core/v4l2-ctrls-core.c +0 -1
index 90d25329661e..932aedc26049 100644
@@ -1661,7 +1661,6 @@ void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
 	kvfree(hdl->buckets);
 	hdl->buckets = NULL;
 	hdl->cached = NULL;
-	hdl->error = 0;
 	mutex_unlock(hdl->lock);
 	mutex_destroy(&hdl->_lock);
 }
#3 lore raw
In reply to Sakari Ailus

Hi Sakari,

Thank you for the patch.

Quoted 47 lines · Sakari Ailus

It's a common pattern in drivers to free the control handler's resources and then return the handler's error code on drivers' error handling paths. Alas, the v4l2_ctrl_handler_free() function also zeroes the error field, effectively indicating successful return to the caller.

There's no apparent need to touch the error field while releasing the control handler's resources and cleaning up stale pointers. Not touching the handler's error field is a more certain way to address this problem than changing all the users, in which case the pattern would be likely to re-emerge in new drivers.

Do just that, don't touch the control handler's error field in v4l2_ctrl_handler_free().

---
Hi all,

This is currently affecting the following drivers:

	drivers/media/i2c/imx334.c
	drivers/media/i2c/imx335.c
	drivers/media/i2c/imx412.c
	drivers/media/i2c/ov2740.c
	drivers/media/i2c/ov5675.c
	drivers/media/i2c/ov9282.c
	drivers/media/pci/tw68/tw68-core.c
	drivers/media/platform/samsung/s5p-jpeg/jpeg-core.c
	drivers/media/platform/samsung/s3c-camif/camif-capture.c
	drivers/media/platform/verisilicon/hantro_drv.c
	drivers/media/test-drivers/visl/visl-core.c

The fact that one of them is a test driver tells a lot. :-)

The patch is untested. If we agree to do this, the patch should probably be cc'd to stable, too.

- Sakari

 drivers/media/v4l2-core/v4l2-ctrls-core.c | 1 -
 1 file changed, 1 deletion(-)
drivers/media/v4l2-core/v4l2-ctrls-core.c +0 -0
index 90d25329661e..932aedc26049 100644

drivers/media/v4l2-core/v4l2-ctrls-core.c void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl) · line 1665

Quoted 6 lines
drivers/media/v4l2-core/v4l2-ctrls-core.c +0 -1
@@ -1661,7 +1661,6 @@ void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
 	kvfree(hdl->buckets);
 	hdl->buckets = NULL;
 	hdl->cached = NULL;
-	hdl->error = 0;
 	mutex_unlock(hdl->lock);
 	mutex_destroy(&hdl->_lock);

The function could even return hdl->error, in which case drivers could do

	if (handler->error)
		return v4l2_ctrl_handler_free(handler);

Up to you. The change is good either way.

Quoted 1 line
drivers/media/v4l2-core/v4l2-ctrls-core.c +0 -0
 }
Signature · 3 lines
Regards,

Laurent Pinchart
#4 lore raw
Quoted 62 lines · Laurent Pinchart

Hi Sakari,

Thank you for the patch.

Quoted 47 lines · Sakari Ailus

It's a common pattern in drivers to free the control handler's resources and then return the handler's error code on drivers' error handling paths. Alas, the v4l2_ctrl_handler_free() function also zeroes the error field, effectively indicating successful return to the caller.

There's no apparent need to touch the error field while releasing the control handler's resources and cleaning up stale pointers. Not touching the handler's error field is a more certain way to address this problem than changing all the users, in which case the pattern would be likely to re-emerge in new drivers.

Do just that, don't touch the control handler's error field in v4l2_ctrl_handler_free().

---
Hi all,

This is currently affecting the following drivers:

	drivers/media/i2c/imx334.c
	drivers/media/i2c/imx335.c
	drivers/media/i2c/imx412.c
	drivers/media/i2c/ov2740.c
	drivers/media/i2c/ov5675.c
	drivers/media/i2c/ov9282.c
	drivers/media/pci/tw68/tw68-core.c
	drivers/media/platform/samsung/s5p-jpeg/jpeg-core.c
	drivers/media/platform/samsung/s3c-camif/camif-capture.c
	drivers/media/platform/verisilicon/hantro_drv.c
	drivers/media/test-drivers/visl/visl-core.c

The fact that one of them is a test driver tells a lot. :-)

The patch is untested. If we agree to do this, the patch should probably be cc'd to stable, too.

- Sakari

 drivers/media/v4l2-core/v4l2-ctrls-core.c | 1 -
 1 file changed, 1 deletion(-)
drivers/media/v4l2-core/v4l2-ctrls-core.c +0 -0
index 90d25329661e..932aedc26049 100644

drivers/media/v4l2-core/v4l2-ctrls-core.c void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl) · line 1665

Quoted 6 lines
drivers/media/v4l2-core/v4l2-ctrls-core.c +0 -1
@@ -1661,7 +1661,6 @@ void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
 	kvfree(hdl->buckets);
 	hdl->buckets = NULL;
 	hdl->cached = NULL;
-	hdl->error = 0;
 	mutex_unlock(hdl->lock);
 	mutex_destroy(&hdl->_lock);

The function could even return hdl->error, in which case drivers could do

Quoted 4 lines
	if (handler->error)
		return v4l2_ctrl_handler_free(handler);

Up to you. The change is good either way.

Good idea.

Regards,

Hans

Quoted 3 lines
Quoted 2 lines
drivers/media/v4l2-core/v4l2-ctrls-core.c +0 -0
 }
#5 lore raw
In reply to Laurent Pinchart

Hi Laurent,

Quoted 3 lines · Laurent Pinchart

Hi Sakari,

Thank you for the patch.

Thank you for the review!

Quoted 59 lines · Sakari Ailus
Quoted 47 lines

It's a common pattern in drivers to free the control handler's resources and then return the handler's error code on drivers' error handling paths. Alas, the v4l2_ctrl_handler_free() function also zeroes the error field, effectively indicating successful return to the caller.

There's no apparent need to touch the error field while releasing the control handler's resources and cleaning up stale pointers. Not touching the handler's error field is a more certain way to address this problem than changing all the users, in which case the pattern would be likely to re-emerge in new drivers.

Do just that, don't touch the control handler's error field in v4l2_ctrl_handler_free().

---
Hi all,

This is currently affecting the following drivers:

	drivers/media/i2c/imx334.c
	drivers/media/i2c/imx335.c
	drivers/media/i2c/imx412.c
	drivers/media/i2c/ov2740.c
	drivers/media/i2c/ov5675.c
	drivers/media/i2c/ov9282.c
	drivers/media/pci/tw68/tw68-core.c
	drivers/media/platform/samsung/s5p-jpeg/jpeg-core.c
	drivers/media/platform/samsung/s3c-camif/camif-capture.c
	drivers/media/platform/verisilicon/hantro_drv.c
	drivers/media/test-drivers/visl/visl-core.c

The fact that one of them is a test driver tells a lot. :-)

The patch is untested. If we agree to do this, the patch should probably be cc'd to stable, too.

- Sakari

 drivers/media/v4l2-core/v4l2-ctrls-core.c | 1 -
 1 file changed, 1 deletion(-)
drivers/media/v4l2-core/v4l2-ctrls-core.c +0 -0
index 90d25329661e..932aedc26049 100644

drivers/media/v4l2-core/v4l2-ctrls-core.c void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl) · line 1665

Quoted 6 lines
drivers/media/v4l2-core/v4l2-ctrls-core.c +0 -1
@@ -1661,7 +1661,6 @@ void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
 	kvfree(hdl->buckets);
 	hdl->buckets = NULL;
 	hdl->cached = NULL;
-	hdl->error = 0;
 	mutex_unlock(hdl->lock);
 	mutex_destroy(&hdl->_lock);

The function could even return hdl->error, in which case drivers could do

Quoted 4 lines
	if (handler->error)
		return v4l2_ctrl_handler_free(handler);

Up to you. The change is good either way.

I agree. But I'll add another patch for that. This one should be backported whereas the return type change probably shouldn't.

Signature · 3 lines
Regards,

Sakari Ailus