Re: [PATCH 2/3] KVM: Use atomic_long_cmpxchg() instead of an open-coded variant

From: Paolo Bonzini
Date: Fri Nov 26 2021 - 05:36:49 EST


On 11/26/21 01:31, Maciej S. Szmigiero wrote:
- if ((long)old == atomic_long_read(&slots->last_used_slot))
- atomic_long_set(&slots->last_used_slot, (long)new);
+ /*
+ * The atomicity isn't strictly required here since we are
+ * operating on an inactive memslots set anyway.
+ */
+ atomic_long_cmpxchg(&slots->last_used_slot,
+ (unsigned long)old, (unsigned long)new);

I think using read/set is more readable than a comment saying that atomicity is not required.

It's a fairly common pattern, and while I agree that it's a PITA to write atomic_long_read and atomic_long_set, the person that reads the code is also helped by read/set, because they know they have to think about ownership invariants rather than concurrency invariants.

Paolo