Skip to contentSkip to conversation

Control handler free changes

PATCH Superseded Sakari Ailus 1 participant 4 messages Jun 23, 2025 Media (V4L/DVB)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

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 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
#1 lore raw

PATCH v3 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().

Fixes 0996517cf8ea ("V4L/DVB: v4l2: Add new control handling framework")
---
 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 b45809a82f9a..d28596c720d8 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
#2 lore raw

PATCH v3 2/3 media: v4l2-ctrls: Return the handler's error in v4l2_ctrl_handler_free()

In reply to 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);
---
 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
index d28596c720d8..11cedab0dc31 100644
@@ -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 == NULL)
+		return 0;
+
+	if (hdl->buckets == NULL)
+		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
index 3a87096e064f..c32c46286441 100644
@@ -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
#3 lore raw

PATCH v3 3/3 media: Documentation: Document new v4l2_ctrl_handler_free() behaviour

In reply to Sakari Ailus

v4l2_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
index b2e91804829b..fc04907589ab 100644
@@ -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