mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yaohan Li <shunsheng1218@gmail.com>
To: Mauro Carvalho Chehab <mchehab@kernel.org>,
	Hans Verkuil <hverkuil@kernel.org>
Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] media: bttv: stop interrupt and timeout timer before freeing RISC memory
Date: Thu,  3 Dec 2026 16:52:57 +0800	[thread overview]
Message-ID: <20261203085257.2392387-1-shunsheng1218@gmail.com> (raw)

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@vger.kernel.org
Signed-off-by: Yaohan Li <shunsheng1218@gmail.com>
---
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


                 reply	other threads:[~2026-12-03  8:53 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203085257.2392387-1-shunsheng1218@gmail.com \
    --to=shunsheng1218@gmail.com \
    --cc=hverkuil@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®