Control handler free changes
v3 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 v3:
- Use ! for NULL checks, instead of comparing with NULL, in 2nd patch
since v2:
- Note in the 2nd patch v4l2_ctrl_handler_free() returns 0 if hdl is NULL, also avoid NULL pointer dereference in the same case.
- Only Documentation/ changes left in 3rd patch.
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 | 12 ++++++++---- include/media/v4l2-ctrls.h | 4 +++- 3 files changed, 14 insertions(+), 11 deletions(-)
Signature · 1 line
2.39.5
PATCH v4 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 v4 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 7 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]>Reviewed-by Hans Verkuil <[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); }
Signature · 3 lines
Regards, Laurent Pinchart
PATCH v4 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 | 11 ++++++++--- include/media/v4l2-ctrls.h | 4 +++- 2 files changed, 11 insertions(+), 4 deletions(-)
drivers/media/v4l2-core/v4l2-ctrls-core.c +8 -3
@@ -1631,14 +1631,17 @@ 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 ; +if (!hdl) +return 0 ; + +if (!hdl->buckets) +return hdl->error;v4l2_ctrl_handler_free_request (hdl);
@@ -1663,6 +1666,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. +* +* Return: @hdl's error field or 0 if @hdl is NULL. */ -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 v4 2/3] media: v4l2-ctrls: Return the handler's error in v4l2_ctrl_handler_free()
Hi Sakari,
Thank you for the patch.
Quoted 26 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 | 11 ++++++++--- include/media/v4l2-ctrls.h | 4 +++- 2 files changed, 11 insertions(+), 4 deletions(-)drivers/media/v4l2-core/v4l2-ctrls-core.c +1
-1@@ -1631,14 +1631,17 @@ 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;
drivers/media/v4l2-core/v4l2-ctrls-core.c int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl, · line 1644
Quoted 10 lines
drivers/media/v4l2-core/v4l2-ctrls-core.c +5
-2struct v4l2_ctrl *ctrl, *next_ctrl;struct v4l2_subscribed_event *sev, *next_sev; -if (hdl ==NULL || hdl->buckets ==NULL ) -return ; +if (!hdl) +return 0 ; + +if (!hdl->buckets) +return hdl->error;
!hdl->buckets means the handler has been freed, or is not initialized. I'd return 0 in that case.
Quoted 28 lines
drivers/media/v4l2-core/v4l2-ctrls-core.c +2
-0v4l2_ctrl_handler_free_request (hdl);@@ -1663,6 +1666,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. +* +* Return: @hdl's error field or 0 if @hdl is NULL. */ -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 · 3 lines
Regards, Laurent Pinchart
Re: [PATCH v4 2/3] media: v4l2-ctrls: Return the handler's error in v4l2_ctrl_handler_free()
Quoted 34 lines · Laurent Pinchart
Hi Sakari,
Thank you for the patch.
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 | 11 ++++++++--- include/media/v4l2-ctrls.h | 4 +++- 2 files changed, 11 insertions(+), 4 deletions(-)drivers/media/v4l2-core/v4l2-ctrls-core.c +1
-1@@ -1631,14 +1631,17 @@ 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 10 lines
drivers/media/v4l2-core/v4l2-ctrls-core.c int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl, · line 1644
Quoted 7 lines
drivers/media/v4l2-core/v4l2-ctrls-core.c +5
-2-if (hdl ==NULL || hdl->buckets ==NULL ) -return ; +if (!hdl) +return 0 ; + +if (!hdl->buckets) +return hdl->error;!hdl->buckets means the handler has been freed, or is not initialized. I'd return 0 in that case.
Actually it can also mean init failed due to an allocation error, in which case error is set to -ENOMEM. Please ignore this comment.
Quoted 1 line
Reviewed-by Laurent Pinchart <[email protected]>
You can keep this.
Quoted 28 lines
Quoted 28 lines
drivers/media/v4l2-core/v4l2-ctrls-core.c +2
-0v4l2_ctrl_handler_free_request (hdl);@@ -1663,6 +1666,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. +* +* Return: @hdl's error field or 0 if @hdl is NULL. */ -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 · 3 lines
Regards, Laurent Pinchart
Re: [PATCH v4 2/3] media: v4l2-ctrls: Return the handler's error in v4l2_ctrl_handler_free()
In reply to Laurent PinchartQuoted 34 lines · Laurent Pinchart
Hi Sakari,
Thank you for the patch.
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 | 11 ++++++++--- include/media/v4l2-ctrls.h | 4 +++- 2 files changed, 11 insertions(+), 4 deletions(-)drivers/media/v4l2-core/v4l2-ctrls-core.c +1
-1@@ -1631,14 +1631,17 @@ 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 10 lines
drivers/media/v4l2-core/v4l2-ctrls-core.c int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl, · line 1644
Quoted 7 lines
drivers/media/v4l2-core/v4l2-ctrls-core.c +5
-2-if (hdl ==NULL || hdl->buckets ==NULL ) -return ; +if (!hdl) +return 0 ; + +if (!hdl->buckets) +return hdl->error;!hdl->buckets means the handler has been freed, or is not initialized. I'd return 0 in that case.
Ah, no. If v4l2_ctrl_handler_init is called, and allocating hdl->buckets fails, then hdl->error is set to -ENOMEM. You can just continue adding controls etc. and do this at the end:
if (foo->ctrl_handler.error) return v4l2_ctrl_handler_free(&foo->ctrl_handler);
You want to return -ENOMEM in that case, not 0.
Regards,
Hans
Quoted 32 lines
Reviewed-by Laurent Pinchart <[email protected]>Quoted 28 lines
drivers/media/v4l2-core/v4l2-ctrls-core.c +2
-0v4l2_ctrl_handler_free_request (hdl);@@ -1663,6 +1666,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. +* +* Return: @hdl's error field or 0 if @hdl is NULL. */ -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 v4 2/3] media: v4l2-ctrls: Return the handler's error in v4l2_ctrl_handler_free()
In reply to Sakari AilusQuoted 9 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]>
Regards,
Hans
Quoted 55 lines
--- drivers/media/v4l2-core/v4l2-ctrls-core.c | 11 ++++++++--- include/media/v4l2-ctrls.h | 4 +++- 2 files changed, 11 insertions(+), 4 deletions(-)drivers/media/v4l2-core/v4l2-ctrls-core.c +8
-3@@ -1631,14 +1631,17 @@ 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 ; +if (!hdl) +return 0 ; + +if (!hdl->buckets) +return hdl->error;v4l2_ctrl_handler_free_request (hdl);@@ -1663,6 +1666,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. +* +* Return: @hdl's error field or 0 if @hdl is NULL. */ -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
PATCH v4 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 +++------ 1 file changed, 3 insertions(+), 6 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
Signature · 1 line
2.39.5
Re: [PATCH v4 3/3] media: Documentation: Document new v4l2_ctrl_handler_free() behaviour
Quoted 4 lines · Sakari Ailus
v4l2_ctrl_handler_free()no longer resets the handler's error code. Document it.Signed-off-by Sakari Ailus <[email protected]>
Quoted 31 lines
--- Documentation/driver-api/media/v4l2-controls.rst | 9 +++------ 1 file changed, 3 insertions(+), 6 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
Signature · 3 lines
Regards, Laurent Pinchart
Re: [PATCH v4 3/3] media: Documentation: Document new v4l2_ctrl_handler_free() behaviour
In reply to Sakari AilusQuoted 4 lines · Sakari Ailus
v4l2_ctrl_handler_free()no longer resets the handler's error code. Document it.Signed-off-by Sakari Ailus <[email protected]>
Regards,
Hans
Quoted 31 lines
--- Documentation/driver-api/media/v4l2-controls.rst | 9 +++------ 1 file changed, 3 insertions(+), 6 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