2020-02-20 17:38:12

by Jesse Brandeburg

[permalink] [raw]
Subject: [PATCH v2 1/2] x86: fix bitops.h warning with a moved cast

Fix many sparse warnings when building with C=1.

When the kernel is compiled with C=1, there are lots of messages like:
arch/x86/include/asm/bitops.h:77:37: warning: cast truncates bits from constant value (ffffff7f becomes 7f)

CONST_MASK() is using a signed integer "1" to create the mask which
is later cast to (u8) when used. Move the cast to the definition so
and use local variables so sparse and the compiler can see the
correct type.

Verified with a test module (see next patch) and assembly inspection
that the patch doesn't introduce any change in generated code.

Signed-off-by: Jesse Brandeburg <[email protected]>
---
v2: use correct CC: list
---
arch/x86/include/asm/bitops.h | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/arch/x86/include/asm/bitops.h b/arch/x86/include/asm/bitops.h
index 062cdecb2f24..2922b352b6ed 100644
--- a/arch/x86/include/asm/bitops.h
+++ b/arch/x86/include/asm/bitops.h
@@ -46,15 +46,17 @@
* a mask operation on a byte.
*/
#define CONST_MASK_ADDR(nr, addr) WBYTE_ADDR((void *)(addr) + ((nr)>>3))
-#define CONST_MASK(nr) (1 << ((nr) & 7))
+#define CONST_MASK(nr) ((u8)1 << ((nr) & 7))

static __always_inline void
arch_set_bit(long nr, volatile unsigned long *addr)
{
if (__builtin_constant_p(nr)) {
+ u8 cmask = CONST_MASK(nr);
+
asm volatile(LOCK_PREFIX "orb %1,%0"
: CONST_MASK_ADDR(nr, addr)
- : "iq" ((u8)CONST_MASK(nr))
+ : "iq" (cmask)
: "memory");
} else {
asm volatile(LOCK_PREFIX __ASM_SIZE(bts) " %1,%0"
@@ -72,9 +74,11 @@ static __always_inline void
arch_clear_bit(long nr, volatile unsigned long *addr)
{
if (__builtin_constant_p(nr)) {
+ u8 cmaski = ~CONST_MASK(nr);
+
asm volatile(LOCK_PREFIX "andb %1,%0"
: CONST_MASK_ADDR(nr, addr)
- : "iq" ((u8)~CONST_MASK(nr)));
+ : "iq" (cmaski));
} else {
asm volatile(LOCK_PREFIX __ASM_SIZE(btr) " %1,%0"
: : RLONG_ADDR(addr), "Ir" (nr) : "memory");
--
2.24.1


2020-02-20 18:13:15

by Peter Zijlstra

[permalink] [raw]
Subject: Re: [PATCH v2 1/2] x86: fix bitops.h warning with a moved cast

On Thu, Feb 20, 2020 at 09:37:21AM -0800, Jesse Brandeburg wrote:
> Fix many sparse warnings when building with C=1.
>
> When the kernel is compiled with C=1, there are lots of messages like:
> arch/x86/include/asm/bitops.h:77:37: warning: cast truncates bits from constant value (ffffff7f becomes 7f)
>

> @@ -72,9 +74,11 @@ static __always_inline void
> arch_clear_bit(long nr, volatile unsigned long *addr)
> {
> if (__builtin_constant_p(nr)) {
> + u8 cmaski = ~CONST_MASK(nr);
> +
> asm volatile(LOCK_PREFIX "andb %1,%0"
> : CONST_MASK_ADDR(nr, addr)
> - : "iq" ((u8)~CONST_MASK(nr)));
> + : "iq" (cmaski));
> } else {
> asm volatile(LOCK_PREFIX __ASM_SIZE(btr) " %1,%0"
> : : RLONG_ADDR(addr), "Ir" (nr) : "memory");

Urgh, that's sad. So why doesn't this still generate a warning, ~ should
promote your u8 to int, and then you down-cast to u8 on assignment
again.

So now you have more lines, more ugly casts and exactly the same
generated code; where the win?

Perhaps you should write it like:

: "iq" (0xFF ^ CONST_MASK(nr))

hmm?

2020-02-20 22:32:50

by Jesse Brandeburg

[permalink] [raw]
Subject: Re: [PATCH v2 1/2] x86: fix bitops.h warning with a moved cast

On Thu, 20 Feb 2020 19:12:36 +0100 Peter wrote:
> On Thu, Feb 20, 2020 at 09:37:21AM -0800, Jesse Brandeburg wrote:
> > @@ -72,9 +74,11 @@ static __always_inline void
> > arch_clear_bit(long nr, volatile unsigned long *addr)
> > {
> > if (__builtin_constant_p(nr)) {
> > + u8 cmaski = ~CONST_MASK(nr);
> > +
> > asm volatile(LOCK_PREFIX "andb %1,%0"
> > : CONST_MASK_ADDR(nr, addr)
> > - : "iq" ((u8)~CONST_MASK(nr)));
> > + : "iq" (cmaski));
>
> Urgh, that's sad. So why doesn't this still generate a warning, ~ should
> promote your u8 to int, and then you down-cast to u8 on assignment
> again.

My suspicion is that using the right size types on the lvalue causes
the compiler (and sparse) to know that the type and number of bits in
the "iq" statement is unambiguous.

>
> So now you have more lines, more ugly casts and exactly the same
> generated code; where the win?

The win as I see it is that sparse (C=1) doesn't warn, but you're right,
it wasn't my first choice to do it the way I ended up with (see below)

> Perhaps you should write it like:
>
> : "iq" (0xFF ^ CONST_MASK(nr))
>
> hmm?

Thanks! That works, for my tests at least. FWIW, at one point during
review I got some feedback from a build bot (zero day tester) that
certain compilers like gcc 7.5.0 interpret ~CONST_MASK(nr) into needing
32 bits. So I solved that issue by using correctly typed (and width)
local variables, but I didn't try the 0xff^ way at that time.

I'll change back to the simpler version of the changes (without locals)
and with your 0xff^ change, we'll see if anything comes up from build
bots.