[PATCH 4/4] usb: gadget: uvc: retry vb2_reqbufs() with vb_vmalloc_memops if use_sg fail

Xu Yang posted 4 patches 1 month ago
There is a newer version of this series
[PATCH 4/4] usb: gadget: uvc: retry vb2_reqbufs() with vb_vmalloc_memops if use_sg fail
Posted by Xu Yang 1 month ago
Based on the reality[1][2] that vb2_dma_sg_alloc() can't alloc buffer with
device DMA limits, those device will always get below error: "swiotlb
buffer is full (sz: 393216 bytes), total 65536 (slots), used 2358 (slots)"
and the uvc gadget function can't work at all.

The videobuf2-dma-sg.c driver doesn't has a formal improve about this issue
till now. To workaround the issue, lets retry vb2_reqbufs() with
vb_vmalloc_memops if it fails to allocate buffer with vb2_dma_sg_memops.

Link[1]: https://lore.kernel.org/linux-media/20230828075420.2009568-1-anle.pan@nxp.com/
Link[2]: https://lore.kernel.org/linux-media/20230914145812.12851-1-hui.fang@nxp.com/
Signed-off-by: Xu Yang <xu.yang_2@nxp.com>
---
 drivers/usb/gadget/function/uvc_queue.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/drivers/usb/gadget/function/uvc_queue.c b/drivers/usb/gadget/function/uvc_queue.c
index 21d80322cb6148ed87eb77f453a1f1644e4923ae..586e5524c171f115d98af5dda43fb800466f46d2 100644
--- a/drivers/usb/gadget/function/uvc_queue.c
+++ b/drivers/usb/gadget/function/uvc_queue.c
@@ -182,7 +182,15 @@ int uvcg_alloc_buffers(struct uvc_video_queue *queue,
 {
 	int ret;
 
+retry:
 	ret = vb2_reqbufs(&queue->queue, rb);
+	if (ret < 0 && queue->use_sg) {
+		uvc_trace(UVC_TRACE_IOCTL,
+			  "failed to alloc buffer with sg enabled, try non-sg mode\n");
+		queue->use_sg = 0;
+		queue->queue.mem_ops = &vb2_vmalloc_memops;
+		goto retry;
+	}
 
 	return ret ? ret : rb->count;
 }

-- 
2.34.1
Re: [PATCH 4/4] usb: gadget: uvc: retry vb2_reqbufs() with vb_vmalloc_memops if use_sg fail
Posted by Frank Li 1 month ago
On Thu, Jan 08, 2026 at 03:43:05PM +0800, Xu Yang wrote:
> Based on the reality[1][2] that vb2_dma_sg_alloc() can't alloc buffer with
> device DMA limits, those device will always get below error: "swiotlb
> buffer is full (sz: 393216 bytes), total 65536 (slots), used 2358 (slots)"
> and the uvc gadget function can't work at all.
>
> The videobuf2-dma-sg.c driver doesn't has a formal improve about this issue
> till now. To workaround the issue, lets retry vb2_reqbufs() with
> vb_vmalloc_memops if it fails to allocate buffer with vb2_dma_sg_memops.
>
> Link[1]: https://lore.kernel.org/linux-media/20230828075420.2009568-1-anle.pan@nxp.com/
> Link[2]: https://lore.kernel.org/linux-media/20230914145812.12851-1-hui.fang@nxp.com/
> Signed-off-by: Xu Yang <xu.yang_2@nxp.com>
> ---
>  drivers/usb/gadget/function/uvc_queue.c | 8 ++++++++
>  1 file changed, 8 insertions(+)
>
> diff --git a/drivers/usb/gadget/function/uvc_queue.c b/drivers/usb/gadget/function/uvc_queue.c
> index 21d80322cb6148ed87eb77f453a1f1644e4923ae..586e5524c171f115d98af5dda43fb800466f46d2 100644
> --- a/drivers/usb/gadget/function/uvc_queue.c
> +++ b/drivers/usb/gadget/function/uvc_queue.c
> @@ -182,7 +182,15 @@ int uvcg_alloc_buffers(struct uvc_video_queue *queue,
>  {
>  	int ret;
>
> +retry:
>  	ret = vb2_reqbufs(&queue->queue, rb);
> +	if (ret < 0 && queue->use_sg) {
> +		uvc_trace(UVC_TRACE_IOCTL,
> +			  "failed to alloc buffer with sg enabled, try non-sg mode\n");
> +		queue->use_sg = 0;
> +		queue->queue.mem_ops = &vb2_vmalloc_memops;

How it work if dma_sg_alloc() failure,  vmalloc success, follow dma_map()
should be failure for vmalloc()

Frank

> +		goto retry;
> +	}
>
>  	return ret ? ret : rb->count;
>  }
>
> --
> 2.34.1
>
Re: [PATCH 4/4] usb: gadget: uvc: retry vb2_reqbufs() with vb_vmalloc_memops if use_sg fail
Posted by Xu Yang 3 weeks, 6 days ago
On Thu, Jan 08, 2026 at 11:16:08AM -0500, Frank Li wrote:
> On Thu, Jan 08, 2026 at 03:43:05PM +0800, Xu Yang wrote:
> > Based on the reality[1][2] that vb2_dma_sg_alloc() can't alloc buffer with
> > device DMA limits, those device will always get below error: "swiotlb
> > buffer is full (sz: 393216 bytes), total 65536 (slots), used 2358 (slots)"
> > and the uvc gadget function can't work at all.
> >
> > The videobuf2-dma-sg.c driver doesn't has a formal improve about this issue
> > till now. To workaround the issue, lets retry vb2_reqbufs() with
> > vb_vmalloc_memops if it fails to allocate buffer with vb2_dma_sg_memops.
> >
> > Link[1]: https://lore.kernel.org/linux-media/20230828075420.2009568-1-anle.pan@nxp.com/
> > Link[2]: https://lore.kernel.org/linux-media/20230914145812.12851-1-hui.fang@nxp.com/
> > Signed-off-by: Xu Yang <xu.yang_2@nxp.com>
> > ---
> >  drivers/usb/gadget/function/uvc_queue.c | 8 ++++++++
> >  1 file changed, 8 insertions(+)
> >
> > diff --git a/drivers/usb/gadget/function/uvc_queue.c b/drivers/usb/gadget/function/uvc_queue.c
> > index 21d80322cb6148ed87eb77f453a1f1644e4923ae..586e5524c171f115d98af5dda43fb800466f46d2 100644
> > --- a/drivers/usb/gadget/function/uvc_queue.c
> > +++ b/drivers/usb/gadget/function/uvc_queue.c
> > @@ -182,7 +182,15 @@ int uvcg_alloc_buffers(struct uvc_video_queue *queue,
> >  {
> >  	int ret;
> >
> > +retry:
> >  	ret = vb2_reqbufs(&queue->queue, rb);
> > +	if (ret < 0 && queue->use_sg) {
> > +		uvc_trace(UVC_TRACE_IOCTL,
> > +			  "failed to alloc buffer with sg enabled, try non-sg mode\n");
> > +		queue->use_sg = 0;
> > +		queue->queue.mem_ops = &vb2_vmalloc_memops;
> 
> How it work if dma_sg_alloc() failure,  vmalloc success, follow dma_map()
> should be failure for vmalloc()

The point is the videobuf2 subsystem doesn't do dma_map() on vmalloc returned big buffer,
however, it do it for dma_sg returned buffer. 

If use vmalloced buffer, UVC gadget already allocate some small buffer for each usb_request
to do dma transfer, so uvc driver will memcopy data from big buffer to small buffer.

If use dma-sg-ed buffer, uvc driver won't memcopy data, instead it will use part of sg
buffer each time.

Then USB system will do usb_gadget_map_request() again before each transfer.

Thanks,
Xu Yang

> 
> Frank
> 
> > +		goto retry;
> > +	}
> >
> >  	return ret ? ret : rb->count;
> >  }
> >
> > --
> > 2.34.1
> >
Re: [PATCH 4/4] usb: gadget: uvc: retry vb2_reqbufs() with vb_vmalloc_memops if use_sg fail
Posted by Frank Li 3 weeks, 6 days ago
On Mon, Jan 12, 2026 at 06:05:40PM +0800, Xu Yang wrote:
> On Thu, Jan 08, 2026 at 11:16:08AM -0500, Frank Li wrote:
> > On Thu, Jan 08, 2026 at 03:43:05PM +0800, Xu Yang wrote:
> > > Based on the reality[1][2] that vb2_dma_sg_alloc() can't alloc buffer with
> > > device DMA limits, those device will always get below error: "swiotlb
> > > buffer is full (sz: 393216 bytes), total 65536 (slots), used 2358 (slots)"
> > > and the uvc gadget function can't work at all.
> > >
> > > The videobuf2-dma-sg.c driver doesn't has a formal improve about this issue
> > > till now. To workaround the issue, lets retry vb2_reqbufs() with
> > > vb_vmalloc_memops if it fails to allocate buffer with vb2_dma_sg_memops.
> > >
> > > Link[1]: https://lore.kernel.org/linux-media/20230828075420.2009568-1-anle.pan@nxp.com/
> > > Link[2]: https://lore.kernel.org/linux-media/20230914145812.12851-1-hui.fang@nxp.com/
> > > Signed-off-by: Xu Yang <xu.yang_2@nxp.com>
> > > ---
> > >  drivers/usb/gadget/function/uvc_queue.c | 8 ++++++++
> > >  1 file changed, 8 insertions(+)
> > >
> > > diff --git a/drivers/usb/gadget/function/uvc_queue.c b/drivers/usb/gadget/function/uvc_queue.c
> > > index 21d80322cb6148ed87eb77f453a1f1644e4923ae..586e5524c171f115d98af5dda43fb800466f46d2 100644
> > > --- a/drivers/usb/gadget/function/uvc_queue.c
> > > +++ b/drivers/usb/gadget/function/uvc_queue.c
> > > @@ -182,7 +182,15 @@ int uvcg_alloc_buffers(struct uvc_video_queue *queue,
> > >  {
> > >  	int ret;
> > >
> > > +retry:
> > >  	ret = vb2_reqbufs(&queue->queue, rb);
> > > +	if (ret < 0 && queue->use_sg) {
> > > +		uvc_trace(UVC_TRACE_IOCTL,
> > > +			  "failed to alloc buffer with sg enabled, try non-sg mode\n");
> > > +		queue->use_sg = 0;
> > > +		queue->queue.mem_ops = &vb2_vmalloc_memops;
> >
> > How it work if dma_sg_alloc() failure,  vmalloc success, follow dma_map()
> > should be failure for vmalloc()
>
> The point is the videobuf2 subsystem doesn't do dma_map() on vmalloc returned big buffer,
> however, it do it for dma_sg returned buffer.
>
> If use vmalloced buffer, UVC gadget already allocate some small buffer for each usb_request
> to do dma transfer, so uvc driver will memcopy data from big buffer to small buffer.

Need add such information to commit message.

Frank

>
> If use dma-sg-ed buffer, uvc driver won't memcopy data, instead it will use part of sg
> buffer each time.
>
> Then USB system will do usb_gadget_map_request() again before each transfer.
>
> Thanks,
> Xu Yang
>
> >
> > Frank
> >
> > > +		goto retry;
> > > +	}
> > >
> > >  	return ret ? ret : rb->count;
> > >  }
> > >
> > > --
> > > 2.34.1
> > >