blob: 7116b7931c7b807766fef6d820ec929da43940c7 [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
Adam Buchbinder6a6256f2016-02-23 15:34:30 -080028 * that will be woken up; if there's a bunch of consecutive readers at the
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * 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__
H. Peter Anvin1838ef12010-01-18 14:00:34 -080040#include <asm/asm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/*
H. Peter Anvin1838ef12010-01-18 14:00:34 -080043 * The bias values and the counter type limits the number of
44 * potential readers/writers to 32767 for 32 bits and 2147483647
45 * for 64 bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 */
Joe Perches6e5609a2008-03-23 01:03:21 -070047
H. Peter Anvin1838ef12010-01-18 14:00:34 -080048#ifdef CONFIG_X86_64
49# define RWSEM_ACTIVE_MASK 0xffffffffL
50#else
51# define RWSEM_ACTIVE_MASK 0x0000ffffL
52#endif
53
54#define RWSEM_UNLOCKED_VALUE 0x00000000L
55#define RWSEM_ACTIVE_BIAS 0x00000001L
56#define RWSEM_WAITING_BIAS (-RWSEM_ACTIVE_MASK-1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
58#define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
Joe Perches6e5609a2008-03-23 01:03:21 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/*
61 * lock for reading
62 */
63static inline void __down_read(struct rw_semaphore *sem)
64{
Joe Perches6e5609a2008-03-23 01:03:21 -070065 asm volatile("# beginning down_read\n\t"
H. Peter Anvin1838ef12010-01-18 14:00:34 -080066 LOCK_PREFIX _ASM_INC "(%1)\n\t"
Michel Lespinasseb4bcb4c2010-07-20 15:19:45 -070067 /* adds 0x00000001 */
Joe Perches6e5609a2008-03-23 01:03:21 -070068 " jns 1f\n"
69 " call call_rwsem_down_read_failed\n"
70 "1:\n\t"
71 "# ending down_read\n\t"
72 : "+m" (sem->count)
73 : "a" (sem)
74 : "memory", "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
77/*
78 * trylock for reading -- returns 1 if successful, 0 if contention
79 */
H. Peter Anvin117780e2016-06-08 12:38:38 -070080static inline bool __down_read_trylock(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Thomas Gleixnerbde11ef2011-01-26 20:05:53 +000082 long result, tmp;
Joe Perches6e5609a2008-03-23 01:03:21 -070083 asm volatile("# beginning __down_read_trylock\n\t"
Linus Torvalds59c33fa2010-01-12 16:21:09 -080084 " mov %0,%1\n\t"
Joe Perches6e5609a2008-03-23 01:03:21 -070085 "1:\n\t"
Linus Torvalds59c33fa2010-01-12 16:21:09 -080086 " mov %1,%2\n\t"
87 " add %3,%2\n\t"
Joe Perches6e5609a2008-03-23 01:03:21 -070088 " jle 2f\n\t"
Linus Torvalds59c33fa2010-01-12 16:21:09 -080089 LOCK_PREFIX " cmpxchg %2,%0\n\t"
Joe Perches6e5609a2008-03-23 01:03:21 -070090 " jnz 1b\n\t"
91 "2:\n\t"
92 "# ending __down_read_trylock\n\t"
93 : "+m" (sem->count), "=&a" (result), "=&r" (tmp)
94 : "i" (RWSEM_ACTIVE_READ_BIAS)
95 : "memory", "cc");
H. Peter Anvin117780e2016-06-08 12:38:38 -070096 return result >= 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
99/*
100 * lock for writing
101 */
Michal Hocko664b4e22016-04-07 17:12:30 +0200102#define ____down_write(sem, slow_path) \
103({ \
104 long tmp; \
Michal Hocko916633a2016-04-07 17:12:31 +0200105 struct rw_semaphore* ret; \
Josh Poimboeuf55a76b52016-10-13 16:26:15 -0500106 \
Michal Hocko664b4e22016-04-07 17:12:30 +0200107 asm volatile("# beginning down_write\n\t" \
Josh Poimboeuf55a76b52016-10-13 16:26:15 -0500108 LOCK_PREFIX " xadd %1,(%4)\n\t" \
Michal Hocko664b4e22016-04-07 17:12:30 +0200109 /* adds 0xffff0001, returns the old value */ \
110 " test " __ASM_SEL(%w1,%k1) "," __ASM_SEL(%w1,%k1) "\n\t" \
111 /* was the active mask 0 before? */\
112 " jz 1f\n" \
113 " call " slow_path "\n" \
114 "1:\n" \
115 "# ending down_write" \
Josh Poimboeufd5ea93e2017-09-20 16:24:33 -0500116 : "+m" (sem->count), "=d" (tmp), \
117 "=a" (ret), ASM_CALL_CONSTRAINT \
Michal Hocko664b4e22016-04-07 17:12:30 +0200118 : "a" (sem), "1" (RWSEM_ACTIVE_WRITE_BIAS) \
119 : "memory", "cc"); \
120 ret; \
121})
122
Michal Hockof8e04d82016-04-07 17:12:21 +0200123static inline void __down_write(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Michal Hocko664b4e22016-04-07 17:12:30 +0200125 ____down_write(sem, "call_rwsem_down_write_failed");
126}
127
128static inline int __down_write_killable(struct rw_semaphore *sem)
129{
130 if (IS_ERR(____down_write(sem, "call_rwsem_down_write_failed_killable")))
131 return -EINTR;
132
133 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
136/*
137 * trylock for writing -- returns 1 if successful, 0 if contention
138 */
H. Peter Anvin117780e2016-06-08 12:38:38 -0700139static inline bool __down_write_trylock(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
H. Peter Anvin117780e2016-06-08 12:38:38 -0700141 bool result;
142 long tmp0, tmp1;
Michel Lespinassea31a3692013-05-07 06:46:01 -0700143 asm volatile("# beginning __down_write_trylock\n\t"
144 " mov %0,%1\n\t"
145 "1:\n\t"
146 " test " __ASM_SEL(%w1,%k1) "," __ASM_SEL(%w1,%k1) "\n\t"
147 /* was the active mask 0 before? */
148 " jnz 2f\n\t"
149 " mov %1,%2\n\t"
H. Peter Anvin117780e2016-06-08 12:38:38 -0700150 " add %4,%2\n\t"
Michel Lespinassea31a3692013-05-07 06:46:01 -0700151 LOCK_PREFIX " cmpxchg %2,%0\n\t"
152 " jnz 1b\n\t"
153 "2:\n\t"
H. Peter Anvin35ccfb72016-06-08 12:38:44 -0700154 CC_SET(e)
Michel Lespinassea31a3692013-05-07 06:46:01 -0700155 "# ending __down_write_trylock\n\t"
H. Peter Anvin117780e2016-06-08 12:38:38 -0700156 : "+m" (sem->count), "=&a" (tmp0), "=&r" (tmp1),
H. Peter Anvin35ccfb72016-06-08 12:38:44 -0700157 CC_OUT(e) (result)
Michel Lespinassea31a3692013-05-07 06:46:01 -0700158 : "er" (RWSEM_ACTIVE_WRITE_BIAS)
Jan Beulichc9074202016-09-19 07:27:08 -0600159 : "memory");
Michel Lespinassea31a3692013-05-07 06:46:01 -0700160 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
163/*
164 * unlock after reading
165 */
166static inline void __up_read(struct rw_semaphore *sem)
167{
Thomas Gleixnerbde11ef2011-01-26 20:05:53 +0000168 long tmp;
Joe Perches6e5609a2008-03-23 01:03:21 -0700169 asm volatile("# beginning __up_read\n\t"
Linus Torvalds59c33fa2010-01-12 16:21:09 -0800170 LOCK_PREFIX " xadd %1,(%2)\n\t"
Joe Perches6e5609a2008-03-23 01:03:21 -0700171 /* subtracts 1, returns the old value */
172 " jns 1f\n\t"
Michel Lespinasseb4bcb4c2010-07-20 15:19:45 -0700173 " call call_rwsem_wake\n" /* expects old value in %edx */
Joe Perches6e5609a2008-03-23 01:03:21 -0700174 "1:\n"
175 "# ending __up_read\n"
176 : "+m" (sem->count), "=d" (tmp)
Michel Lespinasseb4bcb4c2010-07-20 15:19:45 -0700177 : "a" (sem), "1" (-RWSEM_ACTIVE_READ_BIAS)
Joe Perches6e5609a2008-03-23 01:03:21 -0700178 : "memory", "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
181/*
182 * unlock after writing
183 */
184static inline void __up_write(struct rw_semaphore *sem)
185{
Thomas Gleixnerbde11ef2011-01-26 20:05:53 +0000186 long tmp;
Joe Perches6e5609a2008-03-23 01:03:21 -0700187 asm volatile("# beginning __up_write\n\t"
Linus Torvalds59c33fa2010-01-12 16:21:09 -0800188 LOCK_PREFIX " xadd %1,(%2)\n\t"
Michel Lespinassea751bd82010-07-20 15:19:45 -0700189 /* subtracts 0xffff0001, returns the old value */
190 " jns 1f\n\t"
Michel Lespinasseb4bcb4c2010-07-20 15:19:45 -0700191 " call call_rwsem_wake\n" /* expects old value in %edx */
Joe Perches6e5609a2008-03-23 01:03:21 -0700192 "1:\n\t"
193 "# ending __up_write\n"
Linus Torvalds59c33fa2010-01-12 16:21:09 -0800194 : "+m" (sem->count), "=d" (tmp)
195 : "a" (sem), "1" (-RWSEM_ACTIVE_WRITE_BIAS)
196 : "memory", "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
199/*
200 * downgrade write lock to read lock
201 */
202static inline void __downgrade_write(struct rw_semaphore *sem)
203{
H. Peter Anvin1838ef12010-01-18 14:00:34 -0800204 asm volatile("# beginning __downgrade_write\n\t"
205 LOCK_PREFIX _ASM_ADD "%2,(%1)\n\t"
Avi Kivity0d1622d2010-02-13 10:33:12 +0200206 /*
207 * transitions 0xZZZZ0001 -> 0xYYYY0001 (i386)
208 * 0xZZZZZZZZ00000001 -> 0xYYYYYYYY00000001 (x86_64)
209 */
Joe Perches6e5609a2008-03-23 01:03:21 -0700210 " jns 1f\n\t"
211 " call call_rwsem_downgrade_wake\n"
212 "1:\n\t"
213 "# ending __downgrade_write\n"
214 : "+m" (sem->count)
Avi Kivity0d1622d2010-02-13 10:33:12 +0200215 : "a" (sem), "er" (-RWSEM_WAITING_BIAS)
Joe Perches6e5609a2008-03-23 01:03:21 -0700216 : "memory", "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219#endif /* __KERNEL__ */
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700220#endif /* _ASM_X86_RWSEM_H */