Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

media: uvcvideo: Pass allocation size directly to uvc_alloc_urb_buffer

The uvc_alloc_urb_buffer() function implicitly depended on the
stream->urb_size field, which was set by its caller,
uvc_alloc_urb_buffers(). This implicit data flow makes the code harder
to follow.

More importantly, stream->urb_size was updated within the allocation
loop before the allocation was confirmed to be successful. If the
allocation failed, the stream object would be left with a urb_size that
doesn't correspond to valid, allocated URB buffers.

Refactor uvc_alloc_urb_buffer() to accept the buffer size as an explicit
argument. This makes the function's dependencies clear and improves the
robustness of the error handling path. The stream->urb_size is now set only
after a complete and successful allocation.

This is a pure refactoring and introduces no functional changes.

Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Tested-by: Itay Chamiel <itay.chamiel@q.ai>
Link: https://patch.msgid.link/20260114-uvc-alloc-urb-v1-2-cedf3fb66711@chromium.org
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Hans Verkuil <hverkuil+cisco@kernel.org>

authored by

Ricardo Ribalda and committed by
Hans Verkuil
c8243452 40d3ac25

+8 -6
+8 -6
drivers/media/usb/uvc/uvc_video.c
··· 1771 1771 } 1772 1772 1773 1773 static bool uvc_alloc_urb_buffer(struct uvc_streaming *stream, 1774 - struct uvc_urb *uvc_urb, gfp_t gfp_flags) 1774 + struct uvc_urb *uvc_urb, unsigned int size, 1775 + gfp_t gfp_flags) 1775 1776 { 1776 1777 struct usb_device *udev = stream->dev->udev; 1777 1778 1778 - uvc_urb->buffer = usb_alloc_noncoherent(udev, stream->urb_size, 1779 - gfp_flags, &uvc_urb->dma, 1779 + uvc_urb->buffer = usb_alloc_noncoherent(udev, size, gfp_flags, 1780 + &uvc_urb->dma, 1780 1781 uvc_stream_dir(stream), 1781 1782 &uvc_urb->sgt); 1782 1783 return !!uvc_urb->buffer; ··· 1814 1813 1815 1814 /* Retry allocations until one succeed. */ 1816 1815 for (; npackets > 0; npackets /= 2) { 1817 - stream->urb_size = psize * npackets; 1816 + unsigned int urb_size = psize * npackets; 1818 1817 1819 1818 for (i = 0; i < UVC_URBS; ++i) { 1820 1819 struct uvc_urb *uvc_urb = &stream->uvc_urb[i]; 1821 1820 1822 - if (!uvc_alloc_urb_buffer(stream, uvc_urb, gfp_flags)) { 1821 + if (!uvc_alloc_urb_buffer(stream, uvc_urb, urb_size, 1822 + gfp_flags)) { 1823 1823 uvc_free_urb_buffers(stream); 1824 1824 break; 1825 1825 } ··· 1832 1830 uvc_dbg(stream->dev, VIDEO, 1833 1831 "Allocated %u URB buffers of %ux%u bytes each\n", 1834 1832 UVC_URBS, npackets, psize); 1833 + stream->urb_size = urb_size; 1835 1834 return npackets; 1836 1835 } 1837 1836 } ··· 1840 1837 uvc_dbg(stream->dev, VIDEO, 1841 1838 "Failed to allocate URB buffers (%u bytes per packet)\n", 1842 1839 psize); 1843 - stream->urb_size = 0; 1844 1840 return 0; 1845 1841 } 1846 1842