[PATCH v3] media: tw68: guard empty active list and serialize stop_streaming
From: Ziheng Wei
Date: Mon Nov 30 2026 - 22:01:26 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>
---
v2 -> v3:
- Rebase onto media-committers.git fixes branch (37760be5a04b) per
media-ci robot requirement; no code changes.
- v2: https://lore.kernel.org/r/20261130134228.1412109-1-zihengw03@xxxxxxx
v1 -> v2:
- Correct Fixes tag to e15d1c12c587 ("[media] tw68: refactor and cleanup
the tw68 driver") and add Cc: stable@xxxxxxxxxxxxxxx.
diff --git a/drivers/media/pci/tw68/tw68-core.c b/drivers/media/pci/tw68/tw68-core.c
index 0909b2d9fcdd..70d66d227ab2 100644
--- a/drivers/media/pci/tw68/tw68-core.c
+++ b/drivers/media/pci/tw68/tw68-core.c
@@ -394,9 +394,9 @@ static int tw68_resume(struct device *dev_d)
/*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);
diff --git a/drivers/media/pci/tw68/tw68-video.c b/drivers/media/pci/tw68/tw68-video.c
index 6232bac170d0..8e9ce000c616 100644
--- 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 int tw68_start_streaming(struct vb2_queue *q, unsigned int count)
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 @@ void tw68_irq_video_done(struct tw68_dev *dev, unsigned long status)
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: 37760be5a04b538bbd25099000e37d3208c27efe