[PATCH v3] media: em28xx: defer media device cleanup until final device release
From: Arbab Haider
Date: Thu Nov 26 2026 - 05:08:35 EST
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@xxxxxxxxxxxxxxx
Signed-off-by: Arbab Haider <arbabhaider649@xxxxxxxxx>
---
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