Skip to contentSkip to conversation

usb: dwc3: gadget: Fix isoc interrupt check

PATCH Superseded Thinh Nguyen 2 participants 10 messages Oct 25, 2022

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Thinh Nguyen

author maintainer
#0 lore raw

v2 cover usb: dwc3: gadget: Fix isoc interrupt check

Fix issues where usb_request->no_interrupt is set for isoc transfers.
* Make sure no interrupt is asserted if no_interrupt is set
* Make sure to stop reclaiming TRBs when the driver needs to stop

Thinh Nguyen (2):
usb: dwc3: gadget: Stop processing more requests on IMI
usb: dwc3: gadget: Don't set IMI for no_interrupt

 drivers/usb/dwc3/gadget.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

Changes in v2:
* Fix missing cover letter subject
* No need to check for the last chained TRB

base-commit: fb8f60dd1b67520e0e0d7978ef17d015690acfc1

Signature · 1 line
2.28.0
author maintainer
#1 lore raw

PATCH v2 1/2 usb: dwc3: gadget: Stop processing more requests on IMI

When servicing a transfer completion event, the dwc3 driver will reclaim TRBs of started requests up to the request associated with the interrupt event. Currently we don't check for interrupt due to missed isoc, and the driver may attempt to reclaim TRBs beyond the associated event. This causes invalid memory access when the hardware still owns the TRB. If there's a missed isoc TRB with IMI (interrupt on missed isoc), make sure to stop servicing further.

Note that only the last TRB of chained TRBs has its status updated with missed isoc.

Fixes 72246da40f37 ("usb: Introduce DesignWare USB3 DRD Driver")

---
Changes in v2:
- No need to check for CHN=0 since only the last TRB has its status updated to missed isoc

 drivers/usb/dwc3/gadget.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/usb/dwc3/gadget.c +4 -0
index dd8ecbe61bec..230b3c660054 100644
@@ -3248,6 +3248,10 @@ static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
 	if (event->status & DEPEVT_STATUS_SHORT && !chain)
 		return 1;
 
+	if ((trb->ctrl & DWC3_TRB_CTRL_ISP_IMI) &&
+	    DWC3_TRB_SIZE_TRBSTS(trb->size) == DWC3_TRBSTS_MISSED_ISOC)
+		return 1;
+
 	if ((trb->ctrl & DWC3_TRB_CTRL_IOC) ||
 	    (trb->ctrl & DWC3_TRB_CTRL_LST))
 		return 1;
Signature · 1 line
2.28.0
Reviewed-by Tested-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] usb: dwc3: gadget: Stop processing more requests on IMI

Quoted 42 lines · Thinh Nguyen

When servicing a transfer completion event, the dwc3 driver will reclaim TRBs of started requests up to the request associated with the interrupt event. Currently we don't check for interrupt due to missed isoc, and the driver may attempt to reclaim TRBs beyond the associated event. This causes invalid memory access when the hardware still owns the TRB. If there's a missed isoc TRB with IMI (interrupt on missed isoc), make sure to stop servicing further.

Note that only the last TRB of chained TRBs has its status updated with missed isoc.

Fixes 72246da40f37 ("usb: Introduce DesignWare USB3 DRD Driver")

---
Changes in v2:
- No need to check for CHN=0 since only the last TRB has its status updated to missed isoc

 drivers/usb/dwc3/gadget.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/usb/dwc3/gadget.c +4 -0
index dd8ecbe61bec..230b3c660054 100644
@@ -3248,6 +3248,10 @@ static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
 	if (event->status & DEPEVT_STATUS_SHORT && !chain)
 		return 1;
 
+	if ((trb->ctrl & DWC3_TRB_CTRL_ISP_IMI) &&
+	    DWC3_TRB_SIZE_TRBSTS(trb->size) == DWC3_TRBSTS_MISSED_ISOC)
+		return 1;
+
 	if ((trb->ctrl & DWC3_TRB_CTRL_IOC) ||
 	    (trb->ctrl & DWC3_TRB_CTRL_LST))
 		return 1;
Signature · 1 line
2.28.0

Testing shows that the changes appear to work to prevent the arm-smmu panic I was seeing after missed isoc errors. Also, changes to reclaim trbs only up to the associated interrupt event make sense.

Regards,
Jeff

#3 lore raw

Re: [PATCH v2 1/2] usb: dwc3: gadget: Stop processing more requests on IMI

Hi Thinh,

Quoted 48 lines · Jeff Vanhoof
Quoted 42 lines · Thinh Nguyen

When servicing a transfer completion event, the dwc3 driver will reclaim TRBs of started requests up to the request associated with the interrupt event. Currently we don't check for interrupt due to missed isoc, and the driver may attempt to reclaim TRBs beyond the associated event. This causes invalid memory access when the hardware still owns the TRB. If there's a missed isoc TRB with IMI (interrupt on missed isoc), make sure to stop servicing further.

Note that only the last TRB of chained TRBs has its status updated with missed isoc.

Fixes 72246da40f37 ("usb: Introduce DesignWare USB3 DRD Driver")

---
Changes in v2:
- No need to check for CHN=0 since only the last TRB has its status updated to missed isoc

 drivers/usb/dwc3/gadget.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/usb/dwc3/gadget.c +4 -0
index dd8ecbe61bec..230b3c660054 100644
@@ -3248,6 +3248,10 @@ static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
 	if (event->status & DEPEVT_STATUS_SHORT && !chain)
 		return 1;
 
+	if ((trb->ctrl & DWC3_TRB_CTRL_ISP_IMI) &&
+	    DWC3_TRB_SIZE_TRBSTS(trb->size) == DWC3_TRBSTS_MISSED_ISOC)
+		return 1;
+
 	if ((trb->ctrl & DWC3_TRB_CTRL_IOC) ||
 	    (trb->ctrl & DWC3_TRB_CTRL_LST))
 		return 1;
Signature · 1 line
2.28.0

Testing shows that the changes appear to work to prevent the arm-smmu panic I was seeing after missed isoc errors. Also, changes to reclaim trbs only up to the associated interrupt event make sense.

Quoted 6 lines

Regards,
Jeff

I just followed up with Dan and he mentioned that he was still seeing the arm-smmu panic on his baseline. I will work with him this afternoon to better understand what may be going on there. Let's hold off on merging these changes in until we figure out what is going on. He and I are testing off of different baselines (5.10 vs 5.15), different USB speeds (USB 3 vs 2), and are using different hardware, so I don't know yet why we are seeing a difference here.

Regards,
Jeff

#4 lore raw

Re: [PATCH v2 1/2] usb: dwc3: gadget: Stop processing more requests on IMI

Hi Thinh,

Quoted 54 lines · Jeff Vanhoof

Hi Thinh,

Quoted 51 lines · Jeff Vanhoof
Quoted 42 lines · Thinh Nguyen

When servicing a transfer completion event, the dwc3 driver will reclaim TRBs of started requests up to the request associated with the interrupt event. Currently we don't check for interrupt due to missed isoc, and the driver may attempt to reclaim TRBs beyond the associated event. This causes invalid memory access when the hardware still owns the TRB. If there's a missed isoc TRB with IMI (interrupt on missed isoc), make sure to stop servicing further.

Note that only the last TRB of chained TRBs has its status updated with missed isoc.

Fixes 72246da40f37 ("usb: Introduce DesignWare USB3 DRD Driver")

---
Changes in v2:
- No need to check for CHN=0 since only the last TRB has its status updated to missed isoc

 drivers/usb/dwc3/gadget.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/usb/dwc3/gadget.c +4 -0
index dd8ecbe61bec..230b3c660054 100644
@@ -3248,6 +3248,10 @@ static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
 	if (event->status & DEPEVT_STATUS_SHORT && !chain)
 		return 1;
 
+	if ((trb->ctrl & DWC3_TRB_CTRL_ISP_IMI) &&
+	    DWC3_TRB_SIZE_TRBSTS(trb->size) == DWC3_TRBSTS_MISSED_ISOC)
+		return 1;
+
 	if ((trb->ctrl & DWC3_TRB_CTRL_IOC) ||
 	    (trb->ctrl & DWC3_TRB_CTRL_LST))
 		return 1;
Signature · 1 line
2.28.0

Testing shows that the changes appear to work to prevent the arm-smmu panic I was seeing after missed isoc errors. Also, changes to reclaim trbs only up to the associated interrupt event make sense.

Quoted 9 lines
Quoted 3 lines

Regards,
Jeff

I just followed up with Dan and he mentioned that he was still seeing the arm-smmu panic on his baseline. I will work with him this afternoon to better understand what may be going on there. Let's hold off on merging these changes in until we figure out what is going on. He and I are testing off of different baselines (5.10 vs 5.15), different USB speeds (USB 3 vs 2), and are using different hardware, so I don't know yet why we are seeing a difference here.

Regards,
Jeff

Between the changes for PATCH v2 1/2 & PATCH v2 2/2, are there any extra precautions required for when scatter gather is in use? Should the IMI bit be set only for the last item in the sg list? I suspect something in this area but I have no proof yet. Your thoughts?

Thanks,
Jeff

author maintainer
#5 lore raw

PATCH v2 2/2 usb: dwc3: gadget: Don't set IMI for no_interrupt

In reply to Thinh Nguyen

The gadget driver may have a certain expectation of how the request completion flow should be from to its configuration. Make sure the controller driver respect that. That is, don't set IMI (Interrupt on Missed Isoc) when usb_request->no_interrupt is set.

Fixes 72246da40f37 ("usb: Introduce DesignWare USB3 DRD Driver")

---
Changes in v2:
- None

 drivers/usb/dwc3/gadget.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/usb/dwc3/gadget.c +2 -2
index 230b3c660054..702bdf42ad2f 100644
@@ -1292,8 +1292,8 @@ static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
 			trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
 		}
 
-		/* always enable Interrupt on Missed ISOC */
-		trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
+		if (!req->request.no_interrupt)
+			trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
 		break;
 
 	case USB_ENDPOINT_XFER_BULK:
Signature · 1 line
2.28.0
Reviewed-by Tested-by on v2 2/2
#6 lore raw

Re: [PATCH v2 2/2] usb: dwc3: gadget: Don't set IMI for no_interrupt

Quoted 33 lines · Thinh Nguyen

The gadget driver may have a certain expectation of how the request completion flow should be from to its configuration. Make sure the controller driver respect that. That is, don't set IMI (Interrupt on Missed Isoc) when usb_request->no_interrupt is set.

Fixes 72246da40f37 ("usb: Introduce DesignWare USB3 DRD Driver")

---
Changes in v2:
- None

 drivers/usb/dwc3/gadget.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/usb/dwc3/gadget.c +2 -2
index 230b3c660054..702bdf42ad2f 100644
@@ -1292,8 +1292,8 @@ static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
 			trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
 		}
 
-		/* always enable Interrupt on Missed ISOC */
-		trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
+		if (!req->request.no_interrupt)
+			trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
 		break;
 
 	case USB_ENDPOINT_XFER_BULK:
Signature · 1 line
2.28.0

No new issue seen during testing. Described changes make sense to do.

Regards,
Jeff

#7 lore raw

Re: [PATCH v2 2/2] usb: dwc3: gadget: Don't set IMI for no_interrupt

Hi Thinh,

Quoted 30 lines · Jeff Vanhoof
Quoted 29 lines · Thinh Nguyen

The gadget driver may have a certain expectation of how the request completion flow should be from to its configuration. Make sure the controller driver respect that. That is, don't set IMI (Interrupt on Missed Isoc) when usb_request->no_interrupt is set.

Fixes 72246da40f37 ("usb: Introduce DesignWare USB3 DRD Driver")

---
Changes in v2:
- None

 drivers/usb/dwc3/gadget.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/usb/dwc3/gadget.c +2 -2
index 230b3c660054..702bdf42ad2f 100644
@@ -1292,8 +1292,8 @@ static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
 			trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
 		}
 
-		/* always enable Interrupt on Missed ISOC */
-		trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
+		if (!req->request.no_interrupt)
+			trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
 		break;
 
Quoted 5 lines
Quoted 4 lines
drivers/usb/dwc3/gadget.c +0 -0
 	case USB_ENDPOINT_XFER_BULK:
Signature · 1 line
2.28.0

<snip>

For scatter gather, shouldn't the IMI bit be set only for the TRB associated to the last item in the sg list? Do we need to do something similar to what that was done for IOC in this area?

For ex.:
+ if ((!req->request.no_interrupt && !chain) || must_interrupt)
+ trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;

BTW, Dan indicated that this seems to help resolve the crash mentioned in [PATCH v2 1/2] of this chain.

Thanks,
Jeff

author maintainer
#8 lore raw

Re: [PATCH v2 2/2] usb: dwc3: gadget: Don't set IMI for no_interrupt

Quoted 48 lines · Jeff Vanhoof

Hi Thinh,

Quoted 30 lines · Jeff Vanhoof
Quoted 29 lines · Thinh Nguyen

The gadget driver may have a certain expectation of how the request completion flow should be from to its configuration. Make sure the controller driver respect that. That is, don't set IMI (Interrupt on Missed Isoc) when usb_request->no_interrupt is set.

Fixes 72246da40f37 ("usb: Introduce DesignWare USB3 DRD Driver")

---
Changes in v2:
- None

 drivers/usb/dwc3/gadget.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/usb/dwc3/gadget.c +2 -2
index 230b3c660054..702bdf42ad2f 100644
@@ -1292,8 +1292,8 @@ static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
 			trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
 		}
 
-		/* always enable Interrupt on Missed ISOC */
-		trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
+		if (!req->request.no_interrupt)
+			trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
 		break;
 
Quoted 5 lines
Quoted 4 lines
drivers/usb/dwc3/gadget.c +0 -0
 	case USB_ENDPOINT_XFER_BULK:
Signature · 1 line
2.28.0

<snip>

For scatter gather, shouldn't the IMI bit be set only for the TRB associated to the last item in the sg list? Do we need to do something similar to what that was done for IOC in this area?

For ex.:

(quoted diff) +2 -0
+	if ((!req->request.no_interrupt && !chain) || must_interrupt)
+		trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Quoted 3 lines

BTW, Dan indicated that this seems to help resolve the crash mentioned in [PATCH v2 1/2] of this chain.

Actually, that's correct. Just ignore the "must_interrupt" setting. The programming guide actually noted to set the IMI for the last TRB of the chain also.

I'll send a fix to this.

Thanks,
Thinh

author maintainer
#9 lore raw

Re: [PATCH v2 2/2] usb: dwc3: gadget: Don't set IMI for no_interrupt

Quoted 57 lines · Thinh Nguyen
Quoted 48 lines · Jeff Vanhoof

Hi Thinh,

Quoted 30 lines · Jeff Vanhoof
Quoted 29 lines · Thinh Nguyen

The gadget driver may have a certain expectation of how the request completion flow should be from to its configuration. Make sure the controller driver respect that. That is, don't set IMI (Interrupt on Missed Isoc) when usb_request->no_interrupt is set.

Fixes 72246da40f37 ("usb: Introduce DesignWare USB3 DRD Driver")

---
Changes in v2:
- None

 drivers/usb/dwc3/gadget.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/usb/dwc3/gadget.c +2 -2
index 230b3c660054..702bdf42ad2f 100644
@@ -1292,8 +1292,8 @@ static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
 			trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
 		}
 
-		/* always enable Interrupt on Missed ISOC */
-		trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
+		if (!req->request.no_interrupt)
+			trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
 		break;
 
Quoted 5 lines
Quoted 4 lines
drivers/usb/dwc3/gadget.c +0 -0
 	case USB_ENDPOINT_XFER_BULK:
Signature · 1 line
2.28.0

<snip>

For scatter gather, shouldn't the IMI bit be set only for the TRB associated to the last item in the sg list? Do we need to do something similar to what that was done for IOC in this area?

For ex.:

(quoted diff) +2 -0
+	if ((!req->request.no_interrupt && !chain) || must_interrupt)
+		trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Quoted 3 lines

BTW, Dan indicated that this seems to help resolve the crash mentioned in [PATCH v2 1/2] of this chain.

Actually, that's correct. Just ignore the "must_interrupt" setting. The programming guide actually noted to set the IMI for the last TRB of the chain also.

Quoted 2 lines

I'll send a fix to this.

Hm... I wonder if it's still better to ignore no_interrupt and immediately notify the gadget driver of missed isoc. It's redundant to have both IOC and IMI set under the same condition, but I guess it doesn't hurt to set it for clarity.

So it's either

	if (!no_interrupt && !chain)
		trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
or
	if (!chain)
		trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;

Either way, this needs a fix.

Thanks,
Thinh