Skip to contentSkip to conversation

xen/x86: lock cacheline for add_sized()

PATCH In review Juergen Gross 2 participants 3 messages Aug 7, 2019

lore
Share & tools
lore
#0 lore raw

PATCH v2 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().

Ticket locks are using a read-modify-write operation on parts of the lockword for unlocking, while trying to lock is done by an atomic update of the complete lockword. This requires the unlock operation to be atomic, too, as otherwise the unlock might not write back the correct data.

---
 xen/include/asm-x86/atomic.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/include/asm-x86/atomic.h +1 -1
index 682bcf91b1..897f661191 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 8 lines · Juergen Gross

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

Ticket locks are using a read-modify-write operation on parts of the lockword for unlocking, while trying to lock is done by an atomic update of the complete lockword. This requires the unlock operation to be atomic, too, as otherwise the unlock might not write back the correct data.

I have to take back my reply to v1, and hence I'm afraid that if the change is really needed the description is still insufficient. Let's look at both sides: Acquire is a LOCKed read-modify-write of the full word, with the additional property that the value written back to the low half is unchanged from the value read. Release is

read low half
increment low half
write low half

Since the low half doesn't change during any acquire (including attempts, i.e. try-lock), it doesn't matter if it races with the above sequence. It can freely happen between any two of the three steps.

Therefore what I'm really after is (a) clarification whether the issue you mean to fix was observed in practice and (b) a concrete scenario where things would go wrong.

Jan

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

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

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

Ticket locks are using a read-modify-write operation on parts of the lockword for unlocking, while trying to lock is done by an atomic update of the complete lockword. This requires the unlock operation to be atomic, too, as otherwise the unlock might not write back the correct data.

I have to take back my reply to v1, and hence I'm afraid that if the change is really needed the description is still insufficient. Let's look at both sides: Acquire is a LOCKed read-modify-write of the full word, with the additional property that the value written back to the low half is unchanged from the value read. Release is

Quoted 8 lines

    read low half
    increment low half
    write low half

Since the low half doesn't change during any acquire (including attempts, i.e. try-lock), it doesn't matter if it races with the above sequence. It can freely happen between any two of the three steps.

Hmm, sounds sensible.

Quoted 3 lines

Therefore what I'm really after is (a) clarification whether the issue you mean to fix was observed in practice and (b) a concrete scenario where things would go wrong.

I have seen a crash due to nmi watchdog with my core scheduling series. It really looked like no one was holding the lock, but two cpus were waiting for it. I couldn't find another explanation for that behavior, but there might be another reason for it.

The problem occurred a short time after onlining a cpu when all cpus tried to gather for time calibration and two of the cpus were not joining, as they were waiting for the lock with IRQs disabled.

I have seen this scenario only once, but there are other strange hangs in the same situation, so it might be that the bug I'm still looking for is the reason for the problem I'm trying to solve with this patch.

Juergen

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