Skip to content

Commit e0a25ab

Browse files
Peter Zijlstragregkh
authored andcommitted
mips/atomic: Fix loongson_llsc_mb() wreckage
[ Upstream commit 1c6c1ca ] The comment describing the loongson_llsc_mb() reorder case doesn't make any sense what so ever. Instruction re-ordering is not an SMP artifact, but rather a CPU local phenomenon. Clarify the comment by explaining that these issue cause a coherence fail. For the branch speculation case; if futex_atomic_cmpxchg_inatomic() needs one at the bne branch target, then surely the normal __cmpxch_asm() implementation does too. We cannot rely on the barriers from cmpxchg() because cmpxchg_local() is implemented with the same macro, and branch prediction and speculation are, too, CPU local. Fixes: e02e07e ("MIPS: Loongson: Introduce and use loongson_llsc_mb()") Cc: Huacai Chen <chenhc@lemote.com> Cc: Huang Pei <huangpei@loongson.cn> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Paul Burton <paul.burton@mips.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 8d46fca commit e0a25ab

5 files changed

Lines changed: 32 additions & 16 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,19 +193,20 @@ static __inline__ int atomic_sub_if_positive(int i, atomic_t * v)
193193
if (kernel_uses_llsc) {
194194
int temp;
195195

196+
loongson_llsc_mb();
196197
__asm__ __volatile__(
197198
" .set push \n"
198199
" .set "MIPS_ISA_LEVEL" \n"
199200
"1: ll %1, %2 # atomic_sub_if_positive\n"
200201
" .set pop \n"
201202
" subu %0, %1, %3 \n"
202203
" move %1, %0 \n"
203-
" bltz %0, 1f \n"
204+
" bltz %0, 2f \n"
204205
" .set push \n"
205206
" .set "MIPS_ISA_LEVEL" \n"
206207
" sc %1, %2 \n"
207208
"\t" __scbeqz " %1, 1b \n"
208-
"1: \n"
209+
"2: \n"
209210
" .set pop \n"
210211
: "=&r" (result), "=&r" (temp),
211212
"+" GCC_OFF_SMALL_ASM() (v->counter)

‎arch/mips/include/asm/barrier.h‎

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -238,36 +238,40 @@
238238

239239
/*
240240
* Some Loongson 3 CPUs have a bug wherein execution of a memory access (load,
241-
* store or pref) in between an ll & sc can cause the sc instruction to
241+
* store or prefetch) in between an LL & SC can cause the SC instruction to
242242
* erroneously succeed, breaking atomicity. Whilst it's unusual to write code
243243
* containing such sequences, this bug bites harder than we might otherwise
244244
* expect due to reordering & speculation:
245245
*
246-
* 1) A memory access appearing prior to the ll in program order may actually
247-
* be executed after the ll - this is the reordering case.
246+
* 1) A memory access appearing prior to the LL in program order may actually
247+
* be executed after the LL - this is the reordering case.
248248
*
249-
* In order to avoid this we need to place a memory barrier (ie. a sync
250-
* instruction) prior to every ll instruction, in between it & any earlier
251-
* memory access instructions. Many of these cases are already covered by
252-
* smp_mb__before_llsc() but for the remaining cases, typically ones in
253-
* which multiple CPUs may operate on a memory location but ordering is not
254-
* usually guaranteed, we use loongson_llsc_mb() below.
249+
* In order to avoid this we need to place a memory barrier (ie. a SYNC
250+
* instruction) prior to every LL instruction, in between it and any earlier
251+
* memory access instructions.
255252
*
256253
* This reordering case is fixed by 3A R2 CPUs, ie. 3A2000 models and later.
257254
*
258-
* 2) If a conditional branch exists between an ll & sc with a target outside
259-
* of the ll-sc loop, for example an exit upon value mismatch in cmpxchg()
255+
* 2) If a conditional branch exists between an LL & SC with a target outside
256+
* of the LL-SC loop, for example an exit upon value mismatch in cmpxchg()
260257
* or similar, then misprediction of the branch may allow speculative
261-
* execution of memory accesses from outside of the ll-sc loop.
258+
* execution of memory accesses from outside of the LL-SC loop.
262259
*
263-
* In order to avoid this we need a memory barrier (ie. a sync instruction)
260+
* In order to avoid this we need a memory barrier (ie. a SYNC instruction)
264261
* at each affected branch target, for which we also use loongson_llsc_mb()
265262
* defined below.
266263
*
267264
* This case affects all current Loongson 3 CPUs.
265+
*
266+
* The above described cases cause an error in the cache coherence protocol;
267+
* such that the Invalidate of a competing LL-SC goes 'missing' and SC
268+
* erroneously observes its core still has Exclusive state and lets the SC
269+
* proceed.
270+
*
271+
* Therefore the error only occurs on SMP systems.
268272
*/
269273
#ifdef CONFIG_CPU_LOONGSON3_WORKAROUNDS /* Loongson-3's LLSC workaround */
270-
#define loongson_llsc_mb() __asm__ __volatile__(__WEAK_LLSC_MB : : :"memory")
274+
#define loongson_llsc_mb() __asm__ __volatile__("sync" : : :"memory")
271275
#else
272276
#define loongson_llsc_mb() do { } while (0)
273277
#endif

‎arch/mips/include/asm/bitops.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ static inline int test_and_set_bit(unsigned long nr,
249249
unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
250250
unsigned long temp;
251251

252+
loongson_llsc_mb();
252253
do {
253254
__asm__ __volatile__(
254255
" .set push \n"
@@ -305,6 +306,7 @@ static inline int test_and_set_bit_lock(unsigned long nr,
305306
unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
306307
unsigned long temp;
307308

309+
loongson_llsc_mb();
308310
do {
309311
__asm__ __volatile__(
310312
" .set push \n"
@@ -364,6 +366,7 @@ static inline int test_and_clear_bit(unsigned long nr,
364366
unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
365367
unsigned long temp;
366368

369+
loongson_llsc_mb();
367370
do {
368371
__asm__ __volatile__(
369372
" " __LL "%0, %1 # test_and_clear_bit \n"
@@ -379,6 +382,7 @@ static inline int test_and_clear_bit(unsigned long nr,
379382
unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
380383
unsigned long temp;
381384

385+
loongson_llsc_mb();
382386
do {
383387
__asm__ __volatile__(
384388
" .set push \n"
@@ -438,6 +442,7 @@ static inline int test_and_change_bit(unsigned long nr,
438442
unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
439443
unsigned long temp;
440444

445+
loongson_llsc_mb();
441446
do {
442447
__asm__ __volatile__(
443448
" .set push \n"

‎arch/mips/include/asm/cmpxchg.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ extern unsigned long __xchg_called_with_bad_pointer(void)
4646
__typeof(*(m)) __ret; \
4747
\
4848
if (kernel_uses_llsc) { \
49+
loongson_llsc_mb(); \
4950
__asm__ __volatile__( \
5051
" .set push \n" \
5152
" .set noat \n" \
@@ -117,6 +118,7 @@ static inline unsigned long __xchg(volatile void *ptr, unsigned long x,
117118
__typeof(*(m)) __ret; \
118119
\
119120
if (kernel_uses_llsc) { \
121+
loongson_llsc_mb(); \
120122
__asm__ __volatile__( \
121123
" .set push \n" \
122124
" .set noat \n" \
@@ -134,6 +136,7 @@ static inline unsigned long __xchg(volatile void *ptr, unsigned long x,
134136
: "=&r" (__ret), "=" GCC_OFF_SMALL_ASM() (*m) \
135137
: GCC_OFF_SMALL_ASM() (*m), "Jr" (old), "Jr" (new) \
136138
: "memory"); \
139+
loongson_llsc_mb(); \
137140
} else { \
138141
unsigned long __flags; \
139142
\
@@ -229,6 +232,7 @@ static inline unsigned long __cmpxchg64(volatile void *ptr,
229232
*/
230233
local_irq_save(flags);
231234

235+
loongson_llsc_mb();
232236
asm volatile(
233237
" .set push \n"
234238
" .set " MIPS_ISA_ARCH_LEVEL " \n"
@@ -274,6 +278,7 @@ static inline unsigned long __cmpxchg64(volatile void *ptr,
274278
"r" (old),
275279
"r" (new)
276280
: "memory");
281+
loongson_llsc_mb();
277282

278283
local_irq_restore(flags);
279284
return ret;

‎arch/mips/kernel/syscall.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ static inline int mips_atomic_set(unsigned long addr, unsigned long new)
132132
[efault] "i" (-EFAULT)
133133
: "memory");
134134
} else if (cpu_has_llsc) {
135+
loongson_llsc_mb();
135136
__asm__ __volatile__ (
136137
" .set push \n"
137138
" .set "MIPS_ISA_ARCH_LEVEL" \n"

0 commit comments

Comments
 (0)