blob: 4c25cf6caefa1b64e732a6c1d5d7a6636ebd6a62 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/* rwsem.h: R/W semaphores implemented using XADD/CMPXCHG for i486+
3 *
4 * Written by David Howells (dhowells@redhat.com).
5 *
Dave Jones99122a32008-01-30 13:30:28 +01006 * Derived from asm-x86/semaphore.h
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 *
9 * The MSW of the count is the negated number of active writers and waiting
10 * lockers, and the LSW is the total number of active locks
11 *
12 * The lock count is initialized to 0 (no active and no waiting lockers).
13 *
14 * When a writer subtracts WRITE_BIAS, it'll get 0xffff0001 for the case of an
15 * uncontended lock. This can be determined because XADD returns the old value.
16 * Readers increment by 1 and see a positive value when uncontended, negative
17 * if there are writers (and maybe) readers waiting (in which case it goes to
18 * sleep).
19 *
20 * The value of WAITING_BIAS supports up to 32766 waiting processes. This can
21 * be extended to 65534 by manually checking the whole MSW rather than relying
22 * on the S flag.
23 *
24 * The value of ACTIVE_BIAS supports up to 65535 active processes.
25 *
26 * This should be totally fair - if anything is waiting, a process that wants a
27 * lock will go to the back of the queue. When the currently active lock is
28 * released, if there's a writer at the front of the queue, then that and only
Adam Buchbinder6a6256f2016-02-23 15:34:30 -080029 * that will be woken up; if there's a bunch of consecutive readers at the
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * front, then they'll all be woken up, but no other readers will be.
31 */
32
H. Peter Anvin1965aae2008-10-22 22:26:29 -070033#ifndef _ASM_X86_RWSEM_H
34#define _ASM_X86_RWSEM_H
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#ifndef _LINUX_RWSEM_H
37#error "please don't include asm/rwsem.h directly, use linux/rwsem.h instead"
38#endif
39
40#ifdef __KERNEL__
H. Peter Anvin1838ef12010-01-18 14:00:34 -080041#include <asm/asm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
H. Peter Anvin1838ef12010-01-18 14:00:34 -080044 * The bias values and the counter type limits the number of
45 * potential readers/writers to 32767 for 32 bits and 2147483647
46 * for 64 bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 */
Joe Perches6e5609a2008-03-23 01:03:21 -070048
H. Peter Anvin1838ef12010-01-18 14:00:34 -080049#ifdef CONFIG_X86_64
50# define RWSEM_ACTIVE_MASK 0xffffffffL
51#else
52# define RWSEM_ACTIVE_MASK 0x0000ffffL
53#endif
54
55#define RWSEM_UNLOCKED_VALUE 0x00000000L
56#define RWSEM_ACTIVE_BIAS 0x00000001L
57#define RWSEM_WAITING_BIAS (-RWSEM_ACTIVE_MASK-1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
59#define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
Joe Perches6e5609a2008-03-23 01:03:21 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/*
62 * lock for reading
63 */
Kirill Tkhai19c60922017-09-29 19:06:28 +030064#define ____down_read(sem, slow_path) \
65({ \
66 struct rw_semaphore* ret; \
67 asm volatile("# beginning down_read\n\t" \
68 LOCK_PREFIX _ASM_INC "(%[sem])\n\t" \
69 /* adds 0x00000001 */ \
70 " jns 1f\n" \
71 " call " slow_path "\n" \
72 "1:\n\t" \
73 "# ending down_read\n\t" \
74 : "+m" (sem->count), "=a" (ret), \
75 ASM_CALL_CONSTRAINT \
76 : [sem] "a" (sem) \
77 : "memory", "cc"); \
78 ret; \
79})
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static inline void __down_read(struct rw_semaphore *sem)
82{
Kirill Tkhai19c60922017-09-29 19:06:28 +030083 ____down_read(sem, "call_rwsem_down_read_failed");
84}
85
86static inline int __down_read_killable(struct rw_semaphore *sem)
87{
88 if (IS_ERR(____down_read(sem, "call_rwsem_down_read_failed_killable")))
89 return -EINTR;
90 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93/*
94 * trylock for reading -- returns 1 if successful, 0 if contention
95 */
H. Peter Anvin117780e2016-06-08 12:38:38 -070096static inline bool __down_read_trylock(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Thomas Gleixnerbde11ef2011-01-26 20:05:53 +000098 long result, tmp;
Joe Perches6e5609a2008-03-23 01:03:21 -070099 asm volatile("# beginning __down_read_trylock\n\t"
Miguel Bernal Marin30c23f22017-09-25 18:03:49 -0500100 " mov %[count],%[result]\n\t"
Joe Perches6e5609a2008-03-23 01:03:21 -0700101 "1:\n\t"
Miguel Bernal Marin30c23f22017-09-25 18:03:49 -0500102 " mov %[result],%[tmp]\n\t"
103 " add %[inc],%[tmp]\n\t"
Joe Perches6e5609a2008-03-23 01:03:21 -0700104 " jle 2f\n\t"
Miguel Bernal Marin30c23f22017-09-25 18:03:49 -0500105 LOCK_PREFIX " cmpxchg %[tmp],%[count]\n\t"
Joe Perches6e5609a2008-03-23 01:03:21 -0700106 " jnz 1b\n\t"
107 "2:\n\t"
108 "# ending __down_read_trylock\n\t"
Miguel Bernal Marin30c23f22017-09-25 18:03:49 -0500109 : [count] "+m" (sem->count), [result] "=&a" (result),
110 [tmp] "=&r" (tmp)
111 : [inc] "i" (RWSEM_ACTIVE_READ_BIAS)
Joe Perches6e5609a2008-03-23 01:03:21 -0700112 : "memory", "cc");
H. Peter Anvin117780e2016-06-08 12:38:38 -0700113 return result >= 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
116/*
117 * lock for writing
118 */
Michal Hocko664b4e22016-04-07 17:12:30 +0200119#define ____down_write(sem, slow_path) \
120({ \
121 long tmp; \
Michal Hocko916633a2016-04-07 17:12:31 +0200122 struct rw_semaphore* ret; \
Josh Poimboeuf55a76b52016-10-13 16:26:15 -0500123 \
Michal Hocko664b4e22016-04-07 17:12:30 +0200124 asm volatile("# beginning down_write\n\t" \
Miguel Bernal Marin30c23f22017-09-25 18:03:49 -0500125 LOCK_PREFIX " xadd %[tmp],(%[sem])\n\t" \
Michal Hocko664b4e22016-04-07 17:12:30 +0200126 /* adds 0xffff0001, returns the old value */ \
127 " test " __ASM_SEL(%w1,%k1) "," __ASM_SEL(%w1,%k1) "\n\t" \
128 /* was the active mask 0 before? */\
129 " jz 1f\n" \
130 " call " slow_path "\n" \
131 "1:\n" \
132 "# ending down_write" \
Miguel Bernal Marin30c23f22017-09-25 18:03:49 -0500133 : "+m" (sem->count), [tmp] "=d" (tmp), \
Josh Poimboeuff5caf622017-09-20 16:24:33 -0500134 "=a" (ret), ASM_CALL_CONSTRAINT \
Miguel Bernal Marin30c23f22017-09-25 18:03:49 -0500135 : [sem] "a" (sem), "[tmp]" (RWSEM_ACTIVE_WRITE_BIAS) \
Michal Hocko664b4e22016-04-07 17:12:30 +0200136 : "memory", "cc"); \
137 ret; \
138})
139
Michal Hockof8e04d82016-04-07 17:12:21 +0200140static inline void __down_write(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Michal Hocko664b4e22016-04-07 17:12:30 +0200142 ____down_write(sem, "call_rwsem_down_write_failed");
143}
144
145static inline int __down_write_killable(struct rw_semaphore *sem)
146{
147 if (IS_ERR(____down_write(sem, "call_rwsem_down_write_failed_killable")))
148 return -EINTR;
149
150 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153/*
154 * trylock for writing -- returns 1 if successful, 0 if contention
155 */
H. Peter Anvin117780e2016-06-08 12:38:38 -0700156static inline bool __down_write_trylock(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
H. Peter Anvin117780e2016-06-08 12:38:38 -0700158 bool result;
159 long tmp0, tmp1;
Michel Lespinassea31a3692013-05-07 06:46:01 -0700160 asm volatile("# beginning __down_write_trylock\n\t"
Miguel Bernal Marin30c23f22017-09-25 18:03:49 -0500161 " mov %[count],%[tmp0]\n\t"
Michel Lespinassea31a3692013-05-07 06:46:01 -0700162 "1:\n\t"
163 " test " __ASM_SEL(%w1,%k1) "," __ASM_SEL(%w1,%k1) "\n\t"
164 /* was the active mask 0 before? */
165 " jnz 2f\n\t"
Miguel Bernal Marin30c23f22017-09-25 18:03:49 -0500166 " mov %[tmp0],%[tmp1]\n\t"
167 " add %[inc],%[tmp1]\n\t"
168 LOCK_PREFIX " cmpxchg %[tmp1],%[count]\n\t"
Michel Lespinassea31a3692013-05-07 06:46:01 -0700169 " jnz 1b\n\t"
170 "2:\n\t"
H. Peter Anvin35ccfb72016-06-08 12:38:44 -0700171 CC_SET(e)
Michel Lespinassea31a3692013-05-07 06:46:01 -0700172 "# ending __down_write_trylock\n\t"
Miguel Bernal Marin30c23f22017-09-25 18:03:49 -0500173 : [count] "+m" (sem->count), [tmp0] "=&a" (tmp0),
174 [tmp1] "=&r" (tmp1), CC_OUT(e) (result)
175 : [inc] "er" (RWSEM_ACTIVE_WRITE_BIAS)
Jan Beulichc9074202016-09-19 07:27:08 -0600176 : "memory");
Michel Lespinassea31a3692013-05-07 06:46:01 -0700177 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
180/*
181 * unlock after reading
182 */
183static inline void __up_read(struct rw_semaphore *sem)
184{
Thomas Gleixnerbde11ef2011-01-26 20:05:53 +0000185 long tmp;
Joe Perches6e5609a2008-03-23 01:03:21 -0700186 asm volatile("# beginning __up_read\n\t"
Miguel Bernal Marin30c23f22017-09-25 18:03:49 -0500187 LOCK_PREFIX " xadd %[tmp],(%[sem])\n\t"
Joe Perches6e5609a2008-03-23 01:03:21 -0700188 /* subtracts 1, returns the old value */
189 " jns 1f\n\t"
Michel Lespinasseb4bcb4c2010-07-20 15:19:45 -0700190 " call call_rwsem_wake\n" /* expects old value in %edx */
Joe Perches6e5609a2008-03-23 01:03:21 -0700191 "1:\n"
192 "# ending __up_read\n"
Miguel Bernal Marin30c23f22017-09-25 18:03:49 -0500193 : "+m" (sem->count), [tmp] "=d" (tmp)
194 : [sem] "a" (sem), "[tmp]" (-RWSEM_ACTIVE_READ_BIAS)
Joe Perches6e5609a2008-03-23 01:03:21 -0700195 : "memory", "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
198/*
199 * unlock after writing
200 */
201static inline void __up_write(struct rw_semaphore *sem)
202{
Thomas Gleixnerbde11ef2011-01-26 20:05:53 +0000203 long tmp;
Joe Perches6e5609a2008-03-23 01:03:21 -0700204 asm volatile("# beginning __up_write\n\t"
Miguel Bernal Marin30c23f22017-09-25 18:03:49 -0500205 LOCK_PREFIX " xadd %[tmp],(%[sem])\n\t"
Michel Lespinassea751bd852010-07-20 15:19:45 -0700206 /* subtracts 0xffff0001, returns the old value */
207 " jns 1f\n\t"
Michel Lespinasseb4bcb4c2010-07-20 15:19:45 -0700208 " call call_rwsem_wake\n" /* expects old value in %edx */
Joe Perches6e5609a2008-03-23 01:03:21 -0700209 "1:\n\t"
210 "# ending __up_write\n"
Miguel Bernal Marin30c23f22017-09-25 18:03:49 -0500211 : "+m" (sem->count), [tmp] "=d" (tmp)
212 : [sem] "a" (sem), "[tmp]" (-RWSEM_ACTIVE_WRITE_BIAS)
Linus Torvalds59c33fa2010-01-12 16:21:09 -0800213 : "memory", "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
216/*
217 * downgrade write lock to read lock
218 */
219static inline void __downgrade_write(struct rw_semaphore *sem)
220{
H. Peter Anvin1838ef12010-01-18 14:00:34 -0800221 asm volatile("# beginning __downgrade_write\n\t"
Miguel Bernal Marin30c23f22017-09-25 18:03:49 -0500222 LOCK_PREFIX _ASM_ADD "%[inc],(%[sem])\n\t"
Avi Kivity0d1622d2010-02-13 10:33:12 +0200223 /*
224 * transitions 0xZZZZ0001 -> 0xYYYY0001 (i386)
225 * 0xZZZZZZZZ00000001 -> 0xYYYYYYYY00000001 (x86_64)
226 */
Joe Perches6e5609a2008-03-23 01:03:21 -0700227 " jns 1f\n\t"
228 " call call_rwsem_downgrade_wake\n"
229 "1:\n\t"
230 "# ending __downgrade_write\n"
231 : "+m" (sem->count)
Miguel Bernal Marin30c23f22017-09-25 18:03:49 -0500232 : [sem] "a" (sem), [inc] "er" (-RWSEM_WAITING_BIAS)
Joe Perches6e5609a2008-03-23 01:03:21 -0700233 : "memory", "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236#endif /* __KERNEL__ */
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700237#endif /* _ASM_X86_RWSEM_H */