Skip to content

Commit fd2efaa

Browse files
mrutland-armingomolnar
authored andcommitted
locking/atomics: Rework ordering barriers
Currently architectures can override __atomic_op_*() to define the barriers used before/after a relaxed atomic when used to build acquire/release/fence variants. This has the unfortunate property of requiring the architecture to define the full wrapper for the atomics, rather than just the barriers they care about, and gets in the way of generating atomics which can be easily read. Instead, this patch has architectures define an optional set of barriers: * __atomic_acquire_fence() * __atomic_release_fence() * __atomic_pre_full_fence() * __atomic_post_full_fence() ... which <linux/atomic.h> uses to build the wrappers. It would be nice if we could undef these, along with the __atomic_op_*() wrappers, but that would break the cmpxchg() wrappers, which are written in preprocessor. Undefs would have been nice, but alas. There should be no functional change as a result of this patch. Signed-off-by: Mark Rutland <mark.rutland@arm.com> Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org> Acked-by: Will Deacon <will.deacon@arm.com> Cc: Andrea Parri <parri.andrea@gmail.com> Cc: Boqun Feng <boqun.feng@gmail.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: andy.shevchenko@gmail.com Cc: arnd@arndb.de Cc: aryabinin@virtuozzo.com Cc: catalin.marinas@arm.com Cc: dvyukov@google.com Cc: glider@google.com Cc: linux-arm-kernel@lists.infradead.org Cc: peter@hurleysoftware.com Link: http://lkml.kernel.org/r/20180716113017.3909-7-mark.rutland@arm.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 4d2b25f commit fd2efaa

4 files changed

Lines changed: 36 additions & 44 deletions

File tree

‎arch/alpha/include/asm/atomic.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
* To ensure dependency ordering is preserved for the _relaxed and
1919
* _release atomics, an smp_read_barrier_depends() is unconditionally
2020
* inserted into the _relaxed variants, which are used to build the
21-
* barriered versions. To avoid redundant back-to-back fences, we can
22-
* define the _acquire and _fence versions explicitly.
21+
* barriered versions. Avoid redundant back-to-back fences in the
22+
* _acquire and _fence versions.
2323
*/
24-
#define __atomic_op_acquire(op, args...) op##_relaxed(args)
25-
#define __atomic_op_fence __atomic_op_release
24+
#define __atomic_acquire_fence()
25+
#define __atomic_post_full_fence()
2626

2727
#define ATOMIC_INIT(i) { (i) }
2828
#define ATOMIC64_INIT(i) { (i) }

‎arch/powerpc/include/asm/atomic.h‎

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,11 @@
1818
* a "bne-" instruction at the end, so an isync is enough as a acquire barrier
1919
* on the platform without lwsync.
2020
*/
21-
#define __atomic_op_acquire(op, args...) \
22-
({ \
23-
typeof(op##_relaxed(args)) __ret = op##_relaxed(args); \
24-
__asm__ __volatile__(PPC_ACQUIRE_BARRIER "" : : : "memory"); \
25-
__ret; \
26-
})
27-
28-
#define __atomic_op_release(op, args...) \
29-
({ \
30-
__asm__ __volatile__(PPC_RELEASE_BARRIER "" : : : "memory"); \
31-
op##_relaxed(args); \
32-
})
21+
#define __atomic_acquire_fence() \
22+
__asm__ __volatile__(PPC_ACQUIRE_BARRIER "" : : : "memory")
23+
24+
#define __atomic_release_fence() \
25+
__asm__ __volatile__(PPC_RELEASE_BARRIER "" : : : "memory")
3326

3427
static __inline__ int atomic_read(const atomic_t *v)
3528
{

‎arch/riscv/include/asm/atomic.h‎

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,11 @@
2525

2626
#define ATOMIC_INIT(i) { (i) }
2727

28-
#define __atomic_op_acquire(op, args...) \
29-
({ \
30-
typeof(op##_relaxed(args)) __ret = op##_relaxed(args); \
31-
__asm__ __volatile__(RISCV_ACQUIRE_BARRIER "" ::: "memory"); \
32-
__ret; \
33-
})
34-
35-
#define __atomic_op_release(op, args...) \
36-
({ \
37-
__asm__ __volatile__(RISCV_RELEASE_BARRIER "" ::: "memory"); \
38-
op##_relaxed(args); \
39-
})
28+
#define __atomic_acquire_fence() \
29+
__asm__ __volatile__(RISCV_ACQUIRE_BARRIER "" ::: "memory")
30+
31+
#define __atomic_release_fence() \
32+
__asm__ __volatile__(RISCV_RELEASE_BARRIER "" ::: "memory");
4033

4134
static __always_inline int atomic_read(const atomic_t *v)
4235
{

‎include/linux/atomic.h‎

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,40 +38,46 @@
3838
* barriers on top of the relaxed variant. In the case where the relaxed
3939
* variant is already fully ordered, no additional barriers are needed.
4040
*
41-
* Besides, if an arch has a special barrier for acquire/release, it could
42-
* implement its own __atomic_op_* and use the same framework for building
43-
* variants
44-
*
45-
* If an architecture overrides __atomic_op_acquire() it will probably want
46-
* to define smp_mb__after_spinlock().
41+
* If an architecture overrides __atomic_acquire_fence() it will probably
42+
* want to define smp_mb__after_spinlock().
4743
*/
48-
#ifndef __atomic_op_acquire
44+
#ifndef __atomic_acquire_fence
45+
#define __atomic_acquire_fence smp_mb__after_atomic
46+
#endif
47+
48+
#ifndef __atomic_release_fence
49+
#define __atomic_release_fence smp_mb__before_atomic
50+
#endif
51+
52+
#ifndef __atomic_pre_full_fence
53+
#define __atomic_pre_full_fence smp_mb__before_atomic
54+
#endif
55+
56+
#ifndef __atomic_post_full_fence
57+
#define __atomic_post_full_fence smp_mb__after_atomic
58+
#endif
59+
4960
#define __atomic_op_acquire(op, args...) \
5061
({ \
5162
typeof(op##_relaxed(args)) __ret = op##_relaxed(args); \
52-
smp_mb__after_atomic(); \
63+
__atomic_acquire_fence(); \
5364
__ret; \
5465
})
55-
#endif
5666

57-
#ifndef __atomic_op_release
5867
#define __atomic_op_release(op, args...) \
5968
({ \
60-
smp_mb__before_atomic(); \
69+
__atomic_release_fence(); \
6170
op##_relaxed(args); \
6271
})
63-
#endif
6472

65-
#ifndef __atomic_op_fence
6673
#define __atomic_op_fence(op, args...) \
6774
({ \
6875
typeof(op##_relaxed(args)) __ret; \
69-
smp_mb__before_atomic(); \
76+
__atomic_pre_full_fence(); \
7077
__ret = op##_relaxed(args); \
71-
smp_mb__after_atomic(); \
78+
__atomic_post_full_fence(); \
7279
__ret; \
7380
})
74-
#endif
7581

7682
/* atomic_add_return_relaxed */
7783
#ifndef atomic_add_return_relaxed

0 commit comments

Comments
 (0)