Skip to contentSkip to conversation

xen/x86: lock cacheline for add_sized()

PATCH Superseded Juergen Gross 2 participants 3 messages Aug 6, 2019

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/x86: lock cacheline for add_sized()

add_sized() should use an atomic update of the memory word, as it is used by spin_unlock().

With ticket locks unlocking needs to be atomic in order to avoid very rare races when someone tries to get the lock while the lock holder is just releasing it.

---
 xen/include/asm-x86/atomic.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/include/asm-x86/atomic.h +1 -1
index 682bcf91b1..0ef6a60e69 100644
@@ -21,7 +21,7 @@ static inline void name(volatile type *addr, type val) \
 #define build_add_sized(name, size, type, reg) \
     static inline void name(volatile type *addr, type val)              \
     {                                                                   \
-        asm volatile("add" size " %1,%0"                                \
+        asm volatile("lock; add" size " %1,%0"                          \
                      : "=m" (*addr)                                     \
                      : reg (val));                                      \
     }
Signature · 7 lines
2.16.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw
Quoted 6 lines · Juergen Gross

add_sized() should use an atomic update of the memory word, as it is used by spin_unlock().

With ticket locks unlocking needs to be atomic in order to avoid very rare races when someone tries to get the lock while the lock holder is just releasing it.

Considering the commit introducing the function (3c694aec08) explicitly saying "The add is /not/ atomic" this needs more detail. The idea behind not using a LOCKed access here was, iirc, that no-one else could ever update the head pointer; someone trying to acquire the lock would only write to the tail portion. But yes, I think I can see how this was wrong: The arch_fetch_and_add() acts on head_tail after all, not just tail.

xen/include/asm-x86/atomic.h static inline void name(volatile type *addr, type val) \ · line 24

Quoted 8 lines
xen/include/asm-x86/atomic.h +1 -1
@@ -21,7 +21,7 @@ static inline void name(volatile type *addr, type val) \
  #define build_add_sized(name, size, type, reg) \
      static inline void name(volatile type *addr, type val)              \
      {                                                                   \
-        asm volatile("add" size " %1,%0"                                \
+        asm volatile("lock; add" size " %1,%0"                          \

I realize pre-existing code in our tree uses this not fully correct form of specifying prefixes (there shouldn't really be a line separator between prefix and main mnemonic, unless gas would refuse assembling the result because of it wrongly thinking the prefix was inapplicable to the insn, like is e.g. the case here and there for the REP prefixes), but I think we should try to avoid widening the issue. See e.g. gnttab_clear_flags() where we already have no semicolon, and this has gone fine since around 4.2. I seem to vaguely recall that Linux has been using this construct to avoid build issues with some specific (Sun?) assembler.

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

#2 lore raw
Quoted 8 lines · Jan Beulich
Quoted 6 lines · Juergen Gross

add_sized() should use an atomic update of the memory word, as it is used by spin_unlock().

With ticket locks unlocking needs to be atomic in order to avoid very rare races when someone tries to get the lock while the lock holder is just releasing it.

Quoted 7 lines

Considering the commit introducing the function (3c694aec08) explicitly saying "The add is /not/ atomic" this needs more detail. The idea behind not using a LOCKed access here was, iirc, that no-one else could ever update the head pointer; someone trying to acquire the lock would only write to the tail portion. But yes, I think I can see how this was wrong: The arch_fetch_and_add() acts on head_tail after all, not just tail.

I'll update the commit message.

Quoted 12 lines

xen/include/asm-x86/atomic.h static inline void name(volatile type *addr, type val) \ · line 24

Quoted 9 lines
xen/include/asm-x86/atomic.h +1 -1
@@ -21,7 +21,7 @@ static inline void name(volatile type *addr, type val) \
   #define build_add_sized(name, size, type, reg) \
       static inline void name(volatile type *addr, type val)              \
       {                                                                   \
-        asm volatile("add" size " %1,%0"                                \
+        asm volatile("lock; add" size " %1,%0"                          \

I realize pre-existing code in our tree uses this not fully correct form of specifying prefixes (there shouldn't really be a line

Quoted 8 lines

separator between prefix and main mnemonic, unless gas would refuse assembling the result because of it wrongly thinking the prefix was inapplicable to the insn, like is e.g. the case here and there for the REP prefixes), but I think we should try to avoid widening the issue. See e.g. gnttab_clear_flags() where we already have no semicolon, and this has gone fine since around 4.2. I seem to vaguely recall that Linux has been using this construct to avoid build issues with some specific (Sun?) assembler.

Okay, I'll remove the semicolon.

Juergen

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