Linux USB
 help / color / mirror / Atom feed
* [PATCH] usb: gadget: uvc: cap isoc payload at request size
@ 2026-12-01  7:27 Keisuke Tsukuda
  2026-12-01  7:39 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Keisuke Tsukuda @ 2026-12-01  7:27 UTC (permalink / raw)
  To: nkapron@google.com, gregkh@linuxfoundation.org
  Cc: linux-usb@vger.kernel.org, xu.yang_2@nxp.com, mgr@kernel.org,
	Frank.Li@nxp.com, farhad.alemi@berkeley.edu, Keisuke Tsukuda,
	stable@vger.kernel.org

uvc_buffer_prepare() records a payload size when userspace queues a
frame. If QBUF precedes the first STREAMON, reqs_per_frame is zero
and the payload size is set to max_req_size. That fallback avoids
the earlier zero-divisor calculation, but STREAMON can subsequently
allocate smaller isochronous requests.
After STREAMOFF, reqs_per_frame and req_size are not reset, so a
buffer queued for the next stream can also use the previous stream's
request size.

The copying isochronous encoder uses the stored payload size as its
write limit, while its request buffer is allocated with video->req_size.
For example, a 320x180 YUY2 frame at a 666666 (100 ns) interval with
max_req_size of 512 bytes can leave a pre-STREAMON buffer at 512 while
STREAMON allocates 228-byte requests. The encoder can then write past
the allocation. On the observed device, stream startup produced
list_del warnings in uvcg_video_pump and uvcg_video_hw_submit, followed
by an Oops.

Cap the payload size at the current video->req_size in the pump, after
request sizing and immediately before encoding. This prevents the
copying encoder from exceeding its allocation. The common pump also
applies the bound to scatter-gather request lengths.

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>
---
Hardware validation: Raspberry Pi CM4 (dwc2) to a Linux USB host,
320x180 YUY2. The unmodified module produced list_del corruption and
an Oops at stream start; the patched module delivered 12 frames without
an Oops. The first captured frame was displaced in its lower portion;
later frames were intact.

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,11 @@
 			break;
 		}
 
+		/* Buffer payload size may predate the current request size. */
+		if (!is_bulk)
+			buf->req_payload_size = min(buf->req_payload_size,
+						video->req_size);
+
 		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-01  7:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01  7:27 [PATCH] usb: gadget: uvc: cap isoc payload at request size Keisuke Tsukuda
2026-12-01  7:39 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox