Skip to content

Commit 2f62427

Browse files
committed
Fix __read_seqcount_begin() to use ACCESS_ONCE for sequence value read
We really need to use a ACCESS_ONCE() on the sequence value read in __read_seqcount_begin(), because otherwise the compiler might end up reloading the value in between the test and the return of it. As a result, it might end up returning an odd value (which means that a write is in progress). If the reader is then fast enough that that odd value is still the current one when the read_seqcount_retry() is done, we might end up with a "successful" read sequence, even despite the concurrent write being active. In practice this probably never really happens - there just isn't anything else going on around the read of the sequence count, and the common case is that we end up having a read barrier immediately afterwards. So the code sequence in which gcc might decide to reaload from memory is small, and there's no reason to believe it would ever actually do the reload. But if the compiler ever were to decide to do so, it would be incredibly annoying to debug. Let's just make sure. Cc: [email protected] Signed-off-by: Linus Torvalds <[email protected]>
1 parent f0f376f commit 2f62427

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

include/linux/seqlock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ static inline unsigned __read_seqcount_begin(const seqcount_t *s)
141141
unsigned ret;
142142

143143
repeat:
144-
ret = s->sequence;
144+
ret = ACCESS_ONCE(s->sequence);
145145
if (unlikely(ret & 1)) {
146146
cpu_relax();
147147
goto repeat;

0 commit comments

Comments
 (0)