2012-03-05 18:49:30 +07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 ARM Ltd.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#ifndef __ASM_SPINLOCK_H
|
|
|
|
#define __ASM_SPINLOCK_H
|
|
|
|
|
2015-02-10 10:03:15 +07:00
|
|
|
#include <asm/lse.h>
|
2012-03-05 18:49:30 +07:00
|
|
|
#include <asm/spinlock_types.h>
|
|
|
|
#include <asm/processor.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Spinlock implementation.
|
|
|
|
*
|
|
|
|
* The memory barriers are implicit with the load-acquire and store-release
|
|
|
|
* instructions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static inline void arch_spin_lock(arch_spinlock_t *lock)
|
|
|
|
{
|
|
|
|
unsigned int tmp;
|
2013-10-09 21:54:26 +07:00
|
|
|
arch_spinlock_t lockval, newval;
|
2012-03-05 18:49:30 +07:00
|
|
|
|
|
|
|
asm volatile(
|
2013-10-09 21:54:26 +07:00
|
|
|
/* Atomically increment the next ticket. */
|
2015-02-10 10:03:15 +07:00
|
|
|
ARM64_LSE_ATOMIC_INSN(
|
|
|
|
/* LL/SC */
|
2013-10-09 21:54:26 +07:00
|
|
|
" prfm pstl1strm, %3\n"
|
|
|
|
"1: ldaxr %w0, %3\n"
|
|
|
|
" add %w1, %w0, %w5\n"
|
|
|
|
" stxr %w2, %w1, %3\n"
|
2015-02-10 10:03:15 +07:00
|
|
|
" cbnz %w2, 1b\n",
|
|
|
|
/* LSE atomics */
|
|
|
|
" mov %w2, %w5\n"
|
|
|
|
" ldadda %w2, %w0, %3\n"
|
2016-09-06 22:42:58 +07:00
|
|
|
__nops(3)
|
2015-02-10 10:03:15 +07:00
|
|
|
)
|
|
|
|
|
2013-10-09 21:54:26 +07:00
|
|
|
/* Did we get the lock? */
|
|
|
|
" eor %w1, %w0, %w0, ror #16\n"
|
|
|
|
" cbz %w1, 3f\n"
|
|
|
|
/*
|
|
|
|
* No: spin on the owner. Send a local event to avoid missing an
|
|
|
|
* unlock before the exclusive load.
|
|
|
|
*/
|
|
|
|
" sevl\n"
|
|
|
|
"2: wfe\n"
|
|
|
|
" ldaxrh %w2, %4\n"
|
|
|
|
" eor %w1, %w2, %w0, lsr #16\n"
|
|
|
|
" cbnz %w1, 2b\n"
|
|
|
|
/* We got the lock. Critical section starts here. */
|
|
|
|
"3:"
|
|
|
|
: "=&r" (lockval), "=&r" (newval), "=&r" (tmp), "+Q" (*lock)
|
|
|
|
: "Q" (lock->owner), "I" (1 << TICKET_SHIFT)
|
|
|
|
: "memory");
|
2012-03-05 18:49:30 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int arch_spin_trylock(arch_spinlock_t *lock)
|
|
|
|
{
|
|
|
|
unsigned int tmp;
|
2013-10-09 21:54:26 +07:00
|
|
|
arch_spinlock_t lockval;
|
2012-03-05 18:49:30 +07:00
|
|
|
|
2015-02-10 10:03:15 +07:00
|
|
|
asm volatile(ARM64_LSE_ATOMIC_INSN(
|
|
|
|
/* LL/SC */
|
|
|
|
" prfm pstl1strm, %2\n"
|
|
|
|
"1: ldaxr %w0, %2\n"
|
|
|
|
" eor %w1, %w0, %w0, ror #16\n"
|
|
|
|
" cbnz %w1, 2f\n"
|
|
|
|
" add %w0, %w0, %3\n"
|
|
|
|
" stxr %w1, %w0, %2\n"
|
|
|
|
" cbnz %w1, 1b\n"
|
|
|
|
"2:",
|
|
|
|
/* LSE atomics */
|
|
|
|
" ldr %w0, %2\n"
|
|
|
|
" eor %w1, %w0, %w0, ror #16\n"
|
|
|
|
" cbnz %w1, 1f\n"
|
|
|
|
" add %w1, %w0, %3\n"
|
|
|
|
" casa %w0, %w1, %2\n"
|
2018-01-31 19:12:20 +07:00
|
|
|
" sub %w1, %w1, %3\n"
|
|
|
|
" eor %w1, %w1, %w0\n"
|
2015-02-10 10:03:15 +07:00
|
|
|
"1:")
|
2013-10-09 21:54:26 +07:00
|
|
|
: "=&r" (lockval), "=&r" (tmp), "+Q" (*lock)
|
|
|
|
: "I" (1 << TICKET_SHIFT)
|
|
|
|
: "memory");
|
2012-03-05 18:49:30 +07:00
|
|
|
|
|
|
|
return !tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void arch_spin_unlock(arch_spinlock_t *lock)
|
|
|
|
{
|
2015-02-10 10:03:15 +07:00
|
|
|
unsigned long tmp;
|
|
|
|
|
|
|
|
asm volatile(ARM64_LSE_ATOMIC_INSN(
|
|
|
|
/* LL/SC */
|
2015-07-28 20:48:00 +07:00
|
|
|
" ldrh %w1, %0\n"
|
2015-02-10 10:03:15 +07:00
|
|
|
" add %w1, %w1, #1\n"
|
|
|
|
" stlrh %w1, %0",
|
|
|
|
/* LSE atomics */
|
|
|
|
" mov %w1, #1\n"
|
2016-09-06 22:42:58 +07:00
|
|
|
" staddlh %w1, %0\n"
|
|
|
|
__nops(1))
|
2015-02-10 10:03:15 +07:00
|
|
|
: "=Q" (lock->owner), "=&r" (tmp)
|
|
|
|
:
|
2013-10-09 21:54:26 +07:00
|
|
|
: "memory");
|
|
|
|
}
|
|
|
|
|
2013-10-09 21:54:27 +07:00
|
|
|
static inline int arch_spin_value_unlocked(arch_spinlock_t lock)
|
|
|
|
{
|
|
|
|
return lock.owner == lock.next;
|
|
|
|
}
|
|
|
|
|
2013-10-09 21:54:26 +07:00
|
|
|
static inline int arch_spin_is_locked(arch_spinlock_t *lock)
|
|
|
|
{
|
2014-11-24 16:53:11 +07:00
|
|
|
return !arch_spin_value_unlocked(READ_ONCE(*lock));
|
2013-10-09 21:54:26 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int arch_spin_is_contended(arch_spinlock_t *lock)
|
|
|
|
{
|
2014-11-24 16:53:11 +07:00
|
|
|
arch_spinlock_t lockval = READ_ONCE(*lock);
|
2013-10-09 21:54:26 +07:00
|
|
|
return (lockval.next - lockval.owner) > 1;
|
2012-03-05 18:49:30 +07:00
|
|
|
}
|
2013-10-09 21:54:26 +07:00
|
|
|
#define arch_spin_is_contended arch_spin_is_contended
|
2012-03-05 18:49:30 +07:00
|
|
|
|
2017-10-12 19:20:50 +07:00
|
|
|
#include <asm/qrwlock.h>
|
2012-03-05 18:49:30 +07:00
|
|
|
|
locking: Introduce smp_mb__after_spinlock()
Since its inception, our understanding of ACQUIRE, esp. as applied to
spinlocks, has changed somewhat. Also, I wonder if, with a simple
change, we cannot make it provide more.
The problem with the comment is that the STORE done by spin_lock isn't
itself ordered by the ACQUIRE, and therefore a later LOAD can pass over
it and cross with any prior STORE, rendering the default WMB
insufficient (pointed out by Alan).
Now, this is only really a problem on PowerPC and ARM64, both of
which already defined smp_mb__before_spinlock() as a smp_mb().
At the same time, we can get a much stronger construct if we place
that same barrier _inside_ the spin_lock(). In that case we upgrade
the RCpc spinlock to an RCsc. That would make all schedule() calls
fully transitive against one another.
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Will Deacon <will.deacon@arm.com>
Cc: Alan Stern <stern@rowland.harvard.edu>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Nicholas Piggin <npiggin@gmail.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Paul McKenney <paulmck@linux.vnet.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2016-09-05 16:37:53 +07:00
|
|
|
/* See include/linux/spinlock.h */
|
|
|
|
#define smp_mb__after_spinlock() smp_mb()
|
2016-09-05 17:56:05 +07:00
|
|
|
|
2012-03-05 18:49:30 +07:00
|
|
|
#endif /* __ASM_SPINLOCK_H */
|