Skip to content

Commit e3e72ab

Browse files
Peter Zijlstraingomolnar
authored andcommitted
atomic: Implement atomic_read_ctrl()
Provide atomic_read_ctrl() to mirror READ_ONCE_CTRL(), such that we can more conveniently use atomics in control dependencies. Since we can assume atomic_read() implies a READ_ONCE(), we must only emit an extra smp_read_barrier_depends() in order to upgrade to READ_ONCE_CTRL() semantics. Requested-by: Dmitry Vyukov <dvyukov@google.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Acked-by: Will Deacon <will.deacon@arm.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: linux-kernel@vger.kernel.org Cc: oleg@redhat.com Link: http://lkml.kernel.org/r/20150918115637.GM3604@twins.programming.kicks-ass.net Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 62e8a32 commit e3e72ab

3 files changed

Lines changed: 29 additions & 9 deletions

File tree

‎Documentation/memory-barriers.txt‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,8 @@ as follows:
637637
b = p; /* BUG: Compiler and CPU can both reorder!!! */
638638

639639
Finally, the READ_ONCE_CTRL() includes an smp_read_barrier_depends()
640-
that DEC Alpha needs in order to respect control depedencies.
640+
that DEC Alpha needs in order to respect control depedencies. Alternatively
641+
use one of atomic{,64}_read_ctrl().
641642

642643
So don't leave out the READ_ONCE_CTRL().
643644

@@ -796,9 +797,9 @@ site: https://www.cl.cam.ac.uk/~pes20/ppcmem/index.html.
796797

797798
In summary:
798799

799-
(*) Control dependencies must be headed by READ_ONCE_CTRL().
800-
Or, as a much less preferable alternative, interpose
801-
smp_read_barrier_depends() between a READ_ONCE() and the
800+
(*) Control dependencies must be headed by READ_ONCE_CTRL(),
801+
atomic{,64}_read_ctrl(). Or, as a much less preferable alternative,
802+
interpose smp_read_barrier_depends() between a READ_ONCE() and the
802803
control-dependent write.
803804

804805
(*) Control dependencies can order prior loads against later stores.
@@ -820,10 +821,10 @@ In summary:
820821
and WRITE_ONCE() can help to preserve the needed conditional.
821822

822823
(*) Control dependencies require that the compiler avoid reordering the
823-
dependency into nonexistence. Careful use of READ_ONCE_CTRL()
824-
or smp_read_barrier_depends() can help to preserve your control
825-
dependency. Please see the Compiler Barrier section for more
826-
information.
824+
dependency into nonexistence. Careful use of READ_ONCE_CTRL(),
825+
atomic{,64}_read_ctrl() or smp_read_barrier_depends() can help to
826+
preserve your control dependency. Please see the Compiler Barrier
827+
section for more information.
827828

828829
(*) Control dependencies pair normally with other types of barriers.
829830

‎include/asm-generic/atomic-long.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@ typedef atomic_t atomic_long_t;
3535
#endif
3636

3737
#define ATOMIC_LONG_READ_OP(mo) \
38-
static inline long atomic_long_read##mo(atomic_long_t *l) \
38+
static inline long atomic_long_read##mo(const atomic_long_t *l) \
3939
{ \
4040
ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l; \
4141
\
4242
return (long)ATOMIC_LONG_PFX(_read##mo)(v); \
4343
}
4444
ATOMIC_LONG_READ_OP()
4545
ATOMIC_LONG_READ_OP(_acquire)
46+
ATOMIC_LONG_READ_OP(_ctrl)
4647

4748
#undef ATOMIC_LONG_READ_OP
4849

‎include/linux/atomic.h‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44
#include <asm/atomic.h>
55
#include <asm/barrier.h>
66

7+
#ifndef atomic_read_ctrl
8+
static inline int atomic_read_ctrl(const atomic_t *v)
9+
{
10+
int val = atomic_read(v);
11+
smp_read_barrier_depends(); /* Enforce control dependency. */
12+
return val;
13+
}
14+
#endif
15+
716
/*
817
* Relaxed variants of xchg, cmpxchg and some atomic operations.
918
*
@@ -455,6 +464,15 @@ static inline int atomic_dec_if_positive(atomic_t *v)
455464
#include <asm-generic/atomic64.h>
456465
#endif
457466

467+
#ifndef atomic64_read_ctrl
468+
static inline long long atomic64_read_ctrl(const atomic64_t *v)
469+
{
470+
long long val = atomic64_read(v);
471+
smp_read_barrier_depends(); /* Enforce control dependency. */
472+
return val;
473+
}
474+
#endif
475+
458476
#ifndef atomic64_andnot
459477
static inline void atomic64_andnot(long long i, atomic64_t *v)
460478
{

0 commit comments

Comments
 (0)