2005-04-17 05:20:36 +07:00
|
|
|
#ifndef __ASM_SPINLOCK_H
|
|
|
|
#define __ASM_SPINLOCK_H
|
|
|
|
|
2012-05-11 03:57:11 +07:00
|
|
|
#include <asm/barrier.h>
|
|
|
|
#include <asm/ldcw.h>
|
[PATCH] spinlock consolidation
This patch (written by me and also containing many suggestions of Arjan van
de Ven) does a major cleanup of the spinlock code. It does the following
things:
- consolidates and enhances the spinlock/rwlock debugging code
- simplifies the asm/spinlock.h files
- encapsulates the raw spinlock type and moves generic spinlock
features (such as ->break_lock) into the generic code.
- cleans up the spinlock code hierarchy to get rid of the spaghetti.
Most notably there's now only a single variant of the debugging code,
located in lib/spinlock_debug.c. (previously we had one SMP debugging
variant per architecture, plus a separate generic one for UP builds)
Also, i've enhanced the rwlock debugging facility, it will now track
write-owners. There is new spinlock-owner/CPU-tracking on SMP builds too.
All locks have lockup detection now, which will work for both soft and hard
spin/rwlock lockups.
The arch-level include files now only contain the minimally necessary
subset of the spinlock code - all the rest that can be generalized now
lives in the generic headers:
include/asm-i386/spinlock_types.h | 16
include/asm-x86_64/spinlock_types.h | 16
I have also split up the various spinlock variants into separate files,
making it easier to see which does what. The new layout is:
SMP | UP
----------------------------|-----------------------------------
asm/spinlock_types_smp.h | linux/spinlock_types_up.h
linux/spinlock_types.h | linux/spinlock_types.h
asm/spinlock_smp.h | linux/spinlock_up.h
linux/spinlock_api_smp.h | linux/spinlock_api_up.h
linux/spinlock.h | linux/spinlock.h
/*
* here's the role of the various spinlock/rwlock related include files:
*
* on SMP builds:
*
* asm/spinlock_types.h: contains the raw_spinlock_t/raw_rwlock_t and the
* initializers
*
* linux/spinlock_types.h:
* defines the generic type and initializers
*
* asm/spinlock.h: contains the __raw_spin_*()/etc. lowlevel
* implementations, mostly inline assembly code
*
* (also included on UP-debug builds:)
*
* linux/spinlock_api_smp.h:
* contains the prototypes for the _spin_*() APIs.
*
* linux/spinlock.h: builds the final spin_*() APIs.
*
* on UP builds:
*
* linux/spinlock_type_up.h:
* contains the generic, simplified UP spinlock type.
* (which is an empty structure on non-debug builds)
*
* linux/spinlock_types.h:
* defines the generic type and initializers
*
* linux/spinlock_up.h:
* contains the __raw_spin_*()/etc. version of UP
* builds. (which are NOPs on non-debug, non-preempt
* builds)
*
* (included on UP-non-debug builds:)
*
* linux/spinlock_api_up.h:
* builds the _spin_*() APIs.
*
* linux/spinlock.h: builds the final spin_*() APIs.
*/
All SMP and UP architectures are converted by this patch.
arm, i386, ia64, ppc, ppc64, s390/s390x, x64 was build-tested via
crosscompilers. m32r, mips, sh, sparc, have not been tested yet, but should
be mostly fine.
From: Grant Grundler <grundler@parisc-linux.org>
Booted and lightly tested on a500-44 (64-bit, SMP kernel, dual CPU).
Builds 32-bit SMP kernel (not booted or tested). I did not try to build
non-SMP kernels. That should be trivial to fix up later if necessary.
I converted bit ops atomic_hash lock to raw_spinlock_t. Doing so avoids
some ugly nesting of linux/*.h and asm/*.h files. Those particular locks
are well tested and contained entirely inside arch specific code. I do NOT
expect any new issues to arise with them.
If someone does ever need to use debug/metrics with them, then they will
need to unravel this hairball between spinlocks, atomic ops, and bit ops
that exist only because parisc has exactly one atomic instruction: LDCW
(load and clear word).
From: "Luck, Tony" <tony.luck@intel.com>
ia64 fix
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Arjan van de Ven <arjanv@infradead.org>
Signed-off-by: Grant Grundler <grundler@parisc-linux.org>
Cc: Matthew Wilcox <willy@debian.org>
Signed-off-by: Hirokazu Takata <takata@linux-m32r.org>
Signed-off-by: Mikael Pettersson <mikpe@csd.uu.se>
Signed-off-by: Benoit Boissinot <benoit.boissinot@ens-lyon.org>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2005-09-10 14:25:56 +07:00
|
|
|
#include <asm/processor.h>
|
|
|
|
#include <asm/spinlock_types.h>
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2009-12-03 02:01:25 +07:00
|
|
|
static inline int arch_spin_is_locked(arch_spinlock_t *x)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
|
|
|
volatile unsigned int *a = __ldcw_align(x);
|
|
|
|
return *a == 0;
|
|
|
|
}
|
|
|
|
|
2009-12-03 02:01:25 +07:00
|
|
|
#define arch_spin_lock(lock) arch_spin_lock_flags(lock, 0)
|
2016-05-26 15:35:03 +07:00
|
|
|
|
|
|
|
static inline void arch_spin_unlock_wait(arch_spinlock_t *x)
|
|
|
|
{
|
|
|
|
volatile unsigned int *a = __ldcw_align(x);
|
|
|
|
|
|
|
|
smp_cond_load_acquire(a, VAL);
|
|
|
|
}
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2009-12-03 02:01:25 +07:00
|
|
|
static inline void arch_spin_lock_flags(arch_spinlock_t *x,
|
2005-11-18 04:35:09 +07:00
|
|
|
unsigned long flags)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
|
|
|
volatile unsigned int *a;
|
|
|
|
|
|
|
|
mb();
|
|
|
|
a = __ldcw_align(x);
|
|
|
|
while (__ldcw(a) == 0)
|
2005-11-18 04:35:09 +07:00
|
|
|
while (*a == 0)
|
|
|
|
if (flags & PSW_SM_I) {
|
|
|
|
local_irq_enable();
|
|
|
|
cpu_relax();
|
|
|
|
local_irq_disable();
|
|
|
|
} else
|
|
|
|
cpu_relax();
|
2005-04-17 05:20:36 +07:00
|
|
|
mb();
|
|
|
|
}
|
|
|
|
|
2009-12-03 02:01:25 +07:00
|
|
|
static inline void arch_spin_unlock(arch_spinlock_t *x)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
|
|
|
volatile unsigned int *a;
|
|
|
|
mb();
|
|
|
|
a = __ldcw_align(x);
|
|
|
|
*a = 1;
|
|
|
|
mb();
|
|
|
|
}
|
|
|
|
|
2009-12-03 02:01:25 +07:00
|
|
|
static inline int arch_spin_trylock(arch_spinlock_t *x)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
|
|
|
volatile unsigned int *a;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
mb();
|
|
|
|
a = __ldcw_align(x);
|
|
|
|
ret = __ldcw(a) != 0;
|
|
|
|
mb();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-09-02 20:54:58 +07:00
|
|
|
* Read-write spinlocks, allowing multiple readers but only one writer.
|
2006-09-08 18:43:44 +07:00
|
|
|
* Linux rwlocks are unfair to writers; they can be starved for an indefinite
|
|
|
|
* time by readers. With care, they can also be taken in interrupt context.
|
|
|
|
*
|
|
|
|
* In the PA-RISC implementation, we have a spinlock and a counter.
|
|
|
|
* Readers use the lock to serialise their access to the counter (which
|
|
|
|
* records how many readers currently hold the lock).
|
|
|
|
* Writers hold the spinlock, preventing any readers or other writers from
|
|
|
|
* grabbing the rwlock.
|
2005-04-17 05:20:36 +07:00
|
|
|
*/
|
|
|
|
|
2006-09-08 18:43:44 +07:00
|
|
|
/* Note that we have to ensure interrupts are disabled in case we're
|
|
|
|
* interrupted by some other code that wants to grab the same read lock */
|
2009-12-04 02:08:46 +07:00
|
|
|
static __inline__ void arch_read_lock(arch_rwlock_t *rw)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2006-09-02 20:54:58 +07:00
|
|
|
unsigned long flags;
|
|
|
|
local_irq_save(flags);
|
2009-12-03 02:01:25 +07:00
|
|
|
arch_spin_lock_flags(&rw->lock, flags);
|
2005-04-17 05:20:36 +07:00
|
|
|
rw->counter++;
|
2009-12-03 02:01:25 +07:00
|
|
|
arch_spin_unlock(&rw->lock);
|
2006-09-02 20:54:58 +07:00
|
|
|
local_irq_restore(flags);
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
2006-09-08 18:43:44 +07:00
|
|
|
/* Note that we have to ensure interrupts are disabled in case we're
|
|
|
|
* interrupted by some other code that wants to grab the same read lock */
|
2009-12-04 02:08:46 +07:00
|
|
|
static __inline__ void arch_read_unlock(arch_rwlock_t *rw)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2006-09-02 20:54:58 +07:00
|
|
|
unsigned long flags;
|
|
|
|
local_irq_save(flags);
|
2009-12-03 02:01:25 +07:00
|
|
|
arch_spin_lock_flags(&rw->lock, flags);
|
2005-04-17 05:20:36 +07:00
|
|
|
rw->counter--;
|
2009-12-03 02:01:25 +07:00
|
|
|
arch_spin_unlock(&rw->lock);
|
2006-09-02 20:54:58 +07:00
|
|
|
local_irq_restore(flags);
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
2006-09-08 18:43:44 +07:00
|
|
|
/* Note that we have to ensure interrupts are disabled in case we're
|
|
|
|
* interrupted by some other code that wants to grab the same read lock */
|
2009-12-04 02:08:46 +07:00
|
|
|
static __inline__ int arch_read_trylock(arch_rwlock_t *rw)
|
2006-09-02 20:54:58 +07:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
retry:
|
|
|
|
local_irq_save(flags);
|
2009-12-03 02:01:25 +07:00
|
|
|
if (arch_spin_trylock(&rw->lock)) {
|
2006-09-02 20:54:58 +07:00
|
|
|
rw->counter++;
|
2009-12-03 02:01:25 +07:00
|
|
|
arch_spin_unlock(&rw->lock);
|
2006-09-02 20:54:58 +07:00
|
|
|
local_irq_restore(flags);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
local_irq_restore(flags);
|
|
|
|
/* If write-locked, we fail to acquire the lock */
|
|
|
|
if (rw->counter < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Wait until we have a realistic chance at the lock */
|
2009-12-03 02:01:25 +07:00
|
|
|
while (arch_spin_is_locked(&rw->lock) && rw->counter >= 0)
|
2006-09-02 20:54:58 +07:00
|
|
|
cpu_relax();
|
|
|
|
|
|
|
|
goto retry;
|
|
|
|
}
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2006-09-08 18:43:44 +07:00
|
|
|
/* Note that we have to ensure interrupts are disabled in case we're
|
|
|
|
* interrupted by some other code that wants to read_trylock() this lock */
|
2009-12-04 02:08:46 +07:00
|
|
|
static __inline__ void arch_write_lock(arch_rwlock_t *rw)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2006-09-02 20:54:58 +07:00
|
|
|
unsigned long flags;
|
2005-04-17 05:20:36 +07:00
|
|
|
retry:
|
2006-09-02 20:54:58 +07:00
|
|
|
local_irq_save(flags);
|
2009-12-03 02:01:25 +07:00
|
|
|
arch_spin_lock_flags(&rw->lock, flags);
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2006-09-02 20:54:58 +07:00
|
|
|
if (rw->counter != 0) {
|
2009-12-03 02:01:25 +07:00
|
|
|
arch_spin_unlock(&rw->lock);
|
2006-09-02 20:54:58 +07:00
|
|
|
local_irq_restore(flags);
|
2005-04-17 05:20:36 +07:00
|
|
|
|
[PATCH] spinlock consolidation
This patch (written by me and also containing many suggestions of Arjan van
de Ven) does a major cleanup of the spinlock code. It does the following
things:
- consolidates and enhances the spinlock/rwlock debugging code
- simplifies the asm/spinlock.h files
- encapsulates the raw spinlock type and moves generic spinlock
features (such as ->break_lock) into the generic code.
- cleans up the spinlock code hierarchy to get rid of the spaghetti.
Most notably there's now only a single variant of the debugging code,
located in lib/spinlock_debug.c. (previously we had one SMP debugging
variant per architecture, plus a separate generic one for UP builds)
Also, i've enhanced the rwlock debugging facility, it will now track
write-owners. There is new spinlock-owner/CPU-tracking on SMP builds too.
All locks have lockup detection now, which will work for both soft and hard
spin/rwlock lockups.
The arch-level include files now only contain the minimally necessary
subset of the spinlock code - all the rest that can be generalized now
lives in the generic headers:
include/asm-i386/spinlock_types.h | 16
include/asm-x86_64/spinlock_types.h | 16
I have also split up the various spinlock variants into separate files,
making it easier to see which does what. The new layout is:
SMP | UP
----------------------------|-----------------------------------
asm/spinlock_types_smp.h | linux/spinlock_types_up.h
linux/spinlock_types.h | linux/spinlock_types.h
asm/spinlock_smp.h | linux/spinlock_up.h
linux/spinlock_api_smp.h | linux/spinlock_api_up.h
linux/spinlock.h | linux/spinlock.h
/*
* here's the role of the various spinlock/rwlock related include files:
*
* on SMP builds:
*
* asm/spinlock_types.h: contains the raw_spinlock_t/raw_rwlock_t and the
* initializers
*
* linux/spinlock_types.h:
* defines the generic type and initializers
*
* asm/spinlock.h: contains the __raw_spin_*()/etc. lowlevel
* implementations, mostly inline assembly code
*
* (also included on UP-debug builds:)
*
* linux/spinlock_api_smp.h:
* contains the prototypes for the _spin_*() APIs.
*
* linux/spinlock.h: builds the final spin_*() APIs.
*
* on UP builds:
*
* linux/spinlock_type_up.h:
* contains the generic, simplified UP spinlock type.
* (which is an empty structure on non-debug builds)
*
* linux/spinlock_types.h:
* defines the generic type and initializers
*
* linux/spinlock_up.h:
* contains the __raw_spin_*()/etc. version of UP
* builds. (which are NOPs on non-debug, non-preempt
* builds)
*
* (included on UP-non-debug builds:)
*
* linux/spinlock_api_up.h:
* builds the _spin_*() APIs.
*
* linux/spinlock.h: builds the final spin_*() APIs.
*/
All SMP and UP architectures are converted by this patch.
arm, i386, ia64, ppc, ppc64, s390/s390x, x64 was build-tested via
crosscompilers. m32r, mips, sh, sparc, have not been tested yet, but should
be mostly fine.
From: Grant Grundler <grundler@parisc-linux.org>
Booted and lightly tested on a500-44 (64-bit, SMP kernel, dual CPU).
Builds 32-bit SMP kernel (not booted or tested). I did not try to build
non-SMP kernels. That should be trivial to fix up later if necessary.
I converted bit ops atomic_hash lock to raw_spinlock_t. Doing so avoids
some ugly nesting of linux/*.h and asm/*.h files. Those particular locks
are well tested and contained entirely inside arch specific code. I do NOT
expect any new issues to arise with them.
If someone does ever need to use debug/metrics with them, then they will
need to unravel this hairball between spinlocks, atomic ops, and bit ops
that exist only because parisc has exactly one atomic instruction: LDCW
(load and clear word).
From: "Luck, Tony" <tony.luck@intel.com>
ia64 fix
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Arjan van de Ven <arjanv@infradead.org>
Signed-off-by: Grant Grundler <grundler@parisc-linux.org>
Cc: Matthew Wilcox <willy@debian.org>
Signed-off-by: Hirokazu Takata <takata@linux-m32r.org>
Signed-off-by: Mikael Pettersson <mikpe@csd.uu.se>
Signed-off-by: Benoit Boissinot <benoit.boissinot@ens-lyon.org>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2005-09-10 14:25:56 +07:00
|
|
|
while (rw->counter != 0)
|
|
|
|
cpu_relax();
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
2006-09-02 20:54:58 +07:00
|
|
|
rw->counter = -1; /* mark as write-locked */
|
|
|
|
mb();
|
|
|
|
local_irq_restore(flags);
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
2009-12-04 02:08:46 +07:00
|
|
|
static __inline__ void arch_write_unlock(arch_rwlock_t *rw)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
|
|
|
rw->counter = 0;
|
2009-12-03 02:01:25 +07:00
|
|
|
arch_spin_unlock(&rw->lock);
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
2006-09-08 18:43:44 +07:00
|
|
|
/* Note that we have to ensure interrupts are disabled in case we're
|
|
|
|
* interrupted by some other code that wants to read_trylock() this lock */
|
2009-12-04 02:08:46 +07:00
|
|
|
static __inline__ int arch_write_trylock(arch_rwlock_t *rw)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2006-09-02 20:54:58 +07:00
|
|
|
unsigned long flags;
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
local_irq_save(flags);
|
2009-12-03 02:01:25 +07:00
|
|
|
if (arch_spin_trylock(&rw->lock)) {
|
2006-09-02 20:54:58 +07:00
|
|
|
if (rw->counter == 0) {
|
|
|
|
rw->counter = -1;
|
|
|
|
result = 1;
|
|
|
|
} else {
|
|
|
|
/* Read-locked. Oh well. */
|
2009-12-03 02:01:25 +07:00
|
|
|
arch_spin_unlock(&rw->lock);
|
2006-09-02 20:54:58 +07:00
|
|
|
}
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
2006-09-02 20:54:58 +07:00
|
|
|
local_irq_restore(flags);
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2006-09-02 20:54:58 +07:00
|
|
|
return result;
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
2006-03-25 11:22:02 +07:00
|
|
|
/*
|
|
|
|
* read_can_lock - would read_trylock() succeed?
|
|
|
|
* @lock: the rwlock in question.
|
|
|
|
*/
|
2009-12-04 02:08:46 +07:00
|
|
|
static __inline__ int arch_read_can_lock(arch_rwlock_t *rw)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2006-03-25 11:22:02 +07:00
|
|
|
return rw->counter >= 0;
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
2006-03-25 11:22:02 +07:00
|
|
|
/*
|
|
|
|
* write_can_lock - would write_trylock() succeed?
|
|
|
|
* @lock: the rwlock in question.
|
|
|
|
*/
|
2009-12-04 02:08:46 +07:00
|
|
|
static __inline__ int arch_write_can_lock(arch_rwlock_t *rw)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2006-03-25 11:22:02 +07:00
|
|
|
return !rw->counter;
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
2009-12-04 02:08:46 +07:00
|
|
|
#define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
|
|
|
|
#define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
|
2009-04-03 06:59:46 +07:00
|
|
|
|
2005-04-17 05:20:36 +07:00
|
|
|
#endif /* __ASM_SPINLOCK_H */
|