blob: 606ede126972e568b992268da3df6f64b7e98480 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* rwsem.h: R/W semaphores implemented using XADD/CMPXCHG for i486+
2 *
3 * Written by David Howells (dhowells@redhat.com).
4 *
Dave Jones99122a32008-01-30 13:30:28 +01005 * Derived from asm-x86/semaphore.h
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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 Anvin1965aae2008-10-22 22:26:29 -070032#ifndef _ASM_X86_RWSEM_H
33#define _ASM_X86_RWSEM_H
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
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 Molnar4ea21762006-07-03 00:24:53 -070043#include <linux/lockdep.h>
H. Peter Anvin1838ef12010-01-18 14:00:34 -080044#include <asm/asm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46struct rwsem_waiter;
47
Ingo Molnard50efc62008-01-30 13:33:00 +010048extern asmregparm struct rw_semaphore *
49 rwsem_down_read_failed(struct rw_semaphore *sem);
50extern asmregparm struct rw_semaphore *
51 rwsem_down_write_failed(struct rw_semaphore *sem);
52extern asmregparm struct rw_semaphore *
53 rwsem_wake(struct rw_semaphore *);
54extern asmregparm struct rw_semaphore *
55 rwsem_downgrade_wake(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/*
58 * the semaphore definition
Linus Torvalds5d0b7232010-01-12 17:57:35 -080059 *
H. Peter Anvin1838ef12010-01-18 14:00:34 -080060 * 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 Torvalds1da177e2005-04-16 15:20:36 -070063 */
Joe Perches6e5609a2008-03-23 01:03:21 -070064
H. Peter Anvin1838ef12010-01-18 14:00:34 -080065#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 Torvalds1da177e2005-04-16 15:20:36 -070074#define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
75#define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
Joe Perches6e5609a2008-03-23 01:03:21 -070076
H. Peter Anvin1838ef12010-01-18 14:00:34 -080077typedef signed long rwsem_count_t;
Linus Torvalds5d0b7232010-01-12 17:57:35 -080078
Joe Perches6e5609a2008-03-23 01:03:21 -070079struct rw_semaphore {
Linus Torvalds5d0b7232010-01-12 17:57:35 -080080 rwsem_count_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 spinlock_t wait_lock;
82 struct list_head wait_list;
Ingo Molnar4ea21762006-07-03 00:24:53 -070083#ifdef CONFIG_DEBUG_LOCK_ALLOC
84 struct lockdep_map dep_map;
85#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070086};
87
Ingo Molnar4ea21762006-07-03 00:24:53 -070088#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 Perches6e5609a2008-03-23 01:03:21 -070095#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 Torvalds1da177e2005-04-16 15:20:36 -0700100
Joe Perches6e5609a2008-03-23 01:03:21 -0700101#define DECLARE_RWSEM(name) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
103
Ingo Molnar4ea21762006-07-03 00:24:53 -0700104extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
105 struct lock_class_key *key);
106
107#define init_rwsem(sem) \
108do { \
109 static struct lock_class_key __key; \
110 \
111 __init_rwsem((sem), #sem, &__key); \
112} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114/*
115 * lock for reading
116 */
117static inline void __down_read(struct rw_semaphore *sem)
118{
Joe Perches6e5609a2008-03-23 01:03:21 -0700119 asm volatile("# beginning down_read\n\t"
H. Peter Anvin1838ef12010-01-18 14:00:34 -0800120 LOCK_PREFIX _ASM_INC "(%1)\n\t"
Joe Perches6e5609a2008-03-23 01:03:21 -0700121 /* adds 0x00000001, returns the old value */
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 Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
131/*
132 * trylock for reading -- returns 1 if successful, 0 if contention
133 */
134static inline int __down_read_trylock(struct rw_semaphore *sem)
135{
Linus Torvalds5d0b7232010-01-12 17:57:35 -0800136 rwsem_count_t result, tmp;
Joe Perches6e5609a2008-03-23 01:03:21 -0700137 asm volatile("# beginning __down_read_trylock\n\t"
Linus Torvalds59c33fa2010-01-12 16:21:09 -0800138 " mov %0,%1\n\t"
Joe Perches6e5609a2008-03-23 01:03:21 -0700139 "1:\n\t"
Linus Torvalds59c33fa2010-01-12 16:21:09 -0800140 " mov %1,%2\n\t"
141 " add %3,%2\n\t"
Joe Perches6e5609a2008-03-23 01:03:21 -0700142 " jle 2f\n\t"
Linus Torvalds59c33fa2010-01-12 16:21:09 -0800143 LOCK_PREFIX " cmpxchg %2,%0\n\t"
Joe Perches6e5609a2008-03-23 01:03:21 -0700144 " 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 Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153/*
154 * lock for writing
155 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700156static inline void __down_write_nested(struct rw_semaphore *sem, int subclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Linus Torvalds5d0b7232010-01-12 17:57:35 -0800158 rwsem_count_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 tmp = RWSEM_ACTIVE_WRITE_BIAS;
Joe Perches6e5609a2008-03-23 01:03:21 -0700161 asm volatile("# beginning down_write\n\t"
Linus Torvalds59c33fa2010-01-12 16:21:09 -0800162 LOCK_PREFIX " xadd %1,(%2)\n\t"
Joe Perches6e5609a2008-03-23 01:03:21 -0700163 /* subtract 0x0000ffff, returns the old value */
Linus Torvalds59c33fa2010-01-12 16:21:09 -0800164 " test %1,%1\n\t"
Joe Perches6e5609a2008-03-23 01:03:21 -0700165 /* was the count 0 before? */
166 " jz 1f\n"
167 " call call_rwsem_down_write_failed\n"
168 "1:\n"
169 "# ending down_write"
170 : "+m" (sem->count), "=d" (tmp)
171 : "a" (sem), "1" (tmp)
172 : "memory", "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Ingo Molnar4ea21762006-07-03 00:24:53 -0700175static inline void __down_write(struct rw_semaphore *sem)
176{
177 __down_write_nested(sem, 0);
178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180/*
181 * trylock for writing -- returns 1 if successful, 0 if contention
182 */
183static inline int __down_write_trylock(struct rw_semaphore *sem)
184{
Linus Torvalds5d0b7232010-01-12 17:57:35 -0800185 rwsem_count_t ret = cmpxchg(&sem->count,
186 RWSEM_UNLOCKED_VALUE,
187 RWSEM_ACTIVE_WRITE_BIAS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 if (ret == RWSEM_UNLOCKED_VALUE)
189 return 1;
190 return 0;
191}
192
193/*
194 * unlock after reading
195 */
196static inline void __up_read(struct rw_semaphore *sem)
197{
Linus Torvalds5d0b7232010-01-12 17:57:35 -0800198 rwsem_count_t tmp = -RWSEM_ACTIVE_READ_BIAS;
Joe Perches6e5609a2008-03-23 01:03:21 -0700199 asm volatile("# beginning __up_read\n\t"
Linus Torvalds59c33fa2010-01-12 16:21:09 -0800200 LOCK_PREFIX " xadd %1,(%2)\n\t"
Joe Perches6e5609a2008-03-23 01:03:21 -0700201 /* subtracts 1, returns the old value */
202 " jns 1f\n\t"
203 " call call_rwsem_wake\n"
204 "1:\n"
205 "# ending __up_read\n"
206 : "+m" (sem->count), "=d" (tmp)
207 : "a" (sem), "1" (tmp)
208 : "memory", "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
211/*
212 * unlock after writing
213 */
214static inline void __up_write(struct rw_semaphore *sem)
215{
Linus Torvalds5d0b7232010-01-12 17:57:35 -0800216 rwsem_count_t tmp;
Joe Perches6e5609a2008-03-23 01:03:21 -0700217 asm volatile("# beginning __up_write\n\t"
Linus Torvalds59c33fa2010-01-12 16:21:09 -0800218 LOCK_PREFIX " xadd %1,(%2)\n\t"
Joe Perches6e5609a2008-03-23 01:03:21 -0700219 /* tries to transition
220 0xffff0001 -> 0x00000000 */
221 " jz 1f\n"
222 " call call_rwsem_wake\n"
223 "1:\n\t"
224 "# ending __up_write\n"
Linus Torvalds59c33fa2010-01-12 16:21:09 -0800225 : "+m" (sem->count), "=d" (tmp)
226 : "a" (sem), "1" (-RWSEM_ACTIVE_WRITE_BIAS)
227 : "memory", "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
230/*
231 * downgrade write lock to read lock
232 */
233static inline void __downgrade_write(struct rw_semaphore *sem)
234{
H. Peter Anvin1838ef12010-01-18 14:00:34 -0800235 asm volatile("# beginning __downgrade_write\n\t"
236 LOCK_PREFIX _ASM_ADD "%2,(%1)\n\t"
Avi Kivity0d1622d2010-02-13 10:33:12 +0200237 /*
238 * transitions 0xZZZZ0001 -> 0xYYYY0001 (i386)
239 * 0xZZZZZZZZ00000001 -> 0xYYYYYYYY00000001 (x86_64)
240 */
Joe Perches6e5609a2008-03-23 01:03:21 -0700241 " jns 1f\n\t"
242 " call call_rwsem_downgrade_wake\n"
243 "1:\n\t"
244 "# ending __downgrade_write\n"
245 : "+m" (sem->count)
Avi Kivity0d1622d2010-02-13 10:33:12 +0200246 : "a" (sem), "er" (-RWSEM_WAITING_BIAS)
Joe Perches6e5609a2008-03-23 01:03:21 -0700247 : "memory", "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
250/*
251 * implement atomic add functionality
252 */
H. Peter Anvin1838ef12010-01-18 14:00:34 -0800253static inline void rwsem_atomic_add(rwsem_count_t delta,
254 struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
H. Peter Anvin1838ef12010-01-18 14:00:34 -0800256 asm volatile(LOCK_PREFIX _ASM_ADD "%1,%0"
Joe Perches6e5609a2008-03-23 01:03:21 -0700257 : "+m" (sem->count)
H. Peter Anvin1838ef12010-01-18 14:00:34 -0800258 : "er" (delta));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
261/*
262 * implement exchange and add functionality
263 */
H. Peter Anvin1838ef12010-01-18 14:00:34 -0800264static inline rwsem_count_t rwsem_atomic_update(rwsem_count_t delta,
265 struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Linus Torvalds5d0b7232010-01-12 17:57:35 -0800267 rwsem_count_t tmp = delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Joe Perches6e5609a2008-03-23 01:03:21 -0700269 asm volatile(LOCK_PREFIX "xadd %0,%1"
270 : "+r" (tmp), "+m" (sem->count)
271 : : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Joe Perches6e5609a2008-03-23 01:03:21 -0700273 return tmp + delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
Rik Van Rieleb92f4e2005-10-29 18:15:44 -0700276static inline int rwsem_is_locked(struct rw_semaphore *sem)
277{
278 return (sem->count != 0);
279}
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281#endif /* __KERNEL__ */
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700282#endif /* _ASM_X86_RWSEM_H */