stmmac: avoid ipq806x constant overflow warning
Subsystems (4)
PATCH 1/1 stmmac: avoid ipq806x constant overflow warning
Building dwmac-ipq806x on a 64-bit architecture produces a harmless warning from gcc:
stmmac/dwmac-ipq806x.c: In function 'ipq806x_gmac_probe':include/linux/bitops.h:6:19: warning: overflow in implicit constant conversion [-Woverflow]
val = QSGMII_PHY_CDR_EN |stmmac/dwmac-ipq806x.c:333:8: note: in expansion of macro 'QSGMII_PHY_CDR_EN'
#define QSGMII_PHY_CDR_EN BIT(0)
#define BIT(nr) (1UL << (nr))
The compiler warns about the fact that a 64-bit literal is passed into a function that takes a 32-bit argument. I could not fully understand why it warns despite the fact that this number is always small enough to fit, but changing the use of BIT() macros into the equivalent hexadecimal representation avoids the warning
---
This came up on the arm64 allmodconfig build
drivers/net/ethernet/stmicro/stmmac/dwmac-ipq806x.c +5 -5
@@ -77,11 +77,11 @@/* Only GMAC1/2/3 support SGMII and their CTL register are not contiguous */ #define QSGMII_PHY_SGMII_CTL (x) ((x == 1) ? 0x134 : \ (0x13c + (4 * (x - 2)))) -#define QSGMII_PHY_CDR_EN BIT(0) -#define QSGMII_PHY_RX_FRONT_EN BIT(1) -#define QSGMII_PHY_RX_SIGNAL_DETECT_EN BIT(2) -#define QSGMII_PHY_TX_DRIVER_EN BIT(3) -#define QSGMII_PHY_QSGMII_EN BIT(7) +#define QSGMII_PHY_CDR_EN 0x01u +#define QSGMII_PHY_RX_FRONT_EN 0x02u +#define QSGMII_PHY_RX_SIGNAL_DETECT_EN 0x04u +#define QSGMII_PHY_TX_DRIVER_EN 0x08u +#define QSGMII_PHY_QSGMII_EN 0x80u#define QSGMII_PHY_PHASE_LOOP_GAIN_OFFSET 12#define QSGMII_PHY_PHASE_LOOP_GAIN_MASK 0x7#define QSGMII_PHY_RX_DC_BIAS_OFFSET 18
David Miller
maintainerFrom: Arnd Bergmann <[email protected]>
Date: Thu, 12 Nov 2015 15:12:48 +0100
Quoted 10 lines
Building dwmac-ipq806x on a 64-bit architecture produces a harmless warning from gcc:
stmmac/dwmac-ipq806x.c: In function 'ipq806x_gmac_probe':include/linux/bitops.h:6:19: warning: overflow in implicit constant conversion [-Woverflow]
val =QSGMII_PHY_CDR_EN|stmmac/dwmac-ipq806x.c:333:8: note: in expansion of macro 'QSGMII_PHY_CDR_EN'
#defineQSGMII_PHY_CDR_ENBIT(0)
#define BIT(nr) (1UL << (nr))
Quoted 8 lines
The compiler warns about the fact that a 64-bit literal is passed into a function that takes a 32-bit argument. I could not fully understand why it warns despite the fact that this number is always small enough to fit, but changing the use of
BIT()macros into the equivalent hexadecimal representation avoids the warningSigned-off-by Arnd Bergmann <[email protected]>Fixes b1c17215d718 ("stmmac: add ipq806x glue layer")
I've seen this warning too on x86_64 and had been meaning to look into it, thanks for taking the initiative. :)
Moving away from using BIT() is somewhat disappointing, because we want to encourage people to use these macros.
I think it's also easier from a driver author and auditing perspective, you can see that something is being defined as bit X and then check the documentation for the chip to see if bit X is correct or not.
With the hex values there is more mental work and room for... mistakes.
Also I don't even understand the compiler's behavior, it's warning about QSGMII_PHY_CDR_EN but if you define only that to "0x1u" it still warns about QSGMII_PHY_CDR_EN.
The warning goes away only if you change all 5 BIT() uses.
This makes me like the change even less, something foul is going on here and I'd rather figure out what that is than install this patch.
Thanks.
Quoted 19 lines · On Thursday 12 November 2015 12:25:28 David Miller wrote:
From: Arnd Bergmann <[email protected]>
Date: Thu, 12 Nov 2015 15:12:48 +0100Quoted 16 lines
Building dwmac-ipq806x on a 64-bit architecture produces a harmless warning from gcc:
stmmac/dwmac-ipq806x.c: In function 'ipq806x_gmac_probe':include/linux/bitops.h:6:19: warning: overflow in implicit constant conversion [-Woverflow]
val =QSGMII_PHY_CDR_EN|stmmac/dwmac-ipq806x.c:333:8: note: in expansion of macro 'QSGMII_PHY_CDR_EN'
#defineQSGMII_PHY_CDR_ENBIT(0)
#define BIT(nr) (1UL << (nr))The compiler warns about the fact that a 64-bit literal is passed into a function that takes a 32-bit argument. I could not fully understand why it warns despite the fact that this number is always small enough to fit, but changing the use of
BIT()macros into the equivalent hexadecimal representation avoids the warning
Quoted 8 lines
Quoted 2 lines
Signed-off-by Arnd Bergmann <[email protected]>Fixes b1c17215d718 ("stmmac: add ipq806x glue layer")I've seen this warning too on
x86_64and had been meaning to look into it, thanks for taking the initiative.Moving away from using
BIT()is somewhat disappointing, because we want to encourage people to use these macros.
Ok, I never really liked that macro, so I didn't mind removing it. ;-)
I spent too much time working at IBM where all internal documentation uses bit numbers that call the MSB bit 0, and drivers use randomly either the IBM notation or the one that everyone else uses, so I always found the hex numbers way more intuitive.
Quoted 5 lines
Also I don't even understand the compiler's behavior, it's warning about
QSGMII_PHY_CDR_ENbut if you define only that to "0x1u" it still warns aboutQSGMII_PHY_CDR_EN.The warning goes away only if you change all 5
BIT()uses.
Yes, I have spent the time to analyze the problem now and it all makes sense. A proper patch follows.
Arnd
PATCH v2 1/1 stmmac: avoid ipq806x constant overflow warning
Building dwmac-ipq806x on a 64-bit architecture produces a harmless warning from gcc:
stmmac/dwmac-ipq806x.c: In function 'ipq806x_gmac_probe':include/linux/bitops.h:6:19: warning: overflow in implicit constant conversion [-Woverflow]
val = QSGMII_PHY_CDR_EN |stmmac/dwmac-ipq806x.c:333:8: note: in expansion of macro 'QSGMII_PHY_CDR_EN'
#define QSGMII_PHY_CDR_EN BIT(0)
#define BIT(nr) (1UL << (nr))
This is a result of the type conversion rules in C, when we take the logical OR of multiple different types. In particular, we have and unsigned long
QSGMII_PHY_CDR_EN == BIT(0) == (1ul << 0) == 0x0000000000000001ul
and a signed int
0xC << QSGMII_PHY_TX_DRV_AMP_OFFSET == 0xc0000000
which together gives a signed long value
0xffffffffc0000001l
and when this is passed into a function that takes an unsigned int type, gcc warns about the signed overflow and the loss of the upper 32-bits that are all ones.
This patch adds 'ul' type modifiers to the literal numbers passed in here, so now the expression remains an 'unsigned long' with the upper bits all zero, and that avoids the signed overflow and the warning.
drivers/net/ethernet/stmicro/stmmac/dwmac-ipq806x.c +5 -5
@@ -337,11 +337,11 @@ static int ipq806x_gmac_probe(struct platform_device *pdev)QSGMII_PHY_RX_SIGNAL_DETECT_EN |QSGMII_PHY_TX_DRIVER_EN |QSGMII_PHY_QSGMII_EN | -0x4 <<QSGMII_PHY_PHASE_LOOP_GAIN_OFFSET | -0x3 <<QSGMII_PHY_RX_DC_BIAS_OFFSET | -0x1 <<QSGMII_PHY_RX_INPUT_EQU_OFFSET | -0x2 <<QSGMII_PHY_CDR_PI_SLEW_OFFSET | - 0xC << QSGMII_PHY_TX_DRV_AMP_OFFSET); +0x4ul <<QSGMII_PHY_PHASE_LOOP_GAIN_OFFSET | +0x3ul <<QSGMII_PHY_RX_DC_BIAS_OFFSET | +0x1ul <<QSGMII_PHY_RX_INPUT_EQU_OFFSET | +0x2ul <<QSGMII_PHY_CDR_PI_SLEW_OFFSET | + 0xCul << QSGMII_PHY_TX_DRV_AMP_OFFSET); } plat_dat->has_gmac = true;
From: Arnd Bergmann <[email protected]>
Date: Thu, 12 Nov 2015 22:03:40 +0100
Quoted 6 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-ipq806x.c +0
-2@@ -337,11 +337,11 @@ static int ipq806x_gmac_probe(struct platform_device *pdev)QSGMII_PHY_RX_SIGNAL_DETECT_EN |QSGMII_PHY_TX_DRIVER_EN | QSGMII_PHY_QSGMII_EN | -0x4 <<QSGMII_PHY_PHASE_LOOP_GAIN_OFFSET | - 0x3 << QSGMII_PHY_RX_DC_BIAS_OFFSET |
drivers/net/ethernet/stmicro/stmmac/dwmac-ipq806x.c static int ipq806x_gmac_probe(struct platform_device *pdev) · line 346
Quoted 10 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-ipq806x.c +5
-3-0x1 <<QSGMII_PHY_RX_INPUT_EQU_OFFSET | -0x2 <<QSGMII_PHY_CDR_PI_SLEW_OFFSET | - 0xC << QSGMII_PHY_TX_DRV_AMP_OFFSET); +0x4ul <<QSGMII_PHY_PHASE_LOOP_GAIN_OFFSET | +0x3ul <<QSGMII_PHY_RX_DC_BIAS_OFFSET | +0x1ul <<QSGMII_PHY_RX_INPUT_EQU_OFFSET | +0x2ul <<QSGMII_PHY_CDR_PI_SLEW_OFFSET | + 0xCul << QSGMII_PHY_TX_DRV_AMP_OFFSET); }
Indeed, this looks so much better.
Applied.
Thanks for looking more deeply into this!
Quoted 24 lines · Arnd Bergmann
Building dwmac-ipq806x on a 64-bit architecture produces a harmless warning from gcc:
stmmac/dwmac-ipq806x.c: In function 'ipq806x_gmac_probe':include/linux/bitops.h:6:19: warning: overflow in implicit constant conversion [-Woverflow]
val =QSGMII_PHY_CDR_EN|stmmac/dwmac-ipq806x.c:333:8: note: in expansion of macro 'QSGMII_PHY_CDR_EN'
#defineQSGMII_PHY_CDR_ENBIT(0)
#define BIT(nr) (1UL << (nr))This is a result of the type conversion rules in C, when we take the logical OR of multiple different types. In particular, we have and unsigned long
QSGMII_PHY_CDR_EN == BIT(0) == (1ul << 0) == 0x0000000000000001uland a signed int
0xC << QSGMII_PHY_TX_DRV_AMP_OFFSET == 0xc0000000which together gives a signed long value
0xffffffffc0000001l
Quoted 7 lines
and when this is passed into a function that takes an unsigned int type, gcc warns about the signed overflow and the loss of the upper 32-bits that are all ones.
This patch adds 'ul' type modifiers to the literal numbers passed in here, so now the expression remains an 'unsigned long' with the upper bits all zero, and that avoids the signed overflow and the warning.
FWIW, the 64-bitness of BIT() on 64-bit platforms is also causing subtle warnings in other places, e.g. when inverting them to create bit mask, cfr.
https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=a9efeca613a8fe5281d7c91f5c8c9ea46f2312f6
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert at linux-m68k.org
In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds
Quoted 42 lines · Geert Uytterhoeven
Quoted 34 lines · Arnd Bergmann
Building dwmac-ipq806x on a 64-bit architecture produces a harmless warning from gcc:
stmmac/dwmac-ipq806x.c: In function 'ipq806x_gmac_probe':include/linux/bitops.h:6:19: warning: overflow in implicit constant conversion [-Woverflow]
val =QSGMII_PHY_CDR_EN|stmmac/dwmac-ipq806x.c:333:8: note: in expansion of macro 'QSGMII_PHY_CDR_EN'
#defineQSGMII_PHY_CDR_ENBIT(0)
#define BIT(nr) (1UL << (nr))This is a result of the type conversion rules in C, when we take the
logical OR of multiple different types. In particular, we have and unsigned longQSGMII_PHY_CDR_EN == BIT(0) == (1ul << 0) ==
0x0000000000000001uland a signed int
0xC << QSGMII_PHY_TX_DRV_AMP_OFFSET == 0xc0000000which together gives a signed long value
0xffffffffc0000001land when this is passed into a function that takes an unsigned int type,
gcc warns about the signed overflow and the loss of the upper 32 -bits that
are all ones.Quoted 5 lines
This patch adds 'ul' type modifiers to the literal numbers passed in
here, so now the expression remains an 'unsigned long' with the upper
bits all zero, and that avoids the signed overflow and the warning.
Quoted 8 lines
FWIW, the 64-bitness of
BIT()on 64-bit platforms is also causing subtle
warnings in other places, e.g. when inverting them to create bit mask, cfr.
https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commi
t/?id=a9efeca613a8fe5281d7c91f5c8c9ea46f2312f6Gr{oetje,eeting}s,
I still think specific length BIT macros
can be useful.
Hi Joe,
Quoted 50 lines · Joe Perches
Quoted 42 lines · Geert Uytterhoeven
Quoted 34 lines · Arnd Bergmann
Building dwmac-ipq806x on a 64-bit architecture produces a harmless warning from gcc:
stmmac/dwmac-ipq806x.c: In function 'ipq806x_gmac_probe':include/linux/bitops.h:6:19: warning: overflow in implicit constant conversion [-Woverflow]
val =QSGMII_PHY_CDR_EN|stmmac/dwmac-ipq806x.c:333:8: note: in expansion of macro 'QSGMII_PHY_CDR_EN'
#defineQSGMII_PHY_CDR_ENBIT(0)
#define BIT(nr) (1UL << (nr))This is a result of the type conversion rules in C, when we take the
logical OR of multiple different types. In particular, we have and unsigned longQSGMII_PHY_CDR_EN == BIT(0) == (1ul << 0) ==
0x0000000000000001uland a signed int
0xC << QSGMII_PHY_TX_DRV_AMP_OFFSET == 0xc0000000which together gives a signed long value
0xffffffffc0000001land when this is passed into a function that takes an unsigned int type,
gcc warns about the signed overflow and the loss of the upper 32 -bits that
are all ones.Quoted 5 lines
This patch adds 'ul' type modifiers to the literal numbers passed in
here, so now the expression remains an 'unsigned long' with the upper
bits all zero, and that avoids the signed overflow and the warning.Quoted 6 lines
FWIW, the 64-bitness of
BIT()on 64-bit platforms is also causing subtle
warnings in other places, e.g. when inverting them to create bit mask, cfr.
https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commi
t/?id=a9efeca613a8fe5281d7c91f5c8c9ea46f2312f6
Quoted 4 lines
I still think specific length BIT macros
can be useful.
Yeah!
I only recently started liking the BIT() macro (before I preferred hex, too). Perhaps because Renesas datasheets use bit numbers all over the place ;-)
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert at linux-m68k.org
In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds