diff options
Diffstat (limited to 'arch/x86/include/asm/rwsem.h')
-rw-r--r-- | arch/x86/include/asm/rwsem.h | 100 |
1 files changed, 57 insertions, 43 deletions
diff --git a/arch/x86/include/asm/rwsem.h b/arch/x86/include/asm/rwsem.h index ca7517d33776..d1e41b0f9b60 100644 --- a/arch/x86/include/asm/rwsem.h +++ b/arch/x86/include/asm/rwsem.h | |||
@@ -41,6 +41,7 @@ | |||
41 | #include <linux/list.h> | 41 | #include <linux/list.h> |
42 | #include <linux/spinlock.h> | 42 | #include <linux/spinlock.h> |
43 | #include <linux/lockdep.h> | 43 | #include <linux/lockdep.h> |
44 | #include <asm/asm.h> | ||
44 | 45 | ||
45 | struct rwsem_waiter; | 46 | struct rwsem_waiter; |
46 | 47 | ||
@@ -55,17 +56,28 @@ extern asmregparm struct rw_semaphore * | |||
55 | 56 | ||
56 | /* | 57 | /* |
57 | * the semaphore definition | 58 | * the semaphore definition |
59 | * | ||
60 | * The bias values and the counter type limits the number of | ||
61 | * potential readers/writers to 32767 for 32 bits and 2147483647 | ||
62 | * for 64 bits. | ||
58 | */ | 63 | */ |
59 | 64 | ||
60 | #define RWSEM_UNLOCKED_VALUE 0x00000000 | 65 | #ifdef CONFIG_X86_64 |
61 | #define RWSEM_ACTIVE_BIAS 0x00000001 | 66 | # define RWSEM_ACTIVE_MASK 0xffffffffL |
62 | #define RWSEM_ACTIVE_MASK 0x0000ffff | 67 | #else |
63 | #define RWSEM_WAITING_BIAS (-0x00010000) | 68 | # define RWSEM_ACTIVE_MASK 0x0000ffffL |
69 | #endif | ||
70 | |||
71 | #define RWSEM_UNLOCKED_VALUE 0x00000000L | ||
72 | #define RWSEM_ACTIVE_BIAS 0x00000001L | ||
73 | #define RWSEM_WAITING_BIAS (-RWSEM_ACTIVE_MASK-1) | ||
64 | #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS | 74 | #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS |
65 | #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS) | 75 | #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS) |
66 | 76 | ||
77 | typedef signed long rwsem_count_t; | ||
78 | |||
67 | struct rw_semaphore { | 79 | struct rw_semaphore { |
68 | signed long count; | 80 | rwsem_count_t count; |
69 | spinlock_t wait_lock; | 81 | spinlock_t wait_lock; |
70 | struct list_head wait_list; | 82 | struct list_head wait_list; |
71 | #ifdef CONFIG_DEBUG_LOCK_ALLOC | 83 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
@@ -105,8 +117,8 @@ do { \ | |||
105 | static inline void __down_read(struct rw_semaphore *sem) | 117 | static inline void __down_read(struct rw_semaphore *sem) |
106 | { | 118 | { |
107 | asm volatile("# beginning down_read\n\t" | 119 | asm volatile("# beginning down_read\n\t" |
108 | LOCK_PREFIX " incl (%%eax)\n\t" | 120 | LOCK_PREFIX _ASM_INC "(%1)\n\t" |
109 | /* adds 0x00000001, returns the old value */ | 121 | /* adds 0x00000001 */ |
110 | " jns 1f\n" | 122 | " jns 1f\n" |
111 | " call call_rwsem_down_read_failed\n" | 123 | " call call_rwsem_down_read_failed\n" |
112 | "1:\n\t" | 124 | "1:\n\t" |
@@ -121,14 +133,14 @@ static inline void __down_read(struct rw_semaphore *sem) | |||
121 | */ | 133 | */ |
122 | static inline int __down_read_trylock(struct rw_semaphore *sem) | 134 | static inline int __down_read_trylock(struct rw_semaphore *sem) |
123 | { | 135 | { |
124 | __s32 result, tmp; | 136 | rwsem_count_t result, tmp; |
125 | asm volatile("# beginning __down_read_trylock\n\t" | 137 | asm volatile("# beginning __down_read_trylock\n\t" |
126 | " movl %0,%1\n\t" | 138 | " mov %0,%1\n\t" |
127 | "1:\n\t" | 139 | "1:\n\t" |
128 | " movl %1,%2\n\t" | 140 | " mov %1,%2\n\t" |
129 | " addl %3,%2\n\t" | 141 | " add %3,%2\n\t" |
130 | " jle 2f\n\t" | 142 | " jle 2f\n\t" |
131 | LOCK_PREFIX " cmpxchgl %2,%0\n\t" | 143 | LOCK_PREFIX " cmpxchg %2,%0\n\t" |
132 | " jnz 1b\n\t" | 144 | " jnz 1b\n\t" |
133 | "2:\n\t" | 145 | "2:\n\t" |
134 | "# ending __down_read_trylock\n\t" | 146 | "# ending __down_read_trylock\n\t" |
@@ -143,20 +155,18 @@ static inline int __down_read_trylock(struct rw_semaphore *sem) | |||
143 | */ | 155 | */ |
144 | static inline void __down_write_nested(struct rw_semaphore *sem, int subclass) | 156 | static inline void __down_write_nested(struct rw_semaphore *sem, int subclass) |
145 | { | 157 | { |
146 | int tmp; | 158 | rwsem_count_t tmp; |
147 | |||
148 | tmp = RWSEM_ACTIVE_WRITE_BIAS; | ||
149 | asm volatile("# beginning down_write\n\t" | 159 | asm volatile("# beginning down_write\n\t" |
150 | LOCK_PREFIX " xadd %%edx,(%%eax)\n\t" | 160 | LOCK_PREFIX " xadd %1,(%2)\n\t" |
151 | /* subtract 0x0000ffff, returns the old value */ | 161 | /* adds 0xffff0001, returns the old value */ |
152 | " testl %%edx,%%edx\n\t" | 162 | " test %1,%1\n\t" |
153 | /* was the count 0 before? */ | 163 | /* was the count 0 before? */ |
154 | " jz 1f\n" | 164 | " jz 1f\n" |
155 | " call call_rwsem_down_write_failed\n" | 165 | " call call_rwsem_down_write_failed\n" |
156 | "1:\n" | 166 | "1:\n" |
157 | "# ending down_write" | 167 | "# ending down_write" |
158 | : "+m" (sem->count), "=d" (tmp) | 168 | : "+m" (sem->count), "=d" (tmp) |
159 | : "a" (sem), "1" (tmp) | 169 | : "a" (sem), "1" (RWSEM_ACTIVE_WRITE_BIAS) |
160 | : "memory", "cc"); | 170 | : "memory", "cc"); |
161 | } | 171 | } |
162 | 172 | ||
@@ -170,9 +180,9 @@ static inline void __down_write(struct rw_semaphore *sem) | |||
170 | */ | 180 | */ |
171 | static inline int __down_write_trylock(struct rw_semaphore *sem) | 181 | static inline int __down_write_trylock(struct rw_semaphore *sem) |
172 | { | 182 | { |
173 | signed long ret = cmpxchg(&sem->count, | 183 | rwsem_count_t ret = cmpxchg(&sem->count, |
174 | RWSEM_UNLOCKED_VALUE, | 184 | RWSEM_UNLOCKED_VALUE, |
175 | RWSEM_ACTIVE_WRITE_BIAS); | 185 | RWSEM_ACTIVE_WRITE_BIAS); |
176 | if (ret == RWSEM_UNLOCKED_VALUE) | 186 | if (ret == RWSEM_UNLOCKED_VALUE) |
177 | return 1; | 187 | return 1; |
178 | return 0; | 188 | return 0; |
@@ -183,16 +193,16 @@ static inline int __down_write_trylock(struct rw_semaphore *sem) | |||
183 | */ | 193 | */ |
184 | static inline void __up_read(struct rw_semaphore *sem) | 194 | static inline void __up_read(struct rw_semaphore *sem) |
185 | { | 195 | { |
186 | __s32 tmp = -RWSEM_ACTIVE_READ_BIAS; | 196 | rwsem_count_t tmp; |
187 | asm volatile("# beginning __up_read\n\t" | 197 | asm volatile("# beginning __up_read\n\t" |
188 | LOCK_PREFIX " xadd %%edx,(%%eax)\n\t" | 198 | LOCK_PREFIX " xadd %1,(%2)\n\t" |
189 | /* subtracts 1, returns the old value */ | 199 | /* subtracts 1, returns the old value */ |
190 | " jns 1f\n\t" | 200 | " jns 1f\n\t" |
191 | " call call_rwsem_wake\n" | 201 | " call call_rwsem_wake\n" /* expects old value in %edx */ |
192 | "1:\n" | 202 | "1:\n" |
193 | "# ending __up_read\n" | 203 | "# ending __up_read\n" |
194 | : "+m" (sem->count), "=d" (tmp) | 204 | : "+m" (sem->count), "=d" (tmp) |
195 | : "a" (sem), "1" (tmp) | 205 | : "a" (sem), "1" (-RWSEM_ACTIVE_READ_BIAS) |
196 | : "memory", "cc"); | 206 | : "memory", "cc"); |
197 | } | 207 | } |
198 | 208 | ||
@@ -201,18 +211,17 @@ static inline void __up_read(struct rw_semaphore *sem) | |||
201 | */ | 211 | */ |
202 | static inline void __up_write(struct rw_semaphore *sem) | 212 | static inline void __up_write(struct rw_semaphore *sem) |
203 | { | 213 | { |
214 | rwsem_count_t tmp; | ||
204 | asm volatile("# beginning __up_write\n\t" | 215 | asm volatile("# beginning __up_write\n\t" |
205 | " movl %2,%%edx\n\t" | 216 | LOCK_PREFIX " xadd %1,(%2)\n\t" |
206 | LOCK_PREFIX " xaddl %%edx,(%%eax)\n\t" | 217 | /* subtracts 0xffff0001, returns the old value */ |
207 | /* tries to transition | 218 | " jns 1f\n\t" |
208 | 0xffff0001 -> 0x00000000 */ | 219 | " call call_rwsem_wake\n" /* expects old value in %edx */ |
209 | " jz 1f\n" | ||
210 | " call call_rwsem_wake\n" | ||
211 | "1:\n\t" | 220 | "1:\n\t" |
212 | "# ending __up_write\n" | 221 | "# ending __up_write\n" |
213 | : "+m" (sem->count) | 222 | : "+m" (sem->count), "=d" (tmp) |
214 | : "a" (sem), "i" (-RWSEM_ACTIVE_WRITE_BIAS) | 223 | : "a" (sem), "1" (-RWSEM_ACTIVE_WRITE_BIAS) |
215 | : "memory", "cc", "edx"); | 224 | : "memory", "cc"); |
216 | } | 225 | } |
217 | 226 | ||
218 | /* | 227 | /* |
@@ -221,33 +230,38 @@ static inline void __up_write(struct rw_semaphore *sem) | |||
221 | static inline void __downgrade_write(struct rw_semaphore *sem) | 230 | static inline void __downgrade_write(struct rw_semaphore *sem) |
222 | { | 231 | { |
223 | asm volatile("# beginning __downgrade_write\n\t" | 232 | asm volatile("# beginning __downgrade_write\n\t" |
224 | LOCK_PREFIX " addl %2,(%%eax)\n\t" | 233 | LOCK_PREFIX _ASM_ADD "%2,(%1)\n\t" |
225 | /* transitions 0xZZZZ0001 -> 0xYYYY0001 */ | 234 | /* |
235 | * transitions 0xZZZZ0001 -> 0xYYYY0001 (i386) | ||
236 | * 0xZZZZZZZZ00000001 -> 0xYYYYYYYY00000001 (x86_64) | ||
237 | */ | ||
226 | " jns 1f\n\t" | 238 | " jns 1f\n\t" |
227 | " call call_rwsem_downgrade_wake\n" | 239 | " call call_rwsem_downgrade_wake\n" |
228 | "1:\n\t" | 240 | "1:\n\t" |
229 | "# ending __downgrade_write\n" | 241 | "# ending __downgrade_write\n" |
230 | : "+m" (sem->count) | 242 | : "+m" (sem->count) |
231 | : "a" (sem), "i" (-RWSEM_WAITING_BIAS) | 243 | : "a" (sem), "er" (-RWSEM_WAITING_BIAS) |
232 | : "memory", "cc"); | 244 | : "memory", "cc"); |
233 | } | 245 | } |
234 | 246 | ||
235 | /* | 247 | /* |
236 | * implement atomic add functionality | 248 | * implement atomic add functionality |
237 | */ | 249 | */ |
238 | static inline void rwsem_atomic_add(int delta, struct rw_semaphore *sem) | 250 | static inline void rwsem_atomic_add(rwsem_count_t delta, |
251 | struct rw_semaphore *sem) | ||
239 | { | 252 | { |
240 | asm volatile(LOCK_PREFIX "addl %1,%0" | 253 | asm volatile(LOCK_PREFIX _ASM_ADD "%1,%0" |
241 | : "+m" (sem->count) | 254 | : "+m" (sem->count) |
242 | : "ir" (delta)); | 255 | : "er" (delta)); |
243 | } | 256 | } |
244 | 257 | ||
245 | /* | 258 | /* |
246 | * implement exchange and add functionality | 259 | * implement exchange and add functionality |
247 | */ | 260 | */ |
248 | static inline int rwsem_atomic_update(int delta, struct rw_semaphore *sem) | 261 | static inline rwsem_count_t rwsem_atomic_update(rwsem_count_t delta, |
262 | struct rw_semaphore *sem) | ||
249 | { | 263 | { |
250 | int tmp = delta; | 264 | rwsem_count_t tmp = delta; |
251 | 265 | ||
252 | asm volatile(LOCK_PREFIX "xadd %0,%1" | 266 | asm volatile(LOCK_PREFIX "xadd %0,%1" |
253 | : "+r" (tmp), "+m" (sem->count) | 267 | : "+r" (tmp), "+m" (sem->count) |