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 Closes: https://lore.kernel.org/r/CA+0ovCh5uOgB5CQm5eqZFoWE11Uqs4=t4yES-7E1_oH60HtaKw@mail.gmail.com Cc: stable@vger.kernel.org Signed-off-by: Keisuke Tsukuda --- 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);