[PATCH] media: tw5864: Keep video state alive until the last file is closed

From: Shuo Qin

Date: Wed Dec 02 2026 - 07:04:07 EST


Unbinding a TW5864 device while a video file is open can leave that file
pointing to freed driver state. The input, video_device, VB2 queue and
control handler are embedded in a devres allocation. Unregistering the
video nodes does not wait for all open files to be closed, but remove
frees the control handlers immediately and devres then frees the inputs.
Closing an old file or registering a new video node can consequently
access freed memory.

Allocate the parent independently of devres and free it from the
v4l2_device release callback. The references taken for registered video
nodes then keep the parent alive until all files have gone away. Release
each control handler and input mutex from the video_device release
callback, after file-handle and event cleanup has finished. Hold a PCI
device reference for the same lifetime.

Keep the initial V4L2 reference until probe failure or remove has finished
cleaning up the hardware, queues and buffers. Explicitly free the managed
IRQ before cancelling frame work so the handler cannot queue new work
after the work has been drained or outlive the parent allocation.

Fixes: 34d1324edd31 ("[media] pci: Add tw5864 driver")
Cc: stable@xxxxxxxxxxxxxxx
Co-developed-by: Bin Yu <byu@xxxxxxxxxxxxx>
Signed-off-by: Bin Yu <byu@xxxxxxxxxxxxx>
Co-developed-by: Cheng Wen <wencheng@xxxxxxxxxxxxx>
Signed-off-by: Cheng Wen <wencheng@xxxxxxxxxxxxx>
Signed-off-by: Shuo Qin <23159100078@xxxxxxxxxxxxxxxxx>
---
drivers/media/pci/tw5864/tw5864-core.c | 25 +++++++++++++++++++++----
drivers/media/pci/tw5864/tw5864-video.c | 11 +++++++++--
2 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/drivers/media/pci/tw5864/tw5864-core.c b/drivers/media/pci/tw5864/tw5864-core.c
index 7e38900f71c34f2d0cebd98b63fd5e5ca69ed0cd..1fd6e9ba256a2da413e046ba40f947044658f2cb 100644
--- a/drivers/media/pci/tw5864/tw5864-core.c
+++ b/drivers/media/pci/tw5864/tw5864-core.c
@@ -238,25 +238,35 @@ static void tw5864_timer_isr(struct tw5864_dev *dev)
}
}

+static void tw5864_release(struct v4l2_device *v4l2_dev)
+{
+ struct tw5864_dev *dev =
+ container_of(v4l2_dev, struct tw5864_dev, v4l2_dev);
+
+ pci_dev_put(dev->pci);
+ kfree(dev);
+}
+
static int tw5864_initdev(struct pci_dev *pci_dev,
const struct pci_device_id *pci_id)
{
struct tw5864_dev *dev;
int err;

- dev = devm_kzalloc_obj(&pci_dev->dev, *dev);
+ dev = kzalloc_obj(*dev);
if (!dev)
return -ENOMEM;

snprintf(dev->name, sizeof(dev->name), "%s:%s", DRIVER_NAME,
pci_name(pci_dev));

+ dev->pci = pci_dev_get(pci_dev);
+ dev->v4l2_dev.release = tw5864_release;
err = v4l2_device_register(&pci_dev->dev, &dev->v4l2_dev);
if (err)
- return err;
+ goto free_dev;

/* pci init */
- dev->pci = pci_dev;
err = pcim_enable_device(pci_dev);
if (err) {
dev_err(&dev->pci->dev, "pcim_enable_device() failed\n");
@@ -308,6 +318,11 @@ static int tw5864_initdev(struct pci_dev *pci_dev,
tw5864_video_fini(dev);
unreg_v4l2:
v4l2_device_unregister(&dev->v4l2_dev);
+ v4l2_device_put(&dev->v4l2_dev);
+ return err;
+free_dev:
+ pci_dev_put(dev->pci);
+ kfree(dev);
return err;
}

@@ -317,13 +332,15 @@ static void tw5864_finidev(struct pci_dev *pci_dev)
struct tw5864_dev *dev =
container_of(v4l2_dev, struct tw5864_dev, v4l2_dev);

- /* shutdown subsystems */
+ /* Drain the IRQ handler before cancelling the work it can schedule. */
tw5864_interrupts_disable(dev);
+ devm_free_irq(&pci_dev->dev, pci_dev->irq, dev);

/* unregister */
tw5864_video_fini(dev);

v4l2_device_unregister(&dev->v4l2_dev);
+ v4l2_device_put(&dev->v4l2_dev);
}

static struct pci_driver tw5864_pci_driver = {
diff --git a/drivers/media/pci/tw5864/tw5864-video.c b/drivers/media/pci/tw5864/tw5864-video.c
index 0a08708e52b08283074db66611969312762182a8..593c01641c081c6c35e70209e4936a9052e71b94 100644
--- a/drivers/media/pci/tw5864/tw5864-video.c
+++ b/drivers/media/pci/tw5864/tw5864-video.c
@@ -918,11 +918,19 @@ static const struct v4l2_ioctl_ops video_ioctl_ops = {
#endif
};

+static void tw5864_video_release(struct video_device *vdev)
+{
+ struct tw5864_input *input = video_get_drvdata(vdev);
+
+ v4l2_ctrl_handler_free(&input->hdl);
+ mutex_destroy(&input->lock);
+}
+
static const struct video_device tw5864_video_template = {
.name = "tw5864_video",
.fops = &video_fops,
.ioctl_ops = &video_ioctl_ops,
- .release = video_device_release_empty,
+ .release = tw5864_video_release,
.tvnorms = TW5864_NORMS,
.device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
V4L2_CAP_STREAMING,
@@ -1190,7 +1198,6 @@ static int tw5864_video_input_init(struct tw5864_input *input, int video_nr)
static void tw5864_video_input_fini(struct tw5864_input *dev)
{
vb2_video_unregister_device(&dev->vdev);
- v4l2_ctrl_handler_free(&dev->hdl);
}

void tw5864_video_fini(struct tw5864_dev *dev)