Skip to contentSkip to conversation

x86: bitops: fix build regression

PATCH Superseded Nick Desaulniers 10 participants 45 messages May 5, 2020 – May 14, 2020

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: bitops: fix build regression

From: Sedat Dilek <[email protected]>

It turns out that if your config tickles __builtin_constant_p via differences in choices to inline or not, this now produces invalid assembly:

$ cat foo.c
long a(long b, long c) {
  asm("orb\t%1, %0" : "+q"(c): "r"(b));
  return c;
}
$ gcc foo.c
foo.c: Assembler messages:
foo.c:2: Error: `%rax' not allowed with `orb'

The "q" constraint only has meanting on -m32 otherwise is treated as "r".

This is easily reproducible via Clang+CONFIG_STAGING=y+CONFIG_VT6656=m, or Clang+allyesconfig.

Keep the masking operation to appease sparse (make C=1), add back the cast in order to properly select the proper 8b register alias.

[Nick: reworded]

---
 arch/x86/include/asm/bitops.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/include/asm/bitops.h +2 -2
index b392571c1f1d..139122e5b25b 100644
@@ -54,7 +54,7 @@ arch_set_bit(long nr, volatile unsigned long *addr)
 	if (__builtin_constant_p(nr)) {
 		asm volatile(LOCK_PREFIX "orb %1,%0"
 			: CONST_MASK_ADDR(nr, addr)
-			: "iq" (CONST_MASK(nr) & 0xff)
+			: "iq" ((u8)(CONST_MASK(nr) & 0xff))
 			: "memory");
 	} else {
 		asm volatile(LOCK_PREFIX __ASM_SIZE(bts) " %1,%0"
@@ -74,7 +74,7 @@ arch_clear_bit(long nr, volatile unsigned long *addr)
 	if (__builtin_constant_p(nr)) {
 		asm volatile(LOCK_PREFIX "andb %1,%0"
 			: CONST_MASK_ADDR(nr, addr)
-			: "iq" (CONST_MASK(nr) ^ 0xff));
+			: "iq" ((u8)(CONST_MASK(nr) ^ 0xff)));
 	} else {
 		asm volatile(LOCK_PREFIX __ASM_SIZE(btr) " %1,%0"
 			: : RLONG_ADDR(addr), "Ir" (nr) : "memory");
Signature · 1 line
2.26.2.526.g744177e7f7-goog
#1 lore raw
Quoted 57 lines · Nick Desaulniers

From: Sedat Dilek <[email protected]>

It turns out that if your config tickles __builtin_constant_p via differences in choices to inline or not, this now produces invalid assembly:

$ cat foo.c
long a(long b, long c) {
 asm("orb\t%1, %0" : "+q"(c): "r"(b));
 return c;
}
$ gcc foo.c
foo.c: Assembler messages:
foo.c:2: Error: `%rax' not allowed with `orb'

The "q" constraint only has meanting on -m32 otherwise is treated as "r".

This is easily reproducible via Clang+CONFIG_STAGING=y+CONFIG_VT6656=m, or Clang+allyesconfig.

Keep the masking operation to appease sparse (make C=1), add back the cast in order to properly select the proper 8b register alias.

[Nick: reworded]

---
arch/x86/include/asm/bitops.h | 4 ++--

1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/include/asm/bitops.h +1 -1
index b392571c1f1d..139122e5b25b 100644
@@ -54,7 +54,7 @@ arch_set_bit(long nr, volatile unsigned long *addr)
 	if (__builtin_constant_p(nr)) {
 		asm volatile(LOCK_PREFIX "orb %1,%0"
 			: CONST_MASK_ADDR(nr, addr)
-			: "iq" (CONST_MASK(nr) & 0xff)
+			: "iq" ((u8)(CONST_MASK(nr) & 0xff))
 			: "memory");
 	} else {
 		asm volatile(LOCK_PREFIX __ASM_SIZE(bts) " %1,%0"

arch/x86/include/asm/bitops.h arch_clear_bit(long nr, volatile unsigned long *addr) · line 80

Quoted 8 lines
arch/x86/include/asm/bitops.h +1 -1
@@ -74,7 +74,7 @@ arch_clear_bit(long nr, volatile unsigned long *addr)
 	if (__builtin_constant_p(nr)) {
 		asm volatile(LOCK_PREFIX "andb %1,%0"
 			: CONST_MASK_ADDR(nr, addr)
-			: "iq" (CONST_MASK(nr) ^ 0xff));
+			: "iq" ((u8)(CONST_MASK(nr) ^ 0xff)));
 	} else {
 		asm volatile(LOCK_PREFIX __ASM_SIZE(btr) " %1,%0"
 			: : RLONG_ADDR(addr), "Ir" (nr) : "memory");

Drop & 0xff and change ^ 0xff to ~.

The redundancy is confusing.

Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.
#2 lore raw
Quoted 70 lines · On Tue, May 5, 2020 at 11:07 AM <[email protected]> wrote:
Quoted 57 lines · Nick Desaulniers

From: Sedat Dilek <[email protected]>

It turns out that if your config tickles __builtin_constant_p via differences in choices to inline or not, this now produces invalid assembly:

$ cat foo.c
long a(long b, long c) {
 asm("orb\t%1, %0" : "+q"(c): "r"(b));
 return c;
}
$ gcc foo.c
foo.c: Assembler messages:
foo.c:2: Error: `%rax' not allowed with `orb'

The "q" constraint only has meanting on -m32 otherwise is treated as "r".

This is easily reproducible via Clang+CONFIG_STAGING=y+CONFIG_VT6656=m, or Clang+allyesconfig.

Keep the masking operation to appease sparse (make C=1), add back the cast in order to properly select the proper 8b register alias.

[Nick: reworded]

---
arch/x86/include/asm/bitops.h | 4 ++--

1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/include/asm/bitops.h +1 -1
index b392571c1f1d..139122e5b25b 100644
@@ -54,7 +54,7 @@ arch_set_bit(long nr, volatile unsigned long *addr)
      if (__builtin_constant_p(nr)) {
              asm volatile(LOCK_PREFIX "orb %1,%0"
                      : CONST_MASK_ADDR(nr, addr)
-                      : "iq" (CONST_MASK(nr) & 0xff)
+                      : "iq" ((u8)(CONST_MASK(nr) & 0xff))
                      : "memory");
      } else {
              asm volatile(LOCK_PREFIX __ASM_SIZE(bts) " %1,%0"

arch/x86/include/asm/bitops.h arch_clear_bit(long nr, volatile unsigned long *addr) · line 80

Quoted 8 lines
arch/x86/include/asm/bitops.h +1 -1
@@ -74,7 +74,7 @@ arch_clear_bit(long nr, volatile unsigned long *addr)
      if (__builtin_constant_p(nr)) {
              asm volatile(LOCK_PREFIX "andb %1,%0"
                      : CONST_MASK_ADDR(nr, addr)
-                      : "iq" (CONST_MASK(nr) ^ 0xff));
+                      : "iq" ((u8)(CONST_MASK(nr) ^ 0xff)));
      } else {
              asm volatile(LOCK_PREFIX __ASM_SIZE(btr) " %1,%0"
                      : : RLONG_ADDR(addr), "Ir" (nr) : "memory");

Drop & 0xff and change ^ 0xff to ~.

Quoted 1 line

The redundancy is confusing.

Thanks for the review. While I would also like to have less redundancy, we have ourselves a catch-22 that that won't resolve.

Without the cast to u8, gcc and clang will not select low-8-bit registers required for the b suffix on orb and andb, which results in an assembler error.
Without the mask, sparse will warn about the upper bytes of the value being truncated.
(I guess that would have been a more concise commit message).

Signature · 2 lines
Thanks,
~Nick Desaulniers
#3 lore raw
In reply to [email protected]
Quoted 14 lines · On Tue, May 05, 2020 at 11:07:24AM -0700, [email protected] wrote:
Quoted 12 lines · Nick Desaulniers
arch/x86/include/asm/bitops.h +1 -1
@@ -54,7 +54,7 @@ arch_set_bit(long nr, volatile unsigned long *addr)
 	if (__builtin_constant_p(nr)) {
 		asm volatile(LOCK_PREFIX "orb %1,%0"
 			: CONST_MASK_ADDR(nr, addr)
-			: "iq" (CONST_MASK(nr) & 0xff)
+			: "iq" ((u8)(CONST_MASK(nr) & 0xff))
 			: "memory");
 	} else {
 		asm volatile(LOCK_PREFIX __ASM_SIZE(bts) " %1,%0"
@@ -74,7 +74,7 @@ arch_clear_bit(long nr, volatile unsigned long *addr)
 	if (__builtin_constant_p(nr)) {
 		asm volatile(LOCK_PREFIX "andb %1,%0"
Quoted 8 lines

arch/x86/include/asm/bitops.h arch_clear_bit(long nr, volatile unsigned long *addr) · line 80

Quoted 6 lines
arch/x86/include/asm/bitops.h +1 -1
 			: CONST_MASK_ADDR(nr, addr)
-			: "iq" (CONST_MASK(nr) ^ 0xff));
+			: "iq" ((u8)(CONST_MASK(nr) ^ 0xff)));
 	} else {
 		asm volatile(LOCK_PREFIX __ASM_SIZE(btr) " %1,%0"
 			: : RLONG_ADDR(addr), "Ir" (nr) : "memory");

Drop & 0xff and change ^ 0xff to ~.

But then we're back to sparse being unhappy, no? The thing with ~ is that it will set high bits which will be truncated, which makes sparse sad.

#4 lore raw
Quoted 25 lines · Peter Zijlstra
Quoted 14 lines · On Tue, May 05, 2020 at 11:07:24AM -0700, [email protected] wrote:
Quoted 12 lines · Nick Desaulniers
arch/x86/include/asm/bitops.h +1 -1
@@ -54,7 +54,7 @@ arch_set_bit(long nr, volatile unsigned long *addr)
    if (__builtin_constant_p(nr)) {
            asm volatile(LOCK_PREFIX "orb %1,%0"
                    : CONST_MASK_ADDR(nr, addr)
-                    : "iq" (CONST_MASK(nr) & 0xff)
+                    : "iq" ((u8)(CONST_MASK(nr) & 0xff))
                    : "memory");
    } else {
            asm volatile(LOCK_PREFIX __ASM_SIZE(bts) " %1,%0"
@@ -74,7 +74,7 @@ arch_clear_bit(long nr, volatile unsigned long *addr)
    if (__builtin_constant_p(nr)) {
            asm volatile(LOCK_PREFIX "andb %1,%0"
Quoted 8 lines

arch/x86/include/asm/bitops.h arch_clear_bit(long nr, volatile unsigned long *addr) · line 80

Quoted 6 lines
arch/x86/include/asm/bitops.h +1 -1
                    : CONST_MASK_ADDR(nr, addr)
-                    : "iq" (CONST_MASK(nr) ^ 0xff));
+                    : "iq" ((u8)(CONST_MASK(nr) ^ 0xff)));
    } else {
            asm volatile(LOCK_PREFIX __ASM_SIZE(btr) " %1,%0"
                    : : RLONG_ADDR(addr), "Ir" (nr) : "memory");

Drop & 0xff and change ^ 0xff to ~.

Quoted 3 lines

But then we're back to sparse being unhappy, no? The thing with ~ is that it will set high bits which will be truncated, which makes sparse sad.

This change will make sparse happy and allow these cleanups:
#define CONST_MASK(nr) ((u8)1 << ((nr) & 7))

Tested with GCC 9.3.1.

--
Brian Gerst

#5 lore raw
Quoted 3 lines · Brian Gerst

This change will make sparse happy and allow these cleanups:
#define CONST_MASK(nr) ((u8)1 << ((nr) & 7))

yep, this is more elegant, IMO. Will send a v3 later with this change. Looking at the uses of CONST_MASK, I noticed arch_change_bit() currently has the (u8) cast from commit 838e8bb71dc0c ("x86: Implement change_bit with immediate operand as "lock xorb""), so that instance can get cleaned up with the above suggestion.

Signature · 2 lines
Thanks,
~Nick Desaulniers
#6 lore raw
Quoted 6 lines · Nick Desaulniers
Quoted 3 lines · Brian Gerst

This change will make sparse happy and allow these cleanups:
#define CONST_MASK(nr) ((u8)1 << ((nr) & 7))

Quoted 6 lines

yep, this is more elegant, IMO. Will send a v3 later with this change. Looking at the uses of CONST_MASK, I noticed arch_change_bit() currently has the (u8) cast from commit 838e8bb71dc0c ("x86: Implement change_bit with immediate operand as "lock xorb""), so that instance can get cleaned up with the above suggestion.

Oh, we need the cast to be the final operation. The binary AND and XOR in 2 of the 3 uses of CONST_MASK implicitly promote the operands of the binary operand to int, so the type of the evaluated subexpression is int.
https://wiki.sei.cmu.edu/confluence/display/c/EXP14-C.+Beware+of+integer+promotion+when+performing+bitwise+operations+on+integer+types+smaller+than+int
So I think this version (v2) is most precise fix, and would be better than defining more macros or (worse) using metaprogramming.

Signature · 2 lines
Thanks,
~Nick Desaulniers
#7 lore raw
Quoted 16 lines · Nick Desaulniers
Quoted 6 lines · Nick Desaulniers
Quoted 3 lines · Brian Gerst

This change will make sparse happy and allow these cleanups:
#define CONST_MASK(nr) ((u8)1 << ((nr) & 7))

Quoted 6 lines

yep, this is more elegant, IMO. Will send a v3 later with this change. Looking at the uses of CONST_MASK, I noticed arch_change_bit() currently has the (u8) cast from commit 838e8bb71dc0c ("x86: Implement change_bit with immediate operand as "lock xorb""), so that instance can get cleaned up with the above suggestion.

Quoted 7 lines

Oh, we need the cast to be the final operation. The binary AND and XOR in 2 of the 3 uses of CONST_MASK implicitly promote the operands of the binary operand to int, so the type of the evaluated subexpression is int.
https://wiki.sei.cmu.edu/confluence/display/c/EXP14-C.+Beware+of+integer+promotion+when+performing+bitwise+operations+on+integer+types+smaller+than+int
So I think this version (v2) is most precise fix, and would be better than defining more macros or (worse) using metaprogramming.

One last suggestion. Add the "b" modifier to the mask operand: "orb %b1, %0". That forces the compiler to use the 8-bit register name instead of trying to deduce the width from the input.

--
Brian Gerst

#8 lore raw
Quoted 26 lines · Brian Gerst
Quoted 16 lines · Nick Desaulniers
Quoted 6 lines · Nick Desaulniers
Quoted 3 lines · Brian Gerst

This change will make sparse happy and allow these cleanups:
#define CONST_MASK(nr) ((u8)1 << ((nr) & 7))

Quoted 6 lines

yep, this is more elegant, IMO. Will send a v3 later with this change. Looking at the uses of CONST_MASK, I noticed arch_change_bit() currently has the (u8) cast from commit 838e8bb71dc0c ("x86: Implement change_bit with immediate operand as "lock xorb""), so that instance can get cleaned up with the above suggestion.

Quoted 7 lines

Oh, we need the cast to be the final operation. The binary AND and XOR in 2 of the 3 uses of CONST_MASK implicitly promote the operands of the binary operand to int, so the type of the evaluated subexpression is int.
https://wiki.sei.cmu.edu/confluence/display/c/EXP14-C.+Beware+of+integer+promotion+when+performing+bitwise+operations+on+integer+types+smaller+than+int
So I think this version (v2) is most precise fix, and would be better than defining more macros or (worse) using metaprogramming.

Quoted 3 lines

One last suggestion. Add the "b" modifier to the mask operand: "orb %b1, %0". That forces the compiler to use the 8-bit register name instead of trying to deduce the width from the input.

Ah right: https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#x86Operandmodifiers

Looks like that works for both compilers. In that case, we can likely drop the & 0xff, too. Let me play with that, then I'll hopefully send a v3 today.

Signature · 2 lines
Thanks,
~Nick Desaulniers
#9 lore raw
Quoted 11 lines · Nick Desaulniers
Quoted 4 lines

One last suggestion. Add the "b" modifier to the mask operand: "orb %b1, %0". That forces the compiler to use the 8-bit register name instead of trying to deduce the width from the input.

Ah right: https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#x86Operandmodifiers

Looks like that works for both compilers. In that case, we can likely drop the & 0xff, too. Let me play with that, then I'll hopefully send a v3 today.

Good idea. I requested a while ago that they document these modifiers; they chose not to document them all which in some ways is good; it shows what they are willing to commit to indefinitely.

-hpa

#10 lore raw

PATCH v3 1/1 x86: bitops: fix build regression

This is easily reproducible via CC=clang+CONFIG_STAGING=y+CONFIG_VT6656=m.

It turns out that if your config tickles __builtin_constant_p via differences in choices to inline or not, these statements produce invalid assembly:

$ cat foo.c
long a(long b, long c) {
  asm("orb\t%1, %0" : "+q"(c): "r"(b));
  return c;
}
$ gcc foo.c
foo.c: Assembler messages:
foo.c:2: Error: `%rax' not allowed with `orb'

Use the %b "x86 Operand Modifier" to instead force register allocation to select a lower-8-bit GPR operand.

The "q" constraint only has meaning on -m32 otherwise is treated as "r". Not all GPRs have low-8-bit aliases for -m32.

---
Changes V2 -> V3:
* use %b "x86 Operand Modifier" instead of bitwise op then cast.
* reword commit message.
* add Brian and HPA suggested by tags
* drop Nathan & Sedat Tested by/reviewed by tags (new patch is different enough).

Changes V1 -> V2:
* change authorship/signed-off-by to Ilie
* add Nathan's Tested by/reviewed by
* update commit message slightly with info sent to HPA.

 arch/x86/include/asm/bitops.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/x86/include/asm/bitops.h +4 -4
index b392571c1f1d..03e24286e4eb 100644
@@ -52,9 +52,9 @@ static __always_inline void
 arch_set_bit(long nr, volatile unsigned long *addr)
 {
 	if (__builtin_constant_p(nr)) {
-		asm volatile(LOCK_PREFIX "orb %1,%0"
+		asm volatile(LOCK_PREFIX "orb %b1,%0"
 			: CONST_MASK_ADDR(nr, addr)
-			: "iq" (CONST_MASK(nr) & 0xff)
+			: "iq" (CONST_MASK(nr))
 			: "memory");
 	} else {
 		asm volatile(LOCK_PREFIX __ASM_SIZE(bts) " %1,%0"
@@ -72,9 +72,9 @@ static __always_inline void
 arch_clear_bit(long nr, volatile unsigned long *addr)
 {
 	if (__builtin_constant_p(nr)) {
-		asm volatile(LOCK_PREFIX "andb %1,%0"
+		asm volatile(LOCK_PREFIX "andb %b1,%0"
 			: CONST_MASK_ADDR(nr, addr)
-			: "iq" (CONST_MASK(nr) ^ 0xff));
+			: "iq" (~CONST_MASK(nr)));
 	} else {
 		asm volatile(LOCK_PREFIX __ASM_SIZE(btr) " %1,%0"
 			: : RLONG_ADDR(addr), "Ir" (nr) : "memory");
Signature · 1 line
2.26.2.645.ge9eca65c58-goog
#11 lore raw
Quoted 34 lines · Nick Desaulniers

This is easily reproducible via CC=clang+CONFIG_STAGING=y+CONFIG_VT6656=m.

It turns out that if your config tickles __builtin_constant_p via differences in choices to inline or not, these statements produce invalid assembly:

$ cat foo.c
long a(long b, long c) {
  asm("orb\t%1, %0" : "+q"(c): "r"(b));
  return c;
}
$ gcc foo.c
foo.c: Assembler messages:
foo.c:2: Error: `%rax' not allowed with `orb'

Use the %b "x86 Operand Modifier" to instead force register allocation to select a lower-8-bit GPR operand.

The "q" constraint only has meaning on -m32 otherwise is treated as "r". Not all GPRs have low-8-bit aliases for -m32.

Quoted 7 lines

---
Changes V2 -> V3:
* use %b "x86 Operand Modifier" instead of bitwise op then cast.
* reword commit message.
* add Brian and HPA suggested by tags
* drop Nathan & Sedat Tested by/reviewed by tags (new patch is different enough).

Oh, and I took over authorship for this version.

Quoted 40 lines

Changes V1 -> V2:
* change authorship/signed-off-by to Ilie
* add Nathan's Tested by/reviewed by
* update commit message slightly with info sent to HPA.

 arch/x86/include/asm/bitops.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/x86/include/asm/bitops.h +4 -5
index b392571c1f1d..03e24286e4eb 100644
@@ -52,9 +52,9 @@ static __always_inline void
 arch_set_bit(long nr, volatile unsigned long *addr)
 {
        if (__builtin_constant_p(nr)) {
-               asm volatile(LOCK_PREFIX "orb %1,%0"
+               asm volatile(LOCK_PREFIX "orb %b1,%0"
                        : CONST_MASK_ADDR(nr, addr)
-                       : "iq" (CONST_MASK(nr) & 0xff)
+                       : "iq" (CONST_MASK(nr))
                        : "memory");
        } else {
                asm volatile(LOCK_PREFIX __ASM_SIZE(bts) " %1,%0"
@@ -72,9 +72,9 @@ static __always_inline void
 arch_clear_bit(long nr, volatile unsigned long *addr)
 {
        if (__builtin_constant_p(nr)) {
-               asm volatile(LOCK_PREFIX "andb %1,%0"
+               asm volatile(LOCK_PREFIX "andb %b1,%0"
                        : CONST_MASK_ADDR(nr, addr)
-                       : "iq" (CONST_MASK(nr) ^ 0xff));
+                       : "iq" (~CONST_MASK(nr)));
        } else {
                asm volatile(LOCK_PREFIX __ASM_SIZE(btr) " %1,%0"
                        : : RLONG_ADDR(addr), "Ir" (nr) : "memory");
--

2.26.2.645.ge9eca65c58-goog

Signature · 2 lines
Thanks,
~Nick Desaulniers
#12 lore raw
In reply to Nick Desaulniers
Quoted 68 lines · Nick Desaulniers

This is easily reproducible via CC=clang+CONFIG_STAGING=y+CONFIG_VT6656=m.

It turns out that if your config tickles __builtin_constant_p via differences in choices to inline or not, these statements produce invalid assembly:

$ cat foo.c
long a(long b, long c) {
  asm("orb\t%1, %0" : "+q"(c): "r"(b));
  return c;
}
$ gcc foo.c
foo.c: Assembler messages:
foo.c:2: Error: `%rax' not allowed with `orb'

Use the %b "x86 Operand Modifier" to instead force register allocation to select a lower-8-bit GPR operand.

The "q" constraint only has meaning on -m32 otherwise is treated as "r". Not all GPRs have low-8-bit aliases for -m32.

---
Changes V2 -> V3:
* use %b "x86 Operand Modifier" instead of bitwise op then cast.
* reword commit message.
* add Brian and HPA suggested by tags
* drop Nathan & Sedat Tested by/reviewed by tags (new patch is different enough).

Changes V1 -> V2:
* change authorship/signed-off-by to Ilie
* add Nathan's Tested by/reviewed by
* update commit message slightly with info sent to HPA.

 arch/x86/include/asm/bitops.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/x86/include/asm/bitops.h +2 -2
index b392571c1f1d..03e24286e4eb 100644
@@ -52,9 +52,9 @@ static __always_inline void
 arch_set_bit(long nr, volatile unsigned long *addr)
 {
        if (__builtin_constant_p(nr)) {
-               asm volatile(LOCK_PREFIX "orb %1,%0"
+               asm volatile(LOCK_PREFIX "orb %b1,%0"
                        : CONST_MASK_ADDR(nr, addr)
-                       : "iq" (CONST_MASK(nr) & 0xff)
+                       : "iq" (CONST_MASK(nr))
                        : "memory");
        } else {
                asm volatile(LOCK_PREFIX __ASM_SIZE(bts) " %1,%0"
@@ -72,9 +72,9 @@ static __always_inline void
 arch_clear_bit(long nr, volatile unsigned long *addr)

arch/x86/include/asm/bitops.h static __always_inline void · line 80

Quoted 10 lines
arch/x86/include/asm/bitops.h +2 -2
 {
        if (__builtin_constant_p(nr)) {
-               asm volatile(LOCK_PREFIX "andb %1,%0"
+               asm volatile(LOCK_PREFIX "andb %b1,%0"
                        : CONST_MASK_ADDR(nr, addr)
-                       : "iq" (CONST_MASK(nr) ^ 0xff));
+                       : "iq" (~CONST_MASK(nr)));
        } else {
                asm volatile(LOCK_PREFIX __ASM_SIZE(btr) " %1,%0"
                        : : RLONG_ADDR(addr), "Ir" (nr) : "memory");

arch_change_bit() should also be changed, but otherwise looks good.

--
Brian Gerst

#13 lore raw

PATCH v4 1/1 x86: bitops: fix build regression

This is easily reproducible via CC=clang+CONFIG_STAGING=y+CONFIG_VT6656=m.

It turns out that if your config tickles __builtin_constant_p via differences in choices to inline or not, these statements produce invalid assembly:

$ cat foo.c
long a(long b, long c) {
  asm("orb\t%1, %0" : "+q"(c): "r"(b));
  return c;
}
$ gcc foo.c
foo.c: Assembler messages:
foo.c:2: Error: `%rax' not allowed with `orb'

Use the %b "x86 Operand Modifier" to instead force register allocation to select a lower-8-bit GPR operand.

The "q" constraint only has meaning on -m32 otherwise is treated as "r". Not all GPRs have low-8-bit aliases for -m32.

---
Changes V3 -> V4:
* drop (u8) cast from arch_change_bit() as well.

Changes V2 -> V3:
* use %b "x86 Operand Modifier" instead of bitwise op then cast.
* reword commit message.
* add Brian and HPA suggested by tags
* drop Nathan & Sedat Tested by/reviewed by tags (new patch is different enough).
* Take over authorship.

Changes V1 -> V2:
* change authorship/signed-off-by to Ilie
* add Nathan's Tested by/reviewed by
* update commit message slightly with info sent to HPA.

 arch/x86/include/asm/bitops.h | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
arch/x86/include/asm/bitops.h +5 -5
index b392571c1f1d..8a8b7bb9677b 100644
@@ -52,9 +52,9 @@ static __always_inline void
 arch_set_bit(long nr, volatile unsigned long *addr)
 {
 	if (__builtin_constant_p(nr)) {
-		asm volatile(LOCK_PREFIX "orb %1,%0"
+		asm volatile(LOCK_PREFIX "orb %b1,%0"
 			: CONST_MASK_ADDR(nr, addr)
-			: "iq" (CONST_MASK(nr) & 0xff)
+			: "iq" (CONST_MASK(nr))
 			: "memory");
 	} else {
 		asm volatile(LOCK_PREFIX __ASM_SIZE(bts) " %1,%0"
@@ -72,9 +72,9 @@ static __always_inline void
 arch_clear_bit(long nr, volatile unsigned long *addr)
 {
 	if (__builtin_constant_p(nr)) {
-		asm volatile(LOCK_PREFIX "andb %1,%0"
+		asm volatile(LOCK_PREFIX "andb %b1,%0"
 			: CONST_MASK_ADDR(nr, addr)
-			: "iq" (CONST_MASK(nr) ^ 0xff));
+			: "iq" (~CONST_MASK(nr)));
 	} else {
 		asm volatile(LOCK_PREFIX __ASM_SIZE(btr) " %1,%0"
 			: : RLONG_ADDR(addr), "Ir" (nr) : "memory");
@@ -125,7 +125,7 @@ arch_change_bit(long nr, volatile unsigned long *addr)
 	if (__builtin_constant_p(nr)) {
 		asm volatile(LOCK_PREFIX "xorb %1,%0"
 			: CONST_MASK_ADDR(nr, addr)
-			: "iq" ((u8)CONST_MASK(nr)));
+			: "iq" (CONST_MASK(nr)));
 	} else {
 		asm volatile(LOCK_PREFIX __ASM_SIZE(btc) " %1,%0"
 			: : RLONG_ADDR(addr), "Ir" (nr) : "memory");
Signature · 1 line
2.26.2.645.ge9eca65c58-goog
#14 lore raw

PATCH v5 1/1 x86: bitops: fix build regression

This is easily reproducible via CC=clang+CONFIG_STAGING=y+CONFIG_VT6656=m.

It turns out that if your config tickles __builtin_constant_p via differences in choices to inline or not, these statements produce invalid assembly:

$ cat foo.c
long a(long b, long c) {
  asm("orb\t%1, %0" : "+q"(c): "r"(b));
  return c;
}
$ gcc foo.c
foo.c: Assembler messages:
foo.c:2: Error: `%rax' not allowed with `orb'

Use the %b "x86 Operand Modifier" to instead force register allocation to select a lower-8-bit GPR operand.

The "q" constraint only has meaning on -m32 otherwise is treated as "r". Not all GPRs have low-8-bit aliases for -m32.

---
Changes V4 -> V5:
* actually use %b in arch_change_bit().

Changes V3 -> V4:
* drop (u8) cast from arch_change_bit() as well.

Changes V2 -> V3:
* use %b "x86 Operand Modifier" instead of bitwise op then cast.
* reword commit message.
* add Brian and HPA suggested by tags
* drop Nathan & Sedat Tested by/reviewed by tags (new patch is different enough).
* Take over authorship.

Changes V1 -> V2:
* change authorship/signed-off-by to Ilie
* add Nathan's Tested by/reviewed by
* update commit message slightly with info sent to HPA.

 arch/x86/include/asm/bitops.h | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
arch/x86/include/asm/bitops.h +6 -6
index b392571c1f1d..35460fef39b8 100644
@@ -52,9 +52,9 @@ static __always_inline void
 arch_set_bit(long nr, volatile unsigned long *addr)
 {
 	if (__builtin_constant_p(nr)) {
-		asm volatile(LOCK_PREFIX "orb %1,%0"
+		asm volatile(LOCK_PREFIX "orb %b1,%0"
 			: CONST_MASK_ADDR(nr, addr)
-			: "iq" (CONST_MASK(nr) & 0xff)
+			: "iq" (CONST_MASK(nr))
 			: "memory");
 	} else {
 		asm volatile(LOCK_PREFIX __ASM_SIZE(bts) " %1,%0"
@@ -72,9 +72,9 @@ static __always_inline void
 arch_clear_bit(long nr, volatile unsigned long *addr)
 {
 	if (__builtin_constant_p(nr)) {
-		asm volatile(LOCK_PREFIX "andb %1,%0"
+		asm volatile(LOCK_PREFIX "andb %b1,%0"
 			: CONST_MASK_ADDR(nr, addr)
-			: "iq" (CONST_MASK(nr) ^ 0xff));
+			: "iq" (~CONST_MASK(nr)));
 	} else {
 		asm volatile(LOCK_PREFIX __ASM_SIZE(btr) " %1,%0"
 			: : RLONG_ADDR(addr), "Ir" (nr) : "memory");
@@ -123,9 +123,9 @@ static __always_inline void
 arch_change_bit(long nr, volatile unsigned long *addr)
 {
 	if (__builtin_constant_p(nr)) {
-		asm volatile(LOCK_PREFIX "xorb %1,%0"
+		asm volatile(LOCK_PREFIX "xorb %b1,%0"
 			: CONST_MASK_ADDR(nr, addr)
-			: "iq" ((u8)CONST_MASK(nr)));
+			: "iq" (CONST_MASK(nr)));
 	} else {
 		asm volatile(LOCK_PREFIX __ASM_SIZE(btc) " %1,%0"
 			: : RLONG_ADDR(addr), "Ir" (nr) : "memory");
Signature · 1 line
2.26.2.645.ge9eca65c58-goog
Reviewed-by Tested-by on v5 1/1
#15 lore raw
Quoted 21 lines · Nick Desaulniers

This is easily reproducible via CC=clang+CONFIG_STAGING=y+CONFIG_VT6656=m.

It turns out that if your config tickles __builtin_constant_p via differences in choices to inline or not, these statements produce invalid assembly:

$ cat foo.c
long a(long b, long c) {
  asm("orb\t%1, %0" : "+q"(c): "r"(b));
  return c;
}
$ gcc foo.c
foo.c: Assembler messages:
foo.c:2: Error: `%rax' not allowed with `orb'

Use the %b "x86 Operand Modifier" to instead force register allocation to select a lower-8-bit GPR operand.

The "q" constraint only has meaning on -m32 otherwise is treated as "r". Not all GPRs have low-8-bit aliases for -m32.

Quoted 12 lines
Quoted 65 lines

---
Changes V4 -> V5:
* actually use %b in arch_change_bit().

Changes V3 -> V4:
* drop (u8) cast from arch_change_bit() as well.

Changes V2 -> V3:
* use %b "x86 Operand Modifier" instead of bitwise op then cast.
* reword commit message.
* add Brian and HPA suggested by tags
* drop Nathan & Sedat Tested by/reviewed by tags (new patch is different enough).
* Take over authorship.

Changes V1 -> V2:
* change authorship/signed-off-by to Ilie
* add Nathan's Tested by/reviewed by
* update commit message slightly with info sent to HPA.

 arch/x86/include/asm/bitops.h | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
arch/x86/include/asm/bitops.h +6 -6
index b392571c1f1d..35460fef39b8 100644
@@ -52,9 +52,9 @@ static __always_inline void
 arch_set_bit(long nr, volatile unsigned long *addr)
 {
 	if (__builtin_constant_p(nr)) {
-		asm volatile(LOCK_PREFIX "orb %1,%0"
+		asm volatile(LOCK_PREFIX "orb %b1,%0"
 			: CONST_MASK_ADDR(nr, addr)
-			: "iq" (CONST_MASK(nr) & 0xff)
+			: "iq" (CONST_MASK(nr))
 			: "memory");
 	} else {
 		asm volatile(LOCK_PREFIX __ASM_SIZE(bts) " %1,%0"
@@ -72,9 +72,9 @@ static __always_inline void
 arch_clear_bit(long nr, volatile unsigned long *addr)
 {
 	if (__builtin_constant_p(nr)) {
-		asm volatile(LOCK_PREFIX "andb %1,%0"
+		asm volatile(LOCK_PREFIX "andb %b1,%0"
 			: CONST_MASK_ADDR(nr, addr)
-			: "iq" (CONST_MASK(nr) ^ 0xff));
+			: "iq" (~CONST_MASK(nr)));
 	} else {
 		asm volatile(LOCK_PREFIX __ASM_SIZE(btr) " %1,%0"
 			: : RLONG_ADDR(addr), "Ir" (nr) : "memory");
@@ -123,9 +123,9 @@ static __always_inline void
 arch_change_bit(long nr, volatile unsigned long *addr)
 {
 	if (__builtin_constant_p(nr)) {
-		asm volatile(LOCK_PREFIX "xorb %1,%0"
+		asm volatile(LOCK_PREFIX "xorb %b1,%0"
 			: CONST_MASK_ADDR(nr, addr)
-			: "iq" ((u8)CONST_MASK(nr)));
+			: "iq" (CONST_MASK(nr)));
 	} else {
 		asm volatile(LOCK_PREFIX __ASM_SIZE(btc) " %1,%0"
 			: : RLONG_ADDR(addr), "Ir" (nr) : "memory");
Signature · 1 line
2.26.2.645.ge9eca65c58-goog
Reviewed-by
#16 lore raw
Quoted 3 lines · On Fri, 8 May 2020 13:28:35 -0700 Nathan Chancellor <[email protected]>…
Quoted 2 lines · Nick Desaulniers

Use the %b "x86 Operand Modifier" to instead force register allocation to select a lower-8-bit GPR operand.

This looks OK to me, I appreciate the work done to find the right fix and clean up the code while not breaking sparse! I had a look at the assembly from gcc 9.3.1 and it looks good. Thanks!

Tested-by
#17 lore raw
Quoted 5 lines · Jesse Brandeburg
Quoted 1 line · On Fri, 8 May 2020 13:28:35 -0700 Nathan Chancellor <[email protected]>…

On Fri, May 08, 2020 at 11:32:29AM -0700, Nick Desaulniers wrote:

Quoted 9 lines
Quoted 2 lines
Quoted 2 lines

Use the %b "x86 Operand Modifier" to instead force register allocation to select a lower-8-bit GPR operand.

This looks OK to me, I appreciate the work done to find the right fix and clean up the code while not breaking sparse! I had a look at the assembly from gcc 9.3.1 and it looks good. Thanks!

Tested v5 on Debian/testing AMD64 with a selfmade llvm-toolchain (LLVM/Clang/LLD) v10.0.1+git92d5c1be9ee9

Please add:

For details see
<https://github.com/ClangBuiltLinux/linux/issues/961#issuecomment-626104287>

Thanks to all involved people.

- Sedat -

#18 lore raw
In reply to Nick Desaulniers
Quoted 16 lines · Nick Desaulniers

This is easily reproducible via CC=clang+CONFIG_STAGING=y+CONFIG_VT6656=m.

It turns out that if your config tickles __builtin_constant_p via differences in choices to inline or not, these statements produce invalid assembly:

$ cat foo.c
long a(long b, long c) {
  asm("orb\t%1, %0" : "+q"(c): "r"(b));
  return c;
}
$ gcc foo.c
foo.c: Assembler messages:
foo.c:2: Error: `%rax' not allowed with `orb'
Quoted 6 lines

Use the %b "x86 Operand Modifier" to instead force register allocation to select a lower-8-bit GPR operand.

The "q" constraint only has meaning on -m32 otherwise is treated as "r". Not all GPRs have low-8-bit aliases for -m32.

Looks very good!
One question though, does it work with minimum supported version of gcc?

Quoted 77 lines

---
Changes V4 -> V5:
* actually use %b in arch_change_bit().

Changes V3 -> V4:
* drop (u8) cast from arch_change_bit() as well.

Changes V2 -> V3:
* use %b "x86 Operand Modifier" instead of bitwise op then cast.
* reword commit message.
* add Brian and HPA suggested by tags
* drop Nathan & Sedat Tested by/reviewed by tags (new patch is different enough).
* Take over authorship.

Changes V1 -> V2:
* change authorship/signed-off-by to Ilie
* add Nathan's Tested by/reviewed by
* update commit message slightly with info sent to HPA.

 arch/x86/include/asm/bitops.h | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
arch/x86/include/asm/bitops.h +6 -7
index b392571c1f1d..35460fef39b8 100644
@@ -52,9 +52,9 @@ static __always_inline void
 arch_set_bit(long nr, volatile unsigned long *addr)
 {
        if (__builtin_constant_p(nr)) {
-               asm volatile(LOCK_PREFIX "orb %1,%0"
+               asm volatile(LOCK_PREFIX "orb %b1,%0"
                        : CONST_MASK_ADDR(nr, addr)
-                       : "iq" (CONST_MASK(nr) & 0xff)
+                       : "iq" (CONST_MASK(nr))
                        : "memory");
        } else {
                asm volatile(LOCK_PREFIX __ASM_SIZE(bts) " %1,%0"
@@ -72,9 +72,9 @@ static __always_inline void
 arch_clear_bit(long nr, volatile unsigned long *addr)
 {
        if (__builtin_constant_p(nr)) {
-               asm volatile(LOCK_PREFIX "andb %1,%0"
+               asm volatile(LOCK_PREFIX "andb %b1,%0"
                        : CONST_MASK_ADDR(nr, addr)
-                       : "iq" (CONST_MASK(nr) ^ 0xff));
+                       : "iq" (~CONST_MASK(nr)));
        } else {
                asm volatile(LOCK_PREFIX __ASM_SIZE(btr) " %1,%0"
                        : : RLONG_ADDR(addr), "Ir" (nr) : "memory");
@@ -123,9 +123,9 @@ static __always_inline void
 arch_change_bit(long nr, volatile unsigned long *addr)
 {
        if (__builtin_constant_p(nr)) {
-               asm volatile(LOCK_PREFIX "xorb %1,%0"
+               asm volatile(LOCK_PREFIX "xorb %b1,%0"
                        : CONST_MASK_ADDR(nr, addr)
-                       : "iq" ((u8)CONST_MASK(nr)));
+                       : "iq" (CONST_MASK(nr)));
        } else {
                asm volatile(LOCK_PREFIX __ASM_SIZE(btc) " %1,%0"
                        : : RLONG_ADDR(addr), "Ir" (nr) : "memory");
--

2.26.2.645.ge9eca65c58-goog

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#19 lore raw
Quoted 21 lines · Andy Shevchenko
Quoted 19 lines · Nick Desaulniers

This is easily reproducible via CC=clang+CONFIG_STAGING=y+CONFIG_VT6656=m.

It turns out that if your config tickles __builtin_constant_p via differences in choices to inline or not, these statements produce invalid assembly:

$ cat foo.c
long a(long b, long c) {
  asm("orb\t%1, %0" : "+q"(c): "r"(b));
  return c;
}
$ gcc foo.c
foo.c: Assembler messages:
foo.c:2: Error: `%rax' not allowed with `orb'

Use the %b "x86 Operand Modifier" to instead force register allocation to select a lower-8-bit GPR operand.

Quoted 6 lines
Quoted 3 lines

The "q" constraint only has meaning on -m32 otherwise is treated as "r". Not all GPRs have low-8-bit aliases for -m32.

Looks very good!
One question though, does it work with minimum supported version of gcc?

Yes. The operand width modifiers have been around a long time but not well documented until more recently.

--
Brian Gerst

#20 lore raw
In reply to Andy Shevchenko
Quoted 21 lines · Andy Shevchenko
Quoted 19 lines · Nick Desaulniers

This is easily reproducible via CC=clang+CONFIG_STAGING=y+CONFIG_VT6656=m.

It turns out that if your config tickles __builtin_constant_p via differences in choices to inline or not, these statements produce invalid assembly:

$ cat foo.c
long a(long b, long c) {
  asm("orb\t%1, %0" : "+q"(c): "r"(b));
  return c;
}
$ gcc foo.c
foo.c: Assembler messages:
foo.c:2: Error: `%rax' not allowed with `orb'

Use the %b "x86 Operand Modifier" to instead force register allocation to select a lower-8-bit GPR operand.

Quoted 6 lines
Quoted 3 lines

The "q" constraint only has meaning on -m32 otherwise is treated as "r". Not all GPRs have low-8-bit aliases for -m32.

Looks very good!
One question though, does it work with minimum supported version of gcc?

Yes; the oldest version of GCC in godbolt.org is GCC 4.1.2 which supports the syntax and generates the expected lower-8-bit register operands.

Signature · 2 lines
Thanks,
~Nick Desaulniers
#21 lore raw
In reply to Nick Desaulniers

From: Nick Desaulniers

Quoted 1 line

Sent: 08 May 2020 19:32

..

Quoted 3 lines

It turns out that if your config tickles __builtin_constant_p via differences in choices to inline or not, these statements produce invalid assembly:

...

Quoted 5 lines
arch/x86/include/asm/bitops.h +0 -0
index b392571c1f1d..35460fef39b8 100644

arch/x86/include/asm/bitops.h static __always_inline void · line 58

Quoted 9 lines
arch/x86/include/asm/bitops.h +2 -2
@@ -52,9 +52,9 @@ static __always_inline void
 arch_set_bit(long nr, volatile unsigned long *addr)
 {
 	if (__builtin_constant_p(nr)) {
-		asm volatile(LOCK_PREFIX "orb %1,%0"
+		asm volatile(LOCK_PREFIX "orb %b1,%0"
 			: CONST_MASK_ADDR(nr, addr)
-			: "iq" (CONST_MASK(nr) & 0xff)
+			: "iq" (CONST_MASK(nr))
 			: "memory");

What happens if CONST_MASK() is changed to:
#define CONST_MASK_(n) (n == 0 ? 1 : n == 1 ? 2 : n ....) #define CONST_MASK(n) CONST_MASK_(((n) & 7))

and a separate definition for the inverse mask.

The lack of arithmetic promotion may mean that the only "i" constraint is needed.

David

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

Reviewed-by on v5 1/1
#22 lore raw
In reply to Nick Desaulniers
Quoted 22 lines · Nick Desaulniers

This is easily reproducible via CC=clang+CONFIG_STAGING=y+CONFIG_VT6656=m.

It turns out that if your config tickles __builtin_constant_p via differences in choices to inline or not, these statements produce invalid assembly:

$ cat foo.c
long a(long b, long c) {
  asm("orb\t%1, %0" : "+q"(c): "r"(b));
  return c;
}
$ gcc foo.c
foo.c: Assembler messages:
foo.c:2: Error: `%rax' not allowed with `orb'

Use the %b "x86 Operand Modifier" to instead force register allocation to select a lower-8-bit GPR operand.

The "q" constraint only has meaning on -m32 otherwise is treated as "r". Not all GPRs have low-8-bit aliases for -m32.

Quoted 12 lines
#23 lore raw

Bumping for this to get reviewed+picked up. Fixes a regression I would prefer didn't ship in 5.7.

Quoted 38 lines · Brian Gerst
Quoted 22 lines · Nick Desaulniers

This is easily reproducible via CC=clang+CONFIG_STAGING=y+CONFIG_VT6656=m.

It turns out that if your config tickles __builtin_constant_p via differences in choices to inline or not, these statements produce invalid assembly:

$ cat foo.c
long a(long b, long c) {
  asm("orb\t%1, %0" : "+q"(c): "r"(b));
  return c;
}
$ gcc foo.c
foo.c: Assembler messages:
foo.c:2: Error: `%rax' not allowed with `orb'

Use the %b "x86 Operand Modifier" to instead force register allocation to select a lower-8-bit GPR operand.

The "q" constraint only has meaning on -m32 otherwise is treated as "r". Not all GPRs have low-8-bit aliases for -m32.

Quoted 12 lines
Signature · 2 lines
Thanks,
~Nick Desaulniers
#24 lore raw
In reply to H. Peter Anvin

From: Peter Anvin

Quoted 9 lines

Sent: 08 May 2020 18:32

Quoted 7 lines · Nick Desaulniers
Quoted 4 lines

One last suggestion. Add the "b" modifier to the mask operand: "orb %b1, %0". That forces the compiler to use the 8-bit register name instead of trying to deduce the width from the input.

Ah right: https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#x86Operandmodifiers

Quoted 8 lines
Quoted 4 lines

Looks like that works for both compilers. In that case, we can likely drop the & 0xff, too. Let me play with that, then I'll hopefully send a v3 today.

Good idea. I requested a while ago that they document these modifiers; they chose not to document them all which in some ways is good; it shows what they are willing to commit to indefinitely.

I thought the intention here was to explicitly do a byte access. If the constant bit number has had a div/mod by 8 done on it then the address can be misaligned - so you mustn't do a non-byte sized locked access.

OTOH the original base address must be aligned.

Looking at some instruction timing, BTS/BTR aren't too bad if the bit number is a constant. But are 6 or 7 clocks slower if it is in %cl. Given these are locked RMW bus cycles they'll always be slow!

How about an asm multi-part alternative that uses a byte offset and byte constant if the compiler thinks the mask is constant or a 4-byte offset and 32bit mask if it doesn't.

The other alternative is to just use BTS/BTS and (maybe) rely on the assembler to add in the word offset to the base address.

David

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

#25 lore raw
Quoted 42 lines · David Laight

From: Peter Anvin

Quoted 4 lines

Sent: 08 May 2020 18:32

Quoted 2 lines · Nick Desaulniers
Quoted 2 lines

One last suggestion. Add the "b" modifier to the mask operand:

"orb

Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

%b1, %0". That forces the compiler to use the 8-bit register name instead of trying to deduce the width from the input.

Ah right:

https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#x86Operandmodifiers

Quoted 2 lines
Quoted 2 lines

Looks like that works for both compilers. In that case, we can

likely

Quoted 5 lines
Quoted 3 lines

drop the & 0xff, too. Let me play with that, then I'll hopefully send a v3 today.

Good idea. I requested a while ago that they document these

modifiers; they

Quoted 1 line

chose not to document them all which in some ways is good; it shows

what they

Quoted 1 line

are willing to commit to indefinitely.

I thought the intention here was to explicitly do a byte access. If the constant bit number has had a div/mod by 8 done on it then the address can be misaligned - so you mustn't do a non-byte sized locked access.

OTOH the original base address must be aligned.

Looking at some instruction timing, BTS/BTR aren't too bad if the bit number is a constant. But are 6 or 7 clocks slower if it is in %cl. Given these are locked RMW bus cycles they'll always be slow!

How about an asm multi-part alternative that uses a byte offset and byte constant if the compiler thinks the mask is constant or a 4-byte offset and 32bit mask if it doesn't.

The other alternative is to just use BTS/BTS and (maybe) rely on the assembler to add in the word offset to the base address.

Quoted 6 lines

David

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

I don't understand what you are getting at here.

The intent is to do a byte access. The "multi-part asm" you are talking about is also already there...

Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.
#26 lore raw
In reply to Peter Zijlstra
Quoted 27 lines · Peter Zijlstra
Quoted 1 line · On Tue, May 05, 2020 at 11:07:24AM -0700, [email protected] wrote:

On May 5, 2020 10:44:22 AM PDT, Nick Desaulniers

Quoted 1 line · <[email protected]> wrote:

Review arch_set_bit(long nr, volatile unsigned long

Quoted 1 line
(quoted diff) +0 -0
@@ -54,7 +54,7 @@ arch_set_bit(long nr, volatile unsigned long

*addr)

Quoted 9 lines

arch/x86/include/asm/bitops.h arch_clear_bit(long nr, volatile unsigned long

Quoted 9 lines
arch/x86/include/asm/bitops.h +1 -1
 	if (__builtin_constant_p(nr)) {
 		asm volatile(LOCK_PREFIX "orb %1,%0"
 			: CONST_MASK_ADDR(nr, addr)
-			: "iq" (CONST_MASK(nr) & 0xff)
+			: "iq" ((u8)(CONST_MASK(nr) & 0xff))
 			: "memory");
 	} else {
 		asm volatile(LOCK_PREFIX __ASM_SIZE(bts) " %1,%0"
@@ -74,7 +74,7 @@ arch_clear_bit(long nr, volatile unsigned long

*addr)

Quoted 10 lines

arch/x86/include/asm/bitops.h arch_clear_bit(long nr, volatile unsigned long *addr) · line 80

Quoted 8 lines
arch/x86/include/asm/bitops.h +1 -1
 	if (__builtin_constant_p(nr)) {
 		asm volatile(LOCK_PREFIX "andb %1,%0"
 			: CONST_MASK_ADDR(nr, addr)
-			: "iq" (CONST_MASK(nr) ^ 0xff));
+			: "iq" ((u8)(CONST_MASK(nr) ^ 0xff)));
 	} else {
 		asm volatile(LOCK_PREFIX __ASM_SIZE(btr) " %1,%0"
 			: : RLONG_ADDR(addr), "Ir" (nr) : "memory");

Drop & 0xff and change ^ 0xff to ~.

Quoted 3 lines

But then we're back to sparse being unhappy, no? The thing with ~ is that it will set high bits which will be truncated, which makes sparse sad.

In that case, sparse is just broken.

Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.
#27 lore raw
In reply to Nick Desaulniers
Quoted 15 lines · Nick Desaulniers

From: Sedat Dilek <[email protected]>

It turns out that if your config tickles __builtin_constant_p via differences in choices to inline or not, this now produces invalid assembly:

$ cat foo.c
long a(long b, long c) {
  asm("orb\t%1, %0" : "+q"(c): "r"(b));
  return c;
}
$ gcc foo.c
foo.c: Assembler messages:
foo.c:2: Error: `%rax' not allowed with `orb'
Quoted 5 lines

The "q" constraint only has meanting on -m32 otherwise is treated as "r".

This is easily reproducible via Clang+CONFIG_STAGING=y+CONFIG_VT6656=m, or Clang+allyesconfig.

For what it's worth, I don't see this with allyesconfig.

Quoted 6 lines

Keep the masking operation to appease sparse (make C=1), add back the cast in order to properly select the proper 8b register alias.

[Nick: reworded]

The offending commit was added in 5.7-rc1; we shouldn't need to Cc stable since this should be picked up as an -rc fix.

Quoted 8 lines

Not to split hairs but this is Ilie's diff, he should probably be the author with Sedat's Reported-by/Tested-by.

https://github.com/ClangBuiltLinux/linux/issues/961#issuecomment-608239458

But eh, it's all a team effort plus that can only happen with Ilie's explicit consent for a Signed-off-by.

I am currently doing a set of builds with clang-11 with this patch on top of 5.7-rc4 to make sure that all of the cases I have found work. Once that is done, I'll comment back with a tag.

Quoted 32 lines
---
 arch/x86/include/asm/bitops.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/include/asm/bitops.h +2 -2
index b392571c1f1d..139122e5b25b 100644
@@ -54,7 +54,7 @@ arch_set_bit(long nr, volatile unsigned long *addr)
 	if (__builtin_constant_p(nr)) {
 		asm volatile(LOCK_PREFIX "orb %1,%0"
 			: CONST_MASK_ADDR(nr, addr)
-			: "iq" (CONST_MASK(nr) & 0xff)
+			: "iq" ((u8)(CONST_MASK(nr) & 0xff))
 			: "memory");
 	} else {
 		asm volatile(LOCK_PREFIX __ASM_SIZE(bts) " %1,%0"
@@ -74,7 +74,7 @@ arch_clear_bit(long nr, volatile unsigned long *addr)
 	if (__builtin_constant_p(nr)) {
 		asm volatile(LOCK_PREFIX "andb %1,%0"
 			: CONST_MASK_ADDR(nr, addr)
-			: "iq" (CONST_MASK(nr) ^ 0xff));
+			: "iq" ((u8)(CONST_MASK(nr) ^ 0xff)));
 	} else {
 		asm volatile(LOCK_PREFIX __ASM_SIZE(btr) " %1,%0"
 			: : RLONG_ADDR(addr), "Ir" (nr) : "memory");
Signature · 1 line
2.26.2.526.g744177e7f7-goog

Cheers,
Nathan

#28 lore raw
Quoted 44 lines · Nathan Chancellor
Quoted 15 lines · Nick Desaulniers

From: Sedat Dilek <[email protected]>

It turns out that if your config tickles __builtin_constant_p via differences in choices to inline or not, this now produces invalid assembly:

$ cat foo.c
long a(long b, long c) {
  asm("orb\t%1, %0" : "+q"(c): "r"(b));
  return c;
}
$ gcc foo.c
foo.c: Assembler messages:
foo.c:2: Error: `%rax' not allowed with `orb'
Quoted 5 lines

The "q" constraint only has meanting on -m32 otherwise is treated as "r".

This is easily reproducible via Clang+CONFIG_STAGING=y+CONFIG_VT6656=m, or Clang+allyesconfig.

For what it's worth, I don't see this with allyesconfig.

Quoted 6 lines

Keep the masking operation to appease sparse (make C=1), add back the cast in order to properly select the proper 8b register alias.

[Nick: reworded]

The offending commit was added in 5.7-rc1; we shouldn't need to Cc stable since this should be picked up as an -rc fix.

Quoted 8 lines
Quoted 8 lines

Not to split hairs but this is Ilie's diff, he should probably be the author with Sedat's Reported-by/Tested-by.

https://github.com/ClangBuiltLinux/linux/issues/961#issuecomment-608239458

But eh, it's all a team effort plus that can only happen with Ilie's explicit consent for a Signed-off-by.

Digital dementia... Looking 3 weeks back I have put all relevant informations into the patches in [1], mentionning the diff is from Ilie.
Ilie for what reason did not react on any response for 3 weeks in the CBL issue-tracker.
I think Nick wants to quickly fix the Kernel-CI-Bot issue seen with Clang. Personally, I hope this patch will be upstreamed in (one of) the next RC release.

I agree on CC:stable can be dropped.
Check causing commit-id:

$ git describe --contains 1651e700664b4
v5.7-rc1~122^2

Thanks.

Regards,
- Sedat -

[1] https://github.com/ClangBuiltLinux/linux/issues/961#issuecomment-613207374

Quoted 39 lines

I am currently doing a set of builds with clang-11 with this patch on top of 5.7-rc4 to make sure that all of the cases I have found work. Once that is done, I'll comment back with a tag.

Quoted 30 lines
---
 arch/x86/include/asm/bitops.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/include/asm/bitops.h +2 -3
index b392571c1f1d..139122e5b25b 100644
@@ -54,7 +54,7 @@ arch_set_bit(long nr, volatile unsigned long *addr)
      if (__builtin_constant_p(nr)) {
              asm volatile(LOCK_PREFIX "orb %1,%0"
                      : CONST_MASK_ADDR(nr, addr)
-                     : "iq" (CONST_MASK(nr) & 0xff)
+                     : "iq" ((u8)(CONST_MASK(nr) & 0xff))
                      : "memory");
      } else {
              asm volatile(LOCK_PREFIX __ASM_SIZE(bts) " %1,%0"
@@ -74,7 +74,7 @@ arch_clear_bit(long nr, volatile unsigned long *addr)
      if (__builtin_constant_p(nr)) {
              asm volatile(LOCK_PREFIX "andb %1,%0"
                      : CONST_MASK_ADDR(nr, addr)
-                     : "iq" (CONST_MASK(nr) ^ 0xff));
+                     : "iq" ((u8)(CONST_MASK(nr) ^ 0xff)));
      } else {
              asm volatile(LOCK_PREFIX __ASM_SIZE(btr) " %1,%0"
                      : : RLONG_ADDR(addr), "Ir" (nr) : "memory");
--
Quoted 2 lines

2.26.2.526.g744177e7f7-goog

Cheers,
Nathan

Reviewed-by Tested-by
#29 lore raw
In reply to Nathan Chancellor
Quoted 46 lines · Nathan Chancellor
Quoted 15 lines · Nick Desaulniers

From: Sedat Dilek <[email protected]>

It turns out that if your config tickles __builtin_constant_p via differences in choices to inline or not, this now produces invalid assembly:

$ cat foo.c
long a(long b, long c) {
  asm("orb\t%1, %0" : "+q"(c): "r"(b));
  return c;
}
$ gcc foo.c
foo.c: Assembler messages:
foo.c:2: Error: `%rax' not allowed with `orb'
Quoted 5 lines

The "q" constraint only has meanting on -m32 otherwise is treated as "r".

This is easily reproducible via Clang+CONFIG_STAGING=y+CONFIG_VT6656=m, or Clang+allyesconfig.

For what it's worth, I don't see this with allyesconfig.

Quoted 6 lines

Keep the masking operation to appease sparse (make C=1), add back the cast in order to properly select the proper 8b register alias.

[Nick: reworded]

The offending commit was added in 5.7-rc1; we shouldn't need to Cc stable since this should be picked up as an -rc fix.

Quoted 8 lines

Not to split hairs but this is Ilie's diff, he should probably be the author with Sedat's Reported-by/Tested-by.

Quoted 8 lines

https://github.com/ClangBuiltLinux/linux/issues/961#issuecomment-608239458

But eh, it's all a team effort plus that can only happen with Ilie's explicit consent for a Signed-off-by.

I am currently doing a set of builds with clang-11 with this patch on top of 5.7-rc4 to make sure that all of the cases I have found work. Once that is done, I'll comment back with a tag.

Quoted 35 lines
Quoted 32 lines
---
 arch/x86/include/asm/bitops.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/include/asm/bitops.h +2 -2
index b392571c1f1d..139122e5b25b 100644
@@ -54,7 +54,7 @@ arch_set_bit(long nr, volatile unsigned long *addr)
 	if (__builtin_constant_p(nr)) {
 		asm volatile(LOCK_PREFIX "orb %1,%0"
 			: CONST_MASK_ADDR(nr, addr)
-			: "iq" (CONST_MASK(nr) & 0xff)
+			: "iq" ((u8)(CONST_MASK(nr) & 0xff))
 			: "memory");
 	} else {
 		asm volatile(LOCK_PREFIX __ASM_SIZE(bts) " %1,%0"
@@ -74,7 +74,7 @@ arch_clear_bit(long nr, volatile unsigned long *addr)
 	if (__builtin_constant_p(nr)) {
 		asm volatile(LOCK_PREFIX "andb %1,%0"
 			: CONST_MASK_ADDR(nr, addr)
-			: "iq" (CONST_MASK(nr) ^ 0xff));
+			: "iq" ((u8)(CONST_MASK(nr) ^ 0xff)));
 	} else {
 		asm volatile(LOCK_PREFIX __ASM_SIZE(btr) " %1,%0"
 			: : RLONG_ADDR(addr), "Ir" (nr) : "memory");
Signature · 1 line
2.26.2.526.g744177e7f7-goog

Cheers,
Nathan

#30 lore raw
In reply to Nathan Chancellor
Quoted 6 lines · Nathan Chancellor
Quoted 2 lines · Nick Desaulniers

This is easily reproducible via Clang+CONFIG_STAGING=y+CONFIG_VT6656=m, or Clang+allyesconfig.

For what it's worth, I don't see this with allyesconfig.

Oops, ok, I'll drop that from the commit message in v2. I was testing with the former.

Quoted 10 lines
Quoted 7 lines

Keep the masking operation to appease sparse (make C=1), add back the cast in order to properly select the proper 8b register alias.

[Nick: reworded]

The offending commit was added in 5.7-rc1; we shouldn't need to Cc stable since this should be picked up as an -rc fix.

Got it, will drop in v2.

Quoted 10 lines
Quoted 9 lines
Quoted 4 lines

Not to split hairs but this is Ilie's diff, he should probably be the author with Sedat's Reported-by/Tested-by.

https://github.com/ClangBuiltLinux/linux/issues/961#issuecomment-608239458

Ooh, you're right. Sorry about that Ilie. I'm usually pretty pedantic about getting that right; my mistake. I'll fix that in v2. As Sedat noted, the issue tracker has been a little quiet on this issue, but I'll note that there are extraordinary circumstances going on in the world these days (COVID) so delays should be anticipated.

Ilie, may I put your authorship and signed off by tag on the V2?

Quoted 3 lines

But eh, it's all a team effort plus that can only happen with Ilie's explicit consent for a Signed-off-by.

Signature · 2 lines
Thanks,
~Nick Desaulniers
#31 lore raw

Hi Nick,

Quoted 1 line

Ilie, may I put your authorship and signed off by tag on the V2?

Yes, of course. With the current global situation I took some time off and didn't follow the latest discussion.

Feel free to credit/rework the code as you see fit.

Thanks,
I.H.

#32 lore raw

PATCH v2 1/1 x86: bitops: fix build regression

From: Ilie Halip <[email protected]>

It turns out that if your config tickles __builtin_constant_p via differences in choices to inline or not, this now produces invalid assembly:

$ cat foo.c
long a(long b, long c) {
  asm("orb\t%1, %0" : "+q"(c): "r"(b));
  return c;
}
$ gcc foo.c
foo.c: Assembler messages:
foo.c:2: Error: `%rax' not allowed with `orb'

The "q" constraint only has meanting on -m32 otherwise is treated as "r".

This is easily reproducible via Clang+CONFIG_STAGING=y+CONFIG_VT6656=m.

Without the cast to u8, gcc and clang will not select low-8-bit registers required for the b suffix on orb and andb, which results in an assembler error. Without the mask, sparse will warn about the upper bytes of the value being truncated.

[Nick: reworded]

---
Changes V1 -> V2:
* change authorship/signed-off-by to Ilie
* add Nathan's Tested by/reviewed by
* update commit message slightly with info sent to HPA.

 arch/x86/include/asm/bitops.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/include/asm/bitops.h +2 -2
index b392571c1f1d..139122e5b25b 100644
@@ -54,7 +54,7 @@ arch_set_bit(long nr, volatile unsigned long *addr)
 	if (__builtin_constant_p(nr)) {
 		asm volatile(LOCK_PREFIX "orb %1,%0"
 			: CONST_MASK_ADDR(nr, addr)
-			: "iq" (CONST_MASK(nr) & 0xff)
+			: "iq" ((u8)(CONST_MASK(nr) & 0xff))
 			: "memory");
 	} else {
 		asm volatile(LOCK_PREFIX __ASM_SIZE(bts) " %1,%0"
@@ -74,7 +74,7 @@ arch_clear_bit(long nr, volatile unsigned long *addr)
 	if (__builtin_constant_p(nr)) {
 		asm volatile(LOCK_PREFIX "andb %1,%0"
 			: CONST_MASK_ADDR(nr, addr)
-			: "iq" (CONST_MASK(nr) ^ 0xff));
+			: "iq" ((u8)(CONST_MASK(nr) ^ 0xff)));
 	} else {
 		asm volatile(LOCK_PREFIX __ASM_SIZE(btr) " %1,%0"
 			: : RLONG_ADDR(addr), "Ir" (nr) : "memory");
Signature · 1 line
2.26.2.526.g744177e7f7-goog
#33 lore raw
In reply to Nick Desaulniers
Quoted 48 lines · Nick Desaulniers

From: Sedat Dilek <[email protected]>

It turns out that if your config tickles __builtin_constant_p via differences in choices to inline or not, this now produces invalid assembly:

$ cat foo.c
long a(long b, long c) {
  asm("orb\t%1, %0" : "+q"(c): "r"(b));
  return c;
}
$ gcc foo.c
foo.c: Assembler messages:
foo.c:2: Error: `%rax' not allowed with `orb'

The "q" constraint only has meanting on -m32 otherwise is treated as "r".

This is easily reproducible via Clang+CONFIG_STAGING=y+CONFIG_VT6656=m, or Clang+allyesconfig.

Keep the masking operation to appease sparse (make C=1), add back the cast in order to properly select the proper 8b register alias.

[Nick: reworded]

---
 arch/x86/include/asm/bitops.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/include/asm/bitops.h +0 -0
index b392571c1f1d..139122e5b25b 100644

arch/x86/include/asm/bitops.h arch_set_bit(long nr, volatile unsigned long *addr) · line 57

Quoted 5 lines
arch/x86/include/asm/bitops.h +1 -1
@@ -54,7 +54,7 @@ arch_set_bit(long nr, volatile unsigned long *addr)
        if (__builtin_constant_p(nr)) {
                asm volatile(LOCK_PREFIX "orb %1,%0"
                        : CONST_MASK_ADDR(nr, addr)
-                       : "iq" (CONST_MASK(nr) & 0xff)
+                       : "iq" ((u8)(CONST_MASK(nr) & 0xff))

I think a better fix would be to make CONST_MASK() return a u8 value rather than have to cast on every use.

Also I question the need for the "q" constraint. It was added in commit 437a0a54 as a workaround for GCC 3.4.4. Now that the minimum GCC version is 4.6, is this still necessary?

--
Brian Gerst

#34 lore raw
Quoted 63 lines · Brian Gerst
Quoted 16 lines · Nick Desaulniers

From: Sedat Dilek <[email protected]>

It turns out that if your config tickles __builtin_constant_p via differences in choices to inline or not, this now produces invalid assembly:

$ cat foo.c
long a(long b, long c) {
  asm("orb\t%1, %0" : "+q"(c): "r"(b));
  return c;
}
$ gcc foo.c
foo.c: Assembler messages:
foo.c:2: Error: `%rax' not allowed with `orb'
Quoted 4 lines

The "q" constraint only has meanting on -m32 otherwise is treated as "r".

This is easily reproducible via

Clang+CONFIG_STAGING=y+CONFIG_VT6656=m,

Quoted 3 lines

or Clang+allyesconfig.

Keep the masking operation to appease sparse (make C=1), add back

the

Quoted 8 lines

cast in order to properly select the proper 8b register alias.

[Nick: reworded]

Link:

https://lore.kernel.org/lkml/[email protected]/

Quoted 8 lines

Review

Quoted 5 lines
---
 arch/x86/include/asm/bitops.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
(quoted diff) +0 -0

b/arch/x86/include/asm/bitops.h

arch/x86/include/asm/bitops.h arch_set_bit(long nr, volatile unsigned long *addr) · line 57

Quoted 9 lines

index b392571c1f1d..139122e5b25b 100644

arch/x86/include/asm/bitops.h +1 -1
@@ -54,7 +54,7 @@ arch_set_bit(long nr, volatile unsigned long *addr)
        if (__builtin_constant_p(nr)) {
                asm volatile(LOCK_PREFIX "orb %1,%0"
                        : CONST_MASK_ADDR(nr, addr)
-                       : "iq" (CONST_MASK(nr) & 0xff)
+                       : "iq" ((u8)(CONST_MASK(nr) & 0xff))

I think a better fix would be to make CONST_MASK() return a u8 value rather than have to cast on every use.

Quoted 6 lines

Also I question the need for the "q" constraint. It was added in commit 437a0a54 as a workaround for GCC 3.4.4. Now that the minimum GCC version is 4.6, is this still necessary?

--
Brian Gerst

Yes, "q" is needed on i386.

Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.
#35 lore raw
Quoted 65 lines · On Thu, May 7, 2020 at 3:02 AM <[email protected]> wrote:
Quoted 63 lines · Brian Gerst
Quoted 16 lines · Nick Desaulniers

From: Sedat Dilek <[email protected]>

It turns out that if your config tickles __builtin_constant_p via differences in choices to inline or not, this now produces invalid assembly:

$ cat foo.c
long a(long b, long c) {
  asm("orb\t%1, %0" : "+q"(c): "r"(b));
  return c;
}
$ gcc foo.c
foo.c: Assembler messages:
foo.c:2: Error: `%rax' not allowed with `orb'
Quoted 4 lines

The "q" constraint only has meanting on -m32 otherwise is treated as "r".

This is easily reproducible via

Clang+CONFIG_STAGING=y+CONFIG_VT6656=m,

Quoted 3 lines

or Clang+allyesconfig.

Keep the masking operation to appease sparse (make C=1), add back

the

Quoted 8 lines

cast in order to properly select the proper 8b register alias.

[Nick: reworded]

Link:

https://lore.kernel.org/lkml/[email protected]/

Quoted 8 lines

Review

Quoted 5 lines
---
 arch/x86/include/asm/bitops.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
(quoted diff) +0 -0

b/arch/x86/include/asm/bitops.h

arch/x86/include/asm/bitops.h arch_set_bit(long nr, volatile unsigned long *addr) · line 57

Quoted 9 lines

index b392571c1f1d..139122e5b25b 100644

arch/x86/include/asm/bitops.h +1 -1
@@ -54,7 +54,7 @@ arch_set_bit(long nr, volatile unsigned long *addr)
        if (__builtin_constant_p(nr)) {
                asm volatile(LOCK_PREFIX "orb %1,%0"
                        : CONST_MASK_ADDR(nr, addr)
-                       : "iq" (CONST_MASK(nr) & 0xff)
+                       : "iq" ((u8)(CONST_MASK(nr) & 0xff))

I think a better fix would be to make CONST_MASK() return a u8 value rather than have to cast on every use.

Quoted 8 lines
Quoted 6 lines

Also I question the need for the "q" constraint. It was added in commit 437a0a54 as a workaround for GCC 3.4.4. Now that the minimum GCC version is 4.6, is this still necessary?

--
Brian Gerst

Yes, "q" is needed on i386.

I think the bug this worked around was that the compiler didn't detect that CONST_MASK(nr) was also constant and doesn't need to be put into a register. The question is does that bug still exist on compiler versions we care about?

--
Brian Gerst

#36 lore raw

From: Brian Gerst

Quoted 1 line

Sent: 07 May 2020 14:32

...

Quoted 4 lines

I think the bug this worked around was that the compiler didn't detect that CONST_MASK(nr) was also constant and doesn't need to be put into a register. The question is does that bug still exist on compiler versions we care about?

Hmmm...
That ought to have been fixed instead of worrying about the fact that an invalid register was used.

Alternatively is there any reason not to use the bts/btc instructions? Yes, I know they'll do wider accesses, but variable bit numbers do. It is also possible that the assembler will support constant bit numbers >= 32 by adding to the address offset.

David

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

#37 lore raw
Quoted 18 lines · David Laight

From: Brian Gerst

Quoted 1 line

Sent: 07 May 2020 14:32

...

Quoted 1 line

I think the bug this worked around was that the compiler didn't

detect

Quoted 3 lines

that CONST_MASK(nr) was also constant and doesn't need to be put into a register. The question is does that bug still exist on compiler versions we care about?

Hmmm...
That ought to have been fixed instead of worrying about the fact that an invalid register was used.

Alternatively is there any reason not to use the bts/btc instructions? Yes, I know they'll do wider accesses, but variable bit numbers do. It is also possible that the assembler will support constant bit numbers >= 32 by adding to the address offset.

Quoted 6 lines

David

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

They're slower, and for unaligned locked fields can be severely so.

Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.
#38 lore raw
In reply to Brian Gerst
Quoted 80 lines · Brian Gerst
Quoted 2 lines · On Thu, May 7, 2020 at 3:02 AM <[email protected]> wrote:

On May 6, 2020 11:18:09 PM PDT, Brian Gerst <[email protected]>

Quoted 18 lines · wrote:
Quoted 18 lines
Quoted 16 lines · Nick Desaulniers

From: Sedat Dilek <[email protected]>

It turns out that if your config tickles __builtin_constant_p via differences in choices to inline or not, this now produces invalid assembly:

$ cat foo.c
long a(long b, long c) {
  asm("orb\t%1, %0" : "+q"(c): "r"(b));
  return c;
}
$ gcc foo.c
foo.c: Assembler messages:
foo.c:2: Error: `%rax' not allowed with `orb'
Quoted 1 line
Quoted 1 line
Quoted 1 line

The "q" constraint only has meanting on -m32 otherwise is treated

as

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

"r".

This is easily reproducible via

Clang+CONFIG_STAGING=y+CONFIG_VT6656=m,

Quoted 3 lines

or Clang+allyesconfig.

Keep the masking operation to appease sparse (make C=1), add

back

Quoted 11 lines
Quoted 11 lines

the

Quoted 8 lines

cast in order to properly select the proper 8b register alias.

[Nick: reworded]

Link:

https://lore.kernel.org/lkml/[email protected]/

Quoted 1 line
Fixes 1651e700664b4 ("x86: Fix bitops.h warning with a moved

cast")

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
---
 arch/x86/include/asm/bitops.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 6 lines
Quoted 6 lines

Review

Quoted 1 line
(quoted diff) +0 -0

b/arch/x86/include/asm/bitops.h

arch/x86/include/asm/bitops.h arch_set_bit(long nr, volatile unsigned long

Quoted 4 lines

index b392571c1f1d..139122e5b25b 100644

arch/x86/include/asm/bitops.h +0 -0
@@ -54,7 +54,7 @@ arch_set_bit(long nr, volatile unsigned long

*addr)

Quoted 9 lines
Quoted 9 lines

arch/x86/include/asm/bitops.h arch_set_bit(long nr, volatile unsigned long *addr) · line 57

Quoted 5 lines
arch/x86/include/asm/bitops.h +1 -1
        if (__builtin_constant_p(nr)) {
                asm volatile(LOCK_PREFIX "orb %1,%0"
                        : CONST_MASK_ADDR(nr, addr)
-                       : "iq" (CONST_MASK(nr) & 0xff)
+                       : "iq" ((u8)(CONST_MASK(nr) & 0xff))

I think a better fix would be to make CONST_MASK() return a u8 value rather than have to cast on every use.

Quoted 8 lines
Quoted 6 lines

Also I question the need for the "q" constraint. It was added in commit 437a0a54 as a workaround for GCC 3.4.4. Now that the minimum GCC version is 4.6, is this still necessary?

--
Brian Gerst

Yes, "q" is needed on i386.

Quoted 7 lines

I think the bug this worked around was that the compiler didn't detect that CONST_MASK(nr) was also constant and doesn't need to be put into a register. The question is does that bug still exist on compiler versions we care about?

--
Brian Gerst

The compiler is free to do that, including for legit reasons (common subexpression elimination, especially.) So yes.

Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.
#39 lore raw
In reply to Brian Gerst

From: Brian Gerst

Quoted 1 line

Sent: 07 May 2020 07:18

...

Quoted 11 lines

arch/x86/include/asm/bitops.h arch_set_bit(long nr, volatile unsigned long *addr) · line 57

Quoted 8 lines
arch/x86/include/asm/bitops.h +1 -1
@@ -54,7 +54,7 @@ arch_set_bit(long nr, volatile unsigned long *addr)
        if (__builtin_constant_p(nr)) {
                asm volatile(LOCK_PREFIX "orb %1,%0"
                        : CONST_MASK_ADDR(nr, addr)
-                       : "iq" (CONST_MASK(nr) & 0xff)
+                       : "iq" ((u8)(CONST_MASK(nr) & 0xff))

I think a better fix would be to make CONST_MASK() return a u8 value rather than have to cast on every use.

Or assign to a local variable - then it doesn't matter how the value is actually calculated. So:

			u8 mask = CONST_MASK(nr);
			asm volatile(LOCK_PREFIX "orb %1,%0"
				: CONST_MASK_ADDR(nr, addr)
				: "iq" mask

	David

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

#40 lore raw
Quoted 25 lines · David Laight

From: Brian Gerst

Quoted 1 line

Sent: 07 May 2020 07:18

...

Quoted 3 lines

arch/x86/include/asm/bitops.h arch_set_bit(long nr, volatile unsigned long

Quoted 3 lines
arch/x86/include/asm/bitops.h +0 -0
@@ -54,7 +54,7 @@ arch_set_bit(long nr, volatile unsigned long

*addr)

Quoted 8 lines

arch/x86/include/asm/bitops.h arch_set_bit(long nr, volatile unsigned long *addr) · line 57

Quoted 5 lines
arch/x86/include/asm/bitops.h +1 -1
        if (__builtin_constant_p(nr)) {
                asm volatile(LOCK_PREFIX "orb %1,%0"
                        : CONST_MASK_ADDR(nr, addr)
-                       : "iq" (CONST_MASK(nr) & 0xff)
+                       : "iq" ((u8)(CONST_MASK(nr) & 0xff))

I think a better fix would be to make CONST_MASK() return a u8 value rather than have to cast on every use.

Or assign to a local variable - then it doesn't matter how the value is actually calculated. So:

			u8 mask = CONST_MASK(nr);
			asm volatile(LOCK_PREFIX "orb %1,%0"
				: CONST_MASK_ADDR(nr, addr)
				: "iq" mask

	David
Quoted 4 lines

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

"const u8" please...

Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.
#41 lore raw

From: [email protected]

Quoted 25 lines

Sent: 07 May 2020 08:59

Quoted 23 lines · David Laight

From: Brian Gerst

Quoted 1 line

Sent: 07 May 2020 07:18

...

Quoted 3 lines

arch/x86/include/asm/bitops.h arch_set_bit(long nr, volatile unsigned long

Quoted 3 lines
arch/x86/include/asm/bitops.h +0 -0
@@ -54,7 +54,7 @@ arch_set_bit(long nr, volatile unsigned long

*addr)

Quoted 8 lines

arch/x86/include/asm/bitops.h arch_set_bit(long nr, volatile unsigned long *addr) · line 57

Quoted 5 lines
arch/x86/include/asm/bitops.h +1 -1
        if (__builtin_constant_p(nr)) {
                asm volatile(LOCK_PREFIX "orb %1,%0"
                        : CONST_MASK_ADDR(nr, addr)
-                       : "iq" (CONST_MASK(nr) & 0xff)
+                       : "iq" ((u8)(CONST_MASK(nr) & 0xff))

I think a better fix would be to make CONST_MASK() return a u8 value rather than have to cast on every use.

Or assign to a local variable - then it doesn't matter how the value is actually calculated. So:

			u8 mask = CONST_MASK(nr);
			asm volatile(LOCK_PREFIX "orb %1,%0"
				: CONST_MASK_ADDR(nr, addr)
				: "iq" mask
Quoted 8 lines
Quoted 6 lines

David

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

"const u8" please...

Why, just a waste of disk space.

David

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

#42 lore raw
Quoted 38 lines · David Laight

From: [email protected]

Quoted 2 lines

Sent: 07 May 2020 08:59
On May 7, 2020 12:44:44 AM PDT, David Laight

Quoted 7 lines · <[email protected]> wrote:
Quoted 7 lines

From: Brian Gerst

Quoted 1 line

Sent: 07 May 2020 07:18

...

Quoted 3 lines

arch/x86/include/asm/bitops.h arch_set_bit(long nr, volatile unsigned long

Quoted 3 lines
arch/x86/include/asm/bitops.h +0 -0
@@ -54,7 +54,7 @@ arch_set_bit(long nr, volatile unsigned long

*addr)

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

arch/x86/include/asm/bitops.h arch_set_bit(long nr, volatile unsigned long *addr) · line 57

Quoted 5 lines
arch/x86/include/asm/bitops.h +1 -1
        if (__builtin_constant_p(nr)) {
                asm volatile(LOCK_PREFIX "orb %1,%0"
                        : CONST_MASK_ADDR(nr, addr)
-                       : "iq" (CONST_MASK(nr) & 0xff)
+                       : "iq" ((u8)(CONST_MASK(nr) & 0xff))

I think a better fix would be to make CONST_MASK() return a u8

value

Quoted 9 lines
Quoted 9 lines
Quoted 1 line

rather than have to cast on every use.

Or assign to a local variable - then it doesn't matter how the value is actually calculated. So:

			u8 mask = CONST_MASK(nr);
			asm volatile(LOCK_PREFIX "orb %1,%0"
				: CONST_MASK_ADDR(nr, addr)
				: "iq" mask
Quoted 4 lines
Quoted 4 lines

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton

Keynes,

Quoted 4 lines
Quoted 2 lines

MK1 1PT, UK
Registration No: 1397386 (Wales)

"const u8" please...

Quoted 8 lines

Why, just a waste of disk space.

David

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

Descriptive.

Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.
#43 lore raw
In reply to David Laight
Quoted 9 lines · David Laight

From: Brian Gerst

Quoted 4 lines

Sent: 07 May 2020 07:18

I think a better fix would be to make CONST_MASK() return a u8 value rather than have to cast on every use.

Or assign to a local variable - then it doesn't matter how the value is actually calculated. So:

                        u8 mask = CONST_MASK(nr);

Another case with negation won't work like this I believe.
So, I thin kthe patch we have is good enough, no need to seek for an evil.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#44 lore raw
In reply to Brian Gerst
Quoted 7 lines · Brian Gerst

I think a better fix would be to make CONST_MASK() return a u8 value rather than have to cast on every use.

Also I question the need for the "q" constraint. It was added in commit 437a0a54 as a workaround for GCC 3.4.4. Now that the minimum GCC version is 4.6, is this still necessary?

TL;DR yes

ah, thanks for the git archeology, it's useful. I don't think this is a compiler bug however, just the compiler being more strict that the b suffix on orb requires a 8b register operand. For 32b x86, q asm constraint is required, because not all GPR's had lower 8b register aliases, as Arnd found and HPA noted as well.

I like your suggested change to CONST_MASK, and will send that in a bit.

Signature · 2 lines
Thanks,
~Nick Desaulniers