[PATCH v3 1/2] media: hdpvr: don't recycle an in-flight buffer

From: 25031212351

Date: Fri Nov 20 2026 - 11:20:48 EST


From: YuanZhe Liu <25031212351@xxxxxxxxxxxxxxxxx>

hdpvr_read() gives a fully read buffer back to the device by moving it to
dev->free_buff_list and marking it available. The buffer status is checked
before dev->io_mutex is taken, but the worker can resubmit the same buffer
in the meantime, and the buffer then has an URB in flight and belongs on
dev->rec_buff_list.

If it is moved to dev->free_buff_list anyway, the worker later tries to
submit the active URB again, which triggers the "URB %p submitted while
active" warning in usb_submit_urb() and fails the submission, and
hdpvr_cancel_queue() no longer sees the URB, so hdpvr_free_buffers() can
free the URB and its DMA buffer while the HCD still owns them.

Two readers of the same video device can reach this, as
hdpvr_get_next_buffer() returns the receive buffer to every reader without
removing it from the list, and hdpvr_read() is not serialized.

Only put the buffer back on the free list if it is still waiting to be
read.

Fixes: 9aba42efe85b ("V4L/DVB (11096): V4L2 Driver for the Hauppauge HD PVR usb capture device")
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: YuanZhe Liu <25031212351@xxxxxxxxxxxxxxxxx>
---
v3:
- Split out of the combined patch sent as v2; no code change.
drivers/media/usb/hdpvr/hdpvr-video.c | 29 ++++++++++++++++++---------
1 file changed, 20 insertions(+), 9 deletions(-)

diff --git a/drivers/media/usb/hdpvr/hdpvr-video.c b/drivers/media/usb/hdpvr/hdpvr-video.c
index 4290b2367fa3..98480a67287a 100644
--- a/drivers/media/usb/hdpvr/hdpvr-video.c
+++ b/drivers/media/usb/hdpvr/hdpvr-video.c
@@ -246,6 +246,25 @@ static struct hdpvr_buffer *hdpvr_get_next_buffer(struct hdpvr_device *dev)
return buf;
}

+/*
+ * Put a consumed buffer back on the free list, unless the worker has
+ * resubmitted it in the meantime: the position and the buffer list of a
+ * buffer with an URB in flight must not be touched.
+ */
+static void hdpvr_recycle_buffer(struct hdpvr_device *dev,
+ struct hdpvr_buffer *buf)
+{
+ mutex_lock(&dev->io_mutex);
+ if (buf->status == BUFSTAT_READY) {
+ buf->pos = 0;
+ buf->status = BUFSTAT_AVAILABLE;
+ list_move_tail(&buf->buff_list, &dev->free_buff_list);
+ }
+ mutex_unlock(&dev->io_mutex);
+
+ wake_up_interruptible(&dev->wait_buffer);
+}
+
static void hdpvr_transmit_buffers(struct work_struct *work)
{
struct hdpvr_device *dev = container_of(work, struct hdpvr_device,
@@ -506,18 +525,10 @@ static ssize_t hdpvr_read(struct file *file, char __user *buffer, size_t count,

/* finished, take next buffer */
if (buf->pos == urb->actual_length) {
- mutex_lock(&dev->io_mutex);
- buf->pos = 0;
- buf->status = BUFSTAT_AVAILABLE;
-
- list_move_tail(&buf->buff_list, &dev->free_buff_list);
+ hdpvr_recycle_buffer(dev, buf);

print_buffer_status();

- mutex_unlock(&dev->io_mutex);
-
- wake_up_interruptible(&dev->wait_buffer);
-
buf = hdpvr_get_next_buffer(dev);
}
}
--
2.34.1