All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] usb: gadget: uvc: compute isoc payload size at encode time
@ 2026-12-02  7:15 Keisuke Tsukuda
  2026-12-02  7:22 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Keisuke Tsukuda @ 2026-12-02  7:15 UTC (permalink / raw)
  To: Neill Kapron, Greg Kroah-Hartman
  Cc: linux-usb@vger.kernel.org, Xu Yang, Michael Grzeschik, Frank Li,
	Farhad Alemi, stable@vger.kernel.org

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);

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-02  7:22 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02  7:15 [PATCH v2] usb: gadget: uvc: compute isoc payload size at encode time Keisuke Tsukuda
2026-12-02  7:22 ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.