seqlock: read sequence number atomically
authorEmilio G. Cota <cota@braap.org>
Mon, 24 Aug 2015 00:23:36 +0000 (20:23 -0400)
committerPaolo Bonzini <pbonzini@redhat.com>
Mon, 7 Sep 2015 16:12:39 +0000 (18:12 +0200)
With this change we make sure that the compiler will not
optimise the read of the sequence number in any way.

Signed-off-by: Emilio G. Cota <cota@braap.org>
Message-Id: <1440375847-17603-8-git-send-email-cota@braap.org>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
include/qemu/seqlock.h

index f1256f5487f30e0696fc0170d01a6f5151cee4b8..70b01fd60d803f7352da578d5957d6deb0711a32 100644 (file)
@@ -55,18 +55,18 @@ static inline void seqlock_write_unlock(QemuSeqLock *sl)
 static inline unsigned seqlock_read_begin(QemuSeqLock *sl)
 {
     /* Always fail if a write is in progress.  */
-    unsigned ret = sl->sequence & ~1;
+    unsigned ret = atomic_read(&sl->sequence);
 
     /* Read sequence before reading other fields.  */
     smp_rmb();
-    return ret;
+    return ret & ~1;
 }
 
 static inline int seqlock_read_retry(const QemuSeqLock *sl, unsigned start)
 {
     /* Read other fields before reading final sequence.  */
     smp_rmb();
-    return unlikely(sl->sequence != start);
+    return unlikely(atomic_read(&sl->sequence) != start);
 }
 
 #endif