usb: cdns3: three bug fixes for v5.10
cover usb: cdns3: three bug fixes for v5.10
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
PATCH 1/3 usb: cdns3: gadget: suspicious implicit sign extension
For 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.
--- drivers/usb/cdns3/gadget.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/cdns3/gadget.h +1 -1
@@ -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
PATCH 2/3 usb: cdns3: gadget: own the lock wrongly at the suspend routine
In reply to Peter ChenWhen 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.
--- drivers/usb/cdns3/gadget.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/usb/cdns3/gadget.c +5 -4
@@ -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
PATCH 3/3 usb: cdns3: Fix on-chip memory overflow issue
In reply to Peter ChenFrom: 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.
--- 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
@@ -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), - ®s->ep_cfg); + ep_cfg =EP_CFG_ENABLE |EP_CFG_MAXPKTSIZE (max_packet_size); + +if (!(priv_ep->flags &EP_CONFIGURED )) +writel (ep_cfg, ®s->ep_cfg);writel (EP_STS_EN_SETUPEN |EP_STS_EN_DESCMISEN |EP_STS_EN_TRBERREN , ®s->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), - ®s->ep_cfg); +if (!(priv_ep->flags &EP_CONFIGURED )) +writel (ep_cfg, ®s->ep_cfg); + + priv_ep->flags |=EP_CONFIGURED ;writel (EP_STS_EN_SETUPEN |EP_STS_EN_TRBERREN , ®s->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
@@ -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
Re: [PATCH 1/3] usb: cdns3: gadget: suspicious implicit sign extension
In reply to Peter ChenHi,
Quoted 7 lines · Peter Chen
For 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 ifpriv_ep->trb_burst_sizeis equal or larger than0x80;
Quoted 6 lines
Below is the Coverity warning:
sign_extension: Suspicious implicit sign extension:priv_ep->trb_burst_sizewith type u8 (8 bits, unsigned) is promoted inpriv_ep->trb_burst_size<< 24 to type int (32 bits, signed), then sign-extended to type unsigned long (64 bits, unsigned). Ifpriv_ep->trb_burst_size<< 24 is greater than0x7FFFFFFF, the upper bits of the result will all be 1.
looks like a false positive...
Quoted 12 lines
Cc <[email protected]> #v5.8+Fixes 7733f6c32e36 ("usb: cdns3: Add Cadence USB3 DRD Driver")Signed-off-by Peter Chen <[email protected]>--- drivers/usb/cdns3/gadget.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/usb/cdns3/gadget.h +0
-0
drivers/usb/cdns3/gadget.h struct cdns3_trb { · line 1075
Quoted 5 lines
drivers/usb/cdns3/gadget.h +1
-1@@ -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))
... because TRB_BURST_LEN() is used to intialize a __le32 type. Even if it ends up being sign extended, the top 32-bits will be ignored.
I'll apply, but it looks like a pointless fix. We shouldn't need it for stable
Signature · 1 line
balbi
Re: [PATCH 1/3] usb: cdns3: gadget: suspicious implicit sign extension
In reply to Peter ChenHi,
Quoted 7 lines · Peter Chen
For 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 ifpriv_ep->trb_burst_sizeis equal or larger than0x80;
Quoted 6 lines
Below is the Coverity warning:
sign_extension: Suspicious implicit sign extension:priv_ep->trb_burst_sizewith type u8 (8 bits, unsigned) is promoted inpriv_ep->trb_burst_size<< 24 to type int (32 bits, signed), then sign-extended to type unsigned long (64 bits, unsigned). Ifpriv_ep->trb_burst_size<< 24 is greater than0x7FFFFFFF, the upper bits of the result will all be 1.
looks like a false positive...
Quoted 12 lines
Cc <[email protected]> #v5.8+Fixes 7733f6c32e36 ("usb: cdns3: Add Cadence USB3 DRD Driver")Signed-off-by Peter Chen <[email protected]>--- drivers/usb/cdns3/gadget.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/usb/cdns3/gadget.h +0
-0
drivers/usb/cdns3/gadget.h struct cdns3_trb { · line 1075
Quoted 5 lines
drivers/usb/cdns3/gadget.h +1
-1@@ -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))
... because TRB_BURST_LEN() is used to intialize a __le32 type. Even if it ends up being sign extended, the top 32-bits will be ignored.
I'll apply, but it looks like a pointless fix. We shouldn't need it for stable.
Signature · 1 line
balbi
Re: [PATCH 2/3] usb: cdns3: gadget: own the lock wrongly at the suspend routine
In reply to Peter ChenHi,
drivers/usb/cdns3/gadget.c static void cdns3_check_usb_interrupt_proceed(struct cdns3_device *priv_dev, · line 1785
Quoted 7 lines · Peter Chen
drivers/usb/cdns3/gadget.c +2
-0@@ -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);
don't you need to add sparse __releases() an __acquires() markers?
drivers/usb/cdns3/gadget.c static int cdns3_gadget_suspend(struct cdns3 *cdns, bool do_wakeup) · line 3270
Quoted 10 lines
drivers/usb/cdns3/gadget.c +2
-0priv_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);
ditto
Signature · 1 line
balbi
Re: [PATCH 2/3] usb: cdns3: gadget: own the lock wrongly at the suspend routine
In reply to Peter ChenHi,
drivers/usb/cdns3/gadget.c static void cdns3_check_usb_interrupt_proceed(struct cdns3_device *priv_dev, · line 1785
Quoted 7 lines · Peter Chen
drivers/usb/cdns3/gadget.c +2
-0@@ -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);
don't you need to add sparse __releases() an __acquires() markers?
drivers/usb/cdns3/gadget.c static int cdns3_gadget_suspend(struct cdns3 *cdns, bool do_wakeup) · line 3270
Quoted 10 lines
drivers/usb/cdns3/gadget.c +2
-0priv_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);
ditto
Signature · 1 line
balbi
Re: [PATCH 3/3] usb: cdns3: Fix on-chip memory overflow issue
In reply to Peter ChenHi,
Quoted 13 lines · Peter Chen
From: Pawel Laszczak <[email protected]>
Patch fixes issue caused setting On-chip memory overflow bit in
usb_stsregister. The issue occurred becauseEP_CFGregister 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 functioncdns3_ep_configbefore delegatingSET_CONFIGURATIONrequest 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 incdns3_gadget_ep_enableand 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]>Signed-off-by Pawel Laszczak <[email protected]>Signed-off-by Peter Chen <[email protected]>
This looks very large for a fix, are you sure there isn't a minimal fix hidden somewhere?
Signature · 1 line
balbi
Re: [PATCH 1/3] usb: cdns3: gadget: suspicious implicit sign extension
In reply to Felipe BalbiQuoted 34 lines · Felipe Balbi
Hi,
Quoted 7 lines · Peter Chen
For 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 ifpriv_ep->trb_burst_sizeis equal or larger than0x80;Quoted 6 lines
Below is the Coverity warning:
sign_extension: Suspicious implicit sign extension:priv_ep->trb_burst_sizewith type u8 (8 bits, unsigned) is promoted inpriv_ep->trb_burst_size<< 24 to type int (32 bits, signed), then sign-extended to type unsigned long (64 bits, unsigned). Ifpriv_ep->trb_burst_size<< 24 is greater than0x7FFFFFFF, the upper bits of the result will all be 1.looks like a false positive...
Quoted 14 lines
Cc <[email protected]> #v5.8+Fixes 7733f6c32e36 ("usb: cdns3: Add Cadence USB3 DRD Driver")Signed-off-by Peter Chen <[email protected]>--- drivers/usb/cdns3/gadget.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/usb/cdns3/gadget.h +0
-0@@ -1072,7 +1072,7 @@ struct cdns3_trb {#define TRB_TDL_SS_SIZE_GET (p) (((p) & GENMASK(23, 17)) >> 17)
Quoted 9 lines
drivers/usb/cdns3/gadget.h struct cdns3_trb { · line 1075
Quoted 3 lines
drivers/usb/cdns3/gadget.h +1
-1/* 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))... because
TRB_BURST_LEN()is used to intialize a__le32type. Even if it ends up being sign extended, the top 32-bits will be ignored.I'll apply, but it looks like a pointless fix. We shouldn't need it for stable
At my v2:
It is:
#define TRB_BURST_LEN(p) ((unsigned int)((p) << 24) & GENMASK(31, 24))
It is not related to high 32-bits issue, it is sign extended issue for 32 bits. If p is a unsigned char data, the compiler will consider
(p) << 24 is int, but not unsigned int. So, if the p is larger than 0x80, the (p) << 24 will be overflow.
If you compile the code:
unsigned int k = 0x80 << 24 + 0x81;
It will report build warning:
warning: left shift count >= width of type [-Wshift-count-overflow]
Signature · 2 lines
Thanks, Peter Chen
RE: [PATCH 1/3] usb: cdns3: gadget: suspicious implicit sign extension
From: Peter Chen
Quoted 56 lines
Sent: 27 October 2020 09:49
Quoted 34 lines · Felipe Balbi
Hi,
Quoted 7 lines · Peter Chen
For 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 ifpriv_ep->trb_burst_sizeis equal or larger than0x80;Quoted 6 lines
Below is the Coverity warning:
sign_extension: Suspicious implicit sign extension:priv_ep->trb_burst_sizewith type u8 (8 bits, unsigned) is promoted inpriv_ep->trb_burst_size<< 24 to type int (32 bits, signed), then sign-extended to type unsigned long (64 bits, unsigned). Ifpriv_ep->trb_burst_size<< 24 is greater than0x7FFFFFFF, the upper bits of the result will all be 1.looks like a false positive...
Quoted 14 lines
Cc <[email protected]> #v5.8+Fixes 7733f6c32e36 ("usb: cdns3: Add Cadence USB3 DRD Driver")Signed-off-by Peter Chen <[email protected]>--- drivers/usb/cdns3/gadget.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/usb/cdns3/gadget.h +0
-0@@ -1072,7 +1072,7 @@ struct cdns3_trb {#define TRB_TDL_SS_SIZE_GET (p) (((p) & GENMASK(23, 17)) >> 17)Quoted 9 lines
drivers/usb/cdns3/gadget.h struct cdns3_trb { · line 1075
Quoted 3 lines
drivers/usb/cdns3/gadget.h +1
-1/* 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))... because
TRB_BURST_LEN()is used to intialize a__le32type. Even if it ends up being sign extended, the top 32-bits will be ignored.I'll apply, but it looks like a pointless fix. We shouldn't need it for stable
At my v2:
It is:
#defineTRB_BURST_LEN(p)((unsigned int)((p) << 24) &GENMASK(31, 24))It is not related to high 32-bits issue, it is sign extended issue for 32 bits. If p is a unsigned char data, the compiler will consider
(p) << 24 is int, but not unsigned int. So, if the p is larger than0x80, the (p) << 24 will be overflow.
Quoted 6 lines
If you compile the code:
unsigned int k =
0x80<< 24 +0x81;It will report build warning:
warning: left shift count >= width of type [-Wshift-count-overflow]
Something like:
#define TRB_BURST_LEN(p) (((p) + 0u) << 24)
will remove the warning.
The GENMASK() is pretty pointless - the compiler may fail to optimise it away.
If p is 64bit the high bits should get discarded pretty quickly.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Re: [PATCH 1/3] usb: cdns3: gadget: suspicious implicit sign extension
In reply to Peter ChenQuoted 6 lines · Peter Chen
If you compile the code:
unsigned int k =
0x80<< 24 +0x81;It will report build warning:
warning: left shift count >= width of type [-Wshift-count-overflow]
That's a separate issue. I believe (but haven't checked) that the << operator has lower precedence than +, so the compiler interprets the expression as:
unsigned int k = 0x80 << (24 + 0x81);
and it's pretty obvious why this causes an error. Instead, try compiling:
unsigned int k = (0x80 << 24) + 0x81;
You may get an error message about signed-integer overflow, but not about shift-count overflow.
Alan Stern
RE: [PATCH 1/3] usb: cdns3: gadget: suspicious implicit sign extension
Quoted 6 lines
That's a separate issue. I believe (but haven't checked) that the << operator has lower precedence than +, so the compiler interprets the expression as:
unsigned int k =
0x80<< (24 +0x81);
Quoted 8 lines
and it's pretty obvious why this causes an error. Instead, try compiling:
unsigned int k = (
0x80<< 24) +0x81;You may get an error message about signed-integer overflow, but not about shift-count overflow.
Hi Alan,
Your analysis is correct, I did not check the warning message correctly.
Peter