Linux USB
 help / color / mirror / Atom feed
From: Keisuke Tsukuda <tkdkei@outlook.jp>
To: Neill Kapron <nkapron@google.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: "linux-usb@vger.kernel.org" <linux-usb@vger.kernel.org>,
	Xu Yang <xu.yang_2@nxp.com>, Michael Grzeschik <mgr@kernel.org>,
	Frank Li <Frank.Li@nxp.com>,
	Farhad Alemi <farhad.alemi@berkeley.edu>,
	"stable@vger.kernel.org" <stable@vger.kernel.org>
Subject: [PATCH v2] usb: gadget: uvc: compute isoc payload size at encode time
Date: Wed, 2 Dec 2026 07:15:55 +0000	[thread overview]
Message-ID: <20261202071552.5481-1-tkdkei@outlook.jp> (raw)

uvc_buffer_prepare() calculates req_payload_size when userspace queues a
buffer. Before the first STREAMON, reqs_per_frame is zero, so the payload
size falls back to max_req_size. After the host selects streaming alt 1,
this fallback can exceed the isochronous request size later allocated at
STREAMON. If QBUF occurs before alt 1, max_req_size is still zero; the
copying encoder then subtracts the UVC header length from a zero payload
size. Either case can make it copy past the request buffer. A buffer
queued before a later STREAMON can also use the previous stream's request
layout because reqs_per_frame and req_size are not reset.

On a Raspberry Pi CM4, a 320x180 YUYV stream with a 512-byte stored
payload and 228-byte requests produced list_del corruption and an Oops.

Calculate the payload size in uvcg_video_pump() instead, after the
current request count and size have been set, and remove the prepare-time
calculation. Bound the result by req_size. Round up with a quotient and
remainder, then cap the data and header lengths before adding them to
avoid 32-bit overflow.

Fixes: 98ad03291560 ("usb: gadget: uvc: set req_length based on payload by nreqs instead of req_size")
Fixes: 2edc1acb1a25 ("usb: gadget: uvc: fix req_payload_size calculation")
Reported-by: Farhad Alemi <farhad.alemi@berkeley.edu>
Closes: https://lore.kernel.org/r/CA+0ovCh5uOgB5CQm5eqZFoWE11Uqs4=t4yES-7E1_oH60HtaKw@mail.gmail.com
Cc: stable@vger.kernel.org
Signed-off-by: Keisuke Tsukuda <tkdkei@outlook.jp>
---
v2: Recompute the payload size in the pump. The v1 clamp could leave a
    pre-alt-1 queued buffer at zero or preserve a previous stream's value.

v1: https://lore.kernel.org/r/20261201072659.94982-1-tkdkei@outlook.jp
Review: https://lore.kernel.org/r/sashiko-outbox-242783@kernel.org

Hardware validation: Raspberry Pi CM4 (dwc2) to a Linux USB host, 320x180
YUYV. A test-only userspace program queued four buffers before enabling
the UVC function and streamed the same buffers after the host selected
alt 1. The host captured 12 frames without a kernel fault. The first
frame's lower portion was displaced; frames 2-12 were intact.

diff --git a/drivers/usb/gadget/function/uvc_queue.c b/drivers/usb/gadget/function/uvc_queue.c
--- a/drivers/usb/gadget/function/uvc_queue.c
+++ b/drivers/usb/gadget/function/uvc_queue.c
@@ -61,7 +61,6 @@
 static int uvc_buffer_prepare(struct vb2_buffer *vb)
 {
 	struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
-	struct uvc_video *video = container_of(queue, struct uvc_video, queue);
 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 	struct uvc_buffer *buf = container_of(vbuf, struct uvc_buffer, buf);
 
@@ -86,17 +85,6 @@
 		buf->bytesused = 0;
 	} else {
 		buf->bytesused = vb2_get_plane_payload(vb, 0);
-
-		if (video->reqs_per_frame != 0)	{
-			buf->req_payload_size =
-				DIV_ROUND_UP(buf->bytesused +
-					(video->reqs_per_frame * UVCG_REQUEST_HEADER_LEN),
-					video->reqs_per_frame);
-			if (buf->req_payload_size > video->req_size)
-				buf->req_payload_size = video->req_size;
-		} else {
-			buf->req_payload_size = video->max_req_size;
-		}
 	}
 
 	return 0;
diff --git a/drivers/usb/gadget/function/uvc_video.c b/drivers/usb/gadget/function/uvc_video.c
--- a/drivers/usb/gadget/function/uvc_video.c
+++ b/drivers/usb/gadget/function/uvc_video.c
@@ -651,6 +651,22 @@
 			break;
 		}
 
+		if (!is_bulk) {
+			unsigned int data_size;
+
+			/*
+			 * A buffer can be queued before STREAMON, when the request
+			 * layout is unset or still reflects a previous stream.
+			 */
+			data_size = buf->bytesused / video->reqs_per_frame;
+			if (buf->bytesused % video->reqs_per_frame)
+				data_size++;
+			data_size = min(data_size, video->req_size);
+			buf->req_payload_size = data_size +
+				min(video->req_size - data_size,
+				    UVCG_REQUEST_HEADER_LEN);
+		}
+
 		video->encode(req, video, buf);
 
 		spin_unlock_irqrestore(&queue->irqlock, flags);

             reply	other threads:[~2026-12-02  7:16 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  7:15 Keisuke Tsukuda [this message]
2026-12-02  7:22 ` [PATCH v2] usb: gadget: uvc: compute isoc payload size at encode time sashiko-bot

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=20261202071552.5481-1-tkdkei@outlook.jp \
    --to=tkdkei@outlook.jp \
    --cc=Frank.Li@nxp.com \
    --cc=farhad.alemi@berkeley.edu \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=mgr@kernel.org \
    --cc=nkapron@google.com \
    --cc=stable@vger.kernel.org \
    --cc=xu.yang_2@nxp.com \
    /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