1212 * Atomic exchange.
1313 * Since it can be used to implement critical sections
1414 * it must clobber "memory" (also for interrupts in UP).
15+ *
16+ * The leading and the trailing memory barriers guarantee that these
17+ * operations are fully ordered.
18+ *
1519 */
1620
1721static inline unsigned long
1822____xchg (_u8 , volatile char * m , unsigned long val )
1923{
2024 unsigned long ret , tmp , addr64 ;
2125
26+ smp_mb ();
2227 __asm__ __volatile__(
2328 " andnot %4,7,%3\n"
2429 " insbl %1,%4,%1\n"
@@ -43,6 +48,7 @@ ____xchg(_u16, volatile short *m, unsigned long val)
4348{
4449 unsigned long ret , tmp , addr64 ;
4550
51+ smp_mb ();
4652 __asm__ __volatile__(
4753 " andnot %4,7,%3\n"
4854 " inswl %1,%4,%1\n"
@@ -67,6 +73,7 @@ ____xchg(_u32, volatile int *m, unsigned long val)
6773{
6874 unsigned long dummy ;
6975
76+ smp_mb ();
7077 __asm__ __volatile__(
7178 "1: ldl_l %0,%4\n"
7279 " bis $31,%3,%1\n"
@@ -87,6 +94,7 @@ ____xchg(_u64, volatile long *m, unsigned long val)
8794{
8895 unsigned long dummy ;
8996
97+ smp_mb ();
9098 __asm__ __volatile__(
9199 "1: ldq_l %0,%4\n"
92100 " bis $31,%3,%1\n"
@@ -128,17 +136,20 @@ ____xchg(, volatile void *ptr, unsigned long x, int size)
128136 * store NEW in MEM. Return the initial value in MEM. Success is
129137 * indicated by comparing RETURN with OLD.
130138 *
131- * The memory barrier should be placed in SMP only when we actually
132- * make the change. If we don't change anything (so if the returned
133- * prev is equal to old) then we aren't acquiring anything new and
134- * we don't need any memory barrier as far I can tell.
139+ * The leading and the trailing memory barriers guarantee that these
140+ * operations are fully ordered.
141+ *
142+ * The trailing memory barrier is placed in SMP unconditionally, in
143+ * order to guarantee that dependency ordering is preserved when a
144+ * dependency is headed by an unsuccessful operation.
135145 */
136146
137147static inline unsigned long
138148____cmpxchg (_u8 , volatile char * m , unsigned char old , unsigned char new )
139149{
140150 unsigned long prev , tmp , cmp , addr64 ;
141151
152+ smp_mb ();
142153 __asm__ __volatile__(
143154 " andnot %5,7,%4\n"
144155 " insbl %1,%5,%1\n"
@@ -150,8 +161,8 @@ ____cmpxchg(_u8, volatile char *m, unsigned char old, unsigned char new)
150161 " or %1,%2,%2\n"
151162 " stq_c %2,0(%4)\n"
152163 " beq %2,3f\n"
153- __ASM__MB
154164 "2:\n"
165+ __ASM__MB
155166 ".subsection 2\n"
156167 "3: br 1b\n"
157168 ".previous"
@@ -166,6 +177,7 @@ ____cmpxchg(_u16, volatile short *m, unsigned short old, unsigned short new)
166177{
167178 unsigned long prev , tmp , cmp , addr64 ;
168179
180+ smp_mb ();
169181 __asm__ __volatile__(
170182 " andnot %5,7,%4\n"
171183 " inswl %1,%5,%1\n"
@@ -177,8 +189,8 @@ ____cmpxchg(_u16, volatile short *m, unsigned short old, unsigned short new)
177189 " or %1,%2,%2\n"
178190 " stq_c %2,0(%4)\n"
179191 " beq %2,3f\n"
180- __ASM__MB
181192 "2:\n"
193+ __ASM__MB
182194 ".subsection 2\n"
183195 "3: br 1b\n"
184196 ".previous"
@@ -193,15 +205,16 @@ ____cmpxchg(_u32, volatile int *m, int old, int new)
193205{
194206 unsigned long prev , cmp ;
195207
208+ smp_mb ();
196209 __asm__ __volatile__(
197210 "1: ldl_l %0,%5\n"
198211 " cmpeq %0,%3,%1\n"
199212 " beq %1,2f\n"
200213 " mov %4,%1\n"
201214 " stl_c %1,%2\n"
202215 " beq %1,3f\n"
203- __ASM__MB
204216 "2:\n"
217+ __ASM__MB
205218 ".subsection 2\n"
206219 "3: br 1b\n"
207220 ".previous"
@@ -216,15 +229,16 @@ ____cmpxchg(_u64, volatile long *m, unsigned long old, unsigned long new)
216229{
217230 unsigned long prev , cmp ;
218231
232+ smp_mb ();
219233 __asm__ __volatile__(
220234 "1: ldq_l %0,%5\n"
221235 " cmpeq %0,%3,%1\n"
222236 " beq %1,2f\n"
223237 " mov %4,%1\n"
224238 " stq_c %1,%2\n"
225239 " beq %1,3f\n"
226- __ASM__MB
227240 "2:\n"
241+ __ASM__MB
228242 ".subsection 2\n"
229243 "3: br 1b\n"
230244 ".previous"
0 commit comments