Control handler free changes
v2 cover Control handler free changes
Hi folks,
This set changes the control handler's error code handing when the handler is free and documents it.
since v1:
- Added the Fixes: / Cc: stable tags.
- Added two patches to return the handler's error and document the error isn't changed when the handler is freed.
Sakari Ailus (3):
media: v4l2-ctrls: Don't reset handler's error in
v4l2_ctrl_handler_free()
media: v4l2-ctrls: Return the handler's error in
v4l2_ctrl_handler_free() media: Documentation: Document new v4l2_ctrl_handler_free() behaviour
Documentation/driver-api/media/v4l2-controls.rst | 9 +++------ drivers/media/v4l2-core/v4l2-ctrls-core.c | 7 ++++--- include/media/v4l2-ctrls.h | 7 +++++-- 3 files changed, 12 insertions(+), 11 deletions(-)
Signature · 1 line
2.39.5
PATCH v2 1/3 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().
--- drivers/media/v4l2-core/v4l2-ctrls-core.c | 1 - 1 file changed, 1 deletion(-)
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); }
Signature · 1 line
2.39.5
Re: [PATCH v2 1/3] media: v4l2-ctrls: Don't reset handler's error in v4l2_ctrl_handler_free()
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 6 lines
Do just that, don't touch the control handler's error field in
v4l2_ctrl_handler_free().Fixes 0996517cf8ea ("V4L/DVB: v4l2: Add new control handling framework")Signed-off-by Sakari Ailus <[email protected]>
Quoted 16 lines
--- drivers/media/v4l2-core/v4l2-ctrls-core.c | 1 - 1 file changed, 1 deletion(-)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); }
PATCH v2 2/3 media: v4l2-ctrls: Return the handler's error in v4l2_ctrl_handler_free()
In reply to Sakari Ailusv4l2_ctrl_handler_free() used to return void but changing this to int, returning the handler's error code, enables the drivers to simply return the handler's error in this common error handling pattern:
if (handler->error) return v4l2_ctrl_handler_free(handler);
--- drivers/media/v4l2-core/v4l2-ctrls-core.c | 6 ++++-- include/media/v4l2-ctrls.h | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-)
drivers/media/v4l2-core/v4l2-ctrls-core.c +4 -2
@@ -1631,14 +1631,14 @@ int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl,EXPORT_SYMBOL (v4l2_ctrl_handler_init_class);/* Free all controls and control refs */ -void v4l2_ctrl_handler_free (struct v4l2_ctrl_handler *hdl) +int v4l2_ctrl_handler_free (struct v4l2_ctrl_handler *hdl) {struct v4l2_ctrl_ref *ref, *next_ref;struct v4l2_ctrl *ctrl, *next_ctrl;struct v4l2_subscribed_event *sev, *next_sev;if (hdl ==NULL || hdl->buckets ==NULL ) -return ; +return hdl->error;v4l2_ctrl_handler_free_request (hdl);
@@ -1663,6 +1663,8 @@ void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl) hdl->cached =NULL ;mutex_unlock (hdl->lock);mutex_destroy (&hdl->_lock); + +return hdl->error; }EXPORT_SYMBOL (v4l2_ctrl_handler_free);
include/media/v4l2-ctrls.h +3 -1
@@ -579,8 +579,10 @@ int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl,* @hdl: The control handler. * * Does nothing if @hdl == NULL. +* +* Returns the handler's error field. */ -void v4l2_ctrl_handler_free (struct v4l2_ctrl_handler *hdl); +int v4l2_ctrl_handler_free (struct v4l2_ctrl_handler *hdl);/** * v4l2_ctrl_lock() - Helper function to lock the handler
Signature · 1 line
2.39.5
Re: [PATCH v2 2/3] media: v4l2-ctrls: Return the handler's error in v4l2_ctrl_handler_free()
Quoted 22 lines · Sakari Ailus
v4l2_ctrl_handler_free()used to return void but changing this to int, returning the handler's error code, enables the drivers to simply return the handler's error in this common error handling pattern:if (handler->error) return v4l2_ctrl_handler_free(handler);Suggested-by Laurent Pinchart <[email protected]>Signed-off-by Sakari Ailus <[email protected]>--- drivers/media/v4l2-core/v4l2-ctrls-core.c | 6 ++++-- include/media/v4l2-ctrls.h | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-)drivers/media/v4l2-core/v4l2-ctrls-core.c +0
-0@@ -1631,14 +1631,14 @@ int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl,EXPORT_SYMBOL (v4l2_ctrl_handler_init_class);/* Free all controls and control refs */
drivers/media/v4l2-core/v4l2-ctrls-core.c int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl, · line 1641
Quoted 10 lines
drivers/media/v4l2-core/v4l2-ctrls-core.c +2
-2-void v4l2_ctrl_handler_free (struct v4l2_ctrl_handler *hdl) +int v4l2_ctrl_handler_free (struct v4l2_ctrl_handler *hdl) {struct v4l2_ctrl_ref *ref, *next_ref;struct v4l2_ctrl *ctrl, *next_ctrl;struct v4l2_subscribed_event *sev, *next_sev;if (hdl ==NULL || hdl->buckets ==NULL ) -return ; +return hdl->error;
Hmm, that will crash if hdl == NULL!
I'm not really sure what this should return if hdl == NULL: 0 or some error code.
I'm inclined to just return 0 in that case. If hdl can be NULL, then presumably it is intentional and not an error.
Quoted 17 lines
drivers/media/v4l2-core/v4l2-ctrls-core.c +2
-0v4l2_ctrl_handler_free_request (hdl);@@ -1663,6 +1663,8 @@ void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl) hdl->cached =NULL ;mutex_unlock (hdl->lock);mutex_destroy (&hdl->_lock); + +return hdl->error; }EXPORT_SYMBOL (v4l2_ctrl_handler_free);include/media/v4l2-ctrls.h +0
-0
include/media/v4l2-ctrls.h int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl, · line 581
Quoted 3 lines
include/media/v4l2-ctrls.h +0
-0@@ -579,8 +579,10 @@ int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl,* @hdl: The control handler. * * Does nothing if @hdl == NULL.
Also here it states that it does nothing. So returning 0 is probably best. And document here that it does in fact return 0 if hdl == NULL.
Regards,
Hans
Quoted 8 lines
include/media/v4l2-ctrls.h +3
-1+* +* Returns the handler's error field. */ -void v4l2_ctrl_handler_free (struct v4l2_ctrl_handler *hdl); +int v4l2_ctrl_handler_free (struct v4l2_ctrl_handler *hdl);/** * v4l2_ctrl_lock() - Helper function to lock the handler
Re: [PATCH v2 2/3] media: v4l2-ctrls: Return the handler's error in v4l2_ctrl_handler_free()
Hi Hans,
Quoted 30 lines · Hans Verkuil
Quoted 29 lines · Sakari Ailus
v4l2_ctrl_handler_free()used to return void but changing this to int, returning the handler's error code, enables the drivers to simply return the handler's error in this common error handling pattern:if (handler->error) return v4l2_ctrl_handler_free(handler);Suggested-by Laurent Pinchart <[email protected]>Signed-off-by Sakari Ailus <[email protected]>--- drivers/media/v4l2-core/v4l2-ctrls-core.c | 6 ++++-- include/media/v4l2-ctrls.h | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-)drivers/media/v4l2-core/v4l2-ctrls-core.c +1
-1@@ -1631,14 +1631,14 @@ int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl,EXPORT_SYMBOL (v4l2_ctrl_handler_init_class);/* Free all controls and control refs */ -void v4l2_ctrl_handler_free (struct v4l2_ctrl_handler *hdl) +int v4l2_ctrl_handler_free (struct v4l2_ctrl_handler *hdl) {struct v4l2_ctrl_ref *ref, *next_ref;struct v4l2_ctrl *ctrl, *next_ctrl;struct v4l2_subscribed_event *sev, *next_sev;
Quoted 5 lines
drivers/media/v4l2-core/v4l2-ctrls-core.c int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl, · line 1641
Quoted 3 lines
drivers/media/v4l2-core/v4l2-ctrls-core.c +1
-1if (hdl ==NULL || hdl->buckets ==NULL ) -return ; +return hdl->error;Hmm, that will crash if hdl == NULL!
Oops. Indeed it does.
Quoted 18 lines
I'm not really sure what this should return if hdl == NULL: 0 or some error code.
I'm inclined to just return 0 in that case. If hdl can be NULL, then presumably it is intentional and not an error.
Quoted 12 lines
drivers/media/v4l2-core/v4l2-ctrls-core.c +2
-0v4l2_ctrl_handler_free_request (hdl);@@ -1663,6 +1663,8 @@ void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl) hdl->cached =NULL ;mutex_unlock (hdl->lock);mutex_destroy (&hdl->_lock); + +return hdl->error; }EXPORT_SYMBOL (v4l2_ctrl_handler_free);
Quoted 11 lines
include/media/v4l2-ctrls.h int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl, · line 581
Quoted 8 lines
include/media/v4l2-ctrls.h +0
-0@@ -579,8 +579,10 @@ int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl,* @hdl: The control handler. * * Does nothing if @hdl == NULL. Also here it states that it does nothing. So returning 0 is probably best. And document here that it does in fact return 0 if hdl == NULL.
Ack. I'll send v2 with these changes.
Signature · 3 lines
Regards, Sakari Ailus
Re: [PATCH v2 2/3] media: v4l2-ctrls: Return the handler's error in v4l2_ctrl_handler_free()
In reply to Sakari AilusHi Sakari,
kernel test robot noticed the following build warnings:
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Sakari-Ailus/media-v4l2-ctrls-Don-t-reset-handler-s-error-in-v4l2_ctrl_handler_free/20250619-175457 base: https://git.linuxtv.org/media-ci/media-pending.git master patch link: https://lore.kernel.org/r/20250619095227.1939114-3-sakari.ailus%40linux.intel.com patch subject: [PATCH v2 2/3] media: v4l2-ctrls: Return the handler's error in v4l2_ctrl_handler_free() config: i386-randconfig-141-20250620 (https://download.01.org/0day-ci/archive/20250620/[email protected]/config) compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]> | Reported-by: Dan Carpenter <[email protected]> | Closes: https://lore.kernel.org/r/[email protected]/
smatch warnings:drivers/media/v4l2-core/v4l2-ctrls-core.c:1641 v4l2_ctrl_handler_free() error: we previously assumed 'hdl' could be null (see line 1640)
vim +/hdl +1641 drivers/media/v4l2-core/v4l2-ctrls-core.c
b56f94c6d2dfdd Sakari Ailus 2025-06-19 1634 int v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
71c689dc2e732d Hans Verkuil 2021-04-27 1635 {
71c689dc2e732d Hans Verkuil 2021-04-27 1636 struct v4l2_ctrl_ref *ref, *next_ref;
71c689dc2e732d Hans Verkuil 2021-04-27 1637 struct v4l2_ctrl *ctrl, *next_ctrl;
71c689dc2e732d Hans Verkuil 2021-04-27 1638 struct v4l2_subscribed_event *sev, *next_sev;
71c689dc2e732d Hans Verkuil 2021-04-27 1639
71c689dc2e732d Hans Verkuil 2021-04-27 @1640 if (hdl == NULL || hdl->buckets == NULL)
^^^^^^^^^^^hdl is NULL
b56f94c6d2dfdd Sakari Ailus 2025-06-19 @1641 return hdl->error;
^^^^^^^^^^Dereferenced.
71c689dc2e732d Hans Verkuil 2021-04-27 1642
71c689dc2e732d Hans Verkuil 2021-04-27 1643 v4l2_ctrl_handler_free_request(hdl);
71c689dc2e732d Hans Verkuil 2021-04-27 1644
71c689dc2e732d Hans Verkuil 2021-04-27 1645 mutex_lock(hdl->lock);
71c689dc2e732d Hans Verkuil 2021-04-27 1646 /* Free all nodes */
71c689dc2e732d Hans Verkuil 2021-04-27 1647 list_for_each_entry_safe(ref, next_ref, &hdl->ctrl_refs, node) {Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
PATCH v2 3/3 media: Documentation: Document new v4l2_ctrl_handler_free() behaviour
In reply to Sakari Ailusv4l2_ctrl_handler_free() no longer resets the handler's error code. Document it.
--- Documentation/driver-api/media/v4l2-controls.rst | 9 +++------ include/media/v4l2-ctrls.h | 3 ++- 2 files changed, 5 insertions(+), 7 deletions(-)
Documentation/driver-api/media/v4l2-controls.rst +3 -6
@@ -110,6 +110,7 @@ For sub-device drivers: v4l2_ctrl_handler_free(&foo->ctrl_handler); +:c:func:`v4l2_ctrl_handler_free` does not touch the handler's ``error`` field. 2) Add controls:
@@ -191,12 +192,8 @@ These functions are typically called right after the V4L2_CID_TEST_PATTERN, ARRAY_SIZE(test_pattern) - 1, 0, 0, test_pattern); ... - if (foo->ctrl_handler.error) { - int err = foo->ctrl_handler.error; - - v4l2_ctrl_handler_free(&foo->ctrl_handler); - return err; - } + if (foo->ctrl_handler.error) + return v4l2_ctrl_handler_free(&foo->ctrl_handler); The :c:func:`v4l2_ctrl_new_std` function returns the v4l2_ctrl pointer to the new control, but if you do not need to access the pointer outside the
include/media/v4l2-ctrls.h +2 -1
@@ -578,7 +578,8 @@ int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl,* the control list. * @hdl: The control handler. * -* Does nothing if @hdl == NULL. +* Does nothing if @hdl == NULL. Does not reset the handler's error code in any +* case. * * Returns the handler's error field. */
Signature · 1 line
2.39.5
Re: [PATCH v2 3/3] media: Documentation: Document new v4l2_ctrl_handler_free() behaviour
Quoted 41 lines · Sakari Ailus
v4l2_ctrl_handler_free()no longer resets the handler's error code. Document it.Signed-off-by Sakari Ailus <[email protected]>--- Documentation/driver-api/media/v4l2-controls.rst | 9 +++------ include/media/v4l2-ctrls.h | 3 ++- 2 files changed, 5 insertions(+), 7 deletions(-)Documentation/driver-api/media/v4l2-controls.rst +3
-6@@ -110,6 +110,7 @@ For sub-device drivers: v4l2_ctrl_handler_free(&foo->ctrl_handler); +:c:func:`v4l2_ctrl_handler_free` does not touch the handler's ``error`` field. 2) Add controls:@@ -191,12 +192,8 @@ These functions are typically called right after the V4L2_CID_TEST_PATTERN, ARRAY_SIZE(test_pattern) - 1, 0, 0, test_pattern); - if (foo->ctrl_handler.error) { - int err = foo->ctrl_handler.error; - - v4l2_ctrl_handler_free(&foo->ctrl_handler); - return err; - } + if (foo->ctrl_handler.error) + return v4l2_ctrl_handler_free(&foo->ctrl_handler); The :c:func:`v4l2_ctrl_new_std` function returns the v4l2_ctrl pointer to the new control, but if you do not need to access the pointer outside theinclude/media/v4l2-ctrls.h +0
-0
include/media/v4l2-ctrls.h int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl, · line 582
Quoted 6 lines
include/media/v4l2-ctrls.h +2
-1@@ -578,7 +578,8 @@ int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl,* the control list. * @hdl: The control handler. * -* Does nothing if @hdl == NULL. +* Does nothing if @hdl == NULL. Does not reset the handler's error code in any +* case.
So this should change to something like:
* If @hdl == NULL, then it just returns 0 without doing anything.
include/media/v4l2-ctrls.h int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl, · line 585
Quoted 3 lines
include/media/v4l2-ctrls.h +0
-0* * Returns the handler's error field. */
I don't think that there is a reason to state that the error code isn't reset. Documenting that the error field is returned already indicates that it isn't reset.
Actually, patch 1/3 should probably document this in v4l2-ctrls.h: that patch will be backported, and it doesn't return the error code, so there is makes sense to state that error field isn't reset. In patch 2/3 it can be removed again and you just say that it returns the handler's error field.
This documentation patch shouldn't touch v4l2-ctrls.h at all.
Regards,
Hans
Regards,
Hans
Re: [PATCH v2 3/3] media: Documentation: Document new v4l2_ctrl_handler_free() behaviour
Hi Hans,
Quoted 61 lines · Hans Verkuil
Quoted 41 lines · Sakari Ailus
v4l2_ctrl_handler_free()no longer resets the handler's error code. Document it.Signed-off-by Sakari Ailus <[email protected]>--- Documentation/driver-api/media/v4l2-controls.rst | 9 +++------ include/media/v4l2-ctrls.h | 3 ++- 2 files changed, 5 insertions(+), 7 deletions(-)Documentation/driver-api/media/v4l2-controls.rst +3
-6@@ -110,6 +110,7 @@ For sub-device drivers: v4l2_ctrl_handler_free(&foo->ctrl_handler); +:c:func:`v4l2_ctrl_handler_free` does not touch the handler's ``error`` field. 2) Add controls:@@ -191,12 +192,8 @@ These functions are typically called right after the V4L2_CID_TEST_PATTERN, ARRAY_SIZE(test_pattern) - 1, 0, 0, test_pattern); - if (foo->ctrl_handler.error) { - int err = foo->ctrl_handler.error; - - v4l2_ctrl_handler_free(&foo->ctrl_handler); - return err; - } + if (foo->ctrl_handler.error) + return v4l2_ctrl_handler_free(&foo->ctrl_handler); The :c:func:`v4l2_ctrl_new_std` function returns the v4l2_ctrl pointer to the new control, but if you do not need to access the pointer outside theinclude/media/v4l2-ctrls.h +0
-0include/media/v4l2-ctrls.h int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl, · line 582
Quoted 6 lines
include/media/v4l2-ctrls.h +2
-1@@ -578,7 +578,8 @@ int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl,* the control list. * @hdl: The control handler. * -* Does nothing if @hdl == NULL. +* Does nothing if @hdl == NULL. Does not reset the handler's error code in any +* case. So this should change to something like:
* If @hdl == NULL, then it just returns 0 without doing anything.
include/media/v4l2-ctrls.h int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl, · line 585
Quoted 3 lines
include/media/v4l2-ctrls.h +0
-0* * Returns the handler's error field. */ I don't think that there is a reason to state that the error code isn't reset. Documenting that the error field is returned already indicates that it isn't reset.
Quoted 6 lines
Actually, patch 1/3 should probably document this in
v4l2-ctrls.h: that patch will be backported, and it doesn't return the error code, so there is makes sense to state that error field isn't reset. In patch 2/3 it can be removed again and you just say that it returns the handler's error field.This documentation patch shouldn't touch
v4l2-ctrls.hat all.
I'd really keep the backported patch as small as possible as it can be backported with the control framework, in other words I'd keep it as-is.
I'll drop the changes to v4l2-ctrl.h from this patch.
Signature · 3 lines
Regards, Sakari Ailus