diff mbox series

[08/10] mfd: atmel-smc: Silence comparison of unsigned expression < 0 is always false warning (W=1)

Message ID 20200625064619.2775707-9-lee.jones@linaro.org
State New
Headers show
Series [01/10] mfd: wm8350-core: Supply description wm8350_reg_{un}lock args | expand

Commit Message

Lee Jones June 25, 2020, 6:46 a.m. UTC
GENMASK and it's callees conduct checking to ensure the passed
parameters are valid.  One of those checks is for '< 0'.  So if an
unsigned value is passed, in an invalid comparison takes place.

Judging from the current code, it looks as though 'unsigned int'
is the correct type to use, so simply cast these small values
with no chance of being false negative to signed int for
comparison/error checking purposes.

Squashes the following W=1 warnings:

 In file included from /home/lee/projects/linux/kernel/include/linux/bits.h:23,
 from /home/lee/projects/linux/kernel/include/linux/bitops.h:5,
 from /home/lee/projects/linux/kernel/include/linux/kernel.h:12,
 from /home/lee/projects/linux/kernel/include/linux/mfd/syscon/atmel-smc.h:14,
 from /home/lee/projects/linux/kernel/drivers/mfd/atmel-smc.c:11:
 /home/lee/projects/linux/kernel/drivers/mfd/atmel-smc.c: In function ‘atmel_smc_cs_encode_ncycles’:
 /home/lee/projects/linux/kernel/include/linux/bits.h:26:28: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits]
 26 | __builtin_constant_p((l) > (h)), (l) > (h), 0)))
 | ^
 /home/lee/projects/linux/kernel/include/linux/build_bug.h:16:62: note: in definition of macro ‘BUILD_BUG_ON_ZERO’
 16 | #define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
 | ^
 /home/lee/projects/linux/kernel/include/linux/bits.h:39:3: note: in expansion of macro ‘GENMASK_INPUT_CHECK’
 39 | (GENMASK_INPUT_CHECK(h, l) + __GENMASK(h, l))
 | ^~~~~~~~~~~~~~~~~~~
 /home/lee/projects/linux/kernel/drivers/mfd/atmel-smc.c:49:25: note: in expansion of macro ‘GENMASK’
 49 | unsigned int lsbmask = GENMASK(msbpos - 1, 0);
 | ^~~~~~~
 /home/lee/projects/linux/kernel/include/linux/bits.h:26:40: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits]
 26 | __builtin_constant_p((l) > (h)), (l) > (h), 0)))
 | ^
 /home/lee/projects/linux/kernel/include/linux/build_bug.h:16:62: note: in definition of macro ‘BUILD_BUG_ON_ZERO’
 16 | #define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
 | ^
 /home/lee/projects/linux/kernel/include/linux/bits.h:39:3: note: in expansion of macro ‘GENMASK_INPUT_CHECK’
 39 | (GENMASK_INPUT_CHECK(h, l) + __GENMASK(h, l))
 | ^~~~~~~~~~~~~~~~~~~

Cc: <stable@vger.kernel.org>
Cc: Nicolas Ferre <nicolas.ferre@microchip.com>
Cc: Alexandre Belloni <alexandre.belloni@bootlin.com>
Cc: Ludovic Desroches <ludovic.desroches@microchip.com>
Cc: Boris Brezillon <boris.brezillon@free-electrons.com>
Signed-off-by: Lee Jones <lee.jones@linaro.org>

---
 drivers/mfd/atmel-smc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

-- 
2.25.1

Comments

Alexandre Belloni June 26, 2020, 8:56 p.m. UTC | #1
Hi,

On 25/06/2020 07:46:17+0100, Lee Jones wrote:
> GENMASK and it's callees conduct checking to ensure the passed

> parameters are valid.  One of those checks is for '< 0'.  So if an

> unsigned value is passed, in an invalid comparison takes place.

> 

> Judging from the current code, it looks as though 'unsigned int'

> is the correct type to use, so simply cast these small values

> with no chance of being false negative to signed int for

> comparison/error checking purposes.

> 


I've been thinking about that one but shouldn't the proper fix be in
GENMASK? My understanding is that this happens because l is 0 and I
don't think GENMASK would ever expect negative number. What about simply
checking that h != l when l is 0?

> Squashes the following W=1 warnings:

> 

>  In file included from /home/lee/projects/linux/kernel/include/linux/bits.h:23,

>  from /home/lee/projects/linux/kernel/include/linux/bitops.h:5,

>  from /home/lee/projects/linux/kernel/include/linux/kernel.h:12,

>  from /home/lee/projects/linux/kernel/include/linux/mfd/syscon/atmel-smc.h:14,

>  from /home/lee/projects/linux/kernel/drivers/mfd/atmel-smc.c:11:

>  /home/lee/projects/linux/kernel/drivers/mfd/atmel-smc.c: In function ‘atmel_smc_cs_encode_ncycles’:

>  /home/lee/projects/linux/kernel/include/linux/bits.h:26:28: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits]

>  26 | __builtin_constant_p((l) > (h)), (l) > (h), 0)))

>  | ^

>  /home/lee/projects/linux/kernel/include/linux/build_bug.h:16:62: note: in definition of macro ‘BUILD_BUG_ON_ZERO’

>  16 | #define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))

>  | ^

>  /home/lee/projects/linux/kernel/include/linux/bits.h:39:3: note: in expansion of macro ‘GENMASK_INPUT_CHECK’

>  39 | (GENMASK_INPUT_CHECK(h, l) + __GENMASK(h, l))

>  | ^~~~~~~~~~~~~~~~~~~

>  /home/lee/projects/linux/kernel/drivers/mfd/atmel-smc.c:49:25: note: in expansion of macro ‘GENMASK’

>  49 | unsigned int lsbmask = GENMASK(msbpos - 1, 0);

>  | ^~~~~~~

>  /home/lee/projects/linux/kernel/include/linux/bits.h:26:40: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits]

>  26 | __builtin_constant_p((l) > (h)), (l) > (h), 0)))

>  | ^

>  /home/lee/projects/linux/kernel/include/linux/build_bug.h:16:62: note: in definition of macro ‘BUILD_BUG_ON_ZERO’

>  16 | #define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))

>  | ^

>  /home/lee/projects/linux/kernel/include/linux/bits.h:39:3: note: in expansion of macro ‘GENMASK_INPUT_CHECK’

>  39 | (GENMASK_INPUT_CHECK(h, l) + __GENMASK(h, l))

>  | ^~~~~~~~~~~~~~~~~~~

> 

> Cc: <stable@vger.kernel.org>

> Cc: Nicolas Ferre <nicolas.ferre@microchip.com>

> Cc: Alexandre Belloni <alexandre.belloni@bootlin.com>

> Cc: Ludovic Desroches <ludovic.desroches@microchip.com>

> Cc: Boris Brezillon <boris.brezillon@free-electrons.com>

> Signed-off-by: Lee Jones <lee.jones@linaro.org>

> ---

>  drivers/mfd/atmel-smc.c | 4 ++--

>  1 file changed, 2 insertions(+), 2 deletions(-)

> 

> diff --git a/drivers/mfd/atmel-smc.c b/drivers/mfd/atmel-smc.c

> index 1fa2ec950e7df..17bbe9d1fa740 100644

> --- a/drivers/mfd/atmel-smc.c

> +++ b/drivers/mfd/atmel-smc.c

> @@ -46,8 +46,8 @@ static int atmel_smc_cs_encode_ncycles(unsigned int ncycles,

>  				       unsigned int msbfactor,

>  				       unsigned int *encodedval)

>  {

> -	unsigned int lsbmask = GENMASK(msbpos - 1, 0);

> -	unsigned int msbmask = GENMASK(msbwidth - 1, 0);

> +	unsigned int lsbmask = GENMASK((int)msbpos - 1, 0);

> +	unsigned int msbmask = GENMASK((int)msbwidth - 1, 0);

>  	unsigned int msb, lsb;

>  	int ret = 0;

>  

> -- 

> 2.25.1

> 


-- 
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
Lee Jones June 30, 2020, 12:10 p.m. UTC | #2
On Fri, 26 Jun 2020, Alexandre Belloni wrote:

> Hi,

> 

> On 25/06/2020 07:46:17+0100, Lee Jones wrote:

> > GENMASK and it's callees conduct checking to ensure the passed

> > parameters are valid.  One of those checks is for '< 0'.  So if an

> > unsigned value is passed, in an invalid comparison takes place.

> > 

> > Judging from the current code, it looks as though 'unsigned int'

> > is the correct type to use, so simply cast these small values

> > with no chance of being false negative to signed int for

> > comparison/error checking purposes.

> > 

> 

> I've been thinking about that one but shouldn't the proper fix be in

> GENMASK? My understanding is that this happens because l is 0 and I

> don't think GENMASK would ever expect negative number. What about simply

> checking that h != l when l is 0?


Looks like Rikard Falkeborn recently submitted a patch to 'fix' this
issue.  However, Linus slammed it, decrying that it's the warning
that's wrong, not the code.  Looks like the type-limits warning might
be getting downgraded to W=2 instead (although I don't see a patch
yet).

I'm going to drop this patch.

-- 
Lee Jones [李琼斯]
Senior Technical Lead - Developer Services
Linaro.org │ Open source software for Arm SoCs
Follow Linaro: Facebook | Twitter | Blog
diff mbox series

Patch

diff --git a/drivers/mfd/atmel-smc.c b/drivers/mfd/atmel-smc.c
index 1fa2ec950e7df..17bbe9d1fa740 100644
--- a/drivers/mfd/atmel-smc.c
+++ b/drivers/mfd/atmel-smc.c
@@ -46,8 +46,8 @@  static int atmel_smc_cs_encode_ncycles(unsigned int ncycles,
 				       unsigned int msbfactor,
 				       unsigned int *encodedval)
 {
-	unsigned int lsbmask = GENMASK(msbpos - 1, 0);
-	unsigned int msbmask = GENMASK(msbwidth - 1, 0);
+	unsigned int lsbmask = GENMASK((int)msbpos - 1, 0);
+	unsigned int msbmask = GENMASK((int)msbwidth - 1, 0);
 	unsigned int msb, lsb;
 	int ret = 0;