Skip to contentSkip to conversation

x86/atomic: Improvements and simplifications to assembly constraints

PATCH Superseded Andrew Cooper 2 participants 6 messages Nov 21, 2018 – Nov 22, 2018

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/atomic: Improvements and simplifications to assembly constraints

* Some of the single-byte versions specify "=q" as the output. AFAICT, there was not a legitimate reason to restrict the use of %esi/%edi in the 32-bit build. Either way, in 64-bit, it is equivelent to "=r".
* Constraints in the form "=r" (x) : "0" (x) can be folded to just "+r" (x)
* Switch to using named parameters (mostly for legibility) which in particular helps with...
* __xchg(), __cmpxchg() and __cmpxchg_user() modify their memory operand, so must list it as an output operand. This only works because they each have a memory clobber to give the construct full compiler-barrier properties.
* Every memory operand has an explicit known size. Letting the compiler see the real size rather than obscuring it with __xg() allows for the removal of the instruction size suffixes without introducing ambiguity.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

Interestingly, switching to use output memory operands has the following perturbance in the build:

add/remove: 0/0 grow/shrink: 3/5 up/down: 70/-124 (-54)
Function old new delta
do_mmu_update 7041 7101 +60

  mctelem_process_deferred                     234     242      +8
  cpufreq_governor_dbs                         851     853      +2
  _set_status                                  162     161      -1
  create_irq                                   325     323      -2
  do_tmem_put                                 2066    2062      -4
  task_switch_load_seg                         892     884      -8

_get_page_type 6057 5948 -109

but as far as I can tell, it is exclusively down to different register scheduling choices.

---
 xen/include/asm-x86/system.h        | 99 +++++++++++++++++--------------------
 xen/include/asm-x86/x86_64/system.h | 24 ++++-----
 2 files changed, 57 insertions(+), 66 deletions(-)
xen/include/asm-x86/system.h +44 -55
index 483cd20..8764e31 100644
@@ -23,9 +23,6 @@
 #define xchg(ptr,v) \
     ((__typeof__(*(ptr)))__xchg((unsigned long)(v),(ptr),sizeof(*(ptr))))
 
-struct __xchg_dummy { unsigned long a[100]; };
-#define __xg(x) ((volatile struct __xchg_dummy *)(x))
-
 #include <asm/x86_64/system.h>
 
 /*
@@ -39,28 +36,24 @@ static always_inline unsigned long __xchg(
     switch ( size )
     {
     case 1:
-        asm volatile ( "xchgb %b0,%1"
-                       : "=q" (x)
-                       : "m" (*__xg(ptr)), "0" (x)
-                       : "memory" );
+        asm volatile ( "xchg %b[x], %[ptr]"
+                       : [x] "+r" (x), [ptr] "+m" (*(uint8_t *)ptr)
+                       :: "memory" );
         break;
     case 2:
-        asm volatile ( "xchgw %w0,%1"
-                       : "=r" (x)
-                       : "m" (*__xg(ptr)), "0" (x)
-                       : "memory" );
+        asm volatile ( "xchg %w[x], %[ptr]"
+                       : [x] "+r" (x), [ptr] "+m" (*(uint16_t *)ptr)
+                       :: "memory" );
         break;
     case 4:
-        asm volatile ( "xchgl %k0,%1"
-                       : "=r" (x)
-                       : "m" (*__xg(ptr)), "0" (x)
-                       : "memory" );
+        asm volatile ( "xchg %k[x], %[ptr]"
+                       : [x] "+r" (x), [ptr] "+m" (*(uint32_t *)ptr)
+                       :: "memory" );
         break;
     case 8:
-        asm volatile ( "xchgq %0,%1"
-                       : "=r" (x)
-                       : "m" (*__xg(ptr)), "0" (x)
-                       : "memory" );
+        asm volatile ( "xchg %q[x], %[ptr]"
+                       : [x] "+r" (x), [ptr] "+m" (*(uint64_t *)ptr)
+                       :: "memory" );
         break;
     }
     return x;
@@ -79,31 +72,27 @@ static always_inline unsigned long __cmpxchg(
     switch ( size )
     {
     case 1:
-        asm volatile ( "lock; cmpxchgb %b1,%2"
-                       : "=a" (prev)
-                       : "q" (new), "m" (*__xg(ptr)),
-                       "0" (old)
+        asm volatile ( "lock; cmpxchg %b[new], %[ptr]"
+                       : "=a" (prev), [ptr] "+m" (*(uint8_t *)ptr)
+                       : [new] "r" (new), "0" (old)
                        : "memory" );
         return prev;
     case 2:
-        asm volatile ( "lock; cmpxchgw %w1,%2"
-                       : "=a" (prev)
-                       : "r" (new), "m" (*__xg(ptr)),
-                       "0" (old)
+        asm volatile ( "lock; cmpxchg %w[new], %[ptr]"
+                       : "=a" (prev), [ptr] "+m" (*(uint16_t *)ptr)
+                       : [new] "r" (new), "0" (old)
                        : "memory" );
         return prev;
     case 4:
-        asm volatile ( "lock; cmpxchgl %k1,%2"
-                       : "=a" (prev)
-                       : "r" (new), "m" (*__xg(ptr)),
-                       "0" (old)
+        asm volatile ( "lock; cmpxchg %k[new], %[ptr]"
+                       : "=a" (prev), [ptr] "+m" (*(uint32_t *)ptr)
+                       : [new] "r" (new), "0" (old)
                        : "memory" );
         return prev;
     case 8:
-        asm volatile ( "lock; cmpxchgq %1,%2"
-                       : "=a" (prev)
-                       : "r" (new), "m" (*__xg(ptr)),
-                       "0" (old)
+        asm volatile ( "lock; cmpxchg %q[new], %[ptr]"
+                       : "=a" (prev), [ptr] "+m" (*(uint64_t *)ptr)
+                       : [new] "r" (new), "0" (old)
                        : "memory" );
         return prev;
     }
@@ -118,24 +107,24 @@ static always_inline unsigned long cmpxchg_local_(
     switch ( size )
     {
     case 1:
-        asm volatile ( "cmpxchgb %b2, %1"
-                       : "=a" (prev), "+m" (*(uint8_t *)ptr)
-                       : "q" (new), "0" (old) );
+        asm volatile ( "cmpxchg %b[new], %[ptr]"
+                       : "=a" (prev), [ptr] "+m" (*(uint8_t *)ptr)
+                       : [new] "r" (new), "0" (old) );
         break;
     case 2:
-        asm volatile ( "cmpxchgw %w2, %1"
-                       : "=a" (prev), "+m" (*(uint16_t *)ptr)
-                       : "r" (new), "0" (old) );
+        asm volatile ( "cmpxchg %w[new], %[ptr]"
+                       : "=a" (prev), [ptr] "+m" (*(uint16_t *)ptr)
+                       : [new] "r" (new), "0" (old) );
         break;
     case 4:
-        asm volatile ( "cmpxchgl %k2, %1"
-                       : "=a" (prev), "+m" (*(uint32_t *)ptr)
-                       : "r" (new), "0" (old) );
+        asm volatile ( "cmpxchg %k[new], %[ptr]"
+                       : "=a" (prev), [ptr] "+m" (*(uint32_t *)ptr)
+                       : [new] "r" (new), "0" (old) );
         break;
     case 8:
-        asm volatile ( "cmpxchgq %2, %1"
-                       : "=a" (prev), "+m" (*(uint64_t *)ptr)
-                       : "r" (new), "0" (old) );
+        asm volatile ( "cmpxchg %q[new], %[ptr]"
+                       : "=a" (prev), [ptr] "+m" (*(uint64_t *)ptr)
+                       : [new] "r" (new), "0" (old) );
         break;
     }
 
@@ -161,23 +150,23 @@ static always_inline unsigned long __xadd(
     switch ( size )
     {
     case 1:
-        asm volatile ( "lock; xaddb %b0,%1"
-                       : "+r" (v), "+m" (*__xg(ptr))
+        asm volatile ( "lock; xadd %b[v], %[ptr]"
+                       : [v] "+r" (v), [ptr] "+m" (*(uint8_t *)ptr)
                        :: "memory");
         return v;
     case 2:
-        asm volatile ( "lock; xaddw %w0,%1"
-                       : "+r" (v), "+m" (*__xg(ptr))
+        asm volatile ( "lock; xadd %w[v], %[ptr]"
+                       : [v] "+r" (v), [ptr] "+m" (*(uint16_t *)ptr)
                        :: "memory");
         return v;
     case 4:
-        asm volatile ( "lock; xaddl %k0,%1"
-                       : "+r" (v), "+m" (*__xg(ptr))
+        asm volatile ( "lock; xadd %k[v], %[ptr]"
+                       : [v] "+r" (v), [ptr] "+m" (*(uint32_t *)ptr)
                        :: "memory");
         return v;
     case 8:
-        asm volatile ( "lock; xaddq %q0,%1"
-                       : "+r" (v), "+m" (*__xg(ptr))
+        asm volatile ( "lock; xadd %q[v], %[ptr]"
+                       : [v] "+r" (v), [ptr] "+m" (*(uint64_t *)ptr)
                        :: "memory");
 
         return v;
xen/include/asm-x86/x86_64/system.h +13 -11
index fae57ba..9c68f4f 100644
@@ -25,7 +25,7 @@ static always_inline __uint128_t __cmpxchg16b(
 
     /* Don't use "=A" here - clang can't deal with that. */
     asm volatile ( "lock; cmpxchg16b %2"
-                   : "=d" (prev.hi), "=a" (prev.lo), "+m" (*__xg(ptr))
+                   : "=d" (prev.hi), "=a" (prev.lo), "+m" (*(__uint128_t *)ptr)
                    : "c" (new.hi), "b" (new.lo), "0" (old.hi), "1" (old.lo) );
 
     return prev.raw;
@@ -63,36 +63,38 @@ static always_inline __uint128_t cmpxchg16b_local_(
  * If no fault occurs then _o is updated to the value we saw at _p. If this
  * is the same as the initial value of _o then _n is written to location _p.
  */
-#define __cmpxchg_user(_p,_o,_n,_isuff,_oppre,_regtype)                 \
+#define __cmpxchg_user(_p, _o, _n, _oppre)                              \
     stac();                                                             \
     asm volatile (                                                      \
-        "1: lock; cmpxchg"_isuff" %"_oppre"2,%3\n"                      \
+        "1: lock; cmpxchg %"_oppre"[new], %[ptr]\n"                     \
         "2:\n"                                                          \
         ".section .fixup,\"ax\"\n"                                      \
         "3:     movl $1,%1\n"                                           \
         "       jmp 2b\n"                                               \
         ".previous\n"                                                   \
         _ASM_EXTABLE(1b, 3b)                                            \
-        : "=a" (_o), "=r" (_rc)                                         \
-        : _regtype (_n), "m" (*__xg((volatile void *)_p)), "0" (_o), "1" (0) \
+        : "+a" (_o), "=r" (_rc),                                        \
+          [ptr] "+m" (*(volatile typeof(*(_p)) *)(_p))                  \
+        : [new] "r" (_n), "1" (0)                                       \
         : "memory");                                                    \
     clac()
 
-#define cmpxchg_user(_p,_o,_n)                                          \
+#define cmpxchg_user(_p, _o, _n)                                        \
 ({                                                                      \
     int _rc;                                                            \
-    switch ( sizeof(*(_p)) ) {                                          \
+    switch ( sizeof(*(_p)) )                                            \
+    {                                                                   \
     case 1:                                                             \
-        __cmpxchg_user(_p,_o,_n,"b","b","q");                           \
+        __cmpxchg_user(_p, _o, _n, "b");                                \
         break;                                                          \
     case 2:                                                             \
-        __cmpxchg_user(_p,_o,_n,"w","w","r");                           \
+        __cmpxchg_user(_p, _o, _n, "w");                                \
         break;                                                          \
     case 4:                                                             \
-        __cmpxchg_user(_p,_o,_n,"l","k","r");                           \
+        __cmpxchg_user(_p, _o, _n, "k");                                \
         break;                                                          \
     case 8:                                                             \
-        __cmpxchg_user(_p,_o,_n,"q","","r");                            \
+        __cmpxchg_user(_p, _o, _n, "q");                                \
         break;                                                          \
     }                                                                   \
     _rc;                                                                \
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 21.11.18 at 20:37, <[email protected]> wrote:

* Some of the single-byte versions specify "=q" as the output. AFAICT, there was not a legitimate reason to restrict the use of %esi/%edi in the 32-bit build. Either way, in 64-bit, it is equivelent to "=r".

I'm confused about the 32-bit part here: Of course it was necessary to restrict the compiler to the low 4 registers in that case. It's just not clear to me whether you've just written it down wrongly, or whether you indeed think the way it reads to me.

Quoted 43 lines

* Constraints in the form "=r" (x) : "0" (x) can be folded to just "+r" (x)
* Switch to using named parameters (mostly for legibility) which in particular helps with...
* __xchg(), __cmpxchg() and __cmpxchg_user() modify their memory operand, so must list it as an output operand. This only works because they each have a memory clobber to give the construct full compiler-barrier properties.
* Every memory operand has an explicit known size. Letting the compiler see the real size rather than obscuring it with __xg() allows for the removal of the instruction size suffixes without introducing ambiguity.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

Interestingly, switching to use output memory operands has the following perturbance in the build:

add/remove: 0/0 grow/shrink: 3/5 up/down: 70/-124 (-54)
Function old new delta
do_mmu_update 7041 7101 +60

  mctelem_process_deferred                     234     242      +8
  cpufreq_governor_dbs                         851     853      +2
  _set_status                                  162     161      -1
  create_irq                                   325     323      -2
  do_tmem_put                                 2066    2062      -4
  task_switch_load_seg                         892     884      -8

_get_page_type 6057 5948 -109

but as far as I can tell, it is exclusively down to different register scheduling choices.

---
 xen/include/asm-x86/system.h        | 99 
+++++++++++++++++--------------------
 xen/include/asm-x86/x86_64/system.h | 24 ++++-----
 2 files changed, 57 insertions(+), 66 deletions(-)
xen/include/asm-x86/system.h +0 -0
index 483cd20..8764e31 100644

xen/include/asm-x86/system.h line 27

Quoted 5 lines
xen/include/asm-x86/system.h +0 -2
@@ -23,9 +23,6 @@
 #define xchg(ptr,v) \
     ((__typeof__(*(ptr)))__xchg((unsigned long)(v),(ptr),sizeof(*(ptr))))
 
-struct __xchg_dummy { unsigned long a[100]; };
-#define __xg(x) ((volatile struct __xchg_dummy *)(x))

I never fully understood why we have this, so I'm happy to see it go away. I see it has gone away in Linux back in 2.6.36.

xen/include/asm-x86/system.h static always_inline unsigned long __cmpxchg( · line 77

Quoted 11 lines
xen/include/asm-x86/system.h +3 -4
@@ -79,31 +72,27 @@ static always_inline unsigned long __cmpxchg(
     switch ( size )
     {
     case 1:
-        asm volatile ( "lock; cmpxchgb %b1,%2"
-                       : "=a" (prev)
-                       : "q" (new), "m" (*__xg(ptr)),
-                       "0" (old)
+        asm volatile ( "lock; cmpxchg %b[new], %[ptr]"
+                       : "=a" (prev), [ptr] "+m" (*(uint8_t *)ptr)
+                       : [new] "r" (new), "0" (old)

Any reason you retain the reference by number in the input constraint here, rather than giving its corresponding output one a name?

Also since you're playing with this anyway - is there a need to retain the bogus ; after "lock"?

xen/include/asm-x86/x86_64/system.h static always_inline __uint128_t __cmpxchg16b( · line 27

Quoted 6 lines
xen/include/asm-x86/x86_64/system.h +0 -0
@@ -25,7 +25,7 @@ static always_inline __uint128_t __cmpxchg16b(
 
     /* Don't use "=A" here - clang can't deal with that. */
     asm volatile ( "lock; cmpxchg16b %2"

Any reason not to change this to named operands as well?

xen/include/asm-x86/x86_64/system.h static always_inline __uint128_t cmpxchg16b_local_( · line 72

Quoted 13 lines
xen/include/asm-x86/x86_64/system.h +2 -2
@@ -63,36 +63,38 @@ static always_inline __uint128_t cmpxchg16b_local_(
  * If no fault occurs then _o is updated to the value we saw at _p. If this
  * is the same as the initial value of _o then _n is written to location _p.
  */
-#define __cmpxchg_user(_p,_o,_n,_isuff,_oppre,_regtype)                 \
+#define __cmpxchg_user(_p, _o, _n, _oppre)                              \
     stac();                                                             \
     asm volatile (                                                      \
-        "1: lock; cmpxchg"_isuff" %"_oppre"2,%3\n"                      \
+        "1: lock; cmpxchg %"_oppre"[new], %[ptr]\n"                     \
         "2:\n"                                                          \
         ".section .fixup,\"ax\"\n"                                      \
         "3:     movl $1,%1\n"                                           \

Any what about this?

xen/include/asm-x86/x86_64/system.h static always_inline __uint128_t cmpxchg16b_local_( · line 77

Quoted 7 lines
xen/include/asm-x86/x86_64/system.h +2 -2
         "       jmp 2b\n"                                               \
         ".previous\n"                                                   \
         _ASM_EXTABLE(1b, 3b)                                            \
-        : "=a" (_o), "=r" (_rc)                                         \
-        : _regtype (_n), "m" (*__xg((volatile void *)_p)), "0" (_o), "1" (0) \
+        : "+a" (_o), "=r" (_rc),                                        \
+          [ptr] "+m" (*(volatile typeof(*(_p)) *)(_p))                  \

Does casting to add "volatile" here really make any difference, considering the asm() itself is a volatile one and has a memory clobber?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 8 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 21.11.18 at 20:37, <[email protected]> wrote:

* Some of the single-byte versions specify "=q" as the output. AFAICT, there was not a legitimate reason to restrict the use of %esi/%edi in the 32-bit build. Either way, in 64-bit, it is equivelent to "=r".

I'm confused about the 32-bit part here: Of course it was necessary to restrict the compiler to the low 4 registers in that case. It's just not clear to me whether you've just written it down wrongly, or whether you indeed think the way it reads to me.

Wait - are you saying that the combination of "=r" and %b0 would actually fail to build if the compiler happened to chose %edi/%esi?

Now that you point it out, I can see why %esi/%edi aren't actually encodable in this circumstance, but surely the fact that the compiler has to pick a byte register means that it wouldn't end up choosing these?

Quoted 47 lines
Quoted 47 lines

* Constraints in the form "=r" (x) : "0" (x) can be folded to just "+r" (x)
* Switch to using named parameters (mostly for legibility) which in particular helps with...
* __xchg(), __cmpxchg() and __cmpxchg_user() modify their memory operand, so must list it as an output operand. This only works because they each have a memory clobber to give the construct full compiler-barrier properties.
* Every memory operand has an explicit known size. Letting the compiler see the real size rather than obscuring it with __xg() allows for the removal of the instruction size suffixes without introducing ambiguity.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

Interestingly, switching to use output memory operands has the following perturbance in the build:

add/remove: 0/0 grow/shrink: 3/5 up/down: 70/-124 (-54)
Function old new delta
do_mmu_update 7041 7101 +60

  mctelem_process_deferred                     234     242      +8
  cpufreq_governor_dbs                         851     853      +2
  _set_status                                  162     161      -1
  create_irq                                   325     323      -2
  do_tmem_put                                 2066    2062      -4
  task_switch_load_seg                         892     884      -8

_get_page_type 6057 5948 -109

but as far as I can tell, it is exclusively down to different register scheduling choices.

---
 xen/include/asm-x86/system.h        | 99 
+++++++++++++++++--------------------
 xen/include/asm-x86/x86_64/system.h | 24 ++++-----
 2 files changed, 57 insertions(+), 66 deletions(-)
xen/include/asm-x86/system.h +0 -0
index 483cd20..8764e31 100644
@@ -23,9 +23,6 @@
 #define xchg(ptr,v) \
     ((__typeof__(*(ptr)))__xchg((unsigned long)(v),(ptr),sizeof(*(ptr))))
 
Quoted 4 lines

xen/include/asm-x86/system.h line 27

Quoted 2 lines
xen/include/asm-x86/system.h +0 -2
-struct __xchg_dummy { unsigned long a[100]; };
-#define __xg(x) ((volatile struct __xchg_dummy *)(x))

I never fully understood why we have this, so I'm happy to see it go away. I see it has gone away in Linux back in 2.6.36.

I've been trying to get rid of it for several years now.  I'm glad to see it gone.

Quoted 15 lines

xen/include/asm-x86/system.h static always_inline unsigned long __cmpxchg( · line 77

Quoted 12 lines
xen/include/asm-x86/system.h +3 -4
@@ -79,31 +72,27 @@ static always_inline unsigned long __cmpxchg(
     switch ( size )
     {
     case 1:
-        asm volatile ( "lock; cmpxchgb %b1,%2"
-                       : "=a" (prev)
-                       : "q" (new), "m" (*__xg(ptr)),
-                       "0" (old)
+        asm volatile ( "lock; cmpxchg %b[new], %[ptr]"
+                       : "=a" (prev), [ptr] "+m" (*(uint8_t *)ptr)
+                       : [new] "r" (new), "0" (old)

Any reason you retain the reference by number in the input constraint here, rather than giving its corresponding output one a name?

Not specifically.  I suppose this is doable because the constraint is an explicitly register.

Quoted 3 lines

Also since you're playing with this anyway - is there a need to retain the bogus ; after "lock"?

Ok.

Quoted 8 lines

xen/include/asm-x86/x86_64/system.h static always_inline __uint128_t __cmpxchg16b( · line 27

Quoted 7 lines
xen/include/asm-x86/x86_64/system.h +0 -0
@@ -25,7 +25,7 @@ static always_inline __uint128_t __cmpxchg16b(
 
     /* Don't use "=A" here - clang can't deal with that. */
     asm volatile ( "lock; cmpxchg16b %2"

Any reason not to change this to named operands as well?

Simply code perturbance, and the fact that it isn't exactly ambiguous to begin with.  I can change it.

Quoted 15 lines
Quoted 4 lines
xen/include/asm-x86/x86_64/system.h +0 -0
@@ -63,36 +63,38 @@ static always_inline __uint128_t cmpxchg16b_local_(
  * If no fault occurs then _o is updated to the value we saw at _p. If this
  * is the same as the initial value of _o then _n is written to location _p.

xen/include/asm-x86/x86_64/system.h static always_inline __uint128_t cmpxchg16b_local_( · line 72

Quoted 10 lines
xen/include/asm-x86/x86_64/system.h +2 -2
  */
-#define __cmpxchg_user(_p,_o,_n,_isuff,_oppre,_regtype)                 \
+#define __cmpxchg_user(_p, _o, _n, _oppre)                              \
     stac();                                                             \
     asm volatile (                                                      \
-        "1: lock; cmpxchg"_isuff" %"_oppre"2,%3\n"                      \
+        "1: lock; cmpxchg %"_oppre"[new], %[ptr]\n"                     \
         "2:\n"                                                          \
         ".section .fixup,\"ax\"\n"                                      \
         "3:     movl $1,%1\n"                                           \

Any what about this?

I'm certain that I fix that at one point.  I must have lost it in a rebase.

Quoted 11 lines

xen/include/asm-x86/x86_64/system.h static always_inline __uint128_t cmpxchg16b_local_( · line 77

Quoted 8 lines
xen/include/asm-x86/x86_64/system.h +2 -2
         "       jmp 2b\n"                                               \
         ".previous\n"                                                   \
         _ASM_EXTABLE(1b, 3b)                                            \
-        : "=a" (_o), "=r" (_rc)                                         \
-        : _regtype (_n), "m" (*__xg((volatile void *)_p)), "0" (_o), "1" (0) \
+        : "+a" (_o), "=r" (_rc),                                        \
+          [ptr] "+m" (*(volatile typeof(*(_p)) *)(_p))                  \

Does casting to add "volatile" here really make any difference, considering the asm() itself is a volatile one and has a memory clobber?

Yes.  mod_l1_entry() hits a BUG() without it.

Until I understand why, I purposefully didn't change the volatility of any of these constructs in what is otherwise a cleanup patch.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 22.11.18 at 13:38, <[email protected]> wrote:
Quoted 4 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 21.11.18 at 20:37, <[email protected]> wrote:

* Some of the single-byte versions specify "=q" as the output. AFAICT, there was not a legitimate reason to restrict the use of %esi/%edi in the 32-bit build. Either way, in 64-bit, it is equivelent to "=r".

Quoted 7 lines
Quoted 4 lines

I'm confused about the 32-bit part here: Of course it was necessary to restrict the compiler to the low 4 registers in that case. It's just not clear to me whether you've just written it down wrongly, or whether you indeed think the way it reads to me.

Wait - are you saying that the combination of "=r" and %b0 would actually fail to build if the compiler happened to chose %edi/%esi?

Of course: It would produce %sil / %dil, which exist only in 64-bit mode.

Quoted 3 lines

Now that you point it out, I can see why %esi/%edi aren't actually encodable in this circumstance, but surely the fact that the compiler has to pick a byte register means that it wouldn't end up choosing these?

How does it know it needs to pick a byte register when your constraint is "r"? That's what "q" is for.

Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
xen/include/asm-x86/system.h +0 -2
@@ -79,31 +72,27 @@ static always_inline unsigned long __cmpxchg(
     switch ( size )
     {
     case 1:
-        asm volatile ( "lock; cmpxchgb %b1,%2"
-                       : "=a" (prev)
Quoted 8 lines

xen/include/asm-x86/system.h static always_inline unsigned long __cmpxchg( · line 77

Quoted 5 lines
xen/include/asm-x86/system.h +3 -2
-                       : "q" (new), "m" (*__xg(ptr)),
-                       "0" (old)
+        asm volatile ( "lock; cmpxchg %b[new], %[ptr]"
+                       : "=a" (prev), [ptr] "+m" (*(uint8_t *)ptr)
+                       : [new] "r" (new), "0" (old)

Any reason you retain the reference by number in the input constraint here, rather than giving its corresponding output one a name?

Quoted 2 lines

Not specifically. I suppose this is doable because the constraint is an explicitly register.

I don't understand: What does register or no have to do with it? Did you perhaps misunderstand? I'm asking for [prev] "a" (prev) and then "[prev]" (old).

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
xen/include/asm-x86/x86_64/system.h +2 -2
         "       jmp 2b\n"                                               \
         ".previous\n"                                                   \
         _ASM_EXTABLE(1b, 3b)                                            \
-        : "=a" (_o), "=r" (_rc)                                         \
-        : _regtype (_n), "m" (*__xg((volatile void *)_p)), "0" (_o), "1" (0) \
+        : "+a" (_o), "=r" (_rc),                                        \
+          [ptr] "+m" (*(volatile typeof(*(_p)) *)(_p))                  \
Quoted 5 lines
Quoted 3 lines

Does casting to add "volatile" here really make any difference, considering the asm() itself is a volatile one and has a memory clobber?

Yes. mod_l1_entry() hits a BUG() without it.

Ouch.

Quoted 2 lines

Until I understand why, I purposefully didn't change the volatility of any of these constructs in what is otherwise a cleanup patch.

I think this would be quite relevant to understand irrespective of the exact shape this change is going to end up having. Which exact BUG() is getting triggered?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw
Quoted 12 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 22.11.18 at 13:38, <[email protected]> wrote:
Quoted 4 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 21.11.18 at 20:37, <[email protected]> wrote:

* Some of the single-byte versions specify "=q" as the output. AFAICT, there was not a legitimate reason to restrict the use of %esi/%edi in the 32-bit build. Either way, in 64-bit, it is equivelent to "=r".

Quoted 6 lines
Quoted 4 lines

I'm confused about the 32-bit part here: Of course it was necessary to restrict the compiler to the low 4 registers in that case. It's just not clear to me whether you've just written it down wrongly, or whether you indeed think the way it reads to me.

Wait - are you saying that the combination of "=r" and %b0 would actually fail to build if the compiler happened to chose %edi/%esi?

Quoted 8 lines

Of course: It would produce %sil / %dil, which exist only in 64-bit mode.

Quoted 3 lines

Now that you point it out, I can see why %esi/%edi aren't actually encodable in this circumstance, but surely the fact that the compiler has to pick a byte register means that it wouldn't end up choosing these?

How does it know it needs to pick a byte register when your constraint is "r"? That's what "q" is for.

Ok fine.  I'll reword in light of this behaviour.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines

xen/include/asm-x86/system.h static always_inline unsigned long __cmpxchg( · line 77

Quoted 12 lines
xen/include/asm-x86/system.h +3 -4
@@ -79,31 +72,27 @@ static always_inline unsigned long __cmpxchg(
     switch ( size )
     {
     case 1:
-        asm volatile ( "lock; cmpxchgb %b1,%2"
-                       : "=a" (prev)
-                       : "q" (new), "m" (*__xg(ptr)),
-                       "0" (old)
+        asm volatile ( "lock; cmpxchg %b[new], %[ptr]"
+                       : "=a" (prev), [ptr] "+m" (*(uint8_t *)ptr)
+                       : [new] "r" (new), "0" (old)

Any reason you retain the reference by number in the input constraint here, rather than giving its corresponding output one a name?

Quoted 5 lines
Quoted 2 lines

Not specifically. I suppose this is doable because the constraint is an explicitly register.

I don't understand: What does register or no have to do with it? Did you perhaps misunderstand? I'm asking for [prev] "a" (prev) and then "[prev]" (old).

Hmm - I'd forgotten that syntax, but I'm still not going to do it like that.

Using "=a" (prev) : "a" (old) is fine, and slightly better than what we have currently.  It also closely matches how the instruction is described in the manual, whereas using "[prev]" (old) is longer, and adds a level of indirection which makes the code harder to follow.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

xen/include/asm-x86/x86_64/system.h static always_inline __uint128_t cmpxchg16b_local_( · line 77

Quoted 8 lines
xen/include/asm-x86/x86_64/system.h +2 -2
         "       jmp 2b\n"                                               \
         ".previous\n"                                                   \
         _ASM_EXTABLE(1b, 3b)                                            \
-        : "=a" (_o), "=r" (_rc)                                         \
-        : _regtype (_n), "m" (*__xg((volatile void *)_p)), "0" (_o), "1" (0) \
+        : "+a" (_o), "=r" (_rc),                                        \
+          [ptr] "+m" (*(volatile typeof(*(_p)) *)(_p))                  \

Does casting to add "volatile" here really make any difference, considering the asm() itself is a volatile one and has a memory clobber?

Quoted 8 lines
Quoted 1 line

Yes. mod_l1_entry() hits a BUG() without it.

Ouch.

Quoted 2 lines

Until I understand why, I purposefully didn't change the volatility of any of these constructs in what is otherwise a cleanup patch.

I think this would be quite relevant to understand irrespective of the exact shape this change is going to end up having. Which exact BUG() is getting triggered?

Well - that's a good question.  We definitely hit a ud2 instruction, but get a bogus details printed.

I'm fairly sure I've ruled out a partially stale build, but I'm beginning to suspect that release builds without debugging symbols (which I'm using to reduce the binary diff between builds) causes Xen's symbol table to generated incorrectly.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw
Quoted 25 lines
Quoted 1 line
Quoted 1 line · On 22.11.18 at 14:58, <[email protected]> wrote:
Quoted 17 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 22.11.18 at 13:38, <[email protected]> wrote:
Quoted 15 lines · Jan Beulich

xen/include/asm-x86/system.h static always_inline unsigned long __cmpxchg( · line 77

Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 21.11.18 at 20:37, <[email protected]> wrote:
xen/include/asm-x86/system.h +3 -4
@@ -79,31 +72,27 @@ static always_inline unsigned long __cmpxchg(
     switch ( size )
     {
     case 1:
-        asm volatile ( "lock; cmpxchgb %b1,%2"
-                       : "=a" (prev)
-                       : "q" (new), "m" (*__xg(ptr)),
-                       "0" (old)
+        asm volatile ( "lock; cmpxchg %b[new], %[ptr]"
+                       : "=a" (prev), [ptr] "+m" (*(uint8_t *)ptr)
+                       : [new] "r" (new), "0" (old)

Any reason you retain the reference by number in the input constraint here, rather than giving its corresponding output one a name?

Quoted 5 lines
Quoted 2 lines

Not specifically. I suppose this is doable because the constraint is an explicitly register.

I don't understand: What does register or no have to do with it? Did you perhaps misunderstand? I'm asking for [prev] "a" (prev) and then "[prev]" (old).

Quoted 6 lines

Hmm - I'd forgotten that syntax, but I'm still not going to do it like that.

Using "=a" (prev) : "a" (old) is fine, and slightly better than what we have currently. It also closely matches how the instruction is described in the manual, whereas using "[prev]" (old) is longer, and adds a level of indirection which makes the code harder to follow.

Fine with me - all I'm after it to see the "0" gone.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel