public inbox for linux-media@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v5] staging: most: video: prevent probes during component exit
@ 2025-12-03  8:34 Pavan Kumar Yalagada
  2025-12-03  9:05 ` Greg KH
  2025-12-03  9:13 ` Dan Carpenter
  0 siblings, 2 replies; 4+ messages in thread
From: Pavan Kumar Yalagada @ 2025-12-03  8:34 UTC (permalink / raw)
  To: parthiban.veerasooran, christian.gromm, gregkh
  Cc: dan.carpenter, laurent.pinchart+renesas, hverkuil+cisco,
	linux-staging, linux-media, pavankumaryalagada

When comp_exit() runs, comp_probe_channel() could still add new devices
to video_devices, creating a race and potentially leaving the list in
an inconsistent state.

This patch prevents new devices from being added while exiting by:

- comp_probe_channel() checks comp_exiting before modifying video_devices.

	if (comp_exiting) {
		spin_unlock_irq(&list_lock);
		ret = -BUSY;
		goto err_unreg;
	}

This ensures that all partially created resources are properly freed
and no new channels are allowed while the driver is being unloaded.

Signed-off-by: Pavan Kumar Yalagada <pavankumaryalagada@gmail.com>

---

v5:
 - Removed unlikely() macro since this is not fast path.
 - Added comp_unregister_videodev() to err_unreg cleanup patch.
 - Removed blank line in comp_exit().

v4:
 - Used unlikely() for the comp_exiting check to optimize branch
   prediction.
 - Removed err_cleanup and use err_unreg for proper cleanup
   of partially initialized mdev/v4l2 structures.
 - Removed redundant spinlock usage around 'comp_exiting = true;'
   comp_exit().
 - Removed explicit kfree(mdev) in comp_exit() loop.

v3:
 - comp_exiting flag update and memory cleanup for early exits.
 - Commit message clarified for reviewers.
 - Removed WARN/BUG as it becomes unnecessary.
---
 drivers/staging/most/video/video.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/drivers/staging/most/video/video.c b/drivers/staging/most/video/video.c
index 32f71d9a9cf7..11e63f5d5b84 100644
--- a/drivers/staging/most/video/video.c
+++ b/drivers/staging/most/video/video.c
@@ -60,6 +60,8 @@ static inline struct comp_fh *to_comp_fh(struct file *filp)
 static LIST_HEAD(video_devices);
 static DEFINE_SPINLOCK(list_lock);

+static bool comp_exiting;
+
 static inline bool data_ready(struct most_video_dev *mdev)
 {
 	return !list_empty(&mdev->pending_mbos);
@@ -498,11 +500,17 @@ static int comp_probe_channel(struct most_interface *iface, int channel_idx,
 		goto err_unreg;

 	spin_lock_irq(&list_lock);
+	if (comp_exiting) {
+		spin_unlock_irq(&list_lock);
+		ret = -EBUSY;
+		goto err_unreg;
+	}
 	list_add(&mdev->list, &video_devices);
 	spin_unlock_irq(&list_lock);
 	return 0;

 err_unreg:
+	comp_unregister_videodev(mdev);
 	v4l2_device_disconnect(&mdev->v4l2_dev);
 	v4l2_device_put(&mdev->v4l2_dev);
 	return ret;
@@ -555,6 +563,8 @@ static void __exit comp_exit(void)
 {
 	struct most_video_dev *mdev, *tmp;

+	comp_exiting = true;
+
 	/*
 	 * As the mostcore currently doesn't call disconnect_channel()
 	 * for linked channels while we call most_deregister_component()
@@ -575,7 +585,6 @@ static void __exit comp_exit(void)

 	most_deregister_configfs_subsys(&comp);
 	most_deregister_component(&comp);
-	BUG_ON(!list_empty(&video_devices));
 }

 module_init(comp_init);
--
2.47.3


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-12-03  9:32 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-03  8:34 [PATCH v5] staging: most: video: prevent probes during component exit Pavan Kumar Yalagada
2025-12-03  9:05 ` Greg KH
2025-12-03  9:31   ` Dan Carpenter
2025-12-03  9:13 ` Dan Carpenter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox