Skip to content

Commit eccc2da

Browse files
mrutland-armingomolnar
authored andcommitted
atomics/treewide: Make atomic_fetch_add_unless() optional
Several architectures these have a near-identical implementation based on atomic_read() and atomic_cmpxchg() which we can instead define in <linux/atomic.h>, so let's do so, using something close to the existing x86 implementation with try_cmpxchg(). Where an architecture provides its own atomic_fetch_add_unless(), it must define a preprocessor symbol for it. The instrumented atomics are updated accordingly. Note that arch/arc's existing atomic_fetch_add_unless() had redundant barriers, as these are already present in its atomic_cmpxchg() implementation. There should be no functional change as a result of this patch. Signed-off-by: Mark Rutland <mark.rutland@arm.com> Reviewed-by: Geert Uytterhoeven <geert@linux-m68k.org> Reviewed-by: Will Deacon <will.deacon@arm.com> Acked-by: Geert Uytterhoeven <geert@linux-m68k.org> Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org> Acked-by: Palmer Dabbelt <palmer@sifive.com> Cc: Boqun Feng <boqun.feng@gmail.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Vineet Gupta <vgupta@synopsys.com> Link: https://lore.kernel.org/lkml/20180621121321.4761-7-mark.rutland@arm.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent bef8282 commit eccc2da

21 files changed

Lines changed: 34 additions & 231 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ static __inline__ int atomic_fetch_add_unless(atomic_t *v, int a, int u)
235235
smp_mb();
236236
return old;
237237
}
238-
238+
#define atomic_fetch_add_unless atomic_fetch_add_unless
239239

240240
/**
241241
* atomic64_add_unless - add unless the number is a given value

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

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -308,34 +308,6 @@ ATOMIC_OPS(xor, ^=, CTOP_INST_AXOR_DI_R2_R2_R3)
308308
#undef ATOMIC_OP_RETURN
309309
#undef ATOMIC_OP
310310

311-
/**
312-
* atomic_fetch_add_unless - add unless the number is a given value
313-
* @v: pointer of type atomic_t
314-
* @a: the amount to add to v...
315-
* @u: ...unless v is equal to u.
316-
*
317-
* Atomically adds @a to @v, so long as it was not @u.
318-
* Returns the old value of @v
319-
*/
320-
#define atomic_fetch_add_unless(v, a, u) \
321-
({ \
322-
int c, old; \
323-
\
324-
/* \
325-
* Explicit full memory barrier needed before/after as \
326-
* LLOCK/SCOND thmeselves don't provide any such semantics \
327-
*/ \
328-
smp_mb(); \
329-
\
330-
c = atomic_read(v); \
331-
while (c != (u) && (old = atomic_cmpxchg((v), c, c + (a))) != c)\
332-
c = old; \
333-
\
334-
smp_mb(); \
335-
\
336-
c; \
337-
})
338-
339311
#define atomic_inc(v) atomic_add(1, v)
340312
#define atomic_dec(v) atomic_sub(1, v)
341313

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ static inline int atomic_fetch_add_unless(atomic_t *v, int a, int u)
156156

157157
return oldval;
158158
}
159+
#define atomic_fetch_add_unless atomic_fetch_add_unless
159160

160161
#else /* ARM_ARCH_6 */
161162

@@ -215,16 +216,6 @@ static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
215216
return ret;
216217
}
217218

218-
static inline int atomic_fetch_add_unless(atomic_t *v, int a, int u)
219-
{
220-
int c, old;
221-
222-
c = atomic_read(v);
223-
while (c != u && (old = atomic_cmpxchg((v), c, c + a)) != c)
224-
c = old;
225-
return c;
226-
}
227-
228219
#endif /* __LINUX_ARM_ARCH__ */
229220

230221
#define ATOMIC_OPS(op, c_op, asm_op) \

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@
125125
#define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
126126
#define atomic_sub_and_test(i, v) (atomic_sub_return((i), (v)) == 0)
127127
#define atomic_add_negative(i, v) (atomic_add_return((i), (v)) < 0)
128-
#define atomic_fetch_add_unless(v, a, u) ___atomic_add_unless(v, a, u,)
129128
#define atomic_andnot atomic_andnot
130129

131130
/*

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,6 @@ static inline int atomic_fetch_add_unless(atomic_t *v, int a, int u)
106106
arch_local_irq_restore(flags);
107107
return ret;
108108
}
109+
#define atomic_fetch_add_unless atomic_fetch_add_unless
109110

110111
#endif /* __ARCH_H8300_ATOMIC __ */

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ static inline int atomic_fetch_add_unless(atomic_t *v, int a, int u)
196196
);
197197
return __oldval;
198198
}
199+
#define atomic_fetch_add_unless atomic_fetch_add_unless
199200

200201
#define atomic_inc(v) atomic_add(1, (v))
201202
#define atomic_dec(v) atomic_sub(1, (v))

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -215,22 +215,6 @@ ATOMIC64_FETCH_OP(xor, ^)
215215
(cmpxchg(&((v)->counter), old, new))
216216
#define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
217217

218-
static __inline__ int atomic_fetch_add_unless(atomic_t *v, int a, int u)
219-
{
220-
int c, old;
221-
c = atomic_read(v);
222-
for (;;) {
223-
if (unlikely(c == (u)))
224-
break;
225-
old = atomic_cmpxchg((v), c, c + (a));
226-
if (likely(old == c))
227-
break;
228-
c = old;
229-
}
230-
return c;
231-
}
232-
233-
234218
static __inline__ long atomic64_add_unless(atomic64_t *v, long a, long u)
235219
{
236220
long c, old;

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -211,19 +211,4 @@ static inline int atomic_add_negative(int i, atomic_t *v)
211211
return c != 0;
212212
}
213213

214-
static __inline__ int atomic_fetch_add_unless(atomic_t *v, int a, int u)
215-
{
216-
int c, old;
217-
c = atomic_read(v);
218-
for (;;) {
219-
if (unlikely(c == (u)))
220-
break;
221-
old = atomic_cmpxchg((v), c, c + (a));
222-
if (likely(old == c))
223-
break;
224-
c = old;
225-
}
226-
return c;
227-
}
228-
229214
#endif /* __ARCH_M68K_ATOMIC __ */

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

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -274,30 +274,6 @@ static __inline__ int atomic_sub_if_positive(int i, atomic_t * v)
274274
#define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
275275
#define atomic_xchg(v, new) (xchg(&((v)->counter), (new)))
276276

277-
/**
278-
* atomic_fetch_add_unless - add unless the number is a given value
279-
* @v: pointer of type atomic_t
280-
* @a: the amount to add to v...
281-
* @u: ...unless v is equal to u.
282-
*
283-
* Atomically adds @a to @v, so long as it was not @u.
284-
* Returns the old value of @v.
285-
*/
286-
static __inline__ int atomic_fetch_add_unless(atomic_t *v, int a, int u)
287-
{
288-
int c, old;
289-
c = atomic_read(v);
290-
for (;;) {
291-
if (unlikely(c == (u)))
292-
break;
293-
old = atomic_cmpxchg((v), c, c + (a));
294-
if (likely(old == c))
295-
break;
296-
c = old;
297-
}
298-
return c;
299-
}
300-
301277
#define atomic_dec_return(v) atomic_sub_return(1, (v))
302278
#define atomic_inc_return(v) atomic_add_return(1, (v))
303279

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

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -77,30 +77,6 @@ static __inline__ int atomic_read(const atomic_t *v)
7777
#define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
7878
#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
7979

80-
/**
81-
* atomic_fetch_add_unless - add unless the number is a given value
82-
* @v: pointer of type atomic_t
83-
* @a: the amount to add to v...
84-
* @u: ...unless v is equal to u.
85-
*
86-
* Atomically adds @a to @v, so long as it was not @u.
87-
* Returns the old value of @v.
88-
*/
89-
static __inline__ int atomic_fetch_add_unless(atomic_t *v, int a, int u)
90-
{
91-
int c, old;
92-
c = atomic_read(v);
93-
for (;;) {
94-
if (unlikely(c == (u)))
95-
break;
96-
old = atomic_cmpxchg((v), c, c + (a));
97-
if (likely(old == c))
98-
break;
99-
c = old;
100-
}
101-
return c;
102-
}
103-
10480
#define ATOMIC_OP(op, c_op) \
10581
static __inline__ void atomic_##op(int i, atomic_t *v) \
10682
{ \

0 commit comments

Comments
 (0)