xref: /linux/include/asm-generic/qrwlock.h (revision 493e2ba2)
1c942fddfSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
270af2f8aSWaiman Long /*
370af2f8aSWaiman Long  * Queue read/write lock
470af2f8aSWaiman Long  *
5493e2ba2SPalmer Dabbelt  * These use generic atomic and locking routines, but depend on a fair spinlock
6493e2ba2SPalmer Dabbelt  * implementation in order to be fair themselves.  The implementation in
7493e2ba2SPalmer Dabbelt  * asm-generic/spinlock.h meets these requirements.
8493e2ba2SPalmer Dabbelt  *
970af2f8aSWaiman Long  * (C) Copyright 2013-2014 Hewlett-Packard Development Company, L.P.
1070af2f8aSWaiman Long  *
1170af2f8aSWaiman Long  * Authors: Waiman Long <waiman.long@hp.com>
1270af2f8aSWaiman Long  */
1370af2f8aSWaiman Long #ifndef __ASM_GENERIC_QRWLOCK_H
1470af2f8aSWaiman Long #define __ASM_GENERIC_QRWLOCK_H
1570af2f8aSWaiman Long 
1670af2f8aSWaiman Long #include <linux/atomic.h>
1770af2f8aSWaiman Long #include <asm/barrier.h>
1870af2f8aSWaiman Long #include <asm/processor.h>
1970af2f8aSWaiman Long 
2070af2f8aSWaiman Long #include <asm-generic/qrwlock_types.h>
21d8d0da4eSWaiman Long 
22d8d0da4eSWaiman Long /* Must be included from asm/spinlock.h after defining arch_spin_is_locked.  */
2370af2f8aSWaiman Long 
2470af2f8aSWaiman Long /*
252db34e8bSpan xinhui  * Writer states & reader shift and bias.
2670af2f8aSWaiman Long  */
27d1331661SWill Deacon #define	_QW_WAITING	0x100		/* A writer is waiting	   */
28d1331661SWill Deacon #define	_QW_LOCKED	0x0ff		/* A writer holds the lock */
29d1331661SWill Deacon #define	_QW_WMASK	0x1ff		/* Writer mask		   */
30d1331661SWill Deacon #define	_QR_SHIFT	9		/* Reader count shift	   */
3170af2f8aSWaiman Long #define _QR_BIAS	(1U << _QR_SHIFT)
3270af2f8aSWaiman Long 
3370af2f8aSWaiman Long /*
3470af2f8aSWaiman Long  * External function declarations
3570af2f8aSWaiman Long  */
36b519b56eSWill Deacon extern void queued_read_lock_slowpath(struct qrwlock *lock);
37f7d71f20SWaiman Long extern void queued_write_lock_slowpath(struct qrwlock *lock);
3870af2f8aSWaiman Long 
3970af2f8aSWaiman Long /**
40f7d71f20SWaiman Long  * queued_read_trylock - try to acquire read lock of a queued rwlock
4170af2f8aSWaiman Long  * @lock : Pointer to queued rwlock structure
4270af2f8aSWaiman Long  * Return: 1 if lock acquired, 0 if failed
4370af2f8aSWaiman Long  */
queued_read_trylock(struct qrwlock * lock)44f7d71f20SWaiman Long static inline int queued_read_trylock(struct qrwlock *lock)
4570af2f8aSWaiman Long {
46f44ca087SArnd Bergmann 	int cnts;
4770af2f8aSWaiman Long 
4870af2f8aSWaiman Long 	cnts = atomic_read(&lock->cnts);
4970af2f8aSWaiman Long 	if (likely(!(cnts & _QW_WMASK))) {
5077e430e3SWill Deacon 		cnts = (u32)atomic_add_return_acquire(_QR_BIAS, &lock->cnts);
5170af2f8aSWaiman Long 		if (likely(!(cnts & _QW_WMASK)))
5270af2f8aSWaiman Long 			return 1;
5370af2f8aSWaiman Long 		atomic_sub(_QR_BIAS, &lock->cnts);
5470af2f8aSWaiman Long 	}
5570af2f8aSWaiman Long 	return 0;
5670af2f8aSWaiman Long }
5770af2f8aSWaiman Long 
5870af2f8aSWaiman Long /**
59f7d71f20SWaiman Long  * queued_write_trylock - try to acquire write lock of a queued rwlock
6070af2f8aSWaiman Long  * @lock : Pointer to queued rwlock structure
6170af2f8aSWaiman Long  * Return: 1 if lock acquired, 0 if failed
6270af2f8aSWaiman Long  */
queued_write_trylock(struct qrwlock * lock)63f7d71f20SWaiman Long static inline int queued_write_trylock(struct qrwlock *lock)
6470af2f8aSWaiman Long {
65f44ca087SArnd Bergmann 	int cnts;
6670af2f8aSWaiman Long 
6770af2f8aSWaiman Long 	cnts = atomic_read(&lock->cnts);
6870af2f8aSWaiman Long 	if (unlikely(cnts))
6970af2f8aSWaiman Long 		return 0;
7070af2f8aSWaiman Long 
7127df8968SMatthew Wilcox 	return likely(atomic_try_cmpxchg_acquire(&lock->cnts, &cnts,
7227df8968SMatthew Wilcox 				_QW_LOCKED));
7370af2f8aSWaiman Long }
7470af2f8aSWaiman Long /**
75f7d71f20SWaiman Long  * queued_read_lock - acquire read lock of a queued rwlock
7670af2f8aSWaiman Long  * @lock: Pointer to queued rwlock structure
7770af2f8aSWaiman Long  */
queued_read_lock(struct qrwlock * lock)78f7d71f20SWaiman Long static inline void queued_read_lock(struct qrwlock *lock)
7970af2f8aSWaiman Long {
80f44ca087SArnd Bergmann 	int cnts;
8170af2f8aSWaiman Long 
8277e430e3SWill Deacon 	cnts = atomic_add_return_acquire(_QR_BIAS, &lock->cnts);
8370af2f8aSWaiman Long 	if (likely(!(cnts & _QW_WMASK)))
8470af2f8aSWaiman Long 		return;
8570af2f8aSWaiman Long 
8670af2f8aSWaiman Long 	/* The slowpath will decrement the reader count, if necessary. */
87b519b56eSWill Deacon 	queued_read_lock_slowpath(lock);
8870af2f8aSWaiman Long }
8970af2f8aSWaiman Long 
9070af2f8aSWaiman Long /**
91f7d71f20SWaiman Long  * queued_write_lock - acquire write lock of a queued rwlock
9270af2f8aSWaiman Long  * @lock : Pointer to queued rwlock structure
9370af2f8aSWaiman Long  */
queued_write_lock(struct qrwlock * lock)94f7d71f20SWaiman Long static inline void queued_write_lock(struct qrwlock *lock)
9570af2f8aSWaiman Long {
96f44ca087SArnd Bergmann 	int cnts = 0;
9770af2f8aSWaiman Long 	/* Optimize for the unfair lock case where the fair flag is 0. */
9827df8968SMatthew Wilcox 	if (likely(atomic_try_cmpxchg_acquire(&lock->cnts, &cnts, _QW_LOCKED)))
9970af2f8aSWaiman Long 		return;
10070af2f8aSWaiman Long 
101f7d71f20SWaiman Long 	queued_write_lock_slowpath(lock);
10270af2f8aSWaiman Long }
10370af2f8aSWaiman Long 
10470af2f8aSWaiman Long /**
105f7d71f20SWaiman Long  * queued_read_unlock - release read lock of a queued rwlock
10670af2f8aSWaiman Long  * @lock : Pointer to queued rwlock structure
10770af2f8aSWaiman Long  */
queued_read_unlock(struct qrwlock * lock)108f7d71f20SWaiman Long static inline void queued_read_unlock(struct qrwlock *lock)
10970af2f8aSWaiman Long {
11070af2f8aSWaiman Long 	/*
11170af2f8aSWaiman Long 	 * Atomically decrement the reader count
11270af2f8aSWaiman Long 	 */
11377e430e3SWill Deacon 	(void)atomic_sub_return_release(_QR_BIAS, &lock->cnts);
11470af2f8aSWaiman Long }
11570af2f8aSWaiman Long 
11670af2f8aSWaiman Long /**
117f7d71f20SWaiman Long  * queued_write_unlock - release write lock of a queued rwlock
11870af2f8aSWaiman Long  * @lock : Pointer to queued rwlock structure
11970af2f8aSWaiman Long  */
queued_write_unlock(struct qrwlock * lock)120f7d71f20SWaiman Long static inline void queued_write_unlock(struct qrwlock *lock)
12170af2f8aSWaiman Long {
122d1331661SWill Deacon 	smp_store_release(&lock->wlocked, 0);
12370af2f8aSWaiman Long }
12470af2f8aSWaiman Long 
12526128cb6SBen Gardon /**
12626128cb6SBen Gardon  * queued_rwlock_is_contended - check if the lock is contended
12726128cb6SBen Gardon  * @lock : Pointer to queued rwlock structure
12826128cb6SBen Gardon  * Return: 1 if lock contended, 0 otherwise
12926128cb6SBen Gardon  */
queued_rwlock_is_contended(struct qrwlock * lock)13026128cb6SBen Gardon static inline int queued_rwlock_is_contended(struct qrwlock *lock)
13126128cb6SBen Gardon {
13226128cb6SBen Gardon 	return arch_spin_is_locked(&lock->wait_lock);
13326128cb6SBen Gardon }
13426128cb6SBen Gardon 
13570af2f8aSWaiman Long /*
13670af2f8aSWaiman Long  * Remapping rwlock architecture specific functions to the corresponding
13770af2f8aSWaiman Long  * queued rwlock functions.
13870af2f8aSWaiman Long  */
139f7d71f20SWaiman Long #define arch_read_lock(l)		queued_read_lock(l)
140f7d71f20SWaiman Long #define arch_write_lock(l)		queued_write_lock(l)
141f7d71f20SWaiman Long #define arch_read_trylock(l)		queued_read_trylock(l)
142f7d71f20SWaiman Long #define arch_write_trylock(l)		queued_write_trylock(l)
143f7d71f20SWaiman Long #define arch_read_unlock(l)		queued_read_unlock(l)
144f7d71f20SWaiman Long #define arch_write_unlock(l)		queued_write_unlock(l)
14526128cb6SBen Gardon #define arch_rwlock_is_contended(l)	queued_rwlock_is_contended(l)
14670af2f8aSWaiman Long 
14770af2f8aSWaiman Long #endif /* __ASM_GENERIC_QRWLOCK_H */
148