[PATCH v11 01/14] media: v4l2-core: Preserve existing subdevice nodes on rollback
From: Maurizio Casciano
Date: Sat Nov 21 2026 - 08:33:04 EST
Subdevice node registration skips nodes which already exist. On failure,
however, it walks the subdevice list from the beginning and unregisters
nodes regardless of whether the current call created them. On AtomISP
this can remove the camera sensor nodes when a lens rebind fails.
Keep a list of the video devices registered by this call and unregister
only those devices on failure. Record each device before creating its
media interface link so a failure in that step rolls back the new node
as well. Calls with no missing nodes need no bookkeeping allocation.
Codex drafted the change and tested allocation, registration and
media-link failure using the registration helper with mocked operations.
Fixes: db0f4691d974 ("[media] v4l: Allow calling v4l2_device_register_subdev_nodes() multiple times")
Signed-off-by: Maurizio Casciano <mauriziocasciano7@xxxxxxxxx>
---
drivers/media/v4l2-core/v4l2-device.c | 26 ++++++++++++++++++++------
include/media/v4l2-device.h | 3 +++
2 files changed, 23 insertions(+), 6 deletions(-)
diff --git a/drivers/media/v4l2-core/v4l2-device.c b/drivers/media/v4l2-core/v4l2-device.c
index b4036105c..8cfca95ce 100644
--- a/drivers/media/v4l2-core/v4l2-device.c
+++ b/drivers/media/v4l2-core/v4l2-device.c
@@ -191,10 +191,23 @@ static void v4l2_device_release_subdev_node(struct video_device *vdev)
int __v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev,
bool read_only)
{
+ unsigned int count = 0, registered = 0;
+ struct video_device **new_nodes;
struct video_device *vdev;
struct v4l2_subdev *sd;
int err;
+ list_for_each_entry(sd, &v4l2_dev->subdevs, list)
+ if ((sd->flags & V4L2_SUBDEV_FL_HAS_DEVNODE) && !sd->devnode)
+ count++;
+
+ if (!count)
+ return 0;
+
+ new_nodes = kcalloc(count, sizeof(*new_nodes), GFP_KERNEL);
+ if (!new_nodes)
+ return -ENOMEM;
+
/* Register a device node for every subdev marked with the
* V4L2_SUBDEV_FL_HAS_DEVNODE flag.
*/
@@ -228,6 +241,7 @@ int __v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev,
kfree(vdev);
goto clean_up;
}
+ new_nodes[registered++] = vdev;
#if defined(CONFIG_MEDIA_CONTROLLER)
sd->entity.info.dev.major = VIDEO_MAJOR;
sd->entity.info.dev.minor = vdev->minor;
@@ -247,15 +261,15 @@ int __v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev,
}
#endif
}
- return 0;
+ err = 0;
+ goto out;
clean_up:
- list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
- if (!sd->devnode)
- break;
- video_unregister_device(sd->devnode);
- }
+ while (registered)
+ video_unregister_device(new_nodes[--registered]);
+out:
+ kfree(new_nodes);
return err;
}
EXPORT_SYMBOL_GPL(__v4l2_device_register_subdev_nodes);
diff --git a/include/media/v4l2-device.h b/include/media/v4l2-device.h
index 25f69b1b8..6d8834d40 100644
--- a/include/media/v4l2-device.h
+++ b/include/media/v4l2-device.h
@@ -183,6 +183,9 @@ void v4l2_device_unregister_subdev(struct v4l2_subdev *sd);
* @read_only: subdevices read-only flag. True to register the subdevices
* device nodes in read-only mode, false to allow full access to the
* subdevice userspace API.
+ *
+ * Existing nodes are kept even if registration of a new node fails.
+ * The caller must serialize this operation with changes to the subdevice list.
*/
int __must_check
__v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev,
--
2.53.0