Skip to contentSkip to conversation

usb: cdns3: three bug fixes for v5.10

PATCH In review Peter Chen 4 participants 10 messages Oct 22, 2020 – Oct 29, 2020

Subsystems (4)
lore
Share & tools
lore

Peter Chen

author
#0 lore raw

v2 cover usb: cdns3: three bug fixes for v5.10

Changes for v2:
- Move position of explicit cast to unsigned int before ((p) << 24) [Patch 1/3]
- No changes for other patches.

Pawel Laszczak (1):
usb: cdns3: Fix on-chip memory overflow issue

Peter Chen (2):
usb: cdns3: gadget: suspicious implicit sign extension
usb: cdns3: gadget: own the lock wrongly at the suspend routine

 drivers/usb/cdns3/ep0.c    |  65 ++++++++++++----------
 drivers/usb/cdns3/gadget.c | 111 +++++++++++++++++++++----------------
 drivers/usb/cdns3/gadget.h |   5 +-
 3 files changed, 100 insertions(+), 81 deletions(-)
Signature · 1 line
2.17.1
author
#1 lore raw

PATCH v2 1/3 usb: cdns3: gadget: suspicious implicit sign extension

The code:
trb->length = cpu_to_le32(TRB_BURST_LEN(priv_ep->trb_burst_size)

	       	| TRB_LEN(length));

TRB_BURST_LEN(priv_ep->trb_burst_size) may be overflow for int 32 if priv_ep->trb_burst_size is equal or larger than 0x80;

Below is the Coverity warning:
sign_extension: Suspicious implicit sign extension: priv_ep->trb_burst_size with type u8 (8 bits, unsigned) is promoted in priv_ep->trb_burst_size << 24 to type int (32 bits, signed), then sign-extended to type unsigned long (64 bits, unsigned). If priv_ep->trb_burst_size << 24 is greater than 0x7FFFFFFF, the upper bits of the result will all be 1.

To fix it, it needs to add an explicit cast to unsigned int type for ((p) << 24).

---
Changes for v2:
- Move position of explicit cast to unsigned int before ((p) << 24) [Patch 1/3]

 drivers/usb/cdns3/gadget.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/cdns3/gadget.h +1 -1
index 1ccecd237530..737377913788 100644
@@ -1072,7 +1072,7 @@ struct cdns3_trb {
 #define TRB_TDL_SS_SIZE_GET(p)	(((p) & GENMASK(23, 17)) >> 17)
 
 /* transfer_len bitmasks - bits 31:24 */
-#define TRB_BURST_LEN(p)	(((p) << 24) & GENMASK(31, 24))
+#define TRB_BURST_LEN(p)	((unsigned int)((p) << 24) & GENMASK(31, 24))
 #define TRB_BURST_LEN_GET(p)	(((p) & GENMASK(31, 24)) >> 24)
 
 /* Data buffer pointer bitmasks*/
Signature · 1 line
2.17.1
author
#2 lore raw

PATCH v2 2/3 usb: cdns3: gadget: own the lock wrongly at the suspend routine

In reply to Peter Chen

When the system goes to suspend, if the controller is at device mode with cable connecting to host, the call stack is: cdns3_suspend-> cdns3_gadget_suspend -> cdns3_disconnect_gadget, after cdns3_disconnect_gadget is called, it owns lock wrongly, it causes the system being deadlock after resume due to at cdns3_device_thread_irq_handler, it tries to get the lock, but can't get it forever.

To fix it, we delete the unlock-lock operations at cdns3_disconnect_gadget, and do it at the caller.

Fixes b1234e3b3b26 ("usb: cdns3: add runtime PM support")
---
 drivers/usb/cdns3/gadget.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/usb/cdns3/gadget.c +5 -4
index 6ff3aa3db497..c127af6c0fe8 100644
@@ -1744,11 +1744,8 @@ static int cdns3_check_ep_interrupt_proceed(struct cdns3_endpoint *priv_ep)
 
 static void cdns3_disconnect_gadget(struct cdns3_device *priv_dev)
 {
-	if (priv_dev->gadget_driver && priv_dev->gadget_driver->disconnect) {
-		spin_unlock(&priv_dev->lock);
+	if (priv_dev->gadget_driver && priv_dev->gadget_driver->disconnect)
 		priv_dev->gadget_driver->disconnect(&priv_dev->gadget);
-		spin_lock(&priv_dev->lock);
-	}
 }
 
 /**
@@ -1783,7 +1780,9 @@ static void cdns3_check_usb_interrupt_proceed(struct cdns3_device *priv_dev,
 
 	/* Disconnection detected */
 	if (usb_ists & (USB_ISTS_DIS2I | USB_ISTS_DISI)) {
+		spin_unlock(&priv_dev->lock);
 		cdns3_disconnect_gadget(priv_dev);
+		spin_lock(&priv_dev->lock);
 		priv_dev->gadget.speed = USB_SPEED_UNKNOWN;
 		usb_gadget_set_state(&priv_dev->gadget, USB_STATE_NOTATTACHED);
 		cdns3_hw_reset_eps_config(priv_dev);
@@ -3266,7 +3265,9 @@ static int cdns3_gadget_suspend(struct cdns3 *cdns, bool do_wakeup)
 {
 	struct cdns3_device *priv_dev = cdns->gadget_dev;
 
+	spin_unlock(&cdns->lock);
 	cdns3_disconnect_gadget(priv_dev);
+	spin_lock(&cdns->lock);
 
 	priv_dev->gadget.speed = USB_SPEED_UNKNOWN;
 	usb_gadget_set_state(&priv_dev->gadget, USB_STATE_NOTATTACHED);
Signature · 1 line
2.17.1
#3 lore raw

Re: [PATCH v2 2/3] usb: cdns3: gadget: own the lock wrongly at the suspend routine

Quoted 7 lines · Peter Chen

When the system goes to suspend, if the controller is at device mode with cable connecting to host, the call stack is: cdns3_suspend-> cdns3_gadget_suspend -> cdns3_disconnect_gadget, after cdns3_disconnect_gadget is called, it owns lock wrongly, it causes the system being deadlock after resume due to at cdns3_device_thread_irq_handler, it tries to get the lock, but can't get it forever.

Quoted 5 lines

To fix it, we delete the unlock-lock operations at cdns3_disconnect_gadget, and do it at the caller.

Fixes b1234e3b3b26 ("usb: cdns3: add runtime PM support")

comment from previous thread is still valid. Missing __releases() and __acquires() annotation.

Signature · 1 line
balbi
author
#4 lore raw

Re: [PATCH v2 2/3] usb: cdns3: gadget: own the lock wrongly at the suspend routine

Quoted 9 lines · Felipe Balbi
Quoted 7 lines · Peter Chen

When the system goes to suspend, if the controller is at device mode with cable connecting to host, the call stack is: cdns3_suspend-> cdns3_gadget_suspend -> cdns3_disconnect_gadget, after cdns3_disconnect_gadget is called, it owns lock wrongly, it causes the system being deadlock after resume due to at cdns3_device_thread_irq_handler, it tries to get the lock, but can't get it forever.

Quoted 9 lines
Quoted 5 lines

To fix it, we delete the unlock-lock operations at cdns3_disconnect_gadget, and do it at the caller.

Fixes b1234e3b3b26 ("usb: cdns3: add runtime PM support")

comment from previous thread is still valid. Missing __releases() and __acquires() annotation.

Thanks, will add __must_hold sparse checker

Signature · 2 lines
Thanks,
Peter Chen
author
#5 lore raw

PATCH v2 3/3 usb: cdns3: Fix on-chip memory overflow issue

In reply to Peter Chen

From: Pawel Laszczak <[email protected]>

Patch fixes issue caused setting On-chip memory overflow bit in usb_sts register. The issue occurred because EP_CFG register was set twice before USB_STS.CFGSTS was set. Every write operation on EP_CFG.BUFFERING causes that controller increases internal counter holding the number of reserved on-chip buffers. First time this register was updated in function cdns3_ep_config before delegating SET_CONFIGURATION request to class driver and again it was updated when class wanted to enable endpoint. This patch fixes this issue by configuring endpoints enabled by class driver in cdns3_gadget_ep_enable and others just before status stage.

Cc <[email protected]> #v5.8+
Fixes 7733f6c32e36 ("usb: cdns3: Add Cadence USB3 DRD Driver")
Reported-and-tested-by Peter Chen <[email protected]>
---
 drivers/usb/cdns3/ep0.c    |  65 ++++++++++++-----------
 drivers/usb/cdns3/gadget.c | 102 +++++++++++++++++++++----------------
 drivers/usb/cdns3/gadget.h |   3 +-
 3 files changed, 94 insertions(+), 76 deletions(-)
drivers/usb/cdns3/ep0.c +35 -30
index 4761c852d9c4..d3121a32cc68 100644
@@ -137,48 +137,36 @@ static int cdns3_req_ep0_set_configuration(struct cdns3_device *priv_dev,
 					   struct usb_ctrlrequest *ctrl_req)
 {
 	enum usb_device_state device_state = priv_dev->gadget.state;
-	struct cdns3_endpoint *priv_ep;
 	u32 config = le16_to_cpu(ctrl_req->wValue);
 	int result = 0;
-	int i;
 
 	switch (device_state) {
 	case USB_STATE_ADDRESS:
-		/* Configure non-control EPs */
-		for (i = 0; i < CDNS3_ENDPOINTS_MAX_COUNT; i++) {
-			priv_ep = priv_dev->eps[i];
-			if (!priv_ep)
-				continue;
-
-			if (priv_ep->flags & EP_CLAIMED)
-				cdns3_ep_config(priv_ep);
-		}
-
 		result = cdns3_ep0_delegate_req(priv_dev, ctrl_req);
 
-		if (result)
-			return result;
-
-		if (!config) {
-			cdns3_hw_reset_eps_config(priv_dev);
-			usb_gadget_set_state(&priv_dev->gadget,
-					     USB_STATE_ADDRESS);
-		}
+		if (result || !config)
+			goto reset_config;
 
 		break;
 	case USB_STATE_CONFIGURED:
 		result = cdns3_ep0_delegate_req(priv_dev, ctrl_req);
+		if (!config && !result)
+			goto reset_config;
 
-		if (!config && !result) {
-			cdns3_hw_reset_eps_config(priv_dev);
-			usb_gadget_set_state(&priv_dev->gadget,
-					     USB_STATE_ADDRESS);
-		}
 		break;
 	default:
-		result = -EINVAL;
+		return -EINVAL;
 	}
 
+	return 0;
+
+reset_config:
+	if (result != USB_GADGET_DELAYED_STATUS)
+		cdns3_hw_reset_eps_config(priv_dev);
+
+	usb_gadget_set_state(&priv_dev->gadget,
+			     USB_STATE_ADDRESS);
+
 	return result;
 }
 
@@ -705,6 +693,7 @@ static int cdns3_gadget_ep0_queue(struct usb_ep *ep,
 	unsigned long flags;
 	int ret = 0;
 	u8 zlp = 0;
+	int i;
 
 	spin_lock_irqsave(&priv_dev->lock, flags);
 	trace_cdns3_ep0_queue(priv_dev, request);
@@ -720,6 +709,17 @@ static int cdns3_gadget_ep0_queue(struct usb_ep *ep,
 		u32 val;
 
 		cdns3_select_ep(priv_dev, 0x00);
+
+		/*
+		 * Configure all non-control EPs which are not enabled by class driver
+		 */
+		for (i = 0; i < CDNS3_ENDPOINTS_MAX_COUNT; i++) {
+			priv_ep = priv_dev->eps[i];
+			if (priv_ep && priv_ep->flags & EP_CLAIMED &&
+			    !(priv_ep->flags & EP_ENABLED))
+				cdns3_ep_config(priv_ep, 0);
+		}
+
 		cdns3_set_hw_configuration(priv_dev);
 		cdns3_ep0_complete_setup(priv_dev, 0, 1);
 		/* wait until configuration set */
@@ -811,6 +811,7 @@ void cdns3_ep0_config(struct cdns3_device *priv_dev)
 	struct cdns3_usb_regs __iomem *regs;
 	struct cdns3_endpoint *priv_ep;
 	u32 max_packet_size = 64;
+	u32 ep_cfg;
 
 	regs = priv_dev->regs;
 
@@ -842,8 +843,10 @@ void cdns3_ep0_config(struct cdns3_device *priv_dev)
 				       BIT(0) | BIT(16));
 	}
 
-	writel(EP_CFG_ENABLE | EP_CFG_MAXPKTSIZE(max_packet_size),
-	       &regs->ep_cfg);
+	ep_cfg = EP_CFG_ENABLE | EP_CFG_MAXPKTSIZE(max_packet_size);
+
+	if (!(priv_ep->flags & EP_CONFIGURED))
+		writel(ep_cfg, &regs->ep_cfg);
 
 	writel(EP_STS_EN_SETUPEN | EP_STS_EN_DESCMISEN | EP_STS_EN_TRBERREN,
 	       &regs->ep_sts_en);
@@ -851,8 +854,10 @@ void cdns3_ep0_config(struct cdns3_device *priv_dev)
 	/* init ep in */
 	cdns3_select_ep(priv_dev, USB_DIR_IN);
 
-	writel(EP_CFG_ENABLE | EP_CFG_MAXPKTSIZE(max_packet_size),
-	       &regs->ep_cfg);
+	if (!(priv_ep->flags & EP_CONFIGURED))
+		writel(ep_cfg, &regs->ep_cfg);
+
+	priv_ep->flags |= EP_CONFIGURED;
 
 	writel(EP_STS_EN_SETUPEN | EP_STS_EN_TRBERREN, &regs->ep_sts_en);
 
drivers/usb/cdns3/gadget.c +57 -45

177 lines not on this page · raw message

drivers/usb/cdns3/gadget.h +2 -1
index 737377913788..21fa461c518e 100644
@@ -1159,6 +1159,7 @@ struct cdns3_endpoint {
 #define EP_QUIRK_EXTRA_BUF_DET	BIT(12)
 #define EP_QUIRK_EXTRA_BUF_EN	BIT(13)
 #define EP_TDLCHK_EN		BIT(15)
+#define EP_CONFIGURED		BIT(16)
 	u32			flags;
 
 	struct cdns3_request	*descmis_req;
@@ -1360,7 +1361,7 @@ void cdns3_gadget_giveback(struct cdns3_endpoint *priv_ep,
 int cdns3_init_ep0(struct cdns3_device *priv_dev,
 		   struct cdns3_endpoint *priv_ep);
 void cdns3_ep0_config(struct cdns3_device *priv_dev);
-void cdns3_ep_config(struct cdns3_endpoint *priv_ep);
+int cdns3_ep_config(struct cdns3_endpoint *priv_ep, bool enable);
 void cdns3_check_ep0_interrupt_proceed(struct cdns3_device *priv_dev, int dir);
 int __cdns3_gadget_wakeup(struct cdns3_device *priv_dev);
 
Signature · 1 line
2.17.1
#6 lore raw

Re: [PATCH v2 3/3] usb: cdns3: Fix on-chip memory overflow issue

Hi,

Quoted 13 lines · Peter Chen

From: Pawel Laszczak <[email protected]>

Patch fixes issue caused setting On-chip memory overflow bit in usb_sts register. The issue occurred because EP_CFG register was set twice before USB_STS.CFGSTS was set. Every write operation on EP_CFG.BUFFERING causes that controller increases internal counter holding the number of reserved on-chip buffers. First time this register was updated in function cdns3_ep_config before delegating SET_CONFIGURATION request to class driver and again it was updated when class wanted to enable endpoint. This patch fixes this issue by configuring endpoints enabled by class driver in cdns3_gadget_ep_enable and others just before status stage.

Quoted 5 lines
Cc <[email protected]> #v5.8+
Fixes 7733f6c32e36 ("usb: cdns3: Add Cadence USB3 DRD Driver")
Reported-and-tested-by Peter Chen <[email protected]>

comment from previous thread is still valid. Far too extensive change to qualify for stable or -rc. Sure there isn't a minimal patch possible?

Signature · 1 line
balbi
maintainer
#7 lore raw

RE: [PATCH v2 3/3] usb: cdns3: Fix on-chip memory overflow issue

Hi,

Quoted 15 lines
Quoted 13 lines · Peter Chen

From: Pawel Laszczak <[email protected]>

Patch fixes issue caused setting On-chip memory overflow bit in usb_sts register. The issue occurred because EP_CFG register was set twice before USB_STS.CFGSTS was set. Every write operation on EP_CFG.BUFFERING causes that controller increases internal counter holding the number of reserved on-chip buffers. First time this register was updated in function cdns3_ep_config before delegating SET_CONFIGURATION request to class driver and again it was updated when class wanted to enable endpoint. This patch fixes this issue by configuring endpoints enabled by class driver in cdns3_gadget_ep_enable and others just before status stage.

Quoted 9 lines
Quoted 5 lines
Cc <[email protected]> #v5.8+
Fixes 7733f6c32e36 ("usb: cdns3: Add Cadence USB3 DRD Driver")
Reported-and-tested-by Peter Chen <[email protected]>

comment from previous thread is still valid. Far too extensive change to qualify for stable or -rc. Sure there isn't a minimal patch possible?

I think that it's the only way to fix this issue. This fix require to displacement of code fragments and it's the reason why this patch is such extensive.

Regards,
Pawel

maintainer
#8 lore raw
In reply to Peter Chen
Quoted 10 lines · Peter Chen

Changes for v2:
- Move position of explicit cast to unsigned int before ((p) << 24) [Patch 1/3]
- No changes for other patches.

Pawel Laszczak (1):
usb: cdns3: Fix on-chip memory overflow issue

Peter Chen (2):
usb: cdns3: gadget: suspicious implicit sign extension
usb: cdns3: gadget: own the lock wrongly at the suspend routine

Are you going to send me patches to queue up for this driver any time soon? I've seen lots of different ones fly by, but no "please take this" type of hint to me, so I have no idea what to do at all...

thanks,

greg k-h

author
#9 lore raw
Quoted 10 lines · Peter Chen
Quoted 8 lines

Changes for v2:
- Move position of explicit cast to unsigned int before ((p) << 24) [Patch 1/3]
- No changes for other patches.

Pawel Laszczak (1):
usb: cdns3: Fix on-chip memory overflow issue

Quoted 8 lines
Quoted 3 lines

Peter Chen (2):
usb: cdns3: gadget: suspicious implicit sign extension
usb: cdns3: gadget: own the lock wrongly at the suspend routine

Are you going to send me patches to queue up for this driver any time soon? I've seen lots of different ones fly by, but no "please take this" type of hint to me, so I have no idea what to do at all...

I will queue them up after reviewing, thanks.

Peter