2019-05-20 17:08:01 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2009-05-13 22:56:35 +00:00
|
|
|
/*
|
2021-05-25 14:02:05 +00:00
|
|
|
* Generic C implementation of atomic counter operations. Do not include in
|
|
|
|
* machine independent code.
|
2011-07-26 23:09:08 +00:00
|
|
|
*
|
2009-05-13 22:56:35 +00:00
|
|
|
* Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
|
|
|
|
* Written by David Howells (dhowells@redhat.com)
|
|
|
|
*/
|
|
|
|
#ifndef __ASM_GENERIC_ATOMIC_H
|
|
|
|
#define __ASM_GENERIC_ATOMIC_H
|
|
|
|
|
2012-03-28 17:30:03 +00:00
|
|
|
#include <asm/cmpxchg.h>
|
arch: Prepare for smp_mb__{before,after}_atomic()
Since the smp_mb__{before,after}*() ops are fundamentally dependent on
how an arch can implement atomics it doesn't make sense to have 3
variants of them. They must all be the same.
Furthermore, the 3 variants suggest they're only valid for those 3
atomic ops, while we have many more where they could be applied.
So move away from
smp_mb__{before,after}_{atomic,clear}_{dec,inc,bit}() and reduce the
interface to just the two: smp_mb__{before,after}_atomic().
This patch prepares the way by introducing default implementations in
asm-generic/barrier.h that default to a full barrier and providing
__deprecated inlines for the previous 6 barriers if they're not
provided by the arch.
This should allow for a mostly painless transition (lots of deprecated
warns in the interim).
Signed-off-by: Peter Zijlstra <peterz@infradead.org>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Link: http://lkml.kernel.org/n/tip-wr59327qdyi9mbzn6x937s4e@git.kernel.org
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: "Chen, Gong" <gong.chen@linux.intel.com>
Cc: John Sullivan <jsrhbz@kanargh.force9.co.uk>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Mauro Carvalho Chehab <m.chehab@samsung.com>
Cc: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
Cc: "Theodore Ts'o" <tytso@mit.edu>
Cc: linux-arch@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2014-02-06 17:16:07 +00:00
|
|
|
#include <asm/barrier.h>
|
2012-03-28 17:30:03 +00:00
|
|
|
|
2009-05-13 22:56:35 +00:00
|
|
|
#ifdef CONFIG_SMP
|
2014-04-23 14:12:30 +00:00
|
|
|
|
|
|
|
/* we can build all atomic primitives from cmpxchg */
|
|
|
|
|
|
|
|
#define ATOMIC_OP(op, c_op) \
|
2021-05-25 14:02:08 +00:00
|
|
|
static inline void generic_atomic_##op(int i, atomic_t *v) \
|
2014-04-23 14:12:30 +00:00
|
|
|
{ \
|
|
|
|
int c, old; \
|
|
|
|
\
|
|
|
|
c = v->counter; \
|
2021-05-25 14:02:31 +00:00
|
|
|
while ((old = arch_cmpxchg(&v->counter, c, c c_op i)) != c) \
|
2014-04-23 14:12:30 +00:00
|
|
|
c = old; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define ATOMIC_OP_RETURN(op, c_op) \
|
2021-05-25 14:02:08 +00:00
|
|
|
static inline int generic_atomic_##op##_return(int i, atomic_t *v) \
|
2014-04-23 14:12:30 +00:00
|
|
|
{ \
|
|
|
|
int c, old; \
|
|
|
|
\
|
|
|
|
c = v->counter; \
|
2021-05-25 14:02:31 +00:00
|
|
|
while ((old = arch_cmpxchg(&v->counter, c, c c_op i)) != c) \
|
2014-04-23 14:12:30 +00:00
|
|
|
c = old; \
|
|
|
|
\
|
|
|
|
return c c_op i; \
|
|
|
|
}
|
|
|
|
|
locking/atomic: Implement atomic{,64,_long}_fetch_{add,sub,and,andnot,or,xor}{,_relaxed,_acquire,_release}()
Now that all the architectures have implemented support for these new
atomic primitives add on the generic infrastructure to expose and use
it.
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Will Deacon <will.deacon@arm.com>
Cc: linux-arch@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2016-04-17 22:54:38 +00:00
|
|
|
#define ATOMIC_FETCH_OP(op, c_op) \
|
2021-05-25 14:02:08 +00:00
|
|
|
static inline int generic_atomic_fetch_##op(int i, atomic_t *v) \
|
locking/atomic: Implement atomic{,64,_long}_fetch_{add,sub,and,andnot,or,xor}{,_relaxed,_acquire,_release}()
Now that all the architectures have implemented support for these new
atomic primitives add on the generic infrastructure to expose and use
it.
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Will Deacon <will.deacon@arm.com>
Cc: linux-arch@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2016-04-17 22:54:38 +00:00
|
|
|
{ \
|
|
|
|
int c, old; \
|
|
|
|
\
|
|
|
|
c = v->counter; \
|
2021-05-25 14:02:31 +00:00
|
|
|
while ((old = arch_cmpxchg(&v->counter, c, c c_op i)) != c) \
|
locking/atomic: Implement atomic{,64,_long}_fetch_{add,sub,and,andnot,or,xor}{,_relaxed,_acquire,_release}()
Now that all the architectures have implemented support for these new
atomic primitives add on the generic infrastructure to expose and use
it.
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Will Deacon <will.deacon@arm.com>
Cc: linux-arch@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2016-04-17 22:54:38 +00:00
|
|
|
c = old; \
|
|
|
|
\
|
|
|
|
return c; \
|
|
|
|
}
|
|
|
|
|
2014-04-23 14:12:30 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
#include <linux/irqflags.h>
|
|
|
|
|
|
|
|
#define ATOMIC_OP(op, c_op) \
|
2021-05-25 14:02:08 +00:00
|
|
|
static inline void generic_atomic_##op(int i, atomic_t *v) \
|
2014-04-23 14:12:30 +00:00
|
|
|
{ \
|
|
|
|
unsigned long flags; \
|
|
|
|
\
|
|
|
|
raw_local_irq_save(flags); \
|
|
|
|
v->counter = v->counter c_op i; \
|
|
|
|
raw_local_irq_restore(flags); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define ATOMIC_OP_RETURN(op, c_op) \
|
2021-05-25 14:02:08 +00:00
|
|
|
static inline int generic_atomic_##op##_return(int i, atomic_t *v) \
|
2014-04-23 14:12:30 +00:00
|
|
|
{ \
|
|
|
|
unsigned long flags; \
|
|
|
|
int ret; \
|
|
|
|
\
|
|
|
|
raw_local_irq_save(flags); \
|
|
|
|
ret = (v->counter = v->counter c_op i); \
|
|
|
|
raw_local_irq_restore(flags); \
|
|
|
|
\
|
|
|
|
return ret; \
|
|
|
|
}
|
|
|
|
|
locking/atomic: Implement atomic{,64,_long}_fetch_{add,sub,and,andnot,or,xor}{,_relaxed,_acquire,_release}()
Now that all the architectures have implemented support for these new
atomic primitives add on the generic infrastructure to expose and use
it.
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Will Deacon <will.deacon@arm.com>
Cc: linux-arch@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2016-04-17 22:54:38 +00:00
|
|
|
#define ATOMIC_FETCH_OP(op, c_op) \
|
2021-05-25 14:02:08 +00:00
|
|
|
static inline int generic_atomic_fetch_##op(int i, atomic_t *v) \
|
locking/atomic: Implement atomic{,64,_long}_fetch_{add,sub,and,andnot,or,xor}{,_relaxed,_acquire,_release}()
Now that all the architectures have implemented support for these new
atomic primitives add on the generic infrastructure to expose and use
it.
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Will Deacon <will.deacon@arm.com>
Cc: linux-arch@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2016-04-17 22:54:38 +00:00
|
|
|
{ \
|
|
|
|
unsigned long flags; \
|
|
|
|
int ret; \
|
|
|
|
\
|
|
|
|
raw_local_irq_save(flags); \
|
|
|
|
ret = v->counter; \
|
|
|
|
v->counter = v->counter c_op i; \
|
|
|
|
raw_local_irq_restore(flags); \
|
|
|
|
\
|
|
|
|
return ret; \
|
|
|
|
}
|
|
|
|
|
2014-04-23 14:12:30 +00:00
|
|
|
#endif /* CONFIG_SMP */
|
|
|
|
|
|
|
|
ATOMIC_OP_RETURN(add, +)
|
|
|
|
ATOMIC_OP_RETURN(sub, -)
|
|
|
|
|
locking/atomic: Implement atomic{,64,_long}_fetch_{add,sub,and,andnot,or,xor}{,_relaxed,_acquire,_release}()
Now that all the architectures have implemented support for these new
atomic primitives add on the generic infrastructure to expose and use
it.
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Will Deacon <will.deacon@arm.com>
Cc: linux-arch@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2016-04-17 22:54:38 +00:00
|
|
|
ATOMIC_FETCH_OP(add, +)
|
|
|
|
ATOMIC_FETCH_OP(sub, -)
|
|
|
|
ATOMIC_FETCH_OP(and, &)
|
|
|
|
ATOMIC_FETCH_OP(or, |)
|
|
|
|
ATOMIC_FETCH_OP(xor, ^)
|
|
|
|
|
2021-05-25 14:02:07 +00:00
|
|
|
ATOMIC_OP(add, +)
|
|
|
|
ATOMIC_OP(sub, -)
|
2014-04-23 14:12:30 +00:00
|
|
|
ATOMIC_OP(and, &)
|
|
|
|
ATOMIC_OP(or, |)
|
2014-04-23 17:32:50 +00:00
|
|
|
ATOMIC_OP(xor, ^)
|
2014-04-23 14:12:30 +00:00
|
|
|
|
locking/atomic: Implement atomic{,64,_long}_fetch_{add,sub,and,andnot,or,xor}{,_relaxed,_acquire,_release}()
Now that all the architectures have implemented support for these new
atomic primitives add on the generic infrastructure to expose and use
it.
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Will Deacon <will.deacon@arm.com>
Cc: linux-arch@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2016-04-17 22:54:38 +00:00
|
|
|
#undef ATOMIC_FETCH_OP
|
2014-04-23 14:12:30 +00:00
|
|
|
#undef ATOMIC_OP_RETURN
|
|
|
|
#undef ATOMIC_OP
|
|
|
|
|
2021-05-25 14:02:08 +00:00
|
|
|
#define arch_atomic_add_return generic_atomic_add_return
|
|
|
|
#define arch_atomic_sub_return generic_atomic_sub_return
|
|
|
|
|
|
|
|
#define arch_atomic_fetch_add generic_atomic_fetch_add
|
|
|
|
#define arch_atomic_fetch_sub generic_atomic_fetch_sub
|
|
|
|
#define arch_atomic_fetch_and generic_atomic_fetch_and
|
|
|
|
#define arch_atomic_fetch_or generic_atomic_fetch_or
|
|
|
|
#define arch_atomic_fetch_xor generic_atomic_fetch_xor
|
|
|
|
|
|
|
|
#define arch_atomic_add generic_atomic_add
|
|
|
|
#define arch_atomic_sub generic_atomic_sub
|
|
|
|
#define arch_atomic_and generic_atomic_and
|
|
|
|
#define arch_atomic_or generic_atomic_or
|
|
|
|
#define arch_atomic_xor generic_atomic_xor
|
|
|
|
|
|
|
|
#define arch_atomic_read(v) READ_ONCE((v)->counter)
|
|
|
|
#define arch_atomic_set(v, i) WRITE_ONCE(((v)->counter), (i))
|
|
|
|
|
|
|
|
#define arch_atomic_xchg(ptr, v) (arch_xchg(&(ptr)->counter, (v)))
|
|
|
|
#define arch_atomic_cmpxchg(v, old, new) (arch_cmpxchg(&((v)->counter), (old), (new)))
|
|
|
|
|
2009-05-13 22:56:35 +00:00
|
|
|
#endif /* __ASM_GENERIC_ATOMIC_H */
|