x86/atomic: Improvements and simplifications to assembly constraints
PATCH v2 1/1 x86/atomic: Improvements and simplifications to assembly constraints
* Some of the single-byte versions specify "=q" as the output. This is a remnent of the 32bit build and can be relaxed to "=r" in 64bit builds.
* 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.
* Misc style changes.
---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
v2:
* Correct comment WRT 32bit builds and "=q" constraints
* Drop semicolons after the lock prefix
* Further reduce the use of positional parameters
The reason the volatile cast in __cmpxchg_user() can't be dropped is because without it, the compiler uses a stack copy rather than the in-memory copy, which ends up tripping:
/* Allowed to change in Accessed/Dirty flags only. */ BUG_ON((t ^ old) & ~(intpte_t)(_PAGE_ACCESSED|_PAGE_DIRTY));
--- xen/include/asm-x86/system.h | 99 +++++++++++++++++-------------------- xen/include/asm-x86/x86_64/system.h | 38 +++++++------- 2 files changed, 65 insertions(+), 72 deletions(-)
xen/include/asm-x86/system.h +44 -55
@@ -24,9 +24,6 @@ static inline void clflush(const void *p)#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> /*
@@ -40,28 +37,24 @@ static always_inline unsigned long __xchg(switch ( size ) {case 1 : - asmvolatile ("xchgb %b0,%1" - :"=q" (x) - :"m" (*__xg (ptr)),"0" (x) - :"memory" ); + asmvolatile ("xchg %b[x], %[ptr]" + : [x]"+r" (x), [ptr]"+m" (*(uint8_t *)ptr) + ::"memory" );break ;case 2 : - asmvolatile ("xchgw %w0,%1" - :"=r" (x) - :"m" (*__xg (ptr)),"0" (x) - :"memory" ); + asmvolatile ("xchg %w[x], %[ptr]" + : [x]"+r" (x), [ptr]"+m" (*(uint16_t *)ptr) + ::"memory" );break ;case 4 : - asmvolatile ("xchgl %k0,%1" - :"=r" (x) - :"m" (*__xg (ptr)),"0" (x) - :"memory" ); + asmvolatile ("xchg %k[x], %[ptr]" + : [x]"+r" (x), [ptr]"+m" (*(uint32_t *)ptr) + ::"memory" );break ;case 8 : - asmvolatile ("xchgq %0,%1" - :"=r" (x) - :"m" (*__xg (ptr)),"0" (x) - :"memory" ); + asmvolatile ("xchg %q[x], %[ptr]" + : [x]"+r" (x), [ptr]"+m" (*(uint64_t *)ptr) + ::"memory" );break ; }return x;
@@ -80,31 +73,27 @@ static always_inline unsigned long __cmpxchg(switch ( size ) {case 1 : - asmvolatile ("lock; cmpxchgb %b1,%2" - :"=a" (prev) - :"q" (new),"m" (*__xg (ptr)), -"0" (old) + asmvolatile ("lock cmpxchg %b[new], %[ptr]" + :"=a" (prev), [ptr]"+m" (*(uint8_t *)ptr) + : [new]"r" (new),"a" (old) :"memory" );return prev;case 2 : - asmvolatile ("lock; cmpxchgw %w1,%2" - :"=a" (prev) - :"r" (new),"m" (*__xg (ptr)), -"0" (old) + asmvolatile ("lock cmpxchg %w[new], %[ptr]" + :"=a" (prev), [ptr]"+m" (*(uint16_t *)ptr) + : [new]"r" (new),"a" (old) :"memory" );return prev;case 4 : - asmvolatile ("lock; cmpxchgl %k1,%2" - :"=a" (prev) - :"r" (new),"m" (*__xg (ptr)), -"0" (old) + asmvolatile ("lock cmpxchg %k[new], %[ptr]" + :"=a" (prev), [ptr]"+m" (*(uint32_t *)ptr) + : [new]"r" (new),"a" (old) :"memory" );return prev;case 8 : - asmvolatile ("lock; cmpxchgq %1,%2" - :"=a" (prev) - :"r" (new),"m" (*__xg (ptr)), -"0" (old) + asmvolatile ("lock cmpxchg %q[new], %[ptr]" + :"=a" (prev), [ptr]"+m" (*(uint64_t *)ptr) + : [new]"r" (new),"a" (old) :"memory" );return prev; }
@@ -119,24 +108,24 @@ static always_inline unsigned long cmpxchg_local_(switch ( size ) {case 1 : - asmvolatile ("cmpxchgb %b2, %1" - :"=a" (prev),"+m" (*(uint8_t *)ptr) - :"q" (new),"0" (old) ); + asmvolatile ("cmpxchg %b[new], %[ptr]" + :"=a" (prev), [ptr]"+m" (*(uint8_t *)ptr) + : [new]"r" (new),"a" (old) );break ;case 2 : - asmvolatile ("cmpxchgw %w2, %1" - :"=a" (prev),"+m" (*(uint16_t *)ptr) - :"r" (new),"0" (old) ); + asmvolatile ("cmpxchg %w[new], %[ptr]" + :"=a" (prev), [ptr]"+m" (*(uint16_t *)ptr) + : [new]"r" (new),"a" (old) );break ;case 4 : - asmvolatile ("cmpxchgl %k2, %1" - :"=a" (prev),"+m" (*(uint32_t *)ptr) - :"r" (new),"0" (old) ); + asmvolatile ("cmpxchg %k[new], %[ptr]" + :"=a" (prev), [ptr]"+m" (*(uint32_t *)ptr) + : [new]"r" (new),"a" (old) );break ;case 8 : - asmvolatile ("cmpxchgq %2, %1" - :"=a" (prev),"+m" (*(uint64_t *)ptr) - :"r" (new),"0" (old) ); + asmvolatile ("cmpxchg %q[new], %[ptr]" + :"=a" (prev), [ptr]"+m" (*(uint64_t *)ptr) + : [new]"r" (new),"a" (old) );break ; }
@@ -162,23 +151,23 @@ static always_inline unsigned long __xadd(switch ( size ) {case 1 : - asmvolatile ("lock; xaddb %b0,%1" - :"+r" (v),"+m" (*__xg (ptr)) + asmvolatile ("lock xadd %b[v], %[ptr]" + : [v]"+r" (v), [ptr]"+m" (*(uint8_t *)ptr) ::"memory" );return v;case 2 : - asmvolatile ("lock; xaddw %w0,%1" - :"+r" (v),"+m" (*__xg (ptr)) + asmvolatile ("lock xadd %w[v], %[ptr]" + : [v]"+r" (v), [ptr]"+m" (*(uint16_t *)ptr) ::"memory" );return v;case 4 : - asmvolatile ("lock; xaddl %k0,%1" - :"+r" (v),"+m" (*__xg (ptr)) + asmvolatile ("lock xadd %k[v], %[ptr]" + : [v]"+r" (v), [ptr]"+m" (*(uint32_t *)ptr) ::"memory" );return v;case 8 : - asmvolatile ("lock; xaddq %q0,%1" - :"+r" (v),"+m" (*__xg (ptr)) + asmvolatile ("lock xadd %q[v], %[ptr]" + : [v]"+r" (v), [ptr]"+m" (*(uint64_t *)ptr) ::"memory" );return v;
xen/include/asm-x86/x86_64/system.h +21 -17
@@ -24,9 +24,10 @@ static always_inline __uint128_t __cmpxchg16b(ASSERT (cpu_has_cx16);/* Don't use "=A" here - clang can't deal with that. */ - asmvolatile ("lock; cmpxchg16b %2" - :"=d" (prev.hi),"=a" (prev.lo),"+m" (*__xg (ptr)) - :"c" (new.hi),"b" (new.lo),"0" (old.hi),"1" (old.lo) ); + asmvolatile ("lock cmpxchg16b %[ptr]" + :"=d" (prev.hi),"=a" (prev.lo), + [ptr]"+m" (*(__uint128_t *)ptr) + :"c" (new.hi),"b" (new.lo),"d" (old.hi),"a" (old.lo) );return prev.raw; }
@@ -42,9 +43,10 @@ static always_inline __uint128_t cmpxchg16b_local_(ASSERT (cpu_has_cx16);/* Don't use "=A" here - clang can't deal with that. */ - asmvolatile ("cmpxchg16b %2" - :"=d" (prev.hi),"=a" (prev.lo),"+m" (*(__uint128_t *)ptr) - :"c" (new.hi),"b" (new.lo),"0" (old.hi),"1" (old.lo) ); + asmvolatile ("cmpxchg16b %[ptr]" + :"=d" (prev.hi),"=a" (prev.lo), + [ptr]"+m" (*(__uint128_t *)ptr) + :"c" (new.hi),"b" (new.lo),"d" (old.hi),"a" (old.lo) );return prev.raw; }
@@ -63,36 +65,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" \ + "3: movl $1, %[rc]\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), [rc] "=r" (_rc), \ + [ptr] "+m" (*(volatile typeof(*(_p)) *)(_p)) \ + : [new] "r" (_n), "[rc]" (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
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 18.03.19 at 12:27, <[email protected]> wrote:
* Some of the single-byte versions specify "=q" as the output. This is a remnent of the 32bit build and can be relaxed to "=r" in 64bit builds.
I have to admit that I don't understand the "relaxed" part of this:
"q" and "r" represent the exact same set of registers on 64-bit. Unless the conversion allows further code folding, I think it wouldn't be a bad idea to retain the distinction, just for cases like code eventually getting shared via something like lib/x86/.
Quoted 6 lines
The reason the volatile cast in
__cmpxchg_user()can't be dropped is because without it, the compiler uses a stack copy rather than the in-memory copy, which ends up tripping:/* Allowed to change in Accessed/Dirty flags only. */
BUG_ON((t ^ old) & ~(intpte_t)(_PAGE_ACCESSED|_PAGE_DIRTY));
Isn't this hinting at some other shortcoming or even flaw then? If the compiler generally did such transformations, I'm afraid a lot of other code would be at risk too, including some of what you modify here.
In any event I think it would be a good idea to have a code comment for this as well.
Quoted 29 lines
xen/include/asm-x86/system.h +9
-12@@ -40,28 +37,24 @@ static always_inline unsigned long __xchg(switch ( size ) {case 1 : - asmvolatile ("xchgb %b0,%1" - :"=q" (x) - :"m" (*__xg (ptr)),"0" (x) - :"memory" ); + asmvolatile ("xchg %b[x], %[ptr]" + : [x]"+r" (x), [ptr]"+m" (*(uint8_t *)ptr) + ::"memory" );break ;case 2 : - asmvolatile ("xchgw %w0,%1" - :"=r" (x) - :"m" (*__xg (ptr)),"0" (x) - :"memory" ); + asmvolatile ("xchg %w[x], %[ptr]" + : [x]"+r" (x), [ptr]"+m" (*(uint16_t *)ptr) + ::"memory" );break ;case 4 : - asmvolatile ("xchgl %k0,%1" - :"=r" (x) - :"m" (*__xg (ptr)),"0" (x) - :"memory" ); + asmvolatile ("xchg %k[x], %[ptr]" + : [x]"+r" (x), [ptr]"+m" (*(uint32_t *)ptr) + ::"memory" );
xen/include/asm-x86/system.h static always_inline unsigned long __xchg( · line 58
Quoted 10 lines
xen/include/asm-x86/system.h +3
-4break ;case 8 : - asmvolatile ("xchgq %0,%1" - :"=r" (x) - :"m" (*__xg (ptr)),"0" (x) - :"memory" ); + asmvolatile ("xchg %q[x], %[ptr]" + : [x]"+r" (x), [ptr]"+m" (*(uint64_t *)ptr) + ::"memory" );break ;
Is the q modifier really useful to have here (and elsewhere below)?
Quoted 12 lines
xen/include/asm-x86/x86_64/system.h +2
-2@@ -63,36 +65,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" \
xen/include/asm-x86/x86_64/system.h static always_inline __uint128_t cmpxchg16b_local_( · line 80
Quoted 10 lines
xen/include/asm-x86/x86_64/system.h +4
-3-"3: movl $1,%1\n" \ +"3: movl $1, %[rc]\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), [rc] "=r" (_rc), \ +[ptr] " +m" (*(volatile typeof(*(_p)) *)(_p)) \ + : [new] "r" (_n), "[rc]" (0) \
Wouldn't it further help readability a little if _rc was initialized to zero right when getting declared, eliminating the last input arg here (the output then would need to be "+r" of course)? And since then you actually touch all lines containing uses of _rc, it would be a good opportunity to also rename the variable to get rid of the leading underscore.
Anyway, with at least the "relaxed" part of the description changed (e.g. to "converted") or explained verbally in a reply, with or without the other items taken care of
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 8 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 18.03.19 at 12:27, <[email protected]> wrote:
* Some of the single-byte versions specify "=q" as the output. This is a remnent of the 32bit build and can be relaxed to "=r" in 64bit builds.
I have to admit that I don't understand the "relaxed" part of this:
"q" and "r" represent the exact same set of registers on 64-bit. Unless the conversion allows further code folding, I think it wouldn't be a bad idea to retain the distinction, just for cases like code eventually getting shared via something likelib/x86/.
The change from =q to =r is specifically to allow the folding to +r
Quoted 11 lines
Quoted 7 lines
The reason the volatile cast in
__cmpxchg_user()can't be dropped is because without it, the compiler uses a stack copy rather than the in-memory copy, which ends up tripping:/* Allowed to change in Accessed/Dirty flags only. */
BUG_ON((t ^ old) & ~(intpte_t)(_PAGE_ACCESSED|_PAGE_DIRTY));Isn't this hinting at some other shortcoming or even flaw then? If the compiler generally did such transformations, I'm afraid a lot of other code would be at risk too, including some of what you modify here.
I don't think there is any flaw or shortcoming. Without the volatile, the compiler doesn't know that there are any side effects, so can legitimately operate on a local stack copy so long as it copies things back later.
In practice, this is an operation on shared memory which has to happen on the shared memory pointer.
Quoted 2 lines
In any event I think it would be a good idea to have a code comment for this as well.
I don't see how that would help. The same applies to all atomic operations, even test_bit().
Quoted 33 lines
Quoted 33 lines
xen/include/asm-x86/system.h +9
-13@@ -40,28 +37,24 @@ static always_inline unsigned long __xchg(switch ( size ) {case 1 : - asmvolatile ("xchgb %b0,%1" - :"=q" (x) - :"m" (*__xg (ptr)),"0" (x) - :"memory" ); + asmvolatile ("xchg %b[x], %[ptr]" + : [x]"+r" (x), [ptr]"+m" (*(uint8_t *)ptr) + ::"memory" );break ;case 2 : - asmvolatile ("xchgw %w0,%1" - :"=r" (x) - :"m" (*__xg (ptr)),"0" (x) - :"memory" ); + asmvolatile ("xchg %w[x], %[ptr]" + : [x]"+r" (x), [ptr]"+m" (*(uint16_t *)ptr) + ::"memory" );break ;case 4 : - asmvolatile ("xchgl %k0,%1" - :"=r" (x) - :"m" (*__xg (ptr)),"0" (x) - :"memory" ); + asmvolatile ("xchg %k[x], %[ptr]" + : [x]"+r" (x), [ptr]"+m" (*(uint32_t *)ptr) + ::"memory" );break ;case 8 : - asm volatile ( "xchgq %0,%1"
Quoted 8 lines
xen/include/asm-x86/system.h static always_inline unsigned long __xchg( · line 58
Quoted 7 lines
xen/include/asm-x86/system.h +3
-3- : "=r" (x) - : "m" (*__xg(ptr)), "0" (x) - : "memory" ); + asmvolatile ("xchg %q[x], %[ptr]" + : [x]"+r" (x), [ptr]"+m" (*(uint64_t *)ptr) + ::"memory" );break ;Is the q modifier really useful to have here (and elsewhere below)?
Yes - it is strictly necessary, because otherwise it gets derived from the type of (x) which is unsigned long even in the smaller size constructs.
Quoted 18 lines
Quoted 18 lines
xen/include/asm-x86/x86_64/system.h +3
-3@@ -63,36 +65,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" \ + "3: movl $1, %[rc]\n" \ " jmp 2b\n" \ ".previous\n" \ _ASM_EXTABLE(1b, 3b) \
Quoted 8 lines
xen/include/asm-x86/x86_64/system.h static always_inline __uint128_t cmpxchg16b_local_( · line 80
Quoted 5 lines
xen/include/asm-x86/x86_64/system.h +3
-2- : "=a" (_o), "=r" (_rc) \ - : _regtype (_n), "m" (*__xg((volatile void *)_p)), "0" (_o), "1" (0) \ + : "+a" (_o), [rc] "=r" (_rc), \ + [ptr] "+m" (*(volatile typeof(*(_p)) *)(_p)) \ + : [new] "r" (_n), "[rc]" (0) \Wouldn't it further help readability a little if
_rcwas initialized to zero right when getting declared, eliminating the last input arg here (the output then would need to be "+r" of course)?
I can do.
Quoted 4 lines
And since then you
actually touch all lines containing uses of_rc, it would be a good opportunity to also rename the variable to get rid of the leading underscore.
I'm not sure that is a sensible move. Its a macro-scope variable from cmpxchg_user() which still needs disambiguating from potential names of parameters.
Quoted 5 lines
Anyway, with at least the "relaxed" part of the description changed (e.g. to "converted") or explained verbally in a reply, with or without the other items taken care of
Reviewed-by Jan Beulich <[email protected]>
Hopefully my reply is sufficient?
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 26 lines · Andrew Cooper
Quoted 18 lines
Quoted 18 lines
(quoted diff) +3
-3@@ -63,36 +65,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" \ + "3: movl $1, %[rc]\n" \ " jmp 2b\n" \ ".previous\n" \ _ASM_EXTABLE(1b, 3b) \Quoted 8 lines
Review static always_inline __uint128_t cmpxchg16b_local_( · line 80
Quoted 5 lines
(quoted diff) +3
-2- : "=a" (_o), "=r" (_rc) \ - : _regtype (_n), "m" (*__xg((volatile void *)_p)), "0" (_o), "1" (0) \ + : "+a" (_o), [rc] "=r" (_rc), \ + [ptr] "+m" (*(volatile typeof(*(_p)) *)(_p)) \ + : [new] "r" (_n), "[rc]" (0) \Wouldn't it further help readability a little if
_rcwas initialized to zero right when getting declared, eliminating the last input arg here (the output then would need to be "+r" of course)?
Quoted 1 line
I can do.
I've got the following incremental fix which I intend to fold in.
xen/include/asm-x86/x86_64/system.h +9 -8
@@ -65,7 +65,7 @@ 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, _oppre) \ +#define __cmpxchg_user (_p, _o, _n, _rc, _oppre) \ stac(); \ asm volatile ( \ "1: lock cmpxchg %"_oppre"[new], %[ptr]\n" \
@@ -75,28 +75,29 @@ static always_inline __uint128_t cmpxchg16b_local_(" jmp 2b\n" \".previous\n" \ _ASM_EXTABLE(1b ,3b ) \ - : "+a" (_o), [rc] "=r" (_rc), \ + : "+a" (_o), [rc] "+r" (_rc), \[ptr] " +m" (*(volatile typeof(*(_p)) *)(_p)) \ - : [new] "r" (_n), "[rc]" (0) \ +: [new] " r" (_n) \ : "memory"); \ clac() #define cmpxchg_user(_p, _o, _n) \ ({ \ - int _rc; \ + int _rc = 0; \ + \ switch ( sizeof(*(_p)) ) \ { \ case 1: \ - __cmpxchg_user(_p, _o, _n, "b"); \ + __cmpxchg_user(_p, _o, _n, _rc, "b"); \ break; \ case 2: \ - __cmpxchg_user(_p, _o, _n, "w"); \ + __cmpxchg_user(_p, _o, _n, _rc, "w"); \ break; \ case 4: \ - __cmpxchg_user(_p, _o, _n, "k"); \ + __cmpxchg_user(_p, _o, _n, _rc, "k"); \ break; \ case 8: \ - __cmpxchg_user(_p, _o, _n, "q"); \ + __cmpxchg_user(_p, _o, _n, _rc, "q"); \ break; \ } \ _rc; \
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 18.03.19 at 15:11, <[email protected]> wrote:
Quoted 3 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 18.03.19 at 12:27, <[email protected]> wrote:
* Some of the single-byte versions specify "=q" as the output. This is a remnent of the 32bit build and can be relaxed to "=r" in 64bit builds.
Quoted 7 lines
Quoted 5 lines
I have to admit that I don't understand the "relaxed" part of this:
"q" and "r" represent the exact same set of registers on 64-bit. Unless the conversion allows further code folding, I think it wouldn't be a bad idea to retain the distinction, just for cases like code eventually getting shared via something likelib/x86/.The change from =q to =r is specifically to allow the folding to +r
Hmm, I still don't understand. "+q" is as valid as "+r". And "q" does not make any size implications, i.e. registers in that group aren't implicitly byte ones (albeit this aspect doesn't even matter here).
Quoted 11 lines
Quoted 10 lines
Quoted 6 lines
The reason the volatile cast in
__cmpxchg_user()can't be dropped is because without it, the compiler uses a stack copy rather than the in-memory copy, which ends up tripping:/* Allowed to change in Accessed/Dirty flags only. */
BUG_ON((t ^ old) & ~(intpte_t)(_PAGE_ACCESSED|_PAGE_DIRTY));Isn't this hinting at some other shortcoming or even flaw then? If the compiler generally did such transformations, I'm afraid a lot of other code would be at risk too, including some of what you modify here.
Quoted 7 lines
I don't think there is any flaw or shortcoming. Without the volatile, the compiler doesn't know that there are any side effects, so can legitimately operate on a local stack copy so long as it copies things back later.
In practice, this is an operation on shared memory which has to happen on the shared memory pointer.
Yes, of course. But volatile isn't guaranteed to have this effect. Iirc there's even a statement to this effect in the gcc docs. It just so happens that the compiler wouldn't normally make a memory copy of a memory variable. But it wouldn't normally do so for non-volatile memory either (because it would be an anti-optimization), so there has to be something special here.
Quoted 5 lines
Quoted 2 lines
In any event I think it would be a good idea to have a code comment for this as well.
I don't see how that would help. The same applies to all atomic operations, even
test_bit().
I don't think any other operation has casts to void in it - certainly none of the others you touch here. In fact in (at least) __xchg() and __cmpxchg() you cast away volatile, which I think you want to avoid. test_bit() (as the example you give) doesn't cast to volatile, it merely alters the type, but the incoming pointer is already a volatile one.
Then again this is a macro, so I guess the cast can be counted to be the equivalent of an implicit conversion due to a function parameter being pointer-to-volatile.
Quoted 41 lines
Quoted 32 lines
Quoted 32 lines
xen/include/asm-x86/system.h +9
-13@@ -40,28 +37,24 @@ static always_inline unsigned long __xchg(switch ( size ) {case 1 : - asmvolatile ("xchgb %b0,%1" - :"=q" (x) - :"m" (*__xg (ptr)),"0" (x) - :"memory" ); + asmvolatile ("xchg %b[x], %[ptr]" + : [x]"+r" (x), [ptr]"+m" (*(uint8_t *)ptr) + ::"memory" );break ;case 2 : - asmvolatile ("xchgw %w0,%1" - :"=r" (x) - :"m" (*__xg (ptr)),"0" (x) - :"memory" ); + asmvolatile ("xchg %w[x], %[ptr]" + : [x]"+r" (x), [ptr]"+m" (*(uint16_t *)ptr) + ::"memory" );break ;case 4 : - asmvolatile ("xchgl %k0,%1" - :"=r" (x) - :"m" (*__xg (ptr)),"0" (x) - :"memory" ); + asmvolatile ("xchg %k[x], %[ptr]" + : [x]"+r" (x), [ptr]"+m" (*(uint32_t *)ptr) + ::"memory" );break ;case 8 : - asm volatile ( "xchgq %0,%1"Quoted 8 lines
xen/include/asm-x86/system.h static always_inline unsigned long __xchg( · line 58
Quoted 7 lines
xen/include/asm-x86/system.h +3
-3- : "=r" (x) - : "m" (*__xg(ptr)), "0" (x) - : "memory" ); + asmvolatile ("xchg %q[x], %[ptr]" + : [x]"+r" (x), [ptr]"+m" (*(uint64_t *)ptr) + ::"memory" );break ;Is the q modifier really useful to have here (and elsewhere below)?
Quoted 2 lines
Yes - it is strictly necessary, because otherwise it gets derived from the type of (x) which is unsigned long even in the smaller size constructs.
What you say explains why you need the b, w, and k modifiers. It doesn't explain the q one, since sizeof(unsigned long) is 8 and hence exactly what would result without the modifier.
Quoted 28 lines
Quoted 17 lines
Quoted 17 lines
xen/include/asm-x86/x86_64/system.h +3
-3@@ -63,36 +65,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" \ + "3: movl $1, %[rc]\n" \ " jmp 2b\n" \ ".previous\n" \ _ASM_EXTABLE(1b, 3b) \Quoted 8 lines
xen/include/asm-x86/x86_64/system.h static always_inline __uint128_t cmpxchg16b_local_( · line 80
Quoted 5 lines
xen/include/asm-x86/x86_64/system.h +3
-2- : "=a" (_o), "=r" (_rc) \ - : _regtype (_n), "m" (*__xg((volatile void *)_p)), "0" (_o), "1" (0) \ + : "+a" (_o), [rc] "=r" (_rc), \ + [ptr] "+m" (*(volatile typeof(*(_p)) *)(_p)) \ + : [new] "r" (_n), "[rc]" (0) \Wouldn't it further help readability a little if
_rcwas initialized to zero right when getting declared, eliminating the last input arg here (the output then would need to be "+r" of course)?I can do.
Quoted 8 lines
Quoted 4 lines
And since then you
actually touch all lines containing uses of_rc, it would be a good opportunity to also rename the variable to get rid of the leading underscore.I'm not sure that is a sensible move. Its a macro-scope variable from
cmpxchg_user()which still needs disambiguating from potential names of parameters.
I don't understand. For one, the macro parameters are all (wrongly) underscore-prefixed too, so there's no risk of collision there. Disambiguation is needed against outer scope variables, yet this should be done by an underscore suffix (as we commonly do) or yet some other mechanism. A leading underscore here is not in line with the C spec.
Quoted 6 lines
Quoted 4 lines
Anyway, with at least the "relaxed" part of the description changed (e.g. to "converted") or explained verbally in a reply, with or without the other items taken care of
Reviewed-by Jan Beulich <[email protected]>Hopefully my reply is sufficient?
Well, as per above not for now: I still don't see any relaxation you do.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 28 lines
Quoted 1 line
Quoted 1 line · On 18.03.19 at 15:49, <[email protected]> wrote:
Quoted 26 lines · Andrew Cooper
Quoted 26 lines
Quoted 13 lines
(quoted diff) +2
-2@@ -63,36 +65,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" \Review static always_inline __uint128_t cmpxchg16b_local_( · line 80
Quoted 10 lines
(quoted diff) +4
-3- "3: movl $1,%1\n" \ + "3: movl $1, %[rc]\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), [rc] "=r" (_rc), \ + [ptr] "+m" (*(volatile typeof(*(_p)) *)(_p)) \ + : [new] "r" (_n), "[rc]" (0) \Wouldn't it further help readability a little if
_rcwas initialized to zero right when getting declared, eliminating the last input arg here (the output then would need to be "+r" of course)?
Quoted 3 lines
Quoted 1 line
I can do.
I've got the following incremental fix which I intend to fold in.
LGTM.
Jan
Quoted 42 lines
xen/include/asm-x86/x86_64/system.h +0
-0@@ -65,7 +65,7 @@ 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 +7
-6-#define __cmpxchg_user (_p, _o, _n, _oppre) \ +#define __cmpxchg_user (_p, _o, _n, _rc, _oppre) \ stac(); \ asm volatile ( \ "1: lock cmpxchg %"_oppre"[new], %[ptr]\n" \@@ -75,28 +75,29 @@ static always_inline __uint128_t cmpxchg16b_local_(" jmp 2b\n" \".previous\n" \ _ASM_EXTABLE(1b ,3b ) \ - : "+a" (_o), [rc] "=r" (_rc), \ + : "+a" (_o), [rc] "+r" (_rc), \[ptr] " +m" (*(volatile typeof(*(_p)) *)(_p)) \ - : [new] "r" (_n), "[rc]" (0) \ +: [new] " r" (_n) \ : "memory"); \ clac() #define cmpxchg_user(_p, _o, _n) \ ({ \ - int _rc; \ + int _rc = 0; \ + \ switch ( sizeof(*(_p)) ) \ { \ case 1: \ - __cmpxchg_user(_p, _o, _n, "b"); \ + __cmpxchg_user(_p, _o, _n, _rc, "b"); \ break; \ case 2: \ - __cmpxchg_user(_p, _o, _n, "w"); \ + __cmpxchg_user(_p, _o, _n, _rc, "w"); \ break; \
xen/include/asm-x86/x86_64/system.h static always_inline __uint128_t cmpxchg16b_local_( · line 103
Quoted 10 lines
xen/include/asm-x86/x86_64/system.h +2
-2case 4 : \ -__cmpxchg_user (_p, _o, _n,"k" ); \ +__cmpxchg_user (_p, _o, _n, _rc,"k" ); \break ; \case 8 : \ -__cmpxchg_user (_p, _o, _n,"q" ); \ +__cmpxchg_user (_p, _o, _n, _rc,"q" ); \break ; \ } \ _rc; \
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 10 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 18.03.19 at 15:11, <[email protected]> wrote:
Quoted 8 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 18.03.19 at 12:27, <[email protected]> wrote:
* Some of the single-byte versions specify "=q" as the output. This is a remnent of the 32bit build and can be relaxed to "=r" in 64bit builds.
I have to admit that I don't understand the "relaxed" part of this:
"q" and "r" represent the exact same set of registers on 64-bit. Unless the conversion allows further code folding, I think it wouldn't be a bad idea to retain the distinction, just for cases like code eventually getting shared via something likelib/x86/.
Quoted 4 lines
Quoted 1 line
The change from =q to =r is specifically to allow the folding to +r
Hmm, I still don't understand. "+q" is as valid as "+r". And "q" does not make any size implications, i.e. registers in that group aren't implicitly byte ones (albeit this aspect doesn't even matter here).
I'm at a complete loss to understand what you are objecting to here.
"q" and "r" mean different things on 32bit, and "+q" would be wrong to use there.
"q" and "r" are synonymous on 64bit, but "r" is still the one to use because that is the more common constraint, and therefore the more familiar for people reading the code.
Quoted 41 lines
Quoted 41 lines
Quoted 41 lines
Quoted 30 lines
xen/include/asm-x86/system.h +9
-12@@ -40,28 +37,24 @@ static always_inline unsigned long __xchg(switch ( size ) {case 1 : - asmvolatile ("xchgb %b0,%1" - :"=q" (x) - :"m" (*__xg (ptr)),"0" (x) - :"memory" ); + asmvolatile ("xchg %b[x], %[ptr]" + : [x]"+r" (x), [ptr]"+m" (*(uint8_t *)ptr) + ::"memory" );break ;case 2 : - asmvolatile ("xchgw %w0,%1" - :"=r" (x) - :"m" (*__xg (ptr)),"0" (x) - :"memory" ); + asmvolatile ("xchg %w[x], %[ptr]" + : [x]"+r" (x), [ptr]"+m" (*(uint16_t *)ptr) + ::"memory" );break ;case 4 : - asmvolatile ("xchgl %k0,%1" - :"=r" (x) - :"m" (*__xg (ptr)),"0" (x) - :"memory" ); + asmvolatile ("xchg %k[x], %[ptr]" + : [x]"+r" (x), [ptr]"+m" (*(uint32_t *)ptr) + ::"memory" );xen/include/asm-x86/system.h static always_inline unsigned long __xchg( · line 58
Quoted 10 lines
xen/include/asm-x86/system.h +3
-4break ;case 8 : - asmvolatile ("xchgq %0,%1" - :"=r" (x) - :"m" (*__xg (ptr)),"0" (x) - :"memory" ); + asmvolatile ("xchg %q[x], %[ptr]" + : [x]"+r" (x), [ptr]"+m" (*(uint64_t *)ptr) + ::"memory" );break ;Is the q modifier really useful to have here (and elsewhere below)?
Quoted 5 lines
Quoted 2 lines
Yes - it is strictly necessary, because otherwise it gets derived from the type of (x) which is unsigned long even in the smaller size constructs.
What you say explains why you need the b, w, and k modifiers. It doesn't explain the q one, since sizeof(unsigned long) is 8 and hence exactly what would result without the modifier.
Oh, in which case, "for consistency".
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 22.03.19 at 21:19, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 18.03.19 at 15:11, <[email protected]> wrote:
Quoted 8 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 18.03.19 at 12:27, <[email protected]> wrote:
* Some of the single-byte versions specify "=q" as the output. This is a remnent of the 32bit build and can be relaxed to "=r" in 64bit builds.
I have to admit that I don't understand the "relaxed" part of this:
"q" and "r" represent the exact same set of registers on 64-bit. Unless the conversion allows further code folding, I think it wouldn't be a bad idea to retain the distinction, just for cases like code eventually getting shared via something likelib/x86/.
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
The change from =q to =r is specifically to allow the folding to +r
Hmm, I still don't understand. "+q" is as valid as "+r". And "q" does not make any size implications, i.e. registers in that group aren't implicitly byte ones (albeit this aspect doesn't even matter here).
I'm at a complete loss to understand what you are objecting to here.
The main point of my comment is your use of the word "relax" when there's nothing you really relax (afaics - below you confirm yourself that the two are synonyms on 64-bit).
Quoted 2 lines
"q" and "r" mean different things on 32bit, and "+q" would be wrong to use there.
Why would "+q" be wrong? You want "q" (to constrain the register set to those registers which are byte accessible) and you also want "+" (as it's an in/out, and the folding from "=q" plus "0" is as valid as the folding from "=r" plus "0" to "+r").
Quoted 3 lines
"q" and "r" are synonymous on 64bit, but "r" is still the one to use because that is the more common constraint, and therefore the more familiar for people reading the code.
I understand this part of your argument; I don't see you responding at all to the potential code re-usability point I've been making. Yet there's weighing to be done between the two.
Quoted 40 lines
Quoted 40 lines
Quoted 40 lines
Quoted 40 lines
Quoted 29 lines
xen/include/asm-x86/system.h +9
-12@@ -40,28 +37,24 @@ static always_inline unsigned long __xchg(switch ( size ) {case 1 : - asmvolatile ("xchgb %b0,%1" - :"=q" (x) - :"m" (*__xg (ptr)),"0" (x) - :"memory" ); + asmvolatile ("xchg %b[x], %[ptr]" + : [x]"+r" (x), [ptr]"+m" (*(uint8_t *)ptr) + ::"memory" );break ;case 2 : - asmvolatile ("xchgw %w0,%1" - :"=r" (x) - :"m" (*__xg (ptr)),"0" (x) - :"memory" ); + asmvolatile ("xchg %w[x], %[ptr]" + : [x]"+r" (x), [ptr]"+m" (*(uint16_t *)ptr) + ::"memory" );break ;case 4 : - asmvolatile ("xchgl %k0,%1" - :"=r" (x) - :"m" (*__xg (ptr)),"0" (x) - :"memory" ); + asmvolatile ("xchg %k[x], %[ptr]" + : [x]"+r" (x), [ptr]"+m" (*(uint32_t *)ptr) + ::"memory" );xen/include/asm-x86/system.h static always_inline unsigned long __xchg( · line 58
Quoted 10 lines
xen/include/asm-x86/system.h +3
-4break ;case 8 : - asmvolatile ("xchgq %0,%1" - :"=r" (x) - :"m" (*__xg (ptr)),"0" (x) - :"memory" ); + asmvolatile ("xchg %q[x], %[ptr]" + : [x]"+r" (x), [ptr]"+m" (*(uint64_t *)ptr) + ::"memory" );break ;Is the q modifier really useful to have here (and elsewhere below)?
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Yes - it is strictly necessary, because otherwise it gets derived from the type of (x) which is unsigned long even in the smaller size constructs.
What you say explains why you need the b, w, and k modifiers. It doesn't explain the q one, since sizeof(unsigned long) is 8 and hence exactly what would result without the modifier.
Oh, in which case, "for consistency".
Well, okay, albeit along the lines of the code re-usability point made before (and mentioned above) personally I'd prefer if we kept the code in a shape where it could _in principle_ be (easily) re-used for 32-bit.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel