[PATCH] media: bttv: stop interrupt and timeout timer before freeing RISC memory
From: Yaohan Li
Date: Thu Dec 03 2026 - 03:53:09 EST
In bttv_remove(), btcx_riscmem_free() frees the main RISC program buffer
and clears btv->main.cpu before free_irq() and timer teardown run. If
btv->timeout expires (or a shared PCI interrupt enters bttv_irq()) during
that window, bttv_irq_timeout() calls bttv_buffer_activate_video() and
bttv_set_dma(), dereferencing btv->main.cpu after it has been cleared.
Commit bd5b50b329e8 ("media: bttv: fix use after free error due to
btv->timeout timer") added timer synchronization before the final kfree()
of btv, but left both free_irq() and timer teardown after the main RISC
buffer had already been freed.
Move free_irq() and timer_shutdown_sync(&btv->timeout) right after
bttv_unregister_video() so the interrupt handler and watchdog timer are
stopped and cannot rearm before freeing the control handlers and
btv->main.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Yaohan Li <shunsheng1218@xxxxxxxxx>
---
Based on v7.4-rc5 (fe6ff8d5d675); applies cleanly on media-committers fixes
and next.
Note on scope: this patch only fixes the IRQ/timer vs btv->main RISC buffer
teardown order inside bttv_remove(); it does not change broader V4L2 open
file-handle lifetime across hot-unplug.
I don't have physical Bt848/Bt878 hardware, so I tested this in QEMU
(x86_64, KASAN + PROVE_LOCKING) with the in-tree bttv.ko bound to QEMU
pci-testdev and a virtual PCI interrupt. Unbinding while btv->timeout is
armed triggers a NULL pointer dereference in bttv_buffer_activate_video()
from bttv_irq_timeout() on the unmodified driver; with this patch applied,
3 consecutive unbind/rebind cycles and module unload pass with 0 KASAN,
Oops, or lockdep warnings. Also build-tested on x86_64 and arm64 with W=1
(0 warnings).
AI assistance: Claude and Gemini helped audit the timer/IRQ teardown path,
build the QEMU test harness, and draft the changelog. I reviewed the driver
code, test logs, and final patch myself.
drivers/media/pci/bt8xx/bttv-driver.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/drivers/media/pci/bt8xx/bttv-driver.c b/drivers/media/pci/bt8xx/bttv-driver.c
index 8b9ca59f9434f..630dec58985c7 100644
--- a/drivers/media/pci/bt8xx/bttv-driver.c
+++ b/drivers/media/pci/bt8xx/bttv-driver.c
@@ -3484,14 +3484,16 @@ static void bttv_remove(struct pci_dev *pci_dev)
/* unregister video4linux */
bttv_unregister_video(btv);
+ /* stop interrupt and timeout timer before freeing RISC memory */
+ free_irq(btv->c.pci->irq, btv);
+ timer_shutdown_sync(&btv->timeout);
+
/* free allocated memory */
v4l2_ctrl_handler_free(&btv->ctrl_handler);
v4l2_ctrl_handler_free(&btv->radio_ctrl_handler);
btcx_riscmem_free(btv->c.pci,&btv->main);
/* free resources */
- free_irq(btv->c.pci->irq,btv);
- timer_delete_sync(&btv->timeout);
iounmap(btv->bt848_mmio);
release_mem_region(pci_resource_start(btv->c.pci,0),
pci_resource_len(btv->c.pci,0));
--
2.34.1