2020-04-21 13:32:21

by Matwey V. Kornilov

[permalink] [raw]
Subject: [PATCH v3 0/6] musb: Improve performance for hub-attached webcams

From: "Matwey V. Kornilov" <[email protected]>

The series is concerned to issues with isochronous transfer while
streaming the USB webcam data. I discovered the issue first time
when attached PWC USB webcam to AM335x-based BeagleBone Black SBC.
It appeared that the root issue was in numerous missed IN requests
during isochronous transfer where each missing leaded to the frame
drop. Since every IN request is triggered in MUSB driver
individually, it is important to queue the send IN request as
earlier as possible when the previous IN completed. At the same
time the URB giveback handler of the device driver has also to be
called there, that leads to arbitrarily delay depending on the
device driver performance. The details with the references are
described in [1].

The issue has two parts:

1) peripheral driver URB callback performance
2) MUSB host driver performance

It appeared that the first part is related to the wrong memory
allocation strategy in the most USB webcam drivers. Non-cached
memory is used in assumption that coherent DMA memory leads to
the better performance than non-coherent memory in conjunction with
the proper synchronization. Yet the assumption might be valid for
x86 platforms some time ago, the issue was fixed for PWC driver in:

1161db6776bd ("media: usb: pwc: Don't use coherent DMA buffers for ISO transfer")

that leads to 3.5x performance gain. The more generic fix for this
common issue are coming for the rest drivers [2].

The patch allowed successfully running full-speed USB PWC webcams
attached directly to BeagleBone Black USB port.

However, the second part of the issue is still present for
peripheral device attached through the high-speed USB hub due to
its 125us frame time. The patch series is intended to reorganize
musb_advance_schedule() to allow host to send IN request quicker.

The patch series is organized as the following. First three patches
improve readability of the existing code in
musb_advance_schedule(). Patches 4 and 5 introduce updated
signature for musb_start_urb(). The last patch introduce new
code-path in musb_advance_schedule() which allows for faster
response.

References:

[1] https://www.spinics.net/lists/linux-usb/msg165735.html
[2] https://www.spinics.net/lists/linux-media/msg144279.html

Changes since v2:
- rebase changes onto v5.7-rc2
Changes since v1:
- Patch 6 was redone to keep URB giveback order and stop transmission at
erroneous URB.

Matwey V. Kornilov (6):
usb: musb: Use USB_DIR_IN when calling musb_advance_schedule()
usb: musb: Introduce musb_qh_empty() helper function
usb: musb: Introduce musb_qh_free() helper function
usb: musb: Rename musb_start_urb() to musb_start_next_urb()
usb: musb: Introduce musb_start_urb()
usb: musb: Decrease URB starting latency in musb_advance_schedule()

drivers/usb/musb/musb_host.c | 132 ++++++++++++++++++++++-------------
drivers/usb/musb/musb_host.h | 1 +
2 files changed, 86 insertions(+), 47 deletions(-)

--
2.25.0


2020-04-21 13:32:28

by Matwey V. Kornilov

[permalink] [raw]
Subject: [PATCH v3 1/6] usb: musb: Use USB_DIR_IN when calling musb_advance_schedule()

From: "Matwey V. Kornilov" <[email protected]>

Use USB_DIR_IN instead of 1 when calling musb_advance_schedule().
This is consistent with the rest of musb_host.c code and impoves
the readability.

Signed-off-by: Matwey V. Kornilov <[email protected]>
---
drivers/usb/musb/musb_host.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/usb/musb/musb_host.c b/drivers/usb/musb/musb_host.c
index 8736f4251a22..b0614c59e0e5 100644
--- a/drivers/usb/musb/musb_host.c
+++ b/drivers/usb/musb/musb_host.c
@@ -1169,7 +1169,7 @@ irqreturn_t musb_h_ep0_irq(struct musb *musb)

/* call completion handler if done */
if (complete)
- musb_advance_schedule(musb, urb, hw_ep, 1);
+ musb_advance_schedule(musb, urb, hw_ep, USB_DIR_IN);
done:
return retval;
}
--
2.25.0

2020-04-21 13:32:39

by Matwey V. Kornilov

[permalink] [raw]
Subject: [PATCH v3 2/6] usb: musb: Introduce musb_qh_empty() helper function

From: "Matwey V. Kornilov" <[email protected]>

Use musb_qh_empty() instead of &qh->hep->urb_list to avoid code
duplicating.

Signed-off-by: Matwey V. Kornilov <[email protected]>
---
drivers/usb/musb/musb_host.c | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/drivers/usb/musb/musb_host.c b/drivers/usb/musb/musb_host.c
index b0614c59e0e5..af906f072cb3 100644
--- a/drivers/usb/musb/musb_host.c
+++ b/drivers/usb/musb/musb_host.c
@@ -80,6 +80,11 @@ static void musb_ep_program(struct musb *musb, u8 epnum,
struct urb *urb, int is_out,
u8 *buf, u32 offset, u32 len);

+static bool musb_qh_empty(struct musb_qh *qh)
+{
+ return list_empty(&qh->hep->urb_list);
+}
+
/*
* Clear TX fifo. Needed to avoid BABBLE errors.
*/
@@ -324,7 +329,7 @@ static void musb_advance_schedule(struct musb *musb, struct urb *urb,
/* reclaim resources (and bandwidth) ASAP; deschedule it, and
* invalidate qh as soon as list_empty(&hep->urb_list)
*/
- if (list_empty(&qh->hep->urb_list)) {
+ if (musb_qh_empty(qh)) {
struct list_head *head;
struct dma_controller *dma = musb->dma_controller;

@@ -2397,7 +2402,7 @@ static int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
/* If nothing else (usually musb_giveback) is using it
* and its URB list has emptied, recycle this qh.
*/
- if (ready && list_empty(&qh->hep->urb_list)) {
+ if (ready && musb_qh_empty(qh)) {
qh->hep->hcpriv = NULL;
list_del(&qh->ring);
kfree(qh);
@@ -2442,7 +2447,7 @@ musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
/* Then nuke all the others ... and advance the
* queue on hw_ep (e.g. bulk ring) when we're done.
*/
- while (!list_empty(&hep->urb_list)) {
+ while (!musb_qh_empty(qh)) {
urb = next_urb(qh);
urb->status = -ESHUTDOWN;
musb_advance_schedule(musb, urb, qh->hw_ep, is_in);
@@ -2452,7 +2457,7 @@ musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
* other transfers, and since !qh->is_ready nothing
* will activate any of these as it advances.
*/
- while (!list_empty(&hep->urb_list))
+ while (!musb_qh_empty(qh))
musb_giveback(musb, next_urb(qh), -ESHUTDOWN);

hep->hcpriv = NULL;
--
2.25.0

2020-04-21 13:32:59

by Matwey V. Kornilov

[permalink] [raw]
Subject: [PATCH v3 4/6] usb: musb: Rename musb_start_urb() to musb_start_next_urb()

From: "Matwey V. Kornilov" <[email protected]>

In the following commit we introduce new musb_start_urb() function
which will be able to start arbitrary urb. In order to have
intuitive function names we rename musb_start_urb() to
musb_start_next_urb().

Signed-off-by: Matwey V. Kornilov <[email protected]>
---
drivers/usb/musb/musb_host.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/usb/musb/musb_host.c b/drivers/usb/musb/musb_host.c
index 28d6576b6d8d..c3c9aa8ba973 100644
--- a/drivers/usb/musb/musb_host.c
+++ b/drivers/usb/musb/musb_host.c
@@ -213,7 +213,7 @@ static struct musb_qh *musb_ep_get_qh(struct musb_hw_ep *ep, int is_in)
* Context: controller locked, irqs blocked
*/
static void
-musb_start_urb(struct musb *musb, int is_in, struct musb_qh *qh)
+musb_start_next_urb(struct musb *musb, int is_in, struct musb_qh *qh)
{
u32 len;
void __iomem *mbase = musb->mregs;
@@ -385,7 +385,7 @@ static void musb_advance_schedule(struct musb *musb, struct urb *urb,
if (qh != NULL && qh->is_ready) {
musb_dbg(musb, "... next ep%d %cX urb %p",
hw_ep->epnum, is_in ? 'R' : 'T', next_urb(qh));
- musb_start_urb(musb, is_in, qh);
+ musb_start_next_urb(musb, is_in, qh);
}
}

@@ -975,7 +975,7 @@ static void musb_bulk_nak_timeout(struct musb *musb, struct musb_hw_ep *ep,
}

if (next_qh)
- musb_start_urb(musb, is_in, next_qh);
+ musb_start_next_urb(musb, is_in, next_qh);
}
}

@@ -2108,7 +2108,7 @@ static int musb_schedule(
qh->hw_ep = hw_ep;
qh->hep->hcpriv = qh;
if (idle)
- musb_start_urb(musb, is_in, qh);
+ musb_start_next_urb(musb, is_in, qh);
return 0;
}

--
2.25.0

2020-04-21 13:33:11

by Matwey V. Kornilov

[permalink] [raw]
Subject: [PATCH v3 6/6] usb: musb: Decrease URB starting latency in musb_advance_schedule()

From: "Matwey V. Kornilov" <[email protected]>

Previously, the algorithm was the following:

1. giveback current URB
2. if current qh is not empty
then start next URB
3. if current qh is empty
then dispose the qh, find next qh if any, and start URB.

It may take a while to run urb->callback inside URB giveback which is
run synchronously in musb. In order to improve the latency we rearrange
the function behaviour for the case when qh is not empty: next URB is
started before URB giveback. When qh is empty or current URB has an
error then the behaviour is intentionally kept in order
a) not to break existing inter qh scheduling: URB giveback could
potentionally enqueue other URB to the empty qh preventing it from being
disposed;
b) allow the class driver to cancel outstanding URBs in the queue.

Correct URB giveback order is guaranteed as the following. For each
qh there can be at most three ready URBs processing by the driver.
Indeed, every ready URB can send at most one URB in
musb_advance_schedule(), and in the worst case scenario we have the
following ready URBs:
1) URB in the giveback lock protected section inside musb_giveback()
2) URB waiting at the giveback lock acqusition in musb_giveback()
3) URB waiting at the controller lock acqusition in the glue layer
interrput handler
Here URB #2 and URB #3 are triggered by URB #1 and URB #2
correspondingly when they passed through musb_advance_schedule().
Since URB #3 is waiting before musb_advance_schedule(), no other new
URBs will be sent until URB#1 is finished, URB#2 goes to the giveback
lock protected section, and URB#3 goes to the controller lock protected
musb_advance_schedule().

Before this patch, time spent in urb->callback led to the following
glitches between the host and a hub during isoc transfer (line 4):

11.624492 d= 0.000124 [130.6 + 1.050] [ 4] SPLIT
11.624492 d= 0.000000 [130.6 + 1.467] [ 3] IN : 3.5
11.624493 d= 0.000000 [130.6 + 1.967] [ 37] DATA0: aa 08 [skipped...]
11.625617 d= 0.001124 [131.7 + 1.050] [ 4] SPLIT
11.625617 d= 0.000000 [131.7 + 1.467] [ 3] IN : 3.5
11.625867 d= 0.000250 [132.1 + 1.050] [ 4] SPLIT
11.625867 d= 0.000000 [132.1 + 1.467] [ 3] IN : 3.5
11.625868 d= 0.000001 [132.1 + 1.983] [ 3] DATA0: 00 00
11.626617 d= 0.000749 [132.7 + 1.050] [ 4] SPLIT
11.626617 d= 0.000000 [132.7 + 1.467] [ 3] IN : 3.5
11.626867 d= 0.000250 [133.1 + 1.050] [ 4] SPLIT
11.626867 d= 0.000000 [133.1 + 1.467] [ 3] IN : 3.5
11.626868 d= 0.000000 [133.1 + 1.967] [ 3] DATA0: 00 00

After the hub, they look as the following and may lead to broken
perepherial transfer (as in case of PWC based webcam):

11.332004 d= 0.000997 [ 30.0 + 3.417] [ 3] IN : 5.5
11.332007 d= 0.000003 [ 30.0 + 6.833] [800] DATA0: 8a 1c [skipped...]
11.334004 d= 0.001997 [ 32.0 + 3.417] [ 3] IN : 5.5
11.334007 d= 0.000003 [ 32.0 + 6.750] [ 3] DATA0: 00 00
11.335004 d= 0.000997 [ 33 + 3.417] [ 3] IN : 5.5
11.335007 d= 0.000003 [ 33 + 6.750] [ 3] DATA0: 00 00

Removing this glitches makes us able to successfully run 10fps
video stream from the webcam attached via USB hub. That was
previously impossible.

Signed-off-by: Matwey V. Kornilov <[email protected]>
---
drivers/usb/musb/musb_host.c | 36 ++++++++++++++++++++++++++++++++----
drivers/usb/musb/musb_host.h | 1 +
2 files changed, 33 insertions(+), 4 deletions(-)

diff --git a/drivers/usb/musb/musb_host.c b/drivers/usb/musb/musb_host.c
index 46d9dd7d6f67..863f55907ddc 100644
--- a/drivers/usb/musb/musb_host.c
+++ b/drivers/usb/musb/musb_host.c
@@ -85,6 +85,11 @@ static bool musb_qh_empty(struct musb_qh *qh)
return list_empty(&qh->hep->urb_list);
}

+static bool musb_qh_singular(struct musb_qh *qh)
+{
+ return list_is_singular(&qh->hep->urb_list);
+}
+
static void musb_qh_unlink_hep(struct musb_qh *qh)
{
if (!qh->hep)
@@ -301,15 +306,24 @@ musb_start_next_urb(struct musb *musb, int is_in, struct musb_qh *qh)
}

/* Context: caller owns controller lock, IRQs are blocked */
-static void musb_giveback(struct musb *musb, struct urb *urb, int status)
+static void musb_giveback(struct musb *musb, struct musb_qh *qh, struct urb *urb, int status)
__releases(musb->lock)
__acquires(musb->lock)
{
trace_musb_urb_gb(musb, urb);

+ /*
+ * This line is protected by the controller lock: at most
+ * one thread waiting on the giveback lock.
+ */
+ spin_lock(&qh->giveback_lock);
usb_hcd_unlink_urb_from_ep(musb->hcd, urb);
+
spin_unlock(&musb->lock);
+
usb_hcd_giveback_urb(musb->hcd, urb, status);
+ spin_unlock(&qh->giveback_lock);
+
spin_lock(&musb->lock);
}

@@ -344,8 +358,21 @@ static void musb_advance_schedule(struct musb *musb, struct urb *urb,
break;
}

+ if (ready && !musb_qh_singular(qh) && !status) {
+ struct urb *next_urb = list_next_entry(urb, urb_list);
+
+ musb_dbg(musb, "... next ep%d %cX urb %p", hw_ep->epnum, is_in ? 'R' : 'T', next_urb);
+ musb_start_urb(musb, is_in, qh, next_urb);
+
+ qh->is_ready = 0;
+ musb_giveback(musb, qh, urb, status);
+ qh->is_ready = ready;
+
+ return;
+ }
+
qh->is_ready = 0;
- musb_giveback(musb, urb, status);
+ musb_giveback(musb, qh, urb, status);
qh->is_ready = ready;

/* reclaim resources (and bandwidth) ASAP; deschedule it, and
@@ -2174,6 +2201,7 @@ static int musb_urb_enqueue(
qh->hep = hep;
qh->dev = urb->dev;
INIT_LIST_HEAD(&qh->ring);
+ spin_lock_init(&qh->giveback_lock);
qh->is_ready = 1;

qh->maxpacket = usb_endpoint_maxp(epd);
@@ -2406,7 +2434,7 @@ static int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
int ready = qh->is_ready;

qh->is_ready = 0;
- musb_giveback(musb, urb, 0);
+ musb_giveback(musb, qh, urb, 0);
qh->is_ready = ready;

/* If nothing else (usually musb_giveback) is using it
@@ -2465,7 +2493,7 @@ musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
* will activate any of these as it advances.
*/
while (!musb_qh_empty(qh))
- musb_giveback(musb, next_urb(qh), -ESHUTDOWN);
+ musb_giveback(musb, qh, next_urb(qh), -ESHUTDOWN);

musb_qh_free(qh);
}
diff --git a/drivers/usb/musb/musb_host.h b/drivers/usb/musb/musb_host.h
index 2999845632ce..6223b0177c68 100644
--- a/drivers/usb/musb/musb_host.h
+++ b/drivers/usb/musb/musb_host.h
@@ -19,6 +19,7 @@ struct musb_qh {
struct musb_hw_ep *hw_ep; /* current binding */

struct list_head ring; /* of musb_qh */
+ spinlock_t giveback_lock; /* to keep URB giveback order */
/* struct musb_qh *next; */ /* for periodic tree */
u8 mux; /* qh multiplexed to hw_ep */

--
2.25.0

2020-04-21 13:33:23

by Matwey V. Kornilov

[permalink] [raw]
Subject: [PATCH v3 5/6] usb: musb: Introduce musb_start_urb()

From: "Matwey V. Kornilov" <[email protected]>

This function allows us to start arbitrary urb.

Signed-off-by: Matwey V. Kornilov <[email protected]>
---
drivers/usb/musb/musb_host.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/musb/musb_host.c b/drivers/usb/musb/musb_host.c
index c3c9aa8ba973..46d9dd7d6f67 100644
--- a/drivers/usb/musb/musb_host.c
+++ b/drivers/usb/musb/musb_host.c
@@ -213,11 +213,10 @@ static struct musb_qh *musb_ep_get_qh(struct musb_hw_ep *ep, int is_in)
* Context: controller locked, irqs blocked
*/
static void
-musb_start_next_urb(struct musb *musb, int is_in, struct musb_qh *qh)
+musb_start_urb(struct musb *musb, int is_in, struct musb_qh *qh, struct urb *urb)
{
u32 len;
void __iomem *mbase = musb->mregs;
- struct urb *urb = next_urb(qh);
void *buf = urb->transfer_buffer;
u32 offset = 0;
struct musb_hw_ep *hw_ep = qh->hw_ep;
@@ -293,6 +292,14 @@ musb_start_next_urb(struct musb *musb, int is_in, struct musb_qh *qh)
}
}

+static void
+musb_start_next_urb(struct musb *musb, int is_in, struct musb_qh *qh)
+{
+ struct urb *urb = next_urb(qh);
+
+ musb_start_urb(musb, is_in, qh, urb);
+}
+
/* Context: caller owns controller lock, IRQs are blocked */
static void musb_giveback(struct musb *musb, struct urb *urb, int status)
__releases(musb->lock)
--
2.25.0

2020-04-21 13:34:42

by Matwey V. Kornilov

[permalink] [raw]
Subject: [PATCH v3 3/6] usb: musb: Introduce musb_qh_free() helper function

From: "Matwey V. Kornilov" <[email protected]>

Reduce the following similar snippets by using musb_qh_free().

qh->hep->hcpriv = NULL;
list_del(&qh->ring);
kfree(qh);

Signed-off-by: Matwey V. Kornilov <[email protected]>
---
drivers/usb/musb/musb_host.c | 66 +++++++++++++++++-------------------
1 file changed, 32 insertions(+), 34 deletions(-)

diff --git a/drivers/usb/musb/musb_host.c b/drivers/usb/musb/musb_host.c
index af906f072cb3..28d6576b6d8d 100644
--- a/drivers/usb/musb/musb_host.c
+++ b/drivers/usb/musb/musb_host.c
@@ -85,6 +85,21 @@ static bool musb_qh_empty(struct musb_qh *qh)
return list_empty(&qh->hep->urb_list);
}

+static void musb_qh_unlink_hep(struct musb_qh *qh)
+{
+ if (!qh->hep)
+ return;
+
+ qh->hep->hcpriv = NULL;
+}
+
+static void musb_qh_free(struct musb_qh *qh)
+{
+ musb_qh_unlink_hep(qh);
+ list_del(&qh->ring);
+ kfree(qh);
+}
+
/*
* Clear TX fifo. Needed to avoid BABBLE errors.
*/
@@ -330,7 +345,7 @@ static void musb_advance_schedule(struct musb *musb, struct urb *urb,
* invalidate qh as soon as list_empty(&hep->urb_list)
*/
if (musb_qh_empty(qh)) {
- struct list_head *head;
+ struct list_head *head = NULL;
struct dma_controller *dma = musb->dma_controller;

if (is_in) {
@@ -349,34 +364,22 @@ static void musb_advance_schedule(struct musb *musb, struct urb *urb,

/* Clobber old pointers to this qh */
musb_ep_set_qh(ep, is_in, NULL);
- qh->hep->hcpriv = NULL;

- switch (qh->type) {
+ /* USB_ENDPOINT_XFER_CONTROL and USB_ENDPOINT_XFER_BULK: fifo
+ * policy for these lists, except that NAKing should rotate
+ * a qh to the end (for fairness).
+ * USB_ENDPOINT_XFER_ISOC and USB_ENDPOINT_XFER_INT: this is
+ * where periodic bandwidth should be de-allocated if it's
+ * tracked and allocated; and where we'd update the schedule
+ * tree...
+ */
+ if (qh->mux == 1
+ && (qh->type == USB_ENDPOINT_XFER_CONTROL || qh->type == USB_ENDPOINT_XFER_BULK))
+ head = qh->ring.prev;

- case USB_ENDPOINT_XFER_CONTROL:
- case USB_ENDPOINT_XFER_BULK:
- /* fifo policy for these lists, except that NAKing
- * should rotate a qh to the end (for fairness).
- */
- if (qh->mux == 1) {
- head = qh->ring.prev;
- list_del(&qh->ring);
- kfree(qh);
- qh = first_qh(head);
- break;
- }
- /* fall through */
+ musb_qh_free(qh);

- case USB_ENDPOINT_XFER_ISOC:
- case USB_ENDPOINT_XFER_INT:
- /* this is where periodic bandwidth should be
- * de-allocated if it's tracked and allocated;
- * and where we'd update the schedule tree...
- */
- kfree(qh);
- qh = NULL;
- break;
- }
+ qh = head ? first_qh(head) : NULL;
}

if (qh != NULL && qh->is_ready) {
@@ -2402,11 +2405,8 @@ static int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
/* If nothing else (usually musb_giveback) is using it
* and its URB list has emptied, recycle this qh.
*/
- if (ready && musb_qh_empty(qh)) {
- qh->hep->hcpriv = NULL;
- list_del(&qh->ring);
- kfree(qh);
- }
+ if (ready && musb_qh_empty(qh))
+ musb_qh_free(qh);
} else
ret = musb_cleanup_urb(urb, qh);
done:
@@ -2460,9 +2460,7 @@ musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
while (!musb_qh_empty(qh))
musb_giveback(musb, next_urb(qh), -ESHUTDOWN);

- hep->hcpriv = NULL;
- list_del(&qh->ring);
- kfree(qh);
+ musb_qh_free(qh);
}
exit:
spin_unlock_irqrestore(&musb->lock, flags);
--
2.25.0