Skip to contentSkip to conversation

usb:gadget:uvc Do not use worker thread to pump usb requests

PATCH Superseded Jayant Chowdhary 6 participants 31 messages Oct 25, 2023 – Nov 20, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb:gadget:uvc Do not use worker thread to pump usb requests

This patch is based on top of
https://lore.kernel.org/linux-usb/[email protected]/T/#t:

When we use an async work queue to perform the function of pumping usb requests to the usb controller, it is possible that thread scheduling affects at what cadence we're able to pump requests. This could mean usb requests miss their uframes - resulting in video stream flickers on the host device.

In this patch, we move the pumping of usb requests to
1) uvcg_video_complete() complete handler for both isoc + bulk endpoints. We still send 0 length requests when there is no uvc buffer available to encode.
2) uvc_v4l2_qbuf - only for bulk endpoints since it is not legal to send 0 length requests.

---
 drivers/usb/gadget/function/f_uvc.c     |  4 --
 drivers/usb/gadget/function/uvc.h       |  4 +-
 drivers/usb/gadget/function/uvc_v4l2.c  |  5 +-
 drivers/usb/gadget/function/uvc_video.c | 72 ++++++++++++++++---------
 drivers/usb/gadget/function/uvc_video.h |  2 +
 5 files changed, 52 insertions(+), 35 deletions(-)
drivers/usb/gadget/function/f_uvc.c +0 -4
index ae08341961eb..53cb2539486d 100644
@@ -959,14 +959,10 @@ static void uvc_function_unbind(struct usb_configuration *c,
 {
 	struct usb_composite_dev *cdev = c->cdev;
 	struct uvc_device *uvc = to_uvc(f);
-	struct uvc_video *video = &uvc->video;
 	long wait_ret = 1;
 
 	uvcg_info(f, "%s()\n", __func__);
 
-	if (video->async_wq)
-		destroy_workqueue(video->async_wq);
-
 	/*
 	 * If we know we're connected via v4l2, then there should be a cleanup
 	 * of the device from userspace either via UVC_EVENT_DISCONNECT or
drivers/usb/gadget/function/uvc.h +1 -3
index be0d012aa244..498f344fda4b 100644
@@ -88,9 +88,6 @@ struct uvc_video {
 	struct uvc_device *uvc;
 	struct usb_ep *ep;
 
-	struct work_struct pump;
-	struct workqueue_struct *async_wq;
-
 	/* Frame parameters */
 	u8 bpp;
 	u32 fcc;
@@ -116,6 +113,7 @@ struct uvc_video {
 	/* Context data used by the completion handler */
 	__u32 payload_size;
 	__u32 max_payload_size;
+	bool is_bulk;
 
 	struct uvc_video_queue queue;
 	unsigned int fid;
drivers/usb/gadget/function/uvc_v4l2.c +1 -4
index f4d2e24835d4..678ea6df7b5c 100644
@@ -414,10 +414,7 @@ uvc_v4l2_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
 	ret = uvcg_queue_buffer(&video->queue, b);
 	if (ret < 0)
 		return ret;
-
-	if (uvc->state == UVC_STATE_STREAMING)
-		queue_work(video->async_wq, &video->pump);
-
+	uvcg_video_pump_qbuf(video);
 	return ret;
 }
 
drivers/usb/gadget/function/uvc_video.c +48 -24

139 lines not on this page · raw message

drivers/usb/gadget/function/uvc_video.h +2 -0
index 03adeefa343b..29c6b9a2e9c3 100644
@@ -18,4 +18,6 @@ int uvcg_video_enable(struct uvc_video *video, int enable);
 
 int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc);
 
+void uvcg_video_pump_qbuf(struct uvc_video *video);
+
 #endif /* __UVC_VIDEO_H__ */
#1 lore raw
Quoted 113 lines · Jayant Chowdhary

This patch is based on top of
https://lore.kernel.org/linux-usb/[email protected]/T/#t:

When we use an async work queue to perform the function of pumping usb requests to the usb controller, it is possible that thread scheduling affects at what cadence we're able to pump requests. This could mean usb requests miss their uframes - resulting in video stream flickers on the host device.

In this patch, we move the pumping of usb requests to
1) uvcg_video_complete() complete handler for both isoc + bulk endpoints. We still send 0 length requests when there is no uvc buffer available to encode.
2) uvc_v4l2_qbuf - only for bulk endpoints since it is not legal to send 0 length requests.

---

drivers/usb/gadget/function/f_uvc.c     |  4 --
drivers/usb/gadget/function/uvc.h       |  4 +-
drivers/usb/gadget/function/uvc_v4l2.c  |  5 +-
drivers/usb/gadget/function/uvc_video.c | 72 ++++++++++++++++---------
drivers/usb/gadget/function/uvc_video.h |  2 +
5 files changed, 52 insertions(+), 35 deletions(-)
drivers/usb/gadget/function/f_uvc.c +0 -4
index ae08341961eb..53cb2539486d 100644
@@ -959,14 +959,10 @@ static void uvc_function_unbind(struct usb_configuration *c,
 {
 	struct usb_composite_dev *cdev = c->cdev;
 	struct uvc_device *uvc = to_uvc(f);
-	struct uvc_video *video = &uvc->video;
 	long wait_ret = 1;
 
 	uvcg_info(f, "%s()\n", __func__);
 
-	if (video->async_wq)
-		destroy_workqueue(video->async_wq);
-
 	/*
 	 * If we know we're connected via v4l2, then there should be a cleanup
 	 * of the device from userspace either via UVC_EVENT_DISCONNECT or
drivers/usb/gadget/function/uvc.h +1 -3
index be0d012aa244..498f344fda4b 100644
@@ -88,9 +88,6 @@ struct uvc_video {
 	struct uvc_device *uvc;
 	struct usb_ep *ep;
 
-	struct work_struct pump;
-	struct workqueue_struct *async_wq;
-
 	/* Frame parameters */
 	u8 bpp;
 	u32 fcc;
@@ -116,6 +113,7 @@ struct uvc_video {
 	/* Context data used by the completion handler */
 	__u32 payload_size;
 	__u32 max_payload_size;
+	bool is_bulk;
 
 	struct uvc_video_queue queue;
 	unsigned int fid;
drivers/usb/gadget/function/uvc_v4l2.c +1 -4
index f4d2e24835d4..678ea6df7b5c 100644
@@ -414,10 +414,7 @@ uvc_v4l2_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
 	ret = uvcg_queue_buffer(&video->queue, b);
 	if (ret < 0)
 		return ret;
-
-	if (uvc->state == UVC_STATE_STREAMING)
-		queue_work(video->async_wq, &video->pump);
-
+	uvcg_video_pump_qbuf(video);
 	return ret;
 }
 
drivers/usb/gadget/function/uvc_video.c +2 -0
index ab3f02054e85..143453e9f003 100644
@@ -24,6 +24,8 @@
 * Video codecs
 */
 
+static void uvcg_video_pump(struct uvc_video *video);
+

static int
uvc_video_encode_header(struct uvc_video *video, struct uvc_buffer *buf,
u8 *data, int len)

drivers/usb/gadget/function/uvc_video.c +3 -1
@@ -329,7 +331,9 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
 	 */
 	if (video->is_enabled) {
 		list_add_tail(&req->list, &video->req_free);
-		queue_work(video->async_wq, &video->pump);
+		spin_unlock_irqrestore(&video->req_lock, flags);
+		uvcg_video_pump(video);
+		return;
 	} else {
 		uvc_video_free_request(ureq, ep);
 	}
@@ -409,20 +413,32 @@ uvc_video_alloc_requests(struct uvc_video *video)
 * Video streaming
 */
 

drivers/usb/gadget/function/uvc_video.c uvc_video_alloc_requests(struct uvc_video *video) · line 425

Quoted 10 lines
drivers/usb/gadget/function/uvc_video.c +10 -0
+void uvcg_video_pump_qbuf(struct uvc_video *video)
+{
+	if (video->is_bulk) {
+		/*
+		 * Only call uvcg_video_pump() from qbuf, for bulk eps since
+		 * for isoc, the complete handler will call uvcg_video_pump()
+		 * consistently. Calling it for isoc eps, while correct
+		 * will increase contention for video->req_lock since the
+		 * complete handler will be called more often.
+		 */

Could you move the comment above the condition and remove the extra braces here.

Quoted 127 lines
drivers/usb/gadget/function/uvc_video.c +33 -23
+		uvcg_video_pump(video);
+	}
+}
+
 /*
 * uvcg_video_pump - Pump video data into the USB requests
 *
 * This function fills the available USB requests (listed in req_free) with
 * video data from the queued buffers.
 */
-static void uvcg_video_pump(struct work_struct *work)
+static void uvcg_video_pump(struct uvc_video *video)
 {
-	struct uvc_video *video = container_of(work, struct uvc_video, pump);
 	struct uvc_video_queue *queue = &video->queue;
-	/* video->max_payload_size is only set when using bulk transfer */
-	bool is_bulk = video->max_payload_size;
 	struct usb_request *req = NULL;
-	struct uvc_buffer *buf;
+	struct uvc_request *ureq = NULL;
+	struct uvc_buffer *buf = NULL, *last_buf = NULL;
 	unsigned long flags;
 	bool buf_done;
 	int ret;
@@ -455,7 +471,8 @@ static void uvcg_video_pump(struct work_struct *work)
 		if (buf != NULL) {
 			video->encode(req, video, buf);
 			buf_done = buf->state == UVC_BUF_STATE_DONE;
-		} else if (!(queue->flags & UVC_QUEUE_DISCONNECTED) && !is_bulk) {
+		} else if (!(queue->flags & UVC_QUEUE_DISCONNECTED) &&
+				!video->is_bulk) {
 			/*
 			 * No video buffer available; the queue is still connected and
 			 * we're transferring over ISOC. Queue a 0 length request to
@@ -500,18 +517,30 @@ static void uvcg_video_pump(struct work_struct *work)
 			req->no_interrupt = 1;
 		}
 
-		/* Queue the USB request */
-		ret = uvcg_video_ep_queue(video, req);
 		spin_unlock_irqrestore(&queue->irqlock, flags);
-
+		spin_lock_irqsave(&video->req_lock, flags);
+		if (video->is_enabled) {
+			/* Queue the USB request */
+			ret = uvcg_video_ep_queue(video, req);
+			/* Endpoint now owns the request */
+			req = NULL;
+			video->req_int_count++;
+		} else {
+			ret =  -ENODEV;
+			ureq = req->context;
+			last_buf = ureq->last_buf;
+			ureq->last_buf = NULL;
+		}
+		spin_unlock_irqrestore(&video->req_lock, flags);
 		if (ret < 0) {
+			if (last_buf != NULL) {
+				// Return the buffer to the queue in the case the
+				// request was not queued to the ep.
+				uvcg_complete_buffer(&video->queue, last_buf);
+			}
 			uvcg_queue_cancel(queue, 0);
 			break;
 		}
-
-		/* Endpoint now owns the request */
-		req = NULL;
-		video->req_int_count++;
 	}
 
 	if (!req)
@@ -556,7 +585,6 @@ uvcg_video_disable(struct uvc_video *video)
 	}
 	spin_unlock_irqrestore(&video->req_lock, flags);
 
-	cancel_work_sync(&video->pump);
 	uvcg_queue_cancel(&video->queue, 0);
 
 	spin_lock_irqsave(&video->req_lock, flags);
@@ -626,14 +654,16 @@ int uvcg_video_enable(struct uvc_video *video, int enable)
 	if (video->max_payload_size) {
 		video->encode = uvc_video_encode_bulk;
 		video->payload_size = 0;
-	} else
+		video->is_bulk = true;
+	} else {
 		video->encode = video->queue.use_sg ?
 			uvc_video_encode_isoc_sg : uvc_video_encode_isoc;
+		video->is_bulk = false;
+	}
 
 	video->req_int_count = 0;
 
-	queue_work(video->async_wq, &video->pump);
-
+	uvcg_video_pump(video);
 	return ret;
 }
 
@@ -646,12 +676,6 @@ int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
 	INIT_LIST_HEAD(&video->ureqs);
 	INIT_LIST_HEAD(&video->req_free);
 	spin_lock_init(&video->req_lock);
-	INIT_WORK(&video->pump, uvcg_video_pump);
-
-	/* Allocate a work queue for asynchronous video pump handler. */
-	video->async_wq = alloc_workqueue("uvcgadget", WQ_UNBOUND | WQ_HIGHPRI, 0);
-	if (!video->async_wq)
-		return -EINVAL;
 
 	video->uvc = uvc;
 	video->fcc = V4L2_PIX_FMT_YUYV;
drivers/usb/gadget/function/uvc_video.h +2 -0
index 03adeefa343b..29c6b9a2e9c3 100644
@@ -18,4 +18,6 @@ int uvcg_video_enable(struct uvc_video *video, int enable);
 
 int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc);
 
+void uvcg_video_pump_qbuf(struct uvc_video *video);
+
 #endif /* __UVC_VIDEO_H__ */
Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#2 lore raw

PATCH v2 1/1 usb:gadget:uvc Do not use worker thread to pump usb requests

This patch is based on top of
https://lore.kernel.org/linux-usb/[email protected]/T/#t:

When we use an async work queue to perform the function of pumping usb requests to the usb controller, it is possible that thread scheduling affects at what cadence we're able to pump requests. This could mean usb requests miss their uframes - resulting in video stream flickers on the host device.

In this patch, we move the pumping of usb requests to
1) uvcg_video_complete() complete handler for both isoc + bulk endpoints. We still send 0 length requests when there is no uvc buffer available to encode.
2) uvc_v4l2_qbuf - only for bulk endpoints since it is not legal to send 0 length requests.

---
v1->v2: Fix code style and add self Signed-off-by

 drivers/usb/gadget/function/f_uvc.c     |  4 --
 drivers/usb/gadget/function/uvc.h       |  4 +-
 drivers/usb/gadget/function/uvc_v4l2.c  |  5 +-
 drivers/usb/gadget/function/uvc_video.c | 71 ++++++++++++++++---------
 drivers/usb/gadget/function/uvc_video.h |  2 +
 5 files changed, 51 insertions(+), 35 deletions(-)
drivers/usb/gadget/function/f_uvc.c +0 -4
index ae08341961eb..53cb2539486d 100644
@@ -959,14 +959,10 @@ static void uvc_function_unbind(struct usb_configuration *c,
 {
 	struct usb_composite_dev *cdev = c->cdev;
 	struct uvc_device *uvc = to_uvc(f);
-	struct uvc_video *video = &uvc->video;
 	long wait_ret = 1;
 
 	uvcg_info(f, "%s()\n", __func__);
 
-	if (video->async_wq)
-		destroy_workqueue(video->async_wq);
-
 	/*
 	 * If we know we're connected via v4l2, then there should be a cleanup
 	 * of the device from userspace either via UVC_EVENT_DISCONNECT or
drivers/usb/gadget/function/uvc.h +1 -3
index be0d012aa244..498f344fda4b 100644
@@ -88,9 +88,6 @@ struct uvc_video {
 	struct uvc_device *uvc;
 	struct usb_ep *ep;
 
-	struct work_struct pump;
-	struct workqueue_struct *async_wq;
-
 	/* Frame parameters */
 	u8 bpp;
 	u32 fcc;
@@ -116,6 +113,7 @@ struct uvc_video {
 	/* Context data used by the completion handler */
 	__u32 payload_size;
 	__u32 max_payload_size;
+	bool is_bulk;
 
 	struct uvc_video_queue queue;
 	unsigned int fid;
drivers/usb/gadget/function/uvc_v4l2.c +1 -4
index f4d2e24835d4..678ea6df7b5c 100644
@@ -414,10 +414,7 @@ uvc_v4l2_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
 	ret = uvcg_queue_buffer(&video->queue, b);
 	if (ret < 0)
 		return ret;
-
-	if (uvc->state == UVC_STATE_STREAMING)
-		queue_work(video->async_wq, &video->pump);
-
+	uvcg_video_pump_qbuf(video);
 	return ret;
 }
 
drivers/usb/gadget/function/uvc_video.c +47 -24

138 lines not on this page · raw message

drivers/usb/gadget/function/uvc_video.h +2 -0
index 03adeefa343b..29c6b9a2e9c3 100644
@@ -18,4 +18,6 @@ int uvcg_video_enable(struct uvc_video *video, int enable);
 
 int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc);
 
+void uvcg_video_pump_qbuf(struct uvc_video *video);
+
 #endif /* __UVC_VIDEO_H__ */
Signature · 1 line
2.42.0.820.g83a721a137-goog
maintainer
#3 lore raw
Quoted 2 lines · Jayant Chowdhary

This patch is based on top of
https://lore.kernel.org/linux-usb/[email protected]/T/#t:

That doesn't work in the changelog of a patch at all, it goes below the --- line p lease.

Quoted 14 lines

When we use an async work queue to perform the function of pumping usb requests to the usb controller, it is possible that thread scheduling affects at what cadence we're able to pump requests. This could mean usb requests miss their uframes - resulting in video stream flickers on the host device.

In this patch, we move the pumping of usb requests to
1) uvcg_video_complete() complete handler for both isoc + bulk endpoints. We still send 0 length requests when there is no uvc buffer available to encode.
2) uvc_v4l2_qbuf - only for bulk endpoints since it is not legal to send 0 length requests.

Quoted 7 lines

---
v1->v2: Fix code style and add self Signed-off-by

Great, but as signed-off-by kind of implies you tested it, no need for the tested-by now, right? Not a big deal, and normally I'd ignore it but I know you at least have to do one more version of this based on the above problem...

thanks,

greg k-h

#4 lore raw
In reply to Jayant Chowdhary

Hi Jayant,

Thank you for the patch.

Quoted 9 lines · Jayant Chowdhary

This patch is based on top of
https://lore.kernel.org/linux-usb/[email protected]/T/#t:

When we use an async work queue to perform the function of pumping usb requests to the usb controller, it is possible that thread scheduling affects at what cadence we're able to pump requests. This could mean usb requests miss their uframes - resulting in video stream flickers on the host device.

Quoted 4 lines

In this patch, we move the pumping of usb requests to
1) uvcg_video_complete() complete handler for both isoc + bulk endpoints. We still send 0 length requests when there is no uvc buffer available to encode.

This means you will end up copying large amounts of data in interrupt context. The work queue was there to avoid exactly that, as it will introduce delays that can affect other parts of the system. I think this is a problem.

Quoted 52 lines

2) uvc_v4l2_qbuf - only for bulk endpoints since it is not legal to send 0 length requests.

---
v1->v2: Fix code style and add self Signed-off-by

 drivers/usb/gadget/function/f_uvc.c     |  4 --
 drivers/usb/gadget/function/uvc.h       |  4 +-
 drivers/usb/gadget/function/uvc_v4l2.c  |  5 +-
 drivers/usb/gadget/function/uvc_video.c | 71 ++++++++++++++++---------
 drivers/usb/gadget/function/uvc_video.h |  2 +
 5 files changed, 51 insertions(+), 35 deletions(-)
drivers/usb/gadget/function/f_uvc.c +0 -4
index ae08341961eb..53cb2539486d 100644
@@ -959,14 +959,10 @@ static void uvc_function_unbind(struct usb_configuration *c,
 {
 	struct usb_composite_dev *cdev = c->cdev;
 	struct uvc_device *uvc = to_uvc(f);
-	struct uvc_video *video = &uvc->video;
 	long wait_ret = 1;
 
 	uvcg_info(f, "%s()\n", __func__);
 
-	if (video->async_wq)
-		destroy_workqueue(video->async_wq);
-
 	/*
 	 * If we know we're connected via v4l2, then there should be a cleanup
 	 * of the device from userspace either via UVC_EVENT_DISCONNECT or
drivers/usb/gadget/function/uvc.h +0 -3
index be0d012aa244..498f344fda4b 100644
@@ -88,9 +88,6 @@ struct uvc_video {
 	struct uvc_device *uvc;
 	struct usb_ep *ep;
 
-	struct work_struct pump;
-	struct workqueue_struct *async_wq;
-
 	/* Frame parameters */
 	u8 bpp;
 	u32 fcc;

drivers/usb/gadget/function/uvc.h struct uvc_video { · line 116

Quoted 4 lines
drivers/usb/gadget/function/uvc.h +1 -0
@@ -116,6 +113,7 @@ struct uvc_video {
 	/* Context data used by the completion handler */
 	__u32 payload_size;
 	__u32 max_payload_size;
+	bool is_bulk;

This should be introduced in a separate patch.

Quoted 48 lines
drivers/usb/gadget/function/uvc.h +0 -0
 	struct uvc_video_queue queue;
 	unsigned int fid;
drivers/usb/gadget/function/uvc_v4l2.c +1 -4
index f4d2e24835d4..678ea6df7b5c 100644
@@ -414,10 +414,7 @@ uvc_v4l2_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
 	ret = uvcg_queue_buffer(&video->queue, b);
 	if (ret < 0)
 		return ret;
-
-	if (uvc->state == UVC_STATE_STREAMING)
-		queue_work(video->async_wq, &video->pump);
-
+	uvcg_video_pump_qbuf(video);
 	return ret;
 }
 
drivers/usb/gadget/function/uvc_video.c +6 -1
index ab3f02054e85..0fcd8e5edbac 100644
@@ -24,6 +24,8 @@
  * Video codecs
  */
 
+static void uvcg_video_pump(struct uvc_video *video);
+
 static int
 uvc_video_encode_header(struct uvc_video *video, struct uvc_buffer *buf,
 		u8 *data, int len)
@@ -329,7 +331,9 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
 	 */
 	if (video->is_enabled) {
 		list_add_tail(&req->list, &video->req_free);
-		queue_work(video->async_wq, &video->pump);
+		spin_unlock_irqrestore(&video->req_lock, flags);
+		uvcg_video_pump(video);
+		return;
 	} else {
 		uvc_video_free_request(ureq, ep);
 	}
@@ -409,20 +413,31 @@ uvc_video_alloc_requests(struct uvc_video *video)
  * Video streaming
  */
 
+void uvcg_video_pump_qbuf(struct uvc_video *video)

drivers/usb/gadget/function/uvc_video.c uvc_video_alloc_requests(struct uvc_video *video) · line 426

Quoted 10 lines
drivers/usb/gadget/function/uvc_video.c +10 -0
+{
+	/*
+	 * Only call uvcg_video_pump() from qbuf, for bulk eps since
+	 * for isoc, the complete handler will call uvcg_video_pump()
+	 * consistently. Calling it for isoc eps, while correct
+	 * will increase contention for video->req_lock since the
+	 * complete handler will be called more often.
+	*/
+	if (video->is_bulk)
+		uvcg_video_pump(video);

Am I the only one to see the *major* race condition that this patch introduces ?

Quoted 48 lines
drivers/usb/gadget/function/uvc_video.c +15 -9
+}
+
 /*
  * uvcg_video_pump - Pump video data into the USB requests
  *
  * This function fills the available USB requests (listed in req_free) with
  * video data from the queued buffers.
  */
-static void uvcg_video_pump(struct work_struct *work)
+static void uvcg_video_pump(struct uvc_video *video)
 {
-	struct uvc_video *video = container_of(work, struct uvc_video, pump);
 	struct uvc_video_queue *queue = &video->queue;
-	/* video->max_payload_size is only set when using bulk transfer */
-	bool is_bulk = video->max_payload_size;
 	struct usb_request *req = NULL;
-	struct uvc_buffer *buf;
+	struct uvc_request *ureq = NULL;
+	struct uvc_buffer *buf = NULL, *last_buf = NULL;
 	unsigned long flags;
 	bool buf_done;
 	int ret;
@@ -455,7 +470,8 @@ static void uvcg_video_pump(struct work_struct *work)
 		if (buf != NULL) {
 			video->encode(req, video, buf);
 			buf_done = buf->state == UVC_BUF_STATE_DONE;
-		} else if (!(queue->flags & UVC_QUEUE_DISCONNECTED) && !is_bulk) {
+		} else if (!(queue->flags & UVC_QUEUE_DISCONNECTED) &&
+				!video->is_bulk) {
 			/*
 			 * No video buffer available; the queue is still connected and
 			 * we're transferring over ISOC. Queue a 0 length request to
@@ -500,18 +516,30 @@ static void uvcg_video_pump(struct work_struct *work)
 			req->no_interrupt = 1;
 		}
 
-		/* Queue the USB request */
-		ret = uvcg_video_ep_queue(video, req);
 		spin_unlock_irqrestore(&queue->irqlock, flags);
-
+		spin_lock_irqsave(&video->req_lock, flags);
+		if (video->is_enabled) {
+			/* Queue the USB request */
+			ret = uvcg_video_ep_queue(video, req);
+			/* Endpoint now owns the request */
+			req = NULL;
+			video->req_int_count++;
+		} else {

drivers/usb/gadget/function/uvc_video.c static void uvcg_video_pump(struct work_struct *work) · line 537

Quoted 10 lines
drivers/usb/gadget/function/uvc_video.c +9 -0
+			ret =  -ENODEV;
+			ureq = req->context;
+			last_buf = ureq->last_buf;
+			ureq->last_buf = NULL;
+		}
+		spin_unlock_irqrestore(&video->req_lock, flags);
 		if (ret < 0) {
+			if (last_buf != NULL) {
+				// Return the buffer to the queue in the case the
+				// request was not queued to the ep.

Wrong comment style.

Quoted 64 lines
drivers/usb/gadget/function/uvc_video.c +7 -14
+				uvcg_complete_buffer(&video->queue, last_buf);
+			}
 			uvcg_queue_cancel(queue, 0);
 			break;
 		}
-
-		/* Endpoint now owns the request */
-		req = NULL;
-		video->req_int_count++;
 	}
 
 	if (!req)
@@ -556,7 +584,6 @@ uvcg_video_disable(struct uvc_video *video)
 	}
 	spin_unlock_irqrestore(&video->req_lock, flags);
 
-	cancel_work_sync(&video->pump);
 	uvcg_queue_cancel(&video->queue, 0);
 
 	spin_lock_irqsave(&video->req_lock, flags);
@@ -626,14 +653,16 @@ int uvcg_video_enable(struct uvc_video *video, int enable)
 	if (video->max_payload_size) {
 		video->encode = uvc_video_encode_bulk;
 		video->payload_size = 0;
-	} else
+		video->is_bulk = true;
+	} else {
 		video->encode = video->queue.use_sg ?
 			uvc_video_encode_isoc_sg : uvc_video_encode_isoc;
+		video->is_bulk = false;
+	}
 
 	video->req_int_count = 0;
 
-	queue_work(video->async_wq, &video->pump);
-
+	uvcg_video_pump(video);
 	return ret;
 }
 
@@ -646,12 +675,6 @@ int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
 	INIT_LIST_HEAD(&video->ureqs);
 	INIT_LIST_HEAD(&video->req_free);
 	spin_lock_init(&video->req_lock);
-	INIT_WORK(&video->pump, uvcg_video_pump);
-
-	/* Allocate a work queue for asynchronous video pump handler. */
-	video->async_wq = alloc_workqueue("uvcgadget", WQ_UNBOUND | WQ_HIGHPRI, 0);
-	if (!video->async_wq)
-		return -EINVAL;
 
 	video->uvc = uvc;
 	video->fcc = V4L2_PIX_FMT_YUYV;
drivers/usb/gadget/function/uvc_video.h +2 -0
index 03adeefa343b..29c6b9a2e9c3 100644
@@ -18,4 +18,6 @@ int uvcg_video_enable(struct uvc_video *video, int enable);
 
 int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc);
 
+void uvcg_video_pump_qbuf(struct uvc_video *video);
+
 #endif /* __UVC_VIDEO_H__ */
Signature · 3 lines
Regards,

Laurent Pinchart
#5 lore raw
Quoted 17 lines · Laurent Pinchart

Thank you for the patch.

Quoted 9 lines · Jayant Chowdhary

This patch is based on top of
https://lore.kernel.org/linux-usb/[email protected]/T/#t:

When we use an async work queue to perform the function of pumping usb requests to the usb controller, it is possible that thread scheduling affects at what cadence we're able to pump requests. This could mean usb requests miss their uframes - resulting in video stream flickers on the host device.

Quoted 4 lines

In this patch, we move the pumping of usb requests to
1) uvcg_video_complete() complete handler for both isoc + bulk endpoints. We still send 0 length requests when there is no uvc buffer available to encode.

Quoted 4 lines

This means you will end up copying large amounts of data in interrupt context. The work queue was there to avoid exactly that, as it will introduce delays that can affect other parts of the system. I think this is a problem.

Regarding Thin's argument about possible scheduling latency that is already introducing real errors, this seemed like a good solution.

But sure, this potential latency introduced in the interrupt context can trigger other side effects.

However I think we need some compromise since both arguments are very valid.

Any ideas, how to solve this?

Quoted 106 lines
Quoted 52 lines

2) uvc_v4l2_qbuf - only for bulk endpoints since it is not legal to send 0 length requests.

---
v1->v2: Fix code style and add self Signed-off-by

 drivers/usb/gadget/function/f_uvc.c     |  4 --
 drivers/usb/gadget/function/uvc.h       |  4 +-
 drivers/usb/gadget/function/uvc_v4l2.c  |  5 +-
 drivers/usb/gadget/function/uvc_video.c | 71 ++++++++++++++++---------
 drivers/usb/gadget/function/uvc_video.h |  2 +
 5 files changed, 51 insertions(+), 35 deletions(-)
drivers/usb/gadget/function/f_uvc.c +0 -4
index ae08341961eb..53cb2539486d 100644
@@ -959,14 +959,10 @@ static void uvc_function_unbind(struct usb_configuration *c,
 {
 	struct usb_composite_dev *cdev = c->cdev;
 	struct uvc_device *uvc = to_uvc(f);
-	struct uvc_video *video = &uvc->video;
 	long wait_ret = 1;
 
 	uvcg_info(f, "%s()\n", __func__);
 
-	if (video->async_wq)
-		destroy_workqueue(video->async_wq);
-
 	/*
 	 * If we know we're connected via v4l2, then there should be a cleanup
 	 * of the device from userspace either via UVC_EVENT_DISCONNECT or
drivers/usb/gadget/function/uvc.h +0 -3
index be0d012aa244..498f344fda4b 100644
@@ -88,9 +88,6 @@ struct uvc_video {
 	struct uvc_device *uvc;
 	struct usb_ep *ep;
 
-	struct work_struct pump;
-	struct workqueue_struct *async_wq;
-
 	/* Frame parameters */
 	u8 bpp;
 	u32 fcc;

drivers/usb/gadget/function/uvc.h struct uvc_video { · line 116

Quoted 4 lines
drivers/usb/gadget/function/uvc.h +1 -0
@@ -116,6 +113,7 @@ struct uvc_video {
 	/* Context data used by the completion handler */
 	__u32 payload_size;
 	__u32 max_payload_size;
+	bool is_bulk;

This should be introduced in a separate patch.

Quoted 47 lines
drivers/usb/gadget/function/uvc.h +0 -0
 	struct uvc_video_queue queue;
 	unsigned int fid;
drivers/usb/gadget/function/uvc_v4l2.c +1 -4
index f4d2e24835d4..678ea6df7b5c 100644
@@ -414,10 +414,7 @@ uvc_v4l2_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
 	ret = uvcg_queue_buffer(&video->queue, b);
 	if (ret < 0)
 		return ret;
-
-	if (uvc->state == UVC_STATE_STREAMING)
-		queue_work(video->async_wq, &video->pump);
-
+	uvcg_video_pump_qbuf(video);
 	return ret;
 }
 
drivers/usb/gadget/function/uvc_video.c +5 -1
index ab3f02054e85..0fcd8e5edbac 100644
@@ -24,6 +24,8 @@
  * Video codecs
  */
 
+static void uvcg_video_pump(struct uvc_video *video);
+
 static int
 uvc_video_encode_header(struct uvc_video *video, struct uvc_buffer *buf,
 		u8 *data, int len)
@@ -329,7 +331,9 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
 	 */
 	if (video->is_enabled) {
 		list_add_tail(&req->list, &video->req_free);
-		queue_work(video->async_wq, &video->pump);
+		spin_unlock_irqrestore(&video->req_lock, flags);
+		uvcg_video_pump(video);
+		return;
 	} else {
 		uvc_video_free_request(ureq, ep);
 	}
@@ -409,20 +413,31 @@ uvc_video_alloc_requests(struct uvc_video *video)
  * Video streaming
  */
 
Quoted 14 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_alloc_requests(struct uvc_video *video) · line 426

Quoted 11 lines
drivers/usb/gadget/function/uvc_video.c +11 -0
+void uvcg_video_pump_qbuf(struct uvc_video *video)
+{
+	/*
+	 * Only call uvcg_video_pump() from qbuf, for bulk eps since
+	 * for isoc, the complete handler will call uvcg_video_pump()
+	 * consistently. Calling it for isoc eps, while correct
+	 * will increase contention for video->req_lock since the
+	 * complete handler will be called more often.
+	*/
+	if (video->is_bulk)
+		uvcg_video_pump(video);

Am I the only one to see the *major* race condition that this patch introduces ?

Possible that you are. Please elaborate.

Quoted 131 lines
Quoted 48 lines
drivers/usb/gadget/function/uvc_video.c +15 -9
+}
+
 /*
  * uvcg_video_pump - Pump video data into the USB requests
  *
  * This function fills the available USB requests (listed in req_free) with
  * video data from the queued buffers.
  */
-static void uvcg_video_pump(struct work_struct *work)
+static void uvcg_video_pump(struct uvc_video *video)
 {
-	struct uvc_video *video = container_of(work, struct uvc_video, pump);
 	struct uvc_video_queue *queue = &video->queue;
-	/* video->max_payload_size is only set when using bulk transfer */
-	bool is_bulk = video->max_payload_size;
 	struct usb_request *req = NULL;
-	struct uvc_buffer *buf;
+	struct uvc_request *ureq = NULL;
+	struct uvc_buffer *buf = NULL, *last_buf = NULL;
 	unsigned long flags;
 	bool buf_done;
 	int ret;
@@ -455,7 +470,8 @@ static void uvcg_video_pump(struct work_struct *work)
 		if (buf != NULL) {
 			video->encode(req, video, buf);
 			buf_done = buf->state == UVC_BUF_STATE_DONE;
-		} else if (!(queue->flags & UVC_QUEUE_DISCONNECTED) && !is_bulk) {
+		} else if (!(queue->flags & UVC_QUEUE_DISCONNECTED) &&
+				!video->is_bulk) {
 			/*
 			 * No video buffer available; the queue is still connected and
 			 * we're transferring over ISOC. Queue a 0 length request to
@@ -500,18 +516,30 @@ static void uvcg_video_pump(struct work_struct *work)
 			req->no_interrupt = 1;
 		}
 
-		/* Queue the USB request */
-		ret = uvcg_video_ep_queue(video, req);
 		spin_unlock_irqrestore(&queue->irqlock, flags);
-
+		spin_lock_irqsave(&video->req_lock, flags);
+		if (video->is_enabled) {
+			/* Queue the USB request */
+			ret = uvcg_video_ep_queue(video, req);
+			/* Endpoint now owns the request */
+			req = NULL;
+			video->req_int_count++;
+		} else {

drivers/usb/gadget/function/uvc_video.c static void uvcg_video_pump(struct work_struct *work) · line 537

Quoted 10 lines
drivers/usb/gadget/function/uvc_video.c +9 -0
+			ret =  -ENODEV;
+			ureq = req->context;
+			last_buf = ureq->last_buf;
+			ureq->last_buf = NULL;
+		}
+		spin_unlock_irqrestore(&video->req_lock, flags);
 		if (ret < 0) {
+			if (last_buf != NULL) {
+				// Return the buffer to the queue in the case the
+				// request was not queued to the ep.

Wrong comment style.

Quoted 64 lines
drivers/usb/gadget/function/uvc_video.c +7 -14
+				uvcg_complete_buffer(&video->queue, last_buf);
+			}
 			uvcg_queue_cancel(queue, 0);
 			break;
 		}
-
-		/* Endpoint now owns the request */
-		req = NULL;
-		video->req_int_count++;
 	}
 
 	if (!req)
@@ -556,7 +584,6 @@ uvcg_video_disable(struct uvc_video *video)
 	}
 	spin_unlock_irqrestore(&video->req_lock, flags);
 
-	cancel_work_sync(&video->pump);
 	uvcg_queue_cancel(&video->queue, 0);
 
 	spin_lock_irqsave(&video->req_lock, flags);
@@ -626,14 +653,16 @@ int uvcg_video_enable(struct uvc_video *video, int enable)
 	if (video->max_payload_size) {
 		video->encode = uvc_video_encode_bulk;
 		video->payload_size = 0;
-	} else
+		video->is_bulk = true;
+	} else {
 		video->encode = video->queue.use_sg ?
 			uvc_video_encode_isoc_sg : uvc_video_encode_isoc;
+		video->is_bulk = false;
+	}
 
 	video->req_int_count = 0;
 
-	queue_work(video->async_wq, &video->pump);
-
+	uvcg_video_pump(video);
 	return ret;
 }
 
@@ -646,12 +675,6 @@ int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
 	INIT_LIST_HEAD(&video->ureqs);
 	INIT_LIST_HEAD(&video->req_free);
 	spin_lock_init(&video->req_lock);
-	INIT_WORK(&video->pump, uvcg_video_pump);
-
-	/* Allocate a work queue for asynchronous video pump handler. */
-	video->async_wq = alloc_workqueue("uvcgadget", WQ_UNBOUND | WQ_HIGHPRI, 0);
-	if (!video->async_wq)
-		return -EINVAL;
 
 	video->uvc = uvc;
 	video->fcc = V4L2_PIX_FMT_YUYV;
drivers/usb/gadget/function/uvc_video.h +2 -0
index 03adeefa343b..29c6b9a2e9c3 100644
@@ -18,4 +18,6 @@ int uvcg_video_enable(struct uvc_video *video, int enable);
 
 int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc);
 
+void uvcg_video_pump_qbuf(struct uvc_video *video);
+
 #endif /* __UVC_VIDEO_H__ */
Signature · 3 lines
Regards,

Laurent Pinchart
Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#6 lore raw
Quoted 21 lines · Michael Grzeschik
Quoted 15 lines · Laurent Pinchart
Quoted 9 lines · Jayant Chowdhary

This patch is based on top of
https://lore.kernel.org/linux-usb/[email protected]/T/#t:

When we use an async work queue to perform the function of pumping usb requests to the usb controller, it is possible that thread scheduling affects at what cadence we're able to pump requests. This could mean usb requests miss their uframes - resulting in video stream flickers on the host device.

Quoted 4 lines

In this patch, we move the pumping of usb requests to
1) uvcg_video_complete() complete handler for both isoc + bulk endpoints. We still send 0 length requests when there is no uvc buffer available to encode.

Quoted 4 lines

This means you will end up copying large amounts of data in interrupt context. The work queue was there to avoid exactly that, as it will introduce delays that can affect other parts of the system. I think this is a problem.

Quoted 7 lines

Regarding Thin's argument about possible scheduling latency that is already introducing real errors, this seemed like a good solution.

But sure, this potential latency introduced in the interrupt context can trigger other side effects.

However I think we need some compromise since both arguments are very valid.

Agreed.

Quoted 1 line

Any ideas, how to solve this?

I'm afraid not.

Quoted 121 lines
Quoted 106 lines
Quoted 52 lines

2) uvc_v4l2_qbuf - only for bulk endpoints since it is not legal to send 0 length requests.

---
v1->v2: Fix code style and add self Signed-off-by

 drivers/usb/gadget/function/f_uvc.c     |  4 --
 drivers/usb/gadget/function/uvc.h       |  4 +-
 drivers/usb/gadget/function/uvc_v4l2.c  |  5 +-
 drivers/usb/gadget/function/uvc_video.c | 71 ++++++++++++++++---------
 drivers/usb/gadget/function/uvc_video.h |  2 +
 5 files changed, 51 insertions(+), 35 deletions(-)
drivers/usb/gadget/function/f_uvc.c +0 -4
index ae08341961eb..53cb2539486d 100644
@@ -959,14 +959,10 @@ static void uvc_function_unbind(struct usb_configuration *c,
 {
 	struct usb_composite_dev *cdev = c->cdev;
 	struct uvc_device *uvc = to_uvc(f);
-	struct uvc_video *video = &uvc->video;
 	long wait_ret = 1;
 
 	uvcg_info(f, "%s()\n", __func__);
 
-	if (video->async_wq)
-		destroy_workqueue(video->async_wq);
-
 	/*
 	 * If we know we're connected via v4l2, then there should be a cleanup
 	 * of the device from userspace either via UVC_EVENT_DISCONNECT or
drivers/usb/gadget/function/uvc.h +0 -3
index be0d012aa244..498f344fda4b 100644
@@ -88,9 +88,6 @@ struct uvc_video {
 	struct uvc_device *uvc;
 	struct usb_ep *ep;
 
-	struct work_struct pump;
-	struct workqueue_struct *async_wq;
-
 	/* Frame parameters */
 	u8 bpp;
 	u32 fcc;

drivers/usb/gadget/function/uvc.h struct uvc_video { · line 116

Quoted 4 lines
drivers/usb/gadget/function/uvc.h +1 -0
@@ -116,6 +113,7 @@ struct uvc_video {
 	/* Context data used by the completion handler */
 	__u32 payload_size;
 	__u32 max_payload_size;
+	bool is_bulk;

This should be introduced in a separate patch.

Quoted 47 lines
drivers/usb/gadget/function/uvc.h +0 -0
 	struct uvc_video_queue queue;
 	unsigned int fid;
drivers/usb/gadget/function/uvc_v4l2.c +1 -4
index f4d2e24835d4..678ea6df7b5c 100644
@@ -414,10 +414,7 @@ uvc_v4l2_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
 	ret = uvcg_queue_buffer(&video->queue, b);
 	if (ret < 0)
 		return ret;
-
-	if (uvc->state == UVC_STATE_STREAMING)
-		queue_work(video->async_wq, &video->pump);
-
+	uvcg_video_pump_qbuf(video);
 	return ret;
 }
 
drivers/usb/gadget/function/uvc_video.c +5 -1
index ab3f02054e85..0fcd8e5edbac 100644
@@ -24,6 +24,8 @@
  * Video codecs
  */
 
+static void uvcg_video_pump(struct uvc_video *video);
+
 static int
 uvc_video_encode_header(struct uvc_video *video, struct uvc_buffer *buf,
 		u8 *data, int len)
@@ -329,7 +331,9 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
 	 */
 	if (video->is_enabled) {
 		list_add_tail(&req->list, &video->req_free);
-		queue_work(video->async_wq, &video->pump);
+		spin_unlock_irqrestore(&video->req_lock, flags);
+		uvcg_video_pump(video);
+		return;
 	} else {
 		uvc_video_free_request(ureq, ep);
 	}
@@ -409,20 +413,31 @@ uvc_video_alloc_requests(struct uvc_video *video)
  * Video streaming
  */
 
Quoted 14 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_alloc_requests(struct uvc_video *video) · line 426

Quoted 11 lines
drivers/usb/gadget/function/uvc_video.c +11 -0
+void uvcg_video_pump_qbuf(struct uvc_video *video)
+{
+	/*
+	 * Only call uvcg_video_pump() from qbuf, for bulk eps since
+	 * for isoc, the complete handler will call uvcg_video_pump()
+	 * consistently. Calling it for isoc eps, while correct
+	 * will increase contention for video->req_lock since the
+	 * complete handler will be called more often.
+	*/
+	if (video->is_bulk)
+		uvcg_video_pump(video);

Am I the only one to see the *major* race condition that this patch introduces ?

Quoted 1 line

Possible that you are. Please elaborate.

uvcg_video_pump() can now run multiple times in parallel on multiple CPUs. Look at the while() loop in the function, and consider what will happen when run on two CPUs concurrently. See below for an additional comment on this.

Quoted 36 lines
Quoted 36 lines
Quoted 36 lines
drivers/usb/gadget/function/uvc_video.c +7 -6
+}
+
 /*
  * uvcg_video_pump - Pump video data into the USB requests
  *
  * This function fills the available USB requests (listed in req_free) with
  * video data from the queued buffers.
  */
-static void uvcg_video_pump(struct work_struct *work)
+static void uvcg_video_pump(struct uvc_video *video)
 {
-	struct uvc_video *video = container_of(work, struct uvc_video, pump);
 	struct uvc_video_queue *queue = &video->queue;
-	/* video->max_payload_size is only set when using bulk transfer */
-	bool is_bulk = video->max_payload_size;
 	struct usb_request *req = NULL;
-	struct uvc_buffer *buf;
+	struct uvc_request *ureq = NULL;
+	struct uvc_buffer *buf = NULL, *last_buf = NULL;
 	unsigned long flags;
 	bool buf_done;
 	int ret;
@@ -455,7 +470,8 @@ static void uvcg_video_pump(struct work_struct *work)
 		if (buf != NULL) {
 			video->encode(req, video, buf);
 			buf_done = buf->state == UVC_BUF_STATE_DONE;
-		} else if (!(queue->flags & UVC_QUEUE_DISCONNECTED) && !is_bulk) {
+		} else if (!(queue->flags & UVC_QUEUE_DISCONNECTED) &&
+				!video->is_bulk) {
 			/*
 			 * No video buffer available; the queue is still connected and
 			 * we're transferring over ISOC. Queue a 0 length request to
@@ -500,18 +516,30 @@ static void uvcg_video_pump(struct work_struct *work)
 			req->no_interrupt = 1;
 		}
 
Quoted 4 lines
Quoted 4 lines

drivers/usb/gadget/function/uvc_video.c static void uvcg_video_pump(struct work_struct *work) · line 506

Quoted 4 lines
drivers/usb/gadget/function/uvc_video.c +0 -3
-		/* Queue the USB request */
-		ret = uvcg_video_ep_queue(video, req);
 		spin_unlock_irqrestore(&queue->irqlock, flags);
-

Here's one problematic point. The code above may have run on CPU A, which releases IRQ lock. CPU B may then run the same code to encode the next chunk of data in a request, and proceed to the code below before CPU A. The requests will then be queued in the wrong order.

In the next iteration of this patch, I would like to see a clear explanation in the commit message of why there is no race condition (after fixing the existing ones, of course). Writing it down forces going through the mental exercise of thinking about the race conditions, which should help catching them.

Quoted 85 lines
Quoted 85 lines
Quoted 8 lines
drivers/usb/gadget/function/uvc_video.c +8 -0
+		spin_lock_irqsave(&video->req_lock, flags);
+		if (video->is_enabled) {
+			/* Queue the USB request */
+			ret = uvcg_video_ep_queue(video, req);
+			/* Endpoint now owns the request */
+			req = NULL;
+			video->req_int_count++;
+		} else {

drivers/usb/gadget/function/uvc_video.c static void uvcg_video_pump(struct work_struct *work) · line 537

Quoted 10 lines
drivers/usb/gadget/function/uvc_video.c +9 -0
+			ret =  -ENODEV;
+			ureq = req->context;
+			last_buf = ureq->last_buf;
+			ureq->last_buf = NULL;
+		}
+		spin_unlock_irqrestore(&video->req_lock, flags);
 		if (ret < 0) {
+			if (last_buf != NULL) {
+				// Return the buffer to the queue in the case the
+				// request was not queued to the ep.

Wrong comment style.

Quoted 64 lines
drivers/usb/gadget/function/uvc_video.c +7 -14
+				uvcg_complete_buffer(&video->queue, last_buf);
+			}
 			uvcg_queue_cancel(queue, 0);
 			break;
 		}
-
-		/* Endpoint now owns the request */
-		req = NULL;
-		video->req_int_count++;
 	}
 
 	if (!req)
@@ -556,7 +584,6 @@ uvcg_video_disable(struct uvc_video *video)
 	}
 	spin_unlock_irqrestore(&video->req_lock, flags);
 
-	cancel_work_sync(&video->pump);
 	uvcg_queue_cancel(&video->queue, 0);
 
 	spin_lock_irqsave(&video->req_lock, flags);
@@ -626,14 +653,16 @@ int uvcg_video_enable(struct uvc_video *video, int enable)
 	if (video->max_payload_size) {
 		video->encode = uvc_video_encode_bulk;
 		video->payload_size = 0;
-	} else
+		video->is_bulk = true;
+	} else {
 		video->encode = video->queue.use_sg ?
 			uvc_video_encode_isoc_sg : uvc_video_encode_isoc;
+		video->is_bulk = false;
+	}
 
 	video->req_int_count = 0;
 
-	queue_work(video->async_wq, &video->pump);
-
+	uvcg_video_pump(video);
 	return ret;
 }
 
@@ -646,12 +675,6 @@ int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
 	INIT_LIST_HEAD(&video->ureqs);
 	INIT_LIST_HEAD(&video->req_free);
 	spin_lock_init(&video->req_lock);
-	INIT_WORK(&video->pump, uvcg_video_pump);
-
-	/* Allocate a work queue for asynchronous video pump handler. */
-	video->async_wq = alloc_workqueue("uvcgadget", WQ_UNBOUND | WQ_HIGHPRI, 0);
-	if (!video->async_wq)
-		return -EINVAL;
 
 	video->uvc = uvc;
 	video->fcc = V4L2_PIX_FMT_YUYV;
drivers/usb/gadget/function/uvc_video.h +2 -0
index 03adeefa343b..29c6b9a2e9c3 100644
@@ -18,4 +18,6 @@ int uvcg_video_enable(struct uvc_video *video, int enable);
 
 int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc);
 
+void uvcg_video_pump_qbuf(struct uvc_video *video);
+
 #endif /* __UVC_VIDEO_H__ */
Signature · 3 lines
Regards,

Laurent Pinchart
#7 lore raw
Quoted 28 lines · Laurent Pinchart
Quoted 27 lines · Michael Grzeschik
Quoted 15 lines · Laurent Pinchart
Quoted 9 lines · Jayant Chowdhary

This patch is based on top of
https://lore.kernel.org/linux-usb/[email protected]/T/#t:

When we use an async work queue to perform the function of pumping usb requests to the usb controller, it is possible that thread scheduling affects at what cadence we're able to pump requests. This could mean usb requests miss their uframes - resulting in video stream flickers on the host device.

Quoted 4 lines

In this patch, we move the pumping of usb requests to
1) uvcg_video_complete() complete handler for both isoc + bulk endpoints. We still send 0 length requests when there is no uvc buffer available to encode.

Quoted 4 lines

This means you will end up copying large amounts of data in interrupt context. The work queue was there to avoid exactly that, as it will introduce delays that can affect other parts of the system. I think this is a problem.

Regarding Thin's argument about possible scheduling latency that is already introducing real errors, this seemed like a good solution.

But sure, this potential latency introduced in the interrupt context can trigger other side effects.

Quoted 7 lines
Quoted 1 line

However I think we need some compromise since both arguments are very valid.

Agreed.

Quoted 1 line

Any ideas, how to solve this?

I'm afraid not.

We discussed this and came to the conclusion that we could make use of kthread_create and sched_setattr with an attr->sched_policy = SCHED_DEADLINE here instead of the workqueue. This way we would ensure that the worker would be triggered with hard definitions.

Since the SG case is not that heavy on the completion handler, we could also make this kthread conditionaly to the memcpy case.

Quoted 164 lines
Quoted 121 lines
Quoted 106 lines
Quoted 52 lines

2) uvc_v4l2_qbuf - only for bulk endpoints since it is not legal to send 0 length requests.

---
v1->v2: Fix code style and add self Signed-off-by

 drivers/usb/gadget/function/f_uvc.c     |  4 --
 drivers/usb/gadget/function/uvc.h       |  4 +-
 drivers/usb/gadget/function/uvc_v4l2.c  |  5 +-
 drivers/usb/gadget/function/uvc_video.c | 71 ++++++++++++++++---------
 drivers/usb/gadget/function/uvc_video.h |  2 +
 5 files changed, 51 insertions(+), 35 deletions(-)
drivers/usb/gadget/function/f_uvc.c +0 -4
index ae08341961eb..53cb2539486d 100644
@@ -959,14 +959,10 @@ static void uvc_function_unbind(struct usb_configuration *c,
 {
 	struct usb_composite_dev *cdev = c->cdev;
 	struct uvc_device *uvc = to_uvc(f);
-	struct uvc_video *video = &uvc->video;
 	long wait_ret = 1;
 
 	uvcg_info(f, "%s()\n", __func__);
 
-	if (video->async_wq)
-		destroy_workqueue(video->async_wq);
-
 	/*
 	 * If we know we're connected via v4l2, then there should be a cleanup
 	 * of the device from userspace either via UVC_EVENT_DISCONNECT or
drivers/usb/gadget/function/uvc.h +0 -3
index be0d012aa244..498f344fda4b 100644
@@ -88,9 +88,6 @@ struct uvc_video {
 	struct uvc_device *uvc;
 	struct usb_ep *ep;
 
-	struct work_struct pump;
-	struct workqueue_struct *async_wq;
-
 	/* Frame parameters */
 	u8 bpp;
 	u32 fcc;

drivers/usb/gadget/function/uvc.h struct uvc_video { · line 116

Quoted 4 lines
drivers/usb/gadget/function/uvc.h +1 -0
@@ -116,6 +113,7 @@ struct uvc_video {
 	/* Context data used by the completion handler */
 	__u32 payload_size;
 	__u32 max_payload_size;
+	bool is_bulk;

This should be introduced in a separate patch.

Quoted 47 lines
drivers/usb/gadget/function/uvc.h +0 -0
 	struct uvc_video_queue queue;
 	unsigned int fid;
drivers/usb/gadget/function/uvc_v4l2.c +1 -4
index f4d2e24835d4..678ea6df7b5c 100644
@@ -414,10 +414,7 @@ uvc_v4l2_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
 	ret = uvcg_queue_buffer(&video->queue, b);
 	if (ret < 0)
 		return ret;
-
-	if (uvc->state == UVC_STATE_STREAMING)
-		queue_work(video->async_wq, &video->pump);
-
+	uvcg_video_pump_qbuf(video);
 	return ret;
 }
 
drivers/usb/gadget/function/uvc_video.c +5 -1
index ab3f02054e85..0fcd8e5edbac 100644
@@ -24,6 +24,8 @@
  * Video codecs
  */
 
+static void uvcg_video_pump(struct uvc_video *video);
+
 static int
 uvc_video_encode_header(struct uvc_video *video, struct uvc_buffer *buf,
 		u8 *data, int len)
@@ -329,7 +331,9 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
 	 */
 	if (video->is_enabled) {
 		list_add_tail(&req->list, &video->req_free);
-		queue_work(video->async_wq, &video->pump);
+		spin_unlock_irqrestore(&video->req_lock, flags);
+		uvcg_video_pump(video);
+		return;
 	} else {
 		uvc_video_free_request(ureq, ep);
 	}
@@ -409,20 +413,31 @@ uvc_video_alloc_requests(struct uvc_video *video)
  * Video streaming
  */
 
Quoted 14 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_alloc_requests(struct uvc_video *video) · line 426

Quoted 11 lines
drivers/usb/gadget/function/uvc_video.c +11 -0
+void uvcg_video_pump_qbuf(struct uvc_video *video)
+{
+	/*
+	 * Only call uvcg_video_pump() from qbuf, for bulk eps since
+	 * for isoc, the complete handler will call uvcg_video_pump()
+	 * consistently. Calling it for isoc eps, while correct
+	 * will increase contention for video->req_lock since the
+	 * complete handler will be called more often.
+	*/
+	if (video->is_bulk)
+		uvcg_video_pump(video);

Am I the only one to see the *major* race condition that this patch introduces ?

Quoted 1 line

Possible that you are. Please elaborate.

uvcg_video_pump() can now run multiple times in parallel on multiple CPUs. Look at the while() loop in the function, and consider what will happen when run on two CPUs concurrently. See below for an additional comment on this.

Quoted 36 lines
Quoted 36 lines
Quoted 36 lines
drivers/usb/gadget/function/uvc_video.c +7 -6
+}
+
 /*
  * uvcg_video_pump - Pump video data into the USB requests
  *
  * This function fills the available USB requests (listed in req_free) with
  * video data from the queued buffers.
  */
-static void uvcg_video_pump(struct work_struct *work)
+static void uvcg_video_pump(struct uvc_video *video)
 {
-	struct uvc_video *video = container_of(work, struct uvc_video, pump);
 	struct uvc_video_queue *queue = &video->queue;
-	/* video->max_payload_size is only set when using bulk transfer */
-	bool is_bulk = video->max_payload_size;
 	struct usb_request *req = NULL;
-	struct uvc_buffer *buf;
+	struct uvc_request *ureq = NULL;
+	struct uvc_buffer *buf = NULL, *last_buf = NULL;
 	unsigned long flags;
 	bool buf_done;
 	int ret;
@@ -455,7 +470,8 @@ static void uvcg_video_pump(struct work_struct *work)
 		if (buf != NULL) {
 			video->encode(req, video, buf);
 			buf_done = buf->state == UVC_BUF_STATE_DONE;
-		} else if (!(queue->flags & UVC_QUEUE_DISCONNECTED) && !is_bulk) {
+		} else if (!(queue->flags & UVC_QUEUE_DISCONNECTED) &&
+				!video->is_bulk) {
 			/*
 			 * No video buffer available; the queue is still connected and
 			 * we're transferring over ISOC. Queue a 0 length request to
@@ -500,18 +516,30 @@ static void uvcg_video_pump(struct work_struct *work)
 			req->no_interrupt = 1;
 		}
 
Quoted 9 lines
Quoted 4 lines
Quoted 4 lines

drivers/usb/gadget/function/uvc_video.c static void uvcg_video_pump(struct work_struct *work) · line 506

Quoted 4 lines
drivers/usb/gadget/function/uvc_video.c +0 -3
-		/* Queue the USB request */
-		ret = uvcg_video_ep_queue(video, req);
 		spin_unlock_irqrestore(&queue->irqlock, flags);
-

Here's one problematic point. The code above may have run on CPU A, which releases IRQ lock. CPU B may then run the same code to encode the next chunk of data in a request, and proceed to the code below before CPU A. The requests will then be queued in the wrong order.

Right

Quoted 5 lines

In the next iteration of this patch, I would like to see a clear explanation in the commit message of why there is no race condition (after fixing the existing ones, of course). Writing it down forces going through the mental exercise of thinking about the race conditions, which should help catching them.

Agreed.

Quoted 91 lines
Quoted 85 lines
Quoted 85 lines
Quoted 8 lines
drivers/usb/gadget/function/uvc_video.c +8 -0
+		spin_lock_irqsave(&video->req_lock, flags);
+		if (video->is_enabled) {
+			/* Queue the USB request */
+			ret = uvcg_video_ep_queue(video, req);
+			/* Endpoint now owns the request */
+			req = NULL;
+			video->req_int_count++;
+		} else {

drivers/usb/gadget/function/uvc_video.c static void uvcg_video_pump(struct work_struct *work) · line 537

Quoted 10 lines
drivers/usb/gadget/function/uvc_video.c +9 -0
+			ret =  -ENODEV;
+			ureq = req->context;
+			last_buf = ureq->last_buf;
+			ureq->last_buf = NULL;
+		}
+		spin_unlock_irqrestore(&video->req_lock, flags);
 		if (ret < 0) {
+			if (last_buf != NULL) {
+				// Return the buffer to the queue in the case the
+				// request was not queued to the ep.

Wrong comment style.

Quoted 64 lines
drivers/usb/gadget/function/uvc_video.c +7 -14
+				uvcg_complete_buffer(&video->queue, last_buf);
+			}
 			uvcg_queue_cancel(queue, 0);
 			break;
 		}
-
-		/* Endpoint now owns the request */
-		req = NULL;
-		video->req_int_count++;
 	}
 
 	if (!req)
@@ -556,7 +584,6 @@ uvcg_video_disable(struct uvc_video *video)
 	}
 	spin_unlock_irqrestore(&video->req_lock, flags);
 
-	cancel_work_sync(&video->pump);
 	uvcg_queue_cancel(&video->queue, 0);
 
 	spin_lock_irqsave(&video->req_lock, flags);
@@ -626,14 +653,16 @@ int uvcg_video_enable(struct uvc_video *video, int enable)
 	if (video->max_payload_size) {
 		video->encode = uvc_video_encode_bulk;
 		video->payload_size = 0;
-	} else
+		video->is_bulk = true;
+	} else {
 		video->encode = video->queue.use_sg ?
 			uvc_video_encode_isoc_sg : uvc_video_encode_isoc;
+		video->is_bulk = false;
+	}
 
 	video->req_int_count = 0;
 
-	queue_work(video->async_wq, &video->pump);
-
+	uvcg_video_pump(video);
 	return ret;
 }
 
@@ -646,12 +675,6 @@ int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
 	INIT_LIST_HEAD(&video->ureqs);
 	INIT_LIST_HEAD(&video->req_free);
 	spin_lock_init(&video->req_lock);
-	INIT_WORK(&video->pump, uvcg_video_pump);
-
-	/* Allocate a work queue for asynchronous video pump handler. */
-	video->async_wq = alloc_workqueue("uvcgadget", WQ_UNBOUND | WQ_HIGHPRI, 0);
-	if (!video->async_wq)
-		return -EINVAL;
 
 	video->uvc = uvc;
 	video->fcc = V4L2_PIX_FMT_YUYV;
drivers/usb/gadget/function/uvc_video.h +2 -0
index 03adeefa343b..29c6b9a2e9c3 100644
@@ -18,4 +18,6 @@ int uvcg_video_enable(struct uvc_video *video, int enable);
 
 int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc);
 
+void uvcg_video_pump_qbuf(struct uvc_video *video);
+
 #endif /* __UVC_VIDEO_H__ */
Signature · 3 lines
Regards,

Laurent Pinchart
Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#8 lore raw
Quoted 37 lines · Michael Grzeschik
Quoted 28 lines · Laurent Pinchart
Quoted 27 lines · Michael Grzeschik
Quoted 15 lines · Laurent Pinchart
Quoted 9 lines · Jayant Chowdhary

This patch is based on top of
https://lore.kernel.org/linux-usb/[email protected]/T/#t:

When we use an async work queue to perform the function of pumping usb requests to the usb controller, it is possible that thread scheduling affects at what cadence we're able to pump requests. This could mean usb requests miss their uframes - resulting in video stream flickers on the host device.

Quoted 4 lines

In this patch, we move the pumping of usb requests to
1) uvcg_video_complete() complete handler for both isoc + bulk endpoints. We still send 0 length requests when there is no uvc buffer available to encode.

Quoted 4 lines

This means you will end up copying large amounts of data in interrupt context. The work queue was there to avoid exactly that, as it will introduce delays that can affect other parts of the system. I think this is a problem.

Regarding Thin's argument about possible scheduling latency that is already introducing real errors, this seemed like a good solution.

But sure, this potential latency introduced in the interrupt context can trigger other side effects.

Quoted 7 lines
Quoted 1 line

However I think we need some compromise since both arguments are very valid.

Agreed.

Quoted 1 line

Any ideas, how to solve this?

I'm afraid not.

Quoted 7 lines

We discussed this and came to the conclusion that we could make use of kthread_create and sched_setattr with an attr->sched_policy = SCHED_DEADLINE here instead of the workqueue. This way we would ensure that the worker would be triggered with hard definitions.

Since the SG case is not that heavy on the completion handler, we could also make this kthread conditionaly to the memcpy case.

If you don't mind a naive suggestion from someone who knows nothing about the driver...

An attractive possibility is to have the work queue (or kthread) do the time-consuming copying, but leave the submission up to the completion handler. If the data isn't ready (or there's no data to send) when the handler runs, then queue a 0-length request.

That will give you the best of both worlds: low latency while in interrupt context and a steady, constant flow of USB transfers at all times. The question of how to schedule the work queue or kthread is a separate matter, not directly relevant to this design decision.

Alan Stern

#9 lore raw
Quoted 54 lines · Alan Stern
Quoted 37 lines · Michael Grzeschik
Quoted 28 lines · Laurent Pinchart
Quoted 27 lines · Michael Grzeschik
Quoted 15 lines · Laurent Pinchart
Quoted 9 lines · Jayant Chowdhary

This patch is based on top of
https://lore.kernel.org/linux-usb/[email protected]/T/#t:

When we use an async work queue to perform the function of pumping usb requests to the usb controller, it is possible that thread scheduling affects at what cadence we're able to pump requests. This could mean usb requests miss their uframes - resulting in video stream flickers on the host device.

Quoted 4 lines

In this patch, we move the pumping of usb requests to
1) uvcg_video_complete() complete handler for both isoc + bulk endpoints. We still send 0 length requests when there is no uvc buffer available to encode.

Quoted 4 lines

This means you will end up copying large amounts of data in interrupt context. The work queue was there to avoid exactly that, as it will introduce delays that can affect other parts of the system. I think this is a problem.

Regarding Thin's argument about possible scheduling latency that is already introducing real errors, this seemed like a good solution.

But sure, this potential latency introduced in the interrupt context can trigger other side effects.

Quoted 7 lines
Quoted 1 line

However I think we need some compromise since both arguments are very valid.

Agreed.

Quoted 1 line

Any ideas, how to solve this?

I'm afraid not.

Quoted 7 lines

We discussed this and came to the conclusion that we could make use of kthread_create and sched_setattr with an attr->sched_policy = SCHED_DEADLINE here instead of the workqueue. This way we would ensure that the worker would be triggered with hard definitions.

Since the SG case is not that heavy on the completion handler, we could also make this kthread conditionaly to the memcpy case.

If you don't mind a naive suggestion from someone who knows nothing about the driver...

An attractive possibility is to have the work queue (or kthread) do the time-consuming copying, but leave the submission up to the completion handler. If the data isn't ready (or there's no data to send) when the handler runs, then queue a 0-length request.

Quoted 4 lines

That will give you the best of both worlds: low latency while in interrupt context and a steady, constant flow of USB transfers at all times. The question of how to schedule the work queue or kthread is a separate matter, not directly relevant to this design decision.

That's it. This is probably the best way to tackle the overall problem.

So we leave the call of the encode callback to the worker, that will probably still can be a workqueue. The complete callback is calling the explicit uvcg_video_ep_queue when prepared requests are available and if there is nothing pending it will just enqueue zero requests.

Thank you Alan, this makes so much sense!

Jayant, Laurent: Do you agree?
If yes, Jayant will you change the patch accordingly?

Michael

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#10 lore raw

Hi,

Quoted 67 lines · Michael Grzeschik
Quoted 54 lines · Alan Stern
Quoted 37 lines · Michael Grzeschik
Quoted 28 lines · Laurent Pinchart
Quoted 27 lines · Michael Grzeschik
Quoted 15 lines · Laurent Pinchart
Quoted 9 lines · Jayant Chowdhary

This patch is based on top of
https://lore.kernel.org/linux-usb/[email protected]/T/#t:

When we use an async work queue to perform the function of pumping usb requests to the usb controller, it is possible that thread scheduling affects at what cadence we're able to pump requests. This could mean usb requests miss their uframes - resulting in video stream flickers on the host device.

Quoted 4 lines

In this patch, we move the pumping of usb requests to
1) uvcg_video_complete() complete handler for both isoc + bulk endpoints. We still send 0 length requests when there is no uvc buffer available to encode.

Quoted 4 lines

This means you will end up copying large amounts of data in interrupt context. The work queue was there to avoid exactly that, as it will introduce delays that can affect other parts of the system. I think this is a problem.

Regarding Thin's argument about possible scheduling latency that is already introducing real errors, this seemed like a good solution.

But sure, this potential latency introduced in the interrupt context can trigger other side effects.

Quoted 7 lines
Quoted 1 line

However I think we need some compromise since both arguments are very valid.

Agreed.

Quoted 1 line

Any ideas, how to solve this?

I'm afraid not.

Quoted 7 lines

We discussed this and came to the conclusion that we could make use of kthread_create and sched_setattr with an attr->sched_policy = SCHED_DEADLINE here instead of the workqueue. This way we would ensure that the worker would be triggered with hard definitions.

Since the SG case is not that heavy on the completion handler, we could also make this kthread conditionaly to the memcpy case.

If you don't mind a naive suggestion from someone who knows nothing about the driver...

An attractive possibility is to have the work queue (or kthread) do the time-consuming copying, but leave the submission up to the completion handler.  If the data isn't ready (or there's no data to send) when the handler runs, then queue a 0-length request.

Quoted 4 lines

That will give you the best of both worlds: low latency while in interrupt context and a steady, constant flow of USB transfers at all times.  The question of how to schedule the work queue or kthread is a separate matter, not directly relevant to this design decision.

That's it. This is probably the best way to tackle the overall problem.

So we leave the call of the encode callback to the worker, that will probably still can be a workqueue. The complete callback is calling the explicit uvcg_video_ep_queue when prepared requests are available and if there is nothing pending it will just enqueue zero requests.

Quoted 6 lines

Thank you Alan, this makes so much sense!

Jayant, Laurent: Do you agree?
If yes, Jayant will you change the patch accordingly?

Thanks for all the discussion Greg, Michael, Laurent and Alan. Apologies for not responding earlier since I am OOO.

While I  haven't tried this out this does seem like a very good idea. Thank you Alan! I will aim to make changes and post a patch on Monday night PST.

Jayant

#11 lore raw

Hi,

Quoted 69 lines · Jayant Chowdhary

Hi,

Quoted 60 lines · Michael Grzeschik
Quoted 48 lines · Alan Stern
Quoted 31 lines · Michael Grzeschik
Quoted 30 lines · Laurent Pinchart
Quoted 19 lines · Michael Grzeschik
Quoted 10 lines · Laurent Pinchart
Quoted 9 lines · Jayant Chowdhary

This patch is based on top of
https://lore.kernel.org/linux-usb/[email protected]/T/#t:

When we use an async work queue to perform the function of pumping usb requests to the usb controller, it is possible that thread scheduling affects at what cadence we're able to pump requests. This could mean usb requests miss their uframes - resulting in video stream flickers on the host device.

Quoted 8 lines
Quoted 4 lines

In this patch, we move the pumping of usb requests to
1) uvcg_video_complete() complete handler for both isoc + bulk endpoints. We still send 0 length requests when there is no uvc buffer available to encode.

This means you will end up copying large amounts of data in interrupt context. The work queue was there to avoid exactly that, as it will introduce delays that can affect other parts of the system. I think this is a problem.

Quoted 7 lines

Regarding Thin's argument about possible scheduling latency that is already introducing real errors, this seemed like a good solution.

But sure, this potential latency introduced in the interrupt context can trigger other side effects.

However I think we need some compromise since both arguments are very valid.

Agreed.

Quoted 1 line

Any ideas, how to solve this?

Quoted 8 lines
Quoted 1 line

I'm afraid not.

We discussed this and came to the conclusion that we could make use of kthread_create and sched_setattr with an attr->sched_policy = SCHED_DEADLINE here instead of the workqueue. This way we would ensure that the worker would be triggered with hard definitions.

Since the SG case is not that heavy on the completion handler, we could also make this kthread conditionaly to the memcpy case.

If you don't mind a naive suggestion from someone who knows nothing about the driver...

An attractive possibility is to have the work queue (or kthread) do the time-consuming copying, but leave the submission up to the completion handler.  If the data isn't ready (or there's no data to send) when the handler runs, then queue a 0-length request.

Quoted 4 lines

That will give you the best of both worlds: low latency while in interrupt context and a steady, constant flow of USB transfers at all times.  The question of how to schedule the work queue or kthread is a separate matter, not directly relevant to this design decision.

That's it. This is probably the best way to tackle the overall problem.

So we leave the call of the encode callback to the worker, that will probably still can be a workqueue. The complete callback is calling the explicit uvcg_video_ep_queue when prepared requests are available and if there is nothing pending it will just enqueue zero requests.

Quoted 6 lines

Thank you Alan, this makes so much sense!

Jayant, Laurent: Do you agree?
If yes, Jayant will you change the patch accordingly?

Quoted 5 lines

Thanks for all the discussion Greg, Michael, Laurent and Alan. Apologies for not responding earlier since I am OOO.

While I  haven't tried this out this does seem like a very good idea. Thank you Alan! I will aim to make changes and post a patch on Monday night PST.

I got caught up with some work which is taking longer than expected. Apologies for the delay :) I'm testing some things out right now. I hope to be able to post a patch in the next couple of days. Thanks for your patience.

Jayant

#12 lore raw

Hi,

Quoted 72 lines · Jayant Chowdhary

Hi,

Quoted 69 lines · Jayant Chowdhary

Hi,

Quoted 66 lines · Michael Grzeschik
Quoted 48 lines · Alan Stern
Quoted 31 lines · Michael Grzeschik
Quoted 30 lines · Laurent Pinchart
Quoted 19 lines · Michael Grzeschik
Quoted 10 lines · Laurent Pinchart
Quoted 9 lines · Jayant Chowdhary

This patch is based on top of
https://lore.kernel.org/linux-usb/[email protected]/T/#t:

When we use an async work queue to perform the function of pumping usb requests to the usb controller, it is possible that thread scheduling affects at what cadence we're able to pump requests. This could mean usb requests miss their uframes - resulting in video stream flickers on the host device.

Quoted 8 lines
Quoted 4 lines

In this patch, we move the pumping of usb requests to
1) uvcg_video_complete() complete handler for both isoc + bulk endpoints. We still send 0 length requests when there is no uvc buffer available to encode.

This means you will end up copying large amounts of data in interrupt context. The work queue was there to avoid exactly that, as it will introduce delays that can affect other parts of the system. I think this is a problem.

Quoted 7 lines

Regarding Thin's argument about possible scheduling latency that is already introducing real errors, this seemed like a good solution.

But sure, this potential latency introduced in the interrupt context can trigger other side effects.

However I think we need some compromise since both arguments are very valid.

Agreed.

Quoted 1 line

Any ideas, how to solve this?

Quoted 8 lines
Quoted 1 line

I'm afraid not.

We discussed this and came to the conclusion that we could make use of kthread_create and sched_setattr with an attr->sched_policy = SCHED_DEADLINE here instead of the workqueue. This way we would ensure that the worker would be triggered with hard definitions.

Since the SG case is not that heavy on the completion handler, we could also make this kthread conditionaly to the memcpy case.

If you don't mind a naive suggestion from someone who knows nothing about the driver...

An attractive possibility is to have the work queue (or kthread) do the time-consuming copying, but leave the submission up to the completion handler.  If the data isn't ready (or there's no data to send) when the handler runs, then queue a 0-length request.

Quoted 4 lines

That will give you the best of both worlds: low latency while in interrupt context and a steady, constant flow of USB transfers at all times.  The question of how to schedule the work queue or kthread is a separate matter, not directly relevant to this design decision.

That's it. This is probably the best way to tackle the overall problem.

So we leave the call of the encode callback to the worker, that will probably still can be a workqueue. The complete callback is calling the explicit uvcg_video_ep_queue when prepared requests are available and if there is nothing pending it will just enqueue zero requests.

Thank you Alan, this makes so much sense!

Jayant, Laurent: Do you agree?
If yes, Jayant will you change the patch accordingly?

Quoted 8 lines
Quoted 5 lines

Thanks for all the discussion Greg, Michael, Laurent and Alan. Apologies for not responding earlier since I am OOO.

While I  haven't tried this out this does seem like a very good idea. Thank you Alan! I will aim to make changes and post a patch on Monday night PST.

I got caught up with some work which is taking longer than expected. Apologies for the delay :) I'm testing some things out right now. I hope to be able to post a patch in the next couple of days. Thanks for your patience.

I posted another patch at https://lore.kernel.org/linux-usb/[email protected]/T/#u.
I've not split this into 2 patches since here, we have a common function that handles both the bulk and isoc cases and I feel they're logically related.

Thank you

Jayant

maintainer
#13 lore raw
In reply to Jayant Chowdhary
Quoted 9 lines · Jayant Chowdhary

This patch is based on top of
https://lore.kernel.org/linux-usb/[email protected]/T/#t:

When we use an async work queue to perform the function of pumping usb requests to the usb controller, it is possible that thread scheduling affects at what cadence we're able to pump requests. This could mean usb requests miss their uframes - resulting in video stream flickers on the host device.

Quoted 6 lines

In this patch, we move the pumping of usb requests to
1) uvcg_video_complete() complete handler for both isoc + bulk endpoints. We still send 0 length requests when there is no uvc buffer available to encode.
2) uvc_v4l2_qbuf - only for bulk endpoints since it is not legal to send 0 length requests.

Usually when you have to enumerate things in a patch, that implies it needs to be broken up into multiple changes. Why isn't that necessary here?

Quoted 21 lines

---
v1->v2: Fix code style and add self Signed-off-by

 drivers/usb/gadget/function/f_uvc.c     |  4 --
 drivers/usb/gadget/function/uvc.h       |  4 +-
 drivers/usb/gadget/function/uvc_v4l2.c  |  5 +-
 drivers/usb/gadget/function/uvc_video.c | 71 ++++++++++++++++---------
 drivers/usb/gadget/function/uvc_video.h |  2 +
 5 files changed, 51 insertions(+), 35 deletions(-)
drivers/usb/gadget/function/f_uvc.c +0 -0
index ae08341961eb..53cb2539486d 100644

drivers/usb/gadget/function/f_uvc.c static void uvc_function_unbind(struct usb_configuration *c, · line 964

Quoted 7 lines
drivers/usb/gadget/function/f_uvc.c +0 -1
@@ -959,14 +959,10 @@ static void uvc_function_unbind(struct usb_configuration *c,
 {
 	struct usb_composite_dev *cdev = c->cdev;
 	struct uvc_device *uvc = to_uvc(f);
-	struct uvc_video *video = &uvc->video;
 	long wait_ret = 1;
 
 	uvcg_info(f, "%s()\n", __func__);

meta-comment, lines like this need to be deleted in the future. Not relevent here, but for future work if you want to add such a cleanup to a patch series, I'd be grateful.

Quoted 22 lines
drivers/usb/gadget/function/f_uvc.c +0 -3
-	if (video->async_wq)
-		destroy_workqueue(video->async_wq);
-
 	/*
 	 * If we know we're connected via v4l2, then there should be a cleanup
 	 * of the device from userspace either via UVC_EVENT_DISCONNECT or
drivers/usb/gadget/function/uvc.h +0 -3
index be0d012aa244..498f344fda4b 100644
@@ -88,9 +88,6 @@ struct uvc_video {
 	struct uvc_device *uvc;
 	struct usb_ep *ep;
 
-	struct work_struct pump;
-	struct workqueue_struct *async_wq;
-
 	/* Frame parameters */
 	u8 bpp;
 	u32 fcc;

drivers/usb/gadget/function/uvc.h struct uvc_video { · line 116

Quoted 4 lines
drivers/usb/gadget/function/uvc.h +1 -0
@@ -116,6 +113,7 @@ struct uvc_video {
 	/* Context data used by the completion handler */
 	__u32 payload_size;
 	__u32 max_payload_size;
+	bool is_bulk;

As Laurent said, this should be a separate patch.

Quoted 81 lines
drivers/usb/gadget/function/uvc.h +0 -0
 	struct uvc_video_queue queue;
 	unsigned int fid;
drivers/usb/gadget/function/uvc_v4l2.c +1 -4
index f4d2e24835d4..678ea6df7b5c 100644
@@ -414,10 +414,7 @@ uvc_v4l2_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
 	ret = uvcg_queue_buffer(&video->queue, b);
 	if (ret < 0)
 		return ret;
-
-	if (uvc->state == UVC_STATE_STREAMING)
-		queue_work(video->async_wq, &video->pump);
-
+	uvcg_video_pump_qbuf(video);
 	return ret;
 }
 
drivers/usb/gadget/function/uvc_video.c +21 -6
index ab3f02054e85..0fcd8e5edbac 100644
@@ -24,6 +24,8 @@
  * Video codecs
  */
 
+static void uvcg_video_pump(struct uvc_video *video);
+
 static int
 uvc_video_encode_header(struct uvc_video *video, struct uvc_buffer *buf,
 		u8 *data, int len)
@@ -329,7 +331,9 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
 	 */
 	if (video->is_enabled) {
 		list_add_tail(&req->list, &video->req_free);
-		queue_work(video->async_wq, &video->pump);
+		spin_unlock_irqrestore(&video->req_lock, flags);
+		uvcg_video_pump(video);
+		return;
 	} else {
 		uvc_video_free_request(ureq, ep);
 	}
@@ -409,20 +413,31 @@ uvc_video_alloc_requests(struct uvc_video *video)
  * Video streaming
  */
 
+void uvcg_video_pump_qbuf(struct uvc_video *video)
+{
+	/*
+	 * Only call uvcg_video_pump() from qbuf, for bulk eps since
+	 * for isoc, the complete handler will call uvcg_video_pump()
+	 * consistently. Calling it for isoc eps, while correct
+	 * will increase contention for video->req_lock since the
+	 * complete handler will be called more often.
+	*/
+	if (video->is_bulk)
+		uvcg_video_pump(video);
+}
+
 /*
  * uvcg_video_pump - Pump video data into the USB requests
  *
  * This function fills the available USB requests (listed in req_free) with
  * video data from the queued buffers.
  */
-static void uvcg_video_pump(struct work_struct *work)
+static void uvcg_video_pump(struct uvc_video *video)
 {
-	struct uvc_video *video = container_of(work, struct uvc_video, pump);
 	struct uvc_video_queue *queue = &video->queue;
-	/* video->max_payload_size is only set when using bulk transfer */
-	bool is_bulk = video->max_payload_size;
 	struct usb_request *req = NULL;
-	struct uvc_buffer *buf;
+	struct uvc_request *ureq = NULL;
+	struct uvc_buffer *buf = NULL, *last_buf = NULL;
 	unsigned long flags;
 	bool buf_done;
 	int ret;

drivers/usb/gadget/function/uvc_video.c static void uvcg_video_pump(struct work_struct *work) · line 474

Quoted 6 lines
drivers/usb/gadget/function/uvc_video.c +2 -1
@@ -455,7 +470,8 @@ static void uvcg_video_pump(struct work_struct *work)
 		if (buf != NULL) {
 			video->encode(req, video, buf);
 			buf_done = buf->state == UVC_BUF_STATE_DONE;
-		} else if (!(queue->flags & UVC_QUEUE_DISCONNECTED) && !is_bulk) {
+		} else if (!(queue->flags & UVC_QUEUE_DISCONNECTED) &&
+				!video->is_bulk) {

No need to wrap the line.

Quoted 50 lines
drivers/usb/gadget/function/uvc_video.c +19 -8
 			/*
 			 * No video buffer available; the queue is still connected and
 			 * we're transferring over ISOC. Queue a 0 length request to
@@ -500,18 +516,30 @@ static void uvcg_video_pump(struct work_struct *work)
 			req->no_interrupt = 1;
 		}
 
-		/* Queue the USB request */
-		ret = uvcg_video_ep_queue(video, req);
 		spin_unlock_irqrestore(&queue->irqlock, flags);
-
+		spin_lock_irqsave(&video->req_lock, flags);
+		if (video->is_enabled) {
+			/* Queue the USB request */
+			ret = uvcg_video_ep_queue(video, req);
+			/* Endpoint now owns the request */
+			req = NULL;
+			video->req_int_count++;
+		} else {
+			ret =  -ENODEV;
+			ureq = req->context;
+			last_buf = ureq->last_buf;
+			ureq->last_buf = NULL;
+		}
+		spin_unlock_irqrestore(&video->req_lock, flags);
 		if (ret < 0) {
+			if (last_buf != NULL) {
+				// Return the buffer to the queue in the case the
+				// request was not queued to the ep.
+				uvcg_complete_buffer(&video->queue, last_buf);
+			}
 			uvcg_queue_cancel(queue, 0);
 			break;
 		}
-
-		/* Endpoint now owns the request */
-		req = NULL;
-		video->req_int_count++;
 	}
 
 	if (!req)
@@ -556,7 +584,6 @@ uvcg_video_disable(struct uvc_video *video)
 	}
 	spin_unlock_irqrestore(&video->req_lock, flags);
 
-	cancel_work_sync(&video->pump);
 	uvcg_queue_cancel(&video->queue, 0);
 
 	spin_lock_irqsave(&video->req_lock, flags);

drivers/usb/gadget/function/uvc_video.c int uvcg_video_enable(struct uvc_video *video, int enable) · line 660

Quoted 9 lines
drivers/usb/gadget/function/uvc_video.c +3 -1
@@ -626,14 +653,16 @@ int uvcg_video_enable(struct uvc_video *video, int enable)
 	if (video->max_payload_size) {
 		video->encode = uvc_video_encode_bulk;
 		video->payload_size = 0;
-	} else
+		video->is_bulk = true;
+	} else {
 		video->encode = video->queue.use_sg ?
 			uvc_video_encode_isoc_sg : uvc_video_encode_isoc;
+		video->is_bulk = false;

Isn't this already set to 0?

And wait, this is still the bulk endpoint, right? Or am I missing something?

thanks,

greg k-h

#14 lore raw

PATCH v3 1/1 usb:gadget:uvc Do not use worker thread to queue isoc usb requests

In reply to Jayant Chowdhary

When we use an async work queue to perform the function of pumping usb requests to the usb controller, it is possible that amongst other factors, thread scheduling affects at what cadence we're able to pump requests. This could mean isoc usb requests miss their uframes - resulting in video stream flickers on the host device.

To avoid this, we make the async_wq thread only produce isoc usb_requests with uvc buffers encoded into them. The process of queueing to the endpoint is done by the uvc_video_complete() handler. In case no usb_requests are ready with encoded information, we just queue a zero length request to the endpoint from the complete handler.

For bulk endpoints the async_wq thread still queues usb requests to the endpoint.

---
Based on top of
https://lore.kernel.org/linux-usb/[email protected]/T/#t:
v1->v2: Added self Signed-Off-by and addressed review comments v2->v3: Encode to usb requests in async_wq; queue to ep in complete handler
for isoc transfers.

 drivers/usb/gadget/function/uvc.h       |   8 +
 drivers/usb/gadget/function/uvc_video.c | 187 +++++++++++++++++++-----
 2 files changed, 156 insertions(+), 39 deletions(-)
drivers/usb/gadget/function/uvc.h +8 -0
index e8d4c87f1e09..82c783410554 100644
@@ -105,7 +105,15 @@ struct uvc_video {
 	bool is_enabled; /* tracks whether video stream is enabled */
 	unsigned int req_size;
 	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
+
+	/* USB requests video pump thread can encode into*/
 	struct list_head req_free;
+
+	/*
+	 * USB requests video pump thread has already encoded into. These are
+	 * ready to be queued to the endpoint.
+	 */
+	struct list_head req_ready;
 	spinlock_t req_lock;
 
 	unsigned int req_int_count;
drivers/usb/gadget/function/uvc_video.c +148 -39

269 lines not on this page · raw message

Signature · 1 line
2.42.0.869.gea05f2083d-goog
#15 lore raw

Re: [PATCH v3] usb:gadget:uvc Do not use worker thread to queue isoc usb requests

Hi Jayant - thanks for the patch

Quoted 35 lines · Jayant Chowdhary

When we use an async work queue to perform the function of pumping usb requests to the usb controller, it is possible that amongst other factors, thread scheduling affects at what cadence we're able to pump requests. This could mean isoc usb requests miss their uframes - resulting in video stream flickers on the host device.

To avoid this, we make the async_wq thread only produce isoc usb_requests with uvc buffers encoded into them. The process of queueing to the endpoint is done by the uvc_video_complete() handler. In case no usb_requests are ready with encoded information, we just queue a zero length request to the endpoint from the complete handler.

For bulk endpoints the async_wq thread still queues usb requests to the endpoint.

---
Based on top of
https://lore.kernel.org/linux-usb/[email protected]/T/#t:
v1->v2: Added self Signed-Off-by and addressed review comments v2->v3: Encode to usb requests in async_wq; queue to ep in complete handler
for isoc transfers.

  drivers/usb/gadget/function/uvc.h       |   8 +
  drivers/usb/gadget/function/uvc_video.c | 187 +++++++++++++++++++-----
  2 files changed, 156 insertions(+), 39 deletions(-)
drivers/usb/gadget/function/uvc.h +0 -0
index e8d4c87f1e09..82c783410554 100644

drivers/usb/gadget/function/uvc.h struct uvc_video { · line 109

Quoted 5 lines
drivers/usb/gadget/function/uvc.h +2 -0
@@ -105,7 +105,15 @@ struct uvc_video {
  	bool is_enabled; /* tracks whether video stream is enabled */
  	unsigned int req_size;
  	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
+
+	/* USB requests video pump thread can encode into*/

"USB requests that the video pump thread can encode into", and a space before the closing */ please (and the same a few more times below).

Quoted 15 lines
drivers/usb/gadget/function/uvc.h +6 -0
  	struct list_head req_free;
+
+	/*
+	 * USB requests video pump thread has already encoded into. These are
+	 * ready to be queued to the endpoint.
+	 */
+	struct list_head req_ready;
  	spinlock_t req_lock;
 
  	unsigned int req_int_count;
drivers/usb/gadget/function/uvc_video.c +0 -0
index 53feb790a4c3..c84183e9afcc 100644

drivers/usb/gadget/function/uvc_video.c static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req) · line 273

Quoted 6 lines
drivers/usb/gadget/function/uvc_video.c +3 -0
@@ -268,6 +268,98 @@ static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req)
  	return ret;
  }
 
+/* This function must be called with video->req_lock held*/
+static int uvcg_video_usb_req_queue(struct uvc_video *video,
+	struct usb_request *req, bool queue_to_ep) {

Brace on a new line please - same a few more times below

drivers/usb/gadget/function/uvc_video.c static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req) · line 274

Quoted 1 line
drivers/usb/gadget/function/uvc_video.c +1 -0
+	bool is_bulk = video->max_payload_size;

empty line here

Quoted 41 lines
drivers/usb/gadget/function/uvc_video.c +41 -0
+	if (!video->is_enabled) {
+		uvc_video_free_request(req->context, video->ep);
+		return -ENODEV;
+	}
+	if (queue_to_ep) {
+		struct uvc_request *ureq = req->context;
+		/*
+		 * With USB3 handling more requests at a higher speed, we can't
+		 * afford to generate an interrupt for every request. Decide to
+		 * interrupt:
+		 *
+		 * - When no more requests are available in the free queue, as
+		 *   this may be our last chance to refill the endpoint's
+		 *   request queue.
+		 *
+		 * - When this is request is the last request for the video
+		 *   buffer, as we want to start sending the next video buffer
+		 *   ASAP in case it doesn't get started already in the next
+		 *   iteration of this loop.
+		 *
+		 * - Four times over the length of the requests queue (as
+		 *   indicated by video->uvc_num_requests), as a trade-off
+		 *   between latency and interrupt load.
+		*/
+		if (list_empty(&video->req_free) || ureq->last_buf ||
+			!(video->req_int_count %
+			DIV_ROUND_UP(video->uvc_num_requests, 4))) {
+			video->req_int_count = 0;
+			req->no_interrupt = 0;
+		} else {
+			req->no_interrupt = 1;
+		}
+		video->req_int_count++;
+		return uvcg_video_ep_queue(video, req);
+	} else {
+		/*
+		* If we're not queing to the ep, for isoc we're queing
+		* to the req_ready list, otherwise req_free.
+		*/
+		struct list_head *list =
+			is_bulk ? &video->req_free : &video->req_ready;

drivers/usb/gadget/function/uvc_video.c static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req) · line 325

Quoted 10 lines
drivers/usb/gadget/function/uvc_video.c +10 -0
+		list_add_tail(&req->list, list);
+	}
+	return 0;
+}
+
+static int uvcg_video_ep_queue_zero_length(struct usb_request *req,
+	struct uvc_video *video) {
+	req->length = 0;
+	return uvcg_video_ep_queue(video, req);
+}

Not sure this is worth its own function

Quoted 13 lines
drivers/usb/gadget/function/uvc_video.c +13 -0
+
+/* Must only be called from uvcg_video_enable - since after that we only want to
+ * queue requests to the endpoint from the uvc_video_complete complete handler.
+ * This function is needed in order to 'kick start' the flow of requests from
+ * gadget driver to the usb controller.
+ */
+static void uvc_video_ep_queue_initial_requests(struct uvc_video *video) {
+	struct usb_request *req = NULL;
+	unsigned long flags = 0;
+	unsigned int count = 0;
+	int ret = 0;
+	/* We only queue half of the free list since we still want to have
+	 * some free usb_requests in the free list for the video_pump async_wq

drivers/usb/gadget/function/uvc_video.c static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req) · line 348

Quoted 10 lines
drivers/usb/gadget/function/uvc_video.c +10 -0
+	 * thread to encode uvc buffers into. Otherwise we could get into a
+	 * situation where the free list does not have any usb requests to
+	 * encode into - we always end up queueing 0 length requests to the
+	 * end point.
+	 */
+	unsigned half_list_size = video->uvc_num_requests / 2;
+	spin_lock_irqsave(&video->req_lock, flags);
+	/* Take these requests off the free list and queue them all to the
+	 * endpoint. Since we queue the requests with the req_lock held,
+	 */

This comment seems to be incomplete? You also want an opening /* on its own line:

/*
 * Multi line comments
 * look like this
 */

Quoted 4 lines
drivers/usb/gadget/function/uvc_video.c +4 -0
+	while (count < half_list_size) {
+		req = list_first_entry(&video->req_free, struct usb_request,
+					list);
+		list_del(&req->list);

drivers/usb/gadget/function/uvc_video.c static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req) · line 362

Quoted 10 lines
drivers/usb/gadget/function/uvc_video.c +10 -0
+		ret = uvcg_video_ep_queue_zero_length(req, video);
+		if (ret < 0) {
+			uvcg_queue_cancel(&video->queue, /*disconnect*/0);
+			break;
+		}
+		count++;
+	}
+	spin_unlock_irqrestore(&video->req_lock, flags);
+}
+

So if I'm understanding the new starting sequence right for an isoc endpoint there's an initial flight of half the requests (between 2 and 32) that are queued as zero length - the very first one to .complete() being re-queued as a zero length request before the workqueue is started and encodes data into the _other_ half of the requests which were left in req_free and putting them into req_ready. At that point the .complete()s being run start to pick requests off req_ready instead and they get sent out with data...does that sound right?

What are the implications of those initial 3-33 zero length requests? What kind of latency can that introduce to the start of the video stream?

Quoted 33 lines
drivers/usb/gadget/function/uvc_video.c +18 -1
  static void
  uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
  {
@@ -276,6 +368,8 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
  	struct uvc_video_queue *queue = &video->queue;
  	struct uvc_buffer *last_buf = NULL;
  	unsigned long flags;
+	bool is_bulk = video->max_payload_size;
+	int ret = 0;
 
  	spin_lock_irqsave(&video->req_lock, flags);
  	if (!video->is_enabled) {
@@ -329,7 +423,38 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
  	 * back to req_free
  	 */
  	if (video->is_enabled) {
-		list_add_tail(&req->list, &video->req_free);
+		/*
+		 * Here we check whether any request is available in the ready
+		 * list. If it is, queue it to the ep and add the current
+		 * usb_request to the req_free list - for video_pump to fill in.
+		 * Otherwise, just use the current usb_request to queue a 0
+		 * length request to the ep. Since we always add to the req_free
+		 * list if we dequeue from the ready list, there will never
+		 * be a situation where the req_free list is completely out of
+		 * requests and cannot recover.
+		 */
+		struct usb_request *to_queue = req;
+		to_queue->length = 0;
+		if (!list_empty(&video->req_ready)) {
+			to_queue = list_first_entry(&video->req_ready,
+				struct usb_request, list);
+			list_del(&to_queue->list);

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 451

Quoted 10 lines
drivers/usb/gadget/function/uvc_video.c +10 -0
+			/* Add it to the free list. */
+			list_add_tail(&req->list, &video->req_free);
+		}
+		/*
+		 * Queue to the endpoint. The actual queueing to ep will
+		 * only happen on one thread - the async_wq for bulk endpoints
+		 * and this thread for isoc endpoints.
+		 */
+		ret = uvcg_video_usb_req_queue(video, to_queue,
+					       /*queue_to_ep*/!is_bulk);

In principle in-line comments are fine, but I don't think the parameter name is worth a comment

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 457

Quoted 6 lines
drivers/usb/gadget/function/uvc_video.c +6 -0
+		if(ret < 0) {
+			uvcg_queue_cancel(queue, 0);
+		}
+		/* Queue work to the wq as well since its possible that a buffer
+		 * may not have been completed.
+		 */

The phrasing of this implies this is a bit of defensive programming, but if we don't queue to the wq here then doesn't that mean it'll never run?

Quoted 63 lines
drivers/usb/gadget/function/uvc_video.c +2 -26
  		queue_work(video->async_wq, &video->pump);
  	} else {
  		uvc_video_free_request(ureq, ep);
@@ -347,6 +472,7 @@ uvc_video_free_requests(struct uvc_video *video)
 
  	INIT_LIST_HEAD(&video->ureqs);
  	INIT_LIST_HEAD(&video->req_free);
+	INIT_LIST_HEAD(&video->req_ready);
  	video->req_size = 0;
  	return 0;
  }
@@ -424,8 +550,7 @@ static void uvcg_video_pump(struct work_struct *work)
  	struct usb_request *req = NULL;
  	struct uvc_buffer *buf;
  	unsigned long flags;
-	bool buf_done;
-	int ret;
+	int ret = 0;
 
  	while (true) {
  		if (!video->ep->enabled)
@@ -454,7 +579,6 @@ static void uvcg_video_pump(struct work_struct *work)
 
  		if (buf != NULL) {
  			video->encode(req, video, buf);
-			buf_done = buf->state == UVC_BUF_STATE_DONE;
  		} else if (!(queue->flags & UVC_QUEUE_DISCONNECTED) && !is_bulk) {
  			/*
  			 * No video buffer available; the queue is still connected and
@@ -462,7 +586,6 @@ static void uvcg_video_pump(struct work_struct *work)
  			 * prevent missed ISOC transfers.
  			 */
  			req->length = 0;
-			buf_done = false;
  		} else {
  			/*
  			 * Either the queue has been disconnected or no video buffer
@@ -473,45 +596,26 @@ static void uvcg_video_pump(struct work_struct *work)
  			break;
  		}
 
-		/*
-		 * With USB3 handling more requests at a higher speed, we can't
-		 * afford to generate an interrupt for every request. Decide to
-		 * interrupt:
-		 *
-		 * - When no more requests are available in the free queue, as
-		 *   this may be our last chance to refill the endpoint's
-		 *   request queue.
-		 *
-		 * - When this is request is the last request for the video
-		 *   buffer, as we want to start sending the next video buffer
-		 *   ASAP in case it doesn't get started already in the next
-		 *   iteration of this loop.
-		 *
-		 * - Four times over the length of the requests queue (as
-		 *   indicated by video->uvc_num_requests), as a trade-off
-		 *   between latency and interrupt load.
-		 */
-		if (list_empty(&video->req_free) || buf_done ||
-		    !(video->req_int_count %
-		       DIV_ROUND_UP(video->uvc_num_requests, 4))) {
-			video->req_int_count = 0;

drivers/usb/gadget/function/uvc_video.c static void uvcg_video_pump(struct work_struct *work) · line 601

Quoted 10 lines
drivers/usb/gadget/function/uvc_video.c +1 -7
-			req->no_interrupt = 0;
-		} else {
-			req->no_interrupt = 1;
-		}
-
-		/* Queue the USB request */
-		ret = uvcg_video_ep_queue(video, req);
  		spin_unlock_irqrestore(&queue->irqlock, flags);
 
+		/* Queue the USB request.*/

I think just drop this - it was always superfluous.

Quoted 23 lines
drivers/usb/gadget/function/uvc_video.c +11 -2
+		spin_lock_irqsave(&video->req_lock, flags);
+		/* For bulk end points we queue from the worker thread
+		 * since we would preferably not want to wait on requests
+		 * to be ready, in the uvcg_video_complete() handler.
+		 * For isoc endpoints we add the request to the ready list
+		 * and only queue it to the endpoint from the complete handler.
+		 */
+		ret = uvcg_video_usb_req_queue(video, req, is_bulk);
+		spin_unlock_irqrestore(&video->req_lock, flags);
+
  		if (ret < 0) {
  			uvcg_queue_cancel(queue, 0);
  			break;
  		}
 
-		/* Endpoint now owns the request */
+		/* The request is owned by  the endpoint / ready list*/
  		req = NULL;
-		video->req_int_count++;
  	}
 
  	if (!req)

drivers/usb/gadget/function/uvc_video.c uvcg_video_disable(struct uvc_video *video) · line 674

Quoted 5 lines
drivers/usb/gadget/function/uvc_video.c +1 -1
@@ -567,7 +671,7 @@ uvcg_video_disable(struct uvc_video *video)
 
  	spin_lock_irqsave(&video->req_lock, flags);
  	/*
-	 * Remove all uvc_reqeusts from ureqs with list_del_init
+	 * Remove all uvc_requests from ureqs with list_del_init

This should get fixed in the earlier series.

drivers/usb/gadget/function/uvc_video.c uvcg_video_disable(struct uvc_video *video) · line 582

Quoted 8 lines
drivers/usb/gadget/function/uvc_video.c +0 -1
  	 * This lets uvc_video_free_request correctly identify
  	 * if the uvc_request is attached to a list or not when freeing
  	 * memory.
@@ -579,9 +683,13 @@ uvcg_video_disable(struct uvc_video *video)
  		list_del(&req->list);
  		uvc_video_free_request(req->context, video->ep);
  	}
-

keep the empty line please

drivers/usb/gadget/function/uvc_video.c uvcg_video_disable(struct uvc_video *video) · line 689

Quoted 4 lines
drivers/usb/gadget/function/uvc_video.c +4 -0
+	list_for_each_entry_safe(req, temp, &video->req_ready, list) {
+		list_del(&req->list);
+		uvc_video_free_request(req->context, video->ep);
+	}

and one here too.

Quoted 23 lines
drivers/usb/gadget/function/uvc_video.c +3 -1
  	INIT_LIST_HEAD(&video->ureqs);
  	INIT_LIST_HEAD(&video->req_free);
+	INIT_LIST_HEAD(&video->req_ready);
  	video->req_size = 0;
  	spin_unlock_irqrestore(&video->req_lock, flags);
 
@@ -635,7 +743,7 @@ int uvcg_video_enable(struct uvc_video *video)
 
  	video->req_int_count = 0;
 
-	queue_work(video->async_wq, &video->pump);
+	uvc_video_ep_queue_initial_requests(video);
 
  	return ret;
  }
@@ -648,6 +756,7 @@ int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
  	video->is_enabled = false;
  	INIT_LIST_HEAD(&video->ureqs);
  	INIT_LIST_HEAD(&video->req_free);
+	INIT_LIST_HEAD(&video->req_ready);
  	spin_lock_init(&video->req_lock);
  	INIT_WORK(&video->pump, uvcg_video_pump);
 
#16 lore raw

PATCH v4 1/1 usb:gadget:uvc Do not use worker thread to pump isoc usb requests

When we use an async work queue to perform the function of pumping usb requests to the usb controller, it is possible that amongst other factors, thread scheduling affects at what cadence we're able to pump requests. This could mean isoc usb requests miss their uframes - resulting in video stream flickers on the host device.

To avoid this, we make the async_wq thread only produce isoc usb_requests with uvc buffers encoded into them. The process of queueing to the endpoint is done by the uvc_video_complete() handler. In case no usb_requests are ready with encoded information, we just queue a zero length request to the endpoint from the complete handler.

For bulk endpoints the async_wq thread still queues usb requests to the endpoint.

---
Based on top of
https://lore.kernel.org/linux-usb/[email protected]/T/#t:
v1->v2: Added self Signed-Off-by and addressed review comments v2->v3: Encode to usb requests in async_wq; queue to ep in complete handler
for isoc transfers.
v3->v4: Address review comments around code style.

 drivers/usb/gadget/function/uvc.h       |   8 +
 drivers/usb/gadget/function/uvc_video.c | 195 +++++++++++++++++++-----
 2 files changed, 165 insertions(+), 38 deletions(-)
drivers/usb/gadget/function/uvc.h +8 -0
index e8d4c87f1e09..5ff454528bd8 100644
@@ -105,7 +105,15 @@ struct uvc_video {
 	bool is_enabled; /* tracks whether video stream is enabled */
 	unsigned int req_size;
 	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
+
+	/* USB requests that the video pump thread can encode into */
 	struct list_head req_free;
+
+	/*
+	 * USB requests video pump thread has already encoded into. These are
+	 * ready to be queued to the endpoint.
+	 */
+	struct list_head req_ready;
 	spinlock_t req_lock;
 
 	unsigned int req_int_count;
drivers/usb/gadget/function/uvc_video.c +157 -38

277 lines not on this page · raw message

Signature · 1 line
2.42.0.869.gea05f2083d-goog
#17 lore raw

PATCH v5 1/1 usb:gadget:uvc Do not use worker thread to pump isoc usb requests

When we use an async work queue to perform the function of pumping usb requests to the usb controller, it is possible that amongst other factors, thread scheduling affects at what cadence we're able to pump requests. This could mean isoc usb requests miss their uframes - resulting in video stream flickers on the host device.

To avoid this, we make the async_wq thread only produce isoc usb_requests with uvc buffers encoded into them. The process of queueing to the endpoint is done by the uvc_video_complete() handler. In case no usb_requests are ready with encoded information, we just queue a zero length request to the endpoint from the complete handler.

For bulk endpoints the async_wq thread still queues usb requests to the endpoint.

Change-Id I8a33cbf83fb2f04376826185079f8b25404fe761

---
Based on top of
https://lore.kernel.org/linux-usb/[email protected]/T/#t:
v1->v2: Added self Signed-Off-by and addressed review comments v2->v3: Encode to usb requests in async_wq; queue to ep in complete handler
for isoc transfers.
v3->v4: Address review comments around code style.
v4->v5: Update comments. Remove 0 length request queueing from async_wq
thread since it is already done by the complete handler.

 drivers/usb/gadget/function/uvc.h       |   8 +
 drivers/usb/gadget/function/uvc_video.c | 204 ++++++++++++++++++------
 2 files changed, 166 insertions(+), 46 deletions(-)
drivers/usb/gadget/function/uvc.h +8 -0
index e8d4c87f1e09..5ff454528bd8 100644
@@ -105,7 +105,15 @@ struct uvc_video {
 	bool is_enabled; /* tracks whether video stream is enabled */
 	unsigned int req_size;
 	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
+
+	/* USB requests that the video pump thread can encode into */
 	struct list_head req_free;
+
+	/*
+	 * USB requests video pump thread has already encoded into. These are
+	 * ready to be queued to the endpoint.
+	 */
+	struct list_head req_ready;
 	spinlock_t req_lock;
 
 	unsigned int req_int_count;
drivers/usb/gadget/function/uvc_video.c +158 -46

280 lines not on this page · raw message

Signature · 1 line
2.42.0.869.gea05f2083d-goog
maintainer
#18 lore raw

Re: [PATCH v5] usb:gadget:uvc Do not use worker thread to pump isoc usb requests

Quoted 20 lines · Jayant Chowdhary

When we use an async work queue to perform the function of pumping usb requests to the usb controller, it is possible that amongst other factors, thread scheduling affects at what cadence we're able to pump requests. This could mean isoc usb requests miss their uframes - resulting in video stream flickers on the host device.

To avoid this, we make the async_wq thread only produce isoc usb_requests with uvc buffers encoded into them. The process of queueing to the endpoint is done by the uvc_video_complete() handler. In case no usb_requests are ready with encoded information, we just queue a zero length request to the endpoint from the complete handler.

For bulk endpoints the async_wq thread still queues usb requests to the endpoint.

Change-Id I8a33cbf83fb2f04376826185079f8b25404fe761
Quoted 9 lines

---
Based on top of
https://lore.kernel.org/linux-usb/[email protected]/T/#t:
v1->v2: Added self Signed-Off-by and addressed review comments v2->v3: Encode to usb requests in async_wq; queue to ep in complete handler
for isoc transfers.
v3->v4: Address review comments around code style.
v4->v5: Update comments. Remove 0 length request queueing from async_wq
thread since it is already done by the complete handler.

You forgot to run checkpatch.pl :(

#19 lore raw

Re: [PATCH v5] usb:gadget:uvc Do not use worker thread to pump isoc usb requests

Quoted 30 lines · Greg KH
Quoted 20 lines · Jayant Chowdhary

When we use an async work queue to perform the function of pumping usb requests to the usb controller, it is possible that amongst other factors, thread scheduling affects at what cadence we're able to pump requests. This could mean isoc usb requests miss their uframes - resulting in video stream flickers on the host device.

To avoid this, we make the async_wq thread only produce isoc usb_requests with uvc buffers encoded into them. The process of queueing to the endpoint is done by the uvc_video_complete() handler. In case no usb_requests are ready with encoded information, we just queue a zero length request to the endpoint from the complete handler.

For bulk endpoints the async_wq thread still queues usb requests to the endpoint.

Change-Id I8a33cbf83fb2f04376826185079f8b25404fe761
Quoted 9 lines

---
Based on top of
https://lore.kernel.org/linux-usb/[email protected]/T/#t:
v1->v2: Added self Signed-Off-by and addressed review comments v2->v3: Encode to usb requests in async_wq; queue to ep in complete handler
for isoc transfers.
v3->v4: Address review comments around code style.
v4->v5: Update comments. Remove 0 length request queueing from async_wq
thread since it is already done by the complete handler.

Quoted 1 line

You forgot to run checkpatch.pl :(

My apologies, I sent out v6 with checkpatch.pl fixed.

Thanks

#20 lore raw

PATCH v6 1/1 usb:gadget:uvc Do not use worker thread to pump isoc usb requests

In reply to Jayant Chowdhary

When we use an async work queue to perform the function of pumping usb requests to the usb controller, it is possible that amongst other factors, thread scheduling affects at what cadence we're able to pump requests. This could mean isoc usb requests miss their uframes - resulting in video stream flickers on the host device.

To avoid this, we make the async_wq thread only produce isoc usb_requests with uvc buffers encoded into them. The process of queueing to the endpoint is done by the uvc_video_complete() handler. In case no usb_requests are ready with encoded information, we just queue a zero length request to the endpoint from the complete handler.

For bulk endpoints the async_wq thread still queues usb requests to the endpoint.

---
Based on top of
https://lore.kernel.org/linux-usb/[email protected]/T/#t:
v1->v2: Added self Signed-Off-by and addressed review comments v2->v3: Encode to usb requests in async_wq; queue to ep in complete handler
for isoc transfers.
v3->v4: Address review comments around code style.
v4->v5: Update comments. Remove 0 length request queueing from async_wq
thread since it is already done by the complete handler.
v5->v6: Fix checkpatch.pl suggestions.

 drivers/usb/gadget/function/uvc.h       |   8 +
 drivers/usb/gadget/function/uvc_video.c | 204 ++++++++++++++++++------
 2 files changed, 166 insertions(+), 46 deletions(-)
drivers/usb/gadget/function/uvc.h +8 -0
index e8d4c87f1e09..5ff454528bd8 100644
@@ -105,7 +105,15 @@ struct uvc_video {
 	bool is_enabled; /* tracks whether video stream is enabled */
 	unsigned int req_size;
 	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
+
+	/* USB requests that the video pump thread can encode into */
 	struct list_head req_free;
+
+	/*
+	 * USB requests video pump thread has already encoded into. These are
+	 * ready to be queued to the endpoint.
+	 */
+	struct list_head req_ready;
 	spinlock_t req_lock;
 
 	unsigned int req_int_count;
drivers/usb/gadget/function/uvc_video.c +158 -46

280 lines not on this page · raw message

Signature · 1 line
2.42.0.869.gea05f2083d-goog
#21 lore raw

Re: [PATCH v6] usb:gadget:uvc Do not use worker thread to pump isoc usb requests

Hi Jayant, thanks for the update. I just have a couple of styling comments.

Quoted 115 lines · Jayant Chowdhary

When we use an async work queue to perform the function of pumping usb requests to the usb controller, it is possible that amongst other factors, thread scheduling affects at what cadence we're able to pump requests. This could mean isoc usb requests miss their uframes - resulting in video stream flickers on the host device.

To avoid this, we make the async_wq thread only produce isoc usb_requests with uvc buffers encoded into them. The process of queueing to the endpoint is done by the uvc_video_complete() handler. In case no usb_requests are ready with encoded information, we just queue a zero length request to the endpoint from the complete handler.

For bulk endpoints the async_wq thread still queues usb requests to the endpoint.

---
Based on top of
https://lore.kernel.org/linux-usb/[email protected]/T/#t:
v1->v2: Added self Signed-Off-by and addressed review comments v2->v3: Encode to usb requests in async_wq; queue to ep in complete handler
for isoc transfers.
v3->v4: Address review comments around code style.
v4->v5: Update comments. Remove 0 length request queueing from async_wq
thread since it is already done by the complete handler.
v5->v6: Fix checkpatch.pl suggestions.

  drivers/usb/gadget/function/uvc.h       |   8 +
  drivers/usb/gadget/function/uvc_video.c | 204 ++++++++++++++++++------
  2 files changed, 166 insertions(+), 46 deletions(-)
drivers/usb/gadget/function/uvc.h +8 -0
index e8d4c87f1e09..5ff454528bd8 100644
@@ -105,7 +105,15 @@ struct uvc_video {
  	bool is_enabled; /* tracks whether video stream is enabled */
  	unsigned int req_size;
  	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
+
+	/* USB requests that the video pump thread can encode into */
  	struct list_head req_free;
+
+	/*
+	 * USB requests video pump thread has already encoded into. These are
+	 * ready to be queued to the endpoint.
+	 */
+	struct list_head req_ready;
  	spinlock_t req_lock;
 
  	unsigned int req_int_count;
drivers/usb/gadget/function/uvc_video.c +53 -0
index 53feb790a4c3..d5311456fa8a 100644
@@ -268,6 +268,100 @@ static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req)
  	return ret;
  }
 
+/* This function must be called with video->req_lock held. */
+static int uvcg_video_usb_req_queue(struct uvc_video *video,
+	struct usb_request *req, bool queue_to_ep)
+{
+	bool is_bulk = video->max_payload_size;
+	struct list_head *list = NULL;
+
+	if (!video->is_enabled) {
+		uvc_video_free_request(req->context, video->ep);
+		return -ENODEV;
+	}
+	if (queue_to_ep) {
+		struct uvc_request *ureq = req->context;
+		/*
+		 * With USB3 handling more requests at a higher speed, we can't
+		 * afford to generate an interrupt for every request. Decide to
+		 * interrupt:
+		 *
+		 * - When no more requests are available in the free queue, as
+		 *   this may be our last chance to refill the endpoint's
+		 *   request queue.
+		 *
+		 * - When this is request is the last request for the video
+		 *   buffer, as we want to start sending the next video buffer
+		 *   ASAP in case it doesn't get started already in the next
+		 *   iteration of this loop.
+		 *
+		 * - Four times over the length of the requests queue (as
+		 *   indicated by video->uvc_num_requests), as a trade-off
+		 *   between latency and interrupt load.
+		 */
+		if (list_empty(&video->req_free) || ureq->last_buf ||
+			!(video->req_int_count %
+			DIV_ROUND_UP(video->uvc_num_requests, 4))) {
+			video->req_int_count = 0;
+			req->no_interrupt = 0;
+		} else {
+			req->no_interrupt = 1;
+		}
+		video->req_int_count++;
+		return uvcg_video_ep_queue(video, req);
+	}
+	/*
+	 * If we're not queuing to the ep, for isoc we're queuing
+	 * to the req_ready list, otherwise req_free.
+	 */
+	list = is_bulk ? &video->req_free : &video->req_ready;
+	list_add_tail(&req->list, list);
+	return 0;
+}
+
+/*
+ * Must only be called from uvcg_video_enable - since after that we only want to

drivers/usb/gadget/function/uvc_video.c static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req) · line 333

Quoted 10 lines
drivers/usb/gadget/function/uvc_video.c +10 -0
+ * queue requests to the endpoint from the uvc_video_complete complete handler.
+ * This function is needed in order to 'kick start' the flow of requests from
+ * gadget driver to the usb controller.
+ */
+static void uvc_video_ep_queue_initial_requests(struct uvc_video *video)
+{
+	struct usb_request *req = NULL;
+	unsigned long flags = 0;
+	unsigned int count = 0;
+	int ret = 0;

Add an empty line here please

Quoted 14 lines
drivers/usb/gadget/function/uvc_video.c +14 -0
+	/*
+	 * We only queue half of the free list since we still want to have
+	 * some free usb_requests in the free list for the video_pump async_wq
+	 * thread to encode uvc buffers into. Otherwise we could get into a
+	 * situation where the free list does not have any usb requests to
+	 * encode into - we always end up queueing 0 length requests to the
+	 * end point.
+	 */
+	unsigned int half_list_size = video->uvc_num_requests / 2;
+
+	spin_lock_irqsave(&video->req_lock, flags);
+	/*
+	 * Take these requests off the free list and queue them all to the
+	 * endpoint. Since we queue 0 length requests with the req_lock held,

drivers/usb/gadget/function/uvc_video.c static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req) · line 357

Quoted 10 lines
drivers/usb/gadget/function/uvc_video.c +10 -0
+	 * there isn't any 'data' race involved here with the complete handler.
+	 */
+	while (count < half_list_size) {
+		req = list_first_entry(&video->req_free, struct usb_request,
+					list);
+		list_del(&req->list);
+		req->length = 0;
+		ret = uvcg_video_ep_queue(video, req);
+		if (ret < 0) {
+			uvcg_queue_cancel(&video->queue, /*disconnect*/0);

Drop the /*disconnect*/ comment please

Quoted 33 lines
drivers/usb/gadget/function/uvc_video.c +17 -2
+			break;
+		}
+		count++;
+	}
+	spin_unlock_irqrestore(&video->req_lock, flags);
+}
+
  static void
  uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
  {
@@ -276,6 +370,8 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
  	struct uvc_video_queue *queue = &video->queue;
  	struct uvc_buffer *last_buf = NULL;
  	unsigned long flags;
+	bool is_bulk = video->max_payload_size;
+	int ret = 0;
 
  	spin_lock_irqsave(&video->req_lock, flags);
  	if (!video->is_enabled) {
@@ -329,8 +425,46 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
  	 * back to req_free
  	 */
  	if (video->is_enabled) {
-		list_add_tail(&req->list, &video->req_free);
-		queue_work(video->async_wq, &video->pump);
+		/*
+		 * Here we check whether any request is available in the ready
+		 * list. If it is, queue it to the ep and add the current
+		 * usb_request to the req_free list - for video_pump to fill in.
+		 * Otherwise, just use the current usb_request to queue a 0
+		 * length request to the ep. Since we always add to the req_free
+		 * list if we dequeue from the ready list, there will never
+		 * be a situation where the req_free list is completely out of

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 445

Quoted 10 lines
drivers/usb/gadget/function/uvc_video.c +10 -0
+		 * requests and cannot recover.
+		 */
+		struct usb_request *to_queue = req;
+
+		to_queue->length = 0;
+		if (!list_empty(&video->req_ready)) {
+			to_queue = list_first_entry(&video->req_ready,
+				struct usb_request, list);
+			list_del(&to_queue->list);
+			/* Add it to the free list. */

I would drop the "Add it to the free list" comment; the code is clear already.

Quoted 117 lines
drivers/usb/gadget/function/uvc_video.c +35 -42
+			list_add_tail(&req->list, &video->req_free);
+			/*
+			 * Queue work to the wq as well since it is possible that a
+			 * buffer may not have been completely encoded with the set of
+			 * in-flight usb requests for whih the complete callbacks are
+			 * firing.
+			 * In that case, if we do not queue work to the worker thread,
+			 * the buffer will never be marked as complete - and therefore
+			 * not be returned to userpsace. As a result,
+			 * dequeue -> queue -> dequeue flow of uvc buffers will not
+			 * happen.
+			 */
+			queue_work(video->async_wq, &video->pump);
+		}
+		/*
+		 * Queue to the endpoint. The actual queueing to ep will
+		 * only happen on one thread - the async_wq for bulk endpoints
+		 * and this thread for isoc endpoints.
+		 */
+		ret = uvcg_video_usb_req_queue(video, to_queue, !is_bulk);
+		if (ret < 0)
+			uvcg_queue_cancel(queue, 0);
  	} else {
  		uvc_video_free_request(ureq, ep);
  	}
@@ -347,6 +481,7 @@ uvc_video_free_requests(struct uvc_video *video)
 
  	INIT_LIST_HEAD(&video->ureqs);
  	INIT_LIST_HEAD(&video->req_free);
+	INIT_LIST_HEAD(&video->req_ready);
  	video->req_size = 0;
  	return 0;
  }
@@ -424,8 +559,7 @@ static void uvcg_video_pump(struct work_struct *work)
  	struct usb_request *req = NULL;
  	struct uvc_buffer *buf;
  	unsigned long flags;
-	bool buf_done;
-	int ret;
+	int ret = 0;
 
  	while (true) {
  		if (!video->ep->enabled)
@@ -454,15 +588,6 @@ static void uvcg_video_pump(struct work_struct *work)
 
  		if (buf != NULL) {
  			video->encode(req, video, buf);
-			buf_done = buf->state == UVC_BUF_STATE_DONE;
-		} else if (!(queue->flags & UVC_QUEUE_DISCONNECTED) && !is_bulk) {
-			/*
-			 * No video buffer available; the queue is still connected and
-			 * we're transferring over ISOC. Queue a 0 length request to
-			 * prevent missed ISOC transfers.
-			 */
-			req->length = 0;
-			buf_done = false;
  		} else {
  			/*
  			 * Either the queue has been disconnected or no video buffer
@@ -473,45 +598,25 @@ static void uvcg_video_pump(struct work_struct *work)
  			break;
  		}
 
-		/*
-		 * With USB3 handling more requests at a higher speed, we can't
-		 * afford to generate an interrupt for every request. Decide to
-		 * interrupt:
-		 *
-		 * - When no more requests are available in the free queue, as
-		 *   this may be our last chance to refill the endpoint's
-		 *   request queue.
-		 *
-		 * - When this is request is the last request for the video
-		 *   buffer, as we want to start sending the next video buffer
-		 *   ASAP in case it doesn't get started already in the next
-		 *   iteration of this loop.
-		 *
-		 * - Four times over the length of the requests queue (as
-		 *   indicated by video->uvc_num_requests), as a trade-off
-		 *   between latency and interrupt load.
-		 */
-		if (list_empty(&video->req_free) || buf_done ||
-		    !(video->req_int_count %
-		       DIV_ROUND_UP(video->uvc_num_requests, 4))) {
-			video->req_int_count = 0;
-			req->no_interrupt = 0;
-		} else {
-			req->no_interrupt = 1;
-		}
-
-		/* Queue the USB request */
-		ret = uvcg_video_ep_queue(video, req);
  		spin_unlock_irqrestore(&queue->irqlock, flags);
 
+		spin_lock_irqsave(&video->req_lock, flags);
+		/* For bulk end points we queue from the worker thread
+		 * since we would preferably not want to wait on requests
+		 * to be ready, in the uvcg_video_complete() handler.
+		 * For isoc endpoints we add the request to the ready list
+		 * and only queue it to the endpoint from the complete handler.
+		 */
+		ret = uvcg_video_usb_req_queue(video, req, is_bulk);
+		spin_unlock_irqrestore(&video->req_lock, flags);
+
  		if (ret < 0) {
  			uvcg_queue_cancel(queue, 0);
  			break;
  		}
 
-		/* Endpoint now owns the request */
+		/* The request is owned by  the endpoint / ready list. */
  		req = NULL;
-		video->req_int_count++;
  	}
 
  	if (!req)

drivers/usb/gadget/function/uvc_video.c uvcg_video_disable(struct uvc_video *video) · line 675

Quoted 5 lines
drivers/usb/gadget/function/uvc_video.c +1 -1
@@ -567,7 +672,7 @@ uvcg_video_disable(struct uvc_video *video)
 
  	spin_lock_irqsave(&video->req_lock, flags);
  	/*
-	 * Remove all uvc_reqeusts from ureqs with list_del_init
+	* Remove all uvc_requests from ureqs with list_del_init

Did the alignment of the * get messed up here as well as the typo fix or is it just my mail client being weird?

Quoted 35 lines
drivers/usb/gadget/function/uvc_video.c +8 -1
  	 * This lets uvc_video_free_request correctly identify
  	 * if the uvc_request is attached to a list or not when freeing
  	 * memory.
@@ -580,8 +685,14 @@ uvcg_video_disable(struct uvc_video *video)
  		uvc_video_free_request(req->context, video->ep);
  	}
 
+	list_for_each_entry_safe(req, temp, &video->req_ready, list) {
+		list_del(&req->list);
+		uvc_video_free_request(req->context, video->ep);
+	}
+
  	INIT_LIST_HEAD(&video->ureqs);
  	INIT_LIST_HEAD(&video->req_free);
+	INIT_LIST_HEAD(&video->req_ready);
  	video->req_size = 0;
  	spin_unlock_irqrestore(&video->req_lock, flags);
 
@@ -635,7 +746,7 @@ int uvcg_video_enable(struct uvc_video *video)
 
  	video->req_int_count = 0;
 
-	queue_work(video->async_wq, &video->pump);
+	uvc_video_ep_queue_initial_requests(video);
 
  	return ret;
  }
@@ -648,6 +759,7 @@ int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
  	video->is_enabled = false;
  	INIT_LIST_HEAD(&video->ureqs);
  	INIT_LIST_HEAD(&video->req_free);
+	INIT_LIST_HEAD(&video->req_ready);
  	spin_lock_init(&video->req_lock);
  	INIT_WORK(&video->pump, uvcg_video_pump);
 
#22 lore raw

Re: [PATCH v6] usb:gadget:uvc Do not use worker thread to pump isoc usb requests

Hi Dan,
Thanks for the comments. I've uploaded a new patch at https://lore.kernel.org/linux-usb/[email protected]/T/#u.

Quoted 119 lines · Dan Scally

Hi Jayant, thanks for the update. I just have a couple of styling comments.

Quoted 116 lines · Jayant Chowdhary

When we use an async work queue to perform the function of pumping usb requests to the usb controller, it is possible that amongst other factors, thread scheduling affects at what cadence we're able to pump requests. This could mean isoc usb requests miss their uframes - resulting in video stream flickers on the host device.

To avoid this, we make the async_wq thread only produce isoc usb_requests with uvc buffers encoded into them. The process of queueing to the endpoint is done by the uvc_video_complete() handler. In case no usb_requests are ready with encoded information, we just queue a zero length request to the endpoint from the complete handler.

For bulk endpoints the async_wq thread still queues usb requests to the endpoint.

---
  Based on top of
  https://lore.kernel.org/linux-usb/[email protected]/T/#t:
  v1->v2: Added self Signed-Off-by and addressed review comments v2->v3: Encode to usb requests in async_wq; queue to ep in complete handler for isoc transfers.
  v3->v4: Address review comments around code style.
  v4->v5: Update comments. Remove 0 length request queueing from async_wq thread since it is already done by the complete handler. v5->v6: Fix checkpatch.pl suggestions.

  drivers/usb/gadget/function/uvc.h       |   8 + drivers/usb/gadget/function/uvc_video.c | 204 ++++++++++++++++++------

  2 files changed, 166 insertions(+), 46 deletions(-)
drivers/usb/gadget/function/uvc.h +0 -0
index e8d4c87f1e09..5ff454528bd8 100644
@@ -105,7 +105,15 @@ struct uvc_video {

      bool is_enabled; /* tracks whether video stream is enabled */ unsigned int req_size;
      struct list_head ureqs; /* all uvc_requests allocated by uvc_video */ +
+    /* USB requests that the video pump thread can encode into */ struct list_head req_free;
+
+    /*
+     * USB requests video pump thread has already encoded into. These are +     * ready to be queued to the endpoint.
+     */
+    struct list_head req_ready;
      spinlock_t req_lock;
        unsigned int req_int_count;

drivers/usb/gadget/function/uvc_video.c +0 -0
index 53feb790a4c3..d5311456fa8a 100644
@@ -268,6 +268,100 @@ static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req)
       return ret;
   }

  +/* This function must be called with video->req_lock held. */

drivers/usb/gadget/function/uvc_video.c +55 -0
+static int uvcg_video_usb_req_queue(struct uvc_video *video,
+    struct usb_request *req, bool queue_to_ep)
+{
+    bool is_bulk = video->max_payload_size;
+    struct list_head *list = NULL;
+
+    if (!video->is_enabled) {
+        uvc_video_free_request(req->context, video->ep);
+        return -ENODEV;
+    }
+    if (queue_to_ep) {
+        struct uvc_request *ureq = req->context;
+        /*
+         * With USB3 handling more requests at a higher speed, we can't
+         * afford to generate an interrupt for every request. Decide to
+         * interrupt:
+         *
+         * - When no more requests are available in the free queue, as
+         *   this may be our last chance to refill the endpoint's
+         *   request queue.
+         *
+         * - When this is request is the last request for the video
+         *   buffer, as we want to start sending the next video buffer
+         *   ASAP in case it doesn't get started already in the next
+         *   iteration of this loop.
+         *
+         * - Four times over the length of the requests queue (as
+         *   indicated by video->uvc_num_requests), as a trade-off
+         *   between latency and interrupt load.
+         */
+        if (list_empty(&video->req_free) || ureq->last_buf ||
+            !(video->req_int_count %
+            DIV_ROUND_UP(video->uvc_num_requests, 4))) {
+            video->req_int_count = 0;
+            req->no_interrupt = 0;
+        } else {
+            req->no_interrupt = 1;
+        }
+        video->req_int_count++;
+        return uvcg_video_ep_queue(video, req);
+    }
+    /*
+     * If we're not queuing to the ep, for isoc we're queuing
+     * to the req_ready list, otherwise req_free.
+     */
+    list = is_bulk ? &video->req_free : &video->req_ready;
+    list_add_tail(&req->list, list);
+    return 0;
+}
+
+/*
+ * Must only be called from uvcg_video_enable - since after that we only want to
+ * queue requests to the endpoint from the uvc_video_complete complete handler.
+ * This function is needed in order to 'kick start' the flow of requests from
+ * gadget driver to the usb controller.
Quoted 8 lines

drivers/usb/gadget/function/uvc_video.c static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req) · line 333

Quoted 7 lines

+ */

drivers/usb/gadget/function/uvc_video.c +6 -0
+static void uvc_video_ep_queue_initial_requests(struct uvc_video *video)
+{
+    struct usb_request *req = NULL;
+    unsigned long flags = 0;
+    unsigned int count = 0;
+    int ret = 0;

Add an empty line here please

Done.

Quoted 26 lines
Quoted 16 lines

+    /*
+     * We only queue half of the free list since we still want to have +     * some free usb_requests in the free list for the video_pump async_wq +     * thread to encode uvc buffers into. Otherwise we could get into a +     * situation where the free list does not have any usb requests to +     * encode into - we always end up queueing 0 length requests to the +     * end point.
+     */
+    unsigned int half_list_size = video->uvc_num_requests / 2; +
+    spin_lock_irqsave(&video->req_lock, flags);
+    /*
+     * Take these requests off the free list and queue them all to the +     * endpoint. Since we queue 0 length requests with the req_lock held, +     * there isn't any 'data' race involved here with the complete handler. +     */

Quoted 8 lines

+    while (count < half_list_size) {
+        req = list_first_entry(&video->req_free, struct usb_request, +                    list);
+        list_del(&req->list);
+        req->length = 0;
+        ret = uvcg_video_ep_queue(video, req);
+        if (ret < 0) {
+            uvcg_queue_cancel(&video->queue, /*disconnect*/0);

Quoted 1 line

Drop the /*disconnect*/ comment please

Done.

Quoted 36 lines
Quoted 36 lines

+            break;
+        }
+        count++;
+    }
+    spin_unlock_irqrestore(&video->req_lock, flags);

drivers/usb/gadget/function/uvc_video.c +2 -0
+}
+

  static void
  uvc_video_complete(struct usb_ep *ep, struct usb_request *req) {

drivers/usb/gadget/function/uvc_video.c +13 -2
@@ -276,6 +370,8 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
       struct uvc_video_queue *queue = &video->queue;
       struct uvc_buffer *last_buf = NULL;
       unsigned long flags;
+    bool is_bulk = video->max_payload_size;
+    int ret = 0;
         spin_lock_irqsave(&video->req_lock, flags);
       if (!video->is_enabled) {
@@ -329,8 +425,46 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
        * back to req_free
        */
       if (video->is_enabled) {
-        list_add_tail(&req->list, &video->req_free);
-        queue_work(video->async_wq, &video->pump);
+        /*
+         * Here we check whether any request is available in the ready
+         * list. If it is, queue it to the ep and add the current
+         * usb_request to the req_free list - for video_pump to fill in.
+         * Otherwise, just use the current usb_request to queue a 0
+         * length request to the ep. Since we always add to the req_free
+         * list if we dequeue from the ready list, there will never
+         * be a situation where the req_free list is completely out of
+         * requests and cannot recover.
+         */
+        struct usb_request *to_queue = req;
Quoted 8 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 445

Quoted 7 lines
drivers/usb/gadget/function/uvc_video.c +7 -0
+
+        to_queue->length = 0;
+        if (!list_empty(&video->req_ready)) {
+            to_queue = list_first_entry(&video->req_ready,
+                struct usb_request, list);
+            list_del(&to_queue->list);
+            /* Add it to the free list. */

I would drop the "Add it to the free list" comment; the code is clear already.

Done.

Quoted 108 lines
Quoted 108 lines
drivers/usb/gadget/function/uvc_video.c +24 -11
+            list_add_tail(&req->list, &video->req_free);
+            /*
+             * Queue work to the wq as well since it is possible that a
+             * buffer may not have been completely encoded with the set of
+             * in-flight usb requests for whih the complete callbacks are
+             * firing.
+             * In that case, if we do not queue work to the worker thread,
+             * the buffer will never be marked as complete - and therefore
+             * not be returned to userpsace. As a result,
+             * dequeue -> queue -> dequeue flow of uvc buffers will not
+             * happen.
+             */
+            queue_work(video->async_wq, &video->pump);
+        }
+        /*
+         * Queue to the endpoint. The actual queueing to ep will
+         * only happen on one thread - the async_wq for bulk endpoints
+         * and this thread for isoc endpoints.
+         */
+        ret = uvcg_video_usb_req_queue(video, to_queue, !is_bulk);
+        if (ret < 0)
+            uvcg_queue_cancel(queue, 0);
       } else {
           uvc_video_free_request(ureq, ep);
       }
@@ -347,6 +481,7 @@ uvc_video_free_requests(struct uvc_video *video)
         INIT_LIST_HEAD(&video->ureqs);
       INIT_LIST_HEAD(&video->req_free);
+    INIT_LIST_HEAD(&video->req_ready);
       video->req_size = 0;
       return 0;
   }
@@ -424,8 +559,7 @@ static void uvcg_video_pump(struct work_struct *work)
       struct usb_request *req = NULL;
       struct uvc_buffer *buf;
       unsigned long flags;
-    bool buf_done;
-    int ret;
+    int ret = 0;
         while (true) {
           if (!video->ep->enabled)
@@ -454,15 +588,6 @@ static void uvcg_video_pump(struct work_struct *work)
             if (buf != NULL) {
               video->encode(req, video, buf);
-            buf_done = buf->state == UVC_BUF_STATE_DONE;
-        } else if (!(queue->flags & UVC_QUEUE_DISCONNECTED) && !is_bulk) {
-            /*
-             * No video buffer available; the queue is still connected and
-             * we're transferring over ISOC. Queue a 0 length request to
-             * prevent missed ISOC transfers.
-             */
-            req->length = 0;
-            buf_done = false;
           } else {
               /*
                * Either the queue has been disconnected or no video buffer
@@ -473,45 +598,25 @@ static void uvcg_video_pump(struct work_struct *work)
               break;
           }

  -        /*
-         * With USB3 handling more requests at a higher speed, we can't -         * afford to generate an interrupt for every request. Decide to -         * interrupt:
-         *
-         * - When no more requests are available in the free queue, as -         *   this may be our last chance to refill the endpoint's -         *   request queue.
-         *
-         * - When this is request is the last request for the video -         *   buffer, as we want to start sending the next video buffer -         *   ASAP in case it doesn't get started already in the next -         *   iteration of this loop.
-         *
-         * - Four times over the length of the requests queue (as -         *   indicated by video->uvc_num_requests), as a trade-off -         *   between latency and interrupt load.
-         */
-        if (list_empty(&video->req_free) || buf_done ||
-            !(video->req_int_count %
-               DIV_ROUND_UP(video->uvc_num_requests, 4))) { -            video->req_int_count = 0;
-            req->no_interrupt = 0;
-        } else {
-            req->no_interrupt = 1;
-        }
-
-        /* Queue the USB request */
-        ret = uvcg_video_ep_queue(video, req); spin_unlock_irqrestore(&queue->irqlock, flags);
  +        spin_lock_irqsave(&video->req_lock, flags);
+        /* For bulk end points we queue from the worker thread +         * since we would preferably not want to wait on requests +         * to be ready, in the uvcg_video_complete() handler. +         * For isoc endpoints we add the request to the ready list +         * and only queue it to the endpoint from the complete handler. +         */
+        ret = uvcg_video_usb_req_queue(video, req, is_bulk); +        spin_unlock_irqrestore(&video->req_lock, flags);
+
          if (ret < 0) {
              uvcg_queue_cancel(queue, 0);
              break;
          }
  -        /* Endpoint now owns the request */
+        /* The request is owned by  the endpoint / ready list. */ req = NULL;
-        video->req_int_count++;
      }

Quoted 9 lines

drivers/usb/gadget/function/uvc_video.c uvcg_video_disable(struct uvc_video *video) · line 674

Quoted 6 lines

        if (!req)

drivers/usb/gadget/function/uvc_video.c +1 -1
@@ -567,7 +672,7 @@ uvcg_video_disable(struct uvc_video *video)
         spin_lock_irqsave(&video->req_lock, flags);
       /*
-     * Remove all uvc_reqeusts from ureqs with list_del_init
+    * Remove all uvc_requests from ureqs with list_del_init

Did the alignment of the * get messed up here as well as the typo fix or is it just my mail client being weird?

I think the alignment did indeed get messed up. Fixed.

Thank you,

Jayant

#23 lore raw

PATCH v7 1/1 usb:gadget:uvc Do not use worker thread to pump isoc usb requests

In reply to Jayant Chowdhary

When we use an async work queue to perform the function of pumping usb requests to the usb controller, it is possible that amongst other factors, thread scheduling affects at what cadence we're able to pump requests. This could mean isoc usb requests miss their uframes - resulting in video stream flickers on the host device.

To avoid this, we make the async_wq thread only produce isoc usb_requests with uvc buffers encoded into them. The process of queueing to the endpoint is done by the uvc_video_complete() handler. In case no usb_requests are ready with encoded information, we just queue a zero length request to the endpoint from the complete handler.

For bulk endpoints the async_wq thread still queues usb requests to the endpoint.

---
Based on top of
https://lore.kernel.org/linux-usb/[email protected]/T/#t:
v1->v2: Added self Signed-Off-by and addressed review comments v2->v3: Encode to usb requests in async_wq; queue to ep in complete handler
for isoc transfers.
v3->v4: Address review comments around code style.
v4->v5: Update comments. Remove 0 length request queueing from async_wq
thread since it is already done by the complete handler.
v5->v6: Fix checkpatch.pl suggestions.
v6->v7: Fix code style review comments.

 drivers/usb/gadget/function/uvc.h       |   8 +
 drivers/usb/gadget/function/uvc_video.c | 204 ++++++++++++++++++------
 2 files changed, 166 insertions(+), 46 deletions(-)
drivers/usb/gadget/function/uvc.h +8 -0
index e8d4c87f1e09..5ff454528bd8 100644
@@ -105,7 +105,15 @@ struct uvc_video {
 	bool is_enabled; /* tracks whether video stream is enabled */
 	unsigned int req_size;
 	struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
+
+	/* USB requests that the video pump thread can encode into */
 	struct list_head req_free;
+
+	/*
+	 * USB requests video pump thread has already encoded into. These are
+	 * ready to be queued to the endpoint.
+	 */
+	struct list_head req_ready;
 	spinlock_t req_lock;
 
 	unsigned int req_int_count;
drivers/usb/gadget/function/uvc_video.c +158 -46

280 lines not on this page · raw message

Signature · 1 line
2.43.0.rc0.421.g78406f8d94-goog
#24 lore raw

Re: [PATCH v3] usb:gadget:uvc Do not use worker thread to queue isoc usb requests

In reply to Dan Scally

Hi Dan,
Thank you for the comments.
I uploaded a new patch at https://lore.kernel.org/linux-usb/[email protected]/T/#u.

Quoted 33 lines · Dan Scally

Hi Jayant - thanks for the patch

Quoted 30 lines · Jayant Chowdhary

When we use an async work queue to perform the function of pumping usb requests to the usb controller, it is possible that amongst other factors, thread scheduling affects at what cadence we're able to pump requests. This could mean isoc usb requests miss their uframes - resulting in video stream flickers on the host device.

To avoid this, we make the async_wq thread only produce isoc usb_requests with uvc buffers encoded into them. The process of queueing to the endpoint is done by the uvc_video_complete() handler. In case no usb_requests are ready with encoded information, we just queue a zero length request to the endpoint from the complete handler.

For bulk endpoints the async_wq thread still queues usb requests to the endpoint.

---
  Based on top of
  https://lore.kernel.org/linux-usb/[email protected]/T/#t:
  v1->v2: Added self Signed-Off-by and addressed review comments v2->v3: Encode to usb requests in async_wq; queue to ep in complete handler for isoc transfers.

  drivers/usb/gadget/function/uvc.h       |   8 + drivers/usb/gadget/function/uvc_video.c | 187 +++++++++++++++++++-----

  2 files changed, 156 insertions(+), 39 deletions(-)
Quoted 12 lines
Quoted 10 lines
drivers/usb/gadget/function/uvc.h +0 -0
index e8d4c87f1e09..82c783410554 100644
@@ -105,7 +105,15 @@ struct uvc_video {

      bool is_enabled; /* tracks whether video stream is enabled */ unsigned int req_size;
      struct list_head ureqs; /* all uvc_requests allocated by uvc_video */ +
+    /* USB requests video pump thread can encode into*/

"USB requests that the video pump thread can encode into", and a space before the closing */ please (and the same a few more times below).

Done.

Quoted 13 lines
Quoted 13 lines

      struct list_head req_free;
+
+    /*
+     * USB requests video pump thread has already encoded into. These are +     * ready to be queued to the endpoint.
+     */
+    struct list_head req_ready;
      spinlock_t req_lock;
        unsigned int req_int_count;

drivers/usb/gadget/function/uvc_video.c +0 -1
index 53feb790a4c3..c84183e9afcc 100644
--- a/drivers/usb/gadget/function/uvc_video.c
Quoted 8 lines
Quoted 7 lines

+++ b/drivers/usb/gadget/function/uvc_video.c

drivers/usb/gadget/function/uvc_video.c +0 -0
@@ -268,6 +268,98 @@ static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req)
       return ret;
   }

  +/* This function must be called with video->req_lock held*/ +static int uvcg_video_usb_req_queue(struct uvc_video *video, +    struct usb_request *req, bool queue_to_ep) {

Brace on a new line please - same a few more times below

Done.

Quoted 46 lines
Quoted 1 line

+    bool is_bulk = video->max_payload_size;

empty line here

Quoted 44 lines

+    if (!video->is_enabled) {
+        uvc_video_free_request(req->context, video->ep);
+        return -ENODEV;
+    }
+    if (queue_to_ep) {
+        struct uvc_request *ureq = req->context;
+        /*
+         * With USB3 handling more requests at a higher speed, we can't +         * afford to generate an interrupt for every request. Decide to +         * interrupt:
+         *
+         * - When no more requests are available in the free queue, as +         *   this may be our last chance to refill the endpoint's +         *   request queue.
+         *
+         * - When this is request is the last request for the video +         *   buffer, as we want to start sending the next video buffer +         *   ASAP in case it doesn't get started already in the next +         *   iteration of this loop.
+         *
+         * - Four times over the length of the requests queue (as +         *   indicated by video->uvc_num_requests), as a trade-off +         *   between latency and interrupt load.
+        */
+        if (list_empty(&video->req_free) || ureq->last_buf || +            !(video->req_int_count %
+            DIV_ROUND_UP(video->uvc_num_requests, 4))) {
+            video->req_int_count = 0;
+            req->no_interrupt = 0;
+        } else {
+            req->no_interrupt = 1;
+        }
+        video->req_int_count++;
+        return uvcg_video_ep_queue(video, req);
+    } else {
+        /*
+        * If we're not queing to the ep, for isoc we're queing +        * to the req_ready list, otherwise req_free.
+        */
+        struct list_head *list =
+            is_bulk ? &video->req_free : &video->req_ready; +        list_add_tail(&req->list, list);
+    }
+    return 0;

Quoted 8 lines

drivers/usb/gadget/function/uvc_video.c static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req) · line 325

Quoted 7 lines
drivers/usb/gadget/function/uvc_video.c +7 -0
+}
+
+static int uvcg_video_ep_queue_zero_length(struct usb_request *req,
+    struct uvc_video *video) {
+    req->length = 0;
+    return uvcg_video_ep_queue(video, req);
+}

Not sure this is worth its own function

Removed the function.

Quoted 17 lines
Quoted 17 lines
drivers/usb/gadget/function/uvc_video.c +17 -0
+
+/* Must only be called from uvcg_video_enable - since after that we only want to
+ * queue requests to the endpoint from the uvc_video_complete complete handler.
+ * This function is needed in order to 'kick start' the flow of requests from
+ * gadget driver to the usb controller.
+ */
+static void uvc_video_ep_queue_initial_requests(struct uvc_video *video) {
+    struct usb_request *req = NULL;
+    unsigned long flags = 0;
+    unsigned int count = 0;
+    int ret = 0;
+    /* We only queue half of the free list since we still want to have
+     * some free usb_requests in the free list for the video_pump async_wq
+     * thread to encode uvc buffers into. Otherwise we could get into a
+     * situation where the free list does not have any usb requests to
+     * encode into - we always end up queueing 0 length requests to the
+     * end point.
Quoted 8 lines
Quoted 6 lines

+     */
+    unsigned half_list_size = video->uvc_num_requests / 2; +    spin_lock_irqsave(&video->req_lock, flags);
+    /* Take these requests off the free list and queue them all to the +     * endpoint. Since we queue the requests with the req_lock held, +     */

This comment seems to be incomplete? You also want an opening /* on its own line:

Apologies I think I missed out completing this comment I will send out another patch later.

Quoted 7 lines

/*
 * Multi line comments
 * look like this
 */

Done.

Quoted 8 lines
Quoted 8 lines

+    while (count < half_list_size) {
+        req = list_first_entry(&video->req_free, struct usb_request, +                    list);
+        list_del(&req->list);
+        ret = uvcg_video_ep_queue_zero_length(req, video); +        if (ret < 0) {
+            uvcg_queue_cancel(&video->queue, /*disconnect*/0); +            break;

Quoted 10 lines

drivers/usb/gadget/function/uvc_video.c

Quoted 6 lines

+        }
+        count++;
+    }
+    spin_unlock_irqrestore(&video->req_lock, flags);

drivers/usb/gadget/function/uvc_video.c +2 -0
+}
+

So if I'm understanding the new starting sequence right for an isoc endpoint there's an initial flight of half the requests (between 2 and 32) that are queued as zero length - the very first one to .complete() being re-queued as a zero length request before the workqueue is started and encodes data into the _other_ half of the requests which were left in req_free and putting them into req_ready. At that point the .complete()s being run start to pick requests off req_ready instead and they get sent out with data...does that sound right?

That is correct - the first half of number of usb requests allocated (2, 32) are queued at zero length initially. We’ll have half of the requests being sent to the ep in flight and half in the free list yes. queue_work will actually start with either uvc_v4l2_qbuf (uvc_v4l2.c) or at a zero length request being completed - whichever comes first.

Quoted 1 line

What are the implications of those initial 3-33 zero length requests? What kind of latency can that introduce to the start of the video stream?

At the worst, we’ll have a 32 x 125us(uframe period) = 4ms delay for the first frame of the uvc buffer stream being sent out to the usb controller. After that, since uvc buffers are typically queued at a much lower rate than usb requests being sent to the endpoint, we should be fine ? In my local testing, I don't see any delay observable to the naked eye.

Quoted 38 lines
Quoted 38 lines

  static void
  uvc_video_complete(struct usb_ep *ep, struct usb_request *req) {

drivers/usb/gadget/function/uvc_video.c +23 -1
@@ -276,6 +368,8 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
       struct uvc_video_queue *queue = &video->queue;
       struct uvc_buffer *last_buf = NULL;
       unsigned long flags;
+    bool is_bulk = video->max_payload_size;
+    int ret = 0;
         spin_lock_irqsave(&video->req_lock, flags);
       if (!video->is_enabled) {
@@ -329,7 +423,38 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
        * back to req_free
        */
       if (video->is_enabled) {
-        list_add_tail(&req->list, &video->req_free);
+        /*
+         * Here we check whether any request is available in the ready
+         * list. If it is, queue it to the ep and add the current
+         * usb_request to the req_free list - for video_pump to fill in.
+         * Otherwise, just use the current usb_request to queue a 0
+         * length request to the ep. Since we always add to the req_free
+         * list if we dequeue from the ready list, there will never
+         * be a situation where the req_free list is completely out of
+         * requests and cannot recover.
+         */
+        struct usb_request *to_queue = req;
+        to_queue->length = 0;
+        if (!list_empty(&video->req_ready)) {
+            to_queue = list_first_entry(&video->req_ready,
+                struct usb_request, list);
+            list_del(&to_queue->list);
+            /* Add it to the free list. */
+            list_add_tail(&req->list, &video->req_free);
+        }
+        /*
+         * Queue to the endpoint. The actual queueing to ep will
Quoted 8 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 451

Quoted 5 lines
drivers/usb/gadget/function/uvc_video.c +5 -0
+         * only happen on one thread - the async_wq for bulk endpoints
+         * and this thread for isoc endpoints.
+         */
+        ret = uvcg_video_usb_req_queue(video, to_queue,
+                           /*queue_to_ep*/!is_bulk);

In principle in-line comments are fine, but I don't think the parameter name is worth a comment

Done.

Quoted 10 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 457

Quoted 7 lines
drivers/usb/gadget/function/uvc_video.c +6 -0
+        if(ret < 0) {
+            uvcg_queue_cancel(queue, 0);
+        }
+        /* Queue work to the wq as well since its possible that a buffer
+         * may not have been completed.
+         */

The phrasing of this implies this is a bit of defensive programming, but if we don't queue to the wq here then doesn't that mean it'll never run?

I've updated the comment here - it is possible that we hit a situation where the in-flight usb requests may not be enough to completely encode a uvc buffer. In that case if we don't call queue_work, we'll never get the buffer marked as 'completed' and the buffer won't be returned to user-space. That'll prevent the dequeue->queue->dequeue loop and flow of buffers.

Quoted 62 lines
Quoted 62 lines
drivers/usb/gadget/function/uvc_video.c +2 -4
           queue_work(video->async_wq, &video->pump);
       } else {
           uvc_video_free_request(ureq, ep);
@@ -347,6 +472,7 @@ uvc_video_free_requests(struct uvc_video *video)
         INIT_LIST_HEAD(&video->ureqs);
       INIT_LIST_HEAD(&video->req_free);
+    INIT_LIST_HEAD(&video->req_ready);
       video->req_size = 0;
       return 0;
   }
@@ -424,8 +550,7 @@ static void uvcg_video_pump(struct work_struct *work)
       struct usb_request *req = NULL;
       struct uvc_buffer *buf;
       unsigned long flags;
-    bool buf_done;
-    int ret;
+    int ret = 0;
         while (true) {
           if (!video->ep->enabled)
@@ -454,7 +579,6 @@ static void uvcg_video_pump(struct work_struct *work)
             if (buf != NULL) {
               video->encode(req, video, buf);
-            buf_done = buf->state == UVC_BUF_STATE_DONE;
           } else if (!(queue->flags & UVC_QUEUE_DISCONNECTED) && !is_bulk) {
               /*
                * No video buffer available; the queue is still connected and
@@ -462,7 +586,6 @@ static void uvcg_video_pump(struct work_struct *work)
                * prevent missed ISOC transfers.
                */
               req->length = 0;
-            buf_done = false;
           } else {
               /*
                * Either the queue has been disconnected or no video buffer
@@ -473,45 +596,26 @@ static void uvcg_video_pump(struct work_struct *work)
               break;
           }

  -        /*
-         * With USB3 handling more requests at a higher speed, we can't -         * afford to generate an interrupt for every request. Decide to -         * interrupt:
-         *
-         * - When no more requests are available in the free queue, as -         *   this may be our last chance to refill the endpoint's -         *   request queue.
-         *
-         * - When this is request is the last request for the video -         *   buffer, as we want to start sending the next video buffer -         *   ASAP in case it doesn't get started already in the next -         *   iteration of this loop.
-         *
-         * - Four times over the length of the requests queue (as -         *   indicated by video->uvc_num_requests), as a trade-off -         *   between latency and interrupt load.
-         */
-        if (list_empty(&video->req_free) || buf_done ||
-            !(video->req_int_count %
-               DIV_ROUND_UP(video->uvc_num_requests, 4))) { -            video->req_int_count = 0;
-            req->no_interrupt = 0;
-        } else {

Quoted 8 lines
Quoted 7 lines

-            req->no_interrupt = 1;
-        }
-
-        /* Queue the USB request */
-        ret = uvcg_video_ep_queue(video, req); spin_unlock_irqrestore(&queue->irqlock, flags); +        /* Queue the USB request.*/

I think just drop this - it was always superfluous.

The uvcg_video_usb_req_queue function mentions that req_lock must be held while calling it - since its possible we might add to the req_ready list. We could say the function should hold req_lock only when the queue_to_ep parameter is false - but that doesn't seem as clean ?

Quoted 26 lines
Quoted 21 lines

+        spin_lock_irqsave(&video->req_lock, flags);
+        /* For bulk end points we queue from the worker thread +         * since we would preferably not want to wait on requests +         * to be ready, in the uvcg_video_complete() handler. +         * For isoc endpoints we add the request to the ready list +         * and only queue it to the endpoint from the complete handler. +         */
+        ret = uvcg_video_usb_req_queue(video, req, is_bulk); +        spin_unlock_irqrestore(&video->req_lock, flags);
+
          if (ret < 0) {
              uvcg_queue_cancel(queue, 0);
              break;
          }
  -        /* Endpoint now owns the request */
+        /* The request is owned by  the endpoint / ready list*/ req = NULL;
-        video->req_int_count++;
      }
        if (!req)

drivers/usb/gadget/function/uvc_video.c uvcg_video_disable(struct uvc_video *video) · line 673

Quoted 4 lines
drivers/usb/gadget/function/uvc_video.c +1 -1
@@ -567,7 +671,7 @@ uvcg_video_disable(struct uvc_video *video)
         spin_lock_irqsave(&video->req_lock, flags);
       /*
-     * Remove all uvc_reqeusts from ureqs with list_del_init
+     * Remove all uvc_requests from ureqs with list_del_init

This should get fixed in the earlier series.

Quoted 9 lines

drivers/usb/gadget/function/uvc_video.c uvcg_video_disable(struct uvc_video *video) · line 582

Quoted 8 lines
drivers/usb/gadget/function/uvc_video.c +0 -1
        * This lets uvc_video_free_request correctly identify
        * if the uvc_request is attached to a list or not when freeing
        * memory.
@@ -579,9 +683,13 @@ uvcg_video_disable(struct uvc_video *video)
           list_del(&req->list);
           uvc_video_free_request(req->context, video->ep);
       }
-

keep the empty line please

Done.

Quoted 5 lines

drivers/usb/gadget/function/uvc_video.c uvcg_video_disable(struct uvc_video *video) · line 689

Quoted 4 lines
drivers/usb/gadget/function/uvc_video.c +4 -0
+    list_for_each_entry_safe(req, temp, &video->req_ready, list) {
+        list_del(&req->list);
+        uvc_video_free_request(req->context, video->ep);
+    }

and one here too.

Done.

Thanks!

Quoted 19 lines
Quoted 19 lines
drivers/usb/gadget/function/uvc_video.c +1 -0
       INIT_LIST_HEAD(&video->ureqs);
       INIT_LIST_HEAD(&video->req_free);
+    INIT_LIST_HEAD(&video->req_ready);
       video->req_size = 0;
       spin_unlock_irqrestore(&video->req_lock, flags);
   @@ -635,7 +743,7 @@ int uvcg_video_enable(struct uvc_video *video)

        video->req_int_count = 0;
  -    queue_work(video->async_wq, &video->pump);
+    uvc_video_ep_queue_initial_requests(video);
        return ret;
  }

drivers/usb/gadget/function/uvc_video.c +1 -0
@@ -648,6 +756,7 @@ int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
       video->is_enabled = false;
       INIT_LIST_HEAD(&video->ureqs);
       INIT_LIST_HEAD(&video->req_free);
+    INIT_LIST_HEAD(&video->req_ready);
       spin_lock_init(&video->req_lock);
       INIT_WORK(&video->pump, uvcg_video_pump);

#25 lore raw

Re: [PATCH v3] usb:gadget:uvc Do not use worker thread to queue isoc usb requests

Quoted 190 lines · Jayant Chowdhary

Hi Dan,
Thank you for the comments.
I uploaded a new patch at https://lore.kernel.org/linux-usb/[email protected]/T/#u.

Quoted 33 lines · Dan Scally

Hi Jayant - thanks for the patch

Quoted 30 lines · Jayant Chowdhary

When we use an async work queue to perform the function of pumping usb requests to the usb controller, it is possible that amongst other factors, thread scheduling affects at what cadence we're able to pump requests. This could mean isoc usb requests miss their uframes - resulting in video stream flickers on the host device.

To avoid this, we make the async_wq thread only produce isoc usb_requests with uvc buffers encoded into them. The process of queueing to the endpoint is done by the uvc_video_complete() handler. In case no usb_requests are ready with encoded information, we just queue a zero length request to the endpoint from the complete handler.

For bulk endpoints the async_wq thread still queues usb requests to the endpoint.

---
  Based on top of
  https://lore.kernel.org/linux-usb/[email protected]/T/#t:
  v1->v2: Added self Signed-Off-by and addressed review comments v2->v3: Encode to usb requests in async_wq; queue to ep in complete handler for isoc transfers.

  drivers/usb/gadget/function/uvc.h       |   8 + drivers/usb/gadget/function/uvc_video.c | 187 +++++++++++++++++++-----

  2 files changed, 156 insertions(+), 39 deletions(-)
Quoted 12 lines
Quoted 10 lines
drivers/usb/gadget/function/uvc.h +0 -0
index e8d4c87f1e09..82c783410554 100644
@@ -105,7 +105,15 @@ struct uvc_video {

      bool is_enabled; /* tracks whether video stream is enabled */ unsigned int req_size;
      struct list_head ureqs; /* all uvc_requests allocated by uvc_video */ +
+    /* USB requests video pump thread can encode into*/

"USB requests that the video pump thread can encode into", and a space before the closing */ please (and the same a few more times below).

Done.

Quoted 13 lines
Quoted 13 lines

      struct list_head req_free;
+
+    /*
+     * USB requests video pump thread has already encoded into. These are +     * ready to be queued to the endpoint.
+     */
+    struct list_head req_ready;
      spinlock_t req_lock;
        unsigned int req_int_count;

drivers/usb/gadget/function/uvc_video.c +0 -1
index 53feb790a4c3..c84183e9afcc 100644
--- a/drivers/usb/gadget/function/uvc_video.c
Quoted 8 lines
Quoted 7 lines

+++ b/drivers/usb/gadget/function/uvc_video.c

drivers/usb/gadget/function/uvc_video.c +0 -0
@@ -268,6 +268,98 @@ static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req)
       return ret;
   }

  +/* This function must be called with video->req_lock held*/ +static int uvcg_video_usb_req_queue(struct uvc_video *video, +    struct usb_request *req, bool queue_to_ep) {

Brace on a new line please - same a few more times below

Done.

Quoted 46 lines
Quoted 1 line

+    bool is_bulk = video->max_payload_size;

empty line here

Quoted 44 lines

+    if (!video->is_enabled) {
+        uvc_video_free_request(req->context, video->ep);
+        return -ENODEV;
+    }
+    if (queue_to_ep) {
+        struct uvc_request *ureq = req->context;
+        /*
+         * With USB3 handling more requests at a higher speed, we can't +         * afford to generate an interrupt for every request. Decide to +         * interrupt:
+         *
+         * - When no more requests are available in the free queue, as +         *   this may be our last chance to refill the endpoint's +         *   request queue.
+         *
+         * - When this is request is the last request for the video +         *   buffer, as we want to start sending the next video buffer +         *   ASAP in case it doesn't get started already in the next +         *   iteration of this loop.
+         *
+         * - Four times over the length of the requests queue (as +         *   indicated by video->uvc_num_requests), as a trade-off +         *   between latency and interrupt load.
+        */
+        if (list_empty(&video->req_free) || ureq->last_buf || +            !(video->req_int_count %
+            DIV_ROUND_UP(video->uvc_num_requests, 4))) {
+            video->req_int_count = 0;
+            req->no_interrupt = 0;
+        } else {
+            req->no_interrupt = 1;
+        }
+        video->req_int_count++;
+        return uvcg_video_ep_queue(video, req);
+    } else {
+        /*
+        * If we're not queing to the ep, for isoc we're queing +        * to the req_ready list, otherwise req_free.
+        */
+        struct list_head *list =
+            is_bulk ? &video->req_free : &video->req_ready; +        list_add_tail(&req->list, list);
+    }
+    return 0;

Quoted 8 lines

drivers/usb/gadget/function/uvc_video.c static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req) · line 325

Quoted 7 lines
drivers/usb/gadget/function/uvc_video.c +7 -0
+}
+
+static int uvcg_video_ep_queue_zero_length(struct usb_request *req,
+    struct uvc_video *video) {
+    req->length = 0;
+    return uvcg_video_ep_queue(video, req);
+}

Not sure this is worth its own function

Removed the function.

Quoted 17 lines
Quoted 17 lines
drivers/usb/gadget/function/uvc_video.c +17 -0
+
+/* Must only be called from uvcg_video_enable - since after that we only want to
+ * queue requests to the endpoint from the uvc_video_complete complete handler.
+ * This function is needed in order to 'kick start' the flow of requests from
+ * gadget driver to the usb controller.
+ */
+static void uvc_video_ep_queue_initial_requests(struct uvc_video *video) {
+    struct usb_request *req = NULL;
+    unsigned long flags = 0;
+    unsigned int count = 0;
+    int ret = 0;
+    /* We only queue half of the free list since we still want to have
+     * some free usb_requests in the free list for the video_pump async_wq
+     * thread to encode uvc buffers into. Otherwise we could get into a
+     * situation where the free list does not have any usb requests to
+     * encode into - we always end up queueing 0 length requests to the
+     * end point.
Quoted 8 lines
Quoted 6 lines

+     */
+    unsigned half_list_size = video->uvc_num_requests / 2; +    spin_lock_irqsave(&video->req_lock, flags);
+    /* Take these requests off the free list and queue them all to the +     * endpoint. Since we queue the requests with the req_lock held, +     */

This comment seems to be incomplete? You also want an opening /* on its own line:

Apologies I think I missed out completing this comment I will send out another patch later.

Quoted 7 lines

/*
 * Multi line comments
 * look like this
 */

Done.

Quoted 8 lines
Quoted 8 lines

+    while (count < half_list_size) {
+        req = list_first_entry(&video->req_free, struct usb_request, +                    list);
+        list_del(&req->list);
+        ret = uvcg_video_ep_queue_zero_length(req, video); +        if (ret < 0) {
+            uvcg_queue_cancel(&video->queue, /*disconnect*/0); +            break;

Quoted 10 lines

drivers/usb/gadget/function/uvc_video.c

Quoted 6 lines

+        }
+        count++;
+    }
+    spin_unlock_irqrestore(&video->req_lock, flags);

drivers/usb/gadget/function/uvc_video.c +2 -0
+}
+

So if I'm understanding the new starting sequence right for an isoc endpoint there's an initial flight of half the requests (between 2 and 32) that are queued as zero length - the very first one to .complete() being re-queued as a zero length request before the workqueue is started and encodes data into the _other_ half of the requests which were left in req_free and putting them into req_ready. At that point the .complete()s being run start to pick requests off req_ready instead and they get sent out with data...does that sound right?

Quoted 8 lines

That is correct - the first half of number of usb requests allocated (2, 32) are queued at zero length initially. We’ll have half of the requests being sent to the ep in flight and half in the free list yes. queue_work will actually start with either uvc_v4l2_qbuf (uvc_v4l2.c) or at a zero length request being completed - whichever comes first.

Quoted 1 line

What are the implications of those initial 3-33 zero length requests? What kind of latency can that introduce to the start of the video stream?

At the worst, we’ll have a 32 x 125us(uframe period) = 4ms delay for the first frame of the uvc buffer stream being sent out to the usb controller. After that, since uvc buffers are typically queued at a much lower rate than usb requests being sent to the endpoint, we should be fine ? In my local testing, I don't see any delay observable to the naked eye.

You won't see the any effect in most cases, because what you actually lose is potential bandwidth. Let's think of a possible scenario; you have the hard limits of displaying a frame every 33ms. If the frame is small enough to be transmitted in the remaining 29ms with the active bandwidth tweaks in the usb endpoint (mult, burst), the streaming is totally fine.

The only downside is that you loose possible 4ms of processing time after the total frame did hit the host. But this only matters if the presentation time stamp (PTS) is actually set and would take an effect.

Regards,
Michael

Quoted 209 lines
Quoted 38 lines
Quoted 38 lines

  static void
  uvc_video_complete(struct usb_ep *ep, struct usb_request *req) {

drivers/usb/gadget/function/uvc_video.c +23 -1
@@ -276,6 +368,8 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
       struct uvc_video_queue *queue = &video->queue;
       struct uvc_buffer *last_buf = NULL;
       unsigned long flags;
+    bool is_bulk = video->max_payload_size;
+    int ret = 0;
         spin_lock_irqsave(&video->req_lock, flags);
       if (!video->is_enabled) {
@@ -329,7 +423,38 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
        * back to req_free
        */
       if (video->is_enabled) {
-        list_add_tail(&req->list, &video->req_free);
+        /*
+         * Here we check whether any request is available in the ready
+         * list. If it is, queue it to the ep and add the current
+         * usb_request to the req_free list - for video_pump to fill in.
+         * Otherwise, just use the current usb_request to queue a 0
+         * length request to the ep. Since we always add to the req_free
+         * list if we dequeue from the ready list, there will never
+         * be a situation where the req_free list is completely out of
+         * requests and cannot recover.
+         */
+        struct usb_request *to_queue = req;
+        to_queue->length = 0;
+        if (!list_empty(&video->req_ready)) {
+            to_queue = list_first_entry(&video->req_ready,
+                struct usb_request, list);
+            list_del(&to_queue->list);
+            /* Add it to the free list. */
+            list_add_tail(&req->list, &video->req_free);
+        }
+        /*
+         * Queue to the endpoint. The actual queueing to ep will
Quoted 8 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 451

Quoted 5 lines
drivers/usb/gadget/function/uvc_video.c +5 -0
+         * only happen on one thread - the async_wq for bulk endpoints
+         * and this thread for isoc endpoints.
+         */
+        ret = uvcg_video_usb_req_queue(video, to_queue,
+                           /*queue_to_ep*/!is_bulk);

In principle in-line comments are fine, but I don't think the parameter name is worth a comment

Done.

Quoted 10 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 457

Quoted 7 lines
drivers/usb/gadget/function/uvc_video.c +6 -0
+        if(ret < 0) {
+            uvcg_queue_cancel(queue, 0);
+        }
+        /* Queue work to the wq as well since its possible that a buffer
+         * may not have been completed.
+         */

The phrasing of this implies this is a bit of defensive programming, but if we don't queue to the wq here then doesn't that mean it'll never run?

I've updated the comment here - it is possible that we hit a situation where the in-flight usb requests may not be enough to completely encode a uvc buffer. In that case if we don't call queue_work, we'll never get the buffer marked as 'completed' and the buffer won't be returned to user-space. That'll prevent the dequeue->queue->dequeue loop and flow of buffers.

Quoted 62 lines
Quoted 62 lines
drivers/usb/gadget/function/uvc_video.c +2 -4
           queue_work(video->async_wq, &video->pump);
       } else {
           uvc_video_free_request(ureq, ep);
@@ -347,6 +472,7 @@ uvc_video_free_requests(struct uvc_video *video)
         INIT_LIST_HEAD(&video->ureqs);
       INIT_LIST_HEAD(&video->req_free);
+    INIT_LIST_HEAD(&video->req_ready);
       video->req_size = 0;
       return 0;
   }
@@ -424,8 +550,7 @@ static void uvcg_video_pump(struct work_struct *work)
       struct usb_request *req = NULL;
       struct uvc_buffer *buf;
       unsigned long flags;
-    bool buf_done;
-    int ret;
+    int ret = 0;
         while (true) {
           if (!video->ep->enabled)
@@ -454,7 +579,6 @@ static void uvcg_video_pump(struct work_struct *work)
             if (buf != NULL) {
               video->encode(req, video, buf);
-            buf_done = buf->state == UVC_BUF_STATE_DONE;
           } else if (!(queue->flags & UVC_QUEUE_DISCONNECTED) && !is_bulk) {
               /*
                * No video buffer available; the queue is still connected and
@@ -462,7 +586,6 @@ static void uvcg_video_pump(struct work_struct *work)
                * prevent missed ISOC transfers.
                */
               req->length = 0;
-            buf_done = false;
           } else {
               /*
                * Either the queue has been disconnected or no video buffer
@@ -473,45 +596,26 @@ static void uvcg_video_pump(struct work_struct *work)
               break;
           }

  -        /*
-         * With USB3 handling more requests at a higher speed, we can't -         * afford to generate an interrupt for every request. Decide to -         * interrupt:
-         *
-         * - When no more requests are available in the free queue, as -         *   this may be our last chance to refill the endpoint's -         *   request queue.
-         *
-         * - When this is request is the last request for the video -         *   buffer, as we want to start sending the next video buffer -         *   ASAP in case it doesn't get started already in the next -         *   iteration of this loop.
-         *
-         * - Four times over the length of the requests queue (as -         *   indicated by video->uvc_num_requests), as a trade-off -         *   between latency and interrupt load.
-         */
-        if (list_empty(&video->req_free) || buf_done ||
-            !(video->req_int_count %
-               DIV_ROUND_UP(video->uvc_num_requests, 4))) { -            video->req_int_count = 0;
-            req->no_interrupt = 0;
-        } else {

Quoted 8 lines
Quoted 7 lines

-            req->no_interrupt = 1;
-        }
-
-        /* Queue the USB request */
-        ret = uvcg_video_ep_queue(video, req); spin_unlock_irqrestore(&queue->irqlock, flags); +        /* Queue the USB request.*/

I think just drop this - it was always superfluous.

The uvcg_video_usb_req_queue function mentions that req_lock must be held while calling it - since its possible we might add to the req_ready list. We could say the function should hold req_lock only when the queue_to_ep parameter is false - but that doesn't seem as clean ?

Quoted 26 lines
Quoted 21 lines

+        spin_lock_irqsave(&video->req_lock, flags);
+        /* For bulk end points we queue from the worker thread +         * since we would preferably not want to wait on requests +         * to be ready, in the uvcg_video_complete() handler. +         * For isoc endpoints we add the request to the ready list +         * and only queue it to the endpoint from the complete handler. +         */
+        ret = uvcg_video_usb_req_queue(video, req, is_bulk); +        spin_unlock_irqrestore(&video->req_lock, flags);
+
          if (ret < 0) {
              uvcg_queue_cancel(queue, 0);
              break;
          }
  -        /* Endpoint now owns the request */
+        /* The request is owned by  the endpoint / ready list*/ req = NULL;
-        video->req_int_count++;
      }
        if (!req)

drivers/usb/gadget/function/uvc_video.c uvcg_video_disable(struct uvc_video *video) · line 673

Quoted 4 lines
drivers/usb/gadget/function/uvc_video.c +1 -1
@@ -567,7 +671,7 @@ uvcg_video_disable(struct uvc_video *video)
         spin_lock_irqsave(&video->req_lock, flags);
       /*
-     * Remove all uvc_reqeusts from ureqs with list_del_init
+     * Remove all uvc_requests from ureqs with list_del_init

This should get fixed in the earlier series.

Quoted 9 lines

drivers/usb/gadget/function/uvc_video.c uvcg_video_disable(struct uvc_video *video) · line 582

Quoted 8 lines
drivers/usb/gadget/function/uvc_video.c +0 -1
        * This lets uvc_video_free_request correctly identify
        * if the uvc_request is attached to a list or not when freeing
        * memory.
@@ -579,9 +683,13 @@ uvcg_video_disable(struct uvc_video *video)
           list_del(&req->list);
           uvc_video_free_request(req->context, video->ep);
       }
-

keep the empty line please

Done.

Quoted 5 lines

drivers/usb/gadget/function/uvc_video.c uvcg_video_disable(struct uvc_video *video) · line 689

Quoted 4 lines
drivers/usb/gadget/function/uvc_video.c +4 -0
+    list_for_each_entry_safe(req, temp, &video->req_ready, list) {
+        list_del(&req->list);
+        uvc_video_free_request(req->context, video->ep);
+    }

and one here too.

Done.

Thanks!

Quoted 19 lines
Quoted 19 lines
drivers/usb/gadget/function/uvc_video.c +1 -0
       INIT_LIST_HEAD(&video->ureqs);
       INIT_LIST_HEAD(&video->req_free);
+    INIT_LIST_HEAD(&video->req_ready);
       video->req_size = 0;
       spin_unlock_irqrestore(&video->req_lock, flags);
   @@ -635,7 +743,7 @@ int uvcg_video_enable(struct uvc_video *video)

        video->req_int_count = 0;
  -    queue_work(video->async_wq, &video->pump);
+    uvc_video_ep_queue_initial_requests(video);
        return ret;
  }

drivers/usb/gadget/function/uvc_video.c +1 -0
@@ -648,6 +756,7 @@ int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
       video->is_enabled = false;
       INIT_LIST_HEAD(&video->ureqs);
       INIT_LIST_HEAD(&video->req_free);
+    INIT_LIST_HEAD(&video->req_ready);
       spin_lock_init(&video->req_lock);
       INIT_WORK(&video->pump, uvcg_video_pump);

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#26 lore raw

Re: [PATCH v3] usb:gadget:uvc Do not use worker thread to queue isoc usb requests

Hi Michael,

Quoted 200 lines · Michael Grzeschik
Quoted 190 lines · Jayant Chowdhary

Hi Dan,
Thank you for the comments.
I uploaded a new patch at https://lore.kernel.org/linux-usb/[email protected]/T/#u.

Quoted 33 lines · Dan Scally

Hi Jayant - thanks for the patch

Quoted 30 lines · Jayant Chowdhary

When we use an async work queue to perform the function of pumping usb requests to the usb controller, it is possible that amongst other factors, thread scheduling affects at what cadence we're able to pump requests. This could mean isoc usb requests miss their uframes - resulting in video stream flickers on the host device.

To avoid this, we make the async_wq thread only produce isoc usb_requests with uvc buffers encoded into them. The process of queueing to the endpoint is done by the uvc_video_complete() handler. In case no usb_requests are ready with encoded information, we just queue a zero length request to the endpoint from the complete handler.

For bulk endpoints the async_wq thread still queues usb requests to the endpoint.

---
  Based on top of
  https://lore.kernel.org/linux-usb/[email protected]/T/#t:
  v1->v2: Added self Signed-Off-by and addressed review comments v2->v3: Encode to usb requests in async_wq; queue to ep in complete handler for isoc transfers.

  drivers/usb/gadget/function/uvc.h       |   8 + drivers/usb/gadget/function/uvc_video.c | 187 +++++++++++++++++++-----

  2 files changed, 156 insertions(+), 39 deletions(-)
Quoted 12 lines
Quoted 10 lines
drivers/usb/gadget/function/uvc.h +0 -0
index e8d4c87f1e09..82c783410554 100644
@@ -105,7 +105,15 @@ struct uvc_video {

      bool is_enabled; /* tracks whether video stream is enabled */ unsigned int req_size;
      struct list_head ureqs; /* all uvc_requests allocated by uvc_video */ +
+    /* USB requests video pump thread can encode into*/

"USB requests that the video pump thread can encode into", and a space before the closing */ please (and the same a few more times below).

Done.

Quoted 13 lines
Quoted 13 lines

      struct list_head req_free;
+
+    /*
+     * USB requests video pump thread has already encoded into. These are +     * ready to be queued to the endpoint.
+     */
+    struct list_head req_ready;
      spinlock_t req_lock;
        unsigned int req_int_count;

drivers/usb/gadget/function/uvc_video.c +0 -1
index 53feb790a4c3..c84183e9afcc 100644
--- a/drivers/usb/gadget/function/uvc_video.c
Quoted 8 lines
Quoted 7 lines

+++ b/drivers/usb/gadget/function/uvc_video.c

drivers/usb/gadget/function/uvc_video.c +0 -0
@@ -268,6 +268,98 @@ static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req)
       return ret;
   }

  +/* This function must be called with video->req_lock held*/ +static int uvcg_video_usb_req_queue(struct uvc_video *video, +    struct usb_request *req, bool queue_to_ep) {

Brace on a new line please - same a few more times below

Done.

Quoted 46 lines
Quoted 1 line

+    bool is_bulk = video->max_payload_size;

empty line here

Quoted 44 lines

+    if (!video->is_enabled) {
+        uvc_video_free_request(req->context, video->ep);
+        return -ENODEV;
+    }
+    if (queue_to_ep) {
+        struct uvc_request *ureq = req->context;
+        /*
+         * With USB3 handling more requests at a higher speed, we can't +         * afford to generate an interrupt for every request. Decide to +         * interrupt:
+         *
+         * - When no more requests are available in the free queue, as +         *   this may be our last chance to refill the endpoint's +         *   request queue.
+         *
+         * - When this is request is the last request for the video +         *   buffer, as we want to start sending the next video buffer +         *   ASAP in case it doesn't get started already in the next +         *   iteration of this loop.
+         *
+         * - Four times over the length of the requests queue (as +         *   indicated by video->uvc_num_requests), as a trade-off +         *   between latency and interrupt load.
+        */
+        if (list_empty(&video->req_free) || ureq->last_buf || +            !(video->req_int_count %
+            DIV_ROUND_UP(video->uvc_num_requests, 4))) {
+            video->req_int_count = 0;
+            req->no_interrupt = 0;
+        } else {
+            req->no_interrupt = 1;
+        }
+        video->req_int_count++;
+        return uvcg_video_ep_queue(video, req);
+    } else {
+        /*
+        * If we're not queing to the ep, for isoc we're queing +        * to the req_ready list, otherwise req_free.
+        */
+        struct list_head *list =
+            is_bulk ? &video->req_free : &video->req_ready; +        list_add_tail(&req->list, list);
+    }
+    return 0;

Quoted 8 lines

drivers/usb/gadget/function/uvc_video.c static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req) · line 325

Quoted 7 lines
drivers/usb/gadget/function/uvc_video.c +7 -0
+}
+
+static int uvcg_video_ep_queue_zero_length(struct usb_request *req,
+    struct uvc_video *video) {
+    req->length = 0;
+    return uvcg_video_ep_queue(video, req);
+}

Not sure this is worth its own function

Removed the function.

Quoted 17 lines
Quoted 17 lines
drivers/usb/gadget/function/uvc_video.c +17 -0
+
+/* Must only be called from uvcg_video_enable - since after that we only want to
+ * queue requests to the endpoint from the uvc_video_complete complete handler.
+ * This function is needed in order to 'kick start' the flow of requests from
+ * gadget driver to the usb controller.
+ */
+static void uvc_video_ep_queue_initial_requests(struct uvc_video *video) {
+    struct usb_request *req = NULL;
+    unsigned long flags = 0;
+    unsigned int count = 0;
+    int ret = 0;
+    /* We only queue half of the free list since we still want to have
+     * some free usb_requests in the free list for the video_pump async_wq
+     * thread to encode uvc buffers into. Otherwise we could get into a
+     * situation where the free list does not have any usb requests to
+     * encode into - we always end up queueing 0 length requests to the
+     * end point.
Quoted 8 lines
Quoted 6 lines

+     */
+    unsigned half_list_size = video->uvc_num_requests / 2; +    spin_lock_irqsave(&video->req_lock, flags);
+    /* Take these requests off the free list and queue them all to the +     * endpoint. Since we queue the requests with the req_lock held, +     */

This comment seems to be incomplete? You also want an opening /* on its own line:

Apologies I think I missed out completing this comment I will send out another patch later.

Quoted 7 lines

/*
 * Multi line comments
 * look like this
 */

Done.

Quoted 8 lines
Quoted 8 lines

+    while (count < half_list_size) {
+        req = list_first_entry(&video->req_free, struct usb_request, +                    list);
+        list_del(&req->list);
+        ret = uvcg_video_ep_queue_zero_length(req, video); +        if (ret < 0) {
+            uvcg_queue_cancel(&video->queue, /*disconnect*/0); +            break;

Quoted 10 lines

drivers/usb/gadget/function/uvc_video.c

Quoted 6 lines

+        }
+        count++;
+    }
+    spin_unlock_irqrestore(&video->req_lock, flags);

drivers/usb/gadget/function/uvc_video.c +2 -0
+}
+

So if I'm understanding the new starting sequence right for an isoc endpoint there's an initial flight of half the requests (between 2 and 32) that are queued as zero length - the very first one to .complete() being re-queued as a zero length request before the workqueue is started and encodes data into the _other_ half of the requests which were left in req_free and putting them into req_ready. At that point the .complete()s being run start to pick requests off req_ready instead and they get sent out with data...does that sound right?

Quoted 8 lines

That is correct - the first half of number of usb requests allocated (2, 32) are queued at zero length initially. We’ll have half of the requests being sent to the ep in flight and half in the free list yes. queue_work will actually start with either uvc_v4l2_qbuf (uvc_v4l2.c) or at a zero length request being completed - whichever comes first.

Quoted 1 line

What are the implications of those initial 3-33 zero length requests? What kind of latency can that introduce to the start of the video stream?

At the worst, we’ll have  a 32 x 125us(uframe period) = 4ms  delay for the first frame of the uvc buffer stream being sent out to the usb controller. After that, since uvc buffers are typically queued at a much lower rate than usb requests being sent to the endpoint, we should be fine ? In my local testing, I don't see any delay observable to the naked eye.

Quoted 7 lines

You won't see the any effect in most cases, because what you actually lose is potential bandwidth. Let's think of a possible scenario; you have the hard limits of displaying a frame every 33ms. If the frame is small enough to be transmitted in the remaining 29ms with the active bandwidth tweaks in the usb endpoint (mult, burst), the streaming is totally fine.

Would we really lose bandwidth as long as the time taken to encode a frame and send it to the USB endpoint is really <= 33ms (in that case we can't meet the 30 fps requirement anyway)

If we consider a 30fps stream and picture the timeline. The host expects the first frame at 33ms, second at 66ms and so on. We start encoding the first frame at A- 4ms. Let's say we take 32ms to encode it and send it. Its received by the host at B- 36ms. The next frame starts encoding and again takes 32ms. The host gets it at point C which is at 68ms in the timeline. If we keep following the train, we see that the difference in the skew keeps decreasing and we eventually start hitting the expected timeline.

0ms  4ms                  33ms  36ms          66ms  68ms               99ms 100ms
|----|---------------------|-----|-------------|----|--------------------|--|
     A<------------------------->B<----------------->C<--------------------->D

Thanks,
Jayant

Quoted 217 lines

The only downside is that you loose possible 4ms of processing time after the total frame did hit the host. But this only matters if the presentation time stamp (PTS) is actually set and would take an effect.

Regards,
Michael

Quoted 209 lines
Quoted 38 lines
Quoted 38 lines

  static void
  uvc_video_complete(struct usb_ep *ep, struct usb_request *req) {

drivers/usb/gadget/function/uvc_video.c +23 -1
@@ -276,6 +368,8 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
       struct uvc_video_queue *queue = &video->queue;
       struct uvc_buffer *last_buf = NULL;
       unsigned long flags;
+    bool is_bulk = video->max_payload_size;
+    int ret = 0;
         spin_lock_irqsave(&video->req_lock, flags);
       if (!video->is_enabled) {
@@ -329,7 +423,38 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
        * back to req_free
        */
       if (video->is_enabled) {
-        list_add_tail(&req->list, &video->req_free);
+        /*
+         * Here we check whether any request is available in the ready
+         * list. If it is, queue it to the ep and add the current
+         * usb_request to the req_free list - for video_pump to fill in.
+         * Otherwise, just use the current usb_request to queue a 0
+         * length request to the ep. Since we always add to the req_free
+         * list if we dequeue from the ready list, there will never
+         * be a situation where the req_free list is completely out of
+         * requests and cannot recover.
+         */
+        struct usb_request *to_queue = req;
+        to_queue->length = 0;
+        if (!list_empty(&video->req_ready)) {
+            to_queue = list_first_entry(&video->req_ready,
+                struct usb_request, list);
+            list_del(&to_queue->list);
+            /* Add it to the free list. */
+            list_add_tail(&req->list, &video->req_free);
+        }
+        /*
+         * Queue to the endpoint. The actual queueing to ep will
Quoted 8 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 451

Quoted 5 lines
drivers/usb/gadget/function/uvc_video.c +5 -0
+         * only happen on one thread - the async_wq for bulk endpoints
+         * and this thread for isoc endpoints.
+         */
+        ret = uvcg_video_usb_req_queue(video, to_queue,
+                           /*queue_to_ep*/!is_bulk);

In principle in-line comments are fine, but I don't think the parameter name is worth a comment

Done.

Quoted 10 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 457

Quoted 7 lines
drivers/usb/gadget/function/uvc_video.c +6 -0
+        if(ret < 0) {
+            uvcg_queue_cancel(queue, 0);
+        }
+        /* Queue work to the wq as well since its possible that a buffer
+         * may not have been completed.
+         */

The phrasing of this implies this is a bit of defensive programming, but if we don't queue to the wq here then doesn't that mean it'll never run?

I've updated the comment here - it is possible that we hit a situation where the in-flight usb requests may not be enough to completely encode a uvc buffer. In that case if we don't call queue_work, we'll never get the buffer marked as 'completed' and the buffer won't be returned to user-space. That'll prevent the dequeue->queue->dequeue loop and flow of buffers.

Quoted 62 lines
Quoted 62 lines
drivers/usb/gadget/function/uvc_video.c +2 -4
           queue_work(video->async_wq, &video->pump);
       } else {
           uvc_video_free_request(ureq, ep);
@@ -347,6 +472,7 @@ uvc_video_free_requests(struct uvc_video *video)
         INIT_LIST_HEAD(&video->ureqs);
       INIT_LIST_HEAD(&video->req_free);
+    INIT_LIST_HEAD(&video->req_ready);
       video->req_size = 0;
       return 0;
   }
@@ -424,8 +550,7 @@ static void uvcg_video_pump(struct work_struct *work)
       struct usb_request *req = NULL;
       struct uvc_buffer *buf;
       unsigned long flags;
-    bool buf_done;
-    int ret;
+    int ret = 0;
         while (true) {
           if (!video->ep->enabled)
@@ -454,7 +579,6 @@ static void uvcg_video_pump(struct work_struct *work)
             if (buf != NULL) {
               video->encode(req, video, buf);
-            buf_done = buf->state == UVC_BUF_STATE_DONE;
           } else if (!(queue->flags & UVC_QUEUE_DISCONNECTED) && !is_bulk) {
               /*
                * No video buffer available; the queue is still connected and
@@ -462,7 +586,6 @@ static void uvcg_video_pump(struct work_struct *work)
                * prevent missed ISOC transfers.
                */
               req->length = 0;
-            buf_done = false;
           } else {
               /*
                * Either the queue has been disconnected or no video buffer
@@ -473,45 +596,26 @@ static void uvcg_video_pump(struct work_struct *work)
               break;
           }

  -        /*
-         * With USB3 handling more requests at a higher speed, we can't -         * afford to generate an interrupt for every request. Decide to -         * interrupt:
-         *
-         * - When no more requests are available in the free queue, as -         *   this may be our last chance to refill the endpoint's -         *   request queue.
-         *
-         * - When this is request is the last request for the video -         *   buffer, as we want to start sending the next video buffer -         *   ASAP in case it doesn't get started already in the next -         *   iteration of this loop.
-         *
-         * - Four times over the length of the requests queue (as -         *   indicated by video->uvc_num_requests), as a trade-off -         *   between latency and interrupt load.
-         */
-        if (list_empty(&video->req_free) || buf_done ||
-            !(video->req_int_count %
-               DIV_ROUND_UP(video->uvc_num_requests, 4))) { -            video->req_int_count = 0;
-            req->no_interrupt = 0;
-        } else {

Quoted 8 lines
Quoted 7 lines

-            req->no_interrupt = 1;
-        }
-
-        /* Queue the USB request */
-        ret = uvcg_video_ep_queue(video, req); spin_unlock_irqrestore(&queue->irqlock, flags); +        /* Queue the USB request.*/

I think just drop this - it was always superfluous.

The uvcg_video_usb_req_queue function mentions that req_lock must be held while calling it - since its possible we might add to the req_ready list. We could say the function should hold req_lock only when the queue_to_ep parameter is false - but that doesn't seem as clean ?

Quoted 26 lines
Quoted 21 lines

+        spin_lock_irqsave(&video->req_lock, flags);
+        /* For bulk end points we queue from the worker thread +         * since we would preferably not want to wait on requests +         * to be ready, in the uvcg_video_complete() handler. +         * For isoc endpoints we add the request to the ready list +         * and only queue it to the endpoint from the complete handler. +         */
+        ret = uvcg_video_usb_req_queue(video, req, is_bulk); +        spin_unlock_irqrestore(&video->req_lock, flags);
+
          if (ret < 0) {
              uvcg_queue_cancel(queue, 0);
              break;
          }
  -        /* Endpoint now owns the request */
+        /* The request is owned by  the endpoint / ready list*/ req = NULL;
-        video->req_int_count++;
      }
        if (!req)

drivers/usb/gadget/function/uvc_video.c uvcg_video_disable(struct uvc_video *video) · line 673

Quoted 4 lines
drivers/usb/gadget/function/uvc_video.c +1 -1
@@ -567,7 +671,7 @@ uvcg_video_disable(struct uvc_video *video)
         spin_lock_irqsave(&video->req_lock, flags);
       /*
-     * Remove all uvc_reqeusts from ureqs with list_del_init
+     * Remove all uvc_requests from ureqs with list_del_init

This should get fixed in the earlier series.

Quoted 9 lines

drivers/usb/gadget/function/uvc_video.c uvcg_video_disable(struct uvc_video *video) · line 582

Quoted 8 lines
drivers/usb/gadget/function/uvc_video.c +0 -1
        * This lets uvc_video_free_request correctly identify
        * if the uvc_request is attached to a list or not when freeing
        * memory.
@@ -579,9 +683,13 @@ uvcg_video_disable(struct uvc_video *video)
           list_del(&req->list);
           uvc_video_free_request(req->context, video->ep);
       }
-

keep the empty line please

Done.

Quoted 5 lines

drivers/usb/gadget/function/uvc_video.c uvcg_video_disable(struct uvc_video *video) · line 689

Quoted 4 lines
drivers/usb/gadget/function/uvc_video.c +4 -0
+    list_for_each_entry_safe(req, temp, &video->req_ready, list) {
+        list_del(&req->list);
+        uvc_video_free_request(req->context, video->ep);
+    }

and one here too.

Done.

Thanks!

Quoted 19 lines
Quoted 19 lines
drivers/usb/gadget/function/uvc_video.c +1 -0
       INIT_LIST_HEAD(&video->ureqs);
       INIT_LIST_HEAD(&video->req_free);
+    INIT_LIST_HEAD(&video->req_ready);
       video->req_size = 0;
       spin_unlock_irqrestore(&video->req_lock, flags);
   @@ -635,7 +743,7 @@ int uvcg_video_enable(struct uvc_video *video)

        video->req_int_count = 0;
  -    queue_work(video->async_wq, &video->pump);
+    uvc_video_ep_queue_initial_requests(video);
        return ret;
  }

drivers/usb/gadget/function/uvc_video.c +1 -0
@@ -648,6 +756,7 @@ int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
       video->is_enabled = false;
       INIT_LIST_HEAD(&video->ureqs);
       INIT_LIST_HEAD(&video->req_free);
+    INIT_LIST_HEAD(&video->req_ready);
       spin_lock_init(&video->req_lock);
       INIT_WORK(&video->pump, uvcg_video_pump);

#27 lore raw

Re: [PATCH v3] usb:gadget:uvc Do not use worker thread to queue isoc usb requests

In reply to Jayant Chowdhary

Hi Jayant

Quoted 180 lines · Jayant Chowdhary

Hi Dan,
Thank you for the comments.
I uploaded a new patch at https://lore.kernel.org/linux-usb/[email protected]/T/#u.

Quoted 33 lines · Dan Scally

Hi Jayant - thanks for the patch

Quoted 30 lines · Jayant Chowdhary

When we use an async work queue to perform the function of pumping usb requests to the usb controller, it is possible that amongst other factors, thread scheduling affects at what cadence we're able to pump requests. This could mean isoc usb requests miss their uframes - resulting in video stream flickers on the host device.

To avoid this, we make the async_wq thread only produce isoc usb_requests with uvc buffers encoded into them. The process of queueing to the endpoint is done by the uvc_video_complete() handler. In case no usb_requests are ready with encoded information, we just queue a zero length request to the endpoint from the complete handler.

For bulk endpoints the async_wq thread still queues usb requests to the endpoint.

---
  Based on top of
  https://lore.kernel.org/linux-usb/[email protected]/T/#t:
  v1->v2: Added self Signed-Off-by and addressed review comments v2->v3: Encode to usb requests in async_wq; queue to ep in complete handler for isoc transfers.

   drivers/usb/gadget/function/uvc.h       |   8 +
   drivers/usb/gadget/function/uvc_video.c | 187 +++++++++++++++++++-----
   2 files changed, 156 insertions(+), 39 deletions(-)
Quoted 11 lines

drivers/usb/gadget/function/uvc.h struct uvc_video { · line 109

Quoted 10 lines
drivers/usb/gadget/function/uvc.h +2 -0
index e8d4c87f1e09..82c783410554 100644
@@ -105,7 +105,15 @@ struct uvc_video {
       bool is_enabled; /* tracks whether video stream is enabled */
       unsigned int req_size;
       struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
+
+    /* USB requests video pump thread can encode into*/

"USB requests that the video pump thread can encode into", and a space before the closing */ please (and the same a few more times below).

Done.

Quoted 12 lines
Quoted 12 lines
drivers/usb/gadget/function/uvc.h +6 -0
       struct list_head req_free;
+
+    /*
+     * USB requests video pump thread has already encoded into. These are
+     * ready to be queued to the endpoint.
+     */
+    struct list_head req_ready;
       spinlock_t req_lock;
         unsigned int req_int_count;
drivers/usb/gadget/function/uvc_video.c +0 -1
index 53feb790a4c3..c84183e9afcc 100644
--- a/drivers/usb/gadget/function/uvc_video.c
Quoted 8 lines

drivers/usb/gadget/function/uvc_video.c static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req) · line 272

Quoted 7 lines

+++ b/drivers/usb/gadget/function/uvc_video.c

drivers/usb/gadget/function/uvc_video.c +2 -0
@@ -268,6 +268,98 @@ static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req)
       return ret;
   }
   +/* This function must be called with video->req_lock held*/
+static int uvcg_video_usb_req_queue(struct uvc_video *video,
+    struct usb_request *req, bool queue_to_ep) {

Brace on a new line please - same a few more times below

Done.

Quoted 46 lines

drivers/usb/gadget/function/uvc_video.c static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req) · line 273

Quoted 1 line
drivers/usb/gadget/function/uvc_video.c +1 -0
+    bool is_bulk = video->max_payload_size;

empty line here

Quoted 44 lines
drivers/usb/gadget/function/uvc_video.c +44 -0
+    if (!video->is_enabled) {
+        uvc_video_free_request(req->context, video->ep);
+        return -ENODEV;
+    }
+    if (queue_to_ep) {
+        struct uvc_request *ureq = req->context;
+        /*
+         * With USB3 handling more requests at a higher speed, we can't
+         * afford to generate an interrupt for every request. Decide to
+         * interrupt:
+         *
+         * - When no more requests are available in the free queue, as
+         *   this may be our last chance to refill the endpoint's
+         *   request queue.
+         *
+         * - When this is request is the last request for the video
+         *   buffer, as we want to start sending the next video buffer
+         *   ASAP in case it doesn't get started already in the next
+         *   iteration of this loop.
+         *
+         * - Four times over the length of the requests queue (as
+         *   indicated by video->uvc_num_requests), as a trade-off
+         *   between latency and interrupt load.
+        */
+        if (list_empty(&video->req_free) || ureq->last_buf ||
+            !(video->req_int_count %
+            DIV_ROUND_UP(video->uvc_num_requests, 4))) {
+            video->req_int_count = 0;
+            req->no_interrupt = 0;
+        } else {
+            req->no_interrupt = 1;
+        }
+        video->req_int_count++;
+        return uvcg_video_ep_queue(video, req);
+    } else {
+        /*
+        * If we're not queing to the ep, for isoc we're queing
+        * to the req_ready list, otherwise req_free.
+        */
+        struct list_head *list =
+            is_bulk ? &video->req_free : &video->req_ready;
+        list_add_tail(&req->list, list);
+    }
+    return 0;
Quoted 8 lines

drivers/usb/gadget/function/uvc_video.c static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req) · line 325

Quoted 7 lines
drivers/usb/gadget/function/uvc_video.c +7 -0
+}
+
+static int uvcg_video_ep_queue_zero_length(struct usb_request *req,
+    struct uvc_video *video) {
+    req->length = 0;
+    return uvcg_video_ep_queue(video, req);
+}

Not sure this is worth its own function

Removed the function.

Quoted 16 lines
Quoted 16 lines
drivers/usb/gadget/function/uvc_video.c +16 -0
+
+/* Must only be called from uvcg_video_enable - since after that we only want to
+ * queue requests to the endpoint from the uvc_video_complete complete handler.
+ * This function is needed in order to 'kick start' the flow of requests from
+ * gadget driver to the usb controller.
+ */
+static void uvc_video_ep_queue_initial_requests(struct uvc_video *video) {
+    struct usb_request *req = NULL;
+    unsigned long flags = 0;
+    unsigned int count = 0;
+    int ret = 0;
+    /* We only queue half of the free list since we still want to have
+     * some free usb_requests in the free list for the video_pump async_wq
+     * thread to encode uvc buffers into. Otherwise we could get into a
+     * situation where the free list does not have any usb requests to
+     * encode into - we always end up queueing 0 length requests to the
Quoted 8 lines

drivers/usb/gadget/function/uvc_video.c static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req) · line 348

Quoted 7 lines
drivers/usb/gadget/function/uvc_video.c +7 -0
+     * end point.
+     */
+    unsigned half_list_size = video->uvc_num_requests / 2;
+    spin_lock_irqsave(&video->req_lock, flags);
+    /* Take these requests off the free list and queue them all to the
+     * endpoint. Since we queue the requests with the req_lock held,
+     */

This comment seems to be incomplete? You also want an opening /* on its own line:

Apologies I think I missed out completing this comment I will send out another patch later.

Quoted 6 lines

/*
 * Multi line comments
 * look like this
 */

Done.

Quoted 7 lines
Quoted 7 lines
drivers/usb/gadget/function/uvc_video.c +7 -0
+    while (count < half_list_size) {
+        req = list_first_entry(&video->req_free, struct usb_request,
+                    list);
+        list_del(&req->list);
+        ret = uvcg_video_ep_queue_zero_length(req, video);
+        if (ret < 0) {
+            uvcg_queue_cancel(&video->queue, /*disconnect*/0);
Quoted 10 lines

drivers/usb/gadget/function/uvc_video.c static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req) · line 362

Quoted 7 lines
drivers/usb/gadget/function/uvc_video.c +7 -0
+            break;
+        }
+        count++;
+    }
+    spin_unlock_irqrestore(&video->req_lock, flags);
+}
+

So if I'm understanding the new starting sequence right for an isoc endpoint there's an initial flight of half the requests (between 2 and 32) that are queued as zero length - the very first one to .complete() being re-queued as a zero length request before the workqueue is started and encodes data into the _other_ half of the requests which were left in req_free and putting them into req_ready. At that point the .complete()s being run start to pick requests off req_ready instead and they get sent out with data...does that sound right?

Quoted 6 lines

That is correct - the first half of number of usb requests allocated (2, 32) are queued at zero length initially. We’ll have half of the requests being sent to the ep in flight and half in the free list yes. queue_work will actually start with either uvc_v4l2_qbuf (uvc_v4l2.c) or at a zero length request being completed - whichever comes first.

Quoted 1 line

What are the implications of those initial 3-33 zero length requests? What kind of latency can that introduce to the start of the video stream?

At the worst, we’ll have a 32 x 125us(uframe period) = 4ms delay for the first frame of the uvc buffer stream being sent out to the usb controller. After that, since uvc buffers are typically queued at a much lower rate than usb requests being sent to the endpoint, we should be fine ?

I think that the 'ongoing' stream should be fine using this method yes, though if possible I'd like to avoid introducing the delay to the first frame. Do you know if there's a simple way to remove it? I recognise the delay is small so I don't think it's necessarily a dealbreaker but it would be nice if we could avoid it.

Quoted 130 lines

In my local testing, I don't see any delay observable to the naked eye.

Quoted 36 lines
Quoted 36 lines
drivers/usb/gadget/function/uvc_video.c +22 -1
   static void
   uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
   {
@@ -276,6 +368,8 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
       struct uvc_video_queue *queue = &video->queue;
       struct uvc_buffer *last_buf = NULL;
       unsigned long flags;
+    bool is_bulk = video->max_payload_size;
+    int ret = 0;
         spin_lock_irqsave(&video->req_lock, flags);
       if (!video->is_enabled) {
@@ -329,7 +423,38 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
        * back to req_free
        */
       if (video->is_enabled) {
-        list_add_tail(&req->list, &video->req_free);
+        /*
+         * Here we check whether any request is available in the ready
+         * list. If it is, queue it to the ep and add the current
+         * usb_request to the req_free list - for video_pump to fill in.
+         * Otherwise, just use the current usb_request to queue a 0
+         * length request to the ep. Since we always add to the req_free
+         * list if we dequeue from the ready list, there will never
+         * be a situation where the req_free list is completely out of
+         * requests and cannot recover.
+         */
+        struct usb_request *to_queue = req;
+        to_queue->length = 0;
+        if (!list_empty(&video->req_ready)) {
+            to_queue = list_first_entry(&video->req_ready,
+                struct usb_request, list);
+            list_del(&to_queue->list);
+            /* Add it to the free list. */
+            list_add_tail(&req->list, &video->req_free);
+        }
+        /*
Quoted 8 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 451

Quoted 6 lines
drivers/usb/gadget/function/uvc_video.c +6 -0
+         * Queue to the endpoint. The actual queueing to ep will
+         * only happen on one thread - the async_wq for bulk endpoints
+         * and this thread for isoc endpoints.
+         */
+        ret = uvcg_video_usb_req_queue(video, to_queue,
+                           /*queue_to_ep*/!is_bulk);

In principle in-line comments are fine, but I don't think the parameter name is worth a comment

Done.

Quoted 8 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 457

Quoted 6 lines
drivers/usb/gadget/function/uvc_video.c +6 -0
+        if(ret < 0) {
+            uvcg_queue_cancel(queue, 0);
+        }
+        /* Queue work to the wq as well since its possible that a buffer
+         * may not have been completed.
+         */

The phrasing of this implies this is a bit of defensive programming, but if we don't queue to the wq here then doesn't that mean it'll never run?

I've updated the comment here - it is possible that we hit a situation where the in-flight usb requests may not be enough to completely encode a uvc buffer. In that case if we don't call queue_work, we'll never get the buffer marked as 'completed' and the buffer won't be returned to user-space. That'll prevent the dequeue->queue->dequeue loop and flow of buffers.

Quoted 61 lines
Quoted 61 lines
drivers/usb/gadget/function/uvc_video.c +2 -27
           queue_work(video->async_wq, &video->pump);
       } else {
           uvc_video_free_request(ureq, ep);
@@ -347,6 +472,7 @@ uvc_video_free_requests(struct uvc_video *video)
         INIT_LIST_HEAD(&video->ureqs);
       INIT_LIST_HEAD(&video->req_free);
+    INIT_LIST_HEAD(&video->req_ready);
       video->req_size = 0;
       return 0;
   }
@@ -424,8 +550,7 @@ static void uvcg_video_pump(struct work_struct *work)
       struct usb_request *req = NULL;
       struct uvc_buffer *buf;
       unsigned long flags;
-    bool buf_done;
-    int ret;
+    int ret = 0;
         while (true) {
           if (!video->ep->enabled)
@@ -454,7 +579,6 @@ static void uvcg_video_pump(struct work_struct *work)
             if (buf != NULL) {
               video->encode(req, video, buf);
-            buf_done = buf->state == UVC_BUF_STATE_DONE;
           } else if (!(queue->flags & UVC_QUEUE_DISCONNECTED) && !is_bulk) {
               /*
                * No video buffer available; the queue is still connected and
@@ -462,7 +586,6 @@ static void uvcg_video_pump(struct work_struct *work)
                * prevent missed ISOC transfers.
                */
               req->length = 0;
-            buf_done = false;
           } else {
               /*
                * Either the queue has been disconnected or no video buffer
@@ -473,45 +596,26 @@ static void uvcg_video_pump(struct work_struct *work)
               break;
           }
   -        /*
-         * With USB3 handling more requests at a higher speed, we can't
-         * afford to generate an interrupt for every request. Decide to
-         * interrupt:
-         *
-         * - When no more requests are available in the free queue, as
-         *   this may be our last chance to refill the endpoint's
-         *   request queue.
-         *
-         * - When this is request is the last request for the video
-         *   buffer, as we want to start sending the next video buffer
-         *   ASAP in case it doesn't get started already in the next
-         *   iteration of this loop.
-         *
-         * - Four times over the length of the requests queue (as
-         *   indicated by video->uvc_num_requests), as a trade-off
-         *   between latency and interrupt load.
-         */
-        if (list_empty(&video->req_free) || buf_done ||
-            !(video->req_int_count %
-               DIV_ROUND_UP(video->uvc_num_requests, 4))) {
-            video->req_int_count = 0;
-            req->no_interrupt = 0;
-        } else {
Quoted 8 lines

drivers/usb/gadget/function/uvc_video.c static void uvcg_video_pump(struct work_struct *work)

Quoted 7 lines
drivers/usb/gadget/function/uvc_video.c +0 -5
-            req->no_interrupt = 1;
-        }
-
-        /* Queue the USB request */
-        ret = uvcg_video_ep_queue(video, req);
           spin_unlock_irqrestore(&queue->irqlock, flags);
   +        /* Queue the USB request.*/

I think just drop this - it was always superfluous.

Quoted 4 lines

The uvcg_video_usb_req_queue function mentions that req_lock must be held while calling it - since its possible we might add to the req_ready list. We could say the function should hold req_lock only when the queue_to_ep parameter is false - but that doesn't seem as clean ?

Sorry - I wasn't clear here. I meant that the comment "Queue the USB request" was superfluous rather than the spin_lock_irqsave()

Quoted 66 lines
Quoted 27 lines
Quoted 22 lines
drivers/usb/gadget/function/uvc_video.c +11 -1
+        spin_lock_irqsave(&video->req_lock, flags);
+        /* For bulk end points we queue from the worker thread
+         * since we would preferably not want to wait on requests
+         * to be ready, in the uvcg_video_complete() handler.
+         * For isoc endpoints we add the request to the ready list
+         * and only queue it to the endpoint from the complete handler.
+         */
+        ret = uvcg_video_usb_req_queue(video, req, is_bulk);
+        spin_unlock_irqrestore(&video->req_lock, flags);
+
           if (ret < 0) {
               uvcg_queue_cancel(queue, 0);
               break;
           }
   -        /* Endpoint now owns the request */
+        /* The request is owned by  the endpoint / ready list*/
           req = NULL;
-        video->req_int_count++;
       }
         if (!req)

drivers/usb/gadget/function/uvc_video.c uvcg_video_disable(struct uvc_video *video) · line 673

Quoted 4 lines
drivers/usb/gadget/function/uvc_video.c +1 -1
@@ -567,7 +671,7 @@ uvcg_video_disable(struct uvc_video *video)
         spin_lock_irqsave(&video->req_lock, flags);
       /*
-     * Remove all uvc_reqeusts from ureqs with list_del_init
+     * Remove all uvc_requests from ureqs with list_del_init

This should get fixed in the earlier series.

Quoted 9 lines

drivers/usb/gadget/function/uvc_video.c uvcg_video_disable(struct uvc_video *video) · line 582

Quoted 8 lines
drivers/usb/gadget/function/uvc_video.c +0 -1
        * This lets uvc_video_free_request correctly identify
        * if the uvc_request is attached to a list or not when freeing
        * memory.
@@ -579,9 +683,13 @@ uvcg_video_disable(struct uvc_video *video)
           list_del(&req->list);
           uvc_video_free_request(req->context, video->ep);
       }
-

keep the empty line please

Done.

Quoted 5 lines

drivers/usb/gadget/function/uvc_video.c uvcg_video_disable(struct uvc_video *video) · line 689

Quoted 4 lines
drivers/usb/gadget/function/uvc_video.c +4 -0
+    list_for_each_entry_safe(req, temp, &video->req_ready, list) {
+        list_del(&req->list);
+        uvc_video_free_request(req->context, video->ep);
+    }

and one here too.

Done.

Thanks!

Quoted 19 lines
Quoted 19 lines
drivers/usb/gadget/function/uvc_video.c +1 -0
       INIT_LIST_HEAD(&video->ureqs);
       INIT_LIST_HEAD(&video->req_free);
+    INIT_LIST_HEAD(&video->req_ready);
       video->req_size = 0;
       spin_unlock_irqrestore(&video->req_lock, flags);
   @@ -635,7 +743,7 @@ int uvcg_video_enable(struct uvc_video *video)

        video->req_int_count = 0;
  -    queue_work(video->async_wq, &video->pump);
+    uvc_video_ep_queue_initial_requests(video);
        return ret;
  }

drivers/usb/gadget/function/uvc_video.c +1 -0
@@ -648,6 +756,7 @@ int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
       video->is_enabled = false;
       INIT_LIST_HEAD(&video->ureqs);
       INIT_LIST_HEAD(&video->req_free);
+    INIT_LIST_HEAD(&video->req_ready);
       spin_lock_init(&video->req_lock);
       INIT_WORK(&video->pump, uvcg_video_pump);
 
#28 lore raw

Re: [PATCH v3] usb:gadget:uvc Do not use worker thread to queue isoc usb requests

Hi Dan,
Thanks for the comments.
I sent out v6 here https://lore.kernel.org/linux-usb/[email protected]/T/#u

Quoted 186 lines · Dan Scally

Hi Jayant

Quoted 183 lines · Jayant Chowdhary

Hi Dan,
Thank you for the comments.
I uploaded a new patch at https://lore.kernel.org/linux-usb/[email protected]/T/#u.

Quoted 33 lines · Dan Scally

Hi Jayant - thanks for the patch

Quoted 30 lines · Jayant Chowdhary

When we use an async work queue to perform the function of pumping usb requests to the usb controller, it is possible that amongst other factors, thread scheduling affects at what cadence we're able to pump requests. This could mean isoc usb requests miss their uframes - resulting in video stream flickers on the host device.

To avoid this, we make the async_wq thread only produce isoc usb_requests with uvc buffers encoded into them. The process of queueing to the endpoint is done by the uvc_video_complete() handler. In case no usb_requests are ready with encoded information, we just queue a zero length request to the endpoint from the complete handler.

For bulk endpoints the async_wq thread still queues usb requests to the endpoint.

---
   Based on top of
   https://lore.kernel.org/linux-usb/[email protected]/T/#t:
   v1->v2: Added self Signed-Off-by and addressed review comments v2->v3: Encode to usb requests in async_wq; queue to ep in complete handler for isoc transfers.

   drivers/usb/gadget/function/uvc.h       |   8 + drivers/usb/gadget/function/uvc_video.c | 187 +++++++++++++++++++-----

   2 files changed, 156 insertions(+), 39 deletions(-)
Quoted 11 lines
Quoted 10 lines
drivers/usb/gadget/function/uvc.h +0 -0
index e8d4c87f1e09..82c783410554 100644
@@ -105,7 +105,15 @@ struct uvc_video {

       bool is_enabled; /* tracks whether video stream is enabled */ unsigned int req_size;
       struct list_head ureqs; /* all uvc_requests allocated by uvc_video */ +
+    /* USB requests video pump thread can encode into*/

"USB requests that the video pump thread can encode into", and a space before the closing */ please (and the same a few more times below).

Done.

Quoted 12 lines
Quoted 12 lines

       struct list_head req_free;
+
+    /*
+     * USB requests video pump thread has already encoded into. These are +     * ready to be queued to the endpoint.
+     */
+    struct list_head req_ready;
       spinlock_t req_lock;
         unsigned int req_int_count;

drivers/usb/gadget/function/uvc_video.c +0 -1
index 53feb790a4c3..c84183e9afcc 100644
--- a/drivers/usb/gadget/function/uvc_video.c
Quoted 8 lines
Quoted 7 lines

+++ b/drivers/usb/gadget/function/uvc_video.c

drivers/usb/gadget/function/uvc_video.c +0 -0
@@ -268,6 +268,98 @@ static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req)
        return ret;
    }

   +/* This function must be called with video->req_lock held*/ +static int uvcg_video_usb_req_queue(struct uvc_video *video, +    struct usb_request *req, bool queue_to_ep) {

Brace on a new line please - same a few more times below

Done.

Quoted 46 lines
Quoted 1 line

+    bool is_bulk = video->max_payload_size;

empty line here

Quoted 44 lines

+    if (!video->is_enabled) {
+        uvc_video_free_request(req->context, video->ep);
+        return -ENODEV;
+    }
+    if (queue_to_ep) {
+        struct uvc_request *ureq = req->context;
+        /*
+         * With USB3 handling more requests at a higher speed, we can't +         * afford to generate an interrupt for every request. Decide to +         * interrupt:
+         *
+         * - When no more requests are available in the free queue, as +         *   this may be our last chance to refill the endpoint's +         *   request queue.
+         *
+         * - When this is request is the last request for the video +         *   buffer, as we want to start sending the next video buffer +         *   ASAP in case it doesn't get started already in the next +         *   iteration of this loop.
+         *
+         * - Four times over the length of the requests queue (as +         *   indicated by video->uvc_num_requests), as a trade-off +         *   between latency and interrupt load.
+        */
+        if (list_empty(&video->req_free) || ureq->last_buf || +            !(video->req_int_count %
+            DIV_ROUND_UP(video->uvc_num_requests, 4))) {
+            video->req_int_count = 0;
+            req->no_interrupt = 0;
+        } else {
+            req->no_interrupt = 1;
+        }
+        video->req_int_count++;
+        return uvcg_video_ep_queue(video, req);
+    } else {
+        /*
+        * If we're not queing to the ep, for isoc we're queing +        * to the req_ready list, otherwise req_free.
+        */
+        struct list_head *list =
+            is_bulk ? &video->req_free : &video->req_ready; +        list_add_tail(&req->list, list);
+    }
+    return 0;

Quoted 8 lines

drivers/usb/gadget/function/uvc_video.c static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req) · line 325

Quoted 7 lines
drivers/usb/gadget/function/uvc_video.c +7 -0
+}
+
+static int uvcg_video_ep_queue_zero_length(struct usb_request *req,
+    struct uvc_video *video) {
+    req->length = 0;
+    return uvcg_video_ep_queue(video, req);
+}

Not sure this is worth its own function

Removed the function.

Quoted 16 lines
Quoted 16 lines
drivers/usb/gadget/function/uvc_video.c +16 -0
+
+/* Must only be called from uvcg_video_enable - since after that we only want to
+ * queue requests to the endpoint from the uvc_video_complete complete handler.
+ * This function is needed in order to 'kick start' the flow of requests from
+ * gadget driver to the usb controller.
+ */
+static void uvc_video_ep_queue_initial_requests(struct uvc_video *video) {
+    struct usb_request *req = NULL;
+    unsigned long flags = 0;
+    unsigned int count = 0;
+    int ret = 0;
+    /* We only queue half of the free list since we still want to have
+     * some free usb_requests in the free list for the video_pump async_wq
+     * thread to encode uvc buffers into. Otherwise we could get into a
+     * situation where the free list does not have any usb requests to
+     * encode into - we always end up queueing 0 length requests to the
Quoted 8 lines
Quoted 7 lines

+     * end point.
+     */
+    unsigned half_list_size = video->uvc_num_requests / 2; +    spin_lock_irqsave(&video->req_lock, flags);
+    /* Take these requests off the free list and queue them all to the +     * endpoint. Since we queue the requests with the req_lock held, +     */

This comment seems to be incomplete? You also want an opening /* on its own line:

Apologies I think I missed out completing this comment I will send out another patch later.

Quoted 6 lines

/*
  * Multi line comments
  * look like this
  */

Done.

Quoted 7 lines
Quoted 7 lines

+    while (count < half_list_size) {
+        req = list_first_entry(&video->req_free, struct usb_request, +                    list);
+        list_del(&req->list);
+        ret = uvcg_video_ep_queue_zero_length(req, video); +        if (ret < 0) {
+            uvcg_queue_cancel(&video->queue, /*disconnect*/0);

Quoted 10 lines

drivers/usb/gadget/function/uvc_video.c

Quoted 7 lines

+            break;
+        }
+        count++;
+    }
+    spin_unlock_irqrestore(&video->req_lock, flags);

drivers/usb/gadget/function/uvc_video.c +2 -0
+}
+

So if I'm understanding the new starting sequence right for an isoc endpoint there's an initial flight of half the requests (between 2 and 32) that are queued as zero length - the very first one to .complete() being re-queued as a zero length request before the workqueue is started and encodes data into the _other_ half of the requests which were left in req_free and putting them into req_ready. At that point the .complete()s being run start to pick requests off req_ready instead and they get sent out with data...does that sound right?

That is correct - the first half of number of usb requests allocated (2, 32) are queued at zero length initially. We’ll have half of the requests being sent to the ep in flight and half in the free list yes. queue_work will actually start with either uvc_v4l2_qbuf (uvc_v4l2.c) or at a zero length request being completed - whichever comes first.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

What are the implications of those initial 3-33 zero length requests? What kind of latency can that introduce to the start of the video stream?

At the worst, we’ll have  a 32 x 125us(uframe period) = 4ms  delay for the first frame of the uvc buffer stream being sent out to the usb controller. After that, since uvc buffers are typically queued at a much lower rate than usb requests being sent to the endpoint, we should be fine ?

I think that the 'ongoing' stream should be fine using this method yes, though if possible I'd like to avoid introducing the delay to the first frame. Do you know if there's a simple way to remove it? I recognise the delay is small so I don't think it's necessarily a dealbreaker but it would be nice if we could avoid it.

We could introduce a flag and have the async_wq thread queue requests to the ep for the first uvc buffer. However, what that would do is it would possibly add a skew between the first and second frames. Let's say we send out frame 1 at t = 0ms. It is possible that by the time frame 2 comes around, we have 32 0 length usb requests queued up in the usb controller. As a result the time distance between frame 1's start and frame 2's start would be 33ms + 4ms = 37ms (instead of 4ms and 37ms). So its a tradeoff between skew vs 4ms delay in starting the stream. The current logic avoids the skew at the expense of the delay in the first frame - and its simpler to follow in code. Happy to hear your and others' thoughts on this as well.

Quoted 56 lines
Quoted 56 lines

In my local testing, I don't see any delay observable to the naked eye.

Quoted 36 lines
Quoted 36 lines

   static void
   uvc_video_complete(struct usb_ep *ep, struct usb_request *req) {

drivers/usb/gadget/function/uvc_video.c +22 -1
@@ -276,6 +368,8 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
        struct uvc_video_queue *queue = &video->queue;
        struct uvc_buffer *last_buf = NULL;
        unsigned long flags;
+    bool is_bulk = video->max_payload_size;
+    int ret = 0;
          spin_lock_irqsave(&video->req_lock, flags);
        if (!video->is_enabled) {
@@ -329,7 +423,38 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
         * back to req_free
         */
        if (video->is_enabled) {
-        list_add_tail(&req->list, &video->req_free);
+        /*
+         * Here we check whether any request is available in the ready
+         * list. If it is, queue it to the ep and add the current
+         * usb_request to the req_free list - for video_pump to fill in.
+         * Otherwise, just use the current usb_request to queue a 0
+         * length request to the ep. Since we always add to the req_free
+         * list if we dequeue from the ready list, there will never
+         * be a situation where the req_free list is completely out of
+         * requests and cannot recover.
+         */
+        struct usb_request *to_queue = req;
+        to_queue->length = 0;
+        if (!list_empty(&video->req_ready)) {
+            to_queue = list_first_entry(&video->req_ready,
+                struct usb_request, list);
+            list_del(&to_queue->list);
+            /* Add it to the free list. */
+            list_add_tail(&req->list, &video->req_free);
+        }
+        /*
Quoted 8 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 451

Quoted 6 lines
drivers/usb/gadget/function/uvc_video.c +6 -0
+         * Queue to the endpoint. The actual queueing to ep will
+         * only happen on one thread - the async_wq for bulk endpoints
+         * and this thread for isoc endpoints.
+         */
+        ret = uvcg_video_usb_req_queue(video, to_queue,
+                           /*queue_to_ep*/!is_bulk);

In principle in-line comments are fine, but I don't think the parameter name is worth a comment

Done.

Quoted 7 lines
Quoted 6 lines
drivers/usb/gadget/function/uvc_video.c +6 -0
+        if(ret < 0) {
+            uvcg_queue_cancel(queue, 0);
+        }
+        /* Queue work to the wq as well since its possible that a buffer
+         * may not have been completed.
+         */
Quoted 5 lines
Quoted 5 lines
Quoted 1 line

The phrasing of this implies this is a bit of defensive programming, but if we don't queue to the wq here then doesn't that mean it'll never run?

I've updated the comment here - it is possible that we hit a situation where the in-flight usb requests may not be enough to completely encode a uvc buffer. In that case if we don't call queue_work, we'll never get the buffer marked as 'completed' and the buffer won't be returned to user-space. That'll prevent the dequeue->queue->dequeue loop and flow of buffers.

I added this queue_work call to the if statement which checks if the ready list has any requests. If it doesn't, there's no point in queuing any work - since we wouldn't be giving back to the req_free list.

Thank you

Quoted 70 lines
Quoted 70 lines
Quoted 70 lines
Quoted 61 lines
drivers/usb/gadget/function/uvc_video.c +2 -4
            queue_work(video->async_wq, &video->pump);
        } else {
            uvc_video_free_request(ureq, ep);
@@ -347,6 +472,7 @@ uvc_video_free_requests(struct uvc_video *video)
          INIT_LIST_HEAD(&video->ureqs);
        INIT_LIST_HEAD(&video->req_free);
+    INIT_LIST_HEAD(&video->req_ready);
        video->req_size = 0;
        return 0;
    }
@@ -424,8 +550,7 @@ static void uvcg_video_pump(struct work_struct *work)
        struct usb_request *req = NULL;
        struct uvc_buffer *buf;
        unsigned long flags;
-    bool buf_done;
-    int ret;
+    int ret = 0;
          while (true) {
            if (!video->ep->enabled)
@@ -454,7 +579,6 @@ static void uvcg_video_pump(struct work_struct *work)
              if (buf != NULL) {
                video->encode(req, video, buf);
-            buf_done = buf->state == UVC_BUF_STATE_DONE;
            } else if (!(queue->flags & UVC_QUEUE_DISCONNECTED) && !is_bulk) {
                /*
                 * No video buffer available; the queue is still connected and
@@ -462,7 +586,6 @@ static void uvcg_video_pump(struct work_struct *work)
                 * prevent missed ISOC transfers.
                 */
                req->length = 0;
-            buf_done = false;
            } else {
                /*
                 * Either the queue has been disconnected or no video buffer
@@ -473,45 +596,26 @@ static void uvcg_video_pump(struct work_struct *work)
                break;
            }

   -        /*
-         * With USB3 handling more requests at a higher speed, we can't -         * afford to generate an interrupt for every request. Decide to -         * interrupt:
-         *
-         * - When no more requests are available in the free queue, as -         *   this may be our last chance to refill the endpoint's -         *   request queue.
-         *
-         * - When this is request is the last request for the video -         *   buffer, as we want to start sending the next video buffer -         *   ASAP in case it doesn't get started already in the next -         *   iteration of this loop.
-         *
-         * - Four times over the length of the requests queue (as -         *   indicated by video->uvc_num_requests), as a trade-off -         *   between latency and interrupt load.
-         */
-        if (list_empty(&video->req_free) || buf_done ||
-            !(video->req_int_count %
-               DIV_ROUND_UP(video->uvc_num_requests, 4))) { -            video->req_int_count = 0;
-            req->no_interrupt = 0;

Quoted 8 lines

-        } else {
-            req->no_interrupt = 1;
-        }
-
-        /* Queue the USB request */
-        ret = uvcg_video_ep_queue(video, req); spin_unlock_irqrestore(&queue->irqlock, flags); +        /* Queue the USB request.*/

I think just drop this - it was always superfluous.

Quoted 7 lines
Quoted 4 lines

The uvcg_video_usb_req_queue function mentions that req_lock must be held while calling it - since its possible we might add to the req_ready list. We could say the function should hold req_lock only when the queue_to_ep parameter is false - but that doesn't seem as clean ?

Sorry - I wasn't clear here. I meant that the comment "Queue the USB request" was superfluous rather than the spin_lock_irqsave()

Removed.

Quoted 67 lines
Quoted 67 lines
Quoted 28 lines
Quoted 23 lines

+        spin_lock_irqsave(&video->req_lock, flags);
+        /* For bulk end points we queue from the worker thread +         * since we would preferably not want to wait on requests +         * to be ready, in the uvcg_video_complete() handler. +         * For isoc endpoints we add the request to the ready list +         * and only queue it to the endpoint from the complete handler. +         */
+        ret = uvcg_video_usb_req_queue(video, req, is_bulk); +        spin_unlock_irqrestore(&video->req_lock, flags);
+
           if (ret < 0) {
               uvcg_queue_cancel(queue, 0);
               break;
           }
   -        /* Endpoint now owns the request */
+        /* The request is owned by  the endpoint / ready list*/ req = NULL;
-        video->req_int_count++;
       }
         if (!req)

drivers/usb/gadget/function/uvc_video.c uvcg_video_disable(struct uvc_video *video) · line 673

Quoted 4 lines
drivers/usb/gadget/function/uvc_video.c +1 -1
@@ -567,7 +671,7 @@ uvcg_video_disable(struct uvc_video *video)
          spin_lock_irqsave(&video->req_lock, flags);
        /*
-     * Remove all uvc_reqeusts from ureqs with list_del_init
+     * Remove all uvc_requests from ureqs with list_del_init

This should get fixed in the earlier series.

Quoted 9 lines

drivers/usb/gadget/function/uvc_video.c uvcg_video_disable(struct uvc_video *video) · line 582

Quoted 8 lines
drivers/usb/gadget/function/uvc_video.c +0 -1
         * This lets uvc_video_free_request correctly identify
         * if the uvc_request is attached to a list or not when freeing
         * memory.
@@ -579,9 +683,13 @@ uvcg_video_disable(struct uvc_video *video)
            list_del(&req->list);
            uvc_video_free_request(req->context, video->ep);
        }
-

keep the empty line please

Done.

Quoted 5 lines

drivers/usb/gadget/function/uvc_video.c uvcg_video_disable(struct uvc_video *video) · line 689

Quoted 4 lines
drivers/usb/gadget/function/uvc_video.c +4 -0
+    list_for_each_entry_safe(req, temp, &video->req_ready, list) {
+        list_del(&req->list);
+        uvc_video_free_request(req->context, video->ep);
+    }

and one here too.

Done.

Thanks!

Quoted 19 lines
Quoted 19 lines
drivers/usb/gadget/function/uvc_video.c +1 -0
        INIT_LIST_HEAD(&video->ureqs);
        INIT_LIST_HEAD(&video->req_free);
+    INIT_LIST_HEAD(&video->req_ready);
        video->req_size = 0;
        spin_unlock_irqrestore(&video->req_lock, flags);
    @@ -635,7 +743,7 @@ int uvcg_video_enable(struct uvc_video *video)

         video->req_int_count = 0;
   -    queue_work(video->async_wq, &video->pump); +    uvc_video_ep_queue_initial_requests(video); return ret;
   }

drivers/usb/gadget/function/uvc_video.c +1 -0
@@ -648,6 +756,7 @@ int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
        video->is_enabled = false;
        INIT_LIST_HEAD(&video->ureqs);
        INIT_LIST_HEAD(&video->req_free);
+    INIT_LIST_HEAD(&video->req_ready);
        spin_lock_init(&video->req_lock);
        INIT_WORK(&video->pump, uvcg_video_pump);

#29 lore raw

Re: [PATCH v3] usb:gadget:uvc Do not use worker thread to queue isoc usb requests

Hi Dan,
I was wondering if you had a chance to look at v6 of the patch :
https://lore.kernel.org/linux-usb/[email protected]/T/#u ?

Thank you!

Jayant

Quoted 405 lines · Jayant Chowdhary

Hi Dan,
Thanks for the comments.
I sent out v6 here https://lore.kernel.org/linux-usb/[email protected]/T/#u

Quoted 182 lines · Dan Scally

Hi Jayant

Quoted 179 lines · Jayant Chowdhary

Hi Dan,
Thank you for the comments.
I uploaded a new patch at https://lore.kernel.org/linux-usb/[email protected]/T/#u.

Quoted 33 lines · Dan Scally

Hi Jayant - thanks for the patch

Quoted 30 lines · Jayant Chowdhary

When we use an async work queue to perform the function of pumping usb requests to the usb controller, it is possible that amongst other factors, thread scheduling affects at what cadence we're able to pump requests. This could mean isoc usb requests miss their uframes - resulting in video stream flickers on the host device.

To avoid this, we make the async_wq thread only produce isoc usb_requests with uvc buffers encoded into them. The process of queueing to the endpoint is done by the uvc_video_complete() handler. In case no usb_requests are ready with encoded information, we just queue a zero length request to the endpoint from the complete handler.

For bulk endpoints the async_wq thread still queues usb requests to the endpoint.

---
   Based on top of
   https://lore.kernel.org/linux-usb/[email protected]/T/#t:
   v1->v2: Added self Signed-Off-by and addressed review comments v2->v3: Encode to usb requests in async_wq; queue to ep in complete handler for isoc transfers.

   drivers/usb/gadget/function/uvc.h       |   8 + drivers/usb/gadget/function/uvc_video.c | 187 +++++++++++++++++++-----

   2 files changed, 156 insertions(+), 39 deletions(-)
Quoted 11 lines
Quoted 10 lines
drivers/usb/gadget/function/uvc.h +0 -0
index e8d4c87f1e09..82c783410554 100644
@@ -105,7 +105,15 @@ struct uvc_video {

       bool is_enabled; /* tracks whether video stream is enabled */ unsigned int req_size;
       struct list_head ureqs; /* all uvc_requests allocated by uvc_video */ +
+    /* USB requests video pump thread can encode into*/

"USB requests that the video pump thread can encode into", and a space before the closing */ please (and the same a few more times below).

Done.

Quoted 12 lines
Quoted 12 lines

       struct list_head req_free;
+
+    /*
+     * USB requests video pump thread has already encoded into. These are +     * ready to be queued to the endpoint.
+     */
+    struct list_head req_ready;
       spinlock_t req_lock;
         unsigned int req_int_count;

drivers/usb/gadget/function/uvc_video.c +0 -1
index 53feb790a4c3..c84183e9afcc 100644
--- a/drivers/usb/gadget/function/uvc_video.c
Quoted 8 lines
Quoted 7 lines

+++ b/drivers/usb/gadget/function/uvc_video.c

drivers/usb/gadget/function/uvc_video.c +0 -0
@@ -268,6 +268,98 @@ static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req)
        return ret;
    }

   +/* This function must be called with video->req_lock held*/ +static int uvcg_video_usb_req_queue(struct uvc_video *video, +    struct usb_request *req, bool queue_to_ep) {

Brace on a new line please - same a few more times below

Done.

Quoted 46 lines
Quoted 1 line

+    bool is_bulk = video->max_payload_size;

empty line here

Quoted 44 lines

+    if (!video->is_enabled) {
+        uvc_video_free_request(req->context, video->ep);
+        return -ENODEV;
+    }
+    if (queue_to_ep) {
+        struct uvc_request *ureq = req->context;
+        /*
+         * With USB3 handling more requests at a higher speed, we can't +         * afford to generate an interrupt for every request. Decide to +         * interrupt:
+         *
+         * - When no more requests are available in the free queue, as +         *   this may be our last chance to refill the endpoint's +         *   request queue.
+         *
+         * - When this is request is the last request for the video +         *   buffer, as we want to start sending the next video buffer +         *   ASAP in case it doesn't get started already in the next +         *   iteration of this loop.
+         *
+         * - Four times over the length of the requests queue (as +         *   indicated by video->uvc_num_requests), as a trade-off +         *   between latency and interrupt load.
+        */
+        if (list_empty(&video->req_free) || ureq->last_buf || +            !(video->req_int_count %
+            DIV_ROUND_UP(video->uvc_num_requests, 4))) {
+            video->req_int_count = 0;
+            req->no_interrupt = 0;
+        } else {
+            req->no_interrupt = 1;
+        }
+        video->req_int_count++;
+        return uvcg_video_ep_queue(video, req);
+    } else {
+        /*
+        * If we're not queing to the ep, for isoc we're queing +        * to the req_ready list, otherwise req_free.
+        */
+        struct list_head *list =
+            is_bulk ? &video->req_free : &video->req_ready; +        list_add_tail(&req->list, list);
+    }
+    return 0;

Quoted 8 lines

drivers/usb/gadget/function/uvc_video.c static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req) · line 325

Quoted 7 lines
drivers/usb/gadget/function/uvc_video.c +7 -0
+}
+
+static int uvcg_video_ep_queue_zero_length(struct usb_request *req,
+    struct uvc_video *video) {
+    req->length = 0;
+    return uvcg_video_ep_queue(video, req);
+}

Not sure this is worth its own function

Removed the function.

Quoted 16 lines
Quoted 16 lines
drivers/usb/gadget/function/uvc_video.c +16 -0
+
+/* Must only be called from uvcg_video_enable - since after that we only want to
+ * queue requests to the endpoint from the uvc_video_complete complete handler.
+ * This function is needed in order to 'kick start' the flow of requests from
+ * gadget driver to the usb controller.
+ */
+static void uvc_video_ep_queue_initial_requests(struct uvc_video *video) {
+    struct usb_request *req = NULL;
+    unsigned long flags = 0;
+    unsigned int count = 0;
+    int ret = 0;
+    /* We only queue half of the free list since we still want to have
+     * some free usb_requests in the free list for the video_pump async_wq
+     * thread to encode uvc buffers into. Otherwise we could get into a
+     * situation where the free list does not have any usb requests to
+     * encode into - we always end up queueing 0 length requests to the
Quoted 8 lines
Quoted 7 lines

+     * end point.
+     */
+    unsigned half_list_size = video->uvc_num_requests / 2; +    spin_lock_irqsave(&video->req_lock, flags);
+    /* Take these requests off the free list and queue them all to the +     * endpoint. Since we queue the requests with the req_lock held, +     */

This comment seems to be incomplete? You also want an opening /* on its own line:

Apologies I think I missed out completing this comment I will send out another patch later.

Quoted 5 lines

/*
  * Multi line comments
  * look like this
  */

Done.

Quoted 17 lines
Quoted 12 lines

+    while (count < half_list_size) {
+        req = list_first_entry(&video->req_free, struct usb_request, +                    list);
+        list_del(&req->list);
+        ret = uvcg_video_ep_queue_zero_length(req, video); +        if (ret < 0) {
+            uvcg_queue_cancel(&video->queue, /*disconnect*/0); +            break;
+        }
+        count++;
+    }
+    spin_unlock_irqrestore(&video->req_lock, flags);

drivers/usb/gadget/function/uvc_video.c

Quoted 2 lines
drivers/usb/gadget/function/uvc_video.c +2 -0
+}
+

So if I'm understanding the new starting sequence right for an isoc endpoint there's an initial flight of half the requests (between 2 and 32) that are queued as zero length - the very first one to .complete() being re-queued as a zero length request before the workqueue is started and encodes data into the _other_ half of the requests which were left in req_free and putting them into req_ready. At that point the .complete()s being run start to pick requests off req_ready instead and they get sent out with data...does that sound right?

Quoted 8 lines
Quoted 6 lines

That is correct - the first half of number of usb requests allocated (2, 32) are queued at zero length initially. We’ll have half of the requests being sent to the ep in flight and half in the free list yes. queue_work will actually start with either uvc_v4l2_qbuf (uvc_v4l2.c) or at a zero length request being completed - whichever comes first.

Quoted 1 line

What are the implications of those initial 3-33 zero length requests? What kind of latency can that introduce to the start of the video stream?

At the worst, we’ll have  a 32 x 125us(uframe period) = 4ms  delay for the first frame of the uvc buffer stream being sent out to the usb controller. After that, since uvc buffers are typically queued at a much lower rate than usb requests being sent to the endpoint, we should be fine ?

I think that the 'ongoing' stream should be fine using this method yes, though if possible I'd like to avoid introducing the delay to the first frame. Do you know if there's a simple way to remove it? I recognise the delay is small so I don't think it's necessarily a dealbreaker but it would be nice if we could avoid it.

We could introduce a flag and have the async_wq thread queue requests to the ep for the first uvc buffer. However, what that would do is it would possibly add a skew between the first and second frames. Let's say we send out frame 1 at t = 0ms. It is possible that by the time frame 2 comes around, we have 32 0 length usb requests queued up in the usb controller. As a result the time distance between frame 1's start and frame 2's start would be 33ms + 4ms = 37ms (instead of 4ms and 37ms). So its a tradeoff between skew vs 4ms delay in starting the stream. The current logic avoids the skew at the expense of the delay in the first frame - and its simpler to follow in code. Happy to hear your and others' thoughts on this as well.

Quoted 53 lines
Quoted 53 lines

In my local testing, I don't see any delay observable to the naked eye.

Quoted 35 lines
Quoted 35 lines

   static void
   uvc_video_complete(struct usb_ep *ep, struct usb_request *req) {

drivers/usb/gadget/function/uvc_video.c +21 -1
@@ -276,6 +368,8 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
        struct uvc_video_queue *queue = &video->queue;
        struct uvc_buffer *last_buf = NULL;
        unsigned long flags;
+    bool is_bulk = video->max_payload_size;
+    int ret = 0;
          spin_lock_irqsave(&video->req_lock, flags);
        if (!video->is_enabled) {
@@ -329,7 +423,38 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
         * back to req_free
         */
        if (video->is_enabled) {
-        list_add_tail(&req->list, &video->req_free);
+        /*
+         * Here we check whether any request is available in the ready
+         * list. If it is, queue it to the ep and add the current
+         * usb_request to the req_free list - for video_pump to fill in.
+         * Otherwise, just use the current usb_request to queue a 0
+         * length request to the ep. Since we always add to the req_free
+         * list if we dequeue from the ready list, there will never
+         * be a situation where the req_free list is completely out of
+         * requests and cannot recover.
+         */
+        struct usb_request *to_queue = req;
+        to_queue->length = 0;
+        if (!list_empty(&video->req_ready)) {
+            to_queue = list_first_entry(&video->req_ready,
+                struct usb_request, list);
+            list_del(&to_queue->list);
+            /* Add it to the free list. */
+            list_add_tail(&req->list, &video->req_free);
+        }
Quoted 8 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 451

Quoted 7 lines
drivers/usb/gadget/function/uvc_video.c +7 -0
+        /*
+         * Queue to the endpoint. The actual queueing to ep will
+         * only happen on one thread - the async_wq for bulk endpoints
+         * and this thread for isoc endpoints.
+         */
+        ret = uvcg_video_usb_req_queue(video, to_queue,
+                           /*queue_to_ep*/!is_bulk);

In principle in-line comments are fine, but I don't think the parameter name is worth a comment

Done.

Quoted 6 lines
Quoted 6 lines
drivers/usb/gadget/function/uvc_video.c +6 -0
+        if(ret < 0) {
+            uvcg_queue_cancel(queue, 0);
+        }
+        /* Queue work to the wq as well since its possible that a buffer
+         * may not have been completed.
+         */
Quoted 4 lines
Quoted 4 lines
Quoted 1 line

The phrasing of this implies this is a bit of defensive programming, but if we don't queue to the wq here then doesn't that mean it'll never run?

I've updated the comment here - it is possible that we hit a situation where the in-flight usb requests may not be enough to completely encode a uvc buffer. In that case if we don't call queue_work, we'll never get the buffer marked as 'completed' and the buffer won't be returned to user-space. That'll prevent the dequeue->queue->dequeue loop and flow of buffers.

I added this queue_work call to the if statement which checks if the ready list has any requests. If it doesn't, there's no point in queuing any work - since we wouldn't be giving back to the req_free list.

Thank you

Quoted 69 lines
Quoted 69 lines
Quoted 69 lines
Quoted 60 lines
drivers/usb/gadget/function/uvc_video.c +2 -4
            queue_work(video->async_wq, &video->pump);
        } else {
            uvc_video_free_request(ureq, ep);
@@ -347,6 +472,7 @@ uvc_video_free_requests(struct uvc_video *video)
          INIT_LIST_HEAD(&video->ureqs);
        INIT_LIST_HEAD(&video->req_free);
+    INIT_LIST_HEAD(&video->req_ready);
        video->req_size = 0;
        return 0;
    }
@@ -424,8 +550,7 @@ static void uvcg_video_pump(struct work_struct *work)
        struct usb_request *req = NULL;
        struct uvc_buffer *buf;
        unsigned long flags;
-    bool buf_done;
-    int ret;
+    int ret = 0;
          while (true) {
            if (!video->ep->enabled)
@@ -454,7 +579,6 @@ static void uvcg_video_pump(struct work_struct *work)
              if (buf != NULL) {
                video->encode(req, video, buf);
-            buf_done = buf->state == UVC_BUF_STATE_DONE;
            } else if (!(queue->flags & UVC_QUEUE_DISCONNECTED) && !is_bulk) {
                /*
                 * No video buffer available; the queue is still connected and
@@ -462,7 +586,6 @@ static void uvcg_video_pump(struct work_struct *work)
                 * prevent missed ISOC transfers.
                 */
                req->length = 0;
-            buf_done = false;
            } else {
                /*
                 * Either the queue has been disconnected or no video buffer
@@ -473,45 +596,26 @@ static void uvcg_video_pump(struct work_struct *work)
                break;
            }

   -        /*
-         * With USB3 handling more requests at a higher speed, we can't -         * afford to generate an interrupt for every request. Decide to -         * interrupt:
-         *
-         * - When no more requests are available in the free queue, as -         *   this may be our last chance to refill the endpoint's -         *   request queue.
-         *
-         * - When this is request is the last request for the video -         *   buffer, as we want to start sending the next video buffer -         *   ASAP in case it doesn't get started already in the next -         *   iteration of this loop.
-         *
-         * - Four times over the length of the requests queue (as -         *   indicated by video->uvc_num_requests), as a trade-off -         *   between latency and interrupt load.
-         */
-        if (list_empty(&video->req_free) || buf_done ||
-            !(video->req_int_count %
-               DIV_ROUND_UP(video->uvc_num_requests, 4))) { -            video->req_int_count = 0;
-            req->no_interrupt = 0;

Quoted 8 lines

-        } else {
-            req->no_interrupt = 1;
-        }
-
-        /* Queue the USB request */
-        ret = uvcg_video_ep_queue(video, req); spin_unlock_irqrestore(&queue->irqlock, flags); +        /* Queue the USB request.*/

I think just drop this - it was always superfluous.

Quoted 6 lines
Quoted 4 lines

The uvcg_video_usb_req_queue function mentions that req_lock must be held while calling it - since its possible we might add to the req_ready list. We could say the function should hold req_lock only when the queue_to_ep parameter is false - but that doesn't seem as clean ?

Sorry - I wasn't clear here. I meant that the comment "Queue the USB request" was superfluous rather than the spin_lock_irqsave()

Removed.

Quoted 65 lines
Quoted 65 lines
Quoted 26 lines
Quoted 21 lines

+        spin_lock_irqsave(&video->req_lock, flags);
+        /* For bulk end points we queue from the worker thread +         * since we would preferably not want to wait on requests +         * to be ready, in the uvcg_video_complete() handler. +         * For isoc endpoints we add the request to the ready list +         * and only queue it to the endpoint from the complete handler. +         */
+        ret = uvcg_video_usb_req_queue(video, req, is_bulk); +        spin_unlock_irqrestore(&video->req_lock, flags);
+
           if (ret < 0) {
               uvcg_queue_cancel(queue, 0);
               break;
           }
   -        /* Endpoint now owns the request */
+        /* The request is owned by  the endpoint / ready list*/ req = NULL;
-        video->req_int_count++;
       }
         if (!req)

drivers/usb/gadget/function/uvc_video.c uvcg_video_disable(struct uvc_video *video) · line 673

Quoted 4 lines
drivers/usb/gadget/function/uvc_video.c +1 -1
@@ -567,7 +671,7 @@ uvcg_video_disable(struct uvc_video *video)
          spin_lock_irqsave(&video->req_lock, flags);
        /*
-     * Remove all uvc_reqeusts from ureqs with list_del_init
+     * Remove all uvc_requests from ureqs with list_del_init

This should get fixed in the earlier series.

Quoted 9 lines

drivers/usb/gadget/function/uvc_video.c uvcg_video_disable(struct uvc_video *video) · line 582

Quoted 8 lines
drivers/usb/gadget/function/uvc_video.c +0 -1
         * This lets uvc_video_free_request correctly identify
         * if the uvc_request is attached to a list or not when freeing
         * memory.
@@ -579,9 +683,13 @@ uvcg_video_disable(struct uvc_video *video)
            list_del(&req->list);
            uvc_video_free_request(req->context, video->ep);
        }
-

keep the empty line please

Done.

Quoted 5 lines

drivers/usb/gadget/function/uvc_video.c uvcg_video_disable(struct uvc_video *video) · line 689

Quoted 4 lines
drivers/usb/gadget/function/uvc_video.c +4 -0
+    list_for_each_entry_safe(req, temp, &video->req_ready, list) {
+        list_del(&req->list);
+        uvc_video_free_request(req->context, video->ep);
+    }

and one here too.

Done.

Thanks!

Quoted 19 lines
Quoted 19 lines
drivers/usb/gadget/function/uvc_video.c +1 -0
        INIT_LIST_HEAD(&video->ureqs);
        INIT_LIST_HEAD(&video->req_free);
+    INIT_LIST_HEAD(&video->req_ready);
        video->req_size = 0;
        spin_unlock_irqrestore(&video->req_lock, flags);
    @@ -635,7 +743,7 @@ int uvcg_video_enable(struct uvc_video *video)

         video->req_int_count = 0;
   -    queue_work(video->async_wq, &video->pump); +    uvc_video_ep_queue_initial_requests(video); return ret;
   }

drivers/usb/gadget/function/uvc_video.c +1 -0
@@ -648,6 +756,7 @@ int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
        video->is_enabled = false;
        INIT_LIST_HEAD(&video->ureqs);
        INIT_LIST_HEAD(&video->req_free);
+    INIT_LIST_HEAD(&video->req_ready);
        spin_lock_init(&video->req_lock);
        INIT_WORK(&video->pump, uvcg_video_pump);

#30 lore raw

Re: [PATCH v3] usb:gadget:uvc Do not use worker thread to queue isoc usb requests

Morning

Quoted 3 lines · Jayant Chowdhary

Hi Dan,
I was wondering if you had a chance to look at v6 of the patch :
https://lore.kernel.org/linux-usb/[email protected]/T/#u ?

Now I did, sorry for the delay

Quoted 203 lines

Thank you!

Jayant

Quoted 198 lines · Jayant Chowdhary

Hi Dan,
Thanks for the comments.
I sent out v6 here https://lore.kernel.org/linux-usb/[email protected]/T/#u

Quoted 182 lines · Dan Scally

Hi Jayant

Quoted 179 lines · Jayant Chowdhary

Hi Dan,
Thank you for the comments.
I uploaded a new patch at https://lore.kernel.org/linux-usb/[email protected]/T/#u.

Quoted 33 lines · Dan Scally

Hi Jayant - thanks for the patch

Quoted 30 lines · Jayant Chowdhary

When we use an async work queue to perform the function of pumping usb requests to the usb controller, it is possible that amongst other factors, thread scheduling affects at what cadence we're able to pump requests. This could mean isoc usb requests miss their uframes - resulting in video stream flickers on the host device.

To avoid this, we make the async_wq thread only produce isoc usb_requests with uvc buffers encoded into them. The process of queueing to the endpoint is done by the uvc_video_complete() handler. In case no usb_requests are ready with encoded information, we just queue a zero length request to the endpoint from the complete handler.

For bulk endpoints the async_wq thread still queues usb requests to the endpoint.

---
   Based on top of
   https://lore.kernel.org/linux-usb/[email protected]/T/#t:
   v1->v2: Added self Signed-Off-by and addressed review comments v2->v3: Encode to usb requests in async_wq; queue to ep in complete handler for isoc transfers.

    drivers/usb/gadget/function/uvc.h       |   8 +
    drivers/usb/gadget/function/uvc_video.c | 187 +++++++++++++++++++-----
    2 files changed, 156 insertions(+), 39 deletions(-)
Quoted 11 lines

drivers/usb/gadget/function/uvc.h struct uvc_video { · line 109

Quoted 10 lines
drivers/usb/gadget/function/uvc.h +2 -0
index e8d4c87f1e09..82c783410554 100644
@@ -105,7 +105,15 @@ struct uvc_video {
        bool is_enabled; /* tracks whether video stream is enabled */
        unsigned int req_size;
        struct list_head ureqs; /* all uvc_requests allocated by uvc_video */
+
+    /* USB requests video pump thread can encode into*/

"USB requests that the video pump thread can encode into", and a space before the closing */ please (and the same a few more times below).

Done.

Quoted 12 lines
Quoted 12 lines
drivers/usb/gadget/function/uvc.h +6 -0
        struct list_head req_free;
+
+    /*
+     * USB requests video pump thread has already encoded into. These are
+     * ready to be queued to the endpoint.
+     */
+    struct list_head req_ready;
        spinlock_t req_lock;
          unsigned int req_int_count;
drivers/usb/gadget/function/uvc_video.c +0 -1
index 53feb790a4c3..c84183e9afcc 100644
--- a/drivers/usb/gadget/function/uvc_video.c
Quoted 8 lines

drivers/usb/gadget/function/uvc_video.c static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req) · line 272

Quoted 7 lines

+++ b/drivers/usb/gadget/function/uvc_video.c

drivers/usb/gadget/function/uvc_video.c +2 -0
@@ -268,6 +268,98 @@ static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req)
        return ret;
    }
    +/* This function must be called with video->req_lock held*/
+static int uvcg_video_usb_req_queue(struct uvc_video *video,
+    struct usb_request *req, bool queue_to_ep) {

Brace on a new line please - same a few more times below

Done.

Quoted 46 lines

drivers/usb/gadget/function/uvc_video.c static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req) · line 273

Quoted 1 line
drivers/usb/gadget/function/uvc_video.c +1 -0
+    bool is_bulk = video->max_payload_size;

empty line here

Quoted 44 lines
drivers/usb/gadget/function/uvc_video.c +44 -0
+    if (!video->is_enabled) {
+        uvc_video_free_request(req->context, video->ep);
+        return -ENODEV;
+    }
+    if (queue_to_ep) {
+        struct uvc_request *ureq = req->context;
+        /*
+         * With USB3 handling more requests at a higher speed, we can't
+         * afford to generate an interrupt for every request. Decide to
+         * interrupt:
+         *
+         * - When no more requests are available in the free queue, as
+         *   this may be our last chance to refill the endpoint's
+         *   request queue.
+         *
+         * - When this is request is the last request for the video
+         *   buffer, as we want to start sending the next video buffer
+         *   ASAP in case it doesn't get started already in the next
+         *   iteration of this loop.
+         *
+         * - Four times over the length of the requests queue (as
+         *   indicated by video->uvc_num_requests), as a trade-off
+         *   between latency and interrupt load.
+        */
+        if (list_empty(&video->req_free) || ureq->last_buf ||
+            !(video->req_int_count %
+            DIV_ROUND_UP(video->uvc_num_requests, 4))) {
+            video->req_int_count = 0;
+            req->no_interrupt = 0;
+        } else {
+            req->no_interrupt = 1;
+        }
+        video->req_int_count++;
+        return uvcg_video_ep_queue(video, req);
+    } else {
+        /*
+        * If we're not queing to the ep, for isoc we're queing
+        * to the req_ready list, otherwise req_free.
+        */
+        struct list_head *list =
+            is_bulk ? &video->req_free : &video->req_ready;
+        list_add_tail(&req->list, list);
+    }
+    return 0;
Quoted 8 lines

drivers/usb/gadget/function/uvc_video.c static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req) · line 325

Quoted 7 lines
drivers/usb/gadget/function/uvc_video.c +7 -0
+}
+
+static int uvcg_video_ep_queue_zero_length(struct usb_request *req,
+    struct uvc_video *video) {
+    req->length = 0;
+    return uvcg_video_ep_queue(video, req);
+}

Not sure this is worth its own function

Removed the function.

Quoted 16 lines
Quoted 16 lines
drivers/usb/gadget/function/uvc_video.c +16 -0
+
+/* Must only be called from uvcg_video_enable - since after that we only want to
+ * queue requests to the endpoint from the uvc_video_complete complete handler.
+ * This function is needed in order to 'kick start' the flow of requests from
+ * gadget driver to the usb controller.
+ */
+static void uvc_video_ep_queue_initial_requests(struct uvc_video *video) {
+    struct usb_request *req = NULL;
+    unsigned long flags = 0;
+    unsigned int count = 0;
+    int ret = 0;
+    /* We only queue half of the free list since we still want to have
+     * some free usb_requests in the free list for the video_pump async_wq
+     * thread to encode uvc buffers into. Otherwise we could get into a
+     * situation where the free list does not have any usb requests to
+     * encode into - we always end up queueing 0 length requests to the
Quoted 8 lines

drivers/usb/gadget/function/uvc_video.c static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req) · line 348

Quoted 7 lines
drivers/usb/gadget/function/uvc_video.c +7 -0
+     * end point.
+     */
+    unsigned half_list_size = video->uvc_num_requests / 2;
+    spin_lock_irqsave(&video->req_lock, flags);
+    /* Take these requests off the free list and queue them all to the
+     * endpoint. Since we queue the requests with the req_lock held,
+     */

This comment seems to be incomplete? You also want an opening /* on its own line:

Apologies I think I missed out completing this comment I will send out another patch later.

Quoted 5 lines

/*
  * Multi line comments
  * look like this
  */

Done.

Quoted 17 lines
Quoted 4 lines
drivers/usb/gadget/function/uvc_video.c +4 -0
+    while (count < half_list_size) {
+        req = list_first_entry(&video->req_free, struct usb_request,
+                    list);
+        list_del(&req->list);

drivers/usb/gadget/function/uvc_video.c static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req) · line 362

Quoted 10 lines
drivers/usb/gadget/function/uvc_video.c +10 -0
+        ret = uvcg_video_ep_queue_zero_length(req, video);
+        if (ret < 0) {
+            uvcg_queue_cancel(&video->queue, /*disconnect*/0);
+            break;
+        }
+        count++;
+    }
+    spin_unlock_irqrestore(&video->req_lock, flags);
+}
+

So if I'm understanding the new starting sequence right for an isoc endpoint there's an initial flight of half the requests (between 2 and 32) that are queued as zero length - the very first one to .complete() being re-queued as a zero length request before the workqueue is started and encodes data into the _other_ half of the requests which were left in req_free and putting them into req_ready. At that point the .complete()s being run start to pick requests off req_ready instead and they get sent out with data...does that sound right?

Quoted 7 lines
Quoted 6 lines

That is correct - the first half of number of usb requests allocated (2, 32) are queued at zero length initially. We’ll have half of the requests being sent to the ep in flight and half in the free list yes. queue_work will actually start with either uvc_v4l2_qbuf (uvc_v4l2.c) or at a zero length request being completed - whichever comes first.

Quoted 1 line

What are the implications of those initial 3-33 zero length requests? What kind of latency can that introduce to the start of the video stream?

At the worst, we’ll have  a 32 x 125us(uframe period) = 4ms  delay for the first frame of the uvc buffer stream being sent out to the usb controller. After that, since uvc buffers are typically queued at a much lower rate than usb requests being sent to the endpoint, we should be fine ?

I think that the 'ongoing' stream should be fine using this method yes, though if possible I'd like to avoid introducing the delay to the first frame. Do you know if there's a simple way to remove it? I recognise the delay is small so I don't think it's necessarily a dealbreaker but it would be nice if we could avoid it.

We could introduce a flag and have the async_wq thread queue requests to the ep for the first uvc buffer. However, what that would do is it would possibly add a skew between the first and second frames. Let's say we send out frame 1 at t = 0ms. It is possible that by the time frame 2 comes around, we have 32 0 length usb requests queued up in the usb controller. As a result the time distance between frame 1's start and frame 2's start would be 33ms + 4ms = 37ms (instead of 4ms and 37ms). So its a tradeoff between skew vs 4ms delay in starting the stream. The current logic avoids the skew at the expense of the delay in the first frame - and its simpler to follow in code. Happy to hear your and others' thoughts on this as well.

Alright; I like the skew less than the delay. Like I say I don't think it's a dealbreaker, so let's leave that part as is.

Quoted 205 lines
Quoted 205 lines
Quoted 55 lines
Quoted 55 lines

In my local testing, I don't see any delay observable to the naked eye.

Quoted 35 lines
Quoted 35 lines
drivers/usb/gadget/function/uvc_video.c +21 -1
    static void
    uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
    {
@@ -276,6 +368,8 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
        struct uvc_video_queue *queue = &video->queue;
        struct uvc_buffer *last_buf = NULL;
        unsigned long flags;
+    bool is_bulk = video->max_payload_size;
+    int ret = 0;
          spin_lock_irqsave(&video->req_lock, flags);
        if (!video->is_enabled) {
@@ -329,7 +423,38 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
         * back to req_free
         */
        if (video->is_enabled) {
-        list_add_tail(&req->list, &video->req_free);
+        /*
+         * Here we check whether any request is available in the ready
+         * list. If it is, queue it to the ep and add the current
+         * usb_request to the req_free list - for video_pump to fill in.
+         * Otherwise, just use the current usb_request to queue a 0
+         * length request to the ep. Since we always add to the req_free
+         * list if we dequeue from the ready list, there will never
+         * be a situation where the req_free list is completely out of
+         * requests and cannot recover.
+         */
+        struct usb_request *to_queue = req;
+        to_queue->length = 0;
+        if (!list_empty(&video->req_ready)) {
+            to_queue = list_first_entry(&video->req_ready,
+                struct usb_request, list);
+            list_del(&to_queue->list);
+            /* Add it to the free list. */
+            list_add_tail(&req->list, &video->req_free);
+        }
Quoted 8 lines

drivers/usb/gadget/function/uvc_video.c uvc_video_complete(struct usb_ep *ep, struct usb_request *req) · line 451

Quoted 7 lines
drivers/usb/gadget/function/uvc_video.c +7 -0
+        /*
+         * Queue to the endpoint. The actual queueing to ep will
+         * only happen on one thread - the async_wq for bulk endpoints
+         * and this thread for isoc endpoints.
+         */
+        ret = uvcg_video_usb_req_queue(video, to_queue,
+                           /*queue_to_ep*/!is_bulk);

In principle in-line comments are fine, but I don't think the parameter name is worth a comment

Done.

Quoted 6 lines
Quoted 6 lines
drivers/usb/gadget/function/uvc_video.c +6 -0
+        if(ret < 0) {
+            uvcg_queue_cancel(queue, 0);
+        }
+        /* Queue work to the wq as well since its possible that a buffer
+         * may not have been completed.
+         */
Quoted 4 lines
Quoted 4 lines
Quoted 1 line

The phrasing of this implies this is a bit of defensive programming, but if we don't queue to the wq here then doesn't that mean it'll never run?

I've updated the comment here - it is possible that we hit a situation where the in-flight usb requests may not be enough to completely encode a uvc buffer. In that case if we don't call queue_work, we'll never get the buffer marked as 'completed' and the buffer won't be returned to user-space. That'll prevent the dequeue->queue->dequeue loop and flow of buffers.

I added this queue_work call to the if statement which checks if the ready list has any requests. If it doesn't, there's no point in queuing any work - since we wouldn't be giving back to the req_free list.

Thank you

Quoted 69 lines
Quoted 69 lines
Quoted 69 lines
Quoted 58 lines
drivers/usb/gadget/function/uvc_video.c +2 -24
            queue_work(video->async_wq, &video->pump);
        } else {
            uvc_video_free_request(ureq, ep);
@@ -347,6 +472,7 @@ uvc_video_free_requests(struct uvc_video *video)
          INIT_LIST_HEAD(&video->ureqs);
        INIT_LIST_HEAD(&video->req_free);
+    INIT_LIST_HEAD(&video->req_ready);
        video->req_size = 0;
        return 0;
    }
@@ -424,8 +550,7 @@ static void uvcg_video_pump(struct work_struct *work)
        struct usb_request *req = NULL;
        struct uvc_buffer *buf;
        unsigned long flags;
-    bool buf_done;
-    int ret;
+    int ret = 0;
          while (true) {
            if (!video->ep->enabled)
@@ -454,7 +579,6 @@ static void uvcg_video_pump(struct work_struct *work)
              if (buf != NULL) {
                video->encode(req, video, buf);
-            buf_done = buf->state == UVC_BUF_STATE_DONE;
            } else if (!(queue->flags & UVC_QUEUE_DISCONNECTED) && !is_bulk) {
                /*
                 * No video buffer available; the queue is still connected and
@@ -462,7 +586,6 @@ static void uvcg_video_pump(struct work_struct *work)
                 * prevent missed ISOC transfers.
                 */
                req->length = 0;
-            buf_done = false;
            } else {
                /*
                 * Either the queue has been disconnected or no video buffer
@@ -473,45 +596,26 @@ static void uvcg_video_pump(struct work_struct *work)
                break;
            }
    -        /*
-         * With USB3 handling more requests at a higher speed, we can't
-         * afford to generate an interrupt for every request. Decide to
-         * interrupt:
-         *
-         * - When no more requests are available in the free queue, as
-         *   this may be our last chance to refill the endpoint's
-         *   request queue.
-         *
-         * - When this is request is the last request for the video
-         *   buffer, as we want to start sending the next video buffer
-         *   ASAP in case it doesn't get started already in the next
-         *   iteration of this loop.
-         *
-         * - Four times over the length of the requests queue (as
-         *   indicated by video->uvc_num_requests), as a trade-off
-         *   between latency and interrupt load.
-         */
-        if (list_empty(&video->req_free) || buf_done ||
-            !(video->req_int_count %
-               DIV_ROUND_UP(video->uvc_num_requests, 4))) {

drivers/usb/gadget/function/uvc_video.c static void uvcg_video_pump(struct work_struct *work) · line 600

Quoted 10 lines
drivers/usb/gadget/function/uvc_video.c +0 -8
-            video->req_int_count = 0;
-            req->no_interrupt = 0;
-        } else {
-            req->no_interrupt = 1;
-        }
-
-        /* Queue the USB request */
-        ret = uvcg_video_ep_queue(video, req);
            spin_unlock_irqrestore(&queue->irqlock, flags);
    +        /* Queue the USB request.*/

I think just drop this - it was always superfluous.

Quoted 5 lines
Quoted 4 lines

The uvcg_video_usb_req_queue function mentions that req_lock must be held while calling it - since its possible we might add to the req_ready list. We could say the function should hold req_lock only when the queue_to_ep parameter is false - but that doesn't seem as clean ?

Sorry - I wasn't clear here. I meant that the comment "Queue the USB request" was superfluous rather than the spin_lock_irqsave()

Removed.

Quoted 65 lines
Quoted 65 lines
Quoted 26 lines
Quoted 21 lines
drivers/usb/gadget/function/uvc_video.c +11 -1
+        spin_lock_irqsave(&video->req_lock, flags);
+        /* For bulk end points we queue from the worker thread
+         * since we would preferably not want to wait on requests
+         * to be ready, in the uvcg_video_complete() handler.
+         * For isoc endpoints we add the request to the ready list
+         * and only queue it to the endpoint from the complete handler.
+         */
+        ret = uvcg_video_usb_req_queue(video, req, is_bulk);
+        spin_unlock_irqrestore(&video->req_lock, flags);
+
            if (ret < 0) {
                uvcg_queue_cancel(queue, 0);
                break;
            }
    -        /* Endpoint now owns the request */
+        /* The request is owned by  the endpoint / ready list*/
            req = NULL;
-        video->req_int_count++;
        }
          if (!req)

drivers/usb/gadget/function/uvc_video.c uvcg_video_disable(struct uvc_video *video) · line 673

Quoted 4 lines
drivers/usb/gadget/function/uvc_video.c +1 -1
@@ -567,7 +671,7 @@ uvcg_video_disable(struct uvc_video *video)
          spin_lock_irqsave(&video->req_lock, flags);
        /*
-     * Remove all uvc_reqeusts from ureqs with list_del_init
+     * Remove all uvc_requests from ureqs with list_del_init

This should get fixed in the earlier series.

Quoted 9 lines

drivers/usb/gadget/function/uvc_video.c uvcg_video_disable(struct uvc_video *video) · line 582

Quoted 8 lines
drivers/usb/gadget/function/uvc_video.c +0 -1
         * This lets uvc_video_free_request correctly identify
         * if the uvc_request is attached to a list or not when freeing
         * memory.
@@ -579,9 +683,13 @@ uvcg_video_disable(struct uvc_video *video)
            list_del(&req->list);
            uvc_video_free_request(req->context, video->ep);
        }
-

keep the empty line please

Done.

Quoted 5 lines

drivers/usb/gadget/function/uvc_video.c uvcg_video_disable(struct uvc_video *video) · line 689

Quoted 4 lines
drivers/usb/gadget/function/uvc_video.c +4 -0
+    list_for_each_entry_safe(req, temp, &video->req_ready, list) {
+        list_del(&req->list);
+        uvc_video_free_request(req->context, video->ep);
+    }

and one here too.

Done.

Thanks!

Quoted 19 lines
Quoted 19 lines
drivers/usb/gadget/function/uvc_video.c +1 -0
        INIT_LIST_HEAD(&video->ureqs);
        INIT_LIST_HEAD(&video->req_free);
+    INIT_LIST_HEAD(&video->req_ready);
        video->req_size = 0;
        spin_unlock_irqrestore(&video->req_lock, flags);
    @@ -635,7 +743,7 @@ int uvcg_video_enable(struct uvc_video *video)

         video->req_int_count = 0;
   -    queue_work(video->async_wq, &video->pump);
+    uvc_video_ep_queue_initial_requests(video);
         return ret;
   }

drivers/usb/gadget/function/uvc_video.c +1 -0
@@ -648,6 +756,7 @@ int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
        video->is_enabled = false;
        INIT_LIST_HEAD(&video->ureqs);
        INIT_LIST_HEAD(&video->req_free);
+    INIT_LIST_HEAD(&video->req_ready);
        spin_lock_init(&video->req_lock);
        INIT_WORK(&video->pump, uvcg_video_pump);