From: Hongbing Hu <huhb04@gmail.com>
To: laurent.pinchart@ideasonboard.com, hansg@kernel.org
Cc: mchehab@kernel.org, linux-media@vger.kernel.org,
linux-kernel@vger.kernel.org, Hongbing Hu <huhb04@gmail.com>,
stable@vger.kernel.org
Subject: [PATCH v2] media: uvcvideo: defer cancelled buffer completion until copies finish
Date: Thu, 3 Sep 2026 22:15:32 +0800 [thread overview]
Message-ID: <20260903141532.6693-1-huhb1@xiaopeng.com> (raw)
In-Reply-To: <20260903131141.6368-1-huhb1@xiaopeng.com>
From: Hongbing Hu <huhb04@gmail.com>
uvc_queue_cancel() returns queued buffers to videobuf2 directly via
vb2_buffer_done(). This is unsafe because asynchronous memcpy workers may
still hold a reference on the same uvc_buffer. Once vb2_buffer_done() has
run, userspace can dequeue and requeue the buffer while the old worker is
still running, leading to the same list_head being inserted into irqqueue
twice and corrupting the list.
The crash manifests in two ways depending on the drop-corrupted-frames
module parameter:
1. With the drop corrupted frames enabled, the old worker's
final kref_put reaches uvc_queue_buffer_complete(), sees buf->error set,
and calls uvc_queue_buffer_requeue(). This performs list_add_tail() on
buf->queue even though userspace has already QBUF'd the same buffer and
added it to irqqueue.
2. With nodrop=1, uvc_queue_buffer_complete() calls vb2_buffer_done() a
second time. If userspace has already dequeued and requeued the buffer,
the second completion exposes it to userspace again, and the next
DQBUF/QBUF inserts the still-linked list_head into irqqueue a second
time.
Both cases produce the kind of list_del corruption seen here:
list_del corruption. next->prev should be ..., but was ...
kernel BUG at lib/list_debug.c:64!
Fix the cancel path to remove buffers from irqqueue and drop the queue's
reference through uvc_queue_buffer_release() (i.e. kref_put()). The final
VB2 completion then only happens from uvc_queue_buffer_complete() once the
last async reference is released, so userspace cannot reuse the buffer
while an old worker still accesses it.
A buffer whose state is already UVC_BUF_STATE_ERROR is forced to complete
as an error and must not be requeued by the corrupted-frame policy. Normal
malformed frames are still requeued/dropped as before.
Fixes: 01e90464e42e ("media: uvcvideo: queue: Support asynchronous buffer handling")
Cc: stable@vger.kernel.org
Signed-off-by: Hongbing Hu <huhb04@gmail.com>
---
v2: corrected author real name in From/SOB per maintainer feedback
drivers/media/usb/uvc/uvc_queue.c | 30 ++++++++++++++++++++++++++++--
1 file changed, 28 insertions(+), 2 deletions(-)
diff --git a/drivers/media/usb/uvc/uvc_queue.c b/drivers/media/usb/uvc/uvc_queue.c
index 3c002c8f44..9206e0a355 100644
--- a/drivers/media/usb/uvc/uvc_queue.c
+++ b/drivers/media/usb/uvc/uvc_queue.c
@@ -289,10 +289,19 @@ int uvc_queue_init(struct uvc_streaming *stream, struct uvc_video_queue *queue,
*/
void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
{
+ struct uvc_buffer *buf, *next;
+ struct list_head local_list;
unsigned long flags;
+ INIT_LIST_HEAD(&local_list);
+
spin_lock_irqsave(&queue->irqlock, flags);
- __uvc_queue_return_buffers(queue, UVC_BUF_STATE_ERROR);
+ while (!list_empty(&queue->irqqueue)) {
+ buf = list_first_entry(&queue->irqqueue, struct uvc_buffer, queue);
+ list_del(&buf->queue);
+ buf->state = UVC_BUF_STATE_ERROR;
+ list_add_tail(&buf->queue, &local_list);
+ }
/*
* This must be protected by the irqlock spinlock to avoid race
* conditions between uvc_buffer_queue and the disconnection event that
@@ -303,6 +312,17 @@ void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
if (disconnect)
queue->flags |= UVC_QUEUE_DISCONNECTED;
spin_unlock_irqrestore(&queue->irqlock, flags);
+
+ /*
+ * Release the queue-owned kref outside the irqlock. The final VB2
+ * completion only happens from uvc_queue_buffer_complete() once all
+ * asynchronous copy references have been released, preventing userspace
+ * from reusing the buffer while an old worker still accesses it.
+ */
+ list_for_each_entry_safe(buf, next, &local_list, queue) {
+ list_del(&buf->queue);
+ uvc_queue_buffer_release(buf);
+ }
}
/*
@@ -356,7 +376,13 @@ static void uvc_queue_buffer_complete(struct kref *ref)
struct vb2_buffer *vb = &buf->buf.vb2_buf;
struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
- if (buf->error && !uvc_no_drop_param) {
+ /*
+ * Buffers cancelled from uvc_queue_cancel() are forced to complete as
+ * errors. They must not be requeued by the corrupted-frame policy even
+ * when buf->error is set.
+ */
+ if (buf->state != UVC_BUF_STATE_ERROR &&
+ buf->error && !uvc_no_drop_param) {
uvc_queue_buffer_requeue(queue, buf);
return;
}
--
2.34.1
next prev parent reply other threads:[~2026-09-03 14:15 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-03 13:11 [PATCH] media: uvcvideo: defer cancelled buffer completion until copies finish huhb1
2026-09-03 13:24 ` Laurent Pinchart
2026-09-03 14:15 ` Hongbing Hu [this message]
2026-09-04 13:00 ` [PATCH v2] " Ricardo Ribalda
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=20260903141532.6693-1-huhb1@xiaopeng.com \
--to=huhb04@gmail.com \
--cc=hansg@kernel.org \
--cc=laurent.pinchart@ideasonboard.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=mchehab@kernel.org \
--cc=stable@vger.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