linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] usb: dwc3: gadget: Properly track pending and queued SG
@ 2021-05-13  3:17 Thinh Nguyen
  2021-05-14  7:26 ` Felipe Balbi
  0 siblings, 1 reply; 2+ messages in thread
From: Thinh Nguyen @ 2021-05-13  3:17 UTC (permalink / raw)
  To: Felipe Balbi, Greg Kroah-Hartman, linux-usb
  Cc: John Youn, stable, Michael Grzeschik

The driver incorrectly uses req->num_pending_sgs to track both the
number of pending and queued SG entries. It only prepares the next
request if the previous is done, and it doesn't update num_pending_sgs
until there is TRB completion interrupt. This may starve the controller
of more TRBs until the num_pending_sgs is decremented.

Fix this by decrementing the num_pending_sgs after they are queued and
properly track both num_mapped_sgs and num_queued_sgs.

Cc: <stable@vger.kernel.org>
Tested-by: Michael Grzeschik <m.grzeschik@pengutronix.de>
Reported-by: Michael Grzeschik <m.grzeschik@pengutronix.de>
Fixes: c96e6725db9d ("usb: dwc3: gadget: Correct the logic for queuing sgs")
Signed-off-by: Thinh Nguyen <Thinh.Nguyen@synopsys.com>
---
 drivers/usb/dwc3/gadget.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c
index 49ca5da5e279..612825a39f82 100644
--- a/drivers/usb/dwc3/gadget.c
+++ b/drivers/usb/dwc3/gadget.c
@@ -1244,6 +1244,7 @@ static int dwc3_prepare_trbs_sg(struct dwc3_ep *dep,
 			req->start_sg = sg_next(s);
 
 		req->num_queued_sgs++;
+		req->num_pending_sgs--;
 
 		/*
 		 * The number of pending SG entries may not correspond to the
@@ -1251,7 +1252,7 @@ static int dwc3_prepare_trbs_sg(struct dwc3_ep *dep,
 		 * don't include unused SG entries.
 		 */
 		if (length == 0) {
-			req->num_pending_sgs -= req->request.num_mapped_sgs - req->num_queued_sgs;
+			req->num_pending_sgs = 0;
 			break;
 		}
 
@@ -2873,15 +2874,15 @@ static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep,
 	struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
 	struct scatterlist *sg = req->sg;
 	struct scatterlist *s;
-	unsigned int pending = req->num_pending_sgs;
+	unsigned int num_queued = req->num_queued_sgs;
 	unsigned int i;
 	int ret = 0;
 
-	for_each_sg(sg, s, pending, i) {
+	for_each_sg(sg, s, num_queued, i) {
 		trb = &dep->trb_pool[dep->trb_dequeue];
 
 		req->sg = sg_next(s);
-		req->num_pending_sgs--;
+		req->num_queued_sgs--;
 
 		ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req,
 				trb, event, status, true);
@@ -2904,7 +2905,7 @@ static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep,
 
 static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req)
 {
-	return req->num_pending_sgs == 0;
+	return req->num_pending_sgs == 0 && req->num_queued_sgs == 0;
 }
 
 static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep,
@@ -2913,7 +2914,7 @@ static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep,
 {
 	int ret;
 
-	if (req->num_pending_sgs)
+	if (req->request.num_mapped_sgs)
 		ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event,
 				status);
 	else

base-commit: d9ff1096a840dddea3d5cfa2149ff7da9f499fb2
-- 
2.28.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] usb: dwc3: gadget: Properly track pending and queued SG
  2021-05-13  3:17 [PATCH] usb: dwc3: gadget: Properly track pending and queued SG Thinh Nguyen
@ 2021-05-14  7:26 ` Felipe Balbi
  0 siblings, 0 replies; 2+ messages in thread
From: Felipe Balbi @ 2021-05-14  7:26 UTC (permalink / raw)
  To: Thinh Nguyen, Greg Kroah-Hartman, linux-usb
  Cc: John Youn, stable, Michael Grzeschik

[-- Attachment #1: Type: text/plain, Size: 891 bytes --]

Thinh Nguyen <Thinh.Nguyen@synopsys.com> writes:

> The driver incorrectly uses req->num_pending_sgs to track both the
> number of pending and queued SG entries. It only prepares the next
> request if the previous is done, and it doesn't update num_pending_sgs
> until there is TRB completion interrupt. This may starve the controller
> of more TRBs until the num_pending_sgs is decremented.
>
> Fix this by decrementing the num_pending_sgs after they are queued and
> properly track both num_mapped_sgs and num_queued_sgs.
>
> Cc: <stable@vger.kernel.org>
> Tested-by: Michael Grzeschik <m.grzeschik@pengutronix.de>
> Reported-by: Michael Grzeschik <m.grzeschik@pengutronix.de>
> Fixes: c96e6725db9d ("usb: dwc3: gadget: Correct the logic for queuing sgs")
> Signed-off-by: Thinh Nguyen <Thinh.Nguyen@synopsys.com>

Acked-by: Felipe Balbi <balbi@kernel.org>

-- 
balbi

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 511 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-05-14  7:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-13  3:17 [PATCH] usb: dwc3: gadget: Properly track pending and queued SG Thinh Nguyen
2021-05-14  7:26 ` Felipe Balbi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).