1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* rwsem.h: R/W semaphores implemented using XADD/CMPXCHG for i486+
4 * Written by David Howells (dhowells@redhat.com).
6 * Derived from asm-x86/semaphore.h
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
12 * The lock count is initialized to 0 (no active and no waiting lockers).
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
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
24 * The value of ACTIVE_BIAS supports up to 65535 active processes.
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
29 * that will be woken up; if there's a bunch of consecutive readers at the
30 * front, then they'll all be woken up, but no other readers will be.
33 #ifndef _ASM_X86_RWSEM_H
34 #define _ASM_X86_RWSEM_H
36 #ifndef _LINUX_RWSEM_H
37 #error "please don't include asm/rwsem.h directly, use linux/rwsem.h instead"
44 * The bias values and the counter type limits the number of
45 * potential readers/writers to 32767 for 32 bits and 2147483647
50 # define RWSEM_ACTIVE_MASK 0xffffffffL
52 # define RWSEM_ACTIVE_MASK 0x0000ffffL
55 #define RWSEM_UNLOCKED_VALUE 0x00000000L
56 #define RWSEM_ACTIVE_BIAS 0x00000001L
57 #define RWSEM_WAITING_BIAS (-RWSEM_ACTIVE_MASK-1)
58 #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
59 #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
64 #define ____down_read(sem, slow_path) \
66 struct rw_semaphore* ret; \
67 asm volatile("# beginning down_read\n\t" \
68 LOCK_PREFIX _ASM_INC "(%[sem])\n\t" \
69 /* adds 0x00000001 */ \
71 " call " slow_path "\n" \
73 "# ending down_read\n\t" \
74 : "+m" (sem->count), "=a" (ret), \
81 static inline void __down_read(struct rw_semaphore
*sem
)
83 ____down_read(sem
, "call_rwsem_down_read_failed");
86 static inline int __down_read_killable(struct rw_semaphore
*sem
)
88 if (IS_ERR(____down_read(sem
, "call_rwsem_down_read_failed_killable")))
94 * trylock for reading -- returns 1 if successful, 0 if contention
96 static inline bool __down_read_trylock(struct rw_semaphore
*sem
)
99 asm volatile("# beginning __down_read_trylock\n\t"
100 " mov %[count],%[result]\n\t"
102 " mov %[result],%[tmp]\n\t"
103 " add %[inc],%[tmp]\n\t"
105 LOCK_PREFIX
" cmpxchg %[tmp],%[count]\n\t"
108 "# ending __down_read_trylock\n\t"
109 : [count
] "+m" (sem
->count
), [result
] "=&a" (result
),
111 : [inc
] "i" (RWSEM_ACTIVE_READ_BIAS
)
119 #define ____down_write(sem, slow_path) \
122 struct rw_semaphore* ret; \
124 asm volatile("# beginning down_write\n\t" \
125 LOCK_PREFIX " xadd %[tmp],(%[sem])\n\t" \
126 /* 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? */\
130 " call " slow_path "\n" \
132 "# ending down_write" \
133 : "+m" (sem->count), [tmp] "=d" (tmp), \
134 "=a" (ret), ASM_CALL_CONSTRAINT \
135 : [sem] "a" (sem), "[tmp]" (RWSEM_ACTIVE_WRITE_BIAS) \
140 static inline void __down_write(struct rw_semaphore
*sem
)
142 ____down_write(sem
, "call_rwsem_down_write_failed");
145 static inline int __down_write_killable(struct rw_semaphore
*sem
)
147 if (IS_ERR(____down_write(sem
, "call_rwsem_down_write_failed_killable")))
154 * trylock for writing -- returns 1 if successful, 0 if contention
156 static inline bool __down_write_trylock(struct rw_semaphore
*sem
)
160 asm volatile("# beginning __down_write_trylock\n\t"
161 " mov %[count],%[tmp0]\n\t"
163 " test " __ASM_SEL(%w1
,%k1
) "," __ASM_SEL(%w1
,%k1
) "\n\t"
164 /* was the active mask 0 before? */
166 " mov %[tmp0],%[tmp1]\n\t"
167 " add %[inc],%[tmp1]\n\t"
168 LOCK_PREFIX
" cmpxchg %[tmp1],%[count]\n\t"
172 "# ending __down_write_trylock\n\t"
173 : [count
] "+m" (sem
->count
), [tmp0
] "=&a" (tmp0
),
174 [tmp1
] "=&r" (tmp1
), CC_OUT(e
) (result
)
175 : [inc
] "er" (RWSEM_ACTIVE_WRITE_BIAS
)
181 * unlock after reading
183 static inline void __up_read(struct rw_semaphore
*sem
)
186 asm volatile("# beginning __up_read\n\t"
187 LOCK_PREFIX
" xadd %[tmp],(%[sem])\n\t"
188 /* subtracts 1, returns the old value */
190 " call call_rwsem_wake\n" /* expects old value in %edx */
192 "# ending __up_read\n"
193 : "+m" (sem
->count
), [tmp
] "=d" (tmp
)
194 : [sem
] "a" (sem
), "[tmp]" (-RWSEM_ACTIVE_READ_BIAS
)
199 * unlock after writing
201 static inline void __up_write(struct rw_semaphore
*sem
)
204 asm volatile("# beginning __up_write\n\t"
205 LOCK_PREFIX
" xadd %[tmp],(%[sem])\n\t"
206 /* subtracts 0xffff0001, returns the old value */
208 " call call_rwsem_wake\n" /* expects old value in %edx */
210 "# ending __up_write\n"
211 : "+m" (sem
->count
), [tmp
] "=d" (tmp
)
212 : [sem
] "a" (sem
), "[tmp]" (-RWSEM_ACTIVE_WRITE_BIAS
)
217 * downgrade write lock to read lock
219 static inline void __downgrade_write(struct rw_semaphore
*sem
)
221 asm volatile("# beginning __downgrade_write\n\t"
222 LOCK_PREFIX _ASM_ADD
"%[inc],(%[sem])\n\t"
224 * transitions 0xZZZZ0001 -> 0xYYYY0001 (i386)
225 * 0xZZZZZZZZ00000001 -> 0xYYYYYYYY00000001 (x86_64)
228 " call call_rwsem_downgrade_wake\n"
230 "# ending __downgrade_write\n"
232 : [sem
] "a" (sem
), [inc
] "er" (-RWSEM_WAITING_BIAS
)
236 #endif /* __KERNEL__ */
237 #endif /* _ASM_X86_RWSEM_H */