netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH][V2] net: dsa: mv88e6xxx: avoid unintended sign extension on a 16 bit shift
@ 2018-02-16 16:55 Colin King
  2018-02-16 21:36 ` David Miller
  2018-02-19 12:19 ` David Laight
  0 siblings, 2 replies; 4+ messages in thread
From: Colin King @ 2018-02-16 16:55 UTC (permalink / raw)
  To: Andrew Lunn, Vivien Didelot, Florian Fainelli, netdev
  Cc: kernel-janitors, linux-kernel

From: Colin Ian King <colin.king@canonical.com>

The shifting of timehi by 16 bits to the left will be promoted to
a 32 bit signed int and then sign-extended to an u64. If the top bit
of timehi is set then all then all the upper bits of ns end up as also
being set because of the sign-extension. Fix this by making timehi and
timelo u64.  Also move the declaration of ns.

Detected by CoverityScan, CID#1465288 ("Unintended sign extension")

Fixes: c6fe0ad2c349 ("net: dsa: mv88e6xxx: add rx/tx timestamping support")
Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 drivers/net/dsa/mv88e6xxx/hwtstamp.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/net/dsa/mv88e6xxx/hwtstamp.c b/drivers/net/dsa/mv88e6xxx/hwtstamp.c
index b251d534b70d..2149d332dea0 100644
--- a/drivers/net/dsa/mv88e6xxx/hwtstamp.c
+++ b/drivers/net/dsa/mv88e6xxx/hwtstamp.c
@@ -293,7 +293,8 @@ static void mv88e6xxx_get_rxts(struct mv88e6xxx_chip *chip,
 			       struct sk_buff *skb, u16 reg,
 			       struct sk_buff_head *rxq)
 {
-	u16 buf[4] = { 0 }, status, timelo, timehi, seq_id;
+	u16 buf[4] = { 0 }, status, seq_id;
+	u64 ns, timelo, timehi;
 	struct skb_shared_hwtstamps *shwt;
 	int err;
 
@@ -321,7 +322,7 @@ static void mv88e6xxx_get_rxts(struct mv88e6xxx_chip *chip,
 	 */
 	for ( ; skb; skb = skb_dequeue(rxq)) {
 		if (mv88e6xxx_ts_valid(status) && seq_match(skb, seq_id)) {
-			u64 ns = timehi << 16 | timelo;
+			ns = timehi << 16 | timelo;
 
 			mutex_lock(&chip->reg_lock);
 			ns = timecounter_cyc2time(&chip->tstamp_tc, ns);
-- 
2.15.1

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

* Re: [PATCH][V2] net: dsa: mv88e6xxx: avoid unintended sign extension on a 16 bit shift
  2018-02-16 16:55 [PATCH][V2] net: dsa: mv88e6xxx: avoid unintended sign extension on a 16 bit shift Colin King
@ 2018-02-16 21:36 ` David Miller
  2018-02-19 12:19 ` David Laight
  1 sibling, 0 replies; 4+ messages in thread
From: David Miller @ 2018-02-16 21:36 UTC (permalink / raw)
  To: colin.king
  Cc: andrew, vivien.didelot, f.fainelli, netdev, kernel-janitors,
	linux-kernel

From: Colin King <colin.king@canonical.com>
Date: Fri, 16 Feb 2018 16:55:05 +0000

> From: Colin Ian King <colin.king@canonical.com>
> 
> The shifting of timehi by 16 bits to the left will be promoted to
> a 32 bit signed int and then sign-extended to an u64. If the top bit
> of timehi is set then all then all the upper bits of ns end up as also
> being set because of the sign-extension. Fix this by making timehi and
> timelo u64.  Also move the declaration of ns.
> 
> Detected by CoverityScan, CID#1465288 ("Unintended sign extension")
> 
> Fixes: c6fe0ad2c349 ("net: dsa: mv88e6xxx: add rx/tx timestamping support")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>

Please indicate the appropriate target tree in your Subject lines
in the future, for this it should be "[PATCH net-next]".

Applied, thanks.

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

* RE: [PATCH][V2] net: dsa: mv88e6xxx: avoid unintended sign extension on a 16 bit shift
  2018-02-16 16:55 [PATCH][V2] net: dsa: mv88e6xxx: avoid unintended sign extension on a 16 bit shift Colin King
  2018-02-16 21:36 ` David Miller
@ 2018-02-19 12:19 ` David Laight
  2018-02-19 17:37   ` Richard Cochran
  1 sibling, 1 reply; 4+ messages in thread
From: David Laight @ 2018-02-19 12:19 UTC (permalink / raw)
  To: 'Colin King', Andrew Lunn, Vivien Didelot,
	Florian Fainelli, netdev@vger.kernel.org
  Cc: kernel-janitors@vger.kernel.org, linux-kernel@vger.kernel.org

From: Colin King
> Sent: 16 February 2018 16:55
> 
> From: Colin Ian King <colin.king@canonical.com>
> 
> The shifting of timehi by 16 bits to the left will be promoted to
> a 32 bit signed int and then sign-extended to an u64. If the top bit
> of timehi is set then all then all the upper bits of ns end up as also
> being set because of the sign-extension. Fix this by making timehi and
> timelo u64.  Also move the declaration of ns.
> 
> Detected by CoverityScan, CID#1465288 ("Unintended sign extension")
> 
> Fixes: c6fe0ad2c349 ("net: dsa: mv88e6xxx: add rx/tx timestamping support")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  drivers/net/dsa/mv88e6xxx/hwtstamp.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/net/dsa/mv88e6xxx/hwtstamp.c b/drivers/net/dsa/mv88e6xxx/hwtstamp.c
> index b251d534b70d..2149d332dea0 100644
> --- a/drivers/net/dsa/mv88e6xxx/hwtstamp.c
> +++ b/drivers/net/dsa/mv88e6xxx/hwtstamp.c
> @@ -293,7 +293,8 @@ static void mv88e6xxx_get_rxts(struct mv88e6xxx_chip *chip,
>  			       struct sk_buff *skb, u16 reg,
>  			       struct sk_buff_head *rxq)
>  {
> -	u16 buf[4] = { 0 }, status, timelo, timehi, seq_id;
> +	u16 buf[4] = { 0 }, status, seq_id;
> +	u64 ns, timelo, timehi;
>  	struct skb_shared_hwtstamps *shwt;
>  	int err;
> 
> @@ -321,7 +322,7 @@ static void mv88e6xxx_get_rxts(struct mv88e6xxx_chip *chip,
>  	 */
>  	for ( ; skb; skb = skb_dequeue(rxq)) {
>  		if (mv88e6xxx_ts_valid(status) && seq_match(skb, seq_id)) {
> -			u64 ns = timehi << 16 | timelo;
> +			ns = timehi << 16 | timelo;

This seems to be somewhat excessive 64bit maths on a 32bit system.
It is more than enough to make timelo/timehi 'unsigned int'.

	David

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

* Re: [PATCH][V2] net: dsa: mv88e6xxx: avoid unintended sign extension on a 16 bit shift
  2018-02-19 12:19 ` David Laight
@ 2018-02-19 17:37   ` Richard Cochran
  0 siblings, 0 replies; 4+ messages in thread
From: Richard Cochran @ 2018-02-19 17:37 UTC (permalink / raw)
  To: David Laight
  Cc: 'Colin King', Andrew Lunn, Vivien Didelot,
	Florian Fainelli, netdev@vger.kernel.org,
	kernel-janitors@vger.kernel.org, linux-kernel@vger.kernel.org

On Mon, Feb 19, 2018 at 12:19:16PM +0000, David Laight wrote:
> This seems to be somewhat excessive 64bit maths on a 32bit system.
> It is more than enough to make timelo/timehi 'unsigned int'.

Do you see a difference in the generated code?

Thanks,
Richard

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

end of thread, other threads:[~2018-02-19 17:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-02-16 16:55 [PATCH][V2] net: dsa: mv88e6xxx: avoid unintended sign extension on a 16 bit shift Colin King
2018-02-16 21:36 ` David Miller
2018-02-19 12:19 ` David Laight
2018-02-19 17:37   ` Richard Cochran

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).