2009-05-14 05:56:35 +07:00
|
|
|
/*
|
2011-07-27 06:09:08 +07:00
|
|
|
* Generic C implementation of atomic counter operations. Usable on
|
|
|
|
* UP systems only. Do not include in machine independent code.
|
|
|
|
*
|
2009-05-14 05:56:35 +07:00
|
|
|
* Originally implemented for MN10300.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
|
|
|
|
* Written by David Howells (dhowells@redhat.com)
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public Licence
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the Licence, or (at your option) any later version.
|
|
|
|
*/
|
|
|
|
#ifndef __ASM_GENERIC_ATOMIC_H
|
|
|
|
#define __ASM_GENERIC_ATOMIC_H
|
|
|
|
|
2012-03-29 00:30:03 +07: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-07 00:16:07 +07:00
|
|
|
#include <asm/barrier.h>
|
2012-03-29 00:30:03 +07:00
|
|
|
|
2014-04-23 21:12:30 +07:00
|
|
|
/*
|
|
|
|
* atomic_$op() - $op integer to atomic variable
|
|
|
|
* @i: integer value to $op
|
|
|
|
* @v: pointer to the atomic variable
|
|
|
|
*
|
|
|
|
* Atomically $ops @i to @v. Does not strictly guarantee a memory-barrier, use
|
|
|
|
* smp_mb__{before,after}_atomic().
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* atomic_$op_return() - $op interer to atomic variable and returns the result
|
|
|
|
* @i: integer value to $op
|
|
|
|
* @v: pointer to the atomic variable
|
|
|
|
*
|
|
|
|
* Atomically $ops @i to @v. Does imply a full memory barrier.
|
|
|
|
*/
|
|
|
|
|
2009-05-14 05:56:35 +07:00
|
|
|
#ifdef CONFIG_SMP
|
2014-04-23 21:12:30 +07:00
|
|
|
|
|
|
|
/* we can build all atomic primitives from cmpxchg */
|
|
|
|
|
|
|
|
#define ATOMIC_OP(op, c_op) \
|
|
|
|
static inline void atomic_##op(int i, atomic_t *v) \
|
|
|
|
{ \
|
|
|
|
int c, old; \
|
|
|
|
\
|
|
|
|
c = v->counter; \
|
|
|
|
while ((old = cmpxchg(&v->counter, c, c c_op i)) != c) \
|
|
|
|
c = old; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define ATOMIC_OP_RETURN(op, c_op) \
|
|
|
|
static inline int atomic_##op##_return(int i, atomic_t *v) \
|
|
|
|
{ \
|
|
|
|
int c, old; \
|
|
|
|
\
|
|
|
|
c = v->counter; \
|
|
|
|
while ((old = cmpxchg(&v->counter, c, c c_op i)) != c) \
|
|
|
|
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-18 05:54:38 +07:00
|
|
|
#define ATOMIC_FETCH_OP(op, c_op) \
|
|
|
|
static inline int atomic_fetch_##op(int i, atomic_t *v) \
|
|
|
|
{ \
|
|
|
|
int c, old; \
|
|
|
|
\
|
|
|
|
c = v->counter; \
|
|
|
|
while ((old = cmpxchg(&v->counter, c, c c_op i)) != c) \
|
|
|
|
c = old; \
|
|
|
|
\
|
|
|
|
return c; \
|
|
|
|
}
|
|
|
|
|
2014-04-23 21:12:30 +07:00
|
|
|
#else
|
|
|
|
|
|
|
|
#include <linux/irqflags.h>
|
|
|
|
|
|
|
|
#define ATOMIC_OP(op, c_op) \
|
|
|
|
static inline void atomic_##op(int i, atomic_t *v) \
|
|
|
|
{ \
|
|
|
|
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) \
|
|
|
|
static inline int atomic_##op##_return(int i, atomic_t *v) \
|
|
|
|
{ \
|
|
|
|
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-18 05:54:38 +07:00
|
|
|
#define ATOMIC_FETCH_OP(op, c_op) \
|
|
|
|
static inline int atomic_fetch_##op(int i, atomic_t *v) \
|
|
|
|
{ \
|
|
|
|
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 21:12:30 +07:00
|
|
|
#endif /* CONFIG_SMP */
|
|
|
|
|
|
|
|
#ifndef atomic_add_return
|
|
|
|
ATOMIC_OP_RETURN(add, +)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef atomic_sub_return
|
|
|
|
ATOMIC_OP_RETURN(sub, -)
|
|
|
|
#endif
|
|
|
|
|
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-18 05:54:38 +07:00
|
|
|
#ifndef atomic_fetch_add
|
|
|
|
ATOMIC_FETCH_OP(add, +)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef atomic_fetch_sub
|
|
|
|
ATOMIC_FETCH_OP(sub, -)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef atomic_fetch_and
|
|
|
|
ATOMIC_FETCH_OP(and, &)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef atomic_fetch_or
|
|
|
|
ATOMIC_FETCH_OP(or, |)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef atomic_fetch_xor
|
|
|
|
ATOMIC_FETCH_OP(xor, ^)
|
|
|
|
#endif
|
|
|
|
|
2015-07-15 20:47:25 +07:00
|
|
|
#ifndef atomic_and
|
2014-04-23 21:12:30 +07:00
|
|
|
ATOMIC_OP(and, &)
|
2015-07-15 20:47:25 +07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef atomic_or
|
2014-04-23 21:12:30 +07:00
|
|
|
ATOMIC_OP(or, |)
|
2015-07-15 20:47:25 +07:00
|
|
|
#endif
|
|
|
|
|
2014-04-24 00:32:50 +07:00
|
|
|
#ifndef atomic_xor
|
|
|
|
ATOMIC_OP(xor, ^)
|
2014-04-23 21:12:30 +07:00
|
|
|
#endif
|
|
|
|
|
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-18 05:54:38 +07:00
|
|
|
#undef ATOMIC_FETCH_OP
|
2014-04-23 21:12:30 +07:00
|
|
|
#undef ATOMIC_OP_RETURN
|
|
|
|
#undef ATOMIC_OP
|
|
|
|
|
2009-05-14 05:56:35 +07:00
|
|
|
/*
|
|
|
|
* Atomic operations that C can't guarantee us. Useful for
|
|
|
|
* resource counting etc..
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define ATOMIC_INIT(i) { (i) }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* atomic_read - read atomic variable
|
|
|
|
* @v: pointer of type atomic_t
|
|
|
|
*
|
2010-05-25 04:33:09 +07:00
|
|
|
* Atomically reads the value of @v.
|
2009-05-14 05:56:35 +07:00
|
|
|
*/
|
2011-07-27 06:09:11 +07:00
|
|
|
#ifndef atomic_read
|
2015-09-18 16:13:10 +07:00
|
|
|
#define atomic_read(v) READ_ONCE((v)->counter)
|
2011-07-27 06:09:11 +07:00
|
|
|
#endif
|
2009-05-14 05:56:35 +07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* atomic_set - set atomic variable
|
|
|
|
* @v: pointer of type atomic_t
|
|
|
|
* @i: required value
|
|
|
|
*
|
2010-05-25 04:33:09 +07:00
|
|
|
* Atomically sets the value of @v to @i.
|
2009-05-14 05:56:35 +07:00
|
|
|
*/
|
2015-09-18 16:13:10 +07:00
|
|
|
#define atomic_set(v, i) WRITE_ONCE(((v)->counter), (i))
|
2009-05-14 05:56:35 +07:00
|
|
|
|
2010-10-07 20:08:55 +07:00
|
|
|
#include <linux/irqflags.h>
|
2009-05-14 05:56:35 +07:00
|
|
|
|
|
|
|
static inline int atomic_add_negative(int i, atomic_t *v)
|
|
|
|
{
|
|
|
|
return atomic_add_return(i, v) < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void atomic_add(int i, atomic_t *v)
|
|
|
|
{
|
|
|
|
atomic_add_return(i, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void atomic_sub(int i, atomic_t *v)
|
|
|
|
{
|
|
|
|
atomic_sub_return(i, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void atomic_inc(atomic_t *v)
|
|
|
|
{
|
|
|
|
atomic_add_return(1, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void atomic_dec(atomic_t *v)
|
|
|
|
{
|
|
|
|
atomic_sub_return(1, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define atomic_dec_return(v) atomic_sub_return(1, (v))
|
|
|
|
#define atomic_inc_return(v) atomic_add_return(1, (v))
|
|
|
|
|
|
|
|
#define atomic_sub_and_test(i, v) (atomic_sub_return((i), (v)) == 0)
|
2011-07-27 06:09:09 +07:00
|
|
|
#define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
|
|
|
|
#define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
|
2009-05-14 05:56:35 +07:00
|
|
|
|
2010-06-27 17:26:06 +07:00
|
|
|
#define atomic_xchg(ptr, v) (xchg(&(ptr)->counter, (v)))
|
|
|
|
#define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), (old), (new)))
|
|
|
|
|
2014-05-14 02:30:56 +07:00
|
|
|
#ifndef __atomic_add_unless
|
2011-07-27 06:09:07 +07:00
|
|
|
static inline int __atomic_add_unless(atomic_t *v, int a, int u)
|
2010-06-27 17:26:06 +07:00
|
|
|
{
|
2014-04-23 21:12:30 +07:00
|
|
|
int c, old;
|
|
|
|
c = atomic_read(v);
|
|
|
|
while (c != u && (old = atomic_cmpxchg(v, c, c + a)) != c)
|
|
|
|
c = old;
|
|
|
|
return c;
|
2009-05-14 05:56:35 +07:00
|
|
|
}
|
2014-05-14 02:30:56 +07:00
|
|
|
#endif
|
2011-07-27 06:09:10 +07:00
|
|
|
|
2009-05-14 05:56:35 +07:00
|
|
|
#endif /* __ASM_GENERIC_ATOMIC_H */
|