Message ID | 7bdc7ee3dcc09a109cfaf9fb8662fb49ca0bec2c.1637884349.git.maciej.szmigiero@oracle.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | KVM: Scalable memslots implementation additional patches | expand |
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
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index d4399db06d49..367c1cba26d2 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1378,8 +1378,12 @@ static void kvm_replace_memslot(struct kvm *kvm, hash_del(&old->id_node[idx]); interval_tree_remove(&old->hva_node[idx], &slots->hva_tree); - 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); if (!new) { kvm_erase_gfn_node(slots, old);