linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 00/12] treewide: Fix GENMASK misuses
@ 2019-07-10  5:04 ` Joe Perches
  2019-07-10  5:04   ` [PATCH 04/12] iio: adc: max9611: Fix misuse of GENMASK macro Joe Perches
                     ` (3 more replies)
  0 siblings, 4 replies; 13+ messages in thread
From: Joe Perches @ 2019-07-10  5:04 UTC (permalink / raw)
  To: Andrew Morton, Patrick Venture, Nancy Yuen, Benjamin Fair,
	Andrew Jeffery, openbmc, linux-kernel, linux-aspeed,
	linux-arm-kernel, linux-amlogic, netdev, linux-mediatek,
	linux-stm32, linux-wireless, linux-media
  Cc: dri-devel, linux-iio, linux-mmc, devel, alsa-devel

These GENMASK uses are inverted argument order and the
actual masks produced are incorrect.  Fix them.

Add checkpatch tests to help avoid more misuses too.

Joe Perches (12):
  checkpatch: Add GENMASK tests
  clocksource/drivers/npcm: Fix misuse of GENMASK macro
  drm: aspeed_gfx: Fix misuse of GENMASK macro
  iio: adc: max9611: Fix misuse of GENMASK macro
  irqchip/gic-v3-its: Fix misuse of GENMASK macro
  mmc: meson-mx-sdio: Fix misuse of GENMASK macro
  net: ethernet: mediatek: Fix misuses of GENMASK macro
  net: stmmac: Fix misuses of GENMASK macro
  rtw88: Fix misuse of GENMASK macro
  phy: amlogic: G12A: Fix misuse of GENMASK macro
  staging: media: cedrus: Fix misuse of GENMASK macro
  ASoC: wcd9335: Fix misuse of GENMASK macro

 drivers/clocksource/timer-npcm7xx.c               |  2 +-
 drivers/gpu/drm/aspeed/aspeed_gfx.h               |  2 +-
 drivers/iio/adc/max9611.c                         |  2 +-
 drivers/irqchip/irq-gic-v3-its.c                  |  2 +-
 drivers/mmc/host/meson-mx-sdio.c                  |  2 +-
 drivers/net/ethernet/mediatek/mtk_eth_soc.h       |  2 +-
 drivers/net/ethernet/mediatek/mtk_sgmii.c         |  2 +-
 drivers/net/ethernet/stmicro/stmmac/descs.h       |  2 +-
 drivers/net/ethernet/stmicro/stmmac/dwmac-sun8i.c |  4 ++--
 drivers/net/wireless/realtek/rtw88/rtw8822b.c     |  2 +-
 drivers/phy/amlogic/phy-meson-g12a-usb2.c         |  2 +-
 drivers/staging/media/sunxi/cedrus/cedrus_regs.h  |  2 +-
 scripts/checkpatch.pl                             | 15 +++++++++++++++
 sound/soc/codecs/wcd-clsh-v2.c                    |  2 +-
 14 files changed, 29 insertions(+), 14 deletions(-)

-- 
2.15.0


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

* [PATCH 04/12] iio: adc: max9611: Fix misuse of GENMASK macro
  2019-07-10  5:04 ` [PATCH 00/12] treewide: Fix GENMASK misuses Joe Perches
@ 2019-07-10  5:04   ` Joe Perches
  2019-07-14 11:54     ` Jonathan Cameron
  2019-07-10  9:17   ` [PATCH 00/12] treewide: Fix GENMASK misuses Johannes Berg
                     ` (2 subsequent siblings)
  3 siblings, 1 reply; 13+ messages in thread
From: Joe Perches @ 2019-07-10  5:04 UTC (permalink / raw)
  To: Andrew Morton, linux-kernel
  Cc: Jonathan Cameron, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, linux-iio

Arguments are supposed to be ordered high then low.

Signed-off-by: Joe Perches <joe@perches.com>
---
 drivers/iio/adc/max9611.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/iio/adc/max9611.c b/drivers/iio/adc/max9611.c
index 917223d5ff5b..0e3c6529fc4c 100644
--- a/drivers/iio/adc/max9611.c
+++ b/drivers/iio/adc/max9611.c
@@ -83,7 +83,7 @@
 #define MAX9611_TEMP_MAX_POS		0x7f80
 #define MAX9611_TEMP_MAX_NEG		0xff80
 #define MAX9611_TEMP_MIN_NEG		0xd980
-#define MAX9611_TEMP_MASK		GENMASK(7, 15)
+#define MAX9611_TEMP_MASK		GENMASK(15, 7)
 #define MAX9611_TEMP_SHIFT		0x07
 #define MAX9611_TEMP_RAW(_r)		((_r) >> MAX9611_TEMP_SHIFT)
 #define MAX9611_TEMP_SCALE_NUM		1000000
-- 
2.15.0


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

* Re: [PATCH 00/12] treewide: Fix GENMASK misuses
  2019-07-10  5:04 ` [PATCH 00/12] treewide: Fix GENMASK misuses Joe Perches
  2019-07-10  5:04   ` [PATCH 04/12] iio: adc: max9611: Fix misuse of GENMASK macro Joe Perches
@ 2019-07-10  9:17   ` Johannes Berg
  2019-07-10  9:43     ` Russell King - ARM Linux admin
  2019-07-11 21:30   ` David Miller
  2019-07-12 12:54   ` Andrzej Hajda
  3 siblings, 1 reply; 13+ messages in thread
From: Johannes Berg @ 2019-07-10  9:17 UTC (permalink / raw)
  To: Joe Perches, Andrew Morton, Patrick Venture, Nancy Yuen,
	Benjamin Fair, Andrew Jeffery, openbmc, linux-kernel,
	linux-aspeed, linux-arm-kernel, linux-amlogic, netdev,
	linux-mediatek, linux-stm32, linux-wireless, linux-media
  Cc: dri-devel, linux-iio, linux-mmc, devel, alsa-devel

On Tue, 2019-07-09 at 22:04 -0700, Joe Perches wrote:
> These GENMASK uses are inverted argument order and the
> actual masks produced are incorrect.  Fix them.
> 
> Add checkpatch tests to help avoid more misuses too.
> 
> Joe Perches (12):
>   checkpatch: Add GENMASK tests

IMHO this doesn't make a lot of sense as a checkpatch test - just throw
in a BUILD_BUG_ON()?

johannes


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

* Re: [PATCH 00/12] treewide: Fix GENMASK misuses
  2019-07-10  9:17   ` [PATCH 00/12] treewide: Fix GENMASK misuses Johannes Berg
@ 2019-07-10  9:43     ` Russell King - ARM Linux admin
  2019-07-10 15:45       ` Joe Perches
  0 siblings, 1 reply; 13+ messages in thread
From: Russell King - ARM Linux admin @ 2019-07-10  9:43 UTC (permalink / raw)
  To: Johannes Berg
  Cc: Joe Perches, Andrew Morton, Patrick Venture, Nancy Yuen,
	Benjamin Fair, Andrew Jeffery, openbmc, linux-kernel,
	linux-aspeed, linux-arm-kernel, linux-amlogic, netdev,
	linux-mediatek, linux-stm32, linux-wireless, linux-media,
	linux-iio, devel, alsa-devel, linux-mmc, dri-devel

On Wed, Jul 10, 2019 at 11:17:31AM +0200, Johannes Berg wrote:
> On Tue, 2019-07-09 at 22:04 -0700, Joe Perches wrote:
> > These GENMASK uses are inverted argument order and the
> > actual masks produced are incorrect.  Fix them.
> > 
> > Add checkpatch tests to help avoid more misuses too.
> > 
> > Joe Perches (12):
> >   checkpatch: Add GENMASK tests
> 
> IMHO this doesn't make a lot of sense as a checkpatch test - just throw
> in a BUILD_BUG_ON()?

My personal take on this is that GENMASK() is really not useful, it's
just pure obfuscation and leads to exactly these kinds of mistakes.

Yes, I fully understand the argument that you can just specify the
start and end bits, and it _in theory_ makes the code more readable.

However, the problem is when writing code.  GENMASK(a, b).  Is a the
starting bit or ending bit?  Is b the number of bits?  It's confusing
and causes mistakes resulting in incorrect code.  A BUILD_BUG_ON()
can catch some of the cases, but not all of them.

For example:

	GENMASK(6, 2)

would satisify the requirement that a > b, so a BUILD_BUG_ON() will
not trigger, but was the author meaning 0x3c or 0xc0?

Personally, I've decided I am _not_ going to use GENMASK() in my code
because I struggle to get the macro arguments correct - I'm _much_
happier, and it is way more reliable for me to write the mask in hex
notation.

I think this is where use of a ternary operator would come in use.  The
normal way of writing a number of bits tends to be "a:b", so if GENMASK
took something like GENMASK(6:2), then I'd have less issue with it,
because it's argument is then in a familiar notation.

Yes, I'm sure that someone will point out that the GENMASK arguments
are just in the same order, but that doesn't prevent _me_ frequently
getting it wrong - and that's the point.  The macro seems to me to
cause more problems than it solves.

-- 
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTC broadband for 0.8mile line in suburbia: sync at 12.1Mbps down 622kbps up
According to speedtest.net: 11.9Mbps down 500kbps up

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

* Re: [PATCH 00/12] treewide: Fix GENMASK misuses
  2019-07-10  9:43     ` Russell King - ARM Linux admin
@ 2019-07-10 15:45       ` Joe Perches
  2019-07-10 16:01         ` Joe Perches
  0 siblings, 1 reply; 13+ messages in thread
From: Joe Perches @ 2019-07-10 15:45 UTC (permalink / raw)
  To: Russell King - ARM Linux admin, Johannes Berg
  Cc: Andrew Morton, Patrick Venture, Nancy Yuen, Benjamin Fair,
	Andrew Jeffery, openbmc, linux-kernel, linux-aspeed,
	linux-arm-kernel, linux-amlogic, netdev, linux-mediatek,
	linux-stm32, linux-wireless, linux-media, linux-iio, devel,
	alsa-devel, linux-mmc, dri-devel

On Wed, 2019-07-10 at 10:43 +0100, Russell King - ARM Linux admin wrote:
> On Wed, Jul 10, 2019 at 11:17:31AM +0200, Johannes Berg wrote:
> > On Tue, 2019-07-09 at 22:04 -0700, Joe Perches wrote:
> > > These GENMASK uses are inverted argument order and the
> > > actual masks produced are incorrect.  Fix them.
> > > 
> > > Add checkpatch tests to help avoid more misuses too.
> > > 
> > > Joe Perches (12):
> > >   checkpatch: Add GENMASK tests
> > 
> > IMHO this doesn't make a lot of sense as a checkpatch test - just throw
> > in a BUILD_BUG_ON()?

I tried that.

It'd can't be done as it's used in declarations
and included in asm files and it uses the UL()
macro.

I also tried just making it do the right thing
whatever the argument order.

Oh well.

> My personal take on this is that GENMASK() is really not useful, it's
> just pure obfuscation and leads to exactly these kinds of mistakes.
> 
> Yes, I fully understand the argument that you can just specify the
> start and end bits, and it _in theory_ makes the code more readable.
> 
> However, the problem is when writing code.  GENMASK(a, b).  Is a the
> starting bit or ending bit?  Is b the number of bits?  It's confusing
> and causes mistakes resulting in incorrect code.  A BUILD_BUG_ON()
> can catch some of the cases, but not all of them.

It's a horrid little macro and I agree with Russell.

I also think if it existed at all it should have been
GENMASK(low, high) not GENMASK(high, low).

I


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

* Re: [PATCH 00/12] treewide: Fix GENMASK misuses
  2019-07-10 15:45       ` Joe Perches
@ 2019-07-10 16:01         ` Joe Perches
  0 siblings, 0 replies; 13+ messages in thread
From: Joe Perches @ 2019-07-10 16:01 UTC (permalink / raw)
  To: Russell King - ARM Linux admin, Johannes Berg
  Cc: Andrew Morton, Patrick Venture, Nancy Yuen, Benjamin Fair,
	Andrew Jeffery, openbmc, linux-kernel, linux-aspeed,
	linux-arm-kernel, linux-amlogic, netdev, linux-mediatek,
	linux-stm32, linux-wireless, linux-media, linux-iio, devel,
	alsa-devel, linux-mmc, dri-devel

On Wed, 2019-07-10 at 08:45 -0700, Joe Perches wrote:
> On Wed, 2019-07-10 at 10:43 +0100, Russell King - ARM Linux admin wrote:
> > On Wed, Jul 10, 2019 at 11:17:31AM +0200, Johannes Berg wrote:
> > > On Tue, 2019-07-09 at 22:04 -0700, Joe Perches wrote:
> > > > These GENMASK uses are inverted argument order and the
> > > > actual masks produced are incorrect.  Fix them.
> > > > 
> > > > Add checkpatch tests to help avoid more misuses too.
> > > > 
> > > > Joe Perches (12):
> > > >   checkpatch: Add GENMASK tests
> > > 
> > > IMHO this doesn't make a lot of sense as a checkpatch test - just throw
> > > in a BUILD_BUG_ON()?
> 
> I tried that.
> 
> It'd can't be done as it's used in declarations
> and included in asm files and it uses the UL()
> macro.
> 
> I also tried just making it do the right thing
> whatever the argument order.

I forgot.

I also made all those arguments when it was
introduced in 2013.

https://lore.kernel.org/patchwork/patch/414248/

> Oh well.

yeah.



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

* Re: [PATCH 00/12] treewide: Fix GENMASK misuses
  2019-07-10  5:04 ` [PATCH 00/12] treewide: Fix GENMASK misuses Joe Perches
  2019-07-10  5:04   ` [PATCH 04/12] iio: adc: max9611: Fix misuse of GENMASK macro Joe Perches
  2019-07-10  9:17   ` [PATCH 00/12] treewide: Fix GENMASK misuses Johannes Berg
@ 2019-07-11 21:30   ` David Miller
  2019-07-12 12:54   ` Andrzej Hajda
  3 siblings, 0 replies; 13+ messages in thread
From: David Miller @ 2019-07-11 21:30 UTC (permalink / raw)
  To: joe
  Cc: akpm, venture, yuenn, benjaminfair, andrew, openbmc,
	linux-kernel, linux-aspeed, linux-arm-kernel, linux-amlogic,
	netdev, linux-mediatek, linux-stm32, linux-wireless, linux-media,
	dri-devel, linux-iio, linux-mmc, devel, alsa-devel

From: Joe Perches <joe@perches.com>
Date: Tue,  9 Jul 2019 22:04:13 -0700

> These GENMASK uses are inverted argument order and the
> actual masks produced are incorrect.  Fix them.
> 
> Add checkpatch tests to help avoid more misuses too.

Patches #7 and #8 applied to 'net', with appropriate Fixes tags
added to #8.

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

* Re: [PATCH 00/12] treewide: Fix GENMASK misuses
  2019-07-10  5:04 ` [PATCH 00/12] treewide: Fix GENMASK misuses Joe Perches
                     ` (2 preceding siblings ...)
  2019-07-11 21:30   ` David Miller
@ 2019-07-12 12:54   ` Andrzej Hajda
  3 siblings, 0 replies; 13+ messages in thread
From: Andrzej Hajda @ 2019-07-12 12:54 UTC (permalink / raw)
  To: Joe Perches, Andrew Morton, Patrick Venture, Nancy Yuen,
	Benjamin Fair, Andrew Jeffery, openbmc, linux-kernel,
	linux-aspeed, linux-arm-kernel, linux-amlogic, netdev,
	linux-mediatek, linux-stm32, linux-wireless, linux-media
  Cc: linux-iio, devel, alsa-devel, linux-mmc, dri-devel

Hi Joe,

On 10.07.2019 07:04, Joe Perches wrote:
> These GENMASK uses are inverted argument order and the
> actual masks produced are incorrect.  Fix them.
>
> Add checkpatch tests to help avoid more misuses too.
>
> Joe Perches (12):
>   checkpatch: Add GENMASK tests
>   clocksource/drivers/npcm: Fix misuse of GENMASK macro
>   drm: aspeed_gfx: Fix misuse of GENMASK macro
>   iio: adc: max9611: Fix misuse of GENMASK macro
>   irqchip/gic-v3-its: Fix misuse of GENMASK macro
>   mmc: meson-mx-sdio: Fix misuse of GENMASK macro
>   net: ethernet: mediatek: Fix misuses of GENMASK macro
>   net: stmmac: Fix misuses of GENMASK macro
>   rtw88: Fix misuse of GENMASK macro
>   phy: amlogic: G12A: Fix misuse of GENMASK macro
>   staging: media: cedrus: Fix misuse of GENMASK macro
>   ASoC: wcd9335: Fix misuse of GENMASK macro
>
>  drivers/clocksource/timer-npcm7xx.c               |  2 +-
>  drivers/gpu/drm/aspeed/aspeed_gfx.h               |  2 +-
>  drivers/iio/adc/max9611.c                         |  2 +-
>  drivers/irqchip/irq-gic-v3-its.c                  |  2 +-
>  drivers/mmc/host/meson-mx-sdio.c                  |  2 +-
>  drivers/net/ethernet/mediatek/mtk_eth_soc.h       |  2 +-
>  drivers/net/ethernet/mediatek/mtk_sgmii.c         |  2 +-
>  drivers/net/ethernet/stmicro/stmmac/descs.h       |  2 +-
>  drivers/net/ethernet/stmicro/stmmac/dwmac-sun8i.c |  4 ++--
>  drivers/net/wireless/realtek/rtw88/rtw8822b.c     |  2 +-
>  drivers/phy/amlogic/phy-meson-g12a-usb2.c         |  2 +-
>  drivers/staging/media/sunxi/cedrus/cedrus_regs.h  |  2 +-
>  scripts/checkpatch.pl                             | 15 +++++++++++++++
>  sound/soc/codecs/wcd-clsh-v2.c                    |  2 +-
>  14 files changed, 29 insertions(+), 14 deletions(-)
>
After adding following compile time check:

------

diff --git a/Makefile b/Makefile
index 5102b2bbd224..ac4ea5f443a9 100644
--- a/Makefile
+++ b/Makefile
@@ -457,7 +457,7 @@ KBUILD_AFLAGS   := -D__ASSEMBLY__ -fno-PIE
 KBUILD_CFLAGS   := -Wall -Wundef -Werror=strict-prototypes -Wno-trigraphs \
                   -fno-strict-aliasing -fno-common -fshort-wchar -fno-PIE \
                   -Werror=implicit-function-declaration
-Werror=implicit-int \
-                  -Wno-format-security \
+                  -Wno-format-security -Werror=div-by-zero \
                   -std=gnu89
 KBUILD_CPPFLAGS := -D__KERNEL__
 KBUILD_AFLAGS_KERNEL :=
diff --git a/include/linux/bits.h b/include/linux/bits.h
index 669d69441a62..61d74b103055 100644
--- a/include/linux/bits.h
+++ b/include/linux/bits.h
@@ -19,11 +19,11 @@
  * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
  */
 #define GENMASK(h, l) \
-       (((~UL(0)) - (UL(1) << (l)) + 1) & \
+       (((~UL(0)) - (UL(1) << (l)) + 1 + 0/((h) >= (l))) & \
         (~UL(0) >> (BITS_PER_LONG - 1 - (h))))
 
 #define GENMASK_ULL(h, l) \
-       (((~ULL(0)) - (ULL(1) << (l)) + 1) & \
+       (((~ULL(0)) - (ULL(1) << (l)) + 1 + 0/((h) >= (l))) & \
         (~ULL(0) >> (BITS_PER_LONG_LONG - 1 - (h))))
 
 #endif /* __LINUX_BITS_H */

-------

I was able to detect one more GENMASK misue (AARCH64, allyesconfig):

  CC      drivers/phy/rockchip/phy-rockchip-inno-hdmi.o
In file included from ../include/linux/bitops.h:5:0,
                 from ../include/linux/kernel.h:12,
                 from ../include/linux/clk.h:13,
                 from ../drivers/phy/rockchip/phy-rockchip-inno-hdmi.c:9:
../drivers/phy/rockchip/phy-rockchip-inno-hdmi.c: In function
‘inno_hdmi_phy_rk3328_power_on’:
../include/linux/bits.h:22:37: error: division by zero [-Werror=div-by-zero]
  (((~UL(0)) - (UL(1) << (l)) + 1 + 0/((h) >= (l))) & \
                                     ^
../drivers/phy/rockchip/phy-rockchip-inno-hdmi.c:24:42: note: in
expansion of macro ‘GENMASK’
 #define UPDATE(x, h, l)  (((x) << (l)) & GENMASK((h), (l)))
                                          ^~~~~~~
../drivers/phy/rockchip/phy-rockchip-inno-hdmi.c:201:50: note: in
expansion of macro ‘UPDATE’
 #define RK3328_TERM_RESISTOR_CALIB_SPEED_7_0(x)  UPDATE(x, 7, 9)
                                                  ^~~~~~
../drivers/phy/rockchip/phy-rockchip-inno-hdmi.c:1046:26: note: in
expansion of macro ‘RK3328_TERM_RESISTOR_CALIB_SPEED_7_0’
   inno_write(inno, 0xc6, RK3328_TERM_RESISTOR_CALIB_SPEED_7_0(v));


Of course I do not advise to add the check as is to Kernel - it is
undefined behavior area AFAIK.


Regards

Andrzej


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

* Re: [PATCH 04/12] iio: adc: max9611: Fix misuse of GENMASK macro
  2019-07-10  5:04   ` [PATCH 04/12] iio: adc: max9611: Fix misuse of GENMASK macro Joe Perches
@ 2019-07-14 11:54     ` Jonathan Cameron
  2019-07-14 12:19       ` Joe Perches
  0 siblings, 1 reply; 13+ messages in thread
From: Jonathan Cameron @ 2019-07-14 11:54 UTC (permalink / raw)
  To: Joe Perches
  Cc: Andrew Morton, linux-kernel, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, linux-iio

On Tue,  9 Jul 2019 22:04:17 -0700
Joe Perches <joe@perches.com> wrote:

> Arguments are supposed to be ordered high then low.
> 
> Signed-off-by: Joe Perches <joe@perches.com>
Applied to the fixes-togreg branch of iio.git and marked for
stable etc.

Thanks,

Jonathan

> ---
>  drivers/iio/adc/max9611.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/iio/adc/max9611.c b/drivers/iio/adc/max9611.c
> index 917223d5ff5b..0e3c6529fc4c 100644
> --- a/drivers/iio/adc/max9611.c
> +++ b/drivers/iio/adc/max9611.c
> @@ -83,7 +83,7 @@
>  #define MAX9611_TEMP_MAX_POS		0x7f80
>  #define MAX9611_TEMP_MAX_NEG		0xff80
>  #define MAX9611_TEMP_MIN_NEG		0xd980
> -#define MAX9611_TEMP_MASK		GENMASK(7, 15)
> +#define MAX9611_TEMP_MASK		GENMASK(15, 7)
>  #define MAX9611_TEMP_SHIFT		0x07
>  #define MAX9611_TEMP_RAW(_r)		((_r) >> MAX9611_TEMP_SHIFT)
>  #define MAX9611_TEMP_SCALE_NUM		1000000


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

* Re: [PATCH 04/12] iio: adc: max9611: Fix misuse of GENMASK macro
  2019-07-14 11:54     ` Jonathan Cameron
@ 2019-07-14 12:19       ` Joe Perches
  2019-07-14 14:37         ` Jacopo Mondi
  2019-07-29 21:52         ` Jacopo Mondi
  0 siblings, 2 replies; 13+ messages in thread
From: Joe Perches @ 2019-07-14 12:19 UTC (permalink / raw)
  To: Jonathan Cameron, Jacopo Mondi
  Cc: Andrew Morton, linux-kernel, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, linux-iio

On Sun, 2019-07-14 at 12:54 +0100, Jonathan Cameron wrote:
> On Tue,  9 Jul 2019 22:04:17 -0700
> Joe Perches <joe@perches.com> wrote:
> 
> > Arguments are supposed to be ordered high then low.
> > 
> > Signed-off-by: Joe Perches <joe@perches.com>
> 
> Applied to the fixes-togreg branch of iio.git and marked for
> stable etc.

This mask is used in an init function called from a probe.

I don't have this hardware but it looks as if it could
never have worked so I doubt the driver and the hardware
have ever been tested.

Does anyone have this device in actual use?


	regval = ret & MAX9611_TEMP_MASK;

	if ((regval > MAX9611_TEMP_MAX_POS &&
	     regval < MAX9611_TEMP_MIN_NEG) ||
	     regval > MAX9611_TEMP_MAX_NEG) {
		dev_err(max9611->dev,
			"Invalid value received from ADC 0x%4x: aborting\n",
			regval);
		return -EIO;
	}


> Thanks,
> 
> Jonathan
> 
> > ---
> >  drivers/iio/adc/max9611.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/drivers/iio/adc/max9611.c b/drivers/iio/adc/max9611.c
> > index 917223d5ff5b..0e3c6529fc4c 100644
> > --- a/drivers/iio/adc/max9611.c
> > +++ b/drivers/iio/adc/max9611.c
> > @@ -83,7 +83,7 @@
> >  #define MAX9611_TEMP_MAX_POS		0x7f80
> >  #define MAX9611_TEMP_MAX_NEG		0xff80
> >  #define MAX9611_TEMP_MIN_NEG		0xd980
> > -#define MAX9611_TEMP_MASK		GENMASK(7, 15)
> > +#define MAX9611_TEMP_MASK		GENMASK(15, 7)
> >  #define MAX9611_TEMP_SHIFT		0x07
> >  #define MAX9611_TEMP_RAW(_r)		((_r) >> MAX9611_TEMP_SHIFT)
> >  #define MAX9611_TEMP_SCALE_NUM		1000000


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

* Re: [PATCH 04/12] iio: adc: max9611: Fix misuse of GENMASK macro
  2019-07-14 12:19       ` Joe Perches
@ 2019-07-14 14:37         ` Jacopo Mondi
  2019-07-29 21:52         ` Jacopo Mondi
  1 sibling, 0 replies; 13+ messages in thread
From: Jacopo Mondi @ 2019-07-14 14:37 UTC (permalink / raw)
  To: Joe Perches
  Cc: Jonathan Cameron, Jacopo Mondi, Andrew Morton, linux-kernel,
	Hartmut Knaack, Lars-Peter Clausen, Peter Meerwald-Stadler,
	linux-iio

[-- Attachment #1: Type: text/plain, Size: 2394 bytes --]

Hi Joe, Jonathan,

On Sun, Jul 14, 2019 at 05:19:32AM -0700, Joe Perches wrote:
> On Sun, 2019-07-14 at 12:54 +0100, Jonathan Cameron wrote:
> > On Tue,  9 Jul 2019 22:04:17 -0700
> > Joe Perches <joe@perches.com> wrote:
> >
> > > Arguments are supposed to be ordered high then low.
> > >
> > > Signed-off-by: Joe Perches <joe@perches.com>
> >
> > Applied to the fixes-togreg branch of iio.git and marked for
> > stable etc.
>
> This mask is used in an init function called from a probe.
>
> I don't have this hardware but it looks as if it could
> never have worked so I doubt the driver and the hardware
> have ever been tested.
>

Ups, this is embarassing! Thanks for noticing

I actually tested the device before sending the driver of course

> Does anyone have this device in actual use?
>

The driver is currently in use in Renesas Gen3 Salvator boards:
arch/arm64/boot/dts/renesas/salvator-common.dtsi:466

I might need some time before I can actually test and tell what's
happening though. It might work by pure chance. Fortunately the mask
is only used for validation of the temperature reading at probe time,
and I can tell this passes (we would have noticed otherwise, Salvator
is one of the reference Gen3 platforms for upstream development).

As said I might need some time before I can test this, but the change
is indeed correct.

Thanks
   j

>
> 	regval = ret & MAX9611_TEMP_MASK;
>
> 	if ((regval > MAX9611_TEMP_MAX_POS &&
> 	     regval < MAX9611_TEMP_MIN_NEG) ||
> 	     regval > MAX9611_TEMP_MAX_NEG) {
> 		dev_err(max9611->dev,
> 			"Invalid value received from ADC 0x%4x: aborting\n",
> 			regval);
> 		return -EIO;
> 	}
>
>
> > Thanks,
> >
> > Jonathan
> >
> > > ---
> > >  drivers/iio/adc/max9611.c | 2 +-
> > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > >
> > > diff --git a/drivers/iio/adc/max9611.c b/drivers/iio/adc/max9611.c
> > > index 917223d5ff5b..0e3c6529fc4c 100644
> > > --- a/drivers/iio/adc/max9611.c
> > > +++ b/drivers/iio/adc/max9611.c
> > > @@ -83,7 +83,7 @@
> > >  #define MAX9611_TEMP_MAX_POS		0x7f80
> > >  #define MAX9611_TEMP_MAX_NEG		0xff80
> > >  #define MAX9611_TEMP_MIN_NEG		0xd980
> > > -#define MAX9611_TEMP_MASK		GENMASK(7, 15)
> > > +#define MAX9611_TEMP_MASK		GENMASK(15, 7)
> > >  #define MAX9611_TEMP_SHIFT		0x07
> > >  #define MAX9611_TEMP_RAW(_r)		((_r) >> MAX9611_TEMP_SHIFT)
> > >  #define MAX9611_TEMP_SCALE_NUM		1000000
>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [PATCH 04/12] iio: adc: max9611: Fix misuse of GENMASK macro
  2019-07-14 12:19       ` Joe Perches
  2019-07-14 14:37         ` Jacopo Mondi
@ 2019-07-29 21:52         ` Jacopo Mondi
  2019-07-31  8:37           ` Jonathan Cameron
  1 sibling, 1 reply; 13+ messages in thread
From: Jacopo Mondi @ 2019-07-29 21:52 UTC (permalink / raw)
  To: Joe Perches
  Cc: Jonathan Cameron, Jacopo Mondi, Andrew Morton, linux-kernel,
	Hartmut Knaack, Lars-Peter Clausen, Peter Meerwald-Stadler,
	linux-iio

[-- Attachment #1: Type: text/plain, Size: 2717 bytes --]

Hello,
  so I finally run some test and...

On Sun, Jul 14, 2019 at 05:19:32AM -0700, Joe Perches wrote:
> On Sun, 2019-07-14 at 12:54 +0100, Jonathan Cameron wrote:
> > On Tue,  9 Jul 2019 22:04:17 -0700
> > Joe Perches <joe@perches.com> wrote:
> >
> > > Arguments are supposed to be ordered high then low.
> > >
> > > Signed-off-by: Joe Perches <joe@perches.com>
> >
> > Applied to the fixes-togreg branch of iio.git and marked for
> > stable etc.

I don't see it in v5.3-rc2, has it been collected or are we still in
time for an additional fix?

>
> This mask is used in an init function called from a probe.
>
> I don't have this hardware but it looks as if it could
> never have worked so I doubt the driver and the hardware
> have ever been tested.
>
> Does anyone have this device in actual use?

Because it turns out this is 2 times embarrassing. The mask definition
is indeed wrong, as Joe reported and fixed, but also this line
>
> 	regval = ret & MAX9611_TEMP_MASK;

is very wrong as regval is read as:
        ret = max9611_read_single(max9611, CONF_TEMP, &regval);

So that should actually be:
        regval &= MAX9611_TEMP_MASK;
not
 	regval = ret & MAX9611_TEMP_MASK;
Ups...

Yes, it worked by chance, as regval was always 0, which is in the
range of acceptable temperatures :/

>
> 	if ((regval > MAX9611_TEMP_MAX_POS &&
> 	     regval < MAX9611_TEMP_MIN_NEG) ||
> 	     regval > MAX9611_TEMP_MAX_NEG) {

Also reading this condition and how I had defined the temperature
calculation formula makes me wonder if this it totally correct, but
for the moment:

1) if Joe's patch has been collected, I can send an additional patch to
fix how regval is computed.
2) If Joe's patch still have to be collected, the regval computation
might be fixed there.

Sorry for taking so long to get back to you and thanks for noticing.

Thanks
  j

> 		dev_err(max9611->dev,
> 			"Invalid value received from ADC 0x%4x: aborting\n",
> 			regval);
> 		return -EIO;
> 	}
>
>
> > Thanks,
> >
> > Jonathan
> >
> > > ---
> > >  drivers/iio/adc/max9611.c | 2 +-
> > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > >
> > > diff --git a/drivers/iio/adc/max9611.c b/drivers/iio/adc/max9611.c
> > > index 917223d5ff5b..0e3c6529fc4c 100644
> > > --- a/drivers/iio/adc/max9611.c
> > > +++ b/drivers/iio/adc/max9611.c
> > > @@ -83,7 +83,7 @@
> > >  #define MAX9611_TEMP_MAX_POS		0x7f80
> > >  #define MAX9611_TEMP_MAX_NEG		0xff80
> > >  #define MAX9611_TEMP_MIN_NEG		0xd980
> > > -#define MAX9611_TEMP_MASK		GENMASK(7, 15)
> > > +#define MAX9611_TEMP_MASK		GENMASK(15, 7)
> > >  #define MAX9611_TEMP_SHIFT		0x07
> > >  #define MAX9611_TEMP_RAW(_r)		((_r) >> MAX9611_TEMP_SHIFT)
> > >  #define MAX9611_TEMP_SCALE_NUM		1000000
>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [PATCH 04/12] iio: adc: max9611: Fix misuse of GENMASK macro
  2019-07-29 21:52         ` Jacopo Mondi
@ 2019-07-31  8:37           ` Jonathan Cameron
  0 siblings, 0 replies; 13+ messages in thread
From: Jonathan Cameron @ 2019-07-31  8:37 UTC (permalink / raw)
  To: Jacopo Mondi
  Cc: Joe Perches, Jonathan Cameron, Jacopo Mondi, Andrew Morton,
	linux-kernel, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, linux-iio

On Mon, 29 Jul 2019 23:52:14 +0200
Jacopo Mondi <jacopo@jmondi.org> wrote:

> Hello,
>   so I finally run some test and...
> 
> On Sun, Jul 14, 2019 at 05:19:32AM -0700, Joe Perches wrote:
> > On Sun, 2019-07-14 at 12:54 +0100, Jonathan Cameron wrote:  
> > > On Tue,  9 Jul 2019 22:04:17 -0700
> > > Joe Perches <joe@perches.com> wrote:
> > >  
> > > > Arguments are supposed to be ordered high then low.
> > > >
> > > > Signed-off-by: Joe Perches <joe@perches.com>  
> > >
> > > Applied to the fixes-togreg branch of iio.git and marked for
> > > stable etc.  
> 
> I don't see it in v5.3-rc2, has it been collected or are we still in
> time for an additional fix?
> 
> >
> > This mask is used in an init function called from a probe.
> >
> > I don't have this hardware but it looks as if it could
> > never have worked so I doubt the driver and the hardware
> > have ever been tested.
> >
> > Does anyone have this device in actual use?  
> 
> Because it turns out this is 2 times embarrassing. The mask definition
> is indeed wrong, as Joe reported and fixed, but also this line
> >
> > 	regval = ret & MAX9611_TEMP_MASK;  
> 
> is very wrong as regval is read as:
>         ret = max9611_read_single(max9611, CONF_TEMP, &regval);
> 
> So that should actually be:
>         regval &= MAX9611_TEMP_MASK;
> not
>  	regval = ret & MAX9611_TEMP_MASK;
> Ups...
> 
> Yes, it worked by chance, as regval was always 0, which is in the
> range of acceptable temperatures :/
> 
> >
> > 	if ((regval > MAX9611_TEMP_MAX_POS &&
> > 	     regval < MAX9611_TEMP_MIN_NEG) ||
> > 	     regval > MAX9611_TEMP_MAX_NEG) {  
> 
> Also reading this condition and how I had defined the temperature
> calculation formula makes me wonder if this it totally correct, but
> for the moment:
> 
> 1) if Joe's patch has been collected, I can send an additional patch to
> fix how regval is computed.
> 2) If Joe's patch still have to be collected, the regval computation
> might be fixed there.

I think this will have hit linux-next on the same day as your email.

https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/commit/drivers/iio/adc?id=ae8cc91a7d85e018c0c267f580820b2bb558cd48

So follow up patch please.

Thanks!

Jonathan
> 
> Sorry for taking so long to get back to you and thanks for noticing.
> 
> Thanks
>   j
> 
> > 		dev_err(max9611->dev,
> > 			"Invalid value received from ADC 0x%4x: aborting\n",
> > 			regval);
> > 		return -EIO;
> > 	}
> >
> >  
> > > Thanks,
> > >
> > > Jonathan
> > >  
> > > > ---
> > > >  drivers/iio/adc/max9611.c | 2 +-
> > > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > > >
> > > > diff --git a/drivers/iio/adc/max9611.c b/drivers/iio/adc/max9611.c
> > > > index 917223d5ff5b..0e3c6529fc4c 100644
> > > > --- a/drivers/iio/adc/max9611.c
> > > > +++ b/drivers/iio/adc/max9611.c
> > > > @@ -83,7 +83,7 @@
> > > >  #define MAX9611_TEMP_MAX_POS		0x7f80
> > > >  #define MAX9611_TEMP_MAX_NEG		0xff80
> > > >  #define MAX9611_TEMP_MIN_NEG		0xd980
> > > > -#define MAX9611_TEMP_MASK		GENMASK(7, 15)
> > > > +#define MAX9611_TEMP_MASK		GENMASK(15, 7)
> > > >  #define MAX9611_TEMP_SHIFT		0x07
> > > >  #define MAX9611_TEMP_RAW(_r)		((_r) >> MAX9611_TEMP_SHIFT)
> > > >  #define MAX9611_TEMP_SCALE_NUM		1000000  
> >  
> 



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

end of thread, other threads:[~2019-07-31  8:37 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CGME20190710050444epcas1p250f7aa0f8798a7757df51d66f5970c2a@epcas1p2.samsung.com>
2019-07-10  5:04 ` [PATCH 00/12] treewide: Fix GENMASK misuses Joe Perches
2019-07-10  5:04   ` [PATCH 04/12] iio: adc: max9611: Fix misuse of GENMASK macro Joe Perches
2019-07-14 11:54     ` Jonathan Cameron
2019-07-14 12:19       ` Joe Perches
2019-07-14 14:37         ` Jacopo Mondi
2019-07-29 21:52         ` Jacopo Mondi
2019-07-31  8:37           ` Jonathan Cameron
2019-07-10  9:17   ` [PATCH 00/12] treewide: Fix GENMASK misuses Johannes Berg
2019-07-10  9:43     ` Russell King - ARM Linux admin
2019-07-10 15:45       ` Joe Perches
2019-07-10 16:01         ` Joe Perches
2019-07-11 21:30   ` David Miller
2019-07-12 12:54   ` Andrzej Hajda

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