[PATCH v2] media: tw68: guard empty active list and serialize stop_streaming
From: Ziheng Wei
Date: Mon Nov 30 2026 - 08:43:08 EST
tw68_irq_video_done() unconditionally calls list_entry(dev->active.next,
...) and vb2_buffer_done() on TW68_DMAPI interrupts without checking for
an empty list, while tw68_stop_streaming() drains dev->active without
masking video IRQs, calling synchronize_irq(), or holding dev->slock. A
racing interrupt during STREAMOFF/close therefore operates on a bogus
struct tw68_buf derived from &dev->active. tw68_resume() has the same
unconditional container_of() on dev->active.next when no buffer is queued.
Use list_first_entry_or_null() in tw68_irq_video_done() and tw68_resume(),
and mask video IRQs, synchronize_irq(), and lock dev->slock in
tw68_stop_streaming().
Found by LLM-assisted static analysis and runtime-tested in QEMU+KASAN
(VB2 state warning and corrupted active list before the fix;
0 findings after).
Fixes: e15d1c12c587 ("[media] tw68: refactor and cleanup the tw68 driver")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Ziheng Wei <zihengw03@xxxxxxx>
---
v1 -> v2: correct Fixes tag to e15d1c12c587 ("[media] tw68: refactor and cleanup the tw68 driver") where the vb2 conversion removed the list_empty() guards and introduced tw68_stop_streaming() (thanks to Sashiko AI review), and add Cc: stable@xxxxxxxxxxxxxxx.
diff --git a/drivers/media/pci/tw68/tw68-core.c b/drivers/media/pci/tw68/tw68-core.c
--- a/drivers/media/pci/tw68/tw68-core.c
+++ b/drivers/media/pci/tw68/tw68-core.c
@@ -394,10 +394,9 @@
/*resume unfinished buffer(s)*/
spin_lock_irqsave(&dev->slock, flags);
- buf = container_of(dev->active.next, struct tw68_buf, list);
-
- tw68_video_start_dma(dev, buf);
-
+ buf = list_first_entry_or_null(&dev->active, struct tw68_buf, list);
+ if (buf)
+ tw68_video_start_dma(dev, buf);
spin_unlock_irqrestore(&dev->slock, flags);
return 0;
diff --git a/drivers/media/pci/tw68/tw68-video.c b/drivers/media/pci/tw68/tw68-video.c
--- a/drivers/media/pci/tw68/tw68-video.c
+++ b/drivers/media/pci/tw68/tw68-video.c
@@ -16,6 +16,7 @@
* Copyright (C) 2014 Hans Verkuil <hverkuil@xxxxxxxxxx>
*/
+#include <linux/interrupt.h>
#include <linux/module.h>
#include <media/v4l2-common.h>
#include <media/v4l2-event.h>
@@ -505,16 +506,23 @@
static void tw68_stop_streaming(struct vb2_queue *q)
{
struct tw68_dev *dev = vb2_get_drv_priv(q);
+ unsigned long flags;
/* Stop risc & fifo */
tw_clearl(TW68_DMAC, TW68_DMAP_EN | TW68_FIFO_EN);
+ dev->pci_irqmask &= ~dev->board_virqmask;
+ tw_clearl(TW68_INTMASK, dev->board_virqmask);
+ synchronize_irq(dev->pci->irq);
+
+ spin_lock_irqsave(&dev->slock, flags);
while (!list_empty(&dev->active)) {
struct tw68_buf *buf =
- container_of(dev->active.next, struct tw68_buf, list);
+ list_first_entry(&dev->active, struct tw68_buf, list);
list_del(&buf->list);
vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
}
+ spin_unlock_irqrestore(&dev->slock, flags);
}
static const struct vb2_ops tw68_video_qops = {
@@ -982,13 +990,17 @@
struct tw68_buf *buf;
spin_lock(&dev->slock);
- buf = list_entry(dev->active.next, struct tw68_buf, list);
- list_del(&buf->list);
+ buf = list_first_entry_or_null(&dev->active, struct tw68_buf,
+ list);
+ if (buf)
+ list_del(&buf->list);
spin_unlock(&dev->slock);
- buf->vb.vb2_buf.timestamp = ktime_get_ns();
- buf->vb.field = dev->field;
- buf->vb.sequence = dev->seqnr++;
- vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
+ if (buf) {
+ buf->vb.vb2_buf.timestamp = ktime_get_ns();
+ buf->vb.field = dev->field;
+ buf->vb.sequence = dev->seqnr++;
+ vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
+ }
status &= ~(TW68_DMAPI);
if (0 == status)
return;
base-commit: 97d73c8251226cf27cc6656b4c542869bb8c69fa