Skip to content

Commit 18cc181

Browse files
mrutland-armIngo Molnar
authored andcommitted
atomics/treewide: Make test ops optional
Some of the atomics return the result of a test applied after the atomic operation, and almost all architectures implement these as trivial wrappers around the underlying atomic. Specifically: * <atomic>_inc_and_test(v) is (<atomic>_inc_return(v) == 0) * <atomic>_dec_and_test(v) is (<atomic>_dec_return(v) == 0) * <atomic>_sub_and_test(i, v) is (<atomic>_sub_return(i, v) == 0) * <atomic>_add_negative(i, v) is (<atomic>_add_return(i, v) < 0) Rather than have these definitions duplicated in all architectures, with minor inconsistencies in formatting and documentation, let's make these operations optional, with default fallbacks as above. Implementations must now provide a preprocessor symbol. The instrumented atomics are updated accordingly. Both x86 and m68k have custom implementations, which are left as-is, given preprocessor symbols to avoid being overridden. There should be no functional change as a result of this patch. Signed-off-by: Mark Rutland <mark.rutland@arm.com> 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> Link: https://lore.kernel.org/lkml/20180621121321.4761-16-mark.rutland@arm.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 3567013 commit 18cc181

File tree

24 files changed

+160
-410
lines changed

24 files changed

+160
-410
lines changed

arch/alpha/include/asm/atomic.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -297,24 +297,12 @@ static inline long atomic64_dec_if_positive(atomic64_t *v)
297297
return old - 1;
298298
}
299299

300-
#define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
301-
#define atomic64_add_negative(a, v) (atomic64_add_return((a), (v)) < 0)
302-
303300
#define atomic_dec_return(v) atomic_sub_return(1,(v))
304301
#define atomic64_dec_return(v) atomic64_sub_return(1,(v))
305302

306303
#define atomic_inc_return(v) atomic_add_return(1,(v))
307304
#define atomic64_inc_return(v) atomic64_add_return(1,(v))
308305

309-
#define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
310-
#define atomic64_sub_and_test(i,v) (atomic64_sub_return((i), (v)) == 0)
311-
312-
#define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0)
313-
#define atomic64_inc_and_test(v) (atomic64_add_return(1, (v)) == 0)
314-
315-
#define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
316-
#define atomic64_dec_and_test(v) (atomic64_sub_return(1, (v)) == 0)
317-
318306
#define atomic_inc(v) atomic_add(1,(v))
319307
#define atomic64_inc(v) atomic64_add(1,(v))
320308

arch/arc/include/asm/atomic.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -311,14 +311,8 @@ ATOMIC_OPS(xor, ^=, CTOP_INST_AXOR_DI_R2_R2_R3)
311311
#define atomic_inc(v) atomic_add(1, v)
312312
#define atomic_dec(v) atomic_sub(1, v)
313313

314-
#define atomic_inc_and_test(v) (atomic_add_return(1, v) == 0)
315-
#define atomic_dec_and_test(v) (atomic_sub_return(1, v) == 0)
316314
#define atomic_inc_return(v) atomic_add_return(1, (v))
317315
#define atomic_dec_return(v) atomic_sub_return(1, (v))
318-
#define atomic_sub_and_test(i, v) (atomic_sub_return(i, v) == 0)
319-
320-
#define atomic_add_negative(i, v) (atomic_add_return(i, v) < 0)
321-
322316

323317
#ifdef CONFIG_GENERIC_ATOMIC64
324318

@@ -566,14 +560,10 @@ static inline long long atomic64_fetch_add_unless(atomic64_t *v, long long a,
566560
}
567561
#define atomic64_fetch_add_unless atomic64_fetch_add_unless
568562

569-
#define atomic64_add_negative(a, v) (atomic64_add_return((a), (v)) < 0)
570563
#define atomic64_inc(v) atomic64_add(1LL, (v))
571564
#define atomic64_inc_return(v) atomic64_add_return(1LL, (v))
572-
#define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
573-
#define atomic64_sub_and_test(a, v) (atomic64_sub_return((a), (v)) == 0)
574565
#define atomic64_dec(v) atomic64_sub(1LL, (v))
575566
#define atomic64_dec_return(v) atomic64_sub_return(1LL, (v))
576-
#define atomic64_dec_and_test(v) (atomic64_dec_return((v)) == 0)
577567

578568
#endif /* !CONFIG_GENERIC_ATOMIC64 */
579569

arch/arm/include/asm/atomic.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,8 @@ ATOMIC_OPS(xor, ^=, eor)
248248
#define atomic_inc(v) atomic_add(1, v)
249249
#define atomic_dec(v) atomic_sub(1, v)
250250

251-
#define atomic_inc_and_test(v) (atomic_add_return(1, v) == 0)
252-
#define atomic_dec_and_test(v) (atomic_sub_return(1, v) == 0)
253251
#define atomic_inc_return_relaxed(v) (atomic_add_return_relaxed(1, v))
254252
#define atomic_dec_return_relaxed(v) (atomic_sub_return_relaxed(1, v))
255-
#define atomic_sub_and_test(i, v) (atomic_sub_return(i, v) == 0)
256-
257-
#define atomic_add_negative(i,v) (atomic_add_return(i, v) < 0)
258253

259254
#ifndef CONFIG_GENERIC_ATOMIC64
260255
typedef struct {
@@ -517,14 +512,10 @@ static inline long long atomic64_fetch_add_unless(atomic64_t *v, long long a,
517512
}
518513
#define atomic64_fetch_add_unless atomic64_fetch_add_unless
519514

520-
#define atomic64_add_negative(a, v) (atomic64_add_return((a), (v)) < 0)
521515
#define atomic64_inc(v) atomic64_add(1LL, (v))
522516
#define atomic64_inc_return_relaxed(v) atomic64_add_return_relaxed(1LL, (v))
523-
#define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
524-
#define atomic64_sub_and_test(a, v) (atomic64_sub_return((a), (v)) == 0)
525517
#define atomic64_dec(v) atomic64_sub(1LL, (v))
526518
#define atomic64_dec_return_relaxed(v) atomic64_sub_return_relaxed(1LL, (v))
527-
#define atomic64_dec_and_test(v) (atomic64_dec_return((v)) == 0)
528519

529520
#endif /* !CONFIG_GENERIC_ATOMIC64 */
530521
#endif

arch/arm64/include/asm/atomic.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,6 @@
110110

111111
#define atomic_inc(v) atomic_add(1, (v))
112112
#define atomic_dec(v) atomic_sub(1, (v))
113-
#define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
114-
#define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
115-
#define atomic_sub_and_test(i, v) (atomic_sub_return((i), (v)) == 0)
116-
#define atomic_add_negative(i, v) (atomic_add_return((i), (v)) < 0)
117113
#define atomic_andnot atomic_andnot
118114

119115
/*
@@ -185,10 +181,6 @@
185181

186182
#define atomic64_inc(v) atomic64_add(1, (v))
187183
#define atomic64_dec(v) atomic64_sub(1, (v))
188-
#define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
189-
#define atomic64_dec_and_test(v) (atomic64_dec_return(v) == 0)
190-
#define atomic64_sub_and_test(i, v) (atomic64_sub_return((i), (v)) == 0)
191-
#define atomic64_add_negative(i, v) (atomic64_add_return((i), (v)) < 0)
192184
#define atomic64_andnot atomic64_andnot
193185

194186
#endif

arch/h8300/include/asm/atomic.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,12 @@ ATOMIC_OPS(sub, -=)
6969
#undef ATOMIC_OP_RETURN
7070
#undef ATOMIC_OP
7171

72-
#define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
73-
#define atomic_sub_and_test(i, v) (atomic_sub_return(i, v) == 0)
74-
7572
#define atomic_inc_return(v) atomic_add_return(1, v)
7673
#define atomic_dec_return(v) atomic_sub_return(1, v)
7774

7875
#define atomic_inc(v) (void)atomic_inc_return(v)
79-
#define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
8076

8177
#define atomic_dec(v) (void)atomic_dec_return(v)
82-
#define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
8378

8479
static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
8580
{

arch/hexagon/include/asm/atomic.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,6 @@ static inline int atomic_fetch_add_unless(atomic_t *v, int a, int u)
201201
#define atomic_inc(v) atomic_add(1, (v))
202202
#define atomic_dec(v) atomic_sub(1, (v))
203203

204-
#define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0)
205-
#define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
206-
#define atomic_sub_and_test(i, v) (atomic_sub_return(i, (v)) == 0)
207-
#define atomic_add_negative(i, v) (atomic_add_return(i, (v)) < 0)
208-
209204
#define atomic_inc_return(v) (atomic_add_return(1, v))
210205
#define atomic_dec_return(v) (atomic_sub_return(1, v))
211206

arch/ia64/include/asm/atomic.h

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -231,34 +231,11 @@ static __inline__ long atomic64_dec_if_positive(atomic64_t *v)
231231
return dec;
232232
}
233233

234-
/*
235-
* Atomically add I to V and return TRUE if the resulting value is
236-
* negative.
237-
*/
238-
static __inline__ int
239-
atomic_add_negative (int i, atomic_t *v)
240-
{
241-
return atomic_add_return(i, v) < 0;
242-
}
243-
244-
static __inline__ long
245-
atomic64_add_negative (__s64 i, atomic64_t *v)
246-
{
247-
return atomic64_add_return(i, v) < 0;
248-
}
249-
250234
#define atomic_dec_return(v) atomic_sub_return(1, (v))
251235
#define atomic_inc_return(v) atomic_add_return(1, (v))
252236
#define atomic64_dec_return(v) atomic64_sub_return(1, (v))
253237
#define atomic64_inc_return(v) atomic64_add_return(1, (v))
254238

255-
#define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
256-
#define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
257-
#define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0)
258-
#define atomic64_sub_and_test(i,v) (atomic64_sub_return((i), (v)) == 0)
259-
#define atomic64_dec_and_test(v) (atomic64_sub_return(1, (v)) == 0)
260-
#define atomic64_inc_and_test(v) (atomic64_add_return(1, (v)) == 0)
261-
262239
#define atomic_add(i,v) (void)atomic_add_return((i), (v))
263240
#define atomic_sub(i,v) (void)atomic_sub_return((i), (v))
264241
#define atomic_inc(v) atomic_add(1, (v))

arch/m68k/include/asm/atomic.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ static inline int atomic_dec_and_test(atomic_t *v)
138138
__asm__ __volatile__("subql #1,%1; seq %0" : "=d" (c), "+m" (*v));
139139
return c != 0;
140140
}
141+
#define atomic_dec_and_test atomic_dec_and_test
141142

142143
static inline int atomic_dec_and_test_lt(atomic_t *v)
143144
{
@@ -155,6 +156,7 @@ static inline int atomic_inc_and_test(atomic_t *v)
155156
__asm__ __volatile__("addql #1,%1; seq %0" : "=d" (c), "+m" (*v));
156157
return c != 0;
157158
}
159+
#define atomic_inc_and_test atomic_inc_and_test
158160

159161
#ifdef CONFIG_RMW_INSNS
160162

@@ -201,6 +203,7 @@ static inline int atomic_sub_and_test(int i, atomic_t *v)
201203
: ASM_DI (i));
202204
return c != 0;
203205
}
206+
#define atomic_sub_and_test atomic_sub_and_test
204207

205208
static inline int atomic_add_negative(int i, atomic_t *v)
206209
{
@@ -210,5 +213,6 @@ static inline int atomic_add_negative(int i, atomic_t *v)
210213
: ASM_DI (i));
211214
return c != 0;
212215
}
216+
#define atomic_add_negative atomic_add_negative
213217

214218
#endif /* __ARCH_M68K_ATOMIC __ */

arch/mips/include/asm/atomic.h

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -277,37 +277,6 @@ static __inline__ int atomic_sub_if_positive(int i, atomic_t * v)
277277
#define atomic_dec_return(v) atomic_sub_return(1, (v))
278278
#define atomic_inc_return(v) atomic_add_return(1, (v))
279279

280-
/*
281-
* atomic_sub_and_test - subtract value from variable and test result
282-
* @i: integer value to subtract
283-
* @v: pointer of type atomic_t
284-
*
285-
* Atomically subtracts @i from @v and returns
286-
* true if the result is zero, or false for all
287-
* other cases.
288-
*/
289-
#define atomic_sub_and_test(i, v) (atomic_sub_return((i), (v)) == 0)
290-
291-
/*
292-
* atomic_inc_and_test - increment and test
293-
* @v: pointer of type atomic_t
294-
*
295-
* Atomically increments @v by 1
296-
* and returns true if the result is zero, or false for all
297-
* other cases.
298-
*/
299-
#define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
300-
301-
/*
302-
* atomic_dec_and_test - decrement by 1 and test
303-
* @v: pointer of type atomic_t
304-
*
305-
* Atomically decrements @v by 1 and
306-
* returns true if the result is 0, or false for all other
307-
* cases.
308-
*/
309-
#define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
310-
311280
/*
312281
* atomic_dec_if_positive - decrement by 1 if old value positive
313282
* @v: pointer of type atomic_t
@@ -330,17 +299,6 @@ static __inline__ int atomic_sub_if_positive(int i, atomic_t * v)
330299
*/
331300
#define atomic_dec(v) atomic_sub(1, (v))
332301

333-
/*
334-
* atomic_add_negative - add and test if negative
335-
* @v: pointer of type atomic_t
336-
* @i: integer value to add
337-
*
338-
* Atomically adds @i to @v and returns true
339-
* if the result is negative, or false when
340-
* result is greater than or equal to zero.
341-
*/
342-
#define atomic_add_negative(i, v) (atomic_add_return(i, (v)) < 0)
343-
344302
#ifdef CONFIG_64BIT
345303

346304
#define ATOMIC64_INIT(i) { (i) }
@@ -599,37 +557,6 @@ static __inline__ long atomic64_sub_if_positive(long i, atomic64_t * v)
599557
#define atomic64_dec_return(v) atomic64_sub_return(1, (v))
600558
#define atomic64_inc_return(v) atomic64_add_return(1, (v))
601559

602-
/*
603-
* atomic64_sub_and_test - subtract value from variable and test result
604-
* @i: integer value to subtract
605-
* @v: pointer of type atomic64_t
606-
*
607-
* Atomically subtracts @i from @v and returns
608-
* true if the result is zero, or false for all
609-
* other cases.
610-
*/
611-
#define atomic64_sub_and_test(i, v) (atomic64_sub_return((i), (v)) == 0)
612-
613-
/*
614-
* atomic64_inc_and_test - increment and test
615-
* @v: pointer of type atomic64_t
616-
*
617-
* Atomically increments @v by 1
618-
* and returns true if the result is zero, or false for all
619-
* other cases.
620-
*/
621-
#define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
622-
623-
/*
624-
* atomic64_dec_and_test - decrement by 1 and test
625-
* @v: pointer of type atomic64_t
626-
*
627-
* Atomically decrements @v by 1 and
628-
* returns true if the result is 0, or false for all other
629-
* cases.
630-
*/
631-
#define atomic64_dec_and_test(v) (atomic64_sub_return(1, (v)) == 0)
632-
633560
/*
634561
* atomic64_dec_if_positive - decrement by 1 if old value positive
635562
* @v: pointer of type atomic64_t
@@ -652,17 +579,6 @@ static __inline__ long atomic64_sub_if_positive(long i, atomic64_t * v)
652579
*/
653580
#define atomic64_dec(v) atomic64_sub(1, (v))
654581

655-
/*
656-
* atomic64_add_negative - add and test if negative
657-
* @v: pointer of type atomic64_t
658-
* @i: integer value to add
659-
*
660-
* Atomically adds @i to @v and returns true
661-
* if the result is negative, or false when
662-
* result is greater than or equal to zero.
663-
*/
664-
#define atomic64_add_negative(i, v) (atomic64_add_return(i, (v)) < 0)
665-
666582
#endif /* CONFIG_64BIT */
667583

668584
#endif /* _ASM_ATOMIC_H */

arch/parisc/include/asm/atomic.h

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -142,22 +142,6 @@ ATOMIC_OPS(xor, ^=)
142142
#define atomic_inc_return(v) (atomic_add_return( 1,(v)))
143143
#define atomic_dec_return(v) (atomic_add_return( -1,(v)))
144144

145-
#define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
146-
147-
/*
148-
* atomic_inc_and_test - increment and test
149-
* @v: pointer of type atomic_t
150-
*
151-
* Atomically increments @v by 1
152-
* and returns true if the result is zero, or false for all
153-
* other cases.
154-
*/
155-
#define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
156-
157-
#define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
158-
159-
#define atomic_sub_and_test(i,v) (atomic_sub_return((i),(v)) == 0)
160-
161145
#define ATOMIC_INIT(i) { (i) }
162146

163147
#ifdef CONFIG_64BIT
@@ -246,12 +230,6 @@ atomic64_read(const atomic64_t *v)
246230
#define atomic64_inc_return(v) (atomic64_add_return( 1,(v)))
247231
#define atomic64_dec_return(v) (atomic64_add_return( -1,(v)))
248232

249-
#define atomic64_add_negative(a, v) (atomic64_add_return((a), (v)) < 0)
250-
251-
#define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
252-
#define atomic64_dec_and_test(v) (atomic64_dec_return(v) == 0)
253-
#define atomic64_sub_and_test(i,v) (atomic64_sub_return((i),(v)) == 0)
254-
255233
/* exported interface */
256234
#define atomic64_cmpxchg(v, o, n) \
257235
((__typeof__((v)->counter))cmpxchg(&((v)->counter), (o), (n)))

0 commit comments

Comments
 (0)