Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* rwsem.h: R/W semaphores implemented using XADD/CMPXCHG for i486+ |
| 2 | * |
| 3 | * Written by David Howells (dhowells@redhat.com). |
| 4 | * |
Dave Jones | 99122a3 | 2008-01-30 13:30:28 +0100 | [diff] [blame] | 5 | * Derived from asm-x86/semaphore.h |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * |
| 8 | * The MSW of the count is the negated number of active writers and waiting |
| 9 | * lockers, and the LSW is the total number of active locks |
| 10 | * |
| 11 | * The lock count is initialized to 0 (no active and no waiting lockers). |
| 12 | * |
| 13 | * When a writer subtracts WRITE_BIAS, it'll get 0xffff0001 for the case of an |
| 14 | * uncontended lock. This can be determined because XADD returns the old value. |
| 15 | * Readers increment by 1 and see a positive value when uncontended, negative |
| 16 | * if there are writers (and maybe) readers waiting (in which case it goes to |
| 17 | * sleep). |
| 18 | * |
| 19 | * The value of WAITING_BIAS supports up to 32766 waiting processes. This can |
| 20 | * be extended to 65534 by manually checking the whole MSW rather than relying |
| 21 | * on the S flag. |
| 22 | * |
| 23 | * The value of ACTIVE_BIAS supports up to 65535 active processes. |
| 24 | * |
| 25 | * This should be totally fair - if anything is waiting, a process that wants a |
| 26 | * lock will go to the back of the queue. When the currently active lock is |
| 27 | * released, if there's a writer at the front of the queue, then that and only |
| 28 | * that will be woken up; if there's a bunch of consequtive readers at the |
| 29 | * front, then they'll all be woken up, but no other readers will be. |
| 30 | */ |
| 31 | |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 32 | #ifndef _ASM_X86_RWSEM_H |
| 33 | #define _ASM_X86_RWSEM_H |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | #ifndef _LINUX_RWSEM_H |
| 36 | #error "please don't include asm/rwsem.h directly, use linux/rwsem.h instead" |
| 37 | #endif |
| 38 | |
| 39 | #ifdef __KERNEL__ |
| 40 | |
| 41 | #include <linux/list.h> |
| 42 | #include <linux/spinlock.h> |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 43 | #include <linux/lockdep.h> |
H. Peter Anvin | 1838ef1 | 2010-01-18 14:00:34 -0800 | [diff] [blame] | 44 | #include <asm/asm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | struct rwsem_waiter; |
| 47 | |
Ingo Molnar | d50efc6 | 2008-01-30 13:33:00 +0100 | [diff] [blame] | 48 | extern asmregparm struct rw_semaphore * |
| 49 | rwsem_down_read_failed(struct rw_semaphore *sem); |
| 50 | extern asmregparm struct rw_semaphore * |
| 51 | rwsem_down_write_failed(struct rw_semaphore *sem); |
| 52 | extern asmregparm struct rw_semaphore * |
| 53 | rwsem_wake(struct rw_semaphore *); |
| 54 | extern asmregparm struct rw_semaphore * |
| 55 | rwsem_downgrade_wake(struct rw_semaphore *sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | /* |
| 58 | * the semaphore definition |
Linus Torvalds | 5d0b723 | 2010-01-12 17:57:35 -0800 | [diff] [blame] | 59 | * |
H. Peter Anvin | 1838ef1 | 2010-01-18 14:00:34 -0800 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | */ |
Joe Perches | 6e5609a | 2008-03-23 01:03:21 -0700 | [diff] [blame] | 64 | |
H. Peter Anvin | 1838ef1 | 2010-01-18 14:00:34 -0800 | [diff] [blame] | 65 | #ifdef CONFIG_X86_64 |
| 66 | # define RWSEM_ACTIVE_MASK 0xffffffffL |
| 67 | #else |
| 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) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS |
| 75 | #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS) |
Joe Perches | 6e5609a | 2008-03-23 01:03:21 -0700 | [diff] [blame] | 76 | |
H. Peter Anvin | 1838ef1 | 2010-01-18 14:00:34 -0800 | [diff] [blame] | 77 | typedef signed long rwsem_count_t; |
Linus Torvalds | 5d0b723 | 2010-01-12 17:57:35 -0800 | [diff] [blame] | 78 | |
Joe Perches | 6e5609a | 2008-03-23 01:03:21 -0700 | [diff] [blame] | 79 | struct rw_semaphore { |
Linus Torvalds | 5d0b723 | 2010-01-12 17:57:35 -0800 | [diff] [blame] | 80 | rwsem_count_t count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | spinlock_t wait_lock; |
| 82 | struct list_head wait_list; |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 83 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 84 | struct lockdep_map dep_map; |
| 85 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 88 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 89 | # define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname } |
| 90 | #else |
| 91 | # define __RWSEM_DEP_MAP_INIT(lockname) |
| 92 | #endif |
| 93 | |
| 94 | |
Joe Perches | 6e5609a | 2008-03-23 01:03:21 -0700 | [diff] [blame] | 95 | #define __RWSEM_INITIALIZER(name) \ |
| 96 | { \ |
| 97 | RWSEM_UNLOCKED_VALUE, __SPIN_LOCK_UNLOCKED((name).wait_lock), \ |
| 98 | LIST_HEAD_INIT((name).wait_list) __RWSEM_DEP_MAP_INIT(name) \ |
| 99 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
Joe Perches | 6e5609a | 2008-03-23 01:03:21 -0700 | [diff] [blame] | 101 | #define DECLARE_RWSEM(name) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | struct rw_semaphore name = __RWSEM_INITIALIZER(name) |
| 103 | |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 104 | extern void __init_rwsem(struct rw_semaphore *sem, const char *name, |
| 105 | struct lock_class_key *key); |
| 106 | |
| 107 | #define init_rwsem(sem) \ |
| 108 | do { \ |
| 109 | static struct lock_class_key __key; \ |
| 110 | \ |
| 111 | __init_rwsem((sem), #sem, &__key); \ |
| 112 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | /* |
| 115 | * lock for reading |
| 116 | */ |
| 117 | static inline void __down_read(struct rw_semaphore *sem) |
| 118 | { |
Joe Perches | 6e5609a | 2008-03-23 01:03:21 -0700 | [diff] [blame] | 119 | asm volatile("# beginning down_read\n\t" |
H. Peter Anvin | 1838ef1 | 2010-01-18 14:00:34 -0800 | [diff] [blame] | 120 | LOCK_PREFIX _ASM_INC "(%1)\n\t" |
Michel Lespinasse | b4bcb4c | 2010-07-20 15:19:45 -0700 | [diff] [blame] | 121 | /* adds 0x00000001 */ |
Joe Perches | 6e5609a | 2008-03-23 01:03:21 -0700 | [diff] [blame] | 122 | " jns 1f\n" |
| 123 | " call call_rwsem_down_read_failed\n" |
| 124 | "1:\n\t" |
| 125 | "# ending down_read\n\t" |
| 126 | : "+m" (sem->count) |
| 127 | : "a" (sem) |
| 128 | : "memory", "cc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /* |
| 132 | * trylock for reading -- returns 1 if successful, 0 if contention |
| 133 | */ |
| 134 | static inline int __down_read_trylock(struct rw_semaphore *sem) |
| 135 | { |
Linus Torvalds | 5d0b723 | 2010-01-12 17:57:35 -0800 | [diff] [blame] | 136 | rwsem_count_t result, tmp; |
Joe Perches | 6e5609a | 2008-03-23 01:03:21 -0700 | [diff] [blame] | 137 | asm volatile("# beginning __down_read_trylock\n\t" |
Linus Torvalds | 59c33fa | 2010-01-12 16:21:09 -0800 | [diff] [blame] | 138 | " mov %0,%1\n\t" |
Joe Perches | 6e5609a | 2008-03-23 01:03:21 -0700 | [diff] [blame] | 139 | "1:\n\t" |
Linus Torvalds | 59c33fa | 2010-01-12 16:21:09 -0800 | [diff] [blame] | 140 | " mov %1,%2\n\t" |
| 141 | " add %3,%2\n\t" |
Joe Perches | 6e5609a | 2008-03-23 01:03:21 -0700 | [diff] [blame] | 142 | " jle 2f\n\t" |
Linus Torvalds | 59c33fa | 2010-01-12 16:21:09 -0800 | [diff] [blame] | 143 | LOCK_PREFIX " cmpxchg %2,%0\n\t" |
Joe Perches | 6e5609a | 2008-03-23 01:03:21 -0700 | [diff] [blame] | 144 | " jnz 1b\n\t" |
| 145 | "2:\n\t" |
| 146 | "# ending __down_read_trylock\n\t" |
| 147 | : "+m" (sem->count), "=&a" (result), "=&r" (tmp) |
| 148 | : "i" (RWSEM_ACTIVE_READ_BIAS) |
| 149 | : "memory", "cc"); |
| 150 | return result >= 0 ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | /* |
| 154 | * lock for writing |
| 155 | */ |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 156 | static inline void __down_write_nested(struct rw_semaphore *sem, int subclass) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | { |
Linus Torvalds | 5d0b723 | 2010-01-12 17:57:35 -0800 | [diff] [blame] | 158 | rwsem_count_t tmp; |
Joe Perches | 6e5609a | 2008-03-23 01:03:21 -0700 | [diff] [blame] | 159 | asm volatile("# beginning down_write\n\t" |
Linus Torvalds | 59c33fa | 2010-01-12 16:21:09 -0800 | [diff] [blame] | 160 | LOCK_PREFIX " xadd %1,(%2)\n\t" |
Michel Lespinasse | b4bcb4c | 2010-07-20 15:19:45 -0700 | [diff] [blame] | 161 | /* adds 0xffff0001, returns the old value */ |
Linus Torvalds | 59c33fa | 2010-01-12 16:21:09 -0800 | [diff] [blame] | 162 | " test %1,%1\n\t" |
Joe Perches | 6e5609a | 2008-03-23 01:03:21 -0700 | [diff] [blame] | 163 | /* was the count 0 before? */ |
| 164 | " jz 1f\n" |
| 165 | " call call_rwsem_down_write_failed\n" |
| 166 | "1:\n" |
| 167 | "# ending down_write" |
| 168 | : "+m" (sem->count), "=d" (tmp) |
Michel Lespinasse | b4bcb4c | 2010-07-20 15:19:45 -0700 | [diff] [blame] | 169 | : "a" (sem), "1" (RWSEM_ACTIVE_WRITE_BIAS) |
Joe Perches | 6e5609a | 2008-03-23 01:03:21 -0700 | [diff] [blame] | 170 | : "memory", "cc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 173 | static inline void __down_write(struct rw_semaphore *sem) |
| 174 | { |
| 175 | __down_write_nested(sem, 0); |
| 176 | } |
| 177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | /* |
| 179 | * trylock for writing -- returns 1 if successful, 0 if contention |
| 180 | */ |
| 181 | static inline int __down_write_trylock(struct rw_semaphore *sem) |
| 182 | { |
Linus Torvalds | 5d0b723 | 2010-01-12 17:57:35 -0800 | [diff] [blame] | 183 | rwsem_count_t ret = cmpxchg(&sem->count, |
| 184 | RWSEM_UNLOCKED_VALUE, |
| 185 | RWSEM_ACTIVE_WRITE_BIAS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | if (ret == RWSEM_UNLOCKED_VALUE) |
| 187 | return 1; |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * unlock after reading |
| 193 | */ |
| 194 | static inline void __up_read(struct rw_semaphore *sem) |
| 195 | { |
Michel Lespinasse | b4bcb4c | 2010-07-20 15:19:45 -0700 | [diff] [blame] | 196 | rwsem_count_t tmp; |
Joe Perches | 6e5609a | 2008-03-23 01:03:21 -0700 | [diff] [blame] | 197 | asm volatile("# beginning __up_read\n\t" |
Linus Torvalds | 59c33fa | 2010-01-12 16:21:09 -0800 | [diff] [blame] | 198 | LOCK_PREFIX " xadd %1,(%2)\n\t" |
Joe Perches | 6e5609a | 2008-03-23 01:03:21 -0700 | [diff] [blame] | 199 | /* subtracts 1, returns the old value */ |
| 200 | " jns 1f\n\t" |
Michel Lespinasse | b4bcb4c | 2010-07-20 15:19:45 -0700 | [diff] [blame] | 201 | " call call_rwsem_wake\n" /* expects old value in %edx */ |
Joe Perches | 6e5609a | 2008-03-23 01:03:21 -0700 | [diff] [blame] | 202 | "1:\n" |
| 203 | "# ending __up_read\n" |
| 204 | : "+m" (sem->count), "=d" (tmp) |
Michel Lespinasse | b4bcb4c | 2010-07-20 15:19:45 -0700 | [diff] [blame] | 205 | : "a" (sem), "1" (-RWSEM_ACTIVE_READ_BIAS) |
Joe Perches | 6e5609a | 2008-03-23 01:03:21 -0700 | [diff] [blame] | 206 | : "memory", "cc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /* |
| 210 | * unlock after writing |
| 211 | */ |
| 212 | static inline void __up_write(struct rw_semaphore *sem) |
| 213 | { |
Linus Torvalds | 5d0b723 | 2010-01-12 17:57:35 -0800 | [diff] [blame] | 214 | rwsem_count_t tmp; |
Joe Perches | 6e5609a | 2008-03-23 01:03:21 -0700 | [diff] [blame] | 215 | asm volatile("# beginning __up_write\n\t" |
Linus Torvalds | 59c33fa | 2010-01-12 16:21:09 -0800 | [diff] [blame] | 216 | LOCK_PREFIX " xadd %1,(%2)\n\t" |
Michel Lespinasse | a751bd8 | 2010-07-20 15:19:45 -0700 | [diff] [blame] | 217 | /* subtracts 0xffff0001, returns the old value */ |
| 218 | " jns 1f\n\t" |
Michel Lespinasse | b4bcb4c | 2010-07-20 15:19:45 -0700 | [diff] [blame] | 219 | " call call_rwsem_wake\n" /* expects old value in %edx */ |
Joe Perches | 6e5609a | 2008-03-23 01:03:21 -0700 | [diff] [blame] | 220 | "1:\n\t" |
| 221 | "# ending __up_write\n" |
Linus Torvalds | 59c33fa | 2010-01-12 16:21:09 -0800 | [diff] [blame] | 222 | : "+m" (sem->count), "=d" (tmp) |
| 223 | : "a" (sem), "1" (-RWSEM_ACTIVE_WRITE_BIAS) |
| 224 | : "memory", "cc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /* |
| 228 | * downgrade write lock to read lock |
| 229 | */ |
| 230 | static inline void __downgrade_write(struct rw_semaphore *sem) |
| 231 | { |
H. Peter Anvin | 1838ef1 | 2010-01-18 14:00:34 -0800 | [diff] [blame] | 232 | asm volatile("# beginning __downgrade_write\n\t" |
| 233 | LOCK_PREFIX _ASM_ADD "%2,(%1)\n\t" |
Avi Kivity | 0d1622d | 2010-02-13 10:33:12 +0200 | [diff] [blame] | 234 | /* |
| 235 | * transitions 0xZZZZ0001 -> 0xYYYY0001 (i386) |
| 236 | * 0xZZZZZZZZ00000001 -> 0xYYYYYYYY00000001 (x86_64) |
| 237 | */ |
Joe Perches | 6e5609a | 2008-03-23 01:03:21 -0700 | [diff] [blame] | 238 | " jns 1f\n\t" |
| 239 | " call call_rwsem_downgrade_wake\n" |
| 240 | "1:\n\t" |
| 241 | "# ending __downgrade_write\n" |
| 242 | : "+m" (sem->count) |
Avi Kivity | 0d1622d | 2010-02-13 10:33:12 +0200 | [diff] [blame] | 243 | : "a" (sem), "er" (-RWSEM_WAITING_BIAS) |
Joe Perches | 6e5609a | 2008-03-23 01:03:21 -0700 | [diff] [blame] | 244 | : "memory", "cc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /* |
| 248 | * implement atomic add functionality |
| 249 | */ |
H. Peter Anvin | 1838ef1 | 2010-01-18 14:00:34 -0800 | [diff] [blame] | 250 | static inline void rwsem_atomic_add(rwsem_count_t delta, |
| 251 | struct rw_semaphore *sem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | { |
H. Peter Anvin | 1838ef1 | 2010-01-18 14:00:34 -0800 | [diff] [blame] | 253 | asm volatile(LOCK_PREFIX _ASM_ADD "%1,%0" |
Joe Perches | 6e5609a | 2008-03-23 01:03:21 -0700 | [diff] [blame] | 254 | : "+m" (sem->count) |
H. Peter Anvin | 1838ef1 | 2010-01-18 14:00:34 -0800 | [diff] [blame] | 255 | : "er" (delta)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /* |
| 259 | * implement exchange and add functionality |
| 260 | */ |
H. Peter Anvin | 1838ef1 | 2010-01-18 14:00:34 -0800 | [diff] [blame] | 261 | static inline rwsem_count_t rwsem_atomic_update(rwsem_count_t delta, |
| 262 | struct rw_semaphore *sem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | { |
Linus Torvalds | 5d0b723 | 2010-01-12 17:57:35 -0800 | [diff] [blame] | 264 | rwsem_count_t tmp = delta; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
Joe Perches | 6e5609a | 2008-03-23 01:03:21 -0700 | [diff] [blame] | 266 | asm volatile(LOCK_PREFIX "xadd %0,%1" |
| 267 | : "+r" (tmp), "+m" (sem->count) |
| 268 | : : "memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | |
Joe Perches | 6e5609a | 2008-03-23 01:03:21 -0700 | [diff] [blame] | 270 | return tmp + delta; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Rik Van Riel | eb92f4e | 2005-10-29 18:15:44 -0700 | [diff] [blame] | 273 | static inline int rwsem_is_locked(struct rw_semaphore *sem) |
| 274 | { |
| 275 | return (sem->count != 0); |
| 276 | } |
| 277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | #endif /* __KERNEL__ */ |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 279 | #endif /* _ASM_X86_RWSEM_H */ |