mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v3] media: em28xx: defer media device cleanup until final device release
@ 2026-11-26 10:08 Arbab Haider
  0 siblings, 0 replies; only message in thread
From: Arbab Haider @ 2026-11-26 10:08 UTC (permalink / raw)
  To: linux-media; +Cc: mchehab, linux-kernel, Arbab Haider, Hans Verkuil, stable

em28xx_unregister_media_device() clears v4l2_dev.mdev and frees the
media device during disconnect. The driver holds dev->lock, but
v4l2_release() checks whether the device supports media requests before
calling the driver's release callback, which takes that lock. A
concurrent close can still dereference the media device after it has
been freed.

Unregister the media device on disconnect and defer cleanup and freeing
it to em28xx_free_device(). The V4L2 instance holds an em28xx device
reference until the last V4L2 device reference is dropped, which keeps
v4l2_dev.mdev valid throughout V4L2 release. Keep immediate cleanup in
the probe error path, before any V4L2 nodes have been registered.

Fixes: 37ecc7b1278f ("[media] em28xx: add media controller support")
Cc: stable@vger.kernel.org
Signed-off-by: Arbab Haider <arbabhaider649@gmail.com>
---
Changes in v3:
- Rebase onto media-committers/next and replace its disconnect-time
  mdev pointer clear with a lifetime guarantee for V4L2 users.
- Send the cleanup fix on its own, following acceptance of v2 patch 2.

Changes in v2:
- Add the Fixes tag as requested by Media CI.

Validation
- Built drivers/media/usb/em28xx/ on x86_64 with MEDIA_CONTROLLER,
  KASAN and UBSAN enabled.
- Booted QEMU x86_64 with MEDIA_CONTROLLER, KASAN and UBSAN and passed
  three em28xx and em28xx-v4l module load/unload cycles. This smoke test
  ran without an attached em28xx device.

Codex assisted with the rebase, source review, commit message and build.

 drivers/media/usb/em28xx/em28xx-cards.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/drivers/media/usb/em28xx/em28xx-cards.c b/drivers/media/usb/em28xx/em28xx-cards.c
index f4b6dcf597..cf0312fc46 100644
--- a/drivers/media/usb/em28xx/em28xx-cards.c
+++ b/drivers/media/usb/em28xx/em28xx-cards.c
@@ -3773,11 +3773,16 @@ static int em28xx_media_device_init(struct em28xx *dev,
 static void em28xx_unregister_media_device(struct em28xx *dev)
 {
 #ifdef CONFIG_MEDIA_CONTROLLER
-	if (dev->media_dev) {
+	if (dev->media_dev)
 		media_device_unregister(dev->media_dev);
+#endif
+}
+
+static void em28xx_cleanup_media_device(struct em28xx *dev)
+{
+#ifdef CONFIG_MEDIA_CONTROLLER
+	if (dev->media_dev) {
 		media_device_cleanup(dev->media_dev);
-		if (dev->v4l2)
-			dev->v4l2->v4l2_dev.mdev = NULL;
 		kfree(dev->media_dev);
 		dev->media_dev = NULL;
 	}
@@ -3828,6 +3833,8 @@ void em28xx_free_device(struct kref *ref)
 	if (!dev->disconnected)
 		em28xx_release_resources(dev);
 
+	em28xx_cleanup_media_device(dev);
+
 	if (dev->ts == PRIMARY_TS)
 		kfree(dev->alt_max_pkt_size_isoc);
 
@@ -4000,6 +4007,7 @@ static int em28xx_init_dev(struct em28xx *dev, struct usb_device *udev,
 	em28xx_i2c_unregister(dev, 0);
 err_deinit_media:
 	em28xx_unregister_media_device(dev);
+	em28xx_cleanup_media_device(dev);
 	return retval;
 }
 

base-commit: b4e3c4b6a36fd76f57bf3e48638f68ddecc54ba4
-- 
2.53.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-26 10:08 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-26 10:08 [PATCH v3] media: em28xx: defer media device cleanup until final device release Arbab Haider

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

all inboxes | Powered by JetHome®