Skip to contentSkip to conversation

usb: dwc2: gadget: sparse warning of context imbalance

PATCH In review Sudip Mukherjee 5 participants 12 messages Oct 17, 2014 – Oct 23, 2014

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

PATCH v3 1/2 usb: dwc2: gadget: sparse warning of context imbalance

sparse was giving the following warning:
warning: context imbalance in 's3c_hsotg_ep_enable'
- different lock contexts for basic block

we were returning ENOMEM while still holding the spinlock. The sparse warning was fixed by releasing the spinlock before return.

---
 drivers/usb/dwc2/gadget.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/usb/dwc2/gadget.c +5 -2
index 7b5856f..7f25527 100644
@@ -2561,8 +2561,10 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
 			hs_ep->fifo_size = val;
 			break;
 		}
-		if (i == 8)
-			return -ENOMEM;
+		if (i == 8) {
+			ret = -ENOMEM;
+			goto error;
+		}
 	}
 
 	/* for non control endpoints, set PID to D0 */
@@ -2579,6 +2581,7 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
 	/* enable the endpoint interrupt */
 	s3c_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
 
+error:
 	spin_unlock_irqrestore(&hsotg->lock, flags);
 	return ret;
 }
Signature · 1 line
1.8.1.2
#1 lore raw

PATCH v3 2/2 usb: dwc2: gadget: modify return statement

modified the function to have a single return statement at the end instead of multiple return statement in the middle of the function to improve the readability of the code.

This patch depends on the previous patch of the series.

---
 drivers/usb/dwc2/gadget.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/usb/dwc2/gadget.c +3 -1
index 7f25527..e8a8fc7 100644
@@ -2471,7 +2471,8 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
 	dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
 	if (dir_in != hs_ep->dir_in) {
 		dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
-		return -EINVAL;
+		ret = -EINVAL;
+		goto error1;
 	}
 
 	mps = usb_endpoint_maxp(desc);
@@ -2583,6 +2584,7 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
 
 error:
 	spin_unlock_irqrestore(&hsotg->lock, flags);
+error1:
 	return ret;
 }
 
Signature · 1 line
1.8.1.2
#2 lore raw

RE: [PATCH v3 2/2] usb: dwc2: gadget: modify return statement

From: Of Sudip Mukherjee

Quoted 3 lines

modified the function to have a single return statement at the end instead of multiple return statement in the middle of the function to improve the readability of the code.

Many of us would disagree with you there.
Early returns actually make the code easier to read, certainly better than a goto 'end of function'.

David

Quoted 36 lines

This patch depends on the previous patch of the series.

---
 drivers/usb/dwc2/gadget.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/usb/dwc2/gadget.c +3 -2
index 7f25527..e8a8fc7 100644
@@ -2471,7 +2471,8 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
 	dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
 	if (dir_in != hs_ep->dir_in) {
 		dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
-		return -EINVAL;
+		ret = -EINVAL;
+		goto error1;
 	}
 
 	mps = usb_endpoint_maxp(desc);
@@ -2583,6 +2584,7 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
 
 error:
 	spin_unlock_irqrestore(&hsotg->lock, flags);
+error1:
 	return ret;
 }
 
--

1.8.1.2

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw

Re: [PATCH v3 2/2] usb: dwc2: gadget: modify return statement

Quoted 9 lines · David Laight

From: Of Sudip Mukherjee

Quoted 3 lines

modified the function to have a single return statement at the end instead of multiple return statement in the middle of the function to improve the readability of the code.

Many of us would disagree with you there.
Early returns actually make the code easier to read, certainly better than a goto 'end of function'.

actually , frankly speaking, this first return statement was also easier for me to understand. But in my v1 patch , Paul mentioned :

Quoted 2 lines

For a long function like this, I'd rather keep a single return point at the end.

so I thought he meant all the return statements in the function.

thanks
sudip

Quoted 40 lines

David

Quoted 36 lines

This patch depends on the previous patch of the series.

---
 drivers/usb/dwc2/gadget.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/usb/dwc2/gadget.c +3 -2
index 7f25527..e8a8fc7 100644
@@ -2471,7 +2471,8 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
 	dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
 	if (dir_in != hs_ep->dir_in) {
 		dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
-		return -EINVAL;
+		ret = -EINVAL;
+		goto error1;
 	}
 
 	mps = usb_endpoint_maxp(desc);
@@ -2583,6 +2584,7 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
 
 error:
 	spin_unlock_irqrestore(&hsotg->lock, flags);
+error1:
 	return ret;
 }
 
--

1.8.1.2

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#4 lore raw

RE: [PATCH v3 2/2] usb: dwc2: gadget: modify return statement

Quoted 13 lines

From: Sudip Mukherjee [mailto:[email protected]]
Sent: Friday, October 17, 2014 3:03 AM

Quoted 9 lines · David Laight

From: Of Sudip Mukherjee

Quoted 3 lines

modified the function to have a single return statement at the end instead of multiple return statement in the middle of the function to improve the readability of the code.

Many of us would disagree with you there.
Early returns actually make the code easier to read, certainly better than a goto 'end of function'.

Quoted 5 lines

actually , frankly speaking, this first return statement was also easier for me to understand. But in my v1 patch , Paul mentioned :

Quoted 2 lines

For a long function like this, I'd rather keep a single return point at the end.

so I thought he meant all the return statements in the function.

What I didn't like about your first patch was that there were two places where the spinlock was released. I think that is error-prone, as can be seen by the original bug. But I am OK with leaving the first return statement as-is, since the spinlock is not held there.

So I think we should apply patch 1, and drop patch 2.

Signature · 1 line
Paul
#5 lore raw

RE: [PATCH v3 2/2] usb: dwc2: gadget: modify return statement

In reply to Sudip Mukherjee
Quoted 29 lines

From: Sudip Mukherjee [mailto:[email protected]]
Sent: Thursday, October 16, 2014 9:44 PM

modified the function to have a single return statement at the end instead of multiple return statement in the middle of the function to improve the readability of the code.

This patch depends on the previous patch of the series.

---
 drivers/usb/dwc2/gadget.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/usb/dwc2/gadget.c +2 -1
index 7f25527..e8a8fc7 100644
@@ -2471,7 +2471,8 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
 	dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
 	if (dir_in != hs_ep->dir_in) {
 		dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
-		return -EINVAL;
+		ret = -EINVAL;
+		goto error1;
 	}
 
 	mps = usb_endpoint_maxp(desc);

drivers/usb/dwc2/gadget.c static int s3c_hsotg_ep_enable(struct usb_ep *ep, · line 2589

Quoted 6 lines
drivers/usb/dwc2/gadget.c +1 -0
@@ -2583,6 +2584,7 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
 
 error:
 	spin_unlock_irqrestore(&hsotg->lock, flags);
+error1:
 	return ret;
 }

According to the discussion in another thread, let's drop this patch.

Signature · 1 line
Paul
Acked-by on v3 1/2
#6 lore raw
In reply to Sudip Mukherjee
Quoted 33 lines

From: Sudip Mukherjee [mailto:[email protected]]
Sent: Thursday, October 16, 2014 9:44 PM

sparse was giving the following warning:
warning: context imbalance in 's3c_hsotg_ep_enable'
- different lock contexts for basic block

we were returning ENOMEM while still holding the spinlock. The sparse warning was fixed by releasing the spinlock before return.

---
 drivers/usb/dwc2/gadget.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/usb/dwc2/gadget.c +4 -2
index 7b5856f..7f25527 100644
@@ -2561,8 +2561,10 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
 			hs_ep->fifo_size = val;
 			break;
 		}
-		if (i == 8)
-			return -ENOMEM;
+		if (i == 8) {
+			ret = -ENOMEM;
+			goto error;
+		}
 	}
 
 	/* for non control endpoints, set PID to D0 */

drivers/usb/dwc2/gadget.c static int s3c_hsotg_ep_enable(struct usb_ep *ep, · line 2587

Quoted 7 lines
drivers/usb/dwc2/gadget.c +1 -0
@@ -2579,6 +2581,7 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
 	/* enable the endpoint interrupt */
 	s3c_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
 
+error:
 	spin_unlock_irqrestore(&hsotg->lock, flags);
 	return ret;
 }
#7 lore raw
Quoted 36 lines · Paul Zimmerman
Quoted 36 lines

From: Sudip Mukherjee [mailto:[email protected]]
Sent: Thursday, October 16, 2014 9:44 PM

sparse was giving the following warning:
warning: context imbalance in 's3c_hsotg_ep_enable'
- different lock contexts for basic block

we were returning ENOMEM while still holding the spinlock. The sparse warning was fixed by releasing the spinlock before return.

---
 drivers/usb/dwc2/gadget.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/usb/dwc2/gadget.c +4 -2
index 7b5856f..7f25527 100644
@@ -2561,8 +2561,10 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
 			hs_ep->fifo_size = val;
 			break;
 		}
-		if (i == 8)
-			return -ENOMEM;
+		if (i == 8) {
+			ret = -ENOMEM;
+			goto error;
+		}
 	}
 
 	/* for non control endpoints, set PID to D0 */
@@ -2579,6 +2581,7 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
 	/* enable the endpoint interrupt */
 	s3c_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
 
Quoted 6 lines

drivers/usb/dwc2/gadget.c static int s3c_hsotg_ep_enable(struct usb_ep *ep, · line 2587

Quoted 4 lines
drivers/usb/dwc2/gadget.c +1 -0
+error:
 	spin_unlock_irqrestore(&hsotg->lock, flags);
 	return ret;
 }

v3.18-rc or v3.19 ?

Signature · 1 line
balbi
#8 lore raw
Quoted 40 lines

From: Felipe Balbi [mailto:[email protected]]
Sent: Friday, October 17, 2014 11:52 AM

Quoted 36 lines · Paul Zimmerman
Quoted 36 lines

From: Sudip Mukherjee [mailto:[email protected]]
Sent: Thursday, October 16, 2014 9:44 PM

sparse was giving the following warning:
warning: context imbalance in 's3c_hsotg_ep_enable'
- different lock contexts for basic block

we were returning ENOMEM while still holding the spinlock. The sparse warning was fixed by releasing the spinlock before return.

---
 drivers/usb/dwc2/gadget.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/usb/dwc2/gadget.c +4 -2
index 7b5856f..7f25527 100644
@@ -2561,8 +2561,10 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
 			hs_ep->fifo_size = val;
 			break;
 		}
-		if (i == 8)
-			return -ENOMEM;
+		if (i == 8) {
+			ret = -ENOMEM;
+			goto error;
+		}
 	}
 
 	/* for non control endpoints, set PID to D0 */
@@ -2579,6 +2581,7 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
 	/* enable the endpoint interrupt */
 	s3c_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
 
Quoted 8 lines
Quoted 6 lines

drivers/usb/dwc2/gadget.c static int s3c_hsotg_ep_enable(struct usb_ep *ep, · line 2587

Quoted 4 lines
drivers/usb/dwc2/gadget.c +1 -0
+error:
 	spin_unlock_irqrestore(&hsotg->lock, flags);
 	return ret;
 }

v3.18-rc or v3.19 ?

v3.18-rc, since it's a bugfix. And I forgot, this should be marked for 3.17 stable too.

Signature · 1 line
Paul
#9 lore raw
Quoted 47 lines · Paul Zimmerman
Quoted 47 lines

From: Felipe Balbi [mailto:[email protected]]
Sent: Friday, October 17, 2014 11:52 AM

Quoted 42 lines · Paul Zimmerman
Quoted 33 lines

From: Sudip Mukherjee [mailto:[email protected]]
Sent: Thursday, October 16, 2014 9:44 PM

sparse was giving the following warning:
warning: context imbalance in 's3c_hsotg_ep_enable'
- different lock contexts for basic block

we were returning ENOMEM while still holding the spinlock. The sparse warning was fixed by releasing the spinlock before return.

---
 drivers/usb/dwc2/gadget.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/usb/dwc2/gadget.c +4 -2
index 7b5856f..7f25527 100644
@@ -2561,8 +2561,10 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
 			hs_ep->fifo_size = val;
 			break;
 		}
-		if (i == 8)
-			return -ENOMEM;
+		if (i == 8) {
+			ret = -ENOMEM;
+			goto error;
+		}
 	}
 
 	/* for non control endpoints, set PID to D0 */

drivers/usb/dwc2/gadget.c static int s3c_hsotg_ep_enable(struct usb_ep *ep, · line 2587

Quoted 7 lines
drivers/usb/dwc2/gadget.c +1 -0
@@ -2579,6 +2581,7 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
 	/* enable the endpoint interrupt */
 	s3c_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
 
+error:
 	spin_unlock_irqrestore(&hsotg->lock, flags);
 	return ret;
 }
Quoted 4 lines
Quoted 1 line

v3.18-rc or v3.19 ?

v3.18-rc, since it's a bugfix. And I forgot, this should be marked for 3.17 stable too.

Alright, I'll add that.

Signature · 1 line
balbi
#10 lore raw
In reply to Paul Zimmerman
Quoted 47 lines · Paul Zimmerman
Quoted 47 lines

From: Felipe Balbi [mailto:[email protected]]
Sent: Friday, October 17, 2014 11:52 AM

Quoted 42 lines · Paul Zimmerman
Quoted 33 lines

From: Sudip Mukherjee [mailto:[email protected]]
Sent: Thursday, October 16, 2014 9:44 PM

sparse was giving the following warning:
warning: context imbalance in 's3c_hsotg_ep_enable'
- different lock contexts for basic block

we were returning ENOMEM while still holding the spinlock. The sparse warning was fixed by releasing the spinlock before return.

---
 drivers/usb/dwc2/gadget.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/usb/dwc2/gadget.c +4 -2
index 7b5856f..7f25527 100644
@@ -2561,8 +2561,10 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
 			hs_ep->fifo_size = val;
 			break;
 		}
-		if (i == 8)
-			return -ENOMEM;
+		if (i == 8) {
+			ret = -ENOMEM;
+			goto error;
+		}
 	}
 
 	/* for non control endpoints, set PID to D0 */

drivers/usb/dwc2/gadget.c static int s3c_hsotg_ep_enable(struct usb_ep *ep, · line 2587

Quoted 7 lines
drivers/usb/dwc2/gadget.c +1 -0
@@ -2579,6 +2581,7 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
 	/* enable the endpoint interrupt */
 	s3c_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
 
+error:
 	spin_unlock_irqrestore(&hsotg->lock, flags);
 	return ret;
 }
Quoted 5 lines
Quoted 1 line

v3.18-rc or v3.19 ?

v3.18-rc, since it's a bugfix. And I forgot, this should be marked for 3.17 stable too.

hi,
this is not yet added to linux-next , is something pending from my side?

thanks
sudip

Quoted 3 lines
Signature · 1 line
Paul
#11 lore raw
Quoted 48 lines · Sudip Mukherjee
Quoted 47 lines · Paul Zimmerman
Quoted 47 lines

From: Felipe Balbi [mailto:[email protected]]
Sent: Friday, October 17, 2014 11:52 AM

Quoted 42 lines · Paul Zimmerman
Quoted 33 lines

From: Sudip Mukherjee [mailto:[email protected]]
Sent: Thursday, October 16, 2014 9:44 PM

sparse was giving the following warning:
warning: context imbalance in 's3c_hsotg_ep_enable'
- different lock contexts for basic block

we were returning ENOMEM while still holding the spinlock. The sparse warning was fixed by releasing the spinlock before return.

---
 drivers/usb/dwc2/gadget.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/usb/dwc2/gadget.c +4 -2
index 7b5856f..7f25527 100644
@@ -2561,8 +2561,10 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
 			hs_ep->fifo_size = val;
 			break;
 		}
-		if (i == 8)
-			return -ENOMEM;
+		if (i == 8) {
+			ret = -ENOMEM;
+			goto error;
+		}
 	}
 
 	/* for non control endpoints, set PID to D0 */

drivers/usb/dwc2/gadget.c static int s3c_hsotg_ep_enable(struct usb_ep *ep, · line 2587

Quoted 7 lines
drivers/usb/dwc2/gadget.c +1 -0
@@ -2579,6 +2581,7 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
 	/* enable the endpoint interrupt */
 	s3c_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
 
+error:
 	spin_unlock_irqrestore(&hsotg->lock, flags);
 	return ret;
 }
Quoted 7 lines
Quoted 5 lines
Quoted 1 line

v3.18-rc or v3.19 ?

v3.18-rc, since it's a bugfix. And I forgot, this should be marked for 3.17 stable too.

hi,
this is not yet added to linux-next , is something pending from my side?

Patience.