Linux wireless drivers development
 help / color / mirror / Atom feed
* [PATCH] wifi: b43: fix cordic arithmetic
@ 2023-06-27 13:00 Dmitry Antipov
  2023-06-27 14:38 ` Larry Finger
  0 siblings, 1 reply; 6+ messages in thread
From: Dmitry Antipov @ 2023-06-27 13:00 UTC (permalink / raw)
  To: Kalle Valo; +Cc: linux-wireless, Jonas Gorski, lvc-project, Dmitry Antipov

In 'lpphy_start_tx_tone()', 'CORDIC_FLOAT((sample.i * max) & 0xFF)'
is invalid because it is (<32-bit> & 0xff) shifted right by 15 bits
and so always evaluates to zero. Looking through brcmsmac's
'wlc_lcnphy_start_tx_tone()', the result should be masked instead,
i. e. 'CORDIC_FLOAT(sample[i].max) & 0xFF'.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Suggested-by: Jonas Gorski <jonas.gorski@gmail.com>
Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
---
 drivers/net/wireless/broadcom/b43/phy_lp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/net/wireless/broadcom/b43/phy_lp.c b/drivers/net/wireless/broadcom/b43/phy_lp.c
index 0e5c076e7544..e8ef04e509aa 100644
--- a/drivers/net/wireless/broadcom/b43/phy_lp.c
+++ b/drivers/net/wireless/broadcom/b43/phy_lp.c
@@ -1788,8 +1788,8 @@ static void lpphy_start_tx_tone(struct b43_wldev *dev, s32 freq, u16 max)
 	for (i = 0; i < samples; i++) {
 		sample = cordic_calc_iq(CORDIC_FIXED(theta));
 		theta += rotation;
-		buf[i] = CORDIC_FLOAT((sample.i * max) & 0xFF) << 8;
-		buf[i] |= CORDIC_FLOAT((sample.q * max) & 0xFF);
+		buf[i] = (u16)((CORDIC_FLOAT(sample.i * max) & 0xFF) << 8);
+		buf[i] |= (u16)(CORDIC_FLOAT(sample.q * max) & 0xFF);
 	}
 
 	b43_lptab_write_bulk(dev, B43_LPTAB16(5, 0), samples, buf);
-- 
2.41.0


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

* Re: [PATCH] wifi: b43: fix cordic arithmetic
  2023-06-27 13:00 [PATCH] wifi: b43: fix cordic arithmetic Dmitry Antipov
@ 2023-06-27 14:38 ` Larry Finger
  2023-06-27 15:13   ` [PATCH] [v2] " Dmitry Antipov
  0 siblings, 1 reply; 6+ messages in thread
From: Larry Finger @ 2023-06-27 14:38 UTC (permalink / raw)
  To: Dmitry Antipov, Kalle Valo; +Cc: linux-wireless, Jonas Gorski, lvc-project

On 6/27/23 08:00, Dmitry Antipov wrote:
> In 'lpphy_start_tx_tone()', 'CORDIC_FLOAT((sample.i * max) & 0xFF)'
> is invalid because it is (<32-bit> & 0xff) shifted right by 15 bits
> and so always evaluates to zero. Looking through brcmsmac's
> 'wlc_lcnphy_start_tx_tone()', the result should be masked instead,
> i. e. 'CORDIC_FLOAT(sample[i].max) & 0xFF'.
> 
> Found by Linux Verification Center (linuxtesting.org) with SVACE.
> 
> Suggested-by: Jonas Gorski <jonas.gorski@gmail.com>
> Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
> ---
>   drivers/net/wireless/broadcom/b43/phy_lp.c | 4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/net/wireless/broadcom/b43/phy_lp.c b/drivers/net/wireless/broadcom/b43/phy_lp.c
> index 0e5c076e7544..e8ef04e509aa 100644
> --- a/drivers/net/wireless/broadcom/b43/phy_lp.c
> +++ b/drivers/net/wireless/broadcom/b43/phy_lp.c
> @@ -1788,8 +1788,8 @@ static void lpphy_start_tx_tone(struct b43_wldev *dev, s32 freq, u16 max)
>   	for (i = 0; i < samples; i++) {
>   		sample = cordic_calc_iq(CORDIC_FIXED(theta));
>   		theta += rotation;
> -		buf[i] = CORDIC_FLOAT((sample.i * max) & 0xFF) << 8;
> -		buf[i] |= CORDIC_FLOAT((sample.q * max) & 0xFF);
> +		buf[i] = (u16)((CORDIC_FLOAT(sample.i * max) & 0xFF) << 8);
> +		buf[i] |= (u16)(CORDIC_FLOAT(sample.q * max) & 0xFF);
>   	}
>   
>   	b43_lptab_write_bulk(dev, B43_LPTAB16(5, 0), samples, buf);

This has not yet been tested, but it does need a "Fixes:" tag, and a Cc for stable.

Larry


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

* [PATCH] [v2] wifi: b43: fix cordic arithmetic
  2023-06-27 14:38 ` Larry Finger
@ 2023-06-27 15:13   ` Dmitry Antipov
  2023-06-28  8:24     ` David Laight
  0 siblings, 1 reply; 6+ messages in thread
From: Dmitry Antipov @ 2023-06-27 15:13 UTC (permalink / raw)
  To: Larry Finger
  Cc: linux-wireless, Kalle Valo, Jonas Gorski, lvc-project,
	Dmitry Antipov, stable

In 'lpphy_start_tx_tone()', 'CORDIC_FLOAT((sample.i * max) & 0xFF)'
is invalid because it is (<32-bit> & 0xff) shifted right by 15 bits
and so always evaluates to zero. Looking through brcmsmac's
'wlc_lcnphy_start_tx_tone()', the result should be masked instead,
i. e. 'CORDIC_FLOAT(sample[i].max) & 0xFF'.

Fixes: 6f98e62a9f1b ("b43: update cordic code to match current specs")

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Cc: stable@vger.kernel.org
Suggested-by: Jonas Gorski <jonas.gorski@gmail.com>
Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
---
v2: add Cc: stable and Fixes: (Larry Finger)
---
 drivers/net/wireless/broadcom/b43/phy_lp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/net/wireless/broadcom/b43/phy_lp.c b/drivers/net/wireless/broadcom/b43/phy_lp.c
index 0e5c076e7544..e8ef04e509aa 100644
--- a/drivers/net/wireless/broadcom/b43/phy_lp.c
+++ b/drivers/net/wireless/broadcom/b43/phy_lp.c
@@ -1788,8 +1788,8 @@ static void lpphy_start_tx_tone(struct b43_wldev *dev, s32 freq, u16 max)
 	for (i = 0; i < samples; i++) {
 		sample = cordic_calc_iq(CORDIC_FIXED(theta));
 		theta += rotation;
-		buf[i] = CORDIC_FLOAT((sample.i * max) & 0xFF) << 8;
-		buf[i] |= CORDIC_FLOAT((sample.q * max) & 0xFF);
+		buf[i] = (u16)((CORDIC_FLOAT(sample.i * max) & 0xFF) << 8);
+		buf[i] |= (u16)(CORDIC_FLOAT(sample.q * max) & 0xFF);
 	}
 
 	b43_lptab_write_bulk(dev, B43_LPTAB16(5, 0), samples, buf);
-- 
2.41.0


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

* RE: [PATCH] [v2] wifi: b43: fix cordic arithmetic
  2023-06-27 15:13   ` [PATCH] [v2] " Dmitry Antipov
@ 2023-06-28  8:24     ` David Laight
  2023-06-28 10:45       ` Dmitry Antipov
  0 siblings, 1 reply; 6+ messages in thread
From: David Laight @ 2023-06-28  8:24 UTC (permalink / raw)
  To: 'Dmitry Antipov', Larry Finger
  Cc: linux-wireless@vger.kernel.org, Kalle Valo, Jonas Gorski,
	lvc-project@linuxtesting.org, stable@vger.kernel.org

From: Dmitry Antipov
> Sent: 27 June 2023 16:14
> 
> In 'lpphy_start_tx_tone()', 'CORDIC_FLOAT((sample.i * max) & 0xFF)'
> is invalid because it is (<32-bit> & 0xff) shifted right by 15 bits
> and so always evaluates to zero. Looking through brcmsmac's
> 'wlc_lcnphy_start_tx_tone()', the result should be masked instead,
> i. e. 'CORDIC_FLOAT(sample[i].max) & 0xFF'.
> 
> Fixes: 6f98e62a9f1b ("b43: update cordic code to match current specs")
> 
> Found by Linux Verification Center (linuxtesting.org) with SVACE.
> 
> Cc: stable@vger.kernel.org
> Suggested-by: Jonas Gorski <jonas.gorski@gmail.com>
> Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
> ---
> v2: add Cc: stable and Fixes: (Larry Finger)
> ---
>  drivers/net/wireless/broadcom/b43/phy_lp.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/net/wireless/broadcom/b43/phy_lp.c b/drivers/net/wireless/broadcom/b43/phy_lp.c
> index 0e5c076e7544..e8ef04e509aa 100644
> --- a/drivers/net/wireless/broadcom/b43/phy_lp.c
> +++ b/drivers/net/wireless/broadcom/b43/phy_lp.c
> @@ -1788,8 +1788,8 @@ static void lpphy_start_tx_tone(struct b43_wldev *dev, s32 freq, u16 max)
>  	for (i = 0; i < samples; i++) {
>  		sample = cordic_calc_iq(CORDIC_FIXED(theta));
>  		theta += rotation;
> -		buf[i] = CORDIC_FLOAT((sample.i * max) & 0xFF) << 8;
> -		buf[i] |= CORDIC_FLOAT((sample.q * max) & 0xFF);
> +		buf[i] = (u16)((CORDIC_FLOAT(sample.i * max) & 0xFF) << 8);
> +		buf[i] |= (u16)(CORDIC_FLOAT(sample.q * max) & 0xFF);

What are the (u16) casts for?
This code is actually called exactly once with max == 100.
The .i and .q are the sine and cosine << 16 (signed).
The CORDIC_FLOAT() is basically >> 16 (not 15) so the result should
be between -100 and +100.
The & 0xFF is there to strip the sign.
The sin+cos are then packed into a short[] then unpacked to be
written to the hardware later.

>  	}
> 
>  	b43_lptab_write_bulk(dev, B43_LPTAB16(5, 0), samples, buf);

Don't open the bag of worms that contains the above :-)

	David

> --
> 2.41.0

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)


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

* Re: [PATCH] [v2] wifi: b43: fix cordic arithmetic
  2023-06-28  8:24     ` David Laight
@ 2023-06-28 10:45       ` Dmitry Antipov
  2023-06-28 13:19         ` David Laight
  0 siblings, 1 reply; 6+ messages in thread
From: Dmitry Antipov @ 2023-06-28 10:45 UTC (permalink / raw)
  To: David Laight, Larry Finger
  Cc: linux-wireless@vger.kernel.org, Kalle Valo, Jonas Gorski,
	lvc-project@linuxtesting.org, stable@vger.kernel.org

On 6/28/23 11:24, David Laight wrote:

> What are the (u16) casts for?

Well, this is a kind of a C language purism intended to silence

warning: conversion from ‘int’ to ‘u16’ {aka ‘short unsigned int’} may change value [-Wconversion]

observed with W=123.

Dmitry


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

* RE: [PATCH] [v2] wifi: b43: fix cordic arithmetic
  2023-06-28 10:45       ` Dmitry Antipov
@ 2023-06-28 13:19         ` David Laight
  0 siblings, 0 replies; 6+ messages in thread
From: David Laight @ 2023-06-28 13:19 UTC (permalink / raw)
  To: 'Dmitry Antipov', Larry Finger
  Cc: linux-wireless@vger.kernel.org, Kalle Valo, Jonas Gorski,
	lvc-project@linuxtesting.org, stable@vger.kernel.org

From: Dmitry Antipov
> Sent: 28 June 2023 11:45
> 
> On 6/28/23 11:24, David Laight wrote:
> 
> > What are the (u16) casts for?
> 
> Well, this is a kind of a C language purism intended to silence
> warning: conversion from ‘int’ to ‘u16’ {aka ‘short unsigned int’} may change value [-Wconversion]
> observed with W=123.

The problem is that the casts can hide more than just a value being truncated.
In this case the compiler even knows the values don't overflow.

FWIW I've seen generated code for:
	*cp++ = (unsigned char)(value & 0xff);
that masked the value with 0xff once for the & a second time for
the cast and then stored the low byte.
I don't think modern gcc will do that, but a dumb compiler will.

If -Wconversion bleats about these sort of assignments it just
needs permanently disabling (or deleting from the compiler!).

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

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

end of thread, other threads:[~2023-06-28 13:19 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-06-27 13:00 [PATCH] wifi: b43: fix cordic arithmetic Dmitry Antipov
2023-06-27 14:38 ` Larry Finger
2023-06-27 15:13   ` [PATCH] [v2] " Dmitry Antipov
2023-06-28  8:24     ` David Laight
2023-06-28 10:45       ` Dmitry Antipov
2023-06-28 13:19         ` David Laight

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox