aboutsummaryrefslogtreecommitdiffstats
path: root/include/asm-i386/spinlock.h
diff options
context:
space:
mode:
authorIngo Molnar <mingo@elte.hu>2005-09-10 03:25:56 -0400
committerLinus Torvalds <torvalds@g5.osdl.org>2005-09-10 13:06:21 -0400
commitfb1c8f93d869b34cacb8b8932e2b83d96a19d720 (patch)
treea006d078aa02e421a7dc4793c335308204859d36 /include/asm-i386/spinlock.h
parent4327edf6b8a7ac7dce144313947995538842d8fd (diff)
[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>
Diffstat (limited to 'include/asm-i386/spinlock.h')
-rw-r--r--include/asm-i386/spinlock.h200
1 files changed, 70 insertions, 130 deletions
diff --git a/include/asm-i386/spinlock.h b/include/asm-i386/spinlock.h
index f9ff31f40036..23604350cdf4 100644
--- a/include/asm-i386/spinlock.h
+++ b/include/asm-i386/spinlock.h
@@ -7,46 +7,21 @@
7#include <linux/config.h> 7#include <linux/config.h>
8#include <linux/compiler.h> 8#include <linux/compiler.h>
9 9
10asmlinkage int printk(const char * fmt, ...)
11 __attribute__ ((format (printf, 1, 2)));
12
13/* 10/*
14 * Your basic SMP spinlocks, allowing only a single CPU anywhere 11 * Your basic SMP spinlocks, allowing only a single CPU anywhere
15 */ 12 *
16
17typedef struct {
18 volatile unsigned int slock;
19#ifdef CONFIG_DEBUG_SPINLOCK
20 unsigned magic;
21#endif
22#ifdef CONFIG_PREEMPT
23 unsigned int break_lock;
24#endif
25} spinlock_t;
26
27#define SPINLOCK_MAGIC 0xdead4ead
28
29#ifdef CONFIG_DEBUG_SPINLOCK
30#define SPINLOCK_MAGIC_INIT , SPINLOCK_MAGIC
31#else
32#define SPINLOCK_MAGIC_INIT /* */
33#endif
34
35#define SPIN_LOCK_UNLOCKED (spinlock_t) { 1 SPINLOCK_MAGIC_INIT }
36
37#define spin_lock_init(x) do { *(x) = SPIN_LOCK_UNLOCKED; } while(0)
38
39/*
40 * Simple spin lock operations. There are two variants, one clears IRQ's 13 * Simple spin lock operations. There are two variants, one clears IRQ's
41 * on the local processor, one does not. 14 * on the local processor, one does not.
42 * 15 *
43 * We make no fairness assumptions. They have a cost. 16 * We make no fairness assumptions. They have a cost.
17 *
18 * (the type definitions are in asm/spinlock_types.h)
44 */ 19 */
45 20
46#define spin_is_locked(x) (*(volatile signed char *)(&(x)->slock) <= 0) 21#define __raw_spin_is_locked(x) \
47#define spin_unlock_wait(x) do { barrier(); } while(spin_is_locked(x)) 22 (*(volatile signed char *)(&(x)->slock) <= 0)
48 23
49#define spin_lock_string \ 24#define __raw_spin_lock_string \
50 "\n1:\t" \ 25 "\n1:\t" \
51 "lock ; decb %0\n\t" \ 26 "lock ; decb %0\n\t" \
52 "jns 3f\n" \ 27 "jns 3f\n" \
@@ -57,7 +32,7 @@ typedef struct {
57 "jmp 1b\n" \ 32 "jmp 1b\n" \
58 "3:\n\t" 33 "3:\n\t"
59 34
60#define spin_lock_string_flags \ 35#define __raw_spin_lock_string_flags \
61 "\n1:\t" \ 36 "\n1:\t" \
62 "lock ; decb %0\n\t" \ 37 "lock ; decb %0\n\t" \
63 "jns 4f\n\t" \ 38 "jns 4f\n\t" \
@@ -73,86 +48,71 @@ typedef struct {
73 "jmp 1b\n" \ 48 "jmp 1b\n" \
74 "4:\n\t" 49 "4:\n\t"
75 50
51static inline void __raw_spin_lock(raw_spinlock_t *lock)
52{
53 __asm__ __volatile__(
54 __raw_spin_lock_string
55 :"=m" (lock->slock) : : "memory");
56}
57
58static inline void __raw_spin_lock_flags(raw_spinlock_t *lock, unsigned long flags)
59{
60 __asm__ __volatile__(
61 __raw_spin_lock_string_flags
62 :"=m" (lock->slock) : "r" (flags) : "memory");
63}
64
65static inline int __raw_spin_trylock(raw_spinlock_t *lock)
66{
67 char oldval;
68 __asm__ __volatile__(
69 "xchgb %b0,%1"
70 :"=q" (oldval), "=m" (lock->slock)
71 :"0" (0) : "memory");
72 return oldval > 0;
73}
74
76/* 75/*
77 * This works. Despite all the confusion. 76 * __raw_spin_unlock based on writing $1 to the low byte.
78 * (except on PPro SMP or if we are using OOSTORE) 77 * This method works. Despite all the confusion.
78 * (except on PPro SMP or if we are using OOSTORE, so we use xchgb there)
79 * (PPro errata 66, 92) 79 * (PPro errata 66, 92)
80 */ 80 */
81 81
82#if !defined(CONFIG_X86_OOSTORE) && !defined(CONFIG_X86_PPRO_FENCE) 82#if !defined(CONFIG_X86_OOSTORE) && !defined(CONFIG_X86_PPRO_FENCE)
83 83
84#define spin_unlock_string \ 84#define __raw_spin_unlock_string \
85 "movb $1,%0" \ 85 "movb $1,%0" \
86 :"=m" (lock->slock) : : "memory" 86 :"=m" (lock->slock) : : "memory"
87 87
88 88
89static inline void _raw_spin_unlock(spinlock_t *lock) 89static inline void __raw_spin_unlock(raw_spinlock_t *lock)
90{ 90{
91#ifdef CONFIG_DEBUG_SPINLOCK
92 BUG_ON(lock->magic != SPINLOCK_MAGIC);
93 BUG_ON(!spin_is_locked(lock));
94#endif
95 __asm__ __volatile__( 91 __asm__ __volatile__(
96 spin_unlock_string 92 __raw_spin_unlock_string
97 ); 93 );
98} 94}
99 95
100#else 96#else
101 97
102#define spin_unlock_string \ 98#define __raw_spin_unlock_string \
103 "xchgb %b0, %1" \ 99 "xchgb %b0, %1" \
104 :"=q" (oldval), "=m" (lock->slock) \ 100 :"=q" (oldval), "=m" (lock->slock) \
105 :"0" (oldval) : "memory" 101 :"0" (oldval) : "memory"
106 102
107static inline void _raw_spin_unlock(spinlock_t *lock) 103static inline void __raw_spin_unlock(raw_spinlock_t *lock)
108{ 104{
109 char oldval = 1; 105 char oldval = 1;
110#ifdef CONFIG_DEBUG_SPINLOCK
111 BUG_ON(lock->magic != SPINLOCK_MAGIC);
112 BUG_ON(!spin_is_locked(lock));
113#endif
114 __asm__ __volatile__(
115 spin_unlock_string
116 );
117}
118 106
119#endif
120
121static inline int _raw_spin_trylock(spinlock_t *lock)
122{
123 char oldval;
124 __asm__ __volatile__( 107 __asm__ __volatile__(
125 "xchgb %b0,%1" 108 __raw_spin_unlock_string
126 :"=q" (oldval), "=m" (lock->slock) 109 );
127 :"0" (0) : "memory");
128 return oldval > 0;
129} 110}
130 111
131static inline void _raw_spin_lock(spinlock_t *lock)
132{
133#ifdef CONFIG_DEBUG_SPINLOCK
134 if (unlikely(lock->magic != SPINLOCK_MAGIC)) {
135 printk("eip: %p\n", __builtin_return_address(0));
136 BUG();
137 }
138#endif 112#endif
139 __asm__ __volatile__(
140 spin_lock_string
141 :"=m" (lock->slock) : : "memory");
142}
143 113
144static inline void _raw_spin_lock_flags (spinlock_t *lock, unsigned long flags) 114#define __raw_spin_unlock_wait(lock) \
145{ 115 do { while (__raw_spin_is_locked(lock)) cpu_relax(); } while (0)
146#ifdef CONFIG_DEBUG_SPINLOCK
147 if (unlikely(lock->magic != SPINLOCK_MAGIC)) {
148 printk("eip: %p\n", __builtin_return_address(0));
149 BUG();
150 }
151#endif
152 __asm__ __volatile__(
153 spin_lock_string_flags
154 :"=m" (lock->slock) : "r" (flags) : "memory");
155}
156 116
157/* 117/*
158 * Read-write spinlocks, allowing multiple readers 118 * Read-write spinlocks, allowing multiple readers
@@ -163,72 +123,41 @@ static inline void _raw_spin_lock_flags (spinlock_t *lock, unsigned long flags)
163 * can "mix" irq-safe locks - any writer needs to get a 123 * can "mix" irq-safe locks - any writer needs to get a
164 * irq-safe write-lock, but readers can get non-irqsafe 124 * irq-safe write-lock, but readers can get non-irqsafe
165 * read-locks. 125 * read-locks.
126 *
127 * On x86, we implement read-write locks as a 32-bit counter
128 * with the high bit (sign) being the "contended" bit.
129 *
130 * The inline assembly is non-obvious. Think about it.
131 *
132 * Changed to use the same technique as rw semaphores. See
133 * semaphore.h for details. -ben
134 *
135 * the helpers are in arch/i386/kernel/semaphore.c
166 */ 136 */
167typedef struct {
168 volatile unsigned int lock;
169#ifdef CONFIG_DEBUG_SPINLOCK
170 unsigned magic;
171#endif
172#ifdef CONFIG_PREEMPT
173 unsigned int break_lock;
174#endif
175} rwlock_t;
176
177#define RWLOCK_MAGIC 0xdeaf1eed
178
179#ifdef CONFIG_DEBUG_SPINLOCK
180#define RWLOCK_MAGIC_INIT , RWLOCK_MAGIC
181#else
182#define RWLOCK_MAGIC_INIT /* */
183#endif
184
185#define RW_LOCK_UNLOCKED (rwlock_t) { RW_LOCK_BIAS RWLOCK_MAGIC_INIT }
186
187#define rwlock_init(x) do { *(x) = RW_LOCK_UNLOCKED; } while(0)
188 137
189/** 138/**
190 * read_can_lock - would read_trylock() succeed? 139 * read_can_lock - would read_trylock() succeed?
191 * @lock: the rwlock in question. 140 * @lock: the rwlock in question.
192 */ 141 */
193#define read_can_lock(x) ((int)(x)->lock > 0) 142#define __raw_read_can_lock(x) ((int)(x)->lock > 0)
194 143
195/** 144/**
196 * write_can_lock - would write_trylock() succeed? 145 * write_can_lock - would write_trylock() succeed?
197 * @lock: the rwlock in question. 146 * @lock: the rwlock in question.
198 */ 147 */
199#define write_can_lock(x) ((x)->lock == RW_LOCK_BIAS) 148#define __raw_write_can_lock(x) ((x)->lock == RW_LOCK_BIAS)
200 149
201/* 150static inline void __raw_read_lock(raw_rwlock_t *rw)
202 * On x86, we implement read-write locks as a 32-bit counter
203 * with the high bit (sign) being the "contended" bit.
204 *
205 * The inline assembly is non-obvious. Think about it.
206 *
207 * Changed to use the same technique as rw semaphores. See
208 * semaphore.h for details. -ben
209 */
210/* the spinlock helpers are in arch/i386/kernel/semaphore.c */
211
212static inline void _raw_read_lock(rwlock_t *rw)
213{ 151{
214#ifdef CONFIG_DEBUG_SPINLOCK
215 BUG_ON(rw->magic != RWLOCK_MAGIC);
216#endif
217 __build_read_lock(rw, "__read_lock_failed"); 152 __build_read_lock(rw, "__read_lock_failed");
218} 153}
219 154
220static inline void _raw_write_lock(rwlock_t *rw) 155static inline void __raw_write_lock(raw_rwlock_t *rw)
221{ 156{
222#ifdef CONFIG_DEBUG_SPINLOCK
223 BUG_ON(rw->magic != RWLOCK_MAGIC);
224#endif
225 __build_write_lock(rw, "__write_lock_failed"); 157 __build_write_lock(rw, "__write_lock_failed");
226} 158}
227 159
228#define _raw_read_unlock(rw) asm volatile("lock ; incl %0" :"=m" ((rw)->lock) : : "memory") 160static inline int __raw_read_trylock(raw_rwlock_t *lock)
229#define _raw_write_unlock(rw) asm volatile("lock ; addl $" RW_LOCK_BIAS_STR ",%0":"=m" ((rw)->lock) : : "memory")
230
231static inline int _raw_read_trylock(rwlock_t *lock)
232{ 161{
233 atomic_t *count = (atomic_t *)lock; 162 atomic_t *count = (atomic_t *)lock;
234 atomic_dec(count); 163 atomic_dec(count);
@@ -238,7 +167,7 @@ static inline int _raw_read_trylock(rwlock_t *lock)
238 return 0; 167 return 0;
239} 168}
240 169
241static inline int _raw_write_trylock(rwlock_t *lock) 170static inline int __raw_write_trylock(raw_rwlock_t *lock)
242{ 171{
243 atomic_t *count = (atomic_t *)lock; 172 atomic_t *count = (atomic_t *)lock;
244 if (atomic_sub_and_test(RW_LOCK_BIAS, count)) 173 if (atomic_sub_and_test(RW_LOCK_BIAS, count))
@@ -247,4 +176,15 @@ static inline int _raw_write_trylock(rwlock_t *lock)
247 return 0; 176 return 0;
248} 177}
249 178
179static inline void __raw_read_unlock(raw_rwlock_t *rw)
180{
181 asm volatile("lock ; incl %0" :"=m" (rw->lock) : : "memory");
182}
183
184static inline void __raw_write_unlock(raw_rwlock_t *rw)
185{
186 asm volatile("lock ; addl $" RW_LOCK_BIAS_STR ", %0"
187 : "=m" (rw->lock) : : "memory");
188}
189
250#endif /* __ASM_SPINLOCK_H */ 190#endif /* __ASM_SPINLOCK_H */