Skip to content

Commit bf07132

Browse files
melverpaulmckrcu
authored andcommitted
seqlock: Require WRITE_ONCE surrounding raw_seqcount_barrier
This patch proposes to require marked atomic accesses surrounding raw_write_seqcount_barrier. We reason that otherwise there is no way to guarantee propagation nor atomicity of writes before/after the barrier [1]. For example, consider the compiler tears stores either before or after the barrier; in this case, readers may observe a partial value, and because readers are unaware that writes are going on (writes are not in a seq-writer critical section), will complete the seq-reader critical section while having observed some partial state. [1] https://lwn.net/Articles/793253/ This came up when designing and implementing KCSAN, because KCSAN would flag these accesses as data-races. After careful analysis, our reasoning as above led us to conclude that the best thing to do is to propose an amendment to the raw_seqcount_barrier usage. Signed-off-by: Marco Elver <elver@google.com> Acked-by: Paul E. McKenney <paulmck@kernel.org> Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
1 parent 88ecd15 commit bf07132

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

‎include/linux/seqlock.h‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,13 @@ static inline void raw_write_seqcount_end(seqcount_t *s)
265265
* usual consistency guarantee. It is one wmb cheaper, because we can
266266
* collapse the two back-to-back wmb()s.
267267
*
268+
* Note that, writes surrounding the barrier should be declared atomic (e.g.
269+
* via WRITE_ONCE): a) to ensure the writes become visible to other threads
270+
* atomically, avoiding compiler optimizations; b) to document which writes are
271+
* meant to propagate to the reader critical section. This is necessary because
272+
* neither writes before and after the barrier are enclosed in a seq-writer
273+
* critical section that would ensure readers are aware of ongoing writes.
274+
*
268275
* seqcount_t seq;
269276
* bool X = true, Y = false;
270277
*
@@ -284,11 +291,11 @@ static inline void raw_write_seqcount_end(seqcount_t *s)
284291
*
285292
* void write(void)
286293
* {
287-
* Y = true;
294+
* WRITE_ONCE(Y, true);
288295
*
289296
* raw_write_seqcount_barrier(seq);
290297
*
291-
* X = false;
298+
* WRITE_ONCE(X, false);
292299
* }
293300
*/
294301
static inline void raw_write_seqcount_barrier(seqcount_t *s)

0 commit comments

Comments
 (0)