misc: Fix allocation 'borrowed' by vhost_net
[safe/jmp/linux-2.6] / include / linux / seqlock.h
index 4600093..632205c 100644 (file)
@@ -2,7 +2,7 @@
 #define __LINUX_SEQLOCK_H
 /*
  * Reader/writer consistent mechanism without starving writers. This type of
- * lock for data where the reader wants a consitent set of information
+ * lock for data where the reader wants a consistent set of information
  * and is willing to retry if the information changes.  Readers never
  * block but they may have to retry if a writer is in
  * progress. Writers do not wait for readers. 
@@ -44,8 +44,11 @@ typedef struct {
 #define SEQLOCK_UNLOCKED \
                 __SEQLOCK_UNLOCKED(old_style_seqlock_init)
 
-#define seqlock_init(x) \
-               do { *(x) = (seqlock_t) __SEQLOCK_UNLOCKED(x); } while (0)
+#define seqlock_init(x)                                        \
+       do {                                            \
+               (x)->sequence = 0;                      \
+               spin_lock_init(&(x)->lock);             \
+       } while (0)
 
 #define DEFINE_SEQLOCK(x) \
                seqlock_t x = __SEQLOCK_UNLOCKED(x)
@@ -58,10 +61,10 @@ static inline void write_seqlock(seqlock_t *sl)
 {
        spin_lock(&sl->lock);
        ++sl->sequence;
-       smp_wmb();                      
-}      
+       smp_wmb();
+}
 
-static inline void write_sequnlock(seqlock_t *sl) 
+static inline void write_sequnlock(seqlock_t *sl)
 {
        smp_wmb();
        sl->sequence++;
@@ -74,7 +77,7 @@ static inline int write_tryseqlock(seqlock_t *sl)
 
        if (ret) {
                ++sl->sequence;
-               smp_wmb();                      
+               smp_wmb();
        }
        return ret;
 }
@@ -82,23 +85,29 @@ static inline int write_tryseqlock(seqlock_t *sl)
 /* Start of read calculation -- fetch last complete writer token */
 static __always_inline unsigned read_seqbegin(const seqlock_t *sl)
 {
-       unsigned ret = sl->sequence;
+       unsigned ret;
+
+repeat:
+       ret = sl->sequence;
        smp_rmb();
+       if (unlikely(ret & 1)) {
+               cpu_relax();
+               goto repeat;
+       }
+
        return ret;
 }
 
-/* Test if reader processed invalid data.
- * If initial values is odd, 
- *     then writer had already started when section was entered
- * If sequence value changed
- *     then writer changed data while in section
- *    
- * Using xor saves one conditional branch.
+/*
+ * Test if reader processed invalid data.
+ *
+ * If sequence value changed then writer changed data while in section.
  */
-static __always_inline int read_seqretry(const seqlock_t *sl, unsigned iv)
+static __always_inline int read_seqretry(const seqlock_t *sl, unsigned start)
 {
        smp_rmb();
-       return (iv & 1) | (sl->sequence ^ iv);
+
+       return (sl->sequence != start);
 }
 
 
@@ -119,20 +128,26 @@ typedef struct seqcount {
 /* Start of read using pointer to a sequence counter only.  */
 static inline unsigned read_seqcount_begin(const seqcount_t *s)
 {
-       unsigned ret = s->sequence;
+       unsigned ret;
+
+repeat:
+       ret = s->sequence;
        smp_rmb();
+       if (unlikely(ret & 1)) {
+               cpu_relax();
+               goto repeat;
+       }
        return ret;
 }
 
-/* Test if reader processed invalid data.
- * Equivalent to: iv is odd or sequence number has changed.
- *                (iv & 1) || (*s != iv)
- * Using xor saves one conditional branch.
+/*
+ * Test if reader processed invalid data because sequence number has changed.
  */
-static inline int read_seqcount_retry(const seqcount_t *s, unsigned iv)
+static inline int read_seqcount_retry(const seqcount_t *s, unsigned start)
 {
        smp_rmb();
-       return (iv & 1) | (s->sequence ^ iv);
+
+       return s->sequence != start;
 }